ai

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 75 Imported by: 8

Documentation

Overview

Package ai provides provider abstractions, model definitions, and auth storage for pig.

The model catalogs are generated from the exact published Pi package pinned by extensions/sdk-ts. Run `make model-catalogs` after changing the upstream pin.

Index

Constants

View Source
const (
	AnthropicAuthTokenEnv         = "ANTHROPIC_AUTH_TOKEN"
	AnthropicOAuthTokenEnv        = "ANTHROPIC_OAUTH_TOKEN"
	AnthropicAPIKeyEnv            = "ANTHROPIC_API_KEY"
	AnthropicFederationRuleIDEnv  = "ANTHROPIC_FEDERATION_RULE_ID"
	AnthropicOrganizationIDEnv    = "ANTHROPIC_ORGANIZATION_ID"
	AnthropicServiceAccountIDEnv  = "ANTHROPIC_SERVICE_ACCOUNT_ID"
	AnthropicIdentityTokenFileEnv = "ANTHROPIC_IDENTITY_TOKEN_FILE"
	AnthropicWorkspaceIDEnv       = "ANTHROPIC_WORKSPACE_ID"
)

Anthropic credential variables. Mirrors upstream ANTHROPIC_AUTH_TOKEN_ENV, ANTHROPIC_OAUTH_TOKEN_ENV, ANTHROPIC_API_KEY_ENV and the workload identity federation variables of packages/ai/src/env-api-keys.ts.

View Source
const (
	CloudflareWorkersAIBaseURL          = "https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/v1"
	CloudflareAIGatewayCompatBaseURL    = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/compat"
	CloudflareAIGatewayOpenAIBaseURL    = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/openai"
	CloudflareAIGatewayAnthropicBaseURL = "https://gateway.ai.cloudflare.com/v1/{CLOUDFLARE_ACCOUNT_ID}/{CLOUDFLARE_GATEWAY_ID}/anthropic"
)
View Source
const (

	// AnthropicBrowserLoginMethod and AnthropicCopyCodeLoginMethod are the login method IDs Anthropic login offers.
	AnthropicBrowserLoginMethod  = "browser"
	AnthropicCopyCodeLoginMethod = "copy_code"
)
View Source
const (

	// OpenAICodexBrowserLoginMethod and OpenAICodexDeviceCodeLoginMethod are
	// the onSelect option ids for the two Codex login methods. Mirror
	// openai-codex.ts:41-42.
	OpenAICodexBrowserLoginMethod    = "browser"
	OpenAICodexDeviceCodeLoginMethod = "device_code"
)
View Source
const (
	RadiusLoginMethodBrowser    = "browser"
	RadiusLoginMethodDeviceCode = "device-code"
)
View Source
const (
	TestFauxContextWindow = 128000
	TestFauxMaxTokens     = 4096
)

TestFauxProvider is a deterministic, test-only provider used by the integration parity harness. It is deliberately NOT part of the normal model catalog and is only enabled when the binary is launched with the PIG_TEST_FAUX=1 environment variable.

Why this exists: live-model parity gates in tmux are prone to model/provider nondeterminism. Upstream is the source of truth, so parity gates must use a deterministic oracle when the live provider does not reliably materialize a scenario. This provider gives pig a stable scripted backend while upstream gets the same scripted behavior via a temp extension that registers an equivalent custom provider.

Supported scenario values (via PIG_TEST_FAUX_SCENARIO):

  • parity-basic

Any other value returns an EventError. Test-faux model limits match the model the upstream side of the parity harness registers (test/parity/testdata/test-faux-provider.ts), so both sides report the same context window and output budget.

View Source
const AnthropicFederationAuthSource = "workload identity federation"

AnthropicFederationAuthSource is the source the anthropic auth resolver reports when workload identity federation is the only credential. upstream: packages/ai/src/providers/anthropic.ts:68

View Source
const AnthropicOAuthDisplayName = "Anthropic (Claude Pro/Max)"

AnthropicOAuthDisplayName is the OAuth method label upstream renders for Claude Pro/Max subscription login.

View Source
const DefaultHTTPIdleTimeoutMs = 300_000
View Source
const DefaultMaxAgentRetryDelayMs = 60_000

DefaultMaxAgentRetryDelayMs is the default cap for one agent retry delay.

View Source
const DefaultRadiusGateway = "https://radius.pi.dev"

DefaultRadiusGateway is the Radius gateway origin Pi uses by default.

View Source
const MaxProviderErrorBodyChars = 4000
View Source
const (

	// OpenAIChatGPTOAuthName is the OAuth method name of the openai provider.
	OpenAIChatGPTOAuthName = "OpenAI (ChatGPT subscription)"
)
View Source
const OpenAICodexOAuthDisplayName = "OpenAI (ChatGPT Plus/Pro)"

OpenAICodexOAuthDisplayName is the OAuth method label upstream renders for ChatGPT Plus/Pro Codex subscription login.

View Source
const ProviderImagesOpenRouter = "openrouter"
View Source
const RadiusProviderID = "radius"

RadiusProviderID is the built-in Radius provider ID.

View Source
const TestFauxToolStartedMarker = ".pig-faux-tool-started"

TestFauxToolStartedMarker is created in the working directory by the "Run: sleep for a while" scenario's tool as soon as it starts running, so a test can wait for the run to reach tool execution rather than sleeping for a guessed interval.

View Source
const UnsupportedProxyProtocolMessage = "Unsupported proxy protocol. SOCKS and PAC proxy URLs are not supported; use an HTTP or HTTPS proxy URL."

Variables

View Source
var GeneratedClassifierModels = []ClassifierModel{
	{ID: "typesafe/jev", Name: "Jev", API: ClassifierAPI("cloudflare-workers-ai-system-one"), Provider: "cloudflare-workers-ai", BaseURL: "https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai", Input: []string{"text"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000},
	{ID: "jev-1.13", Name: "Jev 1.13", API: ClassifierAPI("typesafe-system-one"), Provider: "opencode", BaseURL: "https://opencode.ai/zen/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000},
	{ID: "jev-1.13-free", Name: "Jev 1.13 Free", API: ClassifierAPI("typesafe-system-one"), Provider: "opencode", BaseURL: "https://opencode.ai/zen/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000},
	{ID: "~typesafe/jev-latest", Name: "TypeSafe: Jev Latest", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000},
	{ID: "inception/mercury-decide:free", Name: "Inception: Mercury Decide (free)", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32768},
	{ID: "jaredpalmer/kev-4b", Name: "Jared Palmer: Kev 4B", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 8192},
	{ID: "respan/span-01", Name: "Respan: Span-01", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.02, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 4096},
	{ID: "respan/span-01-lite", Name: "Respan: Span-01 Lite", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 4096},
	{ID: "respan/span-01-lite:free", Name: "Respan: Span-01 Lite (free)", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 4096},
	{ID: "togethercomputer/tev1-4b-experimental", Name: "Together: Tev1 4B Experimental", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32768},
	{ID: "typesafe/jev-1.13", Name: "TypeSafe: Jev 1.13", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000},
	{ID: "upstage/solar-decide", Name: "Upstage: Solar Decide", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.05, Output: 0, CacheRead: 0.05, CacheWrite: 0}, ContextWindow: 524288},
	{ID: "jev-latest", Name: "Jev", API: ClassifierAPI("typesafe-system-one"), Provider: "typesafe", BaseURL: "https://api.typesafe.ai/v1/", Input: []string{"text"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 64000},
	{ID: "liquid/d1", Name: "Liquid d1", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.04, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 65536},
	{ID: "typesafe-ai/jev", Name: "Jev", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000},
}

GeneratedClassifierModels is the static classifier model catalog (15 entries).

View Source
var GeneratedImageModels = []ImageModel{
	{ID: "black-forest-labs/flux.2-flex", Name: "Black Forest Labs: FLUX.2 Flex", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "black-forest-labs/flux.2-klein-4b", Name: "Black Forest Labs: FLUX.2 Klein 4B", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "black-forest-labs/flux.2-max", Name: "Black Forest Labs: FLUX.2 Max", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "black-forest-labs/flux.2-pro", Name: "Black Forest Labs: FLUX.2 Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "bytedance-seed/seedream-4.5", Name: "ByteDance Seed: Seedream 4.5", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "bytedance-seed/seedream-5-0-lite", Name: "ByteDance Seed: Seedream 5.0 Lite", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "bytedance-seed/seedream-5-0-pro", Name: "ByteDance Seed: Seedream 5.0 Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "google/gemini-2.5-flash-image", Name: "Google: Nano Banana (Gemini 2.5 Flash Image)", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 0.3, Output: 2.5, CacheRead: 0.03, CacheWrite: 0.083333}},
	{ID: "google/gemini-3-pro-image", Name: "Google: Nano Banana Pro (Gemini 3 Pro Image)", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 2, Output: 12, CacheRead: 0.2, CacheWrite: 0.375}},
	{ID: "google/gemini-3-pro-image-preview", Name: "Google: Nano Banana Pro (Gemini 3 Pro Image Preview)", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 2, Output: 12, CacheRead: 0.2, CacheWrite: 0.375}},
	{ID: "google/gemini-3.1-flash-image", Name: "Google: Nano Banana 2 (Gemini 3.1 Flash Image)", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 0.5, Output: 3, CacheRead: 0, CacheWrite: 0}},
	{ID: "google/gemini-3.1-flash-image-preview", Name: "Google: Nano Banana 2 (Gemini 3.1 Flash Image Preview)", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 0.5, Output: 3, CacheRead: 0, CacheWrite: 0}},
	{ID: "google/gemini-3.1-flash-lite-image", Name: "Google: Nano Banana 2 Lite (Gemini 3.1 Flash Lite Image)", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 0.25, Output: 1.5, CacheRead: 0, CacheWrite: 0}},
	{ID: "inclusionai/ming-image-0.1-design", Name: "inclusionAI: Ming Image 0.1 Design", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "inclusionai/ming-image-0.1-design-layer", Name: "inclusionAI: Ming Image 0.1 Design Layer", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "krea/krea-2-large", Name: "Krea: Krea 2 Large", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "krea/krea-2-medium", Name: "Krea: Krea 2 Medium", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "krea/krea-2-medium-turbo", Name: "Krea: Krea 2 Medium Turbo", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "meta/muse-image", Name: "Meta: Muse Image", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "microsoft/mai-image-2.5", Name: "Microsoft AI: MAI-Image-2.5", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 5, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "microsoft/mai-image-2.5-pro", Name: "Microsoft AI: MAI-Image-2.5 Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 5, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "microsoft/mai-image-2.6", Name: "Microsoft AI: MAI-Image-2.6", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 5, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "microsoft/mai-image-2.6-flash", Name: "Microsoft AI: MAI-Image-2.6 Flash", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 1.75, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "openai/gpt-5-image", Name: "OpenAI: GPT-5 Image", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 10, Output: 10, CacheRead: 1.25, CacheWrite: 0}},
	{ID: "openai/gpt-5-image-mini", Name: "OpenAI: GPT-5 Image Mini", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 2.5, Output: 2, CacheRead: 0.25, CacheWrite: 0}},
	{ID: "openai/gpt-5.4-image-2", Name: "OpenAI: GPT-5.4 Image 2", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"image", "text"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image", "text"}, Cost: ModelCost{Input: 8, Output: 15, CacheRead: 2, CacheWrite: 0}},
	{ID: "openai/gpt-image-1", Name: "OpenAI: GPT Image 1", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 10, Output: 10, CacheRead: 1.25, CacheWrite: 0}},
	{ID: "openai/gpt-image-1-mini", Name: "OpenAI: GPT Image 1 Mini", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 2.5, Output: 2.5, CacheRead: 0.25, CacheWrite: 0}},
	{ID: "openai/gpt-image-2", Name: "OpenAI: GPT Image 2", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 8, Output: 8, CacheRead: 2, CacheWrite: 0}},
	{ID: "openai/gpt-image-2.5-flare", Name: "OpenAI: GPT Image 2.5 Flare", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 8, Output: 8, CacheRead: 2, CacheWrite: 0}},
	{ID: "openai/gpt-image-2.5-sunburst", Name: "OpenAI: GPT Image 2.5 Sunburst", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 8, Output: 8, CacheRead: 2, CacheWrite: 0}},
	{ID: "openrouter/auto", Name: "Auto Router", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"text", "image"}, Cost: ModelCost{Input: -1000000, Output: -1000000, CacheRead: 0, CacheWrite: 0}},
	{ID: "openrouter/auto-beta", Name: "Auto Router (Beta)", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"text", "image"}, Cost: ModelCost{Input: -1000000, Output: -1000000, CacheRead: 0, CacheWrite: 0}},
	{ID: "qwen/qwen-image-3", Name: "Qwen: Qwen Image 3", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "qwen/qwen-image-3-pro", Name: "Qwen: Qwen Image 3 Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v3", Name: "Recraft: Recraft V3", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4", Name: "Recraft: Recraft V4", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4-pro", Name: "Recraft: Recraft V4 Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4-pro-vector", Name: "Recraft: Recraft V4 Pro Vector", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4-styles", Name: "Recraft: Recraft V4 Styles", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4-styles-pro", Name: "Recraft: Recraft V4 Styles Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4-styles-pro-vector", Name: "Recraft: Recraft V4 Styles Pro Vector", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4-styles-vector", Name: "Recraft: Recraft V4 Styles Vector", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4-vector", Name: "Recraft: Recraft V4 Vector", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4.1", Name: "Recraft: Recraft V4.1", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4.1-flash", Name: "Recraft: Recraft V4.1 Flash", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4.1-pro", Name: "Recraft: Recraft V4.1 Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4.1-pro-vector", Name: "Recraft: Recraft V4.1 Pro Vector", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4.1-utility", Name: "Recraft: Recraft V4.1 Utility", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4.1-utility-pro", Name: "Recraft: Recraft V4.1 Utility Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "recraft/recraft-v4.1-vector", Name: "Recraft: Recraft V4.1 Vector", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "sourceful/riverflow-v2-fast", Name: "Sourceful: Riverflow V2 Fast", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "sourceful/riverflow-v2-pro", Name: "Sourceful: Riverflow V2 Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "sourceful/riverflow-v2.5-fast", Name: "Sourceful: Riverflow V2.5 Fast", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "sourceful/riverflow-v2.5-pro", Name: "Sourceful: Riverflow V2.5 Pro", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "x-ai/grok-imagine-image-2.0", Name: "xAI: Grok Imagine Image 2.0", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "x-ai/grok-imagine-image-quality", Name: "SpaceXAI: Grok Imagine Image Quality", API: ImageAPI("openrouter-images"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, InputLimits: &ModelInputLimits{Images: &ModelImageInputLimits{Resize: &ModelImageResizeOptions{MaxWidth: 2000, MaxHeight: 2000, MaxBytes: 4718592, JPEGQuality: 80}}}, Output: []string{"image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
}

GeneratedImageModels is the static image model catalog (57 entries).

View Source
var GeneratedModels = []GeneratedModel{}/* 1532 elements not displayed */

GeneratedModels is the static catalog (1532 entries).

View Source
var GeneratedProviders = []string{
	"amazon-bedrock",
	"ant-ling",
	"anthropic",
	"azure-openai-responses",
	"baseten",
	"cerebras",
	"cloudflare-ai-gateway",
	"cloudflare-workers-ai",
	"deepseek",
	"fireworks",
	"github-copilot",
	"google",
	"google-vertex",
	"groq",
	"huggingface",
	"kimi-coding",
	"meta",
	"minimax",
	"minimax-cn",
	"mistral",
	"moonshotai",
	"moonshotai-cn",
	"nvidia",
	"openai",
	"openai-codex",
	"opencode",
	"opencode-go",
	"openrouter",
	"qwen-token-plan",
	"qwen-token-plan-cn",
	"qwen-token-plan-individual",
	"radius",
	"together",
	"typesafe",
	"vercel-ai-gateway",
	"xai",
	"xiaomi",
	"xiaomi-token-plan-ams",
	"xiaomi-token-plan-cn",
	"xiaomi-token-plan-sgp",
	"zai",
	"zai-coding-cn",
}

GeneratedProviders lists the catalog barrel's providers in barrel order (providers/all.ts getBuiltinProviders).

View Source
var ProductVersion = pigversion.Version

ProductVersion is PiG's composite release version for outbound identifiers. It defaults to pigversion.Version because importing coding would create a cycle.

Functions

func APIKeyProviderName

func APIKeyProviderName(id string) string

APIKeyProviderName looks up the display name for an API-key provider id. Returns "" when the provider has no built-in API-key auth method.

func AdjustMaxTokensForThinking

func AdjustMaxTokensForThinking(baseMaxTokens *int, modelMaxTokens int, reasoningLevel string, custom *ThinkingBudgets) (maxTokens, thinkingBudget int)

AdjustMaxTokensForThinking computes maxTokens and thinkingBudget given an optional caller max, model limit, thinking level, and optional custom budgets. A nil base max means "unset": use the model cap and fit the thinking budget inside it. Mirrors upstream adjustMaxTokensForThinking (simple-options.ts:24-47).

func ApplyHTTPProxySettings

func ApplyHTTPProxySettings(httpProxy string) error

ApplyHTTPProxySettings sets absent HTTP_PROXY and HTTPS_PROXY variables. An explicitly empty environment value remains authoritative.

func AssertChatModel

func AssertChatModel(model *Model) error

AssertChatModel rejects a model whose type is not chat (model-operations.ts assertChatModel).

func AssertClassifierModel

func AssertClassifierModel(model AnyModel) error

AssertClassifierModel rejects a model whose type is not classifier (model-operations.ts assertClassifierModel).

func AssertImageModel

func AssertImageModel(model AnyModel) error

AssertImageModel rejects a model whose type is not image (model-operations.ts assertImageModel).

func CalculateContextTokens

func CalculateContextTokens(usage Usage) int

CalculateContextTokens returns the context size a usage block reports: the provider total, else the sum of its components.

func ClampMaxTokensToContext

func ClampMaxTokensToContext(model *Model, context TranscriptContext, maxTokens int) int

ClampMaxTokensToContext reduces a requested output budget to what fits in the model's context window after the estimated request context and a safety margin, never below one token. Mirrors upstream clampMaxTokensToContext, which buildBaseOptions applies to every request.

func ClampOpenAIPromptCacheKey

func ClampOpenAIPromptCacheKey(key string) string

ClampOpenAIPromptCacheKey clamps an OpenAI prompt cache/session key to upstream's 64-Unicode-scalar limit without splitting UTF-8 sequences.

func CleanupSessionResources

func CleanupSessionResources(sessionID string) error

CleanupSessionResources invokes all registered cleanup hooks for the given session id and joins cleanup errors. Empty session ids are ignored by hooks that scope by session.

func CloseOpenAICodexWebSocketSessions

func CloseOpenAICodexWebSocketSessions(sessionID ...string)

CloseOpenAICodexWebSocketSessions closes one session's cached connections, or every cached connection when no session id is supplied.

func CodexAccountID

func CodexAccountID(accessToken string) string

CodexAccountID extracts the chatgpt_account_id claim from a Codex access token, if present. Mirrors upstream getAccountId (openai-codex.ts:281-285). Returns "" if the token doesn't carry the expected JWT shape.

func CompareThinkingLevels

func CompareThinkingLevels(a, b ThinkingLevel) int

CompareThinkingLevels returns -1, 0, or +1 comparing a and b by semantic order (off < minimal < low < medium < high < xhigh). Unknown levels sort before "off".

func ConfigureHTTPDispatcher

func ConfigureHTTPDispatcher(timeoutMs int) error

ConfigureHTTPDispatcher updates the package-global idle timeout used by new streaming HTTP clients. Zero disables the per-read/per-write idle deadline. Mirrors upstream's configureHttpDispatcher(settingsManager.getHttpIdleTimeoutMs()).

func ConfigureProviderRetry

func ConfigureProviderRetry(maxRetries, maxRetryDelayMs int) error

ConfigureProviderRetry sets the package-global provider-request retry policy. It mirrors the resolution of retry.provider.maxRetries / maxRetryDelayMs from settings (sdk.ts passes both into retryProviderRequest). maxRetries<=0 leaves the retry transport inert. Mirrors ConfigureHTTPDispatcher's global-config pattern for the sibling retry.provider.timeoutMs.

func ConfiguredHTTPIdleTimeoutMs

func ConfiguredHTTPIdleTimeoutMs() int

ConfiguredHTTPIdleTimeoutMs returns the currently configured package-global HTTP idle timeout used for new streaming clients.

func ConfiguredProviderRetry

func ConfiguredProviderRetry() (maxRetries, maxRetryDelayMs int)

ConfiguredProviderRetry returns the currently configured provider retry policy: maxRetries and maxRetryDelayMs.

func ContentText

ContentText extracts text blocks in order and joins them with a newline, or the supplied separator. String content passes through unchanged.

func CredentialStoreProviderEnv

func CredentialStoreProviderEnv(ctx context.Context, store CredentialStore, providerID string) (map[string]string, error)

CredentialStoreProviderEnv returns the provider-scoped environment overrides of providerID's API-key credential in store, or nil. It mirrors AuthStorage.GetProviderEnv over any CredentialStore.

func DeclarationsEqual

func DeclarationsEqual(left, right ToolSchema) bool

func EstimateMessageTokens

func EstimateMessageTokens(message Message) int

EstimateMessageTokens estimates one transcript message: system text with its tool changes, text and image content, or assistant text, thinking, and tool calls.

func EstimateTextTokens

func EstimateTextTokens(text string) int

EstimateTextTokens estimates text at four characters per token.

func FindEnvKeys

func FindEnvKeys(provider string, env map[string]string) []string

FindEnvKeys returns the configured environment variables that can provide an API key for provider, in lookup order, or nil. Ambient credential sources (AWS profiles and IAM, Google ADC) are not API keys and are not reported.

func FormatProviderError

func FormatProviderError(norm NormalizedProviderError, prefix ...string) string

FormatProviderError adds body/status only when they are not already carried by the message. Omit prefix to return the unprefixed provider error.

func GetCurrentSystemPrompt

func GetCurrentSystemPrompt(messages []Message) string

GetCurrentSystemPrompt renders replayed instructions and nonempty sections in order.

func GetEnvAPIKey

func GetEnvAPIKey(provider string, env map[string]string) string

GetEnvAPIKey returns a provider's API key from its environment variables, or "<authenticated>" for Vertex ADC and AWS ambient credentials, or "".

func GetImageProviders

func GetImageProviders() []string

GetImageProviders returns the generated image providers in stable order.

func GetOverflowPatterns

func GetOverflowPatterns() []*regexp.Regexp

GetOverflowPatterns returns a copy of the overflow patterns. Mirrors upstream getOverflowPatterns.

func HasApi

func HasApi(model AnyModel, api API) bool

HasApi reports whether model is a chat model on api. Non-chat models never match, even on an equal api string.

func HasNonAdditiveToolChanges

func HasNonAdditiveToolChanges(messages []Message) bool

func HasToolRedefinitions

func HasToolRedefinitions(messages []Message) bool

func IsContextOverflow

func IsContextOverflow(message AssistantMessage, contextWindow int) bool

IsContextOverflow reports whether an assistant message represents a context overflow: an error whose message matches a provider overflow pattern, a successful response whose input exceeds contextWindow (z.ai silent overflow), or a length stop with no output that filled the context window (Xiaomi MiMo). contextWindow 0 disables the usage-based cases. Mirrors upstream isContextOverflow.

func IsModelType

func IsModelType(model AnyModel, modelType ModelType) bool

IsModelType reports whether model has the given type, including chat models without an explicit Type.

func IsOAuthSubscriptionProvider

func IsOAuthSubscriptionProvider(providerID string) bool

IsOAuthSubscriptionProvider reports whether the provider's OAuth login is a subscription login. Mirrors upstream ModelRuntime.isUsingSubscription's auth.oauth?.isSubscription === true test.

func IsRecoverableLength

func IsRecoverableLength(message AssistantMessage, desiredMaxOutput int) bool

IsRecoverableLength reports whether a length stop ended below the intended output limit. desiredMaxOutput must be the limit before any context-based clamping. Mirrors upstream isRecoverableLength.

func IsRetryableAssistantError

func IsRetryableAssistantError(message AssistantMessage) bool

IsRetryableAssistantError classifies whether a failed assistant message looks like a transient provider or transport error. It does not implement retry policy; callers handle context overflow first.

func JSONStringifyLength

func JSONStringifyLength(value any) int

JSONStringifyLength returns the JavaScript length (UTF-16 code units) of JSON.stringify(value), or of "[unserializable]" when value cannot be encoded (estimate.ts safeJsonStringify). encoding/json escapes U+2028, U+2029, and invalid UTF-8 as \uXXXX where JSON.stringify writes one character, so each of those escapes counts as one unit.

func ListProviders

func ListProviders() []string

ListProviders returns the provider ids of the generated catalog barrel in barrel order. Mirrors upstream providers/all.ts:getBuiltinProviders (Object.keys(MODELS)) and compat.ts:getProviders; a provider with only image or classifier models is included.

func ListRuntimeProviders

func ListRuntimeProviders() []string

ListRuntimeProviders returns providers with an implemented runtime API.

func LlamaCppLabelProbabilities

func LlamaCppLabelProbabilities(logprobs []float64, temperature float64) []float64

LlamaCppLabelProbabilities is the softmax over label log-probabilities after dividing them by temperature.

func LlamaCppPeakConfidence

func LlamaCppPeakConfidence(probabilities []float64) float64

LlamaCppPeakConfidence is TypeSafe's documented choice confidence, (n * peak - 1) / (n - 1), clamped to [0, 1].

func LlamaCppServerRoot

func LlamaCppServerRoot(baseURL string) string

LlamaCppServerRoot is the server root: llama.cpp models use the OpenAI-compatible /v1 URL as their base URL.

func MarshalJSONInSourceOrder

func MarshalJSONInSourceOrder(value any, source []byte) ([]byte, error)

MarshalJSONInSourceOrder encodes value with the object member order of the JSON text source. Members source does not have follow in sorted order. A source that is not JSON gives sorted order.

func ModelsAreEqual

func ModelsAreEqual(a, b AnyModel) bool

ModelsAreEqual compares model IDs and declared provider IDs independently of backend construction. Native models without provider metadata use their backend's provider ID. Mirrors upstream models.ts:modelsAreEqual.

func NewFauxProvider

func NewFauxProvider(cfg FauxConfig) *fauxProvider

NewFauxProvider creates a deterministic response source with a shared queue and per-session cache simulation.

func NormalizeRadiusGatewayURL

func NormalizeRadiusGatewayURL(value string) string

NormalizeRadiusGatewayURL adds an https scheme when absent and strips trailing slashes. Mirrors upstream normalizeRadiusGatewayUrl.

func OAuthErrorHTML

func OAuthErrorHTML(message, details string) string

OAuthErrorHTML returns the HTML page shown on OAuth callback error.

func OAuthRefreshTimeout

func OAuthRefreshTimeout(ctx context.Context) (caller context.Context, timeout time.Duration, ok bool)

OAuthRefreshTimeout returns the caller's context and the timeout composed into the refresh signal, or ok false when the refresh signal is the caller's alone.

func OAuthSuccessHTML

func OAuthSuccessHTML(message string) string

OAuthSuccessHTML returns the HTML page shown on successful OAuth callback.

func PiUserAgent

func PiUserAgent() string

pig divergence (D65): upstream getPiUserAgent (packages/ai/src/utils/pi-user-agent.ts) reports "pi (<platform> <release>; <arch>)" from Node's os module, or "pi (browser)" with no Node/Bun runtime. PiG has no browser build and always runs as a native process, so PiUserAgent reports the same platform/release/arch shape with PiG's own product name and composite version instead: "pig/<coding.Version> (<platform> <release>; <arch>)" (owner decision, delivery/OWNER-DECISIONS.md Q4).

func PollOAuthDeviceCodeFlow

func PollOAuthDeviceCodeFlow[T any](ctx context.Context, opts DeviceCodePollOptions[T]) (T, error)

PollOAuthDeviceCodeFlow runs the RFC 8628 polling loop: it calls Poll, honoring pending/slow_down/failed/complete, sleeping the (possibly widened) interval between attempts, until Poll completes or the deadline/ctx fires. Mirrors upstream pollOAuthDeviceCodeFlow.

func ProviderDisplayName

func ProviderDisplayName(providerID string) string

ProviderDisplayName returns the upstream provider.name for a built-in provider, or providerID when the provider is not in the catalog.

func ProviderHeader

func ProviderHeader(value string) *string

ProviderHeader returns a non-null ProviderHeaders value.

func ProviderMaxRetries

func ProviderMaxRetries(ctx context.Context) int

ProviderMaxRetries is the retry budget for requests made with ctx: the WithProviderMaxRetries override, else retry.provider.maxRetries.

func RegisterBuiltInImagesAPIProviders

func RegisterBuiltInImagesAPIProviders()

RegisterBuiltInImagesAPIProviders registers upstream's built-in image API providers. Currently upstream ships OpenRouter image generation only.

func RegisterImagesAPIProvider

func RegisterImagesAPIProvider(provider ImagesAPIProvider)

RegisterImagesAPIProvider registers or replaces an image API provider.

func RegisterOAuthProvider

func RegisterOAuthProvider(id string, provider OAuthProviderInterface)

RegisterOAuthProvider adds a custom OAuth provider. Mirrors upstream registerOAuthProvider (oauth/index.ts:64).

func RegisterSessionResourceCleanup

func RegisterSessionResourceCleanup(cleanup SessionResourceCleanup) func()

RegisterSessionResourceCleanup registers a session-scoped cleanup hook and returns an unregister function. Mirrors upstream registerSessionResourceCleanup.

func RenderSystemMessageUpdate

func RenderSystemMessageUpdate(message SystemMessage) string

RenderSystemMessageUpdate joins instruction blocks with newlines and frames section changes using literal names.

func ResetOAuthProviders

func ResetOAuthProviders()

ResetOAuthProviders removes all custom providers, restoring built-ins only. Mirrors upstream resetOAuthProviders (oauth/index.ts:86).

func ResetOpenAICodexWebSocketDebugStats

func ResetOpenAICodexWebSocketDebugStats(sessionID ...string)

ResetOpenAICodexWebSocketDebugStats clears one session's counters, or all counters when no session id is supplied.

func ResolveCloudflareBaseURL

func ResolveCloudflareBaseURL(providerID, baseURL string, env ProviderEnv) (string, error)

ResolveCloudflareBaseURL substitutes account and gateway placeholders using only explicit provider environment values and JavaScript replacement-string semantics. Missing values retain their placeholders; empty values replace them with empty strings.

func ResolveCopilotEnvFallback

func ResolveCopilotEnvFallback(auth *AuthStorage, envToken string) (bearer, baseURL string, err error)

ResolveCopilotEnvFallback resolves the github-copilot bearer and base URL via the same copilotTokenManager NewCopilotProvider uses, for an auth store with no stored credential and the given env API key. It lets the parity harness assert the env-fallback path (upstream auth-storage.getApiKey step 4) without a live network; production resolution runs through the identical manager inside the provider's stream path.

func ResolveHTTPProxyURLForTarget

func ResolveHTTPProxyURLForTarget(target string, env ProviderEnv) (*url.URL, error)

ResolveHTTPProxyURLForTarget resolves scoped/process proxy aliases and NO_PROXY exclusions. It rejects non-HTTP proxy protocols before a request is sent.

func ResolveOAuthAPIKeyFromStorage

func ResolveOAuthAPIKeyFromStorage(storage *AuthStorage, providerID string) (string, error)

ResolveOAuthAPIKeyFromStorage loads a provider credential from auth.json, refreshes it if needed, persists any refresh, and returns the provider's runtime API key string.

func ResolveOAuthAPIKeyFromStorageContext

func ResolveOAuthAPIKeyFromStorageContext(ctx context.Context, storage *AuthStorage, providerID string) (string, error)

ResolveOAuthAPIKeyFromStorageContext loads, refreshes, and persists a provider credential with the owning operation's cancellation.

func ResolveStoredAPIKeyFromStorage

func ResolveStoredAPIKeyFromStorage(storage *AuthStorage, providerID string) (key string, ok bool, err error)

ResolveStoredAPIKeyFromStorage returns the request key a stored auth.json credential supplies for providerID. Prefer ResolveStoredAPIKeyFromStorageContext when an operation context is available.

func ResolveStoredAPIKeyFromStorageContext

func ResolveStoredAPIKeyFromStorageContext(ctx context.Context, storage *AuthStorage, providerID string) (key string, ok bool, err error)

ResolveStoredAPIKeyFromStorageContext returns a refreshed OAuth key or a resolved api_key credential. A stored credential is checked before caller fallbacks, and OAuth refresh follows ctx.

func RetryDelayMs

func RetryDelayMs(baseDelayMs int, maxAgentDelayMs *int, attempt int) int

RetryDelayMs returns the capped exponential delay for a 1-based attempt. Overflowing products saturate at Number.MAX_SAFE_INTEGER before the cap.

func RunStreamContinuation

func RunStreamContinuation(ctx context.Context, body func(*StreamObservation) error) error

RunStreamContinuation owns a caller's synchronous stream-creation prefix through iteration. The context must carry the shared reaction queue from WithStreamContinuations. The callback uses observation.Context(ctx) for its iterator and Yield when awaiting an already-returned stream.

func SafeJsonStringify

func SafeJsonStringify(value any) string

SafeJsonStringify formats JSON-compatible values without HTML escaping and falls back to their Go string representation when JSON encoding fails.

func TruncateErrorText

func TruncateErrorText(text string, maxChars int) string

TruncateErrorText caps error text in UTF-16 units and reports the number of omitted units.

func UUIDv7

func UUIDv7(timestampMs *int64) (string, error)

UUIDv7 returns a time-ordered UUIDv7. A nil timestamp uses the wall clock and never moves backwards; a supplied timestamp is preserved exactly for follower ids. A process-wide 41-bit sequence seeded from randomness keeps ids unique and ordered within one millisecond.

func UnregisterOAuthProvider

func UnregisterOAuthProvider(id string)

UnregisterOAuthProvider removes a custom OAuth provider. Mirrors upstream unregisterOAuthProvider (oauth/index.ts:74).

func WithOAuthRefreshTimeout

func WithOAuthRefreshTimeout(ctx, caller context.Context, timeout time.Duration) context.Context

WithOAuthRefreshTimeout marks the context of a provider refresh that Pi composes as AbortSignal.any([caller, AbortSignal.timeout(timeout)]) (auth/resolve.ts:149-153). Pi's Models.refresh passes only the caller's signal (models.ts:474) and leaves a context unmarked. A provider that hands its callback a signal, such as an extension bridge, reads the composition with OAuthRefreshTimeout.

func WithProviderMaxRetries

func WithProviderMaxRetries(ctx context.Context, maxRetries int) context.Context

WithProviderMaxRetries returns a context whose provider requests retry at most maxRetries times, overriding retry.provider.maxRetries. It carries upstream's per-request StreamOptions.maxRetries, which the transport-level retry policy cannot see.

func WithProviderRequestRetry

func WithProviderRequestRetry(ctx context.Context, maxRetries int, maxRetryDelayMs *int) context.Context

WithProviderRequestRetry binds an independent caller's retry policy. A nil delay uses Pi's provider default rather than another Session's configuration.

func WithStreamContinuations

func WithStreamContinuations(ctx context.Context) context.Context

WithStreamContinuations attaches a shared reaction queue without acquiring execution. Attach it before publishing a stable signal context that producers and subscribers must share by identity.

Types

type API

type API string

API identifies which provider-API protocol a model uses to talk to its backend. It mirrors upstream `@mariozechner/pi-ai`'s `Api` union type (.upstream/current/packages/ai/src/types.ts:17):

export type Api = KnownApi | (string & {});

The string base type allows forward-compatibility for new APIs added upstream without breaking the pig build, while [KnownAPIs] enumerates the values defined as of the pinned UpstreamVersion.

Use cases: model catalog (GeneratedModel.API), extension provider registration (extension.ProviderConfig.API + ProviderModelConfig.API), stream dispatch (matches one of the Stream<API> handlers).

upstream: types.ts:17 (Api), types.ts:7-15 (KnownApi)

const (
	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"
	APIBedrockConverseStream API = "bedrock-converse-stream"
	APIGoogleGenerativeAI    API = "google-generative-ai"
	APIGoogleVertex          API = "google-vertex"
	APIPiMessages            API = "pi-messages"
)

KnownAPIs enumerates the API values defined by upstream as of UpstreamVersion. Mirrors upstream `KnownApi` union (.upstream/current/packages/ai/src/types.ts:7-15).

Each constant value is the wire-format string consumed by provider dispatch. Provider extensions or generated catalog entries may carry API values not listed here (the underlying type is plain string) - this is forward-compat for new upstream additions.

upstream: types.ts:7-15

func RegisteredAPIs

func RegisteredAPIs() []API

RegisteredAPIs returns the set of built-in API identifiers.

type APIKeyAuth

type APIKeyAuth struct {
	Name string
	// Login performs provider-owned setup. Nil denotes ambient-only authentication.
	Login   func(context.Context, AuthInteraction) (Credential, error)
	Check   func(ctx context.Context, input APIKeyAuthInput) (*AuthCheck, error)
	Resolve func(ctx context.Context, input APIKeyAuthInput) (*AuthResult, error)
}

APIKeyAuth resolves stored-key and ambient API-key auth. Check is an optional side-effect-free availability check; without it availability is checked by resolving. Resolve returns nil when auth is not configured.

func EnvAPIKeyAuth

func EnvAPIKeyAuth(name string, envVars ...string) *APIKeyAuth

EnvAPIKeyAuth is standard API-key auth: a stored credential key wins, otherwise the first set environment variable resolves. Mirrors upstream envApiKeyAuth.

type APIKeyAuthInput

type APIKeyAuthInput struct {
	Ctx        AuthContext
	Credential *Credential
}

APIKeyAuthInput is the input to APIKeyAuth.Check and APIKeyAuth.Resolve. Credential is the stored api_key credential, if any.

type APIKeyProviderInfo

type APIKeyProviderInfo struct {
	ID   string
	Name string
}

APIKeyProviderInfo is a static description of a provider that authenticates with a raw API key (as opposed to OAuth/subscription flow).

func APIKeyProviders

func APIKeyProviders() []APIKeyProviderInfo

APIKeyProviders returns the built-in providers that expose an API-key auth method, sorted by provider.name.

Mirrors upstream interactive-mode.ts:getLoginProviderOptions filtered by auth.apiKey: upstream iterates ModelRuntime.getProviders() (the whole catalog) rather than a hand-maintained subset, so a provider such as opencode-go appears in /login → "Sign in with an API key". Deriving the list here keeps a newly added upstream provider from being silently omitted.

type AnthropicAllowedFallbackModel

type AnthropicAllowedFallbackModel struct {
	Provider string    `json:"provider"`
	Model    string    `json:"model"`
	Cost     ModelCost `json:"cost"`
}

AnthropicAllowedFallbackModel describes a server-side fallback accepted by Anthropic.

type AnthropicConfig

type AnthropicConfig struct {
	// APIKey is the Anthropic API key, sent as X-Api-Key. A subscription
	// (sk-ant-oat) token is sent as an Authorization bearer token with the
	// Claude Code identity instead. When APIKey is empty, the anthropic
	// provider sends ANTHROPIC_AUTH_TOKEN as an Authorization bearer header.
	APIKey string
	// Model is the model name sent in the request (e.g. "claude-sonnet-4-20250514").
	Model string
	// ModelMetadata retains the caller-selected model's capabilities and API metadata instead of resolving an ID through the catalog.
	ModelMetadata *Model
	// ProviderID is the provider label (default: "anthropic").
	ProviderID string
	// BaseURL is the API base (default: https://api.anthropic.com).
	BaseURL string
	// ExtraHeaders are added to every request.
	ExtraHeaders map[string]string
	// Env holds provider-scoped environment overrides that take precedence over
	// the process environment when resolving provider configuration such as
	// PI_CACHE_RETENTION. Mirrors the upstream StreamOptions.env threaded into
	// the provider.
	Env ProviderEnv
	// Compat controls Anthropic-API-specific capability quirks for proxied
	// Anthropic-compatible providers.
	Compat *AnthropicMessagesCompat

	// GetAPIKey dynamically obtains the auth token. Takes precedence over
	// APIKey. Called once per Stream() invocation. Used by GitHub Copilot
	// to auto-refresh the bearer token.
	GetAPIKey func(ctx context.Context) (string, error)
	// GetBaseURL dynamically resolves the base URL. Takes precedence over
	// BaseURL. Used by GitHub Copilot to extract proxy-ep from the
	// refreshed token.
	GetBaseURL func(ctx context.Context) (string, error)
	// DynamicHeaders returns per-request headers (e.g. Copilot X-Initiator).
	// They apply only with UseBearerAuth, the Copilot client.
	DynamicHeaders func(transcript TranscriptContext, opts StreamOptions) map[string]string
	// UseBearerAuth selects the Copilot client: the token is sent as
	// Authorization: Bearer instead of X-Api-Key, with DynamicHeaders and
	// without the Claude Code identity.
	UseBearerAuth bool
}

AnthropicConfig configures the Anthropic Messages API provider.

type AnthropicMessagesCompat

type AnthropicMessagesCompat = OpenAICompat

type AnthropicOAuthProvider

type AnthropicOAuthProvider struct{}

AnthropicOAuthProvider implements OAuthProviderInterface for Anthropic.

func (AnthropicOAuthProvider) GetAPIKey

func (AnthropicOAuthProvider) ID

func (AnthropicOAuthProvider) IsSubscription

func (AnthropicOAuthProvider) IsSubscription() bool

func (AnthropicOAuthProvider) Login

func (AnthropicOAuthProvider) LoginContext

LoginContext asks for the browser or copy code login method, then runs it. Without a select callback it runs browser login, the default. Ports anthropicOAuth.login (.upstream/v1.0.0/packages/ai/src/auth/oauth/anthropic.ts:273).

func (AnthropicOAuthProvider) LoginMethodPrompt

func (AnthropicOAuthProvider) LoginMethodPrompt() OAuthSelectPrompt

LoginMethodPrompt is the login method selection Anthropic login asks first.

func (AnthropicOAuthProvider) Name

func (AnthropicOAuthProvider) RefreshToken

func (AnthropicOAuthProvider) RefreshTokenContext

func (AnthropicOAuthProvider) RefreshTokenContext(ctx context.Context, creds OAuthCredentials) (OAuthCredentials, error)

func (AnthropicOAuthProvider) UsesCallbackServer

func (AnthropicOAuthProvider) UsesCallbackServer() bool

type AnyModel

type AnyModel interface {
	ModelType() ModelType
	ModelID() string
	ProviderID() string
	// CostRates returns the model's per-million-token prices (types.ts BaseModel.cost). A nil model has no price.
	CostRates() ModelCost
	// contains filtered or unexported methods
}

AnyModel is the closed union of everything a provider can list: *Model (chat), *ImageModel and *ClassifierModel.

func AnyModels

func AnyModels[T AnyModel](models []T) []AnyModel

AnyModels widens a typed model list to the AnyModel union.

func DecodeModelsCatalog

func DecodeModelsCatalog(raw []json.RawMessage, providerID string) ([]AnyModel, error)

DecodeModelsCatalog restores the stored models of providerID from their persisted JSON records. Records of types this version does not know, and records of another provider, are dropped.

func GetAllBuiltinModels

func GetAllBuiltinModels(provider string) []AnyModel

GetAllBuiltinModels lists the built-in chat, image and classifier models of one provider.

type AssistantContentBlock

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

AssistantContentBlock is valid inside an assistant message.

type AssistantEventType

type AssistantEventType string

AssistantEventType enumerates all streaming event types.

const (
	EventStart         AssistantEventType = "start"
	EventTextStart     AssistantEventType = "text_start"
	EventTextDelta     AssistantEventType = "text_delta"
	EventTextEnd       AssistantEventType = "text_end"
	EventThinkingStart AssistantEventType = "thinking_start"
	EventThinkingDelta AssistantEventType = "thinking_delta"
	EventThinkingEnd   AssistantEventType = "thinking_end"
	EventToolCallStart AssistantEventType = "toolcall_start"
	EventToolCallDelta AssistantEventType = "toolcall_delta"
	EventToolCallEnd   AssistantEventType = "toolcall_end"
	EventDone          AssistantEventType = "done"
	EventError         AssistantEventType = "error"
)

type AssistantImages

type AssistantImages struct {
	API          ImageAPI
	Provider     string
	Model        string
	Output       []ContentBlock
	ResponseID   string
	Usage        *Usage
	StopReason   ImagesStopReason
	ErrorMessage string
	Timestamp    int64
}

AssistantImages is the final result of an image-generation request.

func GenerateImages

func GenerateImages(ctx context.Context, model ImageModel, imagesCtx ImagesContext, options ProviderImagesOptions) (AssistantImages, error)

GenerateImages dispatches an image-generation request to the model's API provider. It mirrors upstream generateImages(), returning an error only when no provider is registered for the model API.

func GenerateImagesOpenRouter

func GenerateImagesOpenRouter(ctx context.Context, model ImageModel, imagesCtx ImagesContext, options ProviderImagesOptions) AssistantImages

GenerateImagesOpenRouter implements upstream providers/images/openrouter.ts.

func ImageErrorResult

func ImageErrorResult(model *ImageModel, err error, aborted bool) AssistantImages

ImageErrorResult reports a failed image request as an error or aborted result instead of an error (model-operations.ts imageErrorResult).

func (AssistantImages) MarshalJSON

func (r AssistantImages) MarshalJSON() ([]byte, error)

MarshalJSON writes the result with its output blocks.

func (*AssistantImages) UnmarshalJSON

func (r *AssistantImages) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a result.

type AssistantMessage

type AssistantMessage struct {
	Content               []AssistantContentBlock      `json:"content"`
	API                   API                          `json:"api"`
	Provider              string                       `json:"provider"`
	Model                 string                       `json:"model"`
	ResponseModel         string                       `json:"responseModel,omitempty"`
	ResponseID            string                       `json:"responseId,omitempty"`
	ProviderThinkingLevel string                       `json:"providerThinkingLevel,omitempty"`
	Diagnostics           []AssistantMessageDiagnostic `json:"diagnostics,omitempty"`
	Usage                 Usage                        `json:"usage"`
	StopReason            StopReason                   `json:"stopReason"`
	Deferred              *DeferredHandle              `json:"deferred,omitempty"`
	ErrorMessage          string                       `json:"errorMessage,omitempty"`
	RawStopReason         string                       `json:"rawStopReason,omitempty"`
	EndTurn               *bool                        `json:"endTurn,omitempty"`
	Timestamp             int64                        `json:"timestamp"`
	// ThinkingLevel is the thinking level the agent loop requested for this response. Absent outside the agent loop and for legacy responses. Mirrors upstream AssistantMessage.thinkingLevel (packages/ai/src/types.ts:557).
	ThinkingLevel ModelThinkingLevel `json:"thinkingLevel,omitempty"`
	// contains filtered or unexported fields
}

AssistantMessage is a complete or partial model response. A stream event delivers a partial whose exported fields equal the stream state at delivery; they do not change behind readers. Observe and RefreshEvent read the current state of a partial retained past its delivery.

func ReduceAssistantMessageFrames

func ReduceAssistantMessageFrames(frames []AssistantMessageFrame) (*AssistantMessage, error)

ReduceAssistantMessageFrames replays compact frames without mutating them. It returns a nil message when the frames contain no start frame.

func RetryAssistantCall

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

RetryAssistantCall runs one assistant-producing call with bounded retry on transient errors. Successful and aborted responses return immediately; a non-retryable error or an exhausted budget returns the final error message. Cancellation during a backoff sleep returns the failed response normalized to stopReason "aborted" without its error message. A nil or disabled policy returns the first response unchanged.

func (AssistantMessage) MarshalJSON

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

func (*AssistantMessage) Observe

func (message *AssistantMessage) Observe() *AssistantMessage

Observe returns independently owned message data at the current publication. Full views advance all fields; shallow views retain copied scalars and the nested objects present at their copy boundary. An ordinary message is deep-copied; native JSON scalar values and nil containers retain their representation. Nil stays nil.

func (*AssistantMessage) ObserveToolCallIdentity

func (message *AssistantMessage) ObserveToolCallIdentity(index int) (id, name string, toolCall, indexValid bool)

ObserveToolCallIdentity reads only the selected block's kind, ID and name. indexValid distinguishes an absent index from a present non-tool block. Full and shallow views use their retained content reference.

func (*AssistantMessage) ObserveUsage

func (message *AssistantMessage) ObserveUsage() Usage

ObserveUsage returns an owned copy of the current referenced usage without traversing message content, arguments, diagnostics or deferred data.

func (*AssistantMessage) ShallowCopy

func (message *AssistantMessage) ShallowCopy() *AssistantMessage

ShallowCopy copies top-level fields. For a live view it retains synchronized nested references, as the Agent's object spread does in packages/agent/src/agent-loop.ts:416-438. For ordinary data it shares the existing nested values. Nil stays nil.

func (*AssistantMessage) UnmarshalJSON

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

UnmarshalJSON decodes the upstream AssistantMessage wire shape, including its closed content-block union.

type AssistantMessageDiagnostic

type AssistantMessageDiagnostic struct {
	Type      string               `json:"type"`
	Timestamp int64                `json:"timestamp"`
	Error     *DiagnosticErrorInfo `json:"error,omitempty"`
	Details   map[string]any       `json:"details,omitempty"`
}

AssistantMessageDiagnostic is a provider/runtime diagnostic attached to an assistant message when a recoverable failure or transport fallback occurs.

type AssistantMessageEvent

type AssistantMessageEvent interface {
	EventType() AssistantEventType
	// contains filtered or unexported methods
}

AssistantMessageEvent is Pi's closed stream-event union. External providers can construct exported variants but cannot define additional variants.

func RefreshEvent

func RefreshEvent(event AssistantMessageEvent) AssistantMessageEvent

RefreshEvent returns the event with its partial's exported fields set to the stream state at this call. A host dispatcher calls it when it delivers a retained event to a listener after an awaited step, so the listener's direct field reads match what a JavaScript listener sees at that tick. Events without a partial are returned unchanged.

type AssistantMessageEventStream

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

AssistantMessageEventStream is an ordered in-process event stream. Result completes independently of event iteration; unread events remain available until consumed. Subprocess transports impose their own bounded backpressure.

func LazyStream

LazyStream returns before setup runs. Setup starts in the first promise reaction after the call, as the continuation of an async function whose first statement awaits; use LazyStreamSync when Pi's setup has no await before its provider call. The producer owns cancellation and its final event/result; forwarding never replaces an established terminal value with an iterator cancellation.

func LazyStreamSync

LazyStreamSync is LazyStream for a setup that never awaits before it returns its stream. Like an async function without an await, setup runs to completion in the caller's synchronous prefix, before LazyStreamSync returns, and only the forwarding reaction is queued (packages/ai/src/api/lazy.ts:48-50). A caller that already owns the shared continuation queue runs setup inline; any other caller first acquires the queue.

func NewAssistantMessageEventStream

func NewAssistantMessageEventStream() *AssistantMessageEventStream

NewAssistantMessageEventStream creates an open stream.

func StreamSimple

func StreamSimple(ctx context.Context, model *Model, transcript TranscriptContext, options StreamOptions) (*AssistantMessageEventStream, error)

StreamSimple invokes a direct API implementation with model metadata and simple options. Omitted Anthropic reasoning explicitly disables thinking where the selected model permits it. Bedrock leaves authentication to the AWS credential chain or an optional bearer token. Other APIs reject missing request authentication before an event stream exists.

func (*AssistantMessageEventStream) End

func (s *AssistantMessageEventStream) End(result ...*AssistantMessage)

End closes iteration without synthesizing an event. An optional result resolves Result once; ending without a result leaves it pending, as Pi's EventStream.end does.

func (*AssistantMessageEventStream) Events

Events returns a single-pass sequence over the stream's shared FIFO. Multiple iterators divide events in waiter-registration order, matching Pi's queue. Canceling ctx removes an outstanding waiter without a delivery goroutine.

func (*AssistantMessageEventStream) ForwardStream

func (stream *AssistantMessageEventStream) ForwardStream(ctx context.Context, source *AssistantMessageEventStream) error

ForwardStream awaits and forwards one source iterator without observing or cloning its partial references. Its caller owns setup, cancellation, errors and draining. A caller that owns the running turn forwards in it, so the source it just created cannot advance before the first await; any other caller acquires the queue, and a source it created earlier may already have run ahead.

func (*AssistantMessageEventStream) ObservationContext

func (stream *AssistantMessageEventStream) ObservationContext(ctx context.Context) context.Context

ObservationContext attaches this stream's continuation executor to owned setup and forwarding work.

func (*AssistantMessageEventStream) ObserveEvents

ObserveEvents yields the same FIFO as Events and exposes the continuation scope for callers that await asynchronous event handling.

func (*AssistantMessageEventStream) Push

Push appends an event. Pushes after termination are ignored, matching Pi's EventStream. Done and error may terminate without a start event. Missing data and invalid closed-union values return a payload error.

func (*AssistantMessageEventStream) Result

Result waits for a terminal event or End result and returns the first resolved message pointer.

func (*AssistantMessageEventStream) ResultContext

ResultContext waits for the final result or the caller's cancellation without changing stream ownership. The goroutine that runs a consumer callback awaits the result as Pi's `await stream.result()` inside that callback does: it releases the callback's turn and resumes in FIFO order. Any other goroutine only waits, so it never yields a callback that is running.

type AssistantMessageFrame

type AssistantMessageFrame interface {
	FrameType() AssistantMessageFrameType
	// contains filtered or unexported methods
}

AssistantMessageFrame is compact, replayable assistant-message progress. Terminal settlement is intentionally excluded and must be persisted separately. The union is closed like AssistantMessageEvent.

func UnmarshalAssistantMessageFrame

func UnmarshalAssistantMessageFrame(data []byte) (AssistantMessageFrame, error)

UnmarshalAssistantMessageFrame decodes one frame by its `type` discriminator.

type AssistantMessageFrameEncoder

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

AssistantMessageFrameEncoder encodes one assistant stream. Event partials may be a shared live accumulator; the encoder uses per-block offsets to avoid replaying deltas already visible when an older queued event is consumed. The zero value is ready to use.

func (*AssistantMessageFrameEncoder) Encode

Encode converts one stream event into an independently owned frame containing only public replay fields. It returns a nil frame when the event adds nothing replayable (terminal events and covered deltas).

type AssistantMessageFrameType

type AssistantMessageFrameType string

AssistantMessageFrameType discriminates the compact frame union.

const (
	FrameTypeStart              AssistantMessageFrameType = "start"
	FrameTypeTextStart          AssistantMessageFrameType = "text_start"
	FrameTypeTextDelta          AssistantMessageFrameType = "text_delta"
	FrameTypeTextEnd            AssistantMessageFrameType = "text_end"
	FrameTypeThinkingStart      AssistantMessageFrameType = "thinking_start"
	FrameTypeThinkingDelta      AssistantMessageFrameType = "thinking_delta"
	FrameTypeThinkingEnd        AssistantMessageFrameType = "thinking_end"
	FrameTypeToolCallStart      AssistantMessageFrameType = "toolcall_start"
	FrameTypeToolCallCheckpoint AssistantMessageFrameType = "toolcall_checkpoint"
	FrameTypeToolCallDelta      AssistantMessageFrameType = "toolcall_delta"
	FrameTypeToolCallEnd        AssistantMessageFrameType = "toolcall_end"
)

type AuthCheck

type AuthCheck struct {
	Source string
	Type   CredentialType
}

AuthCheck reports that auth is configured without resolving it.

func CheckProviderAuth

func CheckProviderAuth(ctx context.Context, providerID string, auth ProviderAuth, credentials CredentialStore, authContext AuthContext) (*AuthCheck, error)

CheckProviderAuth reports whether a provider's auth is configured without refreshing OAuth or running request-time work where a check exists. Mirrors upstream Models.checkAuth for a known provider.

type AuthContext

type AuthContext struct {
	Env        func(name string) (string, bool)
	FileExists func(path string) bool
}

AuthContext is environment access for auth resolution. Env reports a value only when it is set and not blank.

func DefaultProviderAuthContext

func DefaultProviderAuthContext() AuthContext

DefaultProviderAuthContext reads process environment variables and checks files on disk. It treats ECMAScript-trimmed empty values as absent without trimming nonblank values. Mirrors upstream defaultProviderAuthContext.

type AuthDeviceCodeEvent

type AuthDeviceCodeEvent struct {
	UserCode         string   `json:"userCode"`
	VerificationURI  string   `json:"verificationUri"`
	IntervalSeconds  *float64 `json:"intervalSeconds,omitempty"`
	ExpiresInSeconds *float64 `json:"expiresInSeconds,omitempty"`
}

func (AuthDeviceCodeEvent) MarshalJSON

func (value AuthDeviceCodeEvent) MarshalJSON() ([]byte, error)

func (AuthDeviceCodeEvent) Type

func (AuthDeviceCodeEvent) Type() string

type AuthEvent

type AuthEvent interface {
	Type() string
	// contains filtered or unexported methods
}

type AuthInfoEvent

type AuthInfoEvent struct {
	Message string         `json:"message"`
	Links   []AuthInfoLink `json:"links,omitempty"`
}

func (AuthInfoEvent) MarshalJSON

func (value AuthInfoEvent) MarshalJSON() ([]byte, error)

func (AuthInfoEvent) Type

func (AuthInfoEvent) Type() string
type AuthInfoLink struct {
	URL   string `json:"url"`
	Label string `json:"label,omitempty"`
}

type AuthInteraction

type AuthInteraction struct {
	Prompt func(context.Context, AuthPrompt) (string, error)
	Notify func(AuthEvent)
}

AuthInteraction supplies awaited prompts and synchronous notifications. Login receives its owning context separately.

type AuthManualCodePrompt

type AuthManualCodePrompt struct {
	Message     string `json:"message"`
	Placeholder string `json:"placeholder,omitempty"`
}

func (AuthManualCodePrompt) MarshalJSON

func (value AuthManualCodePrompt) MarshalJSON() ([]byte, error)

func (AuthManualCodePrompt) Type

type AuthProgressEvent

type AuthProgressEvent struct {
	Message string `json:"message"`
}

func (AuthProgressEvent) MarshalJSON

func (value AuthProgressEvent) MarshalJSON() ([]byte, error)

func (AuthProgressEvent) Type

func (AuthProgressEvent) Type() string

type AuthPrompt

type AuthPrompt interface {
	Type() string
	// contains filtered or unexported methods
}

AuthPrompt is a closed login-prompt union. The callback context carries the per-prompt signal.

type AuthResolutionOverrides

type AuthResolutionOverrides struct {
	APIKey *string
	Env    map[string]string
	// MinOAuthValidityMs requires this much remaining OAuth-token validity;
	// nil keeps the default five-minute refresh window without enforcing it.
	MinOAuthValidityMs *float64
}

AuthResolutionOverrides mirrors upstream AuthResolutionOverrides.

type AuthResult

type AuthResult struct {
	Auth ModelAuth
	// Env holds provider-scoped environment/config values resolved from the
	// credential and ambient context.
	Env map[string]string
	// Source is a status label such as "OPENAI_API_KEY" or "OAuth".
	Source string
	// SourcePresent preserves an explicitly empty source received over JSON.
	SourcePresent bool `json:"-"`
}

AuthResult is the result of resolving auth for a provider or model.

func ResolveProviderAuth

func ResolveProviderAuth(ctx context.Context, providerID string, auth ProviderAuth, credentials CredentialStore, authContext AuthContext, overrides AuthResolutionOverrides) (*AuthResult, error)

ResolveProviderAuth resolves request auth for a provider. A stored credential owns the provider: ambient environment is consulted only when nothing is stored, with no fallback after a failed refresh or for a credential type without a matching handler. Mirrors upstream resolveProviderAuth.

func (*AuthResult) UnmarshalJSON

func (r *AuthResult) UnmarshalJSON(data []byte) error

type AuthSecretPrompt

type AuthSecretPrompt struct {
	Message     string `json:"message"`
	Placeholder string `json:"placeholder,omitempty"`
}

func (AuthSecretPrompt) MarshalJSON

func (value AuthSecretPrompt) MarshalJSON() ([]byte, error)

func (AuthSecretPrompt) Type

func (AuthSecretPrompt) Type() string

type AuthSelectOption

type AuthSelectOption struct {
	ID          string `json:"id"`
	Label       string `json:"label"`
	Description string `json:"description,omitempty"`
}

type AuthSelectPrompt

type AuthSelectPrompt struct {
	Message string             `json:"message"`
	Options []AuthSelectOption `json:"options"`
}

func (AuthSelectPrompt) MarshalJSON

func (value AuthSelectPrompt) MarshalJSON() ([]byte, error)

func (AuthSelectPrompt) Type

func (AuthSelectPrompt) Type() string

type AuthSource

type AuthSource string

AuthSource reports where auth would be sourced from. Mirrors upstream AuthStatus.source union.

const (
	AuthSourceStored            AuthSource = "stored"
	AuthSourceRuntime           AuthSource = "runtime"
	AuthSourceEnvironment       AuthSource = "environment"
	AuthSourceFallback          AuthSource = "fallback"
	AuthSourceModelsJSONKey     AuthSource = "models_json_key"
	AuthSourceModelsJSONCommand AuthSource = "models_json_command"
)

type AuthStatus

type AuthStatus struct {
	Configured bool
	Source     AuthSource
	Label      string
}

AuthStatus reports whether auth is configured and where pig would source it from, without exposing secret values.

func CredentialStoreAuthStatus

func CredentialStoreAuthStatus(ctx context.Context, store CredentialStore, providerID string) AuthStatus

CredentialStoreAuthStatus reports whether auth is configured for providerID in store without refreshing OAuth tokens. It mirrors AuthStorage.GetAuthStatus over any CredentialStore.

type AuthStorage

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

AuthStorage is the persistent credential store at <agentDir>/auth.json.

func NewAuthStorage

func NewAuthStorage(path string) (*AuthStorage, error)

NewAuthStorage opens the credential store and materializes a missing auth.json as {} with mode 0600. The parent directory uses mode 0700. Existing bytes and permissions remain unchanged; a failed initial load preserves the last valid snapshot. Instances of the first auth path share that snapshot.

func (*AuthStorage) Delete

func (a *AuthStorage) Delete(ctx context.Context, provider string) error

Delete removes the credential for a provider (no-op if absent). Cancellation aborts process-local or file-lock acquisition and prevents a pending write without releasing another operation's ownership.

func (*AuthStorage) Get

func (a *AuthStorage) Get(provider string) (Credential, bool, error)

Get returns the credential for a provider, if present.

For API-key credentials the Key field is passed through configvalue.Resolve so that auth.json values like "$OPENAI_API_KEY" (explicit env reference) or "!op read op://Personal/openai/key" (shell command) become the runtime value. Bare names are literals (v0.78.1). Mirrors upstream auth-storage.ts which calls resolveConfigValue on the API key on the read path. The on-disk value is left untouched.

func (*AuthStorage) GetAuthStatus

func (a *AuthStorage) GetAuthStatus(provider string) AuthStatus

GetAuthStatus reports whether auth is configured for provider without exposing credential values or refreshing OAuth tokens.

func (*AuthStorage) GetProviderEnv

func (a *AuthStorage) GetProviderEnv(provider string) (map[string]string, error)

GetProviderEnv returns a copy of the provider-scoped environment overrides for an API-key credential, or nil when none are set. Mirrors upstream auth-storage.ts getProviderEnv.

func (*AuthStorage) GetRaw

func (a *AuthStorage) GetRaw(provider string) (Credential, bool, error)

GetRaw returns the credential without applying configvalue.Resolve to the Key field. Use this when you need the on-disk value (e.g. for rewriting auth.json or for OAuth flows where Refresh/Access tokens must round-trip verbatim).

func (*AuthStorage) List

func (a *AuthStorage) List(ctx context.Context) ([]CredentialInfo, error)

List implements CredentialStore in ECMAScript object-key order without resolving configured key values. It shares Read's reload, cancellation, and unsignalled fallback behavior.

func (*AuthStorage) Load

func (a *AuthStorage) Load() (map[string]Credential, error)

Load reads all credentials, creating an empty file if it no longer exists.

func (*AuthStorage) Modify

func (a *AuthStorage) Modify(ctx context.Context, providerID string, fn func(current *Credential) (*Credential, error)) (*Credential, error)

Modify implements CredentialStore under the auth.json file lock. A caller waiting for process-local ownership can cancel without waiting for the active callback.

func (*AuthStorage) Path

func (a *AuthStorage) Path() string

Path returns the auth.json file path.

func (*AuthStorage) Read

func (a *AuthStorage) Read(ctx context.Context, providerID string) (*Credential, error)

Read implements CredentialStore. Concurrent readers share a reload; canceling one reader leaves the others running. A read without a cancellable context falls back to the last valid snapshot on reload failure.

func (*AuthStorage) Set

func (a *AuthStorage) Set(provider string, cred Credential) error

Set writes (or replaces) the credential for a provider. Acquires an exclusive file lock during the read-modify-write cycle so concurrent pi instances refreshing the same OAuth token don't clobber each other.

func (*AuthStorage) Update

func (a *AuthStorage) Update(provider string, mutate func(cur Credential, exists bool) (Credential, error)) error

Update mutates the credential for a provider under the lock. The mutator receives the current credential (zero value if absent) and the boolean indicates whether it existed. Returning an error aborts the write.

type AuthTextPrompt

type AuthTextPrompt struct {
	Message     string `json:"message"`
	Placeholder string `json:"placeholder,omitempty"`
}

func (AuthTextPrompt) MarshalJSON

func (value AuthTextPrompt) MarshalJSON() ([]byte, error)

func (AuthTextPrompt) Type

func (AuthTextPrompt) Type() string

type AuthType

type AuthType = CredentialType

type AuthURLEvent

type AuthURLEvent struct {
	URL          string `json:"url"`
	Instructions string `json:"instructions,omitempty"`
}

func (AuthURLEvent) MarshalJSON

func (value AuthURLEvent) MarshalJSON() ([]byte, error)

func (AuthURLEvent) Type

func (AuthURLEvent) Type() string

type AzureOpenAIResponsesConfig

type AzureOpenAIResponsesConfig struct {
	ModelMetadata  *Model
	APIKey         string
	Model          string
	ProviderID     string
	BaseURL        string
	ExtraHeaders   map[string]string
	SamplingParams map[string]any
	// ThinkingLevelMap overrides catalog effort values when non-nil.
	ThinkingLevelMap    ThinkingLevelMap
	AzureAPIVersion     string
	AzureResourceName   string
	AzureDeploymentName string
	Env                 ProviderEnv
	Compat              *OpenAIResponsesCompat
}

AzureOpenAIResponsesConfig mirrors the upstream Azure wrapper provider while delegating transport/parsing to the shared OpenAI Responses implementation.

type BedrockProvider

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

BedrockProvider implements the Provider interface backed by AWS Bedrock's ConverseStream API.

func NewBedrockProvider

func NewBedrockProvider(model, baseURL string) *BedrockProvider

NewBedrockProvider constructs a Bedrock provider for the given model id. The model id is forwarded verbatim to the ConverseStream call, supporting both base model ids (e.g. "anthropic.claude-3-5-haiku-v1") and inference-profile ARNs.

func NewBedrockProviderWithModel

func NewBedrockProviderWithModel(model Model) *BedrockProvider

NewBedrockProviderWithModel retains the selected model's limits and compatibility metadata for requests and callbacks. Ports packages/ai/src/api/bedrock-converse-stream.ts (stream receives the caller's model, including overrides).

func NewBedrockProviderWithName

func NewBedrockProviderWithName(model, modelName, baseURL string) *BedrockProvider

NewBedrockProviderWithName constructs a Bedrock provider with optional display/model name metadata. The additional name is used only for parity with upstream Bedrock capability detection on application inference profiles whose ARN does not reveal the underlying Claude model family.

func (*BedrockProvider) Close

func (p *BedrockProvider) Close() error

Close releases any persistent resources held by the provider. The bedrockruntime client maintains no goroutines or sockets that outlive a call, so this is a no-op.

func (*BedrockProvider) ID

func (p *BedrockProvider) ID() string

ID returns the selected model's provider identity, or the canonical Bedrock identity for constructors without model metadata.

func (*BedrockProvider) Stream

Stream runs ConverseStream and translates its events into one terminal assistant result. Request failures preserve available HTTP status, AWS error code, and request identity as diagnostics. An explicit CacheRetention overrides the legacy environment preference. Nil contexts are rejected before stream construction.

type CacheRetention

type CacheRetention string

CacheRetention selects the requested prompt-cache lifetime. Empty means the option is unset so the provider can apply its documented default.

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

type ClassifierAPI

type ClassifierAPI string

ClassifierAPI identifies a classifier provider API.

const (
	ClassifierAPITypesafeSystemOne            ClassifierAPI = "typesafe-system-one"
	ClassifierAPICloudflareWorkersAISystemOne ClassifierAPI = "cloudflare-workers-ai-system-one"
	ClassifierAPILlamaCppClassify             ClassifierAPI = "llama-cpp-classify"
)

type ClassifierAnswer

type ClassifierAnswer interface {

	// AnswerType is the wire `type` of the answer: "choice", "score" or "bool".
	AnswerType() string
	// contains filtered or unexported methods
}

ClassifierAnswer is the closed union of classifier answers: ClassifierChoiceAnswer, ClassifierScoreAnswer and ClassifierBoolAnswer.

func LlamaCppAnswerFromProbabilities

func LlamaCppAnswerFromProbabilities(question ClassifierQuestion, keys []string, probabilities []float64) ClassifierAnswer

LlamaCppAnswerFromProbabilities turns label probabilities, in the order of keys, into the public answer shape.

type ClassifierAnswerEntry

type ClassifierAnswerEntry struct {
	ID     string
	Answer ClassifierAnswer
}

ClassifierAnswerEntry is the answer to one named question.

type ClassifierAnswers

type ClassifierAnswers []ClassifierAnswerEntry

ClassifierAnswers keeps the answers of a result in the order the service returned them.

func (ClassifierAnswers) MarshalJSON

func (a ClassifierAnswers) MarshalJSON() ([]byte, error)

MarshalJSON writes the answers as an object keyed by question ID, in order.

func (*ClassifierAnswers) UnmarshalJSON

func (a *ClassifierAnswers) UnmarshalJSON(data []byte) error

UnmarshalJSON reads an object keyed by question ID, keeping the order of the keys.

type ClassifierBoolAnswer

type ClassifierBoolAnswer struct {
	Probability float64
}

ClassifierBoolAnswer is the answer to a bool question.

func (ClassifierBoolAnswer) AnswerType

func (ClassifierBoolAnswer) AnswerType() string

func (ClassifierBoolAnswer) MarshalJSON

func (a ClassifierBoolAnswer) MarshalJSON() ([]byte, error)

type ClassifierBoolCriteria

type ClassifierBoolCriteria struct {
	True  string
	False string
}

ClassifierBoolCriteria describes the two outcomes of a bool question.

type ClassifierBoolQuestion

type ClassifierBoolQuestion struct {
	Instructions string
	Criteria     ClassifierBoolCriteria
}

ClassifierBoolQuestion asks a yes/no question.

func (ClassifierBoolQuestion) MarshalJSON

func (q ClassifierBoolQuestion) MarshalJSON() ([]byte, error)

func (ClassifierBoolQuestion) QuestionType

func (ClassifierBoolQuestion) QuestionType() string

type ClassifierChoiceAnswer

type ClassifierChoiceAnswer struct {
	Choice        string
	Probabilities []ClassifierProbability
	Confidence    float64
}

ClassifierChoiceAnswer is the answer to a choice question.

func (ClassifierChoiceAnswer) AnswerType

func (ClassifierChoiceAnswer) AnswerType() string

func (ClassifierChoiceAnswer) MarshalJSON

func (a ClassifierChoiceAnswer) MarshalJSON() ([]byte, error)

type ClassifierChoiceCriterion

type ClassifierChoiceCriterion struct {
	Key         string
	Description string
}

ClassifierChoiceCriterion is one option of a choice question.

type ClassifierChoiceQuestion

type ClassifierChoiceQuestion struct {
	Instructions string
	Criteria     []ClassifierChoiceCriterion
}

ClassifierChoiceQuestion asks for one of several named options. Criteria keep their declaration order.

func (ClassifierChoiceQuestion) MarshalJSON

func (q ClassifierChoiceQuestion) MarshalJSON() ([]byte, error)

func (ClassifierChoiceQuestion) QuestionType

func (ClassifierChoiceQuestion) QuestionType() string

type ClassifierContext

type ClassifierContext struct {
	State     JsonObject
	Questions ClassifierQuestions
}

ClassifierContext is the input to a classification: the state to judge and the questions to answer about it.

func (ClassifierContext) MarshalJSON

func (c ClassifierContext) MarshalJSON() ([]byte, error)

MarshalJSON writes the context as {state, questions}.

func (*ClassifierContext) UnmarshalJSON

func (c *ClassifierContext) UnmarshalJSON(data []byte) error

UnmarshalJSON reads {state, questions}, keeping the order of the questions.

type ClassifierFunction

ClassifierFunction classifies with one classifier API. It reports failures in the result.

type ClassifierModel

type ClassifierModel struct {
	ID            string
	Name          string
	API           ClassifierAPI
	Provider      string
	BaseURL       string
	Headers       map[string]string
	Input         []string
	InputLimits   *ModelInputLimits
	Cost          ModelCost
	ContextWindow int
}

ClassifierModel is a structured classifier model. It is usable with Models.Classify only.

func GetBuiltinClassifierModel

func GetBuiltinClassifierModel(provider, id string) *ClassifierModel

GetBuiltinClassifierModel reads one built-in classifier model, or nil.

func GetBuiltinClassifierModels

func GetBuiltinClassifierModels(provider string) []*ClassifierModel

GetBuiltinClassifierModels lists the built-in classifier models of one provider, in catalog order. The models are copies: changing one does not change the catalog.

func (*ClassifierModel) CostRates

func (m *ClassifierModel) CostRates() ModelCost

CostRates returns the classifier model's per-million-token prices. A nil model has no price.

func (*ClassifierModel) ModelID

func (m *ClassifierModel) ModelID() string

func (*ClassifierModel) ModelType

func (*ClassifierModel) ModelType() ModelType

func (*ClassifierModel) ProviderID

func (m *ClassifierModel) ProviderID() string

type ClassifierOptions

type ClassifierOptions struct {
	// TelemetryContext parents provider request spans; nil means no recording backend.
	TelemetryContext telemetry.TelemetryContext
	// Fetch replaces HTTP execution without changing the caller's request context or redirect policy.
	Fetch  *http.Client
	APIKey string
	// APIKeySet distinguishes an explicit empty key from an omitted override.
	APIKeySet bool
	Headers   ProviderHeaders
	Env       ProviderEnv
	// TimeoutMs bounds each HTTP attempt. Nil means no timeout.
	TimeoutMs *int
	// MaxRetries is the retry budget after a retryable failure. Nil means 2.
	MaxRetries *int
	// MaxRetryDelayMs caps a server-requested retry delay. Nil means 60000; 0 disables the cap.
	MaxRetryDelayMs *int
	// OnPayload inspects or replaces the request payload before it is sent. Returning false keeps the payload.
	OnPayload  func(payload any, model ClassifierModel) (any, bool, error)
	OnResponse func(response ProviderResponse, model ClassifierModel) error
	// Temperature divides the answer logits before they are normalized into probabilities. It must be positive; APIs
	// that cannot apply it ignore it.
	Temperature *float64
}

ClassifierOptions configures classifier provider requests. Cancellation comes from the request context.

type ClassifierProbability

type ClassifierProbability struct {
	Key         string
	Probability float64
}

ClassifierProbability is the probability of one option of a choice answer.

type ClassifierQuestion

type ClassifierQuestion interface {

	// QuestionType is the wire `type` of the question: "choice", "score" or "bool".
	QuestionType() string
	// contains filtered or unexported methods
}

ClassifierQuestion is the closed union of classifier questions: ClassifierChoiceQuestion, ClassifierScoreQuestion and ClassifierBoolQuestion.

type ClassifierQuestionEntry

type ClassifierQuestionEntry struct {
	ID       string
	Question ClassifierQuestion
}

ClassifierQuestionEntry is one named question.

type ClassifierQuestions

type ClassifierQuestions []ClassifierQuestionEntry

ClassifierQuestions keeps the questions of a request in insertion order.

func (ClassifierQuestions) MarshalJSON

func (q ClassifierQuestions) MarshalJSON() ([]byte, error)

MarshalJSON writes the questions as an object keyed by question ID, in order.

func (*ClassifierQuestions) UnmarshalJSON

func (q *ClassifierQuestions) UnmarshalJSON(data []byte) error

UnmarshalJSON reads an object keyed by question ID, keeping the order of the keys.

type ClassifierResult

type ClassifierResult struct {
	API      ClassifierAPI
	Provider string
	Model    string
	Answers  ClassifierAnswers
	// Usage is the token usage priced at the model's catalog price, when the service reports token counts.
	Usage        *Usage
	StopReason   ClassifierStopReason
	ErrorMessage string
	Timestamp    int64
}

ClassifierResult is the final result of a classification. Failures are results with StopReason error or aborted.

func ClassifierErrorResult

func ClassifierErrorResult(model *ClassifierModel, err error, aborted bool) ClassifierResult

ClassifierErrorResult reports a failed classification as an error or aborted result instead of an error (model-operations.ts classifierErrorResult).

func ClassifyCloudflareWorkersAISystemOne

func ClassifyCloudflareWorkersAISystemOne(ctx context.Context, model ClassifierModel, request ClassifierContext, options ClassifierOptions) ClassifierResult

ClassifyCloudflareWorkersAISystemOne runs System One models on the Workers AI REST endpoint, with public bool values mapped to wire-level noul.

func ClassifyLlamaCpp

func ClassifyLlamaCpp(ctx context.Context, model ClassifierModel, request ClassifierContext, options ClassifierOptions) ClassifierResult

ClassifyLlamaCpp classifies with a chat model on llama-server by reading next-token probabilities of answer labels.

func ClassifyTypesafeSystemOne

func ClassifyTypesafeSystemOne(ctx context.Context, model ClassifierModel, request ClassifierContext, options ClassifierOptions) ClassifierResult

ClassifyTypesafeSystemOne runs TypeSafe's native System One protocol with public bool values mapped to wire-level noul. OpenRouter, OpenCode Zen and Vercel AI Gateway serve the same protocol at other base URLs.

func (ClassifierResult) MarshalJSON

func (r ClassifierResult) MarshalJSON() ([]byte, error)

MarshalJSON writes the result with its answers in order.

func (*ClassifierResult) UnmarshalJSON

func (r *ClassifierResult) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a result, keeping the order of its answers.

type ClassifierScoreAnswer

type ClassifierScoreAnswer struct {
	Score      float64
	Confidence float64
}

ClassifierScoreAnswer is the answer to a score question.

func (ClassifierScoreAnswer) AnswerType

func (ClassifierScoreAnswer) AnswerType() string

func (ClassifierScoreAnswer) MarshalJSON

func (a ClassifierScoreAnswer) MarshalJSON() ([]byte, error)

type ClassifierScoreQuestion

type ClassifierScoreQuestion struct {
	Instructions string
	Criteria     []string
}

ClassifierScoreQuestion asks for a level; Criteria describe the levels from lowest to highest.

func (ClassifierScoreQuestion) MarshalJSON

func (q ClassifierScoreQuestion) MarshalJSON() ([]byte, error)

func (ClassifierScoreQuestion) QuestionType

func (ClassifierScoreQuestion) QuestionType() string

type ClassifierStopReason

type ClassifierStopReason string

ClassifierStopReason is why a classification ended.

const (
	ClassifierStopReasonStop    ClassifierStopReason = "stop"
	ClassifierStopReasonError   ClassifierStopReason = "error"
	ClassifierStopReasonAborted ClassifierStopReason = "aborted"
)

type CodexOAuthProvider

type CodexOAuthProvider struct{}

CodexOAuthProvider implements OAuthProviderInterface for OpenAI Codex.

func (CodexOAuthProvider) GetAPIKey

func (CodexOAuthProvider) ID

func (CodexOAuthProvider) IsSubscription

func (CodexOAuthProvider) IsSubscription() bool

func (CodexOAuthProvider) Login

Login presents the browser/device-code method selection, then runs the chosen flow. Mirrors upstream openaiCodexOAuthProvider.login (openai-codex.ts:568-595).

func (CodexOAuthProvider) LoginContext

func (c CodexOAuthProvider) LoginContext(ctx context.Context, callbacks OAuthLoginCallbacks) (OAuthCredentials, error)

LoginContext presents the browser/device-code method selection and carries ctx through the selected flow.

func (CodexOAuthProvider) Name

func (CodexOAuthProvider) Name() string

func (CodexOAuthProvider) RefreshToken

func (c CodexOAuthProvider) RefreshToken(creds OAuthCredentials) (OAuthCredentials, error)

func (CodexOAuthProvider) RefreshTokenContext

func (CodexOAuthProvider) RefreshTokenContext(ctx context.Context, creds OAuthCredentials) (OAuthCredentials, error)

func (CodexOAuthProvider) UsesCallbackServer

func (CodexOAuthProvider) UsesCallbackServer() bool

type ConstrainedSamplingConfig

type ConstrainedSamplingConfig struct {
	Type     string                   `json:"type"`               // "json_schema" | "grammar"
	Strict   string                   `json:"strict,omitempty"`   // json_schema: "prefer" | "require"
	Variants map[GrammarFormat]string `json:"variants,omitempty"` // grammar: format → definition
}

ConstrainedSamplingConfig is the optional provider-side constrained sampling config for a tool. Type is "json_schema" (JSON-schema strict sampling) or "grammar" (Lark/regex grammar variants). Mirrors upstream ConstrainedSamplingConfig; upstream's `false` maps to a nil pointer.

type ContentBlock

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

ContentBlock is the closed union of provider-facing content types.

func UnmarshalContentBlock

func UnmarshalContentBlock(data []byte) (ContentBlock, error)

UnmarshalContentBlock decodes a single content-block JSON value into the appropriate concrete type. Unknown `type` values are accepted as TextContent with the JSON re-encoded as the text payload: this preserves data we don't recognise rather than silently dropping it.

type ContentBlocks

type ContentBlocks []ContentBlock

ContentBlocks is a slice alias that knows how to decode the discriminated-union JSON form.

Use this when persisting/loading messages from disk:

var msg struct {
    Role    string        `json:"role"`
    Content ContentBlocks `json:"content"`
}

When you only need the slice as `[]ContentBlock` (the historical shape used by streams + tool execution), call `.Blocks()`.

func (ContentBlocks) Blocks

func (cb ContentBlocks) Blocks() []ContentBlock

func (ContentBlocks) MarshalJSON

func (cb ContentBlocks) MarshalJSON() ([]byte, error)

MarshalJSON delegates to the default slice encoder.

func (*ContentBlocks) UnmarshalJSON

func (cb *ContentBlocks) UnmarshalJSON(data []byte) error

UnmarshalJSON sniffs each element's `type` field and decodes into the matching concrete type.

type Context

type Context struct {
	SystemPrompt string
	Messages     []Message
	Tools        []ToolSchema
}

Context is the public request shape accepted before provider normalization.

type ContextUsageEstimate

type ContextUsageEstimate struct {
	Tokens         int
	UsageTokens    int
	TrailingTokens int
	LastUsageIndex int
}

ContextUsageEstimate is an estimated context size. LastUsageIndex is the index of the message whose usage the estimate starts from, or -1 for upstream's null.

func EstimateContextTokens

func EstimateContextTokens(messages []Message) ContextUsageEstimate

EstimateContextTokens estimates a transcript's context size from the last applicable assistant usage plus the messages after it, or from every message when no usage applies.

type CopilotLoginCallbacks

type CopilotLoginCallbacks struct {
	// OnPrompt is invoked once at the start to ask for an enterprise domain.
	// Return "" to use github.com.
	OnPrompt func(ctx context.Context) (string, error)
	// OnAuth is invoked once with the verification URL and user code that
	// the user must visit and enter.
	OnAuth func(verificationURL, userCode string)
	// OnDeviceCode receives the complete device notification and takes precedence over OnAuth.
	OnDeviceCode func(OAuthDeviceCodeInfo)
	// OnProgress is invoked with status messages (e.g. "Enabling models...").
	OnProgress func(msg string)
}

CopilotLoginCallbacks is the user-facing surface for an interactive login. All callbacks are optional except OnAuth (we have to surface the user code somehow) and OnPrompt (only called if the caller wants to support enterprise domain entry: pass a no-op returning "" for vanilla github.com).

type CopilotProviderConfig

type CopilotProviderConfig struct {
	// Auth is the credential store; the provider will read and refresh
	// the github-copilot entry as needed.
	Auth *AuthStorage
	// Model is the Copilot model id (e.g. "gpt-4o", "claude-sonnet-4").
	Model string
	// API selects the wire protocol. When set to APIOpenAIResponses (or
	// "openai-responses"), the provider uses the Responses API instead of
	// the Chat Completions API. This matters for models like gpt-5.4 that
	// require the Responses endpoint when tools + reasoning are combined.
	// Mirrors upstream: the model's `api` field in models.generated.ts
	// determines which endpoint to hit: "openai-completions" vs
	// "openai-responses".
	API API
	// Reasoning indicates the selected Copilot model supports reasoning.
	// Required for the Responses API to include reasoning.effort/summary and
	// stream thinking deltas.
	Reasoning bool
	// EnvToken is the env-resolved API key (COPILOT_GITHUB_TOKEN). When auth.json has no github-copilot OAuth
	// credential, it is used directly as the bearer, mirroring upstream
	// auth-storage.getApiKey step 4 (the env fallback returns getEnvApiKey
	// verbatim). The base URL is derived from the token's proxy-ep claim.
	EnvToken string
	// RuntimeToken returns a non-persistent runtime key (--api-key). When it
	// reports one, it is the bearer ahead of the stored credential and
	// EnvToken, as upstream's RuntimeCredentials overlay masks auth.json.
	RuntimeToken func() (string, bool)
	// BaseURL is the request model's base URL. It applies when a resolved key
	// (RuntimeToken) owns the request: upstream ModelRuntime sends the model
	// with resolution.auth.baseUrl when the credential's toAuth derives one
	// (an OAuth token's proxy-ep endpoint), and otherwise the catalog or
	// models.json baseUrl. Empty falls back to the individual endpoint.
	BaseURL string
	// ModelMetadata is the selected model (catalog, refreshed catalog or
	// models.json), carrying its compat and thinking metadata. Every API leaf
	// reads model.compat as upstream anthropic-messages, openai-completions and
	// openai-responses do. Nil resolves the generated github-copilot catalog
	// entry by id.
	ModelMetadata *Model
}

CopilotProviderConfig configures a github-copilot provider.

type CostTier

type CostTier struct {
	InputTokensAbove    int     `json:"inputTokensAbove"`
	InputCostPer1M      float64 `json:"input"`
	OutputCostPer1M     float64 `json:"output"`
	CacheReadCostPer1M  float64 `json:"cacheRead"`
	CacheWriteCostPer1M float64 `json:"cacheWrite"`
}

CostTier overrides the base per-1M rates for requests whose total input usage (input+cacheRead+cacheWrite) exceeds InputTokensAbove. Mirrors upstream ModelCostTier.

type CreateModelsOptions

type CreateModelsOptions struct {
	Credentials CredentialStore
	ModelsStore ModelsStore
	AuthContext *AuthContext
}

CreateModelsOptions supplies application-owned stores and auth context to model collections.

type CreateProviderOptions

type CreateProviderOptions struct {
	ID      string
	Name    *string
	BaseURL string
	Headers ProviderHeaders
	Auth    ProviderAuth
	// Models are the static baseline models of every type. Models without a type are chat models.
	Models []AnyModel
	// FetchModels returns a dynamic overlay of every type.
	FetchModels     func(RefreshModelsContext) ([]AnyModel, error)
	FilterModels    func([]*Model, *Credential) []*Model
	FilterAllModels func([]AnyModel, *Credential) []AnyModel
	// API is the chat implementation. It is optional when Images or Classifiers is given.
	API         ProviderAPIs
	Images      ProviderImageAPIMap
	Classifiers ProviderClassifierMap
}

type Credential

type Credential struct {
	Extra map[string]json.RawMessage `json:"-"`
	Type  CredentialType             `json:"type"`

	// API-key variant. Field is "key" (matches upstream); we keep an
	// alias "apiKey" on read for backwards compatibility with auth.json
	// files written by older pig builds.
	Key string `json:"key,omitempty"`

	// Env holds provider-scoped environment overrides for an API-key
	// credential. Values take precedence over the process environment when
	// resolving this credential's own `$VAR` references and when the provider
	// reads configuration env. Mirrors upstream auth-storage.ts
	// ApiKeyCredential.env.
	Env map[string]string `json:"env,omitempty"`

	// OAuth variant (used by github-copilot, anthropic, etc.)
	Refresh string `json:"refresh,omitempty"`
	Access  string `json:"access,omitempty"`
	// Expires is the truncated integer projection of expires in milliseconds since the epoch. ExpiresMillis returns Pi's exact value, including a fraction or an absent property.
	// Expiry decisions must use ExpiresMillis. SetExpiresMillis stores a fraction; assigning Expires replaces any retained exact value.
	Expires          int64  `json:"expires,omitempty"`
	ProjectID        string `json:"projectId,omitempty"`
	AccountID        string `json:"accountId,omitempty"`
	EnterpriseDomain string `json:"enterpriseUrl,omitempty"`
	// Scope is the granted OAuth scope some flows (Radius) return and persist.
	Scope string `json:"scope,omitempty"`
	// AvailableModelIDs retains the account's optional picker filter, including an explicit empty list or malformed external value.
	AvailableModelIDs json.RawMessage `json:"availableModelIds,omitempty"`
	// GatewayConfig is the Radius gateway catalog that pre-ModelsStore Radius
	// builds cached on the credential. It is read once to import that catalog.
	GatewayConfig json.RawMessage `json:"gatewayConfig,omitempty"`
	// contains filtered or unexported fields
}

Credential is the on-disk shape for one provider's credentials. We use a flat struct that covers both the api-key and oauth variants - fields not relevant to the type are simply zero-valued.

JSON field names match upstream's typed surface so files are interchangeable with `pi`'s auth.json.

func CredentialFromOAuth

func CredentialFromOAuth(value OAuthCredentials) (Credential, error)

CredentialFromOAuth returns the stored OAuth credential for a token object, as Pi's `{ ...credential, type: "oauth" }` does. It fails only when retained metadata is not valid JSON.

func LoginGitHubCopilot

func LoginGitHubCopilot(ctx context.Context, cb CopilotLoginCallbacks) (Credential, error)

LoginGitHubCopilot runs the OAuth device code flow against github.com (or an enterprise domain) and returns refreshed Copilot credentials. The returned Credential has Type=CredentialOAuth and is ready to be persisted via AuthStorage.Set("github-copilot", cred).

func ReadRawCredential

func ReadRawCredential(ctx context.Context, store CredentialStore, providerID string) (Credential, bool, error)

ReadRawCredential uses GetRaw when the store exposes it. Otherwise it calls Read, which may resolve configuration values according to the supplied store's contract.

func (*Credential) ClearExpires

func (c *Credential) ClearExpires()

ClearExpires removes the expires property.

func (Credential) ExpiresMillis

func (c Credential) ExpiresMillis() float64

ExpiresMillis returns ToNumber of an OAuth credential's expires value. An absent value is NaN, so both orderings compare false.

func (Credential) HasExpires

func (c Credential) HasExpires() bool

HasExpires reports whether the credential carries an expires property.

func (Credential) MarshalJSON

func (c Credential) MarshalJSON() ([]byte, error)

func (Credential) OAuthCredentials

func (c Credential) OAuthCredentials() OAuthCredentials

OAuthCredentials returns the OAuth token object stored in c, including provider-owned metadata and the exact expires value.

func (*Credential) SetExpiresMillis

func (c *Credential) SetExpiresMillis(value float64)

SetExpiresMillis stores a JavaScript number, including a fraction. Expires receives its truncated projection.

func (*Credential) UnmarshalJSON

func (c *Credential) UnmarshalJSON(data []byte) error

type CredentialInfo

type CredentialInfo struct {
	ProviderID string
	Type       CredentialType
}

CredentialInfo is non-secret credential metadata for account/status enumeration. Mirrors upstream CredentialInfo.

type CredentialStore

type CredentialStore interface {
	// Read returns the stored credential, possibly expired, or nil when none
	// is stored. API-key values are resolved as configuration values.
	Read(ctx context.Context, providerID string) (*Credential, error)
	// List returns stored credential metadata in ECMAScript object-key order without resolving values.
	List(ctx context.Context) ([]CredentialInfo, error)
	// Modify runs fn with the current raw credential under the store's
	// lock. A nil result leaves the entry unchanged. It returns the
	// post-write credential.
	Modify(ctx context.Context, providerID string, fn func(current *Credential) (*Credential, error)) (*Credential, error)
	// Delete removes the stored credential, doing nothing if it is absent.
	Delete(ctx context.Context, providerID string) error
}

CredentialStore is app-owned credential storage keyed by provider id. Modify serializes OAuth refreshes as read-modify-write operations; Delete removes a provider's credential for logout. Both honor the operation context.

type CredentialType

type CredentialType string

CredentialType is the discriminator stored as `"type"` in auth.json.

Values match upstream pi `dist/core/auth-storage.d.ts`:

  • "api_key" for raw API keys (field: "key")
  • "oauth" for OAuth refresh/access tokens

Files written by pig must round-trip through upstream pi and vice versa. Earlier PiG builds wrote "type":"api" / "apiKey", which broke parity. The Load() path migrates the old shape forward in place on first read.

const (
	CredentialAPIKey CredentialType = "api_key"
	CredentialOAuth  CredentialType = "oauth"
)

type DeferredCancelOptions

type DeferredCancelOptions = StreamOptions

type DeferredFetchOptions

type DeferredFetchOptions struct {
	StreamOptions
	Wait *float64
}

type DeferredHandle

type DeferredHandle struct {
	Provider    string `json:"provider"`
	ModelID     string `json:"modelId"`
	API         API    `json:"api"`
	ID          string `json:"id"`
	ExpiresAt   *int64 `json:"expiresAt,omitempty"`
	PollAfterMS *int64 `json:"pollAfterMs,omitempty"`
	Data        any    `json:"data,omitempty"`
}

DeferredHandle identifies a provider-side deferred response and the data required to retrieve it later.

type DeferredOption

type DeferredOption struct {
	Object  bool
	Enabled bool
	Window  string
}

DeferredOption is a deferred-generation boolean or an object with an optional window.

func (DeferredOption) MarshalJSON

func (option DeferredOption) MarshalJSON() ([]byte, error)

MarshalJSON emits the upstream boolean or object form.

func (*DeferredOption) UnmarshalJSON

func (option *DeferredOption) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes either deferred-generation option form.

type DeviceCodePollOptions

type DeviceCodePollOptions[T any] struct {
	IntervalSeconds     *float64
	ExpiresInSeconds    *float64
	WaitBeforeFirstPoll bool
	Poll                func() (DeviceCodePollResult[T], error)
}

DeviceCodePollOptions configures PollOAuthDeviceCodeFlow. A nil IntervalSeconds defaults to 5s (RFC 8628 §3.2); a nil ExpiresInSeconds means no deadline. WaitBeforeFirstPoll sleeps one interval before the first poll. The Go API carries the upstream AbortSignal through ctx.

type DeviceCodePollResult

type DeviceCodePollResult[T any] struct {
	Status  DeviceCodePollStatus
	Value   T
	Message string
	// IntervalSeconds is the server-provided polling interval on a slow_down
	// result. Nil (or a non-positive value) applies the RFC 8628 +5s rule.
	IntervalSeconds *float64
}

DeviceCodePollResult is one poll attempt's result. Mirrors upstream OAuthDeviceCodePollResult.

type DeviceCodePollStatus

type DeviceCodePollStatus int

DeviceCodePollStatus is the outcome of one device-code poll attempt.

const (
	// DevicePollPending: keep polling.
	DevicePollPending DeviceCodePollStatus = iota
	// DevicePollSlowDown: keep polling but widen the interval (RFC 8628 §3.5).
	DevicePollSlowDown
	// DevicePollFailed: stop with Message as the error.
	DevicePollFailed
	// DevicePollComplete: stop and return Value.
	DevicePollComplete
)

type DiagnosticErrorInfo

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

DiagnosticErrorInfo is a redacted, serializable error summary attached to assistant diagnostics.

type DoneEvent

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

func (DoneEvent) EventType

func (DoneEvent) EventType() AssistantEventType

func (DoneEvent) MarshalJSON

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

type ErrorEvent

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

func (ErrorEvent) EventType

func (ErrorEvent) EventType() AssistantEventType

func (ErrorEvent) MarshalJSON

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

type FauxConfig

type FauxConfig struct {
	Deferred        *FauxDeferredConfig
	API             API
	ProviderID      string
	Model           string
	Models          []FauxModelDefinition
	TokensPerSecond int
	MinTokenSize    int
	MaxTokenSize    int
}

type FauxContentBlock

type FauxContentBlock struct {
	Type      string         `json:"type"`
	Text      string         `json:"text,omitempty"`
	Thinking  string         `json:"thinking,omitempty"`
	ID        string         `json:"id,omitempty"`
	Name      string         `json:"name,omitempty"`
	Arguments map[string]any `json:"arguments,omitempty"`
}

func FauxText

func FauxText(text string) FauxContentBlock

func FauxThinking

func FauxThinking(thinking string) FauxContentBlock

func FauxToolCall

func FauxToolCall(name string, args map[string]any, id string) FauxContentBlock

type FauxDeferredConfig

type FauxDeferredConfig struct {
	PendingFetches float64
	PollAfterMS    *int64
}

type FauxModelDefinition

type FauxModelDefinition struct {
	ID            string
	Name          string
	Reasoning     bool
	Input         []string
	ContextWindow int
	MaxTokens     int
}

type FauxProviderState

type FauxProviderState struct {
	CallCount          atomic.Int64
	DeferredFetchCount atomic.Int64
}

FauxProviderState exposes the live call counter safely across concurrent factories.

type FauxResponse

type FauxResponse struct {
	Deferred *DeferredHandle

	// Timestamp supplies the response's Unix-millisecond timestamp; nil keeps the provider clock.
	Timestamp    *int64
	Content      []FauxContentBlock
	StopReason   string
	ErrorMessage string
	ResponseID   string
	// contains filtered or unexported fields
}

type FauxResponseFactory

type FauxResponseFactory func(TranscriptContext, StreamOptions, *FauxProviderState, *Model) (FauxResponse, error)

FauxResponseFactory is awaited by its stream; a returned error terminates that stream before generation.

type FauxResponseStep

type FauxResponseStep struct {
	Static  *FauxResponse
	Factory FauxResponseFactory
}

func FauxFactoryStep

func FauxFactoryStep(factory FauxResponseFactory) FauxResponseStep

func FauxStaticStep

func FauxStaticStep(response FauxResponse) FauxResponseStep

type FetchFunction

type FetchFunction func(*http.Request) (*http.Response, error)

FetchFunction is a per-request HTTP transport. The request context owns cancellation; the caller owns and closes the returned streaming response body. Ports packages/ai/src/types.ts (ProviderRequestOptions.fetch).

func (FetchFunction) RoundTrip

func (fetch FetchFunction) RoundTrip(request *http.Request) (*http.Response, error)

type FileModelsStore

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

FileModelsStore mirrors coding-agent core/models-store.ts FileModelsStore: locked JSON storage for dynamically refreshed provider catalogs, keyed by provider id, at <agentDir>/models-store.json by default.

func NewFileModelsStore

func NewFileModelsStore(path string) *FileModelsStore

NewFileModelsStore opens the store at path.

func (*FileModelsStore) Delete

func (s *FileModelsStore) Delete(ctx context.Context, providerID string) error

Delete removes the provider's stored catalog. Like upstream it rewrites the file even when the provider has no entry.

func (*FileModelsStore) Path

func (s *FileModelsStore) Path() string

Path returns the store file path.

func (*FileModelsStore) Read

func (s *FileModelsStore) Read(ctx context.Context, providerID string) (*ModelsStoreEntry, error)

Read returns a fresh copy of the provider's stored catalog. Concurrent readers share reloads and cancel their waits independently; unchanged files use the cached revision.

func (*FileModelsStore) Write

func (s *FileModelsStore) Write(ctx context.Context, providerID string, entry ModelsStoreEntry) error

Write replaces the provider's stored catalog.

type GeneratedModel

type GeneratedModel struct {
	ID                   string
	Provider             string
	DisplayName          string
	API                  API
	BaseURL              string
	Headers              map[string]string
	Compat               *ModelCompat
	ThinkingLevelMap     map[ThinkingLevel]*string
	SamplingParams       map[string]any
	PromptCache          ModelPromptCache
	InputLimits          *ModelInputLimits
	ContextWindow        int
	MaxOutputTokens      int
	InputCostPerMTokens  float64
	OutputCostPerMTokens float64
	CacheReadCost        float64
	CacheWriteCost       float64
	Tiers                []CostTier
	Reasoning            bool
	Capabilities         []string
	Enabled              *bool
	Lab                  string
	Providers            []ModelCatalogProvider
}

GeneratedModel is a static catalog entry for one model. Lives in generated code so the registry init is just a slice copy. Field shape mirrors upstream pi-ai's `Model<api>` discriminated union reduced to the fields the pig runtime actually consumes.

func ListModels

func ListModels(provider string) []GeneratedModel

ListModels returns a copy of the catalog filtered by an optional provider prefix. Empty provider returns everything.

func LookupModel

func LookupModel(spec string) (*GeneratedModel, bool)

LookupModel resolves a spec like "<provider>/<model-id>" or just "<model-id>" against the generated catalog. Returns the matching model and true on success.

When only a bare model id is given and multiple providers ship a model with the same id (rare; "claude-3-5-sonnet" appears under both anthropic and copilot for example), the first match in alphabetical-provider order wins. Callers wanting deterministic resolution should pass the full "<provider>/<model-id>" form.

func LookupModelExact

func LookupModelExact(spec string) (*GeneratedModel, bool)

LookupModelExact resolves only the fully-qualified "provider/id" key, without LookupModel's bare-id fallback across other providers. This is what model resolution needs so that "github-copilot/gpt-4o" yields no match when the copilot catalog lacks gpt-4o, instead of silently borrowing openai's gpt-4o capabilities. Mirrors upstream resolveCliModel, which filters candidate models by provider before matching the id.

func (*GeneratedModel) ToCapabilities

func (m *GeneratedModel) ToCapabilities() ModelCapabilities

ToCapabilities lifts a GeneratedModel into the runtime ModelCapabilities consumed by status line and provider routing.

func (*GeneratedModel) ToModel

func (m *GeneratedModel) ToModel() *Model

ToModel preserves catalog prices, limits, modalities, capabilities and provider metadata for runtime requests.

type GoogleConfig

type GoogleConfig struct {

	// APIKey is the Google AI API key.
	APIKey string
	// Model is the model name (e.g. "gemini-2.5-flash").
	Model string
	// ProviderID is the provider label (default: "google-generative-ai").
	ProviderID string
	// BaseURL overrides the API base (default: https://generativelanguage.googleapis.com).
	BaseURL string
	// APIVersion overrides the API version path (default: "v1beta").
	// Set to "" when BaseURL already includes the version path.
	APIVersion string
	// ExtraHeaders are added to every request.
	ExtraHeaders map[string]string
	// ThinkingLevelMap carries the selected model's logical-to-provider thinking mapping. Nil uses the catalog mapping.
	ThinkingLevelMap ThinkingLevelMap
	// ModelMetadata is the selected model, which an OnProviderStreamEvent observer receives. Nil hands the observer the configured identity only.
	ModelMetadata *Model
	// contains filtered or unexported fields
}

GoogleConfig configures the Google Gemini provider.

type GoogleThinkingLevel

type GoogleThinkingLevel string

GoogleThinkingLevel mirrors the upstream exported thinking-level union used by both the direct Gemini and Vertex providers.

const (
	GoogleThinkingLevelUnspecified GoogleThinkingLevel = "THINKING_LEVEL_UNSPECIFIED"
	GoogleThinkingLevelMinimal     GoogleThinkingLevel = "MINIMAL"
	GoogleThinkingLevelLow         GoogleThinkingLevel = "LOW"
	GoogleThinkingLevelMedium      GoogleThinkingLevel = "MEDIUM"
	GoogleThinkingLevelHigh        GoogleThinkingLevel = "HIGH"
)

type GoogleThinkingOptions

type GoogleThinkingOptions struct {
	Enabled      bool                `json:"enabled"`
	BudgetTokens *int                `json:"budgetTokens,omitempty"`
	Level        GoogleThinkingLevel `json:"level,omitempty"`
}

GoogleThinkingOptions is the raw Google API thinking object. A level takes precedence over a budget; a non-nil zero budget remains explicit.

type GoogleVertexConfig

type GoogleVertexConfig struct {
	APIKey           string
	Model            string
	ProviderID       string
	BaseURL          string
	Project          string
	Location         string
	Headers          map[string]string
	ThinkingLevelMap ThinkingLevelMap
	// ModelMetadata is the selected model, which an OnProviderStreamEvent observer receives. Nil hands the observer the configured identity only.
	ModelMetadata *Model
}

GoogleVertexConfig configures Vertex API-key or Application Default Credentials requests.

type GrammarFormat

type GrammarFormat = string

GrammarFormat is an OpenAI grammar variant key. Mirrors upstream GrammarFormat.

const (
	GrammarFormatOpenAILark  GrammarFormat = "openai_lark"
	GrammarFormatOpenAIRegex GrammarFormat = "openai_regex"
)

type ImageAPI

type ImageAPI string

ImageAPI identifies an image-generation provider API.

const APIImagesOpenRouter ImageAPI = "openrouter-images"

type ImageContent

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

ImageContent is a base64-encoded image in a message.

func (ImageContent) MarshalJSON

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

type ImageModel

type ImageModel struct {
	ID       string
	Name     string
	API      ImageAPI
	Provider string
	BaseURL  string
	Headers  map[string]string
	Input    []string
	// InputLimits carries provider input limits and cache-safe preprocessing metadata.
	InputLimits *ModelInputLimits
	// Output always includes "image"; "text" means the model can also return text blocks.
	Output []string
	Cost   ModelCost
}

ImageModel is an image-generation model. It is usable with Models.GenerateImages only.

func GetImageModel

func GetImageModel(provider string, modelID string) (ImageModel, bool)

GetImageModel returns a generated image model by provider and id.

func GetImageModels

func GetImageModels(provider string) []ImageModel

GetImageModels returns generated image models for provider in stable ID order.

func (*ImageModel) CostRates

func (m *ImageModel) CostRates() ModelCost

CostRates returns the image model's per-million-token prices. A nil model has no price.

func (*ImageModel) ModelID

func (m *ImageModel) ModelID() string

func (*ImageModel) ModelType

func (*ImageModel) ModelType() ModelType

func (*ImageModel) ProviderID

func (m *ImageModel) ProviderID() string

type ImagesAPIProvider

type ImagesAPIProvider struct {
	API            ImageAPI
	GenerateImages ImagesFunction
}

ImagesAPIProvider registers a provider implementation for an ImageAPI.

func GetImagesAPIProvider

func GetImagesAPIProvider(api ImageAPI) (ImagesAPIProvider, bool)

GetImagesAPIProvider returns the registered provider for api.

type ImagesContext

type ImagesContext struct {
	Input []ContentBlock
}

ImagesContext is the input to an image-generation request.

func (ImagesContext) MarshalJSON

func (c ImagesContext) MarshalJSON() ([]byte, error)

MarshalJSON writes the context as {input}.

func (*ImagesContext) UnmarshalJSON

func (c *ImagesContext) UnmarshalJSON(data []byte) error

UnmarshalJSON reads {input}.

type ImagesFunction

ImagesFunction is an image-generation provider function.

type ImagesOptions

type ImagesOptions struct {
	// TelemetryContext parents provider request spans; nil means no recording backend.
	TelemetryContext telemetry.TelemetryContext
	// Fetch replaces HTTP execution without changing the caller's request context or redirect policy.
	Fetch  *http.Client
	APIKey string
	// APIKeySet distinguishes an explicit empty key from an omitted override.
	APIKeySet  bool
	Headers    ProviderHeaders
	Env        map[string]string
	Metadata   map[string]any
	TimeoutMs  int
	MaxRetries int
	OnPayload  func(payload any, model ImageModel) (any, bool, error)
	OnResponse func(response ProviderResponse, model ImageModel) error
}

ImagesOptions configures image-generation provider requests.

type ImagesStopReason

type ImagesStopReason string

ImagesStopReason mirrors upstream ImagesStopReason.

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

type InMemoryAuthStorage

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

InMemoryAuthStorage is a process-local CredentialStore. Mirrors upstream AuthStorage.inMemory; it is a separate type because AuthStorage is bound to its auth.json path.

func NewInMemoryAuthStorage

func NewInMemoryAuthStorage(data map[string]Credential) *InMemoryAuthStorage

NewInMemoryAuthStorage returns a store seeded with a copy of data. Seed keys use lexical order because Go maps have no insertion order; later writes retain their insertion order.

func (*InMemoryAuthStorage) Delete

func (s *InMemoryAuthStorage) Delete(ctx context.Context, providerID string) error

Delete removes a credential in the same cancellable queue as Modify.

func (*InMemoryAuthStorage) List

List implements CredentialStore after preceding store operations settle.

func (*InMemoryAuthStorage) Modify

func (s *InMemoryAuthStorage) Modify(ctx context.Context, providerID string, fn func(current *Credential) (*Credential, error)) (*Credential, error)

Modify serializes callbacks across providers with isolated input and result snapshots. Cancellation releases the caller immediately, but an active callback keeps its queue position until settlement and cannot commit a canceled write.

func (*InMemoryAuthStorage) Read

func (s *InMemoryAuthStorage) Read(ctx context.Context, providerID string) (*Credential, error)

Read returns an isolated credential snapshot after preceding store operations settle. Cancellation releases the caller without bypassing an active mutation.

type InMemoryCredentialStore

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

InMemoryCredentialStore stores literal credentials and serializes mutations per provider. Unlike app AuthStorage, it does not evaluate configuration expressions.

func NewInMemoryCredentialStore

func NewInMemoryCredentialStore() *InMemoryCredentialStore

func (*InMemoryCredentialStore) Delete

func (s *InMemoryCredentialStore) Delete(ctx context.Context, providerID string) error

func (*InMemoryCredentialStore) List

func (*InMemoryCredentialStore) Modify

func (s *InMemoryCredentialStore) Modify(ctx context.Context, providerID string, fn func(*Credential) (*Credential, error)) (*Credential, error)

func (*InMemoryCredentialStore) Read

func (s *InMemoryCredentialStore) Read(ctx context.Context, providerID string) (*Credential, error)

type InMemoryModelsStore

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

InMemoryModelsStore mirrors pi-ai InMemoryModelsStore (and the coding agent's InMemoryCodingAgentModelsStore): a process-local ModelsStore.

func NewInMemoryModelsStore

func NewInMemoryModelsStore() *InMemoryModelsStore

NewInMemoryModelsStore returns an empty in-memory store.

func (*InMemoryModelsStore) Delete

func (s *InMemoryModelsStore) Delete(ctx context.Context, providerID string) error

Delete removes the provider's catalog.

func (*InMemoryModelsStore) Read

func (s *InMemoryModelsStore) Read(ctx context.Context, providerID string) (*ModelsStoreEntry, error)

Read returns a copy of the provider's catalog, or nil when none is stored.

func (*InMemoryModelsStore) Write

func (s *InMemoryModelsStore) Write(ctx context.Context, providerID string, entry ModelsStoreEntry) error

Write stores a copy of the provider's catalog.

type JsonObject

type JsonObject map[string]any

JSONObject is a provider-facing JSON object.

func ParseStreamingJson

func ParseStreamingJson(input string) JsonObject

ParseStreamingJson incrementally reconstructs a streamed tool-argument object. Invalid or incomplete input returns the object prefix parsed so far, or an empty object when no object prefix is available.

type LazyAPICapabilities

type LazyAPICapabilities struct {
	FetchDeferred  bool
	CancelDeferred bool
}

LazyAPICapabilities declares optional methods without loading the implementation.

type LlamaCppLabeledQuestion

type LlamaCppLabeledQuestion struct {
	// Content is the user message: the state, the questions and this question's answer labels.
	Content string
	// Labels are the answer labels the model can emit, in the order of Keys.
	Labels []string
	// Keys are the answer keys each label stands for: choice keys, level indices, or "true" and "false".
	Keys []string
}

LlamaCppLabeledQuestion is one question rendered for the model.

func LlamaCppRenderQuestion

func LlamaCppRenderQuestion(request ClassifierContext, id string) (LlamaCppLabeledQuestion, error)

LlamaCppRenderQuestion writes one question of the request as a user message and picks its labels. It fails for unsupported option counts.

The message is the state, every question of the request with its options, the state again, and then this question with labeled options. A causal model reads the first copy of the state before it knows what is asked; the second copy is read with the questions in view (prompt repetition). Everything before the final question is the same for all questions of a request, so the server's prompt cache evaluates it once.

type LoginOptions

type LoginOptions struct {
	// GetDeviceID returns the stable ID of this app installation, for example sent to OpenAI as its agent host ID.
	// Login flows call it only when they need it, so apps can create the ID on first use; it must return the same ID on
	// every later call. Nil means the app supplies none.
	GetDeviceID func() string
}

LoginOptions is app-supplied context for Models.Login.

type Message

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

Message is the closed provider-facing transcript union.

func TransformMessages

func TransformMessages(messages []Message, model *Model, normalizeToolCallId func(string, *Model, AssistantMessage) string) []Message

TransformMessages prepares history for a target provider without changing retained messages. It preserves valid same-model signatures, normalizes foreign content and paired IDs, drops incomplete assistant turns, and closes unanswered tool calls before the next turn while holding intervening system updates.

func WithoutInitialSystemMessage

func WithoutInitialSystemMessage(messages []Message) []Message

type MistralConfig

type MistralConfig struct {
	// ModelMetadata retains the selected model's input capabilities, identity and thinking map.
	ModelMetadata *Model
	APIKey        string
	Model         string
	ProviderID    string
	BaseURL       string
	ExtraHeaders  map[string]string
	// SessionID for x-affinity header (KV-cache reuse).
	SessionID string
	// Reasoning indicates whether the model supports extended reasoning.
	Reasoning bool
}

MistralConfig configures the Mistral API provider.

type Model

type Model struct {
	// Type is optional: chat is the default model type, so models without it are chat models.
	Type             ModelType
	ID               string
	DisplayName      string
	Provider         Provider
	Capabilities     ModelCapabilities
	ProviderMeta     ProviderMetadata
	Input            []string
	ThinkingLevelMap ThinkingLevelMap
	SamplingParams   map[string]any
	PromptCache      ModelPromptCache
	// InputLimits mirrors upstream Model.inputLimits.
	InputLimits *ModelInputLimits
}

Model pairs an identifier with a provider and capabilities.

func (*Model) CostRates

func (m *Model) CostRates() ModelCost

CostRates returns the model's per-million-token prices in upstream Model.cost shape. A nil model has no price.

func (*Model) ModelID

func (m *Model) ModelID() string

func (*Model) ModelType

func (m *Model) ModelType() ModelType

ModelType returns the explicit Type, or chat for models without one.

func (*Model) ProviderID

func (m *Model) ProviderID() string

type ModelAuth

type ModelAuth struct {
	APIKey  string
	Headers ProviderHeaders
	BaseURL string
}

ModelAuth is request auth for a single model request. Mirrors upstream ModelAuth.

type ModelCapabilities

type ModelCapabilities struct {
	MaxThinking     ThinkingLevel
	SupportsImages  bool
	SupportsToolUse bool
	ContextWindow   int
	MaxOutputTokens int
	InputCostPer1M  float64 // USD per 1M input tokens
	OutputCostPer1M float64 // USD per 1M output tokens
	// CacheReadCostPer1M / CacheWriteCostPer1M are the separate cache rates
	// upstream models expose; the write rate is for 5m (short) writes. 1h
	// writes bill at 2x base input (see CalculateCost).
	CacheReadCostPer1M  float64 // USD per 1M cache-read tokens
	CacheWriteCostPer1M float64 // USD per 1M cache-write tokens
	// CostTiers overrides the base rates for high-volume requests: the highest
	// matching InputTokensAbove threshold applies to the whole request.
	// Mirrors upstream ModelCost.tiers.
	CostTiers []CostTier
}

ModelCapabilities describes what a model supports.

type ModelCatalogProvider

type ModelCatalogProvider struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	Credential string `json:"credential"`
	Source     string `json:"source"`
}

ModelCatalogProvider preserves provider-routing metadata carried by generated catalogs.

type ModelCompat

type ModelCompat = OpenAICompat

ModelCompat is the shared compat bag used by generated model metadata, models.json overrides, and provider configs. It merges the upstream OpenAI-compatible compat fields with the newer Anthropic/OpenAI Responses flags used by specific provider families.

Package-local provider code continues to refer to the API-specific aliases below (OpenAICompat, OpenAIResponsesCompat, AnthropicMessagesCompat), but they all share the same underlying shape so generated metadata and registry merge logic can stay simple. ModelCompat is the shared compat bag used by generated model metadata, the provider configs, and the registry. It is identical to OpenAICompat.

type ModelCost

type ModelCost struct {
	Input      float64    `json:"input"`
	Output     float64    `json:"output"`
	CacheRead  float64    `json:"cacheRead"`
	CacheWrite float64    `json:"cacheWrite"`
	Tiers      []CostTier `json:"tiers,omitempty"`
}

ModelCost is the provider price per million tokens for one model.

type ModelImageInputLimits

type ModelImageInputLimits struct {
	Resize *ModelImageResizeOptions `json:"resize,omitempty"`
	// MaxPerMessage is the maximum number of images in one provider message.
	MaxPerMessage int `json:"maxPerMessage,omitempty"`
	// MaxPerRequest is the maximum number of images across one provider request.
	MaxPerRequest int `json:"maxPerRequest,omitempty"`
}

ModelImageInputLimits describes provider image limits. Zero means omitted.

type ModelImageResizeOptions

type ModelImageResizeOptions struct {
	MaxWidth  int `json:"maxWidth,omitempty"`
	MaxHeight int `json:"maxHeight,omitempty"`
	// MaxBytes limits the base64-encoded image payload.
	MaxBytes    int `json:"maxBytes,omitempty"`
	JPEGQuality int `json:"jpegQuality,omitempty"`
}

ModelImageResizeOptions is the cache-safe resize profile applied before a new image enters conversation history. Upstream values are integers of at least one, so zero means the field is omitted.

type ModelInputLimits

type ModelInputLimits struct {
	// MaxRequestBytes is the maximum serialized provider request size.
	MaxRequestBytes int                    `json:"maxRequestBytes,omitempty"`
	Images          *ModelImageInputLimits `json:"images,omitempty"`
}

ModelInputLimits carries provider input limits and cache-safe preprocessing metadata. Zero numeric fields and nil objects are omitted fields.

func (*ModelInputLimits) Clone

func (l *ModelInputLimits) Clone() *ModelInputLimits

Clone returns a deep copy so catalog, registry, and extension projections never alias mutable limit metadata.

type ModelPromptCache

type ModelPromptCache map[string]int

ModelPromptCache gives the best-effort prompt-cache lifetime in seconds for each retention tier.

type ModelThinkingLevel

type ModelThinkingLevel = ThinkingLevel

type ModelType

type ModelType string

ModelType names what a catalog entry is for. It decides which Models operation accepts the entry.

const (
	ModelTypeChat       ModelType = "chat"
	ModelTypeImage      ModelType = "image"
	ModelTypeClassifier ModelType = "classifier"
)

func GetModelType

func GetModelType(model AnyModel) ModelType

GetModelType returns the type of a model. Chat models without an explicit Type are chat models.

type Models

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

Models owns provider selection, request auth and refresh publication. Callers own operation contexts; callbacks must settle before their owners are discarded.

func BuiltinModels

func BuiltinModels(options ...CreateModelsOptions) *Models

BuiltinModels returns a Models collection with every built-in provider registered.

func CreateModels

func CreateModels(options ...CreateModelsOptions) *Models

func CreateModelsWithModelAuth

func CreateModelsWithModelAuth(modelAuth func(ctx context.Context, model *Model, overrides AuthResolutionOverrides) (*AuthResult, error), options ...CreateModelsOptions) *Models

CreateModelsWithModelAuth supplies model-level authentication for GetModelAuth and every request. The callback must resolve provider auth with GetAuth, not GetModelAuth, to avoid recursion.

func (*Models) CancelDeferred

func (m *Models) CancelDeferred(ctx context.Context, model *Model, handle DeferredHandle, options ...DeferredCancelOptions) error

func (*Models) CheckAuth

func (m *Models) CheckAuth(ctx context.Context, providerID string) (*AuthCheck, error)

func (*Models) Classify

func (m *Models) Classify(ctx context.Context, model *ClassifierModel, request ClassifierContext, options ...ModelsClassifierOptions) ClassifierResult

Classify classifies structured state through the owning provider with auth resolved like Stream. It never returns an error: failures and cancellation are error or aborted results. Ports Models.classify (.upstream/v0.99.1/packages/ai/src/models.ts:966).

func (*Models) ClearProviders

func (m *Models) ClearProviders()

func (*Models) Close

func (m *Models) Close()

Close cancels active refreshes and joins collection-owned operations. Completed refresh signals keep their existing lifetime semantics.

func (*Models) Complete

func (m *Models) Complete(ctx context.Context, model *Model, request Context, options ...StreamOptions) *AssistantMessage

func (*Models) CompleteSimple

func (m *Models) CompleteSimple(ctx context.Context, model *Model, request Context, options ...StreamOptions) *AssistantMessage

func (*Models) DeleteProvider

func (m *Models) DeleteProvider(id string)

func (*Models) FetchDeferred

func (m *Models) FetchDeferred(ctx context.Context, model *Model, handle DeferredHandle, options ...DeferredFetchOptions) *AssistantMessage

func (*Models) GenerateImages

func (m *Models) GenerateImages(ctx context.Context, model *ImageModel, request ImagesContext, options ...ModelsImagesOptions) AssistantImages

GenerateImages generates images through the owning provider with auth resolved like Stream. It never returns an error: unknown providers, unconfigured auth, cancellation and providers without image generation become an error or aborted AssistantImages.

func (*Models) GetAllAvailable

func (m *Models) GetAllAvailable(ctx context.Context, providerID ...string) ([]AnyModel, error)

GetAllAvailable returns models of every type whose providers have complete auth configuration. Without FilterAllModels, FilterModels applies to chat models and every other model is kept.

func (*Models) GetAllModels

func (m *Models) GetAllModels(provider ...string) []AnyModel

GetAllModels reads the last-known models of every type from one provider or all providers. A provider whose listing fails contributes no models.

func (*Models) GetAuth

func (m *Models) GetAuth(ctx context.Context, providerID string, overrides ...AuthResolutionOverrides) (*AuthResult, error)

func (*Models) GetAvailable

func (m *Models) GetAvailable(ctx context.Context, providerID ...string) ([]*Model, error)

func (*Models) GetAvailableOfType

func (m *Models) GetAvailableOfType(ctx context.Context, modelType ModelType, providerID ...string) ([]AnyModel, error)

GetAvailableOfType returns models of one type whose providers have complete auth configuration.

func (*Models) GetModel

func (m *Models) GetModel(provider, id string) *Model

func (*Models) GetModelAuth

func (m *Models) GetModelAuth(ctx context.Context, anyModel AnyModel, overrides ...AuthResolutionOverrides) (*AuthResult, error)

func (*Models) GetModelOfType

func (m *Models) GetModelOfType(modelType ModelType, provider, id string) AnyModel

GetModelOfType looks up one model of the given type in the last-known lists, or returns nil.

func (*Models) GetModels

func (m *Models) GetModels(provider ...string) []*Model

func (*Models) GetModelsOfType

func (m *Models) GetModelsOfType(modelType ModelType, provider ...string) []AnyModel

GetModelsOfType reads the last-known models of one type from one provider or all providers.

func (*Models) GetProvider

func (m *Models) GetProvider(id string) *ModelsProvider

func (*Models) GetProviders

func (m *Models) GetProviders() []*ModelsProvider

func (*Models) Login

func (m *Models) Login(ctx context.Context, providerID string, authType AuthType, interaction AuthInteraction, options ...LoginOptions) (Credential, error)

Login runs a provider-owned login flow and persists its returned credential.

func (*Models) Logout

func (m *Models) Logout(ctx context.Context, providerID string) error

func (*Models) Refresh

func (m *Models) Refresh(ctx context.Context, options ...ModelsRefreshOptions) ModelsRefreshResult

func (*Models) SetProvider

func (m *Models) SetProvider(provider *ModelsProvider)

func (*Models) Stream

func (m *Models) Stream(ctx context.Context, model *Model, request Context, options ...StreamOptions) *AssistantMessageEventStream

func (*Models) StreamDeferred

func (m *Models) StreamDeferred(ctx context.Context, model *Model, handle DeferredHandle, options ...DeferredFetchOptions) *AssistantMessageEventStream

func (*Models) StreamSimple

func (m *Models) StreamSimple(ctx context.Context, model *Model, request Context, options ...StreamOptions) *AssistantMessageEventStream

type ModelsClassifierOptions

type ModelsClassifierOptions struct {
	ClassifierOptions
	// TransformHeaders runs once after configured, auth, and request headers are merged.
	TransformHeaders func(context.Context, ProviderHeaders) (ProviderHeaders, error)
}

ModelsClassifierOptions is ClassifierOptions plus the per-request header transform Models applies.

type ModelsError

type ModelsError struct {
	Code    ModelsErrorCode
	Message string
	Cause   error
}

ModelsError is a coded model-collection failure. Its message carries the cause's detail because callers surface only the message.

func NewModelsError

func NewModelsError(code ModelsErrorCode, message string, cause error) *ModelsError

NewModelsError mirrors the upstream ModelsError constructor, including ECMAScript trimming of the cause detail.

func (*ModelsError) Error

func (e *ModelsError) Error() string

func (*ModelsError) Unwrap

func (e *ModelsError) Unwrap() error

type ModelsErrorCode

type ModelsErrorCode string

ModelsErrorCode mirrors upstream ModelsErrorCode.

const (
	ModelsErrorModelSource     ModelsErrorCode = "model_source"
	ModelsErrorModelValidation ModelsErrorCode = "model_validation"
	ModelsErrorProvider        ModelsErrorCode = "provider"
	ModelsErrorStream          ModelsErrorCode = "stream"
	ModelsErrorAuth            ModelsErrorCode = "auth"
	ModelsErrorOAuth           ModelsErrorCode = "oauth"
)

type ModelsImagesOptions

type ModelsImagesOptions struct {
	ImagesOptions
	// TransformHeaders runs once after configured, auth, and request headers are merged.
	TransformHeaders func(context.Context, ProviderHeaders) (ProviderHeaders, error)
}

ModelsImagesOptions is ImagesOptions plus the per-request header transform Models applies.

type ModelsProvider

type ModelsProvider struct {
	ID            string
	Name          string
	BaseURL       string
	Headers       ProviderHeaders
	Auth          ProviderAuth
	GetModels     func() ([]*Model, error)
	RefreshModels func(RefreshModelsContext) error
	FilterModels  func([]*Model, *Credential) []*Model
	// GetAllModels lists models of every type. Providers with only chat models may leave it nil.
	GetAllModels func() ([]AnyModel, error)
	// FilterAllModels is the credential-specific availability policy across every model type.
	FilterAllModels func([]AnyModel, *Credential) []AnyModel
	// GenerateImages is present when the provider supports image models.
	GenerateImages func(context.Context, *ImageModel, ImagesContext, ImagesOptions) (AssistantImages, error)
	// Classify is present when the provider supports classifier models.
	Classify       func(context.Context, *ClassifierModel, ClassifierContext, ClassifierOptions) (ClassifierResult, error)
	Stream         ModelsStreamFunction
	StreamSimple   ModelsStreamFunction
	FetchDeferred  func(context.Context, *Model, DeferredHandle, DeferredFetchOptions) (*AssistantMessageEventStream, error)
	CancelDeferred func(context.Context, *Model, DeferredHandle, DeferredCancelOptions) error
}

ModelsProvider is the provider-owned runtime unit from models.ts; Provider remains the existing bound-backend streaming contract.

func BuiltinProviders

func BuiltinProviders() []*ModelsProvider

BuiltinProviders returns every built-in provider, freshly constructed.

func CloudflareWorkersAIProvider

func CloudflareWorkersAIProvider() *ModelsProvider

CloudflareWorkersAIProvider is the Cloudflare Workers AI provider.

func CreateProvider

func CreateProvider(input CreateProviderOptions) *ModelsProvider

CreateProvider merges a dynamic overlay over its baseline and routes each request through the model's API implementation. It panics when no chat, image or classifier implementation is given.

func TypesafeProvider

func TypesafeProvider() *ModelsProvider

TypesafeProvider is the TypeSafe provider: classifier models only.

type ModelsPublication

type ModelsPublication struct {
	Persist *ModelsStoreEntry
	// PersistSet with a nil Persist is an explicit null publication, which deletes storage.
	PersistSet bool
	Update     func()
}

type ModelsRefreshOptions

type ModelsRefreshOptions struct {
	AllowNetwork *bool
	Providers    []string
	Force        *bool
}

type ModelsRefreshResult

type ModelsRefreshResult struct {
	Aborted    bool
	Errors     map[string]error
	ErrorOrder []string
}

type ModelsStore

type ModelsStore interface {
	Read(ctx context.Context, providerID string) (*ModelsStoreEntry, error)
	Write(ctx context.Context, providerID string, entry ModelsStoreEntry) error
	Delete(ctx context.Context, providerID string) error
}

ModelsStore mirrors pi-ai ModelsStore: persistent model catalogs keyed by provider ID. A cancelled ctx plays the role of options.signal.

type ModelsStoreEntry

type ModelsStoreEntry struct {
	Models []json.RawMessage `json:"models"`
	// LastModified is the Unix timestamp from the remote catalog's
	// Last-Modified header.
	LastModified *float64 `json:"lastModified,omitempty"`
	// CheckedAt is the Unix timestamp of the last completed remote check.
	CheckedAt *float64 `json:"checkedAt,omitempty"`
	// ETag is the remote catalog's opaque validator, stored verbatim.
	ETag string `json:"etag,omitempty"`
}

ModelsStoreEntry mirrors pi-ai ModelsStoreEntry. Models stay raw JSON because each provider owns the shape of the catalog it persists.

func (ModelsStoreEntry) Clone

func (entry ModelsStoreEntry) Clone() ModelsStoreEntry

Clone returns a deep copy, mirroring upstream's structuredClone at the store boundary.

type MutableModels

type MutableModels = Models

type NestedToolCallRecord

type NestedToolCallRecord struct {
	ID     string               `json:"id"`
	Name   string               `json:"name"`
	Status NestedToolCallStatus `json:"status"`
	// Arguments is omitted when over the size limits.
	Arguments JsonObject `json:"arguments,omitempty"`
	// ArgumentsBytes is the UTF-8 size of the arguments as JSON, set when Arguments is omitted.
	ArgumentsBytes *int   `json:"argumentsBytes,omitempty"`
	DurationMs     *int64 `json:"durationMs,omitempty"`
	// Error is the error text, truncated.
	Error string `json:"error,omitempty"`
	// contains filtered or unexported fields
}

NestedToolCallRecord is a tool call that another tool made while it ran, for example from a codemode script. Arguments is nil when it was over the size limits; ArgumentsBytes then gives their size.

upstream: .upstream/v0.99.1/packages/ai/src/types.ts:573-586 (NestedToolCallRecord)

func (NestedToolCallRecord) MarshalJSON

func (record NestedToolCallRecord) MarshalJSON() ([]byte, error)

MarshalJSON keeps an empty `arguments` object, which differs from omitted arguments (over the size limits): only a nil Arguments is left out.

func (*NestedToolCallRecord) SetArgumentsJSON

func (record *NestedToolCallRecord) SetArgumentsJSON(raw []byte) error

SetArgumentsJSON decodes a complete JSON object into Arguments and keeps its member order.

func (*NestedToolCallRecord) UnmarshalJSON

func (record *NestedToolCallRecord) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes the record and keeps the member order of its arguments.

type NestedToolCallStatus

type NestedToolCallStatus string

NestedToolCallStatus is the state of a NestedToolCallRecord. `unfinished` means the call was still running when the calling tool finished.

upstream: .upstream/v0.99.1/packages/ai/src/types.ts:573-586 (NestedToolCallRecord.status)

const (
	NestedToolCallOK         NestedToolCallStatus = "ok"
	NestedToolCallError      NestedToolCallStatus = "error"
	NestedToolCallUnfinished NestedToolCallStatus = "unfinished"
)

The statuses of upstream's NestedToolCallRecord.status union.

type NestedToolCalls

type NestedToolCalls struct {
	Calls    []NestedToolCallRecord `json:"calls"`
	Complete bool                   `json:"complete"`
}

NestedToolCalls is the bounded record of the nested calls a tool made. Results are not recorded. Complete is false when calls were dropped, arguments omitted, or calls had not finished.

upstream: .upstream/v0.99.1/packages/ai/src/types.ts:588-592 (NestedToolCalls)

type NormalizedProviderError

type NormalizedProviderError struct {
	Status             *int    `json:"status,omitempty"`
	Body               *string `json:"body,omitempty"`
	Message            string  `json:"message"`
	MessageCarriesBody bool    `json:"messageCarriesBody"`
}

NormalizedProviderError separates a provider's HTTP status and body from its error message.

func NormalizeProviderError

func NormalizeProviderError(value any) NormalizedProviderError

NormalizeProviderError extracts supported error metadata without serializing unread response streams or SDK wrapper objects.

type OAuthAuth

type OAuthAuth struct {
	Name           string
	IsSubscription bool
	LoginLabel     string
	Login          func(context.Context, AuthInteraction, LoginOptions) (Credential, error)
	Refresh        func(ctx context.Context, credential Credential) (Credential, error)
	ToAuth         func(credential Credential) (ModelAuth, error)
}

OAuthAuth refreshes stored OAuth credentials and derives request auth from them.

func OAuthProviderAuth

func OAuthProviderAuth(providerID string) (*OAuthAuth, bool)

OAuthProviderAuth returns the OAuth auth method for a provider registered in the OAuth provider registry.

type OAuthAuthInfo

type OAuthAuthInfo struct {
	URL          string
	Instructions string
}

OAuthAuthInfo is the URL + instructions presented to the user.

type OAuthCallbackOrManualInput

type OAuthCallbackOrManualInput[T any] struct {
	// Callback is true when the browser callback completed the sign-in.
	Callback bool
	Value    T
	Input    string
}

OAuthCallbackOrManualInput is the outcome of WaitForCallbackOrManualInput: the callback result or the pasted input.

func WaitForCallbackOrManualInput

func WaitForCallbackOrManualInput[T any](ctx context.Context, interaction AuthInteraction, callback *OAuthCallbackServer[T], prompt AuthManualCodePrompt) (OAuthCallbackOrManualInput[T], error)

WaitForCallbackOrManualInput waits for the browser callback, or for the user to paste the code or redirect URL when the browser cannot reach the loopback server (for example over SSH). Without a callback server only the manual prompt is used. The manual prompt's context ends, and the prompt is joined, before it returns.

type OAuthCallbackServer

type OAuthCallbackServer[T any] struct {
	// RedirectURI is the URI the provider redirects the browser to.
	RedirectURI string
	// contains filtered or unexported fields
}

OAuthCallbackServer waits for one browser redirect.

func StartOAuthCallbackServer

func StartOAuthCallbackServer[T any](ctx context.Context, options OAuthCallbackServerOptions[T]) (*OAuthCallbackServer[T], error)

StartOAuthCallbackServer listens for the provider's redirect. Cancelling ctx ends the wait with "Login cancelled".

func (*OAuthCallbackServer[T]) Cancel

func (s *OAuthCallbackServer[T]) Cancel()

Cancel stops waiting for the browser unless a callback is already being completed.

func (*OAuthCallbackServer[T]) Close

func (s *OAuthCallbackServer[T]) Close()

Close fails a pending wait, stops the listener and drops every connection (callback-server.ts:145-148). Node's server.close() also closes idle connections, so a browser's spare connection cannot keep the server alive past its login; http.Server.Close additionally ends a connection whose Complete is still running.

func (*OAuthCallbackServer[T]) Wait

func (s *OAuthCallbackServer[T]) Wait() (value T, ok bool, err error)

Wait returns the result of Complete, or ok=false after Cancel. It fails when the provider redirects with an error, Complete fails, the context ends or the timeout elapses.

type OAuthCallbackServerOptions

type OAuthCallbackServerOptions[T any] struct {
	// ProviderName is used on the browser page, for example "OpenAI".
	ProviderName string
	// Host is the address to listen on.
	Host string
	// Port is the port to listen on; 0 picks a free port.
	Port int
	Path string
	// RedirectHost is the host in the redirect URI when it differs from Host, for example "localhost".
	RedirectHost string
	// State is the expected state parameter. Nil skips the check for providers that send none.
	State *string
	// Complete finishes the sign-in with the received code before the browser page is sent, so the page can show
	// exchange failures. Its context is the one passed to StartOAuthCallbackServer.
	Complete func(ctx context.Context, code string) (T, error)
	// Timeout ends the wait with an error. Zero means no timeout.
	Timeout time.Duration
}

OAuthCallbackServerOptions configures the loopback redirect handler shared by the browser sign-in flows.

type OAuthCredentialStatus

type OAuthCredentialStatus struct {
	AuthType string
	Source   string
}

OAuthCredentialStatus describes a stored credential owned by a registered OAuth provider outside auth.json. AuthType is "oauth" or "api_key" and Source is rendered by the login selector (for example "stored").

type OAuthCredentialStore

type OAuthCredentialStore interface {
	OAuthCredentialStatus() (OAuthCredentialStatus, bool)
	StoreOAuthCredentials(creds OAuthCredentials) (path string, err error)
	DeleteOAuthCredentials() (deleted bool, err error)
}

OAuthCredentialStore is an optional extension hook for OAuth providers whose credentials live outside auth.json. Built-in upstream providers do not need it; product extensions can implement it to keep their existing credential stores while still participating in the generic /login and /logout surfaces.

type OAuthCredentials

type OAuthCredentials struct {
	Extra   map[string]json.RawMessage `json:"-"`
	Refresh string                     `json:"refresh"`
	Access  string                     `json:"access"`
	// Expires is the truncated integer projection of expires in Unix milliseconds. ExpiresMillis returns Pi's exact JavaScript number, including a fraction or an absent property.
	// Expiry decisions must use ExpiresMillis. SetExpiresMillis stores a fraction; assigning Expires replaces any retained exact value.
	Expires int64 `json:"expires"`
	// ProjectID is used by Google Cloud Code Assist / Antigravity OAuth.
	// Other provider-owned fields are retained in Extra.
	ProjectID string `json:"projectId,omitempty"`
	// AccountID is the provider account associated with the access token.
	AccountID string `json:"accountId,omitempty"`
	// Scope is the granted scope returned by flows that report one (Radius).
	Scope string `json:"scope,omitempty"`
	// contains filtered or unexported fields
}

OAuthCredentials holds the tokens returned by an OAuth flow.

func GetOAuthAPIKey

func GetOAuthAPIKey(providerID string, credentials map[string]OAuthCredentials) (*OAuthCredentials, string, error)

GetOAuthAPIKey mirrors upstream getOAuthApiKey(): resolve an OAuth-backed API key, refreshing the credential if expired.

func GetOAuthAPIKeyContext

func GetOAuthAPIKeyContext(ctx context.Context, providerID string, credentials map[string]OAuthCredentials) (*OAuthCredentials, string, error)

GetOAuthAPIKeyContext resolves an OAuth-backed API key with the owning operation's cancellation.

func LoginAnthropic

func LoginAnthropic(ctx context.Context, callbacks OAuthLoginCallbacks) (OAuthCredentials, error)

LoginAnthropic runs the Anthropic OAuth authorization code + PKCE flow. The browser callback and the manual prompt race; the callback server closes, and the manual prompt is joined, before the token exchange result returns. Ports loginAnthropic (.upstream/v1.0.0/packages/ai/src/auth/oauth/anthropic.ts:138).

func LoginAnthropicCopyCode

func LoginAnthropicCopyCode(ctx context.Context, callbacks OAuthLoginCallbacks) (OAuthCredentials, error)

LoginAnthropicCopyCode runs the Anthropic authorization code + PKCE flow without a callback server: Anthropic's page shows the code, which the user pastes. It works when the browser runs on another machine. Ports loginAnthropicCopyCode (.upstream/v1.0.0/packages/ai/src/auth/oauth/anthropic.ts:191).

func LoginOpenAIChatGPT

func LoginOpenAIChatGPT(ctx context.Context, callbacks OAuthLoginCallbacks) (OAuthCredentials, error)

LoginOpenAIChatGPT runs the Sign in with ChatGPT flow: the browser callback and the pasted redirect URL race, then the authorization code is exchanged for tokens. callbacks.GetDeviceID supplies the installation's device ID.

func LoginOpenAICodex

func LoginOpenAICodex(ctx context.Context, callbacks OAuthLoginCallbacks) (OAuthCredentials, error)

LoginOpenAICodex runs the OpenAI Codex (ChatGPT) OAuth flow. Port 1455 is shared with the Codex CLI; when it is taken, the pasted redirect URL completes the login. Ports loginOpenAICodex (.upstream/v0.99.1/packages/ai/src/auth/oauth/openai-codex.ts:359).

func LoginOpenAICodexDeviceCode

func LoginOpenAICodexDeviceCode(ctx context.Context, onDeviceCode func(OAuthDeviceCodeInfo)) (OAuthCredentials, error)

LoginOpenAICodexDeviceCode runs the Codex device-code login: request a user code, surface it via onDeviceCode, poll for approval, then exchange the authorization code for credentials. Mirrors loginOpenAICodexDeviceCode (openai-codex.ts:433-451).

func LoginOpenRouter

func LoginOpenRouter(ctx context.Context, callbacks OAuthLoginCallbacks) (OAuthCredentials, error)

LoginOpenRouter runs the OpenRouter OAuth PKCE flow: a loopback callback raced against a manual paste. OpenRouter sends no state; the random path keeps stray requests from completing the sign-in. A claimed browser callback always wins over a racing paste. Ports loginOpenRouter (.upstream/v0.99.1/packages/ai/src/auth/oauth/openrouter.ts:113).

func RefreshAnthropicToken

func RefreshAnthropicToken(ctx context.Context, refreshToken string) (OAuthCredentials, error)

RefreshAnthropicToken refreshes an Anthropic OAuth token.

func RefreshCodexToken

func RefreshCodexToken(ctx context.Context, refreshToken string) (OAuthCredentials, error)

RefreshCodexToken refreshes an OpenAI Codex OAuth token. Mirrors upstream refreshAccessToken in openai-codex.ts:133-180.

func RefreshOpenAIChatGPTToken

func RefreshOpenAIChatGPTToken(ctx context.Context, credentials OAuthCredentials) (OAuthCredentials, error)

RefreshOpenAIChatGPTToken refreshes a Sign in with ChatGPT credential with the client ID OpenAI issued at login.

func (*OAuthCredentials) ClearExpires

func (c *OAuthCredentials) ClearExpires()

ClearExpires removes the expires property, as a provider result without one does in Pi.

func (OAuthCredentials) ExpiresMillis

func (c OAuthCredentials) ExpiresMillis() float64

ExpiresMillis returns ToNumber of the credential's expires value, the operand Pi compares with Date.now(). An absent value is NaN, so both orderings compare false.

func (OAuthCredentials) HasExpires

func (c OAuthCredentials) HasExpires() bool

HasExpires reports whether the credential carries an expires property.

func (OAuthCredentials) MarshalJSON

func (c OAuthCredentials) MarshalJSON() ([]byte, error)

func (*OAuthCredentials) SetExpiresMillis

func (c *OAuthCredentials) SetExpiresMillis(value float64)

SetExpiresMillis stores a JavaScript number, including a fraction. Expires receives its truncated projection.

func (*OAuthCredentials) UnmarshalJSON

func (c *OAuthCredentials) UnmarshalJSON(data []byte) error

type OAuthDeviceCodeInfo

type OAuthDeviceCodeInfo struct {
	UserCode         string
	VerificationURI  string
	IntervalSeconds  float64
	ExpiresInSeconds float64
}

OAuthDeviceCodeInfo is the user code + verification URL presented during a device-code (RFC 8628) login flow. Mirrors upstream OAuthDeviceCodeInfo (types.ts:26-31).

type OAuthLoginCallbacks

type OAuthLoginCallbacks struct {
	OnAuth          func(info OAuthAuthInfo)
	OnDeviceCode    func(info OAuthDeviceCodeInfo)
	OnPrompt        func(prompt OAuthPrompt) (string, error)
	OnPromptContext func(context.Context, OAuthPrompt) (string, error)
	OnProgress      func(message string)
	// OnInfo reports an informational notice that is neither progress nor an authorization URL.
	OnInfo                   func(message string)
	OnManualCodeInput        func() (string, error)
	OnManualCodeInputContext func(context.Context) (string, error)
	// OnManualCodePromptContext asks for a pasted code with the flow's own manual_code message and placeholder. Nil falls back to OnManualCodeInputContext, then OnManualCodeInput.
	OnManualCodePromptContext func(context.Context, AuthManualCodePrompt) (string, error)
	OnSelect                  func(prompt OAuthSelectPrompt) (string, error)
	OnSelectContext           func(context.Context, OAuthSelectPrompt) (string, error)
	// GetDeviceID returns the stable ID of this app installation. It comes from LoginOptions.GetDeviceID; nil means the
	// app supplies none.
	GetDeviceID func() string
}

OAuthLoginCallbacks groups the callbacks used during an OAuth login flow.

type OAuthPrompt

type OAuthPrompt struct {
	Message     string
	Placeholder string
	AllowEmpty  bool
}

OAuthPrompt describes an interactive prompt during the OAuth flow.

type OAuthProviderInterface

type OAuthProviderInterface interface {
	// ID returns the provider identifier (e.g. "anthropic").
	ID() string
	// Name returns the human-readable provider name.
	Name() string
	// UsesCallbackServer returns true if the flow uses a local HTTP callback.
	UsesCallbackServer() bool
	// Login runs the OAuth authorization flow.
	Login(callbacks OAuthLoginCallbacks) (OAuthCredentials, error)
	// RefreshToken refreshes expired credentials.
	RefreshToken(creds OAuthCredentials) (OAuthCredentials, error)
	// GetAPIKey extracts the bearer token from credentials.
	GetAPIKey(creds OAuthCredentials) string
}

OAuthProviderInterface is the contract for an OAuth provider.

func GetOAuthProvider

func GetOAuthProvider(id string) (OAuthProviderInterface, bool)

GetOAuthProvider returns an OAuth provider by id (custom first, then built-in).

func GetOAuthProviders

func GetOAuthProviders() []OAuthProviderInterface

GetOAuthProviders returns all OAuth providers (built-in + custom).

type OAuthSelectOption

type OAuthSelectOption struct {
	ID    string
	Label string
}

OAuthSelectOption is one selectable choice in an OAuth flow.

type OAuthSelectPrompt

type OAuthSelectPrompt struct {
	Message string
	Options []OAuthSelectOption
}

OAuthSelectPrompt describes an interactive selection prompt during the OAuth flow.

type OAuthSubscriptionProvider

type OAuthSubscriptionProvider interface {
	IsSubscription() bool
}

OAuthSubscriptionProvider marks an OAuth login billed by a subscription. Mirrors upstream OAuth provider isSubscription.

type OpenAIChatGPTOAuthProvider

type OpenAIChatGPTOAuthProvider struct{}

OpenAIChatGPTOAuthProvider implements OAuthProviderInterface for Sign in with ChatGPT on the openai provider.

func (OpenAIChatGPTOAuthProvider) GetAPIKey

func (OpenAIChatGPTOAuthProvider) ID

func (OpenAIChatGPTOAuthProvider) IsSubscription

func (OpenAIChatGPTOAuthProvider) IsSubscription() bool

func (OpenAIChatGPTOAuthProvider) Login

func (OpenAIChatGPTOAuthProvider) LoginContext

func (OpenAIChatGPTOAuthProvider) Name

func (OpenAIChatGPTOAuthProvider) RefreshToken

func (OpenAIChatGPTOAuthProvider) RefreshTokenContext

func (OpenAIChatGPTOAuthProvider) UsesCallbackServer

func (OpenAIChatGPTOAuthProvider) UsesCallbackServer() bool

type OpenAICodexResponsesConfig

type OpenAICodexResponsesConfig struct {
	APIKey        string
	Model         string
	ModelMetadata *Model
	// ThinkingLevelMap supplies an explicit map when selected ModelMetadata is absent.
	ThinkingLevelMap ThinkingLevelMap
	ProviderID       string
	BaseURL          string
	Compat           *OpenAIResponsesCompat
}

OpenAICodexResponsesConfig mirrors the upstream OpenAI Codex Responses provider (providers/openai-codex-responses.ts). Codex uses the same Responses wire format as OpenAI but with a different base URL, auth scheme (Bearer JWT), and endpoint path (/codex/responses).

Codex uses SSE when explicitly selected. Its default auto transport prefers a session-scoped WebSocket and falls back to SSE before output starts.

type OpenAICodexWebSocketDebugStats

type OpenAICodexWebSocketDebugStats struct {
	Requests                int
	ConnectionsCreated      int
	ConnectionsReused       int
	CachedContextRequests   int
	StoreTrueRequests       int
	FullContextRequests     int
	DeltaRequests           int
	LastInputItems          int
	LastDeltaInputItems     *int
	LastPreviousResponseId  string
	WebSocketFailures       int
	SSEFallbacks            int
	WebSocketFallbackActive bool
	LastWebSocketError      string
}

OpenAICodexWebSocketDebugStats reports connection and continuation behavior for one logical Codex session.

func GetOpenAICodexWebSocketDebugStats

func GetOpenAICodexWebSocketDebugStats(sessionID string) *OpenAICodexWebSocketDebugStats

GetOpenAICodexWebSocketDebugStats returns a copy of one session's counters.

type OpenAICompat

type OpenAICompat struct {
	SupportsDeveloperRole                       *bool  `json:"supportsDeveloperRole,omitempty"`
	SupportsReasoningEffort                     *bool  `json:"supportsReasoningEffort,omitempty"`
	SupportsStore                               *bool  `json:"supportsStore,omitempty"`
	SupportsUsageInStreaming                    *bool  `json:"supportsUsageInStreaming,omitempty"`
	SupportsFinishReason                        *bool  `json:"supportsFinishReason,omitempty"`
	MaxTokensField                              string `json:"maxTokensField,omitempty"`
	RequiresToolResultName                      *bool  `json:"requiresToolResultName,omitempty"`
	RequiresAssistantAfterToolResult            *bool  `json:"requiresAssistantAfterToolResult,omitempty"`
	RequiresThinkingAsText                      *bool  `json:"requiresThinkingAsText,omitempty"`
	RequiresReasoningContentOnAssistantMessages *bool  `json:"requiresReasoningContentOnAssistantMessages,omitempty"`
	SupportsOpenAIGrammarTools                  *bool  `json:"supportsOpenAIGrammarTools,omitempty"`
	SupportsExplicitPromptCacheMode             *bool  `json:"supportsExplicitPromptCacheMode,omitempty"`
	SupportsStrictTools                         *bool  `json:"supportsStrictTools,omitempty"`
	// ThinkingFormat controls how reasoning/thinking content is sent to the provider.
	// Values: "openai" (default), "deepseek", "zai", "qwen", "openrouter".
	// - "openai": standard reasoning_effort parameter
	// - "deepseek": thinking: {type:"enabled"|"disabled"} plus reasoning_effort
	// - "zai": zai-specific thinking format
	// - "qwen": QwenThinkingType with content_type: "thinking"
	// - "openrouter": openrouter reasoning with include_reasoning: true
	ThinkingFormat string `json:"thinkingFormat,omitempty"`
	// CacheControlFormat selects cache markers on the first instruction's last text part, last tool definition, and last user/assistant/tool text part.
	// Values: "" (none), "anthropic".
	CacheControlFormat         string `json:"cacheControlFormat,omitempty"`
	SendSessionAffinityHeaders *bool  `json:"sendSessionAffinityHeaders,omitempty"`
	SupportsStrictMode         *bool  `json:"supportsStrictMode,omitempty"`
	// OpenRouterRouting contains OpenRouter-specific routing config.
	OpenRouterRouting  map[string]any `json:"openRouterRouting,omitempty"`
	ChatTemplateKwargs map[string]any `json:"chatTemplateKwargs,omitempty"`
	VLLMPriority       *float64       `json:"vllmPriority,omitempty"`
	// VercelGatewayRouting contains Vercel gateway routing config.
	VercelGatewayRouting            map[string]any `json:"vercelGatewayRouting,omitempty"`
	SupportsLongCacheRetention      *bool          `json:"supportsLongCacheRetention,omitempty"`
	SendSessionIdHeader             *bool          `json:"sendSessionIdHeader,omitempty"`
	SupportsEagerToolInputStreaming *bool          `json:"supportsEagerToolInputStreaming,omitempty"`
	SupportsCacheControlOnTools     *bool          `json:"supportsCacheControlOnTools,omitempty"`
	// ForceAdaptiveThinking forces adaptive thinking (type: "adaptive"
	// + output_config.effort) regardless of model ID. Built-in models
	// set this in generated metadata. Custom Anthropic-compatible
	// providers can set it for any model requiring adaptive format.
	ForceAdaptiveThinking *bool `json:"forceAdaptiveThinking,omitempty"`
	// AllowEmptySignature preserves thinking blocks with empty signatures
	// instead of converting them to text. For Anthropic-compatible
	// providers that return empty thinking signatures.
	AllowEmptySignature *bool `json:"allowEmptySignature,omitempty"`
	// SupportsTemperature indicates whether the model accepts temperature.
	// Claude Opus 4.7+ rejects non-default temperature values.
	SupportsTemperature *bool `json:"supportsTemperature,omitempty"`
	ZaiToolStream       *bool `json:"zaiToolStream,omitempty"`
	// DeferredToolsMode selects provider-specific deferred-tool
	// serialization on OpenAI Completions. Only "kimi" is defined
	// (Moonshot Kimi). Empty = no deferred-tool handling.
	DeferredToolsMode string `json:"deferredToolsMode,omitempty"`
	// SessionAffinityFormat selects which session-affinity headers are sent
	// from options.SessionID. "openai" sends session_id + x-client-request-id
	// + x-session-affinity (Completions) / session_id + x-client-request-id
	// (Responses); "openai-nosession" omits session_id; "openrouter" sends
	// x-session-id. Empty = auto-detect (openrouter base URL => "openrouter",
	// else "openai"). Replaces the pre-0.81 sendSessionIdHeader flag.
	SessionAffinityFormat SessionAffinityFormat `json:"sessionAffinityFormat,omitempty"`
	// SupportsToolSearch indicates the model supports client-executed tool
	// search for deferred tools (OpenAI Responses). Default: false.
	SupportsToolSearch      *bool `json:"supportsToolSearch,omitempty"`
	SupportsMaxOutputTokens *bool `json:"supportsMaxOutputTokens,omitempty"`
	// SupportsToolReferences indicates the provider supports deferred tools
	// loaded by tool_reference blocks in tool results (Anthropic Messages).
	// Default: true for first-party Anthropic except Haiku / pre-Claude-4.5.
	// ChatTemplateArgs are provider-specific vLLM/Baseten chat-template values.
	ChatTemplateArgs            map[string]any `json:"chatTemplateArgs,omitempty"`
	SupportsThinkingTokenBudget *bool          `json:"supportsThinkingTokenBudget,omitempty"`
	// ThinkingTokenBudgetField selects the budget wire field and takes precedence over SupportsThinkingTokenBudget.
	ThinkingTokenBudgetField       string                          `json:"thinkingTokenBudgetField,omitempty"`
	SupportsAdditionalTools        *bool                           `json:"supportsAdditionalTools,omitempty"`
	SupportsMidConvoEffort         *bool                           `json:"supportsMidConvoEffort,omitempty"`
	SupportsMidConvoSystemMessages *bool                           `json:"supportsMidConvoSystemMessages,omitempty"`
	SupportsMidConvoToolAdditions  *bool                           `json:"supportsMidConvoToolAdditions,omitempty"`
	SupportsMidConvoToolChanges    *bool                           `json:"supportsMidConvoToolChanges,omitempty"`
	AllowedFallbackModels          []AnthropicAllowedFallbackModel `json:"allowedFallbackModels,omitempty"`
}

OpenAICompat controls behavior for non-standard OpenAI-compatible endpoints (Ollama, vLLM, LiteLLM, MLX, etc.). Mirrors upstream OpenAICompletionsCompat (types.ts).

func DetectCompat

func DetectCompat(providerID, baseURL string) *OpenAICompat

DetectCompat is exported for use by model resolution. Returns nil for OpenAI-direct (defaults suffice).

func (OpenAICompat) MarshalJSON

func (compat OpenAICompat) MarshalJSON() ([]byte, error)

MarshalJSON preserves an explicit empty fallback list, which disables server-side fallback rather than inheriting catalog defaults.

type OpenAIConfig

type OpenAIConfig struct {
	// BaseURL is the API base (default: https://api.openai.com/v1).
	BaseURL string
	// APIKey is the bearer token. May be empty for local endpoints.
	APIKey string
	// Model is the model name sent in the request.
	Model string
	// ModelMetadata supplies the selected model's capabilities and thinking map without falling back to a same-named catalog entry.
	ModelMetadata *Model
	// ThinkingLevelMap supplies an explicit map when selected ModelMetadata is absent.
	ThinkingLevelMap ThinkingLevelMap
	// ProviderID is the provider label (e.g. "openai", "openrouter", "ollama").
	ProviderID string
	// ExtraHeaders are added to every request.
	ExtraHeaders   map[string]string
	SamplingParams map[string]any
	// Env holds provider-scoped environment overrides that take precedence over
	// the process environment when resolving provider configuration such as
	// PI_CACHE_RETENTION and Cloudflare base-URL placeholders. Mirrors the
	// upstream StreamOptions.env threaded into the provider.
	Env ProviderEnv

	// Compat controls behavior for non-standard OpenAI-compatible endpoints.
	// Mirrors upstream OpenAICompletionsCompat.
	Compat *OpenAICompat

	// GetAPIKey is an optional callback for dynamically obtaining the bearer
	// token (e.g. refreshing an OAuth access token). When set, takes precedence
	// over APIKey. Called once per Stream() invocation.
	GetAPIKey func(ctx context.Context) (string, error)
	// GetBaseURL is an optional callback for dynamically resolving the base URL
	// (e.g. extracting proxy-ep from a refreshed Copilot token). When set,
	// takes precedence over BaseURL. Called once per Stream() invocation.
	GetBaseURL func(ctx context.Context) (string, error)
	// DynamicHeaders, when set, is called per-request with the StreamOptions
	// and returns headers to add to that request (e.g. Copilot's X-Initiator).
	DynamicHeaders func(transcript TranscriptContext, opts StreamOptions) map[string]string
	// pig additive (D36): opt in to TLS without certificate verification.
	Insecure bool
}

OpenAIConfig configures an OpenAI-compatible provider.

type OpenAIResponsesCompat

type OpenAIResponsesCompat = OpenAICompat

ThinkingLevel controls extended-thinking depth (where supported).

type OpenAIResponsesConfig

type OpenAIResponsesConfig struct {

	// BaseURL is the API base (default: https://api.openai.com/v1).
	BaseURL string
	// APIKey is the bearer token.
	APIKey string
	// Model is the logical model identity used for response metadata and replay capabilities.
	Model string
	// ModelMetadata supplies the selected model's thinking map and input capabilities.
	ModelMetadata *Model
	// ProviderID is the provider label (e.g. "openai", "github-copilot").
	ProviderID string
	// ExtraHeaders are added to every request.
	ExtraHeaders   map[string]string
	SamplingParams map[string]any
	// ThinkingLevelMap supplies an explicit map when selected ModelMetadata is absent.
	ThinkingLevelMap ThinkingLevelMap
	// APIKeyHeader is the header name used for the API key.
	// Default: Authorization.
	APIKeyHeader string
	// APIKeyPrefix is prepended when APIKeyHeader is used.
	// Default: Bearer . Empty string sends the raw key.
	APIKeyPrefix string
	// BaseURLIsEndpoint means BaseURL/GetBaseURL already resolves to the
	// final Responses endpoint and should not have /responses appended.
	BaseURLIsEndpoint bool

	// IsReasoning indicates this model supports extended reasoning.
	IsReasoning bool

	// GetAPIKey dynamically obtains the bearer token. Takes precedence over APIKey.
	GetAPIKey func(ctx context.Context) (string, error)
	// GetBaseURL dynamically resolves the base URL. Takes precedence over BaseURL.
	GetBaseURL func(ctx context.Context) (string, error)
	// DynamicHeaders returns per-request headers.
	DynamicHeaders func(transcript TranscriptContext, opts StreamOptions) map[string]string
	// Env holds provider-scoped environment overrides that take precedence over
	// the process environment for provider configuration (PI_CACHE_RETENTION,
	// Cloudflare base-URL placeholders). Mirrors upstream StreamOptions.env.
	Env ProviderEnv
	// Compat controls Responses-API-specific compat behavior.
	Compat *OpenAIResponsesCompat
	// StrictModeDefault is the provider-level default for supportsStrictMode when
	// the model compat does not set it. Upstream defaults false for the generic
	// openai-responses path and true for azure/codex.
	StrictModeDefault bool
	// StrictToolNull makes a strict-mode function tool default its strict field to
	// JSON null instead of false. Codex passes strict:null; other providers false.
	StrictToolNull bool
	// SkipServiceTierPricing leaves the response's service tier out of the
	// cost. Upstream Azure passes processResponsesStream no
	// applyServiceTierPricing; OpenAI and Codex apply it.
	SkipServiceTierPricing bool

	// Codex selects the ChatGPT Codex request and transport protocol rather
	// than the generic Responses protocol.
	Codex bool
	// contains filtered or unexported fields
}

OpenAIResponsesConfig configures the OpenAI Responses API provider.

type OpenRouterOAuthProvider

type OpenRouterOAuthProvider struct{}

OpenRouterOAuthProvider implements OAuthProviderInterface for OpenRouter.

Unlike the other built-in OAuth providers, OpenRouter is not a subscription login: its flow yields a permanent API key, and upstream openRouterOAuth leaves isSubscription unset.

func (OpenRouterOAuthProvider) GetAPIKey

func (OpenRouterOAuthProvider) ID

func (OpenRouterOAuthProvider) Login

func (OpenRouterOAuthProvider) LoginContext

func (OpenRouterOAuthProvider) Name

func (OpenRouterOAuthProvider) RefreshToken

RefreshToken returns the credential unchanged: an OpenRouter API key does not expire and has no refresh token. Mirrors upstream openRouterOAuth.refresh.

func (OpenRouterOAuthProvider) RefreshTokenContext

func (OpenRouterOAuthProvider) UsesCallbackServer

func (OpenRouterOAuthProvider) UsesCallbackServer() bool

type OrderedSections

type OrderedSections []PromptSection

OrderedSections is the ordered representation of upstream's section object.

func (OrderedSections) MarshalJSON

func (s OrderedSections) MarshalJSON() ([]byte, error)

func (*OrderedSections) UnmarshalJSON

func (s *OrderedSections) UnmarshalJSON(data []byte) error

type PKCE

type PKCE struct {
	Verifier  string
	Challenge string
}

PKCE holds a code verifier and its corresponding S256 challenge.

func GeneratePKCE

func GeneratePKCE() (PKCE, error)

GeneratePKCE creates a random PKCE verifier and SHA-256 challenge.

type PiMessagesConfig

type PiMessagesConfig struct {
	BaseURL      string
	APIKey       string
	GetAPIKey    func(context.Context) (string, error)
	Model        string
	ProviderID   string
	ExtraHeaders map[string]string
	// Debug asks the backend for debug metadata (?debug=1).
	Debug bool
	// ToolChoice is "auto", "none", "required", or {type:"function",...}.
	ToolChoice any
	OnResponse func(PiMessagesResponse) error
	// ModelMetadata is the selected model, which an OnProviderStreamEvent observer receives. Nil hands the observer the configured identity only.
	ModelMetadata *Model
}

PiMessagesConfig configures a pi-messages provider. Debug, ToolChoice, and OnResponse carry upstream PiMessagesOptions fields that StreamOptions lacks.

type PiMessagesEvent

type PiMessagesEvent struct {
	Type                  string         `json:"type"`
	ContentIndex          int            `json:"contentIndex"`
	Delta                 string         `json:"delta"`
	Content               string         `json:"content"`
	ContentSignature      string         `json:"contentSignature"`
	Redacted              bool           `json:"redacted"`
	ID                    string         `json:"id"`
	ToolName              string         `json:"toolName"`
	ToolCall              *ToolCall      `json:"toolCall"`
	Reason                StopReason     `json:"reason"`
	Usage                 Usage          `json:"usage"`
	ErrorMessage          string         `json:"errorMessage"`
	ResponseID            string         `json:"responseId"`
	ProviderThinkingLevel *string        `json:"providerThinkingLevel"`
	Rewrite               map[string]any `json:"rewrite"`
}

PiMessagesEvent is one serialized assistant-message event sent by a pi-messages backend. Type selects which fields apply.

type PiMessagesModel

type PiMessagesModel struct {
	RadiusGatewayModel
	API      API    `json:"api"`
	Provider string `json:"provider"`
	BaseURL  string `json:"baseUrl"`
}

PiMessagesModel is Pi's Model<"pi-messages"> as served by a Radius provider: a gateway model bound to its provider ID, API, and request base URL.

func GetRadiusModels

func GetRadiusModels(providerID string, credential *Credential) []PiMessagesModel

GetRadiusModels returns the legacy credential catalog bound to providerID.

func GetRadiusModelsFromConfig

func GetRadiusModelsFromConfig(providerID string, config RadiusGatewayConfig) []PiMessagesModel

GetRadiusModelsFromConfig binds a gateway catalog to providerID.

type PiMessagesResponse

type PiMessagesResponse struct {
	Status  int
	Headers map[string]string
}

PiMessagesResponse is the HTTP status and headers reported to OnResponse.

type PiMessagesResponseError

type PiMessagesResponseError struct {
	Message           string
	Code              string
	DiagnosticDetails map[string]any
}

PiMessagesResponseError is a non-2xx backend response with diagnostic details.

func (*PiMessagesResponseError) Error

func (e *PiMessagesResponseError) Error() string

type PiMessagesRewriteImpact

type PiMessagesRewriteImpact struct {
	PolicyID            string  `json:"policyId"`
	PolicyVersion       float64 `json:"policyVersion"`
	Changed             bool    `json:"changed"`
	TokenCountChange    float64 `json:"tokenCountChange"`
	MessageCountChange  float64 `json:"messageCountChange"`
	SystemPromptChanged bool    `json:"systemPromptChanged"`
}

PiMessagesRewriteImpact summarizes a server-side message rewrite (for example a gateway policy). It is recorded as a pi_messages_rewrite diagnostic.

type PromptSection

type PromptSection struct {
	Name  string
	Value *string
}

PromptSection preserves the insertion order of named system-prompt sections. A nil Value is an explicit JSON null that removes the named section.

type Provider

type Provider interface {
	// ID returns the provider identifier (e.g. "openai", "github-copilot").
	ID() string
	// Stream initiates a request from an already normalized transcript.
	Stream(ctx context.Context, transcript TranscriptContext, opts StreamOptions) (*AssistantMessageEventStream, error)
	// Close releases any persistent connections or background resources.
	io.Closer
}

Provider is the interface implemented by each LLM backend.

func NewAnthropicProvider

func NewAnthropicProvider(cfg AnthropicConfig) Provider

NewAnthropicProvider creates a Provider backed by the Anthropic Messages API.

func NewAzureOpenAIResponsesProvider

func NewAzureOpenAIResponsesProvider(cfg AzureOpenAIResponsesConfig) Provider

NewAzureOpenAIResponsesProvider creates an Azure OpenAI Responses provider. Mirrors upstream providers/azure-openai-responses.ts.

func NewCopilotProvider

func NewCopilotProvider(cfg CopilotProviderConfig) (Provider, error)

NewCopilotProvider builds a Provider that streams via the Copilot proxy. The wire protocol (Completions vs Responses API) is selected by cfg.API. The credential is auto-refreshed transparently when within 60s of expiry.

func NewGoogleProvider

func NewGoogleProvider(cfg GoogleConfig) Provider

NewGoogleProvider creates a Provider backed by the Google Gemini API.

func NewGoogleVertexProvider

func NewGoogleVertexProvider(cfg GoogleVertexConfig) Provider

NewGoogleVertexProvider creates a Vertex provider. Placeholder keys select ADC; real keys select Vertex express mode without project or location.

func NewMistralProvider

func NewMistralProvider(cfg MistralConfig) Provider

NewMistralProvider creates a Provider backed by the Mistral API. BaseURL defaults to https://api.mistral.ai; requests resolve v1/chat/completions beneath its normalized base pathname, preserving any existing v1 segment.

func NewOpenAICodexResponsesProvider

func NewOpenAICodexResponsesProvider(cfg OpenAICodexResponsesConfig) Provider

NewOpenAICodexResponsesProvider creates an OpenAI Codex Responses provider. Mirrors upstream providers/openai-codex-responses.ts.

func NewOpenAIProvider

func NewOpenAIProvider(cfg OpenAIConfig) Provider

NewOpenAIProvider creates a Provider backed by the OpenAI Completions API.

func NewOpenAIResponsesProvider

func NewOpenAIResponsesProvider(cfg OpenAIResponsesConfig) Provider

NewOpenAIResponsesProvider creates a Provider backed by the OpenAI Responses API.

func NewPiMessagesProvider

func NewPiMessagesProvider(cfg PiMessagesConfig) Provider

NewPiMessagesProvider creates a pi-messages provider. Like upstream fetch, requests are not retried by the provider retry transport.

type ProviderAPIMap

type ProviderAPIMap map[API]*ProviderStreams

type ProviderAPIs

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

ProviderAPIs is the closed single-module or API-keyed implementation union.

type ProviderAuth

type ProviderAuth struct {
	APIKey *APIKeyAuth
	OAuth  *OAuthAuth
}

ProviderAuth lists the auth methods a provider supports.

func BuiltinProviderAuth

func BuiltinProviderAuth(providerID string) (ProviderAuth, error)

BuiltinProviderAuth returns a built-in provider's auth methods. The OAuth method is present when the provider's OAuth flow is ported.

func RadiusProviderAuth

func RadiusProviderAuth(provider *RadiusProvider) ProviderAuth

RadiusProviderAuth mirrors the auth of upstream radiusProvider: the RADIUS_API_KEY api-key method and the provider's gateway OAuth, for the built-in provider and every models.json "oauth": "radius" gateway.

type ProviderClassifier

type ProviderClassifier struct {
	Classify func(context.Context, *ClassifierModel, ClassifierContext, ClassifierOptions) (ClassifierResult, error)
}

ProviderClassifier is a classifier API module.

func CloudflareClassifier

func CloudflareClassifier(classifier *ProviderClassifier) *ProviderClassifier

CloudflareClassifier wraps a classifier module so Cloudflare account and gateway endpoint placeholders materialize from the resolved provider env before dispatch.

func CloudflareWorkersAISystemOneAPI

func CloudflareWorkersAISystemOneAPI() *ProviderClassifier

CloudflareWorkersAISystemOneAPI is the cloudflare-workers-ai-system-one classifier module.

func LlamaCppClassifyAPI

func LlamaCppClassifyAPI() *ProviderClassifier

LlamaCppClassifyAPI is the llama-cpp-classify classifier module.

func TypesafeSystemOneAPI

func TypesafeSystemOneAPI() *ProviderClassifier

TypesafeSystemOneAPI is the typesafe-system-one classifier module.

type ProviderClassifierMap

type ProviderClassifierMap map[ClassifierAPI]*ProviderClassifier

ProviderClassifierMap keys classifier implementations by ClassifierModel.API.

func BuiltinClassifiers

func BuiltinClassifiers(provider string) ProviderClassifierMap

BuiltinClassifiers returns the classifier implementations of one built-in provider by classifier API (all.ts provider assembly).

type ProviderEnv

type ProviderEnv = map[string]string

ProviderEnv holds provider-scoped environment overrides whose values take precedence over the process environment for provider configuration such as regional settings, endpoint placeholders, and proxy variables. Mirrors upstream types.ts ProviderEnv (Record<string, string>).

func AnthropicFederationEnv

func AnthropicFederationEnv(env ProviderEnv) ProviderEnv

AnthropicFederationEnv returns the workload identity federation variables set in env or the process env, or nil unless the rule, organization and identity token file are all set. The service account and workspace travel when set. The anthropic auth resolver, the federation client and the coding registry's availability checks share it, so they agree on when federation is configured. upstream: packages/ai/src/providers/anthropic.ts:47-69, packages/ai/src/api/anthropic-messages.ts:343-372

type ProviderFactory

type ProviderFactory func(apiKey, model, baseURL string) Provider

ProviderFactory creates a Provider from an API key and model spec. Used by the built-in provider registry to construct providers on demand.

func LookupBuiltInProvider

func LookupBuiltInProvider(api API) (ProviderFactory, bool)

LookupBuiltInProvider returns the factory for a built-in API, if registered.

type ProviderHeaders

type ProviderHeaders map[string]*string

ProviderHeaders mirrors Pi's request-header map. A nil value removes an existing header after configured and authentication headers are merged.

func MergeProviderHeaders

func MergeProviderHeaders(base, override ProviderHeaders) ProviderHeaders

MergeProviderHeaders overlays override onto base, replacing headers case-insensitively. Mirrors upstream mergeHeaders in models.ts.

func ProviderHeadersFromStrings

func ProviderHeadersFromStrings(values map[string]string) ProviderHeaders

ProviderHeadersFromStrings converts non-null configured headers to request header values.

type ProviderImageAPIMap

type ProviderImageAPIMap map[ImageAPI]*ProviderImages

ProviderImageAPIMap keys image implementations by ImageModel.API.

type ProviderImages

type ProviderImages struct {
	GenerateImages func(context.Context, *ImageModel, ImagesContext, ImagesOptions) (AssistantImages, error)
}

ProviderImages is an image API module. Rejections are returned as errors.

type ProviderImagesOptions

type ProviderImagesOptions = ImagesOptions

ProviderImagesOptions is the image API options shape.

type ProviderMetadata

type ProviderMetadata struct {
	ProviderID string
	API        API
	BaseURL    string
	Headers    map[string]string
	Compat     *ModelCompat
	Reasoning  bool
}

ProviderMetadata carries generated/registry metadata that provider adapters use for compat decisions beyond the live Provider implementation.

type ProviderResponse

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

ProviderResponse is the status and headers observed before consuming a provider response.

type ProviderStreams

func LazyAPI

func LazyAPI(load func(context.Context) (*ProviderStreams, error), capabilities LazyAPICapabilities) *ProviderStreams

LazyAPI invokes the loader per operation, as Pi does. Module caching belongs to the loader; unsupported deferred callbacks stay nil.

type RadiusGatewayConfig

type RadiusGatewayConfig struct {
	BaseURL string               `json:"baseUrl"`
	Models  []RadiusGatewayModel `json:"models"`
}

RadiusGatewayConfig is the sanitized /v1/config document of a gateway.

func GetRadiusCredentialConfig

func GetRadiusCredentialConfig(credential *Credential) (RadiusGatewayConfig, bool)

GetRadiusCredentialConfig returns the sanitized legacy gateway catalog that pre-ModelsStore Radius builds cached on the OAuth credential.

func LoadRadiusGatewayConfig

func LoadRadiusGatewayConfig(ctx context.Context, gateway, apiKey string) (RadiusGatewayConfig, error)

LoadRadiusGatewayConfig fetches and sanitizes <gateway>/v1/config. Mirrors upstream loadRadiusGatewayConfig. PiG sets no User-Agent here, so net/http sends its default (owner question Q4 tracks Pi's User-Agent).

type RadiusGatewayModel

type RadiusGatewayModel struct {
	ID               string            `json:"id"`
	Name             string            `json:"name"`
	Reasoning        bool              `json:"reasoning"`
	ThinkingLevelMap ThinkingLevelMap  `json:"thinkingLevelMap,omitempty"`
	Input            []string          `json:"input"`
	InputLimits      *ModelInputLimits `json:"inputLimits,omitempty"`
	Cost             RadiusModelCost   `json:"cost"`
	PromptCache      ModelPromptCache  `json:"promptCache,omitempty"`
	ContextWindow    int               `json:"contextWindow"`
	MaxTokens        int               `json:"maxTokens"`
	SamplingParams   map[string]any    `json:"samplingParams,omitempty"`
	Headers          map[string]string `json:"headers,omitempty"`
	Compat           *ModelCompat      `json:"compat,omitempty"`
}

RadiusGatewayModel is one model advertised by a Radius gateway's /v1/config.

type RadiusModelCost

type RadiusModelCost struct {
	Input      float64    `json:"input"`
	Output     float64    `json:"output"`
	CacheRead  float64    `json:"cacheRead"`
	CacheWrite float64    `json:"cacheWrite"`
	Tiers      []CostTier `json:"tiers,omitempty"`
}

RadiusModelCost is the per-million-token pricing carried by a Radius model.

type RadiusOAuth

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

RadiusOAuth is the Radius gateway OAuth flow (browser PKCE or device code).

func CreateRadiusOAuth

func CreateRadiusOAuth(options RadiusOAuthOptions) *RadiusOAuth

CreateRadiusOAuth mirrors upstream createRadiusOAuth.

func (*RadiusOAuth) Gateway

func (o *RadiusOAuth) Gateway() string

Gateway returns the normalized gateway origin used for every OAuth request.

func (*RadiusOAuth) GetAPIKey

func (o *RadiusOAuth) GetAPIKey(credentials OAuthCredentials) string

func (*RadiusOAuth) ID

func (o *RadiusOAuth) ID() string

func (*RadiusOAuth) Login

func (o *RadiusOAuth) Login(callbacks OAuthLoginCallbacks) (OAuthCredentials, error)

Login runs the flow without cancellation. Prefer LoginContext.

func (*RadiusOAuth) LoginContext

func (o *RadiusOAuth) LoginContext(ctx context.Context, callbacks OAuthLoginCallbacks) (OAuthCredentials, error)

LoginContext asks for a sign-in method, then runs the browser or device flow. Cancelling ctx mirrors aborting upstream's interaction.signal.

func (*RadiusOAuth) LoginMethodPrompt

func (o *RadiusOAuth) LoginMethodPrompt() OAuthSelectPrompt

LoginMethodPrompt is the sign-in method selection Pi presents before login.

func (*RadiusOAuth) Name

func (o *RadiusOAuth) Name() string

func (*RadiusOAuth) RefreshToken

func (o *RadiusOAuth) RefreshToken(credentials OAuthCredentials) (OAuthCredentials, error)

RefreshToken exchanges the refresh token directly at the gateway.

func (*RadiusOAuth) RefreshTokenContext

func (o *RadiusOAuth) RefreshTokenContext(ctx context.Context, credentials OAuthCredentials) (OAuthCredentials, error)

RefreshTokenContext exchanges the token with the owning operation's cancellation.

func (*RadiusOAuth) UsesCallbackServer

func (o *RadiusOAuth) UsesCallbackServer() bool

type RadiusOAuthOptions

type RadiusOAuthOptions struct {
	ID      string
	Name    string
	Gateway string
}

RadiusOAuthOptions configures CreateRadiusOAuth. ID is the provider ID the flow is registered under; Pi derives it from the owning provider.

type RadiusOAuthResponseError

type RadiusOAuthResponseError struct {
	Status     int
	OAuthError string
	// contains filtered or unexported fields
}

RadiusOAuthResponseError is a non-2xx OAuth response. Mirrors upstream OAuthResponseError.

func (*RadiusOAuthResponseError) Error

func (e *RadiusOAuthResponseError) Error() string

type RadiusProvider

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

RadiusProvider is the Radius gateway provider: the published static catalog for the default gateway, overlaid by a persisted, dynamically refreshed one.

func NewRadiusProvider

func NewRadiusProvider(options RadiusProviderOptions) *RadiusProvider

NewRadiusProvider mirrors upstream radiusProvider.

func (*RadiusProvider) FindModel

func (p *RadiusProvider) FindModel(id string) (PiMessagesModel, bool)

FindModel returns the effective model with id.

func (*RadiusProvider) Gateway

func (p *RadiusProvider) Gateway() string

func (*RadiusProvider) GetModels

func (p *RadiusProvider) GetModels() []PiMessagesModel

GetModels returns the static catalog with refreshed models overlaid by ID.

func (*RadiusProvider) ID

func (p *RadiusProvider) ID() string

func (*RadiusProvider) Name

func (p *RadiusProvider) Name() string

func (*RadiusProvider) OAuth

func (p *RadiusProvider) OAuth() *RadiusOAuth

func (*RadiusProvider) RefreshModels

func (p *RadiusProvider) RefreshModels(ctx context.Context, refresh RefreshModelsContext) error

RefreshModels restores the stored catalog, imports a legacy credential catalog, and, when network access is allowed, loads the gateway's current catalog with the effective credential.

type RadiusProviderOptions

type RadiusProviderOptions struct {
	ID      string
	Name    string
	Gateway string
}

RadiusProviderOptions configures NewRadiusProvider. Empty fields default to the built-in "radius" provider on DefaultRadiusGateway.

type ReadOnlyAuthStorage

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

ReadOnlyAuthStorage reads auth.json without creating it, its directory, or its lock, and never executes configured key commands. Mirrors upstream ReadOnlyAuthStorage.

func NewReadOnlyAuthStorage

func NewReadOnlyAuthStorage(path string) *ReadOnlyAuthStorage

NewReadOnlyAuthStorage returns a read-only view of the auth.json at path.

func (*ReadOnlyAuthStorage) Delete

Delete always fails: the store is read-only.

func (*ReadOnlyAuthStorage) List

List implements CredentialStore.

func (*ReadOnlyAuthStorage) Modify

Modify always fails: the store is read-only.

func (*ReadOnlyAuthStorage) Read

func (s *ReadOnlyAuthStorage) Read(ctx context.Context, providerID string) (*Credential, error)

Read implements CredentialStore. Command-valued keys are returned unresolved, as upstream does, so a read-only check never runs them.

type RefreshModelsContext

type RefreshModelsContext struct {
	Credential   *Credential
	Stored       *ModelsStoreEntry
	Publish      func(ModelsPublication) (bool, error)
	AllowNetwork bool
	Force        *bool
	Signal       context.Context
}

type RetryCallbacks

type RetryCallbacks struct {
	// OnRetryScheduled runs before the backoff sleep of each 1-indexed retry.
	OnRetryScheduled func(attempt, maxAttempts, delayMs int, errorMessage string) error
	// OnRetryAttemptStart runs after the backoff sleep, before the retried call.
	OnRetryAttemptStart func() error
	// OnRetryFinished runs once when a retried loop ends. finalError is empty
	// when upstream passes no final error.
	OnRetryFinished func(success bool, attempt int, finalError string) error
}

RetryCallbacks are emitted by RetryAssistantCall around each retry. A nil field is skipped; a returned error fails the retry call.

type RetryPolicy

type RetryPolicy struct {
	Enabled bool `json:"enabled"`
	// MaxRetries is the retry budget; the initial call never counts as a retry.
	MaxRetries      int  `json:"maxRetries"`
	BaseDelayMs     int  `json:"baseDelayMs"`
	MaxAgentDelayMs *int `json:"maxAgentDelayMs,omitempty"`
}

RetryPolicy bounds attempts with exponential backoff (baseDelayMs * 2^(attempt-1)). MaxAgentDelayMs caps each computed delay and defaults to DefaultMaxAgentRetryDelayMs when nil.

type RuntimeCredentials

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

RuntimeCredentials is a credential store overlay for non-persistent runtime API keys (--api-key). An override reads as an api_key credential and is never written to the base store. Mirrors upstream packages/coding-agent/src/core/runtime-credentials.ts RuntimeCredentials.

func NewRuntimeCredentials

func NewRuntimeCredentials(store CredentialStore) *RuntimeCredentials

NewRuntimeCredentials overlays runtime API keys on store. A nil store holds no persistent credentials.

func (*RuntimeCredentials) Delete

func (c *RuntimeCredentials) Delete(ctx context.Context, providerID string) error

Delete removes providerID's credential from the base store, then its runtime key. Failed or cancelled deletion leaves the runtime key intact and preserves the cancellation cause.

func (*RuntimeCredentials) HasRuntimeAPIKey

func (c *RuntimeCredentials) HasRuntimeAPIKey(providerID string) bool

HasRuntimeAPIKey reports whether providerID has a runtime key.

func (*RuntimeCredentials) List

List implements CredentialStore: base entries, with every runtime key listed as an api_key entry. It checks cancellation after the base list completes and preserves the caller's cause.

func (*RuntimeCredentials) Modify

func (c *RuntimeCredentials) Modify(ctx context.Context, providerID string, fn func(current *Credential) (*Credential, error)) (*Credential, error)

Modify implements CredentialStore by delegating to the base store; the runtime key is unaffected.

func (*RuntimeCredentials) Read

func (c *RuntimeCredentials) Read(ctx context.Context, providerID string) (*Credential, error)

Read implements CredentialStore: a runtime key reads as an api_key credential ahead of the base store. Cancellation returns the caller's cause.

func (*RuntimeCredentials) RemoveRuntimeAPIKey

func (c *RuntimeCredentials) RemoveRuntimeAPIKey(providerID string)

RemoveRuntimeAPIKey drops providerID's runtime key, revealing the base store's credential.

func (*RuntimeCredentials) RuntimeAPIKey

func (c *RuntimeCredentials) RuntimeAPIKey(providerID string) (string, bool)

RuntimeAPIKey returns providerID's runtime key.

func (*RuntimeCredentials) SetRuntimeAPIKey

func (c *RuntimeCredentials) SetRuntimeAPIKey(providerID, apiKey string)

SetRuntimeAPIKey installs a non-persistent API key for providerID.

type SessionAffinityFormat

type SessionAffinityFormat string

SessionAffinityFormat selects which session-affinity headers a provider sends from options.SessionID. Mirrors upstream SessionAffinityFormat (types.ts). Empty string means auto-detect at the provider.

const (
	// SessionAffinityOpenAI sends session_id + x-client-request-id
	// (+ x-session-affinity on Completions).
	SessionAffinityOpenAI SessionAffinityFormat = "openai"
	// SessionAffinityOpenAINoSession omits session_id.
	SessionAffinityOpenAINoSession SessionAffinityFormat = "openai-nosession"
	// SessionAffinityOpenRouter sends x-session-id.
	SessionAffinityOpenRouter SessionAffinityFormat = "openrouter"
)

type SessionResourceCleanup

type SessionResourceCleanup func(sessionID string)

SessionResourceCleanup releases provider resources scoped to a logical session, such as cached transport connections.

type StartEvent

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

func (StartEvent) EventType

func (StartEvent) EventType() AssistantEventType

func (StartEvent) MarshalJSON

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

type StartFrame

type StartFrame struct {
	Partial AssistantMessage `json:"partial"`
}

func (StartFrame) FrameType

func (StartFrame) MarshalJSON

func (frame StartFrame) MarshalJSON() ([]byte, error)

type StopReason

type StopReason string

StopReason is the terminal state of an assistant response.

const (
	StopReasonPending  StopReason = "pending"
	StopReasonStop     StopReason = "stop"
	StopReasonLength   StopReason = "length"
	StopReasonToolUse  StopReason = "toolUse"
	StopReasonError    StopReason = "error"
	StopReasonAborted  StopReason = "aborted"
	StopReasonDeferred StopReason = "deferred"
)

type StreamContinuation

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

StreamContinuation reserves a FIFO continuation before its owner transfers work to another goroutine. Its owner must Run it once or Cancel it before abandoning queued work.

func (*StreamContinuation) Cancel

func (continuation *StreamContinuation) Cancel() bool

Cancel releases an unclaimed reservation without waiting for its queue position. It returns false once Run owns the callback; that running callback must be canceled and joined by its caller.

func (*StreamContinuation) Run

func (continuation *StreamContinuation) Run(body func(*StreamObservation) error) error

Run waits for the reserved continuation, runs its synchronous callback with an observation scope, and releases ownership on return. A canceled reservation returns context.Canceled without invoking the callback.

type StreamObservation

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

StreamObservation owns the synchronous part of a stream-consumer continuation. Await releases execution while an upstream asynchronous operation waits, then resumes through the same reaction queue.

func StreamObservationFromContext

func StreamObservationFromContext(ctx context.Context) *StreamObservation

StreamObservationFromContext returns the host observation, if this callback belongs to a scoped stream continuation. A callback that runs while a provider response turn holds execution belongs to that turn, even when its context was derived from a consumer that is parked in an await: an awaiting host callback must release the turn that is running.

func (*StreamObservation) Await

func (observation *StreamObservation) Await(wait func() error) error

Await runs a blocking wait outside the synchronous observation and resumes in FIFO continuation order. Prepare any values that upstream reads before its await before calling Await.

func (*StreamObservation) AwaitContinuation

func (observation *StreamObservation) AwaitContinuation(continuation *StreamContinuation, wait func() error) error

AwaitContinuation is Await for a wait that ends when continuation's callback returns. The resume reaction is queued when the callback returns, before its turn releases execution, so the awaiting goroutine keeps its place ahead of any I/O completion that became ready while the callback ran, however late that goroutine is scheduled. An unclaimed or canceled continuation resumes when wait returns, as in Await.

func (*StreamObservation) AwaitExternal

func (observation *StreamObservation) AwaitExternal(wait func() error) error

AwaitExternal is Await for a wait that Pi settles from an I/O completion or timer callback instead of a promise reaction. The continuation runs after every ready reaction, and every reaction those queue, has run, so a wait that ends while the provider's reactions are still running resumes deterministically at their end. An extension handler that awaits its process is such a wait. wait runs outside the observation, as in Await.

func (*StreamObservation) AwaitTick

func (observation *StreamObservation) AwaitTick(wait func() error) error

AwaitTick is Await for a wait that Pi settles from a process.nextTick callback: the continuation runs after every ready reaction and before any I/O completion, including completions queued earlier. A synchronous pipe or file write's callback is such a wait (output-guard.ts:writeRawStdoutChunk). Node queues the callback while the caller still runs, so wait runs first, on the caller's goroutine and still holding the observation; it must not depend on the executor.

func (*StreamObservation) Context

func (observation *StreamObservation) Context(ctx context.Context) context.Context

Context carries the active observation through host callbacks without adding a wire field. A nil observation preserves the caller's context identity.

func (*StreamObservation) PrepareContinuation

func (observation *StreamObservation) PrepareContinuation() *StreamContinuation

PrepareContinuation reserves the next callback's place while the current observation still owns its synchronous prefix.

func (*StreamObservation) Yield

func (observation *StreamObservation) Yield()

Yield awaits an already resolved operation. It reserves the resume reaction before releasing execution, so synchronous completion never depends on goroutine admission.

type StreamOptions

type StreamOptions struct {
	// TelemetryContext parents provider request spans; nil means no recording backend.
	TelemetryContext telemetry.TelemetryContext
	// Deferred preserves the deferred-generation boolean or window object.
	Deferred *DeferredOption
	// Fetch replaces HTTP execution with a caller-owned client, preserving its redirect policy and request context.
	Fetch     *http.Client `json:"-"`
	MaxTokens int
	// Region selects the Bedrock request region. An inference-profile ARN's embedded region takes precedence.
	Region string `json:"region,omitempty"`
	// Profile selects the AWS shared credentials profile for a Bedrock request.
	Profile     string `json:"profile,omitempty"`
	Temperature float64
	// TemperatureSet distinguishes an explicit zero temperature from omission.
	// Non-zero Temperature values remain present for existing callers.
	TemperatureSet  bool
	SamplingParams  map[string]any
	ThinkingBudgets *ThinkingBudgets
	Thinking        ThinkingLevel `json:"reasoning,omitempty"`
	// GoogleThinking carries Google's raw thinking object; Thinking is the provider-neutral reasoning option.
	GoogleThinking       *GoogleThinkingOptions `json:"thinking,omitempty"`
	ThinkingEnabled      *bool                  `json:"thinkingEnabled,omitempty"`
	ThinkingBudgetTokens *int                   `json:"thinkingBudgetTokens,omitempty"`
	Effort               string                 `json:"effort,omitempty"`
	InterleavedThinking  *bool                  `json:"interleavedThinking,omitempty"`
	RequestMetadata      map[string]string      `json:"requestMetadata,omitempty"`
	// ReasoningEffort is the raw OpenAI-compatible or Mistral API effort. Unlike the provider-neutral Thinking level, it is mapped but not clamped so the provider can reject unsupported values.
	ReasoningEffort string
	// PromptMode is the raw Mistral prompt mode, independent of provider-neutral Thinking.
	PromptMode string `json:"promptMode,omitempty"`
	// IsReasoning indicates whether the model supports extended reasoning.
	// Used by providers to decide developer vs system role and reasoning_effort gating.
	// Mirrors upstream model.reasoning field.
	IsReasoning bool
	// ModelCost is the requested model's price. Providers price Usage.Cost
	// with it where upstream providers call calculateCost(model, usage).
	ModelCost ModelCost
	// Env contains request-specific provider environment values. Runtime-resolved
	// values are merged first; request values win.
	Env ProviderEnv
	// TransformHeaders runs once after configured, auth, and request headers are
	// merged. ModelRuntime consumes it and does not forward it to providers.
	TransformHeaders func(context.Context, ProviderHeaders) (ProviderHeaders, error)
	// Headers are request-specific HTTP headers. A nil value deletes a header
	// supplied by configured or resolved authentication state.
	Headers ProviderHeaders
	// Signal carries the request lifetime to extension-owned provider callbacks. Native Provider.Stream implementations receive the same lifetime as their context argument.
	Signal context.Context `json:"-"`
	// APIKey is the request credential; ModelRuntime and direct StreamSimple construct the selected API provider with it. Bedrock also accepts it directly as a bearer token.
	APIKey string
	// CacheRetention requests a provider-supported prompt-cache lifetime. Empty leaves the option unset.
	CacheRetention CacheRetention
	// SessionID is passed to the provider for prompt caching (OpenAI prompt_cache_key). When non-empty and the provider supports it, repeated calls with the same session ID can reuse cached prompt processing. Mirrors upstream openai-completions.ts:449.
	SessionID string
	// ToolChoice selects provider-neutral automatic/no-tool behavior or a
	// provider-specific choice object. Anthropic and Bedrock also accept "any" and
	// {"type":"tool","name":...}.
	ToolChoice any
	// Metadata contains optional provider request metadata. Providers extract
	// recognized fields and ignore the rest.
	Metadata map[string]any
	// Transport requests a provider-specific streaming transport.
	// Empty lets the provider choose its default.
	Transport Transport
	// TimeoutMs bounds the full Mistral request through SSE consumption, and response headers or stream idleness for other supported providers. Nil selects the provider default; zero remains explicit and follows the selected provider's timeout semantics.
	TimeoutMs *int `json:"timeoutMs,omitempty"`
	// WebSocketConnectTimeoutMs bounds only the opening handshake. Nil selects the provider default; zero explicitly disables it.
	WebSocketConnectTimeoutMs *int `json:"websocketConnectTimeoutMs,omitempty"`
	MaxRetries                *int `json:"maxRetries,omitempty"`
	MaxRetryDelayMs           *int `json:"maxRetryDelayMs,omitempty"`
	// OnPayload is an optional payload-inspection hook invoked after the provider builds its native payload and before it serializes and sends the request. Returning nil
	// keeps the payload unchanged; returning a replacement asks providers that
	// support replacement to send that value instead. Mirrors upstream
	// StreamOptions.onPayload.
	OnPayload func(payload any, model *Model) (any, error)
	// OnResponse is awaited before response consumption. The request context owns cancellation.
	OnResponse func(context.Context, ProviderResponse, *Model) error
	// OnProviderStreamEvent observes each parsed provider stream event before normalization. Event data is adapter-owned and read-only; an adapter without explicit support never calls it. Mirrors upstream StreamOptions.onProviderStreamEvent (packages/ai/src/types.ts:198).
	OnProviderStreamEvent func(ctx context.Context, data any, model *Model) error
}

StreamOptions are the options for a streaming LLM call.

type SystemContent

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

SystemContent is the closed content union for system messages.

type SystemMessage

type SystemMessage struct {
	Content      SystemContent   `json:"content"`
	Sections     OrderedSections `json:"sections,omitempty"`
	Timestamp    int64           `json:"timestamp"`
	ToolsAdded   []ToolSchema    `json:"toolsAdded,omitempty"`
	ToolsRemoved []ToolReference `json:"toolsRemoved,omitempty"`
}

SystemMessage changes instructions and tool state at one transcript position.

func CreateInitialSystemMessage

func CreateInitialSystemMessage(systemPrompt string, tools []ToolSchema) *SystemMessage

func GetCurrentSystemMessage

func GetCurrentSystemMessage(messages []Message) *SystemMessage

func GetInitialSystemMessage

func GetInitialSystemMessage(messages []Message) *SystemMessage

func (SystemMessage) MarshalJSON

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

type SystemText

type SystemText string

SystemText carries system instruction text.

type SystemTextBlocks

type SystemTextBlocks []TextContent

SystemTextBlocks carries system instruction text blocks.

type TestFauxProvider

type TestFauxProvider struct {
	Scenario string
	// contains filtered or unexported fields
}

Tool-call IDs use a deterministic counter per StreamOptions.SessionID. An omitted session ID shares the provider's default counter; resumed history seeds a new counter.

func (*TestFauxProvider) Close

func (p *TestFauxProvider) Close() error

func (*TestFauxProvider) ID

func (p *TestFauxProvider) ID() string

func (*TestFauxProvider) Stream

type TextContent

type TextContent struct {
	Text          string `json:"text"`
	TextSignature string `json:"textSignature,omitempty"`
	// contains filtered or unexported fields
}

TextContent is a text fragment in a message.

func (TextContent) MarshalJSON

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

type TextDeltaEvent

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

func (TextDeltaEvent) EventType

func (TextDeltaEvent) EventType() AssistantEventType

func (TextDeltaEvent) MarshalJSON

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

type TextDeltaFrame

type TextDeltaFrame struct {
	ContentIndex int    `json:"contentIndex"`
	Delta        string `json:"delta"`
}

func (TextDeltaFrame) FrameType

func (TextDeltaFrame) MarshalJSON

func (frame TextDeltaFrame) MarshalJSON() ([]byte, error)

type TextEndEvent

type TextEndEvent struct {
	ContentIndex int               `json:"contentIndex"`
	Content      string            `json:"content"`
	Partial      *AssistantMessage `json:"partial"`
	// contains filtered or unexported fields
}

func (TextEndEvent) EventType

func (TextEndEvent) EventType() AssistantEventType

func (TextEndEvent) MarshalJSON

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

type TextEndFrame

type TextEndFrame struct {
	ContentIndex  int    `json:"contentIndex"`
	Content       string `json:"content"`
	TextSignature string `json:"textSignature,omitempty"`
}

func (TextEndFrame) FrameType

func (TextEndFrame) MarshalJSON

func (frame TextEndFrame) MarshalJSON() ([]byte, error)

type TextStartEvent

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

func (TextStartEvent) EventType

func (TextStartEvent) EventType() AssistantEventType

func (TextStartEvent) MarshalJSON

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

type TextStartFrame

type TextStartFrame struct {
	ContentIndex int         `json:"contentIndex"`
	Content      TextContent `json:"content"`
}

func (TextStartFrame) FrameType

func (TextStartFrame) MarshalJSON

func (frame TextStartFrame) MarshalJSON() ([]byte, error)

type ThinkingBudgets

type ThinkingBudgets struct {
	Minimal int
	Low     int
	Medium  int
	High    int
}

ThinkingBudgets maps thinking level names to token budgets.

func DefaultThinkingBudgets

func DefaultThinkingBudgets() ThinkingBudgets

DefaultThinkingBudgets returns the default budget map. Mirrors upstream defaultBudgets (simple-options.ts:32-37).

type ThinkingContent

type ThinkingContent struct {
	Thinking          string `json:"thinking"`
	ThinkingSignature string `json:"thinkingSignature,omitempty"`
	Redacted          bool   `json:"redacted,omitempty"`
	// contains filtered or unexported fields
}

ThinkingContent is a thinking/reasoning block. JSON decoding and provider streams preserve the distinction between omitted and explicitly empty signatures.

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

func (ThinkingDeltaEvent) MarshalJSON

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

type ThinkingDeltaFrame

type ThinkingDeltaFrame struct {
	ContentIndex int    `json:"contentIndex"`
	Delta        string `json:"delta"`
}

func (ThinkingDeltaFrame) FrameType

func (ThinkingDeltaFrame) MarshalJSON

func (frame ThinkingDeltaFrame) MarshalJSON() ([]byte, error)

type ThinkingEndEvent

type ThinkingEndEvent struct {
	ContentIndex int               `json:"contentIndex"`
	Content      string            `json:"content"`
	Partial      *AssistantMessage `json:"partial"`
	// contains filtered or unexported fields
}

func (ThinkingEndEvent) EventType

func (ThinkingEndEvent) MarshalJSON

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

type ThinkingEndFrame

type ThinkingEndFrame struct {
	ContentIndex      int    `json:"contentIndex"`
	Content           string `json:"content"`
	ThinkingSignature string `json:"thinkingSignature,omitempty"`
	Redacted          bool   `json:"redacted,omitempty"`
}

func (ThinkingEndFrame) FrameType

func (ThinkingEndFrame) MarshalJSON

func (frame ThinkingEndFrame) MarshalJSON() ([]byte, error)

type ThinkingLevel

type ThinkingLevel string
const (
	ThinkingOff     ThinkingLevel = "off"
	ThinkingNone    ThinkingLevel = ThinkingOff // backward-compatible alias
	ThinkingMinimal ThinkingLevel = "minimal"   // lightest reasoning: mirrors upstream "minimal"
	ThinkingLow     ThinkingLevel = "low"
	ThinkingMedium  ThinkingLevel = "medium"
	ThinkingHigh    ThinkingLevel = "high"
	// ThinkingXHigh is supported only by models that explicitly declare
	// MaxThinking = ThinkingXHigh in their Capabilities.
	// Mirrors upstream THINKING_LEVELS_WITH_XHIGH (agent-session.ts:232).
	ThinkingXHigh ThinkingLevel = "xhigh"
	// ThinkingMax is supported only by models that explicitly map "max" in
	// their thinkingLevelMap (e.g. Claude Opus 4.6 adaptive thinking). Like
	// xhigh it clamps to "high" for budget-based and non-supporting providers.
	// Mirrors upstream ThinkingLevel "max" (ai/types.ts:79).
	ThinkingMax ThinkingLevel = "max"
)

func ClampThinkingLevel

func ClampThinkingLevel(m *Model, level ThinkingLevel) ThinkingLevel

ClampThinkingLevel clamps level to the nearest supported thinking level. Mirrors upstream models.ts:clampThinkingLevel.

func GetSupportedThinkingLevels

func GetSupportedThinkingLevels(m *Model) []ThinkingLevel

GetSupportedThinkingLevels returns the model's supported thinking levels. Selected model reasoning metadata or a native MaxThinking capability enables reasoning.

type ThinkingLevelMap

type ThinkingLevelMap = map[ModelThinkingLevel]*string

type ThinkingStartEvent

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

func (ThinkingStartEvent) EventType

func (ThinkingStartEvent) MarshalJSON

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

type ThinkingStartFrame

type ThinkingStartFrame struct {
	ContentIndex int             `json:"contentIndex"`
	Content      ThinkingContent `json:"content"`
}

func (ThinkingStartFrame) FrameType

func (ThinkingStartFrame) MarshalJSON

func (frame ThinkingStartFrame) MarshalJSON() ([]byte, error)

type TokenFailure

type TokenFailure struct {
	Type    string
	Message string
	Status  int
}

type ToolCall

type ToolCall struct {
	ID               string     `json:"id"`
	Name             string     `json:"name"`
	Arguments        JsonObject `json:"arguments"`
	ThoughtSignature string     `json:"thoughtSignature,omitempty"`
	Namespace        string     `json:"namespace,omitempty"`
	// contains filtered or unexported fields
}

ToolCall is a tool invocation block in an assistant message. Streaming OpenAI calls also serialize provider scratch fields until finalization or failure. Arguments is read and edited as a Go map; the member order the model sent is kept beside it and is written by ArgumentsJSON and MarshalJSON.

func (ToolCall) ArgumentsJSON

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

ArgumentsJSON encodes Arguments in the member order the model sent them. A nil Arguments is the empty object, as Pi's arguments are always an object.

func (ToolCall) MarshalJSON

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

func (*ToolCall) SetArgumentsJSON

func (content *ToolCall) SetArgumentsJSON(raw []byte) error

SetArgumentsJSON decodes a complete JSON object into Arguments and keeps its member order. A text that is not a JSON object is an error and leaves the call unchanged.

func (*ToolCall) SetStreamingArguments

func (content *ToolCall) SetStreamingArguments(input string)

SetStreamingArguments parses a streamed tool-argument text as ParseStreamingJson does and keeps its member order.

func (*ToolCall) UnmarshalJSON

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

UnmarshalJSON decodes the call and keeps the member order of its arguments.

type ToolCallCheckpointFrame

type ToolCallCheckpointFrame struct {
	ContentIndex int    `json:"contentIndex"`
	JSON         string `json:"json"`
}

func (ToolCallCheckpointFrame) FrameType

func (ToolCallCheckpointFrame) MarshalJSON

func (frame ToolCallCheckpointFrame) MarshalJSON() ([]byte, error)

type ToolCallDeltaEvent

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

func (ToolCallDeltaEvent) EventType

func (ToolCallDeltaEvent) MarshalJSON

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

type ToolCallDeltaFrame

type ToolCallDeltaFrame struct {
	ContentIndex int    `json:"contentIndex"`
	Delta        string `json:"delta"`
}

func (ToolCallDeltaFrame) FrameType

func (ToolCallDeltaFrame) MarshalJSON

func (frame ToolCallDeltaFrame) MarshalJSON() ([]byte, error)

type ToolCallEndEvent

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

func (ToolCallEndEvent) EventType

func (ToolCallEndEvent) MarshalJSON

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

type ToolCallEndFrame

type ToolCallEndFrame struct {
	ContentIndex     int        `json:"contentIndex"`
	ID               string     `json:"id"`
	Name             string     `json:"name"`
	Arguments        JsonObject `json:"arguments"`
	ThoughtSignature string     `json:"thoughtSignature,omitempty"`
	Namespace        string     `json:"namespace,omitempty"`
}

func (ToolCallEndFrame) FrameType

func (ToolCallEndFrame) MarshalJSON

func (frame ToolCallEndFrame) MarshalJSON() ([]byte, error)

type ToolCallStartEvent

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

func (ToolCallStartEvent) EventType

func (ToolCallStartEvent) MarshalJSON

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

type ToolCallStartFrame

type ToolCallStartFrame struct {
	ContentIndex int      `json:"contentIndex"`
	ToolCall     ToolCall `json:"toolCall"`
}

func (ToolCallStartFrame) FrameType

func (ToolCallStartFrame) MarshalJSON

func (frame ToolCallStartFrame) MarshalJSON() ([]byte, error)

type ToolReference

type ToolReference struct {
	Name string `json:"name"`
}

ToolReference names a tool removed from the transcript's active tool set.

type ToolResultMessage

type ToolResultMessage struct {
	ToolCallID string                     `json:"toolCallId"`
	ToolName   string                     `json:"toolName"`
	Content    []ToolResultMessageContent `json:"content"`
	// Details is the tool's details value. Nil is no details unless DetailsNull is set.
	Details any `json:"details,omitempty"`
	// DetailsNull marks a nil Details as an explicit JSON null, which JSON writes as `"details":null`; without it a nil
	// Details is absent and JSON omits the key, as JSON.stringify omits undefined. It has no effect when Details is set.
	DetailsNull bool   `json:"-"`
	Usage       *Usage `json:"usage,omitempty"`
	// NestedCalls are the calls this tool made to other tools. Kept for the session record; not sent to the model.
	// upstream: .upstream/v0.99.1/packages/ai/src/types.ts:604
	NestedCalls *NestedToolCalls `json:"nestedCalls,omitempty"`
	IsError     bool             `json:"isError"`
	Timestamp   int64            `json:"timestamp"`
}

ToolResultMessage carries one tool execution result.

func (ToolResultMessage) MarshalJSON

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

func (*ToolResultMessage) UnmarshalJSON

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

UnmarshalJSON decodes a toolResult message. A details member that is JSON null sets DetailsNull; a missing one leaves Details nil and DetailsNull unset.

type ToolResultMessageContent

type ToolResultMessageContent interface {
	ContentBlock
	// contains filtered or unexported methods
}

ToolResultMessageContent is valid inside a tool-result message.

type ToolSchema

type ToolSchema struct {
	Name             string         `json:"name"`
	Description      string         `json:"description"`
	Parameters       map[string]any `json:"parameters"`                 // JSON Schema object
	PromptGuidelines []string       `json:"promptGuidelines,omitempty"` // Guidelines injected into system prompt
	// ConstrainedSampling is the optional provider-side constrained sampling
	// request for this tool (nil = unset, equivalent to upstream's false).
	// Mirrors upstream Tool.constrainedSampling (types.ts).
	ConstrainedSampling *ConstrainedSamplingConfig `json:"constrainedSampling,omitempty"`
	// ConstrainedSamplingDisabled records upstream's explicit `constrainedSampling: false`. It behaves like an
	// unset request for providers, but transcript tool declarations keep it (transcript.ts:123-129).
	ConstrainedSamplingDisabled bool `json:"-"`
	// contains filtered or unexported fields
}

ToolSchema describes an LLM-callable tool in JSON Schema format. JSON decoding and encoding retain schema declaration order for provider strict-schema derivation; directly authored Go maps use deterministic key order.

func GetCurrentTools

func GetCurrentTools(messages []Message) []ToolSchema

func GetDeclaredTools

func GetDeclaredTools(messages []Message) []ToolSchema

func ToToolDeclaration

func ToToolDeclaration(tool ToolSchema) ToolSchema

func (ToolSchema) MarshalJSON

func (tool ToolSchema) MarshalJSON() ([]byte, error)

MarshalJSON preserves imported parameter ordering when tool definitions cross a session or SDK boundary.

func (*ToolSchema) UnmarshalJSON

func (tool *ToolSchema) UnmarshalJSON(data []byte) error

UnmarshalJSON keeps schema key order as well as its validated data values.

type ToolStateChanges

type ToolStateChanges struct {
	ToolsAdded   []ToolSchema
	ToolsRemoved []ToolReference
}

func GetToolStateChanges

func GetToolStateChanges(previous, current []ToolSchema) ToolStateChanges

type TranscriptContext

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

TranscriptContext is the normalized provider-facing request. Only NormalizeContext and replay helpers construct one.

func CollapseSystemMessages

func CollapseSystemMessages(context TranscriptContext) TranscriptContext

func NormalizeContext

func NormalizeContext(context Context) TranscriptContext

NormalizeContext folds shorthand prompt/tools into a leading system message and normalizes missing content to empty block arrays.

func ResolveTranscript

func ResolveTranscript(context TranscriptContext, supportsMidConversationSystemMessages bool) TranscriptContext

func (TranscriptContext) Messages

func (context TranscriptContext) Messages() []Message

Messages returns a deep copy of the normalized transcript.

type TranscriptTools

type TranscriptTools struct {
	RequestTools     []ToolSchema
	AnchorsAdditions bool
}

func ResolveTranscriptTools

func ResolveTranscriptTools(messages []Message, supportsToolAdditions bool) TranscriptTools

type Transport

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

type UnsupportedStrictSchemaKeywordCheck

type UnsupportedStrictSchemaKeywordCheck func(key string, value any) bool

UnsupportedStrictSchemaKeywordCheck reports whether a provider's strict mode rejects a schema keyword with this value. Mirrors upstream UnsupportedStrictSchemaKeywordCheck.

type Usage

type Usage struct {
	Input        int       `json:"input"`
	Output       int       `json:"output"`
	CacheRead    int       `json:"cacheRead"`
	CacheWrite   int       `json:"cacheWrite"`
	CacheWrite1h *int      `json:"cacheWrite1h,omitempty"`
	Reasoning    *int      `json:"reasoning,omitempty"`
	TotalTokens  int       `json:"totalTokens"`
	Cost         UsageCost `json:"cost"`
}

Usage reports token usage from the provider.

func (Usage) MarshalJSON

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

MarshalJSON emits the upstream Usage wire shape.

func (*Usage) UnmarshalJSON

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

UnmarshalJSON decodes the upstream Usage wire shape.

type UsageCost

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

UsageCost is the provider-reported cost breakdown carried by upstream Usage.

func CalculateCost

func CalculateCost(m AnyModel, usage *Usage) UsageCost

CalculateCost fills usage.Cost from the prices of a chat, image or classifier model and returns it. A nil model has no price. Mirrors upstream models.ts:calculateCost(model: AnyModel, usage).

type UserContent

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

UserContent is the closed content union for user messages.

type UserContentBlock

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

UserContentBlock is valid inside a user message.

type UserContentBlocks

type UserContentBlocks []UserContentBlock

UserContentBlocks carries text and image user content.

type UserMessage

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

UserMessage is one user turn.

func (UserMessage) MarshalJSON

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

type UserText

type UserText string

UserText carries a plain user message.

Source Files

Jump to

Keyboard shortcuts

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