@bastani/pi-ai 0.9.71-alpha.1 → 0.9.72-alpha.1

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 (163) hide show
  1. package/CHANGELOG.md +64 -1
  2. package/README.md +78 -8
  3. package/dist/api/anthropic-messages.d.ts.map +1 -1
  4. package/dist/api/anthropic-messages.js +50 -10
  5. package/dist/api/anthropic-messages.js.map +1 -1
  6. package/dist/api/azure-openai-responses.d.ts.map +1 -1
  7. package/dist/api/azure-openai-responses.js +7 -2
  8. package/dist/api/azure-openai-responses.js.map +1 -1
  9. package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
  10. package/dist/api/bedrock-converse-stream.js +28 -1
  11. package/dist/api/bedrock-converse-stream.js.map +1 -1
  12. package/dist/api/bedrock-converse-stream.lazy.d.ts +7 -0
  13. package/dist/api/bedrock-converse-stream.lazy.d.ts.map +1 -1
  14. package/dist/api/bedrock-converse-stream.lazy.js +18 -5
  15. package/dist/api/bedrock-converse-stream.lazy.js.map +1 -1
  16. package/dist/api/google-generative-ai.d.ts +3 -1
  17. package/dist/api/google-generative-ai.d.ts.map +1 -1
  18. package/dist/api/google-generative-ai.js +4 -2
  19. package/dist/api/google-generative-ai.js.map +1 -1
  20. package/dist/api/google-shared.d.ts +10 -2
  21. package/dist/api/google-shared.d.ts.map +1 -1
  22. package/dist/api/google-shared.js +31 -9
  23. package/dist/api/google-shared.js.map +1 -1
  24. package/dist/api/google-vertex.d.ts +3 -1
  25. package/dist/api/google-vertex.d.ts.map +1 -1
  26. package/dist/api/google-vertex.js +4 -2
  27. package/dist/api/google-vertex.js.map +1 -1
  28. package/dist/api/lazy.d.ts +2 -2
  29. package/dist/api/lazy.d.ts.map +1 -1
  30. package/dist/api/lazy.js +10 -9
  31. package/dist/api/lazy.js.map +1 -1
  32. package/dist/api/mistral-conversations.d.ts.map +1 -1
  33. package/dist/api/mistral-conversations.js +12 -1
  34. package/dist/api/mistral-conversations.js.map +1 -1
  35. package/dist/api/openai-codex-responses.d.ts.map +1 -1
  36. package/dist/api/openai-codex-responses.js +6 -1
  37. package/dist/api/openai-codex-responses.js.map +1 -1
  38. package/dist/api/openai-completions.d.ts +2 -1
  39. package/dist/api/openai-completions.d.ts.map +1 -1
  40. package/dist/api/openai-completions.js +112 -15
  41. package/dist/api/openai-completions.js.map +1 -1
  42. package/dist/api/openai-images-options.d.ts +107 -0
  43. package/dist/api/openai-images-options.d.ts.map +1 -0
  44. package/dist/api/openai-images-options.js +106 -0
  45. package/dist/api/openai-images-options.js.map +1 -0
  46. package/dist/api/openai-images.d.ts +3 -11
  47. package/dist/api/openai-images.d.ts.map +1 -1
  48. package/dist/api/openai-images.js +117 -16
  49. package/dist/api/openai-images.js.map +1 -1
  50. package/dist/api/openai-responses-shared.js +6 -2
  51. package/dist/api/openai-responses-shared.js.map +1 -1
  52. package/dist/api/openai-responses.d.ts.map +1 -1
  53. package/dist/api/openai-responses.js +29 -7
  54. package/dist/api/openai-responses.js.map +1 -1
  55. package/dist/auth/oauth/load.d.ts.map +1 -1
  56. package/dist/auth/oauth/load.js +5 -2
  57. package/dist/auth/oauth/load.js.map +1 -1
  58. package/dist/env-api-keys.d.ts.map +1 -1
  59. package/dist/env-api-keys.js +1 -0
  60. package/dist/env-api-keys.js.map +1 -1
  61. package/dist/image-generation-error.d.ts +203 -0
  62. package/dist/image-generation-error.d.ts.map +1 -0
  63. package/dist/image-generation-error.js +441 -0
  64. package/dist/image-generation-error.js.map +1 -0
  65. package/dist/index.d.ts +4 -1
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +4 -0
  68. package/dist/index.js.map +1 -1
  69. package/dist/lazy-bedrock-provider.d.ts +10 -0
  70. package/dist/lazy-bedrock-provider.d.ts.map +1 -0
  71. package/dist/lazy-bedrock-provider.js +13 -0
  72. package/dist/lazy-bedrock-provider.js.map +1 -0
  73. package/dist/lazy-oauth.d.ts +10 -0
  74. package/dist/lazy-oauth.d.ts.map +1 -0
  75. package/dist/lazy-oauth.js +23 -0
  76. package/dist/lazy-oauth.js.map +1 -0
  77. package/dist/models.d.ts.map +1 -1
  78. package/dist/models.generated.d.ts +4 -0
  79. package/dist/models.generated.d.ts.map +1 -1
  80. package/dist/models.generated.js +4 -0
  81. package/dist/models.generated.js.map +1 -1
  82. package/dist/models.js +8 -8
  83. package/dist/models.js.map +1 -1
  84. package/dist/providers/all.d.ts.map +1 -1
  85. package/dist/providers/all.js +2 -0
  86. package/dist/providers/all.js.map +1 -1
  87. package/dist/providers/azure.d.ts.map +1 -1
  88. package/dist/providers/azure.js +2 -2
  89. package/dist/providers/azure.js.map +1 -1
  90. package/dist/providers/compat-schema.d.ts +15 -0
  91. package/dist/providers/compat-schema.d.ts.map +1 -1
  92. package/dist/providers/compat-schema.js +19 -1
  93. package/dist/providers/compat-schema.js.map +1 -1
  94. package/dist/providers/data/.manifest.json +1 -1
  95. package/dist/providers/data/anthropic.json +1 -1
  96. package/dist/providers/data/azure.json +1 -1
  97. package/dist/providers/data/baseten.json +1 -1
  98. package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
  99. package/dist/providers/data/fireworks.json +1 -1
  100. package/dist/providers/data/github-copilot.json +1 -1
  101. package/dist/providers/data/google-vertex.json +1 -1
  102. package/dist/providers/data/google.json +1 -1
  103. package/dist/providers/data/nvidia.json +1 -1
  104. package/dist/providers/data/openai-images.json +1 -1
  105. package/dist/providers/data/opencode-go.json +1 -1
  106. package/dist/providers/data/opencode.json +1 -1
  107. package/dist/providers/data/openrouter.json +1 -1
  108. package/dist/providers/data/qwen-token-plan-individual-cn.json +1 -0
  109. package/dist/providers/data/qwen-token-plan-individual.json +1 -1
  110. package/dist/providers/data/together.json +1 -1
  111. package/dist/providers/data/vercel-ai-gateway.json +1 -1
  112. package/dist/providers/github-copilot-capabilities.d.ts +67 -0
  113. package/dist/providers/github-copilot-capabilities.d.ts.map +1 -0
  114. package/dist/providers/github-copilot-capabilities.js +81 -0
  115. package/dist/providers/github-copilot-capabilities.js.map +1 -0
  116. package/dist/providers/github-copilot-errors.d.ts +243 -0
  117. package/dist/providers/github-copilot-errors.d.ts.map +1 -0
  118. package/dist/providers/github-copilot-errors.js +720 -0
  119. package/dist/providers/github-copilot-errors.js.map +1 -0
  120. package/dist/providers/github-copilot.d.ts +1 -2
  121. package/dist/providers/github-copilot.d.ts.map +1 -1
  122. package/dist/providers/github-copilot.js +29 -4
  123. package/dist/providers/github-copilot.js.map +1 -1
  124. package/dist/providers/openai-codex.js +1 -1
  125. package/dist/providers/openai-codex.js.map +1 -1
  126. package/dist/providers/qwen-token-plan-individual-cn.d.ts +3 -0
  127. package/dist/providers/qwen-token-plan-individual-cn.d.ts.map +1 -0
  128. package/dist/providers/qwen-token-plan-individual-cn.js +15 -0
  129. package/dist/providers/qwen-token-plan-individual-cn.js.map +1 -0
  130. package/dist/providers/qwen-token-plan-individual-cn.models.d.ts +5 -0
  131. package/dist/providers/qwen-token-plan-individual-cn.models.d.ts.map +1 -0
  132. package/dist/providers/qwen-token-plan-individual-cn.models.js +8 -0
  133. package/dist/providers/qwen-token-plan-individual-cn.models.js.map +1 -0
  134. package/dist/types.d.ts +29 -3
  135. package/dist/types.d.ts.map +1 -1
  136. package/dist/types.js.map +1 -1
  137. package/dist/utils/error-body.d.ts +8 -0
  138. package/dist/utils/error-body.d.ts.map +1 -1
  139. package/dist/utils/error-body.js +28 -1
  140. package/dist/utils/error-body.js.map +1 -1
  141. package/dist/utils/model-operations.d.ts.map +1 -1
  142. package/dist/utils/model-operations.js +6 -0
  143. package/dist/utils/model-operations.js.map +1 -1
  144. package/dist/utils/overflow.d.ts +4 -0
  145. package/dist/utils/overflow.d.ts.map +1 -1
  146. package/dist/utils/overflow.js +16 -3
  147. package/dist/utils/overflow.js.map +1 -1
  148. package/dist/utils/provider-retry.d.ts +2 -0
  149. package/dist/utils/provider-retry.d.ts.map +1 -1
  150. package/dist/utils/provider-retry.js +2 -0
  151. package/dist/utils/provider-retry.js.map +1 -1
  152. package/dist/utils/retry.d.ts +3 -1
  153. package/dist/utils/retry.d.ts.map +1 -1
  154. package/dist/utils/retry.js +9 -0
  155. package/dist/utils/retry.js.map +1 -1
  156. package/dist/utils/thinking-off.d.ts +23 -0
  157. package/dist/utils/thinking-off.d.ts.map +1 -0
  158. package/dist/utils/thinking-off.js +51 -0
  159. package/dist/utils/thinking-off.js.map +1 -0
  160. package/dist/utils/version.d.ts +1 -1
  161. package/dist/utils/version.js +1 -1
  162. package/dist/utils/version.js.map +1 -1
  163. package/package.json +9 -1
package/CHANGELOG.md CHANGED
@@ -4,12 +4,74 @@ This package is a Bastani fork of `@earendil-works/pi-ai`. Upstream history at t
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [0.9.72-alpha.1] - 2026-11-08
8
+
9
+ GPT Image 2.6, typed Copilot failures, and thinking `off` that every provider honors.
10
+
11
+ - **GPT Image 2.6.** `gpt-image-2.6-sunburst` and `gpt-image-2.6-flare` generate animated GIF and SVG output and markup-guided edits, and invalid requests fail locally with a typed error naming the field ([#4332](https://github.com/bastani-inc/atomic/issues/4332)).
12
+ - **Typed GitHub Copilot failures.** Every failed Copilot request carries a typed error with a stable code, so retries, fallback and compaction no longer depend on message text; Gemini on Copilot keeps its reasoning and counts its reasoning tokens ([#4324](https://github.com/bastani-inc/atomic/issues/4324)).
13
+ - **Thinking off at the lowest level.** Models that cannot turn thinking off run at their lowest supported level, with a `thinking_off_fallback` diagnostic, and Azure and Gemini models offer only levels they accept ([#4545](https://github.com/bastani-inc/atomic/issues/4545), [#4552](https://github.com/bastani-inc/atomic/issues/4552), [#4556](https://github.com/bastani-inc/atomic/issues/4556)).
14
+ - **Upstream pi fixes.** Connection errors keep their cause, Responses requests send `session-id`, Azure GPT-6 models get their 1,050,000-token windows, and new compat flags cover stricter proxies (upstream [#11783](https://github.com/earendil-works/pi/pull/11783), [#11793](https://github.com/earendil-works/pi/pull/11793), [#11781](https://github.com/earendil-works/pi/pull/11781), [#9442](https://github.com/earendil-works/pi/pull/9442)).
15
+
16
+ **Breaking change:** If a proxy requires `tools` once the history has tool calls, such as older LiteLLM in front of Anthropic, set `compat.requiresToolsWithToolHistory: true` on that model.
17
+
18
+ ### Breaking Changes
19
+
20
+ - `openai-completions` no longer sends `"tools": []` when a request has no tools but the conversation contains tool calls or tool results; the field is omitted. This fixes 400 errors (`tools must not be an empty array`) from vLLM 0.20+, also behind LiteLLM. Proxies that require `tools` once the history has tool calls, such as older LiteLLM versions in front of Anthropic, now reject these requests; set `compat.requiresToolsWithToolHistory: true` on those models to send the empty array again (upstream [#11891](https://github.com/earendil-works/pi/pull/11891))
21
+
22
+ ### Added
23
+
24
+ - Added the GPT Image 2.6 models `gpt-image-2.6-sunburst` and `gpt-image-2.6-flare` to the `openai-images` provider, priced at $5 text input, $1.25 cached input and $30 image output per million tokens ([#4332](https://github.com/bastani-inc/atomic/issues/4332)).
25
+ - `OpenAIImagesOptions` supports GPT Image 2.6: `outputFormat` (`png`, `jpeg`, `webp`, `svg`, `gif`) with `outputCompression`, animated GIFs with `frameCount` (2-16) and `frameDurationMs`, SVG output with the authoring `svg` model, `complexity`, `moderation` and `safetyIdentifier`. Outputs carry `image/gif` and `image/svg+xml` MIME types. `OPENAI_IMAGES_LIMITS` exports the documented bounds. A live request with 17 GIF frames was rejected, so the maximum is 16, as the API reference says, not the guide's 64 ([#4332](https://github.com/bastani-inc/atomic/issues/4332)).
26
+ - Image blocks in an image request accept `role: "markup"` (a transparent annotation layer for markup-guided edits) or `role: "mask"`. The first image without a role is the base image and the others are references ([#4332](https://github.com/bastani-inc/atomic/issues/4332)).
27
+ - `ImageModel.capabilities` declares the optional request features a model supports (`complexity`, `markup`, `svg`, `gif`). The GPT Image 2.6 models declare all four and the 2.5 models declare `markup`.
28
+ - Requests that cannot succeed fail before anything is sent with an `ImageGenerationConfigError` naming the offending `field`: values out of range, GIF or SVG on an edit, frame options without GIF, SVG with `n` above 1, compression with `png`, `gif` or `svg`, markup with a mask, and features the model does not declare. The checks also cover the request an `onPayload` hook returns or changes in place. Each failure has a `reason` (`out-of-range`, `requires-option`, `conflicts-with-option`, `generation-only`, `unsupported-by-model`, `requires-base-image`, `duplicate-role`), documented in the README ([#4332](https://github.com/bastani-inc/atomic/issues/4332)).
29
+ - Typed image-generation errors: `ImageGenerationError` and its subclasses for configuration, API, quota, rate-limit, server, user (`image_generation_user_error`), moderation (`moderation_blocked`, with `moderationStage` and `categories`) and timeout failures, each with a stable `code`, `retryable`, a `hint`, guards that check the `name` and `code` of the value itself, `findImageGenerationError(error, guard)` to search a `cause` chain, and `imageGenerationErrorFromData()` to rebuild one, with its typed cause chain, from `AssistantImages.error` after it crossed a process boundary. Typed errors carry the HTTP status and the provider's type, code and request ID, never the provider's message, which can quote the prompt. Cause chains retain up to eight links, including foreign wrappers and known errno codes; foreign messages are replaced with `[foreign error message omitted]` ([#4332](https://github.com/bastani-inc/atomic/issues/4332)).
30
+ - `github-copilot` request failures are typed. Every failed Copilot request on Chat Completions, Responses or Messages carries a `GitHubCopilotError` subclass in its assistant message's `diagnostics`, read back with `getGitHubCopilotError(message)`. Each has a stable `code`, `retryable`, a `hint`, structured context and bounded causes. Guards, `findGitHubCopilotError()`, `classifyGitHubCopilotFailure()` and serialization helpers are exported. Retry, fallback and compaction decisions use the typed code ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
31
+ - `describeGitHubCopilotModel(model)` returns a Copilot model's capabilities from its metadata: API, thinking levels and reasoning replay, tool calls, usage, images and limits ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
32
+ - Added the `requiresNonNullAssistantContent` OpenAI Completions compat flag for providers that reject `null` assistant content: replayed assistant messages with no text, such as tool-call-only messages, then send `""` (upstream [#8118](https://github.com/earendil-works/pi/pull/8118))
33
+ - Added the `promptCacheKeyMode` compat option (`auto`, `enabled`, `disabled`) for OpenAI Completions and Responses models. `enabled` sends `prompt_cache_key` to compatible proxies whenever caching is active, independently of long cache retention; `disabled` never sends it, for proxies that reject it. `auto` keeps each API's previous behavior, and `cacheRetention: "none"` still sends no key (upstream [#9442](https://github.com/earendil-works/pi/pull/9442))
34
+ - Added the `qwen-token-plan-individual-cn` provider: the Qwen Token Plan Individual catalog on the China endpoint, using `QWEN_TOKEN_PLAN_CN_API_KEY` (upstream [#7989](https://github.com/earendil-works/pi/pull/7989))
35
+ - Added `@bastani/pi-ai/lazy-oauth` (`registerLazyOAuthFlows()`) and `@bastani/pi-ai/lazy-bedrock-provider` (`registerLazyBedrockProvider()`, built on the new `setBedrockProviderLoader()`) for Node bundles that split at dynamic imports: the OAuth login flows and Bedrock load through literal dynamic imports, so the bundler keeps them inside the bundle as chunks that load on first use, instead of through the variable-specifier imports that load pi-ai's own files at runtime. Nothing changes without calling them (upstream [`da95cd79`](https://github.com/earendil-works/pi/commit/da95cd79c6adc4f273dee84b26f2255b4f215658))
36
+
37
+ ### Changed
38
+
39
+ - `openai-images` no longer retries a quota-exhausted HTTP 429 when `maxRetries` is set; rate-limit and server errors are still retried ([#4332](https://github.com/bastani-inc/atomic/issues/4332)).
40
+ - The bundled `github-copilot` catalog picks each model's API from its models.dev SDK metadata (Anthropic SDK: Messages, OpenAI SDK: Responses, otherwise Chat Completions) instead of from model ID prefixes. The generated catalog is unchanged ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
41
+ - A discovered `github-copilot` model sends strict tool schemas only when its entry lists `structured_outputs`, and takes its images per request and a resize cap from the entry's `limits.vision` ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
42
+ - A `github-copilot` Chat Completions or Responses turn that completes with no text and no tool call now fails with a retryable `empty-response` error instead of finishing empty ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
43
+ - The `openai-codex` provider is named "OpenAI Codex" again instead of "OpenAI Codex (legacy)" (upstream [`36142c1c`](https://github.com/earendil-works/pi/commit/36142c1c7eab1d756e977b5f39149a634cc472ac))
44
+
45
+ ### Fixed
46
+
47
+ - Image requests reject transparent JPEG, GIF and SVG output locally, and an `onPayload` hook cannot leave a markup or mask image without a base image. Rebuilt API errors preserve explicit `retryable` overrides ([#4332](https://github.com/bastani-inc/atomic/issues/4332)).
48
+ - `github-copilot` Gemini models keep the opaque `reasoning_opaque` value Copilot streams as a redacted thinking block and send it back on the same model's next request, so multi-turn tool loops keep their reasoning state ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
49
+ - Chat Completions usage counts a top-level `reasoning_tokens` as output when the response's `total_tokens` shows it sits outside `completion_tokens`, as on `github-copilot` Gemini models, so their output tokens, totals and cost are no longer understated ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
50
+ - Responses stream `error` and `response.failed` events keep the provider's error code on the thrown error ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
51
+ - Messages stream errors keep their structured provider type, request ID and retry hints, so a rejected Copilot request is not retried as a cut-off stream. Error guards reject malformed serialized payloads instead of rebuilding them without their context ([#4324](https://github.com/bastani-inc/atomic/issues/4324))
52
+ - Fixed Azure GPT-6 Luna, Sol, GPT-6.1 Sol and GPT-6.1 Terra using a 272K context window instead of the 1,050,000 tokens Azure Foundry deploys them with, which made sessions compact far earlier than needed (upstream [#11781](https://github.com/earendil-works/pi/pull/11781))
53
+ - Fixed `qwen-token-plan-individual` missing `glm-5.3`, which `qwen-token-plan` and `qwen-token-plan-cn` already list (upstream [#11785](https://github.com/earendil-works/pi/pull/11785))
54
+ - Fixed Claude Sonnet 5 requests failing with a 400 when a temperature was set: Sonnet 5 rejects non-default `temperature`, like Sonnet 5.5, so it is no longer sent (upstream [#7961](https://github.com/earendil-works/pi/pull/7961))
55
+ - Connection errors keep their underlying cause: a request that fails before the provider answers (connection refused or reset, DNS, TLS or certificate, proxy) now reports, for example, `Connection error.: connect ECONNREFUSED 127.0.0.1:8080` instead of only `Connection error.`. The message gets the deepest error of the `cause` chain (up to 5 levels deep) and its code (upstream [#11783](https://github.com/earendil-works/pi/pull/11783))
56
+ - OpenAI Responses requests send the session-affinity header as `session-id` instead of `session_id`. Proxies such as nginx with `underscores_in_headers off` silently dropped the underscored header, and Envoy can be set to reject it. `x-client-request-id` and `prompt_cache_key` are unchanged, and OpenAI Completions still sends `session_id` (upstream [#11793](https://github.com/earendil-works/pi/pull/11793))
57
+ - A request aborted while its setup runs (credential resolution, lazy module loading) now ends with stop reason `aborted` instead of `error`. `lazyStream(model, setup, signal?)` takes the request's abort signal, and `lazyApi`, providers and `Models` pass it through (upstream [#8635](https://github.com/earendil-works/pi/pull/8635))
58
+ - Fixed auto-retry not firing when a gateway reports a stream that dropped mid-reply as `... was cut short after the reply had begun`; that wording is now a retryable provider error (upstream [#11885](https://github.com/earendil-works/pi/pull/11885))
59
+ - Thinking `off` no longer leaves a thinking model at its provider's default effort. On a model whose `thinkingLevelMap.off` is `null`, Anthropic Messages, OpenAI Responses, Azure OpenAI Responses and OpenAI Codex Responses requests run at the model's lowest supported level, the same request as selecting that level, where Claude Opus 5.5 and Sonnet 5.5 used to get a final `high` effort marker and Claude Fable 5, OpenAI o-series and GPT-5 models, Grok and `github-copilot` models omitted the setting. On the Responses APIs the fallback needs that level's effort listed in the model's thinking map. The generated `azure` catalog now copies the efforts the matching `openai` model lists, except `xhigh` and `max`, so Azure GPT-5.1 and later send `none` for `off` and Azure GPT-5, GPT-5 mini and GPT-5 nano fall back to `minimal`. Azure GPT-5.1 and later now offer `off`; their other levels are unchanged. The reply carries a `thinking_off_fallback` diagnostic with the level and provider value sent, as do Gemini requests that already used the lowest level. `github-copilot` Responses models whose thinking map lists an off effort, such as GPT-6 Luna, are now sent it ([#4545](https://github.com/bastani-inc/atomic/issues/4545))
60
+ - A request without a reasoning level now runs a model that cannot turn thinking off at its lowest supported level on Amazon Bedrock, OpenAI Chat Completions and Mistral, with the same `thinking_off_fallback` diagnostic, instead of leaving the effort to the provider's default or sending a disabled toggle the model does not support. This covers Claude Fable 5 and 5.1 and GPT-6 Astra and GPT-6.1 Sol on Bedrock, and Chat Completions and Mistral models such as OpenRouter `openai/gpt-5.1-codex`. On OpenAI Responses, Chat Completions and Mistral the fallback is the lowest supported level whose effort the thinking map lists, so Azure GPT-5 Pro, GPT-5.2 Pro, GPT-5.4 Pro and GPT-5.3 Codex Spark send their lowest listed effort instead of omitting `reasoning`. OpenRouter translates an effort for the routed model, so OpenRouter models whose map lists no effort, such as `openai/o3`, are sent their lowest supported level. A Chat Completions model whose map denies `off` and lists no effort is no longer sent a disabled thinking toggle. The generated `azure` catalog copies the matching `openai` model's `off` entry, so Azure o-series models and `gpt-realtime-2.1` no longer offer `off`, and a request without a reasoning level falls back to `low` or `minimal` instead of sending `none`. Gemini 2.5 Pro no longer offers `off` on `google` and `google-vertex`, and a budget-thinking Gemini model that cannot turn thinking off is sent its lowest level's thinking budget instead of `0`. Levels other than `off` send the same requests as before ([#4552](https://github.com/bastani-inc/atomic/issues/4552))
61
+ - Azure GPT-5 Pro, GPT-5.2 Pro, GPT-5.4 Pro, GPT-5.3 Codex Spark and o-series (`o1`, `o1-pro`, `o3`, `o3-mini`, `o3-pro`, `o4-mini`) deployments no longer offer thinking levels the model rejects, such as `minimal`. Where the matching `openai` model cannot turn thinking off, the generated `azure` catalog copies its `minimal`, `low` and `medium` masks, so thinking `off`, including `streamSimple` with `reasoning: "off"`, runs at the lowest accepted effort (`high` on GPT-5 Pro, `medium` on GPT-5.2 Pro and GPT-5.4 Pro, `low` on the others) instead of sending `minimal`, and an explicit level below that effort runs at it too. The Azure copy of OpenAI's efforts now takes only `off` and efforts listed under their own level name, so a remapped OpenAI effort cannot change an explicit Azure level. `gemini-pro-latest` is sent a `thinkingLevel` (`LOW`, `MEDIUM` or `HIGH`) for every level instead of `thinkingBudget: -1`, and thinking `off` runs it at `LOW`. Thinking `off` on a budget-thinking Gemini model that cannot turn thinking off uses the request's `thinkingBudgets` for its lowest level instead of the default budget; `GoogleOptions` and `GoogleVertexOptions` take `thinkingBudgets` for this ([#4556](https://github.com/bastani-inc/atomic/issues/4556))
62
+
7
63
  ## [0.9.71] - 2026-11-03
8
64
 
65
+ Copilot discovery now preserves readable model names alongside exact request IDs.
66
+
67
+ - **Account model discovery.** OAuth and API-key users get the chat models their account offers, with advertised names and exact IDs, without replacing curated catalog metadata ([#4279](https://github.com/bastani-inc/atomic/pull/4279), [#4303](https://github.com/bastani-inc/atomic/pull/4303)).
68
+ - **Anthropic conversation reuse.** Message threads send only new messages when the server can continue a session, with full-request fallback when its context changes ([#4293](https://github.com/bastani-inc/atomic/pull/4293)).
69
+ - **Tool compatibility.** Plain JSON Schema tools gain coercion and validation through pi-schema while TypeBox tools keep their existing checks ([#4300](https://github.com/bastani-inc/atomic/pull/4300), [#4301](https://github.com/bastani-inc/atomic/pull/4301)).
70
+
9
71
  ### Added
10
72
 
11
73
  - Added the text-only `github-copilot/microsoft-decision-1` classifier with OAuth and API-key support (upstream [#11679](https://github.com/earendil-works/pi/pull/11679)).
12
- - `github-copilot` discovers models from the account's `/models` list. `discoverGitHubCopilotModels(catalog, context)` reads the list with the effective OAuth or API-key credential and returns the chat models the catalog lacks, each under its exact advertised ID as both ID and name and marked `discovered: true`; `parseGitHubCopilotDiscoveredModels(response, catalog)` applies the same rules to a response. It returns `undefined`, not an empty list, when the list cannot be read (no usable credential, or a 400, 401, 403 or 404 response), so a caller keeps the models it found before. Entries with a disabled policy, hidden from the model picker, without tool-call support, or already in the catalog are skipped, so curated entries keep their metadata. Every entry, fast entries included, takes its API, limits, image input and reasoning efforts from its own row, and a Messages entry that advertises adaptive thinking gets `compat.forceAdaptiveThinking`, so the selected effort is sent. A fast entry carries fast route metadata only when its `family` names another model.
74
+ - `github-copilot` discovers models from the account's `/models` list. `discoverGitHubCopilotModels(catalog, context)` reads the list with the effective OAuth or API-key credential and returns the chat models the catalog lacks, each under its exact advertised ID, with its advertised display name or the ID when no name is provided, and marked `discovered: true`; `parseGitHubCopilotDiscoveredModels(response, catalog)` applies the same rules to a response. It returns `undefined`, not an empty list, when the list cannot be read (no usable credential, or a 400, 401, 403 or 404 response), so a caller keeps the models it found before. Entries with a disabled policy, hidden from the model picker, without tool-call support, or already in the catalog are skipped, so curated entries keep their metadata. Every entry, fast entries included, takes its API, limits, image input and reasoning efforts from its own row, and a Messages entry that advertises adaptive thinking gets `compat.forceAdaptiveThinking`, so the selected effort is sent. A fast entry carries fast route metadata only when its `family` names another model.
13
75
  - `Model.discovered` marks a model found in the account's own model list rather than in the built-in catalog.
14
76
  - `RefreshModelsContext.updateCredential` lets a provider's network refresh save account data it read, such as the model lists an availability filter checks, to the OAuth login the refresh used. Nothing is written once the store holds a different token, so a renewed login or another account is never changed.
15
77
  - Anthropic requests with a `sessionId` (and `cacheRetention` not `"none"`) now use message threads (`message-threads-2026-08-12` beta): the server continues from the previous reply and only the new messages are sent instead of the whole conversation. When anything earlier in the request changed, or the server can't continue the thread, the full request is sent. Token counts and prompt caching are unchanged. On by default only for the `anthropic` provider on `api.anthropic.com`; set `compat.supportsMessageThreads` to change that, for example for gateways that forward the `thread` field. Fast-mode requests keep their `speed`, and changing it starts a new thread; requests made with a caller-supplied `client` never use threads. `cleanupSessionResources(sessionId)` frees a session's thread state (upstream [earendil-works/pi#11545](https://github.com/earendil-works/pi/pull/11545))
@@ -23,6 +85,7 @@ This package is a Bastani fork of `@earendil-works/pi-ai`. Upstream history at t
23
85
 
24
86
  ### Fixed
25
87
 
88
+ - `github-copilot` models discovered from the account's `/models` list are named by the display name their list entry advertises (for example `Claude Sonnet 4.5`) instead of repeating their ID, and fall back to the ID when the entry has no name. IDs are unchanged, so requests still use the exact advertised ID ([#4303](https://github.com/bastani-inc/atomic/pull/4303)).
26
89
  - A plain JSON Schema tool whose schema has a `$ref` that names nothing (a missing definition, an external document), as some MCP servers send, now rejects only arguments at that place (`- extra: unresolved reference #/definitions/Missing`) instead of failing every call of the tool (upstream [`6536f801`](https://github.com/earendil-works/pi/commit/6536f80129a266c3619c044f8487c0279fc39cd9))
27
90
  - OpenAI Codex SSE retries no longer leave an `abort` listener on the caller's signal after each completed backoff (upstream [#11708](https://github.com/earendil-works/pi/issues/11708))
28
91
  - The default output limit now leaves 15% of the estimated input unused (at least 4096 tokens), so OpenRouter, which counts the input with its own tokenizer and rejects input plus `max_tokens` above the window, no longer rejects models whose output limit is close to the window with a 400 that was then treated as a context overflow. When that margin would leave less than 32768 tokens of output, only the fixed 4096 margin is kept and output is capped at 32768 (upstream [#10497](https://github.com/earendil-works/pi/issues/10497))
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @bastani/pi-ai
2
2
 
3
- Bastani-branded fork of [`@earendil-works/pi-ai`](https://www.npmjs.com/package/@earendil-works/pi-ai) from [earendil-works/pi](https://github.com/earendil-works/pi). Originally forked at **v0.84.2** (`914cf1472e715297caa30db4b9535d534a9eb718`); applicable upstream Pi AI fixes and the unified model catalog are synced through [`d6fb784c84bb428a074ba3c5c8f345ac78ac9148`](https://github.com/earendil-works/pi/commit/d6fb784c84bb428a074ba3c5c8f345ac78ac9148). `@bastani/pi-ai` publishes at the same version as Atomic. `npm run build` refreshes the models.dev catalog, same as upstream.
3
+ Bastani-branded fork of [`@earendil-works/pi-ai`](https://www.npmjs.com/package/@earendil-works/pi-ai) from [earendil-works/pi](https://github.com/earendil-works/pi). Originally forked at **v0.84.2** (`914cf1472e715297caa30db4b9535d534a9eb718`); applicable upstream Pi AI fixes and the unified model catalog are synced through [`085c84e98e7f4c950f2be606466fff4f75d8fe7c`](https://github.com/earendil-works/pi/commit/085c84e98e7f4c950f2be606466fff4f75d8fe7c). `@bastani/pi-ai` publishes at the same version as Atomic. `npm run build` refreshes the models.dev catalog, same as upstream.
4
4
 
5
5
  The public API is a drop-in replacement: install `@bastani/pi-ai` and import from `@bastani/pi-ai` instead of `@earendil-works/pi-ai`. See [NOTICE.md](NOTICE.md). This package lives in the Atomic monorepo and publishes from `.github/workflows/publish.yml`. The first npm version must be published by hand so trusted publishing can be attached.
6
6
 
@@ -67,7 +67,7 @@ Unified LLM API with provider collections, automatic auth resolution, token and
67
67
  - **OpenAI** (API key or Sign in with ChatGPT)
68
68
  - **Ant Ling**
69
69
  - **Azure OpenAI (Responses)**
70
- - **OpenAI Codex (legacy)** (ChatGPT subscription, requires OAuth, see below)
70
+ - **OpenAI Codex** (ChatGPT subscription, requires OAuth, see below)
71
71
  - **Radius** (API key or OAuth, with a dynamically refreshed gateway catalog)
72
72
  - **TypeSafe** (System One classifier API)
73
73
  - **DeepSeek**
@@ -530,6 +530,7 @@ Built-in providers resolve these env vars (Node.js; in browsers pass `apiKey` ex
530
530
  | Qwen Token Plan (existing catalog) | `QWEN_TOKEN_PLAN_API_KEY` |
531
531
  | Qwen Token Plan (Individual) | `QWEN_TOKEN_PLAN_API_KEY` |
532
532
  | Qwen Token Plan (China) | `QWEN_TOKEN_PLAN_CN_API_KEY` |
533
+ | Qwen Token Plan (Individual, China) | `QWEN_TOKEN_PLAN_CN_API_KEY` |
533
534
  | Xiaomi MiMo (API billing) | `XIAOMI_API_KEY` |
534
535
  | Xiaomi MiMo Token Plan (China) | `XIAOMI_TOKEN_PLAN_CN_API_KEY` |
535
536
  | Xiaomi MiMo Token Plan (Amsterdam) | `XIAOMI_TOKEN_PLAN_AMS_API_KEY` |
@@ -539,6 +540,8 @@ Built-in providers resolve these env vars (Node.js; in browsers pass `apiKey` ex
539
540
  `qwen-token-plan-individual` and `qwen-token-plan` share the international endpoint and
540
541
  `QWEN_TOKEN_PLAN_API_KEY`. The Individual provider exposes only the models documented for Individual
541
542
  subscriptions, while the existing provider retains its broader catalog for backward compatibility.
543
+ `qwen-token-plan-individual-cn` applies the same Individual catalog to the China endpoint and
544
+ `QWEN_TOKEN_PLAN_CN_API_KEY`.
542
545
  Stored credentials remain provider-scoped, so save the key under the provider ID you register.
543
546
 
544
547
  Amazon Bedrock resolves ambient AWS credentials (`AWS_PROFILE`, access key pairs, `AWS_BEARER_TOKEN_BEDROCK`, ECS task roles, web identity tokens); its provider-owned login flow supports bearer tokens, AWS profiles, and the existing credential chain. Vertex AI resolves either an explicit key or gcloud Application Default Credentials plus project/location, with a provider-owned login flow for API keys, ADC, and service-account files.
@@ -908,18 +911,71 @@ console.log(model.output); // ['image'] or ['image', 'text']
908
911
 
909
912
  ### OpenAI Images API
910
913
 
911
- The `openai-images` provider serves OpenAI's GPT Image models (`gpt-image-2.5-sunburst`, `gpt-image-2.5-flare`, `gpt-image-2`, `gpt-image-1.5`, `gpt-image-1`, `gpt-image-1-mini`) through the Images API. It borrows the API key stored for `openai-api`, then the one stored for `openai`, then `OPENAI_API_KEY`; a ChatGPT OAuth credential on `openai` is skipped, so with only that credential the models are unavailable and `generateImages()` returns `Provider is not configured: openai-images`. Text blocks become the `prompt`. Without image blocks the request goes to `/images/generations`; with them it goes to `/images/edits`, uploading each image block. `OpenAIImagesOptions` adds `size`, `quality`, `n`, and `background`, each sent only when set:
914
+ The `openai-images` provider serves OpenAI's GPT Image models (`gpt-image-2.6-sunburst`, `gpt-image-2.6-flare`, `gpt-image-2.5-sunburst`, `gpt-image-2.5-flare`, `gpt-image-2`, `gpt-image-1.5`, `gpt-image-1`, `gpt-image-1-mini`) through the Images API. It borrows the API key stored for `openai-api`, then the one stored for `openai`, then `OPENAI_API_KEY`; a ChatGPT OAuth credential on `openai` is skipped, so with only that credential the models are unavailable and `generateImages()` returns `Provider is not configured: openai-images`. Text blocks become the `prompt`. Without image blocks the request goes to `/images/generations`; with them it goes to `/images/edits`, uploading each image block. `OpenAIImagesOptions` adds the request fields below, each sent only when set:
912
915
 
913
916
  ```typescript
914
917
  import { getBuiltinImageModel } from '@bastani/pi-ai/providers/all';
915
918
 
916
- const flare = getBuiltinImageModel('openai-images', 'gpt-image-2.5-flare');
919
+ const flare = getBuiltinImageModel('openai-images', 'gpt-image-2.6-flare');
917
920
  const result = await models.generateImages(flare, {
918
921
  input: [{ type: 'text', text: 'A red fox in the snow, watercolor' }]
919
922
  }, { size: '1024x1024', quality: 'low' });
920
923
  ```
921
924
 
922
- Each returned image is an `ImageContent` block whose `mimeType` follows the response's `output_format`.
925
+ Each returned image is an `ImageContent` block whose `mimeType` follows the response's `output_format`: `image/png`, `image/jpeg`, `image/webp`, `image/gif` or `image/svg+xml`. An SVG block holds UTF-8 XML in `data` as base64; save a GIF with `.gif` and an SVG with `.svg`.
926
+
927
+ | Option | Sent as | Notes |
928
+ | --- | --- | --- |
929
+ | `size`, `quality`, `n`, `background` | same name | `n` is the number of images. `background: 'transparent'` needs `png` or `webp` output. |
930
+ | `outputFormat` | `output_format` | `png`, `jpeg`, `webp`, `svg` or `gif`. `svg` and `gif` need a GPT Image 2.6 model and work on generations only. |
931
+ | `outputCompression` | `output_compression` | Integer 0-100, for `jpeg` and `webp` output only. |
932
+ | `frameCount` | `frame_count` | GIF frames, an integer from 2 to 16 (`OPENAI_IMAGES_LIMITS.gifFrameCount`). The API defaults to 9. Needs `outputFormat: 'gif'`. |
933
+ | `frameDurationMs` | `frame_duration_ms` | Milliseconds per GIF frame, an integer from 20 to 2000. The API rounds to 10 ms and defaults to 200. Needs `outputFormat: 'gif'`. |
934
+ | `svg` | `svg` | `{ model, reasoning?: { effort } }`: the model that writes the SVG. Needs `outputFormat: 'svg'`. The API requires `svg.model` when SVG model selection is enabled for your account and answers `missing_required_parameter` without it. |
935
+ | `complexity` | `complexity` | `low`, `medium` or `high`: how much the model plans before rendering. GPT Image 2.6 only; the API defaults to `high`. |
936
+ | `moderation` | `moderation` | `auto` or `low`. |
937
+ | `safetyIdentifier` | `safety_identifier` | A stable, hashed end-user identifier of at most 128 characters. |
938
+
939
+ GIF and SVG output are generation-only: they cannot be combined with image blocks, SVG needs `n` of 1, and neither supports streaming, partial images or `outputCompression`. `outputFormat: 'gif'` costs more tokens as `frameCount` grows; GIF `size` is the size of each frame, and the frames must fit the model's size limits, so a small `size` with few frames can be refused (`512x512` with 3 frames worked).
940
+
941
+ **Reference images, markup and mask.** With image blocks the first image without a `role` is the base image to edit and the others are references. Set `role: 'markup'` on a transparent PNG with annotations (circles, sketches) to send it as the `markup` field, which guides the edit of the first image and must have its dimensions; set `role: 'mask'` on an image with an alpha channel to send it as the `mask` field for the first image. A markup or mask image is not counted as a reference, a request takes at most one of each, and they cannot be combined. Markup-guided edits work on GPT Image 2.5 and 2.6.
942
+
943
+ ```typescript
944
+ await models.generateImages(flare, {
945
+ input: [
946
+ { type: 'text', text: 'Recolor the vase inside the red circle to cobalt blue. Remove the circle.' },
947
+ { type: 'image', data: kitchenBase64, mimeType: 'image/webp' },
948
+ { type: 'image', data: overlayBase64, mimeType: 'image/png', role: 'markup' }
949
+ ]
950
+ }, { quality: 'medium' });
951
+ ```
952
+
953
+ **Validation.** A request that cannot succeed fails before anything is sent, with an `ImageGenerationConfigError` naming the `field`, the `reason` and what to change: a value out of range, `frameCount` or `frameDurationMs` without GIF output, GIF or SVG output on an edit, SVG with `n` above 1, `outputCompression` with anything but `jpeg` or `webp`, `svg` without SVG output, markup together with a mask, a role image without a base image, two images with the same role, and `complexity`, GIF, SVG or markup on a model that does not declare it. Models declare the optional features in `ImageModel.capabilities`; a feature that is not declared is unsupported, so a custom `openai-images` model needs `capabilities` to use them. The checks run before an `onPayload` hook sees the request and again on the request that is sent, whether the hook returns a replacement or changes it in place. `reason` is one of:
954
+
955
+ | `reason` | When it fires | What to do |
956
+ | --- | --- | --- |
957
+ | `out-of-range` | `frameCount` outside 2-16, `frameDurationMs` outside 20-2000, `outputCompression` outside 0-100 (or not an integer), or `safetyIdentifier` longer than 128 characters. The bounds are in `OPENAI_IMAGES_LIMITS`. | Use a value within the bounds. |
958
+ | `requires-option` | `frameCount` or `frameDurationMs` without `outputFormat: "gif"`, or `svg` without `outputFormat: "svg"`. `related` names the missing option. | Set the option in `related`, or remove `field`. |
959
+ | `conflicts-with-option` | SVG output with `n` above 1; `outputCompression` with `png`, `gif`, `svg` or no format; `background: "transparent"` with `jpeg`, `gif` or `svg`; a markup image together with a mask image; GIF or SVG output with streaming or partial images. | Remove `field` or the option named in `related`. |
960
+ | `generation-only` | GIF or SVG output on a request with input images, which goes to the edits endpoint. | Remove the input images, or choose `png`, `jpeg` or `webp`. |
961
+ | `unsupported-by-model` | `complexity`, GIF, SVG or markup on a model whose `capabilities` do not declare it. GPT Image 2.6 declares all four; GPT Image 2.5 declares markup; older models declare none. | Use a model that declares the feature. |
962
+ | `requires-base-image` | An image with `role: "markup"` or `role: "mask"` but no image without a role for it to apply to. | Put the base image first, without a role. |
963
+ | `duplicate-role` | Two images with `role: "markup"`, or two with `role: "mask"`. | Send at most one image per role. |
964
+
965
+ **Errors.** `generateImages()` still resolves with `stopReason: 'error'` and `errorMessage`, and now also `error`, a JSON-safe `ImageGenerationErrorData`. Rebuild the typed error with `imageGenerationErrorFromData(result.error)` and branch on its class or guard, `code` or `retryable`, never on the message. Cancellation resolves with `stopReason: 'aborted'` and no `error`. The typed error holds only the HTTP status and the provider's type, code and request ID, in `detail` and in its message; it never holds the provider's message, which can quote the prompt. `errorMessage` keeps the provider's text.
966
+
967
+ | Class (guard) | `code` | When | Retryable | Useful fields |
968
+ | --- | --- | --- | --- | --- |
969
+ | `ImageGenerationConfigError` (`isImageGenerationConfigError`) | `invalid-option` | The options or input are invalid. Nothing was sent. | No | `field`, `reason`, `related`, `model`, `hint` |
970
+ | `ImageGenerationApiError` (`isImageGenerationApiError`) | `api-error` | Any other API failure, including connection errors. | Only 408, 409 and connection errors | `status`, `apiCode`, `apiType`, `requestId` |
971
+ | `ImageGenerationQuotaError` (`isImageGenerationQuotaError`) | `quota-exceeded` | Out of quota or credit, even though it arrives as HTTP 429. | No | as `api-error` |
972
+ | `ImageGenerationRateLimitError` (`isImageGenerationRateLimitError`) | `rate-limited` | HTTP 429 rate limit. | Yes | as `api-error` |
973
+ | `ImageGenerationServerError` (`isImageGenerationServerError`) | `server-error` | HTTP 5xx. | Yes | as `api-error` |
974
+ | `ImageGenerationUserError` (`isImageGenerationUserError`) | `user-error` | `error.type` is `image_generation_user_error`: change the prompt or input images. `apiCode` is the stable discriminator. | No | as `api-error` |
975
+ | `ImageModerationBlockedError` (`isImageModerationBlockedError`) | `moderation-blocked` | `error.code` is `moderation_blocked`. | No | `moderationStage` (`input`, `output`, `unknown`), `categories` |
976
+ | `ImageGenerationTimeoutError` (`isImageGenerationTimeoutError`) | `timeout` | The request exceeded `timeoutMs`. | Yes | `timeoutMs`, `elapsedMs`, `phase` |
977
+
978
+ All extend `ImageGenerationError`, and `isImageGenerationError()` matches any of them. Guards check the value itself: an instance, or an object from another process whose `name` is the class name and whose `code` is that class's code. To search a `cause` chain (at most eight links), use `findImageGenerationError(error, guard)`, for example `findImageGenerationError(error, isImageGenerationQuotaError)`; `isRetryableImageGenerationError()` answers whether the nearest one in the chain is retryable. `result.error` keeps up to eight cause-chain links: family causes retain their data, while foreign causes retain a safe name and known errno code, if any, with their message replaced by `[foreign error message omitted]`. Rehydration preserves these links and codes. With `maxRetries`, rate-limit and server errors are retried; quota and user errors never are. Usage from an SVG request covers the image model only; OpenAI reports the tokens of the SVG-authoring model separately (`svg_usage`), and they are not added to `usage` or its cost.
923
979
 
924
980
  ### Notes and Limitations
925
981
 
@@ -1460,6 +1516,8 @@ interface OpenAICompletionsCompat {
1460
1516
  maxTokensField?: 'max_completion_tokens' | 'max_tokens'; // Which field name to use (default: max_completion_tokens)
1461
1517
  requiresToolResultName?: boolean; // Whether tool results require the `name` field (default: false)
1462
1518
  requiresAssistantAfterToolResult?: boolean; // Whether tool results must be followed by an assistant message (default: false)
1519
+ requiresNonNullAssistantContent?: boolean; // Whether replayed assistant messages with no text must send "" instead of null content (default: false)
1520
+ requiresToolsWithToolHistory?: boolean; // Whether requests without tools must send `"tools": []` when the conversation has tool calls or tool results, as some proxies in front of Anthropic need (default: false, the field is omitted)
1463
1521
  requiresThinkingAsText?: boolean; // Whether thinking blocks must be converted to text (default: false)
1464
1522
  requiresReasoningContentOnAssistantMessages?: boolean; // Whether all replayed assistant messages must include empty reasoning_content when reasoning is enabled (default: auto-detected for DeepSeek)
1465
1523
  thinkingFormat?: 'openai' | 'openrouter' | 'deepseek' | 'together' | 'baseten' | 'zai' | 'qwen' | 'chat-template' | 'qwen-chat-template' | 'string-thinking' | 'ant-ling'; // Format for reasoning param: 'openai' uses reasoning_effort, 'openrouter' uses reasoning: { effort }, 'deepseek' uses thinking: { type } plus reasoning_effort when supported, 'together' uses reasoning: { enabled } plus reasoning_effort when supported, 'baseten' uses configurable chat_template_args plus reasoning_effort when supported, 'zai' uses thinking: { type }, 'qwen' uses enable_thinking, 'chat-template' uses configurable chat_template_kwargs, 'qwen-chat-template' uses chat_template_kwargs.enable_thinking and preserve_thinking, 'string-thinking' uses top-level thinking, 'ant-ling' uses reasoning: { effort } only for mapped efforts (default: openai)
@@ -1475,7 +1533,7 @@ interface OpenAICompletionsCompat {
1475
1533
 
1476
1534
  interface OpenAIResponsesCompat {
1477
1535
  supportsDeveloperRole?: boolean; // Whether provider supports `developer` role vs `system` (default: true)
1478
- sessionAffinityFormat?: 'openai' | 'openai-nosession' | 'openrouter'; // Session-affinity header format: 'openai' sends `session_id` and `x-client-request-id`; 'openai-nosession' sends `x-client-request-id`; 'openrouter' sends `x-session-id`. Does not affect the `prompt_cache_key` body param (default: auto-detected)
1536
+ sessionAffinityFormat?: 'openai' | 'openai-nosession' | 'openrouter'; // Session-affinity header format: 'openai' sends `session-id` and `x-client-request-id`; 'openai-nosession' sends `x-client-request-id`; 'openrouter' sends `x-session-id`. Does not affect the `prompt_cache_key` body param (default: auto-detected)
1479
1537
  supportsLongCacheRetention?: boolean; // Whether provider supports `prompt_cache_retention: "24h"` (default: true)
1480
1538
  supportsStrictMode?: boolean; // Whether provider supports strict JSON-schema function tools (default: false; enabled in metadata for built-in OpenAI models)
1481
1539
  supportsOpenAIGrammarTools?: boolean; // Whether to emit OpenAI custom Lark/regex grammar tools; false falls back to normal function tools (default: false; the generated catalog enables it for capable models)
@@ -1760,6 +1818,18 @@ setBedrockProviderModule(bedrockProviderModule);
1760
1818
 
1761
1819
  That explicit override bundles the AWS SDK. Without it, Bedrock's opaque runtime import expects the package's Bedrock implementation file to be available at runtime.
1762
1820
 
1821
+ Node bundles that split at dynamic imports (esbuild `splitting: true`, `bun build --splitting`) can keep Bedrock and the OAuth login flows inside the bundle but lazy: register loaders with literal dynamic imports, which the bundler turns into chunks that load on first use:
1822
+
1823
+ ```typescript
1824
+ import { registerLazyBedrockProvider } from '@bastani/pi-ai/lazy-bedrock-provider';
1825
+ import { registerLazyOAuthFlows } from '@bastani/pi-ai/lazy-oauth';
1826
+
1827
+ registerLazyBedrockProvider();
1828
+ registerLazyOAuthFlows();
1829
+ ```
1830
+
1831
+ Both modules are Node-only; never import them into a browser bundle.
1832
+
1763
1833
  ### Provider-Scoped Environment Overrides
1764
1834
 
1765
1835
  Pass `env` in stream options to scope provider configuration to a request. Values in `env` are used before process environment variables for provider auth and configuration such as Cloudflare account IDs, Azure OpenAI settings, Vertex project/location, Bedrock settings, `PI_CACHE_RETENTION`, and `HTTP_PROXY`/`HTTPS_PROXY`.
@@ -1785,7 +1855,7 @@ Several providers support OAuth authentication instead of static API keys:
1785
1855
 
1786
1856
  - **Anthropic** (Claude Pro/Max subscription)
1787
1857
  - **OpenAI** (Sign in with ChatGPT, direct Responses API access)
1788
- - **OpenAI Codex (legacy)** (ChatGPT subscription, Codex Responses access)
1858
+ - **OpenAI Codex** (ChatGPT subscription, Codex Responses access)
1789
1859
  - **GitHub Copilot** (Copilot subscription)
1790
1860
  - **OpenRouter** (OAuth PKCE that mints a user-controlled API key)
1791
1861
 
@@ -1867,7 +1937,7 @@ Built-in login and refresh flows are private provider implementations. Use provi
1867
1937
 
1868
1938
  Provider notes:
1869
1939
 
1870
- **OpenAI Codex (legacy)**: Requires a ChatGPT subscription with model access. Includes GPT-6.1 Sol and Codex models with extended context windows and reasoning capabilities. The library handles session-based prompt caching when `sessionId` is provided unless `cacheRetention` is `"none"`. Set `transport` to `"sse"`, `"websocket"`, or `"auto"` for Codex Responses transport selection. WebSocket connections with a `sessionId` and caching enabled are reused per session and expire after 5 minutes of inactivity.
1940
+ **OpenAI Codex**: Requires a ChatGPT subscription with model access. Includes GPT-6.1 Sol and Codex models with extended context windows and reasoning capabilities. The library handles session-based prompt caching when `sessionId` is provided unless `cacheRetention` is `"none"`. Set `transport` to `"sse"`, `"websocket"`, or `"auto"` for Codex Responses transport selection. WebSocket connections with a `sessionId` and caching enabled are reused per session and expire after 5 minutes of inactivity.
1871
1941
 
1872
1942
  Call `cleanupSessionResources(sessionId)` when finished with a Codex session so its pooled WebSocket connection does not keep the process alive. Import it from `@bastani/pi-ai`.
1873
1943
 
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-messages.ts"],"names":[],"mappings":"AAAA,OAAO,SAAkD,MAAM,mBAAmB,CAAC;AAkBnF,OAAO,KAAK,EASX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAKb,MAAM,aAAa,CAAC;AAoLrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAkfhE,MAAM,WAAW,gBAAiB,SAAQ,aAAa;IACtD;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB;;;;;;;;;;;OAWG;IACH,eAAe,CAAC,EAAE,wBAAwB,CAAC;IAC3C;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACtE;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB;AAsTD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CAsazE,CAAC;AA4BF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,oBAAoB,EAAE,mBAAmB,CAgDlF,CAAC"}
1
+ {"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-messages.ts"],"names":[],"mappings":"AAAA,OAAO,SAAkD,MAAM,mBAAmB,CAAC;AAoBnF,OAAO,KAAK,EASX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAMb,MAAM,aAAa,CAAC;AAsLrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAkfhE,MAAM,WAAW,gBAAiB,SAAQ,aAAa;IACtD;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB;;;;;;;;;;;OAWG;IACH,eAAe,CAAC,EAAE,wBAAwB,CAAC;IAC3C;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACtE;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB;AAqUD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CA6azE,CAAC;AA+CF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,oBAAoB,EAAE,mBAAmB,CAgDlF,CAAC"}
@@ -1,10 +1,12 @@
1
1
  import Anthropic, {} from "@anthropic-ai/sdk";
2
2
  import { ANTHROPIC_FEDERATION_RULE_ID_ENV, ANTHROPIC_IDENTITY_TOKEN_FILE_ENV, ANTHROPIC_ORGANIZATION_ID_ENV, ANTHROPIC_SERVICE_ACCOUNT_ID_ENV, ANTHROPIC_WORKSPACE_ID_ENV, } from "../env-api-keys.js";
3
3
  import { calculateCost } from "../models.js";
4
+ import { recordGitHubCopilotFailure } from "../providers/github-copilot-errors.js";
4
5
  import { registerSessionResourceCleanup } from "../session-resources.js";
5
6
  import { raceWithAbortSignal } from "../utils/abort.js";
6
7
  import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.js";
7
8
  import { assertSupportedDocumentMimeType } from "../utils/document-input.js";
9
+ import { withCauseDetail } from "../utils/error-body.js";
8
10
  import { AssistantMessageEventStream } from "../utils/event-stream.js";
9
11
  import { shortHash } from "../utils/hash.js";
10
12
  import { headersToRecord } from "../utils/headers.js";
@@ -19,6 +21,7 @@ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
19
21
  import { createStreamDeadline, withStreamDeadline } from "../utils/stream-deadline.js";
20
22
  import { ToolCallArgumentsStream } from "../utils/streaming-json.js";
21
23
  import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.js";
24
+ import { appendThinkingOffFallbackDiagnostic, getThinkingOffFallbackLevel } from "../utils/thinking-off.js";
22
25
  import { getCurrentTools, getInitialSystemMessage, resolveTranscript, } from "../utils/transcript.js";
23
26
  import { convertAnthropicAssistantContent } from "./anthropic-assistant-content.js";
24
27
  import { ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE, getJsonSchemaToolParameters, isStrictToolLimitErrorMessage, prefersJsonSchemaStrictSampling, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
@@ -753,6 +756,19 @@ async function* iterateSseMessages(body, signal) {
753
756
  reader.releaseLock();
754
757
  }
755
758
  }
759
+ function parseStreamedErrorBody(data) {
760
+ try {
761
+ return JSON.parse(data) ?? data;
762
+ }
763
+ catch {
764
+ return data;
765
+ }
766
+ }
767
+ function streamedErrorType(body) {
768
+ const error = body.error;
769
+ const type = typeof error === "object" && error !== null ? error.type : undefined;
770
+ return typeof type === "string" ? type : undefined;
771
+ }
756
772
  async function* iterateAnthropicEvents(response, signal) {
757
773
  if (!response.body) {
758
774
  throw new Error("Attempted to iterate over an Anthropic response with no body");
@@ -761,7 +777,8 @@ async function* iterateAnthropicEvents(response, signal) {
761
777
  let sawMessageEnd = false;
762
778
  for await (const sse of iterateSseMessages(response.body, signal)) {
763
779
  if (sse.event === "error") {
764
- throw new Error(sse.data);
780
+ const body = parseStreamedErrorBody(sse.data);
781
+ throw new Anthropic.APIError(undefined, body, sse.data, response.headers, streamedErrorType(body));
765
782
  }
766
783
  if (!ANTHROPIC_MESSAGE_EVENTS.has(sse.event ?? "")) {
767
784
  continue;
@@ -809,8 +826,13 @@ export const stream = (model, context, options) => {
809
826
  timestamp: Date.now(),
810
827
  };
811
828
  const streamDeadline = createStreamDeadline(options?.streamDeadlineMs, options?.signal);
829
+ let streamStarted = false;
812
830
  let inputTransformations;
813
831
  try {
832
+ const thinkingOffFallback = getAnthropicThinkingOffFallback(model, options);
833
+ if (thinkingOffFallback) {
834
+ appendThinkingOffFallbackDiagnostic(output, thinkingOffFallback.level, thinkingOffFallback.effort);
835
+ }
814
836
  let client;
815
837
  let isOAuth;
816
838
  let usageModel = model;
@@ -906,6 +928,7 @@ export const stream = (model, context, options) => {
906
928
  }
907
929
  await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
908
930
  stream.push({ type: "start", partial: output });
931
+ streamStarted = true;
909
932
  // Stream state per Anthropic block index, kept off the content blocks so it never reaches partials.
910
933
  const openBlocks = new Map();
911
934
  const openBlock = (anthropicIndex) => {
@@ -1161,8 +1184,10 @@ export const stream = (model, context, options) => {
1161
1184
  recordInputTransformations(output, inputTransformations);
1162
1185
  }
1163
1186
  output.stopReason = options?.signal?.aborted ? "aborted" : "error";
1164
- output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
1187
+ output.errorMessage = error instanceof Error ? withCauseDetail(error) : JSON.stringify(error);
1165
1188
  output.retryAfterMs = error instanceof Error ? getProviderRetryAfterMs(error) : undefined;
1189
+ if (model.provider === "github-copilot")
1190
+ recordGitHubCopilotFailure(output, error, streamStarted);
1166
1191
  stream.push({ type: "error", reason: output.stopReason, error: output });
1167
1192
  stream.end();
1168
1193
  }
@@ -1194,6 +1219,18 @@ function mapThinkingLevelToEffort(model, level) {
1194
1219
  return "high";
1195
1220
  }
1196
1221
  }
1222
+ /**
1223
+ * Thinking `off` on an adaptive model that cannot disable thinking runs at the model's lowest effort.
1224
+ * Sending no effort would leave the API's default, which is `high`.
1225
+ */
1226
+ function getAnthropicThinkingOffFallback(model, options) {
1227
+ if (options?.thinkingEnabled !== false)
1228
+ return undefined;
1229
+ if (model.compat?.forceAdaptiveThinking !== true && !supportsMidConvoEffort(model))
1230
+ return undefined;
1231
+ const level = getThinkingOffFallbackLevel(model);
1232
+ return level === undefined ? undefined : { level, effort: mapThinkingLevelToEffort(model, level) };
1233
+ }
1197
1234
  export const streamSimple = (model, context, options) => {
1198
1235
  if (!getAnthropicFederation(model, options?.apiKey, options?.headers, options?.env)) {
1199
1236
  assertRequestAuth(model.provider, options?.apiKey, options?.headers);
@@ -1387,11 +1424,12 @@ function buildParams(model, context, isOAuthToken, options, { disablePreferredSt
1387
1424
  };
1388
1425
  const initialTools = initialSystemMessage?.toolsAdded ?? [];
1389
1426
  const nativeToolChanges = compat.supportsMidConvoSystemMessages && compat.supportsMidConvoToolChanges && initialTools.length > 0;
1427
+ const thinkingOffFallback = getAnthropicThinkingOffFallback(model, options);
1390
1428
  const thinkingDisabled = model.reasoning && options?.thinkingEnabled === false && model.thinkingLevelMap?.off !== null;
1391
1429
  // Effort cannot change between messages while thinking is disabled, so "off" sends no effort markers.
1392
1430
  const effortMessages = supportsMidConvoEffort(model) && !thinkingDisabled;
1393
1431
  const converted = convertMessages(conversationMessages, isOAuthToken, cacheControl, compat.allowEmptySignature, effortMessages ? model.provider : undefined, nativeToolChanges ? convertToolDefinitions : undefined);
1394
- const activeEffort = options?.effort ?? "high";
1432
+ const activeEffort = options?.effort ?? thinkingOffFallback?.effort ?? "high";
1395
1433
  const params = {
1396
1434
  model: upstreamModelId(model),
1397
1435
  messages: (effortMessages
@@ -1435,6 +1473,7 @@ function buildParams(model, context, isOAuthToken, options, { disablePreferredSt
1435
1473
  // Temperature is incompatible with extended thinking and unsupported on Claude Opus 4.7+.
1436
1474
  if (options?.temperature !== undefined &&
1437
1475
  !options?.thinkingEnabled &&
1476
+ !thinkingOffFallback &&
1438
1477
  !supportsMidConvoEffort(model) &&
1439
1478
  compat.supportsTemperature) {
1440
1479
  params.temperature = options.temperature;
@@ -1462,26 +1501,27 @@ function buildParams(model, context, isOAuthToken, options, { disablePreferredSt
1462
1501
  params.output_config = { effort: "low" };
1463
1502
  }
1464
1503
  else if (model.reasoning) {
1465
- if (options?.thinkingEnabled) {
1504
+ if (options?.thinkingEnabled || thinkingOffFallback) {
1466
1505
  // Default to "summarized" so Opus 4.7 and Mythos Preview behave like
1467
1506
  // older Claude 4 models (whose API default is also "summarized").
1468
- const display = options.thinkingDisplay ?? "summarized";
1507
+ const display = options?.thinkingDisplay ?? "summarized";
1508
+ const effort = options?.effort ?? thinkingOffFallback?.effort;
1469
1509
  if (model.compat?.forceAdaptiveThinking === true) {
1470
1510
  // Adaptive thinking: Claude decides when and how much to think.
1471
1511
  params.thinking = { type: "adaptive", display };
1472
- if (options.effort) {
1512
+ if (effort) {
1473
1513
  // The Anthropic SDK types can lag newly supported effort values such as "xhigh".
1474
1514
  params.output_config =
1475
- options.effort === "xhigh"
1476
- ? { effort: options.effort }
1477
- : { effort: options.effort };
1515
+ effort === "xhigh"
1516
+ ? { effort }
1517
+ : { effort };
1478
1518
  }
1479
1519
  }
1480
1520
  else {
1481
1521
  // Budget-based thinking for older models
1482
1522
  params.thinking = {
1483
1523
  type: "enabled",
1484
- budget_tokens: options.thinkingBudgetTokens || 1024,
1524
+ budget_tokens: options?.thinkingBudgetTokens || 1024,
1485
1525
  display,
1486
1526
  };
1487
1527
  }