kovence-cli 1.2.0 → 1.3.1

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.
package/providers.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
 
3
- const { lookupModelCapabilities } = require("./src/config/model_registry");
3
+ const { lookupModelCapabilities, DEFAULT_MAX_OUTPUT_TOKENS } = require("./src/config/model_registry");
4
4
  const { StreamingToolArgsParser } = require("./src/agent/streaming_write");
5
+ const { isToolFailure } = require("./src/agent/resilience");
5
6
 
6
7
  function buildProviderUrls(provider) {
7
8
  const base = String(provider?.baseUrl || "").replace(/\/+$/, "");
@@ -36,8 +37,9 @@ function buildProviderHeaders(provider) {
36
37
 
37
38
  /**
38
39
  * Convert OpenAI-style messages into Anthropic Messages API shape while guaranteeing valid user-first turns.
40
+ * When `includeThinking` is true, keeps captured thinking/redacted_thinking blocks BEFORE text/tool_use blocks.
39
41
  */
40
- function toAnthropicMessages(messages) {
42
+ function toAnthropicMessages(messages, { includeThinking = false, includeImages = true } = {}) {
41
43
  const systemParts = [];
42
44
  const out = [];
43
45
  let pendingToolResults = [];
@@ -54,16 +56,46 @@ function toAnthropicMessages(messages) {
54
56
  continue;
55
57
  }
56
58
  if (m.role === "tool") {
57
- pendingToolResults.push({
59
+ let resultContent = String(m.content || "");
60
+ if (includeImages && Array.isArray(m.images) && m.images.length > 0 && !m.imagesPruned) {
61
+ resultContent = [
62
+ { type: "text", text: String(m.content || "") },
63
+ ...m.images
64
+ .filter((img) => img && typeof img.data === "string" && img.data)
65
+ .map((img) => ({
66
+ type: "image",
67
+ source: {
68
+ type: "base64",
69
+ media_type: img.mediaType || "image/png",
70
+ data: img.data,
71
+ },
72
+ })),
73
+ ];
74
+ }
75
+ const toolResult = {
58
76
  type: "tool_result",
59
77
  tool_use_id: m.tool_call_id || "tool_0",
60
- content: String(m.content || ""),
61
- });
78
+ content: resultContent,
79
+ };
80
+ if (isToolFailure(m)) {
81
+ toolResult.is_error = true;
82
+ }
83
+ pendingToolResults.push(toolResult);
62
84
  continue;
63
85
  }
64
86
  flushTools();
65
87
  if (m.role === "assistant") {
66
88
  const blocks = [];
89
+ if (includeThinking && Array.isArray(m.thinkingBlocks)) {
90
+ for (const tb of m.thinkingBlocks) {
91
+ if (!tb || typeof tb !== "object") continue;
92
+ if (tb.type === "thinking" && tb.thinking && tb.signature) {
93
+ blocks.push({ type: "thinking", thinking: tb.thinking, signature: tb.signature });
94
+ } else if (tb.type === "redacted_thinking" && tb.data) {
95
+ blocks.push({ type: "redacted_thinking", data: tb.data });
96
+ }
97
+ }
98
+ }
67
99
  if (m.content) blocks.push({ type: "text", text: String(m.content) });
68
100
  if (Array.isArray(m.tool_calls)) {
69
101
  for (const tc of m.tool_calls) {
@@ -98,15 +130,195 @@ function toAnthropicMessages(messages) {
98
130
  if (out.length === 0) {
99
131
  out.push({ role: "user", content: "Continue." });
100
132
  }
101
- return { system: systemParts.join("\n\n"), messages: out };
133
+ return { system: systemParts.join("\n\n"), systemBlocks: systemParts, messages: out };
102
134
  }
103
135
 
104
- function toAnthropicTools(toolDefinitions) {
105
- return toolDefinitions.map((t) => ({
136
+ function toAnthropicTools(toolDefinitions, { enableCaching = true } = {}) {
137
+ /** @type {Array<Record<string, any>>} */
138
+ const out = (Array.isArray(toolDefinitions) ? toolDefinitions : []).map((t) => ({
106
139
  name: t.function.name,
107
140
  description: t.function.description,
108
141
  input_schema: t.function.parameters || { type: "object", properties: {} },
109
142
  }));
143
+ if (enableCaching && out.length > 0) {
144
+ out[out.length - 1].cache_control = { type: "ephemeral" };
145
+ }
146
+ return out;
147
+ }
148
+
149
+ /**
150
+ * Merge all system messages into one leading system message for OpenAI-compatible APIs,
151
+ * and strip internal-only metadata fields (e.g. isError, thinkingBlocks, reasoningContent).
152
+ */
153
+ function mergeOpenAISystemMessages(messages, { includeImages = true } = {}) {
154
+ const systemParts = [];
155
+ const rest = [];
156
+ let pendingToolImages = [];
157
+
158
+ const flushToolImages = () => {
159
+ if (!includeImages || !pendingToolImages.length) {
160
+ pendingToolImages = [];
161
+ return;
162
+ }
163
+ rest.push({
164
+ role: "user",
165
+ content: [
166
+ {
167
+ type: "text",
168
+ text: `[Browser screenshot(s) captured (${pendingToolImages.length})] Inspect visually for layout, styling, alignment, text readability, and UI defects.`,
169
+ },
170
+ ...pendingToolImages.map((img) => ({
171
+ type: "image_url",
172
+ image_url: {
173
+ url: `data:${img.mediaType || "image/png"};base64,${img.data}`,
174
+ },
175
+ })),
176
+ ],
177
+ });
178
+ pendingToolImages = [];
179
+ };
180
+
181
+ for (const m of Array.isArray(messages) ? messages : []) {
182
+ if (!m || !m.role) continue;
183
+ if (m.role === "system") {
184
+ const txt = typeof m.content === "string" ? m.content : JSON.stringify(m.content || "");
185
+ if (txt.trim()) systemParts.push(txt);
186
+ continue;
187
+ }
188
+ if (m.role === "tool") {
189
+ const cleanToolMsg = {
190
+ role: "tool",
191
+ tool_call_id: m.tool_call_id,
192
+ content: m.content,
193
+ };
194
+ if (m.name !== undefined) cleanToolMsg.name = m.name;
195
+ rest.push(cleanToolMsg);
196
+ if (includeImages && Array.isArray(m.images) && m.images.length > 0 && !m.imagesPruned) {
197
+ for (const img of m.images) {
198
+ if (img && typeof img.data === "string" && img.data) pendingToolImages.push(img);
199
+ }
200
+ }
201
+ continue;
202
+ }
203
+ flushToolImages();
204
+ if (m.role === "assistant" && (m.thinkingBlocks !== undefined || m.reasoningContent !== undefined)) {
205
+ const { thinkingBlocks: _tb, reasoningContent: _rc, ...cleanAssistant } = m;
206
+ rest.push(cleanAssistant);
207
+ continue;
208
+ }
209
+ rest.push(m);
210
+ }
211
+ flushToolImages();
212
+ if (systemParts.length > 0) {
213
+ return [{ role: "system", content: systemParts.join("\n\n") }, ...rest];
214
+ }
215
+ return rest;
216
+ }
217
+
218
+ function isCompleteJsonObject(raw) {
219
+ const src = String(raw || "").trim();
220
+ if (!src || !src.startsWith("{")) return false;
221
+ try {
222
+ const parsed = JSON.parse(src);
223
+ return Boolean(parsed) && typeof parsed === "object" && !Array.isArray(parsed);
224
+ } catch {
225
+ return false;
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Create a stateful OpenAI streaming tool_calls delta merger.
231
+ * Keys calls by index, starts a new entry when id changes or (last resort) name differs on complete JSON,
232
+ * never concatenates repeated names, and preserves all earlier calls.
233
+ */
234
+ function createToolCallMerger({ onArgumentsDelta = null } = {}) {
235
+ const calls = [];
236
+ const activeByIndex = new Map();
237
+
238
+ const merge = (deltas) => {
239
+ if (!Array.isArray(deltas)) return;
240
+ for (const d of deltas) {
241
+ if (!d || typeof d !== "object") continue;
242
+ const idx = d.index ?? 0;
243
+ let entry = activeByIndex.get(idx);
244
+ const deltaId = typeof d.id === "string" ? d.id.trim() : "";
245
+ const deltaName = typeof d.function?.name === "string" ? d.function.name : "";
246
+
247
+ let startNew = false;
248
+ if (!entry) {
249
+ startNew = true;
250
+ } else if (deltaId && entry._hasRealId && deltaId !== entry.id) {
251
+ startNew = true;
252
+ } else if (
253
+ !deltaId &&
254
+ deltaName &&
255
+ entry.function.name &&
256
+ deltaName !== entry.function.name &&
257
+ isCompleteJsonObject(entry.function.arguments)
258
+ ) {
259
+ startNew = true;
260
+ }
261
+
262
+ if (startNew) {
263
+ const streamKey = calls.length;
264
+ entry = {
265
+ id: deltaId || `call_${idx}_${streamKey}`,
266
+ type: "function",
267
+ function: { name: "", arguments: "" },
268
+ _hasRealId: Boolean(deltaId),
269
+ _streamKey: streamKey,
270
+ };
271
+ calls.push(entry);
272
+ activeByIndex.set(idx, entry);
273
+ } else if (deltaId && !entry._hasRealId) {
274
+ entry.id = deltaId;
275
+ entry._hasRealId = true;
276
+ }
277
+
278
+ if (deltaName && !entry.function.name) {
279
+ entry.function.name = deltaName;
280
+ }
281
+
282
+ const rawArgs = d.function?.arguments;
283
+ if (rawArgs !== undefined && rawArgs !== null) {
284
+ if (typeof rawArgs === "object") {
285
+ const serialized = JSON.stringify(rawArgs);
286
+ entry.function.arguments = serialized;
287
+ onArgumentsDelta?.(entry._streamKey, serialized, entry);
288
+ } else {
289
+ const chunk = String(rawArgs);
290
+ if (chunk.length > 0) {
291
+ entry.function.arguments += chunk;
292
+ onArgumentsDelta?.(entry._streamKey, chunk, entry);
293
+ }
294
+ }
295
+ }
296
+ }
297
+ };
298
+
299
+ return {
300
+ merge,
301
+ getLastEntry: () => (calls.length ? calls[calls.length - 1] : null),
302
+ getCalls: () =>
303
+ calls.map((c) => ({
304
+ id: c.id,
305
+ type: c.type,
306
+ function: {
307
+ name: c.function.name,
308
+ arguments: c.function.arguments,
309
+ },
310
+ })),
311
+ pushDirect: (entry) => {
312
+ const streamKey = calls.length;
313
+ const wrapped = {
314
+ ...entry,
315
+ _hasRealId: Boolean(entry.id),
316
+ _streamKey: streamKey,
317
+ };
318
+ calls.push(wrapped);
319
+ return wrapped;
320
+ },
321
+ };
110
322
  }
111
323
 
112
324
  /**
@@ -114,16 +326,23 @@ function toAnthropicTools(toolDefinitions) {
114
326
  */
115
327
  function getModelCapabilities(capabilitiesStore, provider, model) {
116
328
  if (!capabilitiesStore) {
117
- return { supportsStreamOptions: true, supportsTools: true };
329
+ return { supportsStreamOptions: true, supportsTools: true, maxTokensField: "max_tokens", maxTokensCap: null };
118
330
  }
119
331
  if (typeof capabilitiesStore.supportsStreamOptions === "boolean" && !capabilitiesStore._byModel) {
120
332
  // Legacy flat capability object compatibility
333
+ if (!capabilitiesStore.maxTokensField) capabilitiesStore.maxTokensField = "max_tokens";
334
+ if (capabilitiesStore.maxTokensCap === undefined) capabilitiesStore.maxTokensCap = null;
121
335
  return capabilitiesStore;
122
336
  }
123
337
  if (!capabilitiesStore._byModel) capabilitiesStore._byModel = new Map();
124
338
  const key = `${provider?.name || "default"}::${model || provider?.defaultModel || "default"}`;
125
339
  if (!capabilitiesStore._byModel.has(key)) {
126
- capabilitiesStore._byModel.set(key, { supportsStreamOptions: true, supportsTools: true });
340
+ capabilitiesStore._byModel.set(key, {
341
+ supportsStreamOptions: true,
342
+ supportsTools: true,
343
+ maxTokensField: "max_tokens",
344
+ maxTokensCap: null,
345
+ });
127
346
  }
128
347
  return capabilitiesStore._byModel.get(key);
129
348
  }
@@ -196,6 +415,72 @@ const STREAM_OPTIONS_UNSUPPORTED_RE =
196
415
  /\b(stream_options|include_usage)\b.*(unsupported|unknown|unrecognized|not allowed|invalid|not supported)/i;
197
416
  const TOOLS_UNSUPPORTED_RE =
198
417
  /(does not support|not support|unsupported|unavailable for|cannot use).{0,40}\b(tools?|function[ _]call|tool_choice)\b|\b(tools?|tool_choice)\b.{0,40}(is|are) not supported/i;
418
+ const MAX_TOKENS_UNSUPPORTED_RE =
419
+ /\bmax_tokens\b.*(?:unsupported|not supported|unknown|unrecognized|use\s+['"]?max_completion_tokens['"]?)|(?:unsupported|unknown)\s+parameter.*['"]?max_tokens['"]?|use\s+['"]?max_completion_tokens['"]?\s+instead/i;
420
+ const MAX_TOKENS_LIMIT_RE =
421
+ /(?:max_tokens|max_completion_tokens|output[ _]tokens?)[^\n\r]*?(?:must be\s*<=?|less than or equal to|at most|cannot exceed|exceeds(?:\s+maximum)?|maximum(?:\s+(?:is|allowed|value))?[:\s]*|limit(?:ed)?\s+to|range\s*\[\s*\d+\s*,\s*)[^\d]*(\d+)/i;
422
+ const REASONING_UNSUPPORTED_RE =
423
+ /\b(reasoning_effort|thinking)\b.*(?:unsupported|unknown|unrecognized|not allowed|not supported|invalid)/i;
424
+ const IMAGE_UNSUPPORTED_RE =
425
+ /\b(image_url|image input|images?|vision|multimodal)\b.*(?:unsupported|not support|unknown|invalid|disallowed|not allowed)|does not support\s+image/i;
426
+
427
+ const THINKING_BUDGETS = {
428
+ low: 2048,
429
+ medium: 6144,
430
+ high: 16384,
431
+ };
432
+
433
+ function extractMaxTokensLimit(errText) {
434
+ const text = String(errText || "");
435
+ const m = text.match(MAX_TOKENS_LIMIT_RE);
436
+ if (m) {
437
+ const n = Number(m[1]);
438
+ if (Number.isFinite(n) && n >= 1) return n;
439
+ }
440
+ if (/max(?:_completion)?_tokens/i.test(text)) {
441
+ const rangeMatch = text.match(/range\s*\[\s*\d+\s*,\s*(\d+)\s*\]/i);
442
+ if (rangeMatch) {
443
+ const n = Number(rangeMatch[1]);
444
+ if (Number.isFinite(n) && n >= 1) return n;
445
+ }
446
+ }
447
+ return null;
448
+ }
449
+
450
+ function resolveMaxOutputTokens({ isAnthropic, bodyBase, settings, registryCaps, modelCaps }) {
451
+ const modelMax = registryCaps.maxOutputTokens || DEFAULT_MAX_OUTPUT_TOKENS;
452
+ const defaultTokens = Math.min(DEFAULT_MAX_OUTPUT_TOKENS, modelMax);
453
+ let val = null;
454
+
455
+ if (Number.isFinite(bodyBase.max_tokens)) {
456
+ val = bodyBase.max_tokens;
457
+ } else if (Number.isFinite(settings?.maxTokens)) {
458
+ val = settings.maxTokens;
459
+ } else if (isAnthropic) {
460
+ val = defaultTokens;
461
+ } else if (registryCaps.known) {
462
+ val = defaultTokens;
463
+ }
464
+
465
+ if (val !== null && registryCaps.known) {
466
+ val = Math.min(val, modelMax);
467
+ }
468
+
469
+ const thinkingBudget = bodyBase.thinking?.budget_tokens;
470
+ if (isAnthropic && Number.isFinite(thinkingBudget) && thinkingBudget > 0) {
471
+ const minWithThinking = thinkingBudget + 4096;
472
+ val = Math.max(val || 0, minWithThinking);
473
+ if (registryCaps.known && modelMax > thinkingBudget) {
474
+ val = Math.min(val, modelMax);
475
+ }
476
+ }
477
+
478
+ if (val !== null && Number.isFinite(modelCaps?.maxTokensCap) && modelCaps.maxTokensCap >= 1) {
479
+ val = Math.min(val, modelCaps.maxTokensCap);
480
+ }
481
+
482
+ return val;
483
+ }
199
484
 
200
485
  async function fetchWithAdapter({
201
486
  provider,
@@ -220,20 +505,52 @@ async function fetchWithAdapter({
220
505
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
221
506
  if (signal?.aborted) throw signal.reason || new Error("Aborted");
222
507
 
508
+ const rawReasoning =
509
+ !bodyBase._noTools && !bodyBase._noReasoning ? (bodyBase.reasoning ?? settings?.reasoning) : "off";
510
+ const reasoningLevel = String(rawReasoning || "off")
511
+ .trim()
512
+ .toLowerCase();
513
+ const thinkingConfig =
514
+ isAnthropic && reasoningLevel in THINKING_BUDGETS
515
+ ? { type: "enabled", budget_tokens: THINKING_BUDGETS[reasoningLevel] }
516
+ : null;
517
+ const effectiveMaxTokens = resolveMaxOutputTokens({
518
+ isAnthropic,
519
+ bodyBase: thinkingConfig ? { ...bodyBase, thinking: bodyBase.thinking || thinkingConfig } : bodyBase,
520
+ settings,
521
+ registryCaps,
522
+ modelCaps,
523
+ });
524
+
223
525
  let payload;
224
526
  if (isAnthropic) {
225
- const conv = toAnthropicMessages(bodyBase.messages);
527
+ const conv = toAnthropicMessages(bodyBase.messages, {
528
+ includeThinking: Boolean(thinkingConfig || bodyBase.thinking),
529
+ includeImages: !bodyBase._noImages,
530
+ });
226
531
  payload = {
227
532
  model,
228
533
  messages: conv.messages,
229
- max_tokens: bodyBase.max_tokens || settings?.maxTokens || 4096,
534
+ max_tokens: effectiveMaxTokens || DEFAULT_MAX_OUTPUT_TOKENS,
230
535
  stream: Boolean(bodyBase.stream),
231
536
  };
232
- if (conv.system) payload.system = conv.system;
537
+ if (Array.isArray(conv.systemBlocks) && conv.systemBlocks.length > 1) {
538
+ payload.system = [
539
+ { type: "text", text: conv.systemBlocks[0], cache_control: { type: "ephemeral" } },
540
+ { type: "text", text: conv.systemBlocks.slice(1).join("\n\n") },
541
+ ];
542
+ } else if (conv.system) {
543
+ payload.system = [{ type: "text", text: conv.system, cache_control: { type: "ephemeral" } }];
544
+ }
233
545
  if (!bodyBase._noTools && settings?.toolsEnabled && modelCaps.supportsTools) {
234
- payload.tools = toAnthropicTools(toolDefinitions);
546
+ payload.tools = toAnthropicTools(toolDefinitions, { enableCaching: true });
547
+ if (bodyBase.toolChoice === "none") {
548
+ payload.tool_choice = { type: "none" };
549
+ }
235
550
  }
236
- if (
551
+ if (thinkingConfig) {
552
+ payload.thinking = thinkingConfig;
553
+ } else if (
237
554
  registryCaps.supportsTemperature !== false &&
238
555
  Number.isFinite(bodyBase.temperature ?? settings?.temperature)
239
556
  ) {
@@ -242,7 +559,9 @@ async function fetchWithAdapter({
242
559
  } else {
243
560
  payload = {
244
561
  model,
245
- messages: bodyBase.messages,
562
+ messages: mergeOpenAISystemMessages(bodyBase.messages, {
563
+ includeImages: !bodyBase._noImages,
564
+ }),
246
565
  stream: Boolean(bodyBase.stream),
247
566
  };
248
567
  if (payload.stream && modelCaps.supportsStreamOptions) {
@@ -255,7 +574,10 @@ async function fetchWithAdapter({
255
574
  registryCaps.supportsTools !== false
256
575
  ) {
257
576
  payload.tools = toolDefinitions;
258
- payload.tool_choice = "auto";
577
+ payload.tool_choice = bodyBase.toolChoice || "auto";
578
+ }
579
+ if (reasoningLevel === "low" || reasoningLevel === "medium" || reasoningLevel === "high") {
580
+ payload.reasoning_effort = reasoningLevel;
259
581
  }
260
582
  if (
261
583
  registryCaps.supportsTemperature !== false &&
@@ -263,12 +585,21 @@ async function fetchWithAdapter({
263
585
  ) {
264
586
  payload.temperature = bodyBase.temperature ?? settings.temperature;
265
587
  }
266
- if (Number.isFinite(bodyBase.max_tokens ?? settings?.maxTokens)) {
267
- payload.max_tokens = bodyBase.max_tokens ?? settings.maxTokens;
588
+ if (Number.isFinite(effectiveMaxTokens)) {
589
+ const field = modelCaps.maxTokensField || "max_tokens";
590
+ payload[field] = effectiveMaxTokens;
268
591
  }
269
592
  }
270
593
 
271
594
  const linked = createLinkedController(signal, timeoutMs);
595
+ const logger = settings?._requestLogger || bodyBase._requestLogger || null;
596
+ if (logger && typeof logger.log === "function") {
597
+ try {
598
+ logger.log({ url: chatUrl, method: "POST", headers, body: payload, provider, model });
599
+ } catch {
600
+ // Non-fatal: request logging failure must never abort provider calls
601
+ }
602
+ }
272
603
  let res;
273
604
  try {
274
605
  res = await fetch(chatUrl, {
@@ -310,7 +641,7 @@ async function fetchWithAdapter({
310
641
 
311
642
  const errText = await res.text();
312
643
  let downgraded = false;
313
- if (downgradeAttempts < 2) {
644
+ if (downgradeAttempts < 3) {
314
645
  if (!isAnthropic && modelCaps.supportsStreamOptions && STREAM_OPTIONS_UNSUPPORTED_RE.test(errText)) {
315
646
  modelCaps.supportsStreamOptions = false;
316
647
  downgraded = true;
@@ -319,6 +650,32 @@ async function fetchWithAdapter({
319
650
  modelCaps.supportsTools = false;
320
651
  downgraded = true;
321
652
  }
653
+ if (
654
+ !isAnthropic &&
655
+ modelCaps.maxTokensField !== "max_completion_tokens" &&
656
+ MAX_TOKENS_UNSUPPORTED_RE.test(errText)
657
+ ) {
658
+ modelCaps.maxTokensField = "max_completion_tokens";
659
+ downgraded = true;
660
+ }
661
+ const statedLimit = extractMaxTokensLimit(errText);
662
+ if (
663
+ statedLimit !== null &&
664
+ Number.isFinite(effectiveMaxTokens) &&
665
+ statedLimit < effectiveMaxTokens &&
666
+ modelCaps.maxTokensCap !== statedLimit
667
+ ) {
668
+ modelCaps.maxTokensCap = statedLimit;
669
+ downgraded = true;
670
+ }
671
+ if (!bodyBase._noReasoning && REASONING_UNSUPPORTED_RE.test(errText)) {
672
+ bodyBase._noReasoning = true;
673
+ downgraded = true;
674
+ }
675
+ if (!bodyBase._noImages && IMAGE_UNSUPPORTED_RE.test(errText)) {
676
+ bodyBase._noImages = true;
677
+ downgraded = true;
678
+ }
322
679
  }
323
680
  if (!downgraded) throw new Error(`HTTP ${res.status}: ${errText}`);
324
681
  downgradeAttempts += 1;
@@ -339,16 +696,25 @@ async function streamProviderTurn({
339
696
  capabilities,
340
697
  toolDefinitions,
341
698
  timeoutMs = 120000,
699
+ maxTokensOverride = null,
700
+ toolChoice = null,
342
701
  }) {
343
702
  const startedAt = Date.now();
344
703
  const streamUi = sink.startThinking("Kovence: ", "Thinking");
704
+ const bodyBase = { messages, stream: true };
705
+ if (Number.isFinite(maxTokensOverride)) {
706
+ bodyBase.max_tokens = maxTokensOverride;
707
+ }
708
+ if (toolChoice) {
709
+ bodyBase.toolChoice = toolChoice;
710
+ }
345
711
  let res;
346
712
  try {
347
713
  res = await fetchWithAdapter({
348
714
  provider,
349
715
  model,
350
716
  settings,
351
- bodyBase: { messages, stream: true },
717
+ bodyBase,
352
718
  signal,
353
719
  capabilities,
354
720
  toolDefinitions,
@@ -365,10 +731,12 @@ async function streamProviderTurn({
365
731
  const decoder = new TextDecoder();
366
732
  let buffer = "";
367
733
  let fullText = "";
734
+ let reasoningText = "";
368
735
  let printedHeader = false;
369
736
  let usage = null;
370
737
  let finishReason = null;
371
- const toolMap = new Map();
738
+ const anthropicToolMap = new Map();
739
+ const anthropicThinkingBlocks = new Map();
372
740
  const isAnthropic = provider.type === "anthropic";
373
741
  // Per-tool streaming write parser for live 5-line window preview and dynamic file stream
374
742
  const writeStreamParsers = new Map();
@@ -381,6 +749,54 @@ async function streamProviderTurn({
381
749
  }
382
750
  };
383
751
 
752
+ /**
753
+ * Called with each raw argument delta chunk for a tool call.
754
+ * When the tool is write_file/edit_file, extracts the unescaped content/replacement
755
+ * characters and target path, streaming them dynamically to sink.
756
+ */
757
+ const pumpWriteChunk = (streamKey, deltaChunk, entry) => {
758
+ if (!entry) return;
759
+ const tName = entry.function?.name || "";
760
+ // If we already know the tool name and it's NOT a write tool, skip entirely.
761
+ // When tName is still empty (name delta hasn't arrived yet), always create/reuse
762
+ // the parser so we don't miss early argument chunks.
763
+ if (tName && !/write_file|edit_file|multi_edit|apply_patch/.test(tName)) return;
764
+
765
+ let parser = writeStreamParsers.get(streamKey);
766
+ if (parser && parser.contentEnded) {
767
+ writeStreamParsers.delete(streamKey);
768
+ parser = null;
769
+ }
770
+ if (!parser) {
771
+ parser = new StreamingToolArgsParser({
772
+ onPath: (filePath) => {
773
+ // Re-read name from live entry — it may have arrived after parser creation
774
+ const currentName = entry.function?.name || "write_file";
775
+ if (!/write_file|edit_file|multi_edit|apply_patch/.test(currentName)) return;
776
+ sink.preRegisterWriteBlock?.(streamKey, currentName, filePath);
777
+ },
778
+ onContentChunk: (unescapedChunk, filePath, _key) => {
779
+ const currentName = entry.function?.name || "write_file";
780
+ if (!/write_file|edit_file|multi_edit|apply_patch/.test(currentName)) return;
781
+ sink.onWriteStreamChunk?.(streamKey, currentName, filePath, unescapedChunk);
782
+ },
783
+ onContentEnd: (filePath, _key) => {
784
+ const currentName = entry.function?.name || "write_file";
785
+ if (!/write_file|edit_file|multi_edit|apply_patch/.test(currentName)) return;
786
+ sink.onWriteStreamChunkEnd?.(streamKey, currentName, filePath);
787
+ },
788
+ });
789
+ writeStreamParsers.set(streamKey, parser);
790
+ }
791
+ parser.push(deltaChunk);
792
+ };
793
+
794
+ const toolMerger = createToolCallMerger({
795
+ onArgumentsDelta: (streamKey, chunk, entry) => {
796
+ pumpWriteChunk(streamKey, chunk, entry);
797
+ },
798
+ });
799
+
384
800
  let lastProgressUpdate = 0;
385
801
  const updateToolProgress = () => {
386
802
  if (firstTokenAt === null) firstTokenAt = Date.now();
@@ -389,7 +805,7 @@ async function streamProviderTurn({
389
805
  if (now - lastProgressUpdate < 80) return;
390
806
  lastProgressUpdate = now;
391
807
 
392
- const activeEntry = [...toolMap.values()].pop();
808
+ const activeEntry = isAnthropic ? [...anthropicToolMap.values()].pop() : toolMerger.getLastEntry();
393
809
  const toolName = activeEntry?.function?.name || "";
394
810
  const args = activeEntry?.function?.arguments || "";
395
811
  const isWrite = /write_file|edit_file|multi_edit|apply_patch/.test(toolName);
@@ -418,83 +834,6 @@ async function streamProviderTurn({
418
834
  sink.setBusy?.(label);
419
835
  };
420
836
 
421
- const mergeOpenAITools = (deltas) => {
422
- for (const d of deltas) {
423
- const idx = d.index ?? 0;
424
- if (!toolMap.has(idx)) {
425
- toolMap.set(idx, {
426
- id: d.id || `call_${idx}`,
427
- type: "function",
428
- function: { name: "", arguments: "" },
429
- });
430
- }
431
- const entry = toolMap.get(idx);
432
- // Detect when the LLM reuses the same slot index for a NEW sequential tool call.
433
- // Indicators: (a) a different call id, or (b) a new function name arriving when
434
- // the entry already had a completed arguments string (heuristic: the previous
435
- // tool's JSON was already closed with "}").
436
- const isNewToolOnSameIdx =
437
- (d.id && d.id !== entry.id) || (d.function?.name && entry.function.arguments.endsWith("}"));
438
- if (isNewToolOnSameIdx) {
439
- if (d.id) entry.id = d.id;
440
- entry.function.name = "";
441
- entry.function.arguments = "";
442
- writeStreamParsers.delete(idx);
443
- }
444
- if (d.function?.name) entry.function.name += d.function.name;
445
- if (d.function?.arguments) {
446
- entry.function.arguments += d.function.arguments;
447
- pumpWriteChunk(idx, d.function.arguments);
448
- }
449
- }
450
- };
451
-
452
- /**
453
- * Called with each raw argument delta chunk for a tool call.
454
- * When the tool is write_file/edit_file, extracts the unescaped content/replacement
455
- * characters and target path, streaming them dynamically to sink.
456
- */
457
- const pumpWriteChunk = (idx, deltaChunk) => {
458
- const entry = toolMap.get(idx);
459
- if (!entry) return;
460
- const tName = entry.function?.name || "";
461
- // If we already know the tool name and it's NOT a write tool, skip entirely.
462
- // When tName is still empty (name delta hasn't arrived yet), always create/reuse
463
- // the parser so we don't miss early argument chunks.
464
- if (tName && !/write_file|edit_file|multi_edit/.test(tName)) return;
465
-
466
- let parser = writeStreamParsers.get(idx);
467
- // If the parser already finished its content stream, it belongs to a *previous*
468
- // tool call that happened to share the same slot index. Discard it so we start
469
- // fresh for the new tool call. This covers models that don't send a new `id`.
470
- if (parser && parser.contentEnded) {
471
- writeStreamParsers.delete(idx);
472
- parser = null;
473
- }
474
- if (!parser) {
475
- parser = new StreamingToolArgsParser({
476
- onPath: (filePath) => {
477
- // Re-read name from live entry — it may have arrived after parser creation
478
- const currentName = entry.function?.name || "write_file";
479
- if (!/write_file|edit_file|multi_edit/.test(currentName)) return;
480
- sink.preRegisterWriteBlock?.(idx, currentName, filePath);
481
- },
482
- onContentChunk: (unescapedChunk, filePath, _key) => {
483
- const currentName = entry.function?.name || "write_file";
484
- if (!/write_file|edit_file|multi_edit/.test(currentName)) return;
485
- sink.onWriteStreamChunk?.(idx, currentName, filePath, unescapedChunk);
486
- },
487
- onContentEnd: (filePath, _key) => {
488
- const currentName = entry.function?.name || "write_file";
489
- if (!/write_file|edit_file|multi_edit/.test(currentName)) return;
490
- sink.onWriteStreamChunkEnd?.(idx, currentName, filePath);
491
- },
492
- });
493
- writeStreamParsers.set(idx, parser);
494
- }
495
- parser.push(deltaChunk);
496
- };
497
-
498
837
  const handleLine = (rawLine) => {
499
838
  const line = rawLine.replace(/\r$/, "");
500
839
  if (!line.startsWith("data:")) return;
@@ -510,48 +849,99 @@ async function streamProviderTurn({
510
849
 
511
850
  if (isAnthropic) {
512
851
  if (json.type === "message_start" && json.message?.usage) {
852
+ const u = json.message.usage;
513
853
  usage = {
514
- prompt_tokens: json.message.usage.input_tokens || 0,
515
- completion_tokens: json.message.usage.output_tokens || 0,
854
+ prompt_tokens: u.input_tokens || 0,
855
+ completion_tokens: u.output_tokens || 0,
856
+ cache_creation_input_tokens: u.cache_creation_input_tokens || 0,
857
+ cache_read_input_tokens: u.cache_read_input_tokens || 0,
516
858
  };
517
859
  } else if (json.type === "message_delta") {
518
860
  if (json.delta?.stop_reason) finishReason = json.delta.stop_reason;
519
861
  if (json.usage) {
862
+ const u = json.usage;
520
863
  usage = {
521
864
  prompt_tokens: usage?.prompt_tokens || 0,
522
- completion_tokens: json.usage.output_tokens ?? usage?.completion_tokens ?? 0,
865
+ completion_tokens: u.output_tokens ?? usage?.completion_tokens ?? 0,
866
+ cache_creation_input_tokens:
867
+ u.cache_creation_input_tokens ?? usage?.cache_creation_input_tokens ?? 0,
868
+ cache_read_input_tokens: u.cache_read_input_tokens ?? usage?.cache_read_input_tokens ?? 0,
523
869
  };
524
870
  }
525
871
  } else if (json.type === "content_block_start" && json.content_block?.type === "tool_use") {
526
- const idx = json.index ?? toolMap.size;
527
- toolMap.set(idx, {
872
+ const idx = json.index ?? anthropicToolMap.size;
873
+ const entry = toolMerger.pushDirect({
528
874
  id: json.content_block.id || `call_${idx}`,
529
875
  type: "function",
530
876
  function: { name: json.content_block.name || "", arguments: "" },
531
877
  });
878
+ anthropicToolMap.set(idx, entry);
532
879
  updateToolProgress();
880
+ } else if (
881
+ json.type === "content_block_start" &&
882
+ (json.content_block?.type === "thinking" || json.content_block?.type === "redacted_thinking")
883
+ ) {
884
+ const idx = json.index ?? anthropicThinkingBlocks.size;
885
+ if (json.content_block.type === "redacted_thinking") {
886
+ anthropicThinkingBlocks.set(idx, {
887
+ type: "redacted_thinking",
888
+ data: json.content_block.data || "",
889
+ });
890
+ } else {
891
+ const initThink = json.content_block.thinking || "";
892
+ if (initThink) reasoningText += initThink;
893
+ anthropicThinkingBlocks.set(idx, {
894
+ type: "thinking",
895
+ thinking: initThink,
896
+ signature: json.content_block.signature || "",
897
+ });
898
+ }
533
899
  } else if (json.type === "content_block_delta") {
534
900
  if (json.delta?.type === "text_delta" && json.delta.text) {
535
901
  markFirstToken(true);
536
902
  streamUi.write(json.delta.text);
537
903
  fullText += json.delta.text;
904
+ } else if (json.delta?.type === "thinking_delta" && json.delta.thinking) {
905
+ const idx = json.index ?? 0;
906
+ const blk = anthropicThinkingBlocks.get(idx) || { type: "thinking", thinking: "", signature: "" };
907
+ blk.thinking += json.delta.thinking;
908
+ anthropicThinkingBlocks.set(idx, blk);
909
+ reasoningText += json.delta.thinking;
910
+ } else if (json.delta?.type === "signature_delta" && json.delta.signature) {
911
+ const idx = json.index ?? 0;
912
+ const blk = anthropicThinkingBlocks.get(idx) || { type: "thinking", thinking: "", signature: "" };
913
+ blk.signature += json.delta.signature;
914
+ anthropicThinkingBlocks.set(idx, blk);
538
915
  } else if (json.delta?.type === "input_json_delta" && json.delta.partial_json) {
539
916
  const idx = json.index ?? 0;
540
- const entry = toolMap.get(idx);
541
- if (entry) entry.function.arguments += json.delta.partial_json;
542
- pumpWriteChunk(idx, json.delta.partial_json);
917
+ const entry = anthropicToolMap.get(idx);
918
+ if (entry) {
919
+ entry.function.arguments += json.delta.partial_json;
920
+ pumpWriteChunk(entry._streamKey, json.delta.partial_json, entry);
921
+ }
543
922
  updateToolProgress();
544
923
  }
545
924
  }
546
925
  return;
547
926
  }
548
927
 
549
- if (json.usage) usage = json.usage;
928
+ if (json.usage) {
929
+ const u = json.usage;
930
+ usage = {
931
+ ...u,
932
+ cache_creation_input_tokens: u.cache_creation_input_tokens || 0,
933
+ cache_read_input_tokens: u.cache_read_input_tokens ?? u.prompt_tokens_details?.cached_tokens ?? 0,
934
+ };
935
+ }
550
936
  const choice = json.choices?.[0];
551
937
  if (choice?.finish_reason) finishReason = choice.finish_reason;
552
938
  const delta = choice?.delta;
939
+ const reasoningDelta = delta?.reasoning_content ?? delta?.reasoning ?? "";
940
+ if (reasoningDelta) {
941
+ reasoningText += reasoningDelta;
942
+ }
553
943
  if (delta?.tool_calls?.length) {
554
- mergeOpenAITools(delta.tool_calls);
944
+ toolMerger.merge(delta.tool_calls);
555
945
  updateToolProgress();
556
946
  }
557
947
  const token = delta?.content ?? json.response ?? "";
@@ -571,7 +961,10 @@ async function streamProviderTurn({
571
961
  buffer = lines.pop();
572
962
  for (const line of lines) handleLine(line);
573
963
  }
574
- if (buffer) handleLine(buffer);
964
+ buffer += decoder.decode();
965
+ if (buffer) {
966
+ for (const line of buffer.split("\n")) handleLine(line);
967
+ }
575
968
  } finally {
576
969
  if (!printedHeader) streamUi.stop(false);
577
970
  }
@@ -584,8 +977,23 @@ async function streamProviderTurn({
584
977
  const genSec = Math.max(0.001, (finishedAt - (firstTokenAt || startedAt)) / 1000);
585
978
  const tokPerSec = outTokens ? Number((outTokens / genSec).toFixed(1)) : 0;
586
979
 
587
- const toolCalls = [...toolMap.entries()].sort((a, b) => a[0] - b[0]).map(([, v]) => v);
588
- return { content: fullText, toolCalls, usage, finishReason, elapsedMs, ttftMs, tokPerSec, outTokens };
980
+ const toolCalls = toolMerger.getCalls();
981
+ const thinkingBlocks = [...anthropicThinkingBlocks.entries()]
982
+ .sort((a, b) => a[0] - b[0])
983
+ .map(([, b]) => b)
984
+ .filter((b) => (b.type === "redacted_thinking" ? Boolean(b.data) : Boolean(b.thinking || b.signature)));
985
+ return {
986
+ content: fullText,
987
+ toolCalls,
988
+ reasoning: reasoningText,
989
+ thinkingBlocks,
990
+ usage,
991
+ finishReason,
992
+ elapsedMs,
993
+ ttftMs,
994
+ tokPerSec,
995
+ outTokens,
996
+ };
589
997
  }
590
998
 
591
999
  async function completeProviderFast({
@@ -622,6 +1030,9 @@ module.exports = {
622
1030
  buildProviderUrls,
623
1031
  buildProviderHeaders,
624
1032
  toAnthropicMessages,
1033
+ toAnthropicTools,
1034
+ mergeOpenAISystemMessages,
1035
+ createToolCallMerger,
625
1036
  getModelCapabilities,
626
1037
  isRetryableStatus,
627
1038
  listProviderModels,