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 IsTerminalNaturalFinish(reason FinishReason) bool
- func ValidateToolCall(call ToolCall) error
- type CachePolicyNormalizer
- type CacheRetentionDefault
- type EstimateConfidence
- type EventType
- type FinishReason
- type HostedToolDefinition
- type HostedToolResultData
- type HostedToolResultError
- type HostedToolResultItem
- type PayloadHasher
- type Provider
- type Request
- type RequestLabels
- type State
- type StreamEvent
- type StreamValidator
- type TokenEstimate
- type TokenEstimateMethod
- type TokenEstimator
- type ToolCall
- type ToolDefinition
- type Usage
- type UsageSource
Constants ¶
const ( TokenEstimateGenericPayload = contextpolicy.EstimateMethodGenericPayload TokenEstimateProviderRenderedPayload = contextpolicy.EstimateMethodProviderRenderedPayload TokenEstimateOfficialPreflightCount = contextpolicy.EstimateMethodOfficialPreflightCount )
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 IsTerminalNaturalFinish ¶
func IsTerminalNaturalFinish(reason FinishReason) bool
func ValidateToolCall ¶
Types ¶
type CachePolicyNormalizer ¶
type CachePolicyNormalizer interface {
NormalizeCachePolicy(cache.CachePolicy) (cache.CachePolicy, error)
}
type CacheRetentionDefault ¶
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" ToolCalls EventType = "tool_calls" Done EventType = "done" Empty EventType = "empty" Truncated EventType = "truncated" Error EventType = "error" UsageEvent EventType = "usage" 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 Provider ¶
type Provider interface {
Stream(context.Context, Request) (<-chan StreamEvent, error)
}
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
Tools []ToolDefinition
HostedTools []HostedToolDefinition
RawPlan cache.RawPlan
Cache cache.CachePolicy
ContextPolicy contextpolicy.Policy
RequestEstimate contextpolicy.RequestEstimate
ContextPressure contextpolicy.ContextPressure
MaxOutputTokens int64
DisableReasoning bool
PreviousState *State
Labels RequestLabels
}
type RequestLabels ¶ added in v0.3.1
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
}
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 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 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" )