@bastani/pi-ai 0.9.40-alpha.2 → 0.9.41-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 (113) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/README.md +28 -0
  3. package/dist/api/anthropic-messages.d.ts.map +1 -1
  4. package/dist/api/anthropic-messages.js +7 -10
  5. package/dist/api/anthropic-messages.js.map +1 -1
  6. package/dist/api/azure-openai-responses.js +2 -2
  7. package/dist/api/azure-openai-responses.js.map +1 -1
  8. package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
  9. package/dist/api/bedrock-converse-stream.js +12 -0
  10. package/dist/api/bedrock-converse-stream.js.map +1 -1
  11. package/dist/api/classifier-shared.d.ts.map +1 -1
  12. package/dist/api/classifier-shared.js +4 -3
  13. package/dist/api/classifier-shared.js.map +1 -1
  14. package/dist/api/google-generative-ai.js +4 -4
  15. package/dist/api/google-generative-ai.js.map +1 -1
  16. package/dist/api/google-vertex.d.ts.map +1 -1
  17. package/dist/api/google-vertex.js +9 -9
  18. package/dist/api/google-vertex.js.map +1 -1
  19. package/dist/api/llama-cpp-classify.d.ts.map +1 -1
  20. package/dist/api/llama-cpp-classify.js +2 -1
  21. package/dist/api/llama-cpp-classify.js.map +1 -1
  22. package/dist/api/mistral-conversations.js +2 -2
  23. package/dist/api/mistral-conversations.js.map +1 -1
  24. package/dist/api/openai-codex-responses.d.ts.map +1 -1
  25. package/dist/api/openai-codex-responses.js +12 -9
  26. package/dist/api/openai-codex-responses.js.map +1 -1
  27. package/dist/api/openai-completions.d.ts.map +1 -1
  28. package/dist/api/openai-completions.js +4 -4
  29. package/dist/api/openai-completions.js.map +1 -1
  30. package/dist/api/openai-responses.d.ts.map +1 -1
  31. package/dist/api/openai-responses.js +15 -7
  32. package/dist/api/openai-responses.js.map +1 -1
  33. package/dist/api/openrouter-images.d.ts.map +1 -1
  34. package/dist/api/openrouter-images.js +4 -3
  35. package/dist/api/openrouter-images.js.map +1 -1
  36. package/dist/api/pi-messages.d.ts.map +1 -1
  37. package/dist/api/pi-messages.js +3 -3
  38. package/dist/api/pi-messages.js.map +1 -1
  39. package/dist/api/simple-options.d.ts.map +1 -1
  40. package/dist/api/simple-options.js +1 -0
  41. package/dist/api/simple-options.js.map +1 -1
  42. package/dist/auth/oauth/anthropic.d.ts.map +1 -1
  43. package/dist/auth/oauth/anthropic.js +15 -2
  44. package/dist/auth/oauth/anthropic.js.map +1 -1
  45. package/dist/auth/oauth/github-copilot.d.ts.map +1 -1
  46. package/dist/auth/oauth/github-copilot.js +7 -2
  47. package/dist/auth/oauth/github-copilot.js.map +1 -1
  48. package/dist/auth/oauth/kimi-coding.d.ts.map +1 -1
  49. package/dist/auth/oauth/kimi-coding.js +24 -21
  50. package/dist/auth/oauth/kimi-coding.js.map +1 -1
  51. package/dist/auth/oauth/meta.d.ts.map +1 -1
  52. package/dist/auth/oauth/meta.js +7 -2
  53. package/dist/auth/oauth/meta.js.map +1 -1
  54. package/dist/auth/oauth/openai-chatgpt.d.ts.map +1 -1
  55. package/dist/auth/oauth/openai-chatgpt.js +10 -0
  56. package/dist/auth/oauth/openai-chatgpt.js.map +1 -1
  57. package/dist/auth/oauth/openai-codex.d.ts.map +1 -1
  58. package/dist/auth/oauth/openai-codex.js +11 -2
  59. package/dist/auth/oauth/openai-codex.js.map +1 -1
  60. package/dist/auth/oauth/radius.d.ts.map +1 -1
  61. package/dist/auth/oauth/radius.js +24 -8
  62. package/dist/auth/oauth/radius.js.map +1 -1
  63. package/dist/auth/oauth/xai.d.ts.map +1 -1
  64. package/dist/auth/oauth/xai.js +11 -0
  65. package/dist/auth/oauth/xai.js.map +1 -1
  66. package/dist/auth/oauth-refresh-errors.d.ts +46 -0
  67. package/dist/auth/oauth-refresh-errors.d.ts.map +1 -0
  68. package/dist/auth/oauth-refresh-errors.js +150 -0
  69. package/dist/auth/oauth-refresh-errors.js.map +1 -0
  70. package/dist/auth/resolve.d.ts +23 -0
  71. package/dist/auth/resolve.d.ts.map +1 -1
  72. package/dist/auth/resolve.js +194 -5
  73. package/dist/auth/resolve.js.map +1 -1
  74. package/dist/index.d.ts +3 -0
  75. package/dist/index.d.ts.map +1 -1
  76. package/dist/index.js +3 -0
  77. package/dist/index.js.map +1 -1
  78. package/dist/models.d.ts +4 -1
  79. package/dist/models.d.ts.map +1 -1
  80. package/dist/models.js +15 -4
  81. package/dist/models.js.map +1 -1
  82. package/dist/providers/data/.manifest.json +1 -1
  83. package/dist/providers/data/amazon-bedrock.json +1 -1
  84. package/dist/providers/data/baseten.json +1 -1
  85. package/dist/providers/data/cerebras.json +1 -1
  86. package/dist/providers/data/github-copilot.json +1 -1
  87. package/dist/providers/data/groq.json +1 -1
  88. package/dist/providers/data/huggingface.json +1 -1
  89. package/dist/providers/data/nvidia.json +1 -1
  90. package/dist/providers/data/opencode.json +1 -1
  91. package/dist/providers/data/openrouter.json +1 -1
  92. package/dist/providers/data/radius.json +1 -1
  93. package/dist/providers/data/together.json +1 -1
  94. package/dist/providers/data/vercel-ai-gateway.json +1 -1
  95. package/dist/types.d.ts +6 -0
  96. package/dist/types.d.ts.map +1 -1
  97. package/dist/types.js.map +1 -1
  98. package/dist/utils/identity.d.ts +65 -0
  99. package/dist/utils/identity.d.ts.map +1 -0
  100. package/dist/utils/identity.js +142 -0
  101. package/dist/utils/identity.js.map +1 -0
  102. package/dist/utils/retry.d.ts.map +1 -1
  103. package/dist/utils/retry.js +9 -8
  104. package/dist/utils/retry.js.map +1 -1
  105. package/dist/utils/version.d.ts +2 -0
  106. package/dist/utils/version.d.ts.map +1 -0
  107. package/dist/utils/version.js +3 -0
  108. package/dist/utils/version.js.map +1 -0
  109. package/package.json +1 -1
  110. package/dist/utils/pi-user-agent.d.ts +0 -2
  111. package/dist/utils/pi-user-agent.d.ts.map +0 -1
  112. package/dist/utils/pi-user-agent.js +0 -12
  113. package/dist/utils/pi-user-agent.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -4,6 +4,54 @@ 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.41-alpha.1] - 2026-10-21
8
+
9
+ ### Breaking Changes
10
+
11
+ - The default `User-Agent` is now `pi/<version> (<platform>; <runtime>; <arch>)` instead of `pi (<platform> <release>; <arch>)`, and `@bastani/pi-ai/utils/pi-user-agent` (`getPiUserAgent()`) is removed; use `formatUserAgent()`.
12
+
13
+ ### Added
14
+
15
+ - Added client identity configuration: an `identity` request option and `createModels({ identity })` default choose the `User-Agent` (`"detailed"` with a version, `"short"`, or custom) and provider attribution headers for OpenRouter, NVIDIA NIM, and OpenCode from `attribution: { name, url, categories? }`. `withClientIdentity()` applies an identity to a `Models` or `MutableModels` a library receives from its host. `formatUserAgent()`, `getIdentityHeaders()`, `getOpenCodeSessionHeaders()`, `mergeClientIdentity()`, `PI_ATTRIBUTION`, and `PI_AI_VERSION` are exported. Attribution matches OpenRouter by its exact `openrouter.ai` host, so a custom endpoint that merely contains that name is not attributed.
16
+ - Bedrock, OpenRouter image generation, pi-messages, and classifier requests now send pi's `User-Agent` too.
17
+ - `Models` requests with a `sessionId` to custom providers pointing at OpenCode (`opencode.ai`) now send the `x-opencode-session` header too, like the built-in OpenCode providers.
18
+
19
+ ### Fixed
20
+
21
+ - `refreshStoredOAuthCredential()` no longer aborts a token exchange when its caller stops waiting. A token rotation that completes after the 60-second refresh timeout is now saved instead of leaving the replaced refresh token stored. The refresh keeps the credential lock until the exchange settles, for up to `OAUTH_REFRESH_LOCK_HOLD_MS`, so no other refresh presents the token it may already have rotated. When the provider rejects a refresh, it returns a newer credential that another process has saved. A refresh rejected with `invalid_grant` reports only the provider's error code and description, never request URLs, bodies, or tokens, followed by an instruction to run `/login <provider>`. `OAUTH_REFRESH_TIMEOUT_MS` and `OAUTH_REFRESH_LOCK_HOLD_MS` are exported.
22
+ - Anthropic OAuth no longer cuts off a token refresh request after 30 seconds, because the provider may already have rotated the refresh token. The 30-second limit still applies to the login code exchange.
23
+ - Kimi Code OAuth no longer cuts off a token refresh request after 30 seconds. It also no longer sends the same refresh token again after an attempt that may have reached the server (a timeout, an abort, a connection lost mid-request, or an HTTP 429 or 5xx response), because the server may already have rotated that token. A refresh is retried only when the connection was never made. The 30-second limit still applies to device login.
24
+ - A refresh rejected because the stored login is revoked or expired now tells you to run `/login <provider>` for more cases than a literal `invalid_grant`. This includes OpenAI's `refresh_token_expired`, `refresh_token_invalidated` and `refresh_token_reused` codes (also in a nested `error.code`), any HTTP 401 from a token endpoint, Kimi Code's 401 and 403 responses, Meta's expired session, and a revoked GitHub Copilot login. Built-in refreshers throw the new exported `OAuthRefreshRejectedError`, which carries only the HTTP status and the provider's error code and description. Custom refreshers can throw it too, and `refreshStoredOAuthCredential()` maps it to the `/login <provider>` message. The description is dropped when it contains the presented token or a URL. Kimi Code's own refresh errors no longer include the provider's raw error description. `isOAuthRefreshRejectedError`, `isPermanentRefreshRejection`, `parseOAuthErrorBody` and the `OAuthRefreshRejection` and `OAuthErrorDetail` types are exported alongside it.
25
+ - A token rotation that completes after its caller stopped waiting is no longer lost when the process exits. The new `drainPendingOAuthRotations(timeoutMs = OAUTH_SHUTDOWN_DRAIN_MS)` waits, for at most `OAUTH_SHUTDOWN_DRAIN_MS` (3 seconds) by default, until every token exchange that has started settles and its result is saved. It resolves `true` when none is left and `false` when the bound passes first, and never rejects. Hosts call it before a graceful exit. A process killed or crashing mid-exchange still cannot save a rotation it never received; `/login <provider>` signs in again. If a late rotation succeeds but saving it fails, the next refresh failure for that provider adds that an earlier refresh succeeded but its new login could not be saved, with only the save error's code or name (for example `ELOCKED`).
26
+ - Sign in with ChatGPT: a ChatGPT Business workspace usage-limit error (`subscription_sharing_workplace_individual_usage_limit_exceeded`) is no longer classified as a transient error to retry, and its message now tells you to ask your workspace admin to increase the limit.
27
+ - Fixed automatic retry not triggering for `stream disconnected before completion` errors relayed by proxies that wrap the Codex CLI ([#11057](https://github.com/earendil-works/pi/issues/11057))
28
+ - Fixed automatic retry treating digits inside request ids or other numbers (for example `req_011CU503xyz` or `5242880 bytes`) as retryable HTTP status codes. Status codes 429, 500, 502-504 and 520-524 now match only as standalone numbers ([#11057](https://github.com/earendil-works/pi/issues/11057))
29
+
30
+ ## [0.9.40] - 2026-10-20
31
+
32
+ ### Breaking Changes
33
+
34
+ - `resolveTranscriptTools()` now takes a `TranscriptToolSupport` (`"none"`, `"additions"`, or `"changes"`) instead of a boolean, and `TranscriptTools.anchorsAdditions` is replaced by `foldedSystemMessages`, the number of leading system messages folded into `requestTools` ([#10940](https://github.com/earendil-works/pi/issues/10940))
35
+
36
+ ### Added
37
+
38
+ - Added `MiniMax-M3.1-Flash` to the `minimax` and `minimax-cn` providers, with thinking levels low through max sent as `output_config.effort`. Thinking cannot be turned off for this model. New MiniMax models from models.dev now appear without a manual allowlist entry; only the older M2, M2.1, M2.5, and M2.5-highspeed models stay hidden ([#11121](https://github.com/earendil-works/pi/issues/11121))
39
+ - Added the `typesafe/jev-1.14.0` classifier for Jev 1.14 alongside `typesafe/jev-latest`.
40
+ - Direct TypeSafe Jev classifiers (`typesafe/jev-latest`, `typesafe/jev-1.14.0`) now send TypeSafe's long-context and image preview headers on every request, accept up to 262K tokens, and accept PNG or JPEG `images`. Images are sent as referenced assets and listed in order under the state's `images` field; other image types, or a state that already has an `images` field, return an error result. Jev on Vercel AI Gateway, OpenCode Zen, OpenRouter, and Cloudflare Workers AI still rejects images.
41
+
42
+ ### Changed
43
+
44
+ - Grok 4.3 and Grok 4.5 on the `xai` provider now offer the `xhigh` thinking level, matching the reasoning efforts xAI supports for these models.
45
+
46
+ ### Fixed
47
+
48
+ - Treat any HTTP 413 from a provider as request-size overflow, so a GitHub Copilot request over its byte-size limit (`413 ... failed to parse request`) now compacts and retries instead of surfacing a raw API error ([pi 73f8de80](https://github.com/earendil-works/pi/commit/73f8de80bc87021375f8b525292ac07492253002))
49
+ - Fixed OpenAI Responses deferred tool loads rewriting the top-level tool list, and losing the prompt cache, on every load after a tool removal. Models with `additional_tools` now declare all tools in `additional_tools` items, starting with one at the head of the input, so removals (announced as text) and redefinitions no longer rewrite the prefix. Tool search and Kimi tool messages rewrite the top-level tools once per removal or redeclaration and keep later loads anchored ([#10940](https://github.com/earendil-works/pi/issues/10940))
50
+
51
+ ### Removed
52
+
53
+ - Removed `gpt-5.5` and `gpt-5.3-codex-spark` from the `openai-codex` provider. The Codex backend rejects both for ChatGPT accounts ("not supported when using Codex with a ChatGPT account"), so selecting them always failed ([#11173](https://github.com/earendil-works/pi/pull/11173))
54
+
7
55
  ## [0.9.40-alpha.2] - 2026-10-20
8
56
 
9
57
  ### Breaking Changes
package/README.md CHANGED
@@ -22,6 +22,7 @@ Unified LLM API with provider collections, automatic auth resolution, token and
22
22
  - [Auth](#auth)
23
23
  - [How Auth Resolves](#how-auth-resolves)
24
24
  - [Transforming Request Headers](#transforming-request-headers)
25
+ - [Client Identity](#client-identity)
25
26
  - [Credential Store](#credential-store)
26
27
  - [Environment Variables](#environment-variables)
27
28
  - [Tools](#tools)
@@ -432,6 +433,33 @@ Header names are merged case-insensitively. Explicit headers override auth/model
432
433
 
433
434
  `transformHeaders` belongs to `Models`, not `Provider`. A `Models` implementation must consume it and remove it before calling `Provider.stream*()`. Provider implementations continue receiving ordinary `ApiStreamOptions` or `SimpleStreamOptions` and never handle the transform themselves. Use this option instead of calling `getAuth(model)` before `stream*()`, which would resolve request auth twice.
434
435
 
436
+ ### Client Identity
437
+
438
+ `identity` controls how requests identify the calling app to providers. Set it on `createModels()` as a default, or per request; a request's fields override the `Models` default field by field:
439
+
440
+ ```typescript
441
+ const models = createModels({
442
+ identity: {
443
+ userAgent: 'detailed',
444
+ version: '2.1.0',
445
+ attribution: { name: 'my-app', url: 'https://my-app.example', categories: ['cloud-agent'] },
446
+ },
447
+ });
448
+
449
+ await models.completeSimple(model, context, { identity: { attribution: null } });
450
+ ```
451
+
452
+ - `userAgent`: `"detailed"` (default) sends `pi/<version> (<platform>; <runtime>; <arch>)`, `"short"` sends `pi`, and `{ custom: "..." }` sends the given value. On Bedrock the value is appended to the AWS SDK user agent.
453
+ - `version`: the version in the detailed `User-Agent`. Default: the pi-ai version.
454
+ - `attribution`: provider attribution headers. Absent (default) or `null` sends none; `null` also turns off attribution from a merged identity. `{ name, url, categories? }` maps to each provider's headers:
455
+ - OpenRouter: `HTTP-Referer: <url>` (the app's identity in OpenRouter's rankings, grouped by origin), `X-OpenRouter-Title: <name>`, and `X-OpenRouter-Categories` (comma-separated).
456
+ - NVIDIA NIM: `X-BILLING-INVOKE-ORIGIN: <name>` with its first letter upper-cased.
457
+ - OpenCode: `x-opencode-client: <name>`.
458
+
459
+ `PI_ATTRIBUTION` is pi's: `{ name: "pi", url: "https://pi.dev", categories: ["cli-agent"] }`.
460
+
461
+ Identity headers are defaults below provider, model, and request headers, so configured headers win and a `null` request header removes one. Providers that send a fixed client identity, such as GitHub Copilot and Anthropic OAuth, keep it. `withClientIdentity(models, identity)` returns a wrapper of a `Models` (or `MutableModels`) whose requests use `identity` over the one the `Models` was created with, for libraries that receive a `Models` from their host. `identity` is plain JSON, so it can be persisted with other request options.
462
+
435
463
  ### Credential Store
436
464
 
437
465
  Stored credentials (API keys entered interactively, OAuth tokens) live in a `CredentialStore` — one type-tagged credential per provider. pi-ai ships an in-memory default; apps inject persistent storage:
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-messages.ts"],"names":[],"mappings":"AAAA,OAAO,SAAiC,MAAM,mBAAmB,CAAC;AAiBlE,OAAO,KAAK,EASX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAOb,MAAM,aAAa,CAAC;AAwKrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAmPhE,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;AAgSD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CAwZzE,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,SAAiC,MAAM,mBAAmB,CAAC;AAiBlE,OAAO,KAAK,EASX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAOb,MAAM,aAAa,CAAC;AAwKrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAmPhE,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;AA4RD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CAyZzE,CAAC;AA4BF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,oBAAoB,EAAE,mBAAmB,CAgDlF,CAAC"}
@@ -5,8 +5,8 @@ import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.js";
5
5
  import { assertSupportedDocumentMimeType } from "../utils/document-input.js";
6
6
  import { AssistantMessageEventStream } from "../utils/event-stream.js";
7
7
  import { headersToRecord } from "../utils/headers.js";
8
+ import { getIdentityHeaders } from "../utils/identity.js";
8
9
  import { parseJsonWithRepair, parseStreamingJson, parseToolCallArguments } from "../utils/json-parse.js";
9
- import { getPiUserAgent } from "../utils/pi-user-agent.js";
10
10
  import { getProviderEnvValue } from "../utils/provider-env.js";
11
11
  import { retryProviderRequest } from "../utils/provider-retry.js";
12
12
  import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
@@ -330,9 +330,6 @@ function mergeHeaders(...headerSources) {
330
330
  }
331
331
  return merged;
332
332
  }
333
- function mergeClientHeaders(...headerSources) {
334
- return mergeHeaders({ "User-Agent": getPiUserAgent() }, ...headerSources);
335
- }
336
333
  function hasHeader(headers, name) {
337
334
  if (!headers)
338
335
  return false;
@@ -606,7 +603,7 @@ export const stream = (model, context, options) => {
606
603
  const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
607
604
  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 &&
608
605
  getAnthropicCompat(model).supportsMidConvoSystemMessages &&
609
- (getInitialSystemMessage(normalizedContext.messages)?.toolsAdded?.length ?? 0) > 0, federation);
606
+ (getInitialSystemMessage(normalizedContext.messages)?.toolsAdded?.length ?? 0) > 0, federation, options?.identity);
610
607
  client = created.client;
611
608
  isOAuth = created.isOAuthToken;
612
609
  }
@@ -993,7 +990,7 @@ export const streamSimple = (model, context, options) => {
993
990
  function isOAuthToken(apiKey) {
994
991
  return apiKey.includes("sk-ant-oat");
995
992
  }
996
- function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStreamingBeta, useServerSideFallbackBeta, optionsHeaders, fetch, dynamicHeaders, sessionId, useNativeToolChanges = false, federation) {
993
+ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStreamingBeta, useServerSideFallbackBeta, optionsHeaders, fetch, dynamicHeaders, sessionId, useNativeToolChanges = false, federation, identity) {
997
994
  // Adaptive thinking models have interleaved thinking built in, so skip the beta header.
998
995
  const needsInterleavedBeta = interleavedThinking && model.compat?.forceAdaptiveThinking !== true;
999
996
  const betaFeatures = [];
@@ -1025,7 +1022,7 @@ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStre
1025
1022
  baseURL: model.baseUrl,
1026
1023
  dangerouslyAllowBrowser: true,
1027
1024
  fetch,
1028
- defaultHeaders: mergeClientHeaders({
1025
+ defaultHeaders: mergeHeaders(getIdentityHeaders(model, identity), {
1029
1026
  accept: "application/json",
1030
1027
  "anthropic-dangerous-direct-browser-access": "true",
1031
1028
  ...(uniqueBetaFeatures.length > 0 ? { "anthropic-beta": uniqueBetaFeatures.join(",") } : {}),
@@ -1033,7 +1030,7 @@ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStre
1033
1030
  });
1034
1031
  return { client, isOAuthToken: false };
1035
1032
  }
1036
- // OAuth: Bearer auth, Claude Code identity headers
1033
+ // OAuth: Bearer auth, Claude Code identity headers instead of the client identity.
1037
1034
  if (apiKey && isOAuthToken(apiKey)) {
1038
1035
  const client = new AtomicAnthropic({
1039
1036
  apiKey: null,
@@ -1041,7 +1038,7 @@ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStre
1041
1038
  baseURL: model.baseUrl,
1042
1039
  dangerouslyAllowBrowser: true,
1043
1040
  fetch,
1044
- defaultHeaders: mergeClientHeaders({
1041
+ defaultHeaders: mergeHeaders({
1045
1042
  accept: "application/json",
1046
1043
  "anthropic-dangerous-direct-browser-access": "true",
1047
1044
  "anthropic-beta": ["claude-code-20250219", "oauth-2025-04-20", ...uniqueBetaFeatures].join(","),
@@ -1058,7 +1055,7 @@ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStre
1058
1055
  const header = compat.sessionAffinityFormat === "openrouter" ? "x-session-id" : "x-session-affinity";
1059
1056
  sessionAffinityHeaders[header] = sessionId;
1060
1057
  }
1061
- const defaultHeaders = mergeClientHeaders({
1058
+ const defaultHeaders = mergeHeaders(getIdentityHeaders(model, identity), {
1062
1059
  accept: "application/json",
1063
1060
  "anthropic-dangerous-direct-browser-access": "true",
1064
1061
  ...(uniqueBetaFeatures.length > 0 ? { "anthropic-beta": uniqueBetaFeatures.join(",") } : {}),