gsd-pi 2.37.1 → 2.38.0-dev.eeb3520

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 (72) hide show
  1. package/README.md +1 -1
  2. package/dist/onboarding.js +1 -0
  3. package/dist/resources/extensions/gsd/auto-dispatch.js +67 -1
  4. package/dist/resources/extensions/gsd/auto-post-unit.js +14 -0
  5. package/dist/resources/extensions/gsd/auto-prompts.js +91 -2
  6. package/dist/resources/extensions/gsd/auto-recovery.js +37 -1
  7. package/dist/resources/extensions/gsd/doctor-providers.js +35 -1
  8. package/dist/resources/extensions/gsd/files.js +41 -0
  9. package/dist/resources/extensions/gsd/observability-validator.js +24 -0
  10. package/dist/resources/extensions/gsd/preferences-types.js +2 -1
  11. package/dist/resources/extensions/gsd/preferences-validation.js +42 -0
  12. package/dist/resources/extensions/gsd/prompts/plan-slice.md +2 -1
  13. package/dist/resources/extensions/gsd/prompts/reactive-execute.md +41 -0
  14. package/dist/resources/extensions/gsd/reactive-graph.js +227 -0
  15. package/dist/resources/extensions/gsd/templates/task-plan.md +11 -3
  16. package/package.json +2 -1
  17. package/packages/pi-ai/dist/env-api-keys.js +13 -0
  18. package/packages/pi-ai/dist/env-api-keys.js.map +1 -1
  19. package/packages/pi-ai/dist/models.generated.d.ts +172 -0
  20. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  21. package/packages/pi-ai/dist/models.generated.js +172 -0
  22. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  23. package/packages/pi-ai/dist/providers/anthropic-shared.d.ts +64 -0
  24. package/packages/pi-ai/dist/providers/anthropic-shared.d.ts.map +1 -0
  25. package/packages/pi-ai/dist/providers/anthropic-shared.js +668 -0
  26. package/packages/pi-ai/dist/providers/anthropic-shared.js.map +1 -0
  27. package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts +5 -0
  28. package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts.map +1 -0
  29. package/packages/pi-ai/dist/providers/anthropic-vertex.js +85 -0
  30. package/packages/pi-ai/dist/providers/anthropic-vertex.js.map +1 -0
  31. package/packages/pi-ai/dist/providers/anthropic.d.ts +4 -30
  32. package/packages/pi-ai/dist/providers/anthropic.d.ts.map +1 -1
  33. package/packages/pi-ai/dist/providers/anthropic.js +47 -764
  34. package/packages/pi-ai/dist/providers/anthropic.js.map +1 -1
  35. package/packages/pi-ai/dist/providers/register-builtins.d.ts.map +1 -1
  36. package/packages/pi-ai/dist/providers/register-builtins.js +6 -0
  37. package/packages/pi-ai/dist/providers/register-builtins.js.map +1 -1
  38. package/packages/pi-ai/dist/types.d.ts +2 -2
  39. package/packages/pi-ai/dist/types.d.ts.map +1 -1
  40. package/packages/pi-ai/dist/types.js.map +1 -1
  41. package/packages/pi-ai/package.json +1 -0
  42. package/packages/pi-ai/src/env-api-keys.ts +14 -0
  43. package/packages/pi-ai/src/models.generated.ts +172 -0
  44. package/packages/pi-ai/src/providers/anthropic-shared.ts +761 -0
  45. package/packages/pi-ai/src/providers/anthropic-vertex.ts +130 -0
  46. package/packages/pi-ai/src/providers/anthropic.ts +76 -868
  47. package/packages/pi-ai/src/providers/register-builtins.ts +7 -0
  48. package/packages/pi-ai/src/types.ts +2 -0
  49. package/packages/pi-coding-agent/dist/core/model-resolver.d.ts.map +1 -1
  50. package/packages/pi-coding-agent/dist/core/model-resolver.js +1 -0
  51. package/packages/pi-coding-agent/dist/core/model-resolver.js.map +1 -1
  52. package/packages/pi-coding-agent/package.json +1 -1
  53. package/packages/pi-coding-agent/src/core/model-resolver.ts +1 -0
  54. package/pkg/package.json +1 -1
  55. package/src/resources/extensions/gsd/auto-dispatch.ts +93 -0
  56. package/src/resources/extensions/gsd/auto-post-unit.ts +14 -0
  57. package/src/resources/extensions/gsd/auto-prompts.ts +125 -3
  58. package/src/resources/extensions/gsd/auto-recovery.ts +42 -0
  59. package/src/resources/extensions/gsd/doctor-providers.ts +38 -1
  60. package/src/resources/extensions/gsd/files.ts +45 -0
  61. package/src/resources/extensions/gsd/observability-validator.ts +27 -0
  62. package/src/resources/extensions/gsd/preferences-types.ts +5 -1
  63. package/src/resources/extensions/gsd/preferences-validation.ts +41 -0
  64. package/src/resources/extensions/gsd/prompts/plan-slice.md +2 -1
  65. package/src/resources/extensions/gsd/prompts/reactive-execute.md +41 -0
  66. package/src/resources/extensions/gsd/reactive-graph.ts +289 -0
  67. package/src/resources/extensions/gsd/templates/task-plan.md +11 -3
  68. package/src/resources/extensions/gsd/tests/doctor-providers.test.ts +108 -3
  69. package/src/resources/extensions/gsd/tests/plan-quality-validator.test.ts +111 -0
  70. package/src/resources/extensions/gsd/tests/reactive-executor.test.ts +511 -0
  71. package/src/resources/extensions/gsd/tests/reactive-graph.test.ts +299 -0
  72. package/src/resources/extensions/gsd/types.ts +43 -0
@@ -1,11 +1,9 @@
1
1
  import { getEnvApiKey } from "../env-api-keys.js";
2
- import { calculateCost } from "../models.js";
3
2
  import { AssistantMessageEventStream } from "../utils/event-stream.js";
4
- import { parseStreamingJson } from "../utils/json-parse.js";
5
- import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
6
3
  import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
7
4
  import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.js";
8
- import { transformMessages } from "./transform-messages.js";
5
+ import { extractRetryAfterMs, mapThinkingLevelToEffort, processAnthropicStream, supportsAdaptiveThinking, } from "./anthropic-shared.js";
6
+ export { extractRetryAfterMs };
9
7
  let _AnthropicClass;
10
8
  async function getAnthropicClass() {
11
9
  if (!_AnthropicClass) {
@@ -14,102 +12,8 @@ async function getAnthropicClass() {
14
12
  }
15
13
  return _AnthropicClass;
16
14
  }
17
- /**
18
- * Resolve cache retention preference.
19
- * Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
20
- */
21
- function resolveCacheRetention(cacheRetention) {
22
- if (cacheRetention) {
23
- return cacheRetention;
24
- }
25
- if (typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "long") {
26
- return "long";
27
- }
28
- return "short";
29
- }
30
- function getCacheControl(baseUrl, cacheRetention) {
31
- const retention = resolveCacheRetention(cacheRetention);
32
- if (retention === "none") {
33
- return { retention };
34
- }
35
- const ttl = retention === "long" && baseUrl.includes("api.anthropic.com") ? "1h" : undefined;
36
- return {
37
- retention,
38
- cacheControl: { type: "ephemeral", ...(ttl && { ttl }) },
39
- };
40
- }
41
15
  // Stealth mode: Mimic Claude Code's tool naming exactly
42
16
  const claudeCodeVersion = "2.1.62";
43
- // Claude Code 2.x tool names (canonical casing)
44
- // Source: https://cchistory.mariozechner.at/data/prompts-2.1.11.md
45
- // To update: https://github.com/badlogic/cchistory
46
- const claudeCodeTools = [
47
- "Read",
48
- "Write",
49
- "Edit",
50
- "Bash",
51
- "Grep",
52
- "Glob",
53
- "AskUserQuestion",
54
- "EnterPlanMode",
55
- "ExitPlanMode",
56
- "KillShell",
57
- "NotebookEdit",
58
- "Skill",
59
- "Task",
60
- "TaskOutput",
61
- "TodoWrite",
62
- "WebFetch",
63
- "WebSearch",
64
- ];
65
- const ccToolLookup = new Map(claudeCodeTools.map((t) => [t.toLowerCase(), t]));
66
- // Convert tool name to CC canonical casing if it matches (case-insensitive)
67
- const toClaudeCodeName = (name) => ccToolLookup.get(name.toLowerCase()) ?? name;
68
- const fromClaudeCodeName = (name, tools) => {
69
- if (tools && tools.length > 0) {
70
- const lowerName = name.toLowerCase();
71
- const matchedTool = tools.find((tool) => tool.name.toLowerCase() === lowerName);
72
- if (matchedTool)
73
- return matchedTool.name;
74
- }
75
- return name;
76
- };
77
- /**
78
- * Convert content blocks to Anthropic API format
79
- */
80
- function convertContentBlocks(content) {
81
- // If only text blocks, return as concatenated string for simplicity
82
- const hasImages = content.some((c) => c.type === "image");
83
- if (!hasImages) {
84
- return sanitizeSurrogates(content.map((c) => c.text).join("\n"));
85
- }
86
- // If we have images, convert to content block array
87
- const blocks = content.map((block) => {
88
- if (block.type === "text") {
89
- return {
90
- type: "text",
91
- text: sanitizeSurrogates(block.text),
92
- };
93
- }
94
- return {
95
- type: "image",
96
- source: {
97
- type: "base64",
98
- media_type: block.mimeType,
99
- data: block.data,
100
- },
101
- };
102
- });
103
- // If only images (no text), add placeholder text block
104
- const hasText = blocks.some((b) => b.type === "text");
105
- if (!hasText) {
106
- blocks.unshift({
107
- type: "text",
108
- text: "(see attached image)",
109
- });
110
- }
111
- return blocks;
112
- }
113
17
  function mergeHeaders(...headerSources) {
114
18
  const merged = {};
115
19
  for (const headers of headerSources) {
@@ -119,383 +23,6 @@ function mergeHeaders(...headerSources) {
119
23
  }
120
24
  return merged;
121
25
  }
122
- /**
123
- * Detect transient network errors that are likely to succeed on retry.
124
- * Covers WebSocket disconnects (Tailscale, VPN), TCP resets, and DNS failures.
125
- */
126
- function isTransientNetworkError(error) {
127
- if (!(error instanceof Error))
128
- return false;
129
- const msg = error.message.toLowerCase();
130
- const code = error.code;
131
- return (code === 'ECONNRESET' ||
132
- code === 'EPIPE' ||
133
- code === 'ETIMEDOUT' ||
134
- code === 'ENOTFOUND' ||
135
- code === 'EAI_AGAIN' ||
136
- msg.includes('connector_closed') ||
137
- msg.includes('socket hang up') ||
138
- msg.includes('network') ||
139
- msg.includes('connection') && msg.includes('closed') ||
140
- msg.includes('fetch failed'));
141
- }
142
- /**
143
- * Extract retry delay from Anthropic error response headers (in milliseconds).
144
- * Checks: retry-after (seconds or RFC date), x-ratelimit-reset-requests, x-ratelimit-reset-tokens.
145
- * Returns undefined if no valid delay is found or if the delay is in the past.
146
- */
147
- function extractRetryAfterMs(headers, errorText = "") {
148
- const normalizeDelay = (ms) => (ms > 0 ? Math.ceil(ms + 1000) : undefined);
149
- const retryAfter = headers.get("retry-after");
150
- if (retryAfter) {
151
- const seconds = Number(retryAfter);
152
- if (Number.isFinite(seconds)) {
153
- const delay = normalizeDelay(seconds * 1000);
154
- if (delay !== undefined)
155
- return delay;
156
- }
157
- const asDate = new Date(retryAfter).getTime();
158
- if (!Number.isNaN(asDate)) {
159
- const delay = normalizeDelay(asDate - Date.now());
160
- if (delay !== undefined)
161
- return delay;
162
- }
163
- }
164
- // x-ratelimit-reset-requests / x-ratelimit-reset-tokens are Unix timestamps (seconds)
165
- for (const header of ["x-ratelimit-reset-requests", "x-ratelimit-reset-tokens"]) {
166
- const value = headers.get(header);
167
- if (value) {
168
- const resetSeconds = Number(value);
169
- if (Number.isFinite(resetSeconds)) {
170
- const delay = normalizeDelay(resetSeconds * 1000 - Date.now());
171
- if (delay !== undefined)
172
- return delay;
173
- }
174
- }
175
- }
176
- return undefined;
177
- }
178
- export const streamAnthropic = (model, context, options) => {
179
- const stream = new AssistantMessageEventStream();
180
- (async () => {
181
- const output = {
182
- role: "assistant",
183
- content: [],
184
- api: model.api,
185
- provider: model.provider,
186
- model: model.id,
187
- usage: {
188
- input: 0,
189
- output: 0,
190
- cacheRead: 0,
191
- cacheWrite: 0,
192
- totalTokens: 0,
193
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
194
- },
195
- stopReason: "stop",
196
- timestamp: Date.now(),
197
- };
198
- try {
199
- const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
200
- let copilotDynamicHeaders;
201
- if (model.provider === "github-copilot") {
202
- const hasImages = hasCopilotVisionInput(context.messages);
203
- copilotDynamicHeaders = buildCopilotDynamicHeaders({
204
- messages: context.messages,
205
- hasImages,
206
- });
207
- }
208
- const { client, isOAuthToken } = await createClient(model, apiKey, options?.interleavedThinking ?? true, options?.headers, copilotDynamicHeaders);
209
- let params = buildParams(model, context, isOAuthToken, options);
210
- const nextParams = await options?.onPayload?.(params, model);
211
- if (nextParams !== undefined) {
212
- params = nextParams;
213
- }
214
- const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });
215
- stream.push({ type: "start", partial: output });
216
- const blocks = output.content;
217
- for await (const event of anthropicStream) {
218
- if (event.type === "message_start") {
219
- // Capture initial token usage from message_start event
220
- // This ensures we have input token counts even if the stream is aborted early
221
- output.usage.input = event.message.usage.input_tokens || 0;
222
- output.usage.output = event.message.usage.output_tokens || 0;
223
- output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
224
- output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
225
- // Anthropic doesn't provide total_tokens, compute from components
226
- output.usage.totalTokens =
227
- output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
228
- calculateCost(model, output.usage);
229
- }
230
- else if (event.type === "content_block_start") {
231
- if (event.content_block.type === "text") {
232
- const block = {
233
- type: "text",
234
- text: "",
235
- index: event.index,
236
- };
237
- output.content.push(block);
238
- stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
239
- }
240
- else if (event.content_block.type === "thinking") {
241
- const block = {
242
- type: "thinking",
243
- thinking: "",
244
- thinkingSignature: "",
245
- index: event.index,
246
- };
247
- output.content.push(block);
248
- stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
249
- }
250
- else if (event.content_block.type === "redacted_thinking") {
251
- const block = {
252
- type: "thinking",
253
- thinking: "[Reasoning redacted]",
254
- thinkingSignature: event.content_block.data,
255
- redacted: true,
256
- index: event.index,
257
- };
258
- output.content.push(block);
259
- stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
260
- }
261
- else if (event.content_block.type === "tool_use") {
262
- const block = {
263
- type: "toolCall",
264
- id: event.content_block.id,
265
- name: isOAuthToken
266
- ? fromClaudeCodeName(event.content_block.name, context.tools)
267
- : event.content_block.name,
268
- arguments: event.content_block.input ?? {},
269
- partialJson: "",
270
- index: event.index,
271
- };
272
- output.content.push(block);
273
- stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
274
- }
275
- else if (event.content_block.type === "server_tool_use") {
276
- const serverBlock = event.content_block;
277
- const block = {
278
- type: "serverToolUse",
279
- id: serverBlock.id,
280
- name: serverBlock.name,
281
- input: serverBlock.input,
282
- index: event.index,
283
- };
284
- output.content.push(block);
285
- stream.push({ type: "server_tool_use", contentIndex: output.content.length - 1, partial: output });
286
- }
287
- else if (event.content_block.type === "web_search_tool_result") {
288
- const resultBlock = event.content_block;
289
- const block = {
290
- type: "webSearchResult",
291
- toolUseId: resultBlock.tool_use_id,
292
- content: resultBlock.content,
293
- index: event.index,
294
- };
295
- output.content.push(block);
296
- stream.push({ type: "web_search_result", contentIndex: output.content.length - 1, partial: output });
297
- }
298
- }
299
- else if (event.type === "content_block_delta") {
300
- if (event.delta.type === "text_delta") {
301
- const index = blocks.findIndex((b) => b.index === event.index);
302
- const block = blocks[index];
303
- if (block && block.type === "text") {
304
- block.text += event.delta.text;
305
- stream.push({
306
- type: "text_delta",
307
- contentIndex: index,
308
- delta: event.delta.text,
309
- partial: output,
310
- });
311
- }
312
- }
313
- else if (event.delta.type === "thinking_delta") {
314
- const index = blocks.findIndex((b) => b.index === event.index);
315
- const block = blocks[index];
316
- if (block && block.type === "thinking") {
317
- block.thinking += event.delta.thinking;
318
- stream.push({
319
- type: "thinking_delta",
320
- contentIndex: index,
321
- delta: event.delta.thinking,
322
- partial: output,
323
- });
324
- }
325
- }
326
- else if (event.delta.type === "input_json_delta") {
327
- const index = blocks.findIndex((b) => b.index === event.index);
328
- const block = blocks[index];
329
- if (block && block.type === "toolCall") {
330
- block.partialJson += event.delta.partial_json;
331
- block.arguments = parseStreamingJson(block.partialJson);
332
- stream.push({
333
- type: "toolcall_delta",
334
- contentIndex: index,
335
- delta: event.delta.partial_json,
336
- partial: output,
337
- });
338
- }
339
- }
340
- else if (event.delta.type === "signature_delta") {
341
- const index = blocks.findIndex((b) => b.index === event.index);
342
- const block = blocks[index];
343
- if (block && block.type === "thinking") {
344
- block.thinkingSignature = block.thinkingSignature || "";
345
- block.thinkingSignature += event.delta.signature;
346
- }
347
- }
348
- }
349
- else if (event.type === "content_block_stop") {
350
- const index = blocks.findIndex((b) => b.index === event.index);
351
- const block = blocks[index];
352
- if (block) {
353
- delete block.index;
354
- if (block.type === "text") {
355
- stream.push({
356
- type: "text_end",
357
- contentIndex: index,
358
- content: block.text,
359
- partial: output,
360
- });
361
- }
362
- else if (block.type === "thinking") {
363
- stream.push({
364
- type: "thinking_end",
365
- contentIndex: index,
366
- content: block.thinking,
367
- partial: output,
368
- });
369
- }
370
- else if (block.type === "toolCall") {
371
- block.arguments = parseStreamingJson(block.partialJson);
372
- delete block.partialJson;
373
- stream.push({
374
- type: "toolcall_end",
375
- contentIndex: index,
376
- toolCall: block,
377
- partial: output,
378
- });
379
- }
380
- // serverToolUse and webSearchResult blocks just need index cleanup (already emitted on start)
381
- }
382
- }
383
- else if (event.type === "message_delta") {
384
- if (event.delta.stop_reason) {
385
- output.stopReason = mapStopReason(event.delta.stop_reason);
386
- }
387
- // Only update usage fields if present (not null).
388
- // Preserves input_tokens from message_start when proxies omit it in message_delta.
389
- if (event.usage.input_tokens != null) {
390
- output.usage.input = event.usage.input_tokens;
391
- }
392
- if (event.usage.output_tokens != null) {
393
- output.usage.output = event.usage.output_tokens;
394
- }
395
- if (event.usage.cache_read_input_tokens != null) {
396
- output.usage.cacheRead = event.usage.cache_read_input_tokens;
397
- }
398
- if (event.usage.cache_creation_input_tokens != null) {
399
- output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
400
- }
401
- // Anthropic doesn't provide total_tokens, compute from components
402
- output.usage.totalTokens =
403
- output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
404
- calculateCost(model, output.usage);
405
- }
406
- }
407
- if (options?.signal?.aborted) {
408
- throw new Error("Request was aborted");
409
- }
410
- if (output.stopReason === "aborted" || output.stopReason === "error") {
411
- throw new Error("An unknown error occurred");
412
- }
413
- stream.push({ type: "done", reason: output.stopReason, message: output });
414
- stream.end();
415
- }
416
- catch (error) {
417
- for (const block of output.content)
418
- delete block.index;
419
- output.stopReason = options?.signal?.aborted ? "aborted" : "error";
420
- output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
421
- if (model.provider === "alibaba-coding-plan") {
422
- output.errorMessage = `[alibaba-coding-plan] ${output.errorMessage}`;
423
- }
424
- const AnthropicSdk = _AnthropicClass;
425
- if (AnthropicSdk && error instanceof AnthropicSdk.APIError && error.headers) {
426
- const retryAfterMs = extractRetryAfterMs(error.headers, error.message);
427
- if (retryAfterMs !== undefined) {
428
- output.retryAfterMs = retryAfterMs;
429
- }
430
- }
431
- // Mark transient network errors as retriable so auto-mode can
432
- // detect them and retry instead of stopping (#833).
433
- if (isTransientNetworkError(error)) {
434
- output.retryAfterMs = output.retryAfterMs ?? 5000;
435
- }
436
- stream.push({ type: "error", reason: output.stopReason, error: output });
437
- stream.end();
438
- }
439
- })();
440
- return stream;
441
- };
442
- /**
443
- * Check if a model supports adaptive thinking (Opus 4.6 and Sonnet 4.6)
444
- */
445
- function supportsAdaptiveThinking(modelId) {
446
- // Opus 4.6 and Sonnet 4.6 model IDs (with or without date suffix)
447
- return (modelId.includes("opus-4-6") ||
448
- modelId.includes("opus-4.6") ||
449
- modelId.includes("sonnet-4-6") ||
450
- modelId.includes("sonnet-4.6"));
451
- }
452
- /**
453
- * Map ThinkingLevel to Anthropic effort levels for adaptive thinking.
454
- * Note: effort "max" is only valid on Opus 4.6.
455
- */
456
- function mapThinkingLevelToEffort(level, modelId) {
457
- switch (level) {
458
- case "minimal":
459
- return "low";
460
- case "low":
461
- return "low";
462
- case "medium":
463
- return "medium";
464
- case "high":
465
- return "high";
466
- case "xhigh":
467
- return modelId.includes("opus-4-6") || modelId.includes("opus-4.6") ? "max" : "high";
468
- default:
469
- return "high";
470
- }
471
- }
472
- export const streamSimpleAnthropic = (model, context, options) => {
473
- const apiKey = options?.apiKey || getEnvApiKey(model.provider);
474
- if (!apiKey) {
475
- throw new Error(`No API key for provider: ${model.provider}`);
476
- }
477
- const base = buildBaseOptions(model, options, apiKey);
478
- if (!options?.reasoning) {
479
- return streamAnthropic(model, context, { ...base, thinkingEnabled: false });
480
- }
481
- // For Opus 4.6 and Sonnet 4.6: use adaptive thinking with effort level
482
- // For older models: use budget-based thinking
483
- if (supportsAdaptiveThinking(model.id)) {
484
- const effort = mapThinkingLevelToEffort(options.reasoning, model.id);
485
- return streamAnthropic(model, context, {
486
- ...base,
487
- thinkingEnabled: true,
488
- effort,
489
- });
490
- }
491
- const adjusted = adjustMaxTokensForThinking(base.maxTokens || 0, model.maxTokens, options.reasoning, options.thinkingBudgets);
492
- return streamAnthropic(model, context, {
493
- ...base,
494
- maxTokens: adjusted.maxTokens,
495
- thinkingEnabled: true,
496
- thinkingBudgetTokens: adjusted.thinkingBudget,
497
- });
498
- };
499
26
  function isOAuthToken(apiKey) {
500
27
  return apiKey.includes("sk-ant-oat");
501
28
  }
@@ -562,299 +89,55 @@ async function createClient(model, apiKey, interleavedThinking, optionsHeaders,
562
89
  });
563
90
  return { client, isOAuthToken: false };
564
91
  }
565
- function buildParams(model, context, isOAuthToken, options) {
566
- const { cacheControl } = getCacheControl(model.baseUrl, options?.cacheRetention);
567
- // Strip variant suffixes like [1m] from model ID before sending to the API.
568
- // The API only accepts the base model ID (e.g. "claude-opus-4-6"),
569
- // not internal variant identifiers (e.g. "claude-opus-4-6[1m]").
570
- // This applies to all auth methods — API keys, OAuth, and Copilot alike.
571
- const apiModelId = model.id.replace(/\[.*\]$/, "");
572
- const params = {
573
- model: apiModelId,
574
- messages: convertMessages(context.messages, model, isOAuthToken, cacheControl),
575
- max_tokens: options?.maxTokens || (model.maxTokens / 3) | 0,
576
- stream: true,
577
- };
578
- // For OAuth tokens, we MUST include Claude Code identity
579
- if (isOAuthToken) {
580
- params.system = [
581
- {
582
- type: "text",
583
- text: "You are Claude Code, Anthropic's official CLI for Claude.",
584
- ...(cacheControl ? { cache_control: cacheControl } : {}),
585
- },
586
- ];
587
- if (context.systemPrompt) {
588
- params.system.push({
589
- type: "text",
590
- text: sanitizeSurrogates(context.systemPrompt),
591
- ...(cacheControl ? { cache_control: cacheControl } : {}),
92
+ export const streamAnthropic = (model, context, options) => {
93
+ const stream = new AssistantMessageEventStream();
94
+ (async () => {
95
+ const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
96
+ let copilotDynamicHeaders;
97
+ if (model.provider === "github-copilot") {
98
+ const hasImages = hasCopilotVisionInput(context.messages);
99
+ copilotDynamicHeaders = buildCopilotDynamicHeaders({
100
+ messages: context.messages,
101
+ hasImages,
592
102
  });
593
103
  }
104
+ const { client, isOAuthToken: isOAuth } = await createClient(model, apiKey, options?.interleavedThinking ?? true, options?.headers, copilotDynamicHeaders);
105
+ processAnthropicStream(stream, {
106
+ client,
107
+ model,
108
+ context,
109
+ isOAuthToken: isOAuth,
110
+ options,
111
+ AnthropicSdkClass: _AnthropicClass,
112
+ });
113
+ })();
114
+ return stream;
115
+ };
116
+ export const streamSimpleAnthropic = (model, context, options) => {
117
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider);
118
+ if (!apiKey) {
119
+ throw new Error(`No API key for provider: ${model.provider}`);
594
120
  }
595
- else if (context.systemPrompt) {
596
- // Add cache control to system prompt for non-OAuth tokens
597
- params.system = [
598
- {
599
- type: "text",
600
- text: sanitizeSurrogates(context.systemPrompt),
601
- ...(cacheControl ? { cache_control: cacheControl } : {}),
602
- },
603
- ];
604
- }
605
- // Temperature is incompatible with extended thinking (adaptive or budget-based).
606
- if (options?.temperature !== undefined && !options?.thinkingEnabled) {
607
- params.temperature = options.temperature;
608
- }
609
- if (context.tools) {
610
- params.tools = convertTools(context.tools, isOAuthToken);
611
- }
612
- // Configure thinking mode: adaptive (Opus 4.6 and Sonnet 4.6) or budget-based (older models)
613
- if (options?.thinkingEnabled && model.reasoning) {
614
- if (supportsAdaptiveThinking(model.id)) {
615
- // Adaptive thinking: Claude decides when and how much to think
616
- params.thinking = { type: "adaptive" };
617
- if (options.effort) {
618
- params.output_config = { effort: options.effort };
619
- }
620
- }
621
- else {
622
- // Budget-based thinking for older models
623
- params.thinking = {
624
- type: "enabled",
625
- budget_tokens: options.thinkingBudgetTokens || 1024,
626
- };
627
- }
628
- }
629
- if (options?.metadata) {
630
- const userId = options.metadata.user_id;
631
- if (typeof userId === "string") {
632
- params.metadata = { user_id: userId };
633
- }
634
- }
635
- if (options?.toolChoice) {
636
- if (typeof options.toolChoice === "string") {
637
- params.tool_choice = { type: options.toolChoice };
638
- }
639
- else {
640
- params.tool_choice = options.toolChoice;
641
- }
642
- }
643
- return params;
644
- }
645
- // Normalize tool call IDs to match Anthropic's required pattern and length
646
- function normalizeToolCallId(id) {
647
- return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
648
- }
649
- function convertMessages(messages, model, isOAuthToken, cacheControl) {
650
- const params = [];
651
- // Transform messages for cross-provider compatibility
652
- const transformedMessages = transformMessages(messages, model, normalizeToolCallId);
653
- for (let i = 0; i < transformedMessages.length; i++) {
654
- const msg = transformedMessages[i];
655
- if (msg.role === "user") {
656
- if (typeof msg.content === "string") {
657
- if (msg.content.trim().length > 0) {
658
- params.push({
659
- role: "user",
660
- content: sanitizeSurrogates(msg.content),
661
- });
662
- }
663
- }
664
- else {
665
- const blocks = msg.content.map((item) => {
666
- if (item.type === "text") {
667
- return {
668
- type: "text",
669
- text: sanitizeSurrogates(item.text),
670
- };
671
- }
672
- else {
673
- return {
674
- type: "image",
675
- source: {
676
- type: "base64",
677
- media_type: item.mimeType,
678
- data: item.data,
679
- },
680
- };
681
- }
682
- });
683
- let filteredBlocks = !model?.input.includes("image") ? blocks.filter((b) => b.type !== "image") : blocks;
684
- filteredBlocks = filteredBlocks.filter((b) => {
685
- if (b.type === "text") {
686
- return b.text.trim().length > 0;
687
- }
688
- return true;
689
- });
690
- if (filteredBlocks.length === 0)
691
- continue;
692
- params.push({
693
- role: "user",
694
- content: filteredBlocks,
695
- });
696
- }
697
- }
698
- else if (msg.role === "assistant") {
699
- const blocks = [];
700
- for (const block of msg.content) {
701
- if (block.type === "text") {
702
- if (block.text.trim().length === 0)
703
- continue;
704
- blocks.push({
705
- type: "text",
706
- text: sanitizeSurrogates(block.text),
707
- });
708
- }
709
- else if (block.type === "thinking") {
710
- // Redacted thinking: pass the opaque payload back as redacted_thinking
711
- if (block.redacted) {
712
- blocks.push({
713
- type: "redacted_thinking",
714
- data: block.thinkingSignature,
715
- });
716
- continue;
717
- }
718
- if (block.thinking.trim().length === 0)
719
- continue;
720
- // If thinking signature is missing/empty (e.g., from aborted stream),
721
- // convert to plain text block without <thinking> tags to avoid API rejection
722
- // and prevent Claude from mimicking the tags in responses
723
- if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
724
- blocks.push({
725
- type: "text",
726
- text: sanitizeSurrogates(block.thinking),
727
- });
728
- }
729
- else {
730
- blocks.push({
731
- type: "thinking",
732
- thinking: sanitizeSurrogates(block.thinking),
733
- signature: block.thinkingSignature,
734
- });
735
- }
736
- }
737
- else if (block.type === "toolCall") {
738
- blocks.push({
739
- type: "tool_use",
740
- id: block.id,
741
- name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
742
- input: block.arguments ?? {},
743
- });
744
- }
745
- else if (block.type === "serverToolUse") {
746
- blocks.push({
747
- type: "server_tool_use",
748
- id: block.id,
749
- name: block.name,
750
- input: block.input ?? {},
751
- });
752
- }
753
- else if (block.type === "webSearchResult") {
754
- blocks.push({
755
- type: "web_search_tool_result",
756
- tool_use_id: block.toolUseId,
757
- content: block.content,
758
- });
759
- }
760
- }
761
- if (blocks.length === 0)
762
- continue;
763
- params.push({
764
- role: "assistant",
765
- content: blocks,
766
- });
767
- }
768
- else if (msg.role === "toolResult") {
769
- // Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint
770
- const toolResults = [];
771
- // Add the current tool result
772
- toolResults.push({
773
- type: "tool_result",
774
- tool_use_id: msg.toolCallId,
775
- content: convertContentBlocks(msg.content),
776
- is_error: msg.isError,
777
- });
778
- // Look ahead for consecutive toolResult messages
779
- let j = i + 1;
780
- while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
781
- const nextMsg = transformedMessages[j]; // We know it's a toolResult
782
- toolResults.push({
783
- type: "tool_result",
784
- tool_use_id: nextMsg.toolCallId,
785
- content: convertContentBlocks(nextMsg.content),
786
- is_error: nextMsg.isError,
787
- });
788
- j++;
789
- }
790
- // Skip the messages we've already processed
791
- i = j - 1;
792
- // Add a single user message with all tool results
793
- params.push({
794
- role: "user",
795
- content: toolResults,
796
- });
797
- }
121
+ const base = buildBaseOptions(model, options, apiKey);
122
+ if (!options?.reasoning) {
123
+ return streamAnthropic(model, context, { ...base, thinkingEnabled: false });
798
124
  }
799
- // Add cache_control to the last user message to cache conversation history
800
- if (cacheControl && params.length > 0) {
801
- const lastMessage = params[params.length - 1];
802
- if (lastMessage.role === "user") {
803
- if (Array.isArray(lastMessage.content)) {
804
- const lastBlock = lastMessage.content[lastMessage.content.length - 1];
805
- if (lastBlock &&
806
- (lastBlock.type === "text" || lastBlock.type === "image" || lastBlock.type === "tool_result")) {
807
- lastBlock.cache_control = cacheControl;
808
- }
809
- }
810
- else if (typeof lastMessage.content === "string") {
811
- lastMessage.content = [
812
- {
813
- type: "text",
814
- text: lastMessage.content,
815
- cache_control: cacheControl,
816
- },
817
- ];
818
- }
819
- }
125
+ // For Opus 4.6 and Sonnet 4.6: use adaptive thinking with effort level
126
+ // For older models: use budget-based thinking
127
+ if (supportsAdaptiveThinking(model.id)) {
128
+ const effort = mapThinkingLevelToEffort(options.reasoning, model.id);
129
+ return streamAnthropic(model, context, {
130
+ ...base,
131
+ thinkingEnabled: true,
132
+ effort,
133
+ });
820
134
  }
821
- return params;
822
- }
823
- function convertTools(tools, isOAuthToken) {
824
- if (!tools)
825
- return [];
826
- return tools.map((tool) => {
827
- const jsonSchema = tool.parameters; // TypeBox already generates JSON Schema
828
- return {
829
- name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name,
830
- description: tool.description,
831
- input_schema: {
832
- type: "object",
833
- properties: jsonSchema.properties || {},
834
- required: jsonSchema.required || [],
835
- },
836
- };
135
+ const adjusted = adjustMaxTokensForThinking(base.maxTokens || 0, model.maxTokens, options.reasoning, options.thinkingBudgets);
136
+ return streamAnthropic(model, context, {
137
+ ...base,
138
+ maxTokens: adjusted.maxTokens,
139
+ thinkingEnabled: true,
140
+ thinkingBudgetTokens: adjusted.thinkingBudget,
837
141
  });
838
- }
839
- function mapStopReason(reason) {
840
- switch (reason) {
841
- case "end_turn":
842
- return "stop";
843
- case "max_tokens":
844
- return "length";
845
- case "tool_use":
846
- return "toolUse";
847
- case "refusal":
848
- return "error";
849
- case "pause_turn": // Stop is good enough -> resubmit
850
- return "stop";
851
- case "stop_sequence":
852
- return "stop"; // We don't supply stop sequences, so this should never happen
853
- case "sensitive": // Content flagged by safety filters (not yet in SDK types)
854
- return "error";
855
- default:
856
- // Handle unknown stop reasons gracefully (API may add new values)
857
- throw new Error(`Unhandled stop reason: ${reason}`);
858
- }
859
- }
142
+ };
860
143
  //# sourceMappingURL=anthropic.js.map