@meridian-flow/runtime 0.0.0-stage → 0.1.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.
Files changed (89) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +67 -2
  3. package/dist/gateway/adapters/anthropic/adapter.d.ts +9 -0
  4. package/dist/gateway/adapters/anthropic/adapter.js +52 -0
  5. package/dist/gateway/adapters/anthropic/request-map.d.ts +46 -0
  6. package/dist/gateway/adapters/anthropic/request-map.js +285 -0
  7. package/dist/gateway/adapters/anthropic/stream-collect.d.ts +67 -0
  8. package/dist/gateway/adapters/anthropic/stream-collect.js +295 -0
  9. package/dist/gateway/adapters/anthropic/tool-schema.d.ts +8 -0
  10. package/dist/gateway/adapters/anthropic/tool-schema.js +68 -0
  11. package/dist/gateway/adapters/credentials.d.ts +25 -0
  12. package/dist/gateway/adapters/credentials.js +118 -0
  13. package/dist/gateway/adapters/gemini/adapter.d.ts +10 -0
  14. package/dist/gateway/adapters/gemini/adapter.js +90 -0
  15. package/dist/gateway/adapters/gemini/request-map.d.ts +29 -0
  16. package/dist/gateway/adapters/gemini/request-map.js +178 -0
  17. package/dist/gateway/adapters/gemini/stream-collect.d.ts +29 -0
  18. package/dist/gateway/adapters/gemini/stream-collect.js +116 -0
  19. package/dist/gateway/adapters/mock/response.d.ts +11 -0
  20. package/dist/gateway/adapters/mock/response.js +160 -0
  21. package/dist/gateway/adapters/mock/samples.d.ts +8 -0
  22. package/dist/gateway/adapters/mock/samples.js +133 -0
  23. package/dist/gateway/adapters/mock/script-queue.d.ts +10 -0
  24. package/dist/gateway/adapters/mock/script-queue.js +60 -0
  25. package/dist/gateway/adapters/mock/server.d.ts +16 -0
  26. package/dist/gateway/adapters/mock/server.js +342 -0
  27. package/dist/gateway/adapters/mock/write-directive.d.ts +15 -0
  28. package/dist/gateway/adapters/mock/write-directive.js +21 -0
  29. package/dist/gateway/adapters/openai/adapter.d.ts +3 -0
  30. package/dist/gateway/adapters/openai/adapter.js +35 -0
  31. package/dist/gateway/adapters/openai/request-map.d.ts +31 -0
  32. package/dist/gateway/adapters/openai/request-map.js +228 -0
  33. package/dist/gateway/adapters/openai/stream-collect.d.ts +63 -0
  34. package/dist/gateway/adapters/openai/stream-collect.js +490 -0
  35. package/dist/gateway/adapters/openai-compatible/adapter.d.ts +3 -0
  36. package/dist/gateway/adapters/openai-compatible/adapter.js +35 -0
  37. package/dist/gateway/adapters/openai-compatible/request-map.d.ts +31 -0
  38. package/dist/gateway/adapters/openai-compatible/request-map.js +180 -0
  39. package/dist/gateway/adapters/openai-compatible/stream-collect.d.ts +84 -0
  40. package/dist/gateway/adapters/openai-compatible/stream-collect.js +215 -0
  41. package/dist/gateway/adapters/provider-http-error.d.ts +65 -0
  42. package/dist/gateway/adapters/provider-http-error.js +188 -0
  43. package/dist/gateway/adapters/provider-stream.d.ts +23 -0
  44. package/dist/gateway/adapters/provider-stream.js +58 -0
  45. package/dist/gateway/attempt-stream.d.ts +27 -0
  46. package/dist/gateway/attempt-stream.js +329 -0
  47. package/dist/gateway/create-gateway.d.ts +25 -0
  48. package/dist/gateway/create-gateway.js +88 -0
  49. package/dist/gateway/deadline.d.ts +56 -0
  50. package/dist/gateway/deadline.js +120 -0
  51. package/dist/gateway/domain/index.d.ts +10 -0
  52. package/dist/gateway/domain/index.js +10 -0
  53. package/dist/gateway/domain/media.d.ts +62 -0
  54. package/dist/gateway/domain/media.js +126 -0
  55. package/dist/gateway/domain/metering.d.ts +7 -0
  56. package/dist/gateway/domain/metering.js +6 -0
  57. package/dist/gateway/domain/resolve-call.d.ts +24 -0
  58. package/dist/gateway/domain/resolve-call.js +39 -0
  59. package/dist/gateway/domain/types.d.ts +457 -0
  60. package/dist/gateway/domain/types.js +10 -0
  61. package/dist/gateway/helpers/messages.d.ts +17 -0
  62. package/dist/gateway/helpers/messages.js +29 -0
  63. package/dist/gateway/helpers/parse-tool-arguments.d.ts +9 -0
  64. package/dist/gateway/helpers/parse-tool-arguments.js +38 -0
  65. package/dist/gateway/helpers/tool-call-pairing.d.ts +13 -0
  66. package/dist/gateway/helpers/tool-call-pairing.js +25 -0
  67. package/dist/gateway/index.d.ts +12 -0
  68. package/dist/gateway/index.js +11 -0
  69. package/dist/gateway/mock-script.d.ts +129 -0
  70. package/dist/gateway/mock-script.js +129 -0
  71. package/dist/gateway/mock.d.ts +11 -0
  72. package/dist/gateway/mock.js +22 -0
  73. package/dist/gateway/ports/gateway.d.ts +18 -0
  74. package/dist/gateway/ports/gateway.js +1 -0
  75. package/dist/gateway/ports/provider-adapter.d.ts +21 -0
  76. package/dist/gateway/ports/provider-adapter.js +1 -0
  77. package/dist/gateway/routing.d.ts +28 -0
  78. package/dist/gateway/routing.js +48 -0
  79. package/dist/gateway/shapes/index.d.ts +7 -0
  80. package/dist/gateway/shapes/index.js +1 -0
  81. package/dist/gateway/shapes/json.d.ts +4 -0
  82. package/dist/gateway/shapes/json.js +1 -0
  83. package/dist/gateway/shapes/provider-error.d.ts +10 -0
  84. package/dist/gateway/shapes/provider-error.js +1 -0
  85. package/dist/gateway/shapes/usage.d.ts +41 -0
  86. package/dist/gateway/shapes/usage.js +29 -0
  87. package/dist/gateway/stream-events.d.ts +21 -0
  88. package/dist/gateway/stream-events.js +26 -0
  89. package/package.json +62 -3
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Anthropic stream accumulator: folds Anthropic Messages streaming events into
3
+ * canonical StreamEvents and a final GenerateResult. The Messages stream is
4
+ * organized around content blocks: `content_block_start` declares a block at a
5
+ * stable `index`, `content_block_delta` streams that block's text/thinking/tool
6
+ * input, and `content_block_stop` closes it.
7
+ *
8
+ * Key decision: the provider content-block `index` is the source-order spine.
9
+ * Text, reasoning, and tool-use parts are accumulated per block index and sorted
10
+ * when building the final result, so persisted content follows the order Claude
11
+ * streamed instead of grouping all reasoning, then all text, then all tools.
12
+ *
13
+ * Provider-protocol grounding (verified against Anthropic SDK 0.100.1 + docs):
14
+ * - Anthropic's documented stream flow is strictly serial per block:
15
+ * `message_start` → (content_block_start → delta(s) → content_block_stop)*
16
+ * → `message_delta` → `message_stop`. Each block's `index` corresponds to
17
+ * its position in the final `Message.content[]` array. The documented flow
18
+ * implies non-interleaving, though the docs do not state it as a separate
19
+ * guarantee.
20
+ * - Delta variants consumed: `text_delta` (visible text), `thinking_delta`
21
+ * (extended-thinking text), `signature_delta` (thinking integrity metadata,
22
+ * not user-visible, arrives just before `content_block_stop`),
23
+ * `input_json_delta` (partial JSON for tool input — must be concatenated
24
+ * before parsing).
25
+ * - StopReason mismatch: current Anthropic docs list
26
+ * `model_context_window_exceeded`, but the installed SDK 0.100.1's
27
+ * `StopReason` union is `'end_turn' | 'max_tokens' | 'stop_sequence' |
28
+ * 'tool_use' | 'pause_turn' | 'refusal'` — that value is not present.
29
+ */
30
+ import type Anthropic from "@anthropic-ai/sdk";
31
+ import type { ContentPart, FinishReason, GenerateResult, StreamEvent } from "../../domain/index.js";
32
+ import { type Usage } from "../../shapes/index.js";
33
+ interface StreamAccumulator {
34
+ longCacheWrites: boolean;
35
+ contentParts: ContentPart[];
36
+ textBlocks: Map<number, string>;
37
+ reasoningBlocks: Map<number, ReasoningBlockEntry>;
38
+ toolCalls: Map<number, {
39
+ id: string;
40
+ name: string;
41
+ arguments: string;
42
+ }>;
43
+ usage: Usage;
44
+ finishReason: FinishReason;
45
+ model: string;
46
+ provider: string;
47
+ }
48
+ interface ReasoningBlockEntry {
49
+ text: string;
50
+ signature?: string;
51
+ redacted?: true;
52
+ data?: string;
53
+ }
54
+ export declare function createStreamAccumulator(model: string, provider: string, longCacheWrites: boolean): StreamAccumulator;
55
+ /**
56
+ * Map Anthropic stop reasons to Meridian FinishReason.
57
+ *
58
+ * The installed SDK 0.100.1 StopReason union is:
59
+ * 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal'
60
+ * Current Anthropic docs additionally list `model_context_window_exceeded`,
61
+ * but that value is not in the SDK union; accept the wire string at this boundary.
62
+ */
63
+ export declare function mapStopReason(reason: string | null | undefined): FinishReason;
64
+ export declare function mapUsage(usage: Anthropic.Messages.Usage | Anthropic.Messages.MessageDeltaUsage, longCacheWrites: boolean, fallback?: Usage): Usage;
65
+ export declare function eventsFromAnthropicStreamEvent(event: Anthropic.Messages.RawMessageStreamEvent, acc: StreamAccumulator): Generator<StreamEvent>;
66
+ export declare function buildGenerateResult(acc: StreamAccumulator): GenerateResult;
67
+ export {};
@@ -0,0 +1,295 @@
1
+ import { parseToolCallArguments } from "../../helpers/parse-tool-arguments.js";
2
+ import { assertValidUsage } from "../../shapes/index.js";
3
+ function emptyUsage() {
4
+ return { inputTokens: 0, outputTokens: 0 };
5
+ }
6
+ export function createStreamAccumulator(model, provider, longCacheWrites) {
7
+ return {
8
+ longCacheWrites,
9
+ contentParts: [],
10
+ textBlocks: new Map(),
11
+ reasoningBlocks: new Map(),
12
+ toolCalls: new Map(),
13
+ usage: emptyUsage(),
14
+ finishReason: "end_turn",
15
+ model,
16
+ provider,
17
+ };
18
+ }
19
+ // ── Finish reason mapping ─────────────────────────────────────────
20
+ /**
21
+ * Map Anthropic stop reasons to Meridian FinishReason.
22
+ *
23
+ * The installed SDK 0.100.1 StopReason union is:
24
+ * 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal'
25
+ * Current Anthropic docs additionally list `model_context_window_exceeded`,
26
+ * but that value is not in the SDK union; accept the wire string at this boundary.
27
+ */
28
+ export function mapStopReason(reason) {
29
+ // Anthropic stop reasons mostly line up with Meridian FinishReason. `refusal`
30
+ // and `pause_turn` have no canonical equivalent today, so they are
31
+ // represented as an error finish and end_turn respectively.
32
+ switch (reason) {
33
+ case "end_turn":
34
+ return "end_turn";
35
+ case "tool_use":
36
+ return "tool_use";
37
+ case "max_tokens":
38
+ return "max_tokens";
39
+ case "stop_sequence":
40
+ return "stop_sequence";
41
+ case "refusal":
42
+ case "model_context_window_exceeded":
43
+ return "error";
44
+ default:
45
+ return "end_turn";
46
+ }
47
+ }
48
+ // ── Usage mapping ─────────────────────────────────────────────────
49
+ export function mapUsage(usage, longCacheWrites, fallback) {
50
+ // Anthropic's input_tokens excludes cache reads and cache creation. Meridian's
51
+ // canonical inputTokens is inclusive, so the adapter owns the additive
52
+ // conversion before usage reaches persistence, displays, or pricing.
53
+ const fallbackCacheRead = fallback?.cacheReadTokens ?? 0;
54
+ const fallbackCacheWrite = fallback?.cacheWriteTokens ?? 0;
55
+ const cacheRead = usage.cache_read_input_tokens ?? fallbackCacheRead;
56
+ const cacheWrite = usage.cache_creation_input_tokens ?? fallbackCacheWrite;
57
+ const uncachedInput = usage.input_tokens ?? (fallback?.inputTokens ?? 0) - fallbackCacheRead - fallbackCacheWrite;
58
+ const result = {
59
+ inputTokens: uncachedInput + cacheRead + cacheWrite,
60
+ outputTokens: usage.output_tokens ?? fallback?.outputTokens ?? 0,
61
+ };
62
+ if (cacheRead > 0)
63
+ result.cacheReadTokens = cacheRead;
64
+ if (cacheWrite > 0)
65
+ result.cacheWriteTokens = cacheWrite;
66
+ // The split arrives on message_start and may be absent on message_delta; keep the earlier value.
67
+ const cacheWriteLong = ("cache_creation" in usage ? usage.cache_creation?.ephemeral_1h_input_tokens : undefined) ??
68
+ fallback?.cacheWriteLongTokens ??
69
+ (longCacheWrites ? cacheWrite : 0);
70
+ if (cacheWriteLong > 0)
71
+ result.cacheWriteLongTokens = cacheWriteLong;
72
+ // Anthropic calls reasoning "thinking"; expose it as canonical
73
+ // Usage.reasoningTokens without leaking the provider term downstream.
74
+ // `output_tokens_details.thinking_tokens` is present in the installed SDK
75
+ // but not explicitly documented in the Anthropic docs pages reviewed —
76
+ // treat as SDK-confirmed, docs-unverified.
77
+ const thinkingTokens = usage.output_tokens_details?.thinking_tokens ?? fallback?.reasoningTokens;
78
+ if (thinkingTokens && thinkingTokens > 0) {
79
+ result.reasoningTokens = thinkingTokens;
80
+ }
81
+ assertValidUsage(result);
82
+ return result;
83
+ }
84
+ // ── Stream event processing ───────────────────────────────────────
85
+ export function* eventsFromAnthropicStreamEvent(event, acc) {
86
+ // The generator emits canonical deltas for low-latency subscribers and keeps
87
+ // block-indexed state for the final source-ordered GenerateResult.
88
+ switch (event.type) {
89
+ case "message_start": {
90
+ // message_start carries early usage (usually input-side counts from the
91
+ // initial request processing). Keep it so usage is available even before
92
+ // the terminal message_delta arrives with final token totals.
93
+ if (event.message.usage) {
94
+ acc.usage = mapUsage(event.message.usage, acc.longCacheWrites);
95
+ yield { type: "usage", usage: acc.usage };
96
+ }
97
+ break;
98
+ }
99
+ case "content_block_start": {
100
+ const block = event.content_block;
101
+ if (block.type === "thinking") {
102
+ // A thinking block may include initial text and/or a signature at start;
103
+ // later thinking_delta/signature_delta events update this same index.
104
+ // The signature_delta arrives just before content_block_stop (per
105
+ // Anthropic streaming docs) and verifies block integrity.
106
+ acc.reasoningBlocks.set(event.index, {
107
+ text: block.thinking ?? "",
108
+ signature: block.signature || undefined,
109
+ });
110
+ }
111
+ if (block.type === "redacted_thinking") {
112
+ // Redacted thinking is opaque by design. Per Anthropic streaming docs,
113
+ // when thinking is omitted the block opens, receives a single
114
+ // signature_delta, and closes. Preserve provider data for replay, but
115
+ // emit no reasoning.delta because there is no visible text.
116
+ acc.reasoningBlocks.set(event.index, {
117
+ text: "",
118
+ redacted: true,
119
+ data: block.data,
120
+ });
121
+ }
122
+ if (block.type === "tool_use") {
123
+ // Tool-use ID and name are known at block start; only the JSON input
124
+ // streams later through input_json_delta fragments.
125
+ acc.toolCalls.set(event.index, {
126
+ id: block.id,
127
+ name: block.name,
128
+ arguments: "",
129
+ });
130
+ }
131
+ break;
132
+ }
133
+ case "content_block_delta": {
134
+ const delta = event.delta;
135
+ if (delta.type === "text_delta") {
136
+ // Deltas carry the content-block index. Use it both for canonical
137
+ // partIndex and for reconstructing the final TextPart at that position.
138
+ acc.textBlocks.set(event.index, (acc.textBlocks.get(event.index) ?? "") + delta.text);
139
+ yield {
140
+ type: "text.delta",
141
+ text: delta.text,
142
+ partIndex: event.index,
143
+ };
144
+ }
145
+ if (delta.type === "thinking_delta") {
146
+ // Thinking deltas are provider-visible reasoning text. Accumulate at the
147
+ // block index and surface as canonical reasoning.delta.
148
+ const entry = acc.reasoningBlocks.get(event.index) ?? { text: "" };
149
+ entry.text += delta.thinking;
150
+ acc.reasoningBlocks.set(event.index, entry);
151
+ yield {
152
+ type: "reasoning.delta",
153
+ text: delta.thinking,
154
+ partIndex: event.index,
155
+ };
156
+ }
157
+ if (delta.type === "signature_delta") {
158
+ // Signature deltas are metadata, not user-visible text. They must be
159
+ // retained on the reasoning block so the final ReasoningPart can be
160
+ // replayed to Anthropic when provider/model origin matches. Per
161
+ // Anthropic docs, signature_delta arrives just before
162
+ // content_block_stop for the same block.
163
+ const entry = acc.reasoningBlocks.get(event.index) ?? { text: "" };
164
+ entry.signature = delta.signature;
165
+ acc.reasoningBlocks.set(event.index, entry);
166
+ }
167
+ if (delta.type === "input_json_delta") {
168
+ // Tool input streams as raw partial JSON (delta.partial_json).
169
+ // The final GenerateResult concatenates and parses the joined string
170
+ // into ToolCall.arguments/tool_use.input. Per Anthropic streaming docs,
171
+ // input_json_delta is the protocol for streaming tool input.
172
+ const toolEntry = acc.toolCalls.get(event.index);
173
+ if (toolEntry) {
174
+ toolEntry.arguments += delta.partial_json;
175
+ yield {
176
+ type: "tool_call.delta",
177
+ id: toolEntry.id,
178
+ name: toolEntry.name,
179
+ argumentsDelta: delta.partial_json,
180
+ partIndex: event.index,
181
+ };
182
+ }
183
+ }
184
+ break;
185
+ }
186
+ case "content_block_stop": {
187
+ // Anthropic block_stop is a delimiter only. Content and metadata have
188
+ // already been accumulated through start/delta events.
189
+ break;
190
+ }
191
+ case "message_delta": {
192
+ // message_delta carries terminal stop_reason and final cumulative usage.
193
+ // Per Anthropic docs, stop_reason is null in message_start, supplied in
194
+ // message_delta, and not provided in other events. The content blocks
195
+ // themselves have already been streamed by this point.
196
+ if (event.delta.stop_reason) {
197
+ acc.finishReason = mapStopReason(event.delta.stop_reason);
198
+ }
199
+ // Update usage with delta (has final token counts).
200
+ if (event.usage) {
201
+ acc.usage = mapUsage(event.usage, acc.longCacheWrites, acc.usage);
202
+ yield { type: "usage", usage: acc.usage };
203
+ }
204
+ if (event.delta.stop_reason === "model_context_window_exceeded") {
205
+ yield {
206
+ type: "error",
207
+ code: "context_overflow",
208
+ message: "Model context window exceeded",
209
+ retryable: false,
210
+ result: buildGenerateResult(acc),
211
+ };
212
+ }
213
+ break;
214
+ }
215
+ case "message_stop": {
216
+ // Build final result later in the adapter. message_stop is just the stream
217
+ // terminator; emitting canonical end here would duplicate adapter logic.
218
+ break;
219
+ }
220
+ }
221
+ }
222
+ // ── Build final result ────────────────────────────────────────────
223
+ export function buildGenerateResult(acc) {
224
+ // Assemble content by provider block index. This is the ordering fix: without
225
+ // this source-order collection, final content would naturally group by map
226
+ // iteration order (all reasoning before all text before all tools).
227
+ //
228
+ // The block `index` = position in final `Message.content[]` (Anthropic
229
+ // docs-confirmed), so sorting by index reconstructs Claude's intended order.
230
+ const indexedParts = [];
231
+ for (const [index, reasoning] of acc.reasoningBlocks.entries()) {
232
+ // Plain thinking becomes a simple canonical ReasoningPart. Signed/redacted
233
+ // thinking carries Anthropic metadata plus origin information for replay.
234
+ if (!reasoning.redacted && !reasoning.signature) {
235
+ indexedParts.push({ index, part: { type: "reasoning", text: reasoning.text } });
236
+ continue;
237
+ }
238
+ indexedParts.push({
239
+ index,
240
+ part: {
241
+ type: "reasoning",
242
+ text: reasoning.text,
243
+ providerOptions: {
244
+ anthropic: reasoning.redacted
245
+ ? { redacted: true, data: reasoning.data ?? "" }
246
+ : { signature: reasoning.signature },
247
+ meridian: { provider: acc.provider, model: acc.model },
248
+ },
249
+ },
250
+ });
251
+ }
252
+ for (const [index, text] of acc.textBlocks.entries()) {
253
+ // Text stays per content-block index so a text block between two reasoning
254
+ // blocks keeps its source position and distinct text blocks do not collapse.
255
+ if (!text)
256
+ continue;
257
+ indexedParts.push({ index, part: { type: "text", text } });
258
+ }
259
+ const toolCalls = [];
260
+ for (const [index, entry] of acc.toolCalls.entries()) {
261
+ const parsed = parseToolCallArguments(entry.arguments);
262
+ const input = parsed.ok ? parsed.arguments : {};
263
+ const parseError = parsed.ok ? undefined : { raw: parsed.raw, message: parsed.message };
264
+ indexedParts.push({
265
+ index,
266
+ part: {
267
+ type: "tool_use",
268
+ toolCallId: entry.id,
269
+ toolName: entry.name,
270
+ input,
271
+ ...(parseError ? { inputParseError: parseError } : {}),
272
+ },
273
+ });
274
+ toolCalls.push({
275
+ id: entry.id,
276
+ name: entry.name,
277
+ arguments: input,
278
+ ...(parseError ? { argumentsParseError: parseError } : {}),
279
+ });
280
+ }
281
+ const content = indexedParts
282
+ .sort((a, b) => a.index - b.index)
283
+ .map(({ part }) => part)
284
+ // Future/unindexed fallback content is appended after the provider-indexed
285
+ // blocks because there is no reliable block position to sort by.
286
+ .concat(acc.contentParts);
287
+ return {
288
+ content,
289
+ toolCalls,
290
+ finishReason: acc.finishReason,
291
+ usage: acc.usage,
292
+ model: acc.model,
293
+ provider: acc.provider,
294
+ };
295
+ }
@@ -0,0 +1,8 @@
1
+ /** Anthropic-only advertisement of canonical command unions; execution keeps its Zod contract. */
2
+ import type { Tool } from "../../domain/index.js";
3
+ /**
4
+ * Root object command unions are the supported projection boundary. Their
5
+ * relationships become generated guidance, not a replacement validation schema.
6
+ * Other root intersections/constraints must not be silently discarded.
7
+ */
8
+ export declare function anthropicToolSchema(schema: Tool["inputSchema"]): Record<string, unknown>;
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Root object command unions are the supported projection boundary. Their
3
+ * relationships become generated guidance, not a replacement validation schema.
4
+ * Other root intersections/constraints must not be silently discarded.
5
+ */
6
+ export function anthropicToolSchema(schema) {
7
+ if ("allOf" in schema)
8
+ throw new Error("Anthropic tool schemas do not support root allOf");
9
+ const { oneOf, anyOf, ...root } = schema;
10
+ const alternatives = oneOf ?? anyOf;
11
+ if (alternatives === undefined)
12
+ return schema;
13
+ if ((oneOf !== undefined && anyOf !== undefined) ||
14
+ !Array.isArray(alternatives) ||
15
+ alternatives.length === 0 ||
16
+ root.properties !== undefined) {
17
+ throw new Error("Anthropic tool projection requires a single root command union without sibling properties");
18
+ }
19
+ const branchKeys = new Set([
20
+ "type",
21
+ "description",
22
+ "properties",
23
+ "required",
24
+ "additionalProperties",
25
+ ]);
26
+ const variants = alternatives;
27
+ for (const branch of variants) {
28
+ if (branch?.type !== "object" ||
29
+ typeof branch.properties?.command?.const !== "string" ||
30
+ Object.keys(branch).some((key) => !branchKeys.has(key)) ||
31
+ (branch.additionalProperties !== undefined &&
32
+ typeof branch.additionalProperties !== "boolean")) {
33
+ throw new Error("Anthropic tool projection requires object branches with literal commands and no branch-level combinators");
34
+ }
35
+ }
36
+ const fields = new Map();
37
+ const commands = [];
38
+ for (const branch of variants) {
39
+ const command = branch.properties.command.const;
40
+ commands.push(`${command}: ${branch.description ?? ""}\n[${command}] ${branch.properties.command.description ?? "Select this command."}\nRequired fields: ${(branch.required ?? []).join(", ") || "none"}.\nAllowed fields: ${Object.keys(branch.properties).join(", ")}. ${branch.additionalProperties === false ? "Do not send other fields." : "Additional fields are allowed."}`);
41
+ for (const [name, field] of Object.entries(branch.properties)) {
42
+ const entry = fields.get(name) ?? { options: [], descriptions: [] };
43
+ const { description, ...constraints } = field;
44
+ if (!entry.options.some((option) => JSON.stringify(option) === JSON.stringify(constraints))) {
45
+ entry.options.push(constraints);
46
+ }
47
+ entry.descriptions.push(`[${command}] ${description ?? `${name} is allowed.`}`);
48
+ fields.set(name, entry);
49
+ }
50
+ }
51
+ return {
52
+ ...root,
53
+ type: "object",
54
+ properties: Object.fromEntries([...fields].map(([name, { options, descriptions }]) => [
55
+ name,
56
+ {
57
+ ...(options.length === 1 ? options[0] : { anyOf: options }),
58
+ description: name === "command" ? commands.join("\n\n") : descriptions.join("\n"),
59
+ },
60
+ ])),
61
+ required: [
62
+ ...new Set([
63
+ ...(root.required ?? []),
64
+ ...(variants[0].required ?? []).filter((name) => variants.every((branch) => branch.required?.includes(name))),
65
+ ]),
66
+ ],
67
+ };
68
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Per-call credentials for the adapters. A request's credential for the routed provider
3
+ * replaces the provider's own (`ProviderConfig.auth`). Each adapter keeps one SDK client per
4
+ * token function, so calls with the same `accessToken` share its token cache whether or not the
5
+ * host reuses the credential object; a key's client is derived per call.
6
+ *
7
+ * Every wire authenticates a token through `bearerFetch`: the SDK is given a placeholder key,
8
+ * and every request's `Authorization` header is replaced with the cached token. One rule
9
+ * decides the refresh: a 401 on a request that used a token is retried once with a fresh one.
10
+ */
11
+ import type { GenerateRequest, ProviderConfig, ProviderCredential } from "../domain/index.js";
12
+ /** Placeholder for SDKs that refuse to start without a key: self-hosted and mock endpoints need none. */
13
+ export declare const PLACEHOLDER_KEY = "not-needed";
14
+ /** The `fetch` and `apiKey` client options for a wire carrying `credential`. */
15
+ export declare function bearerClientOptions(credential: ProviderCredential | undefined, base: typeof fetch, wire?: "anthropic" | "other"): {
16
+ apiKey: string;
17
+ fetch: typeof fetch;
18
+ };
19
+ /**
20
+ * The client for a request: the provider's own, or one derived from the request's credential
21
+ * for this provider; an empty key is no credential. Credentials for other providers are
22
+ * ignored. A token's client is kept by its token function, so its token cache survives across
23
+ * calls; a key's client is derived per call, since it caches nothing and is cheap to build.
24
+ */
25
+ export declare function clientForRequest<Client>(config: Pick<ProviderConfig, "id">, own: Client, derive: (credential: ProviderCredential) => Client): (request: GenerateRequest) => Client;
@@ -0,0 +1,118 @@
1
+ /** Mint a new token this long before `expiresAt`, so one never expires in flight. */
2
+ const EXPIRY_MARGIN_SECONDS = 60;
3
+ /** Placeholder for SDKs that refuse to start without a key: self-hosted and mock endpoints need none. */
4
+ export const PLACEHOLDER_KEY = "not-needed";
5
+ /**
6
+ * Caches one provider's token until shortly before it expires. Concurrent `get`s share one
7
+ * mint, which also keeps single-use identity tokens from being spent twice. A token with no
8
+ * `expiresAt` is kept until the provider rejects it.
9
+ */
10
+ function tokenSource(provider) {
11
+ let cached;
12
+ let minting;
13
+ let force = false;
14
+ return {
15
+ async get() {
16
+ if (cached &&
17
+ (cached.expiresAt === null || cached.expiresAt - Date.now() / 1000 > EXPIRY_MARGIN_SECONDS)) {
18
+ return cached.token;
19
+ }
20
+ if (!minting) {
21
+ const forceRefresh = force;
22
+ force = false;
23
+ minting = provider(forceRefresh ? { forceRefresh } : undefined)
24
+ .then((token) => {
25
+ cached = token;
26
+ return token;
27
+ })
28
+ .catch((error) => {
29
+ // A rejected mint must not lose the instruction to refresh.
30
+ force ||= forceRefresh;
31
+ throw error;
32
+ })
33
+ .finally(() => {
34
+ minting = undefined;
35
+ });
36
+ }
37
+ return (await minting).token;
38
+ },
39
+ invalidate(rejected) {
40
+ // Concurrent requests rejected with the same token invalidate once: a later one
41
+ // must not discard the token the first one already replaced it with.
42
+ if (cached?.token !== rejected)
43
+ return;
44
+ cached = undefined;
45
+ force = true;
46
+ },
47
+ };
48
+ }
49
+ /**
50
+ * `fetch` for a client that authenticates with a token: sends `Authorization: Bearer <token>`
51
+ * and drops any key header the SDK added from its placeholder. The token is minted per
52
+ * request, not per client, so it can be refreshed mid-life.
53
+ *
54
+ * A 401 means the token is bad and no response byte has reached the caller, so the request is
55
+ * sent once more with a fresh token. A second 401 is returned as it is. A request body that
56
+ * cannot be replayed (a stream) is not retried; the next call mints a fresh token.
57
+ */
58
+ function bearerFetch(provider, base, prepare) {
59
+ const source = tokenSource(provider);
60
+ const send = async (input, init) => {
61
+ const token = await source.get();
62
+ const headers = new Headers(init?.headers);
63
+ headers.delete("x-api-key");
64
+ headers.delete("x-goog-api-key");
65
+ headers.set("Authorization", `Bearer ${token}`);
66
+ prepare(headers);
67
+ const response = await base(input, { ...init, headers });
68
+ if (response.status === 401)
69
+ source.invalidate(token);
70
+ return response;
71
+ };
72
+ return async (input, init) => {
73
+ const response = await send(input, init);
74
+ const replayable = !(input instanceof Request) && !(init?.body instanceof ReadableStream);
75
+ if (response.status !== 401 || !replayable)
76
+ return response;
77
+ await response.body?.cancel();
78
+ return send(input, init);
79
+ };
80
+ }
81
+ /** Anthropic's OAuth bearer tokens need this beta, next to any beta the request already names. */
82
+ function anthropicOAuthBeta(headers) {
83
+ const beta = headers.get("anthropic-beta");
84
+ headers.set("anthropic-beta", beta ? `${beta},oauth-2025-04-20` : "oauth-2025-04-20");
85
+ }
86
+ /** The `fetch` and `apiKey` client options for a wire carrying `credential`. */
87
+ export function bearerClientOptions(credential, base, wire = "other") {
88
+ if (credential && "accessToken" in credential) {
89
+ return {
90
+ apiKey: PLACEHOLDER_KEY,
91
+ fetch: bearerFetch(credential.accessToken, base, wire === "anthropic" ? anthropicOAuthBeta : () => { }),
92
+ };
93
+ }
94
+ return { apiKey: credential?.apiKey || PLACEHOLDER_KEY, fetch: base };
95
+ }
96
+ /**
97
+ * The client for a request: the provider's own, or one derived from the request's credential
98
+ * for this provider; an empty key is no credential. Credentials for other providers are
99
+ * ignored. A token's client is kept by its token function, so its token cache survives across
100
+ * calls; a key's client is derived per call, since it caches nothing and is cheap to build.
101
+ */
102
+ export function clientForRequest(config, own, derive) {
103
+ const byToken = new WeakMap();
104
+ return (request) => {
105
+ const credential = request.auth?.[config.id];
106
+ if (!credential || ("apiKey" in credential && !credential.apiKey))
107
+ return own;
108
+ if ("accessToken" in credential) {
109
+ let client = byToken.get(credential.accessToken);
110
+ if (!client) {
111
+ client = derive(credential);
112
+ byToken.set(credential.accessToken, client);
113
+ }
114
+ return client;
115
+ }
116
+ return derive(credential);
117
+ };
118
+ }
@@ -0,0 +1,10 @@
1
+ import type { ProviderConfig } from "../../domain/index.js";
2
+ import type { ProviderAdapter } from "../../ports/provider-adapter.js";
3
+ /**
4
+ * The SDK's `ApiError` holds the response body as a JSON string and drops the headers. This
5
+ * gives `mapProviderHttpError` the shape the other SDKs' errors have: the status, the body's
6
+ * message, the parsed body, and the retry hint as a header. Google answers a bad key with 400
7
+ * (`API_KEY_INVALID`), which is an auth error.
8
+ */
9
+ export declare function geminiErrorShape(err: unknown): unknown;
10
+ export declare function createGeminiAdapter(config: ProviderConfig): ProviderAdapter;