ai

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package ai provides pi's provider-neutral message and streaming contracts.

Index

Constants

View Source
const (
	ModelsErrorModelSource     = auth.ErrorModelSource
	ModelsErrorModelValidation = auth.ErrorModelValidation
	ModelsErrorProvider        = auth.ErrorProvider
	ModelsErrorStream          = auth.ErrorStream
	ModelsErrorAuth            = auth.ErrorAuth
	ModelsErrorOAuth           = auth.ErrorOAuth
)
View Source
const (
	ImagesAPIOpenRouter      ImagesAPI        = "openrouter-images"
	ImagesProviderOpenRouter ImagesProviderID = "openrouter"

	ImagesStopReasonStop    ImagesStopReason = "stop"
	ImagesStopReasonError   ImagesStopReason = "error"
	ImagesStopReasonAborted ImagesStopReason = "aborted"
)

Variables

View Source
var ErrStreamIncomplete = errors.New("ai: stream ended without a terminal event")

Functions

func CalculateCost

func CalculateCost(model *Model, usage *Usage)

CalculateCost fills usage.Cost from the model's rates, applying tiered pricing above each tier's input-token threshold and Anthropic's 2x base input rate for 1h cache writes.

func CleanupSessionResources

func CleanupSessionResources(sessionID ...string) error

CleanupSessionResources runs registered provider cleanups in registration order. Omitting sessionID releases resources for every session.

func ContentText

func ContentText(content any, separators ...string) string

ContentText extracts text blocks and joins them with a newline by default.

func HasAPI

func HasAPI(model *Model, api API) bool

HasAPI reports whether the model streams through the given API shape.

func IsContextOverflow

func IsContextOverflow(message *AssistantMessage, contextWindow float64) bool

IsContextOverflow recognizes upstream's explicit and silent overflow forms.

func IsRetryableAssistantError

func IsRetryableAssistantError(message *AssistantMessage) bool

IsRetryableAssistantError reports whether a failed assistant message is a transient provider or transport error.

func Marshal

func Marshal(value any) ([]byte, error)

Marshal encodes the ai wire format with JSON.stringify-compatible string escaping. Internal protocol and persistence surfaces must use this instead of encoding/json's HTML-safe default.

func MarshalAssistantMessageEvent

func MarshalAssistantMessageEvent(event AssistantMessageEvent) ([]byte, error)

func MarshalMessage

func MarshalMessage(message Message) ([]byte, error)

func MarshalToolCallArguments

func MarshalToolCallArguments(content *ToolCall) ([]byte, error)

MarshalToolCallArguments preserves the provider's decoded JSON shape and object member order while the public argument map remains convenient for ordinary object-shaped tool calls.

func ModelsAreEqual

func ModelsAreEqual(a, b *Model) bool

ModelsAreEqual compares models by id and provider; nil never equals.

func NormalizeJSONStringifyJSON

func NormalizeJSONStringifyJSON(data []byte) ([]byte, error)

NormalizeJSONStringifyJSON parses JSON with JavaScript Number semantics and re-emits the same value using JSON.stringify's ordering and scalar spelling.

func ParseStreamingJSON

func ParseStreamingJSON(input string) any

ParseStreamingJSON parses potentially incomplete JSON captured while a tool call streams. It never fails: complete input parses strictly (retrying with string repair), partial input yields the values received so far, and empty or unusable input becomes an empty object — matching parseStreamingJson exported from upstream pi-ai's index for custom stream handlers.

func RegisterSessionResourceCleanup

func RegisterSessionResourceCleanup(cleanup SessionResourceCleanup) func()

RegisterSessionResourceCleanup installs a provider cleanup and returns an idempotent function that unregisters it.

func SetAssistantMessageErrorBeforeResponseID

func SetAssistantMessageErrorBeforeResponseID(message *AssistantMessage, enabled bool)

SetAssistantMessageErrorBeforeResponseID preserves the order produced when a streaming backend appends errorMessage before responseId to an existing message.

func SetAssistantMessageErrorBeforeTimestamp

func SetAssistantMessageErrorBeforeTimestamp(message *AssistantMessage, enabled bool)

SetAssistantMessageErrorBeforeTimestamp preserves the member order of upstream message constructors that insert errorMessage before timestamp.

func SetToolCallArgumentsJSON

func SetToolCallArgumentsJSON(content *ToolCall, data []byte) error

SetToolCallArgumentsJSON records a complete provider-emitted argument value so a later replay preserves JSON.stringify's original shape and member order. ToolCall.Arguments remains an object-oriented Go convenience; malformed provider values are retained in the wire representation and exposed through ToolCallArgumentsValue.

func SetUsageOptionalsBeforeTotals

func SetUsageOptionalsBeforeTotals(usage *Usage)

SetUsageOptionalsBeforeTotals preserves the order of upstream usage objects built by compaction aggregation.

func ToolCallArgumentsValue

func ToolCallArgumentsValue(content *ToolCall) any

ToolCallArgumentsValue returns the provider-emitted JSON value. Valid tool calls return the public argument map; malformed non-object values remain observable so schema validation and transforms see the same runtime value as upstream.

func UUIDv7

func UUIDv7() (string, error)

UUIDv7 returns a monotonic UUIDv7.

Types

type API

type API string
const (
	APIUnknown              API = ""
	APIOpenAICompletions    API = "openai-completions"
	APIMistralConversations API = "mistral-conversations"
	APIOpenAIResponses      API = "openai-responses"
	APIAzureOpenAIResponses API = "azure-openai-responses"
	APIOpenAICodexResponses API = "openai-codex-responses"
	APIAnthropicMessages    API = "anthropic-messages"
	APIBedrockConverse      API = "bedrock-converse-stream"
	APIGoogleGenerativeAI   API = "google-generative-ai"
	APIGoogleVertex         API = "google-vertex"
	APIPiMessages           API = "pi-messages"
)

type AnthropicMessagesCompat

type AnthropicMessagesCompat struct {
	SupportsEagerToolInputStreaming *bool `json:"supportsEagerToolInputStreaming,omitempty"`
	SupportsLongCacheRetention      *bool `json:"supportsLongCacheRetention,omitempty"`
	SendSessionAffinityHeaders      *bool `json:"sendSessionAffinityHeaders,omitempty"`
	SupportsCacheControlOnTools     *bool `json:"supportsCacheControlOnTools,omitempty"`
	SupportsTemperature             *bool `json:"supportsTemperature,omitempty"`
	ForceAdaptiveThinking           *bool `json:"forceAdaptiveThinking,omitempty"`
	AllowEmptySignature             *bool `json:"allowEmptySignature,omitempty"`
	SupportsToolReferences          *bool `json:"supportsToolReferences,omitempty"`
}

type AssistantCall added in v0.1.2

type AssistantCall func() (*AssistantMessage, error)

AssistantCall produces one terminal assistant message. Returned errors are terminal because upstream retries message-level provider failures, not rejected calls.

type AssistantContent

type AssistantContent []AssistantContentBlock

func (AssistantContent) MarshalJSON

func (blocks AssistantContent) MarshalJSON() ([]byte, error)

func (*AssistantContent) UnmarshalJSON

func (blocks *AssistantContent) UnmarshalJSON(data []byte) error

type AssistantContentBlock

type AssistantContentBlock interface {
	// contains filtered or unexported methods
}

type AssistantImages

type AssistantImages struct {
	API          ImagesAPI        `json:"api"`
	Provider     ImagesProviderID `json:"provider"`
	Model        string           `json:"model"`
	Output       ImagesContent    `json:"output"`
	ResponseID   *string          `json:"responseId,omitempty"`
	Usage        *Usage           `json:"usage,omitempty"`
	StopReason   ImagesStopReason `json:"stopReason"`
	ErrorMessage *string          `json:"errorMessage,omitempty"`
	Timestamp    int64            `json:"timestamp"`
}

type AssistantMessage

type AssistantMessage struct {
	Content       AssistantContent              `json:"content"`
	API           API                           `json:"api"`
	Provider      ProviderID                    `json:"provider"`
	Model         string                        `json:"model"`
	Usage         Usage                         `json:"usage"`
	StopReason    StopReason                    `json:"stopReason"`
	Timestamp     int64                         `json:"timestamp"`
	ResponseID    *string                       `json:"responseId,omitempty"`
	ResponseModel *string                       `json:"responseModel,omitempty"`
	Diagnostics   *[]AssistantMessageDiagnostic `json:"diagnostics,omitempty"`
	ErrorMessage  *string                       `json:"errorMessage,omitempty"`
	// contains filtered or unexported fields
}

func RetryAssistantCall added in v0.1.2

func RetryAssistantCall(
	ctx context.Context,
	produce AssistantCall,
	policy *RetryPolicy,
	callbacks *RetryCallbacks,
) (*AssistantMessage, error)

RetryAssistantCall runs produce once and retries transient assistant errors according to policy. Cancellation during backoff is returned as a shallow copy of the last response with an aborted stop reason and no error message.

func (AssistantMessage) MarshalJSON

func (message AssistantMessage) MarshalJSON() ([]byte, error)

func (*AssistantMessage) UnmarshalJSON

func (message *AssistantMessage) UnmarshalJSON(data []byte) error

type AssistantMessageDiagnostic

type AssistantMessageDiagnostic struct {
	Type      string               `json:"type"`
	Timestamp int64                `json:"timestamp"`
	Error     *DiagnosticErrorInfo `json:"error,omitempty"`
	Details   json.RawMessage      `json:"details,omitempty"`
}

func (AssistantMessageDiagnostic) MarshalJSON

func (diagnostic AssistantMessageDiagnostic) MarshalJSON() ([]byte, error)

func (*AssistantMessageDiagnostic) UnmarshalJSON

func (diagnostic *AssistantMessageDiagnostic) UnmarshalJSON(data []byte) error

type AssistantMessageEvent

type AssistantMessageEvent interface {
	// contains filtered or unexported methods
}

func UnmarshalAssistantMessageEvent

func UnmarshalAssistantMessageEvent(data []byte) (AssistantMessageEvent, error)

type AssistantMessageEventStream

type AssistantMessageEventStream = iter.Seq2[AssistantMessageEvent, error]

type CacheControlFormat

type CacheControlFormat string
const CacheControlAnthropic CacheControlFormat = "anthropic"

type CacheRetention

type CacheRetention string
const (
	CacheRetentionNone  CacheRetention = "none"
	CacheRetentionShort CacheRetention = "short"
	CacheRetentionLong  CacheRetention = "long"
)

type Context

type Context struct {
	SystemPrompt *string     `json:"systemPrompt,omitempty"`
	Messages     MessageList `json:"messages"`
	Tools        *[]Tool     `json:"tools,omitempty"`
}

type Cost

type Cost struct {
	Input      float64 `json:"input"`
	Output     float64 `json:"output"`
	CacheRead  float64 `json:"cacheRead"`
	CacheWrite float64 `json:"cacheWrite"`
	Total      float64 `json:"total"`
}

type CreateImagesProviderOptions

type CreateImagesProviderOptions struct {
	ID            ImagesProviderID
	Name          string
	Auth          auth.ProviderAuth
	Models        []ImagesModel
	RefreshModels func(context.Context) ([]ImagesModel, error)
	API           ImagesFunction
}

type DeferredToolsMode

type DeferredToolsMode string
const DeferredToolsKimi DeferredToolsMode = "kimi"

type DiagnosticErrorInfo

type DiagnosticErrorInfo struct {
	Name    *string         `json:"name,omitempty"`
	Message string          `json:"message"`
	Stack   *string         `json:"stack,omitempty"`
	Code    json.RawMessage `json:"code,omitempty"`
}

func (DiagnosticErrorInfo) MarshalJSON

func (info DiagnosticErrorInfo) MarshalJSON() ([]byte, error)

func (*DiagnosticErrorInfo) UnmarshalJSON

func (info *DiagnosticErrorInfo) UnmarshalJSON(data []byte) error

type DoneEvent

type DoneEvent struct {
	Reason  StopReason        `json:"reason"`
	Message *AssistantMessage `json:"message"`
}

func (DoneEvent) MarshalJSON

func (event DoneEvent) MarshalJSON() ([]byte, error)

type ErrorEvent

type ErrorEvent struct {
	Reason StopReason        `json:"reason"`
	Error  *AssistantMessage `json:"error"`
}

func (ErrorEvent) MarshalJSON

func (event ErrorEvent) MarshalJSON() ([]byte, error)

type HeadersHook

type HeadersHook func(ctx context.Context, headers ProviderHeaders, model *Model) (ProviderHeaders, error)

type ImageContent

type ImageContent struct {
	Data     string `json:"data"`
	MimeType string `json:"mimeType"`
}

func (ImageContent) MarshalJSON

func (content ImageContent) MarshalJSON() ([]byte, error)

func (*ImageContent) UnmarshalJSON

func (content *ImageContent) UnmarshalJSON(data []byte) error

type ImagesAPI

type ImagesAPI string

type ImagesContent

type ImagesContent []ImagesContentBlock

func (ImagesContent) MarshalJSON

func (blocks ImagesContent) MarshalJSON() ([]byte, error)

func (*ImagesContent) UnmarshalJSON

func (blocks *ImagesContent) UnmarshalJSON(data []byte) error

type ImagesContentBlock

type ImagesContentBlock interface {
	// contains filtered or unexported methods
}

type ImagesContext

type ImagesContext struct {
	Input ImagesContent `json:"input"`
}

type ImagesFunction

type ImagesFunction func(ctx context.Context, request ImagesRequest) (*AssistantImages, error)

type ImagesModel

type ImagesModel struct {
	ID               string                          `json:"id"`
	Name             string                          `json:"name"`
	API              ImagesAPI                       `json:"api"`
	Provider         ImagesProviderID                `json:"provider"`
	BaseURL          string                          `json:"baseUrl"`
	ThinkingLevelMap *map[ModelThinkingLevel]*string `json:"thinkingLevelMap,omitempty"`
	Input            InputModalities                 `json:"input"`
	Output           InputModalities                 `json:"output"`
	Cost             ModelCost                       `json:"cost"`
	Headers          *map[string]string              `json:"headers,omitempty"`
}

type ImagesModelsOptions

type ImagesModelsOptions struct {
	Credentials auth.CredentialStore
	AuthContext auth.AuthContext
}

type ImagesOptions

type ImagesOptions struct {
	APIKey          *string            `json:"apiKey,omitempty"`
	Env             ProviderEnv        `json:"env,omitempty"`
	OnPayload       ImagesPayloadHook  `json:"-"`
	OnResponse      ImagesResponseHook `json:"-"`
	Headers         ProviderHeaders    `json:"headers,omitempty"`
	TimeoutMS       *int64             `json:"timeoutMs,omitempty"`
	MaxRetries      *int               `json:"maxRetries,omitempty"`
	MaxRetryDelayMS *int64             `json:"maxRetryDelayMs,omitempty"`
	Metadata        map[string]any     `json:"metadata,omitempty"`
}

type ImagesPayloadHook

type ImagesPayloadHook func(ctx context.Context, payload any, model *ImagesModel) (replacement any, replace bool, err error)

type ImagesProvider

type ImagesProvider interface {
	ID() ImagesProviderID
	Name() string
	Auth() auth.ProviderAuth
	GetModels() ([]ImagesModel, error)
	GenerateImages(context.Context, ImagesRequest) (*AssistantImages, error)
}

func CreateImagesProvider

func CreateImagesProvider(input CreateImagesProviderOptions) ImagesProvider

type ImagesProviderID

type ImagesProviderID string

type ImagesRequest

type ImagesRequest struct {
	Model   *ImagesModel
	Context ImagesContext
	Options *ImagesOptions
}

type ImagesResponseHook

type ImagesResponseHook func(ctx context.Context, response ProviderResponse, model *ImagesModel) error

type ImagesStopReason

type ImagesStopReason string

type InputModalities

type InputModalities []InputModality

func (InputModalities) MarshalJSON

func (modalities InputModalities) MarshalJSON() ([]byte, error)

type InputModality

type InputModality string
const (
	InputText  InputModality = "text"
	InputImage InputModality = "image"
)

type JSONSchema

type JSONSchema = jsonschema.Schema

JSONSchema is a raw JSON Schema value. It preserves its input bytes when marshaled so extension and MCP schemas can cross the provider boundary without a Go-side rewrite.

func JSONSchemaFrom

func JSONSchemaFrom[T any]() (JSONSchema, error)

JSONSchemaFrom derives an inline JSON Schema from T.

func JSONStringEnumSchema

func JSONStringEnumSchema(values ...string) JSONSchema

JSONStringEnumSchema builds a string enum schema in provider wire order.

type MaxTokensField

type MaxTokensField string
const (
	MaxTokensFieldCompletion MaxTokensField = "max_completion_tokens"
	MaxTokensFieldLegacy     MaxTokensField = "max_tokens"
)

type Message

type Message interface {
	// contains filtered or unexported methods
}

func UnmarshalMessage

func UnmarshalMessage(data []byte) (Message, error)

type MessageList

type MessageList []Message

func (MessageList) MarshalJSON

func (messages MessageList) MarshalJSON() ([]byte, error)

func (*MessageList) UnmarshalJSON

func (messages *MessageList) UnmarshalJSON(data []byte) error

type Model

type Model struct {
	ID               string                          `json:"id"`
	Name             string                          `json:"name"`
	API              API                             `json:"api"`
	Provider         ProviderID                      `json:"provider"`
	BaseURL          string                          `json:"baseUrl"`
	Reasoning        bool                            `json:"reasoning"`
	ThinkingLevelMap *map[ModelThinkingLevel]*string `json:"thinkingLevelMap,omitempty"`
	Input            InputModalities                 `json:"input"`
	Cost             ModelCost                       `json:"cost"`
	ContextWindow    float64                         `json:"contextWindow"`
	MaxTokens        float64                         `json:"maxTokens"`
	Headers          *map[string]string              `json:"headers,omitempty"`
	Compat           json.RawMessage                 `json:"compat,omitempty"`
}

type ModelCost

type ModelCost struct {
	ModelCostRates
	Tiers *[]ModelCostTier `json:"tiers,omitempty"`
}

type ModelCostRates

type ModelCostRates struct {
	Input      float64 `json:"input"`
	Output     float64 `json:"output"`
	CacheRead  float64 `json:"cacheRead"`
	CacheWrite float64 `json:"cacheWrite"`
}

type ModelCostTier

type ModelCostTier struct {
	InputTokensAbove float64 `json:"inputTokensAbove"`
	ModelCostRates
}

type ModelThinkingLevel

type ModelThinkingLevel string
const (
	ModelThinkingOff     ModelThinkingLevel = "off"
	ModelThinkingMinimal ModelThinkingLevel = "minimal"
	ModelThinkingLow     ModelThinkingLevel = "low"
	ModelThinkingMedium  ModelThinkingLevel = "medium"
	ModelThinkingHigh    ModelThinkingLevel = "high"
	ModelThinkingXHigh   ModelThinkingLevel = "xhigh"
	ModelThinkingMax     ModelThinkingLevel = "max"
)

func ClampThinkingLevel

func ClampThinkingLevel(model *Model, requested ModelThinkingLevel) ModelThinkingLevel

ClampThinkingLevel returns the requested level when supported, otherwise the nearest supported level upward first, then downward.

func SupportedThinkingLevels

func SupportedThinkingLevels(model *Model) []ModelThinkingLevel

SupportedThinkingLevels lists the model's usable thinking levels: a mapped null removes a level, and xhigh/max require an explicit mapping. A nil model reports every level (Go seam tolerance).

type ModelsError

type ModelsError = auth.Error

type MutableImagesModels

type MutableImagesModels interface {
	ImagesModels
	SetProvider(ImagesProvider)
	DeleteProvider(ImagesProviderID)
	ClearProviders()
}

func CreateImagesModels

func CreateImagesModels(options ...ImagesModelsOptions) MutableImagesModels

type OpenAICompletionsCompat

type OpenAICompletionsCompat struct {
	SupportsStore                               *bool                  `json:"supportsStore,omitempty"`
	SupportsDeveloperRole                       *bool                  `json:"supportsDeveloperRole,omitempty"`
	SupportsReasoningEffort                     *bool                  `json:"supportsReasoningEffort,omitempty"`
	SupportsUsageInStreaming                    *bool                  `json:"supportsUsageInStreaming,omitempty"`
	MaxTokensField                              *MaxTokensField        `json:"maxTokensField,omitempty"`
	RequiresToolResultName                      *bool                  `json:"requiresToolResultName,omitempty"`
	RequiresAssistantAfterToolResult            *bool                  `json:"requiresAssistantAfterToolResult,omitempty"`
	RequiresThinkingAsText                      *bool                  `json:"requiresThinkingAsText,omitempty"`
	RequiresReasoningContentOnAssistantMessages *bool                  `json:"requiresReasoningContentOnAssistantMessages,omitempty"`
	ThinkingFormat                              *ThinkingFormat        `json:"thinkingFormat,omitempty"`
	ChatTemplateKwargs                          *map[string]any        `json:"chatTemplateKwargs,omitempty"`
	OpenRouterRouting                           *OpenRouterRouting     `json:"openRouterRouting,omitempty"`
	VercelGatewayRouting                        *VercelGatewayRouting  `json:"vercelGatewayRouting,omitempty"`
	ZAIToolStream                               *bool                  `json:"zaiToolStream,omitempty"`
	SupportsStrictMode                          *bool                  `json:"supportsStrictMode,omitempty"`
	CacheControlFormat                          *CacheControlFormat    `json:"cacheControlFormat,omitempty"`
	SendSessionAffinityHeaders                  *bool                  `json:"sendSessionAffinityHeaders,omitempty"`
	DeferredToolsMode                           *DeferredToolsMode     `json:"deferredToolsMode,omitempty"`
	SessionAffinityFormat                       *SessionAffinityFormat `json:"sessionAffinityFormat,omitempty"`
	SupportsLongCacheRetention                  *bool                  `json:"supportsLongCacheRetention,omitempty"`
}

type OpenAIResponsesCompat

type OpenAIResponsesCompat struct {
	SupportsDeveloperRole      *bool                  `json:"supportsDeveloperRole,omitempty"`
	SessionAffinityFormat      *SessionAffinityFormat `json:"sessionAffinityFormat,omitempty"`
	SupportsLongCacheRetention *bool                  `json:"supportsLongCacheRetention,omitempty"`
	SupportsToolSearch         *bool                  `json:"supportsToolSearch,omitempty"`
}

type OpenRouterRouting

type OpenRouterRouting struct {
	AllowFallbacks         *bool           `json:"allow_fallbacks,omitempty"`
	RequireParameters      *bool           `json:"require_parameters,omitempty"`
	DataCollection         *string         `json:"data_collection,omitempty"`
	ZDR                    *bool           `json:"zdr,omitempty"`
	EnforceDistillableText *bool           `json:"enforce_distillable_text,omitempty"`
	Order                  *[]string       `json:"order,omitempty"`
	Only                   *[]string       `json:"only,omitempty"`
	Ignore                 *[]string       `json:"ignore,omitempty"`
	Quantizations          *[]string       `json:"quantizations,omitempty"`
	Sort                   json.RawMessage `json:"sort,omitempty"`
	MaxPrice               json.RawMessage `json:"max_price,omitempty"`
	PreferredMinThroughput json.RawMessage `json:"preferred_min_throughput,omitempty"`
	PreferredMaxLatency    json.RawMessage `json:"preferred_max_latency,omitempty"`
	// contains filtered or unexported fields
}

OpenRouterRouting is passed through to the provider verbatim. Upstream never re-types the compat object, so decoded values carry the raw JSON to preserve unknown keys and member order on the wire; the typed fields only serve Go-side construction.

func (OpenRouterRouting) MarshalJSON

func (routing OpenRouterRouting) MarshalJSON() ([]byte, error)

func (*OpenRouterRouting) UnmarshalJSON

func (routing *OpenRouterRouting) UnmarshalJSON(data []byte) error

type PayloadHook

type PayloadHook func(ctx context.Context, payload any, model *Model) (replacement any, replace bool, err error)

type ProviderEnv

type ProviderEnv map[string]string

type ProviderHeaders

type ProviderHeaders map[string]*string

type ProviderID

type ProviderID string

type ProviderResponse

type ProviderResponse struct {
	Status  int               `json:"status"`
	Headers map[string]string `json:"headers"`
}

type RawAssistantMessageEvent

type RawAssistantMessageEvent struct {
	Raw     json.RawMessage
	Partial *AssistantMessage
}

RawAssistantMessageEvent retains a future event shape emitted by a provider while attaching the partial assistant message expected by stream consumers.

func (RawAssistantMessageEvent) MarshalJSON

func (event RawAssistantMessageEvent) MarshalJSON() ([]byte, error)

type Request

type Request struct {
	Model   *Model
	Context Context
	Options *StreamOptions
}

type ResponseHook

type ResponseHook func(ctx context.Context, response ProviderResponse, model *Model) error

type RetryCallbacks added in v0.1.2

type RetryCallbacks struct {
	OnRetryScheduled    func(attempt, maxAttempts int, delayMS int64, errorMessage string) error
	OnRetryAttemptStart func() error
	OnRetryFinished     func(success bool, attempt int, finalError *string) error
}

RetryCallbacks reports the lifecycle of retries after the initial call. Callback errors stop the retry operation, matching rejected async callbacks upstream.

type RetryPolicy added in v0.1.2

type RetryPolicy struct {
	Enabled     bool  `json:"enabled"`
	MaxRetries  int   `json:"maxRetries"`
	BaseDelayMS int64 `json:"baseDelayMs"`
}

RetryPolicy applies bounded exponential backoff to assistant-producing calls. MaxRetries counts retries after the initial call.

type SessionAffinityFormat

type SessionAffinityFormat string
const (
	SessionAffinityOpenAI          SessionAffinityFormat = "openai"
	SessionAffinityOpenAINoSession SessionAffinityFormat = "openai-nosession"
	SessionAffinityOpenRouter      SessionAffinityFormat = "openrouter"
)

type SessionResourceCleanup

type SessionResourceCleanup func(sessionID string) error

SessionResourceCleanup releases provider resources associated with one session. An empty session ID means release every cached session resource.

type SimpleStreamOptions

type SimpleStreamOptions struct {
	StreamOptions
	Reasoning       *ThinkingLevel   `json:"reasoning,omitempty"`
	ThinkingBudgets *ThinkingBudgets `json:"thinkingBudgets,omitempty"`
}

type StartEvent

type StartEvent struct {
	Partial *AssistantMessage `json:"partial"`
}

func (StartEvent) MarshalJSON

func (event StartEvent) MarshalJSON() ([]byte, error)

type StopReason

type StopReason string
const (
	StopReasonStop    StopReason = "stop"
	StopReasonLength  StopReason = "length"
	StopReasonToolUse StopReason = "toolUse"
	StopReasonError   StopReason = "error"
	StopReasonAborted StopReason = "aborted"
)

type StreamFn

type StreamFn func(ctx context.Context, request Request) (AssistantMessageEventStream, error)

type StreamOptions

type StreamOptions struct {
	Temperature               *float64        `json:"temperature,omitempty"`
	MaxTokens                 *float64        `json:"maxTokens,omitempty"`
	APIKey                    *string         `json:"apiKey,omitempty"`
	Transport                 *Transport      `json:"transport,omitempty"`
	CacheRetention            *CacheRetention `json:"cacheRetention,omitempty"`
	SessionID                 *string         `json:"sessionId,omitempty"`
	OnPayload                 PayloadHook     `json:"-"`
	TransformHeaders          HeadersHook     `json:"-"`
	OnResponse                ResponseHook    `json:"-"`
	Headers                   ProviderHeaders `json:"headers,omitempty"`
	TimeoutMS                 *int64          `json:"timeoutMs,omitempty"`
	WebSocketConnectTimeoutMS *int64          `json:"websocketConnectTimeoutMs,omitempty"`
	MaxRetries                *int            `json:"maxRetries,omitempty"`
	MaxRetryDelayMS           *int64          `json:"maxRetryDelayMs,omitempty"`
	Metadata                  map[string]any  `json:"metadata,omitempty"`
	Env                       ProviderEnv     `json:"env,omitempty"`
}

type TextContent

type TextContent struct {
	Text          string  `json:"text"`
	TextSignature *string `json:"textSignature,omitempty"`
	// Index exists only while Anthropic content blocks are streaming and is cleared at block end.
	Index *int `json:"index,omitempty"`
}

func (TextContent) MarshalJSON

func (content TextContent) MarshalJSON() ([]byte, error)

func (*TextContent) UnmarshalJSON

func (content *TextContent) UnmarshalJSON(data []byte) error

type TextDeltaEvent

type TextDeltaEvent struct {
	ContentIndex int               `json:"contentIndex"`
	Delta        string            `json:"delta"`
	Partial      *AssistantMessage `json:"partial"`
}

func (TextDeltaEvent) MarshalJSON

func (event TextDeltaEvent) MarshalJSON() ([]byte, error)

type TextEndEvent

type TextEndEvent struct {
	ContentIndex     int               `json:"contentIndex"`
	Content          string            `json:"content"`
	ContentSignature *string           `json:"contentSignature,omitempty"`
	Partial          *AssistantMessage `json:"partial"`
}

func (TextEndEvent) MarshalJSON

func (event TextEndEvent) MarshalJSON() ([]byte, error)

type TextSignatureV1

type TextSignatureV1 struct {
	Version int    `json:"v"`
	ID      string `json:"id"`
	Phase   string `json:"phase,omitempty"`
}

type TextStartEvent

type TextStartEvent struct {
	ContentIndex int               `json:"contentIndex"`
	Partial      *AssistantMessage `json:"partial"`
}

func (TextStartEvent) MarshalJSON

func (event TextStartEvent) MarshalJSON() ([]byte, error)

type ThinkingBudgets

type ThinkingBudgets struct {
	Minimal *int `json:"minimal,omitempty"`
	Low     *int `json:"low,omitempty"`
	Medium  *int `json:"medium,omitempty"`
	High    *int `json:"high,omitempty"`
}

type ThinkingContent

type ThinkingContent struct {
	Thinking          string  `json:"thinking"`
	ThinkingSignature *string `json:"thinkingSignature,omitempty"`
	Redacted          *bool   `json:"redacted,omitempty"`
	// Index exists only while Anthropic content blocks are streaming and is cleared at block end.
	Index *int `json:"index,omitempty"`
}

func (ThinkingContent) MarshalJSON

func (content ThinkingContent) MarshalJSON() ([]byte, error)

func (*ThinkingContent) UnmarshalJSON

func (content *ThinkingContent) UnmarshalJSON(data []byte) error

type ThinkingDeltaEvent

type ThinkingDeltaEvent struct {
	ContentIndex int               `json:"contentIndex"`
	Delta        string            `json:"delta"`
	Partial      *AssistantMessage `json:"partial"`
}

func (ThinkingDeltaEvent) MarshalJSON

func (event ThinkingDeltaEvent) MarshalJSON() ([]byte, error)

type ThinkingEndEvent

type ThinkingEndEvent struct {
	ContentIndex     int               `json:"contentIndex"`
	Content          string            `json:"content"`
	ContentSignature *string           `json:"contentSignature,omitempty"`
	Redacted         *bool             `json:"redacted,omitempty"`
	Partial          *AssistantMessage `json:"partial"`
}

func (ThinkingEndEvent) MarshalJSON

func (event ThinkingEndEvent) MarshalJSON() ([]byte, error)

type ThinkingFormat

type ThinkingFormat string
const (
	ThinkingFormatOpenAI           ThinkingFormat = "openai"
	ThinkingFormatOpenRouter       ThinkingFormat = "openrouter"
	ThinkingFormatDeepSeek         ThinkingFormat = "deepseek"
	ThinkingFormatTogether         ThinkingFormat = "together"
	ThinkingFormatZAI              ThinkingFormat = "zai"
	ThinkingFormatQwen             ThinkingFormat = "qwen"
	ThinkingFormatChatTemplate     ThinkingFormat = "chat-template"
	ThinkingFormatQwenChatTemplate ThinkingFormat = "qwen-chat-template"
	ThinkingFormatString           ThinkingFormat = "string-thinking"
	ThinkingFormatAntLing          ThinkingFormat = "ant-ling"
)

type ThinkingLevel

type ThinkingLevel string
const (
	ThinkingMinimal ThinkingLevel = "minimal"
	ThinkingLow     ThinkingLevel = "low"
	ThinkingMedium  ThinkingLevel = "medium"
	ThinkingHigh    ThinkingLevel = "high"
	ThinkingXHigh   ThinkingLevel = "xhigh"
	ThinkingMax     ThinkingLevel = "max"
)

type ThinkingStartEvent

type ThinkingStartEvent struct {
	ContentIndex int               `json:"contentIndex"`
	Partial      *AssistantMessage `json:"partial"`
}

func (ThinkingStartEvent) MarshalJSON

func (event ThinkingStartEvent) MarshalJSON() ([]byte, error)

type Tool

type Tool struct {
	Name        string     `json:"name"`
	Label       string     `json:"label,omitempty"`
	Description string     `json:"description"`
	Parameters  JSONSchema `json:"parameters"`
}

type ToolCall

type ToolCall struct {
	ID               string         `json:"id"`
	Name             string         `json:"name"`
	Arguments        map[string]any `json:"arguments"`
	ThoughtSignature *string        `json:"thoughtSignature,omitempty"`

	// Provider adapters clear streaming scratch fields before a terminal message is persisted.
	PartialJSON *string `json:"partialJson,omitempty"`
	PartialArgs *string `json:"partialArgs,omitempty"`
	StreamIndex *int    `json:"streamIndex,omitempty"`
	// Index exists only while Anthropic content blocks are streaming and is cleared at block end.
	Index *int `json:"index,omitempty"`
	// contains filtered or unexported fields
}

func (ToolCall) MarshalJSON

func (content ToolCall) MarshalJSON() ([]byte, error)

func (*ToolCall) UnmarshalJSON

func (content *ToolCall) UnmarshalJSON(data []byte) error

type ToolCallDeltaEvent

type ToolCallDeltaEvent struct {
	ContentIndex int               `json:"contentIndex"`
	Delta        string            `json:"delta"`
	Partial      *AssistantMessage `json:"partial"`
}

func (ToolCallDeltaEvent) MarshalJSON

func (event ToolCallDeltaEvent) MarshalJSON() ([]byte, error)

type ToolCallEndEvent

type ToolCallEndEvent struct {
	ContentIndex int               `json:"contentIndex"`
	ToolCall     *ToolCall         `json:"toolCall"`
	Partial      *AssistantMessage `json:"partial"`
}

func (ToolCallEndEvent) MarshalJSON

func (event ToolCallEndEvent) MarshalJSON() ([]byte, error)

type ToolCallStartEvent

type ToolCallStartEvent struct {
	ContentIndex int               `json:"contentIndex"`
	ID           string            `json:"id,omitempty"`
	ToolName     string            `json:"toolName,omitempty"`
	Partial      *AssistantMessage `json:"partial"`
}

func (ToolCallStartEvent) MarshalJSON

func (event ToolCallStartEvent) MarshalJSON() ([]byte, error)

type ToolResultContent

type ToolResultContent []ToolResultContentBlock

func (ToolResultContent) MarshalJSON

func (blocks ToolResultContent) MarshalJSON() ([]byte, error)

func (*ToolResultContent) UnmarshalJSON

func (blocks *ToolResultContent) UnmarshalJSON(data []byte) error

type ToolResultContentBlock

type ToolResultContentBlock interface {
	// contains filtered or unexported methods
}

type ToolResultMessage

type ToolResultMessage struct {
	ToolCallID     string            `json:"toolCallId"`
	ToolName       string            `json:"toolName"`
	Content        ToolResultContent `json:"content"`
	Details        json.RawMessage   `json:"details,omitempty"`
	Usage          *Usage            `json:"usage,omitempty"`
	AddedToolNames *[]string         `json:"addedToolNames,omitempty"`
	IsError        bool              `json:"isError"`
	Timestamp      int64             `json:"timestamp"`
}

func (ToolResultMessage) MarshalJSON

func (message ToolResultMessage) MarshalJSON() ([]byte, error)

func (*ToolResultMessage) UnmarshalJSON

func (message *ToolResultMessage) UnmarshalJSON(data []byte) error

type Transport

type Transport string
const (
	TransportSSE             Transport = "sse"
	TransportWebSocket       Transport = "websocket"
	TransportWebSocketCached Transport = "websocket-cached"
	TransportAuto            Transport = "auto"
)

type UnknownContentBlock

type UnknownContentBlock struct {
	Raw json.RawMessage
}

UnknownContentBlock retains a future upstream content shape while this port has not learned its concrete Go type yet.

func (UnknownContentBlock) MarshalJSON

func (content UnknownContentBlock) MarshalJSON() ([]byte, error)

type Usage

type Usage struct {
	Input        int64  `json:"input"`
	Output       int64  `json:"output"`
	CacheRead    int64  `json:"cacheRead"`
	CacheWrite   int64  `json:"cacheWrite"`
	Reasoning    *int64 `json:"reasoning,omitempty"`
	TotalTokens  int64  `json:"totalTokens"`
	Cost         Cost   `json:"cost"`
	CacheWrite1h *int64 `json:"cacheWrite1h,omitempty"`
	// contains filtered or unexported fields
}

func (Usage) MarshalJSON

func (usage Usage) MarshalJSON() ([]byte, error)

func (*Usage) UnmarshalJSON

func (usage *Usage) UnmarshalJSON(data []byte) error

type UserContent

type UserContent struct {
	Text   *string
	Blocks UserContentBlocks
}

func NewUserContent

func NewUserContent(blocks ...UserContentBlock) UserContent

func NewUserText

func NewUserText(text string) UserContent

func (UserContent) MarshalJSON

func (content UserContent) MarshalJSON() ([]byte, error)

func (*UserContent) UnmarshalJSON

func (content *UserContent) UnmarshalJSON(data []byte) error

type UserContentBlock

type UserContentBlock interface {
	// contains filtered or unexported methods
}

type UserContentBlocks

type UserContentBlocks []UserContentBlock

func (UserContentBlocks) MarshalJSON

func (blocks UserContentBlocks) MarshalJSON() ([]byte, error)

func (*UserContentBlocks) UnmarshalJSON

func (blocks *UserContentBlocks) UnmarshalJSON(data []byte) error

type UserMessage

type UserMessage struct {
	Content   UserContent `json:"content"`
	Timestamp int64       `json:"timestamp"`
}

func (UserMessage) MarshalJSON

func (message UserMessage) MarshalJSON() ([]byte, error)

type VercelGatewayRouting

type VercelGatewayRouting struct {
	Only  *[]string `json:"only,omitempty"`
	Order *[]string `json:"order,omitempty"`
}

Directories

Path Synopsis
Package models contains the generated model catalog and its persisted refresh overlay.
Package models contains the generated model catalog and its persisted refresh overlay.
cmd/genmodels command
internal/cataloggen
Package cataloggen normalizes the models.dev api.json shape into pi models.
Package cataloggen normalizes the models.dev api.json shape into pi models.
faux
Package faux provides a scripted, in-memory provider for agent and provider tests.
Package faux provides a scripted, in-memory provider for agent and provider tests.

Jump to

Keyboard shortcuts

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