@providerprotocol/ai 0.0.21 → 0.0.23
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 +188 -6
- package/dist/anthropic/index.d.ts +1 -1
- package/dist/anthropic/index.js +115 -39
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-Y3GBJNA2.js → chunk-55X3W2MN.js} +4 -3
- package/dist/chunk-55X3W2MN.js.map +1 -0
- package/dist/chunk-73IIE3QT.js +120 -0
- package/dist/chunk-73IIE3QT.js.map +1 -0
- package/dist/{chunk-M4BMM5IB.js → chunk-MF5ETY5O.js} +13 -4
- package/dist/chunk-MF5ETY5O.js.map +1 -0
- package/dist/{chunk-SKY2JLA7.js → chunk-MKDLXV4O.js} +1 -1
- package/dist/chunk-MKDLXV4O.js.map +1 -0
- package/dist/{chunk-Z7RBRCRN.js → chunk-NWS5IKNR.js} +37 -11
- package/dist/chunk-NWS5IKNR.js.map +1 -0
- package/dist/{chunk-EDENPF3E.js → chunk-QNJO7DSD.js} +152 -53
- package/dist/chunk-QNJO7DSD.js.map +1 -0
- package/dist/{chunk-Z4ILICF5.js → chunk-SBCATNHA.js} +43 -14
- package/dist/chunk-SBCATNHA.js.map +1 -0
- package/dist/chunk-Z6DKC37J.js +50 -0
- package/dist/chunk-Z6DKC37J.js.map +1 -0
- package/dist/google/index.d.ts +22 -7
- package/dist/google/index.js +286 -85
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +3 -3
- package/dist/http/index.js +4 -4
- package/dist/index.d.ts +10 -6
- package/dist/index.js +331 -204
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.d.ts +5 -2
- package/dist/ollama/index.js +87 -28
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +1 -1
- package/dist/openai/index.js +226 -81
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +1 -1
- package/dist/openrouter/index.js +199 -64
- package/dist/openrouter/index.js.map +1 -1
- package/dist/{provider-DGQHYE6I.d.ts → provider-DR1yins0.d.ts} +159 -53
- package/dist/proxy/index.d.ts +2 -2
- package/dist/proxy/index.js +178 -17
- package/dist/proxy/index.js.map +1 -1
- package/dist/{retry-Pcs3hnbu.d.ts → retry-DJiqAslw.d.ts} +11 -2
- package/dist/{stream-Di9acos2.d.ts → stream-BuTrqt_j.d.ts} +103 -41
- package/dist/xai/index.d.ts +1 -1
- package/dist/xai/index.js +189 -75
- package/dist/xai/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-EDENPF3E.js.map +0 -1
- package/dist/chunk-M4BMM5IB.js.map +0 -1
- package/dist/chunk-SKY2JLA7.js.map +0 -1
- package/dist/chunk-Y3GBJNA2.js.map +0 -1
- package/dist/chunk-Z4ILICF5.js.map +0 -1
- package/dist/chunk-Z7RBRCRN.js.map +0 -1
package/dist/openrouter/index.js
CHANGED
|
@@ -1,22 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseJsonResponse
|
|
3
|
+
} from "../chunk-Z6DKC37J.js";
|
|
4
|
+
import {
|
|
5
|
+
StreamEventType
|
|
6
|
+
} from "../chunk-73IIE3QT.js";
|
|
1
7
|
import {
|
|
2
8
|
AssistantMessage,
|
|
3
9
|
createProvider,
|
|
10
|
+
generateId,
|
|
4
11
|
isAssistantMessage,
|
|
5
12
|
isToolResultMessage,
|
|
6
13
|
isUserMessage
|
|
7
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-MF5ETY5O.js";
|
|
8
15
|
import {
|
|
9
16
|
parseSSEStream
|
|
10
|
-
} from "../chunk-
|
|
17
|
+
} from "../chunk-NWS5IKNR.js";
|
|
11
18
|
import {
|
|
12
19
|
resolveApiKey
|
|
13
|
-
} from "../chunk-
|
|
20
|
+
} from "../chunk-55X3W2MN.js";
|
|
14
21
|
import {
|
|
22
|
+
ErrorCode,
|
|
23
|
+
ModalityType,
|
|
15
24
|
UPPError,
|
|
16
25
|
doFetch,
|
|
17
26
|
doStreamFetch,
|
|
18
|
-
normalizeHttpError
|
|
19
|
-
|
|
27
|
+
normalizeHttpError,
|
|
28
|
+
toError
|
|
29
|
+
} from "../chunk-QNJO7DSD.js";
|
|
20
30
|
|
|
21
31
|
// src/providers/openrouter/transform.completions.ts
|
|
22
32
|
function transformRequest(request, modelId) {
|
|
@@ -53,11 +63,21 @@ function transformRequest(request, modelId) {
|
|
|
53
63
|
}
|
|
54
64
|
function transformMessages(messages, system) {
|
|
55
65
|
const result = [];
|
|
56
|
-
if (system) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
66
|
+
if (system !== void 0 && system !== null) {
|
|
67
|
+
const normalizedSystem = normalizeSystem(system);
|
|
68
|
+
if (typeof normalizedSystem === "string") {
|
|
69
|
+
if (normalizedSystem.length > 0) {
|
|
70
|
+
result.push({
|
|
71
|
+
role: "system",
|
|
72
|
+
content: normalizedSystem
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
} else if (normalizedSystem.length > 0) {
|
|
76
|
+
result.push({
|
|
77
|
+
role: "system",
|
|
78
|
+
content: normalizedSystem
|
|
79
|
+
});
|
|
80
|
+
}
|
|
61
81
|
}
|
|
62
82
|
for (const message of messages) {
|
|
63
83
|
if (isToolResultMessage(message)) {
|
|
@@ -72,6 +92,54 @@ function transformMessages(messages, system) {
|
|
|
72
92
|
}
|
|
73
93
|
return result;
|
|
74
94
|
}
|
|
95
|
+
function normalizeSystem(system) {
|
|
96
|
+
if (typeof system === "string") return system;
|
|
97
|
+
if (!Array.isArray(system)) {
|
|
98
|
+
throw new UPPError(
|
|
99
|
+
"System prompt must be a string or an array of text blocks",
|
|
100
|
+
ErrorCode.InvalidRequest,
|
|
101
|
+
"openrouter",
|
|
102
|
+
ModalityType.LLM
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const blocks = [];
|
|
106
|
+
for (const block of system) {
|
|
107
|
+
if (!block || typeof block !== "object") {
|
|
108
|
+
throw new UPPError(
|
|
109
|
+
'System prompt array must contain objects with type "text"',
|
|
110
|
+
ErrorCode.InvalidRequest,
|
|
111
|
+
"openrouter",
|
|
112
|
+
ModalityType.LLM
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
const candidate = block;
|
|
116
|
+
if (candidate.type !== "text" || typeof candidate.text !== "string") {
|
|
117
|
+
throw new UPPError(
|
|
118
|
+
'OpenRouter system blocks must be of type "text" with a string text field',
|
|
119
|
+
ErrorCode.InvalidRequest,
|
|
120
|
+
"openrouter",
|
|
121
|
+
ModalityType.LLM
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
if (candidate.cache_control !== void 0 && !isValidCacheControl(candidate.cache_control)) {
|
|
125
|
+
throw new UPPError(
|
|
126
|
+
"Invalid cache_control for OpenRouter system prompt",
|
|
127
|
+
ErrorCode.InvalidRequest,
|
|
128
|
+
"openrouter",
|
|
129
|
+
ModalityType.LLM
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
blocks.push(block);
|
|
133
|
+
}
|
|
134
|
+
return blocks;
|
|
135
|
+
}
|
|
136
|
+
function isValidCacheControl(value) {
|
|
137
|
+
if (!value || typeof value !== "object") return false;
|
|
138
|
+
const candidate = value;
|
|
139
|
+
if (candidate.type !== "ephemeral") return false;
|
|
140
|
+
if (candidate.ttl !== void 0 && candidate.ttl !== "1h") return false;
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
75
143
|
function filterValidContent(content) {
|
|
76
144
|
return content.filter((c) => c && typeof c.type === "string");
|
|
77
145
|
}
|
|
@@ -225,11 +293,12 @@ function transformResponse(data) {
|
|
|
225
293
|
});
|
|
226
294
|
}
|
|
227
295
|
}
|
|
296
|
+
const responseId = data.id || generateId();
|
|
228
297
|
const message = new AssistantMessage(
|
|
229
298
|
content,
|
|
230
299
|
toolCalls.length > 0 ? toolCalls : void 0,
|
|
231
300
|
{
|
|
232
|
-
id:
|
|
301
|
+
id: responseId,
|
|
233
302
|
metadata: {
|
|
234
303
|
openrouter: {
|
|
235
304
|
model: data.model,
|
|
@@ -300,7 +369,7 @@ function transformStreamEvent(chunk, state) {
|
|
|
300
369
|
const events = [];
|
|
301
370
|
if (chunk.id && !state.id) {
|
|
302
371
|
state.id = chunk.id;
|
|
303
|
-
events.push({ type:
|
|
372
|
+
events.push({ type: StreamEventType.MessageStart, index: 0, delta: {} });
|
|
304
373
|
}
|
|
305
374
|
if (chunk.model) {
|
|
306
375
|
state.model = chunk.model;
|
|
@@ -310,7 +379,7 @@ function transformStreamEvent(chunk, state) {
|
|
|
310
379
|
if (choice.delta.content) {
|
|
311
380
|
state.text += choice.delta.content;
|
|
312
381
|
events.push({
|
|
313
|
-
type:
|
|
382
|
+
type: StreamEventType.TextDelta,
|
|
314
383
|
index: 0,
|
|
315
384
|
delta: { text: choice.delta.content }
|
|
316
385
|
});
|
|
@@ -332,7 +401,7 @@ function transformStreamEvent(chunk, state) {
|
|
|
332
401
|
if (toolCallDelta.function?.arguments) {
|
|
333
402
|
toolCall.arguments += toolCallDelta.function.arguments;
|
|
334
403
|
events.push({
|
|
335
|
-
type:
|
|
404
|
+
type: StreamEventType.ToolCallDelta,
|
|
336
405
|
index,
|
|
337
406
|
delta: {
|
|
338
407
|
toolCallId: toolCall.id,
|
|
@@ -350,7 +419,7 @@ function transformStreamEvent(chunk, state) {
|
|
|
350
419
|
}
|
|
351
420
|
if (choice.finish_reason) {
|
|
352
421
|
state.finishReason = choice.finish_reason;
|
|
353
|
-
events.push({ type:
|
|
422
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
354
423
|
}
|
|
355
424
|
}
|
|
356
425
|
if (chunk.usage) {
|
|
@@ -391,11 +460,12 @@ function buildResponseFromState(state) {
|
|
|
391
460
|
arguments: args
|
|
392
461
|
});
|
|
393
462
|
}
|
|
463
|
+
const messageId = state.id || generateId();
|
|
394
464
|
const message = new AssistantMessage(
|
|
395
465
|
content,
|
|
396
466
|
toolCalls.length > 0 ? toolCalls : void 0,
|
|
397
467
|
{
|
|
398
|
-
id:
|
|
468
|
+
id: messageId,
|
|
399
469
|
metadata: {
|
|
400
470
|
openrouter: {
|
|
401
471
|
model: state.model,
|
|
@@ -455,9 +525,9 @@ function createCompletionsLLMHandler() {
|
|
|
455
525
|
if (!providerRef) {
|
|
456
526
|
throw new UPPError(
|
|
457
527
|
"Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
|
|
458
|
-
|
|
528
|
+
ErrorCode.InvalidRequest,
|
|
459
529
|
"openrouter",
|
|
460
|
-
|
|
530
|
+
ModalityType.LLM
|
|
461
531
|
);
|
|
462
532
|
}
|
|
463
533
|
const model = {
|
|
@@ -498,7 +568,7 @@ function createCompletionsLLMHandler() {
|
|
|
498
568
|
"openrouter",
|
|
499
569
|
"llm"
|
|
500
570
|
);
|
|
501
|
-
const data = await response
|
|
571
|
+
const data = await parseJsonResponse(response, "openrouter", "llm");
|
|
502
572
|
return transformResponse(data);
|
|
503
573
|
},
|
|
504
574
|
stream(request) {
|
|
@@ -523,7 +593,8 @@ function createCompletionsLLMHandler() {
|
|
|
523
593
|
body.stream_options = { include_usage: true };
|
|
524
594
|
const headers = {
|
|
525
595
|
"Content-Type": "application/json",
|
|
526
|
-
Authorization: `Bearer ${apiKey}
|
|
596
|
+
Authorization: `Bearer ${apiKey}`,
|
|
597
|
+
Accept: "text/event-stream"
|
|
527
598
|
};
|
|
528
599
|
if (request.config.headers) {
|
|
529
600
|
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
@@ -552,9 +623,9 @@ function createCompletionsLLMHandler() {
|
|
|
552
623
|
if (!response.body) {
|
|
553
624
|
const error = new UPPError(
|
|
554
625
|
"No response body for streaming request",
|
|
555
|
-
|
|
626
|
+
ErrorCode.ProviderError,
|
|
556
627
|
"openrouter",
|
|
557
|
-
|
|
628
|
+
ModalityType.LLM
|
|
558
629
|
);
|
|
559
630
|
responseReject(error);
|
|
560
631
|
throw error;
|
|
@@ -569,9 +640,9 @@ function createCompletionsLLMHandler() {
|
|
|
569
640
|
const errorData = chunk.error;
|
|
570
641
|
const error = new UPPError(
|
|
571
642
|
errorData.message ?? "Unknown error",
|
|
572
|
-
|
|
643
|
+
ErrorCode.ProviderError,
|
|
573
644
|
"openrouter",
|
|
574
|
-
|
|
645
|
+
ModalityType.LLM
|
|
575
646
|
);
|
|
576
647
|
responseReject(error);
|
|
577
648
|
throw error;
|
|
@@ -584,8 +655,9 @@ function createCompletionsLLMHandler() {
|
|
|
584
655
|
}
|
|
585
656
|
responseResolve(buildResponseFromState(state));
|
|
586
657
|
} catch (error) {
|
|
587
|
-
|
|
588
|
-
|
|
658
|
+
const err = toError(error);
|
|
659
|
+
responseReject(err);
|
|
660
|
+
throw err;
|
|
589
661
|
}
|
|
590
662
|
}
|
|
591
663
|
return {
|
|
@@ -636,12 +708,23 @@ function transformRequest2(request, modelId) {
|
|
|
636
708
|
}
|
|
637
709
|
function transformInputItems(messages, system) {
|
|
638
710
|
const result = [];
|
|
639
|
-
if (system) {
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
711
|
+
if (system !== void 0 && system !== null) {
|
|
712
|
+
const normalizedSystem = normalizeSystem2(system);
|
|
713
|
+
if (typeof normalizedSystem === "string") {
|
|
714
|
+
if (normalizedSystem.length > 0) {
|
|
715
|
+
result.push({
|
|
716
|
+
type: "message",
|
|
717
|
+
role: "system",
|
|
718
|
+
content: normalizedSystem
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
} else if (normalizedSystem.length > 0) {
|
|
722
|
+
result.push({
|
|
723
|
+
type: "message",
|
|
724
|
+
role: "system",
|
|
725
|
+
content: normalizedSystem
|
|
726
|
+
});
|
|
727
|
+
}
|
|
645
728
|
}
|
|
646
729
|
for (const message of messages) {
|
|
647
730
|
const items = transformMessage2(message);
|
|
@@ -655,6 +738,54 @@ function transformInputItems(messages, system) {
|
|
|
655
738
|
}
|
|
656
739
|
return result;
|
|
657
740
|
}
|
|
741
|
+
function normalizeSystem2(system) {
|
|
742
|
+
if (typeof system === "string") return system;
|
|
743
|
+
if (!Array.isArray(system)) {
|
|
744
|
+
throw new UPPError(
|
|
745
|
+
"System prompt must be a string or an array of text blocks",
|
|
746
|
+
ErrorCode.InvalidRequest,
|
|
747
|
+
"openrouter",
|
|
748
|
+
ModalityType.LLM
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
const blocks = [];
|
|
752
|
+
for (const block of system) {
|
|
753
|
+
if (!block || typeof block !== "object") {
|
|
754
|
+
throw new UPPError(
|
|
755
|
+
'System prompt array must contain objects with type "text"',
|
|
756
|
+
ErrorCode.InvalidRequest,
|
|
757
|
+
"openrouter",
|
|
758
|
+
ModalityType.LLM
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
const candidate = block;
|
|
762
|
+
if (candidate.type !== "text" || typeof candidate.text !== "string") {
|
|
763
|
+
throw new UPPError(
|
|
764
|
+
'OpenRouter system blocks must be of type "text" with a string text field',
|
|
765
|
+
ErrorCode.InvalidRequest,
|
|
766
|
+
"openrouter",
|
|
767
|
+
ModalityType.LLM
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
if (candidate.cache_control !== void 0 && !isValidCacheControl2(candidate.cache_control)) {
|
|
771
|
+
throw new UPPError(
|
|
772
|
+
"Invalid cache_control for OpenRouter system prompt",
|
|
773
|
+
ErrorCode.InvalidRequest,
|
|
774
|
+
"openrouter",
|
|
775
|
+
ModalityType.LLM
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
blocks.push(block);
|
|
779
|
+
}
|
|
780
|
+
return blocks;
|
|
781
|
+
}
|
|
782
|
+
function isValidCacheControl2(value) {
|
|
783
|
+
if (!value || typeof value !== "object") return false;
|
|
784
|
+
const candidate = value;
|
|
785
|
+
if (candidate.type !== "ephemeral") return false;
|
|
786
|
+
if (candidate.ttl !== void 0 && candidate.ttl !== "1h") return false;
|
|
787
|
+
return true;
|
|
788
|
+
}
|
|
658
789
|
function filterValidContent2(content) {
|
|
659
790
|
return content.filter((c) => c && typeof c.type === "string");
|
|
660
791
|
}
|
|
@@ -686,7 +817,7 @@ function transformMessage2(message) {
|
|
|
686
817
|
text: c.text,
|
|
687
818
|
annotations: []
|
|
688
819
|
}));
|
|
689
|
-
const messageId = message.id
|
|
820
|
+
const messageId = message.id || generateId();
|
|
690
821
|
if (contentParts.length > 0) {
|
|
691
822
|
items.push({
|
|
692
823
|
type: "message",
|
|
@@ -832,17 +963,18 @@ function transformResponse2(data) {
|
|
|
832
963
|
}
|
|
833
964
|
}
|
|
834
965
|
}
|
|
966
|
+
const responseId = data.id || generateId();
|
|
835
967
|
const message = new AssistantMessage(
|
|
836
968
|
content,
|
|
837
969
|
toolCalls.length > 0 ? toolCalls : void 0,
|
|
838
970
|
{
|
|
839
|
-
id:
|
|
971
|
+
id: responseId,
|
|
840
972
|
metadata: {
|
|
841
973
|
openrouter: {
|
|
842
974
|
model: data.model,
|
|
843
975
|
status: data.status,
|
|
844
976
|
// Store response_id for multi-turn tool calling
|
|
845
|
-
response_id:
|
|
977
|
+
response_id: responseId,
|
|
846
978
|
functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
|
|
847
979
|
}
|
|
848
980
|
}
|
|
@@ -893,7 +1025,7 @@ function transformStreamEvent2(event, state) {
|
|
|
893
1025
|
case "response.created":
|
|
894
1026
|
state.id = event.response.id;
|
|
895
1027
|
state.model = event.response.model;
|
|
896
|
-
events.push({ type:
|
|
1028
|
+
events.push({ type: StreamEventType.MessageStart, index: 0, delta: {} });
|
|
897
1029
|
break;
|
|
898
1030
|
case "response.in_progress":
|
|
899
1031
|
state.status = "in_progress";
|
|
@@ -922,11 +1054,11 @@ function transformStreamEvent2(event, state) {
|
|
|
922
1054
|
}
|
|
923
1055
|
}
|
|
924
1056
|
}
|
|
925
|
-
events.push({ type:
|
|
1057
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
926
1058
|
break;
|
|
927
1059
|
case "response.failed":
|
|
928
1060
|
state.status = "failed";
|
|
929
|
-
events.push({ type:
|
|
1061
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
930
1062
|
break;
|
|
931
1063
|
case "response.output_item.added":
|
|
932
1064
|
if (event.item.type === "function_call") {
|
|
@@ -943,7 +1075,7 @@ function transformStreamEvent2(event, state) {
|
|
|
943
1075
|
state.toolCalls.set(event.output_index, existing);
|
|
944
1076
|
}
|
|
945
1077
|
events.push({
|
|
946
|
-
type:
|
|
1078
|
+
type: StreamEventType.ContentBlockStart,
|
|
947
1079
|
index: event.output_index,
|
|
948
1080
|
delta: {}
|
|
949
1081
|
});
|
|
@@ -969,7 +1101,7 @@ function transformStreamEvent2(event, state) {
|
|
|
969
1101
|
if (!existingText && content.text) {
|
|
970
1102
|
state.textByIndex.set(event.output_index, content.text);
|
|
971
1103
|
events.push({
|
|
972
|
-
type:
|
|
1104
|
+
type: StreamEventType.TextDelta,
|
|
973
1105
|
index: event.output_index,
|
|
974
1106
|
delta: { text: content.text }
|
|
975
1107
|
});
|
|
@@ -983,7 +1115,7 @@ function transformStreamEvent2(event, state) {
|
|
|
983
1115
|
}
|
|
984
1116
|
}
|
|
985
1117
|
events.push({
|
|
986
|
-
type:
|
|
1118
|
+
type: StreamEventType.ContentBlockStop,
|
|
987
1119
|
index: event.output_index,
|
|
988
1120
|
delta: {}
|
|
989
1121
|
});
|
|
@@ -994,7 +1126,7 @@ function transformStreamEvent2(event, state) {
|
|
|
994
1126
|
const currentText = state.textByIndex.get(event.output_index) ?? "";
|
|
995
1127
|
state.textByIndex.set(event.output_index, currentText + textDelta);
|
|
996
1128
|
events.push({
|
|
997
|
-
type:
|
|
1129
|
+
type: StreamEventType.TextDelta,
|
|
998
1130
|
index: event.output_index,
|
|
999
1131
|
delta: { text: textDelta }
|
|
1000
1132
|
});
|
|
@@ -1011,7 +1143,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1011
1143
|
const currentRefusal = state.textByIndex.get(event.output_index) ?? "";
|
|
1012
1144
|
state.textByIndex.set(event.output_index, currentRefusal + event.delta);
|
|
1013
1145
|
events.push({
|
|
1014
|
-
type:
|
|
1146
|
+
type: StreamEventType.TextDelta,
|
|
1015
1147
|
index: event.output_index,
|
|
1016
1148
|
delta: { text: event.delta }
|
|
1017
1149
|
});
|
|
@@ -1035,7 +1167,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1035
1167
|
}
|
|
1036
1168
|
toolCall.arguments += event.delta;
|
|
1037
1169
|
events.push({
|
|
1038
|
-
type:
|
|
1170
|
+
type: StreamEventType.ToolCallDelta,
|
|
1039
1171
|
index: event.output_index,
|
|
1040
1172
|
delta: {
|
|
1041
1173
|
toolCallId: toolCall.callId ?? toolCall.itemId ?? "",
|
|
@@ -1063,7 +1195,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1063
1195
|
}
|
|
1064
1196
|
case "response.reasoning.delta":
|
|
1065
1197
|
events.push({
|
|
1066
|
-
type:
|
|
1198
|
+
type: StreamEventType.ReasoningDelta,
|
|
1067
1199
|
index: 0,
|
|
1068
1200
|
delta: { text: event.delta }
|
|
1069
1201
|
});
|
|
@@ -1125,17 +1257,18 @@ function buildResponseFromState2(state) {
|
|
|
1125
1257
|
});
|
|
1126
1258
|
}
|
|
1127
1259
|
}
|
|
1260
|
+
const responseId = state.id || generateId();
|
|
1128
1261
|
const message = new AssistantMessage(
|
|
1129
1262
|
content,
|
|
1130
1263
|
toolCalls.length > 0 ? toolCalls : void 0,
|
|
1131
1264
|
{
|
|
1132
|
-
id:
|
|
1265
|
+
id: responseId,
|
|
1133
1266
|
metadata: {
|
|
1134
1267
|
openrouter: {
|
|
1135
1268
|
model: state.model,
|
|
1136
1269
|
status: state.status,
|
|
1137
1270
|
// Store response_id for multi-turn tool calling
|
|
1138
|
-
response_id:
|
|
1271
|
+
response_id: responseId,
|
|
1139
1272
|
functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
|
|
1140
1273
|
}
|
|
1141
1274
|
}
|
|
@@ -1186,9 +1319,9 @@ function createResponsesLLMHandler() {
|
|
|
1186
1319
|
if (!providerRef) {
|
|
1187
1320
|
throw new UPPError(
|
|
1188
1321
|
"Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
|
|
1189
|
-
|
|
1322
|
+
ErrorCode.InvalidRequest,
|
|
1190
1323
|
"openrouter",
|
|
1191
|
-
|
|
1324
|
+
ModalityType.LLM
|
|
1192
1325
|
);
|
|
1193
1326
|
}
|
|
1194
1327
|
const model = {
|
|
@@ -1229,13 +1362,13 @@ function createResponsesLLMHandler() {
|
|
|
1229
1362
|
"openrouter",
|
|
1230
1363
|
"llm"
|
|
1231
1364
|
);
|
|
1232
|
-
const data = await response
|
|
1365
|
+
const data = await parseJsonResponse(response, "openrouter", "llm");
|
|
1233
1366
|
if (data.status === "failed" && data.error) {
|
|
1234
1367
|
throw new UPPError(
|
|
1235
1368
|
data.error.message,
|
|
1236
|
-
|
|
1369
|
+
ErrorCode.ProviderError,
|
|
1237
1370
|
"openrouter",
|
|
1238
|
-
|
|
1371
|
+
ModalityType.LLM
|
|
1239
1372
|
);
|
|
1240
1373
|
}
|
|
1241
1374
|
return transformResponse2(data);
|
|
@@ -1261,7 +1394,8 @@ function createResponsesLLMHandler() {
|
|
|
1261
1394
|
body.stream = true;
|
|
1262
1395
|
const headers = {
|
|
1263
1396
|
"Content-Type": "application/json",
|
|
1264
|
-
Authorization: `Bearer ${apiKey}
|
|
1397
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1398
|
+
Accept: "text/event-stream"
|
|
1265
1399
|
};
|
|
1266
1400
|
if (request.config.headers) {
|
|
1267
1401
|
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
@@ -1290,9 +1424,9 @@ function createResponsesLLMHandler() {
|
|
|
1290
1424
|
if (!response.body) {
|
|
1291
1425
|
const error = new UPPError(
|
|
1292
1426
|
"No response body for streaming request",
|
|
1293
|
-
|
|
1427
|
+
ErrorCode.ProviderError,
|
|
1294
1428
|
"openrouter",
|
|
1295
|
-
|
|
1429
|
+
ModalityType.LLM
|
|
1296
1430
|
);
|
|
1297
1431
|
responseReject(error);
|
|
1298
1432
|
throw error;
|
|
@@ -1307,9 +1441,9 @@ function createResponsesLLMHandler() {
|
|
|
1307
1441
|
const errorEvent = event;
|
|
1308
1442
|
const error = new UPPError(
|
|
1309
1443
|
errorEvent.error.message,
|
|
1310
|
-
|
|
1444
|
+
ErrorCode.ProviderError,
|
|
1311
1445
|
"openrouter",
|
|
1312
|
-
|
|
1446
|
+
ModalityType.LLM
|
|
1313
1447
|
);
|
|
1314
1448
|
responseReject(error);
|
|
1315
1449
|
throw error;
|
|
@@ -1322,8 +1456,9 @@ function createResponsesLLMHandler() {
|
|
|
1322
1456
|
}
|
|
1323
1457
|
responseResolve(buildResponseFromState2(state));
|
|
1324
1458
|
} catch (error) {
|
|
1325
|
-
|
|
1326
|
-
|
|
1459
|
+
const err = toError(error);
|
|
1460
|
+
responseReject(err);
|
|
1461
|
+
throw err;
|
|
1327
1462
|
}
|
|
1328
1463
|
}
|
|
1329
1464
|
return {
|
|
@@ -1364,9 +1499,9 @@ function createEmbeddingHandler() {
|
|
|
1364
1499
|
if (!providerRef) {
|
|
1365
1500
|
throw new UPPError(
|
|
1366
1501
|
"Provider reference not set. Handler must be used with createProvider().",
|
|
1367
|
-
|
|
1502
|
+
ErrorCode.InvalidRequest,
|
|
1368
1503
|
"openrouter",
|
|
1369
|
-
|
|
1504
|
+
ModalityType.Embedding
|
|
1370
1505
|
);
|
|
1371
1506
|
}
|
|
1372
1507
|
const model = {
|
|
@@ -1394,9 +1529,9 @@ function createEmbeddingHandler() {
|
|
|
1394
1529
|
}
|
|
1395
1530
|
throw new UPPError(
|
|
1396
1531
|
"OpenRouter embeddings only support text input",
|
|
1397
|
-
|
|
1532
|
+
ErrorCode.InvalidRequest,
|
|
1398
1533
|
"openrouter",
|
|
1399
|
-
|
|
1534
|
+
ModalityType.Embedding
|
|
1400
1535
|
);
|
|
1401
1536
|
});
|
|
1402
1537
|
const body = {
|
|
@@ -1432,7 +1567,7 @@ function createEmbeddingHandler() {
|
|
|
1432
1567
|
body: JSON.stringify(body),
|
|
1433
1568
|
signal: request.signal
|
|
1434
1569
|
}, request.config, "openrouter", "embedding");
|
|
1435
|
-
const data = await response
|
|
1570
|
+
const data = await parseJsonResponse(response, "openrouter", "embedding");
|
|
1436
1571
|
return {
|
|
1437
1572
|
embeddings: data.data.map((d) => ({
|
|
1438
1573
|
vector: d.embedding,
|