Documentation
¶
Overview ¶
Package provider defines the normalized model streaming contract.
Provider implementations must make Stream safe for concurrent calls. Each returned stream must emit zero or more non-terminal events followed by exactly one terminal event: Done, Empty, or Truncated. Streams must not emit events after the terminal event, must close promptly when the context is cancelled, and must return complete unique tool call IDs. Hosted tool result events must match a prior hosted tool call in the same stream. Unknown event types should be treated as provider contract failures.
StreamValidator captures these invariants for adapters and engine tests.
Index ¶
- Constants
- Variables
- func EphemeralUserMessageIndex(messages []session.Message, ephemeral *EphemeralUserMessage) (int, error)
- func IsTerminalNaturalFinish(reason FinishReason) bool
- func MessagesWithEphemeralUser(messages []session.Message, ephemeral *EphemeralUserMessage) ([]session.Message, error)
- func ValidateToolCall(call ToolCall) error
- func ValidateToolCallStream(call ToolCallStream) error
- type CachePolicyNormalizer
- type CacheRetentionDefault
- type EphemeralUserMessage
- type EstimateConfidence
- type EventType
- type FinishReason
- type HostedToolDefinition
- type HostedToolResultData
- type HostedToolResultError
- type HostedToolResultItem
- type PayloadHasher
- type PreparedInputTokenLimit
- type PreparedRequest
- type Provider
- type ReasoningBudget
- type ReasoningCapability
- type ReasoningLevel
- type ReasoningSelection
- type Request
- type RequestLabels
- type RequestPreparer
- type SourceRef
- type State
- type StreamEvent
- type StreamValidator
- type TokenEstimate
- type TokenEstimateCoverage
- type TokenEstimateMethod
- type TokenEstimator
- type ToolCall
- type ToolCallStream
- type ToolDefinition
- type Usage
- type UsageSource
Constants ¶
const ( TokenEstimateGenericPayload = contextpolicy.EstimateMethodGenericPayload TokenEstimateProviderRenderedPayload = contextpolicy.EstimateMethodProviderRenderedPayload TokenEstimateOfficialPreflightCount = contextpolicy.EstimateMethodOfficialPreflightCount )
const ( ReasoningKindNone = "none" ReasoningKindEffort = "effort" ReasoningKindToggle = "toggle" ReasoningKindBudget = "budget" ReasoningKindToggleBudget = "toggle_budget" ReasoningKindEffortBudget = "effort_budget" ReasoningKindAlwaysOn = "always_on" ReasoningKindDynamic = "provider_dynamic" )
Variables ¶
var ErrContextOverflow = errors.New("provider context overflow")
var ErrStreamMissingTerminal = errors.New("provider stream closed without terminal event")
var ErrStreamNotClosedAfterTerminal = errors.New("provider stream did not close after terminal event")
Functions ¶
func EphemeralUserMessageIndex ¶ added in v0.20.0
func EphemeralUserMessageIndex(messages []session.Message, ephemeral *EphemeralUserMessage) (int, error)
func IsTerminalNaturalFinish ¶
func IsTerminalNaturalFinish(reason FinishReason) bool
func MessagesWithEphemeralUser ¶ added in v0.20.0
func ValidateToolCall ¶
func ValidateToolCallStream ¶ added in v0.3.16
func ValidateToolCallStream(call ToolCallStream) error
Types ¶
type CachePolicyNormalizer ¶
type CachePolicyNormalizer interface {
NormalizeCachePolicy(cache.CachePolicy) (cache.CachePolicy, error)
}
type CacheRetentionDefault ¶
type EphemeralUserMessage ¶ added in v0.20.0
EphemeralUserMessage is inserted into one provider request in memory. Its index is relative to canonical non-system history and it has no durable ID.
type EstimateConfidence ¶
type EstimateConfidence string
const ( EstimateExact EstimateConfidence = "exact" EstimateApproximate EstimateConfidence = "approximate" EstimateConservative EstimateConfidence = "conservative" )
type EventType ¶
type EventType string
const ( Delta EventType = "delta" Reasoning EventType = "reasoning" ToolCallStart EventType = "tool_call_start" ToolCallDelta EventType = "tool_call_delta" ToolCallEnd EventType = "tool_call_end" ToolCalls EventType = "tool_calls" Done EventType = "done" Empty EventType = "empty" Truncated EventType = "truncated" Error EventType = "error" UsageEvent EventType = "usage" SourcesEvent EventType = "sources" HostedToolCall EventType = "hosted_tool_call" HostedToolResult EventType = "hosted_tool_result" )
type FinishReason ¶
type FinishReason string
const ( FinishUnknown FinishReason = "unknown" FinishStop FinishReason = "stop" FinishToolCalls FinishReason = "tool_calls" FinishLength FinishReason = "length" FinishContentFilter FinishReason = "content_filter" FinishError FinishReason = "error" FinishCancelled FinishReason = "cancelled" )
func NormalizeFinishReason ¶
type HostedToolDefinition ¶
type HostedToolResultData ¶
type HostedToolResultData struct {
Text string `json:"text,omitempty"`
Results []HostedToolResultItem `json:"results,omitempty"`
Error *HostedToolResultError `json:"error,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
func (HostedToolResultData) IsZero ¶
func (r HostedToolResultData) IsZero() bool
func (HostedToolResultData) SummaryText ¶
func (r HostedToolResultData) SummaryText() string
type HostedToolResultError ¶
type HostedToolResultItem ¶
type PayloadHasher ¶
type PreparedInputTokenLimit ¶ added in v0.25.0
type PreparedInputTokenLimit interface {
EnforceInputTokenLimit() bool
}
PreparedInputTokenLimit marks prepared requests whose estimate is complete enough to reject the request before provider transmission.
type PreparedRequest ¶ added in v0.25.0
type PreparedRequest interface {
Stream(context.Context) (<-chan StreamEvent, error)
TokenEstimate() TokenEstimate
PayloadFingerprint() string
Close() error
}
PreparedRequest is an in-memory, single-use rendering of one exact provider request. Close discards an unconsumed request or releases a consumed request and must be idempotent.
type Provider ¶
type Provider interface {
Stream(context.Context, Request) (<-chan StreamEvent, error)
}
type ReasoningBudget ¶ added in v0.3.19
type ReasoningCapability ¶ added in v0.3.19
type ReasoningCapability struct {
Kind string `json:"kind,omitempty"`
SupportedLevels []ReasoningLevel `json:"supported_levels,omitempty"`
DefaultLevel ReasoningLevel `json:"default_level,omitempty"`
DisableSupported bool `json:"disable_supported,omitempty"`
DefaultEnabled *bool `json:"default_enabled,omitempty"`
Budget ReasoningBudget `json:"budget,omitempty"`
DynamicProviderMetadata bool `json:"dynamic_provider_metadata,omitempty"`
WireShape string `json:"wire_shape,omitempty"`
DisableShape string `json:"disable_shape,omitempty"`
BudgetShape string `json:"budget_shape,omitempty"`
ResponseReasoningFields []string `json:"response_reasoning_fields,omitempty"`
HistoryReplayRequirements []string `json:"history_replay_requirements,omitempty"`
SourceURLs []string `json:"source_urls,omitempty"`
SourceCheckedAt string `json:"source_checked_at,omitempty"`
Fixture string `json:"fixture,omitempty"`
}
func (ReasoningCapability) SupportsBudget ¶ added in v0.3.19
func (c ReasoningCapability) SupportsBudget() bool
func (ReasoningCapability) SupportsLevel ¶ added in v0.3.19
func (c ReasoningCapability) SupportsLevel(level ReasoningLevel) bool
func (ReasoningCapability) ValidateSelection ¶ added in v0.3.19
func (c ReasoningCapability) ValidateSelection(selection ReasoningSelection) error
type ReasoningLevel ¶ added in v0.3.19
type ReasoningLevel string
const ( ReasoningLevelDefault ReasoningLevel = "default" ReasoningLevelOff ReasoningLevel = "off" ReasoningLevelMinimal ReasoningLevel = "minimal" ReasoningLevelLow ReasoningLevel = "low" ReasoningLevelMedium ReasoningLevel = "medium" ReasoningLevelHigh ReasoningLevel = "high" ReasoningLevelXHigh ReasoningLevel = "xhigh" ReasoningLevelMax ReasoningLevel = "max" )
func NormalizeReasoningLevel ¶ added in v0.3.19
func NormalizeReasoningLevel(level ReasoningLevel) ReasoningLevel
type ReasoningSelection ¶ added in v0.3.19
type ReasoningSelection struct {
Level ReasoningLevel `json:"level,omitempty"`
BudgetTokens int64 `json:"budget_tokens,omitempty"`
}
func NormalizeReasoningSelection ¶ added in v0.3.19
func NormalizeReasoningSelection(s ReasoningSelection) ReasoningSelection
func ShortRequestReasoningSelection ¶ added in v0.3.19
func ShortRequestReasoningSelection(capability ReasoningCapability) ReasoningSelection
func (ReasoningSelection) IsZero ¶ added in v0.3.19
func (s ReasoningSelection) IsZero() bool
type Request ¶
type Request struct {
RunID string
ThreadID string
TurnID string
PromptScopeID string
TraceID string
Step int
LogicalRequestID string
Attempt int
OverflowRetried bool
Provider string
Model string
Messages []session.Message
EphemeralUser *EphemeralUserMessage
Tools []ToolDefinition
HostedTools []HostedToolDefinition
RawPlan cache.RawPlan
Cache cache.CachePolicy
ContextPolicy contextpolicy.Policy
RequestEstimate contextpolicy.RequestEstimate
ContextPressure contextpolicy.ContextPressure
MaxOutputTokens int64
Reasoning ReasoningSelection
PreviousState *State
Labels RequestLabels
Prepared PreparedRequest `json:"-"`
}
type RequestLabels ¶ added in v0.3.1
type RequestPreparer ¶ added in v0.25.0
type RequestPreparer interface {
PrepareRequest(context.Context, Request) (PreparedRequest, error)
}
RequestPreparer lets a provider render and estimate the exact request that will be streamed. Native providers need not implement this interface.
type State ¶
type State struct {
Kind string `json:"kind,omitempty"`
ID string `json:"id,omitempty"`
Attributes map[string]string `json:"attributes,omitempty"`
}
func CloneState ¶
type StreamEvent ¶
type StreamValidator ¶
type StreamValidator struct {
// contains filtered or unexported fields
}
StreamValidator checks provider stream invariants that the engine relies on. Providers may emit delta, reasoning, usage, tool call, and hosted tool events before exactly one terminal event. They must not emit unknown event types, duplicate tool call IDs, incomplete tool calls, hosted results without prior hosted calls, or any event after a terminal event.
func (*StreamValidator) Finish ¶
func (v *StreamValidator) Finish() error
func (*StreamValidator) Observe ¶
func (v *StreamValidator) Observe(ev StreamEvent) error
func (*StreamValidator) TerminalSeen ¶
func (v *StreamValidator) TerminalSeen() bool
type TokenEstimate ¶
type TokenEstimate struct {
PrefixTokens int64
MessageTokens int64
ToolDefinitionTokens int64
EstimatedInputTokens int64
Source string
Method TokenEstimateMethod
Confidence EstimateConfidence
Coverage TokenEstimateCoverage
}
TokenEstimate is a preflight request estimate. It is not provider usage and does not imply an official provider token-count API unless Method says so.
func GenericRequestEstimate ¶
func GenericRequestEstimate(req Request) (TokenEstimate, error)
type TokenEstimateCoverage ¶ added in v0.25.0
type TokenEstimateCoverage string
const TokenEstimateCoverageComplete TokenEstimateCoverage = "complete_request"
type TokenEstimateMethod ¶
type TokenEstimateMethod = contextpolicy.EstimateMethod
type TokenEstimator ¶
type TokenEstimator interface {
EstimateTokens(context.Context, Request) (TokenEstimate, error)
}
TokenEstimator lets an adapter estimate the fully rendered request before it is sent. Implementations should report the calculation Method explicitly.
type ToolCallStream ¶ added in v0.3.16
type ToolDefinition ¶
type Usage ¶
type Usage struct {
InputTokens int64 `json:"input_tokens,omitempty"`
OutputTokens int64 `json:"output_tokens,omitempty"`
ReasoningTokens int64 `json:"reasoning_tokens,omitempty"`
CacheReadTokens int64 `json:"cache_read_tokens,omitempty"`
CacheWriteTokens int64 `json:"cache_write_tokens,omitempty"`
TotalTokens int64 `json:"total_tokens,omitempty"`
CostUSD float64 `json:"cost_usd,omitempty"`
Source UsageSource `json:"source,omitempty"`
Available bool `json:"available,omitempty"`
WindowInputTokens int64 `json:"window_input_tokens,omitempty"`
}
func (Usage) Normalized ¶
type UsageSource ¶
type UsageSource string
const ( UsageNative UsageSource = "native" UsageEstimated UsageSource = "estimated" UsageMixed UsageSource = "mixed" UsageUnknown UsageSource = "unknown" )