api

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EventMessageStart marks the beginning of a new message envelope.
	EventMessageStart = "message_start"
	// EventContentBlockStart indicates that a content block (text/tool use) begins streaming.
	EventContentBlockStart = "content_block_start"
	// EventContentBlockDelta carries incremental text or tool input deltas.
	EventContentBlockDelta = "content_block_delta"
	// EventContentBlockStop signals that the content block transmission is finished.
	EventContentBlockStop = "content_block_stop"
	// EventMessageDelta represents aggregated message-level deltas such as usage updates.
	EventMessageDelta = "message_delta"
	// EventMessageStop denotes completion of the message, including final stop reasons.
	EventMessageStop = "message_stop"
	// EventPing is a keepalive heartbeat used by Anthropic streams.
	EventPing = "ping"

	// Agent-specific extension events.
	EventAgentStart          = "agent_start"
	EventAgentStop           = "agent_stop"
	EventIterationStart      = "iteration_start"
	EventIterationStop       = "iteration_stop"
	EventToolExecutionStart  = "tool_execution_start"
	EventToolExecutionOutput = "tool_execution_output"
	EventToolExecutionResult = "tool_execution_result"
	EventError               = "error"
)

Anthropic-compatible SSE event types.

Variables

View Source
var (
	ErrMissingModel            = errors.New("api: model factory is required")
	ErrConcurrentExecution     = errors.New("concurrent execution on same session is not allowed")
	ErrRuntimeClosed           = errors.New("api: runtime is closed")
	ErrToolUseDenied           = errors.New("api: tool use denied by hook")
	ErrToolUseRequiresApproval = errors.New("api: tool use requires approval")
)

Functions

func DefaultSubagentDefinitions

func DefaultSubagentDefinitions() []subagents.Definition

DefaultSubagentDefinitions exposes the built-in subagent type catalog so callers can seed api.Options.Subagents or extend the metadata when wiring custom handlers.

func ResolveProjectRoot

func ResolveProjectRoot() (string, error)

ResolveProjectRoot 智能解析项目根目录。 优先级:环境变量 > 向上查找 go.mod > 当前工作目录 自动处理符号链接(macOS /var -> /private/var)

func WithAutoCompact

func WithAutoCompact(config CompactConfig) func(*Options)

WithAutoCompact configures automatic context compaction.

func WithMaxSessions

func WithMaxSessions(n int) func(*Options)

WithMaxSessions caps how many parallel session histories are retained. Values <= 0 fall back to the default.

func WithModelPool

func WithModelPool(pool map[ModelTier]model.Model) func(*Options)

WithModelPool configures a pool of models indexed by tier.

func WithOTEL

func WithOTEL(config OTELConfig) func(*Options)

WithOTEL configures OpenTelemetry distributed tracing. Requires build tag 'otel' for actual instrumentation; otherwise no-op.

func WithSubagentModelMapping

func WithSubagentModelMapping(mapping map[string]ModelTier) func(*Options)

WithSubagentModelMapping configures subagent-type-to-tier mappings for model selection. Keys should be lowercase subagent type names (e.g., "explore", "plan").

func WithTokenCallback

func WithTokenCallback(fn TokenCallback) func(*Options)

WithTokenCallback sets a callback function that is called synchronously after each model call with the token usage statistics. Automatically enables TokenTracking.

func WithTokenTracking

func WithTokenTracking(enabled bool) func(*Options)

WithTokenTracking enables or disables token usage tracking.

Types

type CIContext

type CIContext struct {
	Provider string
	Pipeline string
	RunID    string
	SHA      string
	Ref      string
	Matrix   map[string]string
	Metadata map[string]string
}

CIContext captures CI/CD metadata for parameter matrix validation.

type CLIContext

type CLIContext struct {
	User      string
	Workspace string
	Args      []string
	Flags     map[string]string
}

CLIContext captures optional metadata supplied by the CLI surface.

type CommandExecution

type CommandExecution struct {
	Definition commands.Definition
	Result     commands.Result
	Err        error
}

CommandExecution records slash command invocations.

type CommandRegistration

type CommandRegistration struct {
	Definition commands.Definition
	Handler    commands.Handler
}

CommandRegistration wires slash command definitions + handlers.

type CompactConfig

type CompactConfig struct {
	Enabled       bool    `json:"enabled"`
	Threshold     float64 `json:"threshold"`      // trigger ratio (default 0.8)
	PreserveCount int     `json:"preserve_count"` // keep latest N messages (default 5)
	SummaryModel  string  `json:"summary_model"`  // model tier/name used for summary

	PreserveInitial  bool `json:"preserve_initial"`   // keep initial messages when compacting
	InitialCount     int  `json:"initial_count"`      // keep first N messages from the compacted prefix
	PreserveUserText bool `json:"preserve_user_text"` // keep recent user messages from the compacted prefix
	UserTextTokens   int  `json:"user_text_tokens"`   // token budget for preserved user messages

	MaxRetries    int           `json:"max_retries"`
	RetryDelay    time.Duration `json:"retry_delay"`
	FallbackModel string        `json:"fallback_model"`

	// RolloutDir enables compact event persistence when non-empty.
	// The directory is resolved relative to Options.ProjectRoot unless absolute.
	RolloutDir string `json:"rollout_dir"`
}

CompactConfig controls automatic context compaction.

type CompactEvent

type CompactEvent struct {
	SessionID             string    `json:"session_id"`
	Timestamp             time.Time `json:"timestamp"`
	Summary               string    `json:"summary"`
	OriginalMessages      int       `json:"original_messages"`
	PreservedMessages     int       `json:"preserved_messages"`
	EstimatedTokensBefore int       `json:"estimated_tokens_before"`
	EstimatedTokensAfter  int       `json:"estimated_tokens_after"`
}

type ContentBlock

type ContentBlock struct {
	Type  string          `json:"type,omitempty"`  // Type is "text" or "tool_use".
	Text  string          `json:"text,omitempty"`  // Text contains streamed text when Type == "text".
	ID    string          `json:"id,omitempty"`    // ID uniquely names the content block.
	Name  string          `json:"name,omitempty"`  // Name describes the tool/function when Type == "tool_use".
	Input json.RawMessage `json:"input,omitempty"` // Input stores raw JSON arguments supplied to the tool.
}

ContentBlock carries either text segments or tool invocation details.

type Delta

type Delta struct {
	Type        string          `json:"type,omitempty"`         // Type is "text_delta" or "input_json_delta".
	Text        string          `json:"text,omitempty"`         // Text contains the appended text fragment when Type == "text_delta".
	PartialJSON json.RawMessage `json:"partial_json,omitempty"` // PartialJSON carries incremental JSON payload slices for tools.
	StopReason  string          `json:"stop_reason,omitempty"`  // StopReason explains why a message terminated.
}

Delta models incremental updates for content blocks or message-level stop data.

type EntryPoint

type EntryPoint string
const (
	EntryPointCLI      EntryPoint = "cli"
	EntryPointCI       EntryPoint = "ci"
	EntryPointPlatform EntryPoint = "platform"
)

type HookRecorder

type HookRecorder interface {
	Record(coreevents.Event)
	Drain() []coreevents.Event
}

HookRecorder mirrors the historical api hook recorder contract.

type Message

type Message struct {
	ID    string `json:"id,omitempty"`    // ID uniquely identifies the message on the Anthropic service.
	Type  string `json:"type,omitempty"`  // Type is typically "message".
	Role  string `json:"role,omitempty"`  // Role is "user", "assistant", etc.
	Model string `json:"model,omitempty"` // Model states which Anthropic model generated the content.
	Usage *Usage `json:"usage,omitempty"` // Usage accumulates total tokens for the message lifecycle.
}

Message represents the Anthropic message envelope streamed over SSE.

type ModeContext

type ModeContext struct {
	EntryPoint EntryPoint
	CLI        *CLIContext
	CI         *CIContext
	Platform   *PlatformContext
}

ModeContext binds an entrypoint to optional contextual metadata blocks.

type ModelFactory

type ModelFactory interface {
	Model(ctx context.Context) (model.Model, error)
}

ModelFactory allows callers to supply arbitrary model implementations.

type ModelFactoryFunc

type ModelFactoryFunc func(context.Context) (model.Model, error)

ModelFactoryFunc turns a function into a ModelFactory.

func (ModelFactoryFunc) Model

func (fn ModelFactoryFunc) Model(ctx context.Context) (model.Model, error)

Model implements ModelFactory.

type ModelStats

type ModelStats struct {
	InputTokens   int64 `json:"input_tokens"`
	OutputTokens  int64 `json:"output_tokens"`
	TotalTokens   int64 `json:"total_tokens"`
	CacheCreation int64 `json:"cache_creation_input_tokens,omitempty"`
	CacheRead     int64 `json:"cache_read_input_tokens,omitempty"`
	RequestCount  int   `json:"request_count"`
}

ModelStats aggregates token usage for a specific model.

type ModelTier

type ModelTier string

ModelTier represents cost-based model classification for optimization.

const (
	ModelTierLow  ModelTier = "low"  // Low cost: Haiku
	ModelTierMid  ModelTier = "mid"  // Mid cost: Sonnet
	ModelTierHigh ModelTier = "high" // High cost: Opus
)

type OTELConfig

type OTELConfig struct {
	// Enabled activates OpenTelemetry tracing.
	Enabled bool `json:"enabled"`

	// ServiceName identifies this service in traces. Defaults to "agentsdk-go".
	ServiceName string `json:"service_name,omitempty"`

	// Endpoint is the OTLP endpoint URL (e.g., "http://localhost:4318").
	Endpoint string `json:"endpoint,omitempty"`

	// Headers are additional HTTP headers sent with OTLP requests.
	Headers map[string]string `json:"headers,omitempty"`

	// SampleRate controls the sampling ratio (0.0-1.0). Defaults to 1.0 (100%).
	SampleRate float64 `json:"sample_rate,omitempty"`

	// Insecure allows non-TLS connections to the endpoint.
	Insecure bool `json:"insecure,omitempty"`
}

OTELConfig configures OpenTelemetry integration for the SDK. When Enabled is true, spans are created for agent runs, model calls, and tool executions. OTEL dependencies are optional and only loaded when using build tag 'otel'.

func DefaultOTELConfig

func DefaultOTELConfig() OTELConfig

DefaultOTELConfig returns sensible defaults for OTEL configuration.

type Options

type Options struct {
	EntryPoint  EntryPoint
	Mode        ModeContext
	ProjectRoot string
	// ConfigRoot is the directory containing declarative runtime files.
	// Defaults to "<ProjectRoot>/.claude" when unset.
	ConfigRoot string
	// EmbedFS 可选的嵌入文件系统,用于支持将 .claude 目录打包到二进制
	// 当设置时,文件加载优先级为:OS 文件系统 > 嵌入 FS
	// 这允许运行时通过创建本地文件来覆盖嵌入的默认配置
	EmbedFS           fs.FS
	SettingsPath      string
	SettingsOverrides *config.Settings
	SettingsLoader    *config.SettingsLoader

	Model        model.Model
	ModelFactory ModelFactory

	// ModelPool maps tiers to model instances for cost optimization.
	// Use ModelTier constants (ModelTierLow, ModelTierMid, ModelTierHigh) as keys.
	ModelPool map[ModelTier]model.Model
	// SubagentModelMapping maps subagent type names to model tiers.
	// Keys should be lowercase subagent types: "general-purpose", "explore", "plan".
	// Subagents not in this map use the default Model.
	SubagentModelMapping map[string]ModelTier

	// DefaultEnableCache sets the default prompt caching behavior for all requests.
	// Individual requests can override this via Request.EnablePromptCache.
	// Prompt caching reduces costs for repeated context (system prompts, conversation history).
	DefaultEnableCache bool

	SystemPrompt string
	RulesEnabled *bool // nil = 默认启用,false = 禁用

	Middleware        []middleware.Middleware
	MiddlewareTimeout time.Duration
	MaxIterations     int
	Timeout           time.Duration
	TokenLimit        int
	MaxSessions       int

	Tools []tool.Tool

	// TaskStore overrides the default in-memory task store used by task_* built-ins.
	// When nil, runtime creates and owns a fresh in-memory store.
	// When provided, ownership remains with the caller.
	TaskStore tasks.Store

	// EnabledBuiltinTools controls which built-in tools are registered when Options.Tools is empty.
	// - nil (default): register all built-ins to preserve current behaviour
	// - empty slice: disable all built-in tools
	// - non-empty: enable only the listed built-ins (case-insensitive).
	// If Tools is non-empty, this whitelist is ignored in favour of the legacy Tools override.
	// Available built-in names include: bash, file_read, file_write, grep, glob.
	EnabledBuiltinTools []string

	// DisallowedTools is a blacklist of tool names (case-insensitive) that will not be registered.
	DisallowedTools []string

	// CustomTools appends caller-supplied tool.Tool implementations to the selected built-ins
	// when Tools is empty. Ignored when Tools is non-empty (legacy override takes priority).
	CustomTools []tool.Tool
	MCPServers  []string

	TypedHooks     []corehooks.ShellHook
	HookMiddleware []coremw.Middleware
	HookTimeout    time.Duration

	Skills    []SkillRegistration
	Commands  []CommandRegistration
	Subagents []SubagentRegistration
	// SkillsDirs overrides skill discovery roots. Paths may be absolute, or
	// relative to ProjectRoot. When empty, defaults to "<ConfigRoot>/skills".
	SkillsDirs []string
	// SkillsRecursive controls recursive SKILL.md discovery. Nil defaults to true.
	SkillsRecursive *bool

	Sandbox SandboxOptions

	// TokenTracking enables token usage statistics collection.
	// When true, the runtime tracks input/output tokens per session and model.
	TokenTracking bool
	// TokenCallback is called synchronously after token usage is recorded.
	// Only called when TokenTracking is true. The callback should be lightweight
	// and non-blocking to avoid delaying the agent execution. If you need async
	// processing, spawn a goroutine inside the callback.
	TokenCallback TokenCallback

	// PermissionRequestHandler handles sandbox PermissionAsk decisions. Returning
	// PermissionAllow continues tool execution; PermissionDeny rejects it; PermissionAsk
	// leaves the request pending.
	PermissionRequestHandler PermissionRequestHandler
	// ApprovalQueue optionally persists permission decisions and supports session whitelists.
	ApprovalQueue *security.ApprovalQueue
	// ApprovalApprover labels approvals/denials stored in ApprovalQueue.
	ApprovalApprover string
	// ApprovalWhitelistTTL controls session whitelist duration for approvals.
	ApprovalWhitelistTTL time.Duration
	// ApprovalWait blocks tool execution until a pending approval is resolved.
	ApprovalWait bool

	// AutoCompact enables automatic context compaction for long sessions.
	AutoCompact CompactConfig

	// OTEL configures OpenTelemetry distributed tracing.
	// Requires build tag 'otel' for actual instrumentation; otherwise no-op.
	OTEL OTELConfig
	// contains filtered or unexported fields
}

Options configures the unified SDK runtime.

type PermissionRequest

type PermissionRequest struct {
	ToolName   string
	ToolParams map[string]any
	SessionID  string
	Rule       string
	Target     string
	Reason     string
	Approval   *security.ApprovalRecord
}

PermissionRequest captures a permission prompt for sandbox "ask" matches.

type PermissionRequestHandler

type PermissionRequestHandler func(context.Context, PermissionRequest) (coreevents.PermissionDecisionType, error)

PermissionRequestHandler lets hosts synchronously allow/deny PermissionAsk decisions.

type PlatformContext

type PlatformContext struct {
	Organization string
	Project      string
	Environment  string
	Labels       map[string]string
}

PlatformContext captures enterprise platform metadata such as org/project.

type Request

type Request struct {
	Prompt            string
	ContentBlocks     []model.ContentBlock // Multimodal content; when non-empty, used alongside Prompt
	Mode              ModeContext
	SessionID         string
	RequestID         string    `json:"request_id,omitempty"` // Auto-generated UUID or user-provided
	Model             ModelTier // Optional: override model tier for this request
	EnablePromptCache *bool     // Optional: enable prompt caching (nil uses global default)
	Traits            []string
	Tags              map[string]string
	Channels          []string
	Metadata          map[string]any
	TargetSubagent    string
	ToolWhitelist     []string
	ForceSkills       []string
}

Request captures a single logical run invocation. Tags/T traits/Channels are forwarded to the declarative runtime layers (skills/subagents) while RunContext overrides the agent-level execution knobs.

type Response

type Response struct {
	Mode           ModeContext
	RequestID      string `json:"request_id,omitempty"` // UUID for distributed tracing
	Result         *Result
	SkillResults   []SkillExecution
	CommandResults []CommandExecution
	Subagent       *subagents.Result
	HookEvents     []coreevents.Event
	// Deprecated: Use Settings instead. Kept for backward compatibility.
	ProjectConfig   *config.Settings
	Settings        *config.Settings
	SandboxSnapshot SandboxReport
	Tags            map[string]string
}

Response aggregates the final agent result together with metadata emitted by the unified runtime pipeline (skills/commands/hooks/etc.).

type Result

type Result struct {
	Output     string
	StopReason string
	Usage      model.Usage
	ToolCalls  []model.ToolCall
}

Result represents the agent execution result.

type RolloutWriter

type RolloutWriter struct {
	// contains filtered or unexported fields
}

func (*RolloutWriter) WriteCompactEvent

func (w *RolloutWriter) WriteCompactEvent(sessionID string, res compactResult) error

type Runtime

type Runtime struct {
	// contains filtered or unexported fields
}

Runtime exposes the unified SDK surface that powers CLI/CI/enterprise entrypoints.

func New

func New(ctx context.Context, opts Options) (*Runtime, error)

New instantiates a unified runtime bound to the provided options.

func (*Runtime) Close

func (rt *Runtime) Close() error

Close releases held resources.

func (*Runtime) Config

func (rt *Runtime) Config() *config.Settings

Config returns the last loaded project config.

func (*Runtime) GetSessionStats

func (rt *Runtime) GetSessionStats(sessionID string) *SessionTokenStats

GetSessionStats returns aggregated token stats for a session.

func (*Runtime) GetTotalStats

func (rt *Runtime) GetTotalStats() *SessionTokenStats

GetTotalStats returns aggregated token stats across all sessions.

func (*Runtime) Run

func (rt *Runtime) Run(ctx context.Context, req Request) (*Response, error)

Run executes the unified pipeline synchronously.

func (*Runtime) RunStream

func (rt *Runtime) RunStream(ctx context.Context, req Request) (<-chan StreamEvent, error)

RunStream executes the pipeline asynchronously and returns events over a channel.

func (*Runtime) Sandbox

func (rt *Runtime) Sandbox() *sandbox.Manager

Sandbox exposes the sandbox manager.

func (*Runtime) Settings

func (rt *Runtime) Settings() *config.Settings

Settings exposes the merged settings.json snapshot for callers that need it.

type SandboxOptions

type SandboxOptions struct {
	Root          string
	AllowedPaths  []string
	NetworkAllow  []string
	ResourceLimit sandbox.ResourceLimits
}

SandboxOptions mirrors sandbox.Manager construction knobs exposed at the API layer so callers can customise filesystem/network/resource guards without touching lower-level packages.

type SandboxReport

type SandboxReport struct {
	Roots          []string
	AllowedPaths   []string
	AllowedDomains []string
	ResourceLimits sandbox.ResourceLimits
}

SandboxReport documents the sandbox configuration attached to the runtime.

type SessionTokenStats

type SessionTokenStats struct {
	SessionID    string                 `json:"session_id"`
	TotalInput   int64                  `json:"total_input"`
	TotalOutput  int64                  `json:"total_output"`
	TotalTokens  int64                  `json:"total_tokens"`
	CacheCreated int64                  `json:"cache_created,omitempty"`
	CacheRead    int64                  `json:"cache_read,omitempty"`
	ByModel      map[string]*ModelStats `json:"by_model,omitempty"`
	RequestCount int                    `json:"request_count"`
	FirstRequest time.Time              `json:"first_request"`
	LastRequest  time.Time              `json:"last_request"`
}

SessionTokenStats aggregates token usage across all requests in a session.

type SkillExecution

type SkillExecution struct {
	Definition skills.Definition
	Result     skills.Result
	Err        error
}

SkillExecution records individual skill invocations.

type SkillRegistration

type SkillRegistration struct {
	Definition skills.Definition
	Handler    skills.Handler
}

SkillRegistration wires runtime skill definitions + handlers.

type SpanContext

type SpanContext interface {
	// TraceID returns the trace identifier.
	TraceID() string

	// SpanID returns the span identifier.
	SpanID() string

	// IsRecording returns true if the span is being recorded.
	IsRecording() bool
}

SpanContext carries span identification for propagation.

type StreamEvent

type StreamEvent struct {
	Type string `json:"type"` // Type identifies the concrete SSE event kind.

	// Anthropic-compatible payloads.
	Message      *Message      `json:"message,omitempty"`       // Message holds the envelope for message_* events.
	Index        *int          `json:"index,omitempty"`         // Index orders content blocks/deltas within the message.
	ContentBlock *ContentBlock `json:"content_block,omitempty"` // ContentBlock contains partial or full block data.
	Delta        *Delta        `json:"delta,omitempty"`         // Delta carries incremental text/tool updates.
	Usage        *Usage        `json:"usage,omitempty"`         // Usage records token consumption snapshots.

	// Agent-specific extensions.
	ToolUseID string      `json:"tool_use_id,omitempty"`      // ToolUseID associates tool events with tool execution records.
	Name      string      `json:"name,omitempty"`             // Name describes the agent, tool, or block responsible for the event.
	Output    interface{} `json:"output,omitempty"`           // Output captures arbitrary structured payloads (e.g., tool stdout).
	IsStderr  *bool       `json:"is_stderr,omitempty"`        // IsStderr marks whether the output originated from stderr (not necessarily an error).
	IsError   *bool       `json:"is_error,omitempty"`         // IsError flags a genuine execution failure surfaced by the runtime/toolchain.
	SessionID string      `json:"session_id,omitempty"`       // SessionID ties events to a long-lived agent session.
	Iteration *int        `json:"iteration,omitempty"`        // Iteration indicates the current agent iteration, if applicable.
	TotalIter *int        `json:"total_iterations,omitempty"` // TotalIter reports the planned maximum iteration count.
}

StreamEvent represents a single SSE dispatch compatible with Anthropic's schema while carrying additional metadata needed by the agent runtime.

type SubagentRegistration

type SubagentRegistration struct {
	Definition subagents.Definition
	Handler    subagents.Handler
}

SubagentRegistration wires runtime subagents into the dispatcher.

type TokenCallback

type TokenCallback func(stats TokenStats)

TokenCallback is called synchronously after token usage is recorded. The callback should be lightweight and non-blocking to avoid delaying the agent execution. If you need async processing, spawn a goroutine inside the callback.

type TokenStats

type TokenStats struct {
	InputTokens   int64     `json:"input_tokens"`
	OutputTokens  int64     `json:"output_tokens"`
	TotalTokens   int64     `json:"total_tokens"`
	CacheCreation int64     `json:"cache_creation_input_tokens,omitempty"`
	CacheRead     int64     `json:"cache_read_input_tokens,omitempty"`
	Model         string    `json:"model"`
	SessionID     string    `json:"session_id"`
	RequestID     string    `json:"request_id"`
	Timestamp     time.Time `json:"timestamp"`
}

TokenStats captures token usage for a single model call.

type Tracer

type Tracer interface {
	// StartAgentSpan creates a span for an agent run.
	StartAgentSpan(sessionID, requestID string, iteration int) SpanContext

	// StartModelSpan creates a child span for model generation.
	StartModelSpan(parent SpanContext, modelName string) SpanContext

	// StartToolSpan creates a child span for tool execution.
	StartToolSpan(parent SpanContext, toolName string) SpanContext

	// EndSpan completes a span with optional attributes.
	EndSpan(span SpanContext, attrs map[string]any, err error)

	// Shutdown gracefully closes the tracer and flushes pending spans.
	Shutdown() error
}

Tracer provides the interface for distributed tracing operations. Implementations are swapped based on build tags (otel vs noop).

func NewTracer

func NewTracer(_ OTELConfig) (Tracer, error)

NewTracer creates a tracer. Without the otel build tag, returns a noop tracer.

type Usage

type Usage struct {
	InputTokens  int `json:"input_tokens,omitempty"`  // InputTokens counts tokens provided in the prompt.
	OutputTokens int `json:"output_tokens,omitempty"` // OutputTokens counts tokens produced by the model/toolchain.
}

Usage records token accounting snapshots compatible with Anthropic payloads.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL