@providerprotocol/ai 0.0.22 → 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 +30 -25
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-7WYBJPJJ.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-RFWLEFAB.js → chunk-QNJO7DSD.js} +61 -16
- package/dist/chunk-QNJO7DSD.js.map +1 -0
- package/dist/{chunk-RS7C25LS.js → chunk-SBCATNHA.js} +9 -5
- package/dist/chunk-SBCATNHA.js.map +1 -0
- package/dist/{chunk-I2VHCGQE.js → chunk-Z6DKC37J.js} +6 -5
- package/dist/chunk-Z6DKC37J.js.map +1 -0
- package/dist/google/index.d.ts +3 -2
- package/dist/google/index.js +38 -33
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +3 -3
- package/dist/index.d.ts +8 -6
- package/dist/index.js +81 -121
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.d.ts +5 -2
- package/dist/ollama/index.js +34 -29
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +1 -1
- package/dist/openai/index.js +58 -53
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +1 -1
- package/dist/openrouter/index.js +57 -52
- package/dist/openrouter/index.js.map +1 -1
- package/dist/{provider-DWEAzeM5.d.ts → provider-DR1yins0.d.ts} +148 -52
- package/dist/proxy/index.d.ts +2 -2
- package/dist/proxy/index.js +11 -9
- package/dist/proxy/index.js.map +1 -1
- package/dist/{retry-DmPmqZL6.d.ts → retry-DJiqAslw.d.ts} +1 -1
- package/dist/{stream-DbkLOIbJ.d.ts → stream-BuTrqt_j.d.ts} +90 -38
- package/dist/xai/index.d.ts +1 -1
- package/dist/xai/index.js +71 -66
- package/dist/xai/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-7WYBJPJJ.js.map +0 -1
- package/dist/chunk-I2VHCGQE.js.map +0 -1
- package/dist/chunk-M4BMM5IB.js.map +0 -1
- package/dist/chunk-RFWLEFAB.js.map +0 -1
- package/dist/chunk-RS7C25LS.js.map +0 -1
package/dist/openrouter/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
parseJsonResponse
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-Z6DKC37J.js";
|
|
4
|
+
import {
|
|
5
|
+
StreamEventType
|
|
6
|
+
} from "../chunk-73IIE3QT.js";
|
|
4
7
|
import {
|
|
5
8
|
AssistantMessage,
|
|
6
9
|
createProvider,
|
|
@@ -8,20 +11,22 @@ import {
|
|
|
8
11
|
isAssistantMessage,
|
|
9
12
|
isToolResultMessage,
|
|
10
13
|
isUserMessage
|
|
11
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-MF5ETY5O.js";
|
|
12
15
|
import {
|
|
13
16
|
parseSSEStream
|
|
14
17
|
} from "../chunk-NWS5IKNR.js";
|
|
15
18
|
import {
|
|
16
19
|
resolveApiKey
|
|
17
|
-
} from "../chunk-
|
|
20
|
+
} from "../chunk-55X3W2MN.js";
|
|
18
21
|
import {
|
|
22
|
+
ErrorCode,
|
|
23
|
+
ModalityType,
|
|
19
24
|
UPPError,
|
|
20
25
|
doFetch,
|
|
21
26
|
doStreamFetch,
|
|
22
27
|
normalizeHttpError,
|
|
23
28
|
toError
|
|
24
|
-
} from "../chunk-
|
|
29
|
+
} from "../chunk-QNJO7DSD.js";
|
|
25
30
|
|
|
26
31
|
// src/providers/openrouter/transform.completions.ts
|
|
27
32
|
function transformRequest(request, modelId) {
|
|
@@ -92,9 +97,9 @@ function normalizeSystem(system) {
|
|
|
92
97
|
if (!Array.isArray(system)) {
|
|
93
98
|
throw new UPPError(
|
|
94
99
|
"System prompt must be a string or an array of text blocks",
|
|
95
|
-
|
|
100
|
+
ErrorCode.InvalidRequest,
|
|
96
101
|
"openrouter",
|
|
97
|
-
|
|
102
|
+
ModalityType.LLM
|
|
98
103
|
);
|
|
99
104
|
}
|
|
100
105
|
const blocks = [];
|
|
@@ -102,26 +107,26 @@ function normalizeSystem(system) {
|
|
|
102
107
|
if (!block || typeof block !== "object") {
|
|
103
108
|
throw new UPPError(
|
|
104
109
|
'System prompt array must contain objects with type "text"',
|
|
105
|
-
|
|
110
|
+
ErrorCode.InvalidRequest,
|
|
106
111
|
"openrouter",
|
|
107
|
-
|
|
112
|
+
ModalityType.LLM
|
|
108
113
|
);
|
|
109
114
|
}
|
|
110
115
|
const candidate = block;
|
|
111
116
|
if (candidate.type !== "text" || typeof candidate.text !== "string") {
|
|
112
117
|
throw new UPPError(
|
|
113
118
|
'OpenRouter system blocks must be of type "text" with a string text field',
|
|
114
|
-
|
|
119
|
+
ErrorCode.InvalidRequest,
|
|
115
120
|
"openrouter",
|
|
116
|
-
|
|
121
|
+
ModalityType.LLM
|
|
117
122
|
);
|
|
118
123
|
}
|
|
119
124
|
if (candidate.cache_control !== void 0 && !isValidCacheControl(candidate.cache_control)) {
|
|
120
125
|
throw new UPPError(
|
|
121
126
|
"Invalid cache_control for OpenRouter system prompt",
|
|
122
|
-
|
|
127
|
+
ErrorCode.InvalidRequest,
|
|
123
128
|
"openrouter",
|
|
124
|
-
|
|
129
|
+
ModalityType.LLM
|
|
125
130
|
);
|
|
126
131
|
}
|
|
127
132
|
blocks.push(block);
|
|
@@ -364,7 +369,7 @@ function transformStreamEvent(chunk, state) {
|
|
|
364
369
|
const events = [];
|
|
365
370
|
if (chunk.id && !state.id) {
|
|
366
371
|
state.id = chunk.id;
|
|
367
|
-
events.push({ type:
|
|
372
|
+
events.push({ type: StreamEventType.MessageStart, index: 0, delta: {} });
|
|
368
373
|
}
|
|
369
374
|
if (chunk.model) {
|
|
370
375
|
state.model = chunk.model;
|
|
@@ -374,7 +379,7 @@ function transformStreamEvent(chunk, state) {
|
|
|
374
379
|
if (choice.delta.content) {
|
|
375
380
|
state.text += choice.delta.content;
|
|
376
381
|
events.push({
|
|
377
|
-
type:
|
|
382
|
+
type: StreamEventType.TextDelta,
|
|
378
383
|
index: 0,
|
|
379
384
|
delta: { text: choice.delta.content }
|
|
380
385
|
});
|
|
@@ -396,7 +401,7 @@ function transformStreamEvent(chunk, state) {
|
|
|
396
401
|
if (toolCallDelta.function?.arguments) {
|
|
397
402
|
toolCall.arguments += toolCallDelta.function.arguments;
|
|
398
403
|
events.push({
|
|
399
|
-
type:
|
|
404
|
+
type: StreamEventType.ToolCallDelta,
|
|
400
405
|
index,
|
|
401
406
|
delta: {
|
|
402
407
|
toolCallId: toolCall.id,
|
|
@@ -414,7 +419,7 @@ function transformStreamEvent(chunk, state) {
|
|
|
414
419
|
}
|
|
415
420
|
if (choice.finish_reason) {
|
|
416
421
|
state.finishReason = choice.finish_reason;
|
|
417
|
-
events.push({ type:
|
|
422
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
418
423
|
}
|
|
419
424
|
}
|
|
420
425
|
if (chunk.usage) {
|
|
@@ -520,9 +525,9 @@ function createCompletionsLLMHandler() {
|
|
|
520
525
|
if (!providerRef) {
|
|
521
526
|
throw new UPPError(
|
|
522
527
|
"Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
|
|
523
|
-
|
|
528
|
+
ErrorCode.InvalidRequest,
|
|
524
529
|
"openrouter",
|
|
525
|
-
|
|
530
|
+
ModalityType.LLM
|
|
526
531
|
);
|
|
527
532
|
}
|
|
528
533
|
const model = {
|
|
@@ -618,9 +623,9 @@ function createCompletionsLLMHandler() {
|
|
|
618
623
|
if (!response.body) {
|
|
619
624
|
const error = new UPPError(
|
|
620
625
|
"No response body for streaming request",
|
|
621
|
-
|
|
626
|
+
ErrorCode.ProviderError,
|
|
622
627
|
"openrouter",
|
|
623
|
-
|
|
628
|
+
ModalityType.LLM
|
|
624
629
|
);
|
|
625
630
|
responseReject(error);
|
|
626
631
|
throw error;
|
|
@@ -635,9 +640,9 @@ function createCompletionsLLMHandler() {
|
|
|
635
640
|
const errorData = chunk.error;
|
|
636
641
|
const error = new UPPError(
|
|
637
642
|
errorData.message ?? "Unknown error",
|
|
638
|
-
|
|
643
|
+
ErrorCode.ProviderError,
|
|
639
644
|
"openrouter",
|
|
640
|
-
|
|
645
|
+
ModalityType.LLM
|
|
641
646
|
);
|
|
642
647
|
responseReject(error);
|
|
643
648
|
throw error;
|
|
@@ -738,9 +743,9 @@ function normalizeSystem2(system) {
|
|
|
738
743
|
if (!Array.isArray(system)) {
|
|
739
744
|
throw new UPPError(
|
|
740
745
|
"System prompt must be a string or an array of text blocks",
|
|
741
|
-
|
|
746
|
+
ErrorCode.InvalidRequest,
|
|
742
747
|
"openrouter",
|
|
743
|
-
|
|
748
|
+
ModalityType.LLM
|
|
744
749
|
);
|
|
745
750
|
}
|
|
746
751
|
const blocks = [];
|
|
@@ -748,26 +753,26 @@ function normalizeSystem2(system) {
|
|
|
748
753
|
if (!block || typeof block !== "object") {
|
|
749
754
|
throw new UPPError(
|
|
750
755
|
'System prompt array must contain objects with type "text"',
|
|
751
|
-
|
|
756
|
+
ErrorCode.InvalidRequest,
|
|
752
757
|
"openrouter",
|
|
753
|
-
|
|
758
|
+
ModalityType.LLM
|
|
754
759
|
);
|
|
755
760
|
}
|
|
756
761
|
const candidate = block;
|
|
757
762
|
if (candidate.type !== "text" || typeof candidate.text !== "string") {
|
|
758
763
|
throw new UPPError(
|
|
759
764
|
'OpenRouter system blocks must be of type "text" with a string text field',
|
|
760
|
-
|
|
765
|
+
ErrorCode.InvalidRequest,
|
|
761
766
|
"openrouter",
|
|
762
|
-
|
|
767
|
+
ModalityType.LLM
|
|
763
768
|
);
|
|
764
769
|
}
|
|
765
770
|
if (candidate.cache_control !== void 0 && !isValidCacheControl2(candidate.cache_control)) {
|
|
766
771
|
throw new UPPError(
|
|
767
772
|
"Invalid cache_control for OpenRouter system prompt",
|
|
768
|
-
|
|
773
|
+
ErrorCode.InvalidRequest,
|
|
769
774
|
"openrouter",
|
|
770
|
-
|
|
775
|
+
ModalityType.LLM
|
|
771
776
|
);
|
|
772
777
|
}
|
|
773
778
|
blocks.push(block);
|
|
@@ -1020,7 +1025,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1020
1025
|
case "response.created":
|
|
1021
1026
|
state.id = event.response.id;
|
|
1022
1027
|
state.model = event.response.model;
|
|
1023
|
-
events.push({ type:
|
|
1028
|
+
events.push({ type: StreamEventType.MessageStart, index: 0, delta: {} });
|
|
1024
1029
|
break;
|
|
1025
1030
|
case "response.in_progress":
|
|
1026
1031
|
state.status = "in_progress";
|
|
@@ -1049,11 +1054,11 @@ function transformStreamEvent2(event, state) {
|
|
|
1049
1054
|
}
|
|
1050
1055
|
}
|
|
1051
1056
|
}
|
|
1052
|
-
events.push({ type:
|
|
1057
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
1053
1058
|
break;
|
|
1054
1059
|
case "response.failed":
|
|
1055
1060
|
state.status = "failed";
|
|
1056
|
-
events.push({ type:
|
|
1061
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
1057
1062
|
break;
|
|
1058
1063
|
case "response.output_item.added":
|
|
1059
1064
|
if (event.item.type === "function_call") {
|
|
@@ -1070,7 +1075,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1070
1075
|
state.toolCalls.set(event.output_index, existing);
|
|
1071
1076
|
}
|
|
1072
1077
|
events.push({
|
|
1073
|
-
type:
|
|
1078
|
+
type: StreamEventType.ContentBlockStart,
|
|
1074
1079
|
index: event.output_index,
|
|
1075
1080
|
delta: {}
|
|
1076
1081
|
});
|
|
@@ -1096,7 +1101,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1096
1101
|
if (!existingText && content.text) {
|
|
1097
1102
|
state.textByIndex.set(event.output_index, content.text);
|
|
1098
1103
|
events.push({
|
|
1099
|
-
type:
|
|
1104
|
+
type: StreamEventType.TextDelta,
|
|
1100
1105
|
index: event.output_index,
|
|
1101
1106
|
delta: { text: content.text }
|
|
1102
1107
|
});
|
|
@@ -1110,7 +1115,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1110
1115
|
}
|
|
1111
1116
|
}
|
|
1112
1117
|
events.push({
|
|
1113
|
-
type:
|
|
1118
|
+
type: StreamEventType.ContentBlockStop,
|
|
1114
1119
|
index: event.output_index,
|
|
1115
1120
|
delta: {}
|
|
1116
1121
|
});
|
|
@@ -1121,7 +1126,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1121
1126
|
const currentText = state.textByIndex.get(event.output_index) ?? "";
|
|
1122
1127
|
state.textByIndex.set(event.output_index, currentText + textDelta);
|
|
1123
1128
|
events.push({
|
|
1124
|
-
type:
|
|
1129
|
+
type: StreamEventType.TextDelta,
|
|
1125
1130
|
index: event.output_index,
|
|
1126
1131
|
delta: { text: textDelta }
|
|
1127
1132
|
});
|
|
@@ -1138,7 +1143,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1138
1143
|
const currentRefusal = state.textByIndex.get(event.output_index) ?? "";
|
|
1139
1144
|
state.textByIndex.set(event.output_index, currentRefusal + event.delta);
|
|
1140
1145
|
events.push({
|
|
1141
|
-
type:
|
|
1146
|
+
type: StreamEventType.TextDelta,
|
|
1142
1147
|
index: event.output_index,
|
|
1143
1148
|
delta: { text: event.delta }
|
|
1144
1149
|
});
|
|
@@ -1162,7 +1167,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1162
1167
|
}
|
|
1163
1168
|
toolCall.arguments += event.delta;
|
|
1164
1169
|
events.push({
|
|
1165
|
-
type:
|
|
1170
|
+
type: StreamEventType.ToolCallDelta,
|
|
1166
1171
|
index: event.output_index,
|
|
1167
1172
|
delta: {
|
|
1168
1173
|
toolCallId: toolCall.callId ?? toolCall.itemId ?? "",
|
|
@@ -1190,7 +1195,7 @@ function transformStreamEvent2(event, state) {
|
|
|
1190
1195
|
}
|
|
1191
1196
|
case "response.reasoning.delta":
|
|
1192
1197
|
events.push({
|
|
1193
|
-
type:
|
|
1198
|
+
type: StreamEventType.ReasoningDelta,
|
|
1194
1199
|
index: 0,
|
|
1195
1200
|
delta: { text: event.delta }
|
|
1196
1201
|
});
|
|
@@ -1314,9 +1319,9 @@ function createResponsesLLMHandler() {
|
|
|
1314
1319
|
if (!providerRef) {
|
|
1315
1320
|
throw new UPPError(
|
|
1316
1321
|
"Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
|
|
1317
|
-
|
|
1322
|
+
ErrorCode.InvalidRequest,
|
|
1318
1323
|
"openrouter",
|
|
1319
|
-
|
|
1324
|
+
ModalityType.LLM
|
|
1320
1325
|
);
|
|
1321
1326
|
}
|
|
1322
1327
|
const model = {
|
|
@@ -1361,9 +1366,9 @@ function createResponsesLLMHandler() {
|
|
|
1361
1366
|
if (data.status === "failed" && data.error) {
|
|
1362
1367
|
throw new UPPError(
|
|
1363
1368
|
data.error.message,
|
|
1364
|
-
|
|
1369
|
+
ErrorCode.ProviderError,
|
|
1365
1370
|
"openrouter",
|
|
1366
|
-
|
|
1371
|
+
ModalityType.LLM
|
|
1367
1372
|
);
|
|
1368
1373
|
}
|
|
1369
1374
|
return transformResponse2(data);
|
|
@@ -1419,9 +1424,9 @@ function createResponsesLLMHandler() {
|
|
|
1419
1424
|
if (!response.body) {
|
|
1420
1425
|
const error = new UPPError(
|
|
1421
1426
|
"No response body for streaming request",
|
|
1422
|
-
|
|
1427
|
+
ErrorCode.ProviderError,
|
|
1423
1428
|
"openrouter",
|
|
1424
|
-
|
|
1429
|
+
ModalityType.LLM
|
|
1425
1430
|
);
|
|
1426
1431
|
responseReject(error);
|
|
1427
1432
|
throw error;
|
|
@@ -1436,9 +1441,9 @@ function createResponsesLLMHandler() {
|
|
|
1436
1441
|
const errorEvent = event;
|
|
1437
1442
|
const error = new UPPError(
|
|
1438
1443
|
errorEvent.error.message,
|
|
1439
|
-
|
|
1444
|
+
ErrorCode.ProviderError,
|
|
1440
1445
|
"openrouter",
|
|
1441
|
-
|
|
1446
|
+
ModalityType.LLM
|
|
1442
1447
|
);
|
|
1443
1448
|
responseReject(error);
|
|
1444
1449
|
throw error;
|
|
@@ -1494,9 +1499,9 @@ function createEmbeddingHandler() {
|
|
|
1494
1499
|
if (!providerRef) {
|
|
1495
1500
|
throw new UPPError(
|
|
1496
1501
|
"Provider reference not set. Handler must be used with createProvider().",
|
|
1497
|
-
|
|
1502
|
+
ErrorCode.InvalidRequest,
|
|
1498
1503
|
"openrouter",
|
|
1499
|
-
|
|
1504
|
+
ModalityType.Embedding
|
|
1500
1505
|
);
|
|
1501
1506
|
}
|
|
1502
1507
|
const model = {
|
|
@@ -1524,9 +1529,9 @@ function createEmbeddingHandler() {
|
|
|
1524
1529
|
}
|
|
1525
1530
|
throw new UPPError(
|
|
1526
1531
|
"OpenRouter embeddings only support text input",
|
|
1527
|
-
|
|
1532
|
+
ErrorCode.InvalidRequest,
|
|
1528
1533
|
"openrouter",
|
|
1529
|
-
|
|
1534
|
+
ModalityType.Embedding
|
|
1530
1535
|
);
|
|
1531
1536
|
});
|
|
1532
1537
|
const body = {
|