kovence-cli 1.0.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/providers.js CHANGED
@@ -1,6 +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
+ const { StreamingToolArgsParser } = require("./src/agent/streaming_write");
5
+ const { isToolFailure } = require("./src/agent/resilience");
4
6
 
5
7
  function buildProviderUrls(provider) {
6
8
  const base = String(provider?.baseUrl || "").replace(/\/+$/, "");
@@ -35,8 +37,9 @@ function buildProviderHeaders(provider) {
35
37
 
36
38
  /**
37
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.
38
41
  */
39
- function toAnthropicMessages(messages) {
42
+ function toAnthropicMessages(messages, { includeThinking = false, includeImages = true } = {}) {
40
43
  const systemParts = [];
41
44
  const out = [];
42
45
  let pendingToolResults = [];
@@ -53,16 +56,46 @@ function toAnthropicMessages(messages) {
53
56
  continue;
54
57
  }
55
58
  if (m.role === "tool") {
56
- 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 = {
57
76
  type: "tool_result",
58
77
  tool_use_id: m.tool_call_id || "tool_0",
59
- content: String(m.content || ""),
60
- });
78
+ content: resultContent,
79
+ };
80
+ if (isToolFailure(m)) {
81
+ toolResult.is_error = true;
82
+ }
83
+ pendingToolResults.push(toolResult);
61
84
  continue;
62
85
  }
63
86
  flushTools();
64
87
  if (m.role === "assistant") {
65
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
+ }
66
99
  if (m.content) blocks.push({ type: "text", text: String(m.content) });
67
100
  if (Array.isArray(m.tool_calls)) {
68
101
  for (const tc of m.tool_calls) {
@@ -97,15 +130,195 @@ function toAnthropicMessages(messages) {
97
130
  if (out.length === 0) {
98
131
  out.push({ role: "user", content: "Continue." });
99
132
  }
100
- return { system: systemParts.join("\n\n"), messages: out };
133
+ return { system: systemParts.join("\n\n"), systemBlocks: systemParts, messages: out };
101
134
  }
102
135
 
103
- function toAnthropicTools(toolDefinitions) {
104
- 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) => ({
105
139
  name: t.function.name,
106
140
  description: t.function.description,
107
141
  input_schema: t.function.parameters || { type: "object", properties: {} },
108
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
+ };
109
322
  }
110
323
 
111
324
  /**
@@ -113,16 +326,23 @@ function toAnthropicTools(toolDefinitions) {
113
326
  */
114
327
  function getModelCapabilities(capabilitiesStore, provider, model) {
115
328
  if (!capabilitiesStore) {
116
- return { supportsStreamOptions: true, supportsTools: true };
329
+ return { supportsStreamOptions: true, supportsTools: true, maxTokensField: "max_tokens", maxTokensCap: null };
117
330
  }
118
331
  if (typeof capabilitiesStore.supportsStreamOptions === "boolean" && !capabilitiesStore._byModel) {
119
332
  // Legacy flat capability object compatibility
333
+ if (!capabilitiesStore.maxTokensField) capabilitiesStore.maxTokensField = "max_tokens";
334
+ if (capabilitiesStore.maxTokensCap === undefined) capabilitiesStore.maxTokensCap = null;
120
335
  return capabilitiesStore;
121
336
  }
122
337
  if (!capabilitiesStore._byModel) capabilitiesStore._byModel = new Map();
123
338
  const key = `${provider?.name || "default"}::${model || provider?.defaultModel || "default"}`;
124
339
  if (!capabilitiesStore._byModel.has(key)) {
125
- 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
+ });
126
346
  }
127
347
  return capabilitiesStore._byModel.get(key);
128
348
  }
@@ -195,6 +415,72 @@ const STREAM_OPTIONS_UNSUPPORTED_RE =
195
415
  /\b(stream_options|include_usage)\b.*(unsupported|unknown|unrecognized|not allowed|invalid|not supported)/i;
196
416
  const TOOLS_UNSUPPORTED_RE =
197
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
+ }
198
484
 
199
485
  async function fetchWithAdapter({
200
486
  provider,
@@ -219,20 +505,52 @@ async function fetchWithAdapter({
219
505
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
220
506
  if (signal?.aborted) throw signal.reason || new Error("Aborted");
221
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
+
222
525
  let payload;
223
526
  if (isAnthropic) {
224
- const conv = toAnthropicMessages(bodyBase.messages);
527
+ const conv = toAnthropicMessages(bodyBase.messages, {
528
+ includeThinking: Boolean(thinkingConfig || bodyBase.thinking),
529
+ includeImages: !bodyBase._noImages,
530
+ });
225
531
  payload = {
226
532
  model,
227
533
  messages: conv.messages,
228
- max_tokens: bodyBase.max_tokens || settings?.maxTokens || 4096,
534
+ max_tokens: effectiveMaxTokens || DEFAULT_MAX_OUTPUT_TOKENS,
229
535
  stream: Boolean(bodyBase.stream),
230
536
  };
231
- 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
+ }
232
545
  if (!bodyBase._noTools && settings?.toolsEnabled && modelCaps.supportsTools) {
233
- payload.tools = toAnthropicTools(toolDefinitions);
546
+ payload.tools = toAnthropicTools(toolDefinitions, { enableCaching: true });
547
+ if (bodyBase.toolChoice === "none") {
548
+ payload.tool_choice = { type: "none" };
549
+ }
234
550
  }
235
- if (
551
+ if (thinkingConfig) {
552
+ payload.thinking = thinkingConfig;
553
+ } else if (
236
554
  registryCaps.supportsTemperature !== false &&
237
555
  Number.isFinite(bodyBase.temperature ?? settings?.temperature)
238
556
  ) {
@@ -241,7 +559,9 @@ async function fetchWithAdapter({
241
559
  } else {
242
560
  payload = {
243
561
  model,
244
- messages: bodyBase.messages,
562
+ messages: mergeOpenAISystemMessages(bodyBase.messages, {
563
+ includeImages: !bodyBase._noImages,
564
+ }),
245
565
  stream: Boolean(bodyBase.stream),
246
566
  };
247
567
  if (payload.stream && modelCaps.supportsStreamOptions) {
@@ -254,7 +574,10 @@ async function fetchWithAdapter({
254
574
  registryCaps.supportsTools !== false
255
575
  ) {
256
576
  payload.tools = toolDefinitions;
257
- 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;
258
581
  }
259
582
  if (
260
583
  registryCaps.supportsTemperature !== false &&
@@ -262,12 +585,21 @@ async function fetchWithAdapter({
262
585
  ) {
263
586
  payload.temperature = bodyBase.temperature ?? settings.temperature;
264
587
  }
265
- if (Number.isFinite(bodyBase.max_tokens ?? settings?.maxTokens)) {
266
- payload.max_tokens = bodyBase.max_tokens ?? settings.maxTokens;
588
+ if (Number.isFinite(effectiveMaxTokens)) {
589
+ const field = modelCaps.maxTokensField || "max_tokens";
590
+ payload[field] = effectiveMaxTokens;
267
591
  }
268
592
  }
269
593
 
270
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
+ }
271
603
  let res;
272
604
  try {
273
605
  res = await fetch(chatUrl, {
@@ -309,7 +641,7 @@ async function fetchWithAdapter({
309
641
 
310
642
  const errText = await res.text();
311
643
  let downgraded = false;
312
- if (downgradeAttempts < 2) {
644
+ if (downgradeAttempts < 3) {
313
645
  if (!isAnthropic && modelCaps.supportsStreamOptions && STREAM_OPTIONS_UNSUPPORTED_RE.test(errText)) {
314
646
  modelCaps.supportsStreamOptions = false;
315
647
  downgraded = true;
@@ -318,6 +650,32 @@ async function fetchWithAdapter({
318
650
  modelCaps.supportsTools = false;
319
651
  downgraded = true;
320
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
+ }
321
679
  }
322
680
  if (!downgraded) throw new Error(`HTTP ${res.status}: ${errText}`);
323
681
  downgradeAttempts += 1;
@@ -338,16 +696,25 @@ async function streamProviderTurn({
338
696
  capabilities,
339
697
  toolDefinitions,
340
698
  timeoutMs = 120000,
699
+ maxTokensOverride = null,
700
+ toolChoice = null,
341
701
  }) {
342
702
  const startedAt = Date.now();
343
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
+ }
344
711
  let res;
345
712
  try {
346
713
  res = await fetchWithAdapter({
347
714
  provider,
348
715
  model,
349
716
  settings,
350
- bodyBase: { messages, stream: true },
717
+ bodyBase,
351
718
  signal,
352
719
  capabilities,
353
720
  toolDefinitions,
@@ -364,11 +731,15 @@ async function streamProviderTurn({
364
731
  const decoder = new TextDecoder();
365
732
  let buffer = "";
366
733
  let fullText = "";
734
+ let reasoningText = "";
367
735
  let printedHeader = false;
368
736
  let usage = null;
369
737
  let finishReason = null;
370
- const toolMap = new Map();
738
+ const anthropicToolMap = new Map();
739
+ const anthropicThinkingBlocks = new Map();
371
740
  const isAnthropic = provider.type === "anthropic";
741
+ // Per-tool streaming write parser for live 5-line window preview and dynamic file stream
742
+ const writeStreamParsers = new Map();
372
743
 
373
744
  const markFirstToken = (forText) => {
374
745
  if (firstTokenAt === null) firstTokenAt = Date.now();
@@ -378,6 +749,54 @@ async function streamProviderTurn({
378
749
  }
379
750
  };
380
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
+
381
800
  let lastProgressUpdate = 0;
382
801
  const updateToolProgress = () => {
383
802
  if (firstTokenAt === null) firstTokenAt = Date.now();
@@ -386,7 +805,7 @@ async function streamProviderTurn({
386
805
  if (now - lastProgressUpdate < 80) return;
387
806
  lastProgressUpdate = now;
388
807
 
389
- const activeEntry = [...toolMap.values()].pop();
808
+ const activeEntry = isAnthropic ? [...anthropicToolMap.values()].pop() : toolMerger.getLastEntry();
390
809
  const toolName = activeEntry?.function?.name || "";
391
810
  const args = activeEntry?.function?.arguments || "";
392
811
  const isWrite = /write_file|edit_file|multi_edit|apply_patch/.test(toolName);
@@ -415,23 +834,6 @@ async function streamProviderTurn({
415
834
  sink.setBusy?.(label);
416
835
  };
417
836
 
418
- const mergeOpenAITools = (deltas) => {
419
- for (const d of deltas) {
420
- const idx = d.index ?? 0;
421
- if (!toolMap.has(idx)) {
422
- toolMap.set(idx, {
423
- id: d.id || `call_${idx}`,
424
- type: "function",
425
- function: { name: "", arguments: "" },
426
- });
427
- }
428
- const entry = toolMap.get(idx);
429
- if (d.id) entry.id = d.id;
430
- if (d.function?.name) entry.function.name += d.function.name;
431
- if (d.function?.arguments) entry.function.arguments += d.function.arguments;
432
- }
433
- };
434
-
435
837
  const handleLine = (rawLine) => {
436
838
  const line = rawLine.replace(/\r$/, "");
437
839
  if (!line.startsWith("data:")) return;
@@ -447,47 +849,99 @@ async function streamProviderTurn({
447
849
 
448
850
  if (isAnthropic) {
449
851
  if (json.type === "message_start" && json.message?.usage) {
852
+ const u = json.message.usage;
450
853
  usage = {
451
- prompt_tokens: json.message.usage.input_tokens || 0,
452
- 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,
453
858
  };
454
859
  } else if (json.type === "message_delta") {
455
860
  if (json.delta?.stop_reason) finishReason = json.delta.stop_reason;
456
861
  if (json.usage) {
862
+ const u = json.usage;
457
863
  usage = {
458
864
  prompt_tokens: usage?.prompt_tokens || 0,
459
- 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,
460
869
  };
461
870
  }
462
871
  } else if (json.type === "content_block_start" && json.content_block?.type === "tool_use") {
463
- const idx = json.index ?? toolMap.size;
464
- toolMap.set(idx, {
872
+ const idx = json.index ?? anthropicToolMap.size;
873
+ const entry = toolMerger.pushDirect({
465
874
  id: json.content_block.id || `call_${idx}`,
466
875
  type: "function",
467
876
  function: { name: json.content_block.name || "", arguments: "" },
468
877
  });
878
+ anthropicToolMap.set(idx, entry);
469
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
+ }
470
899
  } else if (json.type === "content_block_delta") {
471
900
  if (json.delta?.type === "text_delta" && json.delta.text) {
472
901
  markFirstToken(true);
473
902
  streamUi.write(json.delta.text);
474
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);
475
915
  } else if (json.delta?.type === "input_json_delta" && json.delta.partial_json) {
476
916
  const idx = json.index ?? 0;
477
- const entry = toolMap.get(idx);
478
- if (entry) entry.function.arguments += 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
+ }
479
922
  updateToolProgress();
480
923
  }
481
924
  }
482
925
  return;
483
926
  }
484
927
 
485
- 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
+ }
486
936
  const choice = json.choices?.[0];
487
937
  if (choice?.finish_reason) finishReason = choice.finish_reason;
488
938
  const delta = choice?.delta;
939
+ const reasoningDelta = delta?.reasoning_content ?? delta?.reasoning ?? "";
940
+ if (reasoningDelta) {
941
+ reasoningText += reasoningDelta;
942
+ }
489
943
  if (delta?.tool_calls?.length) {
490
- mergeOpenAITools(delta.tool_calls);
944
+ toolMerger.merge(delta.tool_calls);
491
945
  updateToolProgress();
492
946
  }
493
947
  const token = delta?.content ?? json.response ?? "";
@@ -507,7 +961,10 @@ async function streamProviderTurn({
507
961
  buffer = lines.pop();
508
962
  for (const line of lines) handleLine(line);
509
963
  }
510
- if (buffer) handleLine(buffer);
964
+ buffer += decoder.decode();
965
+ if (buffer) {
966
+ for (const line of buffer.split("\n")) handleLine(line);
967
+ }
511
968
  } finally {
512
969
  if (!printedHeader) streamUi.stop(false);
513
970
  }
@@ -520,8 +977,23 @@ async function streamProviderTurn({
520
977
  const genSec = Math.max(0.001, (finishedAt - (firstTokenAt || startedAt)) / 1000);
521
978
  const tokPerSec = outTokens ? Number((outTokens / genSec).toFixed(1)) : 0;
522
979
 
523
- const toolCalls = [...toolMap.entries()].sort((a, b) => a[0] - b[0]).map(([, v]) => v);
524
- 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
+ };
525
997
  }
526
998
 
527
999
  async function completeProviderFast({
@@ -558,6 +1030,9 @@ module.exports = {
558
1030
  buildProviderUrls,
559
1031
  buildProviderHeaders,
560
1032
  toAnthropicMessages,
1033
+ toAnthropicTools,
1034
+ mergeOpenAISystemMessages,
1035
+ createToolCallMerger,
561
1036
  getModelCapabilities,
562
1037
  isRetryableStatus,
563
1038
  listProviderModels,