@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,95 @@
1
+ import type {
2
+ TChatCompletionChunk,
3
+ TChatCompletionResponse,
4
+ } from "@openllmsh/sdk/protocol";
5
+
6
+ /**
7
+ * Claude Code `/compact` uses `Xm()` which requires usable summary text.
8
+ * cf. https://github.com/anthropics/claude-code/issues/11095
9
+ *
10
+ * Some OpenAI-compatible providers still stream `tool_calls` when the request
11
+ * omits `tools` (conversation history is tool-heavy). That yields Anthropic
12
+ * `tool_use` + `stop_reason: tool_use` and breaks compaction extraction even
13
+ * with plenty of `text` deltas.
14
+ */
15
+ export const coerceOpenAiFinishReasonAfterStrippingToolCalls = (
16
+ fr: string | null | undefined,
17
+ ): string | null | undefined => {
18
+ if (fr === "tool_calls" || fr === "function_call") return "stop";
19
+ return fr;
20
+ };
21
+
22
+ export const stripToolCallDeltasFromChunk = (
23
+ chunk: TChatCompletionChunk,
24
+ ): TChatCompletionChunk => ({
25
+ ...chunk,
26
+ choices: chunk.choices.map((choice, idx) => {
27
+ if (idx !== 0) return choice;
28
+ const nextFr = coerceOpenAiFinishReasonAfterStrippingToolCalls(
29
+ choice.finish_reason,
30
+ ) as TChatCompletionChunk["choices"][number]["finish_reason"] | undefined;
31
+ const delta = choice.delta;
32
+ const toolCalls = delta.tool_calls;
33
+ if (
34
+ toolCalls === undefined ||
35
+ toolCalls === null ||
36
+ toolCalls.length === 0
37
+ ) {
38
+ if (nextFr === choice.finish_reason) return choice;
39
+ return { ...choice, finish_reason: nextFr };
40
+ }
41
+ const { tool_calls: _omit, ...restDelta } = delta;
42
+ return {
43
+ ...choice,
44
+ finish_reason: nextFr,
45
+ delta: restDelta,
46
+ };
47
+ }),
48
+ });
49
+
50
+ export const stripToolCallDeltasFromChunkStream = (
51
+ source: ReadableStream<TChatCompletionChunk>,
52
+ ): ReadableStream<TChatCompletionChunk> => {
53
+ const reader = source.getReader();
54
+ return new ReadableStream<TChatCompletionChunk>({
55
+ async pull(controller) {
56
+ const { value, done } = await reader.read();
57
+ if (done) {
58
+ controller.close();
59
+ return;
60
+ }
61
+ controller.enqueue(stripToolCallDeltasFromChunk(value));
62
+ },
63
+ cancel(reason) {
64
+ reader.cancel(reason).catch(() => {});
65
+ },
66
+ });
67
+ };
68
+
69
+ export const stripAssistantToolCallsFromChatCompletion = (
70
+ resp: TChatCompletionResponse,
71
+ ): TChatCompletionResponse => {
72
+ const choice = resp.choices[0];
73
+ if (choice === undefined) return resp;
74
+ const nextFr = coerceOpenAiFinishReasonAfterStrippingToolCalls(
75
+ choice.finish_reason,
76
+ ) as TChatCompletionResponse["choices"][number]["finish_reason"];
77
+ const tc = choice.message.tool_calls;
78
+ if (
79
+ (tc === undefined || tc.length === 0) &&
80
+ nextFr === choice.finish_reason
81
+ ) {
82
+ return resp;
83
+ }
84
+ const { tool_calls: _t, ...restMessage } = choice.message;
85
+ return {
86
+ ...resp,
87
+ choices: [
88
+ {
89
+ ...choice,
90
+ finish_reason: nextFr,
91
+ message: restMessage,
92
+ },
93
+ ],
94
+ };
95
+ };
@@ -0,0 +1,58 @@
1
+ import type { TChatCompletionResponse } from "@openllmsh/sdk/protocol";
2
+
3
+ /**
4
+ * Thrown by provider streaming decoders when the upstream sends an
5
+ * error event mid-stream (e.g. Anthropic's `event: error` for
6
+ * overloaded_error / api_error). Surfaces through the canonical
7
+ * `ReadableStream<TChatCompletionChunk>` so the surface-specific SSE
8
+ * encoder can emit a trailing error frame instead of silently
9
+ * truncating the response.
10
+ */
11
+ export class UpstreamStreamError extends Error {
12
+ readonly upstreamType: string;
13
+ constructor(upstreamType: string, message: string) {
14
+ super(message);
15
+ this.name = "UpstreamStreamError";
16
+ this.upstreamType = upstreamType;
17
+ }
18
+ }
19
+
20
+ /**
21
+ * A stream ended before a terminal chunk after its decoder had already observed
22
+ * usage. The accumulator wraps every failure type, including transport errors,
23
+ * so callers can retain that wire-observed usage without treating an incomplete
24
+ * answer as successful.
25
+ */
26
+ export class IncompleteStreamError extends Error {
27
+ readonly cause: unknown;
28
+ readonly usage: TChatCompletionResponse["usage"] | null;
29
+
30
+ constructor(cause: unknown, usage: TChatCompletionResponse["usage"] | null) {
31
+ super(cause instanceof Error ? cause.message : String(cause));
32
+ this.name = "IncompleteStreamError";
33
+ this.cause = cause;
34
+ this.usage = usage;
35
+ }
36
+ }
37
+
38
+ /** Return usage observed before an incomplete stream error, if any. */
39
+ export const partialUsageFrom = (
40
+ err: unknown,
41
+ ): TChatCompletionResponse["usage"] | null =>
42
+ err instanceof IncompleteStreamError ? err.usage : null;
43
+
44
+ /** Recover the original decoder or transport error from an accumulator wrapper. */
45
+ export const streamFailureCause = (err: unknown): unknown =>
46
+ err instanceof IncompleteStreamError ? err.cause : err;
47
+
48
+ export const upstreamErrorFrom = (
49
+ err: unknown,
50
+ ): { type: string; message: string } => {
51
+ if (err instanceof UpstreamStreamError) {
52
+ return { type: err.upstreamType, message: err.message };
53
+ }
54
+ if (err instanceof Error) {
55
+ return { type: "stream_error", message: err.message };
56
+ }
57
+ return { type: "stream_error", message: String(err) };
58
+ };
@@ -0,0 +1,284 @@
1
+ import type {
2
+ TChatCompletionChunk,
3
+ TChatCompletionResponse,
4
+ } from "@openllmsh/sdk/protocol";
5
+
6
+ const SUBAGENT_TOOL_NAMES = new Set<string>(["Agent", "Task"]);
7
+
8
+ // The shared streaming accumulator reconstructs only choice 0, so the
9
+ // streaming strip scopes its target discovery and edits to that choice.
10
+ // A secondary choice (OpenAI `n > 1`) reuses per-choice tool-call
11
+ // indices, so touching it by a choice-agnostic index would corrupt an
12
+ // unrelated call. The non-streaming path matches by tool NAME per choice
13
+ // and is safe across all choices.
14
+ const TARGET_CHOICE_INDEX = 0;
15
+
16
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
17
+ typeof value === "object" && value !== null && !Array.isArray(value);
18
+
19
+ const stripIsolationArguments = (argumentsJson: string): string => {
20
+ try {
21
+ const parsed: unknown = JSON.parse(argumentsJson);
22
+ if (
23
+ !isRecord(parsed) ||
24
+ (!Object.hasOwn(parsed, "isolation") && !Object.hasOwn(parsed, "cwd"))
25
+ ) {
26
+ return argumentsJson;
27
+ }
28
+ delete parsed.isolation;
29
+ delete parsed.cwd;
30
+ return JSON.stringify(parsed);
31
+ } catch {
32
+ return argumentsJson;
33
+ }
34
+ };
35
+
36
+ /**
37
+ * Drops Claude Code's model-emitted worktree controls from Task/Agent calls.
38
+ * Invalid JSON and calls without either field are returned byte-for-byte.
39
+ */
40
+ export const stripIsolationFromResponse = (
41
+ response: TChatCompletionResponse,
42
+ ): TChatCompletionResponse => {
43
+ let changed = false;
44
+ const choices = response.choices.map((choice) => {
45
+ const toolCalls = choice.message.tool_calls;
46
+ if (
47
+ toolCalls === undefined ||
48
+ toolCalls === null ||
49
+ toolCalls.length === 0
50
+ ) {
51
+ return choice;
52
+ }
53
+ let choiceChanged = false;
54
+ const nextToolCalls = toolCalls.map((toolCall) => {
55
+ if (!SUBAGENT_TOOL_NAMES.has(toolCall.function.name)) return toolCall;
56
+ const argumentsJson = stripIsolationArguments(
57
+ toolCall.function.arguments,
58
+ );
59
+ if (argumentsJson === toolCall.function.arguments) return toolCall;
60
+ choiceChanged = true;
61
+ changed = true;
62
+ return {
63
+ ...toolCall,
64
+ function: { ...toolCall.function, arguments: argumentsJson },
65
+ };
66
+ });
67
+ return choiceChanged
68
+ ? {
69
+ ...choice,
70
+ message: { ...choice.message, tool_calls: nextToolCalls },
71
+ }
72
+ : choice;
73
+ });
74
+ return changed ? { ...response, choices } : response;
75
+ };
76
+
77
+ type TTargetToolCall = {
78
+ readonly index: number;
79
+ readonly arguments: string;
80
+ };
81
+
82
+ /**
83
+ * Cheap path: concatenate choice-0 tool-call argument deltas directly.
84
+ * Avoids `accumulateChunksToResponse` (full chat-completion rebuild) when
85
+ * every target name is visible on the wire and args are plain string joins.
86
+ */
87
+ const targetToolCallsFromDeltas = (
88
+ chunks: ReadonlyArray<TChatCompletionChunk>,
89
+ ): ReadonlyArray<TTargetToolCall> => {
90
+ type TAcc = {
91
+ name: string;
92
+ arguments: string;
93
+ };
94
+ const byIndex = new Map<number, TAcc>();
95
+ for (const chunk of chunks) {
96
+ for (const choice of chunk.choices) {
97
+ if (choice.index !== TARGET_CHOICE_INDEX) continue;
98
+ for (const toolCall of choice.delta.tool_calls ?? []) {
99
+ const existing = byIndex.get(toolCall.index);
100
+ const nameDelta = toolCall.function?.name ?? undefined;
101
+ const argsDelta = toolCall.function?.arguments ?? undefined;
102
+ if (existing === undefined) {
103
+ byIndex.set(toolCall.index, {
104
+ name: nameDelta ?? "",
105
+ arguments: argsDelta ?? "",
106
+ });
107
+ } else {
108
+ if (nameDelta !== undefined && nameDelta.length > 0) {
109
+ existing.name = nameDelta;
110
+ }
111
+ if (argsDelta !== undefined) {
112
+ existing.arguments += argsDelta;
113
+ }
114
+ }
115
+ }
116
+ }
117
+ }
118
+ if (byIndex.size === 0) return [];
119
+ const out: TTargetToolCall[] = [];
120
+ const ordered = [...byIndex.entries()].sort((a, b) => a[0] - b[0]);
121
+ for (const [index, acc] of ordered) {
122
+ if (!SUBAGENT_TOOL_NAMES.has(acc.name)) continue;
123
+ const argumentsJson = stripIsolationArguments(acc.arguments);
124
+ if (argumentsJson === acc.arguments) continue;
125
+ out.push({ index, arguments: argumentsJson });
126
+ }
127
+ return out;
128
+ };
129
+
130
+ const targetToolCallsFrom = (
131
+ chunks: ReadonlyArray<TChatCompletionChunk>,
132
+ ): ReadonlyArray<TTargetToolCall> => targetToolCallsFromDeltas(chunks);
133
+
134
+ const withoutTargetArguments = (
135
+ chunk: TChatCompletionChunk,
136
+ targetIndices: ReadonlySet<number>,
137
+ ): TChatCompletionChunk => {
138
+ let chunkChanged = false;
139
+ const choices = chunk.choices.map((choice) => {
140
+ // Targets live in choice 0 only (see `targetToolCallsFrom`); leave a
141
+ // secondary choice's tool calls — which may reuse the same index —
142
+ // byte-for-byte.
143
+ if (choice.index !== TARGET_CHOICE_INDEX) return choice;
144
+ const toolCalls = choice.delta.tool_calls;
145
+ if (toolCalls === undefined || toolCalls === null || toolCalls.length === 0)
146
+ return choice;
147
+ let choiceChanged = false;
148
+ const nextToolCalls = toolCalls.map((toolCall) => {
149
+ if (
150
+ !targetIndices.has(toolCall.index) ||
151
+ toolCall.function?.arguments === undefined
152
+ ) {
153
+ return toolCall;
154
+ }
155
+ choiceChanged = true;
156
+ chunkChanged = true;
157
+ const { arguments: _arguments, ...functionRest } = toolCall.function;
158
+ return { ...toolCall, function: functionRest };
159
+ });
160
+ return choiceChanged
161
+ ? { ...choice, delta: { ...choice.delta, tool_calls: nextToolCalls } }
162
+ : choice;
163
+ });
164
+ return chunkChanged ? { ...chunk, choices } : chunk;
165
+ };
166
+
167
+ const replacementChunk = (
168
+ template: TChatCompletionChunk,
169
+ targetToolCalls: ReadonlyArray<TTargetToolCall>,
170
+ ): TChatCompletionChunk => ({
171
+ id: template.id,
172
+ object: "chat.completion.chunk",
173
+ created: template.created,
174
+ model: template.model,
175
+ choices: [
176
+ {
177
+ index: 0,
178
+ delta: {
179
+ tool_calls: targetToolCalls.map((toolCall) => ({
180
+ index: toolCall.index,
181
+ function: { arguments: toolCall.arguments },
182
+ })),
183
+ },
184
+ },
185
+ ],
186
+ });
187
+
188
+ const endsTurn = (chunk: TChatCompletionChunk): boolean =>
189
+ chunk.choices.some((choice) => choice.finish_reason != null);
190
+
191
+ const hasToolCallDelta = (chunk: TChatCompletionChunk): boolean =>
192
+ chunk.choices.some((choice) => {
193
+ const toolCalls = choice.delta.tool_calls;
194
+ return (
195
+ toolCalls !== undefined && toolCalls !== null && toolCalls.length > 0
196
+ );
197
+ });
198
+
199
+ /**
200
+ * Reassemble only the tool-call tail. Buffering starts at `first` (the
201
+ * first chunk carrying tool-call deltas); everything before it has
202
+ * already been forwarded. Drains the rest of the source, reconstructs
203
+ * the Task/Agent arguments with the shared wire accumulator, and — when
204
+ * a target is found — emits one clean replacement delta before the
205
+ * terminal chunk. No target (or malformed JSON) → the tail is returned
206
+ * byte-for-byte.
207
+ */
208
+ const rebuildToolCallTail = async (
209
+ first: TChatCompletionChunk,
210
+ reader: ReadableStreamDefaultReader<TChatCompletionChunk>,
211
+ ): Promise<TChatCompletionChunk[]> => {
212
+ const buffered: TChatCompletionChunk[] = [first];
213
+ for (;;) {
214
+ const { value, done } = await reader.read();
215
+ if (done) break;
216
+ buffered.push(value);
217
+ }
218
+ const targetToolCalls = targetToolCallsFrom(buffered);
219
+ const template = buffered[0];
220
+ if (targetToolCalls.length === 0 || template === undefined) {
221
+ return buffered;
222
+ }
223
+ const targetIndices = new Set(
224
+ targetToolCalls.map((toolCall) => toolCall.index),
225
+ );
226
+ const replacement = replacementChunk(template, targetToolCalls);
227
+ let emittedReplacement = false;
228
+ const output: TChatCompletionChunk[] = [];
229
+ for (const nextChunk of buffered) {
230
+ if (!emittedReplacement && endsTurn(nextChunk)) {
231
+ output.push(replacement);
232
+ emittedReplacement = true;
233
+ }
234
+ output.push(withoutTargetArguments(nextChunk, targetIndices));
235
+ }
236
+ if (!emittedReplacement) output.push(replacement);
237
+ return output;
238
+ };
239
+
240
+ /**
241
+ * Streams through chunks that precede the first tool call untouched — a
242
+ * leading role/content chunk reaches the client immediately, not after
243
+ * the whole turn is buffered. From the first tool-call delta onward the
244
+ * tail is buffered and reassembled (see `rebuildToolCallTail`): the two
245
+ * controls are removed from complete Task/Agent JSON and one clean
246
+ * replacement delta is emitted before the terminal chunk. Malformed
247
+ * arguments and non-target tool calls remain byte-for-byte.
248
+ */
249
+ export const stripIsolationFromChunks = (
250
+ source: ReadableStream<TChatCompletionChunk>,
251
+ ): ReadableStream<TChatCompletionChunk> => {
252
+ const reader = source.getReader();
253
+ let tail: TChatCompletionChunk[] | null = null;
254
+
255
+ return new ReadableStream<TChatCompletionChunk>({
256
+ async pull(controller): Promise<void> {
257
+ // Draining the reconstructed tool-call tail.
258
+ if (tail !== null) {
259
+ const next = tail.shift();
260
+ if (next === undefined) controller.close();
261
+ else controller.enqueue(next);
262
+ return;
263
+ }
264
+ // Prelude: forward one chunk per pull until a tool call appears.
265
+ const { value, done } = await reader.read();
266
+ if (done) {
267
+ controller.close();
268
+ return;
269
+ }
270
+ if (!hasToolCallDelta(value)) {
271
+ controller.enqueue(value);
272
+ return;
273
+ }
274
+ // First tool-call chunk: buffer + reconstruct from here on.
275
+ tail = await rebuildToolCallTail(value, reader);
276
+ const next = tail.shift();
277
+ if (next === undefined) controller.close();
278
+ else controller.enqueue(next);
279
+ },
280
+ cancel(reason) {
281
+ reader.cancel(reason).catch(() => {});
282
+ },
283
+ });
284
+ };
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Recovery for a replayed Anthropic `thinking`/`redacted_thinking` block whose
3
+ * `signature` the upstream rejects. Anthropic thinking signatures are bound to
4
+ * the serving model (and account). A multi-turn fallback that hops
5
+ * `claude_code` models (e.g. fable → opus → sonnet) replays a prior hop's
6
+ * thinking into a hop that can't verify the signature → pre-stream HTTP 400:
7
+ * `messages.N.content.M: Invalid \`signature\` in \`thinking\` block`
8
+ *
9
+ * Anthropic's own model-switch guidance is to strip `thinking` and
10
+ * `redacted_thinking` from prior assistant turns (blocks are model-tied).
11
+ * Stripping and retrying the same hop once lets the model re-reason instead of
12
+ * knocking out the entire claude_code arm of the chain — the Anthropic-wire
13
+ * analog of {@link ./encrypted-content.ts}'s Responses `encrypted_content`
14
+ * strip-retry (issue #420).
15
+ *
16
+ * ## Strip scope (cache + tool_use)
17
+ *
18
+ * - **Deterministic + minimal.** Only `thinking` / `redacted_thinking` content
19
+ * blocks are removed. Message order, system/tools/user content, and all other
20
+ * assistant blocks (`text`, `tool_use`, …) are left untouched so the stripped
21
+ * prefix is stable across same-model turns (Anthropic prompt-cache matches an
22
+ * exact per-model prefix).
23
+ * - **Historical only by default.** Thinking on the *active tool-use
24
+ * continuation* assistant turn is PRESERVED: Anthropic requires the final
25
+ * assistant turn's thinking block when that turn carries `tool_use` and the
26
+ * next user turn answers with `tool_result`. Blindly stripping that block
27
+ * breaks the tool loop (`thinking`/`redacted_thinking` blocks in the latest
28
+ * assistant message cannot be modified / must round-trip).
29
+ * - **Limitation (documented, not papered over).** If the preserved final-turn
30
+ * thinking itself carries a foreign-model signature, the same 400 will fire
31
+ * again after strip (we deliberately did not touch it). Cross-model
32
+ * mid-tool-loop continuation cannot be made valid: keeping the block 400s on
33
+ * signature, stripping it 400s on tool-use integrity. The reactive retry then
34
+ * surfaces the original error and the walker advances / fails normally. Same-
35
+ * model tool_use continuation is unaffected (signature still verifies).
36
+ * - **No proactive strip.** Some signature forms embed their minting model
37
+ * (e.g. Anthropic CAIS), so origin is not always opaque — but the origin
38
+ * model is not the deciding factor: only the TARGET model's acceptance is,
39
+ * and that is knowable only from its response. Same-family signatures are
40
+ * frequently accepted, so proactively stripping on every model hop would
41
+ * bust prompt cache on healthy same-model multi-turn for no reason. The
42
+ * reactive 400 → strip → retry path strips only when the upstream actually
43
+ * rejects, which is the correctness backstop.
44
+ */
45
+
46
+ /** Anthropic 400 body for an undecryptable / foreign-model thinking signature.
47
+ * Matches the documented phrasing and tolerates the `messages.N.content.M: `
48
+ * prefix Anthropic attaches; also accepts a bare `Invalid \`signature\` in
49
+ * \`thinking\` block` substring. */
50
+ const THINKING_SIGNATURE_FAILURE_RE =
51
+ /Invalid\s*`signature`\s*in\s*`thinking`\s*block/i;
52
+
53
+ export const isThinkingSignatureError = (rawBody: string): boolean =>
54
+ rawBody.length > 0 && THINKING_SIGNATURE_FAILURE_RE.test(rawBody);
55
+
56
+ const isThinkingBlock = (block: unknown): boolean => {
57
+ if (block === null || typeof block !== "object") return false;
58
+ const type = (block as { type?: unknown }).type;
59
+ return type === "thinking" || type === "redacted_thinking";
60
+ };
61
+
62
+ const isToolUseBlock = (block: unknown): boolean =>
63
+ block !== null &&
64
+ typeof block === "object" &&
65
+ (block as { type?: unknown }).type === "tool_use";
66
+
67
+ const isToolResultBlock = (block: unknown): boolean =>
68
+ block !== null &&
69
+ typeof block === "object" &&
70
+ (block as { type?: unknown }).type === "tool_result";
71
+
72
+ const messageContent = (msg: unknown): unknown[] | null => {
73
+ if (msg === null || typeof msg !== "object") return null;
74
+ const content = (msg as { content?: unknown }).content;
75
+ return Array.isArray(content) ? content : null;
76
+ };
77
+
78
+ const isAssistantWithThinking = (msg: unknown): boolean => {
79
+ if (msg === null || typeof msg !== "object") return false;
80
+ if ((msg as { role?: unknown }).role !== "assistant") return false;
81
+ const content = messageContent(msg);
82
+ return content !== null && content.some(isThinkingBlock);
83
+ };
84
+
85
+ /**
86
+ * True when `messages[i]` is the pending tool-use continuation Anthropic
87
+ * requires thinking to round-trip for. That pair must occupy the FINAL TWO
88
+ * messages: `messages[len-2]` is an assistant turn carrying `tool_use` and
89
+ * `messages[len-1]` is a user turn carrying `tool_result`. Only index
90
+ * `len-2` is active — every earlier assistant-with-thinking (including a
91
+ * completed tool loop that is merely the latest tool_use/tool_result pair
92
+ * but is followed by more turns) is historical and safe to strip.
93
+ */
94
+ const isActiveToolUseContinuation = (
95
+ messages: ReadonlyArray<unknown>,
96
+ index: number,
97
+ ): boolean => {
98
+ // Active only when this message is the penultimate one (final two messages).
99
+ if (messages.length < 2 || index !== messages.length - 2) return false;
100
+
101
+ const cur = messages[index];
102
+ const next = messages[index + 1];
103
+ if (cur === null || typeof cur !== "object") return false;
104
+ if (next === null || typeof next !== "object") return false;
105
+ if ((cur as { role?: unknown }).role !== "assistant") return false;
106
+ if ((next as { role?: unknown }).role !== "user") return false;
107
+
108
+ const curContent = messageContent(cur);
109
+ const nextContent = messageContent(next);
110
+ if (curContent === null || nextContent === null) return false;
111
+ return curContent.some(isToolUseBlock) && nextContent.some(isToolResultBlock);
112
+ };
113
+
114
+ /** True when a built Anthropic Messages body carries at least one assistant
115
+ * `thinking`/`redacted_thinking` block that the strip transform would remove
116
+ * (i.e. not solely the preserved active tool-use continuation). A retry is
117
+ * only worth attempting when stripping would change the request. */
118
+ export const messagesBodyHasStrippableThinking = (body: unknown): boolean => {
119
+ if (body === null || typeof body !== "object") return false;
120
+ const messages = (body as { messages?: unknown }).messages;
121
+ if (!Array.isArray(messages)) return false;
122
+ return messages.some(
123
+ (msg, i) =>
124
+ isAssistantWithThinking(msg) && !isActiveToolUseContinuation(messages, i),
125
+ );
126
+ };
127
+
128
+ /** Return a copy of a built Anthropic Messages body with `thinking` and
129
+ * `redacted_thinking` blocks removed from every HISTORICAL assistant message.
130
+ * The active tool-use continuation assistant turn (tool_use answered by a
131
+ * trailing tool_result) is left intact — see file header. Other content
132
+ * blocks and non-assistant messages are untouched; order is preserved. */
133
+ export const stripMessagesThinkingBlocks = (body: unknown): unknown => {
134
+ if (body === null || typeof body !== "object") return body;
135
+ const b = body as { messages?: unknown };
136
+ if (!Array.isArray(b.messages)) return body;
137
+
138
+ const messages = b.messages.map((msg, i) => {
139
+ if (msg === null || typeof msg !== "object") return msg;
140
+ if ((msg as { role?: unknown }).role !== "assistant") return msg;
141
+ if (isActiveToolUseContinuation(b.messages as ReadonlyArray<unknown>, i)) {
142
+ return msg;
143
+ }
144
+ const content = messageContent(msg);
145
+ if (content === null || !content.some(isThinkingBlock)) return msg;
146
+ // Drop only thinking / redacted_thinking; keep text, tool_use, etc. in order.
147
+ // Empty content after strip is left as `[]` — rare (assistant-only-thinking
148
+ // turns); inventing a placeholder would be non-deterministic / semantic.
149
+ return {
150
+ ...(msg as Record<string, unknown>),
151
+ content: content.filter((block) => !isThinkingBlock(block)),
152
+ };
153
+ });
154
+
155
+ return { ...(body as Record<string, unknown>), messages };
156
+ };