@aexol/spectral 0.7.1 → 0.7.5

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 (219) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/agent/agents.js +1 -1
  3. package/dist/agent/index.js +199 -184
  4. package/dist/commands/serve.js +0 -3
  5. package/dist/designer/data/systems/renault/DESIGN.md +1 -1
  6. package/dist/designer/philosophies.js +668 -0
  7. package/dist/mcp/sampling-handler.js +1 -1
  8. package/dist/memory/commands/status.js +1 -1
  9. package/dist/memory/compaction.js +2 -2
  10. package/dist/memory/config.js +1 -1
  11. package/dist/memory/debug-log.js +1 -1
  12. package/dist/memory/hooks/compaction-hook.js +29 -0
  13. package/dist/memory/index.js +2 -0
  14. package/dist/memory/observer.js +2 -2
  15. package/dist/memory/project-observations-store.js +14 -0
  16. package/dist/memory/tokens.js +1 -1
  17. package/dist/memory/tools/read-project-observations.js +82 -0
  18. package/dist/memory/tools/recall-observation.js +2 -2
  19. package/dist/pi/agent-core/agent-loop.js +501 -0
  20. package/dist/pi/agent-core/agent.js +401 -0
  21. package/dist/pi/agent-core/harness/agent-harness.js +899 -0
  22. package/dist/pi/agent-core/harness/compaction/branch-summarization.js +173 -0
  23. package/dist/pi/agent-core/harness/compaction/compaction.js +532 -0
  24. package/dist/pi/agent-core/harness/compaction/utils.js +130 -0
  25. package/dist/pi/agent-core/harness/env/nodejs.js +485 -0
  26. package/dist/pi/agent-core/harness/messages.js +101 -0
  27. package/dist/pi/agent-core/harness/prompt-templates.js +229 -0
  28. package/dist/pi/agent-core/harness/session/jsonl-repo.js +100 -0
  29. package/dist/pi/agent-core/harness/session/jsonl-storage.js +230 -0
  30. package/dist/pi/agent-core/harness/session/memory-repo.js +41 -0
  31. package/dist/pi/agent-core/harness/session/memory-storage.js +113 -0
  32. package/dist/pi/agent-core/harness/session/repo-utils.js +38 -0
  33. package/dist/pi/agent-core/harness/session/session.js +196 -0
  34. package/dist/pi/agent-core/harness/session/uuid.js +49 -0
  35. package/dist/pi/agent-core/harness/skills.js +310 -0
  36. package/dist/pi/agent-core/harness/system-prompt.js +29 -0
  37. package/dist/pi/agent-core/harness/types.js +93 -0
  38. package/dist/pi/agent-core/harness/utils/shell-output.js +125 -0
  39. package/dist/pi/agent-core/harness/utils/truncate.js +289 -0
  40. package/dist/pi/agent-core/index.js +24 -0
  41. package/dist/pi/agent-core/node.js +2 -0
  42. package/dist/pi/agent-core/proxy.js +277 -0
  43. package/dist/pi/agent-core/types.js +1 -0
  44. package/dist/pi/ai/api-registry.js +43 -0
  45. package/dist/pi/ai/cli.js +120 -0
  46. package/dist/pi/ai/env-api-keys.js +169 -0
  47. package/dist/pi/ai/image-models.generated.js +441 -0
  48. package/dist/pi/ai/image-models.js +22 -0
  49. package/dist/pi/ai/images-api-registry.js +21 -0
  50. package/dist/pi/ai/images.js +13 -0
  51. package/dist/pi/ai/index.js +18 -0
  52. package/dist/pi/ai/models.generated.js +16220 -0
  53. package/dist/pi/ai/models.js +70 -0
  54. package/dist/pi/ai/oauth.js +1 -0
  55. package/dist/pi/ai/providers/anthropic.js +945 -0
  56. package/dist/pi/ai/providers/faux.js +367 -0
  57. package/dist/pi/ai/providers/github-copilot-headers.js +28 -0
  58. package/dist/pi/ai/providers/openai-completions.js +945 -0
  59. package/dist/pi/ai/providers/openai-prompt-cache.js +9 -0
  60. package/dist/pi/ai/providers/register-builtins.js +97 -0
  61. package/dist/pi/ai/providers/simple-options.js +40 -0
  62. package/dist/pi/ai/providers/transform-messages.js +183 -0
  63. package/dist/pi/ai/session-resources.js +21 -0
  64. package/dist/pi/ai/stream.js +26 -0
  65. package/dist/pi/ai/types.js +1 -0
  66. package/dist/pi/ai/utils/diagnostics.js +24 -0
  67. package/dist/pi/ai/utils/event-stream.js +80 -0
  68. package/dist/pi/ai/utils/hash.js +13 -0
  69. package/dist/pi/ai/utils/headers.js +7 -0
  70. package/dist/pi/ai/utils/json-parse.js +112 -0
  71. package/dist/pi/ai/utils/node-http-proxy.js +96 -0
  72. package/dist/pi/ai/utils/oauth/anthropic.js +334 -0
  73. package/dist/pi/ai/utils/oauth/device-code.js +54 -0
  74. package/dist/pi/ai/utils/oauth/github-copilot.js +270 -0
  75. package/dist/pi/ai/utils/oauth/index.js +121 -0
  76. package/dist/pi/ai/utils/oauth/oauth-page.js +104 -0
  77. package/dist/pi/ai/utils/oauth/openai-codex.js +384 -0
  78. package/dist/pi/ai/utils/oauth/pkce.js +30 -0
  79. package/dist/pi/ai/utils/oauth/types.js +1 -0
  80. package/dist/pi/ai/utils/overflow.js +150 -0
  81. package/dist/pi/ai/utils/sanitize-unicode.js +25 -0
  82. package/dist/pi/ai/utils/typebox-helpers.js +20 -0
  83. package/dist/pi/ai/utils/validation.js +280 -0
  84. package/dist/pi/coding-agent/bun/cli.js +7 -0
  85. package/dist/pi/coding-agent/bun/restore-sandbox-env.js +31 -0
  86. package/dist/pi/coding-agent/cli/args.js +340 -0
  87. package/dist/pi/coding-agent/cli/file-processor.js +82 -0
  88. package/dist/pi/coding-agent/cli/initial-message.js +21 -0
  89. package/dist/pi/coding-agent/cli.js +17 -0
  90. package/dist/pi/coding-agent/config.js +414 -0
  91. package/dist/pi/coding-agent/core/agent-session-runtime.js +299 -0
  92. package/dist/pi/coding-agent/core/agent-session-services.js +117 -0
  93. package/dist/pi/coding-agent/core/agent-session.js +2498 -0
  94. package/dist/pi/coding-agent/core/auth-guidance.js +20 -0
  95. package/dist/pi/coding-agent/core/auth-storage.js +441 -0
  96. package/dist/pi/coding-agent/core/bash-executor.js +110 -0
  97. package/dist/pi/coding-agent/core/compaction/branch-summarization.js +242 -0
  98. package/dist/pi/coding-agent/core/compaction/compaction.js +624 -0
  99. package/dist/pi/coding-agent/core/compaction/index.js +6 -0
  100. package/dist/pi/coding-agent/core/compaction/utils.js +152 -0
  101. package/dist/pi/coding-agent/core/defaults.js +1 -0
  102. package/dist/pi/coding-agent/core/diagnostics.js +1 -0
  103. package/dist/pi/coding-agent/core/event-bus.js +24 -0
  104. package/dist/pi/coding-agent/core/exec.js +74 -0
  105. package/dist/pi/coding-agent/core/export-html/ansi-to-html.js +248 -0
  106. package/dist/pi/coding-agent/core/export-html/index.js +225 -0
  107. package/dist/pi/coding-agent/core/export-html/tool-renderer.js +107 -0
  108. package/dist/pi/coding-agent/core/extensions/index.js +8 -0
  109. package/dist/pi/coding-agent/core/extensions/loader.js +485 -0
  110. package/dist/pi/coding-agent/core/extensions/runner.js +824 -0
  111. package/dist/pi/coding-agent/core/extensions/types.js +44 -0
  112. package/dist/pi/coding-agent/core/extensions/wrapper.js +21 -0
  113. package/dist/pi/coding-agent/core/footer-data-provider.js +309 -0
  114. package/dist/pi/coding-agent/core/http-dispatcher.js +47 -0
  115. package/dist/pi/coding-agent/core/index.js +11 -0
  116. package/dist/pi/coding-agent/core/keybindings.js +294 -0
  117. package/dist/pi/coding-agent/core/messages.js +122 -0
  118. package/dist/pi/coding-agent/core/model-registry.js +728 -0
  119. package/dist/pi/coding-agent/core/model-resolver.js +494 -0
  120. package/dist/pi/coding-agent/core/output-guard.js +58 -0
  121. package/dist/pi/coding-agent/core/package-manager.js +2020 -0
  122. package/dist/pi/coding-agent/core/prompt-templates.js +237 -0
  123. package/dist/pi/coding-agent/core/provider-display-names.js +32 -0
  124. package/dist/pi/coding-agent/core/resolve-config-value.js +125 -0
  125. package/dist/pi/coding-agent/core/resource-loader.js +733 -0
  126. package/dist/pi/coding-agent/core/sdk.js +282 -0
  127. package/dist/pi/coding-agent/core/session-cwd.js +37 -0
  128. package/dist/pi/coding-agent/core/session-manager.js +1146 -0
  129. package/dist/pi/coding-agent/core/settings-manager.js +794 -0
  130. package/dist/pi/coding-agent/core/skills.js +386 -0
  131. package/dist/pi/coding-agent/core/slash-commands.js +24 -0
  132. package/dist/pi/coding-agent/core/source-info.js +18 -0
  133. package/dist/pi/coding-agent/core/system-prompt.js +122 -0
  134. package/dist/pi/coding-agent/core/telemetry.js +8 -0
  135. package/dist/pi/coding-agent/core/timings.js +30 -0
  136. package/dist/pi/coding-agent/core/tools/bash.js +341 -0
  137. package/dist/pi/coding-agent/core/tools/edit-diff.js +344 -0
  138. package/dist/pi/coding-agent/core/tools/edit.js +324 -0
  139. package/dist/pi/coding-agent/core/tools/file-mutation-queue.js +36 -0
  140. package/dist/pi/coding-agent/core/tools/find.js +297 -0
  141. package/dist/pi/coding-agent/core/tools/grep.js +303 -0
  142. package/dist/pi/coding-agent/core/tools/index.js +111 -0
  143. package/dist/pi/coding-agent/core/tools/ls.js +168 -0
  144. package/dist/pi/coding-agent/core/tools/output-accumulator.js +183 -0
  145. package/dist/pi/coding-agent/core/tools/path-utils.js +61 -0
  146. package/dist/pi/coding-agent/core/tools/read.js +288 -0
  147. package/dist/pi/coding-agent/core/tools/render-utils.js +48 -0
  148. package/dist/pi/coding-agent/core/tools/tool-definition-wrapper.js +33 -0
  149. package/dist/pi/coding-agent/core/tools/truncate.js +214 -0
  150. package/dist/pi/coding-agent/core/tools/write.js +212 -0
  151. package/dist/pi/coding-agent/index.js +41 -0
  152. package/dist/pi/coding-agent/main.js +5 -0
  153. package/dist/pi/coding-agent/migrations.js +280 -0
  154. package/dist/pi/coding-agent/modes/index.js +7 -0
  155. package/dist/pi/coding-agent/modes/interactive/components/diff.js +132 -0
  156. package/dist/pi/coding-agent/modes/interactive/components/keybinding-hints.js +35 -0
  157. package/dist/pi/coding-agent/modes/interactive/components/visual-truncate.js +32 -0
  158. package/dist/pi/coding-agent/modes/interactive/interactive-mode.js +3 -0
  159. package/dist/pi/coding-agent/modes/interactive/theme/theme.js +1023 -0
  160. package/dist/pi/coding-agent/modes/print-mode.js +130 -0
  161. package/dist/pi/coding-agent/modes/rpc/jsonl.js +48 -0
  162. package/dist/pi/coding-agent/modes/rpc/rpc-client.js +409 -0
  163. package/dist/pi/coding-agent/modes/rpc/rpc-mode.js +600 -0
  164. package/dist/pi/coding-agent/modes/rpc/rpc-types.js +7 -0
  165. package/dist/pi/coding-agent/utils/ansi.js +51 -0
  166. package/dist/pi/coding-agent/utils/changelog.js +86 -0
  167. package/dist/pi/coding-agent/utils/child-process.js +87 -0
  168. package/dist/pi/coding-agent/utils/clipboard-image.js +244 -0
  169. package/dist/pi/coding-agent/utils/clipboard-native.js +13 -0
  170. package/dist/pi/coding-agent/utils/clipboard.js +116 -0
  171. package/dist/pi/coding-agent/utils/exif-orientation.js +157 -0
  172. package/dist/pi/coding-agent/utils/frontmatter.js +25 -0
  173. package/dist/pi/coding-agent/utils/fs-watch.js +24 -0
  174. package/dist/pi/coding-agent/utils/git.js +162 -0
  175. package/dist/pi/coding-agent/utils/html.js +39 -0
  176. package/dist/pi/coding-agent/utils/image-convert.js +38 -0
  177. package/dist/pi/coding-agent/utils/image-resize.js +136 -0
  178. package/dist/pi/coding-agent/utils/mime.js +68 -0
  179. package/dist/pi/coding-agent/utils/paths.js +91 -0
  180. package/dist/pi/coding-agent/utils/photon.js +120 -0
  181. package/dist/pi/coding-agent/utils/pi-user-agent.js +4 -0
  182. package/dist/pi/coding-agent/utils/shell.js +194 -0
  183. package/dist/pi/coding-agent/utils/sleep.js +16 -0
  184. package/dist/pi/coding-agent/utils/syntax-highlight.js +117 -0
  185. package/dist/pi/coding-agent/utils/tools-manager.js +327 -0
  186. package/dist/pi/coding-agent/utils/version-check.js +81 -0
  187. package/dist/pi/coding-agent/utils/windows-self-update.js +76 -0
  188. package/dist/pi/tui/autocomplete.js +631 -0
  189. package/dist/pi/tui/components/box.js +103 -0
  190. package/dist/pi/tui/components/cancellable-loader.js +34 -0
  191. package/dist/pi/tui/components/editor.js +1915 -0
  192. package/dist/pi/tui/components/image.js +88 -0
  193. package/dist/pi/tui/components/input.js +425 -0
  194. package/dist/pi/tui/components/loader.js +68 -0
  195. package/dist/pi/tui/components/markdown.js +633 -0
  196. package/dist/pi/tui/components/select-list.js +158 -0
  197. package/dist/pi/tui/components/settings-list.js +184 -0
  198. package/dist/pi/tui/components/spacer.js +22 -0
  199. package/dist/pi/tui/components/text.js +88 -0
  200. package/dist/pi/tui/components/truncated-text.js +50 -0
  201. package/dist/pi/tui/editor-component.js +1 -0
  202. package/dist/pi/tui/fuzzy.js +109 -0
  203. package/dist/pi/tui/index.js +31 -0
  204. package/dist/pi/tui/keybindings.js +173 -0
  205. package/dist/pi/tui/keys.js +1172 -0
  206. package/dist/pi/tui/kill-ring.js +43 -0
  207. package/dist/pi/tui/stdin-buffer.js +360 -0
  208. package/dist/pi/tui/terminal-image.js +335 -0
  209. package/dist/pi/tui/terminal.js +324 -0
  210. package/dist/pi/tui/tui.js +1076 -0
  211. package/dist/pi/tui/undo-stack.js +24 -0
  212. package/dist/pi/tui/utils.js +1016 -0
  213. package/dist/relay/dispatcher.js +30 -0
  214. package/dist/server/handlers/queue.js +52 -0
  215. package/dist/server/pi-bridge.js +9 -1
  216. package/dist/server/session-stream.js +76 -111
  217. package/dist/server/storage.js +154 -2
  218. package/dist/server/title-generator.js +14 -153
  219. package/package.json +24 -6
@@ -0,0 +1,945 @@
1
+ import Anthropic from "@anthropic-ai/sdk";
2
+ import { getEnvApiKey } from "../env-api-keys.js";
3
+ import { calculateCost } from "../models.js";
4
+ import { AssistantMessageEventStream } from "../utils/event-stream.js";
5
+ import { headersToRecord } from "../utils/headers.js";
6
+ import { parseJsonWithRepair, parseStreamingJson } from "../utils/json-parse.js";
7
+ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
8
+ import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
9
+ import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.js";
10
+ import { transformMessages } from "./transform-messages.js";
11
+ /**
12
+ * Resolve cache retention preference.
13
+ * Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
14
+ */
15
+ function resolveCacheRetention(cacheRetention) {
16
+ if (cacheRetention) {
17
+ return cacheRetention;
18
+ }
19
+ if (typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "long") {
20
+ return "long";
21
+ }
22
+ return "short";
23
+ }
24
+ function getCacheControl(model, cacheRetention) {
25
+ const retention = resolveCacheRetention(cacheRetention);
26
+ if (retention === "none") {
27
+ return { retention };
28
+ }
29
+ const ttl = retention === "long" && getAnthropicCompat(model).supportsLongCacheRetention ? "1h" : undefined;
30
+ return {
31
+ retention,
32
+ cacheControl: { type: "ephemeral", ...(ttl && { ttl }) },
33
+ };
34
+ }
35
+ // Stealth mode: Mimic Claude Code's tool naming exactly
36
+ const claudeCodeVersion = "2.1.75";
37
+ // Claude Code 2.x tool names (canonical casing)
38
+ // Source: https://cchistory.mariozechner.at/data/prompts-2.1.11.md
39
+ // To update: https://github.com/badlogic/cchistory
40
+ const claudeCodeTools = [
41
+ "Read",
42
+ "Write",
43
+ "Edit",
44
+ "Bash",
45
+ "Grep",
46
+ "Glob",
47
+ "AskUserQuestion",
48
+ "EnterPlanMode",
49
+ "ExitPlanMode",
50
+ "KillShell",
51
+ "NotebookEdit",
52
+ "Skill",
53
+ "Task",
54
+ "TaskOutput",
55
+ "TodoWrite",
56
+ "WebFetch",
57
+ "WebSearch",
58
+ ];
59
+ const ccToolLookup = new Map(claudeCodeTools.map((t) => [t.toLowerCase(), t]));
60
+ // Convert tool name to CC canonical casing if it matches (case-insensitive)
61
+ const toClaudeCodeName = (name) => ccToolLookup.get(name.toLowerCase()) ?? name;
62
+ const fromClaudeCodeName = (name, tools) => {
63
+ if (tools && tools.length > 0) {
64
+ const lowerName = name.toLowerCase();
65
+ const matchedTool = tools.find((tool) => tool.name.toLowerCase() === lowerName);
66
+ if (matchedTool)
67
+ return matchedTool.name;
68
+ }
69
+ return name;
70
+ };
71
+ /**
72
+ * Convert content blocks to Anthropic API format
73
+ */
74
+ function convertContentBlocks(content) {
75
+ // If only text blocks, return as concatenated string for simplicity
76
+ const hasImages = content.some((c) => c.type === "image");
77
+ if (!hasImages) {
78
+ return sanitizeSurrogates(content.map((c) => c.text).join("\n"));
79
+ }
80
+ // If we have images, convert to content block array
81
+ const blocks = content.map((block) => {
82
+ if (block.type === "text") {
83
+ return {
84
+ type: "text",
85
+ text: sanitizeSurrogates(block.text),
86
+ };
87
+ }
88
+ return {
89
+ type: "image",
90
+ source: {
91
+ type: "base64",
92
+ media_type: block.mimeType,
93
+ data: block.data,
94
+ },
95
+ };
96
+ });
97
+ // If only images (no text), add placeholder text block
98
+ const hasText = blocks.some((b) => b.type === "text");
99
+ if (!hasText) {
100
+ blocks.unshift({
101
+ type: "text",
102
+ text: "(see attached image)",
103
+ });
104
+ }
105
+ return blocks;
106
+ }
107
+ const FINE_GRAINED_TOOL_STREAMING_BETA = "fine-grained-tool-streaming-2025-05-14";
108
+ const INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
109
+ function getAnthropicCompat(model) {
110
+ // Auto-detect session affinity and cache control support from provider
111
+ const isFireworks = model.provider === "fireworks";
112
+ // Cloudflare providers not supported
113
+ const isCloudflareAiGatewayAnthropic = false;
114
+ return {
115
+ supportsEagerToolInputStreaming: model.compat?.supportsEagerToolInputStreaming ?? !isFireworks,
116
+ supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? !isFireworks,
117
+ sendSessionAffinityHeaders: model.compat?.sendSessionAffinityHeaders ?? !!(isFireworks || isCloudflareAiGatewayAnthropic),
118
+ supportsCacheControlOnTools: model.compat?.supportsCacheControlOnTools ?? !isFireworks,
119
+ };
120
+ }
121
+ function mergeHeaders(...headerSources) {
122
+ const merged = {};
123
+ for (const headers of headerSources) {
124
+ if (headers) {
125
+ Object.assign(merged, headers);
126
+ }
127
+ }
128
+ return merged;
129
+ }
130
+ const ANTHROPIC_MESSAGE_EVENTS = new Set([
131
+ "message_start",
132
+ "message_delta",
133
+ "message_stop",
134
+ "content_block_start",
135
+ "content_block_delta",
136
+ "content_block_stop",
137
+ ]);
138
+ function flushSseEvent(state) {
139
+ if (!state.event && state.data.length === 0) {
140
+ return null;
141
+ }
142
+ const event = {
143
+ event: state.event,
144
+ data: state.data.join("\n"),
145
+ raw: [...state.raw],
146
+ };
147
+ state.event = null;
148
+ state.data = [];
149
+ state.raw = [];
150
+ return event;
151
+ }
152
+ function decodeSseLine(line, state) {
153
+ if (line === "") {
154
+ return flushSseEvent(state);
155
+ }
156
+ state.raw.push(line);
157
+ if (line.startsWith(":")) {
158
+ return null;
159
+ }
160
+ const delimiterIndex = line.indexOf(":");
161
+ const fieldName = delimiterIndex === -1 ? line : line.slice(0, delimiterIndex);
162
+ let value = delimiterIndex === -1 ? "" : line.slice(delimiterIndex + 1);
163
+ if (value.startsWith(" ")) {
164
+ value = value.slice(1);
165
+ }
166
+ if (fieldName === "event") {
167
+ state.event = value;
168
+ }
169
+ else if (fieldName === "data") {
170
+ state.data.push(value);
171
+ }
172
+ return null;
173
+ }
174
+ function nextLineBreakIndex(text) {
175
+ const carriageReturnIndex = text.indexOf("\r");
176
+ const newlineIndex = text.indexOf("\n");
177
+ if (carriageReturnIndex === -1) {
178
+ return newlineIndex;
179
+ }
180
+ if (newlineIndex === -1) {
181
+ return carriageReturnIndex;
182
+ }
183
+ return Math.min(carriageReturnIndex, newlineIndex);
184
+ }
185
+ function consumeLine(text) {
186
+ const lineBreakIndex = nextLineBreakIndex(text);
187
+ if (lineBreakIndex === -1) {
188
+ return null;
189
+ }
190
+ let nextIndex = lineBreakIndex + 1;
191
+ if (text[lineBreakIndex] === "\r" && text[nextIndex] === "\n") {
192
+ nextIndex += 1;
193
+ }
194
+ return {
195
+ line: text.slice(0, lineBreakIndex),
196
+ rest: text.slice(nextIndex),
197
+ };
198
+ }
199
+ async function* iterateSseMessages(body, signal) {
200
+ const reader = body.getReader();
201
+ const decoder = new TextDecoder();
202
+ const state = { event: null, data: [], raw: [] };
203
+ let buffer = "";
204
+ try {
205
+ while (true) {
206
+ if (signal?.aborted) {
207
+ throw new Error("Request was aborted");
208
+ }
209
+ const { value, done } = await reader.read();
210
+ if (done) {
211
+ break;
212
+ }
213
+ buffer += decoder.decode(value, { stream: true });
214
+ let consumed = consumeLine(buffer);
215
+ while (consumed) {
216
+ buffer = consumed.rest;
217
+ const event = decodeSseLine(consumed.line, state);
218
+ if (event) {
219
+ yield event;
220
+ }
221
+ consumed = consumeLine(buffer);
222
+ }
223
+ }
224
+ buffer += decoder.decode();
225
+ let consumed = consumeLine(buffer);
226
+ while (consumed) {
227
+ buffer = consumed.rest;
228
+ const event = decodeSseLine(consumed.line, state);
229
+ if (event) {
230
+ yield event;
231
+ }
232
+ consumed = consumeLine(buffer);
233
+ }
234
+ if (buffer.length > 0) {
235
+ const event = decodeSseLine(buffer, state);
236
+ if (event) {
237
+ yield event;
238
+ }
239
+ }
240
+ const trailingEvent = flushSseEvent(state);
241
+ if (trailingEvent) {
242
+ yield trailingEvent;
243
+ }
244
+ }
245
+ finally {
246
+ reader.releaseLock();
247
+ }
248
+ }
249
+ async function* iterateAnthropicEvents(response, signal) {
250
+ if (!response.body) {
251
+ throw new Error("Attempted to iterate over an Anthropic response with no body");
252
+ }
253
+ let sawMessageStart = false;
254
+ let sawMessageEnd = false;
255
+ for await (const sse of iterateSseMessages(response.body, signal)) {
256
+ if (sse.event === "error") {
257
+ throw new Error(sse.data);
258
+ }
259
+ if (!ANTHROPIC_MESSAGE_EVENTS.has(sse.event ?? "")) {
260
+ continue;
261
+ }
262
+ try {
263
+ const event = parseJsonWithRepair(sse.data);
264
+ if (event.type === "message_start") {
265
+ sawMessageStart = true;
266
+ }
267
+ else if (event.type === "message_stop") {
268
+ sawMessageEnd = true;
269
+ }
270
+ yield event;
271
+ }
272
+ catch (error) {
273
+ const message = error instanceof Error ? error.message : String(error);
274
+ throw new Error(`Could not parse Anthropic SSE event ${sse.event}: ${message}; data=${sse.data}; raw=${sse.raw.join("\\n")}`);
275
+ }
276
+ }
277
+ if (sawMessageStart && !sawMessageEnd) {
278
+ throw new Error("Anthropic stream ended before message_stop");
279
+ }
280
+ }
281
+ export const streamAnthropic = (model, context, options) => {
282
+ const stream = new AssistantMessageEventStream();
283
+ (async () => {
284
+ const output = {
285
+ role: "assistant",
286
+ content: [],
287
+ api: model.api,
288
+ provider: model.provider,
289
+ model: model.id,
290
+ usage: {
291
+ input: 0,
292
+ output: 0,
293
+ cacheRead: 0,
294
+ cacheWrite: 0,
295
+ totalTokens: 0,
296
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
297
+ },
298
+ stopReason: "stop",
299
+ timestamp: Date.now(),
300
+ };
301
+ try {
302
+ let client;
303
+ let isOAuth;
304
+ if (options?.client) {
305
+ client = options.client;
306
+ isOAuth = false;
307
+ }
308
+ else {
309
+ const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
310
+ let copilotDynamicHeaders;
311
+ if (model.provider === "github-copilot") {
312
+ const hasImages = hasCopilotVisionInput(context.messages);
313
+ copilotDynamicHeaders = buildCopilotDynamicHeaders({
314
+ messages: context.messages,
315
+ hasImages,
316
+ });
317
+ }
318
+ const cacheRetention = options?.cacheRetention ?? resolveCacheRetention();
319
+ const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
320
+ const created = createClient(model, apiKey, options?.interleavedThinking ?? true, shouldUseFineGrainedToolStreamingBeta(model, context), options?.headers, copilotDynamicHeaders, cacheSessionId);
321
+ client = created.client;
322
+ isOAuth = created.isOAuthToken;
323
+ }
324
+ let params = buildParams(model, context, isOAuth, options);
325
+ const nextParams = await options?.onPayload?.(params, model);
326
+ if (nextParams !== undefined) {
327
+ params = nextParams;
328
+ }
329
+ const requestOptions = {
330
+ ...(options?.signal ? { signal: options.signal } : {}),
331
+ ...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
332
+ ...(options?.maxRetries !== undefined ? { maxRetries: options.maxRetries } : {}),
333
+ };
334
+ const response = await client.messages.create({ ...params, stream: true }, requestOptions).asResponse();
335
+ await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
336
+ stream.push({ type: "start", partial: output });
337
+ const blocks = output.content;
338
+ for await (const event of iterateAnthropicEvents(response, options?.signal)) {
339
+ if (event.type === "message_start") {
340
+ output.responseId = event.message.id;
341
+ // Capture initial token usage from message_start event
342
+ // This ensures we have input token counts even if the stream is aborted early
343
+ output.usage.input = event.message.usage.input_tokens || 0;
344
+ output.usage.output = event.message.usage.output_tokens || 0;
345
+ output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
346
+ output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
347
+ // Anthropic doesn't provide total_tokens, compute from components
348
+ output.usage.totalTokens =
349
+ output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
350
+ calculateCost(model, output.usage);
351
+ }
352
+ else if (event.type === "content_block_start") {
353
+ if (event.content_block.type === "text") {
354
+ const block = {
355
+ type: "text",
356
+ text: "",
357
+ index: event.index,
358
+ };
359
+ output.content.push(block);
360
+ stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
361
+ }
362
+ else if (event.content_block.type === "thinking") {
363
+ const block = {
364
+ type: "thinking",
365
+ thinking: "",
366
+ thinkingSignature: "",
367
+ index: event.index,
368
+ };
369
+ output.content.push(block);
370
+ stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
371
+ }
372
+ else if (event.content_block.type === "redacted_thinking") {
373
+ const block = {
374
+ type: "thinking",
375
+ thinking: "[Reasoning redacted]",
376
+ thinkingSignature: event.content_block.data,
377
+ redacted: true,
378
+ index: event.index,
379
+ };
380
+ output.content.push(block);
381
+ stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
382
+ }
383
+ else if (event.content_block.type === "tool_use") {
384
+ const block = {
385
+ type: "toolCall",
386
+ id: event.content_block.id,
387
+ name: isOAuth
388
+ ? fromClaudeCodeName(event.content_block.name, context.tools)
389
+ : event.content_block.name,
390
+ arguments: event.content_block.input ?? {},
391
+ partialJson: "",
392
+ index: event.index,
393
+ };
394
+ output.content.push(block);
395
+ stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
396
+ }
397
+ }
398
+ else if (event.type === "content_block_delta") {
399
+ if (event.delta.type === "text_delta") {
400
+ const index = blocks.findIndex((b) => b.index === event.index);
401
+ const block = blocks[index];
402
+ if (block && block.type === "text") {
403
+ block.text += event.delta.text;
404
+ stream.push({
405
+ type: "text_delta",
406
+ contentIndex: index,
407
+ delta: event.delta.text,
408
+ partial: output,
409
+ });
410
+ }
411
+ }
412
+ else if (event.delta.type === "thinking_delta") {
413
+ const index = blocks.findIndex((b) => b.index === event.index);
414
+ const block = blocks[index];
415
+ if (block && block.type === "thinking") {
416
+ block.thinking += event.delta.thinking;
417
+ stream.push({
418
+ type: "thinking_delta",
419
+ contentIndex: index,
420
+ delta: event.delta.thinking,
421
+ partial: output,
422
+ });
423
+ }
424
+ }
425
+ else if (event.delta.type === "input_json_delta") {
426
+ const index = blocks.findIndex((b) => b.index === event.index);
427
+ const block = blocks[index];
428
+ if (block && block.type === "toolCall") {
429
+ block.partialJson += event.delta.partial_json;
430
+ block.arguments = parseStreamingJson(block.partialJson);
431
+ stream.push({
432
+ type: "toolcall_delta",
433
+ contentIndex: index,
434
+ delta: event.delta.partial_json,
435
+ partial: output,
436
+ });
437
+ }
438
+ }
439
+ else if (event.delta.type === "signature_delta") {
440
+ const index = blocks.findIndex((b) => b.index === event.index);
441
+ const block = blocks[index];
442
+ if (block && block.type === "thinking") {
443
+ block.thinkingSignature = block.thinkingSignature || "";
444
+ block.thinkingSignature += event.delta.signature;
445
+ }
446
+ }
447
+ }
448
+ else if (event.type === "content_block_stop") {
449
+ const index = blocks.findIndex((b) => b.index === event.index);
450
+ const block = blocks[index];
451
+ if (block) {
452
+ delete block.index;
453
+ if (block.type === "text") {
454
+ stream.push({
455
+ type: "text_end",
456
+ contentIndex: index,
457
+ content: block.text,
458
+ partial: output,
459
+ });
460
+ }
461
+ else if (block.type === "thinking") {
462
+ stream.push({
463
+ type: "thinking_end",
464
+ contentIndex: index,
465
+ content: block.thinking,
466
+ partial: output,
467
+ });
468
+ }
469
+ else if (block.type === "toolCall") {
470
+ block.arguments = parseStreamingJson(block.partialJson);
471
+ // Finalize in-place and strip the scratch buffer so replay only
472
+ // carries parsed arguments.
473
+ delete block.partialJson;
474
+ stream.push({
475
+ type: "toolcall_end",
476
+ contentIndex: index,
477
+ toolCall: block,
478
+ partial: output,
479
+ });
480
+ }
481
+ }
482
+ }
483
+ else if (event.type === "message_delta") {
484
+ if (event.delta.stop_reason) {
485
+ output.stopReason = mapStopReason(event.delta.stop_reason);
486
+ }
487
+ // Only update usage fields if present (not null).
488
+ // Preserves input_tokens from message_start when proxies omit it in message_delta.
489
+ if (event.usage.input_tokens != null) {
490
+ output.usage.input = event.usage.input_tokens;
491
+ }
492
+ if (event.usage.output_tokens != null) {
493
+ output.usage.output = event.usage.output_tokens;
494
+ }
495
+ if (event.usage.cache_read_input_tokens != null) {
496
+ output.usage.cacheRead = event.usage.cache_read_input_tokens;
497
+ }
498
+ if (event.usage.cache_creation_input_tokens != null) {
499
+ output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
500
+ }
501
+ // Anthropic doesn't provide total_tokens, compute from components
502
+ output.usage.totalTokens =
503
+ output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
504
+ calculateCost(model, output.usage);
505
+ }
506
+ }
507
+ if (options?.signal?.aborted) {
508
+ throw new Error("Request was aborted");
509
+ }
510
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
511
+ throw new Error("An unknown error occurred");
512
+ }
513
+ stream.push({ type: "done", reason: output.stopReason, message: output });
514
+ stream.end();
515
+ }
516
+ catch (error) {
517
+ for (const block of output.content) {
518
+ delete block.index;
519
+ // partialJson is only a streaming scratch buffer; never persist it.
520
+ delete block.partialJson;
521
+ }
522
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
523
+ output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
524
+ stream.push({ type: "error", reason: output.stopReason, error: output });
525
+ stream.end();
526
+ }
527
+ })();
528
+ return stream;
529
+ };
530
+ /**
531
+ * Check if a model supports adaptive thinking (Opus 4.6+, Sonnet 4.6)
532
+ */
533
+ function supportsAdaptiveThinking(modelId) {
534
+ // Adaptive-thinking model IDs (with or without date suffix)
535
+ return (modelId.includes("opus-4-6") ||
536
+ modelId.includes("opus-4.6") ||
537
+ modelId.includes("opus-4-7") ||
538
+ modelId.includes("opus-4.7") ||
539
+ modelId.includes("sonnet-4-6") ||
540
+ modelId.includes("sonnet-4.6"));
541
+ }
542
+ /**
543
+ * Map ThinkingLevel to Anthropic effort levels for adaptive thinking.
544
+ * Note: effort "max" is only valid on Opus 4.6, while Opus 4.7 supports "xhigh".
545
+ */
546
+ function mapThinkingLevelToEffort(model, level) {
547
+ const mapped = level ? model.thinkingLevelMap?.[level] : undefined;
548
+ if (typeof mapped === "string")
549
+ return mapped;
550
+ switch (level) {
551
+ case "minimal":
552
+ case "low":
553
+ return "low";
554
+ case "medium":
555
+ return "medium";
556
+ case "high":
557
+ return "high";
558
+ default:
559
+ return "high";
560
+ }
561
+ }
562
+ export const streamSimpleAnthropic = (model, context, options) => {
563
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider);
564
+ if (!apiKey) {
565
+ throw new Error(`No API key for provider: ${model.provider}`);
566
+ }
567
+ const base = buildBaseOptions(model, options, apiKey);
568
+ if (!options?.reasoning) {
569
+ return streamAnthropic(model, context, { ...base, thinkingEnabled: false });
570
+ }
571
+ // For Opus 4.6 and Sonnet 4.6: use adaptive thinking with effort level
572
+ // For older models: use budget-based thinking
573
+ if (supportsAdaptiveThinking(model.id)) {
574
+ const effort = mapThinkingLevelToEffort(model, options.reasoning);
575
+ return streamAnthropic(model, context, {
576
+ ...base,
577
+ thinkingEnabled: true,
578
+ effort,
579
+ });
580
+ }
581
+ // Undefined means the caller did not request an output cap; let the helper use the model cap.
582
+ // Do not coerce to 0 here, or the thinking budget would become the entire max_tokens value.
583
+ const adjusted = adjustMaxTokensForThinking(base.maxTokens, model.maxTokens, options.reasoning, options.thinkingBudgets);
584
+ return streamAnthropic(model, context, {
585
+ ...base,
586
+ maxTokens: adjusted.maxTokens,
587
+ thinkingEnabled: true,
588
+ thinkingBudgetTokens: adjusted.thinkingBudget,
589
+ });
590
+ };
591
+ function isOAuthToken(apiKey) {
592
+ return apiKey.includes("sk-ant-oat");
593
+ }
594
+ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStreamingBeta, optionsHeaders, dynamicHeaders, sessionId) {
595
+ // Adaptive thinking models (Opus 4.6, Sonnet 4.6) have interleaved thinking built-in.
596
+ // The beta header is deprecated on Opus 4.6 and redundant on Sonnet 4.6, so skip it.
597
+ const needsInterleavedBeta = interleavedThinking && !supportsAdaptiveThinking(model.id);
598
+ const betaFeatures = [];
599
+ if (useFineGrainedToolStreamingBeta) {
600
+ betaFeatures.push(FINE_GRAINED_TOOL_STREAMING_BETA);
601
+ }
602
+ if (needsInterleavedBeta) {
603
+ betaFeatures.push(INTERLEAVED_THINKING_BETA);
604
+ }
605
+ // Copilot: Bearer auth, selective betas.
606
+ if (model.provider === "github-copilot") {
607
+ const client = new Anthropic({
608
+ apiKey: null,
609
+ authToken: apiKey,
610
+ baseURL: model.baseUrl,
611
+ dangerouslyAllowBrowser: true,
612
+ defaultHeaders: mergeHeaders({
613
+ accept: "application/json",
614
+ "anthropic-dangerous-direct-browser-access": "true",
615
+ ...(betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}),
616
+ }, model.headers, dynamicHeaders, optionsHeaders),
617
+ });
618
+ return { client, isOAuthToken: false };
619
+ }
620
+ // OAuth: Bearer auth, Claude Code identity headers
621
+ if (isOAuthToken(apiKey)) {
622
+ const client = new Anthropic({
623
+ apiKey: null,
624
+ authToken: apiKey,
625
+ baseURL: model.baseUrl,
626
+ dangerouslyAllowBrowser: true,
627
+ defaultHeaders: mergeHeaders({
628
+ accept: "application/json",
629
+ "anthropic-dangerous-direct-browser-access": "true",
630
+ "anthropic-beta": ["claude-code-20250219", "oauth-2025-04-20", ...betaFeatures].join(","),
631
+ "user-agent": `claude-cli/${claudeCodeVersion}`,
632
+ "x-app": "cli",
633
+ }, model.headers, optionsHeaders),
634
+ });
635
+ return { client, isOAuthToken: true };
636
+ }
637
+ // API key auth
638
+ const sessionAffinityHeaders = sessionId && getAnthropicCompat(model).sendSessionAffinityHeaders ? { "x-session-affinity": sessionId } : {};
639
+ const client = new Anthropic({
640
+ apiKey,
641
+ authToken: null,
642
+ baseURL: model.baseUrl,
643
+ dangerouslyAllowBrowser: true,
644
+ defaultHeaders: mergeHeaders({
645
+ accept: "application/json",
646
+ "anthropic-dangerous-direct-browser-access": "true",
647
+ ...(betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}),
648
+ }, sessionAffinityHeaders, model.headers, optionsHeaders),
649
+ });
650
+ return { client, isOAuthToken: false };
651
+ }
652
+ function buildParams(model, context, isOAuthToken, options) {
653
+ const { cacheControl } = getCacheControl(model, options?.cacheRetention);
654
+ const params = {
655
+ model: model.id,
656
+ messages: convertMessages(context.messages, model, isOAuthToken, cacheControl),
657
+ max_tokens: options?.maxTokens ?? model.maxTokens,
658
+ stream: true,
659
+ };
660
+ // For OAuth tokens, we MUST include Claude Code identity
661
+ if (isOAuthToken) {
662
+ params.system = [
663
+ {
664
+ type: "text",
665
+ text: "You are Claude Code, Anthropic's official CLI for Claude.",
666
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
667
+ },
668
+ ];
669
+ if (context.systemPrompt) {
670
+ params.system.push({
671
+ type: "text",
672
+ text: sanitizeSurrogates(context.systemPrompt),
673
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
674
+ });
675
+ }
676
+ }
677
+ else if (context.systemPrompt) {
678
+ // Add cache control to system prompt for non-OAuth tokens
679
+ params.system = [
680
+ {
681
+ type: "text",
682
+ text: sanitizeSurrogates(context.systemPrompt),
683
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
684
+ },
685
+ ];
686
+ }
687
+ // Temperature is incompatible with extended thinking (adaptive or budget-based).
688
+ if (options?.temperature !== undefined && !options?.thinkingEnabled) {
689
+ params.temperature = options.temperature;
690
+ }
691
+ if (context.tools && context.tools.length > 0) {
692
+ const compat = getAnthropicCompat(model);
693
+ params.tools = convertTools(context.tools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsCacheControlOnTools ? cacheControl : undefined);
694
+ }
695
+ // Configure thinking mode: adaptive (Opus 4.6+ and Sonnet 4.6),
696
+ // budget-based (older models), or explicitly disabled.
697
+ if (model.reasoning) {
698
+ if (options?.thinkingEnabled) {
699
+ // Default to "summarized" so Opus 4.7 and Mythos Preview behave like
700
+ // older Claude 4 models (whose API default is also "summarized").
701
+ const display = options.thinkingDisplay ?? "summarized";
702
+ if (supportsAdaptiveThinking(model.id)) {
703
+ // Adaptive thinking: Claude decides when and how much to think.
704
+ params.thinking = { type: "adaptive", display };
705
+ if (options.effort) {
706
+ // The Anthropic SDK types can lag newly supported effort values such as "xhigh".
707
+ params.output_config =
708
+ options.effort === "xhigh"
709
+ ? { effort: options.effort }
710
+ : { effort: options.effort };
711
+ }
712
+ }
713
+ else {
714
+ // Budget-based thinking for older models
715
+ params.thinking = {
716
+ type: "enabled",
717
+ budget_tokens: options.thinkingBudgetTokens || 1024,
718
+ display,
719
+ };
720
+ }
721
+ }
722
+ else if (options?.thinkingEnabled === false) {
723
+ params.thinking = { type: "disabled" };
724
+ }
725
+ }
726
+ if (options?.metadata) {
727
+ const userId = options.metadata.user_id;
728
+ if (typeof userId === "string") {
729
+ params.metadata = { user_id: userId };
730
+ }
731
+ }
732
+ if (options?.toolChoice) {
733
+ if (typeof options.toolChoice === "string") {
734
+ params.tool_choice = { type: options.toolChoice };
735
+ }
736
+ else {
737
+ params.tool_choice = options.toolChoice;
738
+ }
739
+ }
740
+ return params;
741
+ }
742
+ // Normalize tool call IDs to match Anthropic's required pattern and length
743
+ function normalizeToolCallId(id) {
744
+ return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
745
+ }
746
+ function convertMessages(messages, model, isOAuthToken, cacheControl) {
747
+ const params = [];
748
+ // Transform messages for cross-provider compatibility
749
+ const transformedMessages = transformMessages(messages, model, normalizeToolCallId);
750
+ for (let i = 0; i < transformedMessages.length; i++) {
751
+ const msg = transformedMessages[i];
752
+ if (msg.role === "user") {
753
+ if (typeof msg.content === "string") {
754
+ if (msg.content.trim().length > 0) {
755
+ params.push({
756
+ role: "user",
757
+ content: sanitizeSurrogates(msg.content),
758
+ });
759
+ }
760
+ }
761
+ else {
762
+ const blocks = msg.content.map((item) => {
763
+ if (item.type === "text") {
764
+ return {
765
+ type: "text",
766
+ text: sanitizeSurrogates(item.text),
767
+ };
768
+ }
769
+ else {
770
+ return {
771
+ type: "image",
772
+ source: {
773
+ type: "base64",
774
+ media_type: item.mimeType,
775
+ data: item.data,
776
+ },
777
+ };
778
+ }
779
+ });
780
+ const filteredBlocks = blocks.filter((b) => {
781
+ if (b.type === "text") {
782
+ return b.text.trim().length > 0;
783
+ }
784
+ return true;
785
+ });
786
+ if (filteredBlocks.length === 0)
787
+ continue;
788
+ params.push({
789
+ role: "user",
790
+ content: filteredBlocks,
791
+ });
792
+ }
793
+ }
794
+ else if (msg.role === "assistant") {
795
+ const blocks = [];
796
+ for (const block of msg.content) {
797
+ if (block.type === "text") {
798
+ if (block.text.trim().length === 0)
799
+ continue;
800
+ blocks.push({
801
+ type: "text",
802
+ text: sanitizeSurrogates(block.text),
803
+ });
804
+ }
805
+ else if (block.type === "thinking") {
806
+ // Redacted thinking: pass the opaque payload back as redacted_thinking
807
+ if (block.redacted) {
808
+ blocks.push({
809
+ type: "redacted_thinking",
810
+ data: block.thinkingSignature,
811
+ });
812
+ continue;
813
+ }
814
+ if (block.thinking.trim().length === 0)
815
+ continue;
816
+ // If thinking signature is missing/empty (e.g., from aborted stream),
817
+ // convert to plain text block without <thinking> tags to avoid API rejection
818
+ // and prevent Claude from mimicking the tags in responses
819
+ if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
820
+ blocks.push({
821
+ type: "text",
822
+ text: sanitizeSurrogates(block.thinking),
823
+ });
824
+ }
825
+ else {
826
+ blocks.push({
827
+ type: "thinking",
828
+ thinking: sanitizeSurrogates(block.thinking),
829
+ signature: block.thinkingSignature,
830
+ });
831
+ }
832
+ }
833
+ else if (block.type === "toolCall") {
834
+ blocks.push({
835
+ type: "tool_use",
836
+ id: block.id,
837
+ name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
838
+ input: block.arguments ?? {},
839
+ });
840
+ }
841
+ }
842
+ if (blocks.length === 0)
843
+ continue;
844
+ params.push({
845
+ role: "assistant",
846
+ content: blocks,
847
+ });
848
+ }
849
+ else if (msg.role === "toolResult") {
850
+ // Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint
851
+ const toolResults = [];
852
+ // Add the current tool result
853
+ toolResults.push({
854
+ type: "tool_result",
855
+ tool_use_id: msg.toolCallId,
856
+ content: convertContentBlocks(msg.content),
857
+ is_error: msg.isError,
858
+ });
859
+ // Look ahead for consecutive toolResult messages
860
+ let j = i + 1;
861
+ while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
862
+ const nextMsg = transformedMessages[j]; // We know it's a toolResult
863
+ toolResults.push({
864
+ type: "tool_result",
865
+ tool_use_id: nextMsg.toolCallId,
866
+ content: convertContentBlocks(nextMsg.content),
867
+ is_error: nextMsg.isError,
868
+ });
869
+ j++;
870
+ }
871
+ // Skip the messages we've already processed
872
+ i = j - 1;
873
+ // Add a single user message with all tool results
874
+ params.push({
875
+ role: "user",
876
+ content: toolResults,
877
+ });
878
+ }
879
+ }
880
+ // Add cache_control to the last user message to cache conversation history
881
+ if (cacheControl && params.length > 0) {
882
+ const lastMessage = params[params.length - 1];
883
+ if (lastMessage.role === "user") {
884
+ if (Array.isArray(lastMessage.content)) {
885
+ const lastBlock = lastMessage.content[lastMessage.content.length - 1];
886
+ if (lastBlock &&
887
+ (lastBlock.type === "text" || lastBlock.type === "image" || lastBlock.type === "tool_result")) {
888
+ lastBlock.cache_control = cacheControl;
889
+ }
890
+ }
891
+ else if (typeof lastMessage.content === "string") {
892
+ lastMessage.content = [
893
+ {
894
+ type: "text",
895
+ text: lastMessage.content,
896
+ cache_control: cacheControl,
897
+ },
898
+ ];
899
+ }
900
+ }
901
+ }
902
+ return params;
903
+ }
904
+ function shouldUseFineGrainedToolStreamingBeta(model, context) {
905
+ return !!context.tools?.length && !getAnthropicCompat(model).supportsEagerToolInputStreaming;
906
+ }
907
+ function convertTools(tools, isOAuthToken, supportsEagerToolInputStreaming, cacheControl) {
908
+ if (!tools)
909
+ return [];
910
+ return tools.map((tool, index) => {
911
+ const schema = tool.parameters;
912
+ return {
913
+ name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name,
914
+ description: tool.description,
915
+ ...(supportsEagerToolInputStreaming ? { eager_input_streaming: true } : {}),
916
+ input_schema: {
917
+ type: "object",
918
+ properties: schema.properties ?? {},
919
+ required: schema.required ?? [],
920
+ },
921
+ ...(cacheControl && index === tools.length - 1 ? { cache_control: cacheControl } : {}),
922
+ };
923
+ });
924
+ }
925
+ function mapStopReason(reason) {
926
+ switch (reason) {
927
+ case "end_turn":
928
+ return "stop";
929
+ case "max_tokens":
930
+ return "length";
931
+ case "tool_use":
932
+ return "toolUse";
933
+ case "refusal":
934
+ return "error";
935
+ case "pause_turn": // Stop is good enough -> resubmit
936
+ return "stop";
937
+ case "stop_sequence":
938
+ return "stop"; // We don't supply stop sequences, so this should never happen
939
+ case "sensitive": // Content flagged by safety filters (not yet in SDK types)
940
+ return "error";
941
+ default:
942
+ // Handle unknown stop reasons gracefully (API may add new values)
943
+ throw new Error(`Unhandled stop reason: ${reason}`);
944
+ }
945
+ }