gsd-pi 2.37.1 → 2.38.0-dev.96dc7fb

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 (116) hide show
  1. package/README.md +1 -1
  2. package/dist/cli.js +9 -0
  3. package/dist/extension-discovery.d.ts +5 -3
  4. package/dist/extension-discovery.js +14 -9
  5. package/dist/onboarding.js +1 -0
  6. package/dist/resources/extensions/browser-tools/package.json +3 -1
  7. package/dist/resources/extensions/cmux/index.js +55 -1
  8. package/dist/resources/extensions/context7/package.json +1 -1
  9. package/dist/resources/extensions/google-search/package.json +3 -1
  10. package/dist/resources/extensions/gsd/auto-dispatch.js +67 -1
  11. package/dist/resources/extensions/gsd/auto-loop.js +7 -1
  12. package/dist/resources/extensions/gsd/auto-post-unit.js +14 -0
  13. package/dist/resources/extensions/gsd/auto-prompts.js +91 -2
  14. package/dist/resources/extensions/gsd/auto-recovery.js +37 -1
  15. package/dist/resources/extensions/gsd/auto-start.js +6 -1
  16. package/dist/resources/extensions/gsd/auto-worktree-sync.js +11 -4
  17. package/dist/resources/extensions/gsd/captures.js +9 -1
  18. package/dist/resources/extensions/gsd/commands-handlers.js +16 -3
  19. package/dist/resources/extensions/gsd/commands.js +20 -1
  20. package/dist/resources/extensions/gsd/doctor-checks.js +82 -0
  21. package/dist/resources/extensions/gsd/doctor-environment.js +78 -0
  22. package/dist/resources/extensions/gsd/doctor-format.js +15 -0
  23. package/dist/resources/extensions/gsd/doctor-providers.js +35 -1
  24. package/dist/resources/extensions/gsd/doctor.js +184 -11
  25. package/dist/resources/extensions/gsd/files.js +41 -0
  26. package/dist/resources/extensions/gsd/observability-validator.js +24 -0
  27. package/dist/resources/extensions/gsd/package.json +1 -1
  28. package/dist/resources/extensions/gsd/preferences-types.js +2 -1
  29. package/dist/resources/extensions/gsd/preferences-validation.js +42 -0
  30. package/dist/resources/extensions/gsd/prompts/plan-slice.md +2 -1
  31. package/dist/resources/extensions/gsd/prompts/reactive-execute.md +41 -0
  32. package/dist/resources/extensions/gsd/reactive-graph.js +227 -0
  33. package/dist/resources/extensions/gsd/templates/task-plan.md +11 -3
  34. package/dist/resources/extensions/gsd/worktree.js +35 -16
  35. package/dist/resources/extensions/subagent/index.js +12 -3
  36. package/dist/resources/extensions/universal-config/package.json +1 -1
  37. package/dist/welcome-screen.d.ts +12 -0
  38. package/dist/welcome-screen.js +53 -0
  39. package/package.json +2 -1
  40. package/packages/pi-ai/dist/env-api-keys.js +13 -0
  41. package/packages/pi-ai/dist/env-api-keys.js.map +1 -1
  42. package/packages/pi-ai/dist/models.generated.d.ts +172 -0
  43. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  44. package/packages/pi-ai/dist/models.generated.js +172 -0
  45. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  46. package/packages/pi-ai/dist/providers/anthropic-shared.d.ts +64 -0
  47. package/packages/pi-ai/dist/providers/anthropic-shared.d.ts.map +1 -0
  48. package/packages/pi-ai/dist/providers/anthropic-shared.js +668 -0
  49. package/packages/pi-ai/dist/providers/anthropic-shared.js.map +1 -0
  50. package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts +5 -0
  51. package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts.map +1 -0
  52. package/packages/pi-ai/dist/providers/anthropic-vertex.js +85 -0
  53. package/packages/pi-ai/dist/providers/anthropic-vertex.js.map +1 -0
  54. package/packages/pi-ai/dist/providers/anthropic.d.ts +4 -30
  55. package/packages/pi-ai/dist/providers/anthropic.d.ts.map +1 -1
  56. package/packages/pi-ai/dist/providers/anthropic.js +47 -764
  57. package/packages/pi-ai/dist/providers/anthropic.js.map +1 -1
  58. package/packages/pi-ai/dist/providers/register-builtins.d.ts.map +1 -1
  59. package/packages/pi-ai/dist/providers/register-builtins.js +6 -0
  60. package/packages/pi-ai/dist/providers/register-builtins.js.map +1 -1
  61. package/packages/pi-ai/dist/types.d.ts +2 -2
  62. package/packages/pi-ai/dist/types.d.ts.map +1 -1
  63. package/packages/pi-ai/dist/types.js.map +1 -1
  64. package/packages/pi-ai/package.json +1 -0
  65. package/packages/pi-ai/src/env-api-keys.ts +14 -0
  66. package/packages/pi-ai/src/models.generated.ts +172 -0
  67. package/packages/pi-ai/src/providers/anthropic-shared.ts +761 -0
  68. package/packages/pi-ai/src/providers/anthropic-vertex.ts +130 -0
  69. package/packages/pi-ai/src/providers/anthropic.ts +76 -868
  70. package/packages/pi-ai/src/providers/register-builtins.ts +7 -0
  71. package/packages/pi-ai/src/types.ts +2 -0
  72. package/packages/pi-coding-agent/dist/core/model-resolver.d.ts.map +1 -1
  73. package/packages/pi-coding-agent/dist/core/model-resolver.js +1 -0
  74. package/packages/pi-coding-agent/dist/core/model-resolver.js.map +1 -1
  75. package/packages/pi-coding-agent/dist/core/package-manager.d.ts.map +1 -1
  76. package/packages/pi-coding-agent/dist/core/package-manager.js +8 -4
  77. package/packages/pi-coding-agent/dist/core/package-manager.js.map +1 -1
  78. package/packages/pi-coding-agent/package.json +1 -1
  79. package/packages/pi-coding-agent/src/core/model-resolver.ts +1 -0
  80. package/packages/pi-coding-agent/src/core/package-manager.ts +8 -4
  81. package/pkg/package.json +1 -1
  82. package/src/resources/extensions/cmux/index.ts +57 -1
  83. package/src/resources/extensions/gsd/auto-dispatch.ts +93 -0
  84. package/src/resources/extensions/gsd/auto-loop.ts +13 -1
  85. package/src/resources/extensions/gsd/auto-post-unit.ts +14 -0
  86. package/src/resources/extensions/gsd/auto-prompts.ts +125 -3
  87. package/src/resources/extensions/gsd/auto-recovery.ts +42 -0
  88. package/src/resources/extensions/gsd/auto-start.ts +7 -1
  89. package/src/resources/extensions/gsd/auto-worktree-sync.ts +12 -3
  90. package/src/resources/extensions/gsd/captures.ts +10 -1
  91. package/src/resources/extensions/gsd/commands-handlers.ts +17 -2
  92. package/src/resources/extensions/gsd/commands.ts +21 -1
  93. package/src/resources/extensions/gsd/doctor-checks.ts +75 -0
  94. package/src/resources/extensions/gsd/doctor-environment.ts +82 -1
  95. package/src/resources/extensions/gsd/doctor-format.ts +20 -0
  96. package/src/resources/extensions/gsd/doctor-providers.ts +38 -1
  97. package/src/resources/extensions/gsd/doctor-types.ts +16 -1
  98. package/src/resources/extensions/gsd/doctor.ts +177 -13
  99. package/src/resources/extensions/gsd/files.ts +45 -0
  100. package/src/resources/extensions/gsd/observability-validator.ts +27 -0
  101. package/src/resources/extensions/gsd/preferences-types.ts +5 -1
  102. package/src/resources/extensions/gsd/preferences-validation.ts +41 -0
  103. package/src/resources/extensions/gsd/prompts/plan-slice.md +2 -1
  104. package/src/resources/extensions/gsd/prompts/reactive-execute.md +41 -0
  105. package/src/resources/extensions/gsd/reactive-graph.ts +289 -0
  106. package/src/resources/extensions/gsd/templates/task-plan.md +11 -3
  107. package/src/resources/extensions/gsd/tests/cmux.test.ts +93 -0
  108. package/src/resources/extensions/gsd/tests/doctor-enhancements.test.ts +266 -0
  109. package/src/resources/extensions/gsd/tests/doctor-providers.test.ts +108 -3
  110. package/src/resources/extensions/gsd/tests/plan-quality-validator.test.ts +111 -0
  111. package/src/resources/extensions/gsd/tests/reactive-executor.test.ts +511 -0
  112. package/src/resources/extensions/gsd/tests/reactive-graph.test.ts +299 -0
  113. package/src/resources/extensions/gsd/tests/worktree.test.ts +47 -0
  114. package/src/resources/extensions/gsd/types.ts +43 -0
  115. package/src/resources/extensions/gsd/worktree.ts +35 -15
  116. package/src/resources/extensions/subagent/index.ts +12 -3
@@ -1,40 +1,29 @@
1
1
  // Lazy-loaded: Anthropic SDK (~500ms) is imported on first use, not at startup.
2
2
  // This avoids penalizing users who don't use Anthropic models.
3
3
  import type Anthropic from "@anthropic-ai/sdk";
4
- import type {
5
- ContentBlockParam,
6
- MessageCreateParamsStreaming,
7
- MessageParam,
8
- } from "@anthropic-ai/sdk/resources/messages.js";
9
4
  import { getEnvApiKey } from "../env-api-keys.js";
10
- import { calculateCost } from "../models.js";
11
5
  import type {
12
- Api,
13
- AssistantMessage,
14
- CacheRetention,
15
6
  Context,
16
- ImageContent,
17
- Message,
18
7
  Model,
19
- ServerToolUseContent,
20
8
  SimpleStreamOptions,
21
- StopReason,
22
9
  StreamFunction,
23
- StreamOptions,
24
- TextContent,
25
- ThinkingContent,
26
- Tool,
27
- ToolCall,
28
- ToolResultMessage,
29
- WebSearchResultContent,
30
10
  } from "../types.js";
31
11
  import { AssistantMessageEventStream } from "../utils/event-stream.js";
32
- import { parseStreamingJson } from "../utils/json-parse.js";
33
- import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
34
12
 
35
13
  import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
36
14
  import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.js";
37
- import { transformMessages } from "./transform-messages.js";
15
+ import {
16
+ type AnthropicEffort,
17
+ type AnthropicOptions,
18
+ extractRetryAfterMs,
19
+ mapThinkingLevelToEffort,
20
+ processAnthropicStream,
21
+ supportsAdaptiveThinking,
22
+ } from "./anthropic-shared.js";
23
+
24
+ // Re-export types used by other modules
25
+ export type { AnthropicEffort, AnthropicOptions };
26
+ export { extractRetryAfterMs };
38
27
 
39
28
  let _AnthropicClass: typeof Anthropic | undefined;
40
29
  async function getAnthropicClass(): Promise<typeof Anthropic> {
@@ -45,154 +34,9 @@ async function getAnthropicClass(): Promise<typeof Anthropic> {
45
34
  return _AnthropicClass;
46
35
  }
47
36
 
48
- /**
49
- * Resolve cache retention preference.
50
- * Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
51
- */
52
- function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention {
53
- if (cacheRetention) {
54
- return cacheRetention;
55
- }
56
- if (typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "long") {
57
- return "long";
58
- }
59
- return "short";
60
- }
61
-
62
- function getCacheControl(
63
- baseUrl: string,
64
- cacheRetention?: CacheRetention,
65
- ): { retention: CacheRetention; cacheControl?: { type: "ephemeral"; ttl?: "1h" } } {
66
- const retention = resolveCacheRetention(cacheRetention);
67
- if (retention === "none") {
68
- return { retention };
69
- }
70
- const ttl = retention === "long" && baseUrl.includes("api.anthropic.com") ? "1h" : undefined;
71
- return {
72
- retention,
73
- cacheControl: { type: "ephemeral", ...(ttl && { ttl }) },
74
- };
75
- }
76
-
77
37
  // Stealth mode: Mimic Claude Code's tool naming exactly
78
38
  const claudeCodeVersion = "2.1.62";
79
39
 
80
- // Claude Code 2.x tool names (canonical casing)
81
- // Source: https://cchistory.mariozechner.at/data/prompts-2.1.11.md
82
- // To update: https://github.com/badlogic/cchistory
83
- const claudeCodeTools = [
84
- "Read",
85
- "Write",
86
- "Edit",
87
- "Bash",
88
- "Grep",
89
- "Glob",
90
- "AskUserQuestion",
91
- "EnterPlanMode",
92
- "ExitPlanMode",
93
- "KillShell",
94
- "NotebookEdit",
95
- "Skill",
96
- "Task",
97
- "TaskOutput",
98
- "TodoWrite",
99
- "WebFetch",
100
- "WebSearch",
101
- ];
102
-
103
- const ccToolLookup = new Map(claudeCodeTools.map((t) => [t.toLowerCase(), t]));
104
-
105
- // Convert tool name to CC canonical casing if it matches (case-insensitive)
106
- const toClaudeCodeName = (name: string) => ccToolLookup.get(name.toLowerCase()) ?? name;
107
- const fromClaudeCodeName = (name: string, tools?: Tool[]) => {
108
- if (tools && tools.length > 0) {
109
- const lowerName = name.toLowerCase();
110
- const matchedTool = tools.find((tool) => tool.name.toLowerCase() === lowerName);
111
- if (matchedTool) return matchedTool.name;
112
- }
113
- return name;
114
- };
115
-
116
- /**
117
- * Convert content blocks to Anthropic API format
118
- */
119
- function convertContentBlocks(content: (TextContent | ImageContent)[]):
120
- | string
121
- | Array<
122
- | { type: "text"; text: string }
123
- | {
124
- type: "image";
125
- source: {
126
- type: "base64";
127
- media_type: "image/jpeg" | "image/png" | "image/gif" | "image/webp";
128
- data: string;
129
- };
130
- }
131
- > {
132
- // If only text blocks, return as concatenated string for simplicity
133
- const hasImages = content.some((c) => c.type === "image");
134
- if (!hasImages) {
135
- return sanitizeSurrogates(content.map((c) => (c as TextContent).text).join("\n"));
136
- }
137
-
138
- // If we have images, convert to content block array
139
- const blocks = content.map((block) => {
140
- if (block.type === "text") {
141
- return {
142
- type: "text" as const,
143
- text: sanitizeSurrogates(block.text),
144
- };
145
- }
146
- return {
147
- type: "image" as const,
148
- source: {
149
- type: "base64" as const,
150
- media_type: block.mimeType as "image/jpeg" | "image/png" | "image/gif" | "image/webp",
151
- data: block.data,
152
- },
153
- };
154
- });
155
-
156
- // If only images (no text), add placeholder text block
157
- const hasText = blocks.some((b) => b.type === "text");
158
- if (!hasText) {
159
- blocks.unshift({
160
- type: "text" as const,
161
- text: "(see attached image)",
162
- });
163
- }
164
-
165
- return blocks;
166
- }
167
-
168
- export type AnthropicEffort = "low" | "medium" | "high" | "max";
169
-
170
- export interface AnthropicOptions extends StreamOptions {
171
- /**
172
- * Enable extended thinking.
173
- * For Opus 4.6 and Sonnet 4.6: uses adaptive thinking (model decides when/how much to think).
174
- * For older models: uses budget-based thinking with thinkingBudgetTokens.
175
- */
176
- thinkingEnabled?: boolean;
177
- /**
178
- * Token budget for extended thinking (older models only).
179
- * Ignored for Opus 4.6 and Sonnet 4.6, which use adaptive thinking.
180
- */
181
- thinkingBudgetTokens?: number;
182
- /**
183
- * Effort level for adaptive thinking (Opus 4.6 and Sonnet 4.6).
184
- * Controls how much thinking Claude allocates:
185
- * - "max": Always thinks with no constraints (Opus 4.6 only)
186
- * - "high": Always thinks, deep reasoning (default)
187
- * - "medium": Moderate thinking, may skip for simple queries
188
- * - "low": Minimal thinking, skips for simple tasks
189
- * Ignored for older models.
190
- */
191
- effort?: AnthropicEffort;
192
- interleavedThinking?: boolean;
193
- toolChoice?: "auto" | "any" | "none" | { type: "tool"; name: string };
194
- }
195
-
196
40
  function mergeHeaders(...headerSources: (Record<string, string> | undefined)[]): Record<string, string> {
197
41
  const merged: Record<string, string> = {};
198
42
  for (const headers of headerSources) {
@@ -203,410 +47,6 @@ function mergeHeaders(...headerSources: (Record<string, string> | undefined)[]):
203
47
  return merged;
204
48
  }
205
49
 
206
- /**
207
- * Detect transient network errors that are likely to succeed on retry.
208
- * Covers WebSocket disconnects (Tailscale, VPN), TCP resets, and DNS failures.
209
- */
210
- function isTransientNetworkError(error: unknown): boolean {
211
- if (!(error instanceof Error)) return false;
212
- const msg = error.message.toLowerCase();
213
- const code = (error as NodeJS.ErrnoException).code;
214
- return (
215
- code === 'ECONNRESET' ||
216
- code === 'EPIPE' ||
217
- code === 'ETIMEDOUT' ||
218
- code === 'ENOTFOUND' ||
219
- code === 'EAI_AGAIN' ||
220
- msg.includes('connector_closed') ||
221
- msg.includes('socket hang up') ||
222
- msg.includes('network') ||
223
- msg.includes('connection') && msg.includes('closed') ||
224
- msg.includes('fetch failed')
225
- );
226
- }
227
-
228
- /**
229
- * Extract retry delay from Anthropic error response headers (in milliseconds).
230
- * Checks: retry-after (seconds or RFC date), x-ratelimit-reset-requests, x-ratelimit-reset-tokens.
231
- * Returns undefined if no valid delay is found or if the delay is in the past.
232
- */
233
- function extractRetryAfterMs(headers: Headers | { get(name: string): string | null }, errorText = ""): number | undefined {
234
- const normalizeDelay = (ms: number): number | undefined => (ms > 0 ? Math.ceil(ms + 1000) : undefined);
235
-
236
- const retryAfter = headers.get("retry-after");
237
- if (retryAfter) {
238
- const seconds = Number(retryAfter);
239
- if (Number.isFinite(seconds)) {
240
- const delay = normalizeDelay(seconds * 1000);
241
- if (delay !== undefined) return delay;
242
- }
243
- const asDate = new Date(retryAfter).getTime();
244
- if (!Number.isNaN(asDate)) {
245
- const delay = normalizeDelay(asDate - Date.now());
246
- if (delay !== undefined) return delay;
247
- }
248
- }
249
-
250
- // x-ratelimit-reset-requests / x-ratelimit-reset-tokens are Unix timestamps (seconds)
251
- for (const header of ["x-ratelimit-reset-requests", "x-ratelimit-reset-tokens"]) {
252
- const value = headers.get(header);
253
- if (value) {
254
- const resetSeconds = Number(value);
255
- if (Number.isFinite(resetSeconds)) {
256
- const delay = normalizeDelay(resetSeconds * 1000 - Date.now());
257
- if (delay !== undefined) return delay;
258
- }
259
- }
260
- }
261
-
262
- return undefined;
263
- }
264
-
265
- export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOptions> = (
266
- model: Model<"anthropic-messages">,
267
- context: Context,
268
- options?: AnthropicOptions,
269
- ): AssistantMessageEventStream => {
270
- const stream = new AssistantMessageEventStream();
271
-
272
- (async () => {
273
- const output: AssistantMessage = {
274
- role: "assistant",
275
- content: [],
276
- api: model.api as Api,
277
- provider: model.provider,
278
- model: model.id,
279
- usage: {
280
- input: 0,
281
- output: 0,
282
- cacheRead: 0,
283
- cacheWrite: 0,
284
- totalTokens: 0,
285
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
286
- },
287
- stopReason: "stop",
288
- timestamp: Date.now(),
289
- };
290
-
291
- try {
292
- const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
293
-
294
- let copilotDynamicHeaders: Record<string, string> | undefined;
295
- if (model.provider === "github-copilot") {
296
- const hasImages = hasCopilotVisionInput(context.messages);
297
- copilotDynamicHeaders = buildCopilotDynamicHeaders({
298
- messages: context.messages,
299
- hasImages,
300
- });
301
- }
302
-
303
- const { client, isOAuthToken } = await createClient(
304
- model,
305
- apiKey,
306
- options?.interleavedThinking ?? true,
307
- options?.headers,
308
- copilotDynamicHeaders,
309
- );
310
- let params = buildParams(model, context, isOAuthToken, options);
311
- const nextParams = await options?.onPayload?.(params, model);
312
- if (nextParams !== undefined) {
313
- params = nextParams as MessageCreateParamsStreaming;
314
- }
315
- const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });
316
- stream.push({ type: "start", partial: output });
317
-
318
- type Block = (ThinkingContent | TextContent | (ToolCall & { partialJson: string }) | ServerToolUseContent | WebSearchResultContent) & { index: number };
319
- const blocks = output.content as Block[];
320
-
321
- for await (const event of anthropicStream) {
322
- if (event.type === "message_start") {
323
- // Capture initial token usage from message_start event
324
- // This ensures we have input token counts even if the stream is aborted early
325
- output.usage.input = event.message.usage.input_tokens || 0;
326
- output.usage.output = event.message.usage.output_tokens || 0;
327
- output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
328
- output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
329
- // Anthropic doesn't provide total_tokens, compute from components
330
- output.usage.totalTokens =
331
- output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
332
- calculateCost(model, output.usage);
333
- } else if (event.type === "content_block_start") {
334
- if (event.content_block.type === "text") {
335
- const block: Block = {
336
- type: "text",
337
- text: "",
338
- index: event.index,
339
- };
340
- output.content.push(block);
341
- stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
342
- } else if (event.content_block.type === "thinking") {
343
- const block: Block = {
344
- type: "thinking",
345
- thinking: "",
346
- thinkingSignature: "",
347
- index: event.index,
348
- };
349
- output.content.push(block);
350
- stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
351
- } else if (event.content_block.type === "redacted_thinking") {
352
- const block: Block = {
353
- type: "thinking",
354
- thinking: "[Reasoning redacted]",
355
- thinkingSignature: event.content_block.data,
356
- redacted: true,
357
- index: event.index,
358
- };
359
- output.content.push(block);
360
- stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
361
- } else if (event.content_block.type === "tool_use") {
362
- const block: Block = {
363
- type: "toolCall",
364
- id: event.content_block.id,
365
- name: isOAuthToken
366
- ? fromClaudeCodeName(event.content_block.name, context.tools)
367
- : event.content_block.name,
368
- arguments: (event.content_block.input as Record<string, any>) ?? {},
369
- partialJson: "",
370
- index: event.index,
371
- };
372
- output.content.push(block);
373
- stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
374
- } else if ((event.content_block as any).type === "server_tool_use") {
375
- const serverBlock = event.content_block as any;
376
- const block: Block = {
377
- type: "serverToolUse",
378
- id: serverBlock.id,
379
- name: serverBlock.name,
380
- input: serverBlock.input,
381
- index: event.index,
382
- };
383
- output.content.push(block);
384
- stream.push({ type: "server_tool_use", contentIndex: output.content.length - 1, partial: output });
385
- } else if ((event.content_block as any).type === "web_search_tool_result") {
386
- const resultBlock = event.content_block as any;
387
- const block: Block = {
388
- type: "webSearchResult",
389
- toolUseId: resultBlock.tool_use_id,
390
- content: resultBlock.content,
391
- index: event.index,
392
- };
393
- output.content.push(block);
394
- stream.push({ type: "web_search_result", contentIndex: output.content.length - 1, partial: output });
395
- }
396
- } else if (event.type === "content_block_delta") {
397
- if (event.delta.type === "text_delta") {
398
- const index = blocks.findIndex((b) => b.index === event.index);
399
- const block = blocks[index];
400
- if (block && block.type === "text") {
401
- block.text += event.delta.text;
402
- stream.push({
403
- type: "text_delta",
404
- contentIndex: index,
405
- delta: event.delta.text,
406
- partial: output,
407
- });
408
- }
409
- } else if (event.delta.type === "thinking_delta") {
410
- const index = blocks.findIndex((b) => b.index === event.index);
411
- const block = blocks[index];
412
- if (block && block.type === "thinking") {
413
- block.thinking += event.delta.thinking;
414
- stream.push({
415
- type: "thinking_delta",
416
- contentIndex: index,
417
- delta: event.delta.thinking,
418
- partial: output,
419
- });
420
- }
421
- } else if (event.delta.type === "input_json_delta") {
422
- const index = blocks.findIndex((b) => b.index === event.index);
423
- const block = blocks[index];
424
- if (block && block.type === "toolCall") {
425
- block.partialJson += event.delta.partial_json;
426
- block.arguments = parseStreamingJson(block.partialJson);
427
- stream.push({
428
- type: "toolcall_delta",
429
- contentIndex: index,
430
- delta: event.delta.partial_json,
431
- partial: output,
432
- });
433
- }
434
- } else if (event.delta.type === "signature_delta") {
435
- const index = blocks.findIndex((b) => b.index === event.index);
436
- const block = blocks[index];
437
- if (block && block.type === "thinking") {
438
- block.thinkingSignature = block.thinkingSignature || "";
439
- block.thinkingSignature += event.delta.signature;
440
- }
441
- }
442
- } else if (event.type === "content_block_stop") {
443
- const index = blocks.findIndex((b) => b.index === event.index);
444
- const block = blocks[index];
445
- if (block) {
446
- delete (block as any).index;
447
- if (block.type === "text") {
448
- stream.push({
449
- type: "text_end",
450
- contentIndex: index,
451
- content: block.text,
452
- partial: output,
453
- });
454
- } else if (block.type === "thinking") {
455
- stream.push({
456
- type: "thinking_end",
457
- contentIndex: index,
458
- content: block.thinking,
459
- partial: output,
460
- });
461
- } else if (block.type === "toolCall") {
462
- block.arguments = parseStreamingJson(block.partialJson);
463
- delete (block as any).partialJson;
464
- stream.push({
465
- type: "toolcall_end",
466
- contentIndex: index,
467
- toolCall: block,
468
- partial: output,
469
- });
470
- }
471
- // serverToolUse and webSearchResult blocks just need index cleanup (already emitted on start)
472
- }
473
- } else if (event.type === "message_delta") {
474
- if (event.delta.stop_reason) {
475
- output.stopReason = mapStopReason(event.delta.stop_reason);
476
- }
477
- // Only update usage fields if present (not null).
478
- // Preserves input_tokens from message_start when proxies omit it in message_delta.
479
- if (event.usage.input_tokens != null) {
480
- output.usage.input = event.usage.input_tokens;
481
- }
482
- if (event.usage.output_tokens != null) {
483
- output.usage.output = event.usage.output_tokens;
484
- }
485
- if (event.usage.cache_read_input_tokens != null) {
486
- output.usage.cacheRead = event.usage.cache_read_input_tokens;
487
- }
488
- if (event.usage.cache_creation_input_tokens != null) {
489
- output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
490
- }
491
- // Anthropic doesn't provide total_tokens, compute from components
492
- output.usage.totalTokens =
493
- output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
494
- calculateCost(model, output.usage);
495
- }
496
- }
497
-
498
- if (options?.signal?.aborted) {
499
- throw new Error("Request was aborted");
500
- }
501
-
502
- if (output.stopReason === "aborted" || output.stopReason === "error") {
503
- throw new Error("An unknown error occurred");
504
- }
505
-
506
- stream.push({ type: "done", reason: output.stopReason, message: output });
507
- stream.end();
508
- } catch (error) {
509
- for (const block of output.content) delete (block as any).index;
510
- output.stopReason = options?.signal?.aborted ? "aborted" : "error";
511
- output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
512
- if (model.provider === "alibaba-coding-plan") {
513
- output.errorMessage = `[alibaba-coding-plan] ${output.errorMessage}`;
514
- }
515
- const AnthropicSdk = _AnthropicClass;
516
- if (AnthropicSdk && error instanceof AnthropicSdk.APIError && error.headers) {
517
- const retryAfterMs = extractRetryAfterMs(error.headers, error.message);
518
- if (retryAfterMs !== undefined) {
519
- output.retryAfterMs = retryAfterMs;
520
- }
521
- }
522
- // Mark transient network errors as retriable so auto-mode can
523
- // detect them and retry instead of stopping (#833).
524
- if (isTransientNetworkError(error)) {
525
- output.retryAfterMs = output.retryAfterMs ?? 5000;
526
- }
527
- stream.push({ type: "error", reason: output.stopReason, error: output });
528
- stream.end();
529
- }
530
- })();
531
-
532
- return stream;
533
- };
534
-
535
- /**
536
- * Check if a model supports adaptive thinking (Opus 4.6 and Sonnet 4.6)
537
- */
538
- function supportsAdaptiveThinking(modelId: string): boolean {
539
- // Opus 4.6 and Sonnet 4.6 model IDs (with or without date suffix)
540
- return (
541
- modelId.includes("opus-4-6") ||
542
- modelId.includes("opus-4.6") ||
543
- modelId.includes("sonnet-4-6") ||
544
- modelId.includes("sonnet-4.6")
545
- );
546
- }
547
-
548
- /**
549
- * Map ThinkingLevel to Anthropic effort levels for adaptive thinking.
550
- * Note: effort "max" is only valid on Opus 4.6.
551
- */
552
- function mapThinkingLevelToEffort(level: SimpleStreamOptions["reasoning"], modelId: string): AnthropicEffort {
553
- switch (level) {
554
- case "minimal":
555
- return "low";
556
- case "low":
557
- return "low";
558
- case "medium":
559
- return "medium";
560
- case "high":
561
- return "high";
562
- case "xhigh":
563
- return modelId.includes("opus-4-6") || modelId.includes("opus-4.6") ? "max" : "high";
564
- default:
565
- return "high";
566
- }
567
- }
568
-
569
- export const streamSimpleAnthropic: StreamFunction<"anthropic-messages", SimpleStreamOptions> = (
570
- model: Model<"anthropic-messages">,
571
- context: Context,
572
- options?: SimpleStreamOptions,
573
- ): AssistantMessageEventStream => {
574
- const apiKey = options?.apiKey || getEnvApiKey(model.provider);
575
- if (!apiKey) {
576
- throw new Error(`No API key for provider: ${model.provider}`);
577
- }
578
-
579
- const base = buildBaseOptions(model, options, apiKey);
580
- if (!options?.reasoning) {
581
- return streamAnthropic(model, context, { ...base, thinkingEnabled: false } satisfies AnthropicOptions);
582
- }
583
-
584
- // For Opus 4.6 and Sonnet 4.6: use adaptive thinking with effort level
585
- // For older models: use budget-based thinking
586
- if (supportsAdaptiveThinking(model.id)) {
587
- const effort = mapThinkingLevelToEffort(options.reasoning, model.id);
588
- return streamAnthropic(model, context, {
589
- ...base,
590
- thinkingEnabled: true,
591
- effort,
592
- } satisfies AnthropicOptions);
593
- }
594
-
595
- const adjusted = adjustMaxTokensForThinking(
596
- base.maxTokens || 0,
597
- model.maxTokens,
598
- options.reasoning,
599
- options.thinkingBudgets,
600
- );
601
-
602
- return streamAnthropic(model, context, {
603
- ...base,
604
- maxTokens: adjusted.maxTokens,
605
- thinkingEnabled: true,
606
- thinkingBudgetTokens: adjusted.thinkingBudget,
607
- } satisfies AnthropicOptions);
608
- };
609
-
610
50
  function isOAuthToken(apiKey: string): boolean {
611
51
  return apiKey.includes("sk-ant-oat");
612
52
  }
@@ -702,315 +142,83 @@ async function createClient(
702
142
  return { client, isOAuthToken: false };
703
143
  }
704
144
 
705
- function buildParams(
145
+ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOptions> = (
706
146
  model: Model<"anthropic-messages">,
707
147
  context: Context,
708
- isOAuthToken: boolean,
709
148
  options?: AnthropicOptions,
710
- ): MessageCreateParamsStreaming {
711
- const { cacheControl } = getCacheControl(model.baseUrl, options?.cacheRetention);
712
- // Strip variant suffixes like [1m] from model ID before sending to the API.
713
- // The API only accepts the base model ID (e.g. "claude-opus-4-6"),
714
- // not internal variant identifiers (e.g. "claude-opus-4-6[1m]").
715
- // This applies to all auth methods — API keys, OAuth, and Copilot alike.
716
- const apiModelId = model.id.replace(/\[.*\]$/, "");
717
- const params: MessageCreateParamsStreaming = {
718
- model: apiModelId,
719
- messages: convertMessages(context.messages, model, isOAuthToken, cacheControl),
720
- max_tokens: options?.maxTokens || (model.maxTokens / 3) | 0,
721
- stream: true,
722
- };
149
+ ): AssistantMessageEventStream => {
150
+ const stream = new AssistantMessageEventStream();
723
151
 
724
- // For OAuth tokens, we MUST include Claude Code identity
725
- if (isOAuthToken) {
726
- params.system = [
727
- {
728
- type: "text",
729
- text: "You are Claude Code, Anthropic's official CLI for Claude.",
730
- ...(cacheControl ? { cache_control: cacheControl } : {}),
731
- },
732
- ];
733
- if (context.systemPrompt) {
734
- params.system.push({
735
- type: "text",
736
- text: sanitizeSurrogates(context.systemPrompt),
737
- ...(cacheControl ? { cache_control: cacheControl } : {}),
152
+ (async () => {
153
+ const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
154
+
155
+ let copilotDynamicHeaders: Record<string, string> | undefined;
156
+ if (model.provider === "github-copilot") {
157
+ const hasImages = hasCopilotVisionInput(context.messages);
158
+ copilotDynamicHeaders = buildCopilotDynamicHeaders({
159
+ messages: context.messages,
160
+ hasImages,
738
161
  });
739
162
  }
740
- } else if (context.systemPrompt) {
741
- // Add cache control to system prompt for non-OAuth tokens
742
- params.system = [
743
- {
744
- type: "text",
745
- text: sanitizeSurrogates(context.systemPrompt),
746
- ...(cacheControl ? { cache_control: cacheControl } : {}),
747
- },
748
- ];
749
- }
750
-
751
- // Temperature is incompatible with extended thinking (adaptive or budget-based).
752
- if (options?.temperature !== undefined && !options?.thinkingEnabled) {
753
- params.temperature = options.temperature;
754
- }
755
-
756
- if (context.tools) {
757
- params.tools = convertTools(context.tools, isOAuthToken);
758
- }
759
-
760
- // Configure thinking mode: adaptive (Opus 4.6 and Sonnet 4.6) or budget-based (older models)
761
- if (options?.thinkingEnabled && model.reasoning) {
762
- if (supportsAdaptiveThinking(model.id)) {
763
- // Adaptive thinking: Claude decides when and how much to think
764
- params.thinking = { type: "adaptive" };
765
- if (options.effort) {
766
- params.output_config = { effort: options.effort };
767
- }
768
- } else {
769
- // Budget-based thinking for older models
770
- params.thinking = {
771
- type: "enabled",
772
- budget_tokens: options.thinkingBudgetTokens || 1024,
773
- };
774
- }
775
- }
776
-
777
- if (options?.metadata) {
778
- const userId = options.metadata.user_id;
779
- if (typeof userId === "string") {
780
- params.metadata = { user_id: userId };
781
- }
782
- }
783
-
784
- if (options?.toolChoice) {
785
- if (typeof options.toolChoice === "string") {
786
- params.tool_choice = { type: options.toolChoice };
787
- } else {
788
- params.tool_choice = options.toolChoice;
789
- }
790
- }
791
163
 
792
- return params;
793
- }
164
+ const { client, isOAuthToken: isOAuth } = await createClient(
165
+ model,
166
+ apiKey,
167
+ options?.interleavedThinking ?? true,
168
+ options?.headers,
169
+ copilotDynamicHeaders,
170
+ );
171
+
172
+ processAnthropicStream(stream, {
173
+ client,
174
+ model,
175
+ context,
176
+ isOAuthToken: isOAuth,
177
+ options,
178
+ AnthropicSdkClass: _AnthropicClass,
179
+ });
180
+ })();
794
181
 
795
- // Normalize tool call IDs to match Anthropic's required pattern and length
796
- function normalizeToolCallId(id: string): string {
797
- return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
798
- }
182
+ return stream;
183
+ };
799
184
 
800
- function convertMessages(
801
- messages: Message[],
185
+ export const streamSimpleAnthropic: StreamFunction<"anthropic-messages", SimpleStreamOptions> = (
802
186
  model: Model<"anthropic-messages">,
803
- isOAuthToken: boolean,
804
- cacheControl?: { type: "ephemeral"; ttl?: "1h" },
805
- ): MessageParam[] {
806
- const params: MessageParam[] = [];
807
-
808
- // Transform messages for cross-provider compatibility
809
- const transformedMessages = transformMessages(messages, model, normalizeToolCallId);
810
-
811
- for (let i = 0; i < transformedMessages.length; i++) {
812
- const msg = transformedMessages[i];
813
-
814
- if (msg.role === "user") {
815
- if (typeof msg.content === "string") {
816
- if (msg.content.trim().length > 0) {
817
- params.push({
818
- role: "user",
819
- content: sanitizeSurrogates(msg.content),
820
- });
821
- }
822
- } else {
823
- const blocks: ContentBlockParam[] = msg.content.map((item) => {
824
- if (item.type === "text") {
825
- return {
826
- type: "text",
827
- text: sanitizeSurrogates(item.text),
828
- };
829
- } else {
830
- return {
831
- type: "image",
832
- source: {
833
- type: "base64",
834
- media_type: item.mimeType as "image/jpeg" | "image/png" | "image/gif" | "image/webp",
835
- data: item.data,
836
- },
837
- };
838
- }
839
- });
840
- let filteredBlocks = !model?.input.includes("image") ? blocks.filter((b) => b.type !== "image") : blocks;
841
- filteredBlocks = filteredBlocks.filter((b) => {
842
- if (b.type === "text") {
843
- return b.text.trim().length > 0;
844
- }
845
- return true;
846
- });
847
- if (filteredBlocks.length === 0) continue;
848
- params.push({
849
- role: "user",
850
- content: filteredBlocks,
851
- });
852
- }
853
- } else if (msg.role === "assistant") {
854
- const blocks: ContentBlockParam[] = [];
855
-
856
- for (const block of msg.content) {
857
- if (block.type === "text") {
858
- if (block.text.trim().length === 0) continue;
859
- blocks.push({
860
- type: "text",
861
- text: sanitizeSurrogates(block.text),
862
- });
863
- } else if (block.type === "thinking") {
864
- // Redacted thinking: pass the opaque payload back as redacted_thinking
865
- if (block.redacted) {
866
- blocks.push({
867
- type: "redacted_thinking",
868
- data: block.thinkingSignature!,
869
- });
870
- continue;
871
- }
872
- if (block.thinking.trim().length === 0) continue;
873
- // If thinking signature is missing/empty (e.g., from aborted stream),
874
- // convert to plain text block without <thinking> tags to avoid API rejection
875
- // and prevent Claude from mimicking the tags in responses
876
- if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
877
- blocks.push({
878
- type: "text",
879
- text: sanitizeSurrogates(block.thinking),
880
- });
881
- } else {
882
- blocks.push({
883
- type: "thinking",
884
- thinking: sanitizeSurrogates(block.thinking),
885
- signature: block.thinkingSignature,
886
- });
887
- }
888
- } else if (block.type === "toolCall") {
889
- blocks.push({
890
- type: "tool_use",
891
- id: block.id,
892
- name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
893
- input: block.arguments ?? {},
894
- });
895
- } else if (block.type === "serverToolUse") {
896
- blocks.push({
897
- type: "server_tool_use",
898
- id: block.id,
899
- name: block.name,
900
- input: block.input ?? {},
901
- } as any);
902
- } else if (block.type === "webSearchResult") {
903
- blocks.push({
904
- type: "web_search_tool_result",
905
- tool_use_id: block.toolUseId,
906
- content: block.content,
907
- } as any);
908
- }
909
- }
910
- if (blocks.length === 0) continue;
911
- params.push({
912
- role: "assistant",
913
- content: blocks,
914
- });
915
- } else if (msg.role === "toolResult") {
916
- // Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint
917
- const toolResults: ContentBlockParam[] = [];
918
-
919
- // Add the current tool result
920
- toolResults.push({
921
- type: "tool_result",
922
- tool_use_id: msg.toolCallId,
923
- content: convertContentBlocks(msg.content),
924
- is_error: msg.isError,
925
- });
926
-
927
- // Look ahead for consecutive toolResult messages
928
- let j = i + 1;
929
- while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
930
- const nextMsg = transformedMessages[j] as ToolResultMessage; // We know it's a toolResult
931
- toolResults.push({
932
- type: "tool_result",
933
- tool_use_id: nextMsg.toolCallId,
934
- content: convertContentBlocks(nextMsg.content),
935
- is_error: nextMsg.isError,
936
- });
937
- j++;
938
- }
939
-
940
- // Skip the messages we've already processed
941
- i = j - 1;
942
-
943
- // Add a single user message with all tool results
944
- params.push({
945
- role: "user",
946
- content: toolResults,
947
- });
948
- }
187
+ context: Context,
188
+ options?: SimpleStreamOptions,
189
+ ): AssistantMessageEventStream => {
190
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider);
191
+ if (!apiKey) {
192
+ throw new Error(`No API key for provider: ${model.provider}`);
949
193
  }
950
194
 
951
- // Add cache_control to the last user message to cache conversation history
952
- if (cacheControl && params.length > 0) {
953
- const lastMessage = params[params.length - 1];
954
- if (lastMessage.role === "user") {
955
- if (Array.isArray(lastMessage.content)) {
956
- const lastBlock = lastMessage.content[lastMessage.content.length - 1];
957
- if (
958
- lastBlock &&
959
- (lastBlock.type === "text" || lastBlock.type === "image" || lastBlock.type === "tool_result")
960
- ) {
961
- (lastBlock as any).cache_control = cacheControl;
962
- }
963
- } else if (typeof lastMessage.content === "string") {
964
- lastMessage.content = [
965
- {
966
- type: "text",
967
- text: lastMessage.content,
968
- cache_control: cacheControl,
969
- },
970
- ] as any;
971
- }
972
- }
195
+ const base = buildBaseOptions(model, options, apiKey);
196
+ if (!options?.reasoning) {
197
+ return streamAnthropic(model, context, { ...base, thinkingEnabled: false } satisfies AnthropicOptions);
973
198
  }
974
199
 
975
- return params;
976
- }
977
-
978
- function convertTools(tools: Tool[], isOAuthToken: boolean): Anthropic.Messages.Tool[] {
979
- if (!tools) return [];
980
-
981
- return tools.map((tool) => {
982
- const jsonSchema = tool.parameters as any; // TypeBox already generates JSON Schema
200
+ // For Opus 4.6 and Sonnet 4.6: use adaptive thinking with effort level
201
+ // For older models: use budget-based thinking
202
+ if (supportsAdaptiveThinking(model.id)) {
203
+ const effort = mapThinkingLevelToEffort(options.reasoning, model.id);
204
+ return streamAnthropic(model, context, {
205
+ ...base,
206
+ thinkingEnabled: true,
207
+ effort,
208
+ } satisfies AnthropicOptions);
209
+ }
983
210
 
984
- return {
985
- name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name,
986
- description: tool.description,
987
- input_schema: {
988
- type: "object" as const,
989
- properties: jsonSchema.properties || {},
990
- required: jsonSchema.required || [],
991
- },
992
- };
993
- });
994
- }
211
+ const adjusted = adjustMaxTokensForThinking(
212
+ base.maxTokens || 0,
213
+ model.maxTokens,
214
+ options.reasoning,
215
+ options.thinkingBudgets,
216
+ );
995
217
 
996
- function mapStopReason(reason: Anthropic.Messages.StopReason | string): StopReason {
997
- switch (reason) {
998
- case "end_turn":
999
- return "stop";
1000
- case "max_tokens":
1001
- return "length";
1002
- case "tool_use":
1003
- return "toolUse";
1004
- case "refusal":
1005
- return "error";
1006
- case "pause_turn": // Stop is good enough -> resubmit
1007
- return "stop";
1008
- case "stop_sequence":
1009
- return "stop"; // We don't supply stop sequences, so this should never happen
1010
- case "sensitive": // Content flagged by safety filters (not yet in SDK types)
1011
- return "error";
1012
- default:
1013
- // Handle unknown stop reasons gracefully (API may add new values)
1014
- throw new Error(`Unhandled stop reason: ${reason}`);
1015
- }
1016
- }
218
+ return streamAnthropic(model, context, {
219
+ ...base,
220
+ maxTokens: adjusted.maxTokens,
221
+ thinkingEnabled: true,
222
+ thinkingBudgetTokens: adjusted.thinkingBudget,
223
+ } satisfies AnthropicOptions);
224
+ };