aegiscode 6.7.5 → 6.7.7

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.
@@ -0,0 +1,679 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * providers.js — direct streaming transport for two wire formats (plan P1
5
+ * §5.1). Pure Node (Electron main), no engine/routing logic: it only speaks
6
+ * the OpenAI-compatible and Anthropic Messages wire formats and normalises
7
+ * both to the shared `{ delta }` chunk shape plus a final result.
8
+ *
9
+ * - OpenAI-compatible: POST {baseURL}/v1/chat/completions (Bearer or keyless)
10
+ * - Anthropic Messages: POST {baseURL}/v1/messages (x-api-key when a key is
11
+ * set + version header; the credential header is omitted, never blanked)
12
+ *
13
+ * Every function returns the same normalised result the renderer already
14
+ * paints: { model, choices: [{ message: { content } }], usage? }.
15
+ */
16
+
17
+ /**
18
+ * Version/endpoint segments a user may already have typed into "base URL".
19
+ * Both OpenAI's and Anthropic's docs hand out base URLs that end in `/v1`, and
20
+ * the settings field invites pasting exactly that — so blindly appending
21
+ * `/v1/<endpoint>` produced `.../v1/v1/chat/completions`, a 404/400 on every
22
+ * call (defect A). A base URL that already names the endpoint
23
+ * (`…/v1/messages`) or carries a longer prefix (`…/openai/v1`) must normalise
24
+ * to the same single `/v1` too.
25
+ */
26
+ const TRAILING_ENDPOINT = /\/(?:chat\/completions|completions|messages|models)$/;
27
+ const TRAILING_VERSION = /\/v\d+(?:\.\d+)?$/;
28
+
29
+ /** Drop a trailing version and/or endpoint segment, keeping any prefix. */
30
+ function stripEndpointSuffix(path) {
31
+ let out = path;
32
+ for (;;) {
33
+ const trimmed = out.replace(/\/+$/, '');
34
+ if (TRAILING_ENDPOINT.test(trimmed)) {
35
+ out = trimmed.replace(TRAILING_ENDPOINT, '');
36
+ continue;
37
+ }
38
+ if (TRAILING_VERSION.test(trimmed)) {
39
+ out = trimmed.replace(TRAILING_VERSION, '');
40
+ continue;
41
+ }
42
+ return trimmed;
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Build the absolute request URL for a wire-format endpoint. `baseURL` may be
48
+ * bare (`https://api.openai.com`), versioned (`…/v1`), versioned with a
49
+ * trailing slash, or already name the endpoint — all of them resolve to
50
+ * exactly one version segment. Any other path prefix survives, as does a
51
+ * query string or fragment. The endpoint path is supplied by the caller so
52
+ * this stays the single shared path builder for both transports.
53
+ */
54
+ function endpointURL(baseURL, endpointPath) {
55
+ const raw = String(baseURL == null ? '' : baseURL).trim();
56
+ const cut = raw.search(/[?#]/);
57
+ const base = cut === -1 ? raw : raw.slice(0, cut);
58
+ const tail = cut === -1 ? '' : raw.slice(cut);
59
+ return `${stripEndpointSuffix(base)}${endpointPath}${tail}`;
60
+ }
61
+
62
+ /** A model id is mandatory over both wire formats; a URL is never one. */
63
+ function requireModel(model) {
64
+ if (typeof model === 'string' && model.trim()) return model.trim();
65
+ const err = new Error(
66
+ 'a model id is required — type the provider\'s model name (a base URL is not a model)'
67
+ );
68
+ err.status = 400;
69
+ throw err;
70
+ }
71
+
72
+ /**
73
+ * Normalise arbitrary message entries to a wire-safe { role, content } row.
74
+ *
75
+ * The agent loop threads tool traffic through this same list: an assistant row
76
+ * carrying `tool_calls` and a `tool` row carrying `tool_call_id` must survive
77
+ * verbatim, and an Anthropic row may carry structured `content` blocks
78
+ * (tool_use / tool_result) instead of a string. Both are passed through
79
+ * untouched; a plain string row normalises exactly as before.
80
+ */
81
+ function normalizeMessages(messages) {
82
+ const out = [];
83
+ if (!Array.isArray(messages)) return out;
84
+ for (const m of messages) {
85
+ if (!m) continue;
86
+ const role = m.role || 'user';
87
+ const structured = m.content != null && typeof m.content !== 'string';
88
+ const content = structured
89
+ ? m.content
90
+ : typeof m.content === 'string'
91
+ ? m.content
92
+ : m.text != null
93
+ ? String(m.text)
94
+ : '';
95
+ const row = { role, content };
96
+ if (Array.isArray(m.tool_calls) && m.tool_calls.length) row.tool_calls = m.tool_calls;
97
+ if (m.tool_call_id) row.tool_call_id = m.tool_call_id;
98
+ if (m.name) row.name = m.name;
99
+ out.push(row);
100
+ }
101
+ return out;
102
+ }
103
+
104
+ /**
105
+ * Append the single-shot `prompt` shorthand as a final user turn.
106
+ *
107
+ * The old builder *replaced* the whole list whenever `prompt` was non-empty:
108
+ * a turn carrying both an agent-loop history and a fresh prompt silently lost
109
+ * every prior message — including any system message, which is why the ported
110
+ * persona would have vanished on the renderer's own call path. History is now
111
+ * preserved and the prompt is appended once (skipped when the history already
112
+ * ends with that exact user turn, so a caller that sends both is not doubled).
113
+ */
114
+ function appendPrompt(out, prompt) {
115
+ if (prompt == null || prompt === '') return out;
116
+ const last = out[out.length - 1];
117
+ if (last && last.role === 'user' && last.content === prompt) return out;
118
+ out.push({ role: 'user', content: prompt });
119
+ return out;
120
+ }
121
+
122
+ /** OpenAI-format message list with the system message prepended. */
123
+ function openAIMessages(messages, system, prompt) {
124
+ const out = [];
125
+ if (system) out.push({ role: 'system', content: system });
126
+ out.push(...normalizeMessages(messages));
127
+ return appendPrompt(out, prompt);
128
+ }
129
+
130
+ /**
131
+ * Fold OpenAI-shaped tool traffic into Anthropic content-block turns.
132
+ *
133
+ * Anthropic's Messages API has no `tool` role and no `tool_calls` field: an
134
+ * assistant turn's tool calls must become `tool_use` content blocks, and each
135
+ * result must become a `tool_result` block inside the *following* `user`
136
+ * turn (parallel results share one turn, not one each — Anthropic 400s on
137
+ * separate turns). Passing the OpenAI shapes through verbatim, which
138
+ * `normalizeMessages` alone did, is a hard 400 on any real tool round-trip.
139
+ */
140
+ function toAnthropicToolTurns(messages) {
141
+ const out = [];
142
+ for (const m of messages) {
143
+ const role = m.role;
144
+ if (role === 'tool') {
145
+ const block = {
146
+ type: 'tool_result',
147
+ tool_use_id: m.tool_call_id || '',
148
+ content: typeof m.content === 'string' ? m.content : JSON.stringify(m.content ?? ''),
149
+ };
150
+ const last = out[out.length - 1];
151
+ if (last && last.role === 'user' && Array.isArray(last.content)) {
152
+ last.content.push(block);
153
+ } else {
154
+ out.push({ role: 'user', content: [block] });
155
+ }
156
+ continue;
157
+ }
158
+ if (role === 'assistant' && Array.isArray(m.tool_calls) && m.tool_calls.length) {
159
+ const blocks = [];
160
+ if (m.content) blocks.push({ type: 'text', text: String(m.content) });
161
+ for (const tc of m.tool_calls) {
162
+ const fn = tc.function || {};
163
+ let input = {};
164
+ if (typeof fn.arguments === 'string') {
165
+ try {
166
+ input = fn.arguments.trim() ? JSON.parse(fn.arguments) : {};
167
+ } catch {
168
+ input = {};
169
+ }
170
+ } else if (fn.arguments && typeof fn.arguments === 'object') {
171
+ input = fn.arguments;
172
+ }
173
+ blocks.push({ type: 'tool_use', id: tc.id || '', name: fn.name || '', input });
174
+ }
175
+ out.push({ role: 'assistant', content: blocks });
176
+ continue;
177
+ }
178
+ out.push(m);
179
+ }
180
+ return out;
181
+ }
182
+
183
+ /** Anthropic-format message list (system is a top-level field, not a turn). */
184
+ function buildAnthropicMessages(messages, prompt) {
185
+ return toAnthropicToolTurns(appendPrompt(normalizeMessages(messages), prompt));
186
+ }
187
+
188
+ // ── Tool-call accumulation ──────────────────────────────────────────────────
189
+ //
190
+ // Both wire formats stream a call in fragments: OpenAI sends
191
+ // `delta.tool_calls[{index, id?, function:{name?, arguments?}}]` and Anthropic
192
+ // sends a `content_block_start` (tool_use) followed by `input_json_delta`
193
+ // fragments keyed by content-block index. The parsers below used to keep only
194
+ // the text delta; the agent loop needs the calls themselves, so each transport
195
+ // accumulates them and reports a normalised `toolCalls: [{id, name, args}]`
196
+ // on its result (plus the provider's own shape, so a caller that speaks one
197
+ // wire format natively can read it unchanged).
198
+
199
+ /** Parse accumulated argument JSON; a malformed fragment degrades to {}. */
200
+ function parseToolArgs(raw) {
201
+ try {
202
+ const v = JSON.parse(raw);
203
+ return v && typeof v === 'object' ? v : {};
204
+ } catch {
205
+ return {};
206
+ }
207
+ }
208
+
209
+ /** Stamp one OpenAI `tool_calls` fragment list into an index-keyed map. */
210
+ function addOpenAIToolCall(map, tc) {
211
+ if (!tc) return;
212
+ const idx = tc.index == null ? 0 : tc.index;
213
+ const cur = map.get(idx) || { id: '', name: '', args: '' };
214
+ if (tc.id) cur.id = tc.id;
215
+ const fn = tc.function || {};
216
+ if (fn.name) cur.name = fn.name;
217
+ if (typeof fn.arguments === 'string') cur.args += fn.arguments;
218
+ map.set(idx, cur);
219
+ }
220
+
221
+ /** Map → normalised calls, preserving the provider's index order. */
222
+ function finalizeToolCalls(map) {
223
+ return [...map.entries()]
224
+ .sort((a, b) => a[0] - b[0])
225
+ .map(([, c]) => ({ id: c.id, name: c.name, args: parseToolArgs(c.args) }));
226
+ }
227
+
228
+ /** One OpenAI-format tool definition → Anthropic's {name, input_schema}. */
229
+ function openaiToAnthropicTool(tool) {
230
+ const fn = (tool && tool.function) || tool || {};
231
+ return {
232
+ name: fn.name,
233
+ description: fn.description || '',
234
+ input_schema: fn.parameters || { type: 'object', properties: {} },
235
+ };
236
+ }
237
+
238
+ /**
239
+ * Idle budget for a direct-provider stream: the longest silence tolerated
240
+ * between two real SSE `data:` frames. Without it `reader.read()` below waits
241
+ * forever on a provider that holds the socket open and never answers — the
242
+ * host hangs with no error and no way out but force-quit. Measured between
243
+ * payloads, never reset by a keep-alive: DeepSeek answers a stalled request
244
+ * with ": keep-alive" comments and nothing else, indefinitely, so a watchdog
245
+ * that treats those as progress can never fire.
246
+ * Generous enough that a slow reasoning model mid-answer is untouched.
247
+ */
248
+ const SSE_IDLE_TIMEOUT_MS = 2 * 60_000;
249
+
250
+ /** Read an SSE body, invoking onEvent(json) for each parsed `data:` payload. */
251
+ async function readSSE(res, onEvent, { idleTimeoutMs = SSE_IDLE_TIMEOUT_MS } = {}) {
252
+ const reader = res.body.getReader();
253
+ const decoder = new TextDecoder();
254
+ let buffer = '';
255
+ let lastPayloadAt = Date.now();
256
+ let keepAlives = 0;
257
+ const readWithIdleTimeout = async () => {
258
+ let timer;
259
+ const remaining = Math.max(0, idleTimeoutMs - (Date.now() - lastPayloadAt));
260
+ const timeout = new Promise((_, reject) => {
261
+ timer = setTimeout(() => {
262
+ reject(new Error(
263
+ keepAlives > 0
264
+ ? `stream stalled - only keep-alives for ${idleTimeoutMs / 1000}s`
265
+ : `stream stalled - no data for ${idleTimeoutMs / 1000}s`
266
+ ));
267
+ }, remaining);
268
+ });
269
+ try {
270
+ return await Promise.race([reader.read(), timeout]);
271
+ } finally {
272
+ clearTimeout(timer);
273
+ }
274
+ };
275
+ for (;;) {
276
+ let done, value;
277
+ try {
278
+ ({ done, value } = await readWithIdleTimeout());
279
+ } catch (err) {
280
+ reader.cancel().catch(() => {});
281
+ throw err;
282
+ }
283
+ if (done) break;
284
+ buffer += decoder.decode(value, { stream: true });
285
+ const lines = buffer.split('\n');
286
+ buffer = lines.pop(); // keep the trailing partial line
287
+ for (const raw of lines) {
288
+ const line = raw.trim();
289
+ if (!line.startsWith('data:')) {
290
+ if (line.startsWith(':')) keepAlives++;
291
+ continue;
292
+ }
293
+ lastPayloadAt = Date.now(); // a real frame: the stream is still speaking
294
+ const payload = line.slice(5).trim();
295
+ if (!payload || payload === '[DONE]') continue;
296
+ let json;
297
+ try {
298
+ json = JSON.parse(payload);
299
+ } catch {
300
+ continue; // partial line
301
+ }
302
+ onEvent(json);
303
+ }
304
+ }
305
+ }
306
+
307
+ /** POST with stream:true; streams SSE events or falls back to plain JSON. */
308
+ async function requestStream({ url, headers, body, signal, onEvent, idleTimeoutMs }) {
309
+ const res = await fetch(url, {
310
+ method: 'POST',
311
+ headers,
312
+ body: JSON.stringify(body),
313
+ signal,
314
+ });
315
+
316
+ if (!res.ok) {
317
+ const text = await res.text().catch(() => '');
318
+ const err = new Error(
319
+ `upstream ${res.status}: ${(text || res.statusText).slice(0, 300)}`
320
+ );
321
+ err.status = res.status;
322
+ throw err;
323
+ }
324
+
325
+ const contentType = res.headers.get('content-type') || '';
326
+ if (contentType.includes('text/event-stream')) {
327
+ await readSSE(res, onEvent, { idleTimeoutMs });
328
+ return;
329
+ }
330
+
331
+ // Server ignored stream:true and answered with plain JSON — one-shot fallback.
332
+ const data = await res.json().catch(() => null);
333
+ if (data) onEvent(data);
334
+ }
335
+
336
+ /** OpenAI-compatible streaming chat (Bearer auth or keyless for Ollama). */
337
+ async function openaiCompatible({
338
+ baseURL,
339
+ apiKey,
340
+ model,
341
+ messages,
342
+ system,
343
+ prompt,
344
+ maxTokens,
345
+ temperature,
346
+ tools,
347
+ toolChoice,
348
+ signal,
349
+ onDelta,
350
+ } = {}) {
351
+ const url = endpointURL(baseURL, '/v1/chat/completions');
352
+ const modelId = requireModel(model);
353
+ const headers = { 'Content-Type': 'application/json' };
354
+ if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
355
+
356
+ const body = {
357
+ model: modelId,
358
+ messages: openAIMessages(messages, system, prompt),
359
+ stream: true,
360
+ };
361
+ // `max_tokens` travels only when the caller actually stated one. Nothing is
362
+ // invented here — not even as a signature default (this parameter used to
363
+ // read `maxTokens = 4096`, so the field appeared even when engine.js passed
364
+ // undefined on purpose). The output length of a reasoning model is not
365
+ // predictable from the request: DeepSeek bills hidden chain-of-thought
366
+ // against the same budget, so a client-chosen 4096 never bounded the answer,
367
+ // it bounded the thinking and truncated turns that would otherwise have
368
+ // finished. engine.js's reasoningBudget() supplies a value when the /effort
369
+ // rung is the only lever the provider exposes; for everything else the
370
+ // provider's own default is the number it was tuned for, and omitting the
371
+ // key is how you ask for it.
372
+ if (Number(maxTokens) > 0) body.max_tokens = Number(maxTokens);
373
+ if (temperature != null) body.temperature = temperature;
374
+ // Only advertise tools when the caller passes a non-empty list — an empty
375
+ // `tools: []` is a 400 on some gateways.
376
+ if (Array.isArray(tools) && tools.length) {
377
+ body.tools = tools;
378
+ if (toolChoice) body.tool_choice = toolChoice;
379
+ }
380
+
381
+ let fullText = '';
382
+ let resultModel = modelId;
383
+ let usage = null;
384
+ let finishReason = null;
385
+ const toolCallMap = new Map(); // index → {id, name, args}
386
+
387
+ await requestStream({
388
+ url,
389
+ headers,
390
+ body,
391
+ signal,
392
+ onEvent: (json) => {
393
+ if (json.error) return;
394
+ if (json.model) resultModel = json.model;
395
+ if (json.usage) usage = json.usage;
396
+ const choice = json.choices && json.choices[0];
397
+ if (choice && choice.finish_reason) finishReason = choice.finish_reason;
398
+ // `delta.content` is an INCREMENT; `message.content` is a SNAPSHOT of
399
+ // the whole message. Collapsing them with `||` made any stream that
400
+ // ends with a message snapshot append the entire answer a second time —
401
+ // the same duplication the shared client had. Emit only the unseen tail.
402
+ const inc = choice && choice.delta && choice.delta.content;
403
+ const snap = choice && choice.message && choice.message.content;
404
+ let add = '';
405
+ if (typeof inc === 'string' && inc) {
406
+ add = inc;
407
+ fullText += inc;
408
+ } else if (typeof snap === 'string' && snap) {
409
+ if (!fullText) {
410
+ add = snap;
411
+ fullText = snap;
412
+ } else if (snap.startsWith(fullText)) {
413
+ add = snap.slice(fullText.length);
414
+ fullText = snap;
415
+ }
416
+ // Disjoint from what was already shown: appending would duplicate, so
417
+ // keep what the caller has seen.
418
+ }
419
+ if (add && onDelta) onDelta({ delta: add });
420
+ // Streamed fragments (delta.tool_calls) and the whole-answer shape a
421
+ // non-streaming fallback returns (message.tool_calls) both land here.
422
+ const fragments =
423
+ (choice && choice.delta && choice.delta.tool_calls) ||
424
+ (choice && choice.message && choice.message.tool_calls);
425
+ if (Array.isArray(fragments)) {
426
+ for (const tc of fragments) addOpenAIToolCall(toolCallMap, tc);
427
+ }
428
+ },
429
+ });
430
+
431
+ const toolCalls = finalizeToolCalls(toolCallMap);
432
+ const message = { content: fullText };
433
+ if (toolCalls.length) {
434
+ message.tool_calls = toolCalls.map((c) => ({
435
+ id: c.id,
436
+ type: 'function',
437
+ function: { name: c.name, arguments: JSON.stringify(c.args) },
438
+ }));
439
+ }
440
+ const result = {
441
+ model: resultModel,
442
+ choices: [{ message, ...(finishReason ? { finish_reason: finishReason } : {}) }],
443
+ };
444
+ if (usage) result.usage = usage;
445
+ if (toolCalls.length) result.toolCalls = toolCalls;
446
+ return result;
447
+ }
448
+
449
+ /**
450
+ * Normalise an Anthropic usage object into the one shape every caller reads.
451
+ *
452
+ * Anthropic reports tokens split across TWO events and in TWO pieces:
453
+ * - `message_start` carries the prompt side (`input_tokens`, plus
454
+ * `cache_creation_input_tokens` / `cache_read_input_tokens`);
455
+ * - `message_delta` carries ONLY the cumulative `output_tokens`.
456
+ * Nothing in the wire format is called `total_tokens`, which is the single
457
+ * field the renderer prints — so every Anthropic-compatible model reported no
458
+ * token count at all while OpenAI-compatible ones showed one.
459
+ *
460
+ * Returns `null` for an empty/absent usage so the caller can omit the field
461
+ * rather than advertise a fake `0`.
462
+ */
463
+ function normalizeAnthropicUsage(raw) {
464
+ if (!raw || typeof raw !== 'object') return null;
465
+ const num = (v) => (typeof v === 'number' && Number.isFinite(v) && v > 0 ? v : 0);
466
+ const input = num(raw.input_tokens);
467
+ const output = num(raw.output_tokens);
468
+ const cacheRead = num(raw.cache_read_input_tokens);
469
+ const cacheCreate = num(raw.cache_creation_input_tokens);
470
+ // Cached prompt tokens are still prompt tokens: OpenAI counts them inside
471
+ // `prompt_tokens` (exposing the subset as prompt_tokens_details.cached), so
472
+ // folding them in keeps the two wire formats' numbers comparable — and a
473
+ // turn that reads a big cached prefix must not look free.
474
+ const prompt = input + cacheRead + cacheCreate;
475
+ if (!prompt && !output) return null;
476
+ const usage = {
477
+ // Anthropic's own names first, so a caller reading native blocks keeps both
478
+ // halves of the split it used to get (it previously got only output_tokens).
479
+ input_tokens: input,
480
+ output_tokens: output,
481
+ // OpenAI spellings, read by the renderer and every other transport here.
482
+ prompt_tokens: prompt,
483
+ completion_tokens: output,
484
+ total_tokens: prompt + output,
485
+ };
486
+ if (cacheRead) usage.cache_read_input_tokens = cacheRead;
487
+ if (cacheCreate) usage.cache_creation_input_tokens = cacheCreate;
488
+ return usage;
489
+ }
490
+
491
+ /** Anthropic Messages streaming chat (x-api-key + anthropic-version). */
492
+ async function anthropicMessages({
493
+ baseURL,
494
+ apiKey,
495
+ model,
496
+ messages,
497
+ system,
498
+ prompt,
499
+ maxTokens,
500
+ temperature,
501
+ tools,
502
+ toolChoice,
503
+ signal,
504
+ onDelta,
505
+ } = {}) {
506
+ const url = endpointURL(baseURL, '/v1/messages');
507
+ const modelId = requireModel(model);
508
+ const headers = {
509
+ 'Content-Type': 'application/json',
510
+ 'anthropic-version': '2023-06-01',
511
+ };
512
+ // Omit the credential header entirely when no key is configured: an empty
513
+ // `x-api-key: ''` is still a credential attempt and turns a proxied/keyless
514
+ // endpoint into a 401 (defect #3).
515
+ if (apiKey) headers['x-api-key'] = apiKey;
516
+
517
+ const body = {
518
+ model: modelId,
519
+ stream: true,
520
+ messages: buildAnthropicMessages(messages, prompt),
521
+ };
522
+ // State the field only when the caller actually gave a number — the same rule
523
+ // the OpenAI-compatible transport below follows, and for the same reason.
524
+ //
525
+ // There is no client-side floor or ceiling constant here, deliberately. The
526
+ // Messages API does require `max_tokens`, but a number this side can only
527
+ // guess at is exactly what used to truncate reasoning turns: the budget is
528
+ // spent on hidden chain-of-thought before the first visible token, so the
529
+ // request "completed" having answered nothing. The size of a call belongs to
530
+ // whoever owns the model — engine.js's reasoningBudget() (the /effort rung,
531
+ // which is what the caller is passing here) or the platform itself, which
532
+ // sizes the pooled class from `effort` server-side and reads a body
533
+ // max_tokens as a ceiling OVER that ladder. So an unstated budget goes out
534
+ // unstated and the platform's value applies; this transport does not
535
+ // substitute one of its own.
536
+ if (Number(maxTokens) > 0) body.max_tokens = Number(maxTokens);
537
+ if (system) body.system = system;
538
+ if (temperature != null) body.temperature = temperature;
539
+ // Anthropic wants {name, description, input_schema} — an OpenAI-shaped list
540
+ // is converted rather than sent raw (this is the documented footgun).
541
+ if (Array.isArray(tools) && tools.length) {
542
+ body.tools = tools.map((t) => (t && t.function ? openaiToAnthropicTool(t) : t));
543
+ if (toolChoice) body.tool_choice = toolChoice;
544
+ }
545
+
546
+ let fullText = '';
547
+ let resultModel = modelId;
548
+ let stopReason = null;
549
+ // Accumulated, never replaced: the prompt half arrives on `message_start` and
550
+ // the output half on `message_delta`, so assignment (which is what this used
551
+ // to do) left whichever event came last owning the whole object.
552
+ const usageAcc = {};
553
+ const toolBlocks = new Map(); // content-block index → {id, name, args}
554
+
555
+ await requestStream({
556
+ url,
557
+ headers,
558
+ body,
559
+ signal,
560
+ onEvent: (json) => {
561
+ if (!json || json.type === 'error' || json.error) {
562
+ if (json && (json.error || json.type === 'error')) {
563
+ const e = json.error || {};
564
+ const msg =
565
+ (typeof e === 'string' ? e : e.message) || 'anthropic stream error';
566
+ const err = new Error(msg);
567
+ err.status = err.status || 400;
568
+ throw err;
569
+ }
570
+ return;
571
+ }
572
+ if (json.type === 'message_start' && json.message) {
573
+ if (json.message.model) resultModel = json.message.model;
574
+ if (json.message.usage) Object.assign(usageAcc, json.message.usage);
575
+ }
576
+ // A tool_use block opens here; its arguments arrive as
577
+ // input_json_delta fragments below (the text deltas we already parsed
578
+ // are `text_delta`, which carry `.text`).
579
+ if (json.type === 'content_block_start' && json.content_block) {
580
+ const cb = json.content_block;
581
+ if (cb.type === 'tool_use') {
582
+ toolBlocks.set(json.index == null ? 0 : json.index, {
583
+ id: cb.id || '',
584
+ name: cb.name || '',
585
+ args: '',
586
+ });
587
+ }
588
+ }
589
+ if (json.type === 'content_block_delta' && json.delta) {
590
+ if (json.delta.type === 'input_json_delta') {
591
+ const block = toolBlocks.get(json.index == null ? 0 : json.index);
592
+ if (block) block.args += json.delta.partial_json || '';
593
+ } else {
594
+ const delta = typeof json.delta.text === 'string' ? json.delta.text : '';
595
+ if (delta) {
596
+ fullText += delta;
597
+ if (onDelta) onDelta({ delta });
598
+ }
599
+ }
600
+ }
601
+ if (json.type === 'message_delta') {
602
+ // `message_delta.usage` is PARTIAL — `output_tokens` only — so it is
603
+ // merged onto what `message_start` already reported, not assigned over
604
+ // it. Assigning it wholesale is what made prompt tokens vanish from
605
+ // every Anthropic-compatible call's accounting.
606
+ if (json.usage) {
607
+ for (const [k, v] of Object.entries(json.usage)) {
608
+ if (k === 'output_tokens') continue;
609
+ // Only take a field this event carries a real value for. Some
610
+ // Anthropic-compatible gateways echo the whole usage object here
611
+ // with a zeroed prompt side, and letting a bogus 0 overwrite the
612
+ // count message_start already reported is the same data loss in a
613
+ // different costume.
614
+ if (usageAcc[k] == null || (typeof v === 'number' && v > 0)) usageAcc[k] = v;
615
+ }
616
+ if (typeof json.usage.output_tokens === 'number') {
617
+ usageAcc.output_tokens = json.usage.output_tokens;
618
+ }
619
+ }
620
+ if (json.delta && json.delta.stop_reason) stopReason = json.delta.stop_reason;
621
+ }
622
+ // Non-streaming fallback: a full Anthropic Message with content blocks.
623
+ if (Array.isArray(json.content)) {
624
+ if (json.model) resultModel = json.model;
625
+ if (json.usage) Object.assign(usageAcc, json.usage);
626
+ if (json.stop_reason) stopReason = json.stop_reason;
627
+ json.content.forEach((block, i) => {
628
+ if (block && block.type === 'text' && block.text && !fullText) {
629
+ fullText += block.text;
630
+ if (onDelta) onDelta({ delta: block.text });
631
+ }
632
+ if (block && block.type === 'tool_use') {
633
+ toolBlocks.set(i, {
634
+ id: block.id || '',
635
+ name: block.name || '',
636
+ args: JSON.stringify(block.input || {}),
637
+ });
638
+ }
639
+ });
640
+ }
641
+ },
642
+ });
643
+
644
+ const toolCalls = finalizeToolCalls(toolBlocks);
645
+ const result = { model: resultModel, choices: [{ message: { content: fullText } }] };
646
+ const usage = normalizeAnthropicUsage(usageAcc);
647
+ if (usage) result.usage = usage;
648
+ if (stopReason) result.stop_reason = stopReason;
649
+ if (toolCalls.length) {
650
+ result.toolCalls = toolCalls;
651
+ // Provider-native shape, for callers that read Anthropic blocks directly.
652
+ result.choices[0].message.tool_calls = toolCalls.map((c) => ({
653
+ id: c.id,
654
+ type: 'function',
655
+ function: { name: c.name, arguments: JSON.stringify(c.args) },
656
+ }));
657
+ }
658
+ return result;
659
+ }
660
+
661
+ module.exports = {
662
+ normalizeMessages,
663
+ openAIMessages,
664
+ buildAnthropicMessages,
665
+ appendPrompt,
666
+ readSSE,
667
+ requestStream,
668
+ openaiCompatible,
669
+ anthropicMessages,
670
+ // shared path builder + model-id guard (unit-tested directly)
671
+ endpointURL,
672
+ stripEndpointSuffix,
673
+ requireModel,
674
+ // tool-call plumbing (unit-tested directly)
675
+ addOpenAIToolCall,
676
+ finalizeToolCalls,
677
+ parseToolArgs,
678
+ openaiToAnthropicTool,
679
+ };