@bastani/pi-ai 0.9.37 → 0.9.38-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +6 -6
  3. package/dist/api/anthropic-messages.d.ts.map +1 -1
  4. package/dist/api/anthropic-messages.js +35 -21
  5. package/dist/api/anthropic-messages.js.map +1 -1
  6. package/dist/api/bedrock-converse-stream.js +16 -18
  7. package/dist/api/bedrock-converse-stream.js.map +1 -1
  8. package/dist/api/google-shared.d.ts.map +1 -1
  9. package/dist/api/google-shared.js +2 -1
  10. package/dist/api/google-shared.js.map +1 -1
  11. package/dist/api/mistral-conversations.js +2 -2
  12. package/dist/api/mistral-conversations.js.map +1 -1
  13. package/dist/api/openai-codex-responses.d.ts +1 -0
  14. package/dist/api/openai-codex-responses.d.ts.map +1 -1
  15. package/dist/api/openai-codex-responses.js +7 -2
  16. package/dist/api/openai-codex-responses.js.map +1 -1
  17. package/dist/api/openai-completions.d.ts +1 -1
  18. package/dist/api/openai-completions.d.ts.map +1 -1
  19. package/dist/api/openai-completions.js +32 -30
  20. package/dist/api/openai-completions.js.map +1 -1
  21. package/dist/api/openai-responses-shared.d.ts +10 -0
  22. package/dist/api/openai-responses-shared.d.ts.map +1 -1
  23. package/dist/api/openai-responses-shared.js +16 -3
  24. package/dist/api/openai-responses-shared.js.map +1 -1
  25. package/dist/api/openai-responses.d.ts.map +1 -1
  26. package/dist/api/openai-responses.js +25 -6
  27. package/dist/api/openai-responses.js.map +1 -1
  28. package/dist/api/openai-retry-hint-client.d.ts +10 -0
  29. package/dist/api/openai-retry-hint-client.d.ts.map +1 -0
  30. package/dist/api/openai-retry-hint-client.js +17 -0
  31. package/dist/api/openai-retry-hint-client.js.map +1 -0
  32. package/dist/api/transform-messages.d.ts +16 -1
  33. package/dist/api/transform-messages.d.ts.map +1 -1
  34. package/dist/api/transform-messages.js +28 -15
  35. package/dist/api/transform-messages.js.map +1 -1
  36. package/dist/auth/oauth/kimi-coding.d.ts +1 -1
  37. package/dist/auth/oauth/kimi-coding.js +1 -1
  38. package/dist/auth/oauth/kimi-coding.js.map +1 -1
  39. package/dist/index.d.ts +1 -1
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +1 -1
  42. package/dist/index.js.map +1 -1
  43. package/dist/model-catalog.d.ts +1 -1
  44. package/dist/model-catalog.d.ts.map +1 -1
  45. package/dist/model-catalog.js +32 -2
  46. package/dist/model-catalog.js.map +1 -1
  47. package/dist/providers/compat-schema.d.ts +159 -0
  48. package/dist/providers/compat-schema.d.ts.map +1 -1
  49. package/dist/providers/compat-schema.js +12 -2
  50. package/dist/providers/compat-schema.js.map +1 -1
  51. package/dist/providers/data/.manifest.json +1 -1
  52. package/dist/providers/data/anthropic.json +1 -1
  53. package/dist/providers/data/baseten.json +1 -1
  54. package/dist/providers/data/fireworks.json +1 -1
  55. package/dist/providers/data/kimi-coding.json +1 -1
  56. package/dist/providers/data/nvidia.json +1 -1
  57. package/dist/providers/data/openai.json +1 -1
  58. package/dist/providers/data/opencode-go.json +1 -1
  59. package/dist/providers/data/opencode.json +1 -1
  60. package/dist/providers/data/openrouter.json +1 -1
  61. package/dist/providers/data/radius.json +1 -1
  62. package/dist/providers/data/together.json +1 -1
  63. package/dist/providers/data/vercel-ai-gateway.json +1 -1
  64. package/dist/providers/data/xiaomi-token-plan-ams.json +1 -1
  65. package/dist/providers/data/xiaomi-token-plan-cn.json +1 -1
  66. package/dist/providers/data/xiaomi-token-plan-sgp.json +1 -1
  67. package/dist/providers/data/xiaomi.json +1 -1
  68. package/dist/providers/data/zai-coding-cn.json +1 -1
  69. package/dist/providers/kimi-coding.d.ts +1 -1
  70. package/dist/providers/kimi-coding.js +3 -3
  71. package/dist/providers/kimi-coding.js.map +1 -1
  72. package/dist/providers/model-schema.d.ts.map +1 -1
  73. package/dist/providers/model-schema.js +4 -2
  74. package/dist/providers/model-schema.js.map +1 -1
  75. package/dist/providers/openai-codex.js.map +1 -1
  76. package/dist/providers/openai-mid-convo-effort.d.ts +2 -0
  77. package/dist/providers/openai-mid-convo-effort.d.ts.map +1 -0
  78. package/dist/providers/openai-mid-convo-effort.js +7 -0
  79. package/dist/providers/openai-mid-convo-effort.js.map +1 -0
  80. package/dist/providers/openai.d.ts.map +1 -1
  81. package/dist/providers/openai.js +11 -2
  82. package/dist/providers/openai.js.map +1 -1
  83. package/dist/providers/opencode-go.d.ts +3 -1
  84. package/dist/providers/opencode-go.d.ts.map +1 -1
  85. package/dist/providers/opencode-go.js +2 -0
  86. package/dist/providers/opencode-go.js.map +1 -1
  87. package/dist/types.d.ts +3 -0
  88. package/dist/types.d.ts.map +1 -1
  89. package/dist/types.js.map +1 -1
  90. package/dist/utils/json-parse.d.ts +2 -0
  91. package/dist/utils/json-parse.d.ts.map +1 -1
  92. package/dist/utils/json-parse.js +57 -3
  93. package/dist/utils/json-parse.js.map +1 -1
  94. package/dist/utils/retry-hints.d.ts +12 -0
  95. package/dist/utils/retry-hints.d.ts.map +1 -0
  96. package/dist/utils/retry-hints.js +50 -0
  97. package/dist/utils/retry-hints.js.map +1 -0
  98. package/dist/utils/retry.d.ts +20 -2
  99. package/dist/utils/retry.d.ts.map +1 -1
  100. package/dist/utils/retry.js +35 -4
  101. package/dist/utils/retry.js.map +1 -1
  102. package/dist/utils/validation.d.ts.map +1 -1
  103. package/dist/utils/validation.js +3 -0
  104. package/dist/utils/validation.js.map +1 -1
  105. package/package.json +2 -2
  106. package/dist/utils/assistant-message-frame.d.ts +0 -77
  107. package/dist/utils/assistant-message-frame.d.ts.map +0 -1
  108. package/dist/utils/assistant-message-frame.js +0 -439
  109. package/dist/utils/assistant-message-frame.js.map +0 -1
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { TelemetryContext } from \"@earendil-works/pi-telemetry\";\nimport type { AnthropicOptions } from \"./api/anthropic-messages.ts\";\nimport type { AzureOpenAIResponsesOptions } from \"./api/azure-openai-responses.ts\";\nimport type { BedrockOptions } from \"./api/bedrock-converse-stream.ts\";\nimport type { GoogleOptions } from \"./api/google-generative-ai.ts\";\nimport type { GoogleVertexOptions } from \"./api/google-vertex.ts\";\nimport type { MistralOptions } from \"./api/mistral-conversations.ts\";\nimport type { OpenAICodexResponsesOptions } from \"./api/openai-codex-responses.ts\";\nimport type { OpenAICompletionsOptions } from \"./api/openai-completions.ts\";\nimport type { OpenAIImagesOptions } from \"./api/openai-images.ts\";\nimport type { OpenAIResponsesOptions } from \"./api/openai-responses.ts\";\nimport type { PiMessagesOptions } from \"./api/pi-messages.ts\";\nimport type {\n\tAnthropicMessagesCompat,\n\tBedrockCompat,\n\tMistralConversationsCompat,\n\tOpenAICompletionsCompat,\n\tOpenAIResponsesCompat,\n} from \"./providers/compat-schema.ts\";\nimport type {\n\tCacheRetention,\n\tModelCost,\n\tModelInputLimits,\n\tModelInputModality,\n\tModelPromptCache,\n\tModelThinkingLevel,\n\tThinkingLevel,\n\tThinkingLevelMap,\n} from \"./providers/model-schema.ts\";\nimport type { AssistantMessageDiagnostic } from \"./utils/diagnostics.ts\";\nimport type { AssistantMessageEventStream } from \"./utils/event-stream.ts\";\n\nexport type {\n\tAnthropicAllowedFallbackModel,\n\tAnthropicMessagesCompat,\n\tBedrockCompat,\n\tChatTemplateKwargValue,\n\tMistralConversationsCompat,\n\tOpenAICompletionsCompat,\n\tOpenAIResponsesCompat,\n\tOpenRouterRouting,\n\tSessionAffinityFormat,\n\tThinkingTokenBudgetField,\n\tVercelGatewayRouting,\n} from \"./providers/compat-schema.ts\";\nexport type {\n\tCacheRetention,\n\tModelCost,\n\tModelCostRates,\n\tModelCostTier,\n\tModelImageInputLimits,\n\tModelImageResizeOptions,\n\tModelInputLimits,\n\tModelInputModality,\n\tModelPromptCache,\n\tModelThinkingLevel,\n\tThinkingLevel,\n\tThinkingLevelMap,\n} from \"./providers/model-schema.ts\";\nexport type { AssistantMessageEventStream } from \"./utils/event-stream.ts\";\n\nexport type KnownApi =\n\t| \"openai-completions\"\n\t| \"mistral-conversations\"\n\t| \"openai-responses\"\n\t| \"azure-openai-responses\"\n\t| \"openai-codex-responses\"\n\t| \"anthropic-messages\"\n\t| \"bedrock-converse-stream\"\n\t| \"google-generative-ai\"\n\t| \"google-vertex\"\n\t| \"pi-messages\";\n\nexport type Api = KnownApi | (string & {});\n\nexport type KnownImageApi = \"openrouter-images\" | \"openai-images\";\n\nexport type ImageApi = KnownImageApi | (string & {});\n\nexport type KnownClassifierApi =\n\t| \"typesafe-system-one\"\n\t| \"cloudflare-workers-ai-system-one\"\n\t| \"llama-cpp-classify\"\n\t| \"openai-decisions\";\n\nexport type ClassifierApi = KnownClassifierApi | (string & {});\n\nexport type CompactorApi = \"morph-compact\";\n\nexport type KnownProvider =\n\t| \"amazon-bedrock\"\n\t| \"ant-ling\"\n\t| \"anthropic\"\n\t| \"anthropic-api\"\n\t| \"google\"\n\t| \"google-vertex\"\n\t| \"openai\"\n\t| \"openai-api\"\n\t| \"azure\"\n\t| \"azure-openai-responses\"\n\t| \"openai-codex\"\n\t| \"openai-decisions\"\n\t| \"openai-images\"\n\t| \"radius\"\n\t| \"typesafe\"\n\t| \"morph\"\n\t| \"nvidia\"\n\t| \"deepseek\"\n\t| \"github-copilot\"\n\t| \"xai\"\n\t| \"groq\"\n\t| \"cerebras\"\n\t| \"openrouter\"\n\t| \"vercel-ai-gateway\"\n\t| \"zai\"\n\t| \"zai-coding-cn\"\n\t| \"mistral\"\n\t| \"minimax\"\n\t| \"minimax-cn\"\n\t| \"moonshotai\"\n\t| \"moonshotai-cn\"\n\t| \"huggingface\"\n\t| \"fireworks\"\n\t| \"together\"\n\t| \"baseten\"\n\t| \"opencode\"\n\t| \"opencode-go\"\n\t| \"kimi-coding\"\n\t| \"meta\"\n\t| \"cloudflare-workers-ai\"\n\t| \"cloudflare-ai-gateway\"\n\t| \"qwen-token-plan\"\n\t| \"qwen-token-plan-cn\"\n\t| \"qwen-token-plan-individual\"\n\t| \"xiaomi\"\n\t| \"xiaomi-token-plan-cn\"\n\t| \"xiaomi-token-plan-ams\"\n\t| \"xiaomi-token-plan-sgp\";\nexport type ProviderId = KnownProvider | string;\n\nexport type ToolChoice = \"auto\" | \"none\";\nexport type SamplingParams = Record<string, unknown>;\nexport type SamplingParamsByThinkingLevel = Partial<Record<ModelThinkingLevel, SamplingParams>>;\n/** Token budgets for each thinking level (token-based providers only) */\nexport interface ThinkingBudgets {\n\tminimal?: number;\n\tlow?: number;\n\tmedium?: number;\n\thigh?: number;\n}\n\n// Base options all providers share\nexport type Transport = \"sse\" | \"websocket\" | \"websocket-cached\" | \"auto\";\n\n/** Provider-scoped environment overrides. Values take precedence over process.env. */\nexport type ProviderEnv = Record<string, string>;\nexport type ProviderHeaders = Record<string, string | null>;\nexport type FetchFunction = typeof globalThis.fetch;\n\nexport interface ProviderResponse {\n\tstatus: number;\n\theaders: Record<string, string>;\n}\n\n/** Authentication, HTTP transport, and lifecycle callbacks shared by provider requests. */\nexport interface ProviderRequestOptions<TModel = Model<Api>> {\n\tsignal?: AbortSignal;\n\t/** Explicit parent context for telemetry produced by this logical request. */\n\ttelemetryContext?: TelemetryContext;\n\tapiKey?: string;\n\t/**\n\t * Optional fetch implementation for provider HTTP requests.\n\t * Defaults to `globalThis.fetch`. Provider adapters that cannot inject a custom implementation may reject it.\n\t * This does not affect WebSocket transports.\n\t */\n\tfetch?: FetchFunction;\n\t/**\n\t * Provider-scoped environment values. These take precedence over process.env for\n\t * provider configuration such as regional settings, endpoint placeholders, and\n\t * proxy variables.\n\t */\n\tenv?: ProviderEnv;\n\t/**\n\t * Optional callback for inspecting or replacing provider payloads before sending.\n\t * Return undefined to keep the payload unchanged.\n\t */\n\tonPayload?: (payload: unknown, model: TModel) => unknown | undefined | Promise<unknown | undefined>;\n\t/**\n\t * Optional callback invoked after an HTTP response is received.\n\t */\n\tonResponse?: (response: ProviderResponse, model: TModel) => void | Promise<void>;\n\t/**\n\t * Optional custom HTTP headers to include in API requests.\n\t * Merged with provider defaults; caller values override default headers.\n\t * On AWS Bedrock these are injected via a Smithy `build`-step middleware so\n\t * they are covered by SigV4 signing; reserved headers (`x-amz-*`,\n\t * `authorization`, `host`) are silently ignored to preserve SigV4 / bearer auth.\n\t * A null value suppresses a provider/API default header with the same name.\n\t */\n\theaders?: ProviderHeaders;\n\t/**\n\t * HTTP request timeout in milliseconds for providers/SDKs that support it.\n\t * For example, OpenAI and Anthropic SDK clients default to 10 minutes.\n\t */\n\ttimeoutMs?: number;\n\t/**\n\t * Maximum retry attempts for providers/SDKs that support client-side retries.\n\t * For example, OpenAI and Anthropic SDK clients default to 2.\n\t */\n\tmaxRetries?: number;\n\t/**\n\t * Maximum delay in milliseconds to wait for a retry when the server requests a long wait.\n\t * If the server's requested delay exceeds this value, the request fails immediately\n\t * with an error containing the requested delay, allowing higher-level retry logic\n\t * to handle it with user visibility.\n\t * Default: 60000 (60 seconds). Set to 0 to disable the cap.\n\t */\n\tmaxRetryDelayMs?: number;\n}\n\nexport interface StreamOptions extends ProviderRequestOptions<Model<Api>> {\n\t/**\n\t * Optional callback invoked after an HTTP response is received and before\n\t * its body stream is consumed.\n\t */\n\tonResponse?: (response: ProviderResponse, model: Model<Api>) => void | Promise<void>;\n\t/**\n\t * Optional observer for each parsed provider stream event before Pi normalization.\n\t * Event data is adapter-owned and must be treated as read-only.\n\t * Adapter support is explicit; unsupported adapters do not invoke it.\n\t */\n\tonProviderStreamEvent?: (data: unknown, model: Model<Api>) => void | Promise<void>;\n\ttemperature?: number;\n\t/**\n\t * Arbitrary sampling parameters merged into the request body as-is, after the named request\n\t * fields, so keys here override them. Lets custom OpenAI-compatible servers (llama.cpp, vLLM,\n\t * SGLang, ...) receive parameters pi does not model, e.g. `top_p`, `top_k`, `min_p`,\n\t * `repetition_penalty`. Merged over `Model.samplingParams` per key. Only applied by\n\t * OpenAI-compatible adapters (completions, responses, Azure responses); other APIs ignore it.\n\t */\n\tsamplingParams?: SamplingParams;\n\tmaxTokens?: number;\n\t/**\n\t * Preferred transport for providers that support multiple transports.\n\t * Providers that do not support this option ignore it.\n\t */\n\ttransport?: Transport;\n\t/**\n\t * Prompt cache retention preference. Providers map this to their supported values.\n\t * Default: \"short\".\n\t */\n\tcacheRetention?: CacheRetention;\n\t/**\n\t * Optional session identifier for providers that support session-based caching.\n\t * Providers can use this to enable prompt caching, request routing, or other\n\t * session-aware features. Ignored by providers that don't support it.\n\t */\n\tsessionId?: string;\n\t/**\n\t * WebSocket connect timeout in milliseconds for providers that support\n\t * WebSocket transports. This covers the connection/open handshake only;\n\t * HTTP/SDK idleness uses `timeoutMs`, while gaps between decoded provider\n\t * stream events use `streamDeadlineMs`.\n\t */\n\twebsocketConnectTimeoutMs?: number;\n\t/**\n\t * Maximum idle gap in milliseconds between two provider stream events.\n\t *\n\t * Enforced below the HTTP layer, so a response body that stalls without ever\n\t * rejecting the adapter's async iterator (for example a decompression failure\n\t * that destroys the stream silently, #2553) still settles the attempt as a\n\t * retryable transport error instead of hanging forever. The timer restarts on\n\t * every event, so it never caps total stream duration.\n\t *\n\t * Defaults to 300000 ms. `0` (or any non-positive value) disables it.\n\t */\n\tstreamDeadlineMs?: number;\n\t/**\n\t * Optional metadata to include in API requests.\n\t * Providers extract the fields they understand and ignore the rest.\n\t * For example, Anthropic uses `user_id` for abuse tracking and rate limiting.\n\t */\n\tmetadata?: Record<string, unknown>;\n}\n\nexport type ProviderStreamOptions = StreamOptions & Record<string, unknown>;\n\nexport interface DeferredFetchOptions extends ProviderRequestOptions<Model<Api>> {\n\t/**\n\t * Maximum provider long-poll duration in milliseconds.\n\t * Defaults to 0, which performs one status check.\n\t */\n\twait?: number;\n}\n\n/** Request options for best-effort deferred-response cancellation. */\nexport type DeferredCancelOptions = ProviderRequestOptions<Model<Api>>;\n\n/**\n * Maps known APIs to their full provider-specific stream option types.\n * Type-only imports from API implementation modules are erased at emit, so\n * this is tree-shake safe.\n */\nexport interface ApiOptionsMap {\n\t\"anthropic-messages\": AnthropicOptions;\n\t\"openai-completions\": OpenAICompletionsOptions;\n\t\"openai-responses\": OpenAIResponsesOptions;\n\t\"openai-codex-responses\": OpenAICodexResponsesOptions;\n\t\"azure-openai-responses\": AzureOpenAIResponsesOptions;\n\t\"google-generative-ai\": GoogleOptions;\n\t\"google-vertex\": GoogleVertexOptions;\n\t\"mistral-conversations\": MistralOptions;\n\t\"bedrock-converse-stream\": BedrockOptions;\n\t\"pi-messages\": PiMessagesOptions;\n}\n\n/**\n * Full stream options for an API. Known APIs resolve to their concrete option\n * type; custom API strings fall back to the generic shape.\n */\nexport type ApiStreamOptions<TApi extends Api> = TApi extends keyof ApiOptionsMap\n\t? ApiOptionsMap[TApi]\n\t: StreamOptions & Record<string, unknown>;\n\n/**\n * The uniform stream contract of an API implementation module: every module\n * under `src/api/` exports `stream` and `streamSimple`; capable modules may also\n * export deferred-response methods. Lazy wrappers (`lazyApi()`) and provider\n * factories pass these around as values. This is the untyped dispatch shape;\n * per-API option typing lives on the implementation modules themselves and on\n * `Provider.stream()` via `ApiStreamOptions`.\n */\nexport interface ProviderStreams {\n\tstream(model: Model<Api>, context: TranscriptContext, options?: StreamOptions): AssistantMessageEventStream;\n\tstreamSimple(\n\t\tmodel: Model<Api>,\n\t\tcontext: TranscriptContext,\n\t\toptions?: SimpleStreamOptions,\n\t): AssistantMessageEventStream;\n\tfetchDeferred?(\n\t\tmodel: Model<Api>,\n\t\thandle: DeferredHandle,\n\t\toptions?: DeferredFetchOptions,\n\t): AssistantMessageEventStream;\n\tcancelDeferred?(model: Model<Api>, handle: DeferredHandle, options?: DeferredCancelOptions): Promise<void>;\n}\n\n/**\n * The uniform contract of an image-generation API implementation module:\n * every image API module under `src/api/` exports exactly `generateImages`,\n * so the module itself satisfies this interface. Lazy wrappers and\n * `createProvider({ images })` pass these around as values.\n */\nexport interface ProviderImages {\n\tgenerateImages(\n\t\tmodel: ImageModel<ImageApi>,\n\t\tcontext: ImagesContext,\n\t\toptions?: ImagesOptions,\n\t): Promise<AssistantImages>;\n}\n\n/** The uniform contract implemented by classifier API modules. */\nexport interface ProviderClassifier {\n\tclassify(\n\t\tmodel: ClassifierModel<ClassifierApi>,\n\t\tcontext: ClassifierContext,\n\t\toptions?: ClassifierOptions,\n\t): Promise<ClassifierResult>;\n}\n\nexport interface ClassifierOptions extends ProviderRequestOptions<ClassifierModel<ClassifierApi>> {\n\t/**\n\t * Divides the answer logits by this value before they are normalized into probabilities.\n\t * Values above 1 soften the distribution; values below 1 sharpen it. Must be positive.\n\t * APIs that cannot apply it ignore it.\n\t */\n\ttemperature?: number;\n}\n\nexport interface ImagesOptions extends ProviderRequestOptions<ImageModel<ImageApi>> {\n\t/**\n\t * Optional metadata to include in API requests.\n\t * Providers extract the fields they understand and ignore the rest.\n\t */\n\tmetadata?: Record<string, unknown>;\n}\n\nexport type ProviderImagesOptions = ImagesOptions & Record<string, unknown>;\n\n/**\n * Maps known image APIs to their request option types, like `ApiOptionsMap`\n * does for chat APIs.\n */\nexport interface ImageApiOptionsMap {\n\t\"openrouter-images\": ImagesOptions;\n\t\"openai-images\": OpenAIImagesOptions;\n}\n\n/**\n * Request options for an image API. Known APIs resolve to their concrete option\n * type; custom API strings fall back to the generic shape.\n */\nexport type ImageApiOptions<TApi extends ImageApi> = TApi extends keyof ImageApiOptionsMap\n\t? ImageApiOptionsMap[TApi]\n\t: ProviderImagesOptions;\n\n// Unified options with reasoning passed to streamSimple() and completeSimple()\nexport interface SimpleStreamOptions extends StreamOptions {\n\t/** Provider-neutral tool selection for simple requests. When omitted, adapters use provider-specific behavior. */\n\ttoolChoice?: ToolChoice;\n\treasoning?: ThinkingLevel;\n\t/** Ask a capable provider to return a durable handle and continue the request asynchronously. */\n\tdeferred?: boolean | { window?: \"15m\" | \"1h\" | \"24h\" };\n\t/** Custom token budgets for thinking levels (token-based providers only) */\n\tthinkingBudgets?: ThinkingBudgets;\n}\n\n// Generic StreamFunction with typed options.\n//\n// Contract:\n// - Receives a normalized transcript: the system prompt and tools live in the\n// leading system message, never on the context itself.\n// - Must return an AssistantMessageEventStream.\n// - Direct streamSimple() calls may throw synchronously when request auth is\n// missing. Once a stream is returned, request/model/runtime failures should\n// be encoded in the stream, not thrown.\n// - Error termination must produce an AssistantMessage with stopReason\n// \"error\" or \"aborted\" and errorMessage, emitted via the stream protocol.\nexport type StreamFunction<TApi extends Api = Api, TOptions extends StreamOptions = StreamOptions> = (\n\tmodel: Model<TApi>,\n\tcontext: TranscriptContext,\n\toptions?: TOptions,\n) => AssistantMessageEventStream;\n\nexport type ImagesFunction<TOptions extends ImagesOptions = ImagesOptions> = (\n\tmodel: ImageModel<ImageApi>,\n\tcontext: ImagesContext,\n\toptions?: TOptions,\n) => Promise<AssistantImages>;\n\nexport type ClassifierFunction<TOptions extends ClassifierOptions = ClassifierOptions> = (\n\tmodel: ClassifierModel<ClassifierApi>,\n\tcontext: ClassifierContext,\n\toptions?: TOptions,\n) => Promise<ClassifierResult>;\n\nexport interface TextSignatureV1 {\n\tv: 1;\n\tid: string;\n\tphase?: \"commentary\" | \"final_answer\";\n}\n\nexport interface TextContent {\n\ttype: \"text\";\n\ttext: string;\n\ttextSignature?: string; // e.g., for OpenAI responses, message metadata (legacy id string or TextSignatureV1 JSON)\n}\n\nexport interface ThinkingContent {\n\ttype: \"thinking\";\n\tthinking: string;\n\tthinkingSignature?: string; // Provider-specific opaque or serialized reasoning replay data\n\t/** When true, the thinking content was redacted by safety filters. The opaque\n\t * encrypted payload is stored in `thinkingSignature` so it can be passed back\n\t * to the API for multi-turn continuity. */\n\tredacted?: boolean;\n}\n\n/**\n * Boundary marker emitted by Anthropic server-side fallback, where one model's output gives way\n * to the next after a classifier refusal.\n *\n * This is public content rather than a stream-only event because it must survive the round trip:\n * \"Keep it exactly where it appeared. The API uses its position to validate the thinking blocks\n * around it, so a request that echoes thinking blocks from both sides of the boundary is rejected\n * if the block is omitted or moved.\"\n * https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback\n */\nexport interface FallbackContent {\n\ttype: \"fallback\";\n\t/** The model that declined. Echoes the model string sent when the declining hop is the request's own model. */\n\tfromModel: string;\n\t/** Resolved id of the model that continues. Always present. */\n\ttoModel: string;\n}\n\nexport interface ImageContent {\n\ttype: \"image\";\n\tdata: string; // base64 encoded image data\n\tmimeType: string; // e.g., \"image/jpeg\", \"image/png\"\n}\n\n/**\n * A document sent as model input, currently PDF only.\n *\n * Anthropic documents PDF as a platform capability — \"All active models support PDF processing\" —\n * routed through the same vision path as images, so it is not a per-model modality. A model\n * advertises it through `Model.input` containing `\"pdf\"`, which generated metadata sets only for\n * the runtimes that can actually serialize a document block. Providers that cannot are sent a\n * visible placeholder instead, exactly as they are for images they cannot accept.\n * https://platform.claude.com/docs/en/build-with-claude/pdf-support\n */\nexport interface DocumentContent {\n\ttype: \"document\";\n\t/** Base64-encoded document bytes. Amazon Bedrock takes raw bytes and decodes this itself. */\n\tdata: string;\n\t/**\n\t * The only media type either serializer implements. It is a literal rather than `string`\n\t * because both paths hardcode PDF — the Anthropic block emits `BetaBase64PDFSource`, whose\n\t * `media_type` is itself a fixed literal, and Bedrock emits `DocumentFormat.PDF` — so a wider\n\t * type would let a caller label arbitrary bytes as a PDF. Widening this union later is\n\t * non-breaking; narrowing it after release would not be. Note that adding a format is not a\n\t * media-type swap on the Anthropic side: plain text needs a structurally different source\n\t * variant (`BetaPlainTextSource`, `type: \"text\"`), and Bedrock's `doc`/`docx`/`xls`/`xlsx`/\n\t * `csv`/`html`/`md` formats have no Anthropic source variant at all.\n\t */\n\tmimeType: \"application/pdf\";\n\t/**\n\t * Optional caller-supplied label. Amazon Bedrock requires a name and warns that it \"is\n\t * vulnerable to prompt injections\", so the Bedrock path sanitizes this to the characters AWS\n\t * permits and falls back to a neutral generated name.\n\t */\n\tname?: string;\n}\n\nexport interface ToolCall {\n\ttype: \"toolCall\";\n\tid: string;\n\tname: string;\n\targuments: JsonObject;\n\tthoughtSignature?: string; // Google-specific: opaque signature for reusing thought context\n\t/** OpenAI Responses namespace for calls to dynamically loaded or namespaced tools. */\n\tnamespace?: string;\n}\n\nexport interface Usage {\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\t/** Subset of `cacheWrite` written with 1h retention. Anthropic and Bedrock report this split. */\n\tcacheWrite1h?: number;\n\t/**\n\t * Reasoning/thinking tokens, when the provider reports them. This is a subset of\n\t * `output`: `output` already includes these tokens. Set to a number (possibly 0) by\n\t * providers that expose a reasoning breakdown; left undefined by providers that don't.\n\t */\n\treasoning?: number;\n\ttotalTokens: number;\n\tcost: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t};\n}\n\nexport type StopReason = \"pending\" | \"stop\" | \"length\" | \"toolUse\" | \"error\" | \"aborted\" | \"deferred\";\n\nexport type JsonValue = null | boolean | number | string | readonly JsonValue[] | JsonObject;\nexport type JsonObject = { [key: string]: JsonValue };\n\ntype IsAny<T> = 0 extends 1 & T ? true : false;\ntype IsExactlyJsonValue<T> = [T] extends [JsonValue] ? ([JsonValue] extends [T] ? true : false) : false;\ntype IsJsonProperty<T> =\n\tIsAny<T> extends true\n\t\t? false\n\t\t: unknown extends T\n\t\t\t? false\n\t\t\t: [Exclude<T, undefined>] extends [never]\n\t\t\t\t? true\n\t\t\t\t: IsJsonCompatible<Exclude<T, undefined>>;\ntype InvalidJsonKeys<T extends object> = {\n\t[TKey in keyof T]-?: TKey extends string | number ? (IsJsonProperty<T[TKey]> extends true ? never : TKey) : TKey;\n}[keyof T];\ntype IsJsonCompatible<T> =\n\tIsAny<T> extends true\n\t\t? false\n\t\t: unknown extends T\n\t\t\t? false\n\t\t\t: IsExactlyJsonValue<T> extends true\n\t\t\t\t? true\n\t\t\t\t: T extends null | boolean | number | string\n\t\t\t\t\t? true\n\t\t\t\t\t: T extends undefined\n\t\t\t\t\t\t? false\n\t\t\t\t\t\t: T extends readonly (infer TItem)[]\n\t\t\t\t\t\t\t? IsJsonCompatible<TItem>\n\t\t\t\t\t\t\t: T extends (...args: never[]) => unknown\n\t\t\t\t\t\t\t\t? false\n\t\t\t\t\t\t\t\t: T extends object\n\t\t\t\t\t\t\t\t\t? [InvalidJsonKeys<T>] extends [never]\n\t\t\t\t\t\t\t\t\t\t? true\n\t\t\t\t\t\t\t\t\t\t: false\n\t\t\t\t\t\t\t\t\t: false;\n\n/** The JSON representation of a typed in-memory value. Optional object properties remain optional. */\nexport type JsonRepresentation<T> =\n\tIsAny<T> extends true\n\t\t? JsonValue\n\t\t: unknown extends T\n\t\t\t? JsonValue\n\t\t\t: [T] extends [JsonValue]\n\t\t\t\t? T\n\t\t\t\t: T extends readonly unknown[]\n\t\t\t\t\t? { [TKey in keyof T]: JsonRepresentation<Exclude<T[TKey], undefined>> }\n\t\t\t\t\t: T extends object\n\t\t\t\t\t\t? { [TKey in keyof T]: JsonRepresentation<Exclude<T[TKey], undefined>> }\n\t\t\t\t\t\t: never;\n\nexport interface DeferredHandle {\n\tprovider: string;\n\tmodelId: string;\n\tapi: string;\n\t/** Provider token, such as a response id or batch id plus row id. */\n\tid: string;\n\texpiresAt?: number;\n\tpollAfterMs?: number;\n\t/** Provider conversion data required to reconstruct the final assistant message. */\n\tdata?: JsonValue;\n}\n\n/**\n * System instructions and tool declarations at one point in the transcript.\n *\n * The leading system message is the system prompt. Later system messages change it:\n * `content` adds instructions from that point on, `sections` replace or remove named\n * prompt sections, and `toolsAdded`/`toolsRemoved` change the tool set. Replaying\n * every system message in order yields the current prompt and tools. Providers that\n * accept system messages mid-conversation send each one in place; other providers\n * rebuild the leading system message from the replayed state.\n */\nexport interface SystemMessage {\n\trole: \"system\";\n\t/** Instruction text. On the leading message this is the base prompt; later, additional instructions. */\n\tcontent: string | TextContent[];\n\t/**\n\t * Named, ordered prompt sections rendered verbatim after `content`. The leading message\n\t * declares them; later messages replace sections by name, and `null` removes one. Keep\n\t * each section self-delimiting (a tag, a heading) so the model can relate an update to\n\t * the original. Avoid integer-like names; JSON objects reorder those.\n\t */\n\tsections?: Record<string, string | null>;\n\t/** Complete definitions of tools that become available at this point. */\n\ttoolsAdded?: Tool[];\n\t/** Tools that stop being available at this point. */\n\ttoolsRemoved?: ToolReference[];\n\ttimestamp: number; // Unix timestamp in milliseconds\n}\n\nexport interface UserMessage {\n\trole: \"user\";\n\tcontent: string | (TextContent | ImageContent | DocumentContent)[];\n\ttimestamp: number; // Unix timestamp in milliseconds\n}\n\nexport interface AssistantMessage {\n\trole: \"assistant\";\n\tcontent: (TextContent | ThinkingContent | ToolCall | FallbackContent)[];\n\tapi: Api;\n\tprovider: ProviderId;\n\tmodel: string;\n\tresponseModel?: string; // Concrete model reported by the provider when different from the requested `model`\n\tresponseId?: string; // Provider-specific response/message identifier when the upstream API exposes one\n\t/** Exact provider-native effort level used for this response. Absent for legacy or unmanaged responses. */\n\tproviderThinkingLevel?: string;\n\tdiagnostics?: AssistantMessageDiagnostic[]; // Redacted provider/runtime diagnostics for failures and recoveries.\n\tusage: Usage;\n\tstopReason: StopReason;\n\tdeferred?: DeferredHandle;\n\terrorMessage?: string;\n\trawStopReason?: string;\n\t/**\n\t * Provider indication of whether the model explicitly ended its turn.\n\t * Preserved for debugging and does not currently affect agent control flow.\n\t */\n\tendTurn?: boolean;\n\ttimestamp: number; // Unix timestamp in milliseconds\n\tdurationMs?: number;\n}\n\n/** A tool call made by another tool, such as a codemode script. */\nexport interface NestedToolCallRecord {\n\tid: string;\n\tname: string;\n\t/** Omitted when over the size limits. */\n\targuments?: JsonObject;\n\t/** UTF-8 JSON size when arguments are omitted. */\n\targumentsBytes?: number;\n\tstatus: \"ok\" | \"error\" | \"unfinished\";\n\tdurationMs?: number;\n\terror?: string;\n}\n\n/** Bounded nested-call record. Results are not recorded. */\nexport interface NestedToolCalls {\n\tcalls: NestedToolCallRecord[];\n\tcomplete: boolean;\n}\n\nexport type ToolResultMessage<TDetails = JsonValue> =\n\tIsJsonCompatible<TDetails> extends true\n\t\t? {\n\t\t\t\trole: \"toolResult\";\n\t\t\t\ttoolCallId: string;\n\t\t\t\ttoolName: string;\n\t\t\t\tcontent: (TextContent | ImageContent)[]; // Supports text and images\n\t\t\t\tdetails?: JsonRepresentation<TDetails>;\n\t\t\t\t/** Usage from the tool execution itself, if available. Not part of main LLM context accounting. */\n\t\t\t\tusage?: Usage;\n\t\t\t\t/** Nested calls retained in the session, not sent to the model. */\n\t\t\t\tnestedCalls?: NestedToolCalls;\n\t\t\t\tisError: boolean;\n\t\t\t\ttimestamp: number; // Unix timestamp in milliseconds\n\t\t\t\tdurationMs?: number;\n\t\t\t}\n\t\t: never;\n\nexport type Message = SystemMessage | UserMessage | AssistantMessage | ToolResultMessage;\n\nexport type ImagesInputContent = TextContent | ImageContent;\nexport type ImagesOutputContent = TextContent | ImageContent;\n\nexport interface ImagesContext {\n\tinput: ImagesInputContent[];\n}\n\nexport type ImagesStopReason = \"stop\" | \"error\" | \"aborted\";\n\nexport interface AssistantImages {\n\tapi: ImageApi;\n\tprovider: ProviderId;\n\tmodel: string;\n\toutput: ImagesOutputContent[];\n\tresponseId?: string;\n\tusage?: Usage;\n\tstopReason: ImagesStopReason;\n\terrorMessage?: string;\n\ttimestamp: number; // Unix timestamp in milliseconds\n}\n\nexport interface ClassifierChoiceQuestion {\n\ttype: \"choice\";\n\tinstructions: string;\n\tcriteria: Record<string, string>;\n}\n\nexport interface ClassifierScoreQuestion {\n\ttype: \"score\";\n\tinstructions: string;\n\tcriteria: string[];\n}\n\nexport interface ClassifierBoolQuestion {\n\ttype: \"bool\";\n\tinstructions: string;\n\tcriteria: { true: string; false: string };\n}\n\nexport type ClassifierQuestion = ClassifierChoiceQuestion | ClassifierScoreQuestion | ClassifierBoolQuestion;\n\nexport interface ClassifierContext {\n\tstate: JsonObject;\n\t/**\n\t * Images judged together with `state`. Only models whose `input` includes `\"image\"` accept them;\n\t * other models return an error result.\n\t */\n\timages?: ImageContent[];\n\tquestions: Record<string, ClassifierQuestion>;\n}\n\nexport interface ClassifierChoiceAnswer {\n\ttype: \"choice\";\n\tchoice: string;\n\tprobabilities: Record<string, number>;\n\tconfidence: number;\n}\n\nexport interface ClassifierScoreAnswer {\n\ttype: \"score\";\n\tscore: number;\n\tconfidence: number;\n}\n\nexport interface ClassifierBoolAnswer {\n\ttype: \"bool\";\n\tprobability: number;\n}\n\nexport type ClassifierAnswer = ClassifierChoiceAnswer | ClassifierScoreAnswer | ClassifierBoolAnswer;\nexport type ClassifierStopReason = \"stop\" | \"error\" | \"aborted\";\n\nexport interface ClassifierResult {\n\tapi: ClassifierApi;\n\tprovider: ProviderId;\n\tmodel: string;\n\tanswers: Record<string, ClassifierAnswer>;\n\t/** Token usage and cost at the model's catalog price, when reported. */\n\tusage?: Usage;\n\tstopReason: ClassifierStopReason;\n\terrorMessage?: string;\n\ttimestamp: number; // Unix timestamp in milliseconds\n}\n\nimport type { TSchema } from \"typebox\";\n\n/** OpenAI grammar variants for constrained sampling. */\nexport type GrammarFormat = \"openai_lark\" | \"openai_regex\";\n\nexport type GrammarVariants = Partial<Record<GrammarFormat, string>>;\n\n/**\n * Optional provider-side constrained sampling configs for a tool.\n *\n * The `json_schema` value roughly maps to the concept of `strict` in APIs which is\n * implemented as json-schema constrained sampling by APIs. Grammar variants let\n * callers provide provider-specific encodings of the same intended language.\n */\nexport type ConstrainedSamplingConfig =\n\t| {\n\t\t\ttype: \"json_schema\";\n\t\t\tstrict: \"prefer\" | \"require\";\n\t }\n\t| {\n\t\t\ttype: \"grammar\";\n\t\t\tvariants: GrammarVariants;\n\t };\n\nexport interface Tool<TParameters extends TSchema = TSchema> {\n\tname: string;\n\tdescription: string;\n\tparameters: TParameters;\n\tconstrainedSampling?: false | ConstrainedSamplingConfig;\n}\n\nexport interface ToolReference {\n\tname: string;\n}\n\n/**\n * Request input accepted by the public stream entry points (`Models.stream()`,\n * `streamSimple()`, ...). `systemPrompt` and `tools` are shorthand for a leading\n * system message; `normalizeContext()` folds them into one before the request\n * reaches a provider.\n */\nexport interface Context {\n\tsystemPrompt?: string;\n\tmessages: Message[];\n\ttools?: Tool[];\n}\n\ndeclare const transcriptContextBrand: unique symbol;\n\n/**\n * Normalized request context passed to providers and API implementations. The\n * prompt and tool declarations are carried by the transcript's system messages.\n * Only `normalizeContext()` produces this type, so a raw `Context` cannot reach\n * provider code by accident.\n */\nexport type TranscriptContext = {\n\tmessages: Message[];\n\treadonly [transcriptContextBrand]: true;\n};\n\n/**\n * Event protocol for AssistantMessageEventStream.\n *\n * Successful streams emit `start` before partial updates and terminate with\n * `done`. A stream may terminate directly with `error` when request setup fails\n * before generation starts; after `start`, failures also terminate with `error`.\n * Direct `streamSimple()` calls throw synchronously when request auth is missing.\n * Updates and `done` must never appear before `start`.\n *\n * Tool-call arguments at `toolcall_start` are provider-specific;\n * `toolcall_delta` carries subsequent JSON updates.\n */\nexport type AssistantMessageEvent =\n\t| { type: \"start\"; partial: AssistantMessage }\n\t| { type: \"text_start\"; contentIndex: number; partial: AssistantMessage }\n\t| { type: \"text_delta\"; contentIndex: number; delta: string; partial: AssistantMessage }\n\t| { type: \"text_end\"; contentIndex: number; content: string; partial: AssistantMessage }\n\t| { type: \"thinking_start\"; contentIndex: number; partial: AssistantMessage }\n\t| { type: \"thinking_delta\"; contentIndex: number; delta: string; partial: AssistantMessage }\n\t| { type: \"thinking_end\"; contentIndex: number; content: string; partial: AssistantMessage }\n\t| { type: \"toolcall_start\"; contentIndex: number; partial: AssistantMessage }\n\t| { type: \"toolcall_delta\"; contentIndex: number; delta: string; partial: AssistantMessage }\n\t| { type: \"toolcall_end\"; contentIndex: number; toolCall: ToolCall; partial: AssistantMessage }\n\t| {\n\t\t\ttype: \"done\";\n\t\t\treason: Extract<StopReason, \"stop\" | \"length\" | \"toolUse\" | \"deferred\">;\n\t\t\tmessage: AssistantMessage;\n\t }\n\t| { type: \"error\"; reason: Extract<StopReason, \"aborted\" | \"error\">; error: AssistantMessage };\n\n/** Service tiers a model can advertise beyond standard processing. */\nexport type ModelServiceTierId = \"priority\" | \"ultrafast\";\n\n/**\n * A service tier a model advertises, following Codex's `ModelInfo.service_tiers`, with the provider's\n * published per-million-token rates for that tier.\n *\n * Adapters send a tier only when the model advertises it. `flex` is an API request option and needs\n * no advertisement.\n */\nexport interface ModelServiceTier {\n\tid: ModelServiceTierId;\n\tcost: ModelCost;\n}\n\n/**\n * Explicit routing metadata that marks a model as the fast-inference variant of another model.\n *\n * Presence of this field — never a `-fast` name suffix — is what gives a model fast semantics.\n * A provider, `models.json` custom model, or extension may own an exact `<base>-fast` ID without\n * this metadata, in which case it is an ordinary model and routes exactly as it is declared.\n */\nexport interface ModelFastRoute {\n\t/** Canonical ID of the normal-speed model this variant pairs with, in the same provider. */\n\tbaseModelId: string;\n\t/** Model ID to send upstream. OpenAI-style routing keeps the base ID; a provider with real fast siblings sends its own ID. */\n\tupstreamModelId: string;\n\t/** Service tier to send with the request. Set only for providers that route fast traffic through an OpenAI-style tier. */\n\tserviceTier?: ModelServiceTierId;\n\t/** Anthropic inference speed to send with the request. Set only for Claude models that support fast mode. */\n\tspeed?: \"fast\";\n}\n\n/** Fields shared by every catalog entry, regardless of what you can do with it. */\nexport interface BaseModel<TApi extends string> {\n\tid: string;\n\tname: string;\n\tapi: TApi;\n\tprovider: ProviderId;\n\tbaseUrl: string;\n\tinput: ModelInputModality[];\n\t/** Provider input limits and cache-safe preprocessing metadata. */\n\tinputLimits?: ModelInputLimits;\n\tcost: ModelCost;\n\theaders?: Record<string, string>;\n}\n\n/** Chat model: usable with `stream()` and friends. */\nexport interface Model<TApi extends Api> extends BaseModel<TApi> {\n\t/**\n\t * Optional: chat is the default model type, so models without `type` are chat\n\t * models. Narrow mixed model lists with `isModelType()` instead of comparing\n\t * `type` directly.\n\t */\n\ttype?: \"chat\";\n\treasoning: boolean;\n\t/**\n\t * Maps pi thinking levels to provider/model-specific values.\n\t * Missing keys use provider defaults. null marks a level as unsupported.\n\t */\n\tthinkingLevelMap?: ThinkingLevelMap;\n\t/** Prompt cache lifetimes per retention tier. Unset when the provider's cache behavior is unknown. */\n\tpromptCache?: ModelPromptCache;\n\tcontextWindow: number;\n\tmaxTokens: number;\n\t/** Default sampling parameters for this model. See {@link StreamOptions.samplingParams}; per-request keys override these. */\n\tsamplingParams?: SamplingParams;\n\t/** Sampling parameter overrides selected by the effective pi thinking level. */\n\tsamplingParamsByThinkingLevel?: SamplingParamsByThinkingLevel;\n\t/**\n\t * Marks this model as the fast-inference variant of {@link ModelFastRoute.baseModelId} and carries the\n\t * upstream routing it needs. Absent on every normal model.\n\t */\n\tfastRoute?: ModelFastRoute;\n\t/**\n\t * Service tiers this model advertises. When set, the Codex adapter sends only advertised tiers.\n\t * When unset, the model carries no tier metadata and only Ultrafast is withheld.\n\t */\n\tserviceTiers?: ModelServiceTier[];\n\t/** Compatibility overrides for OpenAI-compatible APIs. If not set, auto-detected from baseUrl. */\n\tcompat?: TApi extends \"openai-completions\"\n\t\t? OpenAICompletionsCompat\n\t\t: TApi extends \"openai-responses\" | \"azure-openai-responses\" | \"openai-codex-responses\"\n\t\t\t? OpenAIResponsesCompat\n\t\t\t: TApi extends \"anthropic-messages\"\n\t\t\t\t? AnthropicMessagesCompat\n\t\t\t\t: TApi extends \"bedrock-converse-stream\"\n\t\t\t\t\t? BedrockCompat\n\t\t\t\t\t: TApi extends \"mistral-conversations\"\n\t\t\t\t\t\t? MistralConversationsCompat\n\t\t\t\t\t\t: never;\n}\n\n/** Image-generation model: usable with `generateImages()` only. */\nexport interface ImageModel<TApi extends ImageApi> extends BaseModel<TApi> {\n\ttype: \"image\";\n\t/** Output modalities. Always includes `\"image\"`; `\"text\"` means the model can also return text blocks. */\n\toutput: (\"text\" | \"image\")[];\n}\n\n/** Structured classifier model: usable with `classify()` only. */\nexport interface ClassifierModel<TApi extends ClassifierApi> extends BaseModel<TApi> {\n\ttype: \"classifier\";\n\tcontextWindow: number;\n}\n\n/** Dedicated compaction model, excluded from chat model selection. */\nexport interface CompactorModel<TApi extends CompactorApi = CompactorApi> extends BaseModel<TApi> {\n\ttype: \"compactor\";\n}\n\n/** Model shape for each model type. */\nexport interface ModelTypeMap {\n\tchat: Model<Api>;\n\timage: ImageModel<ImageApi>;\n\tclassifier: ClassifierModel<ClassifierApi>;\n\tcompactor: CompactorModel;\n}\n\n/** What a catalog entry is for. Decides which `Models` operation accepts it. */\nexport type ModelType = keyof ModelTypeMap;\n\n/** Anything a provider can list. Narrow with `isModelType()`. */\nexport type AnyModel = ModelTypeMap[ModelType];\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { TelemetryContext } from \"@earendil-works/pi-telemetry\";\nimport type { AnthropicOptions } from \"./api/anthropic-messages.ts\";\nimport type { AzureOpenAIResponsesOptions } from \"./api/azure-openai-responses.ts\";\nimport type { BedrockOptions } from \"./api/bedrock-converse-stream.ts\";\nimport type { GoogleOptions } from \"./api/google-generative-ai.ts\";\nimport type { GoogleVertexOptions } from \"./api/google-vertex.ts\";\nimport type { MistralOptions } from \"./api/mistral-conversations.ts\";\nimport type { OpenAICodexResponsesOptions } from \"./api/openai-codex-responses.ts\";\nimport type { OpenAICompletionsOptions } from \"./api/openai-completions.ts\";\nimport type { OpenAIImagesOptions } from \"./api/openai-images.ts\";\nimport type { OpenAIResponsesOptions } from \"./api/openai-responses.ts\";\nimport type { PiMessagesOptions } from \"./api/pi-messages.ts\";\nimport type {\n\tAnthropicMessagesCompat,\n\tBedrockCompat,\n\tMistralConversationsCompat,\n\tOpenAICompletionsCompat,\n\tOpenAIResponsesCompat,\n} from \"./providers/compat-schema.ts\";\nimport type {\n\tCacheRetention,\n\tModelCost,\n\tModelInputLimits,\n\tModelInputModality,\n\tModelPromptCache,\n\tModelThinkingLevel,\n\tThinkingLevel,\n\tThinkingLevelMap,\n} from \"./providers/model-schema.ts\";\nimport type { AssistantMessageDiagnostic } from \"./utils/diagnostics.ts\";\nimport type { AssistantMessageEventStream } from \"./utils/event-stream.ts\";\n\nexport type {\n\tAnthropicAllowedFallbackModel,\n\tAnthropicMessagesCompat,\n\tBedrockCompat,\n\tChatTemplateKwargValue,\n\tMistralConversationsCompat,\n\tOpenAICompletionsCompat,\n\tOpenAIResponsesCompat,\n\tOpenRouterRouting,\n\tSessionAffinityFormat,\n\tThinkingTokenBudgetField,\n\tVercelGatewayRouting,\n} from \"./providers/compat-schema.ts\";\nexport type {\n\tCacheRetention,\n\tModelCost,\n\tModelCostRates,\n\tModelCostTier,\n\tModelImageInputLimits,\n\tModelImageResizeOptions,\n\tModelInputLimits,\n\tModelInputModality,\n\tModelPromptCache,\n\tModelThinkingLevel,\n\tThinkingLevel,\n\tThinkingLevelMap,\n} from \"./providers/model-schema.ts\";\nexport type { AssistantMessageEventStream } from \"./utils/event-stream.ts\";\n\nexport type KnownApi =\n\t| \"openai-completions\"\n\t| \"mistral-conversations\"\n\t| \"openai-responses\"\n\t| \"azure-openai-responses\"\n\t| \"openai-codex-responses\"\n\t| \"anthropic-messages\"\n\t| \"bedrock-converse-stream\"\n\t| \"google-generative-ai\"\n\t| \"google-vertex\"\n\t| \"pi-messages\";\n\nexport type Api = KnownApi | (string & {});\n\nexport type KnownImageApi = \"openrouter-images\" | \"openai-images\";\n\nexport type ImageApi = KnownImageApi | (string & {});\n\nexport type KnownClassifierApi =\n\t| \"typesafe-system-one\"\n\t| \"cloudflare-workers-ai-system-one\"\n\t| \"llama-cpp-classify\"\n\t| \"openai-decisions\";\n\nexport type ClassifierApi = KnownClassifierApi | (string & {});\n\nexport type CompactorApi = \"morph-compact\";\n\nexport type KnownProvider =\n\t| \"amazon-bedrock\"\n\t| \"ant-ling\"\n\t| \"anthropic\"\n\t| \"anthropic-api\"\n\t| \"google\"\n\t| \"google-vertex\"\n\t| \"openai\"\n\t| \"openai-api\"\n\t| \"azure\"\n\t| \"azure-openai-responses\"\n\t| \"openai-codex\"\n\t| \"openai-decisions\"\n\t| \"openai-images\"\n\t| \"radius\"\n\t| \"typesafe\"\n\t| \"morph\"\n\t| \"nvidia\"\n\t| \"deepseek\"\n\t| \"github-copilot\"\n\t| \"xai\"\n\t| \"groq\"\n\t| \"cerebras\"\n\t| \"openrouter\"\n\t| \"vercel-ai-gateway\"\n\t| \"zai\"\n\t| \"zai-coding-cn\"\n\t| \"mistral\"\n\t| \"minimax\"\n\t| \"minimax-cn\"\n\t| \"moonshotai\"\n\t| \"moonshotai-cn\"\n\t| \"huggingface\"\n\t| \"fireworks\"\n\t| \"together\"\n\t| \"baseten\"\n\t| \"opencode\"\n\t| \"opencode-go\"\n\t| \"kimi-coding\"\n\t| \"meta\"\n\t| \"cloudflare-workers-ai\"\n\t| \"cloudflare-ai-gateway\"\n\t| \"qwen-token-plan\"\n\t| \"qwen-token-plan-cn\"\n\t| \"qwen-token-plan-individual\"\n\t| \"xiaomi\"\n\t| \"xiaomi-token-plan-cn\"\n\t| \"xiaomi-token-plan-ams\"\n\t| \"xiaomi-token-plan-sgp\";\nexport type ProviderId = KnownProvider | string;\n\nexport type ToolChoice = \"auto\" | \"none\";\nexport type SamplingParams = Record<string, unknown>;\nexport type SamplingParamsByThinkingLevel = Partial<Record<ModelThinkingLevel, SamplingParams>>;\n/** Token budgets for each thinking level (token-based providers only) */\nexport interface ThinkingBudgets {\n\tminimal?: number;\n\tlow?: number;\n\tmedium?: number;\n\thigh?: number;\n}\n\n// Base options all providers share\nexport type Transport = \"sse\" | \"websocket\" | \"websocket-cached\" | \"auto\";\n\n/** Provider-scoped environment overrides. Values take precedence over process.env. */\nexport type ProviderEnv = Record<string, string>;\nexport type ProviderHeaders = Record<string, string | null>;\nexport type FetchFunction = typeof globalThis.fetch;\n\nexport interface ProviderResponse {\n\tstatus: number;\n\theaders: Record<string, string>;\n}\n\n/** Authentication, HTTP transport, and lifecycle callbacks shared by provider requests. */\nexport interface ProviderRequestOptions<TModel = Model<Api>> {\n\tsignal?: AbortSignal;\n\t/** Explicit parent context for telemetry produced by this logical request. */\n\ttelemetryContext?: TelemetryContext;\n\tapiKey?: string;\n\t/**\n\t * Optional fetch implementation for provider HTTP requests.\n\t * Defaults to `globalThis.fetch`. Provider adapters that cannot inject a custom implementation may reject it.\n\t * This does not affect WebSocket transports.\n\t */\n\tfetch?: FetchFunction;\n\t/**\n\t * Provider-scoped environment values. These take precedence over process.env for\n\t * provider configuration such as regional settings, endpoint placeholders, and\n\t * proxy variables.\n\t */\n\tenv?: ProviderEnv;\n\t/**\n\t * Optional callback for inspecting or replacing provider payloads before sending.\n\t * Return undefined to keep the payload unchanged.\n\t */\n\tonPayload?: (payload: unknown, model: TModel) => unknown | undefined | Promise<unknown | undefined>;\n\t/**\n\t * Optional callback invoked after an HTTP response is received.\n\t */\n\tonResponse?: (response: ProviderResponse, model: TModel) => void | Promise<void>;\n\t/**\n\t * Optional custom HTTP headers to include in API requests.\n\t * Merged with provider defaults; caller values override default headers.\n\t * On AWS Bedrock these are injected via a Smithy `build`-step middleware so\n\t * they are covered by SigV4 signing; reserved headers (`x-amz-*`,\n\t * `authorization`, `host`) are silently ignored to preserve SigV4 / bearer auth.\n\t * A null value suppresses a provider/API default header with the same name.\n\t */\n\theaders?: ProviderHeaders;\n\t/**\n\t * HTTP request timeout in milliseconds for providers/SDKs that support it.\n\t * For example, OpenAI and Anthropic SDK clients default to 10 minutes.\n\t */\n\ttimeoutMs?: number;\n\t/**\n\t * Maximum retry attempts for providers/SDKs that support client-side retries.\n\t * For example, OpenAI and Anthropic SDK clients default to 2.\n\t */\n\tmaxRetries?: number;\n\t/**\n\t * Maximum delay in milliseconds to wait for a retry when the server requests a long wait.\n\t * If the server's requested delay exceeds this value, the request fails immediately\n\t * with an error containing the requested delay, allowing higher-level retry logic\n\t * to handle it with user visibility.\n\t * Default: 60000 (60 seconds). Set to 0 to disable the cap.\n\t */\n\tmaxRetryDelayMs?: number;\n}\n\nexport interface StreamOptions extends ProviderRequestOptions<Model<Api>> {\n\t/**\n\t * Optional callback invoked after an HTTP response is received and before\n\t * its body stream is consumed.\n\t */\n\tonResponse?: (response: ProviderResponse, model: Model<Api>) => void | Promise<void>;\n\t/**\n\t * Optional observer for each parsed provider stream event before Pi normalization.\n\t * Event data is adapter-owned and must be treated as read-only.\n\t * Adapter support is explicit; unsupported adapters do not invoke it.\n\t */\n\tonProviderStreamEvent?: (data: unknown, model: Model<Api>) => void | Promise<void>;\n\ttemperature?: number;\n\t/**\n\t * Arbitrary sampling parameters merged into the request body as-is, after the named request\n\t * fields, so keys here override them. Lets custom OpenAI-compatible servers (llama.cpp, vLLM,\n\t * SGLang, ...) receive parameters pi does not model, e.g. `top_p`, `top_k`, `min_p`,\n\t * `repetition_penalty`. Merged over `Model.samplingParams` per key. Only applied by\n\t * OpenAI-compatible adapters (completions, responses, Azure responses); other APIs ignore it.\n\t */\n\tsamplingParams?: SamplingParams;\n\tmaxTokens?: number;\n\t/**\n\t * Preferred transport for providers that support multiple transports.\n\t * Providers that do not support this option ignore it.\n\t */\n\ttransport?: Transport;\n\t/**\n\t * Prompt cache retention preference. Providers map this to their supported values.\n\t * Default: \"short\".\n\t */\n\tcacheRetention?: CacheRetention;\n\t/**\n\t * Optional session identifier for providers that support session-based caching.\n\t * Providers can use this to enable prompt caching, request routing, or other\n\t * session-aware features. Ignored by providers that don't support it.\n\t */\n\tsessionId?: string;\n\t/**\n\t * WebSocket connect timeout in milliseconds for providers that support\n\t * WebSocket transports. This covers the connection/open handshake only;\n\t * HTTP/SDK idleness uses `timeoutMs`, while gaps between decoded provider\n\t * stream events use `streamDeadlineMs`.\n\t */\n\twebsocketConnectTimeoutMs?: number;\n\t/**\n\t * Maximum idle gap in milliseconds between two provider stream events.\n\t *\n\t * Enforced below the HTTP layer, so a response body that stalls without ever\n\t * rejecting the adapter's async iterator (for example a decompression failure\n\t * that destroys the stream silently, #2553) still settles the attempt as a\n\t * retryable transport error instead of hanging forever. The timer restarts on\n\t * every event, so it never caps total stream duration.\n\t *\n\t * Defaults to 300000 ms. `0` (or any non-positive value) disables it.\n\t */\n\tstreamDeadlineMs?: number;\n\t/**\n\t * Optional metadata to include in API requests.\n\t * Providers extract the fields they understand and ignore the rest.\n\t * For example, Anthropic uses `user_id` for abuse tracking and rate limiting.\n\t */\n\tmetadata?: Record<string, unknown>;\n}\n\nexport type ProviderStreamOptions = StreamOptions & Record<string, unknown>;\n\nexport interface DeferredFetchOptions extends ProviderRequestOptions<Model<Api>> {\n\t/**\n\t * Maximum provider long-poll duration in milliseconds.\n\t * Defaults to 0, which performs one status check.\n\t */\n\twait?: number;\n}\n\n/** Request options for best-effort deferred-response cancellation. */\nexport type DeferredCancelOptions = ProviderRequestOptions<Model<Api>>;\n\n/**\n * Maps known APIs to their full provider-specific stream option types.\n * Type-only imports from API implementation modules are erased at emit, so\n * this is tree-shake safe.\n */\nexport interface ApiOptionsMap {\n\t\"anthropic-messages\": AnthropicOptions;\n\t\"openai-completions\": OpenAICompletionsOptions;\n\t\"openai-responses\": OpenAIResponsesOptions;\n\t\"openai-codex-responses\": OpenAICodexResponsesOptions;\n\t\"azure-openai-responses\": AzureOpenAIResponsesOptions;\n\t\"google-generative-ai\": GoogleOptions;\n\t\"google-vertex\": GoogleVertexOptions;\n\t\"mistral-conversations\": MistralOptions;\n\t\"bedrock-converse-stream\": BedrockOptions;\n\t\"pi-messages\": PiMessagesOptions;\n}\n\n/**\n * Full stream options for an API. Known APIs resolve to their concrete option\n * type; custom API strings fall back to the generic shape.\n */\nexport type ApiStreamOptions<TApi extends Api> = TApi extends keyof ApiOptionsMap\n\t? ApiOptionsMap[TApi]\n\t: StreamOptions & Record<string, unknown>;\n\n/**\n * The uniform stream contract of an API implementation module: every module\n * under `src/api/` exports `stream` and `streamSimple`; capable modules may also\n * export deferred-response methods. Lazy wrappers (`lazyApi()`) and provider\n * factories pass these around as values. This is the untyped dispatch shape;\n * per-API option typing lives on the implementation modules themselves and on\n * `Provider.stream()` via `ApiStreamOptions`.\n */\nexport interface ProviderStreams {\n\tstream(model: Model<Api>, context: TranscriptContext, options?: StreamOptions): AssistantMessageEventStream;\n\tstreamSimple(\n\t\tmodel: Model<Api>,\n\t\tcontext: TranscriptContext,\n\t\toptions?: SimpleStreamOptions,\n\t): AssistantMessageEventStream;\n\tfetchDeferred?(\n\t\tmodel: Model<Api>,\n\t\thandle: DeferredHandle,\n\t\toptions?: DeferredFetchOptions,\n\t): AssistantMessageEventStream;\n\tcancelDeferred?(model: Model<Api>, handle: DeferredHandle, options?: DeferredCancelOptions): Promise<void>;\n}\n\n/**\n * The uniform contract of an image-generation API implementation module:\n * every image API module under `src/api/` exports exactly `generateImages`,\n * so the module itself satisfies this interface. Lazy wrappers and\n * `createProvider({ images })` pass these around as values.\n */\nexport interface ProviderImages {\n\tgenerateImages(\n\t\tmodel: ImageModel<ImageApi>,\n\t\tcontext: ImagesContext,\n\t\toptions?: ImagesOptions,\n\t): Promise<AssistantImages>;\n}\n\n/** The uniform contract implemented by classifier API modules. */\nexport interface ProviderClassifier {\n\tclassify(\n\t\tmodel: ClassifierModel<ClassifierApi>,\n\t\tcontext: ClassifierContext,\n\t\toptions?: ClassifierOptions,\n\t): Promise<ClassifierResult>;\n}\n\nexport interface ClassifierOptions extends ProviderRequestOptions<ClassifierModel<ClassifierApi>> {\n\t/**\n\t * Divides the answer logits by this value before they are normalized into probabilities.\n\t * Values above 1 soften the distribution; values below 1 sharpen it. Must be positive.\n\t * APIs that cannot apply it ignore it.\n\t */\n\ttemperature?: number;\n}\n\nexport interface ImagesOptions extends ProviderRequestOptions<ImageModel<ImageApi>> {\n\t/**\n\t * Optional metadata to include in API requests.\n\t * Providers extract the fields they understand and ignore the rest.\n\t */\n\tmetadata?: Record<string, unknown>;\n}\n\nexport type ProviderImagesOptions = ImagesOptions & Record<string, unknown>;\n\n/**\n * Maps known image APIs to their request option types, like `ApiOptionsMap`\n * does for chat APIs.\n */\nexport interface ImageApiOptionsMap {\n\t\"openrouter-images\": ImagesOptions;\n\t\"openai-images\": OpenAIImagesOptions;\n}\n\n/**\n * Request options for an image API. Known APIs resolve to their concrete option\n * type; custom API strings fall back to the generic shape.\n */\nexport type ImageApiOptions<TApi extends ImageApi> = TApi extends keyof ImageApiOptionsMap\n\t? ImageApiOptionsMap[TApi]\n\t: ProviderImagesOptions;\n\n// Unified options with reasoning passed to streamSimple() and completeSimple()\nexport interface SimpleStreamOptions extends StreamOptions {\n\t/** Provider-neutral tool selection for simple requests. When omitted, adapters use provider-specific behavior. */\n\ttoolChoice?: ToolChoice;\n\treasoning?: ThinkingLevel;\n\t/** Ask a capable provider to return a durable handle and continue the request asynchronously. */\n\tdeferred?: boolean | { window?: \"15m\" | \"1h\" | \"24h\" };\n\t/** Custom token budgets for thinking levels (token-based providers only) */\n\tthinkingBudgets?: ThinkingBudgets;\n}\n\n// Generic StreamFunction with typed options.\n//\n// Contract:\n// - Receives a normalized transcript: the system prompt and tools live in the\n// leading system message, never on the context itself.\n// - Must return an AssistantMessageEventStream.\n// - Direct streamSimple() calls may throw synchronously when request auth is\n// missing. Once a stream is returned, request/model/runtime failures should\n// be encoded in the stream, not thrown.\n// - Error termination must produce an AssistantMessage with stopReason\n// \"error\" or \"aborted\" and errorMessage, emitted via the stream protocol.\nexport type StreamFunction<TApi extends Api = Api, TOptions extends StreamOptions = StreamOptions> = (\n\tmodel: Model<TApi>,\n\tcontext: TranscriptContext,\n\toptions?: TOptions,\n) => AssistantMessageEventStream;\n\nexport type ImagesFunction<TOptions extends ImagesOptions = ImagesOptions> = (\n\tmodel: ImageModel<ImageApi>,\n\tcontext: ImagesContext,\n\toptions?: TOptions,\n) => Promise<AssistantImages>;\n\nexport type ClassifierFunction<TOptions extends ClassifierOptions = ClassifierOptions> = (\n\tmodel: ClassifierModel<ClassifierApi>,\n\tcontext: ClassifierContext,\n\toptions?: TOptions,\n) => Promise<ClassifierResult>;\n\nexport interface TextSignatureV1 {\n\tv: 1;\n\tid: string;\n\tphase?: \"commentary\" | \"final_answer\";\n}\n\nexport interface TextContent {\n\ttype: \"text\";\n\ttext: string;\n\ttextSignature?: string; // e.g., for OpenAI responses, message metadata (legacy id string or TextSignatureV1 JSON)\n}\n\nexport interface ThinkingContent {\n\ttype: \"thinking\";\n\tthinking: string;\n\tthinkingSignature?: string; // Provider-specific opaque or serialized reasoning replay data\n\t/** When true, the thinking content was redacted by safety filters. The opaque\n\t * encrypted payload is stored in `thinkingSignature` so it can be passed back\n\t * to the API for multi-turn continuity. */\n\tredacted?: boolean;\n}\n\n/**\n * Boundary marker emitted by Anthropic server-side fallback, where one model's output gives way\n * to the next after a classifier refusal.\n *\n * This is public content rather than a stream-only event because it must survive the round trip:\n * \"Keep it exactly where it appeared. The API uses its position to validate the thinking blocks\n * around it, so a request that echoes thinking blocks from both sides of the boundary is rejected\n * if the block is omitted or moved.\"\n * https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback\n */\nexport interface FallbackContent {\n\ttype: \"fallback\";\n\t/** The model that declined. Echoes the model string sent when the declining hop is the request's own model. */\n\tfromModel: string;\n\t/** Resolved id of the model that continues. Always present. */\n\ttoModel: string;\n}\n\nexport interface ImageContent {\n\ttype: \"image\";\n\tdata: string; // base64 encoded image data\n\tmimeType: string; // e.g., \"image/jpeg\", \"image/png\"\n}\n\n/**\n * A document sent as model input, currently PDF only.\n *\n * Anthropic documents PDF as a platform capability — \"All active models support PDF processing\" —\n * routed through the same vision path as images, so it is not a per-model modality. A model\n * advertises it through `Model.input` containing `\"pdf\"`, which generated metadata sets only for\n * the runtimes that can actually serialize a document block. Providers that cannot are sent a\n * visible placeholder instead, exactly as they are for images they cannot accept.\n * https://platform.claude.com/docs/en/build-with-claude/pdf-support\n */\nexport interface DocumentContent {\n\ttype: \"document\";\n\t/** Base64-encoded document bytes. Amazon Bedrock takes raw bytes and decodes this itself. */\n\tdata: string;\n\t/**\n\t * The only media type either serializer implements. It is a literal rather than `string`\n\t * because both paths hardcode PDF — the Anthropic block emits `BetaBase64PDFSource`, whose\n\t * `media_type` is itself a fixed literal, and Bedrock emits `DocumentFormat.PDF` — so a wider\n\t * type would let a caller label arbitrary bytes as a PDF. Widening this union later is\n\t * non-breaking; narrowing it after release would not be. Note that adding a format is not a\n\t * media-type swap on the Anthropic side: plain text needs a structurally different source\n\t * variant (`BetaPlainTextSource`, `type: \"text\"`), and Bedrock's `doc`/`docx`/`xls`/`xlsx`/\n\t * `csv`/`html`/`md` formats have no Anthropic source variant at all.\n\t */\n\tmimeType: \"application/pdf\";\n\t/**\n\t * Optional caller-supplied label. Amazon Bedrock requires a name and warns that it \"is\n\t * vulnerable to prompt injections\", so the Bedrock path sanitizes this to the characters AWS\n\t * permits and falls back to a neutral generated name.\n\t */\n\tname?: string;\n}\n\nexport interface ToolCall {\n\ttype: \"toolCall\";\n\tid: string;\n\tname: string;\n\targuments: JsonObject;\n\tthoughtSignature?: string; // Google-specific: opaque signature for reusing thought context\n\t/** OpenAI Responses namespace for calls to dynamically loaded or namespaced tools. */\n\tnamespace?: string;\n\targumentsError?: string;\n}\n\nexport interface Usage {\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\t/** Subset of `cacheWrite` written with 1h retention. Anthropic and Bedrock report this split. */\n\tcacheWrite1h?: number;\n\t/**\n\t * Reasoning/thinking tokens, when the provider reports them. This is a subset of\n\t * `output`: `output` already includes these tokens. Set to a number (possibly 0) by\n\t * providers that expose a reasoning breakdown; left undefined by providers that don't.\n\t */\n\treasoning?: number;\n\ttotalTokens: number;\n\tcost: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t};\n}\n\nexport type StopReason = \"pending\" | \"stop\" | \"length\" | \"toolUse\" | \"error\" | \"aborted\" | \"deferred\";\n\nexport type JsonValue = null | boolean | number | string | readonly JsonValue[] | JsonObject;\nexport type JsonObject = { [key: string]: JsonValue };\n\ntype IsAny<T> = 0 extends 1 & T ? true : false;\ntype IsExactlyJsonValue<T> = [T] extends [JsonValue] ? ([JsonValue] extends [T] ? true : false) : false;\ntype IsJsonProperty<T> =\n\tIsAny<T> extends true\n\t\t? false\n\t\t: unknown extends T\n\t\t\t? false\n\t\t\t: [Exclude<T, undefined>] extends [never]\n\t\t\t\t? true\n\t\t\t\t: IsJsonCompatible<Exclude<T, undefined>>;\ntype InvalidJsonKeys<T extends object> = {\n\t[TKey in keyof T]-?: TKey extends string | number ? (IsJsonProperty<T[TKey]> extends true ? never : TKey) : TKey;\n}[keyof T];\ntype IsJsonCompatible<T> =\n\tIsAny<T> extends true\n\t\t? false\n\t\t: unknown extends T\n\t\t\t? false\n\t\t\t: IsExactlyJsonValue<T> extends true\n\t\t\t\t? true\n\t\t\t\t: T extends null | boolean | number | string\n\t\t\t\t\t? true\n\t\t\t\t\t: T extends undefined\n\t\t\t\t\t\t? false\n\t\t\t\t\t\t: T extends readonly (infer TItem)[]\n\t\t\t\t\t\t\t? IsJsonCompatible<TItem>\n\t\t\t\t\t\t\t: T extends (...args: never[]) => unknown\n\t\t\t\t\t\t\t\t? false\n\t\t\t\t\t\t\t\t: T extends object\n\t\t\t\t\t\t\t\t\t? [InvalidJsonKeys<T>] extends [never]\n\t\t\t\t\t\t\t\t\t\t? true\n\t\t\t\t\t\t\t\t\t\t: false\n\t\t\t\t\t\t\t\t\t: false;\n\n/** The JSON representation of a typed in-memory value. Optional object properties remain optional. */\nexport type JsonRepresentation<T> =\n\tIsAny<T> extends true\n\t\t? JsonValue\n\t\t: unknown extends T\n\t\t\t? JsonValue\n\t\t\t: [T] extends [JsonValue]\n\t\t\t\t? T\n\t\t\t\t: T extends readonly unknown[]\n\t\t\t\t\t? { [TKey in keyof T]: JsonRepresentation<Exclude<T[TKey], undefined>> }\n\t\t\t\t\t: T extends object\n\t\t\t\t\t\t? { [TKey in keyof T]: JsonRepresentation<Exclude<T[TKey], undefined>> }\n\t\t\t\t\t\t: never;\n\nexport interface DeferredHandle {\n\tprovider: string;\n\tmodelId: string;\n\tapi: string;\n\t/** Provider token, such as a response id or batch id plus row id. */\n\tid: string;\n\texpiresAt?: number;\n\tpollAfterMs?: number;\n\t/** Provider conversion data required to reconstruct the final assistant message. */\n\tdata?: JsonValue;\n}\n\n/**\n * System instructions and tool declarations at one point in the transcript.\n *\n * The leading system message is the system prompt. Later system messages change it:\n * `content` adds instructions from that point on, `sections` replace or remove named\n * prompt sections, and `toolsAdded`/`toolsRemoved` change the tool set. Replaying\n * every system message in order yields the current prompt and tools. Providers that\n * accept system messages mid-conversation send each one in place; other providers\n * rebuild the leading system message from the replayed state.\n */\nexport interface SystemMessage {\n\trole: \"system\";\n\t/** Instruction text. On the leading message this is the base prompt; later, additional instructions. */\n\tcontent: string | TextContent[];\n\t/**\n\t * Named, ordered prompt sections rendered verbatim after `content`. The leading message\n\t * declares them; later messages replace sections by name, and `null` removes one. Keep\n\t * each section self-delimiting (a tag, a heading) so the model can relate an update to\n\t * the original. Avoid integer-like names; JSON objects reorder those.\n\t */\n\tsections?: Record<string, string | null>;\n\t/** Complete definitions of tools that become available at this point. */\n\ttoolsAdded?: Tool[];\n\t/** Tools that stop being available at this point. */\n\ttoolsRemoved?: ToolReference[];\n\ttimestamp: number; // Unix timestamp in milliseconds\n}\n\nexport interface UserMessage {\n\trole: \"user\";\n\tcontent: string | (TextContent | ImageContent | DocumentContent)[];\n\ttimestamp: number; // Unix timestamp in milliseconds\n}\n\nexport interface AssistantMessage {\n\trole: \"assistant\";\n\tcontent: (TextContent | ThinkingContent | ToolCall | FallbackContent)[];\n\tapi: Api;\n\tprovider: ProviderId;\n\tmodel: string;\n\tresponseModel?: string; // Concrete model reported by the provider when different from the requested `model`\n\tresponseId?: string; // Provider-specific response/message identifier when the upstream API exposes one\n\t/** Exact provider-native effort level used for this response. Absent for legacy or unmanaged responses. */\n\tproviderThinkingLevel?: string;\n\tdiagnostics?: AssistantMessageDiagnostic[]; // Redacted provider/runtime diagnostics for failures and recoveries.\n\tusage: Usage;\n\tstopReason: StopReason;\n\tdeferred?: DeferredHandle;\n\terrorMessage?: string;\n\t/** Server-requested retry delay in ms (`Retry-After` or a body `retry_after`), when the provider sent one. */\n\tretryAfterMs?: number;\n\trawStopReason?: string;\n\t/**\n\t * Provider indication of whether the model explicitly ended its turn.\n\t * Preserved for debugging and does not currently affect agent control flow.\n\t */\n\tendTurn?: boolean;\n\ttimestamp: number; // Unix timestamp in milliseconds\n\tdurationMs?: number;\n}\n\n/** A tool call made by another tool, such as a codemode script. */\nexport interface NestedToolCallRecord {\n\tid: string;\n\tname: string;\n\t/** Omitted when over the size limits. */\n\targuments?: JsonObject;\n\t/** UTF-8 JSON size when arguments are omitted. */\n\targumentsBytes?: number;\n\tstatus: \"ok\" | \"error\" | \"unfinished\";\n\tdurationMs?: number;\n\terror?: string;\n}\n\n/** Bounded nested-call record. Results are not recorded. */\nexport interface NestedToolCalls {\n\tcalls: NestedToolCallRecord[];\n\tcomplete: boolean;\n}\n\nexport type ToolResultMessage<TDetails = JsonValue> =\n\tIsJsonCompatible<TDetails> extends true\n\t\t? {\n\t\t\t\trole: \"toolResult\";\n\t\t\t\ttoolCallId: string;\n\t\t\t\ttoolName: string;\n\t\t\t\tcontent: (TextContent | ImageContent)[]; // Supports text and images\n\t\t\t\tdetails?: JsonRepresentation<TDetails>;\n\t\t\t\t/** Usage from the tool execution itself, if available. Not part of main LLM context accounting. */\n\t\t\t\tusage?: Usage;\n\t\t\t\t/** Nested calls retained in the session, not sent to the model. */\n\t\t\t\tnestedCalls?: NestedToolCalls;\n\t\t\t\tisError: boolean;\n\t\t\t\ttimestamp: number; // Unix timestamp in milliseconds\n\t\t\t\tdurationMs?: number;\n\t\t\t}\n\t\t: never;\n\nexport type Message = SystemMessage | UserMessage | AssistantMessage | ToolResultMessage;\n\nexport type ImagesInputContent = TextContent | ImageContent;\nexport type ImagesOutputContent = TextContent | ImageContent;\n\nexport interface ImagesContext {\n\tinput: ImagesInputContent[];\n}\n\nexport type ImagesStopReason = \"stop\" | \"error\" | \"aborted\";\n\nexport interface AssistantImages {\n\tapi: ImageApi;\n\tprovider: ProviderId;\n\tmodel: string;\n\toutput: ImagesOutputContent[];\n\tresponseId?: string;\n\tusage?: Usage;\n\tstopReason: ImagesStopReason;\n\terrorMessage?: string;\n\ttimestamp: number; // Unix timestamp in milliseconds\n}\n\nexport interface ClassifierChoiceQuestion {\n\ttype: \"choice\";\n\tinstructions: string;\n\tcriteria: Record<string, string>;\n}\n\nexport interface ClassifierScoreQuestion {\n\ttype: \"score\";\n\tinstructions: string;\n\tcriteria: string[];\n}\n\nexport interface ClassifierBoolQuestion {\n\ttype: \"bool\";\n\tinstructions: string;\n\tcriteria: { true: string; false: string };\n}\n\nexport type ClassifierQuestion = ClassifierChoiceQuestion | ClassifierScoreQuestion | ClassifierBoolQuestion;\n\nexport interface ClassifierContext {\n\tstate: JsonObject;\n\t/**\n\t * Images judged together with `state`. Only models whose `input` includes `\"image\"` accept them;\n\t * other models return an error result.\n\t */\n\timages?: ImageContent[];\n\tquestions: Record<string, ClassifierQuestion>;\n}\n\nexport interface ClassifierChoiceAnswer {\n\ttype: \"choice\";\n\tchoice: string;\n\tprobabilities: Record<string, number>;\n\tconfidence: number;\n}\n\nexport interface ClassifierScoreAnswer {\n\ttype: \"score\";\n\tscore: number;\n\tconfidence: number;\n}\n\nexport interface ClassifierBoolAnswer {\n\ttype: \"bool\";\n\tprobability: number;\n}\n\nexport type ClassifierAnswer = ClassifierChoiceAnswer | ClassifierScoreAnswer | ClassifierBoolAnswer;\nexport type ClassifierStopReason = \"stop\" | \"error\" | \"aborted\";\n\nexport interface ClassifierResult {\n\tapi: ClassifierApi;\n\tprovider: ProviderId;\n\tmodel: string;\n\tanswers: Record<string, ClassifierAnswer>;\n\t/** Token usage and cost at the model's catalog price, when reported. */\n\tusage?: Usage;\n\tstopReason: ClassifierStopReason;\n\terrorMessage?: string;\n\ttimestamp: number; // Unix timestamp in milliseconds\n}\n\nimport type { TSchema } from \"typebox\";\n\n/** OpenAI grammar variants for constrained sampling. */\nexport type GrammarFormat = \"openai_lark\" | \"openai_regex\";\n\nexport type GrammarVariants = Partial<Record<GrammarFormat, string>>;\n\n/**\n * Optional provider-side constrained sampling configs for a tool.\n *\n * The `json_schema` value roughly maps to the concept of `strict` in APIs which is\n * implemented as json-schema constrained sampling by APIs. Grammar variants let\n * callers provide provider-specific encodings of the same intended language.\n */\nexport type ConstrainedSamplingConfig =\n\t| {\n\t\t\ttype: \"json_schema\";\n\t\t\tstrict: \"prefer\" | \"require\";\n\t }\n\t| {\n\t\t\ttype: \"grammar\";\n\t\t\tvariants: GrammarVariants;\n\t };\n\nexport interface Tool<TParameters extends TSchema = TSchema> {\n\tname: string;\n\tdescription: string;\n\tparameters: TParameters;\n\tconstrainedSampling?: false | ConstrainedSamplingConfig;\n}\n\nexport interface ToolReference {\n\tname: string;\n}\n\n/**\n * Request input accepted by the public stream entry points (`Models.stream()`,\n * `streamSimple()`, ...). `systemPrompt` and `tools` are shorthand for a leading\n * system message; `normalizeContext()` folds them into one before the request\n * reaches a provider.\n */\nexport interface Context {\n\tsystemPrompt?: string;\n\tmessages: Message[];\n\ttools?: Tool[];\n}\n\ndeclare const transcriptContextBrand: unique symbol;\n\n/**\n * Normalized request context passed to providers and API implementations. The\n * prompt and tool declarations are carried by the transcript's system messages.\n * Only `normalizeContext()` produces this type, so a raw `Context` cannot reach\n * provider code by accident.\n */\nexport type TranscriptContext = {\n\tmessages: Message[];\n\treadonly [transcriptContextBrand]: true;\n};\n\n/**\n * Event protocol for AssistantMessageEventStream.\n *\n * Successful streams emit `start` before partial updates and terminate with\n * `done`. A stream may terminate directly with `error` when request setup fails\n * before generation starts; after `start`, failures also terminate with `error`.\n * Direct `streamSimple()` calls throw synchronously when request auth is missing.\n * Updates and `done` must never appear before `start`.\n *\n * Tool-call arguments at `toolcall_start` are provider-specific;\n * `toolcall_delta` carries subsequent JSON updates.\n */\nexport type AssistantMessageEvent =\n\t| { type: \"start\"; partial: AssistantMessage }\n\t| { type: \"text_start\"; contentIndex: number; partial: AssistantMessage }\n\t| { type: \"text_delta\"; contentIndex: number; delta: string; partial: AssistantMessage }\n\t| { type: \"text_end\"; contentIndex: number; content: string; partial: AssistantMessage }\n\t| { type: \"thinking_start\"; contentIndex: number; partial: AssistantMessage }\n\t| { type: \"thinking_delta\"; contentIndex: number; delta: string; partial: AssistantMessage }\n\t| { type: \"thinking_end\"; contentIndex: number; content: string; partial: AssistantMessage }\n\t| { type: \"toolcall_start\"; contentIndex: number; partial: AssistantMessage }\n\t| { type: \"toolcall_delta\"; contentIndex: number; delta: string; partial: AssistantMessage }\n\t| { type: \"toolcall_end\"; contentIndex: number; toolCall: ToolCall; partial: AssistantMessage }\n\t| {\n\t\t\ttype: \"done\";\n\t\t\treason: Extract<StopReason, \"stop\" | \"length\" | \"toolUse\" | \"deferred\">;\n\t\t\tmessage: AssistantMessage;\n\t }\n\t| { type: \"error\"; reason: Extract<StopReason, \"aborted\" | \"error\">; error: AssistantMessage };\n\n/** Service tiers a model can advertise beyond standard processing. */\nexport type ModelServiceTierId = \"priority\" | \"ultrafast\";\n\n/**\n * A service tier a model advertises, following Codex's `ModelInfo.service_tiers`, with the provider's\n * published per-million-token rates for that tier.\n *\n * Adapters send a tier only when the model advertises it. `flex` is an API request option and needs\n * no advertisement.\n */\nexport interface ModelServiceTier {\n\tid: ModelServiceTierId;\n\tcost: ModelCost;\n}\n\n/**\n * Explicit routing metadata that marks a model as the fast-inference variant of another model.\n *\n * Presence of this field — never a `-fast` name suffix — is what gives a model fast semantics.\n * A provider, `models.json` custom model, or extension may own an exact `<base>-fast` ID without\n * this metadata, in which case it is an ordinary model and routes exactly as it is declared.\n */\nexport interface ModelFastRoute {\n\t/** Canonical ID of the normal-speed model this variant pairs with, in the same provider. */\n\tbaseModelId: string;\n\t/** Model ID to send upstream. OpenAI-style routing keeps the base ID; a provider with real fast siblings sends its own ID. */\n\tupstreamModelId: string;\n\t/** Service tier to send with the request. Set only for providers that route fast traffic through an OpenAI-style tier. */\n\tserviceTier?: ModelServiceTierId;\n\t/** Anthropic inference speed to send with the request. Set only for Claude models that support fast mode. */\n\tspeed?: \"fast\";\n}\n\n/** Fields shared by every catalog entry, regardless of what you can do with it. */\nexport interface BaseModel<TApi extends string> {\n\tid: string;\n\tname: string;\n\tapi: TApi;\n\tprovider: ProviderId;\n\tbaseUrl: string;\n\tinput: ModelInputModality[];\n\t/** Provider input limits and cache-safe preprocessing metadata. */\n\tinputLimits?: ModelInputLimits;\n\tcost: ModelCost;\n\theaders?: Record<string, string>;\n}\n\n/** Chat model: usable with `stream()` and friends. */\nexport interface Model<TApi extends Api> extends BaseModel<TApi> {\n\t/**\n\t * Optional: chat is the default model type, so models without `type` are chat\n\t * models. Narrow mixed model lists with `isModelType()` instead of comparing\n\t * `type` directly.\n\t */\n\ttype?: \"chat\";\n\treasoning: boolean;\n\t/**\n\t * Maps pi thinking levels to provider/model-specific values.\n\t * Missing keys use provider defaults. null marks a level as unsupported.\n\t */\n\tthinkingLevelMap?: ThinkingLevelMap;\n\t/** Prompt cache lifetimes per retention tier. Unset when the provider's cache behavior is unknown. */\n\tpromptCache?: ModelPromptCache;\n\tcontextWindow: number;\n\tmaxTokens: number;\n\t/** Default sampling parameters for this model. See {@link StreamOptions.samplingParams}; per-request keys override these. */\n\tsamplingParams?: SamplingParams;\n\t/** Sampling parameter overrides selected by the effective pi thinking level. */\n\tsamplingParamsByThinkingLevel?: SamplingParamsByThinkingLevel;\n\t/**\n\t * Marks this model as the fast-inference variant of {@link ModelFastRoute.baseModelId} and carries the\n\t * upstream routing it needs. Absent on every normal model.\n\t */\n\tfastRoute?: ModelFastRoute;\n\t/**\n\t * Service tiers this model advertises. When set, the Codex adapter sends only advertised tiers.\n\t * When unset, the model carries no tier metadata and only Ultrafast is withheld.\n\t */\n\tserviceTiers?: ModelServiceTier[];\n\t/** Compatibility overrides for OpenAI-compatible APIs. If not set, auto-detected from baseUrl. */\n\tcompat?: TApi extends \"openai-completions\"\n\t\t? OpenAICompletionsCompat\n\t\t: TApi extends \"openai-responses\" | \"azure-openai-responses\" | \"openai-codex-responses\"\n\t\t\t? OpenAIResponsesCompat\n\t\t\t: TApi extends \"anthropic-messages\"\n\t\t\t\t? AnthropicMessagesCompat\n\t\t\t\t: TApi extends \"bedrock-converse-stream\"\n\t\t\t\t\t? BedrockCompat\n\t\t\t\t\t: TApi extends \"mistral-conversations\"\n\t\t\t\t\t\t? MistralConversationsCompat\n\t\t\t\t\t\t: never;\n}\n\n/** Image-generation model: usable with `generateImages()` only. */\nexport interface ImageModel<TApi extends ImageApi> extends BaseModel<TApi> {\n\ttype: \"image\";\n\t/** Output modalities. Always includes `\"image\"`; `\"text\"` means the model can also return text blocks. */\n\toutput: (\"text\" | \"image\")[];\n}\n\n/** Structured classifier model: usable with `classify()` only. */\nexport interface ClassifierModel<TApi extends ClassifierApi> extends BaseModel<TApi> {\n\ttype: \"classifier\";\n\tcontextWindow: number;\n}\n\n/** Dedicated compaction model, excluded from chat model selection. */\nexport interface CompactorModel<TApi extends CompactorApi = CompactorApi> extends BaseModel<TApi> {\n\ttype: \"compactor\";\n}\n\n/** Model shape for each model type. */\nexport interface ModelTypeMap {\n\tchat: Model<Api>;\n\timage: ImageModel<ImageApi>;\n\tclassifier: ClassifierModel<ClassifierApi>;\n\tcompactor: CompactorModel;\n}\n\n/** What a catalog entry is for. Decides which `Models` operation accepts it. */\nexport type ModelType = keyof ModelTypeMap;\n\n/** Anything a provider can list. Narrow with `isModelType()`. */\nexport type AnyModel = ModelTypeMap[ModelType];\n"]}
@@ -1,3 +1,4 @@
1
+ import type { ToolCall } from "../types.ts";
1
2
  /**
2
3
  * Repairs malformed JSON string literals by:
3
4
  * - escaping raw control characters inside strings
@@ -5,6 +6,7 @@
5
6
  */
6
7
  export declare function repairJson(json: string): string;
7
8
  export declare function parseJsonWithRepair<T>(json: string): T;
9
+ export declare function parseToolCallArguments(json: string | undefined): Pick<ToolCall, "arguments" | "argumentsError">;
8
10
  /**
9
11
  * Attempts to parse potentially incomplete JSON during streaming.
10
12
  * Always returns a valid object, even if the JSON is incomplete.
@@ -1 +1 @@
1
- {"version":3,"file":"json-parse.d.ts","sourceRoot":"","sources":["../../src/utils/json-parse.ts"],"names":[],"mappings":"AA0BA;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAmD/C;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAUtD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,MAAM,GAAG,SAAS,GAAG,CAAC,CAoBlG"}
1
+ {"version":3,"file":"json-parse.d.ts","sourceRoot":"","sources":["../../src/utils/json-parse.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AA0B5C;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAmD/C;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAUtD;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC,QAAQ,EAAE,WAAW,GAAG,gBAAgB,CAAC,CAc/G;AAwCD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,MAAM,GAAG,SAAS,GAAG,CAAC,CAoBlG"}
@@ -1,5 +1,5 @@
1
1
  import { parse as partialParse } from "partial-json";
2
- const VALID_JSON_ESCAPES = new Set(['"', "\\", "/", "b", "f", "n", "r", "t", "u"]);
2
+ const VALID_JSON_ESCAPES = new Set(['"', "\\", "/", "b", "f", "n", "r", "t"]);
3
3
  function isControlCharacter(char) {
4
4
  const codePoint = char.codePointAt(0);
5
5
  return codePoint !== undefined && codePoint >= 0x00 && codePoint <= 0x1f;
@@ -80,6 +80,60 @@ export function parseJsonWithRepair(json) {
80
80
  throw error;
81
81
  }
82
82
  }
83
+ export function parseToolCallArguments(json) {
84
+ if (!json?.trim())
85
+ return { arguments: {} };
86
+ try {
87
+ const parsed = parseJsonWithRepair(json);
88
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
89
+ throw new Error("Tool arguments must be a JSON object");
90
+ }
91
+ return { arguments: parsed };
92
+ }
93
+ catch (error) {
94
+ return {
95
+ arguments: {},
96
+ argumentsError: `${error instanceof Error ? error.message : String(error)}\n\nReceived arguments:\n${json}`,
97
+ };
98
+ }
99
+ }
100
+ function restoreJsonStrings(value) {
101
+ if (typeof value === "string")
102
+ return value.slice(1);
103
+ if (Array.isArray(value))
104
+ return value.map(restoreJsonStrings);
105
+ if (value !== null && typeof value === "object") {
106
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key.slice(1), restoreJsonStrings(item)]));
107
+ }
108
+ return value;
109
+ }
110
+ function prefixJsonStrings(json) {
111
+ let prefixed = "";
112
+ let inString = false;
113
+ for (let index = 0; index < json.length; index++) {
114
+ const char = json[index];
115
+ prefixed += char;
116
+ if (!inString) {
117
+ if (char === '"') {
118
+ inString = true;
119
+ prefixed += "$";
120
+ }
121
+ continue;
122
+ }
123
+ if (char === "\\") {
124
+ index += 1;
125
+ if (index < json.length)
126
+ prefixed += json[index];
127
+ }
128
+ else if (char === '"') {
129
+ inString = false;
130
+ }
131
+ }
132
+ return prefixed;
133
+ }
134
+ function parsePartialJson(json) {
135
+ return restoreJsonStrings(partialParse(prefixJsonStrings(json)));
136
+ }
83
137
  /**
84
138
  * Attempts to parse potentially incomplete JSON during streaming.
85
139
  * Always returns a valid object, even if the JSON is incomplete.
@@ -96,12 +150,12 @@ export function parseStreamingJson(partialJson) {
96
150
  }
97
151
  catch {
98
152
  try {
99
- const result = partialParse(partialJson);
153
+ const result = parsePartialJson(partialJson);
100
154
  return (result ?? {});
101
155
  }
102
156
  catch {
103
157
  try {
104
- const result = partialParse(repairJson(partialJson));
158
+ const result = parsePartialJson(repairJson(partialJson));
105
159
  return (result ?? {});
106
160
  }
107
161
  catch {
@@ -1 +1 @@
1
- {"version":3,"file":"json-parse.js","sourceRoot":"","sources":["../../src/utils/json-parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,YAAY,EAAE,MAAM,cAAc,CAAC;AAErD,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAEnF,SAAS,kBAAkB,CAAC,IAAY;IACvC,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IACtC,OAAO,SAAS,KAAK,SAAS,IAAI,SAAS,IAAI,IAAI,IAAI,SAAS,IAAI,IAAI,CAAC;AAC1E,CAAC;AAED,SAAS,sBAAsB,CAAC,IAAY;IAC3C,QAAQ,IAAI,EAAE,CAAC;QACd,KAAK,IAAI;YACR,OAAO,KAAK,CAAC;QACd,KAAK,IAAI;YACR,OAAO,KAAK,CAAC;QACd,KAAK,IAAI;YACR,OAAO,KAAK,CAAC;QACd,KAAK,IAAI;YACR,OAAO,KAAK,CAAC;QACd,KAAK,IAAI;YACR,OAAO,KAAK,CAAC;QACd;YACC,OAAO,MAAM,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,IAAI,MAAM,EAAE,CAAC;IAC9E,CAAC;AACF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACtC,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,IAAI,QAAQ,GAAG,KAAK,CAAC;IAErB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAA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CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAC5E,CAAC;IAED,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAI,IAAY;IAClD,IAAI,CAAC;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,CAAC;IAC9B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,MAAM,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;YAC3B,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAM,CAAC;QACtC,CAAC;QACD,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAA8B,WAA+B;IAC9F,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QAC/C,OAAO,EAAO,CAAC;IAChB,CAAC;IAED,IAAI,CAAC;QACJ,OAAO,mBAAmB,CAAI,WAAW,CAAC,CAAC;IAC5C,CAAC;IAAC,MAAM,CAAC;QACR,IAAI,CAAC;YACJ,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC;YACzC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAM,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACR,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,YAAY,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC;gBACrD,OAAO,CAAC,MAAM,IAAI,EAAE,CAAM,CAAC;YAC5B,CAAC;YAAC,MAAM,CAAC;gBACR,OAAO,EAAO,CAAC;YAChB,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import { parse as partialParse } from \"partial-json\";\n\nconst VALID_JSON_ESCAPES = new Set(['\"', \"\\\\\", \"/\", \"b\", \"f\", \"n\", \"r\", \"t\", \"u\"]);\n\nfunction isControlCharacter(char: string): boolean {\n\tconst codePoint = char.codePointAt(0);\n\treturn codePoint !== undefined && codePoint >= 0x00 && codePoint <= 0x1f;\n}\n\nfunction escapeControlCharacter(char: string): string {\n\tswitch (char) {\n\t\tcase \"\\b\":\n\t\t\treturn \"\\\\b\";\n\t\tcase \"\\f\":\n\t\t\treturn \"\\\\f\";\n\t\tcase \"\\n\":\n\t\t\treturn \"\\\\n\";\n\t\tcase \"\\r\":\n\t\t\treturn \"\\\\r\";\n\t\tcase \"\\t\":\n\t\t\treturn \"\\\\t\";\n\t\tdefault:\n\t\t\treturn `\\\\u${char.codePointAt(0)?.toString(16).padStart(4, \"0\") ?? \"0000\"}`;\n\t}\n}\n\n/**\n * Repairs malformed JSON string literals by:\n * - escaping raw control characters inside strings\n * - doubling backslashes before invalid escape characters\n */\nexport function repairJson(json: string): string {\n\tlet repaired = \"\";\n\tlet inString = false;\n\n\tfor (let index = 0; index < json.length; index++) {\n\t\tconst char = json[index];\n\n\t\tif (!inString) {\n\t\t\trepaired += char;\n\t\t\tif (char === '\"') {\n\t\t\t\tinString = true;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (char === '\"') {\n\t\t\trepaired += char;\n\t\t\tinString = false;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (char === \"\\\\\") {\n\t\t\tconst nextChar = json[index + 1];\n\t\t\tif (nextChar === undefined) {\n\t\t\t\trepaired += \"\\\\\\\\\";\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (nextChar === \"u\") {\n\t\t\t\tconst unicodeDigits = json.slice(index + 2, index + 6);\n\t\t\t\tif (/^[0-9a-fA-F]{4}$/.test(unicodeDigits)) {\n\t\t\t\t\trepaired += `\\\\u${unicodeDigits}`;\n\t\t\t\t\tindex += 5;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (VALID_JSON_ESCAPES.has(nextChar)) {\n\t\t\t\trepaired += `\\\\${nextChar}`;\n\t\t\t\tindex += 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\trepaired += \"\\\\\\\\\";\n\t\t\tcontinue;\n\t\t}\n\n\t\trepaired += isControlCharacter(char) ? escapeControlCharacter(char) : char;\n\t}\n\n\treturn repaired;\n}\n\nexport function parseJsonWithRepair<T>(json: string): T {\n\ttry {\n\t\treturn JSON.parse(json) as T;\n\t} catch (error) {\n\t\tconst repairedJson = repairJson(json);\n\t\tif (repairedJson !== json) {\n\t\t\treturn JSON.parse(repairedJson) as T;\n\t\t}\n\t\tthrow error;\n\t}\n}\n\n/**\n * Attempts to parse potentially incomplete JSON during streaming.\n * Always returns a valid object, even if the JSON is incomplete.\n *\n * @param partialJson The partial JSON string from streaming\n * @returns Parsed object or empty object if parsing fails\n */\nexport function parseStreamingJson<T = Record<string, unknown>>(partialJson: string | undefined): T {\n\tif (!partialJson || partialJson.trim() === \"\") {\n\t\treturn {} as T;\n\t}\n\n\ttry {\n\t\treturn parseJsonWithRepair<T>(partialJson);\n\t} catch {\n\t\ttry {\n\t\t\tconst result = partialParse(partialJson);\n\t\t\treturn (result ?? {}) as T;\n\t\t} catch {\n\t\t\ttry {\n\t\t\t\tconst result = partialParse(repairJson(partialJson));\n\t\t\t\treturn (result ?? {}) as T;\n\t\t\t} catch {\n\t\t\t\treturn {} as T;\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"json-parse.js","sourceRoot":"","sources":["../../src/utils/json-parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,YAAY,EAAE,MAAM,cAAc,CAAC;AAGrD,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAE9E,SAAS,kBAAkB,CAAC,IAAY;IACvC,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,C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CAAC,CAAC;YAC7C,OAAO,CAAC,MAAM,IAAI,EAAE,CAAM,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACR,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,gBAAgB,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC;gBACzD,OAAO,CAAC,MAAM,IAAI,EAAE,CAAM,CAAC;YAC5B,CAAC;YAAC,MAAM,CAAC;gBACR,OAAO,EAAO,CAAC;YAChB,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import { parse as partialParse } from \"partial-json\";\nimport type { ToolCall } from \"../types.ts\";\n\nconst VALID_JSON_ESCAPES = new Set(['\"', \"\\\\\", \"/\", \"b\", \"f\", \"n\", \"r\", \"t\"]);\n\nfunction isControlCharacter(char: string): boolean {\n\tconst codePoint = char.codePointAt(0);\n\treturn codePoint !== undefined && codePoint >= 0x00 && codePoint <= 0x1f;\n}\n\nfunction escapeControlCharacter(char: string): string {\n\tswitch (char) {\n\t\tcase \"\\b\":\n\t\t\treturn \"\\\\b\";\n\t\tcase \"\\f\":\n\t\t\treturn \"\\\\f\";\n\t\tcase \"\\n\":\n\t\t\treturn \"\\\\n\";\n\t\tcase \"\\r\":\n\t\t\treturn \"\\\\r\";\n\t\tcase \"\\t\":\n\t\t\treturn \"\\\\t\";\n\t\tdefault:\n\t\t\treturn `\\\\u${char.codePointAt(0)?.toString(16).padStart(4, \"0\") ?? \"0000\"}`;\n\t}\n}\n\n/**\n * Repairs malformed JSON string literals by:\n * - escaping raw control characters inside strings\n * - doubling backslashes before invalid escape characters\n */\nexport function repairJson(json: string): string {\n\tlet repaired = \"\";\n\tlet inString = false;\n\n\tfor (let index = 0; index < json.length; index++) {\n\t\tconst char = json[index];\n\n\t\tif (!inString) {\n\t\t\trepaired += char;\n\t\t\tif (char === '\"') {\n\t\t\t\tinString = true;\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (char === '\"') {\n\t\t\trepaired += char;\n\t\t\tinString = false;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (char === \"\\\\\") {\n\t\t\tconst nextChar = json[index + 1];\n\t\t\tif (nextChar === undefined) {\n\t\t\t\trepaired += \"\\\\\\\\\";\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (nextChar === \"u\") {\n\t\t\t\tconst unicodeDigits = json.slice(index + 2, index + 6);\n\t\t\t\tif (/^[0-9a-fA-F]{4}$/.test(unicodeDigits)) {\n\t\t\t\t\trepaired += `\\\\u${unicodeDigits}`;\n\t\t\t\t\tindex += 5;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (VALID_JSON_ESCAPES.has(nextChar)) {\n\t\t\t\trepaired += `\\\\${nextChar}`;\n\t\t\t\tindex += 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\trepaired += \"\\\\\\\\\";\n\t\t\tcontinue;\n\t\t}\n\n\t\trepaired += isControlCharacter(char) ? escapeControlCharacter(char) : char;\n\t}\n\n\treturn repaired;\n}\n\nexport function parseJsonWithRepair<T>(json: string): T {\n\ttry {\n\t\treturn JSON.parse(json) as T;\n\t} catch (error) {\n\t\tconst repairedJson = repairJson(json);\n\t\tif (repairedJson !== json) {\n\t\t\treturn JSON.parse(repairedJson) as T;\n\t\t}\n\t\tthrow error;\n\t}\n}\n\nexport function parseToolCallArguments(json: string | undefined): Pick<ToolCall, \"arguments\" | \"argumentsError\"> {\n\tif (!json?.trim()) return { arguments: {} };\n\ttry {\n\t\tconst parsed = parseJsonWithRepair<ToolCall[\"arguments\"] | null>(json);\n\t\tif (typeof parsed !== \"object\" || parsed === null || Array.isArray(parsed)) {\n\t\t\tthrow new Error(\"Tool arguments must be a JSON object\");\n\t\t}\n\t\treturn { arguments: parsed };\n\t} catch (error) {\n\t\treturn {\n\t\t\targuments: {},\n\t\t\targumentsError: `${error instanceof Error ? error.message : String(error)}\\n\\nReceived arguments:\\n${json}`,\n\t\t};\n\t}\n}\n\ntype JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };\n\nfunction restoreJsonStrings(value: JsonValue): JsonValue {\n\tif (typeof value === \"string\") return value.slice(1);\n\tif (Array.isArray(value)) return value.map(restoreJsonStrings);\n\tif (value !== null && typeof value === \"object\") {\n\t\treturn Object.fromEntries(Object.entries(value).map(([key, item]) => [key.slice(1), restoreJsonStrings(item)]));\n\t}\n\treturn value;\n}\n\nfunction prefixJsonStrings(json: string): string {\n\tlet prefixed = \"\";\n\tlet inString = false;\n\tfor (let index = 0; index < json.length; index++) {\n\t\tconst char = json[index];\n\t\tprefixed += char;\n\t\tif (!inString) {\n\t\t\tif (char === '\"') {\n\t\t\t\tinString = true;\n\t\t\t\tprefixed += \"$\";\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === \"\\\\\") {\n\t\t\tindex += 1;\n\t\t\tif (index < json.length) prefixed += json[index];\n\t\t} else if (char === '\"') {\n\t\t\tinString = false;\n\t\t}\n\t}\n\treturn prefixed;\n}\n\nfunction parsePartialJson(json: string): JsonValue {\n\treturn restoreJsonStrings(partialParse(prefixJsonStrings(json)) as JsonValue);\n}\n\n/**\n * Attempts to parse potentially incomplete JSON during streaming.\n * Always returns a valid object, even if the JSON is incomplete.\n *\n * @param partialJson The partial JSON string from streaming\n * @returns Parsed object or empty object if parsing fails\n */\nexport function parseStreamingJson<T = Record<string, unknown>>(partialJson: string | undefined): T {\n\tif (!partialJson || partialJson.trim() === \"\") {\n\t\treturn {} as T;\n\t}\n\n\ttry {\n\t\treturn parseJsonWithRepair<T>(partialJson);\n\t} catch {\n\t\ttry {\n\t\t\tconst result = parsePartialJson(partialJson);\n\t\t\treturn (result ?? {}) as T;\n\t\t} catch {\n\t\t\ttry {\n\t\t\t\tconst result = parsePartialJson(repairJson(partialJson));\n\t\t\t\treturn (result ?? {}) as T;\n\t\t\t} catch {\n\t\t\t\treturn {} as T;\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,12 @@
1
+ interface HeaderReader {
2
+ get(name: string): string | null;
3
+ }
4
+ /**
5
+ * Largest server retry hint in ms from `Retry-After-Ms`, `Retry-After` (seconds or
6
+ * HTTP date) and a JSON body `retry_after` in seconds (Cloudflare 5xx pages).
7
+ */
8
+ export declare function getRetryAfterMs(headers?: HeaderReader, body?: string, now?: number): number | undefined;
9
+ /** Server retry hint carried by a provider SDK error (`headers` plus parsed body `error`), or an explicit `retryAfterMs`. */
10
+ export declare function getProviderRetryAfterMs(error: object): number | undefined;
11
+ export {};
12
+ //# sourceMappingURL=retry-hints.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-hints.d.ts","sourceRoot":"","sources":["../../src/utils/retry-hints.ts"],"names":[],"mappings":"AAAA,UAAU,YAAY;IACrB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CACjC;AAUD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,SAAa,GAAG,MAAM,GAAG,SAAS,CAwB3G;AAED,6HAA6H;AAC7H,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAQzE"}
@@ -0,0 +1,50 @@
1
+ function positiveDelay(value) {
2
+ return Number.isFinite(value) && value >= 0 ? value : undefined;
3
+ }
4
+ function isHeaderReader(value) {
5
+ return value !== null && "get" in value && typeof value.get === "function";
6
+ }
7
+ /**
8
+ * Largest server retry hint in ms from `Retry-After-Ms`, `Retry-After` (seconds or
9
+ * HTTP date) and a JSON body `retry_after` in seconds (Cloudflare 5xx pages).
10
+ */
11
+ export function getRetryAfterMs(headers, body, now = Date.now()) {
12
+ const hints = [];
13
+ const milliseconds = headers?.get("retry-after-ms");
14
+ if (milliseconds?.trim()) {
15
+ const delay = positiveDelay(Number(milliseconds));
16
+ if (delay !== undefined)
17
+ hints.push(delay);
18
+ }
19
+ const retryAfter = headers?.get("retry-after");
20
+ if (retryAfter?.trim()) {
21
+ const seconds = Number(retryAfter);
22
+ const delay = positiveDelay(Number.isFinite(seconds) ? seconds * 1000 : Date.parse(retryAfter) - now);
23
+ if (delay !== undefined)
24
+ hints.push(delay);
25
+ }
26
+ if (body) {
27
+ try {
28
+ const parsed = JSON.parse(body);
29
+ const seconds = parsed?.retry_after ?? parsed?.error?.retry_after;
30
+ if (typeof seconds === "number") {
31
+ const delay = positiveDelay(seconds * 1000);
32
+ if (delay !== undefined)
33
+ hints.push(delay);
34
+ }
35
+ }
36
+ catch { }
37
+ }
38
+ return hints.length ? Math.max(...hints) : undefined;
39
+ }
40
+ /** Server retry hint carried by a provider SDK error (`headers` plus parsed body `error`), or an explicit `retryAfterMs`. */
41
+ export function getProviderRetryAfterMs(error) {
42
+ if ("retryAfterMs" in error && typeof error.retryAfterMs === "number")
43
+ return positiveDelay(error.retryAfterMs);
44
+ const headers = "headers" in error && typeof error.headers === "object" && isHeaderReader(error.headers)
45
+ ? error.headers
46
+ : undefined;
47
+ const body = "error" in error ? JSON.stringify(error.error) : undefined;
48
+ return getRetryAfterMs(headers, body);
49
+ }
50
+ //# sourceMappingURL=retry-hints.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-hints.js","sourceRoot":"","sources":["../../src/utils/retry-hints.ts"],"names":[],"mappings":"AAIA,SAAS,aAAa,CAAC,KAAa;IACnC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACjE,CAAC;AAED,SAAS,cAAc,CAAC,KAAoB;IAC3C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,GAAG,KAAK,UAAU,CAAC;AAC5E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,OAAsB,EAAE,IAA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AAC,MAAM,CAAC,CAAA,CAAC;IACX,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACtD,CAAC;AAED,6HAA6H;AAC7H,MAAM,UAAU,uBAAuB,CAAC,KAAa;IACpD,IAAI,cAAc,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,YAAY,KAAK,QAAQ;QAAE,OAAO,aAAa,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAChH,MAAM,OAAO,GACZ,SAAS,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,IAAI,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC;QACvF,CAAC,CAAC,KAAK,CAAC,OAAO;QACf,CAAC,CAAC,SAAS,CAAC;IACd,MAAM,IAAI,GAAG,OAAO,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACxE,OAAO,eAAe,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACvC,CAAC","sourcesContent":["interface HeaderReader {\n\tget(name: string): string | null;\n}\n\nfunction positiveDelay(value: number): number | undefined {\n\treturn Number.isFinite(value) && value >= 0 ? value : undefined;\n}\n\nfunction isHeaderReader(value: object | null): value is HeaderReader {\n\treturn value !== null && \"get\" in value && typeof value.get === \"function\";\n}\n\n/**\n * Largest server retry hint in ms from `Retry-After-Ms`, `Retry-After` (seconds or\n * HTTP date) and a JSON body `retry_after` in seconds (Cloudflare 5xx pages).\n */\nexport function getRetryAfterMs(headers?: HeaderReader, body?: string, now = Date.now()): number | undefined {\n\tconst hints: number[] = [];\n\tconst milliseconds = headers?.get(\"retry-after-ms\");\n\tif (milliseconds?.trim()) {\n\t\tconst delay = positiveDelay(Number(milliseconds));\n\t\tif (delay !== undefined) hints.push(delay);\n\t}\n\tconst retryAfter = headers?.get(\"retry-after\");\n\tif (retryAfter?.trim()) {\n\t\tconst seconds = Number(retryAfter);\n\t\tconst delay = positiveDelay(Number.isFinite(seconds) ? seconds * 1000 : Date.parse(retryAfter) - now);\n\t\tif (delay !== undefined) hints.push(delay);\n\t}\n\tif (body) {\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(body) as { retry_after?: number; error?: { retry_after?: number } } | null;\n\t\t\tconst seconds = parsed?.retry_after ?? parsed?.error?.retry_after;\n\t\t\tif (typeof seconds === \"number\") {\n\t\t\t\tconst delay = positiveDelay(seconds * 1000);\n\t\t\t\tif (delay !== undefined) hints.push(delay);\n\t\t\t}\n\t\t} catch {}\n\t}\n\treturn hints.length ? Math.max(...hints) : undefined;\n}\n\n/** Server retry hint carried by a provider SDK error (`headers` plus parsed body `error`), or an explicit `retryAfterMs`. */\nexport function getProviderRetryAfterMs(error: object): number | undefined {\n\tif (\"retryAfterMs\" in error && typeof error.retryAfterMs === \"number\") return positiveDelay(error.retryAfterMs);\n\tconst headers =\n\t\t\"headers\" in error && typeof error.headers === \"object\" && isHeaderReader(error.headers)\n\t\t\t? error.headers\n\t\t\t: undefined;\n\tconst body = \"error\" in error ? JSON.stringify(error.error) : undefined;\n\treturn getRetryAfterMs(headers, body);\n}\n"]}
@@ -14,9 +14,25 @@ export interface RetryPolicy {
14
14
  baseDelayMs: number;
15
15
  /** Cap for agent-level retry delays in ms. Defaults to 60 seconds. */
16
16
  maxAgentDelayMs?: number;
17
+ /**
18
+ * Cap for a server retry hint (`Retry-After-Ms`, `Retry-After`, a body `retry_after`).
19
+ * Defaults to 60 seconds; 0 caps the hint by `maxAgentDelayMs` instead.
20
+ */
21
+ maxRetryDelayMs?: number;
17
22
  }
18
23
  export declare const DEFAULT_MAX_AGENT_RETRY_DELAY_MS = 60000;
19
24
  export declare function retryDelayMs(policy: Pick<RetryPolicy, "baseDelayMs" | "maxAgentDelayMs">, attempt: number): number;
25
+ type RetryFailure = Pick<AssistantMessage, "stopReason" | "errorMessage" | "retryAfterMs">;
26
+ /**
27
+ * Wait before retry `attempt` (1-indexed): the server retry hint when the failure
28
+ * carries one, capped by `maxRetryDelayMs`, otherwise the exponential backoff.
29
+ */
30
+ export declare function failureRetryDelayMs(policy: Pick<RetryPolicy, "baseDelayMs" | "maxAgentDelayMs" | "maxRetryDelayMs">, attempt: number, failure: Pick<AssistantMessage, "retryAfterMs">): number;
31
+ /**
32
+ * Delay before retry `attempt` (1-indexed) of a failed assistant call, or `undefined`
33
+ * when it is not retryable or `maxRetries` is spent.
34
+ */
35
+ export declare function assistantRetryDelayMs(policy: RetryPolicy, attempt: number, message: RetryFailure): number | undefined;
20
36
  /** Optional callbacks emitted by {@link retryAssistantCall} around each retry. */
21
37
  export interface RetryCallbacks {
22
38
  /** Emitted before the backoff sleep of each retry attempt (1-indexed). */
@@ -36,7 +52,8 @@ export interface RetryCallbacks {
36
52
  * too, so callers do not need to care when cancellation happened.
37
53
  * - A non-retryable error (per {@link isRetryableAssistantError}, including quota/
38
54
  * billing exhaustion) is returned immediately so deterministic errors fail fast.
39
- * - Otherwise retries up to `maxRetries` times with exponential backoff, emitting
55
+ * - Otherwise retries up to `maxRetries` times, waiting for the server retry hint or
56
+ * the exponential backoff (see {@link assistantRetryDelayMs}), emitting
40
57
  * `onRetryScheduled` before each sleep, `onRetryAttemptStart` after each sleep before
41
58
  * the retried call starts, and `onRetryFinished` once at the end (whether the loop
42
59
  * ends in success, exhausted retries, or an aborted backoff).
@@ -54,5 +71,6 @@ export declare function retryAssistantCall(produce: () => Promise<AssistantMessa
54
71
  * overflow separately, then apply their own retry budget, backoff, and reporting
55
72
  * before restarting the assistant turn.
56
73
  */
57
- export declare function isRetryableAssistantError(message: AssistantMessage): boolean;
74
+ export declare function isRetryableAssistantError(message: RetryFailure): boolean;
75
+ export {};
58
76
  //# sourceMappingURL=retry.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/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AAM,EACf,YAAY,EAAE,MAAM,KAChB,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,mFAAmF;IACnF,mBAAmB,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjD,mFAAmF;IACnF,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACnG;AA0BD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,kBAAkB,CACvC,OAAO,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,EACxC,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,SAAS,CAAC,EAAE,cAAc,GACxB,OAAO,CAAC,gBAAgB,CAAC,CA4C3B;AAED;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAKxE"}
@@ -33,14 +33,21 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
33
33
  "503",
34
34
  "504",
35
35
  "520",
36
+ "\\b52[1-3]\\b",
36
37
  "524",
37
- "service.?unavailable",
38
+ // Also matches "The service is temporarily unavailable" (#10965).
39
+ "service.?(?:is.?)?(?:temporarily.?)?unavailable",
38
40
  "server.?error",
39
41
  "internal.?error",
40
42
  // Wrapper/provider text for transient upstream failures, including OpenRouter
41
43
  // "Provider returned error" responses (#2264).
42
44
  "provider.?returned.?(?:an.?)?error",
43
45
  "exceeded request buffer limit while retrying upstream",
46
+ "unable to forward upstream",
47
+ "upstream.?request.?failed",
48
+ "upstream_stream_read_error",
49
+ "stream was interrupted",
50
+ "http/?2 stream failed",
44
51
  // Network, proxy, and fetch transport failures. This includes OpenAI Codex
45
52
  // raw-fetch failures such as "upstream connect", "connection refused", and
46
53
  // "reset before headers" (#733), plus OpenRouter connection drops (#3317).
@@ -48,8 +55,11 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
48
55
  "connection.?error",
49
56
  "connection.?refused",
50
57
  "connection.?lost",
58
+ "ECONNRESET",
59
+ "connection.?reset",
51
60
  "other side closed",
52
61
  "fetch failed",
62
+ "request to \\S+ failed, reason:",
53
63
  "getaddrinfo",
54
64
  "ENOTFOUND",
55
65
  "EAI_AGAIN",
@@ -104,6 +114,26 @@ export function retryDelayMs(policy, attempt) {
104
114
  const safeDelay = Number.isSafeInteger(delay) ? delay : Number.MAX_SAFE_INTEGER;
105
115
  return Math.min(safeDelay, policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS);
106
116
  }
117
+ /**
118
+ * Wait before retry `attempt` (1-indexed): the server retry hint when the failure
119
+ * carries one, capped by `maxRetryDelayMs`, otherwise the exponential backoff.
120
+ */
121
+ export function failureRetryDelayMs(policy, attempt, failure) {
122
+ const hintMs = failure.retryAfterMs;
123
+ if (hintMs === undefined || !Number.isFinite(hintMs) || hintMs <= 0)
124
+ return retryDelayMs(policy, attempt);
125
+ const capMs = policy.maxRetryDelayMs || (policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS);
126
+ return Math.min(hintMs, capMs);
127
+ }
128
+ /**
129
+ * Delay before retry `attempt` (1-indexed) of a failed assistant call, or `undefined`
130
+ * when it is not retryable or `maxRetries` is spent.
131
+ */
132
+ export function assistantRetryDelayMs(policy, attempt, message) {
133
+ if (!policy.enabled || attempt > policy.maxRetries || !isRetryableAssistantError(message))
134
+ return undefined;
135
+ return failureRetryDelayMs(policy, attempt, message);
136
+ }
107
137
  class RetrySleepAbortError extends Error {
108
138
  constructor() {
109
139
  super("Aborted");
@@ -132,7 +162,8 @@ function sleep(ms, signal) {
132
162
  * too, so callers do not need to care when cancellation happened.
133
163
  * - A non-retryable error (per {@link isRetryableAssistantError}, including quota/
134
164
  * billing exhaustion) is returned immediately so deterministic errors fail fast.
135
- * - Otherwise retries up to `maxRetries` times with exponential backoff, emitting
165
+ * - Otherwise retries up to `maxRetries` times, waiting for the server retry hint or
166
+ * the exponential backoff (see {@link assistantRetryDelayMs}), emitting
136
167
  * `onRetryScheduled` before each sleep, `onRetryAttemptStart` after each sleep before
137
168
  * the retried call starts, and `onRetryFinished` once at the end (whether the loop
138
169
  * ends in success, exhausted retries, or an aborted backoff).
@@ -159,14 +190,14 @@ export async function retryAssistantCall(produce, policy, signal, callbacks) {
159
190
  return response;
160
191
  }
161
192
  // Non-retryable, or budget exhausted: return the final error message.
162
- if (attempt >= maxAttempts || !isRetryableAssistantError(response)) {
193
+ const delayMs = policy ? assistantRetryDelayMs(policy, attempt + 1, response) : undefined;
194
+ if (delayMs === undefined) {
163
195
  if (lastRetry)
164
196
  await callbacks?.onRetryFinished?.(false, lastRetry.attempt, response.errorMessage);
165
197
  return response;
166
198
  }
167
199
  attempt++;
168
200
  lastRetry = { attempt, errorMessage: response.errorMessage || "Unknown error" };
169
- const delayMs = retryDelayMs(policy, attempt);
170
201
  await callbacks?.onRetryScheduled?.(attempt, maxAttempts, delayMs, lastRetry.errorMessage);
171
202
  // Normalize aborts during retry backoff to the same AssistantMessage shape as
172
203
  // provider stream aborts, so callers do not need to care when cancellation happened.