@openllmsh/sdk 0.0.0-stage → 2.10.12-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 (245) hide show
  1. package/ARCHITECTURE.md +43 -0
  2. package/LICENSE +202 -0
  3. package/README.md +142 -2
  4. package/assets/openllm-light.svg +18 -0
  5. package/assets/openllm.svg +18 -0
  6. package/client/chat.ts +140 -0
  7. package/client/devices.ts +269 -0
  8. package/client/http.ts +72 -0
  9. package/client/index.ts +129 -0
  10. package/client/relay-mux.ts +547 -0
  11. package/client/types.ts +51 -0
  12. package/client/vault.ts +239 -0
  13. package/connect/chat.ts +58 -0
  14. package/connect/client.ts +240 -0
  15. package/connect/config.ts +47 -0
  16. package/connect/errors.ts +121 -0
  17. package/connect/index.ts +23 -0
  18. package/connect/oauth.ts +265 -0
  19. package/connect/routes.ts +234 -0
  20. package/connect/store.ts +107 -0
  21. package/mcp/handle.ts +24 -0
  22. package/mcp/index.ts +76 -0
  23. package/mcp/server.ts +319 -0
  24. package/mcp/tools/marketplace.ts +439 -0
  25. package/mcp/tools/memory-client.ts +297 -0
  26. package/mcp/tools/memory-config.ts +32 -0
  27. package/mcp/tools/memory.ts +369 -0
  28. package/mcp/tools/model-priority.ts +88 -0
  29. package/mcp/tools/openllm.ts +883 -0
  30. package/mcp/types.ts +28 -0
  31. package/openapi/call.ts +90 -0
  32. package/openapi/index.ts +8 -0
  33. package/openapi/openapi.json +80213 -0
  34. package/openapi/operations.ts +2075 -0
  35. package/package.json +39 -4
  36. package/protocol/CONTRIBUTING.md +21 -0
  37. package/protocol/README.md +134 -0
  38. package/protocol/access.ts +186 -0
  39. package/protocol/agent-prompt-text.ts +32 -0
  40. package/protocol/agent.ts +991 -0
  41. package/protocol/api-key-syntax.ts +105 -0
  42. package/protocol/assets/openllm-light.svg +18 -0
  43. package/protocol/assets/openllm.svg +18 -0
  44. package/protocol/audio.ts +29 -0
  45. package/protocol/auth.ts +142 -0
  46. package/protocol/changelog.ts +155 -0
  47. package/protocol/chat.ts +458 -0
  48. package/protocol/client-registry.ts +356 -0
  49. package/protocol/config.ts +345 -0
  50. package/protocol/control-channel.ts +115 -0
  51. package/protocol/cooldown-reason.ts +78 -0
  52. package/protocol/cost.ts +10 -0
  53. package/protocol/daemon-port.ts +31 -0
  54. package/protocol/daemon.ts +1524 -0
  55. package/protocol/default-chains.ts +66 -0
  56. package/protocol/device-state.ts +85 -0
  57. package/protocol/doctor-report-local.ts +443 -0
  58. package/protocol/doctor-report-policy.ts +119 -0
  59. package/protocol/doctor-report.ts +947 -0
  60. package/protocol/errors.ts +14 -0
  61. package/protocol/execution-selection.ts +141 -0
  62. package/protocol/hop-grant.ts +33 -0
  63. package/protocol/image-edit-parse.ts +263 -0
  64. package/protocol/images.ts +298 -0
  65. package/protocol/index.ts +74 -0
  66. package/protocol/inference-headers.ts +3 -0
  67. package/protocol/keepalive.ts +69 -0
  68. package/protocol/local-auth.ts +300 -0
  69. package/protocol/local-device-control.ts +133 -0
  70. package/protocol/local-provider.ts +288 -0
  71. package/protocol/local-session.ts +118 -0
  72. package/protocol/marketplace.ts +187 -0
  73. package/protocol/media-attempt.ts +81 -0
  74. package/protocol/media-bytes.ts +28 -0
  75. package/protocol/media-default.ts +357 -0
  76. package/protocol/media-dimensions.ts +132 -0
  77. package/protocol/media-input.ts +61 -0
  78. package/protocol/media-model.ts +142 -0
  79. package/protocol/media-option-issue.ts +90 -0
  80. package/protocol/media-types.ts +70 -0
  81. package/protocol/model-match.ts +1051 -0
  82. package/protocol/model-settings.ts +327 -0
  83. package/protocol/models.ts +767 -0
  84. package/protocol/mux.ts +722 -0
  85. package/protocol/plan-surface.ts +59 -0
  86. package/protocol/provider-models.ts +16 -0
  87. package/protocol/provider-status.ts +206 -0
  88. package/protocol/provider-tier.ts +67 -0
  89. package/protocol/provider-usage.ts +263 -0
  90. package/protocol/providers.ts +901 -0
  91. package/protocol/public-usage-index.ts +354 -0
  92. package/protocol/realtime-generated.ts +31 -0
  93. package/protocol/realtime.ts +324 -0
  94. package/protocol/registry-pointer.ts +118 -0
  95. package/protocol/relay.ts +702 -0
  96. package/protocol/replay-session.ts +11 -0
  97. package/protocol/responses.ts +280 -0
  98. package/protocol/semver.ts +182 -0
  99. package/protocol/setup-mode.ts +22 -0
  100. package/protocol/sha256-hex.ts +98 -0
  101. package/protocol/stats.ts +406 -0
  102. package/protocol/streaming.ts +19 -0
  103. package/protocol/subscription-daemon-floors.ts +33 -0
  104. package/protocol/subscription-provider-compat.ts +104 -0
  105. package/protocol/subscription-provider.ts +32 -0
  106. package/protocol/team-grant.ts +17 -0
  107. package/protocol/team-shareable.ts +61 -0
  108. package/protocol/transcription-encoding.ts +76 -0
  109. package/protocol/url-locality-dns.ts +37 -0
  110. package/protocol/url-locality.ts +288 -0
  111. package/protocol/usage-snapshots.ts +9 -0
  112. package/protocol/vendor-cli-install.ts +29 -0
  113. package/protocol/video-job-plan.ts +50 -0
  114. package/protocol/videos.ts +227 -0
  115. package/tsconfig.json +14 -0
  116. package/tunnel/ARCHITECTURE.md +111 -0
  117. package/tunnel/LICENSE +33 -0
  118. package/tunnel/README.md +45 -0
  119. package/tunnel/assets/openllm-light.svg +18 -0
  120. package/tunnel/assets/openllm.svg +18 -0
  121. package/tunnel/channel-envelope.ts +139 -0
  122. package/tunnel/codec.ts +191 -0
  123. package/tunnel/device-anchor.ts +229 -0
  124. package/tunnel/device-control.ts +118 -0
  125. package/tunnel/device-grant.ts +173 -0
  126. package/tunnel/fleet.ts +77 -0
  127. package/tunnel/index.ts +22 -0
  128. package/tunnel/mux.ts +580 -0
  129. package/tunnel/peer-link.ts +390 -0
  130. package/tunnel/rtc-auth.ts +415 -0
  131. package/tunnel/rtc-duplex.ts +187 -0
  132. package/tunnel/rtc-ice.ts +136 -0
  133. package/tunnel/secure.ts +1298 -0
  134. package/tunnel/stream-reset-error.ts +14 -0
  135. package/tunnel/streams.ts +1430 -0
  136. package/tunnel/team-grant.ts +302 -0
  137. package/tunnel/wire-bytes.ts +102 -0
  138. package/vault/ARCHITECTURE.md +470 -0
  139. package/vault/LICENSE +33 -0
  140. package/vault/api-key.ts +190 -0
  141. package/vault/argon2.ts +106 -0
  142. package/vault/cli-auth-seal.ts +157 -0
  143. package/vault/cred-seal.ts +146 -0
  144. package/vault/credential.ts +55 -0
  145. package/vault/crypto.ts +210 -0
  146. package/vault/daemon-attest.ts +229 -0
  147. package/vault/derive.ts +113 -0
  148. package/vault/device-access.ts +87 -0
  149. package/vault/device-anchor.ts +185 -0
  150. package/vault/device-control.ts +174 -0
  151. package/vault/handoff.ts +396 -0
  152. package/vault/index.ts +21 -0
  153. package/vault/passkey.ts +243 -0
  154. package/vault/phrase.ts +65 -0
  155. package/vault/pin.ts +153 -0
  156. package/vault/public-key-seal.ts +131 -0
  157. package/vault/seed.ts +272 -0
  158. package/vault/team.ts +355 -0
  159. package/vault/vault-item.ts +131 -0
  160. package/vault/vault-migrate-digest.ts +29 -0
  161. package/vault/wrapped.ts +77 -0
  162. package/wire/ARCHITECTURE.md +90 -0
  163. package/wire/CONTRIBUTING.md +19 -0
  164. package/wire/LICENSE +33 -0
  165. package/wire/README.md +51 -0
  166. package/wire/adapters/messages/anthropic-map.ts +37 -0
  167. package/wire/adapters/messages/index.ts +7 -0
  168. package/wire/adapters/messages/reasoning-from-items.ts +38 -0
  169. package/wire/adapters/messages/reasoning-signature.ts +243 -0
  170. package/wire/adapters/messages/request.ts +574 -0
  171. package/wire/adapters/messages/response.ts +215 -0
  172. package/wire/adapters/messages/streaming.ts +1027 -0
  173. package/wire/adapters/messages/usage.ts +43 -0
  174. package/wire/adapters/responses/index.ts +3 -0
  175. package/wire/adapters/responses/request.ts +395 -0
  176. package/wire/adapters/responses/response.ts +88 -0
  177. package/wire/adapters/responses/streaming.ts +344 -0
  178. package/wire/assets/openllm-light.svg +18 -0
  179. package/wire/assets/openllm.svg +18 -0
  180. package/wire/features/account-rank.ts +166 -0
  181. package/wire/features/compaction/compact-request.ts +862 -0
  182. package/wire/features/compaction/compaction-text.ts +43 -0
  183. package/wire/features/context-demote.ts +47 -0
  184. package/wire/features/context-overflow-strategy.ts +26 -0
  185. package/wire/features/context-skip.ts +170 -0
  186. package/wire/features/conversation-affinity.ts +144 -0
  187. package/wire/features/max-tokens-backfill.ts +141 -0
  188. package/wire/features/model-caps.ts +53 -0
  189. package/wire/features/model-settings.ts +24 -0
  190. package/wire/features/output-limit-clamp.ts +64 -0
  191. package/wire/features/quota-gate.ts +85 -0
  192. package/wire/index.ts +44 -0
  193. package/wire/lib/attempt-outcome.ts +43 -0
  194. package/wire/lib/canonical/content-part.ts +38 -0
  195. package/wire/lib/canonical/count-tokens-capability.ts +138 -0
  196. package/wire/lib/canonical/encoding-select.ts +82 -0
  197. package/wire/lib/canonical/image-signature.ts +202 -0
  198. package/wire/lib/canonical/image-sse.ts +216 -0
  199. package/wire/lib/canonical/message.ts +43 -0
  200. package/wire/lib/canonical/token-estimate.ts +266 -0
  201. package/wire/lib/encrypted-content.ts +66 -0
  202. package/wire/lib/error-class.ts +154 -0
  203. package/wire/lib/error-envelope.ts +56 -0
  204. package/wire/lib/forwarded-headers.ts +180 -0
  205. package/wire/lib/provider-error-rules.ts +48 -0
  206. package/wire/lib/refusal.ts +38 -0
  207. package/wire/lib/responses-apply-patch.ts +104 -0
  208. package/wire/lib/responses-namespace.ts +53 -0
  209. package/wire/lib/secret-scrub.ts +101 -0
  210. package/wire/lib/streaming/accumulate.ts +221 -0
  211. package/wire/lib/streaming/finish-reason.ts +33 -0
  212. package/wire/lib/streaming/peek.ts +154 -0
  213. package/wire/lib/streaming/provider-decode.ts +259 -0
  214. package/wire/lib/streaming/response-stream.ts +71 -0
  215. package/wire/lib/streaming/sse.ts +699 -0
  216. package/wire/lib/streaming/strip-tool-calls.ts +95 -0
  217. package/wire/lib/streaming/upstream-error.ts +58 -0
  218. package/wire/lib/strip-subagent-isolation.ts +284 -0
  219. package/wire/lib/thinking-signature.ts +156 -0
  220. package/wire/lib/token-ruler/bpe-core.ts +426 -0
  221. package/wire/lib/token-ruler/index.ts +2 -0
  222. package/wire/lib/token-ruler/ruler.ts +79 -0
  223. package/wire/lib/token-ruler/vocab/claude-ranks.ts +16 -0
  224. package/wire/lib/token-ruler/vocab/claude.ts +22 -0
  225. package/wire/lib/token-ruler/vocab/o200k-ranks.ts +18 -0
  226. package/wire/lib/token-ruler/vocab/o200k.ts +19 -0
  227. package/wire/lib/tool-name-map.ts +271 -0
  228. package/wire/lib/tool-schema.ts +353 -0
  229. package/wire/lib/usage-observer.ts +125 -0
  230. package/wire/providers/anthropic/adaptive-thinking.ts +291 -0
  231. package/wire/providers/anthropic/beta-headers.ts +165 -0
  232. package/wire/providers/anthropic/index.ts +10 -0
  233. package/wire/providers/anthropic/request.ts +607 -0
  234. package/wire/providers/anthropic/response.ts +155 -0
  235. package/wire/providers/anthropic/streaming.ts +315 -0
  236. package/wire/providers/anthropic/web-search-domains.ts +49 -0
  237. package/wire/providers/chatgpt/common.ts +136 -0
  238. package/wire/providers/chatgpt/index.ts +9 -0
  239. package/wire/providers/chatgpt/request.ts +959 -0
  240. package/wire/providers/chatgpt/streaming.ts +1007 -0
  241. package/wire/providers/chatgpt/web-search.ts +63 -0
  242. package/wire/providers/grok/web-search.ts +87 -0
  243. package/wire/providers/kimi/web-search.ts +145 -0
  244. package/wire/providers/upstream-deny.ts +355 -0
  245. package/wire/providers/upstream-request.ts +619 -0
@@ -0,0 +1,19 @@
1
+ # Contributing to `@openllmsh/sdk/wire`
2
+
3
+ This repository is a **read-only mirror**, regenerated from the OpenLLM
4
+ monorepo on every release. Do not expect merges here to stick — the mirror's
5
+ `main` is force-updated from upstream each release.
6
+
7
+ **PRs are welcome and are ingested upstream.** A maintainer pulls your change
8
+ into the monorepo (your commit + authorship preserved), it goes through review
9
+ there, and the next release re-mirrors it back. Your PR is then closed as
10
+ "landed upstream in vX".
11
+
12
+ ## License of contributions
13
+
14
+ `wire` is **source-available under the Business Source License 1.1**
15
+ (BUSL-1.1), with OpenLLM, INC as the sole commercial Licensor. Because
16
+ inbound=outbound does not hold cleanly for BUSL, contributions require a
17
+ **Contributor License Agreement (CLA)** so OpenLLM, INC can keep granting
18
+ commercial licenses over — and perform the eventual MIT relicense of — the
19
+ contributed code. The CLA-assistant bot will prompt you on your first PR.
package/wire/LICENSE ADDED
@@ -0,0 +1,33 @@
1
+ Business Source License 1.1
2
+
3
+ Licensor: OpenLLM, INC
4
+ Licensed Work: OpenLLM Wire
5
+ The Licensed Work is (c) 2026 OpenLLM, INC.
6
+ Additional Use Grant: You may use the Licensed Work for internal business purposes, personal use, development, testing, evaluation, non-production deployments, and self-hosted production deployments that are not offered to third parties as a hosted, managed, or paid service that competes with OpenLLM or any substantially similar AI model gateway, proxy, credential-management, routing, memory, indexing, or inference orchestration product or service. In addition, further uses may be listed and defined at https://github.com/openllmsh/license-grants.
7
+ Change Date: The earlier of the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work, or a date specified at https://github.com/openllmsh/license-grants.
8
+ Change License: MIT License
9
+
10
+ Terms
11
+
12
+ The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use.
13
+
14
+ Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the Change License, and the rights granted in the paragraph above terminate.
15
+
16
+ If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work.
17
+
18
+ All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor.
19
+
20
+ You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from any third party, you must retain all copyright, patent, trademark, and attribution notices contained in the Licensed Work.
21
+
22
+ Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work.
23
+
24
+ This License does not grant you any right in any trademark or logo of Licensor or its affiliates.
25
+
26
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE.
27
+
28
+ Business Source License 1.1 Parameters
29
+
30
+ Licensor: OpenLLM, INC
31
+ Licensed Work: OpenLLM Wire
32
+ Change Date: The earlier of the fourth anniversary of first publication, or a date specified at https://github.com/openllmsh/license-grants.
33
+ Change License: MIT License
package/wire/README.md ADDED
@@ -0,0 +1,51 @@
1
+ <p align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: light)" srcset="./assets/openllm-light.svg">
4
+ <img alt="OpenLLM" src="./assets/openllm.svg" width="300">
5
+ </picture>
6
+ </p>
7
+
8
+ <p align="center"><b>wire</b> — the wire-format engine behind OpenLLM.</p>
9
+
10
+ <p align="center">
11
+ <a href="./LICENSE"><img alt="License: BUSL-1.1" src="https://img.shields.io/badge/license-BUSL--1.1-blue.svg"></a>
12
+ <img alt="source-available" src="https://img.shields.io/badge/source-available-informational.svg">
13
+ </p>
14
+
15
+ ---
16
+
17
+ The pure transforms that let one request speak every dialect — the
18
+ **Anthropic ↔ OpenAI ↔ Responses** translation layer, with no DI, no
19
+ providers, and no `fetch`. Same code the hosted gateway and the local
20
+ daemon both run:
21
+
22
+ - request / response / streaming adapters across all three wire formats
23
+ - reasoning-signature codec · canonical-message + token-estimate helpers
24
+ - SSE primitives (accumulate · tool-strip · provider-decode · heartbeat)
25
+ - the single upstream-request builder (model/stream pinning, Anthropic
26
+ adaptive-thinking + `anthropic-beta`)
27
+
28
+ Depends only on [`@openllmsh/sdk/protocol`](https://github.com/openllmsh/sdk)
29
+ + `effect`.
30
+
31
+ ## Install
32
+
33
+ ```sh
34
+ bun install github:openllmsh/sdk#<tag>
35
+ ```
36
+
37
+ ```ts
38
+ import { toAnthropicMessagesResponse } from "@openllmsh/sdk/wire/adapters/messages/response";
39
+ ```
40
+
41
+ ## License
42
+
43
+ **Source-available** under the [Business Source License 1.1](./LICENSE)
44
+ (© OpenLLM, INC) — use it freely except to run a competing hosted service;
45
+ converts to MIT on the Change Date. This is not OSI open-source.
46
+
47
+ ---
48
+
49
+ > **Read-only mirror.** Regenerated from the OpenLLM monorepo each release.
50
+ > PRs welcome — ingested upstream with your authorship preserved. BUSL
51
+ > contributions require the CLA (the bot will prompt you).
@@ -0,0 +1,37 @@
1
+ import type { TAnthropicStopReason } from "@openllmsh/sdk/protocol";
2
+
3
+ /**
4
+ * Visible answer after stripping a signed thought the model repeated
5
+ * as `content`. Prefix / exact restatement → hide; thought then extra
6
+ * → only the extra; anything else → the content as-is.
7
+ */
8
+ export const visibleAnswerAfterThought = (
9
+ contentSoFar: string,
10
+ thought: string,
11
+ ): string => {
12
+ if (thought.length === 0) return contentSoFar;
13
+ if (thought.startsWith(contentSoFar)) return "";
14
+ if (contentSoFar.startsWith(thought)) {
15
+ return contentSoFar.slice(thought.length);
16
+ }
17
+ return contentSoFar;
18
+ };
19
+
20
+ export const anthropicStopReasonFrom = (
21
+ finish: string | null | undefined,
22
+ ): TAnthropicStopReason | null => {
23
+ if (finish == null) return null;
24
+ switch (finish) {
25
+ case "stop":
26
+ return "end_turn";
27
+ case "length":
28
+ return "max_tokens";
29
+ case "tool_calls":
30
+ case "function_call":
31
+ return "tool_use";
32
+ case "content_filter":
33
+ return "refusal";
34
+ default:
35
+ return null;
36
+ }
37
+ };
@@ -0,0 +1,7 @@
1
+ export * from "./anthropic-map";
2
+ export * from "./reasoning-from-items";
3
+ export * from "./reasoning-signature";
4
+ export * from "./request";
5
+ export * from "./response";
6
+ export * from "./streaming";
7
+ export * from "./usage";
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Flattens LiteLLM-shaped `reasoning_items` summary text for Anthropic `/compact`.
3
+ *
4
+ * Ref:
5
+ * `ref/.litellm/litellm/litellm/completion_extras/litellm_responses_transformation/transformation.py`
6
+ * — `_build_reasoning_item` (dict branch ~78–82) builds each `summary` row as
7
+ * `{ "type": s.get("type", "summary_text"), "text": s.get("text", "") }`.
8
+ * `translate_responses_chunk_to_openai_stream` attaches those items on
9
+ * `response.completed` as `Delta(reasoning_items=...)` (~1321–1356).
10
+ *
11
+ * LiteLLM keeps structured `reasoning_items`; OpenLLM joins non-empty `text`
12
+ * fields with newlines so clients that require user-visible `text` still work.
13
+ */
14
+ export const plainTextFromReasoningItems = (
15
+ items: ReadonlyArray<unknown> | null | undefined,
16
+ ): string => {
17
+ if (items === undefined || items === null || !Array.isArray(items)) {
18
+ return "";
19
+ }
20
+ const parts: string[] = [];
21
+ for (const item of items) {
22
+ if (item === null || typeof item !== "object") continue;
23
+ const rec = item as Record<string, unknown>;
24
+ if (rec.type !== "reasoning") continue;
25
+ const summaryRaw = rec.summary;
26
+ if (!Array.isArray(summaryRaw)) continue;
27
+ for (const s of summaryRaw) {
28
+ if (typeof s === "object" && s !== null && !Array.isArray(s)) {
29
+ const o = s as Record<string, unknown>;
30
+ const text = typeof o.text === "string" ? o.text : "";
31
+ if (text.length > 0) {
32
+ parts.push(text);
33
+ }
34
+ }
35
+ }
36
+ }
37
+ return parts.join("\n");
38
+ };
@@ -0,0 +1,243 @@
1
+ /**
2
+ * Cross-provider reasoning round-trip codec.
3
+ *
4
+ * THE BUG THIS FIXES: when an Anthropic-format client (Claude Code) is
5
+ * served by a *reasoning* upstream on the OpenAI Responses API
6
+ * (ChatGPT/Codex, `store: false` + `include: ["reasoning.encrypted_content"]`),
7
+ * the model emits a `reasoning` output item whose `encrypted_content`
8
+ * encodes its chain-of-thought for the turn. The Responses API REQUIRES
9
+ * that item to be echoed back, in order, immediately before its
10
+ * `function_call`, on the next request. If it is dropped the model
11
+ * restarts reasoning every turn, re-derives the identical tool call,
12
+ * and loops forever (the "70k tokens, never exits" symptom).
13
+ *
14
+ * Anthropic's wire format has exactly one channel that a client
15
+ * preserves and replays verbatim across a tool turn: the `thinking`
16
+ * block's opaque `signature`. So we smuggle the litellm-shaped
17
+ * `reasoning` item(s) through that field — encoded here on the way out,
18
+ * decoded here on the way back in. Both directions go through this one
19
+ * module so the encode/decode contract can never drift.
20
+ *
21
+ * Mirrors litellm `completion_extras/litellm_responses_transformation/
22
+ * transformation.py`: `_build_reasoning_item` (~69-96) and
23
+ * `_reasoning_item_to_response_input` (~99-111).
24
+ */
25
+
26
+ // runtime-only: litellm `ChatCompletionReasoningItem` shape.
27
+ export type TReasoningSummaryPart = {
28
+ readonly type: string;
29
+ readonly text: string;
30
+ };
31
+ export type TReasoningItem = {
32
+ readonly id: string;
33
+ readonly type: "reasoning";
34
+ readonly encrypted_content: string | null;
35
+ readonly summary: ReadonlyArray<TReasoningSummaryPart>;
36
+ };
37
+
38
+ // runtime-only: a `reasoning` item in the Responses API `input` array.
39
+ export type TReasoningResponsesInput = {
40
+ readonly type: "reasoning";
41
+ readonly id?: string;
42
+ readonly summary: ReadonlyArray<TReasoningSummaryPart>;
43
+ readonly encrypted_content?: string;
44
+ };
45
+
46
+ /**
47
+ * Tag prefix so {@link decodeReasoningSignature} only ever consumes a
48
+ * signature WE produced. A genuine Anthropic `signature` (native
49
+ * pass-through path) lacks this prefix and is left untouched.
50
+ */
51
+ const SIGNATURE_PREFIX = "openllm-rs1:";
52
+ const NATIVE_ASSISTANT_SIGNATURE_PREFIX = "openllm-cn1:";
53
+ export const CLAUDE_NATIVE_ASSISTANT_CARRIER = "claude_native_assistant";
54
+ export const CLAUDE_NATIVE_ASSISTANT_PENDING =
55
+ "claude_native_assistant_pending";
56
+
57
+ /** Native assistant history is NOT Responses ciphertext. Keep its wire
58
+ * envelope separate; the Claude facade validates model/projection/blocks
59
+ * before restoring it. This codec only transports the opaque carrier. */
60
+ export const encodeNativeAssistantSignature = (
61
+ items: ReadonlyArray<unknown> | null | undefined,
62
+ ): string | null => {
63
+ const native = (items ?? []).filter(
64
+ (item) =>
65
+ typeof item === "object" &&
66
+ item !== null &&
67
+ (item as { type?: unknown }).type === CLAUDE_NATIVE_ASSISTANT_CARRIER,
68
+ );
69
+ return native.length > 0
70
+ ? NATIVE_ASSISTANT_SIGNATURE_PREFIX + toBase64(JSON.stringify(native))
71
+ : null;
72
+ };
73
+
74
+ export const decodeNativeAssistantSignature = (
75
+ signature: string | null | undefined,
76
+ ): ReadonlyArray<unknown> | null => {
77
+ if (
78
+ typeof signature !== "string" ||
79
+ !signature.startsWith(NATIVE_ASSISTANT_SIGNATURE_PREFIX)
80
+ )
81
+ return null;
82
+ try {
83
+ const parsed: unknown = JSON.parse(
84
+ fromBase64(signature.slice(NATIVE_ASSISTANT_SIGNATURE_PREFIX.length)),
85
+ );
86
+ if (!Array.isArray(parsed) || parsed.length === 0) return null;
87
+ return parsed.every(
88
+ (item) =>
89
+ typeof item === "object" &&
90
+ item !== null &&
91
+ (item as { type?: unknown }).type === CLAUDE_NATIVE_ASSISTANT_CARRIER,
92
+ )
93
+ ? parsed
94
+ : null;
95
+ } catch {
96
+ return null;
97
+ }
98
+ };
99
+
100
+ const toBase64 = (s: string): string => {
101
+ if (typeof Buffer !== "undefined") {
102
+ return Buffer.from(s, "utf8").toString("base64");
103
+ }
104
+ return btoa(unescape(encodeURIComponent(s)));
105
+ };
106
+
107
+ const fromBase64 = (s: string): string => {
108
+ if (typeof Buffer !== "undefined") {
109
+ return Buffer.from(s, "base64").toString("utf8");
110
+ }
111
+ return decodeURIComponent(escape(atob(s)));
112
+ };
113
+
114
+ const normalizeSummary = (raw: unknown): TReasoningSummaryPart[] => {
115
+ if (!Array.isArray(raw)) return [];
116
+ const out: TReasoningSummaryPart[] = [];
117
+ for (const s of raw) {
118
+ if (s !== null && typeof s === "object" && !Array.isArray(s)) {
119
+ const o = s as Record<string, unknown>;
120
+ out.push({
121
+ type: typeof o.type === "string" ? o.type : "summary_text",
122
+ text: typeof o.text === "string" ? o.text : "",
123
+ });
124
+ }
125
+ }
126
+ return out;
127
+ };
128
+
129
+ /**
130
+ * Build a {@link TReasoningItem} from raw Responses-API output. Mirrors
131
+ * litellm `_build_reasoning_item`.
132
+ */
133
+ export const buildReasoningItem = (
134
+ id: string,
135
+ encryptedContent: string | null | undefined,
136
+ summaryRaw: unknown,
137
+ ): TReasoningItem => ({
138
+ id,
139
+ type: "reasoning",
140
+ encrypted_content:
141
+ typeof encryptedContent === "string" ? encryptedContent : null,
142
+ summary: normalizeSummary(summaryRaw),
143
+ });
144
+
145
+ /**
146
+ * Coerce the canonical `reasoning_items` carrier (`S.Array(S.Unknown)`
147
+ * on the chat schema) into typed reasoning items. The single
148
+ * normalizer used everywhere a reasoning item leaves the canonical
149
+ * shape (Anthropic signature emit + Responses `input` build).
150
+ */
151
+ export const reasoningItemsFromUnknown = (
152
+ items: ReadonlyArray<unknown> | null | undefined,
153
+ ): TReasoningItem[] => {
154
+ if (items === undefined || items === null || !Array.isArray(items)) {
155
+ return [];
156
+ }
157
+ const out: TReasoningItem[] = [];
158
+ for (const item of items) {
159
+ if (item === null || typeof item !== "object") continue;
160
+ const rec = item as Record<string, unknown>;
161
+ if (rec.type !== "reasoning") continue;
162
+ out.push(
163
+ buildReasoningItem(
164
+ typeof rec.id === "string" ? rec.id : "",
165
+ typeof rec.encrypted_content === "string"
166
+ ? rec.encrypted_content
167
+ : null,
168
+ rec.summary,
169
+ ),
170
+ );
171
+ }
172
+ return out;
173
+ };
174
+
175
+ /**
176
+ * Encode reasoning items into an Anthropic `thinking.signature`. Returns
177
+ * `null` when there is nothing worth round-tripping (no item carries
178
+ * `encrypted_content`, so the upstream cannot resume from it anyway).
179
+ */
180
+ export const encodeReasoningSignature = (
181
+ items: ReadonlyArray<TReasoningItem>,
182
+ ): string | null => {
183
+ const withState = items.filter(
184
+ (i) =>
185
+ typeof i.encrypted_content === "string" && i.encrypted_content.length > 0,
186
+ );
187
+ if (withState.length === 0) return null;
188
+ return SIGNATURE_PREFIX + toBase64(JSON.stringify(withState));
189
+ };
190
+
191
+ /**
192
+ * Decode a `thinking.signature` produced by
193
+ * {@link encodeReasoningSignature}. Returns `null` for any signature we
194
+ * did not produce (genuine Anthropic signatures, malformed input) so
195
+ * callers fall back to their existing `reasoning_content` text path.
196
+ */
197
+ export const decodeReasoningSignature = (
198
+ signature: string | null | undefined,
199
+ ): TReasoningItem[] | null => {
200
+ if (
201
+ typeof signature !== "string" ||
202
+ !signature.startsWith(SIGNATURE_PREFIX)
203
+ ) {
204
+ return null;
205
+ }
206
+ try {
207
+ const json = fromBase64(signature.slice(SIGNATURE_PREFIX.length));
208
+ const parsed: unknown = JSON.parse(json);
209
+ if (!Array.isArray(parsed)) return null;
210
+ const items = reasoningItemsFromUnknown(parsed);
211
+ return items.length > 0 ? items : null;
212
+ } catch {
213
+ return null;
214
+ }
215
+ };
216
+
217
+ /**
218
+ * Convert a stored reasoning item back to a Responses API `input` item.
219
+ * Mirrors litellm `_reasoning_item_to_response_input`: `summary` is
220
+ * always required (even empty); `encrypted_content` is only sent when
221
+ * present. The ChatGPT/Codex encoder always sends `store: false`, so an
222
+ * item without resumable ciphertext must not name an unpersisted item via
223
+ * `id` (the upstream rejects that combination).
224
+ */
225
+ export const reasoningItemToResponsesInput = (
226
+ item: TReasoningItem,
227
+ ): TReasoningResponsesInput => {
228
+ const encryptedContent =
229
+ typeof item.encrypted_content === "string" &&
230
+ item.encrypted_content.length > 0
231
+ ? item.encrypted_content
232
+ : null;
233
+ return {
234
+ type: "reasoning",
235
+ summary: item.summary,
236
+ ...(encryptedContent !== null
237
+ ? {
238
+ id: item.id.length > 0 ? item.id : `rs_${crypto.randomUUID()}`,
239
+ encrypted_content: encryptedContent,
240
+ }
241
+ : {}),
242
+ };
243
+ };