@providerprotocol/ai 0.0.2 → 0.0.4
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/README.md +5 -1
- package/dist/anthropic/index.d.ts +2 -2
- package/dist/anthropic/index.js +4 -2
- package/dist/anthropic/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +235 -0
- package/dist/openrouter/index.js +1342 -0
- package/dist/openrouter/index.js.map +1 -0
- package/package.json +9 -2
- package/src/openrouter/index.ts +10 -0
- package/src/providers/anthropic/transform.ts +6 -2
- package/src/providers/anthropic/types.ts +3 -3
- package/src/providers/openrouter/index.ts +173 -0
- package/src/providers/openrouter/llm.completions.ts +201 -0
- package/src/providers/openrouter/llm.responses.ts +211 -0
- package/src/providers/openrouter/transform.completions.ts +605 -0
- package/src/providers/openrouter/transform.responses.ts +755 -0
- package/src/providers/openrouter/types.ts +723 -0
|
@@ -0,0 +1,1342 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AssistantMessage,
|
|
3
|
+
isAssistantMessage,
|
|
4
|
+
isToolResultMessage,
|
|
5
|
+
isUserMessage
|
|
6
|
+
} from "../chunk-QUUX4G7U.js";
|
|
7
|
+
import {
|
|
8
|
+
parseSSEStream
|
|
9
|
+
} from "../chunk-X5G4EHL7.js";
|
|
10
|
+
import {
|
|
11
|
+
UPPError,
|
|
12
|
+
doFetch,
|
|
13
|
+
doStreamFetch,
|
|
14
|
+
normalizeHttpError,
|
|
15
|
+
resolveApiKey
|
|
16
|
+
} from "../chunk-SUNYWHTH.js";
|
|
17
|
+
|
|
18
|
+
// src/providers/openrouter/transform.completions.ts
|
|
19
|
+
function transformRequest(request, modelId) {
|
|
20
|
+
const params = request.params ?? {};
|
|
21
|
+
const openrouterRequest = {
|
|
22
|
+
model: modelId,
|
|
23
|
+
messages: transformMessages(request.messages, request.system)
|
|
24
|
+
};
|
|
25
|
+
if (params.temperature !== void 0) {
|
|
26
|
+
openrouterRequest.temperature = params.temperature;
|
|
27
|
+
}
|
|
28
|
+
if (params.top_p !== void 0) {
|
|
29
|
+
openrouterRequest.top_p = params.top_p;
|
|
30
|
+
}
|
|
31
|
+
if (params.top_k !== void 0) {
|
|
32
|
+
openrouterRequest.top_k = params.top_k;
|
|
33
|
+
}
|
|
34
|
+
if (params.min_p !== void 0) {
|
|
35
|
+
openrouterRequest.min_p = params.min_p;
|
|
36
|
+
}
|
|
37
|
+
if (params.top_a !== void 0) {
|
|
38
|
+
openrouterRequest.top_a = params.top_a;
|
|
39
|
+
}
|
|
40
|
+
if (params.max_tokens !== void 0) {
|
|
41
|
+
openrouterRequest.max_tokens = params.max_tokens;
|
|
42
|
+
}
|
|
43
|
+
if (params.frequency_penalty !== void 0) {
|
|
44
|
+
openrouterRequest.frequency_penalty = params.frequency_penalty;
|
|
45
|
+
}
|
|
46
|
+
if (params.presence_penalty !== void 0) {
|
|
47
|
+
openrouterRequest.presence_penalty = params.presence_penalty;
|
|
48
|
+
}
|
|
49
|
+
if (params.repetition_penalty !== void 0) {
|
|
50
|
+
openrouterRequest.repetition_penalty = params.repetition_penalty;
|
|
51
|
+
}
|
|
52
|
+
if (params.stop !== void 0) {
|
|
53
|
+
openrouterRequest.stop = params.stop;
|
|
54
|
+
}
|
|
55
|
+
if (params.logprobs !== void 0) {
|
|
56
|
+
openrouterRequest.logprobs = params.logprobs;
|
|
57
|
+
}
|
|
58
|
+
if (params.top_logprobs !== void 0) {
|
|
59
|
+
openrouterRequest.top_logprobs = params.top_logprobs;
|
|
60
|
+
}
|
|
61
|
+
if (params.seed !== void 0) {
|
|
62
|
+
openrouterRequest.seed = params.seed;
|
|
63
|
+
}
|
|
64
|
+
if (params.user !== void 0) {
|
|
65
|
+
openrouterRequest.user = params.user;
|
|
66
|
+
}
|
|
67
|
+
if (params.logit_bias !== void 0) {
|
|
68
|
+
openrouterRequest.logit_bias = params.logit_bias;
|
|
69
|
+
}
|
|
70
|
+
if (params.prediction !== void 0) {
|
|
71
|
+
openrouterRequest.prediction = params.prediction;
|
|
72
|
+
}
|
|
73
|
+
if (params.transforms !== void 0) {
|
|
74
|
+
openrouterRequest.transforms = params.transforms;
|
|
75
|
+
}
|
|
76
|
+
if (params.models !== void 0) {
|
|
77
|
+
openrouterRequest.models = params.models;
|
|
78
|
+
}
|
|
79
|
+
if (params.route !== void 0) {
|
|
80
|
+
openrouterRequest.route = params.route;
|
|
81
|
+
}
|
|
82
|
+
if (params.provider !== void 0) {
|
|
83
|
+
openrouterRequest.provider = params.provider;
|
|
84
|
+
}
|
|
85
|
+
if (params.debug !== void 0) {
|
|
86
|
+
openrouterRequest.debug = params.debug;
|
|
87
|
+
}
|
|
88
|
+
if (request.tools && request.tools.length > 0) {
|
|
89
|
+
openrouterRequest.tools = request.tools.map(transformTool);
|
|
90
|
+
if (params.parallel_tool_calls !== void 0) {
|
|
91
|
+
openrouterRequest.parallel_tool_calls = params.parallel_tool_calls;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (request.structure) {
|
|
95
|
+
const schema = {
|
|
96
|
+
type: "object",
|
|
97
|
+
properties: request.structure.properties,
|
|
98
|
+
required: request.structure.required,
|
|
99
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
|
|
100
|
+
};
|
|
101
|
+
if (request.structure.description) {
|
|
102
|
+
schema.description = request.structure.description;
|
|
103
|
+
}
|
|
104
|
+
openrouterRequest.response_format = {
|
|
105
|
+
type: "json_schema",
|
|
106
|
+
json_schema: {
|
|
107
|
+
name: "json_response",
|
|
108
|
+
description: request.structure.description,
|
|
109
|
+
schema,
|
|
110
|
+
strict: true
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
} else if (params.response_format !== void 0) {
|
|
114
|
+
openrouterRequest.response_format = params.response_format;
|
|
115
|
+
}
|
|
116
|
+
return openrouterRequest;
|
|
117
|
+
}
|
|
118
|
+
function transformMessages(messages, system) {
|
|
119
|
+
const result = [];
|
|
120
|
+
if (system) {
|
|
121
|
+
result.push({
|
|
122
|
+
role: "system",
|
|
123
|
+
content: system
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
for (const message of messages) {
|
|
127
|
+
if (isToolResultMessage(message)) {
|
|
128
|
+
const toolMessages = transformToolResults(message);
|
|
129
|
+
result.push(...toolMessages);
|
|
130
|
+
} else {
|
|
131
|
+
const transformed = transformMessage(message);
|
|
132
|
+
if (transformed) {
|
|
133
|
+
result.push(transformed);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
function filterValidContent(content) {
|
|
140
|
+
return content.filter((c) => c && typeof c.type === "string");
|
|
141
|
+
}
|
|
142
|
+
function transformMessage(message) {
|
|
143
|
+
if (isUserMessage(message)) {
|
|
144
|
+
const validContent = filterValidContent(message.content);
|
|
145
|
+
if (validContent.length === 1 && validContent[0]?.type === "text") {
|
|
146
|
+
return {
|
|
147
|
+
role: "user",
|
|
148
|
+
content: validContent[0].text
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
role: "user",
|
|
153
|
+
content: validContent.map(transformContentBlock)
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
if (isAssistantMessage(message)) {
|
|
157
|
+
const validContent = filterValidContent(message.content);
|
|
158
|
+
const textContent = validContent.filter((c) => c.type === "text").map((c) => c.text).join("");
|
|
159
|
+
const assistantMessage = {
|
|
160
|
+
role: "assistant",
|
|
161
|
+
content: textContent || null
|
|
162
|
+
};
|
|
163
|
+
if (message.toolCalls && message.toolCalls.length > 0) {
|
|
164
|
+
assistantMessage.tool_calls = message.toolCalls.map((call) => ({
|
|
165
|
+
id: call.toolCallId,
|
|
166
|
+
type: "function",
|
|
167
|
+
function: {
|
|
168
|
+
name: call.toolName,
|
|
169
|
+
arguments: JSON.stringify(call.arguments)
|
|
170
|
+
}
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
return assistantMessage;
|
|
174
|
+
}
|
|
175
|
+
if (isToolResultMessage(message)) {
|
|
176
|
+
const results = message.results.map((result) => ({
|
|
177
|
+
role: "tool",
|
|
178
|
+
tool_call_id: result.toolCallId,
|
|
179
|
+
content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
|
|
180
|
+
}));
|
|
181
|
+
return results[0] ?? null;
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
function transformToolResults(message) {
|
|
186
|
+
if (!isToolResultMessage(message)) {
|
|
187
|
+
const single = transformMessage(message);
|
|
188
|
+
return single ? [single] : [];
|
|
189
|
+
}
|
|
190
|
+
return message.results.map((result) => ({
|
|
191
|
+
role: "tool",
|
|
192
|
+
tool_call_id: result.toolCallId,
|
|
193
|
+
content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
|
|
194
|
+
}));
|
|
195
|
+
}
|
|
196
|
+
function transformContentBlock(block) {
|
|
197
|
+
switch (block.type) {
|
|
198
|
+
case "text":
|
|
199
|
+
return { type: "text", text: block.text };
|
|
200
|
+
case "image": {
|
|
201
|
+
const imageBlock = block;
|
|
202
|
+
let url;
|
|
203
|
+
if (imageBlock.source.type === "base64") {
|
|
204
|
+
url = `data:${imageBlock.mimeType};base64,${imageBlock.source.data}`;
|
|
205
|
+
} else if (imageBlock.source.type === "url") {
|
|
206
|
+
url = imageBlock.source.url;
|
|
207
|
+
} else if (imageBlock.source.type === "bytes") {
|
|
208
|
+
const base64 = btoa(
|
|
209
|
+
Array.from(imageBlock.source.data).map((b) => String.fromCharCode(b)).join("")
|
|
210
|
+
);
|
|
211
|
+
url = `data:${imageBlock.mimeType};base64,${base64}`;
|
|
212
|
+
} else {
|
|
213
|
+
throw new Error("Unknown image source type");
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
type: "image_url",
|
|
217
|
+
image_url: { url }
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
default:
|
|
221
|
+
throw new Error(`Unsupported content type: ${block.type}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function transformTool(tool) {
|
|
225
|
+
return {
|
|
226
|
+
type: "function",
|
|
227
|
+
function: {
|
|
228
|
+
name: tool.name,
|
|
229
|
+
description: tool.description,
|
|
230
|
+
parameters: {
|
|
231
|
+
type: "object",
|
|
232
|
+
properties: tool.parameters.properties,
|
|
233
|
+
required: tool.parameters.required,
|
|
234
|
+
...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function transformResponse(data) {
|
|
240
|
+
const choice = data.choices[0];
|
|
241
|
+
if (!choice) {
|
|
242
|
+
throw new Error("No choices in OpenRouter response");
|
|
243
|
+
}
|
|
244
|
+
const textContent = [];
|
|
245
|
+
let structuredData;
|
|
246
|
+
if (choice.message.content) {
|
|
247
|
+
textContent.push({ type: "text", text: choice.message.content });
|
|
248
|
+
try {
|
|
249
|
+
structuredData = JSON.parse(choice.message.content);
|
|
250
|
+
} catch {
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const toolCalls = [];
|
|
254
|
+
if (choice.message.tool_calls) {
|
|
255
|
+
for (const call of choice.message.tool_calls) {
|
|
256
|
+
let args = {};
|
|
257
|
+
try {
|
|
258
|
+
args = JSON.parse(call.function.arguments);
|
|
259
|
+
} catch {
|
|
260
|
+
}
|
|
261
|
+
toolCalls.push({
|
|
262
|
+
toolCallId: call.id,
|
|
263
|
+
toolName: call.function.name,
|
|
264
|
+
arguments: args
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const message = new AssistantMessage(
|
|
269
|
+
textContent,
|
|
270
|
+
toolCalls.length > 0 ? toolCalls : void 0,
|
|
271
|
+
{
|
|
272
|
+
id: data.id,
|
|
273
|
+
metadata: {
|
|
274
|
+
openrouter: {
|
|
275
|
+
model: data.model,
|
|
276
|
+
finish_reason: choice.finish_reason,
|
|
277
|
+
system_fingerprint: data.system_fingerprint
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
);
|
|
282
|
+
const usage = {
|
|
283
|
+
inputTokens: data.usage.prompt_tokens,
|
|
284
|
+
outputTokens: data.usage.completion_tokens,
|
|
285
|
+
totalTokens: data.usage.total_tokens
|
|
286
|
+
};
|
|
287
|
+
let stopReason = "end_turn";
|
|
288
|
+
switch (choice.finish_reason) {
|
|
289
|
+
case "stop":
|
|
290
|
+
stopReason = "end_turn";
|
|
291
|
+
break;
|
|
292
|
+
case "length":
|
|
293
|
+
stopReason = "max_tokens";
|
|
294
|
+
break;
|
|
295
|
+
case "tool_calls":
|
|
296
|
+
stopReason = "tool_use";
|
|
297
|
+
break;
|
|
298
|
+
case "content_filter":
|
|
299
|
+
stopReason = "content_filter";
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
return {
|
|
303
|
+
message,
|
|
304
|
+
usage,
|
|
305
|
+
stopReason,
|
|
306
|
+
data: structuredData
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
function createStreamState() {
|
|
310
|
+
return {
|
|
311
|
+
id: "",
|
|
312
|
+
model: "",
|
|
313
|
+
text: "",
|
|
314
|
+
toolCalls: /* @__PURE__ */ new Map(),
|
|
315
|
+
finishReason: null,
|
|
316
|
+
inputTokens: 0,
|
|
317
|
+
outputTokens: 0
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
function transformStreamEvent(chunk, state) {
|
|
321
|
+
const events = [];
|
|
322
|
+
if (chunk.id && !state.id) {
|
|
323
|
+
state.id = chunk.id;
|
|
324
|
+
events.push({ type: "message_start", index: 0, delta: {} });
|
|
325
|
+
}
|
|
326
|
+
if (chunk.model) {
|
|
327
|
+
state.model = chunk.model;
|
|
328
|
+
}
|
|
329
|
+
const choice = chunk.choices[0];
|
|
330
|
+
if (choice) {
|
|
331
|
+
if (choice.delta.content) {
|
|
332
|
+
state.text += choice.delta.content;
|
|
333
|
+
events.push({
|
|
334
|
+
type: "text_delta",
|
|
335
|
+
index: 0,
|
|
336
|
+
delta: { text: choice.delta.content }
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
if (choice.delta.tool_calls) {
|
|
340
|
+
for (const toolCallDelta of choice.delta.tool_calls) {
|
|
341
|
+
const index = toolCallDelta.index;
|
|
342
|
+
let toolCall = state.toolCalls.get(index);
|
|
343
|
+
if (!toolCall) {
|
|
344
|
+
toolCall = { id: "", name: "", arguments: "" };
|
|
345
|
+
state.toolCalls.set(index, toolCall);
|
|
346
|
+
}
|
|
347
|
+
if (toolCallDelta.id) {
|
|
348
|
+
toolCall.id = toolCallDelta.id;
|
|
349
|
+
}
|
|
350
|
+
if (toolCallDelta.function?.name) {
|
|
351
|
+
toolCall.name = toolCallDelta.function.name;
|
|
352
|
+
}
|
|
353
|
+
if (toolCallDelta.function?.arguments) {
|
|
354
|
+
toolCall.arguments += toolCallDelta.function.arguments;
|
|
355
|
+
events.push({
|
|
356
|
+
type: "tool_call_delta",
|
|
357
|
+
index,
|
|
358
|
+
delta: {
|
|
359
|
+
toolCallId: toolCall.id,
|
|
360
|
+
toolName: toolCall.name,
|
|
361
|
+
argumentsJson: toolCallDelta.function.arguments
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (choice.finish_reason) {
|
|
368
|
+
state.finishReason = choice.finish_reason;
|
|
369
|
+
events.push({ type: "message_stop", index: 0, delta: {} });
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (chunk.usage) {
|
|
373
|
+
state.inputTokens = chunk.usage.prompt_tokens;
|
|
374
|
+
state.outputTokens = chunk.usage.completion_tokens;
|
|
375
|
+
}
|
|
376
|
+
return events;
|
|
377
|
+
}
|
|
378
|
+
function buildResponseFromState(state) {
|
|
379
|
+
const textContent = [];
|
|
380
|
+
let structuredData;
|
|
381
|
+
if (state.text) {
|
|
382
|
+
textContent.push({ type: "text", text: state.text });
|
|
383
|
+
try {
|
|
384
|
+
structuredData = JSON.parse(state.text);
|
|
385
|
+
} catch {
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const toolCalls = [];
|
|
389
|
+
for (const [, toolCall] of state.toolCalls) {
|
|
390
|
+
let args = {};
|
|
391
|
+
if (toolCall.arguments) {
|
|
392
|
+
try {
|
|
393
|
+
args = JSON.parse(toolCall.arguments);
|
|
394
|
+
} catch {
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
toolCalls.push({
|
|
398
|
+
toolCallId: toolCall.id,
|
|
399
|
+
toolName: toolCall.name,
|
|
400
|
+
arguments: args
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
const message = new AssistantMessage(
|
|
404
|
+
textContent,
|
|
405
|
+
toolCalls.length > 0 ? toolCalls : void 0,
|
|
406
|
+
{
|
|
407
|
+
id: state.id,
|
|
408
|
+
metadata: {
|
|
409
|
+
openrouter: {
|
|
410
|
+
model: state.model,
|
|
411
|
+
finish_reason: state.finishReason
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
);
|
|
416
|
+
const usage = {
|
|
417
|
+
inputTokens: state.inputTokens,
|
|
418
|
+
outputTokens: state.outputTokens,
|
|
419
|
+
totalTokens: state.inputTokens + state.outputTokens
|
|
420
|
+
};
|
|
421
|
+
let stopReason = "end_turn";
|
|
422
|
+
switch (state.finishReason) {
|
|
423
|
+
case "stop":
|
|
424
|
+
stopReason = "end_turn";
|
|
425
|
+
break;
|
|
426
|
+
case "length":
|
|
427
|
+
stopReason = "max_tokens";
|
|
428
|
+
break;
|
|
429
|
+
case "tool_calls":
|
|
430
|
+
stopReason = "tool_use";
|
|
431
|
+
break;
|
|
432
|
+
case "content_filter":
|
|
433
|
+
stopReason = "content_filter";
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
message,
|
|
438
|
+
usage,
|
|
439
|
+
stopReason,
|
|
440
|
+
data: structuredData
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// src/providers/openrouter/llm.completions.ts
|
|
445
|
+
var OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions";
|
|
446
|
+
var OPENROUTER_CAPABILITIES = {
|
|
447
|
+
streaming: true,
|
|
448
|
+
tools: true,
|
|
449
|
+
structuredOutput: true,
|
|
450
|
+
imageInput: true,
|
|
451
|
+
videoInput: false,
|
|
452
|
+
audioInput: false
|
|
453
|
+
};
|
|
454
|
+
function createCompletionsLLMHandler() {
|
|
455
|
+
let providerRef = null;
|
|
456
|
+
return {
|
|
457
|
+
_setProvider(provider) {
|
|
458
|
+
providerRef = provider;
|
|
459
|
+
},
|
|
460
|
+
bind(modelId) {
|
|
461
|
+
if (!providerRef) {
|
|
462
|
+
throw new UPPError(
|
|
463
|
+
"Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
|
|
464
|
+
"INVALID_REQUEST",
|
|
465
|
+
"openrouter",
|
|
466
|
+
"llm"
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
const model = {
|
|
470
|
+
modelId,
|
|
471
|
+
capabilities: OPENROUTER_CAPABILITIES,
|
|
472
|
+
get provider() {
|
|
473
|
+
return providerRef;
|
|
474
|
+
},
|
|
475
|
+
async complete(request) {
|
|
476
|
+
const apiKey = await resolveApiKey(
|
|
477
|
+
request.config,
|
|
478
|
+
"OPENROUTER_API_KEY",
|
|
479
|
+
"openrouter",
|
|
480
|
+
"llm"
|
|
481
|
+
);
|
|
482
|
+
const baseUrl = request.config.baseUrl ?? OPENROUTER_API_URL;
|
|
483
|
+
const body = transformRequest(request, modelId);
|
|
484
|
+
const response = await doFetch(
|
|
485
|
+
baseUrl,
|
|
486
|
+
{
|
|
487
|
+
method: "POST",
|
|
488
|
+
headers: {
|
|
489
|
+
"Content-Type": "application/json",
|
|
490
|
+
Authorization: `Bearer ${apiKey}`
|
|
491
|
+
},
|
|
492
|
+
body: JSON.stringify(body),
|
|
493
|
+
signal: request.signal
|
|
494
|
+
},
|
|
495
|
+
request.config,
|
|
496
|
+
"openrouter",
|
|
497
|
+
"llm"
|
|
498
|
+
);
|
|
499
|
+
const data = await response.json();
|
|
500
|
+
return transformResponse(data);
|
|
501
|
+
},
|
|
502
|
+
stream(request) {
|
|
503
|
+
const state = createStreamState();
|
|
504
|
+
let responseResolve;
|
|
505
|
+
let responseReject;
|
|
506
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
507
|
+
responseResolve = resolve;
|
|
508
|
+
responseReject = reject;
|
|
509
|
+
});
|
|
510
|
+
async function* generateEvents() {
|
|
511
|
+
try {
|
|
512
|
+
const apiKey = await resolveApiKey(
|
|
513
|
+
request.config,
|
|
514
|
+
"OPENROUTER_API_KEY",
|
|
515
|
+
"openrouter",
|
|
516
|
+
"llm"
|
|
517
|
+
);
|
|
518
|
+
const baseUrl = request.config.baseUrl ?? OPENROUTER_API_URL;
|
|
519
|
+
const body = transformRequest(request, modelId);
|
|
520
|
+
body.stream = true;
|
|
521
|
+
body.stream_options = { include_usage: true };
|
|
522
|
+
const response = await doStreamFetch(
|
|
523
|
+
baseUrl,
|
|
524
|
+
{
|
|
525
|
+
method: "POST",
|
|
526
|
+
headers: {
|
|
527
|
+
"Content-Type": "application/json",
|
|
528
|
+
Authorization: `Bearer ${apiKey}`
|
|
529
|
+
},
|
|
530
|
+
body: JSON.stringify(body),
|
|
531
|
+
signal: request.signal
|
|
532
|
+
},
|
|
533
|
+
request.config,
|
|
534
|
+
"openrouter",
|
|
535
|
+
"llm"
|
|
536
|
+
);
|
|
537
|
+
if (!response.ok) {
|
|
538
|
+
const error = await normalizeHttpError(response, "openrouter", "llm");
|
|
539
|
+
responseReject(error);
|
|
540
|
+
throw error;
|
|
541
|
+
}
|
|
542
|
+
if (!response.body) {
|
|
543
|
+
const error = new UPPError(
|
|
544
|
+
"No response body for streaming request",
|
|
545
|
+
"PROVIDER_ERROR",
|
|
546
|
+
"openrouter",
|
|
547
|
+
"llm"
|
|
548
|
+
);
|
|
549
|
+
responseReject(error);
|
|
550
|
+
throw error;
|
|
551
|
+
}
|
|
552
|
+
for await (const data of parseSSEStream(response.body)) {
|
|
553
|
+
if (data === "[DONE]") {
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (typeof data === "object" && data !== null) {
|
|
557
|
+
const chunk = data;
|
|
558
|
+
if ("error" in chunk && chunk.error) {
|
|
559
|
+
const errorData = chunk.error;
|
|
560
|
+
const error = new UPPError(
|
|
561
|
+
errorData.message ?? "Unknown error",
|
|
562
|
+
"PROVIDER_ERROR",
|
|
563
|
+
"openrouter",
|
|
564
|
+
"llm"
|
|
565
|
+
);
|
|
566
|
+
responseReject(error);
|
|
567
|
+
throw error;
|
|
568
|
+
}
|
|
569
|
+
const uppEvents = transformStreamEvent(chunk, state);
|
|
570
|
+
for (const event of uppEvents) {
|
|
571
|
+
yield event;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
responseResolve(buildResponseFromState(state));
|
|
576
|
+
} catch (error) {
|
|
577
|
+
responseReject(error);
|
|
578
|
+
throw error;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return {
|
|
582
|
+
[Symbol.asyncIterator]() {
|
|
583
|
+
return generateEvents();
|
|
584
|
+
},
|
|
585
|
+
response: responsePromise
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
return model;
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// src/providers/openrouter/transform.responses.ts
|
|
595
|
+
function transformRequest2(request, modelId) {
|
|
596
|
+
const params = request.params ?? {};
|
|
597
|
+
const openrouterRequest = {
|
|
598
|
+
model: modelId,
|
|
599
|
+
input: transformInputItems(request.messages, request.system)
|
|
600
|
+
};
|
|
601
|
+
if (params.temperature !== void 0) {
|
|
602
|
+
openrouterRequest.temperature = params.temperature;
|
|
603
|
+
}
|
|
604
|
+
if (params.top_p !== void 0) {
|
|
605
|
+
openrouterRequest.top_p = params.top_p;
|
|
606
|
+
}
|
|
607
|
+
if (params.max_output_tokens !== void 0) {
|
|
608
|
+
openrouterRequest.max_output_tokens = params.max_output_tokens;
|
|
609
|
+
} else if (params.max_tokens !== void 0) {
|
|
610
|
+
openrouterRequest.max_output_tokens = params.max_tokens;
|
|
611
|
+
}
|
|
612
|
+
if (params.reasoning !== void 0) {
|
|
613
|
+
openrouterRequest.reasoning = { ...params.reasoning };
|
|
614
|
+
}
|
|
615
|
+
if (request.tools && request.tools.length > 0) {
|
|
616
|
+
openrouterRequest.tools = request.tools.map(transformTool2);
|
|
617
|
+
if (params.parallel_tool_calls !== void 0) {
|
|
618
|
+
openrouterRequest.parallel_tool_calls = params.parallel_tool_calls;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (request.structure) {
|
|
622
|
+
const schema = {
|
|
623
|
+
type: "object",
|
|
624
|
+
properties: request.structure.properties,
|
|
625
|
+
required: request.structure.required,
|
|
626
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
|
|
627
|
+
};
|
|
628
|
+
if (request.structure.description) {
|
|
629
|
+
schema.description = request.structure.description;
|
|
630
|
+
}
|
|
631
|
+
openrouterRequest.text = {
|
|
632
|
+
format: {
|
|
633
|
+
type: "json_schema",
|
|
634
|
+
name: "json_response",
|
|
635
|
+
description: request.structure.description,
|
|
636
|
+
schema,
|
|
637
|
+
strict: true
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
return openrouterRequest;
|
|
642
|
+
}
|
|
643
|
+
function transformInputItems(messages, system) {
|
|
644
|
+
const result = [];
|
|
645
|
+
if (system) {
|
|
646
|
+
result.push({
|
|
647
|
+
type: "message",
|
|
648
|
+
role: "system",
|
|
649
|
+
content: system
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
for (const message of messages) {
|
|
653
|
+
const items = transformMessage2(message);
|
|
654
|
+
result.push(...items);
|
|
655
|
+
}
|
|
656
|
+
if (result.length === 1 && result[0]?.type === "message") {
|
|
657
|
+
const item = result[0];
|
|
658
|
+
if (item.role === "user" && typeof item.content === "string") {
|
|
659
|
+
return item.content;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return result;
|
|
663
|
+
}
|
|
664
|
+
function filterValidContent2(content) {
|
|
665
|
+
return content.filter((c) => c && typeof c.type === "string");
|
|
666
|
+
}
|
|
667
|
+
function transformMessage2(message) {
|
|
668
|
+
if (isUserMessage(message)) {
|
|
669
|
+
const validContent = filterValidContent2(message.content);
|
|
670
|
+
if (validContent.length === 1 && validContent[0]?.type === "text") {
|
|
671
|
+
return [
|
|
672
|
+
{
|
|
673
|
+
type: "message",
|
|
674
|
+
role: "user",
|
|
675
|
+
content: validContent[0].text
|
|
676
|
+
}
|
|
677
|
+
];
|
|
678
|
+
}
|
|
679
|
+
return [
|
|
680
|
+
{
|
|
681
|
+
type: "message",
|
|
682
|
+
role: "user",
|
|
683
|
+
content: validContent.map(transformContentPart)
|
|
684
|
+
}
|
|
685
|
+
];
|
|
686
|
+
}
|
|
687
|
+
if (isAssistantMessage(message)) {
|
|
688
|
+
const validContent = filterValidContent2(message.content);
|
|
689
|
+
const items = [];
|
|
690
|
+
const contentParts = validContent.filter((c) => c.type === "text").map((c) => ({
|
|
691
|
+
type: "output_text",
|
|
692
|
+
text: c.text,
|
|
693
|
+
annotations: []
|
|
694
|
+
}));
|
|
695
|
+
const messageId = message.id ?? `msg_${Date.now()}`;
|
|
696
|
+
if (contentParts.length > 0) {
|
|
697
|
+
items.push({
|
|
698
|
+
type: "message",
|
|
699
|
+
role: "assistant",
|
|
700
|
+
id: messageId,
|
|
701
|
+
status: "completed",
|
|
702
|
+
content: contentParts
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
const openrouterMeta = message.metadata?.openrouter;
|
|
706
|
+
const functionCallItems = openrouterMeta?.functionCallItems;
|
|
707
|
+
if (functionCallItems && functionCallItems.length > 0) {
|
|
708
|
+
for (const fc of functionCallItems) {
|
|
709
|
+
items.push({
|
|
710
|
+
type: "function_call",
|
|
711
|
+
id: fc.id,
|
|
712
|
+
call_id: fc.call_id,
|
|
713
|
+
name: fc.name,
|
|
714
|
+
arguments: fc.arguments
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
} else if (message.toolCalls && message.toolCalls.length > 0) {
|
|
718
|
+
for (const call of message.toolCalls) {
|
|
719
|
+
items.push({
|
|
720
|
+
type: "function_call",
|
|
721
|
+
id: `fc_${call.toolCallId}`,
|
|
722
|
+
call_id: call.toolCallId,
|
|
723
|
+
name: call.toolName,
|
|
724
|
+
arguments: JSON.stringify(call.arguments)
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return items;
|
|
729
|
+
}
|
|
730
|
+
if (isToolResultMessage(message)) {
|
|
731
|
+
return message.results.map((result, index) => ({
|
|
732
|
+
type: "function_call_output",
|
|
733
|
+
id: `fco_${result.toolCallId}_${index}`,
|
|
734
|
+
call_id: result.toolCallId,
|
|
735
|
+
output: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
|
|
736
|
+
}));
|
|
737
|
+
}
|
|
738
|
+
return [];
|
|
739
|
+
}
|
|
740
|
+
function transformContentPart(block) {
|
|
741
|
+
switch (block.type) {
|
|
742
|
+
case "text":
|
|
743
|
+
return { type: "input_text", text: block.text };
|
|
744
|
+
case "image": {
|
|
745
|
+
const imageBlock = block;
|
|
746
|
+
if (imageBlock.source.type === "base64") {
|
|
747
|
+
return {
|
|
748
|
+
type: "input_image",
|
|
749
|
+
image_url: `data:${imageBlock.mimeType};base64,${imageBlock.source.data}`,
|
|
750
|
+
detail: "auto"
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
if (imageBlock.source.type === "url") {
|
|
754
|
+
return {
|
|
755
|
+
type: "input_image",
|
|
756
|
+
image_url: imageBlock.source.url,
|
|
757
|
+
detail: "auto"
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
if (imageBlock.source.type === "bytes") {
|
|
761
|
+
const base64 = btoa(
|
|
762
|
+
Array.from(imageBlock.source.data).map((b) => String.fromCharCode(b)).join("")
|
|
763
|
+
);
|
|
764
|
+
return {
|
|
765
|
+
type: "input_image",
|
|
766
|
+
image_url: `data:${imageBlock.mimeType};base64,${base64}`,
|
|
767
|
+
detail: "auto"
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
throw new Error("Unknown image source type");
|
|
771
|
+
}
|
|
772
|
+
default:
|
|
773
|
+
throw new Error(`Unsupported content type: ${block.type}`);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
function transformTool2(tool) {
|
|
777
|
+
return {
|
|
778
|
+
type: "function",
|
|
779
|
+
name: tool.name,
|
|
780
|
+
description: tool.description,
|
|
781
|
+
parameters: {
|
|
782
|
+
type: "object",
|
|
783
|
+
properties: tool.parameters.properties,
|
|
784
|
+
required: tool.parameters.required,
|
|
785
|
+
...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
function transformResponse2(data) {
|
|
790
|
+
const textContent = [];
|
|
791
|
+
const toolCalls = [];
|
|
792
|
+
const functionCallItems = [];
|
|
793
|
+
let hadRefusal = false;
|
|
794
|
+
let structuredData;
|
|
795
|
+
for (const item of data.output) {
|
|
796
|
+
if (item.type === "message") {
|
|
797
|
+
const messageItem = item;
|
|
798
|
+
for (const content of messageItem.content) {
|
|
799
|
+
if (content.type === "output_text") {
|
|
800
|
+
textContent.push({ type: "text", text: content.text });
|
|
801
|
+
if (structuredData === void 0) {
|
|
802
|
+
try {
|
|
803
|
+
structuredData = JSON.parse(content.text);
|
|
804
|
+
} catch {
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
} else if (content.type === "refusal") {
|
|
808
|
+
textContent.push({ type: "text", text: content.refusal });
|
|
809
|
+
hadRefusal = true;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
} else if (item.type === "function_call") {
|
|
813
|
+
const functionCall = item;
|
|
814
|
+
let args = {};
|
|
815
|
+
try {
|
|
816
|
+
args = JSON.parse(functionCall.arguments);
|
|
817
|
+
} catch {
|
|
818
|
+
}
|
|
819
|
+
toolCalls.push({
|
|
820
|
+
toolCallId: functionCall.call_id,
|
|
821
|
+
toolName: functionCall.name,
|
|
822
|
+
arguments: args
|
|
823
|
+
});
|
|
824
|
+
functionCallItems.push({
|
|
825
|
+
id: functionCall.id,
|
|
826
|
+
call_id: functionCall.call_id,
|
|
827
|
+
name: functionCall.name,
|
|
828
|
+
arguments: functionCall.arguments
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
const message = new AssistantMessage(
|
|
833
|
+
textContent,
|
|
834
|
+
toolCalls.length > 0 ? toolCalls : void 0,
|
|
835
|
+
{
|
|
836
|
+
id: data.id,
|
|
837
|
+
metadata: {
|
|
838
|
+
openrouter: {
|
|
839
|
+
model: data.model,
|
|
840
|
+
status: data.status,
|
|
841
|
+
// Store response_id for multi-turn tool calling
|
|
842
|
+
response_id: data.id,
|
|
843
|
+
functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
);
|
|
848
|
+
const usage = {
|
|
849
|
+
inputTokens: data.usage.input_tokens,
|
|
850
|
+
outputTokens: data.usage.output_tokens,
|
|
851
|
+
totalTokens: data.usage.total_tokens
|
|
852
|
+
};
|
|
853
|
+
let stopReason = "end_turn";
|
|
854
|
+
if (data.status === "completed") {
|
|
855
|
+
stopReason = toolCalls.length > 0 ? "tool_use" : "end_turn";
|
|
856
|
+
} else if (data.status === "incomplete") {
|
|
857
|
+
stopReason = data.incomplete_details?.reason === "max_output_tokens" ? "max_tokens" : "end_turn";
|
|
858
|
+
} else if (data.status === "failed") {
|
|
859
|
+
stopReason = "error";
|
|
860
|
+
}
|
|
861
|
+
if (hadRefusal && stopReason !== "error") {
|
|
862
|
+
stopReason = "content_filter";
|
|
863
|
+
}
|
|
864
|
+
return {
|
|
865
|
+
message,
|
|
866
|
+
usage,
|
|
867
|
+
stopReason,
|
|
868
|
+
data: structuredData
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
function createStreamState2() {
|
|
872
|
+
return {
|
|
873
|
+
id: "",
|
|
874
|
+
model: "",
|
|
875
|
+
textByIndex: /* @__PURE__ */ new Map(),
|
|
876
|
+
toolCalls: /* @__PURE__ */ new Map(),
|
|
877
|
+
status: "in_progress",
|
|
878
|
+
inputTokens: 0,
|
|
879
|
+
outputTokens: 0,
|
|
880
|
+
hadRefusal: false
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
function transformStreamEvent2(event, state) {
|
|
884
|
+
const events = [];
|
|
885
|
+
switch (event.type) {
|
|
886
|
+
case "response.created":
|
|
887
|
+
state.id = event.response.id;
|
|
888
|
+
state.model = event.response.model;
|
|
889
|
+
events.push({ type: "message_start", index: 0, delta: {} });
|
|
890
|
+
break;
|
|
891
|
+
case "response.in_progress":
|
|
892
|
+
state.status = "in_progress";
|
|
893
|
+
break;
|
|
894
|
+
case "response.completed":
|
|
895
|
+
case "response.done":
|
|
896
|
+
state.status = "completed";
|
|
897
|
+
if (event.response?.usage) {
|
|
898
|
+
state.inputTokens = event.response.usage.input_tokens;
|
|
899
|
+
state.outputTokens = event.response.usage.output_tokens;
|
|
900
|
+
}
|
|
901
|
+
if (event.response?.output) {
|
|
902
|
+
for (let i = 0; i < event.response.output.length; i++) {
|
|
903
|
+
const item = event.response.output[i];
|
|
904
|
+
if (item && item.type === "function_call") {
|
|
905
|
+
const functionCall = item;
|
|
906
|
+
const existing = state.toolCalls.get(i) ?? { arguments: "" };
|
|
907
|
+
existing.itemId = functionCall.id ?? existing.itemId;
|
|
908
|
+
existing.callId = functionCall.call_id ?? existing.callId;
|
|
909
|
+
existing.name = functionCall.name ?? existing.name;
|
|
910
|
+
if (functionCall.arguments) {
|
|
911
|
+
existing.arguments = functionCall.arguments;
|
|
912
|
+
}
|
|
913
|
+
state.toolCalls.set(i, existing);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
events.push({ type: "message_stop", index: 0, delta: {} });
|
|
918
|
+
break;
|
|
919
|
+
case "response.failed":
|
|
920
|
+
state.status = "failed";
|
|
921
|
+
events.push({ type: "message_stop", index: 0, delta: {} });
|
|
922
|
+
break;
|
|
923
|
+
case "response.output_item.added":
|
|
924
|
+
if (event.item.type === "function_call") {
|
|
925
|
+
const functionCall = event.item;
|
|
926
|
+
const existing = state.toolCalls.get(event.output_index) ?? {
|
|
927
|
+
arguments: ""
|
|
928
|
+
};
|
|
929
|
+
existing.itemId = functionCall.id;
|
|
930
|
+
existing.callId = functionCall.call_id;
|
|
931
|
+
existing.name = functionCall.name;
|
|
932
|
+
if (functionCall.arguments) {
|
|
933
|
+
existing.arguments = functionCall.arguments;
|
|
934
|
+
}
|
|
935
|
+
state.toolCalls.set(event.output_index, existing);
|
|
936
|
+
}
|
|
937
|
+
events.push({
|
|
938
|
+
type: "content_block_start",
|
|
939
|
+
index: event.output_index,
|
|
940
|
+
delta: {}
|
|
941
|
+
});
|
|
942
|
+
break;
|
|
943
|
+
case "response.output_item.done":
|
|
944
|
+
if (event.item.type === "function_call") {
|
|
945
|
+
const functionCall = event.item;
|
|
946
|
+
const existing = state.toolCalls.get(event.output_index) ?? {
|
|
947
|
+
arguments: ""
|
|
948
|
+
};
|
|
949
|
+
existing.itemId = functionCall.id;
|
|
950
|
+
existing.callId = functionCall.call_id;
|
|
951
|
+
existing.name = functionCall.name;
|
|
952
|
+
if (functionCall.arguments) {
|
|
953
|
+
existing.arguments = functionCall.arguments;
|
|
954
|
+
}
|
|
955
|
+
state.toolCalls.set(event.output_index, existing);
|
|
956
|
+
} else if (event.item.type === "message") {
|
|
957
|
+
const messageItem = event.item;
|
|
958
|
+
for (const content of messageItem.content || []) {
|
|
959
|
+
if (content.type === "output_text") {
|
|
960
|
+
const existingText = state.textByIndex.get(event.output_index) ?? "";
|
|
961
|
+
if (!existingText && content.text) {
|
|
962
|
+
state.textByIndex.set(event.output_index, content.text);
|
|
963
|
+
events.push({
|
|
964
|
+
type: "text_delta",
|
|
965
|
+
index: event.output_index,
|
|
966
|
+
delta: { text: content.text }
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
events.push({
|
|
973
|
+
type: "content_block_stop",
|
|
974
|
+
index: event.output_index,
|
|
975
|
+
delta: {}
|
|
976
|
+
});
|
|
977
|
+
break;
|
|
978
|
+
case "response.content_part.delta":
|
|
979
|
+
case "response.output_text.delta": {
|
|
980
|
+
const textDelta = event.delta;
|
|
981
|
+
const currentText = state.textByIndex.get(event.output_index) ?? "";
|
|
982
|
+
state.textByIndex.set(event.output_index, currentText + textDelta);
|
|
983
|
+
events.push({
|
|
984
|
+
type: "text_delta",
|
|
985
|
+
index: event.output_index,
|
|
986
|
+
delta: { text: textDelta }
|
|
987
|
+
});
|
|
988
|
+
break;
|
|
989
|
+
}
|
|
990
|
+
case "response.output_text.done":
|
|
991
|
+
case "response.content_part.done":
|
|
992
|
+
if ("text" in event) {
|
|
993
|
+
state.textByIndex.set(event.output_index, event.text);
|
|
994
|
+
}
|
|
995
|
+
break;
|
|
996
|
+
case "response.refusal.delta": {
|
|
997
|
+
state.hadRefusal = true;
|
|
998
|
+
const currentRefusal = state.textByIndex.get(event.output_index) ?? "";
|
|
999
|
+
state.textByIndex.set(event.output_index, currentRefusal + event.delta);
|
|
1000
|
+
events.push({
|
|
1001
|
+
type: "text_delta",
|
|
1002
|
+
index: event.output_index,
|
|
1003
|
+
delta: { text: event.delta }
|
|
1004
|
+
});
|
|
1005
|
+
break;
|
|
1006
|
+
}
|
|
1007
|
+
case "response.refusal.done":
|
|
1008
|
+
state.hadRefusal = true;
|
|
1009
|
+
state.textByIndex.set(event.output_index, event.refusal);
|
|
1010
|
+
break;
|
|
1011
|
+
case "response.function_call_arguments.delta": {
|
|
1012
|
+
let toolCall = state.toolCalls.get(event.output_index);
|
|
1013
|
+
if (!toolCall) {
|
|
1014
|
+
toolCall = { arguments: "" };
|
|
1015
|
+
state.toolCalls.set(event.output_index, toolCall);
|
|
1016
|
+
}
|
|
1017
|
+
if (event.item_id && !toolCall.itemId) {
|
|
1018
|
+
toolCall.itemId = event.item_id;
|
|
1019
|
+
}
|
|
1020
|
+
if (event.call_id && !toolCall.callId) {
|
|
1021
|
+
toolCall.callId = event.call_id;
|
|
1022
|
+
}
|
|
1023
|
+
toolCall.arguments += event.delta;
|
|
1024
|
+
events.push({
|
|
1025
|
+
type: "tool_call_delta",
|
|
1026
|
+
index: event.output_index,
|
|
1027
|
+
delta: {
|
|
1028
|
+
toolCallId: toolCall.callId ?? toolCall.itemId ?? "",
|
|
1029
|
+
toolName: toolCall.name,
|
|
1030
|
+
argumentsJson: event.delta
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
break;
|
|
1034
|
+
}
|
|
1035
|
+
case "response.function_call_arguments.done": {
|
|
1036
|
+
let toolCall = state.toolCalls.get(event.output_index);
|
|
1037
|
+
if (!toolCall) {
|
|
1038
|
+
toolCall = { arguments: "" };
|
|
1039
|
+
state.toolCalls.set(event.output_index, toolCall);
|
|
1040
|
+
}
|
|
1041
|
+
if (event.item_id) {
|
|
1042
|
+
toolCall.itemId = event.item_id;
|
|
1043
|
+
}
|
|
1044
|
+
if (event.call_id) {
|
|
1045
|
+
toolCall.callId = event.call_id;
|
|
1046
|
+
}
|
|
1047
|
+
toolCall.name = event.name;
|
|
1048
|
+
toolCall.arguments = event.arguments;
|
|
1049
|
+
break;
|
|
1050
|
+
}
|
|
1051
|
+
case "response.reasoning.delta":
|
|
1052
|
+
events.push({
|
|
1053
|
+
type: "reasoning_delta",
|
|
1054
|
+
index: 0,
|
|
1055
|
+
delta: { text: event.delta }
|
|
1056
|
+
});
|
|
1057
|
+
break;
|
|
1058
|
+
case "error":
|
|
1059
|
+
break;
|
|
1060
|
+
default:
|
|
1061
|
+
break;
|
|
1062
|
+
}
|
|
1063
|
+
return events;
|
|
1064
|
+
}
|
|
1065
|
+
function buildResponseFromState2(state) {
|
|
1066
|
+
const textContent = [];
|
|
1067
|
+
let structuredData;
|
|
1068
|
+
for (const [, text] of state.textByIndex) {
|
|
1069
|
+
if (text) {
|
|
1070
|
+
textContent.push({ type: "text", text });
|
|
1071
|
+
if (structuredData === void 0) {
|
|
1072
|
+
try {
|
|
1073
|
+
structuredData = JSON.parse(text);
|
|
1074
|
+
} catch {
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
const toolCalls = [];
|
|
1080
|
+
const functionCallItems = [];
|
|
1081
|
+
for (const [, toolCall] of state.toolCalls) {
|
|
1082
|
+
let args = {};
|
|
1083
|
+
if (toolCall.arguments) {
|
|
1084
|
+
try {
|
|
1085
|
+
args = JSON.parse(toolCall.arguments);
|
|
1086
|
+
} catch {
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
const itemId = toolCall.itemId ?? "";
|
|
1090
|
+
const callId = toolCall.callId ?? toolCall.itemId ?? "";
|
|
1091
|
+
const name = toolCall.name ?? "";
|
|
1092
|
+
toolCalls.push({
|
|
1093
|
+
toolCallId: callId,
|
|
1094
|
+
toolName: name,
|
|
1095
|
+
arguments: args
|
|
1096
|
+
});
|
|
1097
|
+
if (itemId && callId && name) {
|
|
1098
|
+
functionCallItems.push({
|
|
1099
|
+
id: itemId,
|
|
1100
|
+
call_id: callId,
|
|
1101
|
+
name,
|
|
1102
|
+
arguments: toolCall.arguments
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
const message = new AssistantMessage(
|
|
1107
|
+
textContent,
|
|
1108
|
+
toolCalls.length > 0 ? toolCalls : void 0,
|
|
1109
|
+
{
|
|
1110
|
+
id: state.id,
|
|
1111
|
+
metadata: {
|
|
1112
|
+
openrouter: {
|
|
1113
|
+
model: state.model,
|
|
1114
|
+
status: state.status,
|
|
1115
|
+
// Store response_id for multi-turn tool calling
|
|
1116
|
+
response_id: state.id,
|
|
1117
|
+
functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
);
|
|
1122
|
+
const usage = {
|
|
1123
|
+
inputTokens: state.inputTokens,
|
|
1124
|
+
outputTokens: state.outputTokens,
|
|
1125
|
+
totalTokens: state.inputTokens + state.outputTokens
|
|
1126
|
+
};
|
|
1127
|
+
let stopReason = "end_turn";
|
|
1128
|
+
if (state.status === "completed") {
|
|
1129
|
+
stopReason = toolCalls.length > 0 ? "tool_use" : "end_turn";
|
|
1130
|
+
} else if (state.status === "failed") {
|
|
1131
|
+
stopReason = "error";
|
|
1132
|
+
}
|
|
1133
|
+
if (state.hadRefusal && stopReason !== "error") {
|
|
1134
|
+
stopReason = "content_filter";
|
|
1135
|
+
}
|
|
1136
|
+
return {
|
|
1137
|
+
message,
|
|
1138
|
+
usage,
|
|
1139
|
+
stopReason,
|
|
1140
|
+
data: structuredData
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
// src/providers/openrouter/llm.responses.ts
|
|
1145
|
+
var OPENROUTER_RESPONSES_API_URL = "https://openrouter.ai/api/v1/responses";
|
|
1146
|
+
var OPENROUTER_CAPABILITIES2 = {
|
|
1147
|
+
streaming: true,
|
|
1148
|
+
tools: true,
|
|
1149
|
+
structuredOutput: true,
|
|
1150
|
+
imageInput: true,
|
|
1151
|
+
videoInput: false,
|
|
1152
|
+
audioInput: false
|
|
1153
|
+
};
|
|
1154
|
+
function createResponsesLLMHandler() {
|
|
1155
|
+
let providerRef = null;
|
|
1156
|
+
return {
|
|
1157
|
+
_setProvider(provider) {
|
|
1158
|
+
providerRef = provider;
|
|
1159
|
+
},
|
|
1160
|
+
bind(modelId) {
|
|
1161
|
+
if (!providerRef) {
|
|
1162
|
+
throw new UPPError(
|
|
1163
|
+
"Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
|
|
1164
|
+
"INVALID_REQUEST",
|
|
1165
|
+
"openrouter",
|
|
1166
|
+
"llm"
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
const model = {
|
|
1170
|
+
modelId,
|
|
1171
|
+
capabilities: OPENROUTER_CAPABILITIES2,
|
|
1172
|
+
get provider() {
|
|
1173
|
+
return providerRef;
|
|
1174
|
+
},
|
|
1175
|
+
async complete(request) {
|
|
1176
|
+
const apiKey = await resolveApiKey(
|
|
1177
|
+
request.config,
|
|
1178
|
+
"OPENROUTER_API_KEY",
|
|
1179
|
+
"openrouter",
|
|
1180
|
+
"llm"
|
|
1181
|
+
);
|
|
1182
|
+
const baseUrl = request.config.baseUrl ?? OPENROUTER_RESPONSES_API_URL;
|
|
1183
|
+
const body = transformRequest2(request, modelId);
|
|
1184
|
+
const response = await doFetch(
|
|
1185
|
+
baseUrl,
|
|
1186
|
+
{
|
|
1187
|
+
method: "POST",
|
|
1188
|
+
headers: {
|
|
1189
|
+
"Content-Type": "application/json",
|
|
1190
|
+
Authorization: `Bearer ${apiKey}`
|
|
1191
|
+
},
|
|
1192
|
+
body: JSON.stringify(body),
|
|
1193
|
+
signal: request.signal
|
|
1194
|
+
},
|
|
1195
|
+
request.config,
|
|
1196
|
+
"openrouter",
|
|
1197
|
+
"llm"
|
|
1198
|
+
);
|
|
1199
|
+
const data = await response.json();
|
|
1200
|
+
if (data.status === "failed" && data.error) {
|
|
1201
|
+
throw new UPPError(
|
|
1202
|
+
data.error.message,
|
|
1203
|
+
"PROVIDER_ERROR",
|
|
1204
|
+
"openrouter",
|
|
1205
|
+
"llm"
|
|
1206
|
+
);
|
|
1207
|
+
}
|
|
1208
|
+
return transformResponse2(data);
|
|
1209
|
+
},
|
|
1210
|
+
stream(request) {
|
|
1211
|
+
const state = createStreamState2();
|
|
1212
|
+
let responseResolve;
|
|
1213
|
+
let responseReject;
|
|
1214
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
1215
|
+
responseResolve = resolve;
|
|
1216
|
+
responseReject = reject;
|
|
1217
|
+
});
|
|
1218
|
+
async function* generateEvents() {
|
|
1219
|
+
try {
|
|
1220
|
+
const apiKey = await resolveApiKey(
|
|
1221
|
+
request.config,
|
|
1222
|
+
"OPENROUTER_API_KEY",
|
|
1223
|
+
"openrouter",
|
|
1224
|
+
"llm"
|
|
1225
|
+
);
|
|
1226
|
+
const baseUrl = request.config.baseUrl ?? OPENROUTER_RESPONSES_API_URL;
|
|
1227
|
+
const body = transformRequest2(request, modelId);
|
|
1228
|
+
body.stream = true;
|
|
1229
|
+
const response = await doStreamFetch(
|
|
1230
|
+
baseUrl,
|
|
1231
|
+
{
|
|
1232
|
+
method: "POST",
|
|
1233
|
+
headers: {
|
|
1234
|
+
"Content-Type": "application/json",
|
|
1235
|
+
Authorization: `Bearer ${apiKey}`
|
|
1236
|
+
},
|
|
1237
|
+
body: JSON.stringify(body),
|
|
1238
|
+
signal: request.signal
|
|
1239
|
+
},
|
|
1240
|
+
request.config,
|
|
1241
|
+
"openrouter",
|
|
1242
|
+
"llm"
|
|
1243
|
+
);
|
|
1244
|
+
if (!response.ok) {
|
|
1245
|
+
const error = await normalizeHttpError(response, "openrouter", "llm");
|
|
1246
|
+
responseReject(error);
|
|
1247
|
+
throw error;
|
|
1248
|
+
}
|
|
1249
|
+
if (!response.body) {
|
|
1250
|
+
const error = new UPPError(
|
|
1251
|
+
"No response body for streaming request",
|
|
1252
|
+
"PROVIDER_ERROR",
|
|
1253
|
+
"openrouter",
|
|
1254
|
+
"llm"
|
|
1255
|
+
);
|
|
1256
|
+
responseReject(error);
|
|
1257
|
+
throw error;
|
|
1258
|
+
}
|
|
1259
|
+
for await (const data of parseSSEStream(response.body)) {
|
|
1260
|
+
if (data === "[DONE]") {
|
|
1261
|
+
continue;
|
|
1262
|
+
}
|
|
1263
|
+
if (typeof data === "object" && data !== null) {
|
|
1264
|
+
const event = data;
|
|
1265
|
+
if (event.type === "error") {
|
|
1266
|
+
const errorEvent = event;
|
|
1267
|
+
const error = new UPPError(
|
|
1268
|
+
errorEvent.error.message,
|
|
1269
|
+
"PROVIDER_ERROR",
|
|
1270
|
+
"openrouter",
|
|
1271
|
+
"llm"
|
|
1272
|
+
);
|
|
1273
|
+
responseReject(error);
|
|
1274
|
+
throw error;
|
|
1275
|
+
}
|
|
1276
|
+
const uppEvents = transformStreamEvent2(event, state);
|
|
1277
|
+
for (const uppEvent of uppEvents) {
|
|
1278
|
+
yield uppEvent;
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
responseResolve(buildResponseFromState2(state));
|
|
1283
|
+
} catch (error) {
|
|
1284
|
+
responseReject(error);
|
|
1285
|
+
throw error;
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
return {
|
|
1289
|
+
[Symbol.asyncIterator]() {
|
|
1290
|
+
return generateEvents();
|
|
1291
|
+
},
|
|
1292
|
+
response: responsePromise
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
};
|
|
1296
|
+
return model;
|
|
1297
|
+
}
|
|
1298
|
+
};
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// src/providers/openrouter/index.ts
|
|
1302
|
+
function createOpenRouterProvider() {
|
|
1303
|
+
let currentApiMode = "completions";
|
|
1304
|
+
const completionsHandler = createCompletionsLLMHandler();
|
|
1305
|
+
const responsesHandler = createResponsesLLMHandler();
|
|
1306
|
+
const fn = function(modelId, options) {
|
|
1307
|
+
const apiMode = options?.api ?? "completions";
|
|
1308
|
+
currentApiMode = apiMode;
|
|
1309
|
+
return { modelId, provider };
|
|
1310
|
+
};
|
|
1311
|
+
const modalities = {
|
|
1312
|
+
get llm() {
|
|
1313
|
+
return currentApiMode === "responses" ? responsesHandler : completionsHandler;
|
|
1314
|
+
}
|
|
1315
|
+
};
|
|
1316
|
+
Object.defineProperties(fn, {
|
|
1317
|
+
name: {
|
|
1318
|
+
value: "openrouter",
|
|
1319
|
+
writable: false,
|
|
1320
|
+
configurable: true
|
|
1321
|
+
},
|
|
1322
|
+
version: {
|
|
1323
|
+
value: "1.0.0",
|
|
1324
|
+
writable: false,
|
|
1325
|
+
configurable: true
|
|
1326
|
+
},
|
|
1327
|
+
modalities: {
|
|
1328
|
+
value: modalities,
|
|
1329
|
+
writable: false,
|
|
1330
|
+
configurable: true
|
|
1331
|
+
}
|
|
1332
|
+
});
|
|
1333
|
+
const provider = fn;
|
|
1334
|
+
completionsHandler._setProvider?.(provider);
|
|
1335
|
+
responsesHandler._setProvider?.(provider);
|
|
1336
|
+
return provider;
|
|
1337
|
+
}
|
|
1338
|
+
var openrouter = createOpenRouterProvider();
|
|
1339
|
+
export {
|
|
1340
|
+
openrouter
|
|
1341
|
+
};
|
|
1342
|
+
//# sourceMappingURL=index.js.map
|