Documentation
¶
Index ¶
- Constants
- Variables
- func ContentPartSize(part fantasy.MessagePart) int
- func EstimatePromptSize(messages []fantasy.Message) int
- func Run(ctx context.Context, opts RunOptions) error
- func ToolResultSize(r fantasy.ToolResultContent) int
- type CompactionOptions
- type CompactionResult
- type Metrics
- type PendingToolCall
- type PersistedStep
- type ProviderTool
- type RunOptions
Constants ¶
const ( // Label values for Chats. StateStreaming = "streaming" StateWaiting = "waiting" // Label values for CompactionTotal. CompactionResultSuccess = "success" CompactionResultError = "error" CompactionResultTimeout = "timeout" )
Variables ¶
var ( ErrInterrupted = xerrors.New("chat interrupted") ErrDynamicToolCall = xerrors.New("dynamic tool call") // ErrStopAfterTool is returned when a tool listed in // StopAfterTools produces a successful result, indicating // the run should terminate cleanly after persistence. ErrStopAfterTool = xerrors.New("stop after tool") )
Functions ¶
func ContentPartSize ¶ added in v2.33.0
func ContentPartSize(part fantasy.MessagePart) int
ContentPartSize returns the byte length of a MessagePart's primary text or data field.
func EstimatePromptSize ¶ added in v2.33.0
EstimatePromptSize returns a cheap byte-size estimate of a fantasy prompt by summing the text content lengths of all message parts. This avoids JSON marshaling overhead.
func Run ¶
func Run(ctx context.Context, opts RunOptions) error
Run executes the chat step-stream loop and delegates persistence/publishing to callbacks.
func ToolResultSize ¶ added in v2.33.0
func ToolResultSize(r fantasy.ToolResultContent) int
ToolResultSize returns the byte length of a ToolResultContent's primary text or data field.
Types ¶
type CompactionOptions ¶
type CompactionOptions struct {
ThresholdPercent int32
ContextLimit int64
SummaryPrompt string
SystemSummaryPrefix string
Timeout time.Duration
Persist func(context.Context, CompactionResult) error
DebugSvc *chatdebug.Service
ChatID uuid.UUID
HistoryTipMessageID int64
// ToolCallID and ToolName identify the synthetic tool call
// used to represent compaction in the message stream.
ToolCallID string
ToolName string
// PublishMessagePart publishes streaming parts to connected
// clients so they see "Summarizing..." / "Summarized" UI
// transitions during compaction.
PublishMessagePart func(codersdk.ChatMessageRole, codersdk.ChatMessagePart)
OnError func(error)
}
type CompactionResult ¶
type Metrics ¶ added in v2.33.0
type Metrics struct {
Chats *prometheus.GaugeVec
MessageCount *prometheus.HistogramVec
PromptSizeBytes *prometheus.HistogramVec
ToolResultSizeBytes *prometheus.HistogramVec
ToolErrorsTotal *prometheus.CounterVec
TTFTSeconds *prometheus.HistogramVec
CompactionTotal *prometheus.CounterVec
StepsTotal *prometheus.CounterVec
StreamRetriesTotal *prometheus.CounterVec
StreamBufferDroppedTotal prometheus.Counter
}
Metrics holds Prometheus metrics for the chatd subsystem.
func NewMetrics ¶ added in v2.33.0
func NewMetrics(reg prometheus.Registerer) *Metrics
NewMetrics creates a new Metrics instance registered with the given registerer.
func NopMetrics ¶ added in v2.33.0
func NopMetrics() *Metrics
NopMetrics returns a Metrics instance that discards all data. Useful for tests and when metrics collection is not desired.
func (*Metrics) RecordCompaction ¶ added in v2.33.0
RecordCompaction classifies and records a compaction attempt. It is a no-op when m is nil.
func (*Metrics) RecordStreamBufferDropped ¶ added in v2.33.0
func (m *Metrics) RecordStreamBufferDropped()
RecordStreamBufferDropped increments stream_buffer_dropped_total once per dropped event. No-op when m is nil.
func (*Metrics) RecordStreamRetry ¶ added in v2.33.0
func (m *Metrics) RecordStreamRetry(provider, model string, classified chaterror.ClassifiedError)
RecordStreamRetry increments stream_retries_total. The caller must obtain classified via chaterror.Classify (non-empty Kind). No-op when m is nil.
func (*Metrics) RecordToolError ¶ added in v2.33.0
RecordToolError increments tool_errors_total for the given tool. No-op when m is nil.
type PendingToolCall ¶ added in v2.33.0
PendingToolCall describes a tool call that targets a dynamic tool. These calls are not executed by the chatloop; instead they are persisted so the caller can fulfill them externally.
type PersistedStep ¶
type PersistedStep struct {
Content []fantasy.Content
Usage fantasy.Usage
ContextLimit sql.NullInt64
ProviderResponseID string
// Runtime is the wall-clock duration of this step,
// covering LLM streaming, tool execution, and retries.
// Zero indicates the duration was not measured (e.g.
// interrupted steps).
Runtime time.Duration
// PendingDynamicToolCalls lists tool calls that target
// dynamic tools. When non-empty the chatloop exits with
// ErrDynamicToolCall so the caller can execute them
// externally and resume the loop.
PendingDynamicToolCalls []PendingToolCall
// ToolCallCreatedAt maps tool-call IDs to the time
// the model emitted each tool call. Applied by the
// persistence layer to set CreatedAt on persisted
// tool-call ChatMessageParts.
ToolCallCreatedAt map[string]time.Time
// ToolResultCreatedAt maps tool-call IDs to the time
// each tool result was produced (or interrupted).
// Applied by the persistence layer to set CreatedAt
// on persisted tool-result ChatMessageParts.
ToolResultCreatedAt map[string]time.Time
}
PersistedStep contains the full content of a completed or interrupted agent step. Content includes both assistant blocks (text, reasoning, tool calls) and tool result blocks. The persistence layer is responsible for splitting these into separate database messages by role.
type ProviderTool ¶
ProviderTool pairs a provider-native tool definition with an optional local executor. When Runner is nil the tool is fully provider-executed (e.g. web search). When Runner is non-nil the definition is sent to the API but execution is handled locally (e.g. computer use).
type RunOptions ¶
type RunOptions struct {
Model fantasy.LanguageModel
Messages []fantasy.Message
Tools []fantasy.AgentTool
MaxSteps int
// StartupTimeout bounds how long each model attempt may
// spend opening the provider stream and waiting for its
// first stream part before the attempt is canceled and
// retried. Zero uses the production default.
StartupTimeout time.Duration
// Clock creates startup guard timers. In production use a
// real clock; tests can inject quartz.NewMock(t) to make
// startup timeout behavior deterministic.
Clock quartz.Clock
ActiveTools []string
ContextLimitFallback int64
// DynamicToolNames lists tool names that are handled
// externally. When the model invokes one of these tools
// the chatloop persists partial results and exits with
// ErrDynamicToolCall instead of executing the tool.
DynamicToolNames map[string]bool
// StopAfterTools lists tool names that, when they produce a
// successful result, cause the run to stop after persisting
// the current step. This is used for plan turns where
// propose_plan should terminate the run on success.
StopAfterTools map[string]struct{}
// ModelConfig holds per-call LLM parameters (temperature,
// max tokens, etc.) read from the chat model configuration.
ModelConfig codersdk.ChatModelCallConfig
// ProviderOptions are provider-specific call options
// converted from ModelConfig.ProviderOptions. This is a
// separate field because the conversion requires knowledge
// of the provider, which lives in chatd, not chatloop.
ProviderOptions fantasy.ProviderOptions
// ProviderTools are provider-native tools (like web search
// and computer use) whose definitions are passed directly
// to the provider API. When a ProviderTool has a non-nil
// Runner, tool calls are executed locally; otherwise the
// provider handles execution (e.g. web search).
ProviderTools []ProviderTool
PersistStep func(context.Context, PersistedStep) error
PublishMessagePart func(
role codersdk.ChatMessageRole,
part codersdk.ChatMessagePart,
)
// Callers should attach correlation fields (chat_id, owner_id, etc.)
// using Logger.With before passing the logger in.
Logger slog.Logger
Compaction *CompactionOptions
ReloadMessages func(context.Context) ([]fantasy.Message, error)
DisableChainMode func()
// PrepareMessages is called before each LLM step with the
// current message history. If it returns non-nil, the returned
// slice replaces messages for this and all subsequent steps.
// Used to inject system context that becomes available mid-loop
// (e.g. AGENTS.md after create_workspace).
PrepareMessages func([]fantasy.Message) []fantasy.Message
// OnRetry is called before each retry attempt when the LLM
// stream fails with a retryable error. It provides the attempt
// number, raw error, normalized classification, and backoff
// delay so callers can publish status events to connected
// clients. Callers should also clear any buffered stream state
// from the failed attempt in this callback to avoid sending
// duplicated content.
OnRetry chatretry.OnRetryFn
OnInterruptedPersistError func(error)
// Metrics records Prometheus metrics for the chatd subsystem.
// When nil, no metrics are recorded.
Metrics *Metrics
// BuiltinToolNames lists tool names that are built into chatd.
BuiltinToolNames map[string]bool
}
RunOptions configures a single streaming chat loop run.