provider

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 9 Imported by: 0

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

View Source
const (
	TokenEstimateGenericPayload          = contextpolicy.EstimateMethodGenericPayload
	TokenEstimateProviderRenderedPayload = contextpolicy.EstimateMethodProviderRenderedPayload
	TokenEstimateOfficialPreflightCount  = contextpolicy.EstimateMethodOfficialPreflightCount
)
View Source
const (
	ReasoningKindNone         = "none"
	ReasoningKindEffort       = "effort"
	ReasoningKindToggle       = "toggle"
	ReasoningKindBudget       = "budget"
	ReasoningKindToggleBudget = "toggle_budget"
	ReasoningKindEffortBudget = "effort_budget"
	ReasoningKindAlwaysOn     = "always_on"
	ReasoningKindDynamic      = "provider_dynamic"
)

Variables

View Source
var ErrContextOverflow = errors.New("provider context overflow")
View Source
var ErrStreamMissingTerminal = errors.New("provider stream closed without terminal event")
View Source
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 MessagesWithEphemeralUser(messages []session.Message, ephemeral *EphemeralUserMessage) ([]session.Message, error)

func ValidateToolCall

func ValidateToolCall(call ToolCall) error

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 CacheRetentionDefault interface {
	DefaultCacheRetention() cache.Retention
}

type EphemeralUserMessage added in v0.20.0

type EphemeralUserMessage struct {
	Message         session.Message
	HistoryInsertAt int
}

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

func NormalizeFinishReason(raw string, hasToolCalls bool, truncated bool, hasText bool) (FinishReason, bool)

type HostedToolDefinition

type HostedToolDefinition struct {
	Name        string
	Type        string
	Description string
	Parameters  map[string]any
	Options     map[string]any
}

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 HostedToolResultError struct {
	Code    string `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

type HostedToolResultItem

type HostedToolResultItem struct {
	Title    string         `json:"title,omitempty"`
	URL      string         `json:"url,omitempty"`
	Snippet  string         `json:"snippet,omitempty"`
	Source   string         `json:"source,omitempty"`
	Metadata map[string]any `json:"metadata,omitempty"`
}

type PayloadHasher

type PayloadHasher interface {
	PayloadHash(Request) (string, error)
}

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 ReasoningBudget struct {
	MinTokens int64 `json:"min_tokens,omitempty"`
	MaxTokens int64 `json:"max_tokens,omitempty"`
}

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 RequestLabels struct {
	Correlation map[string]string
	Host        map[string]string
}

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 SourceRef added in v0.3.13

type SourceRef struct {
	Title string
	URL   string
}

type State

type State struct {
	Kind       string            `json:"kind,omitempty"`
	ID         string            `json:"id,omitempty"`
	Attributes map[string]string `json:"attributes,omitempty"`
}

func CloneState

func CloneState(in *State) *State

type StreamEvent

type StreamEvent struct {
	Type           EventType
	Text           string
	ToolCallStream ToolCallStream
	ToolCalls      []ToolCall
	ToolCall       ToolCall
	HostedResult   HostedToolResultData
	Sources        []SourceRef
	Reason         string
	Usage          Usage
	ResponseID     string
	ResponseState  *State
	Err            error
}

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 ToolCall

type ToolCall struct {
	ID        string
	Name      string
	Args      string
	Reasoning string
}

type ToolCallStream added in v0.3.16

type ToolCallStream struct {
	ID   string
	Name string
}

type ToolDefinition

type ToolDefinition struct {
	Name         string
	Title        string
	Description  string
	InputSchema  map[string]any
	OutputSchema map[string]any
	Strict       bool
	Annotations  map[string]any
}

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) Add

func (u Usage) Add(other Usage) Usage

func (Usage) Normalized

func (u Usage) Normalized() Usage

type UsageSource

type UsageSource string
const (
	UsageNative      UsageSource = "native"
	UsageEstimated   UsageSource = "estimated"
	UsageMixed       UsageSource = "mixed"
	UsageUnknown     UsageSource = "unknown"
	UsageUnavailable UsageSource = "unavailable"
)

Directories

Path Synopsis
Package cache records provider-visible prompt segments and request ledgers.
Package cache records provider-visible prompt segments and request ledgers.

Jump to

Keyboard shortcuts

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