@starci/ai-router 0.6.0 → 0.7.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.
@@ -1,16 +1,32 @@
1
1
  import { isPlainRecord } from "@starci/common";
2
2
  import { AttemptError } from "../attempt.js";
3
+ import { markerOf, MAX_CACHE_BREAKPOINTS, takesCacheControl, withoutCacheControl } from "../cache.js";
4
+ import { digest, memoryOf } from "./thinking-memory.js";
3
5
  /**
4
- * The Anthropic Messages API (`POST /v1/messages`, read 2026-10-31 at platform.claude.com/docs/en/api/messages and
5
- * /build-with-claude/streaming) behind the OpenAI chat-completions shape. docs/design.md, "The Anthropic adapter", maps
6
- * every field; what has no counterpart is refused (`n` above 1) or dropped (seed, penalties, logprobs).
6
+ * The Anthropic Messages API (`POST /v1/messages`) behind the OpenAI chat-completions shape. Read at
7
+ * platform.claude.com/docs/en: /api/messages and /build-with-claude/streaming on 2026-10-31; /build-with-claude/
8
+ * prompt-caching, /thinking, /thinking-tool-workflows and /preserved-thinking on 2026-11-10. docs/design.md, "The
9
+ * Anthropic adapter", maps every field; what has no counterpart is refused (`n` above 1) or dropped (seed, penalties,
10
+ * logprobs).
7
11
  */
8
12
  /** The `anthropic-version` sent unless the provider's headers set one. */
9
13
  export const ANTHROPIC_VERSION = "2023-06-01";
10
- /** The `max_tokens` sent when neither the request nor the provider's `defaultMaxTokens` gives one: Anthropic requires it. */
11
- export const ANTHROPIC_DEFAULT_MAX_TOKENS = 4096;
14
+ /**
15
+ * The `max_tokens` sent when neither the request nor the provider's `defaultMaxTokens` gives one: Anthropic requires it,
16
+ * and on current models thinking counts toward it, so a low cap cuts an answer before its first visible token.
17
+ */
18
+ export const ANTHROPIC_DEFAULT_MAX_TOKENS = 16_000;
12
19
  /** Fields an OpenAI caller may send that Anthropic takes as they are. */
13
20
  const PASSTHROUGH = ["temperature", "top_p", "top_k", "thinking", "output_config", "service_tier"];
21
+ /** What reading an answer needs of its request when the provider replays thinking: where to remember it, and its prefix. */
22
+ class ReplayContext {
23
+ memory;
24
+ prefix;
25
+ constructor(memory, prefix) {
26
+ this.memory = memory;
27
+ this.prefix = prefix;
28
+ }
29
+ }
14
30
  function refuse(message) {
15
31
  return new AttemptError("invalid_request", `anthropic: ${message}`);
16
32
  }
@@ -21,6 +37,18 @@ function textOf(content) {
21
37
  return "";
22
38
  return content.map((part) => (isPlainRecord(part) && typeof part.text === "string" ? part.text : "")).join("");
23
39
  }
40
+ /** A block with the cache breakpoint its OpenAI part or message carries, when it carries one. */
41
+ function marked(block, holder) {
42
+ const marker = markerOf(holder);
43
+ return marker === undefined ? block : { ...block, cache_control: marker };
44
+ }
45
+ /** Puts a message's own breakpoint on its last block. */
46
+ function markLast(list, message) {
47
+ const last = list.at(-1);
48
+ if (last !== undefined)
49
+ list[list.length - 1] = marked(last, message);
50
+ return list;
51
+ }
24
52
  /** An OpenAI `image_url` part as an Anthropic image block: a data URL becomes base64, anything else a URL source. */
25
53
  function imageBlock(part) {
26
54
  const image = isPlainRecord(part.image_url) ? part.image_url.url : part.image_url;
@@ -45,15 +73,33 @@ function blocks(content) {
45
73
  continue;
46
74
  if (part.type === "text") {
47
75
  if (typeof part.text === "string" && part.text !== "")
48
- out.push({ type: "text", text: part.text });
76
+ out.push(marked({ type: "text", text: part.text }, part));
49
77
  }
50
78
  else if (part.type === "image_url")
51
- out.push(imageBlock(part));
79
+ out.push(marked(imageBlock(part), part));
52
80
  else
53
81
  out.push(part);
54
82
  }
55
83
  return out;
56
84
  }
85
+ /** A system or developer message as system blocks: one block, or one per text part when a part marks a breakpoint. */
86
+ function systemBlocks(message) {
87
+ const content = message.content;
88
+ const parts = Array.isArray(content) ? content.filter(isPlainRecord) : [];
89
+ if (!parts.some((part) => markerOf(part) !== undefined)) {
90
+ const text = textOf(content);
91
+ return text === "" ? [] : [marked({ type: "text", text }, message)];
92
+ }
93
+ return markLast(parts.filter((part) => typeof part.text === "string" && part.text !== "").map((part) => marked({ type: "text", text: part.text }, part)), message);
94
+ }
95
+ /** A tool message as a `tool_result` block; its breakpoint is the message's own, else its last marked part's. */
96
+ function toolResult(message) {
97
+ if (typeof message.tool_call_id !== "string")
98
+ throw refuse("a tool message needs tool_call_id");
99
+ const parts = Array.isArray(message.content) ? message.content.filter(isPlainRecord) : [];
100
+ const holder = markerOf(message) === undefined ? (parts.findLast((part) => markerOf(part) !== undefined) ?? {}) : message;
101
+ return marked({ type: "tool_result", tool_use_id: message.tool_call_id, content: textOf(message.content) }, holder);
102
+ }
57
103
  function toolUse(call) {
58
104
  if (!isPlainRecord(call) || !isPlainRecord(call.function) || typeof call.id !== "string" || typeof call.function.name !== "string")
59
105
  throw refuse("a tool call needs an id and a function name");
@@ -71,47 +117,71 @@ function toolUse(call) {
71
117
  throw refuse(`the arguments of tool call ${call.id} must be a JSON object`);
72
118
  return { type: "tool_use", id: call.id, name: call.function.name, input };
73
119
  }
74
- /** OpenAI messages as Anthropic's system text and alternating user/assistant turns; tool results ride in user turns. */
75
- function conversation(messages) {
120
+ /** The key a turn is remembered by: the ids of the tool calls it made, in order. */
121
+ function turnKey(content) {
122
+ return content
123
+ .filter((block) => block.type === "tool_use")
124
+ .map((block) => String(block.id))
125
+ .join(",");
126
+ }
127
+ /** OpenAI messages as Anthropic's system blocks and alternating user/assistant turns; tool results ride in user turns. */
128
+ function conversation(messages, memory) {
76
129
  if (!Array.isArray(messages))
77
130
  throw refuse("messages must be a list");
78
131
  const system = [];
79
132
  const turns = [];
80
- const push = (role, content) => {
133
+ const push = (role, content, replay) => {
81
134
  if (content.length === 0)
82
135
  return;
83
136
  const last = turns.at(-1);
84
137
  // Anthropic wants turns to alternate: consecutive turns of one role (several tool results) merge into one.
85
- if (last?.role === role)
138
+ if (last?.role === role) {
86
139
  last.content.push(...content);
140
+ // A turn merged from two messages is no longer the answer that was remembered.
141
+ last.replay = undefined;
142
+ }
87
143
  else
88
- turns.push({ role, content });
144
+ turns.push({ role, content, replay });
89
145
  };
90
146
  for (const message of messages) {
91
147
  if (!isPlainRecord(message))
92
148
  throw refuse("every message must be an object");
93
149
  const role = message.role;
94
- if (role === "system" || role === "developer") {
95
- const text = textOf(message.content);
96
- if (text !== "")
97
- system.push(text);
98
- }
150
+ if (role === "system" || role === "developer")
151
+ system.push(...systemBlocks(message));
99
152
  else if (role === "user")
100
- push("user", blocks(message.content));
153
+ push("user", markLast(blocks(message.content), message));
101
154
  else if (role === "assistant") {
102
155
  const calls = Array.isArray(message.tool_calls) ? message.tool_calls.map(toolUse) : [];
103
- push("assistant", [...blocks(message.content), ...calls]);
104
- }
105
- else if (role === "tool") {
106
- if (typeof message.tool_call_id !== "string")
107
- throw refuse("a tool message needs tool_call_id");
108
- push("user", [{ type: "tool_result", tool_use_id: message.tool_call_id, content: textOf(message.content) }]);
156
+ push("assistant", markLast([...blocks(message.content), ...calls], message), calls.length === 0 ? undefined : memory?.get(turnKey(calls)));
109
157
  }
158
+ else if (role === "tool")
159
+ push("user", [toolResult(message)]);
110
160
  else
111
161
  throw refuse(`the message role ${String(role)} has no Anthropic counterpart`);
112
162
  }
113
163
  return { system, turns };
114
164
  }
165
+ /**
166
+ * The turns as they are sent, with the digest of the whole request. A remembered turn replaces its rebuilt one, thinking
167
+ * included, only while the request up to it is the request that produced it; else the rebuilt turn goes, without thinking.
168
+ */
169
+ function settle(turns, head, memory) {
170
+ if (memory === null)
171
+ return { messages: turns.map(({ role, content }) => ({ role, content })), prefix: "" };
172
+ let prefix = digest("", head);
173
+ const messages = turns.map((turn) => {
174
+ let content = turn.content;
175
+ if (turn.replay?.prefix === prefix) {
176
+ const kept = turn.content.at(-1);
177
+ // The client's breakpoint on the turn's last block moves to the last block of the replayed turn.
178
+ content = markLast([...turn.replay.content], kept ?? {});
179
+ }
180
+ prefix = digest(prefix, { role: turn.role, content });
181
+ return { role: turn.role, content };
182
+ });
183
+ return { messages, prefix };
184
+ }
115
185
  function tools(list) {
116
186
  if (!Array.isArray(list) || list.length === 0)
117
187
  return undefined;
@@ -119,12 +189,12 @@ function tools(list) {
119
189
  if (!isPlainRecord(tool) || tool.type !== "function" || !isPlainRecord(tool.function))
120
190
  return tool;
121
191
  const fn = tool.function;
122
- return {
192
+ return marked({
123
193
  name: fn.name,
124
194
  ...(typeof fn.description === "string" ? { description: fn.description } : {}),
125
195
  input_schema: isPlainRecord(fn.parameters) ? fn.parameters : { type: "object", properties: {} },
126
196
  ...(fn.strict === true ? { strict: true } : {}),
127
- };
197
+ }, tool);
128
198
  });
129
199
  }
130
200
  function toolChoice(choice, parallel) {
@@ -141,31 +211,44 @@ function toolChoice(choice, parallel) {
141
211
  out = { ...(out ?? { type: "auto" }), disable_parallel_tool_use: true };
142
212
  return out;
143
213
  }
144
- /** Builds the Anthropic Messages body of an OpenAI chat request. */
145
- export function toAnthropicRequest(body, model, stream, provider) {
214
+ function breakpoints(list) {
215
+ return list.filter((item) => isPlainRecord(item) && markerOf(item) !== undefined).length;
216
+ }
217
+ /** Builds the Anthropic Messages body of an OpenAI chat request, with the digest of its prefix when thinking is replayed. */
218
+ function anthropicRequest(body, model, stream, provider) {
146
219
  if (typeof body.n === "number" && body.n > 1)
147
220
  throw refuse("n above 1 is not supported");
148
- const { system, turns } = conversation(body.messages);
221
+ const memory = memoryOf(provider);
222
+ const { system, turns } = conversation(body.messages, memory);
149
223
  const format = body.response_format;
150
224
  let outputConfig = body.output_config;
151
225
  if (isPlainRecord(format) && format.type === "json_object")
152
- system.push("Answer with one JSON object and nothing else.");
226
+ system.push({ type: "text", text: "Answer with one JSON object and nothing else." });
153
227
  if (isPlainRecord(format) && format.type === "json_schema" && isPlainRecord(format.json_schema) && isPlainRecord(format.json_schema.schema)) {
154
228
  outputConfig = { ...(isPlainRecord(outputConfig) ? outputConfig : {}), format: { type: "json_schema", schema: format.json_schema.schema } };
155
229
  }
156
230
  const maxTokens = [body.max_completion_tokens, body.max_tokens].find((value) => typeof value === "number") ?? provider.defaultMaxTokens ?? ANTHROPIC_DEFAULT_MAX_TOKENS;
157
231
  const stop = typeof body.stop === "string" ? [body.stop] : Array.isArray(body.stop) ? body.stop : undefined;
158
- const out = { model, max_tokens: maxTokens, messages: turns };
232
+ const mapped = tools(body.tools);
233
+ // One string as before when nothing marks a breakpoint; blocks when a breakpoint needs one to sit on.
234
+ const systemField = system.length === 0 ? undefined : breakpoints(system) === 0 ? system.map((block) => String(block.text)).join("\n\n") : system;
235
+ const { messages, prefix } = settle(turns, { system: systemField, tools: mapped }, memory);
236
+ const automatic = markerOf(body);
237
+ const marks = breakpoints(system) + breakpoints(mapped ?? []) + messages.reduce((sum, message) => sum + breakpoints(message.content), 0) + (automatic === undefined ? 0 : 1);
238
+ if (marks > MAX_CACHE_BREAKPOINTS)
239
+ throw refuse(`the request marks ${String(marks)} cache breakpoints, the top-level one included; at most ${String(MAX_CACHE_BREAKPOINTS)} are taken`);
240
+ const out = { model, max_tokens: maxTokens, messages };
159
241
  for (const field of PASSTHROUGH)
160
242
  if (body[field] !== undefined)
161
243
  out[field] = body[field];
162
244
  if (outputConfig !== undefined)
163
245
  out.output_config = outputConfig;
164
- if (system.length > 0)
165
- out.system = system.join("\n\n");
246
+ if (systemField !== undefined)
247
+ out.system = systemField;
248
+ if (automatic !== undefined)
249
+ out.cache_control = automatic;
166
250
  if (stop !== undefined)
167
251
  out.stop_sequences = stop;
168
- const mapped = tools(body.tools);
169
252
  if (mapped !== undefined)
170
253
  out.tools = mapped;
171
254
  const choice = toolChoice(body.tool_choice, body.parallel_tool_calls);
@@ -175,7 +258,11 @@ export function toAnthropicRequest(body, model, stream, provider) {
175
258
  out.metadata = { user_id: body.user };
176
259
  if (stream)
177
260
  out.stream = true;
178
- return out;
261
+ return { body: out, prefix };
262
+ }
263
+ /** Builds the Anthropic Messages body of an OpenAI chat request. */
264
+ export function toAnthropicRequest(body, model, stream, provider) {
265
+ return anthropicRequest(body, model, stream, provider).body;
179
266
  }
180
267
  /** Anthropic's stop reason as OpenAI's finish reason. */
181
268
  export function finishReason(reason) {
@@ -198,35 +285,115 @@ export function finishReason(reason) {
198
285
  function count(value) {
199
286
  return typeof value === "number" && Number.isFinite(value) ? value : 0;
200
287
  }
201
- /** Anthropic usage as OpenAI usage: the prompt counts the cache reads and writes, which Anthropic lists apart. */
202
- export function usageOf(usage, outputOverride) {
288
+ /**
289
+ * Anthropic usage as OpenAI usage. Anthropic lists the uncached input, the cache reads and the cache writes apart; the
290
+ * prompt here is their sum, with the reads and writes named in `prompt_tokens_details` (`cached_tokens`,
291
+ * `cache_write_tokens`, and `cache_write_1h_tokens` when the answer splits writes by lifetime). Thinking tokens, when
292
+ * the answer counts them, are `completion_tokens_details.reasoning_tokens`.
293
+ */
294
+ export function usageOf(usage) {
203
295
  if (!isPlainRecord(usage))
204
296
  return undefined;
205
297
  const cached = count(usage.cache_read_input_tokens);
206
- const prompt = count(usage.input_tokens) + count(usage.cache_creation_input_tokens) + cached;
207
- const completion = outputOverride ?? count(usage.output_tokens);
208
- return { prompt_tokens: prompt, completion_tokens: completion, total_tokens: prompt + completion, prompt_tokens_details: { cached_tokens: cached } };
298
+ const written = count(usage.cache_creation_input_tokens);
299
+ const prompt = count(usage.input_tokens) + written + cached;
300
+ const completion = count(usage.output_tokens);
301
+ const lifetimes = isPlainRecord(usage.cache_creation) ? { cache_write_1h_tokens: count(usage.cache_creation.ephemeral_1h_input_tokens) } : {};
302
+ const thinking = isPlainRecord(usage.output_tokens_details) ? usage.output_tokens_details.thinking_tokens : undefined;
303
+ return {
304
+ prompt_tokens: prompt,
305
+ completion_tokens: completion,
306
+ total_tokens: prompt + completion,
307
+ prompt_tokens_details: { cached_tokens: cached, cache_write_tokens: written, ...lifetimes },
308
+ ...(typeof thinking === "number" ? { completion_tokens_details: { reasoning_tokens: thinking } } : {}),
309
+ };
310
+ }
311
+ function isThinking(block) {
312
+ return block.type === "thinking" || block.type === "redacted_thinking";
313
+ }
314
+ /** Remembers an answer that thought and then called tools, so the next request of the loop can send it back whole. */
315
+ function remember(context, content) {
316
+ if (!(context instanceof ReplayContext))
317
+ return;
318
+ const key = turnKey(content);
319
+ if (key !== "" && content.some(isThinking))
320
+ context.memory.set(key, { content, prefix: context.prefix });
209
321
  }
210
322
  /** An Anthropic message as an OpenAI `chat.completion`. */
211
- export function fromAnthropicMessage(json) {
323
+ export function fromAnthropicMessage(json, context) {
212
324
  const content = Array.isArray(json.content) ? json.content.filter(isPlainRecord) : [];
325
+ remember(context, content);
213
326
  const text = content
214
327
  .filter((block) => block.type === "text" && typeof block.text === "string")
215
328
  .map((block) => String(block.text))
216
329
  .join("");
330
+ // Thinking text is a summary or a progress note, present only when the request asked to see it.
331
+ const reasoning = content
332
+ .filter((block) => block.type === "thinking" && typeof block.thinking === "string" && block.thinking !== "")
333
+ .map((block) => String(block.thinking))
334
+ .join("\n\n");
217
335
  const calls = content
218
336
  .filter((block) => block.type === "tool_use")
219
337
  .map((block) => ({ id: String(block.id), type: "function", function: { name: String(block.name), arguments: JSON.stringify(block.input ?? {}) } }));
220
338
  const usage = usageOf(json.usage);
339
+ const details = isPlainRecord(json.stop_details) ? json.stop_details : {};
340
+ const refusal = json.stop_reason === "refusal" ? { refusal: [details.explanation, details.category].find((value) => typeof value === "string") ?? "refusal" } : {};
221
341
  return {
222
342
  id: typeof json.id === "string" ? json.id : "anthropic",
223
343
  object: "chat.completion",
224
344
  created: Math.floor(Date.now() / 1000),
225
345
  model: typeof json.model === "string" ? json.model : "",
226
- choices: [{ index: 0, message: { role: "assistant", content: text === "" && calls.length > 0 ? null : text, ...(calls.length > 0 ? { tool_calls: calls } : {}) }, finish_reason: finishReason(json.stop_reason) }],
346
+ choices: [
347
+ {
348
+ index: 0,
349
+ message: { role: "assistant", content: text === "" && calls.length > 0 ? null : text, ...(reasoning === "" ? {} : { reasoning_content: reasoning }), ...(calls.length > 0 ? { tool_calls: calls } : {}), ...refusal },
350
+ finish_reason: finishReason(json.stop_reason),
351
+ },
352
+ ],
227
353
  ...(usage === undefined ? {} : { usage }),
228
354
  };
229
355
  }
356
+ /** The content blocks of a streamed answer, put back together to be remembered; `whole` turns false when a piece cannot be. */
357
+ class StreamedTurn {
358
+ #blocks = new Map();
359
+ #json = new Map();
360
+ #whole = true;
361
+ start(index, block) {
362
+ this.#blocks.set(index, { ...block });
363
+ }
364
+ delta(index, delta) {
365
+ const block = this.#blocks.get(index);
366
+ if (block === undefined)
367
+ this.#whole = false;
368
+ else if (delta.type === "text_delta")
369
+ block.text = `${typeof block.text === "string" ? block.text : ""}${String(delta.text)}`;
370
+ else if (delta.type === "thinking_delta")
371
+ block.thinking = `${typeof block.thinking === "string" ? block.thinking : ""}${String(delta.thinking)}`;
372
+ else if (delta.type === "signature_delta")
373
+ block.signature = delta.signature;
374
+ else if (delta.type === "input_json_delta")
375
+ this.#json.set(index, `${this.#json.get(index) ?? ""}${String(delta.partial_json)}`);
376
+ // A delta this adapter does not know (citations, a later addition) leaves a block it cannot echo exactly.
377
+ else
378
+ this.#whole = false;
379
+ }
380
+ /** The blocks in order, or undefined when the turn could not be rebuilt exactly. */
381
+ content() {
382
+ for (const [index, json] of this.#json) {
383
+ const block = this.#blocks.get(index);
384
+ if (block === undefined || json.trim() === "")
385
+ continue;
386
+ try {
387
+ block.input = JSON.parse(json);
388
+ }
389
+ catch {
390
+ return undefined;
391
+ }
392
+ }
393
+ return this.#whole ? [...this.#blocks.entries()].sort(([a], [b]) => a - b).map(([, block]) => block) : undefined;
394
+ }
395
+ }
396
+ const ALIVE = { kind: "alive" };
230
397
  /** The Anthropic Messages API behind the OpenAI shape: `x-api-key` auth, `/messages`, and its SSE events as OpenAI chunks. */
231
398
  export const anthropicWire = {
232
399
  secretHeaders: ["x-api-key", "authorization"],
@@ -234,16 +401,21 @@ export const anthropicWire = {
234
401
  const version = Object.entries(provider.headers ?? {}).find(([name]) => name.toLowerCase() === "anthropic-version");
235
402
  return { "x-api-key": secret, "anthropic-version": version?.[1] ?? ANTHROPIC_VERSION };
236
403
  },
237
- chat: (body, model, stream, provider) => ({ path: "/messages", body: toAnthropicRequest(body, model, stream, provider) }),
404
+ chat: (body, model, stream, provider) => {
405
+ const request = anthropicRequest(takesCacheControl(provider, model) ? body : withoutCacheControl(body), model.id, stream, provider);
406
+ const memory = memoryOf(provider);
407
+ return { path: "/messages", body: request.body, ...(memory === null ? {} : { context: new ReplayContext(memory, request.prefix) }) };
408
+ },
238
409
  embeddings: () => {
239
410
  throw refuse("Anthropic has no embeddings API");
240
411
  },
241
412
  chatResponse: fromAnthropicMessage,
242
- async *chunks(events) {
413
+ async *chunks(events, context) {
243
414
  let id = "anthropic";
244
415
  let model = "";
245
- let inputUsage = undefined;
246
- let output = 0;
416
+ // message_start carries the input and cache counts; each message_delta the cumulative counts so far.
417
+ let usage = {};
418
+ const turn = context instanceof ReplayContext ? new StreamedTurn() : null;
247
419
  const created = Math.floor(Date.now() / 1000);
248
420
  // Anthropic numbers content blocks; OpenAI numbers tool calls. A tool_use block's index maps to its tool call's.
249
421
  const toolIndex = new Map();
@@ -266,15 +438,15 @@ export const anthropicWire = {
266
438
  id = message.id;
267
439
  if (typeof message.model === "string")
268
440
  model = message.model;
269
- inputUsage = message.usage;
270
441
  if (isPlainRecord(message.usage))
271
- output = count(message.usage.output_tokens);
442
+ usage = { ...message.usage };
272
443
  yield chunk({ role: "assistant", content: "" });
273
444
  break;
274
445
  }
275
446
  case "content_block_start": {
276
447
  const block = isPlainRecord(data.content_block) ? data.content_block : {};
277
448
  const index = count(data.index);
449
+ turn?.start(index, block);
278
450
  if (block.type === "tool_use") {
279
451
  const call = toolIndex.size;
280
452
  toolIndex.set(index, call);
@@ -282,30 +454,37 @@ export const anthropicWire = {
282
454
  }
283
455
  else if (block.type === "text" && typeof block.text === "string" && block.text !== "")
284
456
  yield chunk({ content: block.text });
457
+ else
458
+ yield ALIVE;
285
459
  break;
286
460
  }
287
461
  case "content_block_delta": {
288
462
  const delta = isPlainRecord(data.delta) ? data.delta : {};
463
+ turn?.delta(count(data.index), delta);
464
+ const call = delta.type === "input_json_delta" && typeof delta.partial_json === "string" ? toolIndex.get(count(data.index)) : undefined;
289
465
  if (delta.type === "text_delta" && typeof delta.text === "string")
290
466
  yield chunk({ content: delta.text });
291
- else if (delta.type === "input_json_delta" && typeof delta.partial_json === "string") {
292
- const call = toolIndex.get(count(data.index));
293
- if (call !== undefined)
294
- yield chunk({ tool_calls: [{ index: call, function: { arguments: delta.partial_json } }] });
295
- }
467
+ else if (call !== undefined)
468
+ yield chunk({ tool_calls: [{ index: call, function: { arguments: delta.partial_json } }] });
469
+ // Thinking text is a summary or a progress note, streamed only when the request asked to see it.
470
+ else if (delta.type === "thinking_delta" && typeof delta.thinking === "string" && delta.thinking !== "")
471
+ yield chunk({ reasoning_content: delta.thinking });
472
+ else
473
+ yield ALIVE;
296
474
  break;
297
475
  }
298
476
  case "message_delta": {
299
477
  const delta = isPlainRecord(data.delta) ? data.delta : {};
300
- // message_delta's usage is cumulative: the last one is the total.
301
- if (isPlainRecord(data.usage) && typeof data.usage.output_tokens === "number")
302
- output = data.usage.output_tokens;
478
+ if (isPlainRecord(data.usage))
479
+ usage = { ...usage, ...Object.fromEntries(Object.entries(data.usage).filter(([, value]) => value !== null && value !== undefined)) };
303
480
  yield chunk({}, finishReason(delta.stop_reason));
304
481
  break;
305
482
  }
306
483
  case "message_stop": {
307
- const usage = usageOf(inputUsage ?? {}, output);
308
- yield { kind: "chunk", chunk: { id, object: "chat.completion.chunk", created, model, choices: [], usage } };
484
+ const content = turn?.content();
485
+ if (content !== undefined)
486
+ remember(context, content);
487
+ yield { kind: "chunk", chunk: { id, object: "chat.completion.chunk", created, model, choices: [], usage: usageOf(usage) } };
309
488
  return;
310
489
  }
311
490
  case "error":
@@ -313,6 +492,7 @@ export const anthropicWire = {
313
492
  return;
314
493
  default:
315
494
  // ping, content_block_stop, and event types Anthropic adds later are tolerated, as its docs ask.
495
+ yield ALIVE;
316
496
  break;
317
497
  }
318
498
  }
@@ -0,0 +1,29 @@
1
+ import type { SseEvent } from "../sse.js";
2
+ import type { AnthropicStreamEvent, ModelConfig, ProviderConfig, Usage } from "../types.js";
3
+ import type { WireRequest } from "./wire.js";
4
+ /**
5
+ * The Anthropic Messages API as its own callers speak it (docs/design.md, "The native Anthropic route"): a request in
6
+ * the Messages shape goes to an Anthropic provider untranslated, and its answer and stream events come back as they
7
+ * are. Cache breakpoints, thinking blocks and anything the API adds later pass through; the router reads only what it
8
+ * accounts for (usage, the first token, an error).
9
+ */
10
+ /** What one item of a provider stream means to the router, whatever shape the caller reads it in. */
11
+ export type Streamed<T> = {
12
+ readonly kind: "item";
13
+ readonly item: T; /** True when the item carries content, so failover is over. */
14
+ readonly token: boolean;
15
+ readonly usage: Usage | undefined;
16
+ } | {
17
+ readonly kind: "error";
18
+ readonly body: unknown;
19
+ } | {
20
+ readonly kind: "malformed";
21
+ } | {
22
+ readonly kind: "alive";
23
+ };
24
+ /** A Messages request as the provider takes it: the lane's model, the caller's body. */
25
+ export declare function nativeRequest(body: Record<string, unknown>, model: ModelConfig, stream: boolean): WireRequest;
26
+ /** The usage of a Messages answer in the router's shape, for accounting. */
27
+ export declare function nativeUsage(usage: unknown): Usage | undefined;
28
+ /** A Messages stream, event by event as the provider sent it, each naming the lane that serves it. */
29
+ export declare function nativeItems(provider: ProviderConfig, model: ModelConfig, events: AsyncIterable<SseEvent>): AsyncGenerator<Streamed<AnthropicStreamEvent>, void, undefined>;
@@ -0,0 +1,56 @@
1
+ import { isPlainRecord } from "@starci/common";
2
+ import { readUsage } from "../shapes.js";
3
+ import { usageOf } from "./anthropic.js";
4
+ /** A Messages request as the provider takes it: the lane's model, the caller's body. */
5
+ export function nativeRequest(body, model, stream) {
6
+ const out = { ...body, model: model.id };
7
+ if (stream)
8
+ out.stream = true;
9
+ else
10
+ delete out.stream;
11
+ return { path: "/messages", body: out };
12
+ }
13
+ /** The usage of a Messages answer in the router's shape, for accounting. */
14
+ export function nativeUsage(usage) {
15
+ return readUsage(usageOf(usage));
16
+ }
17
+ function defined(record) {
18
+ return Object.fromEntries(Object.entries(record).filter(([, value]) => value !== null && value !== undefined));
19
+ }
20
+ /** A Messages stream, event by event as the provider sent it, each naming the lane that serves it. */
21
+ export async function* nativeItems(provider, model, events) {
22
+ // message_start carries the input and cache counts; each message_delta the cumulative counts so far.
23
+ let usage = {};
24
+ for await (const event of events) {
25
+ let data;
26
+ try {
27
+ data = JSON.parse(event.data);
28
+ }
29
+ catch {
30
+ yield { kind: "malformed" };
31
+ continue;
32
+ }
33
+ if (!isPlainRecord(data))
34
+ continue;
35
+ const type = typeof data.type === "string" ? data.type : event.event;
36
+ if (type === "error") {
37
+ yield { kind: "error", body: data };
38
+ return;
39
+ }
40
+ if (type === "message_start" && isPlainRecord(data.message) && isPlainRecord(data.message.usage))
41
+ usage = { ...data.message.usage };
42
+ if (type === "message_delta" && isPlainRecord(data.usage))
43
+ usage = { ...usage, ...defined(data.usage) };
44
+ const settled = type === "message_delta" || type === "message_stop";
45
+ yield {
46
+ kind: "item",
47
+ item: { event: type, data: event.data, provider: provider.id, model: model.id },
48
+ token: type === "content_block_delta",
49
+ usage: settled ? nativeUsage(usage) : undefined,
50
+ };
51
+ if (type === "message_stop")
52
+ return;
53
+ }
54
+ // A stream that ends before message_stop was cut: the attempt failed.
55
+ yield { kind: "error", body: { type: "error", error: { type: "api_error", message: "the stream ended before message_stop" } } };
56
+ }
@@ -0,0 +1,32 @@
1
+ import type { ProviderConfig } from "../types.js";
2
+ /**
3
+ * The thinking blocks of answers that call tools, kept in this process so the next request of the tool loop can send
4
+ * the assistant turn back exactly as Anthropic produced it (docs/design.md, "Thinking with tools"). An OpenAI-shaped
5
+ * client has no field for them, so nothing above the gateway can carry them.
6
+ *
7
+ * A turn is replayed only while everything sent before it is what was sent when it was produced: the API binds a
8
+ * thinking block to that prefix (system, tools and earlier messages) and may refuse a block replayed after an edit. The
9
+ * prefix is kept as a digest, never as text.
10
+ */
11
+ /** One assistant turn as Anthropic answered it, and the digest of the request that produced it. */
12
+ export interface RememberedTurn {
13
+ readonly content: readonly Record<string, unknown>[];
14
+ readonly prefix: string;
15
+ }
16
+ /** The most turns one provider keeps; the oldest is forgotten first. A forgotten turn is sent without its thinking. */
17
+ export declare const THINKING_MEMORY_LIMIT = 2000;
18
+ /** Assistant turns by the ids of the tool calls they made, least recently used first. */
19
+ export declare class ThinkingMemory {
20
+ #private;
21
+ constructor(limit?: number);
22
+ /** The turn that made these tool calls, when it is still remembered. */
23
+ get(key: string): RememberedTurn | undefined;
24
+ /** Remembers a turn, forgetting the least recently used one past the limit. */
25
+ set(key: string, turn: RememberedTurn): void;
26
+ /** How many turns are remembered. */
27
+ get size(): number;
28
+ }
29
+ /** The memory of a provider that replays thinking; null for one that drops it (the default). */
30
+ export declare function memoryOf(provider: ProviderConfig): ThinkingMemory | null;
31
+ /** The digest of a prefix extended by one more piece (the system and tools first, then each turn). */
32
+ export declare function digest(previous: string, piece: unknown): string;