@lenylvt/pi-ai 0.64.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 (167) hide show
  1. package/README.md +203 -0
  2. package/dist/api-registry.d.ts +20 -0
  3. package/dist/api-registry.d.ts.map +1 -0
  4. package/dist/api-registry.js +44 -0
  5. package/dist/api-registry.js.map +1 -0
  6. package/dist/cli.d.ts +3 -0
  7. package/dist/cli.d.ts.map +1 -0
  8. package/dist/cli.js +119 -0
  9. package/dist/cli.js.map +1 -0
  10. package/dist/env-api-keys.d.ts +7 -0
  11. package/dist/env-api-keys.d.ts.map +1 -0
  12. package/dist/env-api-keys.js +13 -0
  13. package/dist/env-api-keys.js.map +1 -0
  14. package/dist/index.d.ts +20 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +14 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/models.d.ts +24 -0
  19. package/dist/models.d.ts.map +1 -0
  20. package/dist/models.generated.d.ts +2332 -0
  21. package/dist/models.generated.d.ts.map +1 -0
  22. package/dist/models.generated.js +2186 -0
  23. package/dist/models.generated.js.map +1 -0
  24. package/dist/models.js +60 -0
  25. package/dist/models.js.map +1 -0
  26. package/dist/oauth.d.ts +2 -0
  27. package/dist/oauth.d.ts.map +1 -0
  28. package/dist/oauth.js +2 -0
  29. package/dist/oauth.js.map +1 -0
  30. package/dist/providers/anthropic.d.ts +40 -0
  31. package/dist/providers/anthropic.d.ts.map +1 -0
  32. package/dist/providers/anthropic.js +749 -0
  33. package/dist/providers/anthropic.js.map +1 -0
  34. package/dist/providers/faux.d.ts +56 -0
  35. package/dist/providers/faux.d.ts.map +1 -0
  36. package/dist/providers/faux.js +367 -0
  37. package/dist/providers/faux.js.map +1 -0
  38. package/dist/providers/github-copilot-headers.d.ts +8 -0
  39. package/dist/providers/github-copilot-headers.d.ts.map +1 -0
  40. package/dist/providers/github-copilot-headers.js +29 -0
  41. package/dist/providers/github-copilot-headers.js.map +1 -0
  42. package/dist/providers/openai-codex-responses.d.ts +9 -0
  43. package/dist/providers/openai-codex-responses.d.ts.map +1 -0
  44. package/dist/providers/openai-codex-responses.js +741 -0
  45. package/dist/providers/openai-codex-responses.js.map +1 -0
  46. package/dist/providers/openai-completions.d.ts +15 -0
  47. package/dist/providers/openai-completions.d.ts.map +1 -0
  48. package/dist/providers/openai-completions.js +687 -0
  49. package/dist/providers/openai-completions.js.map +1 -0
  50. package/dist/providers/openai-responses-shared.d.ts +17 -0
  51. package/dist/providers/openai-responses-shared.d.ts.map +1 -0
  52. package/dist/providers/openai-responses-shared.js +458 -0
  53. package/dist/providers/openai-responses-shared.js.map +1 -0
  54. package/dist/providers/openai-responses.d.ts +13 -0
  55. package/dist/providers/openai-responses.d.ts.map +1 -0
  56. package/dist/providers/openai-responses.js +190 -0
  57. package/dist/providers/openai-responses.js.map +1 -0
  58. package/dist/providers/register-builtins.d.ts +16 -0
  59. package/dist/providers/register-builtins.d.ts.map +1 -0
  60. package/dist/providers/register-builtins.js +140 -0
  61. package/dist/providers/register-builtins.js.map +1 -0
  62. package/dist/providers/simple-options.d.ts +8 -0
  63. package/dist/providers/simple-options.d.ts.map +1 -0
  64. package/dist/providers/simple-options.js +35 -0
  65. package/dist/providers/simple-options.js.map +1 -0
  66. package/dist/providers/transform-messages.d.ts +8 -0
  67. package/dist/providers/transform-messages.d.ts.map +1 -0
  68. package/dist/providers/transform-messages.js +155 -0
  69. package/dist/providers/transform-messages.js.map +1 -0
  70. package/dist/stream.d.ts +8 -0
  71. package/dist/stream.d.ts.map +1 -0
  72. package/dist/stream.js +27 -0
  73. package/dist/stream.js.map +1 -0
  74. package/dist/types.d.ts +283 -0
  75. package/dist/types.d.ts.map +1 -0
  76. package/dist/types.js +2 -0
  77. package/dist/types.js.map +1 -0
  78. package/dist/utils/event-stream.d.ts +21 -0
  79. package/dist/utils/event-stream.d.ts.map +1 -0
  80. package/dist/utils/event-stream.js +81 -0
  81. package/dist/utils/event-stream.js.map +1 -0
  82. package/dist/utils/hash.d.ts +3 -0
  83. package/dist/utils/hash.d.ts.map +1 -0
  84. package/dist/utils/hash.js +14 -0
  85. package/dist/utils/hash.js.map +1 -0
  86. package/dist/utils/json-parse.d.ts +9 -0
  87. package/dist/utils/json-parse.d.ts.map +1 -0
  88. package/dist/utils/json-parse.js +29 -0
  89. package/dist/utils/json-parse.js.map +1 -0
  90. package/dist/utils/oauth/anthropic.d.ts +25 -0
  91. package/dist/utils/oauth/anthropic.d.ts.map +1 -0
  92. package/dist/utils/oauth/anthropic.js +335 -0
  93. package/dist/utils/oauth/anthropic.js.map +1 -0
  94. package/dist/utils/oauth/github-copilot.d.ts +30 -0
  95. package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
  96. package/dist/utils/oauth/github-copilot.js +292 -0
  97. package/dist/utils/oauth/github-copilot.js.map +1 -0
  98. package/dist/utils/oauth/index.d.ts +36 -0
  99. package/dist/utils/oauth/index.d.ts.map +1 -0
  100. package/dist/utils/oauth/index.js +92 -0
  101. package/dist/utils/oauth/index.js.map +1 -0
  102. package/dist/utils/oauth/oauth-page.d.ts +3 -0
  103. package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
  104. package/dist/utils/oauth/oauth-page.js +105 -0
  105. package/dist/utils/oauth/oauth-page.js.map +1 -0
  106. package/dist/utils/oauth/openai-codex.d.ts +34 -0
  107. package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
  108. package/dist/utils/oauth/openai-codex.js +373 -0
  109. package/dist/utils/oauth/openai-codex.js.map +1 -0
  110. package/dist/utils/oauth/pkce.d.ts +13 -0
  111. package/dist/utils/oauth/pkce.d.ts.map +1 -0
  112. package/dist/utils/oauth/pkce.js +31 -0
  113. package/dist/utils/oauth/pkce.js.map +1 -0
  114. package/dist/utils/oauth/types.d.ts +47 -0
  115. package/dist/utils/oauth/types.d.ts.map +1 -0
  116. package/dist/utils/oauth/types.js +2 -0
  117. package/dist/utils/oauth/types.js.map +1 -0
  118. package/dist/utils/overflow.d.ts +53 -0
  119. package/dist/utils/overflow.d.ts.map +1 -0
  120. package/dist/utils/overflow.js +119 -0
  121. package/dist/utils/overflow.js.map +1 -0
  122. package/dist/utils/sanitize-unicode.d.ts +22 -0
  123. package/dist/utils/sanitize-unicode.d.ts.map +1 -0
  124. package/dist/utils/sanitize-unicode.js +26 -0
  125. package/dist/utils/sanitize-unicode.js.map +1 -0
  126. package/dist/utils/typebox-helpers.d.ts +17 -0
  127. package/dist/utils/typebox-helpers.d.ts.map +1 -0
  128. package/dist/utils/typebox-helpers.js +21 -0
  129. package/dist/utils/typebox-helpers.js.map +1 -0
  130. package/dist/utils/validation.d.ts +18 -0
  131. package/dist/utils/validation.d.ts.map +1 -0
  132. package/dist/utils/validation.js +80 -0
  133. package/dist/utils/validation.js.map +1 -0
  134. package/package.json +89 -0
  135. package/src/api-registry.ts +98 -0
  136. package/src/cli.ts +136 -0
  137. package/src/env-api-keys.ts +22 -0
  138. package/src/index.ts +29 -0
  139. package/src/models.generated.ts +2188 -0
  140. package/src/models.ts +82 -0
  141. package/src/oauth.ts +1 -0
  142. package/src/providers/anthropic.ts +905 -0
  143. package/src/providers/faux.ts +498 -0
  144. package/src/providers/github-copilot-headers.ts +37 -0
  145. package/src/providers/openai-codex-responses.ts +929 -0
  146. package/src/providers/openai-completions.ts +811 -0
  147. package/src/providers/openai-responses-shared.ts +513 -0
  148. package/src/providers/openai-responses.ts +251 -0
  149. package/src/providers/register-builtins.ts +232 -0
  150. package/src/providers/simple-options.ts +46 -0
  151. package/src/providers/transform-messages.ts +172 -0
  152. package/src/stream.ts +59 -0
  153. package/src/types.ts +294 -0
  154. package/src/utils/event-stream.ts +87 -0
  155. package/src/utils/hash.ts +13 -0
  156. package/src/utils/json-parse.ts +28 -0
  157. package/src/utils/oauth/anthropic.ts +402 -0
  158. package/src/utils/oauth/github-copilot.ts +396 -0
  159. package/src/utils/oauth/index.ts +123 -0
  160. package/src/utils/oauth/oauth-page.ts +109 -0
  161. package/src/utils/oauth/openai-codex.ts +450 -0
  162. package/src/utils/oauth/pkce.ts +34 -0
  163. package/src/utils/oauth/types.ts +59 -0
  164. package/src/utils/overflow.ts +125 -0
  165. package/src/utils/sanitize-unicode.ts +25 -0
  166. package/src/utils/typebox-helpers.ts +24 -0
  167. package/src/utils/validation.ts +93 -0
@@ -0,0 +1,905 @@
1
+ import Anthropic from "@anthropic-ai/sdk";
2
+ import type {
3
+ ContentBlockParam,
4
+ MessageCreateParamsStreaming,
5
+ MessageParam,
6
+ } from "@anthropic-ai/sdk/resources/messages.js";
7
+ import { getEnvApiKey } from "../env-api-keys.js";
8
+ import { calculateCost } from "../models.js";
9
+ import type {
10
+ Api,
11
+ AssistantMessage,
12
+ CacheRetention,
13
+ Context,
14
+ ImageContent,
15
+ Message,
16
+ Model,
17
+ SimpleStreamOptions,
18
+ StopReason,
19
+ StreamFunction,
20
+ StreamOptions,
21
+ TextContent,
22
+ ThinkingContent,
23
+ Tool,
24
+ ToolCall,
25
+ ToolResultMessage,
26
+ } from "../types.js";
27
+ import { AssistantMessageEventStream } from "../utils/event-stream.js";
28
+ import { parseStreamingJson } from "../utils/json-parse.js";
29
+ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
30
+
31
+ import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
32
+ import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.js";
33
+ import { transformMessages } from "./transform-messages.js";
34
+
35
+ /**
36
+ * Resolve cache retention preference.
37
+ * Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
38
+ */
39
+ function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention {
40
+ if (cacheRetention) {
41
+ return cacheRetention;
42
+ }
43
+ if (typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "long") {
44
+ return "long";
45
+ }
46
+ return "short";
47
+ }
48
+
49
+ function getCacheControl(
50
+ baseUrl: string,
51
+ cacheRetention?: CacheRetention,
52
+ ): { retention: CacheRetention; cacheControl?: { type: "ephemeral"; ttl?: "1h" } } {
53
+ const retention = resolveCacheRetention(cacheRetention);
54
+ if (retention === "none") {
55
+ return { retention };
56
+ }
57
+ const ttl = retention === "long" && baseUrl.includes("api.anthropic.com") ? "1h" : undefined;
58
+ return {
59
+ retention,
60
+ cacheControl: { type: "ephemeral", ...(ttl && { ttl }) },
61
+ };
62
+ }
63
+
64
+ // Stealth mode: Mimic Claude Code's tool naming exactly
65
+ const claudeCodeVersion = "2.1.75";
66
+
67
+ // Claude Code 2.x tool names (canonical casing)
68
+ // Source: https://cchistory.mariozechner.at/data/prompts-2.1.11.md
69
+ // To update: https://github.com/badlogic/cchistory
70
+ const claudeCodeTools = [
71
+ "Read",
72
+ "Write",
73
+ "Edit",
74
+ "Bash",
75
+ "Grep",
76
+ "Glob",
77
+ "AskUserQuestion",
78
+ "EnterPlanMode",
79
+ "ExitPlanMode",
80
+ "KillShell",
81
+ "NotebookEdit",
82
+ "Skill",
83
+ "Task",
84
+ "TaskOutput",
85
+ "TodoWrite",
86
+ "WebFetch",
87
+ "WebSearch",
88
+ ];
89
+
90
+ const ccToolLookup = new Map(claudeCodeTools.map((t) => [t.toLowerCase(), t]));
91
+
92
+ // Convert tool name to CC canonical casing if it matches (case-insensitive)
93
+ const toClaudeCodeName = (name: string) => ccToolLookup.get(name.toLowerCase()) ?? name;
94
+ const fromClaudeCodeName = (name: string, tools?: Tool[]) => {
95
+ if (tools && tools.length > 0) {
96
+ const lowerName = name.toLowerCase();
97
+ const matchedTool = tools.find((tool) => tool.name.toLowerCase() === lowerName);
98
+ if (matchedTool) return matchedTool.name;
99
+ }
100
+ return name;
101
+ };
102
+
103
+ /**
104
+ * Convert content blocks to Anthropic API format
105
+ */
106
+ function convertContentBlocks(content: (TextContent | ImageContent)[]):
107
+ | string
108
+ | Array<
109
+ | { type: "text"; text: string }
110
+ | {
111
+ type: "image";
112
+ source: {
113
+ type: "base64";
114
+ media_type: "image/jpeg" | "image/png" | "image/gif" | "image/webp";
115
+ data: string;
116
+ };
117
+ }
118
+ > {
119
+ // If only text blocks, return as concatenated string for simplicity
120
+ const hasImages = content.some((c) => c.type === "image");
121
+ if (!hasImages) {
122
+ return sanitizeSurrogates(content.map((c) => (c as TextContent).text).join("\n"));
123
+ }
124
+
125
+ // If we have images, convert to content block array
126
+ const blocks = content.map((block) => {
127
+ if (block.type === "text") {
128
+ return {
129
+ type: "text" as const,
130
+ text: sanitizeSurrogates(block.text),
131
+ };
132
+ }
133
+ return {
134
+ type: "image" as const,
135
+ source: {
136
+ type: "base64" as const,
137
+ media_type: block.mimeType as "image/jpeg" | "image/png" | "image/gif" | "image/webp",
138
+ data: block.data,
139
+ },
140
+ };
141
+ });
142
+
143
+ // If only images (no text), add placeholder text block
144
+ const hasText = blocks.some((b) => b.type === "text");
145
+ if (!hasText) {
146
+ blocks.unshift({
147
+ type: "text" as const,
148
+ text: "(see attached image)",
149
+ });
150
+ }
151
+
152
+ return blocks;
153
+ }
154
+
155
+ export type AnthropicEffort = "low" | "medium" | "high" | "max";
156
+
157
+ export interface AnthropicOptions extends StreamOptions {
158
+ /**
159
+ * Enable extended thinking.
160
+ * For Opus 4.6 and Sonnet 4.6: uses adaptive thinking (model decides when/how much to think).
161
+ * For older models: uses budget-based thinking with thinkingBudgetTokens.
162
+ */
163
+ thinkingEnabled?: boolean;
164
+ /**
165
+ * Token budget for extended thinking (older models only).
166
+ * Ignored for Opus 4.6 and Sonnet 4.6, which use adaptive thinking.
167
+ */
168
+ thinkingBudgetTokens?: number;
169
+ /**
170
+ * Effort level for adaptive thinking (Opus 4.6 and Sonnet 4.6).
171
+ * Controls how much thinking Claude allocates:
172
+ * - "max": Always thinks with no constraints (Opus 4.6 only)
173
+ * - "high": Always thinks, deep reasoning (default)
174
+ * - "medium": Moderate thinking, may skip for simple queries
175
+ * - "low": Minimal thinking, skips for simple tasks
176
+ * Ignored for older models.
177
+ */
178
+ effort?: AnthropicEffort;
179
+ interleavedThinking?: boolean;
180
+ toolChoice?: "auto" | "any" | "none" | { type: "tool"; name: string };
181
+ /**
182
+ * Pre-built Anthropic client instance. When provided, skips internal client
183
+ * construction entirely. Use this to inject alternative SDK clients such as
184
+ * `AnthropicVertex` that shares the same messaging API.
185
+ */
186
+ client?: Anthropic;
187
+ }
188
+
189
+ function mergeHeaders(...headerSources: (Record<string, string> | undefined)[]): Record<string, string> {
190
+ const merged: Record<string, string> = {};
191
+ for (const headers of headerSources) {
192
+ if (headers) {
193
+ Object.assign(merged, headers);
194
+ }
195
+ }
196
+ return merged;
197
+ }
198
+
199
+ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOptions> = (
200
+ model: Model<"anthropic-messages">,
201
+ context: Context,
202
+ options?: AnthropicOptions,
203
+ ): AssistantMessageEventStream => {
204
+ const stream = new AssistantMessageEventStream();
205
+
206
+ (async () => {
207
+ const output: AssistantMessage = {
208
+ role: "assistant",
209
+ content: [],
210
+ api: model.api as Api,
211
+ provider: model.provider,
212
+ model: model.id,
213
+ usage: {
214
+ input: 0,
215
+ output: 0,
216
+ cacheRead: 0,
217
+ cacheWrite: 0,
218
+ totalTokens: 0,
219
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
220
+ },
221
+ stopReason: "stop",
222
+ timestamp: Date.now(),
223
+ };
224
+
225
+ try {
226
+ let client: Anthropic;
227
+ let isOAuth: boolean;
228
+
229
+ if (options?.client) {
230
+ client = options.client;
231
+ isOAuth = false;
232
+ } else {
233
+ const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
234
+
235
+ let copilotDynamicHeaders: Record<string, string> | undefined;
236
+ if (model.provider === "github-copilot") {
237
+ const hasImages = hasCopilotVisionInput(context.messages);
238
+ copilotDynamicHeaders = buildCopilotDynamicHeaders({
239
+ messages: context.messages,
240
+ hasImages,
241
+ });
242
+ }
243
+
244
+ const created = createClient(
245
+ model,
246
+ apiKey,
247
+ options?.interleavedThinking ?? true,
248
+ options?.headers,
249
+ copilotDynamicHeaders,
250
+ );
251
+ client = created.client;
252
+ isOAuth = created.isOAuthToken;
253
+ }
254
+ let params = buildParams(model, context, isOAuth, options);
255
+ const nextParams = await options?.onPayload?.(params, model);
256
+ if (nextParams !== undefined) {
257
+ params = nextParams as MessageCreateParamsStreaming;
258
+ }
259
+ const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });
260
+ stream.push({ type: "start", partial: output });
261
+
262
+ type Block = (ThinkingContent | TextContent | (ToolCall & { partialJson: string })) & { index: number };
263
+ const blocks = output.content as Block[];
264
+
265
+ for await (const event of anthropicStream) {
266
+ if (event.type === "message_start") {
267
+ output.responseId = event.message.id;
268
+ // Capture initial token usage from message_start event
269
+ // This ensures we have input token counts even if the stream is aborted early
270
+ output.usage.input = event.message.usage.input_tokens || 0;
271
+ output.usage.output = event.message.usage.output_tokens || 0;
272
+ output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
273
+ output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
274
+ // Anthropic doesn't provide total_tokens, compute from components
275
+ output.usage.totalTokens =
276
+ output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
277
+ calculateCost(model, output.usage);
278
+ } else if (event.type === "content_block_start") {
279
+ if (event.content_block.type === "text") {
280
+ const block: Block = {
281
+ type: "text",
282
+ text: "",
283
+ index: event.index,
284
+ };
285
+ output.content.push(block);
286
+ stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
287
+ } else if (event.content_block.type === "thinking") {
288
+ const block: Block = {
289
+ type: "thinking",
290
+ thinking: "",
291
+ thinkingSignature: "",
292
+ index: event.index,
293
+ };
294
+ output.content.push(block);
295
+ stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
296
+ } else if (event.content_block.type === "redacted_thinking") {
297
+ const block: Block = {
298
+ type: "thinking",
299
+ thinking: "[Reasoning redacted]",
300
+ thinkingSignature: event.content_block.data,
301
+ redacted: true,
302
+ index: event.index,
303
+ };
304
+ output.content.push(block);
305
+ stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
306
+ } else if (event.content_block.type === "tool_use") {
307
+ const block: Block = {
308
+ type: "toolCall",
309
+ id: event.content_block.id,
310
+ name: isOAuth
311
+ ? fromClaudeCodeName(event.content_block.name, context.tools)
312
+ : event.content_block.name,
313
+ arguments: (event.content_block.input as Record<string, any>) ?? {},
314
+ partialJson: "",
315
+ index: event.index,
316
+ };
317
+ output.content.push(block);
318
+ stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
319
+ }
320
+ } else if (event.type === "content_block_delta") {
321
+ if (event.delta.type === "text_delta") {
322
+ const index = blocks.findIndex((b) => b.index === event.index);
323
+ const block = blocks[index];
324
+ if (block && block.type === "text") {
325
+ block.text += event.delta.text;
326
+ stream.push({
327
+ type: "text_delta",
328
+ contentIndex: index,
329
+ delta: event.delta.text,
330
+ partial: output,
331
+ });
332
+ }
333
+ } else if (event.delta.type === "thinking_delta") {
334
+ const index = blocks.findIndex((b) => b.index === event.index);
335
+ const block = blocks[index];
336
+ if (block && block.type === "thinking") {
337
+ block.thinking += event.delta.thinking;
338
+ stream.push({
339
+ type: "thinking_delta",
340
+ contentIndex: index,
341
+ delta: event.delta.thinking,
342
+ partial: output,
343
+ });
344
+ }
345
+ } else if (event.delta.type === "input_json_delta") {
346
+ const index = blocks.findIndex((b) => b.index === event.index);
347
+ const block = blocks[index];
348
+ if (block && block.type === "toolCall") {
349
+ block.partialJson += event.delta.partial_json;
350
+ block.arguments = parseStreamingJson(block.partialJson);
351
+ stream.push({
352
+ type: "toolcall_delta",
353
+ contentIndex: index,
354
+ delta: event.delta.partial_json,
355
+ partial: output,
356
+ });
357
+ }
358
+ } else if (event.delta.type === "signature_delta") {
359
+ const index = blocks.findIndex((b) => b.index === event.index);
360
+ const block = blocks[index];
361
+ if (block && block.type === "thinking") {
362
+ block.thinkingSignature = block.thinkingSignature || "";
363
+ block.thinkingSignature += event.delta.signature;
364
+ }
365
+ }
366
+ } else if (event.type === "content_block_stop") {
367
+ const index = blocks.findIndex((b) => b.index === event.index);
368
+ const block = blocks[index];
369
+ if (block) {
370
+ delete (block as any).index;
371
+ if (block.type === "text") {
372
+ stream.push({
373
+ type: "text_end",
374
+ contentIndex: index,
375
+ content: block.text,
376
+ partial: output,
377
+ });
378
+ } else if (block.type === "thinking") {
379
+ stream.push({
380
+ type: "thinking_end",
381
+ contentIndex: index,
382
+ content: block.thinking,
383
+ partial: output,
384
+ });
385
+ } else if (block.type === "toolCall") {
386
+ block.arguments = parseStreamingJson(block.partialJson);
387
+ delete (block as any).partialJson;
388
+ stream.push({
389
+ type: "toolcall_end",
390
+ contentIndex: index,
391
+ toolCall: block,
392
+ partial: output,
393
+ });
394
+ }
395
+ }
396
+ } else if (event.type === "message_delta") {
397
+ if (event.delta.stop_reason) {
398
+ output.stopReason = mapStopReason(event.delta.stop_reason);
399
+ }
400
+ // Only update usage fields if present (not null).
401
+ // Preserves input_tokens from message_start when proxies omit it in message_delta.
402
+ if (event.usage.input_tokens != null) {
403
+ output.usage.input = event.usage.input_tokens;
404
+ }
405
+ if (event.usage.output_tokens != null) {
406
+ output.usage.output = event.usage.output_tokens;
407
+ }
408
+ if (event.usage.cache_read_input_tokens != null) {
409
+ output.usage.cacheRead = event.usage.cache_read_input_tokens;
410
+ }
411
+ if (event.usage.cache_creation_input_tokens != null) {
412
+ output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
413
+ }
414
+ // Anthropic doesn't provide total_tokens, compute from components
415
+ output.usage.totalTokens =
416
+ output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
417
+ calculateCost(model, output.usage);
418
+ }
419
+ }
420
+
421
+ if (options?.signal?.aborted) {
422
+ throw new Error("Request was aborted");
423
+ }
424
+
425
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
426
+ throw new Error("An unknown error occurred");
427
+ }
428
+
429
+ stream.push({ type: "done", reason: output.stopReason, message: output });
430
+ stream.end();
431
+ } catch (error) {
432
+ for (const block of output.content) delete (block as any).index;
433
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
434
+ output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
435
+ stream.push({ type: "error", reason: output.stopReason, error: output });
436
+ stream.end();
437
+ }
438
+ })();
439
+
440
+ return stream;
441
+ };
442
+
443
+ /**
444
+ * Check if a model supports adaptive thinking (Opus 4.6 and Sonnet 4.6)
445
+ */
446
+ function supportsAdaptiveThinking(modelId: string): boolean {
447
+ // Opus 4.6 and Sonnet 4.6 model IDs (with or without date suffix)
448
+ return (
449
+ modelId.includes("opus-4-6") ||
450
+ modelId.includes("opus-4.6") ||
451
+ modelId.includes("sonnet-4-6") ||
452
+ modelId.includes("sonnet-4.6")
453
+ );
454
+ }
455
+
456
+ /**
457
+ * Map ThinkingLevel to Anthropic effort levels for adaptive thinking.
458
+ * Note: effort "max" is only valid on Opus 4.6.
459
+ */
460
+ function mapThinkingLevelToEffort(level: SimpleStreamOptions["reasoning"], modelId: string): AnthropicEffort {
461
+ switch (level) {
462
+ case "minimal":
463
+ return "low";
464
+ case "low":
465
+ return "low";
466
+ case "medium":
467
+ return "medium";
468
+ case "high":
469
+ return "high";
470
+ case "xhigh":
471
+ return modelId.includes("opus-4-6") || modelId.includes("opus-4.6") ? "max" : "high";
472
+ default:
473
+ return "high";
474
+ }
475
+ }
476
+
477
+ export const streamSimpleAnthropic: StreamFunction<"anthropic-messages", SimpleStreamOptions> = (
478
+ model: Model<"anthropic-messages">,
479
+ context: Context,
480
+ options?: SimpleStreamOptions,
481
+ ): AssistantMessageEventStream => {
482
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider);
483
+ if (!apiKey) {
484
+ throw new Error(`No API key for provider: ${model.provider}`);
485
+ }
486
+
487
+ const base = buildBaseOptions(model, options, apiKey);
488
+ if (!options?.reasoning) {
489
+ return streamAnthropic(model, context, { ...base, thinkingEnabled: false } satisfies AnthropicOptions);
490
+ }
491
+
492
+ // For Opus 4.6 and Sonnet 4.6: use adaptive thinking with effort level
493
+ // For older models: use budget-based thinking
494
+ if (supportsAdaptiveThinking(model.id)) {
495
+ const effort = mapThinkingLevelToEffort(options.reasoning, model.id);
496
+ return streamAnthropic(model, context, {
497
+ ...base,
498
+ thinkingEnabled: true,
499
+ effort,
500
+ } satisfies AnthropicOptions);
501
+ }
502
+
503
+ const adjusted = adjustMaxTokensForThinking(
504
+ base.maxTokens || 0,
505
+ model.maxTokens,
506
+ options.reasoning,
507
+ options.thinkingBudgets,
508
+ );
509
+
510
+ return streamAnthropic(model, context, {
511
+ ...base,
512
+ maxTokens: adjusted.maxTokens,
513
+ thinkingEnabled: true,
514
+ thinkingBudgetTokens: adjusted.thinkingBudget,
515
+ } satisfies AnthropicOptions);
516
+ };
517
+
518
+ function isOAuthToken(apiKey: string): boolean {
519
+ return apiKey.includes("sk-ant-oat");
520
+ }
521
+
522
+ function createClient(
523
+ model: Model<"anthropic-messages">,
524
+ apiKey: string,
525
+ interleavedThinking: boolean,
526
+ optionsHeaders?: Record<string, string>,
527
+ dynamicHeaders?: Record<string, string>,
528
+ ): { client: Anthropic; isOAuthToken: boolean } {
529
+ // Adaptive thinking models (Opus 4.6, Sonnet 4.6) have interleaved thinking built-in.
530
+ // The beta header is deprecated on Opus 4.6 and redundant on Sonnet 4.6, so skip it.
531
+ const needsInterleavedBeta = interleavedThinking && !supportsAdaptiveThinking(model.id);
532
+
533
+ // Copilot: Bearer auth, selective betas (no fine-grained-tool-streaming)
534
+ if (model.provider === "github-copilot") {
535
+ const betaFeatures: string[] = [];
536
+ if (needsInterleavedBeta) {
537
+ betaFeatures.push("interleaved-thinking-2025-05-14");
538
+ }
539
+
540
+ const client = new Anthropic({
541
+ apiKey: null,
542
+ authToken: apiKey,
543
+ baseURL: model.baseUrl,
544
+ dangerouslyAllowBrowser: true,
545
+ defaultHeaders: mergeHeaders(
546
+ {
547
+ accept: "application/json",
548
+ "anthropic-dangerous-direct-browser-access": "true",
549
+ ...(betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}),
550
+ },
551
+ model.headers,
552
+ dynamicHeaders,
553
+ optionsHeaders,
554
+ ),
555
+ });
556
+
557
+ return { client, isOAuthToken: false };
558
+ }
559
+
560
+ const betaFeatures = ["fine-grained-tool-streaming-2025-05-14"];
561
+ if (needsInterleavedBeta) {
562
+ betaFeatures.push("interleaved-thinking-2025-05-14");
563
+ }
564
+
565
+ // OAuth: Bearer auth, Claude Code identity headers
566
+ if (isOAuthToken(apiKey)) {
567
+ const client = new Anthropic({
568
+ apiKey: null,
569
+ authToken: apiKey,
570
+ baseURL: model.baseUrl,
571
+ dangerouslyAllowBrowser: true,
572
+ defaultHeaders: mergeHeaders(
573
+ {
574
+ accept: "application/json",
575
+ "anthropic-dangerous-direct-browser-access": "true",
576
+ "anthropic-beta": `claude-code-20250219,oauth-2025-04-20,${betaFeatures.join(",")}`,
577
+ "user-agent": `claude-cli/${claudeCodeVersion}`,
578
+ "x-app": "cli",
579
+ },
580
+ model.headers,
581
+ optionsHeaders,
582
+ ),
583
+ });
584
+
585
+ return { client, isOAuthToken: true };
586
+ }
587
+
588
+ // API key auth
589
+ const client = new Anthropic({
590
+ apiKey,
591
+ baseURL: model.baseUrl,
592
+ dangerouslyAllowBrowser: true,
593
+ defaultHeaders: mergeHeaders(
594
+ {
595
+ accept: "application/json",
596
+ "anthropic-dangerous-direct-browser-access": "true",
597
+ "anthropic-beta": betaFeatures.join(","),
598
+ },
599
+ model.headers,
600
+ optionsHeaders,
601
+ ),
602
+ });
603
+
604
+ return { client, isOAuthToken: false };
605
+ }
606
+
607
+ function buildParams(
608
+ model: Model<"anthropic-messages">,
609
+ context: Context,
610
+ isOAuthToken: boolean,
611
+ options?: AnthropicOptions,
612
+ ): MessageCreateParamsStreaming {
613
+ const { cacheControl } = getCacheControl(model.baseUrl, options?.cacheRetention);
614
+ const params: MessageCreateParamsStreaming = {
615
+ model: model.id,
616
+ messages: convertMessages(context.messages, model, isOAuthToken, cacheControl),
617
+ max_tokens: options?.maxTokens || (model.maxTokens / 3) | 0,
618
+ stream: true,
619
+ };
620
+
621
+ // For OAuth tokens, we MUST include Claude Code identity
622
+ if (isOAuthToken) {
623
+ params.system = [
624
+ {
625
+ type: "text",
626
+ text: "You are Claude Code, Anthropic's official CLI for Claude.",
627
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
628
+ },
629
+ ];
630
+ if (context.systemPrompt) {
631
+ params.system.push({
632
+ type: "text",
633
+ text: sanitizeSurrogates(context.systemPrompt),
634
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
635
+ });
636
+ }
637
+ } else if (context.systemPrompt) {
638
+ // Add cache control to system prompt for non-OAuth tokens
639
+ params.system = [
640
+ {
641
+ type: "text",
642
+ text: sanitizeSurrogates(context.systemPrompt),
643
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
644
+ },
645
+ ];
646
+ }
647
+
648
+ // Temperature is incompatible with extended thinking (adaptive or budget-based).
649
+ if (options?.temperature !== undefined && !options?.thinkingEnabled) {
650
+ params.temperature = options.temperature;
651
+ }
652
+
653
+ if (context.tools) {
654
+ params.tools = convertTools(context.tools, isOAuthToken);
655
+ }
656
+
657
+ // Configure thinking mode: adaptive (Opus 4.6 and Sonnet 4.6),
658
+ // budget-based (older models), or explicitly disabled.
659
+ if (model.reasoning) {
660
+ if (options?.thinkingEnabled) {
661
+ if (supportsAdaptiveThinking(model.id)) {
662
+ // Adaptive thinking: Claude decides when and how much to think
663
+ params.thinking = { type: "adaptive" };
664
+ if (options.effort) {
665
+ params.output_config = { effort: options.effort };
666
+ }
667
+ } else {
668
+ // Budget-based thinking for older models
669
+ params.thinking = {
670
+ type: "enabled",
671
+ budget_tokens: options.thinkingBudgetTokens || 1024,
672
+ };
673
+ }
674
+ } else if (options?.thinkingEnabled === false) {
675
+ params.thinking = { type: "disabled" };
676
+ }
677
+ }
678
+
679
+ if (options?.metadata) {
680
+ const userId = options.metadata.user_id;
681
+ if (typeof userId === "string") {
682
+ params.metadata = { user_id: userId };
683
+ }
684
+ }
685
+
686
+ if (options?.toolChoice) {
687
+ if (typeof options.toolChoice === "string") {
688
+ params.tool_choice = { type: options.toolChoice };
689
+ } else {
690
+ params.tool_choice = options.toolChoice;
691
+ }
692
+ }
693
+
694
+ return params;
695
+ }
696
+
697
+ // Normalize tool call IDs to match Anthropic's required pattern and length
698
+ function normalizeToolCallId(id: string): string {
699
+ return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
700
+ }
701
+
702
+ function convertMessages(
703
+ messages: Message[],
704
+ model: Model<"anthropic-messages">,
705
+ isOAuthToken: boolean,
706
+ cacheControl?: { type: "ephemeral"; ttl?: "1h" },
707
+ ): MessageParam[] {
708
+ const params: MessageParam[] = [];
709
+
710
+ // Transform messages for cross-provider compatibility
711
+ const transformedMessages = transformMessages(messages, model, normalizeToolCallId);
712
+
713
+ for (let i = 0; i < transformedMessages.length; i++) {
714
+ const msg = transformedMessages[i];
715
+
716
+ if (msg.role === "user") {
717
+ if (typeof msg.content === "string") {
718
+ if (msg.content.trim().length > 0) {
719
+ params.push({
720
+ role: "user",
721
+ content: sanitizeSurrogates(msg.content),
722
+ });
723
+ }
724
+ } else {
725
+ const blocks: ContentBlockParam[] = msg.content.map((item) => {
726
+ if (item.type === "text") {
727
+ return {
728
+ type: "text",
729
+ text: sanitizeSurrogates(item.text),
730
+ };
731
+ } else {
732
+ return {
733
+ type: "image",
734
+ source: {
735
+ type: "base64",
736
+ media_type: item.mimeType as "image/jpeg" | "image/png" | "image/gif" | "image/webp",
737
+ data: item.data,
738
+ },
739
+ };
740
+ }
741
+ });
742
+ let filteredBlocks = !model?.input.includes("image") ? blocks.filter((b) => b.type !== "image") : blocks;
743
+ filteredBlocks = filteredBlocks.filter((b) => {
744
+ if (b.type === "text") {
745
+ return b.text.trim().length > 0;
746
+ }
747
+ return true;
748
+ });
749
+ if (filteredBlocks.length === 0) continue;
750
+ params.push({
751
+ role: "user",
752
+ content: filteredBlocks,
753
+ });
754
+ }
755
+ } else if (msg.role === "assistant") {
756
+ const blocks: ContentBlockParam[] = [];
757
+
758
+ for (const block of msg.content) {
759
+ if (block.type === "text") {
760
+ if (block.text.trim().length === 0) continue;
761
+ blocks.push({
762
+ type: "text",
763
+ text: sanitizeSurrogates(block.text),
764
+ });
765
+ } else if (block.type === "thinking") {
766
+ // Redacted thinking: pass the opaque payload back as redacted_thinking
767
+ if (block.redacted) {
768
+ blocks.push({
769
+ type: "redacted_thinking",
770
+ data: block.thinkingSignature!,
771
+ });
772
+ continue;
773
+ }
774
+ if (block.thinking.trim().length === 0) continue;
775
+ // If thinking signature is missing/empty (e.g., from aborted stream),
776
+ // convert to plain text block without <thinking> tags to avoid API rejection
777
+ // and prevent Claude from mimicking the tags in responses
778
+ if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
779
+ blocks.push({
780
+ type: "text",
781
+ text: sanitizeSurrogates(block.thinking),
782
+ });
783
+ } else {
784
+ blocks.push({
785
+ type: "thinking",
786
+ thinking: sanitizeSurrogates(block.thinking),
787
+ signature: block.thinkingSignature,
788
+ });
789
+ }
790
+ } else if (block.type === "toolCall") {
791
+ blocks.push({
792
+ type: "tool_use",
793
+ id: block.id,
794
+ name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
795
+ input: block.arguments ?? {},
796
+ });
797
+ }
798
+ }
799
+ if (blocks.length === 0) continue;
800
+ params.push({
801
+ role: "assistant",
802
+ content: blocks,
803
+ });
804
+ } else if (msg.role === "toolResult") {
805
+ // Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint
806
+ const toolResults: ContentBlockParam[] = [];
807
+
808
+ // Add the current tool result
809
+ toolResults.push({
810
+ type: "tool_result",
811
+ tool_use_id: msg.toolCallId,
812
+ content: convertContentBlocks(msg.content),
813
+ is_error: msg.isError,
814
+ });
815
+
816
+ // Look ahead for consecutive toolResult messages
817
+ let j = i + 1;
818
+ while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
819
+ const nextMsg = transformedMessages[j] as ToolResultMessage; // We know it's a toolResult
820
+ toolResults.push({
821
+ type: "tool_result",
822
+ tool_use_id: nextMsg.toolCallId,
823
+ content: convertContentBlocks(nextMsg.content),
824
+ is_error: nextMsg.isError,
825
+ });
826
+ j++;
827
+ }
828
+
829
+ // Skip the messages we've already processed
830
+ i = j - 1;
831
+
832
+ // Add a single user message with all tool results
833
+ params.push({
834
+ role: "user",
835
+ content: toolResults,
836
+ });
837
+ }
838
+ }
839
+
840
+ // Add cache_control to the last user message to cache conversation history
841
+ if (cacheControl && params.length > 0) {
842
+ const lastMessage = params[params.length - 1];
843
+ if (lastMessage.role === "user") {
844
+ if (Array.isArray(lastMessage.content)) {
845
+ const lastBlock = lastMessage.content[lastMessage.content.length - 1];
846
+ if (
847
+ lastBlock &&
848
+ (lastBlock.type === "text" || lastBlock.type === "image" || lastBlock.type === "tool_result")
849
+ ) {
850
+ (lastBlock as any).cache_control = cacheControl;
851
+ }
852
+ } else if (typeof lastMessage.content === "string") {
853
+ lastMessage.content = [
854
+ {
855
+ type: "text",
856
+ text: lastMessage.content,
857
+ cache_control: cacheControl,
858
+ },
859
+ ] as any;
860
+ }
861
+ }
862
+ }
863
+
864
+ return params;
865
+ }
866
+
867
+ function convertTools(tools: Tool[], isOAuthToken: boolean): Anthropic.Messages.Tool[] {
868
+ if (!tools) return [];
869
+
870
+ return tools.map((tool) => {
871
+ const jsonSchema = tool.parameters as any; // TypeBox already generates JSON Schema
872
+
873
+ return {
874
+ name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name,
875
+ description: tool.description,
876
+ input_schema: {
877
+ type: "object" as const,
878
+ properties: jsonSchema.properties || {},
879
+ required: jsonSchema.required || [],
880
+ },
881
+ };
882
+ });
883
+ }
884
+
885
+ function mapStopReason(reason: Anthropic.Messages.StopReason | string): StopReason {
886
+ switch (reason) {
887
+ case "end_turn":
888
+ return "stop";
889
+ case "max_tokens":
890
+ return "length";
891
+ case "tool_use":
892
+ return "toolUse";
893
+ case "refusal":
894
+ return "error";
895
+ case "pause_turn": // Stop is good enough -> resubmit
896
+ return "stop";
897
+ case "stop_sequence":
898
+ return "stop"; // We don't supply stop sequences, so this should never happen
899
+ case "sensitive": // Content flagged by safety filters (not yet in SDK types)
900
+ return "error";
901
+ default:
902
+ // Handle unknown stop reasons gracefully (API may add new values)
903
+ throw new Error(`Unhandled stop reason: ${reason}`);
904
+ }
905
+ }