provider

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Overview

Package provider integrates model providers and normalizes their request flows.

Index

Constants

View Source
const (
	OpenAIPromptCacheRetentionInMemory = "in_memory"
	OpenAIPromptCacheRetention24h      = "24h"
)
View Source
const (
	AnthropicThinkingBlockTypeThinking         = "thinking"
	AnthropicThinkingBlockTypeRedactedThinking = "redacted_thinking"
)
View Source
const (
	ReasoningVariantMinimal = "minimal"
	ReasoningVariantNone    = "none"
	ReasoningVariantLow     = "low"
	ReasoningVariantMedium  = "medium"
	ReasoningVariantHigh    = "high"
	ReasoningVariantXHigh   = "xhigh"
	ReasoningVariantMax     = "max"
)

Variables

View Source
var (
	ErrEmbeddingModelRequired            = errors.New("embedding model is required")
	ErrEmbeddingInputsRequired           = errors.New("embedding inputs are required")
	ErrEmbeddingProviderNotConfigured    = errors.New("embedding provider is not configured")
	ErrEmbeddingVectorCountMismatch      = errors.New("embedding vector count mismatch")
	ErrEmbeddingVectorDimensionsMismatch = errors.New("embedding vector dimensions mismatch")
)
View Source
var (
	ErrGitHubCopilotOAuthStoreRequired       = errors.New("github copilot oauth store is required")
	ErrGitHubCopilotOAuthCredentialsRequired = errors.New("github copilot oauth access or refresh token is required")
	ErrGitHubCopilotNotAvailable             = errors.New("github copilot is not available for this GitHub account")
)
View Source
var (
	ErrModelProviderIDRequired = errors.New("model provider_id is required")
	ErrModelIDRequired         = errors.New("model model_id is required")
	ErrModelRefFormat          = errors.New("model ref must be in provider/model format")
)
View Source
var (
	ErrOpenAIAuthRequired             = errors.New("openai api key or oauth auth is required")
	ErrOpenAIOAuthStoreRequired       = errors.New("openai oauth store is required")
	ErrOpenAIOAuthCredentialsRequired = errors.New("openai oauth access or refresh token is required")
)
View Source
var (
	ErrOpenAICompatibleAPIKeyRequired  = errors.New("openai compatible api key is required")
	ErrOpenAICompatibleBaseURLRequired = errors.New("openai compatible base_url is required")
	ErrGitHubCopilotTokenRequired      = errors.New("github copilot token is required")
)
View Source
var (
	ErrProviderRouteIDRequired   = errors.New("provider route id is required")
	ErrProviderRouteClientNeeded = errors.New("provider route client is required")
	ErrProviderNotConfigured     = errors.New("provider not configured")
)
View Source
var ErrAnthropicAPIKeyRequired = errors.New("anthropic api key is required")
View Source
var ErrAnthropicMaxTokensExceeded = errors.New("anthropic response stopped at max_tokens")
View Source
var ErrAnthropicPauseTurnUnsupported = errors.New("anthropic pause_turn is not supported by this runtime")
View Source
var ErrGoogleAPIKeyRequired = errors.New("google api key is required")
View Source
var ErrOpenAIPromptCacheRetentionInvalid = errors.New("openai prompt_cache_retention must be empty, in_memory, or 24h")
View Source
var ErrProviderInputLimitExceeded = errors.New("provider input limit exceeded")

Functions

func BuildOpenAIOAuthAuthorizeURL

func BuildOpenAIOAuthAuthorizeURL(redirectURI, state, codeChallenge, originator string) (string, error)

func BuiltinPromptFS

func BuiltinPromptFS() embed.FS

func CanonicalProviderID

func CanonicalProviderID(providerID string) string

func ComposeInstructions

func ComposeInstructions(baseInstructions, providerID, modelID string) string

func DefaultAnthropicBaseURL

func DefaultAnthropicBaseURL() string

func DefaultGoogleBaseURL

func DefaultGoogleBaseURL() string

func DefaultOpenAIBaseURL

func DefaultOpenAIBaseURL() string

func DefaultOpenAIOAuthBaseURL

func DefaultOpenAIOAuthBaseURL() string

func EffectiveMaxOutputTokens

func EffectiveMaxOutputTokens(req Request) int

func EffectiveReasoningVariantForTurn

func EffectiveReasoningVariantForTurn(model ModelRef, allowedTools []string, requested string) string

func EffectiveThinkingEnabledForTurn

func EffectiveThinkingEnabledForTurn(model ModelRef, allowedTools []string, requested bool) bool

func EstimateInputTokens

func EstimateInputTokens(input Input) int

EstimateInputTokens returns a rough token estimate for a provider input.

func EstimateRequestTokens

func EstimateRequestTokens(req Request) int

EstimateRequestTokens returns a rough token estimate for a provider request, including instructions, tool schemas, and turn inputs. This mirrors the fallback budgeting model used in koda when accurate provider-side token counting is unavailable.

func EstimateTextTokens

func EstimateTextTokens(text string) int

EstimateTextTokens returns a rough token estimate for UTF-8 text. It treats ASCII text at roughly 4 chars/token and non-ASCII runes at roughly 1 token each to avoid undercounting mixed or multibyte content too aggressively.

func IsInputLimitExceeded

func IsInputLimitExceeded(err error) bool

func JoinPromptSections

func JoinPromptSections(parts ...string) string

JoinPromptSections renders the provider-facing prompt text from stable and dynamic prompt sections. When split sections are present, they are the canonical prompt authority.

func LooksLikeAuthProviderResponse

func LooksLikeAuthProviderResponse(statusCode int, message string) bool

func NormalizeOpenAIBaseURL

func NormalizeOpenAIBaseURL(baseURL string) string

func NormalizeReasoningVariant

func NormalizeReasoningVariant(value string) (string, bool)

func OpenAIOAuthCodeChallenge

func OpenAIOAuthCodeChallenge(codeVerifier string) string

func PromptText

func PromptText(req Request) string

PromptText returns the canonical prompt text for a request. Split prompt sections win when present so every provider sees the same effective prompt.

func ReasoningVariantNames

func ReasoningVariantNames() []string

func RegisterProviderAlias

func RegisterProviderAlias(providerID, canonical string)

Experiemental Delete before Release RegisterProviderAlias lets local experimental providers declare that they should inherit the canonical behavior of a shipped provider family.

func RequiresOpenAIReasoningContentReplay

func RequiresOpenAIReasoningContentReplay(req Request) bool

func SuggestedMaxOutputTokens

func SuggestedMaxOutputTokens(ref ModelRef) int

SuggestedMaxOutputTokens returns a provider/model-family limit when the runtime does not have fresher catalog metadata for the selected model.

func SupportedReasoningVariantsForTurn

func SupportedReasoningVariantsForTurn(model ModelRef, allowedTools []string) []string

func SupportsCustomTools

func SupportsCustomTools(model ModelRef) bool

func SupportsReasoningVariants

func SupportsReasoningVariants(model ModelRef) bool

func SupportsReasoningVariantsForTurn

func SupportsReasoningVariantsForTurn(model ModelRef, allowedTools []string) bool

func SupportsThinkingOutputForTurn

func SupportsThinkingOutputForTurn(model ModelRef, allowedTools []string) bool

func SupportsThinkingOutputFromCatalog

func SupportsThinkingOutputFromCatalog(model ModelRef, reasoning bool) bool

func SystemPrompt

func SystemPrompt(providerID, modelID string) string

SystemPrompt returns a provider/model-specific prompt supplement.

Resolution order:

  1. Per-model: ~/.config/kodacode/prompts/{providerID}/{modelID}.md|.txt
  2. Per-provider: ~/.config/kodacode/prompts/{providerID}.md|.txt
  3. Canonical override: ~/.config/kodacode/prompts/openai.md, deepseek.md, etc.
  4. Built-in embedded prompt

func ThinkingOutputSupportForTurn

func ThinkingOutputSupportForTurn(model ModelRef, allowedTools []string) (bool, bool)

func ValidateOpenAIPromptCacheRetention

func ValidateOpenAIPromptCacheRetention(value string) error

Types

type AnthropicClient

type AnthropicClient struct {
	// contains filtered or unexported fields
}

func NewAnthropicClient

func NewAnthropicClient(config AnthropicConfig) (*AnthropicClient, error)

func (*AnthropicClient) CountTokens

func (c *AnthropicClient) CountTokens(ctx context.Context, req Request) (int, TokenCountSource, error)

func (*AnthropicClient) Stream

func (c *AnthropicClient) Stream(ctx context.Context, req Request) (Stream, error)

type AnthropicConfig

type AnthropicConfig struct {
	APIKey     string
	BaseURL    string
	HTTPClient *http.Client
}

type AnthropicThinkingBlock

type AnthropicThinkingBlock struct {
	Type      string
	Thinking  string
	Signature string
	Data      string
}

func (*AnthropicThinkingBlock) Validate

func (b *AnthropicThinkingBlock) Validate() error

type Attachment

type Attachment struct {
	Name     string
	MIMEType string
	DataURL  string
}

func (Attachment) Validate

func (a Attachment) Validate() error

type AuthEntry

type AuthEntry struct {
	Type          AuthType
	Access        string
	Refresh       string
	Expires       int64
	Project       string
	AccountID     string
	ClientVersion string
}

func ExchangeOpenAIOAuthCode

func ExchangeOpenAIOAuthCode(
	ctx context.Context,
	httpClient *http.Client,
	tokenURL string,
	request OpenAIOAuthCodeExchangeRequest,
) (*AuthEntry, error)

func PollGitHubCopilotDeviceCode

func PollGitHubCopilotDeviceCode(
	ctx context.Context,
	httpClient *http.Client,
	challenge GitHubCopilotDeviceCode,
	accessTokenURL string,
	tokenURL string,
) (*AuthEntry, error)

type AuthStore

type AuthStore struct {
	// contains filtered or unexported fields
}

func NewAuthStore

func NewAuthStore() *AuthStore

func NewAuthStoreAt

func NewAuthStoreAt(path string) *AuthStore

func (*AuthStore) Get

func (s *AuthStore) Get(providerID string) *AuthEntry

func (*AuthStore) Path

func (s *AuthStore) Path() string

func (*AuthStore) Remove

func (s *AuthStore) Remove(providerID string) error

func (*AuthStore) Set

func (s *AuthStore) Set(providerID string, entry AuthEntry) error

type AuthType

type AuthType string
const (
	AuthTypeAPI   AuthType = "api"
	AuthTypeOAuth AuthType = "oauth"
)

type CatalogModel

type CatalogModel struct {
	ID                         string
	Name                       string
	Family                     string
	ContextSize                int
	MaxInputTokens             int
	MaxOutputTokens            int
	Reasoning                  bool
	ReasoningKnown             bool
	SupportedReasoningVariants []string
	SupportsThinkingOutput     bool
	ToolCalls                  bool
	ToolCallsKnown             bool
	Vision                     bool
	VisionKnown                bool
	CostInput                  float64
	CostInputKnown             bool
	CostOutput                 float64
	CostOutputKnown            bool
	CostCacheRead              float64
	CostCacheReadKnown         bool
	CostCacheWrite             float64
	CostCacheWriteKnown        bool
	InputModalities            []string
	OutputModalities           []string
	SupportedEndpoints         []string
}

func NormalizeCatalogModelCapabilities

func NormalizeCatalogModelCapabilities(providerID string, model CatalogModel) CatalogModel

func (CatalogModel) Capacity

func (m CatalogModel) Capacity() ModelCapacity

type Client

type Client interface {
	Stream(context.Context, Request) (Stream, error)
}

func NewPromptingClient

func NewPromptingClient(next Client) Client

type ConversationToolCallIDAliases

type ConversationToolCallIDAliases struct {
	// contains filtered or unexported fields
}

ConversationToolCallIDAliases resolves between runtime tool call ids and the provider-safe request-local aliases used in an outbound conversation.

func BuildConversationToolCallIDAliases

func BuildConversationToolCallIDAliases(req Request) ConversationToolCallIDAliases

BuildConversationToolCallIDAliases returns the request-local alias mapping that normalizeConversationToolCallIDs applies to tool call/result inputs.

func (ConversationToolCallIDAliases) OriginalToProvider

func (a ConversationToolCallIDAliases) OriginalToProvider(original string) (string, bool)

func (ConversationToolCallIDAliases) ProviderToOriginal

func (a ConversationToolCallIDAliases) ProviderToOriginal(providerID string) (string, bool)

type Embedder

type Embedder interface {
	Embed(context.Context, EmbeddingRequest) ([][]float32, error)
}

type EmbeddingRequest

type EmbeddingRequest struct {
	Model      ModelRef
	Inputs     []string
	Dimensions int
}

func (EmbeddingRequest) Validate

func (r EmbeddingRequest) Validate() error

type Event

type Event struct {
	Kind                   EventKind
	AssistantDelta         string
	ReasoningDelta         string
	ReasoningSegmentID     string
	AnthropicThinking      *AnthropicThinkingBlock
	GoogleThoughtSignature []byte
	OpenAIReasoningItem    json.RawMessage
	ToolCallID             string
	ToolName               string
	ToolKind               ToolKind
	InputDelta             string
}

func (Event) Validate

func (e Event) Validate() error

type EventKind

type EventKind string
const (
	EventKindAssistantDelta             EventKind = "assistant_delta"
	EventKindReasoningDelta             EventKind = "reasoning_delta"
	EventKindAnthropicThinkingCommitted EventKind = "anthropic_thinking_committed"
	EventKindOpenAIReasoningCommitted   EventKind = "openai_reasoning_committed"
	EventKindToolCallDelta              EventKind = "tool_call_delta"
	EventKindToolCallDone               EventKind = "tool_call_done"
)

type FinishReason

type FinishReason string
const (
	FinishReasonStop          FinishReason = "stop"
	FinishReasonToolCalls     FinishReason = "tool_calls"
	FinishReasonLength        FinishReason = "length"
	FinishReasonContentFilter FinishReason = "content_filter"
	FinishReasonError         FinishReason = "error"
	FinishReasonUnknown       FinishReason = "unknown"
)

func NormalizeFinishReason

func NormalizeFinishReason(reason FinishReason) FinishReason

func StreamFinishReason

func StreamFinishReason(stream Stream) FinishReason

type GitHubCopilotConfig

type GitHubCopilotConfig struct {
	Token      string
	BaseURL    string
	OAuth      *GitHubCopilotOAuthConfig
	HTTPClient *http.Client
}

type GitHubCopilotDeviceCode

type GitHubCopilotDeviceCode struct {
	DeviceCode      string
	UserCode        string
	VerificationURL string
	ExpiresIn       int
	Interval        int
}

func RequestGitHubCopilotDeviceCode

func RequestGitHubCopilotDeviceCode(
	ctx context.Context,
	httpClient *http.Client,
	deviceCodeURL string,
) (*GitHubCopilotDeviceCode, error)

type GitHubCopilotModel

type GitHubCopilotModel struct {
	ID                 string
	Name               string
	Family             string
	ModelPickerEnabled bool
	SupportedEndpoints []string
	ContextSize        int
	MaxInputTokens     int
	MaxOutputTokens    int
	ToolCalls          bool
	Vision             bool
	Reasoning          bool
}

func FetchGitHubCopilotModels

func FetchGitHubCopilotModels(ctx context.Context, config GitHubCopilotConfig) ([]GitHubCopilotModel, error)

type GitHubCopilotOAuthConfig

type GitHubCopilotOAuthConfig struct {
	Entry          AuthEntry
	Store          *AuthStore
	DeviceCodeURL  string
	AccessTokenURL string
	TokenURL       string
}

type GoogleClient

type GoogleClient struct {
	// contains filtered or unexported fields
}

func NewGoogleClient

func NewGoogleClient(ctx context.Context, config GoogleConfig) (*GoogleClient, error)

func (*GoogleClient) CountTokens

func (c *GoogleClient) CountTokens(ctx context.Context, req Request) (int, TokenCountSource, error)

func (*GoogleClient) Stream

func (c *GoogleClient) Stream(ctx context.Context, req Request) (Stream, error)

type GoogleConfig

type GoogleConfig struct {
	APIKey     string
	BaseURL    string
	HTTPClient *http.Client
}

type Input

type Input struct {
	Kind                   InputKind
	Content                string
	Attachments            []Attachment
	AnthropicThinking      *AnthropicThinkingBlock
	GoogleThoughtSignature []byte
	OpenAIReasoningContent string
	OpenAIReasoningItem    json.RawMessage
	CallID                 string
	ToolName               string
	ToolKind               ToolKind
	Arguments              string
	Output                 string
	Error                  string
	RetryOfCallID          string
	ReusedFromCallID       string
	ReusedFromSessionID    string
	ReusedFromTurnID       string
}

func (Input) Validate

func (i Input) Validate() error

type InputKind

type InputKind string
const (
	InputKindUserMessage       InputKind = "user_message"
	InputKindAssistantMessage  InputKind = "assistant_message"
	InputKindAnthropicThinking InputKind = "anthropic_thinking"
	InputKindOpenAIReasoning   InputKind = "openai_reasoning"
	InputKindToolCall          InputKind = "tool_call"
	InputKindToolResult        InputKind = "tool_result"
)

type LocalModelCatalogProvider

type LocalModelCatalogProvider struct {
	ID      string
	Name    string
	BaseURL string
}

type ModelCapacity

type ModelCapacity struct {
	InputTokens  int
	WindowTokens int
	OutputTokens int
}

func NormalizeModelCapacity

func NormalizeModelCapacity(windowTokens, inputTokens, outputTokens int) ModelCapacity

func (ModelCapacity) HasDistinctWindow

func (c ModelCapacity) HasDistinctWindow() bool

type ModelCatalog

type ModelCatalog struct {
	// contains filtered or unexported fields
}

func NewModelCatalog

func NewModelCatalog(config ModelCatalogConfig) *ModelCatalog

func (*ModelCatalog) EnsureFresh

func (c *ModelCatalog) EnsureFresh(ctx context.Context) error

func (*ModelCatalog) Init

func (c *ModelCatalog) Init(ctx context.Context)

func (*ModelCatalog) ModelsForProvider

func (c *ModelCatalog) ModelsForProvider(providerID string) []CatalogModel

func (*ModelCatalog) ProviderName

func (c *ModelCatalog) ProviderName(providerID string) string

func (*ModelCatalog) Refresh

func (c *ModelCatalog) Refresh(ctx context.Context) error

type ModelCatalogConfig

type ModelCatalogConfig struct {
	CacheFile       string
	ExpiryDays      int
	OpenAIOAuth     bool
	OpenAIAPIKey    bool
	RemoteProviders []RemoteModelCatalogProvider
	LocalProviders  []LocalModelCatalogProvider
	ReportError     func(message string, err error)
}

type ModelRef

type ModelRef struct {
	ProviderID string
	ModelID    string
}

func ParseModelRef

func ParseModelRef(value string) (ModelRef, error)

func (ModelRef) String

func (m ModelRef) String() string

func (ModelRef) Validate

func (m ModelRef) Validate() error

type ModelRoute

type ModelRoute struct {
	Primary ModelRef
}

func (ModelRoute) Candidates

func (r ModelRoute) Candidates() []ModelRef

func (ModelRoute) Validate

func (r ModelRoute) Validate() error

type OpenAIBackend

type OpenAIBackend string
const (
	OpenAIBackendPlatformAPI  OpenAIBackend = "platform_api"
	OpenAIBackendChatGPTCodex OpenAIBackend = "chatgpt_codex"
)

type OpenAIClient

type OpenAIClient struct {
	// contains filtered or unexported fields
}

func NewOpenAIClient

func NewOpenAIClient(config OpenAIConfig) (*OpenAIClient, error)

func (*OpenAIClient) CountTokens

func (c *OpenAIClient) CountTokens(ctx context.Context, req Request) (int, TokenCountSource, error)

func (*OpenAIClient) Stream

func (c *OpenAIClient) Stream(ctx context.Context, req Request) (Stream, error)

type OpenAICompatibleClient

type OpenAICompatibleClient struct {
	// contains filtered or unexported fields
}

func NewGitHubCopilotClient

func NewGitHubCopilotClient(config GitHubCopilotConfig) (*OpenAICompatibleClient, error)

func NewOpenAICompatibleClient

func NewOpenAICompatibleClient(config OpenAICompatibleConfig) (*OpenAICompatibleClient, error)

func (*OpenAICompatibleClient) CountTokens

func (*OpenAICompatibleClient) Stream

func (c *OpenAICompatibleClient) Stream(ctx context.Context, req Request) (Stream, error)

type OpenAICompatibleConfig

type OpenAICompatibleConfig struct {
	APIKey     string
	BaseURL    string
	HTTPClient *http.Client
}

type OpenAICompatibleEmbeddingConfig

type OpenAICompatibleEmbeddingConfig struct {
	APIKey     string
	BaseURL    string
	HTTPClient *http.Client
}

type OpenAIConfig

type OpenAIConfig struct {
	APIKey                   string
	OAuth                    *OpenAIOAuthConfig
	Backend                  OpenAIBackend
	BaseURL                  string
	PromptCacheRetention     string
	ResponsesStore           bool
	EncryptedReasoningReplay bool
	HTTPClient               *http.Client
}

type OpenAIEmbedder

type OpenAIEmbedder struct {
	// contains filtered or unexported fields
}

func NewOpenAICompatibleEmbedder

func NewOpenAICompatibleEmbedder(config OpenAICompatibleEmbeddingConfig) (*OpenAIEmbedder, error)

func NewOpenAIEmbedder

func NewOpenAIEmbedder(config OpenAIEmbeddingConfig) (*OpenAIEmbedder, error)

func (*OpenAIEmbedder) Embed

func (e *OpenAIEmbedder) Embed(ctx context.Context, req EmbeddingRequest) ([][]float32, error)

type OpenAIEmbeddingConfig

type OpenAIEmbeddingConfig struct {
	APIKey     string
	OAuth      *OpenAIOAuthConfig
	BaseURL    string
	HTTPClient *http.Client
}

type OpenAIOAuthCodeExchangeRequest

type OpenAIOAuthCodeExchangeRequest struct {
	Code          string
	RedirectURI   string
	CodeVerifier  string
	ClientVersion string
}

type OpenAIOAuthConfig

type OpenAIOAuthConfig struct {
	Entry    AuthEntry
	Store    *AuthStore
	TokenURL string
}

type PromptCacheSupport

type PromptCacheSupport struct {
	ProviderID                   string
	RequestHintsSupported        bool
	CacheReadReportingSupported  bool
	CacheWriteReportingSupported bool
	UnsupportedReason            string
}

func PromptCacheSupportForModel

func PromptCacheSupportForModel(ref ModelRef) PromptCacheSupport

type ProviderError

type ProviderError struct {
	Message    string
	StatusCode int
	Retryable  bool
	RetryAfter time.Duration
	Cause      error
	AuthDebug  *providerAuthDebugState
}

func (*ProviderError) Error

func (e *ProviderError) Error() string

func (*ProviderError) Unwrap

func (e *ProviderError) Unwrap() error

type RawSSEFrame

type RawSSEFrame struct {
	APIMode  string
	Sequence int
	Event    string
	Data     []byte
}

type RawSSEObserver

type RawSSEObserver func(RawSSEFrame)

type RemoteModelCatalogProvider

type RemoteModelCatalogProvider struct {
	ID                 string
	Name               string
	Kind               RemoteModelCatalogProviderKind
	BaseURL            string
	APIKey             string
	OAuth              *OpenAIOAuthConfig
	GitHubCopilotOAuth *GitHubCopilotOAuthConfig
	HTTPClient         *http.Client
}

type RemoteModelCatalogProviderKind

type RemoteModelCatalogProviderKind string
const (
	RemoteModelCatalogProviderOpenAI           RemoteModelCatalogProviderKind = "openai"
	RemoteModelCatalogProviderAnthropic        RemoteModelCatalogProviderKind = "anthropic"
	RemoteModelCatalogProviderGoogle           RemoteModelCatalogProviderKind = "google"
	RemoteModelCatalogProviderOpenAICompatible RemoteModelCatalogProviderKind = "openai_compatible"
	RemoteModelCatalogProviderGitHubCopilot    RemoteModelCatalogProviderKind = "github_copilot"
	RemoteModelCatalogProviderModelsDev        RemoteModelCatalogProviderKind = "models_dev"
)

type Request

type Request struct {
	SessionID       string
	TurnID          string
	AgentID         string
	Model           ModelRef
	MaxOutputTokens int
	// PromptCacheRetention is currently honored by OpenAI requests. Empty uses
	// the provider default.
	PromptCacheRetention           string
	OpenAIResponsesStore           bool
	OpenAIEncryptedReasoningReplay bool
	ThinkingSupported              bool
	ThinkingEnabled                bool
	ThinkingMode                   string
	Instructions                   string
	CacheablePrefix                string
	DynamicSuffix                  string
	Inputs                         []Input
	Tools                          []Tool
	RawSSEObserver                 RawSSEObserver
}

func NormalizePromptRequest

func NormalizePromptRequest(req Request) Request

NormalizePromptRequest rewrites request prompt fields into their canonical form so Instructions always matches the effective provider prompt text.

func PreparePromptRequest

func PreparePromptRequest(req Request) Request

PreparePromptRequest normalizes the request and applies any provider/model- specific prompt supplement so callers can reason about the same provider- facing prompt shape used during execution.

func (Request) Validate

func (r Request) Validate() error

type RequestTokenBreakdown

type RequestTokenBreakdown struct {
	PromptTokens          int
	ConversationTokens    int
	ToolNameTokens        int
	ToolDescriptionTokens int
	ToolSchemaTokens      int
	ToolCount             int
	TotalTokens           int
}

func EstimateRequestTokenBreakdown

func EstimateRequestTokenBreakdown(req Request) RequestTokenBreakdown

EstimateRequestTokenBreakdown returns the same rough token estimate used for budgeting, but split into prompt, conversation, and tool-surface components.

func (RequestTokenBreakdown) ToolSurfaceTokens

func (b RequestTokenBreakdown) ToolSurfaceTokens() int

type RequestTrace

type RequestTrace struct {
	APIMode           string
	ParallelToolCalls bool
}

func StreamRequestTrace

func StreamRequestTrace(stream Stream) (RequestTrace, bool)

type RetryHint

type RetryHint struct {
	Retryable  bool
	RetryAfter time.Duration
}

func RetryHintForError

func RetryHintForError(err error) RetryHint

type RouteAttempt

type RouteAttempt struct {
	SessionID string
	TurnID    string
	AgentID   string
	Attempt   int
	Model     ModelRef
	Selected  bool
	Error     error
}

type RouteError

type RouteError struct {
	// contains filtered or unexported fields
}

func (*RouteError) Error

func (e *RouteError) Error() string

func (*RouteError) RouteTrace

func (e *RouteError) RouteTrace() RouteTrace

func (*RouteError) Unwrap

func (e *RouteError) Unwrap() []error

type RouteTrace

type RouteTrace struct {
	RequestedModel ModelRef
	Attempts       []RouteAttempt
}

func ErrorRouteTrace

func ErrorRouteTrace(err error) (RouteTrace, bool)

func StreamRouteTrace

func StreamRouteTrace(stream Stream) (RouteTrace, bool)

func (RouteTrace) SelectedModel

func (t RouteTrace) SelectedModel() (ModelRef, bool)

type RoutedClient

type RoutedClient struct {
	// contains filtered or unexported fields
}

func NewRoutedClient

func NewRoutedClient(clients map[string]Client) (*RoutedClient, error)

func (*RoutedClient) CountTokens

func (c *RoutedClient) CountTokens(ctx context.Context, req Request) (int, TokenCountSource, error)

func (*RoutedClient) SetRouteObserver

func (c *RoutedClient) SetRouteObserver(observer func(RouteAttempt))

func (*RoutedClient) Stream

func (c *RoutedClient) Stream(ctx context.Context, req Request) (Stream, error)

type RoutedEmbedder

type RoutedEmbedder struct {
	// contains filtered or unexported fields
}

func NewRoutedEmbedder

func NewRoutedEmbedder(embedders map[string]Embedder) *RoutedEmbedder

func (*RoutedEmbedder) Embed

func (r *RoutedEmbedder) Embed(ctx context.Context, req EmbeddingRequest) ([][]float32, error)

type SliceStream

type SliceStream struct {
	// contains filtered or unexported fields
}

func NewSliceStream

func NewSliceStream(events []Event) *SliceStream

func NewSliceStreamWithFinishReason

func NewSliceStreamWithFinishReason(events []Event, reason FinishReason) *SliceStream

func (*SliceStream) Close

func (s *SliceStream) Close() error

func (*SliceStream) FinishReason

func (s *SliceStream) FinishReason() FinishReason

func (*SliceStream) Recv

func (s *SliceStream) Recv() (Event, error)

type Stream

type Stream interface {
	Recv() (Event, error)
	Close() error
}

type SystemPromptResolution

type SystemPromptResolution struct {
	Content       string
	CanonicalName string
	SourcePath    string
	UserOverride  bool
}

func ResolveSystemPrompt

func ResolveSystemPrompt(providerID, modelID string) SystemPromptResolution

type TokenCountSource

type TokenCountSource string
const (
	TokenCountSourceEstimated TokenCountSource = "estimated"
	TokenCountSourceExact     TokenCountSource = "exact"
)

func CountRequestTokens

func CountRequestTokens(ctx context.Context, client Client, req Request) (int, TokenCountSource, error)

type Tool

type Tool struct {
	Name         string
	Description  string
	Kind         ToolKind
	InputSchema  string
	InputFormat  *ToolInputFormat
	ParallelSafe bool `json:"-"`
}

func (Tool) KindOrDefault

func (t Tool) KindOrDefault() ToolKind

func (Tool) Validate

func (t Tool) Validate() error

type ToolInputFormat

type ToolInputFormat struct {
	Type       string
	Syntax     string
	Definition string
}

type ToolKind

type ToolKind string
const (
	ToolKindFunction ToolKind = "function"
	ToolKindCustom   ToolKind = "custom"
)

type UsageReport

type UsageReport struct {
	RequestID             string
	Model                 string
	InputTokens           int
	CacheReadInputTokens  int
	CacheWriteInputTokens int
	OutputTokens          int
	ReasoningTokens       int
	TotalTokens           int
}

func StreamUsageReport

func StreamUsageReport(stream Stream) (UsageReport, bool)

Jump to

Keyboard shortcuts

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