@starci/ai-router 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/README.md +133 -40
- package/dist/ai-router.service.d.ts +2 -2
- package/dist/ai-router.service.js +4 -2
- package/dist/attempt.d.ts +26 -0
- package/dist/attempt.js +29 -0
- package/dist/classify.d.ts +40 -0
- package/dist/classify.js +145 -0
- package/dist/config.d.ts +30 -5
- package/dist/config.js +184 -24
- package/dist/env.d.ts +70 -0
- package/dist/env.js +152 -0
- package/dist/errors.d.ts +1 -3
- package/dist/errors.js +4 -23
- package/dist/fake.d.ts +624 -5
- package/dist/fake.js +177 -21
- package/dist/gateway/config-file.d.ts +8 -3
- package/dist/gateway/config-file.js +53 -8
- package/dist/gateway/gateway.d.ts +2 -0
- package/dist/gateway/gateway.js +19 -3
- package/dist/gateway/index.d.ts +10 -1
- package/dist/gateway/index.js +5 -0
- package/dist/gateway/serve.js +7 -2
- package/dist/headers.d.ts +1 -1
- package/dist/headers.js +12 -6
- package/dist/pool.d.ts +37 -16
- package/dist/pool.js +173 -48
- package/dist/providers.d.ts +34 -0
- package/dist/providers.js +22 -0
- package/dist/router.js +108 -105
- package/dist/secrets.d.ts +8 -0
- package/dist/secrets.js +22 -0
- package/dist/shapes.d.ts +11 -0
- package/dist/shapes.js +124 -0
- package/dist/state-stores.d.ts +38 -0
- package/dist/state-stores.js +101 -0
- package/dist/store.d.ts +26 -7
- package/dist/store.js +64 -2
- package/dist/testing.d.ts +2 -2
- package/dist/testing.js +1 -1
- package/dist/types.d.ts +126 -38
- package/dist/wire/anthropic.d.ts +21 -0
- package/dist/wire/anthropic.js +322 -0
- package/dist/wire/index.d.ts +5 -0
- package/dist/wire/index.js +7 -0
- package/dist/wire/wire.d.ts +42 -0
- package/dist/wire/wire.js +35 -0
- package/docs/design.md +279 -0
- package/package.json +12 -3
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { AttemptError } from "../attempt.js";
|
|
2
|
+
import { isRecord } from "./wire.js";
|
|
3
|
+
/**
|
|
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).
|
|
7
|
+
*/
|
|
8
|
+
/** The `anthropic-version` sent unless the provider's headers set one. */
|
|
9
|
+
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;
|
|
12
|
+
/** Fields an OpenAI caller may send that Anthropic takes as they are. */
|
|
13
|
+
const PASSTHROUGH = ["temperature", "top_p", "top_k", "thinking", "output_config", "service_tier"];
|
|
14
|
+
function refuse(message) {
|
|
15
|
+
return new AttemptError("invalid_request", `anthropic: ${message}`);
|
|
16
|
+
}
|
|
17
|
+
function textOf(content) {
|
|
18
|
+
if (typeof content === "string")
|
|
19
|
+
return content;
|
|
20
|
+
if (!Array.isArray(content))
|
|
21
|
+
return "";
|
|
22
|
+
return content.map((part) => (isRecord(part) && typeof part.text === "string" ? part.text : "")).join("");
|
|
23
|
+
}
|
|
24
|
+
/** An OpenAI `image_url` part as an Anthropic image block: a data URL becomes base64, anything else a URL source. */
|
|
25
|
+
function imageBlock(part) {
|
|
26
|
+
const image = isRecord(part.image_url) ? part.image_url.url : part.image_url;
|
|
27
|
+
if (typeof image !== "string")
|
|
28
|
+
throw refuse("an image_url part has no url");
|
|
29
|
+
const data = /^data:([^;,]+);base64,(.*)$/s.exec(image);
|
|
30
|
+
if (data !== null)
|
|
31
|
+
return { type: "image", source: { type: "base64", media_type: data[1], data: data[2] } };
|
|
32
|
+
return { type: "image", source: { type: "url", url: image } };
|
|
33
|
+
}
|
|
34
|
+
/** OpenAI message content as Anthropic blocks; an empty text is left out (Anthropic refuses it). Other block types pass as they are. */
|
|
35
|
+
function blocks(content) {
|
|
36
|
+
if (content === null || content === undefined)
|
|
37
|
+
return [];
|
|
38
|
+
if (typeof content === "string")
|
|
39
|
+
return content === "" ? [] : [{ type: "text", text: content }];
|
|
40
|
+
if (!Array.isArray(content))
|
|
41
|
+
throw refuse("message content must be a string or a list of parts");
|
|
42
|
+
const out = [];
|
|
43
|
+
for (const part of content) {
|
|
44
|
+
if (!isRecord(part))
|
|
45
|
+
continue;
|
|
46
|
+
if (part.type === "text") {
|
|
47
|
+
if (typeof part.text === "string" && part.text !== "")
|
|
48
|
+
out.push({ type: "text", text: part.text });
|
|
49
|
+
}
|
|
50
|
+
else if (part.type === "image_url")
|
|
51
|
+
out.push(imageBlock(part));
|
|
52
|
+
else
|
|
53
|
+
out.push(part);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
function toolUse(call) {
|
|
58
|
+
if (!isRecord(call) || !isRecord(call.function) || typeof call.id !== "string" || typeof call.function.name !== "string")
|
|
59
|
+
throw refuse("a tool call needs an id and a function name");
|
|
60
|
+
const raw = call.function.arguments;
|
|
61
|
+
let input = {};
|
|
62
|
+
if (typeof raw === "string" && raw.trim() !== "") {
|
|
63
|
+
try {
|
|
64
|
+
input = JSON.parse(raw);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
throw refuse(`the arguments of tool call ${call.id} are not JSON`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (!isRecord(input))
|
|
71
|
+
throw refuse(`the arguments of tool call ${call.id} must be a JSON object`);
|
|
72
|
+
return { type: "tool_use", id: call.id, name: call.function.name, input };
|
|
73
|
+
}
|
|
74
|
+
/** OpenAI messages as Anthropic's system text and alternating user/assistant turns; tool results ride in user turns. */
|
|
75
|
+
function conversation(messages) {
|
|
76
|
+
if (!Array.isArray(messages))
|
|
77
|
+
throw refuse("messages must be a list");
|
|
78
|
+
const system = [];
|
|
79
|
+
const turns = [];
|
|
80
|
+
const push = (role, content) => {
|
|
81
|
+
if (content.length === 0)
|
|
82
|
+
return;
|
|
83
|
+
const last = turns.at(-1);
|
|
84
|
+
// Anthropic wants turns to alternate: consecutive turns of one role (several tool results) merge into one.
|
|
85
|
+
if (last?.role === role)
|
|
86
|
+
last.content.push(...content);
|
|
87
|
+
else
|
|
88
|
+
turns.push({ role, content });
|
|
89
|
+
};
|
|
90
|
+
for (const message of messages) {
|
|
91
|
+
if (!isRecord(message))
|
|
92
|
+
throw refuse("every message must be an object");
|
|
93
|
+
const role = message.role;
|
|
94
|
+
if (role === "system" || role === "developer") {
|
|
95
|
+
const text = textOf(message.content);
|
|
96
|
+
if (text !== "")
|
|
97
|
+
system.push(text);
|
|
98
|
+
}
|
|
99
|
+
else if (role === "user")
|
|
100
|
+
push("user", blocks(message.content));
|
|
101
|
+
else if (role === "assistant") {
|
|
102
|
+
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) }]);
|
|
109
|
+
}
|
|
110
|
+
else
|
|
111
|
+
throw refuse(`the message role ${String(role)} has no Anthropic counterpart`);
|
|
112
|
+
}
|
|
113
|
+
return { system, turns };
|
|
114
|
+
}
|
|
115
|
+
function tools(list) {
|
|
116
|
+
if (!Array.isArray(list) || list.length === 0)
|
|
117
|
+
return undefined;
|
|
118
|
+
return list.map((tool) => {
|
|
119
|
+
if (!isRecord(tool) || tool.type !== "function" || !isRecord(tool.function))
|
|
120
|
+
return tool;
|
|
121
|
+
const fn = tool.function;
|
|
122
|
+
return {
|
|
123
|
+
name: fn.name,
|
|
124
|
+
...(typeof fn.description === "string" ? { description: fn.description } : {}),
|
|
125
|
+
input_schema: isRecord(fn.parameters) ? fn.parameters : { type: "object", properties: {} },
|
|
126
|
+
...(fn.strict === true ? { strict: true } : {}),
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function toolChoice(choice, parallel) {
|
|
131
|
+
let out;
|
|
132
|
+
if (choice === "auto")
|
|
133
|
+
out = { type: "auto" };
|
|
134
|
+
else if (choice === "none")
|
|
135
|
+
out = { type: "none" };
|
|
136
|
+
else if (choice === "required")
|
|
137
|
+
out = { type: "any" };
|
|
138
|
+
else if (isRecord(choice) && isRecord(choice.function) && typeof choice.function.name === "string")
|
|
139
|
+
out = { type: "tool", name: choice.function.name };
|
|
140
|
+
if (parallel === false)
|
|
141
|
+
out = { ...(out ?? { type: "auto" }), disable_parallel_tool_use: true };
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
/** Builds the Anthropic Messages body of an OpenAI chat request. */
|
|
145
|
+
export function toAnthropicRequest(body, model, stream, provider) {
|
|
146
|
+
if (typeof body.n === "number" && body.n > 1)
|
|
147
|
+
throw refuse("n above 1 is not supported");
|
|
148
|
+
const { system, turns } = conversation(body.messages);
|
|
149
|
+
const format = body.response_format;
|
|
150
|
+
let outputConfig = body.output_config;
|
|
151
|
+
if (isRecord(format) && format.type === "json_object")
|
|
152
|
+
system.push("Answer with one JSON object and nothing else.");
|
|
153
|
+
if (isRecord(format) && format.type === "json_schema" && isRecord(format.json_schema) && isRecord(format.json_schema.schema)) {
|
|
154
|
+
outputConfig = { ...(isRecord(outputConfig) ? outputConfig : {}), format: { type: "json_schema", schema: format.json_schema.schema } };
|
|
155
|
+
}
|
|
156
|
+
const maxTokens = [body.max_completion_tokens, body.max_tokens].find((value) => typeof value === "number") ?? provider.defaultMaxTokens ?? ANTHROPIC_DEFAULT_MAX_TOKENS;
|
|
157
|
+
const stop = typeof body.stop === "string" ? [body.stop] : Array.isArray(body.stop) ? body.stop : undefined;
|
|
158
|
+
const out = { model, max_tokens: maxTokens, messages: turns };
|
|
159
|
+
for (const field of PASSTHROUGH)
|
|
160
|
+
if (body[field] !== undefined)
|
|
161
|
+
out[field] = body[field];
|
|
162
|
+
if (outputConfig !== undefined)
|
|
163
|
+
out.output_config = outputConfig;
|
|
164
|
+
if (system.length > 0)
|
|
165
|
+
out.system = system.join("\n\n");
|
|
166
|
+
if (stop !== undefined)
|
|
167
|
+
out.stop_sequences = stop;
|
|
168
|
+
const mapped = tools(body.tools);
|
|
169
|
+
if (mapped !== undefined)
|
|
170
|
+
out.tools = mapped;
|
|
171
|
+
const choice = toolChoice(body.tool_choice, body.parallel_tool_calls);
|
|
172
|
+
if (choice !== undefined)
|
|
173
|
+
out.tool_choice = choice;
|
|
174
|
+
if (typeof body.user === "string")
|
|
175
|
+
out.metadata = { user_id: body.user };
|
|
176
|
+
if (stream)
|
|
177
|
+
out.stream = true;
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
/** Anthropic's stop reason as OpenAI's finish reason. */
|
|
181
|
+
export function finishReason(reason) {
|
|
182
|
+
switch (reason) {
|
|
183
|
+
case "end_turn":
|
|
184
|
+
case "stop_sequence":
|
|
185
|
+
case "pause_turn":
|
|
186
|
+
return "stop";
|
|
187
|
+
case "max_tokens":
|
|
188
|
+
case "model_context_window_exceeded":
|
|
189
|
+
return "length";
|
|
190
|
+
case "tool_use":
|
|
191
|
+
return "tool_calls";
|
|
192
|
+
case "refusal":
|
|
193
|
+
return "content_filter";
|
|
194
|
+
default:
|
|
195
|
+
return reason === null || reason === undefined ? null : "stop";
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function count(value) {
|
|
199
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
200
|
+
}
|
|
201
|
+
/** Anthropic usage as OpenAI usage: the prompt counts the cache reads and writes, which Anthropic lists apart. */
|
|
202
|
+
export function usageOf(usage, outputOverride) {
|
|
203
|
+
if (!isRecord(usage))
|
|
204
|
+
return undefined;
|
|
205
|
+
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 } };
|
|
209
|
+
}
|
|
210
|
+
/** An Anthropic message as an OpenAI `chat.completion`. */
|
|
211
|
+
export function fromAnthropicMessage(json) {
|
|
212
|
+
const content = Array.isArray(json.content) ? json.content.filter(isRecord) : [];
|
|
213
|
+
const text = content
|
|
214
|
+
.filter((block) => block.type === "text" && typeof block.text === "string")
|
|
215
|
+
.map((block) => String(block.text))
|
|
216
|
+
.join("");
|
|
217
|
+
const calls = content
|
|
218
|
+
.filter((block) => block.type === "tool_use")
|
|
219
|
+
.map((block) => ({ id: String(block.id), type: "function", function: { name: String(block.name), arguments: JSON.stringify(block.input ?? {}) } }));
|
|
220
|
+
const usage = usageOf(json.usage);
|
|
221
|
+
return {
|
|
222
|
+
id: typeof json.id === "string" ? json.id : "anthropic",
|
|
223
|
+
object: "chat.completion",
|
|
224
|
+
created: Math.floor(Date.now() / 1000),
|
|
225
|
+
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) }],
|
|
227
|
+
...(usage === undefined ? {} : { usage }),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
/** The Anthropic Messages API behind the OpenAI shape: `x-api-key` auth, `/messages`, and its SSE events as OpenAI chunks. */
|
|
231
|
+
export const anthropicWire = {
|
|
232
|
+
secretHeaders: ["x-api-key", "authorization"],
|
|
233
|
+
headers: (secret, provider) => {
|
|
234
|
+
const version = Object.entries(provider.headers ?? {}).find(([name]) => name.toLowerCase() === "anthropic-version");
|
|
235
|
+
return { "x-api-key": secret, "anthropic-version": version?.[1] ?? ANTHROPIC_VERSION };
|
|
236
|
+
},
|
|
237
|
+
chat: (body, model, stream, provider) => ({ path: "/messages", body: toAnthropicRequest(body, model, stream, provider) }),
|
|
238
|
+
embeddings: () => {
|
|
239
|
+
throw refuse("Anthropic has no embeddings API");
|
|
240
|
+
},
|
|
241
|
+
chatResponse: fromAnthropicMessage,
|
|
242
|
+
async *chunks(events) {
|
|
243
|
+
let id = "anthropic";
|
|
244
|
+
let model = "";
|
|
245
|
+
let inputUsage = undefined;
|
|
246
|
+
let output = 0;
|
|
247
|
+
const created = Math.floor(Date.now() / 1000);
|
|
248
|
+
// Anthropic numbers content blocks; OpenAI numbers tool calls. A tool_use block's index maps to its tool call's.
|
|
249
|
+
const toolIndex = new Map();
|
|
250
|
+
const chunk = (delta, finish = null) => ({ kind: "chunk", chunk: { id, object: "chat.completion.chunk", created, model, choices: [{ index: 0, delta, finish_reason: finish }] } });
|
|
251
|
+
for await (const event of events) {
|
|
252
|
+
let data;
|
|
253
|
+
try {
|
|
254
|
+
data = JSON.parse(event.data);
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
yield { kind: "malformed" };
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (!isRecord(data))
|
|
261
|
+
continue;
|
|
262
|
+
switch (data.type) {
|
|
263
|
+
case "message_start": {
|
|
264
|
+
const message = isRecord(data.message) ? data.message : {};
|
|
265
|
+
if (typeof message.id === "string")
|
|
266
|
+
id = message.id;
|
|
267
|
+
if (typeof message.model === "string")
|
|
268
|
+
model = message.model;
|
|
269
|
+
inputUsage = message.usage;
|
|
270
|
+
if (isRecord(message.usage))
|
|
271
|
+
output = count(message.usage.output_tokens);
|
|
272
|
+
yield chunk({ role: "assistant", content: "" });
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
case "content_block_start": {
|
|
276
|
+
const block = isRecord(data.content_block) ? data.content_block : {};
|
|
277
|
+
const index = count(data.index);
|
|
278
|
+
if (block.type === "tool_use") {
|
|
279
|
+
const call = toolIndex.size;
|
|
280
|
+
toolIndex.set(index, call);
|
|
281
|
+
yield chunk({ tool_calls: [{ index: call, id: block.id, type: "function", function: { name: block.name, arguments: "" } }] });
|
|
282
|
+
}
|
|
283
|
+
else if (block.type === "text" && typeof block.text === "string" && block.text !== "")
|
|
284
|
+
yield chunk({ content: block.text });
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case "content_block_delta": {
|
|
288
|
+
const delta = isRecord(data.delta) ? data.delta : {};
|
|
289
|
+
if (delta.type === "text_delta" && typeof delta.text === "string")
|
|
290
|
+
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
|
+
}
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
case "message_delta": {
|
|
299
|
+
const delta = isRecord(data.delta) ? data.delta : {};
|
|
300
|
+
// message_delta's usage is cumulative: the last one is the total.
|
|
301
|
+
if (isRecord(data.usage) && typeof data.usage.output_tokens === "number")
|
|
302
|
+
output = data.usage.output_tokens;
|
|
303
|
+
yield chunk({}, finishReason(delta.stop_reason));
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
case "message_stop": {
|
|
307
|
+
const usage = usageOf(inputUsage ?? {}, output);
|
|
308
|
+
yield { kind: "chunk", chunk: { id, object: "chat.completion.chunk", created, model, choices: [], usage } };
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
case "error":
|
|
312
|
+
yield { kind: "error", body: data };
|
|
313
|
+
return;
|
|
314
|
+
default:
|
|
315
|
+
// ping, content_block_stop, and event types Anthropic adds later are tolerated, as its docs ask.
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// A stream that ends before message_stop was cut: the attempt failed.
|
|
320
|
+
yield { kind: "error", body: { type: "error", error: { type: "api_error", message: "the stream ended before message_stop" } } };
|
|
321
|
+
},
|
|
322
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ProviderConfig } from "../types.js";
|
|
2
|
+
import type { Wire } from "./wire.js";
|
|
3
|
+
export type { Wire, WireEvent, WireRequest } from "./wire.js";
|
|
4
|
+
/** The protocol adapter a provider is called through, by its kind. */
|
|
5
|
+
export declare function wireOf(provider: ProviderConfig): Wire;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { presetOf } from "../providers.js";
|
|
2
|
+
import { anthropicWire } from "./anthropic.js";
|
|
3
|
+
import { openAiWire } from "./wire.js";
|
|
4
|
+
/** The protocol adapter a provider is called through, by its kind. */
|
|
5
|
+
export function wireOf(provider) {
|
|
6
|
+
return presetOf(provider).protocol === "anthropic" ? anthropicWire : openAiWire;
|
|
7
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { SseEvent } from "../sse.js";
|
|
2
|
+
import type { ProviderConfig } from "../types.js";
|
|
3
|
+
/** A request in the provider's own wire format: the path under its base URL and the JSON body. */
|
|
4
|
+
export interface WireRequest {
|
|
5
|
+
readonly path: string;
|
|
6
|
+
readonly body: Record<string, unknown>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* One item a provider stream yields, already in OpenAI's shape: a chunk (a `chat.completion.chunk` record), an error the
|
|
10
|
+
* provider streamed (its raw error body, classified by the router), or an event that is not JSON.
|
|
11
|
+
*/
|
|
12
|
+
export type WireEvent = {
|
|
13
|
+
readonly kind: "chunk";
|
|
14
|
+
readonly chunk: Record<string, unknown>;
|
|
15
|
+
} | {
|
|
16
|
+
readonly kind: "error";
|
|
17
|
+
readonly body: unknown;
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: "malformed";
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* A protocol adapter: it turns the router's OpenAI chat-completions and embeddings requests into a provider's own and
|
|
23
|
+
* its answers back, so callers above the gateway speak OpenAI whatever the provider below speaks.
|
|
24
|
+
*/
|
|
25
|
+
export interface Wire {
|
|
26
|
+
/** The headers that carry the secret; caller and provider headers can never set them. */
|
|
27
|
+
readonly secretHeaders: readonly string[];
|
|
28
|
+
/** The auth and protocol headers of one attempt. */
|
|
29
|
+
headers(secret: string, provider: ProviderConfig): Record<string, string>;
|
|
30
|
+
/** A chat request; throws an `AttemptError` (`invalid_request`) when the request cannot be expressed. */
|
|
31
|
+
chat(body: Record<string, unknown>, model: string, stream: boolean, provider: ProviderConfig): WireRequest;
|
|
32
|
+
/** An embeddings request. */
|
|
33
|
+
embeddings(body: Record<string, unknown>, model: string): WireRequest;
|
|
34
|
+
/** A unary chat answer as an OpenAI `chat.completion` record. */
|
|
35
|
+
chatResponse(json: Record<string, unknown>): Record<string, unknown>;
|
|
36
|
+
/** A provider stream as OpenAI chunks; the router stops at the end of the iterable. */
|
|
37
|
+
chunks(events: AsyncIterable<SseEvent>): AsyncGenerator<WireEvent, void, undefined>;
|
|
38
|
+
}
|
|
39
|
+
/** True for a plain object. */
|
|
40
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
41
|
+
/** The OpenAI chat-completions and embeddings API with a bearer key, which every OpenAI-compatible provider speaks. */
|
|
42
|
+
export declare const openAiWire: Wire;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** True for a plain object. */
|
|
2
|
+
export function isRecord(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
/** The OpenAI chat-completions and embeddings API with a bearer key, which every OpenAI-compatible provider speaks. */
|
|
6
|
+
export const openAiWire = {
|
|
7
|
+
secretHeaders: ["authorization"],
|
|
8
|
+
headers: (secret) => ({ authorization: `Bearer ${secret}` }),
|
|
9
|
+
chat: (body, model, stream) => ({ path: "/chat/completions", body: { ...body, model, ...(stream ? { stream: true } : {}) } }),
|
|
10
|
+
embeddings: (body, model) => ({ path: "/embeddings", body: { ...body, model } }),
|
|
11
|
+
chatResponse: (json) => json,
|
|
12
|
+
async *chunks(events) {
|
|
13
|
+
for await (const event of events) {
|
|
14
|
+
if (event.data === "[DONE]")
|
|
15
|
+
return;
|
|
16
|
+
let json;
|
|
17
|
+
try {
|
|
18
|
+
json = JSON.parse(event.data);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
yield { kind: "malformed" };
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (!isRecord(json))
|
|
25
|
+
continue;
|
|
26
|
+
// OpenRouter streams an error mid-stream as a chunk with a top-level error (and finish_reason "error").
|
|
27
|
+
if (json.error !== undefined && json.error !== null) {
|
|
28
|
+
yield { kind: "error", body: json };
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(json.choices))
|
|
32
|
+
yield { kind: "chunk", chunk: json };
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|