@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.0

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,1027 @@
1
+ import type {
2
+ TAnthropicStreamEvent,
3
+ TChatCompletionChunk,
4
+ } from "@openllmsh/sdk/protocol";
5
+ import { ensureCompactionSafeVisibleText } from "../../features/compaction/compaction-text";
6
+ import { encodeSseEvent } from "../../lib/streaming/sse";
7
+ import { upstreamErrorFrom } from "../../lib/streaming/upstream-error";
8
+ import {
9
+ anthropicStopReasonFrom,
10
+ visibleAnswerAfterThought,
11
+ } from "./anthropic-map";
12
+ import { plainTextFromReasoningItems } from "./reasoning-from-items";
13
+ import {
14
+ CLAUDE_NATIVE_ASSISTANT_CARRIER,
15
+ CLAUDE_NATIVE_ASSISTANT_PENDING,
16
+ encodeNativeAssistantSignature,
17
+ encodeReasoningSignature,
18
+ reasoningItemsFromUnknown,
19
+ } from "./reasoning-signature";
20
+ import { anthropicMessagesUsageFromInclusive } from "./usage";
21
+
22
+ type TDeltaFoldMode = "incremental" | "snapshot";
23
+
24
+ // runtime-only: stateful translation buffer.
25
+ export type TMessagesStreamState = {
26
+ startEmitted: boolean;
27
+ messageId: string;
28
+ model: string;
29
+ inputTokens: number;
30
+ /**
31
+ * Last-seen `usage.completion_tokens`. The synthetic EOF tail
32
+ * carries no provider usage of its own; without this, a clean
33
+ * `end_turn` terminal `message_delta` would reconcile to
34
+ * `output_tokens: 0` and last-win the live trailer (Claude Code
35
+ * treats `0` as authoritative, unlike input which is `> 0`-guarded).
36
+ */
37
+ outputTokens: number;
38
+ cacheReadTokens: number;
39
+ cacheCreationTokens: number;
40
+ /** Anthropic content_index for the text block. null = not yet opened. */
41
+ textBlockIndex: number | null;
42
+ /** True while the text block is open (between content_block_start and stop). */
43
+ textBlockOpen: boolean;
44
+ /** Responses `reasoning_summary_text.delta` → Anthropic thinking block. */
45
+ thinkingBlockIndex: number | null;
46
+ thinkingBlockOpen: boolean;
47
+ /** OpenAI tool_calls[i].index → Anthropic content_index. */
48
+ toolCallToContentIndex: Map<number, number>;
49
+ /**
50
+ * Tool calls seen but not yet OPENED because their `name` is still empty.
51
+ * Anthropic requires `tool_use.name` at `content_block_start`, and a client
52
+ * (Claude Code) drops a `tool_use` whose name is `""` — so it never runs the
53
+ * tool. The OpenAI Responses wire (grok/chatgpt) can split a tool call's
54
+ * `name` / `id` / argument fragments across separate events, so the first
55
+ * fragment may carry no name. We buffer `id`/`name`/`args` per OpenAI index
56
+ * here and open the block the moment a non-empty name arrives (flushing the
57
+ * buffered args), or belatedly at finish with a synthesized id. Mirrors
58
+ * CLIProxyAPI's accumulate-until-(name)+belated-open (`openai_claude_response.go`).
59
+ */
60
+ pendingToolCalls: Map<number, { id: string; name: string; args: string }>;
61
+ /** True once any tool_use block has been OPENED (announced) for this message. */
62
+ emittedToolUse: boolean;
63
+ /** Anthropic content_index → still-open flag. */
64
+ openToolContentIndexes: Set<number>;
65
+ /** Next free Anthropic content_index. */
66
+ nextContentIndex: number;
67
+ finalStopReason: ReturnType<typeof anthropicStopReasonFrom>;
68
+ /**
69
+ * Concatenation of all `reasoning_content` / thinking deltas. On
70
+ * terminal chunk, if there was no non-empty `text_delta`, we mirror
71
+ * this into a synthetic `text` block so Claude Code `/compact` sees
72
+ * valid user-visible text (same rule as `toAnthropicMessagesResponse`).
73
+ */
74
+ thinkingAccumulated: string;
75
+ /** True after at least one non-empty `content` delta became `text_delta`. */
76
+ emittedNonemptyTextDelta: boolean;
77
+ /** Set when `message_stop` is emitted — detects truncated upstream streams. */
78
+ messageStopEmitted: boolean;
79
+ /** Concatenation of all visible `text_delta`s (content + dumped reasoning). */
80
+ textAccumulated: string;
81
+ /**
82
+ * Snapshot ledger for `delta.content` only. Must not share
83
+ * `textAccumulated`: dumped `reasoning_content` rides that field, and
84
+ * comparing answer snapshots against it would suppress a real answer
85
+ * that happens to prefix the dumped thought.
86
+ */
87
+ contentSnapshotAccumulated: string;
88
+ /**
89
+ * True when the most recently processed chunk carried real provider
90
+ * usage (`prompt_tokens > 0`). Sticky-ever-seen is wrong: a later
91
+ * content chunk after a usage frame is a tear, not a clean close.
92
+ * Live-estimate frames keep prompt_tokens at 0 and stay false.
93
+ */
94
+ lastChunkHadProviderUsage: boolean;
95
+ /**
96
+ * How many leading chars of `thinkingAccumulated` were already emitted as
97
+ * `thinking_delta` events. Keeps streaming aligned when `reasoning_items`
98
+ * arrives as a snapshot (final chunk only).
99
+ */
100
+ thinkingDeltaEmittedLen: number;
101
+ /**
102
+ * Encoded `thinking.signature` carrying the upstream's `reasoning`
103
+ * item(s) (Codex/Responses `encrypted_content`). Set when a chunk
104
+ * carries `reasoning_items`; flushed onto the thinking block right
105
+ * before it closes so Claude Code replays it next turn. Without this
106
+ * the model loses chain-of-thought state and loops forever.
107
+ */
108
+ pendingReasoningSignature: string | null;
109
+ /**
110
+ * Responses hops (Grok / Codex) tag summary deltas with
111
+ * `reasoning_items: []` (and later a real encrypted item). The
112
+ * summary must ride a signed `thinking` block — dumping it as
113
+ * visible `text` and then opening an empty thinking block produces
114
+ * two identical ⏺ prompts around "Thought for 1s".
115
+ */
116
+ signedReasoningChannel: boolean;
117
+ /** Native projection-checked history must keep visible text byte-exact. */
118
+ nativeAssistantChannel: boolean;
119
+ /** True after a signed thinking block was sealed this turn. */
120
+ sealedSignedThinking: boolean;
121
+ /** Provider-executed hosted searches emitted so far — drives the terminal
122
+ * `message_delta.usage.server_tool_use.web_search_requests`. */
123
+ serverSearchCount: number;
124
+ };
125
+
126
+ export const newMessagesStreamState = (): TMessagesStreamState => ({
127
+ startEmitted: false,
128
+ messageId: "",
129
+ model: "",
130
+ inputTokens: 0,
131
+ outputTokens: 0,
132
+ cacheReadTokens: 0,
133
+ cacheCreationTokens: 0,
134
+ textBlockIndex: null,
135
+ textBlockOpen: false,
136
+ thinkingBlockIndex: null,
137
+ thinkingBlockOpen: false,
138
+ toolCallToContentIndex: new Map(),
139
+ pendingToolCalls: new Map(),
140
+ emittedToolUse: false,
141
+ openToolContentIndexes: new Set(),
142
+ nextContentIndex: 0,
143
+ finalStopReason: null,
144
+ thinkingAccumulated: "",
145
+ emittedNonemptyTextDelta: false,
146
+ messageStopEmitted: false,
147
+ textAccumulated: "",
148
+ contentSnapshotAccumulated: "",
149
+ lastChunkHadProviderUsage: false,
150
+ thinkingDeltaEmittedLen: 0,
151
+ pendingReasoningSignature: null,
152
+ signedReasoningChannel: false,
153
+ nativeAssistantChannel: false,
154
+ sealedSignedThinking: false,
155
+ serverSearchCount: 0,
156
+ });
157
+
158
+ /**
159
+ * Dedup / accumulate a text-bearing delta.
160
+ *
161
+ * Mode is channel identity, not string shape: `"The"` + `"There"` and a
162
+ * growing snapshot that happens to start with the accumulated prefix are
163
+ * the same `startsWith` condition. Incremental tokens (Kimi
164
+ * `reasoning_content`, unmarked `delta.content`) always append.
165
+ * Responses summaries (`reasoning_items` present, including `[]`)
166
+ * replay the full prefix — emit only the tail.
167
+ *
168
+ * Identical `incoming === accumulated` is always a same-snapshot repeat
169
+ * and is skipped in both modes.
170
+ */
171
+ /**
172
+ * Snapshot-mode prefix check. Responses-style channels re-send the full
173
+ * prefix; only a true `startsWith` is correct — a head/tail fingerprint
174
+ * falsely treats mid-text revisions that share both boundaries as
175
+ * extensions. Snapshot frames are infrequent enough that O(n) is fine.
176
+ */
177
+ const snapshotExtends = (incoming: string, accumulated: string): boolean => {
178
+ if (accumulated.length === 0) return true;
179
+ if (incoming.length < accumulated.length) return false;
180
+ return incoming.startsWith(accumulated);
181
+ };
182
+
183
+ const foldDelta = (
184
+ incoming: string,
185
+ accumulated: string,
186
+ mode: TDeltaFoldMode,
187
+ ): { readonly next: string; readonly emit: string } => {
188
+ if (incoming.length === 0) return { next: accumulated, emit: "" };
189
+ if (incoming === accumulated) return { next: accumulated, emit: "" };
190
+ if (mode === "snapshot") {
191
+ const accLen = accumulated.length;
192
+ const incLen = incoming.length;
193
+ if (incLen > accLen) {
194
+ if (snapshotExtends(incoming, accumulated)) {
195
+ return { next: incoming, emit: incoming.slice(accLen) };
196
+ }
197
+ return { next: incoming, emit: incoming };
198
+ }
199
+ if (incLen < accLen) {
200
+ if (accumulated.startsWith(incoming)) {
201
+ return { next: accumulated, emit: "" };
202
+ }
203
+ return { next: incoming, emit: incoming };
204
+ }
205
+ // Same length, different content — full replacement.
206
+ return { next: incoming, emit: incoming };
207
+ }
208
+ return { next: accumulated + incoming, emit: incoming };
209
+ };
210
+
211
+ /** Test/bench seam for {@link foldDelta} (snapshot vs incremental). */
212
+ export const foldDeltaForBench = foldDelta;
213
+
214
+ /**
215
+ * Emit the reasoning `signature_delta` onto the thinking block so
216
+ * Claude Code replays it verbatim next turn. Anthropic requires
217
+ * `signature_delta` to be the thinking block's LAST delta, before
218
+ * `content_block_stop` and before any tool_use block opens — so this
219
+ * runs the moment encrypted `reasoning_items` arrive (Codex/Grok send
220
+ * them on the reasoning item's `output_item.done`, ahead of the
221
+ * function call). The human-readable summary rides the same thinking
222
+ * block; dumping it as `text` first is what produced the double ⏺.
223
+ */
224
+ const emitReasoningSignature = (
225
+ state: TMessagesStreamState,
226
+ out: TAnthropicStreamEvent[],
227
+ ): void => {
228
+ if (
229
+ state.pendingReasoningSignature === null ||
230
+ state.openToolContentIndexes.size > 0
231
+ ) {
232
+ return;
233
+ }
234
+ closeTextBlock(state, out);
235
+ closeAllToolBlocks(state, out);
236
+ const idx = openThinkingBlock(state, out);
237
+ const held = state.thinkingAccumulated.slice(state.thinkingDeltaEmittedLen);
238
+ if (held.length > 0) {
239
+ out.push({
240
+ type: "content_block_delta",
241
+ index: idx,
242
+ delta: { type: "thinking_delta", thinking: held },
243
+ });
244
+ state.thinkingDeltaEmittedLen = state.thinkingAccumulated.length;
245
+ }
246
+ out.push({
247
+ type: "content_block_delta",
248
+ index: idx,
249
+ delta: {
250
+ type: "signature_delta",
251
+ signature: state.pendingReasoningSignature,
252
+ },
253
+ });
254
+ state.pendingReasoningSignature = null;
255
+ state.sealedSignedThinking = true;
256
+ closeThinkingBlock(state, out);
257
+ };
258
+
259
+ const closeThinkingBlock = (
260
+ state: TMessagesStreamState,
261
+ out: TAnthropicStreamEvent[],
262
+ ): void => {
263
+ if (state.thinkingBlockOpen && state.thinkingBlockIndex !== null) {
264
+ out.push({
265
+ type: "content_block_stop",
266
+ index: state.thinkingBlockIndex,
267
+ });
268
+ state.thinkingBlockOpen = false;
269
+ }
270
+ };
271
+
272
+ const openThinkingBlock = (
273
+ state: TMessagesStreamState,
274
+ out: TAnthropicStreamEvent[],
275
+ ): number => {
276
+ // A stopped thinking index must not be reopened (Anthropic indices
277
+ // are unique + monotonic). A later reasoning item (Grok interleaves
278
+ // summaries with tool calls) gets a fresh block.
279
+ if (state.thinkingBlockIndex === null || !state.thinkingBlockOpen) {
280
+ state.thinkingBlockIndex = state.nextContentIndex;
281
+ state.nextContentIndex += 1;
282
+ }
283
+ if (!state.thinkingBlockOpen) {
284
+ out.push({
285
+ type: "content_block_start",
286
+ index: state.thinkingBlockIndex,
287
+ content_block: { type: "thinking", thinking: "" },
288
+ });
289
+ state.thinkingBlockOpen = true;
290
+ }
291
+ return state.thinkingBlockIndex;
292
+ };
293
+
294
+ const closeTextBlock = (
295
+ state: TMessagesStreamState,
296
+ out: TAnthropicStreamEvent[],
297
+ ): void => {
298
+ if (state.textBlockOpen && state.textBlockIndex !== null) {
299
+ out.push({
300
+ type: "content_block_stop",
301
+ index: state.textBlockIndex,
302
+ });
303
+ state.textBlockOpen = false;
304
+ }
305
+ };
306
+
307
+ const closeAllToolBlocks = (
308
+ state: TMessagesStreamState,
309
+ out: TAnthropicStreamEvent[],
310
+ ): void => {
311
+ for (const idx of state.openToolContentIndexes) {
312
+ out.push({ type: "content_block_stop", index: idx });
313
+ }
314
+ state.openToolContentIndexes.clear();
315
+ // Fresh tool_call deltas must open new content blocks. If reasoning or
316
+ // another branch stops blocks mid-stream but leaves stale tc.index →
317
+ // Anthropic index mappings, later `input_json_delta` targets a block we
318
+ // already emitted `content_block_stop` for — Claude Code sees prose +
319
+ // truncated pseudo-tools (e.g. literal `<tool_call>` tail).
320
+ state.toolCallToContentIndex.clear();
321
+ };
322
+
323
+ /**
324
+ * Open a tool_use content block for a now-named buffered tool call and flush
325
+ * any argument fragments accumulated while its name was still empty. The block
326
+ * stays open (subsequent fragments stream straight through). `id` is
327
+ * synthesized when the upstream never supplied one, so the block is always
328
+ * executable. Anthropic content blocks are strictly sequential, so any open
329
+ * text / tool / thinking block is closed first.
330
+ */
331
+ const openBufferedToolBlock = (
332
+ state: TMessagesStreamState,
333
+ out: TAnthropicStreamEvent[],
334
+ openAiIndex: number,
335
+ pending: { id: string; name: string; args: string },
336
+ ): number => {
337
+ closeTextBlock(state, out);
338
+ closeAllToolBlocks(state, out);
339
+ closeThinkingBlock(state, out);
340
+ const contentIndex = state.nextContentIndex;
341
+ state.nextContentIndex += 1;
342
+ state.toolCallToContentIndex.set(openAiIndex, contentIndex);
343
+ state.pendingToolCalls.delete(openAiIndex);
344
+ state.emittedToolUse = true;
345
+ state.openToolContentIndexes.add(contentIndex);
346
+ out.push({
347
+ type: "content_block_start",
348
+ index: contentIndex,
349
+ content_block: {
350
+ type: "tool_use",
351
+ id:
352
+ pending.id !== ""
353
+ ? pending.id
354
+ : `toolu_${state.messageId}_${openAiIndex}`,
355
+ name: pending.name,
356
+ input: {},
357
+ },
358
+ });
359
+ if (pending.args.length > 0) {
360
+ out.push({
361
+ type: "content_block_delta",
362
+ index: contentIndex,
363
+ delta: { type: "input_json_delta", partial_json: pending.args },
364
+ });
365
+ }
366
+ return contentIndex;
367
+ };
368
+
369
+ /**
370
+ * Belatedly open every still-pending tool call that accumulated a name but was
371
+ * never opened (its `content_block_start` awaited a non-empty name that only
372
+ * arrived on the terminal event, or the block was reset mid-stream). Called at
373
+ * finish so a named-but-unopened tool still reaches the client. Pending calls
374
+ * that never got a name are dropped (an unnamed tool is unexecutable) — mirrors
375
+ * CLIProxyAPI's belated-emit that skips `accumulator.Name == ""`.
376
+ */
377
+ const flushPendingToolBlocks = (
378
+ state: TMessagesStreamState,
379
+ out: TAnthropicStreamEvent[],
380
+ ): void => {
381
+ const indexes = [...state.pendingToolCalls.keys()].sort((a, b) => a - b);
382
+ for (const idx of indexes) {
383
+ const pending = state.pendingToolCalls.get(idx);
384
+ if (pending === undefined || pending.name === "") {
385
+ state.pendingToolCalls.delete(idx);
386
+ continue;
387
+ }
388
+ const contentIndex = openBufferedToolBlock(state, out, idx, pending);
389
+ out.push({ type: "content_block_stop", index: contentIndex });
390
+ state.openToolContentIndexes.delete(contentIndex);
391
+ }
392
+ };
393
+
394
+ const openTextBlock = (
395
+ state: TMessagesStreamState,
396
+ out: TAnthropicStreamEvent[],
397
+ ): number => {
398
+ closeThinkingBlock(state, out);
399
+ // A previously-stopped content index can never be reopened (Anthropic
400
+ // streaming indices are unique + monotonic). If the text block was
401
+ // closed — e.g. a signed thinking block or a tool block was emitted
402
+ // in between — allocate a FRESH index instead of resurrecting the
403
+ // stopped one.
404
+ if (state.textBlockIndex === null || !state.textBlockOpen) {
405
+ state.textBlockIndex = state.nextContentIndex;
406
+ state.nextContentIndex += 1;
407
+ }
408
+ if (!state.textBlockOpen) {
409
+ out.push({
410
+ type: "content_block_start",
411
+ index: state.textBlockIndex,
412
+ content_block: { type: "text", text: "" },
413
+ });
414
+ state.textBlockOpen = true;
415
+ }
416
+ return state.textBlockIndex;
417
+ };
418
+
419
+ const emitVisibleText = (
420
+ state: TMessagesStreamState,
421
+ out: TAnthropicStreamEvent[],
422
+ text: string,
423
+ ): void => {
424
+ if (text.length === 0) return;
425
+ const idx = openTextBlock(state, out);
426
+ out.push({
427
+ type: "content_block_delta",
428
+ index: idx,
429
+ delta: { type: "text_delta", text },
430
+ });
431
+ state.emittedNonemptyTextDelta = true;
432
+ state.textAccumulated += text;
433
+ };
434
+
435
+ /**
436
+ * Translate one OpenAI ChatCompletion chunk into zero or more
437
+ * Anthropic SSE events. Handles both text deltas and tool_call deltas:
438
+ * each new tool_call opens a new content_block(tool_use); subsequent
439
+ * deltas for that tool_call emit input_json_delta events.
440
+ */
441
+ export const chunkToMessagesEvents = (
442
+ chunk: TChatCompletionChunk,
443
+ state: TMessagesStreamState,
444
+ ): TAnthropicStreamEvent[] => {
445
+ const out: TAnthropicStreamEvent[] = [];
446
+
447
+ if (!state.startEmitted) {
448
+ state.startEmitted = true;
449
+ state.messageId = chunk.id;
450
+ state.model = chunk.model;
451
+ out.push({
452
+ type: "message_start",
453
+ message: {
454
+ id: state.messageId,
455
+ type: "message",
456
+ role: "assistant",
457
+ model: state.model,
458
+ content: [],
459
+ stop_reason: null,
460
+ stop_sequence: null,
461
+ usage: { input_tokens: 0, output_tokens: 0 },
462
+ },
463
+ });
464
+ }
465
+
466
+ const choice = chunk.choices[0];
467
+ const deltaReasoning = choice?.delta.reasoning_content ?? null;
468
+ const deltaText = choice?.delta.content ?? null;
469
+ const deltaToolCalls = choice?.delta.tool_calls ?? null;
470
+ if (
471
+ choice?.delta.reasoning_items?.some((item) => {
472
+ if (typeof item !== "object" || item === null) return false;
473
+ const type = (item as { type?: unknown }).type;
474
+ return (
475
+ type === CLAUDE_NATIVE_ASSISTANT_CARRIER ||
476
+ type === CLAUDE_NATIVE_ASSISTANT_PENDING
477
+ );
478
+ })
479
+ )
480
+ state.nativeAssistantChannel = true;
481
+
482
+ // Responses hops tag summary deltas with `reasoning_items` (empty
483
+ // array = channel marker; later chunks carry the encrypted item).
484
+ // Sticky: once we know the hop is signed, later unsigned-looking
485
+ // fragments stay on the thinking ledger.
486
+ if (
487
+ choice?.delta.reasoning_items !== undefined &&
488
+ choice.delta.reasoning_items !== null
489
+ ) {
490
+ state.signedReasoningChannel = true;
491
+ }
492
+
493
+ if (
494
+ deltaReasoning !== null &&
495
+ deltaReasoning !== undefined &&
496
+ deltaReasoning.length > 0
497
+ ) {
498
+ // Unsigned reasoning (Kimi / DashScope) has no replay-safe
499
+ // signature, so it must be visible `text` — Anthropic hard-rejects
500
+ // a signature-less thinking block on replay. Signed Responses hops
501
+ // (Grok / Codex) hold the same text for the thinking block opened
502
+ // by `emitReasoningSignature`; dumping it first is the double ⏺.
503
+ const reasoningSnap = foldDelta(
504
+ deltaReasoning,
505
+ state.thinkingAccumulated,
506
+ state.signedReasoningChannel ? "snapshot" : "incremental",
507
+ );
508
+ state.thinkingAccumulated = reasoningSnap.next;
509
+ if (
510
+ reasoningSnap.emit.length > 0 &&
511
+ state.openToolContentIndexes.size === 0 &&
512
+ !state.signedReasoningChannel
513
+ ) {
514
+ emitVisibleText(state, out, reasoningSnap.emit);
515
+ state.thinkingDeltaEmittedLen = state.thinkingAccumulated.length;
516
+ }
517
+ }
518
+
519
+ const reasoningItems = reasoningItemsFromUnknown(
520
+ choice?.delta.reasoning_items,
521
+ );
522
+ const nativeSignature = encodeNativeAssistantSignature(
523
+ choice?.delta.reasoning_items,
524
+ );
525
+ if (reasoningItems.length > 0 || nativeSignature !== null) {
526
+ const sig = nativeSignature ?? encodeReasoningSignature(reasoningItems);
527
+ if (sig !== null) {
528
+ // A distinct new signature while one is still pending (Grok interleaves
529
+ // multiple reasoning items) — seal the prior item as its own thinking
530
+ // block before adopting the new one, so neither signature is lost.
531
+ if (
532
+ state.pendingReasoningSignature !== null &&
533
+ state.pendingReasoningSignature !== sig
534
+ ) {
535
+ emitReasoningSignature(state, out);
536
+ }
537
+ state.pendingReasoningSignature = sig;
538
+ }
539
+ }
540
+
541
+ const fromReasoningItems = plainTextFromReasoningItems(
542
+ choice?.delta.reasoning_items,
543
+ );
544
+ // Growing `reasoning_items` snapshot of the same summary. Fold the
545
+ // tail into the ledger. Do not dump as `text` here — signed hops
546
+ // emit it as `thinking_delta` with the signature; unsigned hops
547
+ // flush at content / finish (compaction-safe).
548
+ // Reasoning item summaries are authoritative even when they are not a
549
+ // prefix of the folded streamed delta (e.g. snapshot collapse) — accept a
550
+ // longer snapshot whenever it grows.
551
+ if (fromReasoningItems.length > state.thinkingAccumulated.length) {
552
+ state.thinkingAccumulated = fromReasoningItems;
553
+ }
554
+
555
+ // Seal each signed reasoning item as soon as its encrypted item arrives.
556
+ // Codex can deliver that item before its human summary. In that case the
557
+ // replay-safe result is a signature-only thinking block, not a placeholder;
558
+ // a later summary cannot be attached after `signature_delta` has sealed the
559
+ // block. This keeps the signature last and before a subsequent tool_use.
560
+ emitReasoningSignature(state, out);
561
+
562
+ // Provider-executed hosted searches (Codex `webSearch` items on a chatgpt
563
+ // hop) → self-contained server_tool_use + web_search_tool_result block
564
+ // pairs. Blocks must stay strictly sequential, so anything open closes
565
+ // first; codex interleaves commentary text → search → answer text, and
566
+ // `openTextBlock` allocates a fresh index for the post-search text. The
567
+ // result content is an empty list — Codex never exposes result items; the
568
+ // findings ride the grounded answer text (see the JSON adapter's note).
569
+ const deltaSearches = choice?.delta.server_search_calls ?? null;
570
+ if (deltaSearches !== null && deltaSearches !== undefined) {
571
+ // Boundary: seal any pending signature onto the thinking block before it
572
+ // is closed for the server-search blocks.
573
+ emitReasoningSignature(state, out);
574
+ for (const search of deltaSearches) {
575
+ closeTextBlock(state, out);
576
+ closeAllToolBlocks(state, out);
577
+ closeThinkingBlock(state, out);
578
+ const useIndex = state.nextContentIndex;
579
+ state.nextContentIndex += 1;
580
+ out.push({
581
+ type: "content_block_start",
582
+ index: useIndex,
583
+ content_block: {
584
+ type: "server_tool_use",
585
+ id: search.id,
586
+ name: "web_search",
587
+ input: {},
588
+ },
589
+ });
590
+ out.push({
591
+ type: "content_block_delta",
592
+ index: useIndex,
593
+ delta: {
594
+ type: "input_json_delta",
595
+ partial_json: JSON.stringify({ query: search.query }),
596
+ },
597
+ });
598
+ out.push({ type: "content_block_stop", index: useIndex });
599
+ const resultIndex = state.nextContentIndex;
600
+ state.nextContentIndex += 1;
601
+ out.push({
602
+ type: "content_block_start",
603
+ index: resultIndex,
604
+ content_block: {
605
+ type: "web_search_tool_result",
606
+ tool_use_id: search.id,
607
+ content: (search.results ?? []).map((r) => ({
608
+ type: "web_search_result",
609
+ url: r.url,
610
+ title: r.title ?? r.url,
611
+ })),
612
+ },
613
+ });
614
+ out.push({ type: "content_block_stop", index: resultIndex });
615
+ state.serverSearchCount += 1;
616
+ }
617
+ }
618
+
619
+ // Text delta → open text block (if not already) + content_block_delta.
620
+ // Content is incremental: exact-match skip, else append. A dedicated
621
+ // ledger so dumped reasoning cannot swallow a later answer prefix.
622
+ // Signed hops also drop content that merely restates the thought
623
+ // (field sample: same sentence as a second ⏺ after "Thought for 1s").
624
+ if (deltaText !== null && deltaText !== undefined && deltaText.length > 0) {
625
+ // Boundary: the answer text starts — seal any pending signature before the
626
+ // visible text block opens.
627
+ emitReasoningSignature(state, out);
628
+ const textSnap = foldDelta(
629
+ deltaText,
630
+ state.contentSnapshotAccumulated,
631
+ "incremental",
632
+ );
633
+ state.contentSnapshotAccumulated = textSnap.next;
634
+ let emit = textSnap.emit;
635
+ if (
636
+ !state.nativeAssistantChannel &&
637
+ state.signedReasoningChannel &&
638
+ state.thinkingAccumulated.length > 0
639
+ ) {
640
+ const desired = visibleAnswerAfterThought(
641
+ state.contentSnapshotAccumulated,
642
+ state.thinkingAccumulated,
643
+ );
644
+ if (desired.startsWith(state.textAccumulated)) {
645
+ emit = desired.slice(state.textAccumulated.length);
646
+ } else if (state.textAccumulated.startsWith(desired)) {
647
+ emit = "";
648
+ } else {
649
+ emit = desired;
650
+ }
651
+ }
652
+ emitVisibleText(state, out, emit);
653
+ }
654
+
655
+ // Tool-call deltas → open tool_use blocks + input_json_delta events.
656
+ if (deltaToolCalls !== null && deltaToolCalls !== undefined) {
657
+ // Boundary: seal any pending signature BEFORE the first tool_use block
658
+ // opens. Empty summaries produce a signature-only thinking block.
659
+ emitReasoningSignature(state, out);
660
+ for (const tc of deltaToolCalls) {
661
+ const contentIndex = state.toolCallToContentIndex.get(tc.index);
662
+ if (contentIndex !== undefined) {
663
+ // Block already opened — stream this argument fragment straight through.
664
+ const argFragment = tc.function?.arguments ?? "";
665
+ if (argFragment.length > 0) {
666
+ out.push({
667
+ type: "content_block_delta",
668
+ index: contentIndex,
669
+ delta: { type: "input_json_delta", partial_json: argFragment },
670
+ });
671
+ }
672
+ continue;
673
+ }
674
+ // Not opened yet — accumulate id / name / args in the pending buffer.
675
+ // We CANNOT open a tool_use block until we have a non-empty `name`
676
+ // (Anthropic requires it at `content_block_start`, and Claude Code
677
+ // silently drops a nameless tool_use — the sub-agent never spawns). The
678
+ // Responses wire may deliver name/id/args across separate events, so the
679
+ // first fragment can be nameless.
680
+ const pending = state.pendingToolCalls.get(tc.index) ?? {
681
+ id: "",
682
+ name: "",
683
+ args: "",
684
+ };
685
+ if (tc.id != null && tc.id !== "") pending.id = tc.id;
686
+ const fragmentName = tc.function?.name;
687
+ if (fragmentName != null && fragmentName !== "") {
688
+ pending.name = fragmentName;
689
+ }
690
+ pending.args += tc.function?.arguments ?? "";
691
+ state.pendingToolCalls.set(tc.index, pending);
692
+ // The moment we know the name, open the block and flush buffered args.
693
+ if (pending.name !== "") {
694
+ openBufferedToolBlock(state, out, tc.index, pending);
695
+ }
696
+ }
697
+ }
698
+
699
+ state.lastChunkHadProviderUsage =
700
+ chunk.usage != null && chunk.usage.prompt_tokens > 0;
701
+ if (chunk.usage != null) {
702
+ state.inputTokens = chunk.usage.prompt_tokens;
703
+ state.outputTokens = chunk.usage.completion_tokens;
704
+ state.cacheReadTokens =
705
+ chunk.usage.prompt_tokens_details?.cached_tokens ?? 0;
706
+ state.cacheCreationTokens =
707
+ chunk.usage.prompt_tokens_details?.cache_creation_tokens ?? 0;
708
+ }
709
+
710
+ const turnEnds =
711
+ choice?.finish_reason !== null && choice?.finish_reason !== undefined;
712
+
713
+ // Live token feed: a usage-bearing chunk that does NOT end the turn —
714
+ // the running estimate synthesized by `withLiveUsageEstimate`, or an
715
+ // upstream that reports incremental usage — emits a standalone
716
+ // `message_delta` so a CLI's token counter climbs mid-stream instead
717
+ // of staying at zero until completion. No `stop_reason`, no
718
+ // `message_stop`: the turn is still open and the terminal
719
+ // `message_delta` below reconciles to the provider's exact totals.
720
+ // Normalize only at emit: `state.inputTokens` stays inclusive so EOF
721
+ // can re-inject OpenAI-shaped `prompt_tokens` without a second subtract.
722
+ if (
723
+ chunk.usage != null &&
724
+ !turnEnds &&
725
+ state.startEmitted &&
726
+ !state.messageStopEmitted
727
+ ) {
728
+ out.push({
729
+ type: "message_delta",
730
+ delta: { stop_reason: null, stop_sequence: null },
731
+ usage: anthropicMessagesUsageFromInclusive({
732
+ promptTokens: state.inputTokens,
733
+ completionTokens: chunk.usage.completion_tokens,
734
+ cachedTokens: state.cacheReadTokens,
735
+ cacheCreationTokens: state.cacheCreationTokens,
736
+ }),
737
+ });
738
+ }
739
+
740
+ if (choice?.finish_reason !== null && choice?.finish_reason !== undefined) {
741
+ let finalStopReason = anthropicStopReasonFrom(choice.finish_reason);
742
+ // Seal any pending reasoning signature before flushing terminally buffered
743
+ // tool blocks. An empty summary produces a signature-only thinking block;
744
+ // the call is a no-op when nothing is pending or a tool block is open.
745
+ emitReasoningSignature(state, out);
746
+ // A tool call whose name only arrived on the terminal event (or never
747
+ // streamed a `content_block_start` because its name stayed empty until
748
+ // now) is opened + closed here so a named-but-unopened `Task`/`Agent`
749
+ // call still reaches the client. Runs BEFORE the stop-reason override
750
+ // below (it sets `emittedToolUse` when it opens a block).
751
+ flushPendingToolBlocks(state, out);
752
+ // If the upstream emitted tool_call deltas during the stream but
753
+ // wrongly settled on `finish_reason: "stop"`, override to
754
+ // `tool_use`. Without this, Claude Code receives a
755
+ // `stop_reason: "end_turn"` alongside `tool_use` blocks and never
756
+ // calls the tool back — observed on chatgpt.com Responses API.
757
+ if (
758
+ state.emittedToolUse &&
759
+ (finalStopReason === null || finalStopReason === "end_turn")
760
+ ) {
761
+ finalStopReason = "tool_use";
762
+ }
763
+ state.finalStopReason = finalStopReason;
764
+ closeThinkingBlock(state, out);
765
+ if (
766
+ !state.nativeAssistantChannel &&
767
+ state.textBlockOpen &&
768
+ state.textBlockIndex !== null
769
+ ) {
770
+ const trimmed = state.textAccumulated.trim();
771
+ if (trimmed.length > 0) {
772
+ const safe = ensureCompactionSafeVisibleText(state.textAccumulated);
773
+ if (safe.length > trimmed.length && safe.startsWith(trimmed)) {
774
+ out.push({
775
+ type: "content_block_delta",
776
+ index: state.textBlockIndex,
777
+ delta: { type: "text_delta", text: safe.slice(trimmed.length) },
778
+ });
779
+ }
780
+ }
781
+ }
782
+ closeTextBlock(state, out);
783
+ closeAllToolBlocks(state, out);
784
+ // A signature received while a tool was open is deferred with its
785
+ // reasoning text, then emitted only after the tool block is sealed.
786
+ emitReasoningSignature(state, out);
787
+ closeThinkingBlock(state, out);
788
+ const deferredReasoning = state.thinkingAccumulated.slice(
789
+ state.thinkingDeltaEmittedLen,
790
+ );
791
+ // Unsigned leftover (Kimi / items without encrypted_content) still
792
+ // becomes visible text. Leftover on a sealed signed hop is more
793
+ // summary after the thinking block closed — dumping it is the
794
+ // "polished answer, then the thought again" field sample.
795
+ if (deferredReasoning.length > 0 && !state.sealedSignedThinking) {
796
+ emitVisibleText(
797
+ state,
798
+ out,
799
+ ensureCompactionSafeVisibleText(deferredReasoning),
800
+ );
801
+ state.thinkingDeltaEmittedLen = state.thinkingAccumulated.length;
802
+ closeTextBlock(state, out);
803
+ } else if (
804
+ !state.nativeAssistantChannel &&
805
+ !state.emittedNonemptyTextDelta &&
806
+ state.sealedSignedThinking &&
807
+ state.thinkingAccumulated.length === 0 &&
808
+ !state.emittedToolUse
809
+ ) {
810
+ // Signed thinking with no human summary still needs a compact-safe
811
+ // text block. Do not clone a non-empty sealed summary (second ⏺).
812
+ // A TOOL turn is exempt: `/compact` only ever reads a text-only
813
+ // reply, while a tool_use turn with no summary is the normal shape
814
+ // of a reasoning model calling a tool (every Codex `spark` step).
815
+ // Emitting the fallback there printed a stray "no visible summary
816
+ // text was returned for this compaction" ⏺ before every tool call.
817
+ emitVisibleText(state, out, ensureCompactionSafeVisibleText(""));
818
+ closeTextBlock(state, out);
819
+ }
820
+
821
+ const outputTokens = chunk.usage?.completion_tokens ?? state.outputTokens;
822
+ out.push({
823
+ type: "message_delta",
824
+ delta: {
825
+ stop_reason: state.finalStopReason,
826
+ stop_sequence: null,
827
+ },
828
+ usage: {
829
+ ...anthropicMessagesUsageFromInclusive({
830
+ promptTokens: state.inputTokens,
831
+ completionTokens: outputTokens,
832
+ cachedTokens: state.cacheReadTokens,
833
+ cacheCreationTokens: state.cacheCreationTokens,
834
+ }),
835
+ ...(state.serverSearchCount > 0
836
+ ? {
837
+ server_tool_use: {
838
+ web_search_requests: state.serverSearchCount,
839
+ },
840
+ }
841
+ : {}),
842
+ },
843
+ });
844
+ out.push({ type: "message_stop" });
845
+ state.messageStopEmitted = true;
846
+ }
847
+ return out;
848
+ };
849
+
850
+ /**
851
+ * Encode an Anthropic stream event as SSE bytes. Anthropic uses an
852
+ * `event: <name>\n` prefix line (not just `data:` like OpenAI), so we
853
+ * can't reuse `encodeSseEvent` directly.
854
+ */
855
+ export const encodeAnthropicSseEvent = (
856
+ event: TAnthropicStreamEvent,
857
+ ): Uint8Array => {
858
+ const lines = `event: ${event.type}\n`;
859
+ const body = encodeSseEvent(event);
860
+ const prefix = new TextEncoder().encode(lines);
861
+ const out = new Uint8Array(prefix.byteLength + body.byteLength);
862
+ out.set(prefix, 0);
863
+ out.set(body, prefix.byteLength);
864
+ return out;
865
+ };
866
+
867
+ /**
868
+ * Pipe a stream of OpenAI ChatCompletion chunks into Anthropic-format
869
+ * SSE bytes. Used by the `/v1/messages` handler when the runner
870
+ * returned a streaming outcome.
871
+ */
872
+ export const chunksToMessagesSseBytes = (
873
+ chunks: ReadableStream<TChatCompletionChunk>,
874
+ ): ReadableStream<Uint8Array> => {
875
+ const reader = chunks.getReader();
876
+ const state = newMessagesStreamState();
877
+ const buffer: Uint8Array[] = [];
878
+ // One-chunk lookahead. The OpenAI streaming spec delivers token
879
+ // counts in a SEPARATE trailing chunk (`choices: []`, `usage` set,
880
+ // emitted under `stream_options.include_usage`) that arrives AFTER
881
+ // the `finish_reason` chunk. The terminal `message_delta` is built
882
+ // from the finish chunk, so without folding that trailing usage in
883
+ // first the CLI feed gets `usage:{input_tokens:0,output_tokens:0}` —
884
+ // i.e. no token counter once you go through the proxy. Providers
885
+ // that already put usage on the finish chunk are untouched.
886
+ let pending: TChatCompletionChunk | null = null;
887
+ const readChunk = async (): Promise<
888
+ { value: TChatCompletionChunk; done: false } | { done: true }
889
+ > => {
890
+ if (pending !== null) {
891
+ const v = pending;
892
+ pending = null;
893
+ return { value: v, done: false };
894
+ }
895
+ const r = await reader.read();
896
+ return r.done ? { done: true } : { value: r.value, done: false };
897
+ };
898
+ const isUsageOnly = (c: TChatCompletionChunk): boolean =>
899
+ c.choices.length === 0 && c.usage != null;
900
+ return new ReadableStream<Uint8Array>({
901
+ async pull(controller) {
902
+ try {
903
+ for (;;) {
904
+ if (buffer.length > 0) {
905
+ const next = buffer.shift();
906
+ if (next !== undefined) controller.enqueue(next);
907
+ return;
908
+ }
909
+ const read = await readChunk();
910
+ if (read.done) {
911
+ if (state.startEmitted && !state.messageStopEmitted) {
912
+ // Upstream ended WITHOUT a finish_reason. Mid-tool / mid-
913
+ // text tears (no real usage trailer) stay `length` →
914
+ // Anthropic `max_tokens` so Claude Code does not treat a
915
+ // truncated tool_use as executable. A usage trailer with
916
+ // prompt_tokens > 0 and no open tool is a clean
917
+ // OpenAI-compat close (DashScope / Kimi often omit
918
+ // finish_reason) → `stop` / `end_turn`. Live-estimate
919
+ // frames keep prompt_tokens at 0 and do not flip this.
920
+ // Only the LAST chunk's usage counts: a usage frame
921
+ // followed by more content is a tear, not a clean close.
922
+ const torn =
923
+ state.openToolContentIndexes.size > 0 ||
924
+ state.pendingToolCalls.size > 0;
925
+ const finishReason =
926
+ !torn && state.lastChunkHadProviderUsage ? "stop" : "length";
927
+ const tail = chunkToMessagesEvents(
928
+ {
929
+ id:
930
+ state.messageId !== ""
931
+ ? state.messageId
932
+ : "chatcmpl-truncated",
933
+ object: "chat.completion.chunk",
934
+ created: Math.floor(Date.now() / 1000),
935
+ model: state.model !== "" ? state.model : "unknown",
936
+ // The trailer was already consumed; fold last-seen
937
+ // usage onto this finish chunk so the terminal
938
+ // `message_delta` reconciles to the provider totals
939
+ // instead of `output_tokens: 0`. Keep cache details so
940
+ // re-injection does not wipe inclusive state buckets
941
+ // before the exclusive Anthropic emit.
942
+ usage: {
943
+ prompt_tokens: state.inputTokens,
944
+ completion_tokens: state.outputTokens,
945
+ total_tokens: state.inputTokens + state.outputTokens,
946
+ ...(state.cacheReadTokens > 0 ||
947
+ state.cacheCreationTokens > 0
948
+ ? {
949
+ prompt_tokens_details: {
950
+ ...(state.cacheReadTokens > 0
951
+ ? { cached_tokens: state.cacheReadTokens }
952
+ : {}),
953
+ ...(state.cacheCreationTokens > 0
954
+ ? {
955
+ cache_creation_tokens:
956
+ state.cacheCreationTokens,
957
+ }
958
+ : {}),
959
+ },
960
+ }
961
+ : {}),
962
+ },
963
+ choices: [
964
+ {
965
+ index: 0,
966
+ delta: {},
967
+ finish_reason: finishReason,
968
+ },
969
+ ],
970
+ },
971
+ state,
972
+ );
973
+ for (const e of tail) {
974
+ buffer.push(encodeAnthropicSseEvent(e));
975
+ }
976
+ }
977
+ while (buffer.length > 0) {
978
+ const next = buffer.shift();
979
+ if (next !== undefined) controller.enqueue(next);
980
+ }
981
+ controller.close();
982
+ return;
983
+ }
984
+ let value = read.value;
985
+ // If this chunk ends the turn but has no usage yet, peek the
986
+ // next one: a trailing usage-only chunk gets folded in so the
987
+ // terminal `message_delta` carries real input/output/cache
988
+ // tokens. Anything else is stashed and processed next.
989
+ const endsTurn = value.choices.some(
990
+ (c) => c.finish_reason != null && c.finish_reason !== undefined,
991
+ );
992
+ if (endsTurn && value.usage == null) {
993
+ const la = await readChunk();
994
+ if (!la.done) {
995
+ if (isUsageOnly(la.value)) {
996
+ value = { ...value, usage: la.value.usage };
997
+ } else {
998
+ pending = la.value;
999
+ }
1000
+ }
1001
+ }
1002
+ const events = chunkToMessagesEvents(value, state);
1003
+ for (const e of events) buffer.push(encodeAnthropicSseEvent(e));
1004
+ }
1005
+ } catch (err) {
1006
+ // Upstream errored mid-stream — for example Anthropic's
1007
+ // `event: error` (overloaded_error/api_error). Surface as an
1008
+ // Anthropic-format error event so Claude Code sees a clean
1009
+ // failure rather than an abruptly closed stream (which is
1010
+ // exactly what manifested as "compaction fails at 20%").
1011
+ const { type, message } = upstreamErrorFrom(err);
1012
+ buffer.push(
1013
+ encodeAnthropicSseEvent({
1014
+ type: "error",
1015
+ error: { type, message },
1016
+ }),
1017
+ );
1018
+ const next = buffer.shift();
1019
+ if (next !== undefined) controller.enqueue(next);
1020
+ controller.close();
1021
+ }
1022
+ },
1023
+ cancel(reason) {
1024
+ reader.cancel(reason).catch(() => {});
1025
+ },
1026
+ });
1027
+ };