ai

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 90 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.

Ports packages/ai/src/utils/validation.ts.

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"
	// CloudflareWorkersAIRESTBaseURL is the Workers AI REST endpoint root; model runs are posted to `run` (api/cloudflare.ts:5-7).
	CloudflareWorkersAIRESTBaseURL      = "https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai"
	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 CloudflareGatewayBindingAuthSentinel = "cloudflare-gateway-binding"

CloudflareGatewayBindingAuthSentinel is the placeholder value of an auth header on a binding-routed request. API implementations require an API key or a recognized auth header before dispatch; binding calls are pre-authenticated, so a request sends `cf-aig-authorization: Bearer <sentinel>` to satisfy the check, with nil `Authorization` and `x-api-key` headers so no SDK placeholder reaches the gateway (CLOUDFLARE_GATEWAY_BINDING_AUTH_SENTINEL).

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 MinAnswerTokens = 1024

MinAnswerTokens is the number of tokens always left for the answer when a thinking budget shares the response ceiling. Mirrors upstream MIN_ANSWER_TOKENS (simple-options.ts:68).

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 ThinkingBindingControlsBeta = thinkingBindingControlsBeta

ThinkingBindingControlsBeta is the Anthropic beta that binds replayed signed thinking blocks to the system prompt and tools they were created with.

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 (
	// UserAgentDetailed is the version and platform form: the `"detailed"` mode.
	UserAgentDetailed = UserAgentMode{/* contains filtered or unexported fields */}
	// UserAgentShort is the product name alone: the `"short"` mode.
	UserAgentShort = UserAgentMode{/* contains filtered or unexported fields */}
)
View Source
var AITelemetrySchema = sync.OnceValue(func() *telemetry.TelemetrySchemaDefinition {
	return telemetry.DefineTelemetrySchema(&telemetry.TelemetrySchemaDefinition{
		Version: 1,
		Spans: map[string]telemetry.TelemetrySpanDefinition{
			"pi.ai.request": aiRequestSpan("One logical model request, including authentication and provider setup."),
			"pi.ai.poll":    aiRequestSpan("One deferred-response poll, not the deferred request's lifetime."),
		},
		Metrics: map[string]telemetry.TelemetryMetricDefinition{
			"pi.ai.request.duration": {
				Kind:        telemetry.TelemetryMetricKindHistogram,
				Description: "Logical request or poll duration.",
				Unit:        "ms",
				Attributes:  map[string]telemetry.TelemetryMetricAttributeDefinition{"provider": aiProvider, "model": aiModel, "stop": aiMetricStop()},
			},
			"pi.ai.first_chunk.duration": {
				Kind:        telemetry.TelemetryMetricKindHistogram,
				Description: "Time to the first normalized stream event.",
				Unit:        "ms",
				Attributes:  map[string]telemetry.TelemetryMetricAttributeDefinition{"provider": aiProvider, "model": aiModel},
			},
			"pi.ai.tokens": {
				Kind:        telemetry.TelemetryMetricKindCounter,
				Description: "Reported model token usage; pending and deferred polls are excluded.",
				Unit:        "token",
				Attributes: map[string]telemetry.TelemetryMetricAttributeDefinition{
					"provider": aiProvider,
					"model":    aiModel,
					"kind": func() telemetry.TelemetryMetricAttributeDefinition {
						kind := aiAttribute(telemetry.TelemetryAttributeTypeString, "Token usage category.", true)
						kind.Values = []string{"input", "output", "cache_read", "cache_write"}
						return kind
					}(),
				},
			},
			"pi.ai.cost": {
				Kind:        telemetry.TelemetryMetricKindCounter,
				Description: "Reported model cost; pending and deferred polls are excluded.",
				Unit:        "USD",
				Attributes:  map[string]telemetry.TelemetryMetricAttributeDefinition{"provider": aiProvider, "model": aiModel},
			},
		},
	})
})

AITelemetrySchema is Pi's AI_TELEMETRY_SCHEMA: Pi-owned request metadata, bounded error status, events, and low-cardinality metrics. The schema is built on first use, not at package init, so a process that never reads it pays nothing; every call returns the same schema.

View Source
var GeneratedClassifierModels = []ClassifierModel{
	{ID: "@cf/cloudflare/clef", Name: "Clef", 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", "image"}, Cost: ModelCost{Input: 0.24, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 65536, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("images")}},
	{ID: "@cf/cloudflare/clef-flash", Name: "Clef Flash", 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", "image"}, Cost: ModelCost{Input: 0.038, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 24576, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("images")}},
	{ID: "@cf/cloudflare/clef-omni", Name: "Clef Omni", 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", "image"}, Cost: ModelCost{Input: 0.15, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 64000, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("images")}},
	{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", "image"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("assets")}},
	{ID: "typesafe/jev-1.14.0", Name: "Jev 1.14", 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", "image"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("assets")}},
	{ID: "gpt-6-luna", Name: "GPT-6 Luna", API: ClassifierAPI("openai-decisions"), Provider: "openai", BaseURL: "https://api.openai.com/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.1, Output: 0, CacheRead: 0, CacheWrite: 0, Tiers: []CostTier{{InputTokensAbove: 272000, InputCostPer1M: 0.2, OutputCostPer1M: 0, CacheReadCostPer1M: 0, CacheWriteCostPer1M: 0}}}, ContextWindow: 922000},
	{ID: "jev-1.13", Name: "Jev 1.13", API: ClassifierAPI("typesafe-system-one"), Provider: "opencode", BaseURL: "https://opencode.ai/inference/systemone/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/inference/systemone/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 32000},
	{ID: "~liquid/d1-latest", Name: "LiquidAI: d1 Latest", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.02, Output: 0, CacheRead: 0.02, CacheWrite: 0}, ContextWindow: 65536, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "~liquid/d1-ultra-latest", Name: "LiquidAI: d1-ultra Latest", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0.042, CacheWrite: 0}, ContextWindow: 32768, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "~perplexity/pplx-decider-flash-latest", Name: "Perplexity: Decider Flash Latest", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.01, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "~perplexity/pplx-decider-latest", Name: "Perplexity: Decider Latest", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.02, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "~typesafe/jev-latest", Name: "TypeSafe: Jev Latest", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "baseten/elephant-1", Name: "Baseten: Elephant 1", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.035, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 16384, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "baseten/joey-1", Name: "Baseten: Joey 1", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.01, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 16384, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "baseten/mammoth-1", Name: "Baseten: Mammoth 1", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.1, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 16384, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "cloudflare/clef", Name: "Cloudflare: Clef", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.24, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 65536, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "cloudflare/clef-flash", Name: "Cloudflare: Clef Flash", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.038, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 24576, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "cloudflare/clef-omni", Name: "Cloudflare: Clef Omni", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.15, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 65536, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "inception/mercury-decide", Name: "Inception: Mercury Decide", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.02, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 65536, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{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, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "liquid/d1", Name: "LiquidAI: d1", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.02, Output: 0, CacheRead: 0.02, CacheWrite: 0}, ContextWindow: 65536, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "liquid/d1-ultra", Name: "LiquidAI: d1-ultra", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0.042, CacheWrite: 0}, ContextWindow: 32768, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "liquid/d1.1", Name: "LiquidAI: d1.1", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.02, Output: 0, CacheRead: 0.02, CacheWrite: 0}, ContextWindow: 65536, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "microsoft/microsoft-decision-1", Name: "Microsoft: Microsoft-Decision-1", 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, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "nace-ai/drex-max", Name: "Nace.AI: Drex Max", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.06, Output: 0, CacheRead: 0.06, CacheWrite: 0}, ContextWindow: 262144, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "nace-ai/drex-v1.5", Name: "Nace.AI: Drex v1.5", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.04, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 131072, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "nace-ai/drex-v2.0", Name: "Nace.AI: Drex v2.0", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.06, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 131072, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "openai/gpt-6-luna-decisions", Name: "OpenAI: GPT-6 Luna Decisions", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.1, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 1050000, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "perplexity/pplx-decider-flash-v1.2", Name: "Perplexity: Decider Flash V1.2", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.01, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "perplexity/pplx-decider-v1.2", Name: "Perplexity: Decider V1.2", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.02, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{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, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{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, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{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, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "respan/span-02", Name: "Respan: Span-02", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.01, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 131072, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "scaledown/classify-1", Name: "ScaleDown: Classify-1", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.04, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 1000000, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{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, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{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", "image"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "typesafe/jev-1.14", Name: "TypeSafe: Jev 1.14", API: ClassifierAPI("typesafe-system-one"), Provider: "openrouter", BaseURL: "https://openrouter.ai/api/v1", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0.042, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{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, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "upstage/solar-decide-flash", Name: "Upstage: Solar Decide Flash", 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, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("state-image-urls"), SendImageOptInHeader: new(false)}},
	{ID: "jev-latest", Name: "Jev", API: ClassifierAPI("typesafe-system-one"), Provider: "typesafe", BaseURL: "https://api.typesafe.ai/v1/", Input: []string{"text", "image"}, Cost: ModelCost{Input: 0, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 64000, Compat: &SystemOneCompat{ImageInput: SystemOneImageInput("assets"), SendImageOptInHeader: new(true)}},
	{ID: "baseten/elephant-1", Name: "Elephant-1", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.035, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 16384},
	{ID: "baseten/joey-1", Name: "Joey-1", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.01, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 16384},
	{ID: "baseten/mammoth-1", Name: "Mammoth-1", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.1, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 16384},
	{ID: "convaiinnovations/laya", Name: "Laya", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.0105, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 8192},
	{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.02, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 65536},
	{ID: "liquid/d1-ultra", Name: "Liquid d1 Ultra", 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: 32768},
	{ID: "microsoft/microsoft-decision-1", Name: "Decision-1", 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: 32768},
	{ID: "openai/gpt-6-luna-decisions", Name: "GPT-6 Luna Decisions", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.1, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 1050000},
	{ID: "perplexity/pplx-decider-flash-v1.2", Name: "Decider Flash V1.2 4B", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.01, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144},
	{ID: "perplexity/pplx-decider-v1.2", Name: "Decider V1.2 27B", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.02, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 262144},
	{ID: "scaledown/scaledown-classify-1", Name: "Classify-1", API: ClassifierAPI("typesafe-system-one"), Provider: "vercel-ai-gateway", BaseURL: "https://ai-gateway.vercel.sh/typesafe/v1", Input: []string{"text"}, Cost: ModelCost{Input: 0.05, Output: 0, CacheRead: 0, CacheWrite: 0}, ContextWindow: 4096},
	{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},
	{ID: "typesafe-ai/jev-1.13.0", Name: "Jev 1.13.0", 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},
	{ID: "typesafe-ai/jev-1.14.0", Name: "Jev 1.14.0", 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 (56 entries).

View Source
var GeneratedImageModels = []ImageModel{
	{ID: "black-forest-labs/flux-3-image", Name: "Black Forest Labs: FLUX 3 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: "black-forest-labs/flux-3-image-klein", Name: "Black Forest Labs: FLUX 3 Image Klein", 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-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-flash", Name: "ByteDance Seed: Seedream 5.0 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: 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: "google/gemini-nano-banana-2.1", Name: "Google: Nano Banana 2.1", 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: 1.5, Output: 7.5, CacheRead: 0, CacheWrite: 0}},
	{ID: "krea/k3-edit", Name: "Krea: K3 Edit", 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: 0, Output: 0, 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: 0, Output: 0, CacheRead: 0, CacheWrite: 0}},
	{ID: "qwen/qwen-image-2.1-pro", Name: "Qwen: Qwen Image 2.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: "qwen/qwen-image-2.1-turbo", Name: "Qwen: Qwen Image 2.1 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: "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: "sourceful/riverflow-v2.6-pro", Name: "Sourceful: Riverflow V2.6 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: "tencent/hy-image-v3.5-preview", Name: "Tencent: Hy Image 3.5 Preview", 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 (64 entries).

View Source
var GeneratedModelDataGeneratedAt = new(float64(1793616208346))

GeneratedModelDataGeneratedAt is the generation time shared by every built-in provider catalog, in Unix milliseconds (providers/all.ts getBuiltinModelDataGeneratedAt); nil when the data manifest carries no valid time.

View Source
var GeneratedModels = generatedCatalog()

GeneratedModels is the static catalog (1592 entries). generatedCatalog gives its rows the shared maps the tables below hold.

View Source
var GeneratedProviders = []string{
	"amazon-bedrock",
	"ant-ling",
	"anthropic",
	"azure",
	"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 OpenRouterRoutingVariants = []string{"nitro", "floor"}

OpenRouterRoutingVariants are the routing variants of OpenRouter that PiG offers for every OpenRouter chat model (D199): `:nitro` sorts the model's providers by throughput and `:floor` by price. OpenRouter accepts them on any model id and never lists them in /models, so Pi's catalog has none; https://openrouter.ai/docs/guides/routing/model-variants/overview. The built-in catalog does not store them: a variant is derived from its base entry when it is looked up (LookupModel, LookupModelExact) or listed (ListModels, WithOpenRouterRoutingVariants).

PiAttribution is the attribution used by the agent loop, durable Harness and coding agent (upstream PI_ATTRIBUTION).

pig divergence (D26): the values are PiG's brand (internal/coding/pigidentity), not pi's `{ name: "pi", url: "https://pi.dev", categories: ["cli-agent"] }`.

View Source
var ProductVersion = pigversion.Version

ProductVersion is PiG's composite release version for outbound identifiers: the default version of a detailed User-Agent (PI_AI_VERSION upstream). 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 AbortReason

func AbortReason(ctx context.Context) error

AbortReason is signal.reason for a cancelled ctx: the error an aborted operation rejects with, as signal.throwIfAborted() and raceWithAbortSignal do. A plain cancellation is Node's DOMException AbortError, "This operation was aborted", wrapping context.Canceled; a cancellation cause keeps its own error. It is nil while ctx is live.

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 AppendAssistantMessageDiagnostic

func AppendAssistantMessageDiagnostic(message *AssistantMessage, diagnostic AssistantMessageDiagnostic)

AppendAssistantMessageDiagnostic replaces the message's diagnostics with a new slice that ends with diagnostic, so a slice shared with another message is never written through (utils/diagnostics.ts appendAssistantMessageDiagnostic).

func ApplyHTTPProxySettings

func ApplyHTTPProxySettings(httpProxy string) error

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

func ApplyOmittedReasoning

func ApplyOmittedReasoning(model *Model, options *StreamOptions)

ApplyOmittedReasoning sets the native thinking options a simple stream sends when `reasoning` is omitted: Anthropic disables thinking and Google and Vertex send `thinking: { enabled: false }` unless the caller supplied native thinking.

func AssertChatModel

func AssertChatModel(model AnyModel) error

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

func AssertClassifierInputSupported

func AssertClassifierInputSupported(model *ClassifierModel, request ClassifierContext) error

AssertClassifierInputSupported rejects classifier images for a model whose catalog entry does not accept image input (model-operations.ts assertClassifierInputSupported).

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 AwaitContinuation

func AwaitContinuation[T any](turn *continuationTurn, promise *continuationPromise[T]) T

func BuildCopilotDynamicHeaders

func BuildCopilotDynamicHeaders(messages []Message, hasImages bool) map[string]string

BuildCopilotDynamicHeaders returns the per-request Copilot headers. Ports github-copilot-headers.ts buildCopilotDynamicHeaders.

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 CatalogModelJSON

func CatalogModelJSON(model AnyModel) (data json.RawMessage, ok bool, err error)

CatalogModelJSON is the JSON object of a model that came from a catalog record, the built-in catalog or a models store, as Pi serializes that model object: the record's members in the record's order, with the record's own bytes for every member the model still holds at its catalog value. ok is false for a model without a record.

func CatalogRecordOf

func CatalogRecordOf(model *Model) string

CatalogRecordOf is the catalog record model was made from, its built-in catalog entry or the models store record it was decoded from, as a JSON object in the record's member order, or "" for a model of neither. A model built from it again (GeneratedModel.Record) encodes with CatalogModelJSON in that order.

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 ClampThinkingBudgetToAnswerRoom

func ClampThinkingBudgetToAnswerRoom(thinkingBudget, ceiling int) int

ClampThinkingBudgetToAnswerRoom caps a thinking budget so at least MinAnswerTokens remain under a shared response ceiling. Mirrors upstream clampThinkingBudgetToAnswerRoom (simple-options.ts:88).

func CleanupSessionResources

func CleanupSessionResources(sessionID string) error

CleanupSessionResources calls every registered cleanup hook with the session id, in registration order, and keeps going when one panics (a JavaScript throw). It returns nil when every hook returned, and otherwise an error whose message is "Failed to cleanup session resources" and that wraps each hook's failure (session-resources.ts cleanupSessionResources throws an AggregateError). An empty session id is `undefined`.

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 is Pi's getAccountId (openai-codex.ts:310): the chatgpt_account_id claim of the access token, "" when the token lacks the JWT shape or the claim. Pi's decodeJwt reads the payload with atob and JSON.parse, so the claim is read as Latin-1 characters of the decoded bytes.

func CompareThinkingLevels

func CompareThinkingLevels(a, b ModelThinkingLevel) 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 ContextTransportError

func ContextTransportError(ctx context.Context) error

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 DateParse

func DateParse(s string) float64

DateParse is JavaScript's Date.parse of a JavaScript string: milliseconds since the epoch, or NaN when V8 does not recognise the string. A string that names no zone is local time. The Go string is UTF-8, so U+00A0 and U+FEFF are single white space code units as in V8.

func DateParseHeader

func DateParseHeader(s string) float64

DateParseHeader is Date.parse of an HTTP header value, which Node holds as a ByteString: each byte is one code unit.

func DateParseIn

func DateParseIn(s string, local *time.Location) float64

DateParseIn is DateParse with the zone V8 applies to a string that names none: the zone of a caller that does not use the process's local one.

func DeclarationsEqual

func DeclarationsEqual(left, right ToolSchema) bool

DeclarationsEqual reports whether two tools marshal to the same declaration, prompt guidelines aside. Plain trees are compared in place; anything else is marshaled, as before.

func EncodeModelsCatalog

func EncodeModelsCatalog(models []AnyModel) ([]json.RawMessage, error)

EncodeModelsCatalog writes models as their persisted JSON records, the Pi model shapes a provider object receives and returns.

func EncodeStoredModel

func EncodeStoredModel(model AnyModel) (json.RawMessage, error)

EncodeStoredModel is the persisted catalog record of a model; a model that was decoded from a record re-encodes with its unknown fields and field order.

func ErrorKeyBeforeTimestamp

func ErrorKeyBeforeTimestamp(data []byte) bool

ErrorKeyBeforeTimestamp reports whether the top-level "errorMessage" member of an assistant message object precedes its "timestamp" member: JavaScript keeps the order a message was built in, and a stored message decodes to the order it holds.

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 EstimateTextAndImageContentTokens

func EstimateTextAndImageContentTokens(content UserContent) int

EstimateTextAndImageContentTokens estimates string or block content at 3.5 characters per token, counting each image as 4800 characters (estimate.ts estimateTextAndImageContentTokens).

func EstimateTextTokens

func EstimateTextTokens(text string) int

EstimateTextTokens estimates text at 3.5 characters per token.

func ExtensionModelRecord

func ExtensionModelRecord(definition json.RawMessage, api API, providerID, baseURL string) string

ExtensionModelRecord is the catalog record of the chat model an extension registers from definition, a model definition as the extension wrote it: Pi's `{ ...definition, api, provider, baseUrl, headers: undefined }` (provider-composer.ts extensionModelFromDefinition). The definition's members keep the extension's order; api, provider and baseUrl take their place when the definition has them and follow in that order when it does not; headers is undefined, so it is no member of the object. A model built from the record (GeneratedModel.Record) encodes with CatalogModelJSON as Pi serializes it. A definition that is not a JSON object has no record ("").

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 FirstNonEmptyString

func FirstNonEmptyString(vals ...string) string

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 FormatThrownValue

func FormatThrownValue(value any) string

FormatThrownValue renders a thrown value as text: an error reports its message, or its name when the message is empty, a string is itself, and anything else is formatted like String(value). A nil value is `undefined` (utils/diagnostics.ts formatThrownValue).

func FormatUserAgent

func FormatUserAgent(identity *ClientIdentity) string

FormatUserAgent is the `User-Agent` value of an identity.

pig divergence (D65): see UserAgentMode.

func GetAttributionHeaders

func GetAttributionHeaders(target ClientIdentityTarget, identity *ClientIdentity) map[string]string

GetAttributionHeaders are the provider attribution headers of an identity for a request target. Empty when attribution is off.

func GetBuiltinModelDataGeneratedAt

func GetBuiltinModelDataGeneratedAt() *float64

GetBuiltinModelDataGeneratedAt returns the generation time shared by every built-in provider catalog, in Unix milliseconds, or nil when the data manifest records no valid time. Mirrors upstream providers/all.ts:getBuiltinModelDataGeneratedAt.

func GetClientVersion

func GetClientVersion(identity *ClientIdentity) string

GetClientVersion is the version in the detailed `User-Agent`.

func GetCurrentSystemPrompt

func GetCurrentSystemPrompt[M RoleMessage](messages TranscriptMessages[M]) 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 GetIdentityHeaders

func GetIdentityHeaders(target ClientIdentityTarget, identity *ClientIdentity) map[string]string

GetIdentityHeaders are the default headers an identity adds to a request: `User-Agent` and provider attribution. APIs apply them below model, provider, and request headers, so configured headers override them and null request headers remove them.

func GetJSONSchemaToolParameters

func GetJSONSchemaToolParameters(tool ToolSchema, strict *bool) (jsonSchemaToolParameters, error)

func GetOverflowPatterns

func GetOverflowPatterns() []*regexp.Regexp

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

func GetProviderEnvValue

func GetProviderEnvValue(name string, env ProviderEnv) string

func GetSystemMessageText

func GetSystemMessageText(message SystemMessage) string

GetSystemMessageText renders a system message as its content followed by its non-removed sections (upstream getSystemMessageText).

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 HasCopilotVisionInput

func HasCopilotVisionInput(messages []Message) bool

HasCopilotVisionInput reports whether any message carries image content, including images returned by tools (e.g. the read tool reading a PNG). Triggers the Copilot-Vision-Request header. Ports github-copilot-headers.ts hasCopilotVisionInput, which checks both user messages and tool-result messages for image blocks.

func HasNonAdditiveToolChanges

func HasNonAdditiveToolChanges[M RoleMessage](messages TranscriptMessages[M]) bool

func HasToolRedefinitions deprecated

func HasToolRedefinitions[M RoleMessage](messages TranscriptMessages[M]) bool

HasToolRedefinitions reports whether a tool name was declared twice with different definitions. A transport that can only reference previously declared tools by name cannot replay such a history.

Deprecated: No built-in transport needs this anymore: Anthropic expresses redefinitions with inline tool_definition blocks. Pi keeps it for API compatibility.

func HttpBodyReadOwner

func HttpBodyReadOwner(conn net.Conn) *bodyReadOwner

func InferCopilotInitiator

func InferCopilotInitiator(msgs []Message) string

InferCopilotInitiator returns "agent" if the most recent message is from the assistant or a tool result, "user" otherwise. Ports packages/ai/src/api/github-copilot-headers.ts inferCopilotInitiator.

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 IsGoTransportError

func IsGoTransportError(err error) bool

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 IsOpenRouterBase

func IsOpenRouterBase(provider, id string) bool

IsOpenRouterBase reports whether the model id of provider is an OpenRouter model that takes routing variants, one that is not itself a variant.

func IsRecord

func IsRecord(value json.RawMessage) bool

IsRecord reports whether value is a JSON object, like classifier-shared.ts isRecord (a non-null, non-array object).

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 IsStrictToolLimitErrorMessage

func IsStrictToolLimitErrorMessage(message string) bool

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). It counts the text JSON.stringify writes: no HTML escaping, negative zero as 0, line separators literally.

func JsAwait

func JsAwait[T any](awaiter jsAwaiter, operand jsOperand[T]) (T, error)

func JsPromiseOperand

func JsPromiseOperand[T any](promise *jsPromise[T]) jsOperand[T]

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 MakeStrictJSONSchema

func MakeStrictJSONSchema(parameters map[string]any, isUnsupportedKeyword UnsupportedStrictSchemaKeywordCheck) (map[string]any, error)

MakeStrictJSONSchema converts a tool schema to the strict subset accepted by provider constrained sampling without mutating the authored schema. A nil isUnsupportedKeyword rejects no keyword. A Go map has no key order, so objects take their keys in sorted order here; a ToolSchema keeps its authored order through resolveJSONSchemaStrictSampling.

func MarshalInRecordedOrder

func MarshalInRecordedOrder(value any, order schemaObjectOrder) ([]byte, error)

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 MarshalSchemaWithOrder

func MarshalSchemaWithOrder(value any, order schemaObjectOrder, path string) ([]byte, error)

func MarshalToolParameters

func MarshalToolParameters(parameters map[string]any, order schemaObjectOrder) ([]byte, error)

func ModelProviderID

func ModelProviderID(model *Model) string

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 NewAssistantStreamBuilder

func NewAssistantStreamBuilder(ctx context.Context, api API, provider string, model string) *assistantStreamBuilder

func NewDiagnosticDetails

func NewDiagnosticDetails() diagnosticDetails

func NewIdleTimeoutConn

func NewIdleTimeoutConn(conn net.Conn, network string) *idleTimeoutConn

func NewObservedBodyReadiness

func NewObservedBodyReadiness(body *observedResponseBody) *observedBodyReadiness

func NewObservedProviderBuilder

func NewObservedProviderBuilder(ctx context.Context, api API, provider string, model string) *assistantStreamBuilder

func NewProviderError

func NewProviderError(message string, status *int, body any) error

NewProviderError is an error that carries the status and body of the response it reports, as an SDK error does.

func NodeNetErrorMessage

func NodeNetErrorMessage(err error) (string, bool)

func NormalizeJSONValue

func NormalizeJSONValue(value any) (any, error)

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) (timeout time.Duration, ok bool)

OAuthRefreshTimeout returns the timeout that alone bounds the refresh signal, or ok false when the refresh signal is a caller's cancellation.

func OAuthSuccessHTML

func OAuthSuccessHTML(message string) string

OAuthSuccessHTML returns the HTML page shown on successful OAuth callback.

func ObserveProviderResponse

func ObserveProviderResponse(ctx context.Context, opts StreamOptions, response *http.Response, model *Model) error

func OfferedModels

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

OfferedModels returns models without the chat models of a stripped API. The generated catalog listing (ListModels) and every Models collection read pass their models through it, so a stripped API's models are offered nowhere: not by --list-models, /model, the --models scope, startup's default model, RPC or the extension model registry. An exact catalog lookup (LookupModel, LookupModelExact) still finds such a model, so that using one reports the strip. pig additive (D92): Stock PiG strips no API and returns models unchanged.

func OpenRouterRoutingVariant

func OpenRouterRoutingVariant(provider, id string) (base, variant string, ok bool)

OpenRouterRoutingVariant splits the id of a model of provider into its base id and routing variant. ok is false for any model that is not an OpenRouter routing variant.

func OperationSignal

func OperationSignal(signal context.Context) context.Context

OperationSignal normalizes an optional public signal without imposing a deadline: a nil signal becomes a context that is never cancelled.

upstream: packages/ai/src/utils/abort.ts:operationSignal

func ParseJSONWithRepair

func ParseJSONWithRepair[T any](data string) (T, error)

ParseJSONWithRepair parses json into T and, when strict parsing fails, parses the repairJSON rewrite instead. It returns the strict error when the repair changes nothing (upstream utils/json-parse.ts parseJsonWithRepair).

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. Times are floating-point milliseconds as in upstream, so a NaN or infinite interval or expiry behaves as it does there. Mirrors upstream pollOAuthDeviceCodeFlow.

func PostClassifierRequest

func PostClassifierRequest(ctx context.Context, label, target string, model ClassifierModel, payload any, options ClassifierOptions, noRetryStatuses []int) (json.RawMessage, error)

PostClassifierRequest posts one JSON classifier request with bearer auth, the OnPayload and OnResponse hooks, a fresh timeout per attempt, and provider retries. It returns the response body, which is valid JSON; failures are errors. noRetryStatuses lists HTTP statuses that fail at once although they are normally retried.

func PrefersJSONSchemaStrictSampling

func PrefersJSONSchemaStrictSampling(tool ToolSchema) bool

func ProviderBaseURL

func ProviderBaseURL(providerID string) string

ProviderBaseURL returns the upstream provider.baseUrl of a built-in provider, or "" when the provider declares none.

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 ProviderStripped

func ProviderStripped(providerID string) bool

ProviderStripped reports whether every generated catalog model of providerID is on a stripped API, so the provider offers no model and is no login provider. A provider without catalog models is never stripped. Matching ignores case. pig additive (D92): Stock PiG strips no API, so this is always false there.

func ProvidersRunOnCaller

func ProvidersRunOnCaller(ctx context.Context) bool

ProvidersRunOnCaller reports whether ctx belongs to an operation that carries a file-read snapshot over this package's own stores (WithFileReadSnapshotOnCaller), and so runs no extension code and reads no caller-supplied store.

func RaceWithAbortSignal

func RaceWithAbortSignal[T any](signal context.Context, operation func() (T, error)) (T, error)

RaceWithAbortSignal runs operation and stops waiting for it when signal is cancelled. The operation is never interrupted: it runs to completion on its own goroutine and its result is dropped, so an abandoned operation is observed through settlement as in the upstream promise race. The cancellation cause is returned when the signal wins, including a signal that is already cancelled when the race starts; otherwise the operation's value and error are returned. A nil signal waits for the operation alone.

upstream: packages/ai/src/utils/abort.ts:raceWithAbortSignal

func RegisterAPIProvider

func RegisterAPIProvider(provider APIProvider, sourceID ...string)

RegisterAPIProvider registers an API implementation under an optional source id, replacing any earlier one for the same API (compat.ts registerApiProvider). The registry holds a wrapped copy whose streams reject a model of another API. The built-in implementations register first, as compat.ts registers them when it loads, so an override replaces the built-in.

func RegisterBuiltInAPIProviders

func RegisterBuiltInAPIProviders()

RegisterBuiltInAPIProviders registers the built-in API implementations without replacing an entry that is already registered, so an override registered earlier survives (compat.ts registerBuiltInApiProviders). The registry runs it once at first use, as compat.ts runs it on import.

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, sourceID ...string)

RegisterImagesAPIProvider registers or replaces an image API provider. The optional sourceID names the registering source and is stored on the entry (registerImagesApiProvider(provider, sourceId?)); Pi keeps it without reading it.

func RegisterOAuthProvider

func RegisterOAuthProvider(id string, provider OAuthProviderInterface)

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

func RegisterOptionalAPI

func RegisterOptionalAPI(registration OptionalAPIRegistration)

RegisterOptionalAPI makes the provider of an optional built-in API available to every factory of this process. A provider package calls it from init, so importing the package is what links the provider. It panics on an API that is not optional, on a missing constructor and on a second registration.

func RegisterSessionResourceCleanup

func RegisterSessionResourceCleanup(cleanup SessionResourceCleanup) func()

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

func ReleaseRequestCaches

func ReleaseRequestCaches()

ReleaseRequestCaches forgets every encoding the request caches hold: large strings and finalized messages. The next request encodes what it sends again, as it would have the first time; a build that runs meanwhile finds nothing to splice and encodes its values itself, so its body is the same. pig additive (D112): an interactive session that went idle calls it from its idle collection (D172), so the encodings of the conversation it sent do not stay live while it waits.

func RenderSystemMessageUpdate

func RenderSystemMessageUpdate(message SystemMessage) string

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

func RequiredNumber

func RequiredNumber(label string, value json.RawMessage, field string) (float64, error)

func ResetAPIProviders

func ResetAPIProviders()

ResetAPIProviders clears the registry and registers the built-in implementations again (compat.ts resetApiProviders).

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 ResolveAzureBaseURL

func ResolveAzureBaseURL(modelBaseURL string, options AzureEndpointOptions) (string, error)

ResolveAzureBaseURL resolves the endpoint from the explicit option, AZURE_OPENAI_BASE_URL, the resource name, then the model's base URL, and normalizes it.

func ResolveAzureDeploymentName

func ResolveAzureDeploymentName(modelID string, options AzureEndpointOptions) string

ResolveAzureDeploymentName returns the request model's deployment name: the explicit option, else the AZURE_OPENAI_DEPLOYMENT_NAME_MAP entry for the model, else the model ID. The map is parsed as upstream parseDeploymentNameMap does: each entry is trimmed with String.prototype.trim and split with split("=", 2), which drops the text after a second "="; a later entry for the same model replaces an earlier one; and a model mapped to an empty name falls back to the model ID.

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 ResolveJSONSchemaStrictSampling

func ResolveJSONSchemaStrictSampling(tool ToolSchema, supportsStrictMode bool, isUnsupportedKeyword UnsupportedStrictSchemaKeywordCheck) (*bool, error)

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 RetainThoughtSignature

func RetainThoughtSignature(existing, incoming string) string

RetainThoughtSignature keeps the signature already on a streamed block unless the incoming part carries a non-empty one. Ports packages/ai/src/api/google-shared.ts retainThoughtSignature: it never moves signatures across parts, it only prevents an overwrite with an empty value within one block.

func RetryDelayMs

func RetryDelayMs(policy RetryPolicy, attempt int) int

RetryDelayMs returns the capped exponential delay for a 1-based attempt from the policy base delay and agent cap (only BaseDelayMs and MaxAgentDelayMs are read). Overflowing products saturate at Number.MAX_SAFE_INTEGER before the cap.

func RetryGoogleRequest

func RetryGoogleRequest[T any](ctx context.Context, request func() (T, error), options ...StreamOptions) (T, error)

RetryGoogleRequest is google-shared.ts retryGoogleRequest: retryProviderRequest for the Google SDK, whose ApiError has a status but no headers. A status-only error is given `headers: undefined`, so it is retried by status, and the caller still receives the error it threw. Retries and the delay cap come from the stream options; the context is their AbortSignal.

func RetryProviderRequest

func RetryProviderRequest[T any](ctx context.Context, request func() (T, error), options ProviderRetryOptions) (T, error)

RetryProviderRequest ports provider-retry.ts retryProviderRequest: the request runs again after a retryable ProviderRequestError, the backoff sleep is interruptible by ctx, and a provider-requested delay above MaxRetryDelayMs fails at once. A failure that is not a ProviderRequestError, or whose status is in NoRetryStatuses, is returned as is.

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 encoding fails.

func SanitizeSurrogates

func SanitizeSurrogates(s string) string

func ShortHash

func ShortHash(s string) string

ShortHash ports upstream packages/ai/src/utils/hash.ts shortHash: a 32-bit non-cryptographic hash rendered as two base36 numbers. Iterating UTF-16 code units with uint32 arithmetic (matching JS Math.imul low-32-bit wrap and `>>>`) keeps the output byte-identical to upstream for the tool-call ids it hashes.

func Sleep

func Sleep(ctx context.Context, ms float64) error

Sleep waits ms milliseconds as Node's setTimeout would, or until ctx is cancelled. A context that is already cancelled fails at once, even for a zero delay, and a cancellation during the wait stops the timer. Both return the cancellation cause, as the upstream promise rejects with signal.reason.

upstream: packages/ai/src/utils/sleep.ts:1

func StringEnum

func StringEnum(values []string, options *StringEnumOptions) schema.Schema

StringEnum creates a string enum schema compatible with Google's API and other providers that don't support anyOf/const patterns: {"type":"string","enum":[...]}, plus the description and the default when they are non-empty (string-enum.ts:14-24). The schema is an ordered object, so it serializes in the member order Pi writes, and its enum is the caller's slice, as Pi's is the caller's array.

operation := StringEnum([]string{"add", "subtract", "multiply", "divide"}, &StringEnumOptions{Description: "The operation to perform"})

func StrippedModelError

func StrippedModelError(providerID, modelID string) error

StrippedModelError reports the strip that keeps a catalog model from being offered: the stripped API of providerID's catalog model modelID or, when that catalog lacks modelID, the stripped API of a provider whose catalog models are all on stripped APIs. An empty providerID matches modelID under every provider. Matching ignores case, as model resolution does. It returns nil when no strip explains the model's absence. pig additive (D92): Stock PiG strips no API, so this is always nil there.

func SuspendContinuation

func SuspendContinuation(turn *continuationTurn)

func ThinkingBudgetForLevel

func ThinkingBudgetForLevel(reasoningLevel string, custom *ThinkingBudgets) int

ThinkingBudgetForLevel is the token budget for a reasoning level: a configured budget replaces the default, whatever its value, and xhigh and max use the high budget. A level without a budget (for example "off") yields zero. Mirrors upstream thinkingBudgetForLevel (api/simple-options.ts:81).

func ToolChoiceName

func ToolChoiceName(choice any) (string, bool)

func TrimJSWhitespace

func TrimJSWhitespace(value string) string

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 UnregisterAPIProviders

func UnregisterAPIProviders(sourceID string)

UnregisterAPIProviders removes every implementation registered under a source id (compat.ts unregisterApiProviders).

func UnregisterOAuthProvider

func UnregisterOAuthProvider(id string)

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

func UsesGoogleThinkingLevel

func UsesGoogleThinkingLevel(model *Model) bool

UsesGoogleThinkingLevel reports whether the model takes Gemini's discrete thinkingLevel control instead of the token-based thinkingBudget control. The levels a model supports come from its thinking-level map; this only selects the Google wire format. Mirrors upstream usesGoogleThinkingLevel (google-shared.ts), which lowercases the id before every comparison.

func Utf16Length

func Utf16Length(value string) int

func ValidateProviderRequest

func ValidateProviderRequest(ctx context.Context, transcript TranscriptContext) error

func ValidateToolArguments

func ValidateToolArguments(tool ToolSchema, call ToolCall) (map[string]any, error)

ValidateToolArguments coerces and validates the call's arguments against the tool's JSON Schema with pi-schema, without changing the provider call (validation.ts:54-69). The arguments are read in the call's member order, as Pi's are, so the received arguments of a validation error list them in that order. The schema is read in its authored member order (ParametersJSON), as Pi validates tool.parameters itself, so its errors follow that order.

func ValidateToolArgumentsJSON

func ValidateToolArgumentsJSON(toolName string, schema, args json.RawMessage) (json.RawMessage, error)

ValidateToolArgumentsJSON validates arguments against a JSON schema (ToolSchema.ParametersJSON of a tool, or any JSON Schema text) and returns the coerced arguments as JSON.stringify writes them, in the member order of args, which is what an agent passes to hooks and execution. An empty schema accepts any object.

func ValidateToolArgumentsSchema

func ValidateToolArgumentsSchema(toolName string, schema map[string]any, args json.RawMessage) (json.RawMessage, error)

ValidateToolArgumentsSchema is ValidateToolArgumentsJSON for a schema held as a map; a nil schema accepts any object.

func ValidateToolCall

func ValidateToolCall(tools []ToolSchema, call ToolCall) (map[string]any, error)

ValidateToolCall finds the tool the call names and validates the call's arguments against its schema. It returns an error when no tool has the name or validation fails.

func WithContinuationExecutor

func WithContinuationExecutor(ctx context.Context) (context.Context, *continuationExecutor)

func WithFileReadSnapshot

func WithFileReadSnapshot(ctx context.Context) context.Context

WithFileReadSnapshot starts one model operation's file-read snapshot: its auth.json and models-store.json reads stat once while no write replaces what they read. Only an operation that runs no extension code between its reads may carry one.

func WithFileReadSnapshotOnCaller

func WithFileReadSnapshotOnCaller(ctx context.Context) context.Context

WithFileReadSnapshotOnCaller is WithFileReadSnapshot for an operation whose credential and models stores are this package's own (the file and in-memory ones): an offline one does no I/O but reads of their one view, so its providers may run one after another on the caller (ProvidersRunOnCaller). A caller-supplied store reads with the caller's code, which can wait on anything; Pi races each provider's refresh and check with the caller's signal (models.ts:586,605,684), so an operation over one keeps a goroutine per provider and settles when the caller cancels while its reads finish on their own.

func WithOAuthRefreshTimeout

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

WithOAuthRefreshTimeout marks the context of a provider refresh whose signal Pi builds as AbortSignal.timeout(timeout): the caller's cancellation does not reach it (auth/resolve.ts:refreshStoredOAuthCredential). A provider that hands its callback a signal, such as an extension bridge, reads the timeout 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 AIBinding

type AIBinding struct {
	Fetch FetchFunction
}

AIBinding is the Workers AI binding (`env.AI`). Fetch is the binding's `fetch()` passthrough; it is optional because the published binding type does not declare it, so CreateAIBindingFetch checks it. Upstream's `aiGatewayLogId` member only pins the TypeScript type to the AI binding and has no behaviour, so Go does not carry it.

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.

func StrippableAPIs

func StrippableAPIs() []API

StrippableAPIs lists the APIs a Piglet can strip (strip.apis). A Piglet Binary compiles each one's implementation out with the build tag pig_strip_<api> (see internal/pigstrip.Tag), and a Piglet that Stock PiG runs disables it at runtime. A stripped API's models are not offered (OfferedModels), and building a provider for one of them fails. pig additive (D92): Stock PiG strips no API.

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.

func (*APIKeyAuth) AuthMethodName

func (a *APIKeyAuth) AuthMethodName() string

AuthMethodName is the method's display name, which the provider selector searches (oauth-selector.ts:136 `provider.method?.name`).

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. pig additive (D92): a provider whose catalog models are all on stripped APIs (ProviderStripped) is left out.

type APIProvider

type APIProvider struct {
	API          API
	Stream       StreamFunction
	StreamSimple StreamFunction
}

APIProvider is one API implementation in the global API registry (compat.ts ApiProvider).

func GetAPIProvider

func GetAPIProvider(api API) *APIProvider

GetAPIProvider returns the registered implementation of an API, or nil (compat.ts getApiProvider).

func GetAPIProviders

func GetAPIProviders() []*APIProvider

GetAPIProviders lists the registered implementations in registration order (compat.ts getApiProviders).

type APIStreamFunction

type APIStreamFunction = ModelsStreamFunction

APIStreamFunction is a registered API's stream (compat.ts ApiStreamFunction).

type APIStreamSimpleFunction

type APIStreamSimpleFunction = ModelsStreamFunction

APIStreamSimpleFunction is a registered API's simple stream (compat.ts ApiStreamSimpleFunction).

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 AnthropicClient

type AnthropicClient interface {
	// CreateMessage sends the streaming Messages request and returns its raw response. ctx carries the request's abort. The provider reads the streamed events from the response body and handles a non-success status itself. A retryable failure that implements ProviderRequestError is retried within StreamOptions.MaxRetries, as retryProviderRequest does for Pi's client.
	CreateMessage(ctx context.Context, params AnthropicCreateParams, options AnthropicRequestOptions) (*http.Response, error)
}

AnthropicClient is the part of an Anthropic SDK client that the Anthropic Messages provider uses: Pi's `client.beta.messages.create(params, requestOptions).asResponse()` (anthropic-messages.ts:650). Inject one through StreamOptions.AnthropicClient to send the request through an alternative client that shares the Messages API, such as Anthropic on Vertex.

upstream: packages/ai/src/api/anthropic-messages.ts:286-290 (AnthropicOptions.client), 607-609

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 AnthropicCreateParams

type AnthropicCreateParams struct {
	// Body is the JSON request body. The betas list is not in it.
	Body []byte
	// Betas is the comma-joined anthropic-beta header value the SDK derives from params.betas; nil sends no header.
	Betas *string
	// UserProfileID and WorkspaceID are the anthropic-user-profile-id and anthropic-workspace-id header values the SDK derives from params.user_profile_id
	// and params.workspace_id (beta/messages/messages.js:24-58); nil sends no header.
	UserProfileID, WorkspaceID *string
}

AnthropicCreateParams are the Messages request an injected AnthropicClient sends: the params after the payload hook, with streaming enforced.

type AnthropicEffort

type AnthropicEffort string

AnthropicEffort is the Anthropic output_config effort level (anthropic-messages.ts:186).

const (
	AnthropicEffortLow    AnthropicEffort = "low"
	AnthropicEffortMedium AnthropicEffort = "medium"
	AnthropicEffortHigh   AnthropicEffort = "high"
	AnthropicEffortXHigh  AnthropicEffort = "xhigh"
	AnthropicEffortMax    AnthropicEffort = "max"
)

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 AnthropicRequestOptions

type AnthropicRequestOptions struct {
	// TimeoutMs is StreamOptions.TimeoutMs; nil sets none.
	TimeoutMs *int
}

AnthropicRequestOptions are the SDK request options of the call (anthropic-messages.ts:644-648; its `signal` is CreateMessage's ctx and its `maxRetries: 0` is the provider's own retry).

type AnthropicThinkingDisplay

type AnthropicThinkingDisplay string

AnthropicThinkingDisplay selects whether thinking blocks carry summarized text or an empty field (anthropic-messages.ts:188).

const (
	AnthropicThinkingDisplaySummarized AnthropicThinkingDisplay = "summarized"
	AnthropicThinkingDisplayOmitted    AnthropicThinkingDisplay = "omitted"
)

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(models []AnyModel, providerID string) ([]AnyModel, error)

DecodeModelsCatalog keeps the stored models of providerID. Records of types this version does not know never reach it: ModelsStoreEntry decoding sets them aside.

func DecodeStoredModel

func DecodeStoredModel(data []byte) (model AnyModel, known bool, err error)

DecodeStoredModel restores one persisted catalog record as a typed model that keeps the fields it has no member for. known is false, with a nil model, for a model type this version does not know.

func DecodeStoredModels

func DecodeStoredModels(records []json.RawMessage) ([]AnyModel, error)

DecodeStoredModels restores persisted catalog records in order; records of a model type this version does not know are dropped.

func GetAllBuiltinModels

func GetAllBuiltinModels(provider string) []AnyModel

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

func PersonalizeOpenRouterCatalog

func PersonalizeOpenRouterCatalog(catalog []AnyModel, refresh RefreshModelsContext) ([]AnyModel, error)

PersonalizeOpenRouterCatalog restricts an OpenRouter catalog to the chat models the key may use, with the key's limits and prices from `GET /models/user`. Image and classifier models are returned unchanged. A missing limit keeps the catalog value. The request uses the provider's configured base URL (refresh.BaseURL), so regional endpoints such as `https://us.openrouter.ai/api/v1` apply their region's filtering. When the endpoint rejects the key (401/403) or does not exist (404), for example with a proxy configured in models.json, the catalog is returned unchanged.

func WithOpenRouterRoutingVariantModels

func WithOpenRouterRoutingVariantModels(models []AnyModel) []AnyModel

WithOpenRouterRoutingVariantModels is WithOpenRouterRoutingVariants for a list of models a catalog refresh saved, which replaces the bundled catalog of OpenRouter: it lists each routing variant of every OpenRouter chat model of models right after the model, leaving a variant the list already holds where it is. The variants are derived each time the list is read and never saved, so they follow the current list whether it is the bundled catalog or a refreshed one. It returns models itself when it holds no OpenRouter chat model.

pig divergence (D199): see OpenRouterRoutingVariants.

type ArgumentMember

type ArgumentMember struct {
	Key  string
	JSON string
}

ArgumentMember is one member of a tool call's arguments: its key and the text JSON.stringify gives its value.

type AssistantContentBlock

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

AssistantContentBlock is valid inside an assistant message.

type AssistantContentProjection

type AssistantContentProjection struct {
	Kind         string
	Text         string
	Thinking     string
	ToolCallID   string
	ToolCallName string
	Arguments    JsonObject
}

AssistantContentProjection is what a renderer reads of one content block. Kind is the block's type ("text", "thinking" or "toolCall"); Text, Thinking, ToolCallID, ToolCallName and Arguments are set for the kind that holds them. The values are the observed state's own, not copies: a live view's published state never changes, and a plain message's are its own fields. Arguments must not be modified.

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       []ImagesOutputContent
	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 api/openrouter-images.ts generateImages.

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"`
	RawStopReason         string                       `json:"rawStopReason,omitempty"`
	EndTurn               *bool                        `json:"endTurn,omitempty"`
	Timestamp             int64                        `json:"timestamp"`
	// ErrorMessage follows the timestamp: a provider's catch block sets it on the output it built with its timestamp. A message
	// built with the error first (ErrorBeforeTimestamp) encodes it before the timestamp.
	ErrorMessage string `json:"errorMessage,omitempty"`
	// ErrorBeforeTimestamp records that the message was built with errorMessage before timestamp, as faux.ts builds its
	// messages (fauxAssistantMessage and createErrorMessage spread errorMessage, then timestamp), and as a stored record
	// that lists the keys in that order decodes. JavaScript keeps the order its source wrote, so the order is part of the
	// message; both encoders write it.
	ErrorBeforeTimestamp bool `json:"-"`
	// DurationMs is the milliseconds from the start of the request to the final message, measured with a monotonic clock by the AssistantMessageEventStream. Nil for legacy messages and for deferred results fetched later.
	// upstream: ai/src/types.ts:582 AssistantMessage.durationMs
	DurationMs *int64 `json:"durationMs,omitempty"`
	// ModelThinkingLevel 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 Complete

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

Complete is Stream followed by its final message (compat.ts complete).

func CompleteSimple

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

CompleteSimple is the simple-options form of Complete (compat.ts completeSimple).

func EventPartial

func EventPartial(event AssistantMessageEvent) *AssistantMessage

func FauxAssistantMessage

func FauxAssistantMessage(content FauxAssistantContent, options FauxAssistantMessageOptions) AssistantMessage

FauxAssistantMessage is faux.ts fauxAssistantMessage: a scripted response with the given content blocks, a "stop" stop reason unless options say otherwise, and the creation time as its timestamp unless options supply one.

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

func (AssistantMessage) MessageRole() string

MessageRole is the role property of a message.

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

func (message *AssistantMessage) ObserveProjection(content []AssistantContentProjection) (blocks []AssistantContentProjection, stopReason StopReason, errorMessage string)

ObserveProjection is Observe for a reader of block kinds, texts, thinking, tool calls, stop reason and error message: it appends one projection per content block to content, returns that slice with the stop reason and error message of the same publication, and copies nothing else. Full and shallow views read their current publication as Observe does. Returning the blocks as their own result keeps a caller's buffer on its stack when the caller stores the stop reason or error message.

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

func (message *AssistantMessage) Publication() (cell uintptr, seq uint64)

Publication identifies the state message observes: the producer cell its live view reads and the number of publications the cell has made. While a message's cell and seq are unchanged, every observation of that message returns the same state (a shallow view adds its own top-level fields, which only its holder assigns), so a reader may reuse what it derived from an earlier one. A message that is not a live view returns (0, 0); its fields are the caller's to track.

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"`
	// contains filtered or unexported fields
}

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

func CreateAssistantMessageDiagnostic

func CreateAssistantMessageDiagnostic(diagnosticType string, thrown any, details JsonObject) AssistantMessageDiagnostic

CreateAssistantMessageDiagnostic builds a timestamped diagnostic for a thrown value (utils/diagnostics.ts createAssistantMessageDiagnostic).

func (AssistantMessageDiagnostic) MarshalJSON

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

MarshalJSON writes the diagnostic as Pi's JSON.stringify does: details, when present even if empty, keep the member order they were built or read in.

func (*AssistantMessageDiagnostic) UnmarshalJSON

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

UnmarshalJSON reads a diagnostic and keeps the member order of its details, so a diagnostic loaded from a session is written back as Pi writes the parsed object.

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 APIModuleStream

func APIModuleStream(ctx context.Context, model *Model, stream func(context.Context) (*AssistantMessageEventStream, error)) (*AssistantMessageEventStream, error)

APIModuleStream calls an API module's Go stream. Pi's API modules catch their own failures inside the stream they return (for example google-generative-ai.ts:288-298, azure-openai-responses.ts:115-125), so a Go provider's Stream error ends that stream instead: the module's message, timestamp from the call before errorMessage, with the abort-aware stop reason (`options?.signal?.aborted ? "aborted" : "error"`). A lazySetupError stays a returned error for lazyStream's setup.

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

func LazyStreamSync(ctx context.Context, model *Model, setup func(context.Context) (*AssistantMessageEventStream, error), observer ...LazyStreamObserver) *AssistantMessageEventStream

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 Stream

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

Stream sends a request through the global API registry, filling the API key from the environment (compat.ts stream). A model whose API has no registered implementation is an error.

func StreamAnthropic

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

StreamAnthropic is the anthropic-messages stream (legacy-api-aliases.ts streamAnthropic).

func StreamAzureOpenAIResponses

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

StreamAzureOpenAIResponses is the azure-openai-responses stream (legacy-api-aliases.ts streamAzureOpenAIResponses).

func StreamGoogle

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

StreamGoogle is the google-generative-ai stream (legacy-api-aliases.ts streamGoogle).

func StreamGoogleVertex

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

StreamGoogleVertex is the google-vertex stream (legacy-api-aliases.ts streamGoogleVertex).

func StreamMistral

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

StreamMistral is the mistral-conversations stream (legacy-api-aliases.ts streamMistral).

func StreamOpenAICodexResponses

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

StreamOpenAICodexResponses is the openai-codex-responses stream (legacy-api-aliases.ts streamOpenAICodexResponses).

func StreamOpenAICompletions

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

StreamOpenAICompletions is the openai-completions stream (legacy-api-aliases.ts streamOpenAICompletions).

func StreamOpenAIResponses

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

StreamOpenAIResponses is the openai-responses stream (legacy-api-aliases.ts streamOpenAIResponses).

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, and Google Vertex to Application Default Credentials or an express-mode key. Other APIs reject missing request authentication before an event stream exists.

func StreamSimpleAnthropic

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

StreamSimpleAnthropic is the anthropic-messages simple stream (legacy-api-aliases.ts streamSimpleAnthropic).

func StreamSimpleAzureOpenAIResponses

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

StreamSimpleAzureOpenAIResponses is the azure-openai-responses simple stream (legacy-api-aliases.ts streamSimpleAzureOpenAIResponses).

func StreamSimpleContext

func StreamSimpleContext(ctx context.Context, model *Model, request Context, options ...StreamOptions) (*AssistantMessageEventStream, error)

StreamSimpleContext is compat.ts streamSimple: Stream through the registered simple stream function of the model's API. It takes a Context as Pi's does; StreamSimple is the direct API implementation, which takes the normalized transcript.

func StreamSimpleGoogle

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

StreamSimpleGoogle is the google-generative-ai simple stream (legacy-api-aliases.ts streamSimpleGoogle).

func StreamSimpleGoogleVertex

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

StreamSimpleGoogleVertex is the google-vertex simple stream (legacy-api-aliases.ts streamSimpleGoogleVertex).

func StreamSimpleMistral

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

StreamSimpleMistral is the mistral-conversations simple stream (legacy-api-aliases.ts streamSimpleMistral).

func StreamSimpleOpenAICodexResponses

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

StreamSimpleOpenAICodexResponses is the openai-codex-responses simple stream (legacy-api-aliases.ts streamSimpleOpenAICodexResponses).

func StreamSimpleOpenAICompletions

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

StreamSimpleOpenAICompletions is the openai-completions simple stream (legacy-api-aliases.ts streamSimpleOpenAICompletions).

func StreamSimpleOpenAIResponses

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

StreamSimpleOpenAIResponses is the openai-responses simple stream (legacy-api-aliases.ts streamSimpleOpenAIResponses).

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

ObservePublishedEvents is ObserveEvents without refreshing each event's partial at delivery. A caller hands an event to a reader only through RefreshEvent, called at the tick the event arrives, before anything awaits; a caller that reads nothing of an event skips the copy.

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.

func (*AssistantMessageEventStream) ResultContinuation

func (r *AssistantMessageEventStream) ResultContinuation(executor *continuationExecutor) *continuationPromise[*AssistantMessage]

type AssistantStreamBuilder

type AssistantStreamBuilder = assistantStreamBuilder

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.

func (*AuthCheck) AuthCheckSource

func (c *AuthCheck) AuthCheckSource() string

AuthCheckSource is where the configured auth comes from, as the provider selector prints it.

func (*AuthCheck) AuthCheckType

func (c *AuthCheck) AuthCheckType() string

AuthCheckType is the credential type the check found, "oauth" or "api_key" (the provider selector compares it with the option's auth type).

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"`
	// OpenBrowserOnConfirm offers best-effort browser opening on user confirmation; nil (Pi's undefined) means true, and clients may ignore it.
	OpenBrowserOnConfirm *bool `json:"openBrowserOnConfirm,omitempty"`
	// CopyCodeOnConfirm offers copying UserCode on user confirmation, replacing the clipboard; nil (Pi's undefined) means true.
	CopyCodeOnConfirm *bool    `json:"copyCodeOnConfirm,omitempty"`
	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 AuthOperationOptions

type AuthOperationOptions struct {
	Signal context.Context
}

AuthOperationOptions is Pi's AuthOperationOptions: optional cancellation of a public auth or credential operation. A nil Signal is an operation that only its own context cancels; a cancelled Signal cancels the operation with the Signal's cancellation cause (Pi rejects with `signal.reason`).

upstream: packages/ai/src/auth/types.ts:AuthOperationOptions

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 AzureConfig

type AzureConfig struct {
	BaseURL    string
	APIVersion string
}

AzureConfig is the resolved Azure endpoint.

func ResolveAzureConfig

func ResolveAzureConfig(modelBaseURL string, options AzureEndpointOptions) (AzureConfig, error)

ResolveAzureConfig resolves the endpoint and API version.

type AzureEndpointOptions

type AzureEndpointOptions = StreamOptions

AzureEndpointOptions are the per-request Azure endpoint options: the Azure fields of StreamOptions plus its Env. Azure models ship without a base URL: each user has their own resource, resolved per request.

Ports AzureEndpointOptions (packages/ai/src/api/azure-openai-config.ts:7), which extends StreamOptions.

type AzureOpenAIResponsesConfig

type AzureOpenAIResponsesConfig struct {
	ModelMetadata  *Model
	APIKey         string
	Model          string
	ProviderID     string
	BaseURL        string
	ExtraHeaders   map[string]string
	SamplingParams map[string]any
	// SamplingParamsByThinkingLevel overrides SamplingParams for the effective thinking level.
	SamplingParamsByThinkingLevel SamplingParamsByThinkingLevel
	// ThinkingLevelMap overrides catalog effort values when non-nil.
	ThinkingLevelMap ThinkingLevelMap
	// Env is the provider's environment; request env entries outrank it. The Azure endpoint options (azureBaseUrl, azureResourceName, azureApiVersion, azureDeploymentName) are per-request StreamOptions fields.
	Env    ProviderEnv
	Compat *OpenAIResponsesCompat
	// IsReasoning indicates the model supports extended reasoning (model.reasoning).
	IsReasoning bool
}

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

type BedrockThinkingDisplay

type BedrockThinkingDisplay = AnthropicThinkingDisplay

BedrockThinkingDisplay is how Bedrock Claude returns thinking content; it has the values of AnthropicThinkingDisplay. Mirrors upstream BedrockThinkingDisplay (api/bedrock-converse-stream.ts:77).

type BlockDeltas

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

BlockDeltas is the state a consumer of a streamed message keeps for its open blocks so that a delta costs the delta and not the block: the bytes of earlier deltas stay in one growing buffer. It is the machinery the provider stream builders use, for a consumer outside this package that applies `text += delta` per event (packages/agent/src/proxy.ts processProxyEvent). The zero value is ready to use. A block ends with Release.

func (*BlockDeltas) AppendContent

func (deltas *BlockDeltas) AppendContent(contentIndex int, current, delta string) string

AppendContent is current+delta for the text or thinking of the block at contentIndex.

func (*BlockDeltas) Release

func (deltas *BlockDeltas) Release(contentIndex int)

Release drops the buffers of the block at contentIndex.

type BodyReadOwner

type BodyReadOwner = bodyReadOwner

type BodyReadSignal

type BodyReadSignal = bodyReadSignal

type BuiltinImageProvider

type BuiltinImageProvider string

BuiltinImageProvider is a built-in provider with at least one image model in the generated catalog. upstream: image-models.ts:13 BuiltinImageProvider

func GetImageProviders

func GetImageProviders() []BuiltinImageProvider

GetImageProviders returns the generated image providers in stable order.

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 ChatModelCatalog

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

ChatModelCatalog holds the chat models of one provider by model id.

func AmazonBedrockModels

func AmazonBedrockModels() ChatModelCatalog

AmazonBedrockModels is AMAZON_BEDROCK_MODELS.

func AntLingModels

func AntLingModels() ChatModelCatalog

AntLingModels is ANT_LING_MODELS.

func AnthropicModels

func AnthropicModels() ChatModelCatalog

AnthropicModels is ANTHROPIC_MODELS.

func AzureModels

func AzureModels() ChatModelCatalog

AzureModels is AZURE_MODELS.

func BasetenModels

func BasetenModels() ChatModelCatalog

BasetenModels is BASETEN_MODELS.

func CerebrasModels

func CerebrasModels() ChatModelCatalog

CerebrasModels is CEREBRAS_MODELS.

func CloudflareAIGatewayModels

func CloudflareAIGatewayModels() ChatModelCatalog

CloudflareAIGatewayModels is CLOUDFLARE_AI_GATEWAY_MODELS.

func CloudflareWorkersAIModels

func CloudflareWorkersAIModels() ChatModelCatalog

CloudflareWorkersAIModels is CLOUDFLARE_WORKERS_AI_MODELS.

func DeepSeekModels

func DeepSeekModels() ChatModelCatalog

DeepSeekModels is DEEPSEEK_MODELS.

func FireworksModels

func FireworksModels() ChatModelCatalog

FireworksModels is FIREWORKS_MODELS.

func GitHubCopilotModels

func GitHubCopilotModels() ChatModelCatalog

GitHubCopilotModels is GITHUB_COPILOT_MODELS.

func GoogleModels

func GoogleModels() ChatModelCatalog

GoogleModels is GOOGLE_MODELS.

func GoogleVertexModels

func GoogleVertexModels() ChatModelCatalog

GoogleVertexModels is GOOGLE_VERTEX_MODELS.

func GroqModels

func GroqModels() ChatModelCatalog

GroqModels is GROQ_MODELS.

func HuggingFaceModels

func HuggingFaceModels() ChatModelCatalog

HuggingFaceModels is HUGGINGFACE_MODELS.

func KimiCodingModels

func KimiCodingModels() ChatModelCatalog

KimiCodingModels is KIMI_CODING_MODELS.

func MetaModels

func MetaModels() ChatModelCatalog

MetaModels is META_MODELS.

func MiniMaxCNModels

func MiniMaxCNModels() ChatModelCatalog

MiniMaxCNModels is MINIMAX_CN_MODELS.

func MiniMaxModels

func MiniMaxModels() ChatModelCatalog

MiniMaxModels is MINIMAX_MODELS.

func MistralModels

func MistralModels() ChatModelCatalog

MistralModels is MISTRAL_MODELS.

func MoonshotAICNModels

func MoonshotAICNModels() ChatModelCatalog

MoonshotAICNModels is MOONSHOTAI_CN_MODELS.

func MoonshotAIModels

func MoonshotAIModels() ChatModelCatalog

MoonshotAIModels is MOONSHOTAI_MODELS.

func NVIDIAModels

func NVIDIAModels() ChatModelCatalog

NVIDIAModels is NVIDIA_MODELS.

func OpenAICodexModels

func OpenAICodexModels() ChatModelCatalog

OpenAICodexModels is OPENAI_CODEX_MODELS.

func OpenAIModels

func OpenAIModels() ChatModelCatalog

OpenAIModels is OPENAI_MODELS.

func OpenCodeGoModels

func OpenCodeGoModels() ChatModelCatalog

OpenCodeGoModels is OPENCODE_GO_MODELS.

func OpenCodeModels

func OpenCodeModels() ChatModelCatalog

OpenCodeModels is OPENCODE_MODELS.

func OpenRouterModels

func OpenRouterModels() ChatModelCatalog

OpenRouterModels is OPENROUTER_MODELS.

func QwenTokenPlanCNModels

func QwenTokenPlanCNModels() ChatModelCatalog

QwenTokenPlanCNModels is QWEN_TOKEN_PLAN_CN_MODELS.

func QwenTokenPlanIndividualModels

func QwenTokenPlanIndividualModels() ChatModelCatalog

QwenTokenPlanIndividualModels is QWEN_TOKEN_PLAN_INDIVIDUAL_MODELS.

func QwenTokenPlanModels

func QwenTokenPlanModels() ChatModelCatalog

QwenTokenPlanModels is QWEN_TOKEN_PLAN_MODELS.

func RadiusModels

func RadiusModels() ChatModelCatalog

RadiusModels is RADIUS_MODELS.

func TogetherModels

func TogetherModels() ChatModelCatalog

TogetherModels is TOGETHER_MODELS.

func TypesafeModels

func TypesafeModels() ChatModelCatalog

TypesafeModels is TYPESAFE_MODELS.

func VercelAIGatewayModels

func VercelAIGatewayModels() ChatModelCatalog

VercelAIGatewayModels is VERCEL_AI_GATEWAY_MODELS.

func XAIModels

func XAIModels() ChatModelCatalog

XAIModels is XAI_MODELS.

func XiaomiModels

func XiaomiModels() ChatModelCatalog

XiaomiModels is XIAOMI_MODELS.

func XiaomiTokenPlanAMSModels

func XiaomiTokenPlanAMSModels() ChatModelCatalog

XiaomiTokenPlanAMSModels is XIAOMI_TOKEN_PLAN_AMS_MODELS.

func XiaomiTokenPlanCNModels

func XiaomiTokenPlanCNModels() ChatModelCatalog

XiaomiTokenPlanCNModels is XIAOMI_TOKEN_PLAN_CN_MODELS.

func XiaomiTokenPlanSGPModels

func XiaomiTokenPlanSGPModels() ChatModelCatalog

XiaomiTokenPlanSGPModels is XIAOMI_TOKEN_PLAN_SGP_MODELS.

func ZAICodingCNModels

func ZAICodingCNModels() ChatModelCatalog

ZAICodingCNModels is ZAI_CODING_CN_MODELS.

func ZAIModels

func ZAIModels() ChatModelCatalog

ZAIModels is ZAI_MODELS.

func (ChatModelCatalog) Get

func (c ChatModelCatalog) Get(id string) (M, bool)

Get returns the model with the given id (catalog[id]).

func (ChatModelCatalog) IDs

func (c ChatModelCatalog) IDs() []string

IDs returns the model ids in catalog order (Object.keys).

func (ChatModelCatalog) Len

func (c ChatModelCatalog) Len() int

Len returns the number of models.

func (ChatModelCatalog) Values

func (c ChatModelCatalog) Values() []M

Values returns the models in catalog order (Object.values).

type ChatTemplateKwargValue

type ChatTemplateKwargValue = any

ChatTemplateKwargValue is one chat-template keyword value: a string, number, boolean or null, or a `{ "$var": ... }` placeholder resolved per request (types.ts:90).

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"
	ClassifierAPIOpenAIDecisions              ClassifierAPI = "openai-decisions"
)

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
	// Images are judged together with State. Only models whose Input includes "image" accept them; other models return an error result (types.ts:669-677).
	Images    []ImageContent
	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, images?, questions} (types.ts:669-677 declaration order); images is omitted when absent.

func (*ClassifierContext) UnmarshalJSON

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

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

type ClassifierFunction

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

type ClassifierHTTPError

type ClassifierHTTPError struct {
	// Status is the HTTP status; nil for a timeout.
	Status *int
	// Headers are the response headers; nil for a timeout.
	Headers http.Header
	// Body is the response body text.
	Body    string
	Message string
}

ClassifierHTTPError is a failed HTTP attempt: a non-2xx response, or a timeout (no status, no headers). Only these errors are retried; a transport failure is not.

Ports packages/ai/src/api/classifier-shared.ts (ClassifierHttpError: status, headers and body beside the message).

func (*ClassifierHTTPError) Error

func (e *ClassifierHTTPError) Error() string

func (*ClassifierHTTPError) Name

func (e *ClassifierHTTPError) Name() string

Name is the `name` property (classifier-shared.ts:8-27): a timeout, which has no status or headers, is "TimeoutError" (timeoutError sets it), and an HTTP failure keeps Error's default name, "Error" (httpError sets none).

func (*ClassifierHTTPError) ProviderHeaders

func (e *ClassifierHTTPError) ProviderHeaders() http.Header

func (*ClassifierHTTPError) ProviderStatus

func (e *ClassifierHTTPError) ProviderStatus() *int

ProviderStatus and ProviderHeaders make a failed classifier attempt a ProviderRequestError: a timeout has neither.

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
	// Compat holds the System One settings of typesafe-system-one and cloudflare-workers-ai-system-one models.
	Compat *SystemOneCompat
	// contains filtered or unexported fields
}

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 ClassifierModelCatalog

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

ClassifierModelCatalog holds the classifier models of one provider by model id.

func AmazonBedrockClassifierModels

func AmazonBedrockClassifierModels() ClassifierModelCatalog

AmazonBedrockClassifierModels is AMAZON_BEDROCK_CLASSIFIER_MODELS.

func AntLingClassifierModels

func AntLingClassifierModels() ClassifierModelCatalog

AntLingClassifierModels is ANT_LING_CLASSIFIER_MODELS.

func AnthropicClassifierModels

func AnthropicClassifierModels() ClassifierModelCatalog

AnthropicClassifierModels is ANTHROPIC_CLASSIFIER_MODELS.

func AzureClassifierModels

func AzureClassifierModels() ClassifierModelCatalog

AzureClassifierModels is AZURE_CLASSIFIER_MODELS.

func BasetenClassifierModels

func BasetenClassifierModels() ClassifierModelCatalog

BasetenClassifierModels is BASETEN_CLASSIFIER_MODELS.

func CerebrasClassifierModels

func CerebrasClassifierModels() ClassifierModelCatalog

CerebrasClassifierModels is CEREBRAS_CLASSIFIER_MODELS.

func CloudflareAIGatewayClassifierModels

func CloudflareAIGatewayClassifierModels() ClassifierModelCatalog

CloudflareAIGatewayClassifierModels is CLOUDFLARE_AI_GATEWAY_CLASSIFIER_MODELS.

func CloudflareWorkersAIClassifierModels

func CloudflareWorkersAIClassifierModels() ClassifierModelCatalog

CloudflareWorkersAIClassifierModels is CLOUDFLARE_WORKERS_AI_CLASSIFIER_MODELS.

func DeepSeekClassifierModels

func DeepSeekClassifierModels() ClassifierModelCatalog

DeepSeekClassifierModels is DEEPSEEK_CLASSIFIER_MODELS.

func FireworksClassifierModels

func FireworksClassifierModels() ClassifierModelCatalog

FireworksClassifierModels is FIREWORKS_CLASSIFIER_MODELS.

func GitHubCopilotClassifierModels

func GitHubCopilotClassifierModels() ClassifierModelCatalog

GitHubCopilotClassifierModels is GITHUB_COPILOT_CLASSIFIER_MODELS.

func GoogleClassifierModels

func GoogleClassifierModels() ClassifierModelCatalog

GoogleClassifierModels is GOOGLE_CLASSIFIER_MODELS.

func GoogleVertexClassifierModels

func GoogleVertexClassifierModels() ClassifierModelCatalog

GoogleVertexClassifierModels is GOOGLE_VERTEX_CLASSIFIER_MODELS.

func GroqClassifierModels

func GroqClassifierModels() ClassifierModelCatalog

GroqClassifierModels is GROQ_CLASSIFIER_MODELS.

func HuggingFaceClassifierModels

func HuggingFaceClassifierModels() ClassifierModelCatalog

HuggingFaceClassifierModels is HUGGINGFACE_CLASSIFIER_MODELS.

func KimiCodingClassifierModels

func KimiCodingClassifierModels() ClassifierModelCatalog

KimiCodingClassifierModels is KIMI_CODING_CLASSIFIER_MODELS.

func MetaClassifierModels

func MetaClassifierModels() ClassifierModelCatalog

MetaClassifierModels is META_CLASSIFIER_MODELS.

func MiniMaxCNClassifierModels

func MiniMaxCNClassifierModels() ClassifierModelCatalog

MiniMaxCNClassifierModels is MINIMAX_CN_CLASSIFIER_MODELS.

func MiniMaxClassifierModels

func MiniMaxClassifierModels() ClassifierModelCatalog

MiniMaxClassifierModels is MINIMAX_CLASSIFIER_MODELS.

func MistralClassifierModels

func MistralClassifierModels() ClassifierModelCatalog

MistralClassifierModels is MISTRAL_CLASSIFIER_MODELS.

func MoonshotAICNClassifierModels

func MoonshotAICNClassifierModels() ClassifierModelCatalog

MoonshotAICNClassifierModels is MOONSHOTAI_CN_CLASSIFIER_MODELS.

func MoonshotAIClassifierModels

func MoonshotAIClassifierModels() ClassifierModelCatalog

MoonshotAIClassifierModels is MOONSHOTAI_CLASSIFIER_MODELS.

func NVIDIAClassifierModels

func NVIDIAClassifierModels() ClassifierModelCatalog

NVIDIAClassifierModels is NVIDIA_CLASSIFIER_MODELS.

func OpenAIClassifierModels

func OpenAIClassifierModels() ClassifierModelCatalog

OpenAIClassifierModels is OPENAI_CLASSIFIER_MODELS.

func OpenAICodexClassifierModels

func OpenAICodexClassifierModels() ClassifierModelCatalog

OpenAICodexClassifierModels is OPENAI_CODEX_CLASSIFIER_MODELS.

func OpenCodeClassifierModels

func OpenCodeClassifierModels() ClassifierModelCatalog

OpenCodeClassifierModels is OPENCODE_CLASSIFIER_MODELS.

func OpenCodeGoClassifierModels

func OpenCodeGoClassifierModels() ClassifierModelCatalog

OpenCodeGoClassifierModels is OPENCODE_GO_CLASSIFIER_MODELS.

func OpenRouterClassifierModels

func OpenRouterClassifierModels() ClassifierModelCatalog

OpenRouterClassifierModels is OPENROUTER_CLASSIFIER_MODELS.

func QwenTokenPlanCNClassifierModels

func QwenTokenPlanCNClassifierModels() ClassifierModelCatalog

QwenTokenPlanCNClassifierModels is QWEN_TOKEN_PLAN_CN_CLASSIFIER_MODELS.

func QwenTokenPlanClassifierModels

func QwenTokenPlanClassifierModels() ClassifierModelCatalog

QwenTokenPlanClassifierModels is QWEN_TOKEN_PLAN_CLASSIFIER_MODELS.

func QwenTokenPlanIndividualClassifierModels

func QwenTokenPlanIndividualClassifierModels() ClassifierModelCatalog

QwenTokenPlanIndividualClassifierModels is QWEN_TOKEN_PLAN_INDIVIDUAL_CLASSIFIER_MODELS.

func RadiusClassifierModels

func RadiusClassifierModels() ClassifierModelCatalog

RadiusClassifierModels is RADIUS_CLASSIFIER_MODELS.

func TogetherClassifierModels

func TogetherClassifierModels() ClassifierModelCatalog

TogetherClassifierModels is TOGETHER_CLASSIFIER_MODELS.

func TypesafeClassifierModels

func TypesafeClassifierModels() ClassifierModelCatalog

TypesafeClassifierModels is TYPESAFE_CLASSIFIER_MODELS.

func VercelAIGatewayClassifierModels

func VercelAIGatewayClassifierModels() ClassifierModelCatalog

VercelAIGatewayClassifierModels is VERCEL_AI_GATEWAY_CLASSIFIER_MODELS.

func XAIClassifierModels

func XAIClassifierModels() ClassifierModelCatalog

XAIClassifierModels is XAI_CLASSIFIER_MODELS.

func XiaomiClassifierModels

func XiaomiClassifierModels() ClassifierModelCatalog

XiaomiClassifierModels is XIAOMI_CLASSIFIER_MODELS.

func XiaomiTokenPlanAMSClassifierModels

func XiaomiTokenPlanAMSClassifierModels() ClassifierModelCatalog

XiaomiTokenPlanAMSClassifierModels is XIAOMI_TOKEN_PLAN_AMS_CLASSIFIER_MODELS.

func XiaomiTokenPlanCNClassifierModels

func XiaomiTokenPlanCNClassifierModels() ClassifierModelCatalog

XiaomiTokenPlanCNClassifierModels is XIAOMI_TOKEN_PLAN_CN_CLASSIFIER_MODELS.

func XiaomiTokenPlanSGPClassifierModels

func XiaomiTokenPlanSGPClassifierModels() ClassifierModelCatalog

XiaomiTokenPlanSGPClassifierModels is XIAOMI_TOKEN_PLAN_SGP_CLASSIFIER_MODELS.

func ZAIClassifierModels

func ZAIClassifierModels() ClassifierModelCatalog

ZAIClassifierModels is ZAI_CLASSIFIER_MODELS.

func ZAICodingCNClassifierModels

func ZAICodingCNClassifierModels() ClassifierModelCatalog

ZAICodingCNClassifierModels is ZAI_CODING_CN_CLASSIFIER_MODELS.

func (ClassifierModelCatalog) Get

func (c ClassifierModelCatalog) Get(id string) (M, bool)

Get returns the model with the given id (catalog[id]).

func (ClassifierModelCatalog) IDs

func (c ClassifierModelCatalog) IDs() []string

IDs returns the model ids in catalog order (Object.keys).

func (ClassifierModelCatalog) Len

func (c ClassifierModelCatalog) Len() int

Len returns the number of models.

func (ClassifierModelCatalog) Values

func (c ClassifierModelCatalog) Values() []M

Values returns the models in catalog order (Object.values).

type ClassifierOptions

type ClassifierOptions struct {
	// Telemetry is the explicit parent span for telemetry produced by this logical request; nil is Pi's omitted option, which uses the no-op span.
	Telemetry telemetry.TelemetrySpan
	// 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
	// Identity is how the request identifies the calling app: `User-Agent` and provider attribution headers.
	Identity *ClientIdentity
	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 ClassifyOpenAIDecisions

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

ClassifyOpenAIDecisions classifies through OpenAI's Decisions API.

func ClassifySystemOne

func ClassifySystemOne(ctx context.Context, transport SystemOneTransport, model ClassifierModel, request ClassifierContext, options ClassifierOptions) ClassifierResult

ClassifySystemOne runs one System One classification over the given transport.

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 ClientAttribution

type ClientAttribution struct {
	// Name is the app name: OpenRouter app title, NVIDIA invoke origin (first letter upper-cased), OpenCode client.
	Name string `json:"name"`
	// URL is the app URL: OpenRouter `HTTP-Referer`, the app's identity in OpenRouter's rankings, grouped by origin.
	URL string `json:"url"`
	// Categories are the app categories, e.g. `["cli-agent"]`: OpenRouter `X-OpenRouter-Categories`, comma-separated.
	Categories []string `json:"categories,omitzero"`
}

ClientAttribution is who requests are attributed to. GetAttributionHeaders maps the fields to each provider's headers.

type ClientIdentity

type ClientIdentity struct {
	// UserAgent is the `User-Agent` mode. Nil is `"detailed"`.
	UserAgent *UserAgentMode `json:"userAgent,omitempty"`
	// Attribution is the provider attribution. Nil with AttributionNull false sends none; AttributionNull is upstream's `null`, which also turns off attribution set by a merged identity.
	Attribution *ClientAttribution `json:"attribution,omitempty"`
	// AttributionNull is `attribution: null`.
	AttributionNull bool `json:"-"`
	// Version is the version in the detailed `User-Agent`. Nil is ProductVersion.
	Version *string `json:"version,omitempty"`
}

ClientIdentity is how requests identify the calling app to providers. Every field is optional; absent fields use the defaults: a detailed `User-Agent` with this package's version and no attribution headers. It encodes as plain JSON.

func MergeClientIdentity

func MergeClientIdentity(sources ...*ClientIdentity) *ClientIdentity

MergeClientIdentity merges identities field by field; later sources win and omitted fields are skipped. It returns nil when every source is nil.

func (ClientIdentity) MarshalJSON

func (identity ClientIdentity) MarshalJSON() ([]byte, error)

MarshalJSON writes an explicit null attribution as `"attribution": null`.

func (*ClientIdentity) UnmarshalJSON

func (identity *ClientIdentity) UnmarshalJSON(data []byte) error

UnmarshalJSON reads an explicit `"attribution": null` as AttributionNull.

type ClientIdentityModels

type ClientIdentityModels interface {
	GetModel(provider, id string) *Model
	StreamSimple(ctx context.Context, model *Model, request Context, options ...StreamOptions) *AssistantMessageEventStream
	CompleteSimple(ctx context.Context, model *Model, request Context, options ...StreamOptions) *AssistantMessage
	FetchDeferred(ctx context.Context, model *Model, handle DeferredHandle, options ...DeferredFetchOptions) *AssistantMessage
	CancelDeferred(ctx context.Context, model *Model, handle DeferredHandle, options ...DeferredCancelOptions) error
}

ClientIdentityModels is the request surface of Models that WithClientIdentity wraps: the methods generation and compaction call. *Models, the coding Model Runtime and the durable Harness's models satisfy it.

func WithClientIdentity

func WithClientIdentity(models ClientIdentityModels, identity ClientIdentity) ClientIdentityModels

WithClientIdentity is models with identity as the default identity of its requests: a request's Identity overrides it field by field, and it overrides the identity models was created with. Everything else is delegated unchanged. The result has the optional ModelsLister and ModelsCatalog surfaces models has, and no others. The wrapper is a typed view: Go has no Proxy, so a Models that is not a ClientIdentityModels is not wrapped.

type ClientIdentityTarget

type ClientIdentityTarget struct {
	Provider string
	BaseURL  string
}

ClientIdentityTarget is the provider and endpoint a request goes to.

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 CombinedAbortSignal

type CombinedAbortSignal struct {
	Signal  context.Context
	Cleanup func()
	// contains filtered or unexported fields
}

CombinedAbortSignal is the result of CombineAbortSignals. Signal is nil when no signal was supplied. Cleanup releases the listeners the combination installed.

upstream: packages/ai/src/utils/abort-signals.ts:1

func CombineAbortSignals

func CombineAbortSignals(signals ...context.Context) CombinedAbortSignal

CombineAbortSignals returns one signal that aborts when any supplied signal aborts, with that signal's cause. Nil signals are ignored, no signal gives a nil Signal, and a single signal is returned as it is. A signal that is already cancelled cancels the combination before it is returned, and the signals after it are not observed. Cleanup stops observing the supplied signals. A Go context has no synchronous abort listener, so the combination follows a supplied signal one goroutine hop after that signal is cancelled; wait on Signal.Done rather than reading Err straight after the cancel.

upstream: packages/ai/src/utils/abort-signals.ts:6

type ConstrainedSamplingConfig

type ConstrainedSamplingConfig struct {
	Type     ConstrainedSamplingType   `json:"type"`
	Strict   ConstrainedSamplingStrict `json:"strict,omitempty"`   // json_schema only
	Variants GrammarVariants           `json:"variants,omitempty"` // grammar only: 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 ConstrainedSamplingStrict

type ConstrainedSamplingStrict string

ConstrainedSamplingStrict is how strictly a json_schema tool must be sampled: `"prefer" | "require"`. upstream: packages/ai/src/types.ts:709-711

const (
	ConstrainedSamplingStrictPrefer  ConstrainedSamplingStrict = "prefer"
	ConstrainedSamplingStrictRequire ConstrainedSamplingStrict = "require"
)

type ConstrainedSamplingType

type ConstrainedSamplingType string

ConstrainedSamplingType is the discriminator of ConstrainedSamplingConfig: `"json_schema" | "grammar"`. upstream: packages/ai/src/types.ts:707-715

const (
	ConstrainedSamplingJSONSchema ConstrainedSamplingType = "json_schema"
	ConstrainedSamplingGrammar    ConstrainedSamplingType = "grammar"
)

type ContentBlock

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

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

func DecodedFormBlock

func DecodedFormBlock(block ContentBlock) (ContentBlock, bool)

DecodedFormBlock is the block decoding the JSON encoding of block yields, when that can be proven without encoding or decoding: a text, thinking, image or tool call block that holds only valid UTF-8 text and, for a tool call, arguments that are an object of plain JSON values (nil, bool, finite float64 other than negative zero, string, []any, map[string]any). It reports false for anything else, and the caller decodes the record.

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
	// Frozen declares that nothing modifies Messages, or anything they reference, while a request or stream made
	// from this context is live. Normalization then shares the messages instead of deep-copying them, as pi-ai's
	// normalizeContext does. TranscriptContext.Messages still returns copies.
	// pig additive (D104): pi-ai copies nothing; PiG copies unless the caller declares the messages immutable.
	Frozen bool
	// Validated lets a Frozen context skip validating a value an earlier request of the same transcript already
	// validated: it answers by the value's identity, so the caller promises that no value the memo has seen changes
	// while the memo is in use. It is ignored unless Frozen is set.
	// pig additive (D104): pi-ai validates nothing; PiG checks every tool result's details and call arguments for JSON, and a transcript that grows by one message per turn would otherwise check them all again every turn.
	Validated *ValidationMemo
	// contains filtered or unexported fields
}

Context is the public request shape accepted before provider normalization.

type ContextTokensSource

type ContextTokensSource interface {
	[]Message | TranscriptContext
}

ContextTokensSource is the input of EstimateContextTokens: a transcript or its messages (estimate.ts `TranscriptContext | readonly Message[]`).

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[T ContextTokensSource](context T) 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 ContinuationExecutor

type ContinuationExecutor = continuationExecutor

type ContinuationTurn

type ContinuationTurn = continuationTurn

type ContinuationTurnKey

type ContinuationTurnKey = continuationTurnKey

type ConvertCompletionsMessagesOptions

type ConvertCompletionsMessagesOptions struct {
	// GrammarToolInputProperties maps a grammar tool's name to its constrained input property. Nil derives it from the transcript.
	GrammarToolInputProperties map[string]string
}

ConvertCompletionsMessagesOptions are the options of ConvertCompletionsMessages.

Ports packages/ai/src/api/openai-completions.ts (ConvertCompletionsMessagesOptions).

type ConvertResponsesMessagesOptions

type ConvertResponsesMessagesOptions struct {
	// IncludeSystemPrompt nil is true: the transcript's leading system message becomes the first input item.
	IncludeSystemPrompt *bool
	// GrammarToolInputProperties maps a grammar tool's name to the argument property its custom input carries.
	GrammarToolInputProperties map[string]string
	// SupportsMidConvoSystemMessages sends later system messages in place; otherwise they fold into the leading prompt.
	SupportsMidConvoSystemMessages bool
	SupportsAdditionalTools        bool
	SupportsToolSearch             bool
	ToolOptions                    ConvertResponsesToolsOptions
	// ConfigurationUpdateEffort is the effort of the reply being generated. When set, the thinking level goes in
	// configuration_update items instead of a changing reasoning.effort: one before every earlier reply of this model
	// with its recorded level (ProviderThinkingLevel), and one with this effort at the end, so earlier input never
	// changes (openai-responses-shared.ts convertResponsesMessages configurationUpdateEffort).
	ConfigurationUpdateEffort string
}

ConvertResponsesMessagesOptions mirrors upstream ConvertResponsesMessagesOptions (openai-responses-shared.ts:124).

type ConvertResponsesToolsOptions

type ConvertResponsesToolsOptions struct {
	// Strict is the strict field of a function tool that no constrained-sampling request decides.
	Strict ResponsesStrictDefault
	// SupportsStrictMode nil is true, as `options?.supportsStrictMode ?? true`.
	SupportsStrictMode *bool
	// SupportsOpenAIGrammarTools nil is false.
	SupportsOpenAIGrammarTools bool
	// ToolSearchResult marks every tool as loaded by a tool_search_output (defer_loading).
	ToolSearchResult bool
}

ConvertResponsesToolsOptions mirrors upstream ConvertResponsesToolsOptions (openai-responses-shared.ts:132).

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    float64 `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
	// Identity is the default identity of every request; a request's Identity overrides it field by field.
	Identity *ClientIdentity
}

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        JsonValue `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 DiagnosticCoder

type DiagnosticCoder interface {
	DiagnosticCode() any
}

DiagnosticCoder is implemented by an error that carries a JavaScript-style `error.code`: a string or a number.

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.

func ExtractDiagnosticError

func ExtractDiagnosticError(value any) DiagnosticErrorInfo

ExtractDiagnosticError summarizes a thrown value for a diagnostic. A value that is not an error is reported as a `ThrownValue`. An error reports its name (an empty name is omitted, like upstream's `error.name || undefined`), its message (its name when the message is empty), and a string or numeric code from DiagnosticCoder. Go errors carry no stack, so Stack stays empty (utils/diagnostics.ts extractDiagnosticError). pig divergence (D103): JS Error.stack is not modelled; a Go stack would persist build-machine paths.

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 EventStream

type EventStream[T, R any] struct {
	// contains filtered or unexported fields
}

EventStream delivers pushed events in order to its consumers and resolves one result. An event for which isComplete is true ends the stream and supplies the result through extractResult. Events pushed after the stream ended are dropped. Events are buffered without bound until read. Waiting consumers receive events in the order they began waiting.

func NewEventStream

func NewEventStream[T, R any](isComplete func(T) bool, extractResult func(T) R) *EventStream[T, R]

NewEventStream creates a stream that ends when isComplete reports an event and then resolves with extractResult of that event.

func (*EventStream[T, R]) End

func (s *EventStream[T, R]) End(result ...R)

End ends the stream and releases every waiting consumer; buffered events remain readable. With a result it resolves Result unless Result is already resolved; without one Result stays unresolved, as upstream end() without an argument leaves it. Only the first result counts.

func (*EventStream[T, R]) Events

func (s *EventStream[T, R]) Events() iter.Seq[T]

Events yields buffered and future events in order and returns once the stream ended and its buffer is empty.

func (*EventStream[T, R]) Push

func (s *EventStream[T, R]) Push(event T)

Push delivers an event to the longest-waiting consumer, or buffers it. It ignores events after the stream ended.

func (*EventStream[T, R]) Result

func (s *EventStream[T, R]) Result() R

Result blocks until the stream resolves its result.

func (*EventStream[T, R]) ResultContext

func (s *EventStream[T, R]) ResultContext(ctx context.Context) (R, error)

ResultContext is Result that gives up when ctx ends.

type FauxAssistantContent

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

FauxAssistantContent is the `string | FauxContentBlock | FauxContentBlock[]` content of faux.ts fauxAssistantMessage: a FauxContentString becomes one text block, a FauxContentBlock one block and FauxContentBlocks the blocks as given. A nil content is no blocks.

type FauxAssistantMessageOptions

type FauxAssistantMessageOptions struct {
	StopReason   string
	Deferred     *DeferredHandle
	ErrorMessage string
	ResponseID   string
	Timestamp    *int64
}

FauxAssistantMessageOptions are the options of faux.ts fauxAssistantMessage.

type FauxConfig

type FauxConfig struct {
	Deferred        *FauxDeferredConfig
	API             API
	ProviderID      string
	Model           string
	Models          []FauxModelDefinition
	TokensPerSecond int
	// TokenSize is faux.ts RegisterFauxProviderOptions.tokenSize: the streamed chunk size range in tokens. A nil bound takes the default (3, 5).
	TokenSize *FauxTokenSize
}

type FauxContentBlock

type FauxContentBlock struct {
	Type FauxContentBlockType `json:"type"`
	Text string               `json:"text,omitempty"`
	// TextSignature is the optional TextContent member of a text block (types.ts TextContent.textSignature). Like the optional ToolCall members, only the final message keeps it.
	TextSignature string `json:"textSignature,omitempty"`
	Thinking      string `json:"thinking,omitempty"`
	// ThinkingSignature and Redacted are the optional ThinkingContent members of a thinking block (types.ts ThinkingContent). Only the final message keeps them.
	ThinkingSignature string         `json:"thinkingSignature,omitempty"`
	Redacted          bool           `json:"redacted,omitempty"`
	ID                string         `json:"id,omitempty"`
	Name              string         `json:"name,omitempty"`
	Arguments         map[string]any `json:"arguments,omitempty"`
	// ThoughtSignature and Namespace are the optional ToolCall members of a toolCall block (types.ts ToolCall). The stream carries them on the toolcall_end event and the final message, as faux.ts does; the partial message of the events keeps only id, name and arguments.
	ThoughtSignature string `json:"thoughtSignature,omitempty"`
	Namespace        string `json:"namespace,omitempty"`
	// ArgumentsError is the optional ToolCall member Pi 87f9a364 adds: why the final arguments text did not parse.
	ArgumentsError string `json:"argumentsError,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, options *FauxToolCallOptions) FauxContentBlock

FauxToolCall is faux.ts fauxToolCall(name, arguments, options = {}).

type FauxContentBlockType

type FauxContentBlockType string

FauxContentBlockType is the "type" of a FauxContentBlock: the closed union of TextContent | ThinkingContent | ToolCall types (faux.ts FauxContentBlock).

const (
	FauxContentText     FauxContentBlockType = "text"
	FauxContentThinking FauxContentBlockType = "thinking"
	FauxContentToolCall FauxContentBlockType = "toolCall"
)

The FauxContentBlock types.

type FauxContentBlocks

type FauxContentBlocks []FauxContentBlock

FauxContentBlocks is the array form of fauxAssistantMessage's content.

type FauxContentString

type FauxContentString string

FauxContentString is the string form of fauxAssistantMessage's content: one text block.

type FauxCore

type FauxCore struct {
	// API is `api` (faux.ts:455): the requested API name, else "faux:<time>:<random>".
	API string
	// Provider is `provider` (faux.ts:456): the provider id, "faux" unless one was requested.
	Provider string
	// Models is `models` (faux.ts:477-492): the faux models, in definition order, with the core's api and provider.
	Models []*Model
	// Stream is `stream` (faux.ts:508-571): it shifts the next queued response and streams it for the model, or ends the stream with an error message when none is queued.
	Stream ModelsStreamFunction
	// StreamSimple is `streamSimple` (faux.ts:573-574): stream, which takes the same options.
	StreamSimple ModelsStreamFunction
	// FetchDeferred is `fetchDeferred` (faux.ts:576-638): it answers a fetch of a deferred submission, the original handle while fetches remain pending, then the scripted response.
	FetchDeferred func(ctx context.Context, model *Model, handle DeferredHandle, options DeferredFetchOptions) (*AssistantMessageEventStream, error)
	// CancelDeferred is `cancelDeferred` (faux.ts:640-650): it records the handle as cancelled, so a later fetch of it fails.
	CancelDeferred func(ctx context.Context, model *Model, handle DeferredHandle, options DeferredCancelOptions) error
	// GetModel is `getModel` (faux.ts:652-659): the first model, or the model with the id, nil for an unknown id.
	GetModel func(id ...string) *Model
	// State is `state` (faux.ts:665): the call, deferred fetch and cancelled deferred counters.
	State *FauxProviderState
	// SetResponses is `setResponses` (faux.ts:667-669): the queue becomes a copy of responses.
	SetResponses func(responses []FauxResponseStep)
	// AppendResponses is `appendResponses` (faux.ts:670-672): responses join the end of the queue.
	AppendResponses func(responses []FauxResponseStep)
	// GetPendingResponseCount is `getPendingResponseCount` (faux.ts:673-675): the number of queued responses.
	GetPendingResponseCount func() int
	// contains filtered or unexported fields
}

FauxCore is the object faux.ts createFauxCore returns (faux.ts:454-702): the faux provider's api and provider names, its models, the functions that serve them (stream, streamSimple, fetchDeferred, cancelDeferred, getModel), the call counters, and the response queue. fauxProvider and registerFauxProvider are built on it; here they are NewFauxProvider and RegisterFauxProvider, whose handle shares this core's queue and state. Like upstream's object, every function is a field a caller keeps and calls on its own.

func CreateFauxCore

func CreateFauxCore(options RegisterFauxProviderOptions) *FauxCore

CreateFauxCore is faux.ts createFauxCore(options): it builds the faux provider's core. A nil option field takes upstream's default.

type FauxDeferredConfig

type FauxDeferredConfig struct {
	PendingFetches float64
	PollAfterMS    *int64
}

type FauxModelDefinition

type FauxModelDefinition struct {
	ID        string
	Name      string
	Reasoning bool
	Input     []string
	// InputLimits mirrors upstream FauxModelDefinition.inputLimits.
	InputLimits *ModelInputLimits
	// Cost is the per-million-token price; nil is a free model. Its Tiers are ignored (upstream's cost has none).
	Cost          *ModelCost
	ContextWindow int
	MaxTokens     int
}

type FauxProviderHandle

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

FauxProviderHandle is faux.ts FauxProviderHandle: the registered faux provider with its models and shared response queue.

func NewFauxProvider

func NewFauxProvider(cfg FauxConfig) *FauxProviderHandle

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

func (*FauxProviderHandle) API

func (p *FauxProviderHandle) API() API

API is the API name every model of the handle carries (faux.ts FauxProviderHandle.api): the requested one, else "faux:<time>:<random>".

func (*FauxProviderHandle) AppendResponses

func (p *FauxProviderHandle) AppendResponses(responses []FauxResponseStep)

func (*FauxProviderHandle) CallCount

func (p *FauxProviderHandle) CallCount() int

func (*FauxProviderHandle) CancelledDeferred

func (p *FauxProviderHandle) CancelledDeferred() []DeferredHandle

func (*FauxProviderHandle) Close

func (p *FauxProviderHandle) Close() error

func (*FauxProviderHandle) DeferredFetchCount

func (p *FauxProviderHandle) DeferredFetchCount() int

func (*FauxProviderHandle) GetModel

func (p *FauxProviderHandle) GetModel(id ...string) *Model

GetModel returns a canonical model with its provider identity, or nil for an unknown ID.

func (*FauxProviderHandle) ID

func (p *FauxProviderHandle) ID() string

func (*FauxProviderHandle) Models

func (p *FauxProviderHandle) Models() []*Model

func (*FauxProviderHandle) PendingResponseCount

func (p *FauxProviderHandle) PendingResponseCount() int

func (*FauxProviderHandle) Provider

func (p *FauxProviderHandle) Provider() *ModelsProvider

Provider exposes the model-aware provider definition for an explicit Models collection.

func (*FauxProviderHandle) SetResponses

func (p *FauxProviderHandle) SetResponses(responses []FauxResponseStep)

func (*FauxProviderHandle) State

State is the live call counters of the provider (faux.ts FauxProviderHandle.state).

func (*FauxProviderHandle) Stream

func (*FauxProviderHandle) Unregister

func (p *FauxProviderHandle) Unregister()

type FauxProviderRegistration

type FauxProviderRegistration = FauxProviderHandle

FauxProviderRegistration is faux.ts FauxProviderRegistration: the handle plus unregister, which FauxProviderHandle.Unregister is.

func RegisterFauxProvider

func RegisterFauxProvider(options ...RegisterFauxProviderOptions) *FauxProviderRegistration

RegisterFauxProvider is compat.ts registerFauxProvider: it creates the faux provider (createFauxCore) and registers its API in the global API registry under a generated `faux-provider-<id>` source id, so Stream and StreamSimple reach it for a model of its API. Unregister removes the registry entry.

type FauxProviderState

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

FauxProviderState is the live state of a faux provider (faux.ts FauxProviderState). A factory receives it while other streams run, so every member is read through a method that is safe across concurrent factories.

func (*FauxProviderState) CallCount

func (s *FauxProviderState) CallCount() int

CallCount is the number of streams started on the provider (faux.ts FauxProviderState.callCount).

func (*FauxProviderState) CancelledDeferred

func (s *FauxProviderState) CancelledDeferred() []DeferredHandle

CancelledDeferred returns copies of the handles passed to the provider's cancelDeferred, in call order (faux.ts FauxProviderState.cancelledDeferred).

func (*FauxProviderState) DeferredFetchCount

func (s *FauxProviderState) DeferredFetchCount() int

DeferredFetchCount is the number of deferred fetches started on the provider (faux.ts FauxProviderState.deferredFetchCount).

type FauxResponse

type FauxResponse struct {
	API      API
	Provider string
	Model    string
	Usage    Usage
	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
	// The scripted message keeps these members on every partial and on the final message (faux.ts:282-291 overwrites only api, provider, model, timestamp and usage).
	ResponseModel         string
	ProviderThinkingLevel string
	ThinkingLevel         ModelThinkingLevel
	Diagnostics           []AssistantMessageDiagnostic
	RawStopReason         string
	EndTurn               *bool
	// DurationMs is the scripted message's durationMs; the event stream measures it only when the message has none (event-stream.ts:127).
	DurationMs *int64
	// contains filtered or unexported fields
}

FauxResponse is the scripted AssistantMessage of faux.ts: a FauxResponseStep that carries one response. API, Provider, Model and Usage are the message's own members; the provider replaces them on every message it streams (faux.ts cloneMessage and withUsageEstimate), so a scripted value never reaches a caller.

func (FauxResponse) AssistantMessage

func (r FauxResponse) AssistantMessage() AssistantMessage

AssistantMessage is the AssistantMessage a scripted FauxResponse stands for, the value faux.ts factories and fauxAssistantMessage return. A response without a timestamp takes the creation time, as fauxAssistantMessage does.

type FauxResponseFactory

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

type FauxResponseStep

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

FauxResponseStep is faux.ts FauxResponseStep, `AssistantMessage | FauxResponseFactory`: a scripted AssistantMessage (an ai.AssistantMessage or the FauxResponse that fauxAssistantMessage builds) or a FauxResponseFactory.

func FauxFactoryStep

func FauxFactoryStep(factory FauxResponseFactory) FauxResponseStep

FauxFactoryStep is the step for a function literal, which has no FauxResponseFactory methods until it is converted.

func FauxStaticStep

func FauxStaticStep(response FauxResponse) FauxResponseStep

FauxStaticStep is the step for a scripted response.

type FauxTokenSize

type FauxTokenSize struct {
	Min *int
	Max *int
}

FauxTokenSize is the faux.ts tokenSize option {min?, max?}. A nil Min or Max is absent (the TypeScript `??` default), so a set 0 is not the default.

type FauxToolCallOptions

type FauxToolCallOptions struct {
	// ID names the tool call; empty draws a random "tool:..." id.
	ID string
}

FauxToolCallOptions are the options of faux.ts fauxToolCall.

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 CreateAIBindingFetch

func CreateAIBindingFetch(binding AIBinding) (FetchFunction, error)

CreateAIBindingFetch returns a fetch backed by the AI binding, for models whose base URL names a route the binding serves, including the gateway's provider passthrough `https://workers-binding.ai/ai-gateway/gateways/{gateway}/{provider}/...`. Requests pass through untouched. A binding without Fetch is rejected here, not on the first request. Set the result as `StreamOptions.Fetch` with `&http.Client{Transport: 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 GeminiContent

type GeminiContent = geminiContent

GeminiContent is one Google `Content` of a generateContent request.

func ConvertGoogleMessages

func ConvertGoogleMessages(model *Model, transcript TranscriptContext) []GeminiContent

ConvertGoogleMessages converts a transcript to the Google `Content[]` of model's request: system messages are collapsed, the leading one is dropped (it is sent as systemInstruction) and the rest is converted for the model's provider, id and image support. Ports google-shared.ts convertMessages(model, context).

type GeneratedModel

type GeneratedModel struct {
	ID                   string
	Provider             string
	DisplayName          string
	API                  API
	BaseURL              string
	Headers              map[string]string
	Compat               *ModelCompat
	ThinkingLevelMap     map[ModelThinkingLevel]*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
	// Record is the entry's JSON as Pi's catalog data spells it: the model object Pi serializes, member order included.
	Record string
}

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. It omits the models of a stripped API (OfferedModels); LookupModel and LookupModelExact still find them.

pig divergence (D199): the list of OpenRouter, or of every provider, also holds each OpenRouter model's routing variants (WithOpenRouterRoutingVariants), derived for the list and not stored in the catalog.

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 OfferedGeneratedModels

func OfferedGeneratedModels(models []GeneratedModel) []*GeneratedModel

OfferedGeneratedModels returns pointers to the entries of models whose built models OfferedModels keeps, in catalog order.

func WithOpenRouterRoutingVariants

func WithOpenRouterRoutingVariants(models []GeneratedModel) []GeneratedModel

WithOpenRouterRoutingVariants lists each routing variant of every OpenRouter chat model of models right after the model, leaving a variant the catalog already lists where it is. It returns models itself when it holds no OpenRouter model. A bundled model's variants are derived once and held (derivedOpenRouterVariant), so a list costs a copy of each entry listed.

func (*GeneratedModel) OpenRouterVariant

func (m *GeneratedModel) OpenRouterVariant(variant string) GeneratedModel

OpenRouterVariant is the routing variant of m, an OpenRouter chat entry that is not itself a variant.

pig divergence (D199): Pi lists only what OpenRouter's /models returns. OpenRouter's variants page tells model pickers to derive the routing variants from the base entries and to read every other property from the base entry ("Offering routing variants in a model picker"), so a variant repeats its base model's api, endpoint, pricing, limits, capabilities and thinking levels and differs in its id, its name ("<name> (nitro)") and its JSON record.

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 GoogleSdkThinkingLevel

type GoogleSdkThinkingLevel string

GoogleSdkThinkingLevel is the @google/genai ThinkingLevel enum a thinkingConfig carries (google-shared.ts GoogleSdkThinkingLevel).

func ToGoogleSdkThinkingLevel

func ToGoogleSdkThinkingLevel(level GoogleThinkingLevel) GoogleSdkThinkingLevel

ToGoogleSdkThinkingLevel selects the SDK enum value for a Google API level (google-shared.ts:98 toGoogleSdkThinkingLevel).

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

func ToGoogleThinkingLevel

func ToGoogleThinkingLevel(level ResolvedGoogleThinkingLevel) GoogleThinkingLevel

ToGoogleThinkingLevel maps a resolved level to Google's enum value (google-shared.ts:85 toGoogleThinkingLevel).

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 GrammarVariants

type GrammarVariants = map[GrammarFormat]string

GrammarVariants maps a grammar format to its definition.

upstream: packages/ai/src/types.ts:698

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

func (content ImageContent) AppendStringify(dst []byte) ([]byte, error)

AppendStringify appends the JSON.stringify text of MarshalJSON's output to dst (sdkjson.StringifyAppender).

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
	// contains filtered or unexported fields
}

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

func GetImageModel

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

GetImageModel returns a generated image model by provider and id.

func GetImageModels

func GetImageModels(provider BuiltinImageProvider) []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 ImageModelCatalog

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

ImageModelCatalog holds the image models of one provider by model id.

func AmazonBedrockImageModels

func AmazonBedrockImageModels() ImageModelCatalog

AmazonBedrockImageModels is AMAZON_BEDROCK_IMAGE_MODELS.

func AntLingImageModels

func AntLingImageModels() ImageModelCatalog

AntLingImageModels is ANT_LING_IMAGE_MODELS.

func AnthropicImageModels

func AnthropicImageModels() ImageModelCatalog

AnthropicImageModels is ANTHROPIC_IMAGE_MODELS.

func AzureImageModels

func AzureImageModels() ImageModelCatalog

AzureImageModels is AZURE_IMAGE_MODELS.

func BasetenImageModels

func BasetenImageModels() ImageModelCatalog

BasetenImageModels is BASETEN_IMAGE_MODELS.

func CerebrasImageModels

func CerebrasImageModels() ImageModelCatalog

CerebrasImageModels is CEREBRAS_IMAGE_MODELS.

func CloudflareAIGatewayImageModels

func CloudflareAIGatewayImageModels() ImageModelCatalog

CloudflareAIGatewayImageModels is CLOUDFLARE_AI_GATEWAY_IMAGE_MODELS.

func CloudflareWorkersAIImageModels

func CloudflareWorkersAIImageModels() ImageModelCatalog

CloudflareWorkersAIImageModels is CLOUDFLARE_WORKERS_AI_IMAGE_MODELS.

func DeepSeekImageModels

func DeepSeekImageModels() ImageModelCatalog

DeepSeekImageModels is DEEPSEEK_IMAGE_MODELS.

func FireworksImageModels

func FireworksImageModels() ImageModelCatalog

FireworksImageModels is FIREWORKS_IMAGE_MODELS.

func GitHubCopilotImageModels

func GitHubCopilotImageModels() ImageModelCatalog

GitHubCopilotImageModels is GITHUB_COPILOT_IMAGE_MODELS.

func GoogleImageModels

func GoogleImageModels() ImageModelCatalog

GoogleImageModels is GOOGLE_IMAGE_MODELS.

func GoogleVertexImageModels

func GoogleVertexImageModels() ImageModelCatalog

GoogleVertexImageModels is GOOGLE_VERTEX_IMAGE_MODELS.

func GroqImageModels

func GroqImageModels() ImageModelCatalog

GroqImageModels is GROQ_IMAGE_MODELS.

func HuggingFaceImageModels

func HuggingFaceImageModels() ImageModelCatalog

HuggingFaceImageModels is HUGGINGFACE_IMAGE_MODELS.

func KimiCodingImageModels

func KimiCodingImageModels() ImageModelCatalog

KimiCodingImageModels is KIMI_CODING_IMAGE_MODELS.

func MetaImageModels

func MetaImageModels() ImageModelCatalog

MetaImageModels is META_IMAGE_MODELS.

func MiniMaxCNImageModels

func MiniMaxCNImageModels() ImageModelCatalog

MiniMaxCNImageModels is MINIMAX_CN_IMAGE_MODELS.

func MiniMaxImageModels

func MiniMaxImageModels() ImageModelCatalog

MiniMaxImageModels is MINIMAX_IMAGE_MODELS.

func MistralImageModels

func MistralImageModels() ImageModelCatalog

MistralImageModels is MISTRAL_IMAGE_MODELS.

func MoonshotAICNImageModels

func MoonshotAICNImageModels() ImageModelCatalog

MoonshotAICNImageModels is MOONSHOTAI_CN_IMAGE_MODELS.

func MoonshotAIImageModels

func MoonshotAIImageModels() ImageModelCatalog

MoonshotAIImageModels is MOONSHOTAI_IMAGE_MODELS.

func NVIDIAImageModels

func NVIDIAImageModels() ImageModelCatalog

NVIDIAImageModels is NVIDIA_IMAGE_MODELS.

func OpenAICodexImageModels

func OpenAICodexImageModels() ImageModelCatalog

OpenAICodexImageModels is OPENAI_CODEX_IMAGE_MODELS.

func OpenAIImageModels

func OpenAIImageModels() ImageModelCatalog

OpenAIImageModels is OPENAI_IMAGE_MODELS.

func OpenCodeGoImageModels

func OpenCodeGoImageModels() ImageModelCatalog

OpenCodeGoImageModels is OPENCODE_GO_IMAGE_MODELS.

func OpenCodeImageModels

func OpenCodeImageModels() ImageModelCatalog

OpenCodeImageModels is OPENCODE_IMAGE_MODELS.

func OpenRouterImageModels

func OpenRouterImageModels() ImageModelCatalog

OpenRouterImageModels is OPENROUTER_IMAGE_MODELS.

func QwenTokenPlanCNImageModels

func QwenTokenPlanCNImageModels() ImageModelCatalog

QwenTokenPlanCNImageModels is QWEN_TOKEN_PLAN_CN_IMAGE_MODELS.

func QwenTokenPlanImageModels

func QwenTokenPlanImageModels() ImageModelCatalog

QwenTokenPlanImageModels is QWEN_TOKEN_PLAN_IMAGE_MODELS.

func QwenTokenPlanIndividualImageModels

func QwenTokenPlanIndividualImageModels() ImageModelCatalog

QwenTokenPlanIndividualImageModels is QWEN_TOKEN_PLAN_INDIVIDUAL_IMAGE_MODELS.

func RadiusImageModels

func RadiusImageModels() ImageModelCatalog

RadiusImageModels is RADIUS_IMAGE_MODELS.

func TogetherImageModels

func TogetherImageModels() ImageModelCatalog

TogetherImageModels is TOGETHER_IMAGE_MODELS.

func TypesafeImageModels

func TypesafeImageModels() ImageModelCatalog

TypesafeImageModels is TYPESAFE_IMAGE_MODELS.

func VercelAIGatewayImageModels

func VercelAIGatewayImageModels() ImageModelCatalog

VercelAIGatewayImageModels is VERCEL_AI_GATEWAY_IMAGE_MODELS.

func XAIImageModels

func XAIImageModels() ImageModelCatalog

XAIImageModels is XAI_IMAGE_MODELS.

func XiaomiImageModels

func XiaomiImageModels() ImageModelCatalog

XiaomiImageModels is XIAOMI_IMAGE_MODELS.

func XiaomiTokenPlanAMSImageModels

func XiaomiTokenPlanAMSImageModels() ImageModelCatalog

XiaomiTokenPlanAMSImageModels is XIAOMI_TOKEN_PLAN_AMS_IMAGE_MODELS.

func XiaomiTokenPlanCNImageModels

func XiaomiTokenPlanCNImageModels() ImageModelCatalog

XiaomiTokenPlanCNImageModels is XIAOMI_TOKEN_PLAN_CN_IMAGE_MODELS.

func XiaomiTokenPlanSGPImageModels

func XiaomiTokenPlanSGPImageModels() ImageModelCatalog

XiaomiTokenPlanSGPImageModels is XIAOMI_TOKEN_PLAN_SGP_IMAGE_MODELS.

func ZAICodingCNImageModels

func ZAICodingCNImageModels() ImageModelCatalog

ZAICodingCNImageModels is ZAI_CODING_CN_IMAGE_MODELS.

func ZAIImageModels

func ZAIImageModels() ImageModelCatalog

ZAIImageModels is ZAI_IMAGE_MODELS.

func (ImageModelCatalog) Get

func (c ImageModelCatalog) Get(id string) (M, bool)

Get returns the model with the given id (catalog[id]).

func (ImageModelCatalog) IDs

func (c ImageModelCatalog) IDs() []string

IDs returns the model ids in catalog order (Object.keys).

func (ImageModelCatalog) Len

func (c ImageModelCatalog) Len() int

Len returns the number of models.

func (ImageModelCatalog) Values

func (c ImageModelCatalog) Values() []M

Values returns the models in catalog order (Object.values).

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 []ImagesInputContent
}

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 ImagesInputContent

type ImagesInputContent = ContentBlock

ImagesInputContent is one block of an image-generation request: text or an image (types.ts:614).

type ImagesOptions

type ImagesOptions struct {
	// Telemetry is the explicit parent span for telemetry produced by this logical request; nil is Pi's omitted option, which uses the no-op span.
	Telemetry telemetry.TelemetrySpan
	// 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
	// Identity is how the request identifies the calling app: `User-Agent` and provider attribution headers.
	Identity  *ClientIdentity
	Env       map[string]string
	Metadata  map[string]any
	TimeoutMs int
	// MaxRetries is the retry budget after a retryable failure. Nil uses retry.provider.maxRetries; an explicit zero disables retries.
	MaxRetries *int
	// MaxRetryDelayMs caps a server-requested retry delay. Nil means 60000; 0 disables the cap.
	MaxRetryDelayMs *int
	OnPayload       func(payload any, model ImageModel) (any, bool, error)
	OnResponse      func(response ProviderResponse, model ImageModel) error
}

ImagesOptions configures image-generation provider requests.

type ImagesOutputContent

type ImagesOutputContent = ContentBlock

ImagesOutputContent is one block of an image-generation result: text or an image (types.ts:615).

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

func (s *InMemoryModelsStore) DeleteWith(ctx context.Context, providerID string, options ModelsStoreOperationOptions) error

DeleteWith is InMemoryModelsStore.delete(providerId, options).

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

ReadWith is InMemoryModelsStore.read(providerId, options).

func (*InMemoryModelsStore) Write

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

Write stores a copy of the provider's catalog.

func (*InMemoryModelsStore) WriteWith

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

WriteWith is InMemoryModelsStore.write(providerId, entry, options).

type JSONChange

type JSONChange struct {
	Kind  string // "set" or "append"
	Path  JSONPath
	Value any    // set
	Text  string // append
}

JSONChange is one change a piece of text made to the value: "set" when a key, an array item or a finished value appears (a new string is set once with its first text, a container empty), "append" when the piece extended an open string. Unterminated numbers and literals are not reported until they end. A container nested more than 32 levels deep is reported as one set of its whole value when it closes, so paths stay short.

type JSONPath

type JSONPath []any

JSONPath is a path from the root value: member keys (string) and array indices (int).

type JsAsyncGenerator

type JsAsyncGenerator[T any] = jsAsyncGenerator[T]

type JsonObject

type JsonObject map[string]JsonValue

JsonObject is a provider-facing JSON object (types.ts:459).

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 JsonValue

type JsonValue = any

JsonValue is any JSON value: null, a boolean, a number, a string, an array or a JsonObject (types.ts:458).

type KnownProvider

type KnownProvider string

KnownProvider is a built-in provider id. Ports packages/ai/src/types.ts (KnownProvider).

const (
	KnownProviderAmazonBedrock           KnownProvider = "amazon-bedrock"
	KnownProviderAntLing                 KnownProvider = "ant-ling"
	KnownProviderAnthropic               KnownProvider = "anthropic"
	KnownProviderGoogle                  KnownProvider = "google"
	KnownProviderGoogleVertex            KnownProvider = "google-vertex"
	KnownProviderOpenai                  KnownProvider = "openai"
	KnownProviderAzure                   KnownProvider = "azure"
	KnownProviderOpenaiCodex             KnownProvider = "openai-codex"
	KnownProviderRadius                  KnownProvider = "radius"
	KnownProviderTypesafe                KnownProvider = "typesafe"
	KnownProviderNvidia                  KnownProvider = "nvidia"
	KnownProviderDeepseek                KnownProvider = "deepseek"
	KnownProviderGithubCopilot           KnownProvider = "github-copilot"
	KnownProviderXai                     KnownProvider = "xai"
	KnownProviderGroq                    KnownProvider = "groq"
	KnownProviderCerebras                KnownProvider = "cerebras"
	KnownProviderOpenrouter              KnownProvider = "openrouter"
	KnownProviderVercelAiGateway         KnownProvider = "vercel-ai-gateway"
	KnownProviderZai                     KnownProvider = "zai"
	KnownProviderZaiCodingCn             KnownProvider = "zai-coding-cn"
	KnownProviderMistral                 KnownProvider = "mistral"
	KnownProviderMinimax                 KnownProvider = "minimax"
	KnownProviderMinimaxCn               KnownProvider = "minimax-cn"
	KnownProviderMoonshotai              KnownProvider = "moonshotai"
	KnownProviderMoonshotaiCn            KnownProvider = "moonshotai-cn"
	KnownProviderHuggingface             KnownProvider = "huggingface"
	KnownProviderFireworks               KnownProvider = "fireworks"
	KnownProviderTogether                KnownProvider = "together"
	KnownProviderBaseten                 KnownProvider = "baseten"
	KnownProviderOpencode                KnownProvider = "opencode"
	KnownProviderOpencodeGo              KnownProvider = "opencode-go"
	KnownProviderKimiCoding              KnownProvider = "kimi-coding"
	KnownProviderMeta                    KnownProvider = "meta"
	KnownProviderCloudflareWorkersAi     KnownProvider = "cloudflare-workers-ai"
	KnownProviderCloudflareAiGateway     KnownProvider = "cloudflare-ai-gateway"
	KnownProviderQwenTokenPlan           KnownProvider = "qwen-token-plan"
	KnownProviderQwenTokenPlanCn         KnownProvider = "qwen-token-plan-cn"
	KnownProviderQwenTokenPlanIndividual KnownProvider = "qwen-token-plan-individual"
	KnownProviderXiaomi                  KnownProvider = "xiaomi"
	KnownProviderXiaomiTokenPlanCn       KnownProvider = "xiaomi-token-plan-cn"
	KnownProviderXiaomiTokenPlanAms      KnownProvider = "xiaomi-token-plan-ams"
	KnownProviderXiaomiTokenPlanSgp      KnownProvider = "xiaomi-token-plan-sgp"
)

type LazyAPICapabilities

type LazyAPICapabilities struct {
	FetchDeferred  bool
	CancelDeferred bool
}

LazyAPICapabilities declares optional methods without loading the implementation.

type LazyOAuthInput

type LazyOAuthInput struct {
	Name           string
	IsSubscription bool
	LoginLabel     string
	// Load returns the flow. It runs at most once, at the first Login, Refresh or ToAuth; its error is returned by every one of those calls, as a rejected promise stays rejected.
	Load func() (*OAuthAuth, error)
}

LazyOAuthInput describes an OAuth method whose flow is loaded on first use.

upstream: packages/ai/src/auth/helpers.ts:40 (lazyOAuth input)

type LazyStreamObserver

type LazyStreamObserver struct {
	OnEvent  func(AssistantMessageEvent)
	OnResult func(*AssistantMessage)
}

LazyStreamObserver is a synchronous, read-only observation of the stream's existing forwarding path (lazy.ts LazyStreamObserver). OnEvent runs after each event is pushed to the outer stream, and OnResult once after the outer stream ends; a nil member is an omitted method, and OnResult receives nil for Pi's undefined result.

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
	// AgentName is the name this app introduces itself with during login, for example OpenAI's agent name hint and the Codex originator. Nil means the flow's own default; an empty name is kept as given (auth/types.ts:206-213).
	AgentName *string
}

LoginOptions is app-supplied context for Models.Login.

type Message

type Message interface {
	MessageRole() string
	// contains filtered or unexported methods
}

Message is the closed provider-facing transcript union.

func BorrowedWithoutInitialSystemMessage

func BorrowedWithoutInitialSystemMessage(messages []Message) []Message

func CollapsedMessages

func CollapsedMessages(context TranscriptContext) []Message

func RepairToolFlow

func RepairToolFlow(messages []Message) []Message

RepairToolFlow makes every tool result answer an open tool call, before transformMessages accounts for the calls.

pig divergence (D48): upstream transformMessages sends a result whose calling turn was dropped, a repeated result, and a result that arrives after its flow closed (while it also adds a "No result provided" placeholder for the call), and the provider rejects the request. Here an out-of-order result moves into the slot of its call, and an orphan or repeat is dropped. A result is matched in order against open calls rather than by id alone, because a provider may number its calls per response and reuse an id every turn. A call of an errored or aborted turn is not open: transformMessages omits that turn. A result is not moved back past a turn that opens a call with its id again: it answers that turn, as in Pi's request, so a request Pi sends with an intact tool flow reaches the provider unchanged.

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, drops another model's thinking, 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            []ModelInputModality
	ThinkingLevelMap ThinkingLevelMap
	SamplingParams   map[string]any
	// SamplingParamsByThinkingLevel overrides SamplingParams for the effective thinking level on OpenAI-compatible APIs.
	SamplingParamsByThinkingLevel SamplingParamsByThinkingLevel
	PromptCache                   ModelPromptCache
	// InputLimits mirrors upstream Model.inputLimits.
	InputLimits *ModelInputLimits
	// contains filtered or unexported fields
}

Model pairs an identifier with a provider and capabilities.

func GetBuiltinModel

func GetBuiltinModel(provider, modelID string) *Model

GetBuiltinModel reads one built-in chat model, or nil (providers/all.ts getBuiltinModel; compat.ts getModel).

func GetBuiltinModels

func GetBuiltinModels(provider string) []*Model

GetBuiltinModels lists the built-in chat models of one provider.

func GetRadiusModels

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

GetRadiusModels returns the legacy credential catalog bound to providerID (radius-config.ts:70).

func GetRadiusModelsFromConfig

func GetRadiusModelsFromConfig(providerID string, config RadiusGatewayConfig) []*Model

GetRadiusModelsFromConfig binds a gateway catalog to providerID: Model<"pi-messages">[] (radius-config.ts:61).

func ProviderEventModel

func ProviderEventModel(selected *Model, api API, providerID string, id string) *Model

func ResolveCloudflareModel

func ResolveCloudflareModel(model *Model, env ProviderEnv) *Model

ResolveCloudflareModel materializes the Cloudflare account and gateway placeholders of model's base URL from env (cloudflare-stream.ts:6-17). A missing value keeps its placeholder and an empty one replaces it with the empty string. A nil env or an endpoint without placeholders returns model itself; otherwise the result is a copy and model is untouched.

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, presence included: nil is no tiers member, empty is tiers: [] (see ModelCost).
	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,omitzero"`
}

ModelCost is the provider price per million tokens for one model. Tiers is optional (types.ts ModelCost.tiers): a nil Tiers is an absent member and an empty one an authored [], which JSON keeps apart, so copies of a cost keep a nil Tiers nil and an empty one empty (slices.Clone, not append to nil).

func OpenRouterCost

func OpenRouterCost(pricing *OpenRouterPricing) ModelCost

OpenRouterCost converts OpenRouter's $/token prices to $/1M tokens. A missing, invalid or negative base price is 0. Prompt-length overrides become request-wide tiers; a rate a tier does not list keeps the base price. Time-of-day overrides are skipped because a ModelCost cannot express them.

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 ModelInputModality

type ModelInputModality = string

ModelInputModality is an input a model accepts (model-schema.ts ModelInputModality): "text" or "image". It is an alias of string, so the lists of catalog and models.json inputs stay []string.

const (
	ModelInputText  ModelInputModality = "text"
	ModelInputImage ModelInputModality = "image"
)

The modalities of ModelInputModality.

type ModelPromptCache

type ModelPromptCache map[string]float64

ModelPromptCache gives the best-effort prompt-cache lifetime in seconds for each retention tier. Pi's ModelPromptCacheSchema types a lifetime as Type.Number({ exclusiveMinimum: 0 }), so it need not be whole.

type ModelThinkingLevel

type ModelThinkingLevel string

ModelThinkingLevel is "off" or a ThinkingLevel (types.ts ModelThinkingLevel); it is the level a model supports, maps and clamps to.

const (
	ThinkingOff     ModelThinkingLevel = "off"
	ThinkingMinimal ModelThinkingLevel = "minimal"
	ThinkingLow     ModelThinkingLevel = "low"
	ThinkingMedium  ModelThinkingLevel = "medium"
	ThinkingHigh    ModelThinkingLevel = "high"
	ThinkingXHigh   ModelThinkingLevel = "xhigh"
	ThinkingMax     ModelThinkingLevel = "max"
)

The levels of ModelThinkingLevel: "off" and the request levels.

func ClampReasoning

func ClampReasoning(effort ModelThinkingLevel) ModelThinkingLevel

ClampReasoning maps the levels above high, xhigh and max, to high and returns every other level, including the empty "undefined" level, unchanged. Mirrors upstream clampReasoning (simple-options.ts:77).

func ClampThinkingLevel

func ClampThinkingLevel(m *Model, level ModelThinkingLevel) ModelThinkingLevel

ClampThinkingLevel clamps level to the nearest supported thinking level. Mirrors upstream models.ts:clampThinkingLevel.

func GetSupportedThinkingLevels

func GetSupportedThinkingLevels(m *Model) []ModelThinkingLevel

GetSupportedThinkingLevels returns the model's supported thinking levels. Selected model reasoning metadata or a native MaxThinking capability enables reasoning.

func (ModelThinkingLevel) ReasoningOption

func (level ModelThinkingLevel) ReasoningOption() ThinkingLevel

ReasoningOption is the request option for a model level: "off" is no request (the `reasoning` option omitted), every other level is itself.

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

func (m *Models) CheckAuthWith(ctx context.Context, providerID string, options AuthOperationOptions) (*AuthCheck, error)

CheckAuthWith is Models.checkAuth(providerId, options): CheckAuth cancelled also by options.Signal.

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

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

GetAllAvailableWith is Models.getAllAvailable(providerId, options).

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

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

GetAvailableOfTypeWith is Models.getAvailableOfType(type, providerId, options).

func (*Models) GetAvailableWith

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

GetAvailableWith is Models.getAvailable(providerId, options). An empty providerID is every provider, as Pi's omitted one (GetAvailable already reads an empty id so).

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 of a provider registered with this collection and persists its returned credential.

func (*Models) LoginProvider

func (m *Models) LoginProvider(ctx context.Context, provider *ModelsProvider, authType AuthType, interaction AuthInteraction, options ...LoginOptions) (Credential, error)

LoginProvider runs the provider's own login flow and persists the returned credential in this collection's credential store under the provider's ID. The provider need not be registered here, so a caller can log in a provider it composed itself.

func (*Models) Logout

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

func (*Models) LogoutWith

func (m *Models) LogoutWith(ctx context.Context, providerID string, options AuthOperationOptions) error

LogoutWith is Models.logout(providerId, options).

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 ModelsCatalog

type ModelsCatalog interface {
	GetModelsOfType(modelType ModelType, provider ...string) []AnyModel
	GetAvailableOfType(ctx context.Context, modelType ModelType, provider ...string) ([]AnyModel, error)
	GetModelOfType(modelType ModelType, provider, id string) AnyModel
	Classify(ctx context.Context, model *ClassifierModel, request ClassifierContext, options ...ModelsClassifierOptions) ClassifierResult
	GenerateImages(ctx context.Context, model *ImageModel, request ImagesContext, options ...ModelsImagesOptions) AssistantImages
}

ModelsCatalog is the optional model catalog of a Models: every model type, classification and image generation.

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

func (*ModelsError) Name() string

Name is the upstream `name` property.

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 ModelsLister

type ModelsLister interface {
	GetModels(provider ...string) []*Model
}

ModelsLister is the optional listing of the chat models of a Models.

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 AmazonBedrockProvider

func AmazonBedrockProvider() *ModelsProvider

AmazonBedrockProvider is the built-in "amazon-bedrock" provider.

func AntLingProvider

func AntLingProvider() *ModelsProvider

AntLingProvider is the built-in "ant-ling" provider.

func AnthropicProvider

func AnthropicProvider() *ModelsProvider

AnthropicProvider is the built-in "anthropic" provider.

func AzureProvider

func AzureProvider() *ModelsProvider

AzureProvider is the built-in "azure" provider.

func BasetenProvider

func BasetenProvider() *ModelsProvider

BasetenProvider is the built-in "baseten" provider.

func BuiltinProvider

func BuiltinProvider() *ModelsProvider

BuiltinProvider is the built-in "all" provider.

func BuiltinProviders

func BuiltinProviders() []*ModelsProvider

BuiltinProviders returns every built-in provider, freshly constructed.

func CerebrasProvider

func CerebrasProvider() *ModelsProvider

CerebrasProvider is the built-in "cerebras" provider.

func CloudflareAIGatewayProvider

func CloudflareAIGatewayProvider() *ModelsProvider

CloudflareAIGatewayProvider is the built-in "cloudflare-ai-gateway" provider.

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 DeepseekProvider

func DeepseekProvider() *ModelsProvider

DeepseekProvider is the built-in "deepseek" provider.

func FireworksProvider

func FireworksProvider() *ModelsProvider

FireworksProvider is the built-in "fireworks" provider.

func GithubCopilotProvider

func GithubCopilotProvider() *ModelsProvider

GithubCopilotProvider is the built-in "github-copilot" provider.

func GoogleProvider

func GoogleProvider() *ModelsProvider

GoogleProvider is the Google provider (providers/google.ts googleProvider).

func GoogleVertexProvider

func GoogleVertexProvider() *ModelsProvider

GoogleVertexProvider is the built-in "google-vertex" provider.

func GroqProvider

func GroqProvider() *ModelsProvider

GroqProvider is the built-in "groq" provider.

func HuggingFaceProvider

func HuggingFaceProvider() *ModelsProvider

HuggingFaceProvider is the Hugging Face provider (providers/huggingface.ts huggingfaceProvider).

func KimiCodingProvider

func KimiCodingProvider() *ModelsProvider

KimiCodingProvider is the built-in "kimi-coding" provider.

func MetaProvider

func MetaProvider() *ModelsProvider

MetaProvider is the built-in "meta" provider.

func MinimaxCnProvider

func MinimaxCnProvider() *ModelsProvider

MinimaxCnProvider is the built-in "minimax-cn" provider.

func MinimaxProvider

func MinimaxProvider() *ModelsProvider

MinimaxProvider is the built-in "minimax" provider.

func MistralProvider

func MistralProvider() *ModelsProvider

MistralProvider is the built-in "mistral" provider.

func MoonshotaiCnProvider

func MoonshotaiCnProvider() *ModelsProvider

MoonshotaiCnProvider is the built-in "moonshotai-cn" provider.

func MoonshotaiProvider

func MoonshotaiProvider() *ModelsProvider

MoonshotaiProvider is the built-in "moonshotai" provider.

func NewRadiusProvider

func NewRadiusProvider(radiusOptions ...RadiusProviderOptions) *ModelsProvider

NewRadiusProvider ports radius.ts radiusProvider(options = {}) (radius.ts:22): the Radius gateway Provider<"pi-messages"> with a persisted, dynamically refreshed catalog. options is optional, and no argument is the empty options. It serves the published static catalog for the default gateway until the account's gateway catalog is known, which then replaces it (organization owners can disable models, so the shipped baseline only covers the time before any catalog exists).

func NvidiaProvider

func NvidiaProvider() *ModelsProvider

NvidiaProvider is the built-in "nvidia" provider.

func OpenAIProvider

func OpenAIProvider() *ModelsProvider

OpenAIProvider is the OpenAI provider (providers/openai.ts openaiProvider).

func OpenCodeGoProvider

func OpenCodeGoProvider() *ModelsProvider

OpenCodeGoProvider is the OpenCode Go provider (providers/opencode-go.ts opencodeGoProvider).

func OpenaiCodexProvider

func OpenaiCodexProvider() *ModelsProvider

OpenaiCodexProvider is the built-in "openai-codex" provider.

func OpenaiProvider

func OpenaiProvider() *ModelsProvider

OpenaiProvider is the built-in "openai" provider.

func OpencodeProvider

func OpencodeProvider() *ModelsProvider

OpencodeProvider is the built-in "opencode" provider; its API streams add the OpenCode session header (providers/opencode.ts).

func OpenrouterProvider

func OpenrouterProvider() *ModelsProvider

OpenrouterProvider is the built-in "openrouter" provider.

func QwenTokenPlanCnProvider

func QwenTokenPlanCnProvider() *ModelsProvider

QwenTokenPlanCnProvider is the built-in "qwen-token-plan-cn" provider.

func QwenTokenPlanIndividualProvider

func QwenTokenPlanIndividualProvider() *ModelsProvider

QwenTokenPlanIndividualProvider is the built-in "qwen-token-plan-individual" provider.

func QwenTokenPlanProvider

func QwenTokenPlanProvider() *ModelsProvider

QwenTokenPlanProvider is the built-in "qwen-token-plan" provider.

func TogetherProvider

func TogetherProvider() *ModelsProvider

TogetherProvider is the Together provider (providers/together.ts togetherProvider).

func TypesafeProvider

func TypesafeProvider() *ModelsProvider

TypesafeProvider is the TypeSafe provider: classifier models only.

func VercelAIGatewayProvider

func VercelAIGatewayProvider() *ModelsProvider

VercelAIGatewayProvider is the built-in "vercel-ai-gateway" provider.

func XaiProvider

func XaiProvider() *ModelsProvider

XaiProvider is the built-in "xai" provider.

func XiaomiProvider

func XiaomiProvider() *ModelsProvider

XiaomiProvider is the Xiaomi provider (providers/xiaomi.ts xiaomiProvider).

func XiaomiTokenPlanAmsProvider

func XiaomiTokenPlanAmsProvider() *ModelsProvider

XiaomiTokenPlanAmsProvider is the built-in "xiaomi-token-plan-ams" provider.

func XiaomiTokenPlanCnProvider

func XiaomiTokenPlanCnProvider() *ModelsProvider

XiaomiTokenPlanCnProvider is the built-in "xiaomi-token-plan-cn" provider.

func XiaomiTokenPlanSgpProvider

func XiaomiTokenPlanSgpProvider() *ModelsProvider

XiaomiTokenPlanSgpProvider is the built-in "xiaomi-token-plan-sgp" provider.

func ZaiCodingCnProvider

func ZaiCodingCnProvider() *ModelsProvider

ZaiCodingCnProvider is the built-in "zai-coding-cn" provider.

func ZaiProvider

func ZaiProvider() *ModelsProvider

ZaiProvider is the built-in "zai" provider.

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 []AnyModel
	// LastModified is the Unix timestamp from the remote catalog's
	// Last-Modified header.
	LastModified *float64
	// CheckedAt is the Unix timestamp of the last completed remote check.
	CheckedAt *float64
	// ETag is the remote catalog's opaque validator, stored verbatim.
	ETag string
	// contains filtered or unexported fields
}

ModelsStoreEntry mirrors pi-ai ModelsStoreEntry (models-store.ts:3-15). Models are typed and lossless: a model decoded from a catalog keeps the fields it has no member for and re-encodes them in their original order. A record of a model type this version does not know is kept aside and written back at its position, so reading and rewriting a catalog never drops it; it is not part of Models.

func (ModelsStoreEntry) Clone

func (entry ModelsStoreEntry) Clone() ModelsStoreEntry

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

func (ModelsStoreEntry) MarshalJSON

func (entry ModelsStoreEntry) MarshalJSON() ([]byte, error)

MarshalJSON writes the entry as the persisted object: every model through its catalog record, unknown-type records at their positions.

func (*ModelsStoreEntry) UnmarshalJSON

func (entry *ModelsStoreEntry) UnmarshalJSON(data []byte) error

UnmarshalJSON reads the persisted object; each model keeps its unknown fields, and records of unknown model types are set aside.

type ModelsStoreOperationOptions

type ModelsStoreOperationOptions struct {
	Signal context.Context
}

ModelsStoreOperationOptions is Pi's ModelsStoreOperationOptions: optional cancellation of a models-store operation, with the same meaning as AuthOperationOptions.Signal.

upstream: packages/ai/src/models-store.ts:ModelsStoreOperationOptions

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 NodeFetch

type NodeFetch struct{}

NodeFetch is Node's global fetch under the configured dispatcher, for code that calls fetch directly (the MCP transports and OAuth requests): a failure before the response headers rejects with TypeError("fetch failed"), an aborted request with "This operation was aborted", a body that drops with "terminated", and the idle timeouts of ConfigureHTTPDispatcher apply. It follows the environment proxy rules of the provider clients.

func NewNodeFetch

func NewNodeFetch() *NodeFetch

NewNodeFetch returns a fetch that sends each request through the dispatcher configured at that time, sharing its connection pool with the provider clients.

func (*NodeFetch) Do

func (f *NodeFetch) Do(request *http.Request) (*http.Response, error)

Do sends the request. A rejection is the fetch error itself: http.Client's "Post <url>:" decoration around it is not part of what fetch rejects with. The client is resolved per request because fetch uses the global dispatcher current at the call: ConfigureHTTPDispatcher after NewNodeFetch applies, as Pi's configureHttpDispatcher reinstalling the dispatcher (interactive-mode.ts applyRuntimeSettings) does.

type NodeReadable

type NodeReadable = nodeReadable

func NewIncomingMessageReadable

func NewIncomingMessageReadable(executor *continuationExecutor) *NodeReadable

NewIncomingMessageReadable is the Readable of a Node http.IncomingMessage.

type NodeTransportError

type NodeTransportError = nodeTransportError

func NewNodeTransportError

func NewNodeTransportError(message string, cause error) *NodeTransportError

NewNodeTransportError is the transport error with Node's message, wrapping cause.

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)
	// contains filtered or unexported fields
}

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

func LazyOAuth

func LazyOAuth(input LazyOAuthInput) *OAuthAuth

LazyOAuth wraps an OAuth flow so a provider definition can advertise OAuth without loading the implementation.

upstream: packages/ai/src/auth/helpers.ts:40 (lazyOAuth)

func OAuthProviderAuth

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

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

func (*OAuthAuth) AuthMethodName

func (a *OAuthAuth) AuthMethodName() string

AuthMethodName is the method's display name, which the provider selector searches (oauth-selector.ts:136 `provider.method?.name`).

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
	// OpenBrowserOnConfirm offers best-effort browser opening on user confirmation; nil (Pi's undefined) means true, and clients may ignore it.
	OpenBrowserOnConfirm *bool
	// CopyCodeOnConfirm offers copying UserCode on user confirmation, replacing the clipboard; nil (Pi's undefined) means true.
	CopyCodeOnConfirm *bool
	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)
	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
	// AgentName comes from LoginOptions.AgentName; nil means the flow's own default name.
	AgentName *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 ObservedBodyReadiness

type ObservedBodyReadiness = observedBodyReadiness

type ObservedResponseBody

type ObservedResponseBody = observedResponseBody

func NewObservedResponseBody

func NewObservedResponseBody(body io.ReadCloser, owner *BodyReadOwner) *ObservedResponseBody

NewObservedResponseBody wraps body so the provider reads it as a continuation turn does, through the connection owner.

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 OpenAIChatMessage

type OpenAIChatMessage = oaiMessage

OpenAIChatMessage is one message of a chat-completions request.

func ConvertCompletionsMessages

func ConvertCompletionsMessages(model *Model, transcript TranscriptContext, compat *OpenAICompat, options ConvertCompletionsMessagesOptions) ([]OpenAIChatMessage, error)

ConvertCompletionsMessages converts a transcript to the messages of an openai-completions request for model and compat: the transcript is resolved and transformed for the model, and the instruction message leads. A nil compat resolves every flag to its default.

Ports packages/ai/src/api/openai-completions.ts (convertMessages).

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
	// ExtraHeaders are the model's headers (models.json). They override the default originator and User-Agent; Authorization,
	// chatgpt-account-id and the transport headers stay the provider's.
	ExtraHeaders map[string]string
}

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  OpenRouterRouting                 `json:"openRouterRouting,omitempty"`
	ChatTemplateKwargs map[string]ChatTemplateKwargValue `json:"chatTemplateKwargs,omitempty"`
	VLLMPriority       *float64                          `json:"vllmPriority,omitempty"`
	// VercelGatewayRouting contains Vercel AI Gateway routing preferences.
	VercelGatewayRouting            *VercelGatewayRouting `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       ThinkingTokenBudgetField        `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"`
	// Extra holds the members PiG does not model, as written. Pi's compat schemas allow additional properties and a compat object keeps
	// them (model-config.ts getProvider, provider-composer.ts mergeCompat), so a custom API can read its own compat settings.
	Extra map[string]json.RawMessage `json:"-"`
}

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 the compat flags a provider name and base URL imply (openai-completions.ts detectCompat for a model without an id).

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, and writes the unmodeled members (Extra) after the modeled ones.

func (*OpenAICompat) UnmarshalJSON

func (compat *OpenAICompat) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes the modeled members and keeps every other member of the object in Extra, as Pi's compat object keeps an additional property.

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
	// SamplingParamsByThinkingLevel overrides SamplingParams for the effective thinking level.
	SamplingParamsByThinkingLevel SamplingParamsByThinkingLevel
	// 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
	// contains filtered or unexported fields
}

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
	// SamplingParamsByThinkingLevel overrides SamplingParams for the effective thinking level.
	SamplingParamsByThinkingLevel SamplingParamsByThinkingLevel
	// 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 OpenAIResponsesStreamOptions

type OpenAIResponsesStreamOptions struct {
	// OnProviderStreamEvent observes each wire event before it is mapped, with the model processResponsesStream was given; its error ends
	// the stream. Nil observes nothing. It is StreamOptions.OnProviderStreamEvent (openai-responses-shared.ts:111).
	OnProviderStreamEvent func(ctx context.Context, data any, model *Model) error
	// ServiceTier is the request's service tier, which prices a response that reports none.
	ServiceTier string
	// GrammarToolInputProperties names the input property of each grammar tool by tool name.
	GrammarToolInputProperties map[string]string
	// ResolveServiceTier picks the tier that prices a response from the response's tier and the request's; nil picks the response's
	// tier, then the request's.
	ResolveServiceTier func(responseServiceTier, requestServiceTier string) string
	// ApplyServiceTierPricing scales the response's priced usage by its tier; nil leaves the usage at the default rate.
	ApplyServiceTierPricing func(usage *Usage, serviceTier string)
	// contains filtered or unexported fields
}

OpenAIResponsesStreamOptions is openai-responses-shared.ts OpenAIResponsesStreamOptions: what a Responses adapter injects into processResponsesStream.

type OpenRouterModel

type OpenRouterModel struct {
	ID            string                 `json:"id"`
	ContextLength *float64               `json:"context_length"`
	TopProvider   *OpenRouterTopProvider `json:"top_provider"`
	Pricing       *OpenRouterPricing     `json:"pricing"`
}

OpenRouterModel is the fields pi reads from a model in OpenRouter's `GET /models` and `GET /models/user`.

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 OpenRouterPricing

type OpenRouterPricing struct {
	Prompt          OpenRouterRate              `json:"prompt"`
	Completion      OpenRouterRate              `json:"completion"`
	InputCacheRead  OpenRouterRate              `json:"input_cache_read"`
	InputCacheWrite OpenRouterRate              `json:"input_cache_write"`
	Overrides       []OpenRouterPricingOverride `json:"overrides"`
}

OpenRouterPricing is OpenRouter's per-token prices.

type OpenRouterPricingOverride

type OpenRouterPricingOverride struct {
	MinPromptTokens *float64        `json:"min_prompt_tokens"`
	UTCStart        json.RawMessage `json:"utc_start"`
	UTCEnd          json.RawMessage `json:"utc_end"`
	UTCDays         json.RawMessage `json:"utc_days"`
	Prompt          OpenRouterRate  `json:"prompt"`
	Completion      OpenRouterRate  `json:"completion"`
	InputCacheRead  OpenRouterRate  `json:"input_cache_read"`
	InputCacheWrite OpenRouterRate  `json:"input_cache_write"`
}

OpenRouterPricingOverride is a conditional price. MinPromptTokens selects prompt-length pricing; the utc_* members select time-of-day or weekday pricing and keep their raw JSON because only their presence matters. Missing rates keep the base price.

type OpenRouterRate

type OpenRouterRate string

OpenRouterRate is one of OpenRouter's per-token prices, a decimal string. A JSON number reads as its text, as String(value) does for Number.parseFloat; any other JSON value reads as the empty string, which parseFloat turns into NaN.

func (*OpenRouterRate) UnmarshalJSON

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

type OpenRouterRouting

type OpenRouterRouting = map[string]any

OpenRouterRouting is OpenRouter's provider-routing preferences, sent verbatim as the request's `provider` field (types.ts:977).

type OpenRouterTopProvider

type OpenRouterTopProvider struct {
	ContextLength       *float64 `json:"context_length"`
	MaxCompletionTokens *float64 `json:"max_completion_tokens"`
}

OpenRouterTopProvider is the limits OpenRouter reports for the provider that serves a model. A limit that is absent, null or 0 is not reported.

type OptionalAPIRegistration

type OptionalAPIRegistration struct {
	// API is the API the package implements; it must be one of the optional built-in APIs.
	API API
	// New builds the provider of a model on API, the provider every model-to-provider factory uses.
	New func(model Model) (Provider, error)
	// Factory is the ProviderFactory LookupBuiltInProvider and RegisteredAPIs offer for API.
	Factory ProviderFactory
	// ResponseErrorStatus reports the HTTP status of the provider SDK's response error in an error chain, for NormalizeProviderError. Nil when the provider has no such error.
	ResponseErrorStatus func(error) (int, bool)
}

OptionalAPIRegistration is what a package under ai/providers hands RegisterOptionalAPI from init.

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
	// 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. The per-request options (debug, toolChoice, onResponse) are StreamOptions fields.

type PiMessagesEvent

type PiMessagesEvent struct {
	Type                  PiMessagesEventType `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"`
	ResponseModel         *string             `json:"responseModel"`
	ProviderThinkingLevel *string             `json:"providerThinkingLevel"`
	Rewrite               map[string]any      `json:"rewrite"`
	// contains filtered or unexported fields
}

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

type PiMessagesEventType

type PiMessagesEventType string

PiMessagesEventType is the discriminator of the PiMessagesEvent union.

const (
	PiMessagesEventStart         PiMessagesEventType = "start"
	PiMessagesEventTextStart     PiMessagesEventType = "text_start"
	PiMessagesEventTextDelta     PiMessagesEventType = "text_delta"
	PiMessagesEventTextEnd       PiMessagesEventType = "text_end"
	PiMessagesEventThinkingStart PiMessagesEventType = "thinking_start"
	PiMessagesEventThinkingDelta PiMessagesEventType = "thinking_delta"
	PiMessagesEventThinkingEnd   PiMessagesEventType = "thinking_end"
	PiMessagesEventToolcallStart PiMessagesEventType = "toolcall_start"
	PiMessagesEventToolcallDelta PiMessagesEventType = "toolcall_delta"
	PiMessagesEventToolcallEnd   PiMessagesEventType = "toolcall_end"
	PiMessagesEventDone          PiMessagesEventType = "done"
	PiMessagesEventError         PiMessagesEventType = "error"
)

The event types a pi-messages backend sends (pi-messages.ts PiMessagesEvent).

type PiMessagesResponseError

type PiMessagesResponseError struct {
	Message           string
	Code              string
	DiagnosticDetails map[string]any
	// contains filtered or unexported fields
}

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

func NewPiMessagesResponseError

func NewPiMessagesResponseError(message, code string, diagnosticDetails map[string]any) *PiMessagesResponseError

NewPiMessagesResponseError ports pi-messages.ts PiMessagesResponseError constructor(message, code, diagnosticDetails); an empty code is upstream's undefined.

func (*PiMessagesResponseError) DiagnosticCode

func (e *PiMessagesResponseError) DiagnosticCode() any

DiagnosticCode is the upstream `code` property; it is absent when the backend sent none.

func (*PiMessagesResponseError) Error

func (e *PiMessagesResponseError) Error() string

func (*PiMessagesResponseError) Name

Name is the upstream `name` property.

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 NewGoogleVertexAPIProvider

func NewGoogleVertexAPIProvider(cfg GoogleVertexConfig) (Provider, error)

NewGoogleVertexAPIProvider builds the google-vertex provider for cfg, the provider every model-to-provider factory uses.

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 NewMistralAPIProvider

func NewMistralAPIProvider(cfg MistralConfig) (Provider, error)

NewMistralAPIProvider builds the mistral-conversations provider for cfg, the provider every model-to-provider factory uses.

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 NewOptionalAPIProvider

func NewOptionalAPIProvider(api API, model Model) (Provider, error)

NewOptionalAPIProvider builds the provider of model on an optional built-in API (bedrock-converse-stream). It fails with the strip error when a Piglet strips the API, and when no package registered the API's provider it names the package to import.

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.

func WrapBuiltinProvider

func WrapBuiltinProvider(providerID string, provider Provider) Provider

WrapBuiltinProvider applies to a per-model provider the wrapper that the built-in provider's upstream constructor puts on its API streams (providers/cloudflare-workers-ai.ts:20, providers/cloudflare-ai-gateway.ts:22-24: cloudflareStreams), the same table that registers the built-in providers uses. A provider without a wrapper, or whose API is not one of the HTTP providers that carry an endpoint, is returned as it is.

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.

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 (cloudflare-stream.ts:31-36). The classifier receives model itself when nothing changes.

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 OpenAIDecisionsAPI

func OpenAIDecisionsAPI() *ProviderClassifier

OpenAIDecisionsAPI is the openai-decisions 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

func MergeProviderEnv

func MergeProviderEnv(configured ProviderEnv, request ProviderEnv) ProviderEnv

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 GetOpenCodeSessionHeaders

func GetOpenCodeSessionHeaders(target ClientIdentityTarget, sessionID string) ProviderHeaders

GetOpenCodeSessionHeaders is OpenCode's per-conversation routing header for requests to OpenCode, including custom providers pointing at it. Nil without a session or for other targets.

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 ProviderID

type ProviderID = string

ProviderID is a provider id: a KnownProvider or any other string (types.ts ProviderId = KnownProvider | string).

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.

func OpenRouterImagesAPI

func OpenRouterImagesAPI() *ProviderImages

OpenRouterImagesAPI is the OpenRouter image implementation a provider lists under APIImagesOpenRouter (api/openrouter-images.lazy.ts openrouterImagesApi).

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 ProviderRequestError

type ProviderRequestError interface {
	error
	ProviderStatus() *int
	ProviderHeaders() http.Header
}

ProviderRequestError is the failure shape retryProviderRequest classifies (provider-retry.ts ProviderError): an optional HTTP status and the response headers. A nil status is a failure with no response, which is retried.

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 ProviderRetryOptions

type ProviderRetryOptions struct {
	// MaxRetries is the retry budget after a retryable failure. Nil means 0.
	MaxRetries *int
	// MaxRetryDelayMs caps a server-requested retry delay. Nil means 60000; 0 disables the cap.
	MaxRetryDelayMs *int
	// NoRetryStatuses are HTTP statuses that fail at once although the default policy would retry them.
	NoRetryStatuses []int
}

ProviderRetryOptions is provider-retry.ts ProviderRetryOptions; the signal is RetryProviderRequest's context.

type ProviderStreams

func AnthropicMessagesAPI

func AnthropicMessagesAPI() *ProviderStreams

AnthropicMessagesAPI is the anthropic-messages API (api/anthropic-messages.lazy.ts anthropicMessagesApi).

func AzureOpenAIResponsesAPI

func AzureOpenAIResponsesAPI() *ProviderStreams

AzureOpenAIResponsesAPI is the azure-openai-responses API (azureOpenAIResponsesApi).

func BedrockConverseStreamAPI

func BedrockConverseStreamAPI() *ProviderStreams

BedrockConverseStreamAPI is the bedrock-converse-stream API (bedrockConverseStreamApi).

func CloudflareStreams

func CloudflareStreams(streams *ProviderStreams) *ProviderStreams

CloudflareStreams wraps an API implementation so Cloudflare account and gateway endpoint placeholders materialize from the resolved provider env before dispatch (cloudflare-stream.ts:19-29). As Pi's wrapper returns an object with only stream and streamSimple, the result has no deferred operations, so a Cloudflare-wrapped API does not support deferred responses (models.ts:925-927).

func GoogleGenerativeAIAPI

func GoogleGenerativeAIAPI() *ProviderStreams

GoogleGenerativeAIAPI is the google-generative-ai API (googleGenerativeAIApi).

func GoogleVertexAPI

func GoogleVertexAPI() *ProviderStreams

GoogleVertexAPI is the google-vertex API (googleVertexApi).

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.

func MistralConversationsAPI

func MistralConversationsAPI() *ProviderStreams

MistralConversationsAPI is the mistral-conversations API (mistralConversationsApi).

func OpenAICodexResponsesAPI

func OpenAICodexResponsesAPI() *ProviderStreams

OpenAICodexResponsesAPI is the openai-codex-responses API (openAICodexResponsesApi).

func OpenAICompletionsAPI

func OpenAICompletionsAPI() *ProviderStreams

OpenAICompletionsAPI is the openai-completions API (openAICompletionsApi).

func OpenAIResponsesAPI

func OpenAIResponsesAPI() *ProviderStreams

OpenAIResponsesAPI is the openai-responses API (openAIResponsesApi).

func PiMessagesAPI

func PiMessagesAPI() *ProviderStreams

PiMessagesAPI is the pi-messages API (piMessagesApi).

func WithOpenCodeSessionHeader

func WithOpenCodeSessionHeader(streams *ProviderStreams) *ProviderStreams

WithOpenCodeSessionHeader adds OpenCode's required per-conversation routing header before API dispatch.

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 {
	// Type is Model.type, the optional "chat" a gateway may advertise (types.ts:1119); it travels through the spread of getRadiusModelsFromConfig.
	Type             ModelType         `json:"type,omitempty"`
	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             ModelCost         `json:"cost"`
	PromptCache      ModelPromptCache  `json:"promptCache,omitempty"`
	ContextWindow    int               `json:"contextWindow"`
	MaxTokens        int               `json:"maxTokens"`
	SamplingParams   map[string]any    `json:"samplingParams,omitempty"`
	// SamplingParamsByThinkingLevel is Model.samplingParamsByThinkingLevel: radius-config.ts sanitizeRadiusGatewayConfig and
	// getRadiusModelsFromConfig spread each gateway model, so the field a gateway advertises reaches the Model<"pi-messages">.
	//portlint:allow emptydrop a level missing from the map selects no sampling params, so an explicit empty map and an absent one resolve alike
	SamplingParamsByThinkingLevel SamplingParamsByThinkingLevel `json:"samplingParamsByThinkingLevel,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 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 Go handle of a Radius provider: the Pi provider object radiusProvider returns, with the gateway and the OAuth flow the object's auth was built from.

func NewRadiusGatewayProvider

func NewRadiusGatewayProvider(options RadiusProviderOptions) *RadiusProvider

NewRadiusGatewayProvider is NewRadiusProvider with the gateway and OAuth flow its auth was built from, for callers that configure Radius gateways from models.json.

func (*RadiusProvider) Auth

func (p *RadiusProvider) Auth() ProviderAuth

Auth is the provider's `auth` property (radius.ts:33-40, models.ts Provider.auth): 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.

func (*RadiusProvider) FindModel

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

FindModel returns the effective model with id.

func (*RadiusProvider) Gateway

func (p *RadiusProvider) Gateway() string

func (*RadiusProvider) GetModels

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

GetModels returns the account's catalog once it is known and the static baseline before (radius.ts getModels: dynamicModels ?? baselineModels). Each call returns new copies that the caller owns.

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. ctx is the refresh's abort signal.

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
	// BaseURL is the base URL of the registered provider, including configuration overrides that wrappers apply outside the provider receiving this context (models.ts RefreshModelsContext.baseUrl).
	BaseURL string
}

type RegisterFauxProviderOptions

type RegisterFauxProviderOptions = FauxConfig

RegisterFauxProviderOptions is faux.ts RegisterFauxProviderOptions: the options NewFauxProvider takes.

type ResolvedGoogleThinkingLevel

type ResolvedGoogleThinkingLevel string

ResolvedGoogleThinkingLevel is a ThinkingLevel without "xhigh" and "max" (google-shared.ts ResolvedGoogleThinkingLevel).

const (
	ResolvedGoogleThinkingLevelMinimal ResolvedGoogleThinkingLevel = "minimal"
	ResolvedGoogleThinkingLevelLow     ResolvedGoogleThinkingLevel = "low"
	ResolvedGoogleThinkingLevelMedium  ResolvedGoogleThinkingLevel = "medium"
	ResolvedGoogleThinkingLevelHigh    ResolvedGoogleThinkingLevel = "high"
)

type ResolvedSystemOneCompat

type ResolvedSystemOneCompat struct {
	ImageInput           SystemOneImageInput
	SendImageOptInHeader bool
}

ResolvedSystemOneCompat is SystemOneCompat with every field resolved.

type ResponsesInputItem

type ResponsesInputItem struct {
	// Common
	Type string `json:"type,omitempty"`
	Role string `json:"role,omitempty"`

	// For type=message (user/assistant/system/developer)
	Content json.RawMessage `json:"content,omitempty"`
	Status  string          `json:"status,omitempty"`
	ID      string          `json:"id,omitempty"`
	Phase   string          `json:"phase,omitempty"`

	// For type=function_call (a JSON string) and type=tool_search_call (an object)
	CallID    string          `json:"call_id,omitempty"`
	Namespace string          `json:"namespace,omitempty"`
	Name      string          `json:"name,omitempty"`
	Arguments json.RawMessage `json:"arguments,omitempty"`

	// For type=tool_search_call / tool_search_output: "client"
	Execution string `json:"execution,omitempty"`
	// For type=additional_tools / tool_search_output
	Tools []ResponsesTool `json:"tools,omitempty"`

	// For type=custom_tool_call (grammar tools): the raw string input.
	Input string `json:"input,omitempty"`

	// For type=function_call_output
	Output json.RawMessage `json:"output,omitempty"`

	// For type=reasoning (opaque replay)
	Summary          json.RawMessage `json:"summary,omitempty"`
	EncryptedContent json.RawMessage `json:"encrypted_content,omitempty"`

	// For type=configuration_update
	Reasoning *respReasoningEffort `json:"reasoning,omitempty"`
}

ResponsesInputItem is one element of the Responses API "input" array. Upstream: ResponseInput (union of message | function_call | function_call_output | reasoning).

func ConvertResponsesMessages

func ConvertResponsesMessages(model *Model, transcript TranscriptContext, allowedToolCallProviders map[string]bool, options ConvertResponsesMessagesOptions) ([]ResponsesInputItem, error)

ConvertResponsesMessages converts a transcript to the Responses API "input" array for model: the leading system message (unless options.IncludeSystemPrompt is false), user, assistant and tool-result messages, with tool-call ids normalized for the providers in allowedToolCallProviders. Tools are declared as GetResponsesToolSupport says: in additional_tools items, in synthetic tool search items, or not at all (the request's tool list carries them). Ports packages/ai/src/api/openai-responses-shared.ts convertResponsesMessages.

type ResponsesStrictDefault

type ResponsesStrictDefault int

ResponsesStrictDefault is the strict field of a function tool that no constrained-sampling request decides: absent from the options or false emits false, true emits true and null emits null (convertResponsesTools options.strict: boolean | null).

const (
	ResponsesStrictFalse ResponsesStrictDefault = iota
	ResponsesStrictTrue
	ResponsesStrictNull
)

type ResponsesTool

type ResponsesTool struct {
	Type        string         `json:"type"` // "function" | "custom"
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  map[string]any `json:"parameters,omitempty"`
	// Strict is a tri-state: absent (nil), null, true, or false. The default
	// varies by provider (generic omits it; codex sends null; strict-mode models
	// send false), so it is a raw JSON token rather than *bool.
	Strict json.RawMessage      `json:"strict,omitempty"`
	Format *ResponsesToolFormat `json:"format,omitempty"`
	// DeferLoading marks a tool loaded by a synthetic tool_search_output.
	DeferLoading bool `json:"defer_loading,omitempty"`
	// contains filtered or unexported fields
}

ResponsesTool is one entry of the Responses API "tools" array. A function tool carries parameters (+ strict when the model supports strict mode); a grammar (custom) tool carries a format block instead. Mirrors upstream OpenAITool.

func ConvertResponsesTools

func ConvertResponsesTools(tools []ToolSchema, options ConvertResponsesToolsOptions) ([]ResponsesTool, error)

ConvertResponsesTools maps tools to the Responses API "tools" array. A grammar constrained-sampling tool becomes a custom tool with a format block; a json_schema constrained tool requests strict mode when supported; any other function tool takes the default strict value and emits it only when the model supports strict mode. Ports packages/ai/src/api/openai-responses-shared.ts convertResponsesTools.

func (ResponsesTool) MarshalJSON

func (t ResponsesTool) MarshalJSON() ([]byte, error)

MarshalJSON writes a tool's members in the order convertResponsesTools creates them: strict is assigned after the literal that holds defer_loading (openai-responses-shared.ts:386-393), and the parameters keep the tool schema's own member order.

func (ResponsesTool) MarshalJSONTo

func (t ResponsesTool) MarshalJSONTo(encoder *jsontext.Encoder) error

MarshalJSONTo is MarshalJSON written straight into the request encoder (respRequest.MarshalJSONTo).

type ResponsesToolFormat

type ResponsesToolFormat struct {
	Type       string `json:"type"`   // "grammar"
	Syntax     string `json:"syntax"` // "lark" | "regex"
	Definition string `json:"definition"`
}

ResponsesToolFormat is a grammar (custom) tool's constrained-sampling format. Mirrors upstream OpenAITool custom.format.

type ResponsesToolSupportOptions

type ResponsesToolSupportOptions struct {
	SupportsMidConvoSystemMessages bool
	SupportsAdditionalTools        bool
	SupportsToolSearch             bool
}

ResponsesToolSupportOptions are the compat flags GetResponsesToolSupport reads.

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 RoleMessage

type RoleMessage interface{ MessageRole() string }

RoleMessage is upstream's `{ role: string }`: anything with a role, including an agent message type the transcript helpers do not know.

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 SamplingParams

type SamplingParams = map[string]any

SamplingParams holds free-form request body sampling keys such as temperature, top_p, top_k, min_p or repetition_penalty.

func ResolveSamplingParams

func ResolveSamplingParams(model *Model, thinkingLevel ModelThinkingLevel, requestParams SamplingParams) SamplingParams

ResolveSamplingParams merges the model's sampling defaults, the overrides for the effective thinking level, and the request's sampling keys, later keys winning. The level is first clamped to one the model supports. It returns nil when none of the three is set. Mirrors upstream resolveSamplingParams (simple-options.ts:24-34).

type SamplingParamsByThinkingLevel

type SamplingParamsByThinkingLevel = map[ModelThinkingLevel]SamplingParams

SamplingParamsByThinkingLevel holds sampling overrides keyed by Pi thinking level, not by provider effort value.

type SchemaObjectOrder

type SchemaObjectOrder = schemaObjectOrder

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 ShallowCapture

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

ShallowCapture is ShallowCopy taken now and copied later. Capture records what ShallowCopy would copy, without copying message data or parsing deferred tool arguments: a live view's published state is immutable once published and its nested-object references keep their identity, so the copy ShallowCopy builds from the capture at any later time equals the copy ShallowCopy would have built at the capture. A consumer that may never read the copy pays only for the capture.

func (*ShallowCapture) Capture

func (capture *ShallowCapture) Capture(message *AssistantMessage)

Capture records message as it is now. Nil captures nil.

func (*ShallowCapture) Observe

func (capture *ShallowCapture) Observe() *AssistantMessage

Observe returns what message.ShallowCopy().Observe() returned at the capture: owned message data at that publication.

func (*ShallowCapture) Publication

func (capture *ShallowCapture) Publication() (cell uintptr, seq uint64)

Publication is the captured message's Publication at the capture: the producer cell and its publication count for a live view, (0, 0) otherwise. The copies the capture returns hold the state of that publication.

func (*ShallowCapture) ShallowCopy

func (capture *ShallowCapture) ShallowCopy() *AssistantMessage

ShallowCopy returns what message.ShallowCopy returned at the capture.

type SplitErrorToolResult

type SplitErrorToolResult struct {
	// Content is the result content with each run of images replaced by a note.
	Content []TextContent
	// Caption is the text naming the tool call, to send before Images.
	Caption string
	Images  []ImageContent
}

SplitErrorToolResult is an error tool result split into a text-only result and the images to send after the tool results (transform-messages.ts SplitErrorToolResult).

func SplitErrorToolResultImages

func SplitErrorToolResultImages(message ToolResultMessage) *SplitErrorToolResult

SplitErrorToolResultImages splits the images out of an error tool result, for APIs that reject images in error results (Anthropic, and Claude on Bedrock). Send Content as the tool result and the caption and images after all tool results of the turn, in the same user message. It returns nil for a result that needs no split (Pi #10794, transform-messages.ts splitErrorToolResultImages).

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 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
}

Ports packages/ai/src/utils/event-stream.ts (the reaction-queue ordering a for-await consumer of EventStream observes). 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. The callback runs as a coroutine turn, so a long-lived callback that awaits many times hands execution to other turns without goroutine wakes; it identifies its goroutine once.

func (*StreamContinuation) RunOwned

func (continuation *StreamContinuation) RunOwned(owner StreamOwner, body func(*StreamObservation) error) error

RunOwned is Run for a caller that already holds its own StreamOwner: owner must be CurrentStreamOwner() of the goroutine that calls RunOwned, which is the goroutine the callback runs on. The callback runs directly on that goroutine, for a caller that runs one short callback after another.

type StreamFunction

type StreamFunction = ModelsStreamFunction

StreamFunction is a provider stream for one request (types.ts StreamFunction). Its options are the one StreamOptions.

type StreamObservation

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

Ports packages/agent/src/agent-loop.ts (the awaited event-sink continuations of the for-await loop in streamAssistantResponse). Ports packages/ai/src/utils/event-stream.ts (the observation of events a consumer of EventStream sees, ObserveEvents and ForwardStream). 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. Its awaits are called by the callback it scopes, on the callback's goroutine.

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.

wait must return once the callback has returned: the returning callback also tells the executor that this goroutine is about to serve it, so a turn granted in between waits for this goroutine instead of waking another.

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

func (observation *StreamObservation) Owner() StreamOwner

Owner is the goroutine that runs the callback this observation scopes.

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 {
	// Telemetry is the explicit parent span for telemetry produced by this logical request; nil is Pi's omitted option, which uses the no-op span.
	Telemetry telemetry.TelemetrySpan
	// 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:"-"`
	// AnthropicClient replaces the Anthropic Messages provider's client construction entirely: the provider sends its request through it and skips federation, OAuth and API-key header assembly, as Pi's `AnthropicOptions.client` does (anthropic-messages.ts:286-290, 607-609). Other providers ignore it.
	AnthropicClient AnthropicClient `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"`
	// Project is the Google Cloud project of a Vertex AI request; it takes precedence over the provider configuration and GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT (GoogleVertexOptions.project).
	Project string `json:"project,omitempty"`
	// Location is the Google Cloud location of a Vertex AI request; it takes precedence over the provider configuration and GOOGLE_CLOUD_LOCATION (GoogleVertexOptions.location).
	Location    string `json:"location,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               AnthropicEffort        `json:"effort,omitempty"`
	// ThinkingDisplay controls how Anthropic returns thinking content. Empty means "summarized" when thinking is enabled (anthropic-messages.ts:271, :1238, :1246).
	ThinkingDisplay     AnthropicThinkingDisplay `json:"thinkingDisplay,omitempty"`
	InterleavedThinking *bool                    `json:"interleavedThinking,omitempty"`
	RequestMetadata     map[string]string        `json:"requestMetadata,omitempty"`
	// RequestMetadataOrder is the insertion order of RequestMetadata's keys, which Bedrock's requestMetadata object keeps on the wire.
	// Keys missing from it follow in sorted order. Callers that decode the metadata from JSON fill it.
	RequestMetadataOrder []string `json:"-"`
	// BearerToken is the Bedrock bearer token; it takes precedence over APIKey and AWS_BEARER_TOKEN_BEDROCK (BedrockOptions.bearerToken).
	BearerToken string `json:"bearerToken,omitempty"`
	// ServiceTier is the OpenAI Responses and Codex service_tier request value (OpenAIResponsesOptions.serviceTier).
	ServiceTier string `json:"serviceTier,omitempty"`
	// TextVerbosity is the Codex text verbosity ("low", "medium" or "high"); empty is "low" (OpenAICodexResponsesOptions.textVerbosity).
	TextVerbosity string `json:"textVerbosity,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
	// ReasoningSummary is the raw OpenAI Responses reasoning summary mode ("auto", "detailed" or "concise"). Without an effort it requests medium effort.
	ReasoningSummary string `json:"reasoningSummary,omitempty"`
	// 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
	// RepairToolFlow makes a real provider repair the tool results of its request before transformMessages: an out-of-order result moves to its call and an orphaned or repeated result is dropped. The agent loop sets it for a model request; a direct provider call keeps Pi's request.
	// pig divergence (D48): upstream sends such results and the provider rejects the request.
	RepairToolFlow bool `json:"-"`
	// 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
	// Identity is how the request identifies the calling app: `User-Agent` and provider attribution headers. Absent fields use the defaults: a detailed `User-Agent` and no attribution (ProviderRequestOptions.identity).
	Identity *ClientIdentity `json:"identity,omitempty"`
	// 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 (a ToolChoice or its string) or a
	// provider-specific choice object. Anthropic and Bedrock also accept "any" and
	// {"type":"tool","name":...}.
	ToolChoice any
	// Debug asks a pi-messages backend for debug metadata such as routing response headers (pi-messages.ts PiMessagesOptions.debug).
	Debug bool `json:"debug,omitempty"`
	// Metadata contains optional provider request metadata. Providers extract
	// recognized fields and ignore the rest.
	Metadata map[string]any
	// AzureAPIVersion, AzureResourceName, AzureBaseURL and AzureDeploymentName are the per-request Azure endpoint options (AzureEndpointOptions, api/azure-openai-config.ts:7). Each outranks the matching AZURE_OPENAI_* variable.
	AzureAPIVersion     string `json:"azureApiVersion,omitempty"`
	AzureResourceName   string `json:"azureResourceName,omitempty"`
	AzureBaseURL        string `json:"azureBaseUrl,omitempty"`
	AzureDeploymentName string `json:"azureDeploymentName,omitempty"`
	// 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
	// ProviderStreamEventWanted reports, before an adapter parses a wire event for OnProviderStreamEvent, whether the observer would act on it now. When it reports false, the adapter skips parsing and calling OnProviderStreamEvent for that event; the caller promises that such a call would have returned nil and had no other effect. A wire event that would fail to parse is still parsed, so its error ends the request as before. Nil means the observer always acts.
	ProviderStreamEventWanted func() bool
	// contains filtered or unexported fields
}

StreamOptions are the options for a streaming LLM call.

func BuildBaseOptions

func BuildBaseOptions(model *Model, context TranscriptContext, options StreamOptions, apiKey string) StreamOptions

BuildBaseOptions returns the request options every simple stream sends: the output budget is the requested one, or the model's maximum when none was requested, clamped to the context window; the sampling parameters are resolved for the simple reasoning level, "off" when it is unset; and apiKey, when not empty, replaces options.APIKey. Go has one StreamOptions type for the simple and the provider-specific options, so every other field passes through unchanged.

upstream: packages/ai/src/api/simple-options.ts:buildBaseOptions

func (StreamOptions) RawThinking

func (o StreamOptions) RawThinking() bool

RawThinking reports whether the provider sends the thinking options as given, as the API's own stream does, instead of through the simple stream's levels and budgets.

type StreamOwner

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

StreamOwner identifies the goroutine that runs stream callbacks, so a goroutine that runs one callback after another identifies itself once instead of at each callback.

func CurrentStreamOwner

func CurrentStreamOwner() StreamOwner

CurrentStreamOwner is the owner of the calling goroutine.

type StreamToolCallDelta

type StreamToolCallDelta = streamToolCallDelta

func NewStreamToolCallDelta

func NewStreamToolCallDelta(index int, id, name, argumentsDelta string) StreamToolCallDelta

NewStreamToolCallDelta is the delta that starts or continues the tool call at index.

type StreamingJSONParser

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

StreamingJSONParser is the incremental, tolerant JSON parser described above.

func (*StreamingJSONParser) Append

func (p *StreamingJSONParser) Append(text string) []JSONChange

Append reads the next piece of text and returns the changes it made, in order. It costs time linear in the length of text.

func (*StreamingJSONParser) Snapshot

Snapshot returns the value of the text read so far. It returns the previous snapshot (same Version) while the text read since did not change the value, or while a fresh copy of the open path is not yet paid for.

type StreamingJSONSnapshot

type StreamingJSONSnapshot struct {
	// Value is nil before a value starts (Defined is false); otherwise a plain JSON value that never changes afterwards.
	Value   any
	Defined bool
	// Order is the member order of the objects in Value that are not in sorted order, by path.
	Order schemaObjectOrder

	// Version changes whenever Snapshot made a fresh copy.
	Version uint64
	// contains filtered or unexported fields
}

StreamingJSONSnapshot is the value of the text read so far.

type StringEnumOptions

type StringEnumOptions struct {
	Description string
	Default     string
}

StringEnumOptions are the optional description and default of StringEnum.

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[M RoleMessage](messages TranscriptMessages[M]) *SystemMessage

func GetInitialSystemMessage

func GetInitialSystemMessage[M RoleMessage](messages TranscriptMessages[M]) *SystemMessage

func (SystemMessage) MarshalJSON

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

MarshalJSON writes an empty but present sections object as {}, which Pi's durable store writes after a reset: an absent member and an empty object are different records.

func (SystemMessage) MessageRole

func (SystemMessage) MessageRole() string

MessageRole is the role property of a message.

type SystemOneCompat

type SystemOneCompat struct {
	ImageInput SystemOneImageInput `json:"imageInput,omitempty"`
	// SendImageOptInHeader sends TypeSafe-Danger-Zone-Images: true with requests that carry images.
	SendImageOptInHeader *bool `json:"sendImageOptInHeader,omitempty"`
}

SystemOneCompat holds the compatibility settings of System One classifier APIs (compat-schema.ts SystemOneCompatSchema). An unset field is detected from the provider, baseUrl and model ID.

type SystemOneImageAsset

type SystemOneImageAsset struct {
	Format     string
	DataBase64 string
}

SystemOneImageAsset is a System One image asset: base64 file bytes, referenced by its name from the state.

func (SystemOneImageAsset) MarshalJSON

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

type SystemOneImageAssets

type SystemOneImageAssets struct {
	Names  []string
	Assets []SystemOneImageAsset
}

SystemOneImageAssets are the assets in insertion order, keyed by the names the state references.

func (SystemOneImageAssets) All

All yields the assets in insertion order.

func (SystemOneImageAssets) Get

Get returns the asset with the given name.

func (SystemOneImageAssets) MarshalJSON

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

type SystemOneImageInput

type SystemOneImageInput string

SystemOneImageInput is how a service takes classifier images (SystemOneCompat.ImageInput).

const (
	// SystemOneImageInputAssets is TypeSafe's assets map, with the names placed before the state. PNG and JPEG only.
	SystemOneImageInputAssets SystemOneImageInput = "assets"
	// SystemOneImageInputImages is Clef's images list of data URLs: PNG, JPEG and WebP, at most four.
	SystemOneImageInputImages SystemOneImageInput = "images"
	// SystemOneImageInputStateImageURLs is OpenRouter's form: data URL parts placed before the state.
	SystemOneImageInputStateImageURLs SystemOneImageInput = "state-image-urls"
)

type SystemOneTransport

type SystemOneTransport struct {
	// API is the classifier API this transport implements.
	API ClassifierAPI
	// DetectCompat gives the settings of models that do not set them in Compat.
	DetectCompat func(model ClassifierModel) ResolvedSystemOneCompat
	// Label names the service in error messages.
	Label string
	URL   func(model ClassifierModel) string
	// Payload wraps the System One request in the service's request envelope.
	Payload func(model ClassifierModel, request SystemOneWireRequest) map[string]any
	// Output extracts the System One output ({ answers, usage }) from the service's response envelope.
	Output func(body json.RawMessage) (map[string]json.RawMessage, error)
}

SystemOneTransport holds the differences between services that serve System One models.

type SystemOneWireQuestions

type SystemOneWireQuestions ClassifierQuestions

SystemOneWireQuestions are the questions as System One names them: a public bool question is a wire-level noul.

func (SystemOneWireQuestions) MarshalJSON

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

type SystemOneWireRequest

type SystemOneWireRequest struct {
	// State is the context state, or with images a list of the images or their asset names followed by the state.
	State     any
	Questions SystemOneWireQuestions
	// Assets are the images as System One assets, keyed by the names the state references.
	Assets *SystemOneImageAssets `json:",omitempty"`
	// Images are the images as data URLs, Clef's extension to System One. nil is an absent `images` member and an empty slice
	// is an explicit `[]` (system-one-shared.ts SystemOneRequest.images?: string[]); Body and MarshalJSON both keep that difference.
	Images []string
}

SystemOneWireRequest is the System One request body without the transport-specific envelope.

func (SystemOneWireRequest) Body

func (r SystemOneWireRequest) Body() map[string]any

Body is the request's members in Pi's insertion order: state, questions, then assets or images.

func (SystemOneWireRequest) MarshalJSON

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

MarshalJSON writes the request's members in field order, with an absent assets or images member left out and an empty images list kept as [].

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"`
}

TextContent is a text fragment in a message.

func (TextContent) AppendStringify

func (content TextContent) AppendStringify(dst []byte) ([]byte, error)

AppendStringify appends the JSON.stringify text of MarshalJSON's output to dst (sdkjson.StringifyAppender).

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 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 TextSignatureV1

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

TextSignatureV1 is the JSON payload an OpenAI Responses text block keeps in TextContent.TextSignature (types.ts TextSignatureV1). V is always 1; Phase is "commentary" or "final_answer" when set.

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 ThinkingBudgetLevels

type ThinkingBudgetLevels uint8

ThinkingBudgetLevels is a set of thinking levels that have a configured budget.

const (
	ThinkingBudgetMinimalSet ThinkingBudgetLevels = 1 << iota
	ThinkingBudgetLowSet
	ThinkingBudgetMediumSet
	ThinkingBudgetHighSet
)

The levels of ThinkingBudgets that can carry a configured budget.

type ThinkingBudgets

type ThinkingBudgets struct {
	Minimal int
	Low     int
	Medium  int
	High    int
	// Set flags the levels whose budget was configured, including zero and negative budgets.
	Set ThinkingBudgetLevels
}

ThinkingBudgets maps thinking level names to token budgets. Upstream's ThinkingBudgets has an optional number per level, and a configured budget wins over the default whatever its value (simple-options.ts:82 spreads it over the defaults; google-generative-ai.ts:436 tests !== undefined), so a budget of zero or below is a real setting. A Go int cannot tell "0" from "unset", so Set records the levels configured explicitly; a level also counts as configured when its field is non-zero, which keeps a literal with only the level fields working. Use NewThinkingBudgets to build the value from optional settings.

func DefaultThinkingBudgets

func DefaultThinkingBudgets() ThinkingBudgets

DefaultThinkingBudgets returns the default budget map. Mirrors upstream defaultBudgets (simple-options.ts:32-37).

func NewThinkingBudgets

func NewThinkingBudgets(minimal, low, medium, high *int) *ThinkingBudgets

NewThinkingBudgets builds the custom budgets from optional per-level settings: a nil level is unset, any other value (zero and negative included) is configured. It returns nil when no level is set.

func (ThinkingBudgets) MarshalJSON

func (b ThinkingBudgets) MarshalJSON() ([]byte, error)

MarshalJSON writes the configured levels as upstream's ThinkingBudgets object ({"minimal":n,...}) in the declaration order minimal, low, medium, high; a configured zero is written, an unset level is omitted.

func (*ThinkingBudgets) UnmarshalJSON

func (b *ThinkingBudgets) UnmarshalJSON(data []byte) error

UnmarshalJSON reads upstream's ThinkingBudgets object: a present number is a configured level, whatever its value; an absent or null level is unset.

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

func (content ThinkingContent) AppendStringify(dst []byte) ([]byte, error)

AppendStringify appends the JSON.stringify text of MarshalJSON's output to dst (sdkjson.StringifyAppender).

func (ThinkingContent) MarshalJSON

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

func (ThinkingContent) SignatureEmpty

func (c ThinkingContent) SignatureEmpty() bool

SignatureEmpty reports whether the thinking block carries an empty signature a provider streamed, as opposed to none.

func (*ThinkingContent) UnmarshalJSON

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

func (ThinkingContent) WithSignatureEmpty

func (c ThinkingContent) WithSignatureEmpty(empty bool) ThinkingContent

WithSignatureEmpty returns c marked as carrying an empty signature the provider streamed.

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 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 ThinkingLevel

type ThinkingLevel string

ThinkingLevel is the reasoning effort a request can ask for (types.ts ThinkingLevel). It has no "off": an omitted level means no reasoning request.

const (
	ThinkingLevelMinimal ThinkingLevel = "minimal"
	ThinkingLevelLow     ThinkingLevel = "low"
	ThinkingLevelMedium  ThinkingLevel = "medium"
	ThinkingLevelHigh    ThinkingLevel = "high"
	// ThinkingLevelXHigh is supported only by models whose thinkingLevelMap maps "xhigh".
	ThinkingLevelXHigh ThinkingLevel = "xhigh"
	// ThinkingLevelMax is supported only by models whose thinkingLevelMap maps "max" (for example Claude Opus 4.6 adaptive thinking). Like xhigh it clamps down for models that do not map it.
	ThinkingLevelMax ThinkingLevel = "max"
)

The request levels of ThinkingLevel.

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 ThinkingTokenBudgetField

type ThinkingTokenBudgetField string

ThinkingTokenBudgetField is the top-level request field that caps reasoning tokens on OpenAI-compatible servers.

upstream: packages/ai/src/types.ts:101

const (
	ThinkingTokenBudgetFieldThinkingTokenBudget  ThinkingTokenBudgetField = "thinking_token_budget"
	ThinkingTokenBudgetFieldThinkingBudget       ThinkingTokenBudgetField = "thinking_budget"
	ThinkingTokenBudgetFieldThinkingBudgetTokens ThinkingTokenBudgetField = "thinking_budget_tokens"
)

The request fields an OpenAI-compatible server reads a reasoning-token cap from.

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"`
	// ArgumentsError is set when the model's complete arguments text was not a valid JSON object. Arguments is then empty; the text describes the problem and includes what the model sent, and tool argument validation fails with it (Pi 87f9a364).
	ArgumentsError string `json:"argumentsError,omitempty"`
	// contains filtered or unexported fields
}

ToolCall is a tool invocation block in an assistant message. 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 NewDecodedToolCall

func NewDecodedToolCall(id, name, thoughtSignature, namespace string, arguments JsonObject, rawArguments []byte) (ToolCall, error)

NewDecodedToolCall returns the tool call that decoding {"id":...,"name":...,"arguments":rawArguments,...} yields: the arguments object and the member order of its objects, which rawArguments, the JSON text of the arguments value, fixes. A caller that has already parsed the object uses it to skip the general decode. pig additive (D104): builds a decoded tool call for the single-pass entry decoder.

func ParseToolCallArguments

func ParseToolCallArguments(text string) ToolCall

ParseToolCallArguments is json-parse.ts parseToolCallArguments: the complete arguments text of a finished tool call becomes Pick<ToolCall, "arguments" | "argumentsError">, returned as a ToolCall holding only those two members. Text that is not a JSON object leaves Arguments empty and ArgumentsError describing the problem, never the parsed prefix.

func (*ToolCall) AppendStreamedArguments

func (content *ToolCall) AppendStreamedArguments(stream *ToolCallArgumentsStream, delta string)

AppendStreamedArguments reads a piece of the call's argument text into stream, the call's argument stream, and shows the partial arguments so far (ToolCallArgumentsStream.append).

func (ToolCall) AppendStringify

func (content ToolCall) AppendStringify(dst []byte) ([]byte, error)

AppendStringify appends the JSON.stringify text of MarshalJSON's output to dst (sdkjson.StringifyAppender): arguments in their recorded member order are appended in place rather than encoded apart.

func (ToolCall) ArgumentMembers

func (content ToolCall) ArgumentMembers() ([]ArgumentMember, error)

ArgumentMembers lists the members of Arguments as `Object.entries(arguments).map(([key, value]) => [key, JSON.stringify(value)])` does: integer-like keys ascending first, then the others in the order the model sent them, each value written as JSON.stringify writes it (no HTML escaping, negative zero as 0, finite numbers in JavaScript form, lone surrogates kept).

func (ToolCall) ArgumentOrder

func (c ToolCall) ArgumentOrder() SchemaObjectOrder

ArgumentOrder is the member order the arguments were received in.

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. The bytes may be shared with other calls of ArgumentsJSON: the caller must not modify them.

func (ToolCall) ArgumentsValue

func (r ToolCall) ArgumentsValue() any

func (*ToolCall) CopyArgumentsFrom

func (content *ToolCall) CopyArgumentsFrom(other ToolCall)

CopyArgumentsFrom gives content other's arguments, a non-object value included.

func (*ToolCall) DefaultFalsyArguments

func (content *ToolCall) DefaultFalsyArguments()

DefaultFalsyArguments is `arguments || {}`: a non-object value JavaScript treats as false (null, 0, false, "") becomes the empty object; [] and any other value stay.

func (ToolCall) MarshalJSON

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

func (ToolCall) NonObjectArguments

func (c ToolCall) NonObjectArguments() json.RawMessage

NonObjectArguments is the JSON text of arguments that are valid JSON but not an object; Arguments is nil while it is set.

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

func (content *ToolCall) SetFinalArguments(input string)

SetFinalArguments stores the complete arguments text of a finished tool call as Pi's parseToolCallArguments does: text that is not a valid JSON object leaves Arguments empty and ArgumentsError describing the problem, never the parsed prefix.

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 ToolCallArgumentsStream

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

ToolCallArgumentsStream is the arguments of one streaming tool call: the raw text, for the one full parse when the call ends, and a StreamingJSONParser for the partial arguments in between (Pi's ToolCallArgumentsStream). Providers keep one per call in their own stream state, never on the content block, so the raw text never reaches partial messages or transcripts.

func NewToolCallArgumentsStream

func NewToolCallArgumentsStream() *ToolCallArgumentsStream

NewToolCallArgumentsStream returns an empty stream; its partial arguments are the empty object.

func (*ToolCallArgumentsStream) Append

func (stream *ToolCallArgumentsStream) Append(delta string) (JsonObject, schemaObjectOrder)

Append adds a piece of argument text and returns the partial arguments: a new object whenever they changed.

func (*ToolCallArgumentsStream) Finish

func (stream *ToolCallArgumentsStream) Finish() (JsonObject, schemaObjectOrder, string)

Finish is the final arguments: one full parse of the complete text, which reports malformed arguments.

func (*ToolCallArgumentsStream) Record

func (stream *ToolCallArgumentsStream) Record(text string)

Record adds argument text whose arguments the caller already has, without reading it as a delta.

func (*ToolCallArgumentsStream) Replace

func (stream *ToolCallArgumentsStream) Replace(text string) (delta string, hasDelta bool, arguments JsonObject, order schemaObjectOrder)

Replace takes the provider's complete argument text. When it continues the text so far, it returns the missing piece as delta (hasDelta true); otherwise it starts over with text. Either way it returns the partial arguments of text.

func (*ToolCallArgumentsStream) Text

func (stream *ToolCallArgumentsStream) Text() string

Text is the raw argument text received so far.

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 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 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 ToolChoice

type ToolChoice string

ToolChoice is the provider-neutral tool choice: let the model decide, or call no tool. Mirrors upstream ToolChoice (types.ts:84).

const (
	ToolChoiceAuto ToolChoice = "auto"
	ToolChoiceNone ToolChoice = "none"
)

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"`
	// DurationMs is the milliseconds execute() took, measured with a monotonic clock. Nil when the tool did not run, and for results stored before it was recorded.
	// upstream: ai/src/types.ts ToolResultMessage.durationMs
	DurationMs *int64 `json:"durationMs,omitempty"`
	Timestamp  int64  `json:"timestamp"`
}

ToolResultMessage carries one tool execution result.

func (ToolResultMessage) MarshalJSON

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

func (ToolResultMessage) MessageRole

func (ToolResultMessage) MessageRole() string

MessageRole is the role property of a message.

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 BorrowedCurrentTools

func BorrowedCurrentTools[M RoleMessage](messages TranscriptMessages[M]) []ToolSchema

func GetCurrentTools

func GetCurrentTools[M RoleMessage](messages TranscriptMessages[M]) []ToolSchema

func GetDeclaredTools

func GetDeclaredTools[M RoleMessage](messages TranscriptMessages[M]) []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) ParametersJSON

func (tool ToolSchema) ParametersJSON() ([]byte, error)

ParametersJSON encodes Parameters as JSON.stringify writes the schema: a decoded schema keeps its authored member order, and an object the order does not record (a Go-authored map) has sorted members.

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

func GetToolStateChangesFrom

func GetToolStateChangesFrom[M RoleMessage](messages TranscriptMessages[M], current []ToolSchema) ToolStateChanges

GetToolStateChangesFrom is GetToolStateChanges(GetCurrentTools(messages), current) without the copy of every replayed tool: the replayed declarations are only compared, never returned.

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

CollapseSystemMessages and ResolveTranscript share the messages of the transcript they are given: nothing modifies a transcript's messages (Messages hands out copies, At's callers and borrowers must not modify), and every one of them is already normalized and validated, so only the collapsed system message is new. Providers borrow the result (collapsedMessages, resolvedMessages), so a request build copies no message it does not change. pi-ai shares the same way: resolveTranscript returns the context itself and collapseSystemMessages filters it. upstream: packages/ai/src/utils/transcript.ts:collapseSystemMessages

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

func (context TranscriptContext) At(i int) Message

At returns the transcript's i-th message without copying it, as reading pi-ai's transcript.messages[i] does. The message shares its content with the transcript: modifying anything it references corrupts the transcript. Use Messages for a copy. It panics when i is out of range.

func (TranscriptContext) FrozenContext

func (context TranscriptContext) FrozenContext() Context

FrozenContext is a Context for a stream made from this transcript: its messages are shared, not copied, and normalizing it does not validate them again. The caller must not modify Messages or anything a message references. pig additive (D104): pi-ai's normalizeContext neither copies nor revalidates a transcript's messages.

func (TranscriptContext) Len

func (context TranscriptContext) Len() int

Len is the number of messages in the normalized transcript. pig additive (D104): reads transcript.messages without the copy Messages makes.

func (TranscriptContext) Messages

func (context TranscriptContext) Messages() []Message

Messages returns a deep copy of the normalized transcript.

func (TranscriptContext) WithRepairedToolFlow

func (r TranscriptContext) WithRepairedToolFlow() TranscriptContext

type TranscriptMessages

type TranscriptMessages[M RoleMessage] = []M

TranscriptMessages is any message list: `readonly { role: string }[]` (utils/transcript.ts:40). The replay helpers read only the entries whose role is "system".

type TranscriptToolSupport

type TranscriptToolSupport string

TranscriptToolSupport is how a transport can express tool changes after the first request.

  • TranscriptToolSupportNone: only the top-level tool list exists; it always holds the current tools.
  • TranscriptToolSupportAdditions: tools can be added at a later message, but not removed or redefined.
  • TranscriptToolSupportChanges: every tool, including the initial ones, is declared in place, and a later declaration under the same name replaces the earlier one. Removals are not expressible on the wire, so the transport announces them as text and the removed tools stay declared.
const (
	TranscriptToolSupportNone      TranscriptToolSupport = "none"
	TranscriptToolSupportAdditions TranscriptToolSupport = "additions"
	TranscriptToolSupportChanges   TranscriptToolSupport = "changes"
)

func GetResponsesToolSupport

func GetResponsesToolSupport(options ResponsesToolSupportOptions) TranscriptToolSupport

GetResponsesToolSupport is how a Responses transport expresses tool changes.

additional_tools items accumulate: a later item adds tools and replaces same-name definitions from earlier items, but it cannot replace a top-level tool and cannot remove any tool (verified with strict schemas on the OpenAI, Codex, and Copilot backends, October 2026). Like Codex, every tool is therefore declared in additional_tools items, starting with one at the head of the input, and removals are announced as text. Synthetic tool search can only add tools. Without mid-conversation system messages there is nothing to anchor. Ports packages/ai/src/api/openai-responses-shared.ts getResponsesToolSupport.

type TranscriptTools

type TranscriptTools struct {
	// RequestTools are the tools sent in the top-level request field.
	RequestTools []ToolSchema
	// FoldedSystemMessages is the number of leading system messages whose tool changes RequestTools already holds. Every later
	// system message carries its own tool changes in place.
	FoldedSystemMessages int
}

func ResolveTranscriptTools

func ResolveTranscriptTools[M RoleMessage](messages TranscriptMessages[M], support TranscriptToolSupport) TranscriptTools

ResolveTranscriptTools splits tool declarations between the top-level request field and in-place changes.

With TranscriptToolSupportChanges, the top-level list stays empty and every system message, including the leading one, declares its tools in place: an in-place declaration can replace an earlier in-place one, but not a top-level one. With TranscriptToolSupportAdditions, a removal or redefinition cannot be expressed in place, so the top-level list becomes the tool state after the last such change; later additions stay anchored, and the cached prefix is invalidated once per removal instead of on every later addition. With TranscriptToolSupportNone, the top-level list is the current tool set.

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"`
	// contains filtered or unexported fields
}

Usage reports token usage from the provider.

func DecodedFormUsage

func DecodedFormUsage(usage Usage) (Usage, bool)

DecodedFormUsage is the usage decoding the JSON encoding of usage yields, and false when a cost is not a finite number other than negative zero.

func ParseClassifierUsage

func ParseClassifierUsage(raw json.RawMessage, model ClassifierModel) *Usage

ParseClassifierUsage reads a { input_tokens, output_tokens } object and prices it from the model catalog like chat usage. A missing or malformed usage object leaves the result without usage instead of failing it.

func (Usage) AppendStringify

func (usage Usage) AppendStringify(dst []byte) ([]byte, error)

AppendStringify appends the JSON.stringify text of MarshalJSON's output to dst (sdkjson.StringifyAppender). A non-finite cost is the error MarshalJSON reports.

func (Usage) MarshalJSON

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

MarshalJSON emits the upstream Usage wire shape. A provider creates cacheWrite1h and reasoning on a usage object that already holds totalTokens and cost (anthropic-messages.ts:684, :846-851, bedrock-converse-stream.ts:715), so JSON.stringify writes them last; usage built as one literal (usage-totals.ts combineUsage) holds them before totalTokens.

func (*Usage) SetSplitsAfterCost

func (u *Usage) SetSplitsAfterCost(splits bool)

SetSplitsAfterCost records that the provider created CacheWrite1h or Reasoning after the other members (Usage.MarshalJSON).

func (*Usage) UnmarshalJSON

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

UnmarshalJSON decodes the upstream Usage wire shape and keeps where the source wrote cacheWrite1h and reasoning.

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"`
	// TotalFromProvider is true when Total is the billed cost the provider reported and false when it is a catalog estimate. Nil is the
	// absent member of a usage recorded before the field existed, which also means false (types.ts Usage.cost.totalFromProvider).
	TotalFromProvider *bool `json:"totalFromProvider,omitempty"`
}

UsageCost is the cost breakdown carried by upstream Usage. The component costs are catalog estimates; Total is the best available total.

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 UserAgentMode

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

UserAgentMode is the `User-Agent` sent to providers: UserAgentDetailed, UserAgentShort, or UserAgentCustom(value) (upstream's `"short" | "detailed" | { custom: string }`). The zero value is not a mode; ClientIdentity.UserAgent is nil when the mode is omitted.

pig divergence (D65): detailed is `pig/<version> (<platform> <release>; <arch>)` and short is `pig`, where upstream sends `pi/<version> (<platform>; <runtime>; <arch>)` and `pi`.

func UserAgentCustom

func UserAgentCustom(value string) UserAgentMode

UserAgentCustom is the `{ custom }` mode: the given value is the `User-Agent`.

func (UserAgentMode) Custom

func (mode UserAgentMode) Custom() (string, bool)

Custom reports the value of a custom mode.

func (UserAgentMode) MarshalJSON

func (mode UserAgentMode) MarshalJSON() ([]byte, error)

MarshalJSON writes `"short"`, `"detailed"` or `{"custom":"..."}`.

func (*UserAgentMode) UnmarshalJSON

func (mode *UserAgentMode) UnmarshalJSON(data []byte) error

UnmarshalJSON reads the forms MarshalJSON writes.

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)

func (UserMessage) MessageRole

func (UserMessage) MessageRole() string

MessageRole is the role property of a message.

type UserText

type UserText string

UserText carries a plain user message.

type ValidationMemo

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

ValidationMemo remembers which JSON-valued references (a tool result's details, a tool call's arguments) a Frozen transcript already validated, so a transcript that grows by one message per turn validates each value once instead of once per turn. A value is known by its identity: a pointer, a map, or the first element and length of a slice. A Frozen context promises that nothing modifies a value it references, and a caller that reuses a memo extends that promise to every request the memo serves. The memo holds each value it has seen, so an identity never names two values.

Each normalization keeps the entries its transcript still references and drops the rest, so the memo holds the live transcript's values and no replaced history. The zero value is an empty memo; a nil *ValidationMemo validates every value. pig additive (D104): pi-ai validates nothing.

func NewValidationMemo

func NewValidationMemo() *ValidationMemo

NewValidationMemo returns an empty memo.

type VercelGatewayRouting

type VercelGatewayRouting struct {
	// Only lists provider slugs to exclusively use for a request, for example ["bedrock", "anthropic"]. An empty list is set.
	Only []string `json:"only,omitzero"`
	// Order lists provider slugs to try in order, for example ["anthropic", "openai"]. An empty list is set.
	Order []string `json:"order,omitzero"`
}

VercelGatewayRouting is the Vercel AI Gateway routing preference: which upstream providers the gateway may use and in which order (types.ts VercelGatewayRouting).

Source Files

Directories

Path Synopsis
Package all links every built-in provider package under ai/providers, so a program that imports it offers the same providers as pig does.
Package all links every built-in provider package under ai/providers, so a program that imports it offers the same providers as pig does.
providers
bedrock
Package bedrock is the Amazon Bedrock Converse Stream provider (bedrock-converse-stream).
Package bedrock is the Amazon Bedrock Converse Stream provider (bedrock-converse-stream).

Jump to

Keyboard shortcuts

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