Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultSubagentDefinitions() []subagents.Definition
- func IsPromptPolicyViolation(err error) bool
- func PromptPolicyRefusalMessage() string
- func ResolveProjectRoot() (string, error)
- func WithAutoCompact(config CompactConfig) func(*Options)
- func WithHistoryLimit(n int) func(*Options)
- func WithMaxSessions(n int) func(*Options)
- func WithModelPool(pool map[ModelTier]model.Model) func(*Options)
- func WithOTEL(config OTELConfig) func(*Options)
- func WithSubagentModelMapping(mapping map[string]ModelTier) func(*Options)
- func WithTokenCallback(fn TokenCallback) func(*Options)
- func WithTokenTracking(enabled bool) func(*Options)
- type Attachment
- type CIContext
- type CLIContext
- type CommandExecution
- type CommandRegistration
- type CompactConfig
- type CompactEvent
- type ContentBlock
- type ContextReport
- type ContextTokensReport
- type ContextToolEntry
- type ContextToolsReport
- type Delta
- type EntryPoint
- type HookRecorder
- type MemoryFlushConfig
- type Message
- type ModeContext
- type ModelFactory
- type ModelFactoryFunc
- type ModelStats
- type ModelTier
- type OTELConfig
- type Options
- type PermissionRequest
- type PermissionRequestHandler
- type PlatformContext
- type RealtimeEvent
- type RealtimeEventType
- type Request
- type Response
- type Result
- type RolloutWriter
- type Runtime
- func (rt *Runtime) ClearSession(sessionID string) error
- func (rt *Runtime) Close() error
- func (rt *Runtime) Config() *config.Settings
- func (rt *Runtime) GetContextReport() ContextReport
- func (rt *Runtime) GetHistory(sessionID string) ([]message.Message, error)
- func (rt *Runtime) GetSessionStats(sessionID string) *SessionTokenStats
- func (rt *Runtime) GetTotalStats() *SessionTokenStats
- func (rt *Runtime) Run(ctx context.Context, req Request) (*Response, error)
- func (rt *Runtime) RunStream(ctx context.Context, req Request) (<-chan StreamEvent, error)
- func (rt *Runtime) Sandbox() *sandbox.Manager
- func (rt *Runtime) Settings() *config.Settings
- type SandboxOptions
- type SandboxReport
- type SessionTokenStats
- type SkillExecution
- type SkillRegistration
- type SpanContext
- type StreamEvent
- type SubagentRegistration
- type TokenCallback
- type TokenStats
- type ToolCallSummary
- type Tracer
- type Usage
Constants ¶
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" // EventFinalResponse carries the complete api.Response at stream end. EventFinalResponse = "final_response" EventError = "error" )
Anthropic-compatible SSE event types.
Variables ¶
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") )
var ErrPromptPolicyViolation = errors.New("api: prompt policy violation")
ErrPromptPolicyViolation is the sentinel error for prompt-policy blocks.
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 IsPromptPolicyViolation ¶ added in v0.1.6
IsPromptPolicyViolation reports whether err represents a prompt-policy block.
func PromptPolicyRefusalMessage ¶ added in v0.1.6
func PromptPolicyRefusalMessage() string
PromptPolicyRefusalMessage returns the canonical guard refusal text.
func ResolveProjectRoot ¶
ResolveProjectRoot 智能解析项目根目录。 优先级:环境变量 > 向上查找 go.mod > 当前工作目录 自动处理符号链接(macOS /var -> /private/var)
func WithAutoCompact ¶
func WithAutoCompact(config CompactConfig) func(*Options)
WithAutoCompact configures automatic context compaction.
func WithHistoryLimit ¶
WithHistoryLimit sets the maximum number of user turns to load from persisted history. Older turns are dropped before the context is sent to the model. Values <= 0 disable the limit (all history is loaded).
func WithMaxSessions ¶
WithMaxSessions caps how many parallel session histories are retained. Values <= 0 fall back to the default.
func WithModelPool ¶
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 ¶
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 ¶
WithTokenTracking enables or disables token usage tracking.
Types ¶
type Attachment ¶
type Attachment struct {
Type string // "image" or "file"
FilePath string // Local file path
MimeType string // e.g., "image/jpeg", "image/png"
}
Attachment represents a file attachment (e.g., image) for vision API
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 ¶
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 ContextReport ¶ added in v0.1.5
type ContextReport struct {
SystemChars int `json:"systemChars"`
SkillsChars int `json:"skillsChars"`
Tools ContextToolsReport `json:"tools"`
EstTokens ContextTokensReport `json:"estTokens"`
}
type ContextTokensReport ¶ added in v0.1.5
type ContextToolEntry ¶ added in v0.1.5
type ContextToolsReport ¶ added in v0.1.5
type ContextToolsReport struct {
Count int `json:"count"`
ListChars int `json:"listChars"`
SchemaChars int `json:"schemaChars"`
Entries []ContextToolEntry `json:"entries"`
}
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 MemoryFlushConfig ¶
type MemoryFlushConfig struct {
// Enabled turns memory flush on or off. Default false.
Enabled bool
// ReserveTokensFloor is the number of tokens reserved for model output during the flush turn.
// This is separate from ContextWindowHardMinTokens (which is the hard reject threshold).
// 0 defaults to 20000 (same as openclaw's DEFAULT_PI_COMPACTION_RESERVE_TOKENS_FLOOR).
ReserveTokensFloor int
// SoftThresholdTokens is the additional early-trigger buffer before ReserveTokensFloor.
// 0 defaults to 4000 (same as openclaw).
SoftThresholdTokens int
// Prompt is the synthetic user message injected for the flush turn.
// Empty uses the default openclaw-style prompt.
Prompt string
}
MemoryFlushConfig controls automatic memory flush before context window exhaustion. Flush fires when estimated session tokens >= ContextWindowTokens - ReserveTokensFloor - SoftThresholdTokens. With defaults and a 200k context window this triggers at ~176k tokens (88%), matching openclaw.
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 ¶
ModelFactory allows callers to supply arbitrary model implementations.
type ModelFactoryFunc ¶
ModelFactoryFunc turns a function into a 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.
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
// 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
// PromptGuardEnabled blocks user attempts to request/rewrite/reveal
// system or hidden instructions. nil defaults to true.
PromptGuardEnabled *bool
// PromptGuardModelFactory provides a dedicated classifier model for
// multilingual prompt-exfiltration detection. When nil, input guard falls
// back to fail-open (output guard still applies).
PromptGuardModelFactory ModelFactory
// OutputGuardEnabled redacts assistant outputs that appear to disclose
// system prompt content. nil defaults to true.
OutputGuardEnabled *bool
RulesEnabled *bool // nil = 默认启用,false = 禁用
Middleware []middleware.Middleware
MiddlewareTimeout time.Duration
MaxIterations int
Timeout time.Duration
TokenLimit int
MaxSessions int
// HistoryLimit caps the number of user turns loaded from persisted history
// into the active session context. 0 means no limit (all history loaded).
// Keeping this small reduces token usage and encourages use of memory tools.
HistoryLimit int
// Real-time event callback for progress updates during execution.
// Triggered synchronously every ProgressInterval tool calls.
// Default behavior (if nil): no real-time events, only post-execution events in Response.HookEvents.
RealtimeEventCallback func(event RealtimeEvent)
// ProgressInterval controls how often progress log messages are sent (every N tool calls).
// 0 means use the default (5). Only effective when RealtimeEventCallback is set.
ProgressInterval int
// AutoRecall enables automatic memory injection before each agent turn.
// When true, the runtime searches MEMORY.md + memory/*.md using the user's
// prompt and prepends the top results as <relevant-memories> context.
// This ensures the agent always has relevant past context without needing
// to decide whether to call MemorySearch itself.
AutoRecall bool
// AutoRecallMaxResults controls how many memory snippets are injected.
// 0 means use the default (3).
AutoRecallMaxResults int
Tools []tool.Tool
// 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
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
// ContextWindowTokens is the total context window size in tokens for the configured model.
// When set to > 0, enables the Context Window Guard which warns when approaching the limit
// and emits pressure signals when estimated remaining tokens fall below ContextWindowHardMinTokens.
// Must be configured manually when using self-hosted proxies that don't advertise context size.
// Common values: 200000 (Claude 3.5/3.7 Sonnet), 128000 (GPT-4o), 32000 (smaller models).
ContextWindowTokens int
// ContextWindowWarnRatio is the fraction of ContextWindowTokens at which a warning is emitted.
// 0 or unset defaults to 0.8 (warn when more than 80% of context window is estimated used).
ContextWindowWarnRatio float64
// ContextWindowHardMinTokens is the minimum estimated remaining tokens below which the runtime
// raises an internal pressure signal (no user-facing reset instructions).
// 0 or unset defaults to 2000.
ContextWindowHardMinTokens int
// MemoryFlush controls automatic memory flush before context window exhaustion.
// When enabled, the runtime runs a hidden agent turn to persist important memories
// to disk (memory/YYYY-MM-DD.md) when input tokens approach the configured limit.
// This mirrors openclaw's pre-compaction memory flush behaviour.
// Requires ContextWindowTokens to be set.
MemoryFlush MemoryFlushConfig
// 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
// Logger 自定义日志记录器。如果为 nil,使用默认的控制台 logger。
// 调用方可以注入自己的 logger.Logger 来控制日志输出和格式。
Logger logger.Logger
// 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 RealtimeEvent ¶
type RealtimeEvent struct {
Type RealtimeEventType // Event type
Message string // Human-readable message
Count int // Tool count (for progress) or error count (for error guard)
LastTool string // Name of the most recent tool
Timestamp time.Time // When the event occurred
SessionID string // Optional session identifier
Metadata map[string]any // Optional structured payload
// Recent tool calls with details (for progress updates)
RecentCalls []ToolCallSummary
}
RealtimeEvent represents a real-time event during agent execution
type RealtimeEventType ¶
type RealtimeEventType string
RealtimeEventType defines the type of real-time event during agent execution
const ( RealtimeEventProgressUpdate RealtimeEventType = "progress_update" // Progress report during execution RealtimeEventErrorGuard RealtimeEventType = "error_guard" // Error threshold reached RealtimeEventContextWindowWarn RealtimeEventType = "context_window_warn" // Context window approaching limit RealtimeEventMemoryFlushStart RealtimeEventType = "memory_flush_start" // Automatic memory flush started RealtimeEventMemoryFlushDone RealtimeEventType = "memory_flush_done" // Automatic memory flush completed RealtimeEventMemoryFlushFailed RealtimeEventType = "memory_flush_failed" // Automatic memory flush failed )
type Request ¶
type Request struct {
Prompt string
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
Attachments []Attachment // Optional: images or files for multimodal requests
}
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
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 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 (*Runtime) ClearSession ¶
ClearSession removes all conversation history for the given session. This includes both in-memory history and persisted history on disk. The session will start fresh on the next Run() call.
func (*Runtime) GetContextReport ¶ added in v0.1.5
func (rt *Runtime) GetContextReport() ContextReport
GetContextReport returns an estimated static context breakdown (excluding history).
func (*Runtime) GetHistory ¶
GetHistory returns all stored messages for the given session. It first checks the in-memory store, then falls back to persisted history. Returns nil, nil if no history exists for the session.
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) RunStream ¶
RunStream executes the pipeline asynchronously and returns events over a channel.
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 ToolCallSummary ¶
type ToolCallSummary struct {
Name string // Tool name
Params string // Summarized params (truncated to 100 chars)
Output string // Summarized output (truncated to 200 chars)
}
ToolCallSummary contains summarized info about a tool 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.
Source Files
¶
- agent.go
- claude_embed_hooks.go
- compact.go
- compact_prompt.go
- event_extractors.go
- helpers.go
- history_persistence.go
- hooks_bridge.go
- mcp_bridge.go
- options.go
- otel_config.go
- otel_noop.go
- progress.go
- prompt_guard.go
- request_helpers.go
- rollout.go
- runtime_helpers.go
- runtime_helpers_unix.go
- sandbox_bridge.go
- settings_bridge.go
- stats.go
- stream.go