@bastani/pi-ai 0.9.70 → 0.9.71-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 (55) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +5 -2
  3. package/dist/api/anthropic-messages.d.ts.map +1 -1
  4. package/dist/api/anthropic-messages.js +249 -17
  5. package/dist/api/anthropic-messages.js.map +1 -1
  6. package/dist/api/github-copilot-headers.d.ts +11 -0
  7. package/dist/api/github-copilot-headers.d.ts.map +1 -1
  8. package/dist/api/github-copilot-headers.js +18 -1
  9. package/dist/api/github-copilot-headers.js.map +1 -1
  10. package/dist/api/openai-codex-responses.d.ts.map +1 -1
  11. package/dist/api/openai-codex-responses.js +7 -3
  12. package/dist/api/openai-codex-responses.js.map +1 -1
  13. package/dist/api/simple-options.d.ts +1 -0
  14. package/dist/api/simple-options.d.ts.map +1 -1
  15. package/dist/api/simple-options.js +16 -2
  16. package/dist/api/simple-options.js.map +1 -1
  17. package/dist/auth/oauth/github-copilot.d.ts.map +1 -1
  18. package/dist/auth/oauth/github-copilot.js +4 -58
  19. package/dist/auth/oauth/github-copilot.js.map +1 -1
  20. package/dist/models.d.ts +8 -1
  21. package/dist/models.d.ts.map +1 -1
  22. package/dist/models.js +12 -0
  23. package/dist/models.js.map +1 -1
  24. package/dist/providers/compat-schema.d.ts +3 -0
  25. package/dist/providers/compat-schema.d.ts.map +1 -1
  26. package/dist/providers/compat-schema.js +3 -0
  27. package/dist/providers/compat-schema.js.map +1 -1
  28. package/dist/providers/data/.manifest.json +1 -1
  29. package/dist/providers/data/github-copilot.json +1 -1
  30. package/dist/providers/data/opencode-go.json +1 -1
  31. package/dist/providers/data/opencode.json +1 -1
  32. package/dist/providers/data/openrouter.json +1 -1
  33. package/dist/providers/github-copilot-account.d.ts +18 -0
  34. package/dist/providers/github-copilot-account.d.ts.map +1 -0
  35. package/dist/providers/github-copilot-account.js +57 -0
  36. package/dist/providers/github-copilot-account.js.map +1 -0
  37. package/dist/providers/github-copilot.d.ts +32 -2
  38. package/dist/providers/github-copilot.d.ts.map +1 -1
  39. package/dist/providers/github-copilot.js +263 -7
  40. package/dist/providers/github-copilot.js.map +1 -1
  41. package/dist/types.d.ts +18 -1
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/types.js.map +1 -1
  44. package/dist/utils/transcript.d.ts +1 -1
  45. package/dist/utils/transcript.js +1 -1
  46. package/dist/utils/transcript.js.map +1 -1
  47. package/dist/utils/validation.d.ts +9 -4
  48. package/dist/utils/validation.d.ts.map +1 -1
  49. package/dist/utils/validation.js +67 -19
  50. package/dist/utils/validation.js.map +1 -1
  51. package/dist/utils/version.d.ts +1 -1
  52. package/dist/utils/version.d.ts.map +1 -1
  53. package/dist/utils/version.js +1 -1
  54. package/dist/utils/version.js.map +1 -1
  55. package/package.json +5 -5
package/CHANGELOG.md CHANGED
@@ -4,6 +4,63 @@ 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.71] - 2026-11-03
8
+
9
+ ### Added
10
+
11
+ - 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.
13
+ - `Model.discovered` marks a model found in the account's own model list rather than in the built-in catalog.
14
+ - `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
+ - 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))
16
+ - `StreamOptions.disableContinuation` keeps a request from using or replacing server-side continuation state such as Anthropic message threads. Set it on requests that repeat an earlier one, such as cache warming (upstream [earendil-works/pi#11545](https://github.com/earendil-works/pi/pull/11545))
17
+
18
+ ### Changed
19
+
20
+ - `validateToolArguments()` and `validateToolCall()` validate a plain JSON Schema, one TypeBox did not build (an MCP tool's, a JSON round-tripped declaration, a schema from `@earendil-works/pi-schema`'s builders), with `@earendil-works/pi-schema` (`compile(schema, { coerce: true }).parse()`), a new dependency. Such tools get pi-schema's coercion, the superset of TypeBox's `Value.Convert` rules and pi-ai's own: `"1"`/`"0"` to `true`/`false`, `"null"` to `null`, a fractional numeric string to an integer (`"42.1"` to 42), a single value wrapped into an array, and `null` for a required string is now `"null"` instead of `""`. Their validation errors list pi-schema's messages (`- value: missing`, `- limit: expected number, got string`), and the `url` and `json-pointer-uri-fragment` formats are checked. TypeBox-built schemas keep TypeBox's conversion, checks (refinements and `Format.Set()` formats included) and messages, and `Type`, `Static`, `TSchema` and the TypeBox `StringEnum()` stay exported (upstream [`6536f801`](https://github.com/earendil-works/pi/commit/6536f80129a266c3619c044f8487c0279fc39cd9), adapted to keep TypeBox)
21
+ - `Tool.parameters` accepts any JSON Schema object: `Tool<TParameters extends object = TSchema>`. The default is still `TSchema`, so existing `Tool` and `Tool<typeof schema>` types are unchanged (upstream [`6536f801`](https://github.com/earendil-works/pi/commit/6536f80129a266c3619c044f8487c0279fc39cd9))
22
+ - `github-copilot` availability keeps fast-route models for a credential without a fast list, such as an API key, because only the account's own model list adds them. A login that lists its fast IDs offers each listed ID even when it is not a picker entry. A network refresh that reads the account's list also saves its model lists to the OAuth login that read it, so a model the refresh discovers is offered at once, while the login's lists still decide for every model, so models another login discovered are not offered.
23
+
24
+ ### Fixed
25
+
26
+ - 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
+ - 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
+ - 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))
29
+ - Anthropic non-strict tool schemas keep the root `$defs` and `definitions`, so property `$ref`s resolve. Before, Claude could send a JSON string where an object was expected, and some Anthropic-compatible gateways rejected the request. Root `anyOf`/`oneOf`/`allOf` are still left out because Anthropic rejects them (upstream [#9557](https://github.com/earendil-works/pi/issues/9557))
30
+ - OpenCode Go and Zen Claude 5.5 models, Opus 5 and Fable 5.1 now use per-message effort and send `thinking.block_binding: drop_block`, like the Anthropic provider. OpenCode Go Claude Haiku 5.5 failed with a 400 (`Invalid signature in thinking block ... bound to a different conversation`) after the tool list, system prompt or earlier history changed, and kept failing on every retry (upstream [#10954](https://github.com/earendil-works/pi/issues/10954))
31
+
32
+ ## [0.9.71-alpha.1] - 2026-11-03
33
+
34
+ Copilot discovery now preserves readable model names alongside exact request IDs.
35
+
36
+ - **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)).
37
+ - **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)).
38
+ - **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)).
39
+
40
+ ### Added
41
+
42
+ - 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)).
43
+ - `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.
44
+ - `Model.discovered` marks a model found in the account's own model list rather than in the built-in catalog.
45
+ - `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.
46
+ - 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))
47
+ - `StreamOptions.disableContinuation` keeps a request from using or replacing server-side continuation state such as Anthropic message threads. Set it on requests that repeat an earlier one, such as cache warming (upstream [earendil-works/pi#11545](https://github.com/earendil-works/pi/pull/11545))
48
+
49
+ ### Changed
50
+
51
+ - `validateToolArguments()` and `validateToolCall()` validate a plain JSON Schema, one TypeBox did not build (an MCP tool's, a JSON round-tripped declaration, a schema from `@earendil-works/pi-schema`'s builders), with `@earendil-works/pi-schema` (`compile(schema, { coerce: true }).parse()`), a new dependency. Such tools get pi-schema's coercion, the superset of TypeBox's `Value.Convert` rules and pi-ai's own: `"1"`/`"0"` to `true`/`false`, `"null"` to `null`, a fractional numeric string to an integer (`"42.1"` to 42), a single value wrapped into an array, and `null` for a required string is now `"null"` instead of `""`. Their validation errors list pi-schema's messages (`- value: missing`, `- limit: expected number, got string`), and the `url` and `json-pointer-uri-fragment` formats are checked. TypeBox-built schemas keep TypeBox's conversion, checks (refinements and `Format.Set()` formats included) and messages, and `Type`, `Static`, `TSchema` and the TypeBox `StringEnum()` stay exported (upstream [`6536f801`](https://github.com/earendil-works/pi/commit/6536f80129a266c3619c044f8487c0279fc39cd9), adapted to keep TypeBox)
52
+ - `Tool.parameters` accepts any JSON Schema object: `Tool<TParameters extends object = TSchema>`. The default is still `TSchema`, so existing `Tool` and `Tool<typeof schema>` types are unchanged (upstream [`6536f801`](https://github.com/earendil-works/pi/commit/6536f80129a266c3619c044f8487c0279fc39cd9))
53
+ - `github-copilot` availability keeps fast-route models for a credential without a fast list, such as an API key, because only the account's own model list adds them. A login that lists its fast IDs offers each listed ID even when it is not a picker entry. A network refresh that reads the account's list also saves its model lists to the OAuth login that read it, so a model the refresh discovers is offered at once, while the login's lists still decide for every model, so models another login discovered are not offered.
54
+
55
+ ### Fixed
56
+
57
+ - `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)).
58
+ - 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))
59
+ - 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))
60
+ - 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))
61
+ - Anthropic non-strict tool schemas keep the root `$defs` and `definitions`, so property `$ref`s resolve. Before, Claude could send a JSON string where an object was expected, and some Anthropic-compatible gateways rejected the request. Root `anyOf`/`oneOf`/`allOf` are still left out because Anthropic rejects them (upstream [#9557](https://github.com/earendil-works/pi/issues/9557))
62
+ - OpenCode Go and Zen Claude 5.5 models, Opus 5 and Fable 5.1 now use per-message effort and send `thinking.block_binding: drop_block`, like the Anthropic provider. OpenCode Go Claude Haiku 5.5 failed with a 400 (`Invalid signature in thinking block ... bound to a different conversation`) after the tool list, system prompt or earlier history changed, and kept failing on every retry (upstream [#10954](https://github.com/earendil-works/pi/issues/10954))
63
+
7
64
  ## [0.9.64] - 2026-11-02
8
65
 
9
66
  ### Added
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 [`1b347794e2a630e4359f2584f4eea388145d0ddf`](https://github.com/earendil-works/pi/commit/1b347794e2a630e4359f2584f4eea388145d0ddf). `@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 [`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.
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
 
@@ -934,7 +934,7 @@ Each returned image is an `ImageContent` block whose `mimeType` follows the resp
934
934
 
935
935
  ## Classification
936
936
 
937
- Classifier models consume structured JSON state and answer one or more typed questions. They do not use chat or image-generation APIs. TypeSafe's Jev model is available from these built-in providers:
937
+ Classifier models consume structured JSON state and answer one or more typed questions. They do not use chat or image-generation APIs. Built-in providers include TypeSafe's Jev and Microsoft-Decision-1:
938
938
 
939
939
  | Provider | Model IDs | Auth |
940
940
  | --- | --- | --- |
@@ -943,6 +943,7 @@ Classifier models consume structured JSON state and answer one or more typed que
943
943
  | `cloudflare-workers-ai` | `typesafe/jev`, `typesafe/jev-1.14.0`, `@cf/cloudflare/clef`, `@cf/cloudflare/clef-flash`, `@cf/cloudflare/clef-omni` | `CLOUDFLARE_API_KEY` and `CLOUDFLARE_ACCOUNT_ID` |
944
944
  | `vercel-ai-gateway` | `typesafe-ai/jev` | `AI_GATEWAY_API_KEY` |
945
945
  | `opencode` | `jev-1.13`, `jev-1.13-free` | `OPENCODE_API_KEY` |
946
+ | `github-copilot` | `microsoft-decision-1` | `COPILOT_GITHUB_TOKEN` or GitHub Copilot OAuth |
946
947
 
947
948
  ```typescript
948
949
  import { builtinModels } from '@bastani/pi-ai/providers/all';
@@ -1440,6 +1441,8 @@ Built-in API implementations live under `./api/<api-id>`:
1440
1441
 
1441
1442
  Importing an implementation module loads its SDK. The `./api/<id>.lazy` wrappers (used by the provider factories) defer that load to the first request when the runtime or bundler supports dynamic import chunking. Legacy raw API subpaths from older releases (`./anthropic`, `./google`, `./mistral`, `./openai-completions`, ...) were removed; use `@bastani/pi-ai/api/<api-id>`.
1442
1443
 
1444
+ **Anthropic message threads (beta):** with `sessionId` set and `cacheRetention` not `"none"`, Anthropic requests continue from the previous reply on the server (`message-threads-2026-08-12` beta) and send only the new messages. When anything earlier in the request changed, or the server can't continue, 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. Requests made with a caller-supplied `client` never use threads. Set `disableContinuation: true` on requests that repeat an earlier one, such as cache warming. Call `cleanupSessionResources(sessionId)` when a session ends.
1445
+
1443
1446
  ### OpenAI Compatibility Settings
1444
1447
 
1445
1448
  The `openai-completions` API is implemented by many providers with minor differences. By default, the library auto-detects compatibility settings based on `baseUrl` for a small set of known OpenAI-compatible providers (Cerebras, xAI, Chutes, DeepSeek, NVIDIA NIM, Together AI, zAi, OpenCode, Cloudflare Workers AI, etc.). For custom proxies or unknown endpoints, you can override these settings via the `compat` field. For `openai-responses` models, the compat field supports Responses-specific flags.
@@ -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;AAiBnF,OAAO,KAAK,EASX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAKb,MAAM,aAAa,CAAC;AAmLrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAqPhE,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,CA6YzE,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;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,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 { registerSessionResourceCleanup } from "../session-resources.js";
4
5
  import { raceWithAbortSignal } from "../utils/abort.js";
5
6
  import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.js";
6
7
  import { assertSupportedDocumentMimeType } from "../utils/document-input.js";
7
8
  import { AssistantMessageEventStream } from "../utils/event-stream.js";
9
+ import { shortHash } from "../utils/hash.js";
8
10
  import { headersToRecord } from "../utils/headers.js";
9
11
  import { getIdentityHeaders } from "../utils/identity.js";
10
12
  import { parseJsonWithRepair } from "../utils/json-parse.js";
@@ -139,6 +141,7 @@ const SERVER_SIDE_FALLBACK_BETA = "server-side-fallback-2026-07-01";
139
141
  const THINKING_BINDING_CONTROLS_BETA = "thinking-binding-controls-2026-08-01";
140
142
  const MID_CONVERSATION_OUTPUT_CONFIG_BETA = "mid-conversation-output-config-2026-07-01";
141
143
  const INLINE_TOOLS_BETA = "inline-tools-2026-09-15";
144
+ const MESSAGE_THREADS_BETA = "message-threads-2026-08-12";
142
145
  const FAST_MODE_BETA = "fast-mode-2026-02-01";
143
146
  /**
144
147
  * Fast mode bills every token category at twice the standard rate for each supported model, across
@@ -327,8 +330,206 @@ function getAnthropicCompat(model) {
327
330
  supportsStrictTools: model.compat?.supportsStrictTools ?? false,
328
331
  supportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,
329
332
  supportsMidConvoToolChanges: model.compat?.supportsMidConvoToolChanges ?? false,
333
+ // Gateways that drop unknown request fields would silently turn a `continue` into a request
334
+ // without the earlier conversation, so the default is limited to the `anthropic` provider on
335
+ // Anthropic's own host. Copilot's endpoint does not accept the threads beta.
336
+ supportsMessageThreads: model.compat?.supportsMessageThreads ??
337
+ (model.provider === "anthropic" && isAnthropicApiHost(model.baseUrl)),
330
338
  };
331
339
  }
340
+ /** Whether `baseUrl` is Anthropic's own API host, compared by parsed hostname (not a substring). */
341
+ function isAnthropicApiHost(baseUrl) {
342
+ try {
343
+ return new URL(baseUrl).hostname === "api.anthropic.com";
344
+ }
345
+ catch {
346
+ return false;
347
+ }
348
+ }
349
+ /** Per session: where the next request can continue, or "off" after the endpoint rejected threads. */
350
+ const threadState = new Map();
351
+ const MAX_THREAD_STATE_ENTRIES = 10_000;
352
+ /** Rejections of a `continue` that a new thread fixes: expired, raced, or a fixed setting changed. */
353
+ const NEW_THREAD_ERRORS = new Set([
354
+ "thread_not_found",
355
+ "thread_already_continued",
356
+ "thread_continue_in_flight",
357
+ "thread_fingerprint_mismatch",
358
+ ]);
359
+ /**
360
+ * Sets a session's thread state, keeping at most `MAX_THREAD_STATE_ENTRIES` sessions.
361
+ *
362
+ * State is normally freed by `cleanupSessionResources(sessionId)` when a session ends. The cap
363
+ * bounds memory for library users that never call it, for example a server passing a new
364
+ * `sessionId` per conversation. Dropping the least recently updated session is cheap: its thread
365
+ * has most likely expired on the server already, and otherwise its next request starts a new thread.
366
+ */
367
+ function setThreadState(sessionId, value) {
368
+ // Re-insert so Map order stays oldest-first for the cap.
369
+ threadState.delete(sessionId);
370
+ threadState.set(sessionId, value);
371
+ if (threadState.size > MAX_THREAD_STATE_ENTRIES) {
372
+ const oldest = threadState.keys().next().value;
373
+ if (oldest !== undefined)
374
+ threadState.delete(oldest);
375
+ }
376
+ }
377
+ registerSessionResourceCleanup((sessionId) => {
378
+ if (sessionId)
379
+ threadState.delete(sessionId);
380
+ else
381
+ threadState.clear();
382
+ });
383
+ /**
384
+ * Hash of everything a `continue` builds on: the settings that must stay the same within a thread
385
+ * and the messages the server already holds.
386
+ *
387
+ * - Settings, as sent: the endpoint (a thread exists on one endpoint only) and the fixed request
388
+ * fields. A `continue` omits `system` and `tools` and the server keeps the thread's own, so only
389
+ * this check can notice a change to them, including their cache markers. The server rejects
390
+ * changes to the other fields with `thread_fingerprint_mismatch`; checking them here saves that
391
+ * rejected request. Atomic also checks the fast-mode `speed`.
392
+ * - Messages, normalized so messages that mean the same to the API hash the same.
393
+ */
394
+ function hashThreadPrefix(model, params, messages) {
395
+ const { system, tools, thinking, output_config, tool_choice, betas, fallbacks } = params;
396
+ const settings = JSON.stringify([
397
+ model.provider,
398
+ model.baseUrl,
399
+ params.model,
400
+ system,
401
+ tools,
402
+ thinking,
403
+ output_config,
404
+ tool_choice,
405
+ betas,
406
+ fallbacks,
407
+ params.speed,
408
+ ]);
409
+ const conversation = JSON.stringify(messages.map(normalizeThreadMessage));
410
+ return shortHash(settings + conversation);
411
+ }
412
+ /**
413
+ * Normalizes one message so messages that mean the same to the API compare equal. Only message and
414
+ * content-block fields are touched; tool inputs and every other value compare exactly as sent.
415
+ *
416
+ * - Cache markers don't change what a message means, but they move: Atomic puts one on the newest
417
+ * message, so the message that had it in the last request has none in this one.
418
+ * - To attach that marker, the newest user message's string content is sent as one text block; the
419
+ * next request sends the same message as a string: "hi" vs [{ type: "text", text: "hi" }]. The
420
+ * same holds for a tool result's content.
421
+ */
422
+ function normalizeThreadMessage(message) {
423
+ if (message === null || typeof message !== "object")
424
+ return message;
425
+ const { content, ...rest } = message;
426
+ return { ...rest, content: normalizeThreadContent(content) };
427
+ }
428
+ function normalizeThreadContent(content) {
429
+ if (typeof content === "string")
430
+ return [{ type: "text", text: content }];
431
+ if (!Array.isArray(content))
432
+ return content;
433
+ return content.map((block) => {
434
+ if (block === null || typeof block !== "object")
435
+ return block;
436
+ const { cache_control: _cacheControl, ...rest } = block;
437
+ if (rest.type !== "tool_result")
438
+ return rest;
439
+ const toolResult = rest;
440
+ return { ...toolResult, content: normalizeThreadContent(toolResult.content) };
441
+ });
442
+ }
443
+ function createThreadRequest(sessionId, params) {
444
+ return { sessionId, body: { ...params, thread: { type: "create" } } };
445
+ }
446
+ function prepareThreadRequest(model, sessionId, params) {
447
+ const state = threadState.get(sessionId);
448
+ if (state === "off")
449
+ return undefined;
450
+ if (state !== undefined &&
451
+ params.messages.length > state.prefixCount &&
452
+ hashThreadPrefix(model, params, params.messages.slice(0, state.prefixCount)) === state.prefixHash) {
453
+ // Each reply can be continued once; deleting also keeps a concurrent request from trying.
454
+ threadState.delete(sessionId);
455
+ const { system: _system, tools: _tools, ...rest } = params;
456
+ return {
457
+ body: {
458
+ ...rest,
459
+ messages: params.messages.slice(state.prefixCount),
460
+ thread: { type: "continue", previous_message_id: state.replyId },
461
+ },
462
+ sessionId,
463
+ };
464
+ }
465
+ return createThreadRequest(sessionId, params);
466
+ }
467
+ /** The `error_code` Anthropic puts in `error.details` of thread rejections. */
468
+ function getThreadErrorCode(error) {
469
+ const body = error.error;
470
+ const code = body?.error?.details?.error_code;
471
+ return typeof code === "string" ? code : undefined;
472
+ }
473
+ /**
474
+ * Sends a thread request. A rejection about the thread (never a started stream) is resent once in
475
+ * full: as a new thread when the old one can't be continued, or without `thread` when the endpoint
476
+ * can't use threads, which also turns threads off for the session. Other errors are passed on.
477
+ * Returns the response and the thread request that produced it, if any.
478
+ */
479
+ async function sendThreadRequest(thread, params, send, output, signal) {
480
+ try {
481
+ return { response: await send(thread.body), thread };
482
+ }
483
+ catch (error) {
484
+ if (signal?.aborted || !(error instanceof Anthropic.APIError))
485
+ throw error;
486
+ const errorCode = getThreadErrorCode(error);
487
+ const startNewThread = errorCode !== undefined && NEW_THREAD_ERRORS.has(errorCode);
488
+ // The endpoint can't use threads: it can't thread this request, or it doesn't know the
489
+ // `thread` field and rejects it without a code ("thread: Extra inputs are not permitted"),
490
+ // e.g. with an `anthropic-beta` header that lacks the threads beta.
491
+ const threadsUnsupported = errorCode === "thread_unsupported_request" ||
492
+ (errorCode === undefined && error.status === 400 && error.message.includes("thread"));
493
+ // Any other error (prompt too long, unknown model, strict tool limits, ...) is not about the
494
+ // thread, and resending would only fail again.
495
+ if (!startNewThread && !threadsUnsupported)
496
+ throw error;
497
+ // Recorded before the resend, so a failed resend keeps it.
498
+ appendAssistantMessageDiagnostic(output, {
499
+ type: "anthropic_thread_fallback",
500
+ timestamp: Date.now(),
501
+ error: { message: error.message },
502
+ details: { reason: errorCode ?? String(error.status) },
503
+ });
504
+ if (startNewThread) {
505
+ const created = createThreadRequest(thread.sessionId, params);
506
+ return { response: await send(created.body), thread: created };
507
+ }
508
+ // With the threads beta, a request without `thread` is a normal request.
509
+ const response = await send(params);
510
+ setThreadState(thread.sessionId, "off");
511
+ return { response, thread: undefined };
512
+ }
513
+ }
514
+ /**
515
+ * Records where the session's next request can continue: after `output`, the clean reply to the
516
+ * thread request `thread`, whose full request was `params`.
517
+ */
518
+ function recordThreadReply(model, thread, params, output, isOAuth, allowEmptySignature) {
519
+ // `responseModel` is set only when another model answered (server-side fallback); the server
520
+ // does not store those replies, so they cannot be continued.
521
+ if (output.responseId === undefined || output.responseModel !== undefined)
522
+ return;
523
+ // Convert the reply like the next request will, keeping only the assistant message
524
+ // (transformMessages adds placeholder results for trailing tool calls).
525
+ const reply = convertMessages(transformMessages([output], model, normalizeToolCallId), isOAuth, undefined, allowEmptySignature).messages.filter((message) => message.role === "assistant");
526
+ const prefix = [...params.messages, ...reply];
527
+ setThreadState(thread.sessionId, {
528
+ prefixCount: prefix.length,
529
+ prefixHash: hashThreadPrefix(model, params, prefix),
530
+ replyId: output.responseId,
531
+ });
532
+ }
332
533
  function mergeHeaders(...headerSources) {
333
534
  const merged = {};
334
535
  for (const headers of headerSources) {
@@ -586,7 +787,8 @@ async function* iterateAnthropicEvents(response, signal) {
586
787
  }
587
788
  export const stream = (model, context, options) => {
588
789
  const stream = new AssistantMessageEventStream();
589
- const normalizedContext = resolveTranscript(context, getAnthropicCompat(model).supportsMidConvoSystemMessages);
790
+ const compat = getAnthropicCompat(model);
791
+ const normalizedContext = resolveTranscript(context, compat.supportsMidConvoSystemMessages);
590
792
  const currentTools = getCurrentTools(normalizedContext.messages);
591
793
  (async () => {
592
794
  const output = {
@@ -613,6 +815,14 @@ export const stream = (model, context, options) => {
613
815
  let isOAuth;
614
816
  let usageModel = model;
615
817
  let servedSpeed = requestedSpeed(model);
818
+ const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
819
+ const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
820
+ // Session whose requests may use message threads; undefined when threads don't apply. A
821
+ // caller-supplied client carries its own `anthropic-beta` header, which Atomic cannot extend
822
+ // with the threads beta, so it never uses threads. Sessions where the endpoint rejected threads
823
+ // ("off") still send the threads beta, which the resend that turned them off was accepted
824
+ // with; `prepareThreadRequest()` just sends no `thread`.
825
+ const threadSessionId = compat.supportsMessageThreads && !options?.client ? cacheSessionId : undefined;
616
826
  if (options?.client) {
617
827
  client = options.client;
618
828
  isOAuth = false;
@@ -631,11 +841,9 @@ export const stream = (model, context, options) => {
631
841
  apiKey,
632
842
  }));
633
843
  }
634
- const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
635
- const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
636
- const created = createClient(model, apiKey, model.reasoning && options?.thinkingEnabled === true && (options.interleavedThinking ?? true), shouldUseFineGrainedToolStreamingBeta(model, context), shouldUseServerSideFallbackBeta(model), options?.headers, options?.fetch, copilotDynamicHeaders, cacheSessionId, getAnthropicCompat(model).supportsMidConvoToolChanges &&
637
- getAnthropicCompat(model).supportsMidConvoSystemMessages &&
638
- (getInitialSystemMessage(normalizedContext.messages)?.toolsAdded?.length ?? 0) > 0, federation, options?.identity);
844
+ const created = createClient(model, apiKey, model.reasoning && options?.thinkingEnabled === true && (options.interleavedThinking ?? true), shouldUseFineGrainedToolStreamingBeta(model, context), shouldUseServerSideFallbackBeta(model), options?.headers, options?.fetch, copilotDynamicHeaders, cacheSessionId, compat.supportsMidConvoToolChanges &&
845
+ compat.supportsMidConvoSystemMessages &&
846
+ (getInitialSystemMessage(normalizedContext.messages)?.toolsAdded?.length ?? 0) > 0, federation, options?.identity, threadSessionId !== undefined);
639
847
  client = created.client;
640
848
  isOAuth = created.isOAuthToken;
641
849
  }
@@ -644,24 +852,38 @@ export const stream = (model, context, options) => {
644
852
  ...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
645
853
  maxRetries: 0,
646
854
  };
855
+ const send = (body) => retryProviderRequest(() => client.beta.messages.create({ ...body, stream: true }, requestOptions).asResponse(), {
856
+ maxRetries: options?.maxRetries,
857
+ maxRetryDelayMs: options?.maxRetryDelayMs,
858
+ signal: streamDeadline.signal,
859
+ });
647
860
  let disablePreferredStrictTools = false;
648
861
  let response;
862
+ let params;
863
+ // The thread request that produced the response; undefined when no `thread` was sent.
864
+ let thread;
649
865
  for (;;) {
650
866
  streamDeadline.signal?.throwIfAborted();
651
- const built = buildParams(model, normalizedContext, isOAuth, options, disablePreferredStrictTools);
867
+ const built = buildParams(model, normalizedContext, isOAuth, options, {
868
+ disablePreferredStrictTools,
869
+ messageThreads: threadSessionId !== undefined,
870
+ });
652
871
  if (built.providerThinkingLevel)
653
872
  output.providerThinkingLevel = built.providerThinkingLevel;
654
- let params = built.params;
873
+ params = built.params;
655
874
  const nextParams = await options?.onPayload?.(params, model);
656
875
  if (nextParams !== undefined)
657
876
  params = nextParams;
658
877
  assertPayloadPreservesFastRoute(model, params);
878
+ const prepared = threadSessionId !== undefined && options?.disableContinuation !== true
879
+ ? prepareThreadRequest(model, threadSessionId, params)
880
+ : undefined;
659
881
  try {
660
- response = await retryProviderRequest(() => client.beta.messages.create({ ...params, stream: true }, requestOptions).asResponse(), {
661
- maxRetries: options?.maxRetries,
662
- maxRetryDelayMs: options?.maxRetryDelayMs,
663
- signal: streamDeadline.signal,
664
- });
882
+ const sent = prepared
883
+ ? await sendThreadRequest(prepared, params, send, output, streamDeadline.signal)
884
+ : { response: await send(params), thread: undefined };
885
+ response = sent.response;
886
+ thread = sent.thread;
665
887
  break;
666
888
  }
667
889
  catch (error) {
@@ -928,6 +1150,8 @@ export const stream = (model, context, options) => {
928
1150
  if (output.stopReason === "aborted" || output.stopReason === "error") {
929
1151
  throw new Error(output.errorMessage || "An unknown error occurred");
930
1152
  }
1153
+ if (thread)
1154
+ recordThreadReply(model, thread, params, output, isOAuth, compat.allowEmptySignature);
931
1155
  stream.push({ type: "done", reason: output.stopReason, message: output });
932
1156
  stream.end();
933
1157
  }
@@ -1008,7 +1232,7 @@ export const streamSimple = (model, context, options) => {
1008
1232
  function isOAuthToken(apiKey) {
1009
1233
  return apiKey.includes("sk-ant-oat");
1010
1234
  }
1011
- function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStreamingBeta, useServerSideFallbackBeta, optionsHeaders, fetch, dynamicHeaders, sessionId, useNativeToolChanges = false, federation, identity) {
1235
+ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStreamingBeta, useServerSideFallbackBeta, optionsHeaders, fetch, dynamicHeaders, sessionId, useNativeToolChanges = false, federation, identity, useMessageThreads = false) {
1012
1236
  // Adaptive thinking models have interleaved thinking built in, so skip the beta header.
1013
1237
  const needsInterleavedBeta = interleavedThinking && model.compat?.forceAdaptiveThinking !== true;
1014
1238
  const betaFeatures = [];
@@ -1031,6 +1255,8 @@ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStre
1031
1255
  betaFeatures.push(INLINE_TOOLS_BETA);
1032
1256
  if (requestedSpeed(model) === "fast")
1033
1257
  betaFeatures.push(FAST_MODE_BETA);
1258
+ if (useMessageThreads)
1259
+ betaFeatures.push(MESSAGE_THREADS_BETA);
1034
1260
  const uniqueBetaFeatures = [...new Set(betaFeatures)];
1035
1261
  // Copilot: Bearer auth, selective betas.
1036
1262
  if (model.provider === "github-copilot") {
@@ -1103,7 +1329,7 @@ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStre
1103
1329
  });
1104
1330
  return { client, isOAuthToken: false };
1105
1331
  }
1106
- function getNativeToolChangeBetas(model, context, isOAuth, options) {
1332
+ function getNativeToolChangeBetas(model, context, isOAuth, options, messageThreads) {
1107
1333
  let configured;
1108
1334
  for (const headers of [model.headers, options?.headers]) {
1109
1335
  for (const [name, value] of Object.entries(headers ?? {})) {
@@ -1139,9 +1365,11 @@ function getNativeToolChangeBetas(model, context, isOAuth, options) {
1139
1365
  features.push(MID_CONVERSATION_OUTPUT_CONFIG_BETA, THINKING_BINDING_CONTROLS_BETA);
1140
1366
  if (requestedSpeed(model) === "fast")
1141
1367
  features.push(FAST_MODE_BETA);
1368
+ if (messageThreads)
1369
+ features.push(MESSAGE_THREADS_BETA);
1142
1370
  return [...new Set(features)];
1143
1371
  }
1144
- function buildParams(model, context, isOAuthToken, options, disablePreferredStrictTools = false) {
1372
+ function buildParams(model, context, isOAuthToken, options, { disablePreferredStrictTools, messageThreads }) {
1145
1373
  const { cacheControl } = getCacheControl(model, options?.cacheRetention, options?.env);
1146
1374
  const compat = getAnthropicCompat(model);
1147
1375
  const initialSystemMessage = getInitialSystemMessage(context.messages);
@@ -1173,7 +1401,7 @@ function buildParams(model, context, isOAuthToken, options, disablePreferredStri
1173
1401
  stream: true,
1174
1402
  };
1175
1403
  if (nativeToolChanges)
1176
- params.betas = getNativeToolChangeBetas(model, context, isOAuthToken, options);
1404
+ params.betas = getNativeToolChangeBetas(model, context, isOAuthToken, options, messageThreads);
1177
1405
  const speed = requestedSpeed(model);
1178
1406
  if (speed !== undefined)
1179
1407
  params.speed = speed;
@@ -1574,6 +1802,10 @@ function convertTools(tools, isOAuthToken, supportsEagerToolInputStreaming, supp
1574
1802
  type: "object",
1575
1803
  properties: projectedUnion?.properties ?? schema.properties ?? {},
1576
1804
  required: projectedUnion?.required ?? schema.required ?? [],
1805
+ // Keep shared definitions so property $refs still resolve. Other root keywords stay out:
1806
+ // Anthropic rejects root anyOf/oneOf/allOf with a 400.
1807
+ ...(schema.$defs === undefined ? {} : { $defs: schema.$defs }),
1808
+ ...(schema.definitions === undefined ? {} : { definitions: schema.definitions }),
1577
1809
  };
1578
1810
  const inputSchema = strict === true && projectedUnion === undefined
1579
1811
  ? {