@providerprotocol/ai 0.0.21 → 0.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/anthropic/index.d.ts +1 -1
  2. package/dist/anthropic/index.js +100 -29
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-Y3GBJNA2.js → chunk-7WYBJPJJ.js} +2 -2
  5. package/dist/chunk-I2VHCGQE.js +49 -0
  6. package/dist/chunk-I2VHCGQE.js.map +1 -0
  7. package/dist/{chunk-SKY2JLA7.js → chunk-MKDLXV4O.js} +1 -1
  8. package/dist/chunk-MKDLXV4O.js.map +1 -0
  9. package/dist/{chunk-Z7RBRCRN.js → chunk-NWS5IKNR.js} +37 -11
  10. package/dist/chunk-NWS5IKNR.js.map +1 -0
  11. package/dist/{chunk-EDENPF3E.js → chunk-RFWLEFAB.js} +96 -42
  12. package/dist/chunk-RFWLEFAB.js.map +1 -0
  13. package/dist/{chunk-Z4ILICF5.js → chunk-RS7C25LS.js} +35 -10
  14. package/dist/chunk-RS7C25LS.js.map +1 -0
  15. package/dist/google/index.d.ts +20 -6
  16. package/dist/google/index.js +261 -65
  17. package/dist/google/index.js.map +1 -1
  18. package/dist/http/index.d.ts +3 -3
  19. package/dist/http/index.js +4 -4
  20. package/dist/index.d.ts +7 -5
  21. package/dist/index.js +286 -119
  22. package/dist/index.js.map +1 -1
  23. package/dist/ollama/index.d.ts +1 -1
  24. package/dist/ollama/index.js +66 -12
  25. package/dist/ollama/index.js.map +1 -1
  26. package/dist/openai/index.d.ts +1 -1
  27. package/dist/openai/index.js +183 -43
  28. package/dist/openai/index.js.map +1 -1
  29. package/dist/openrouter/index.d.ts +1 -1
  30. package/dist/openrouter/index.js +161 -31
  31. package/dist/openrouter/index.js.map +1 -1
  32. package/dist/{provider-DGQHYE6I.d.ts → provider-DWEAzeM5.d.ts} +11 -1
  33. package/dist/proxy/index.d.ts +2 -2
  34. package/dist/proxy/index.js +171 -12
  35. package/dist/proxy/index.js.map +1 -1
  36. package/dist/{retry-Pcs3hnbu.d.ts → retry-DmPmqZL6.d.ts} +11 -2
  37. package/dist/{stream-Di9acos2.d.ts → stream-DbkLOIbJ.d.ts} +15 -5
  38. package/dist/xai/index.d.ts +1 -1
  39. package/dist/xai/index.js +139 -30
  40. package/dist/xai/index.js.map +1 -1
  41. package/package.json +1 -1
  42. package/dist/chunk-EDENPF3E.js.map +0 -1
  43. package/dist/chunk-SKY2JLA7.js.map +0 -1
  44. package/dist/chunk-Z4ILICF5.js.map +0 -1
  45. package/dist/chunk-Z7RBRCRN.js.map +0 -1
  46. /package/dist/{chunk-Y3GBJNA2.js.map → chunk-7WYBJPJJ.js.map} +0 -0
@@ -1,4 +1,4 @@
1
- import { g as Provider } from '../provider-DGQHYE6I.js';
1
+ import { g as Provider } from '../provider-DWEAzeM5.js';
2
2
 
3
3
  /**
4
4
  * OpenRouter-specific types for the Unified Provider Protocol.
@@ -1,22 +1,27 @@
1
+ import {
2
+ parseJsonResponse
3
+ } from "../chunk-I2VHCGQE.js";
1
4
  import {
2
5
  AssistantMessage,
3
6
  createProvider,
7
+ generateId,
4
8
  isAssistantMessage,
5
9
  isToolResultMessage,
6
10
  isUserMessage
7
11
  } from "../chunk-M4BMM5IB.js";
8
12
  import {
9
13
  parseSSEStream
10
- } from "../chunk-Z7RBRCRN.js";
14
+ } from "../chunk-NWS5IKNR.js";
11
15
  import {
12
16
  resolveApiKey
13
- } from "../chunk-Y3GBJNA2.js";
17
+ } from "../chunk-7WYBJPJJ.js";
14
18
  import {
15
19
  UPPError,
16
20
  doFetch,
17
21
  doStreamFetch,
18
- normalizeHttpError
19
- } from "../chunk-EDENPF3E.js";
22
+ normalizeHttpError,
23
+ toError
24
+ } from "../chunk-RFWLEFAB.js";
20
25
 
21
26
  // src/providers/openrouter/transform.completions.ts
22
27
  function transformRequest(request, modelId) {
@@ -53,11 +58,21 @@ function transformRequest(request, modelId) {
53
58
  }
54
59
  function transformMessages(messages, system) {
55
60
  const result = [];
56
- if (system) {
57
- result.push({
58
- role: "system",
59
- content: system
60
- });
61
+ if (system !== void 0 && system !== null) {
62
+ const normalizedSystem = normalizeSystem(system);
63
+ if (typeof normalizedSystem === "string") {
64
+ if (normalizedSystem.length > 0) {
65
+ result.push({
66
+ role: "system",
67
+ content: normalizedSystem
68
+ });
69
+ }
70
+ } else if (normalizedSystem.length > 0) {
71
+ result.push({
72
+ role: "system",
73
+ content: normalizedSystem
74
+ });
75
+ }
61
76
  }
62
77
  for (const message of messages) {
63
78
  if (isToolResultMessage(message)) {
@@ -72,6 +87,54 @@ function transformMessages(messages, system) {
72
87
  }
73
88
  return result;
74
89
  }
90
+ function normalizeSystem(system) {
91
+ if (typeof system === "string") return system;
92
+ if (!Array.isArray(system)) {
93
+ throw new UPPError(
94
+ "System prompt must be a string or an array of text blocks",
95
+ "INVALID_REQUEST",
96
+ "openrouter",
97
+ "llm"
98
+ );
99
+ }
100
+ const blocks = [];
101
+ for (const block of system) {
102
+ if (!block || typeof block !== "object") {
103
+ throw new UPPError(
104
+ 'System prompt array must contain objects with type "text"',
105
+ "INVALID_REQUEST",
106
+ "openrouter",
107
+ "llm"
108
+ );
109
+ }
110
+ const candidate = block;
111
+ if (candidate.type !== "text" || typeof candidate.text !== "string") {
112
+ throw new UPPError(
113
+ 'OpenRouter system blocks must be of type "text" with a string text field',
114
+ "INVALID_REQUEST",
115
+ "openrouter",
116
+ "llm"
117
+ );
118
+ }
119
+ if (candidate.cache_control !== void 0 && !isValidCacheControl(candidate.cache_control)) {
120
+ throw new UPPError(
121
+ "Invalid cache_control for OpenRouter system prompt",
122
+ "INVALID_REQUEST",
123
+ "openrouter",
124
+ "llm"
125
+ );
126
+ }
127
+ blocks.push(block);
128
+ }
129
+ return blocks;
130
+ }
131
+ function isValidCacheControl(value) {
132
+ if (!value || typeof value !== "object") return false;
133
+ const candidate = value;
134
+ if (candidate.type !== "ephemeral") return false;
135
+ if (candidate.ttl !== void 0 && candidate.ttl !== "1h") return false;
136
+ return true;
137
+ }
75
138
  function filterValidContent(content) {
76
139
  return content.filter((c) => c && typeof c.type === "string");
77
140
  }
@@ -225,11 +288,12 @@ function transformResponse(data) {
225
288
  });
226
289
  }
227
290
  }
291
+ const responseId = data.id || generateId();
228
292
  const message = new AssistantMessage(
229
293
  content,
230
294
  toolCalls.length > 0 ? toolCalls : void 0,
231
295
  {
232
- id: data.id,
296
+ id: responseId,
233
297
  metadata: {
234
298
  openrouter: {
235
299
  model: data.model,
@@ -391,11 +455,12 @@ function buildResponseFromState(state) {
391
455
  arguments: args
392
456
  });
393
457
  }
458
+ const messageId = state.id || generateId();
394
459
  const message = new AssistantMessage(
395
460
  content,
396
461
  toolCalls.length > 0 ? toolCalls : void 0,
397
462
  {
398
- id: state.id,
463
+ id: messageId,
399
464
  metadata: {
400
465
  openrouter: {
401
466
  model: state.model,
@@ -498,7 +563,7 @@ function createCompletionsLLMHandler() {
498
563
  "openrouter",
499
564
  "llm"
500
565
  );
501
- const data = await response.json();
566
+ const data = await parseJsonResponse(response, "openrouter", "llm");
502
567
  return transformResponse(data);
503
568
  },
504
569
  stream(request) {
@@ -523,7 +588,8 @@ function createCompletionsLLMHandler() {
523
588
  body.stream_options = { include_usage: true };
524
589
  const headers = {
525
590
  "Content-Type": "application/json",
526
- Authorization: `Bearer ${apiKey}`
591
+ Authorization: `Bearer ${apiKey}`,
592
+ Accept: "text/event-stream"
527
593
  };
528
594
  if (request.config.headers) {
529
595
  for (const [key, value] of Object.entries(request.config.headers)) {
@@ -584,8 +650,9 @@ function createCompletionsLLMHandler() {
584
650
  }
585
651
  responseResolve(buildResponseFromState(state));
586
652
  } catch (error) {
587
- responseReject(error);
588
- throw error;
653
+ const err = toError(error);
654
+ responseReject(err);
655
+ throw err;
589
656
  }
590
657
  }
591
658
  return {
@@ -636,12 +703,23 @@ function transformRequest2(request, modelId) {
636
703
  }
637
704
  function transformInputItems(messages, system) {
638
705
  const result = [];
639
- if (system) {
640
- result.push({
641
- type: "message",
642
- role: "system",
643
- content: system
644
- });
706
+ if (system !== void 0 && system !== null) {
707
+ const normalizedSystem = normalizeSystem2(system);
708
+ if (typeof normalizedSystem === "string") {
709
+ if (normalizedSystem.length > 0) {
710
+ result.push({
711
+ type: "message",
712
+ role: "system",
713
+ content: normalizedSystem
714
+ });
715
+ }
716
+ } else if (normalizedSystem.length > 0) {
717
+ result.push({
718
+ type: "message",
719
+ role: "system",
720
+ content: normalizedSystem
721
+ });
722
+ }
645
723
  }
646
724
  for (const message of messages) {
647
725
  const items = transformMessage2(message);
@@ -655,6 +733,54 @@ function transformInputItems(messages, system) {
655
733
  }
656
734
  return result;
657
735
  }
736
+ function normalizeSystem2(system) {
737
+ if (typeof system === "string") return system;
738
+ if (!Array.isArray(system)) {
739
+ throw new UPPError(
740
+ "System prompt must be a string or an array of text blocks",
741
+ "INVALID_REQUEST",
742
+ "openrouter",
743
+ "llm"
744
+ );
745
+ }
746
+ const blocks = [];
747
+ for (const block of system) {
748
+ if (!block || typeof block !== "object") {
749
+ throw new UPPError(
750
+ 'System prompt array must contain objects with type "text"',
751
+ "INVALID_REQUEST",
752
+ "openrouter",
753
+ "llm"
754
+ );
755
+ }
756
+ const candidate = block;
757
+ if (candidate.type !== "text" || typeof candidate.text !== "string") {
758
+ throw new UPPError(
759
+ 'OpenRouter system blocks must be of type "text" with a string text field',
760
+ "INVALID_REQUEST",
761
+ "openrouter",
762
+ "llm"
763
+ );
764
+ }
765
+ if (candidate.cache_control !== void 0 && !isValidCacheControl2(candidate.cache_control)) {
766
+ throw new UPPError(
767
+ "Invalid cache_control for OpenRouter system prompt",
768
+ "INVALID_REQUEST",
769
+ "openrouter",
770
+ "llm"
771
+ );
772
+ }
773
+ blocks.push(block);
774
+ }
775
+ return blocks;
776
+ }
777
+ function isValidCacheControl2(value) {
778
+ if (!value || typeof value !== "object") return false;
779
+ const candidate = value;
780
+ if (candidate.type !== "ephemeral") return false;
781
+ if (candidate.ttl !== void 0 && candidate.ttl !== "1h") return false;
782
+ return true;
783
+ }
658
784
  function filterValidContent2(content) {
659
785
  return content.filter((c) => c && typeof c.type === "string");
660
786
  }
@@ -686,7 +812,7 @@ function transformMessage2(message) {
686
812
  text: c.text,
687
813
  annotations: []
688
814
  }));
689
- const messageId = message.id ?? `msg_${Date.now()}`;
815
+ const messageId = message.id || generateId();
690
816
  if (contentParts.length > 0) {
691
817
  items.push({
692
818
  type: "message",
@@ -832,17 +958,18 @@ function transformResponse2(data) {
832
958
  }
833
959
  }
834
960
  }
961
+ const responseId = data.id || generateId();
835
962
  const message = new AssistantMessage(
836
963
  content,
837
964
  toolCalls.length > 0 ? toolCalls : void 0,
838
965
  {
839
- id: data.id,
966
+ id: responseId,
840
967
  metadata: {
841
968
  openrouter: {
842
969
  model: data.model,
843
970
  status: data.status,
844
971
  // Store response_id for multi-turn tool calling
845
- response_id: data.id,
972
+ response_id: responseId,
846
973
  functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
847
974
  }
848
975
  }
@@ -1125,17 +1252,18 @@ function buildResponseFromState2(state) {
1125
1252
  });
1126
1253
  }
1127
1254
  }
1255
+ const responseId = state.id || generateId();
1128
1256
  const message = new AssistantMessage(
1129
1257
  content,
1130
1258
  toolCalls.length > 0 ? toolCalls : void 0,
1131
1259
  {
1132
- id: state.id,
1260
+ id: responseId,
1133
1261
  metadata: {
1134
1262
  openrouter: {
1135
1263
  model: state.model,
1136
1264
  status: state.status,
1137
1265
  // Store response_id for multi-turn tool calling
1138
- response_id: state.id,
1266
+ response_id: responseId,
1139
1267
  functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
1140
1268
  }
1141
1269
  }
@@ -1229,7 +1357,7 @@ function createResponsesLLMHandler() {
1229
1357
  "openrouter",
1230
1358
  "llm"
1231
1359
  );
1232
- const data = await response.json();
1360
+ const data = await parseJsonResponse(response, "openrouter", "llm");
1233
1361
  if (data.status === "failed" && data.error) {
1234
1362
  throw new UPPError(
1235
1363
  data.error.message,
@@ -1261,7 +1389,8 @@ function createResponsesLLMHandler() {
1261
1389
  body.stream = true;
1262
1390
  const headers = {
1263
1391
  "Content-Type": "application/json",
1264
- Authorization: `Bearer ${apiKey}`
1392
+ Authorization: `Bearer ${apiKey}`,
1393
+ Accept: "text/event-stream"
1265
1394
  };
1266
1395
  if (request.config.headers) {
1267
1396
  for (const [key, value] of Object.entries(request.config.headers)) {
@@ -1322,8 +1451,9 @@ function createResponsesLLMHandler() {
1322
1451
  }
1323
1452
  responseResolve(buildResponseFromState2(state));
1324
1453
  } catch (error) {
1325
- responseReject(error);
1326
- throw error;
1454
+ const err = toError(error);
1455
+ responseReject(err);
1456
+ throw err;
1327
1457
  }
1328
1458
  }
1329
1459
  return {
@@ -1432,7 +1562,7 @@ function createEmbeddingHandler() {
1432
1562
  body: JSON.stringify(body),
1433
1563
  signal: request.signal
1434
1564
  }, request.config, "openrouter", "embedding");
1435
- const data = await response.json();
1565
+ const data = await parseJsonResponse(response, "openrouter", "embedding");
1436
1566
  return {
1437
1567
  embeddings: data.data.map((d) => ({
1438
1568
  vector: d.embedding,