@providerprotocol/ai 0.0.20 → 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 (54) hide show
  1. package/dist/anthropic/index.d.ts +184 -14
  2. package/dist/anthropic/index.js +306 -107
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-P5IRTEM5.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-UMKWXGO3.js → chunk-M4BMM5IB.js} +86 -2
  8. package/dist/chunk-M4BMM5IB.js.map +1 -0
  9. package/dist/{chunk-SKY2JLA7.js → chunk-MKDLXV4O.js} +1 -1
  10. package/dist/chunk-MKDLXV4O.js.map +1 -0
  11. package/dist/{chunk-Z7RBRCRN.js → chunk-NWS5IKNR.js} +37 -11
  12. package/dist/chunk-NWS5IKNR.js.map +1 -0
  13. package/dist/{chunk-U3FZWV4U.js → chunk-RFWLEFAB.js} +100 -43
  14. package/dist/chunk-RFWLEFAB.js.map +1 -0
  15. package/dist/{chunk-U4JJC2YX.js → chunk-RS7C25LS.js} +36 -11
  16. package/dist/chunk-RS7C25LS.js.map +1 -0
  17. package/dist/google/index.d.ts +35 -24
  18. package/dist/google/index.js +273 -99
  19. package/dist/google/index.js.map +1 -1
  20. package/dist/http/index.d.ts +3 -3
  21. package/dist/http/index.js +4 -4
  22. package/dist/index.d.ts +103 -38
  23. package/dist/index.js +346 -153
  24. package/dist/index.js.map +1 -1
  25. package/dist/ollama/index.d.ts +14 -16
  26. package/dist/ollama/index.js +68 -16
  27. package/dist/ollama/index.js.map +1 -1
  28. package/dist/openai/index.d.ts +25 -133
  29. package/dist/openai/index.js +208 -122
  30. package/dist/openai/index.js.map +1 -1
  31. package/dist/openrouter/index.d.ts +28 -53
  32. package/dist/openrouter/index.js +179 -72
  33. package/dist/openrouter/index.js.map +1 -1
  34. package/dist/provider-DWEAzeM5.d.ts +1329 -0
  35. package/dist/proxy/index.d.ts +2 -3
  36. package/dist/proxy/index.js +174 -17
  37. package/dist/proxy/index.js.map +1 -1
  38. package/dist/{retry-DR7YRJDz.d.ts → retry-DmPmqZL6.d.ts} +12 -3
  39. package/dist/{stream-DRHy6q1a.d.ts → stream-DbkLOIbJ.d.ts} +15 -5
  40. package/dist/xai/index.d.ts +16 -88
  41. package/dist/xai/index.js +167 -86
  42. package/dist/xai/index.js.map +1 -1
  43. package/package.json +4 -1
  44. package/dist/chunk-MSR5P65T.js +0 -39
  45. package/dist/chunk-MSR5P65T.js.map +0 -1
  46. package/dist/chunk-SKY2JLA7.js.map +0 -1
  47. package/dist/chunk-U3FZWV4U.js.map +0 -1
  48. package/dist/chunk-U4JJC2YX.js.map +0 -1
  49. package/dist/chunk-UMKWXGO3.js.map +0 -1
  50. package/dist/chunk-Z7RBRCRN.js.map +0 -1
  51. package/dist/content-DEl3z_W2.d.ts +0 -276
  52. package/dist/image-Dhq-Yuq4.d.ts +0 -456
  53. package/dist/provider-BBMBZuGn.d.ts +0 -570
  54. /package/dist/{chunk-P5IRTEM5.js.map → chunk-7WYBJPJJ.js.map} +0 -0
@@ -1,29 +1,4 @@
1
- import { d as Provider, f as ModelReference, b as LLMHandler, c as EmbeddingHandler } from '../provider-BBMBZuGn.js';
2
-
3
- /**
4
- * @fileoverview OpenRouter Embeddings API Handler
5
- *
6
- * This module implements the embedding handler for OpenRouter's embeddings API.
7
- * OpenRouter provides access to multiple embedding providers through an OpenAI-compatible endpoint.
8
- *
9
- * @see {@link https://openrouter.ai/docs/api/reference/embeddings OpenRouter Embeddings API Reference}
10
- * @module providers/openrouter/embed
11
- */
12
-
13
- /**
14
- * OpenRouter embedding parameters.
15
- * Passed through unchanged to the API.
16
- */
17
- interface OpenRouterEmbedParams {
18
- /** Output dimensions (model-dependent) */
19
- dimensions?: number;
20
- /** Encoding format: 'float' or 'base64' */
21
- encoding_format?: 'float' | 'base64';
22
- /** A unique identifier representing your end-user */
23
- user?: string;
24
- /** Input type hint for some models */
25
- input_type?: string;
26
- }
1
+ import { g as Provider } from '../provider-DWEAzeM5.js';
27
2
 
28
3
  /**
29
4
  * OpenRouter-specific types for the Unified Provider Protocol.
@@ -255,10 +230,30 @@ type OpenRouterResponseFormat = {
255
230
  };
256
231
 
257
232
  /**
258
- * Union type for both Completions and Responses API parameter types.
259
- * Used internally to type the modalities handler.
233
+ * @fileoverview OpenRouter Embeddings API Handler
234
+ *
235
+ * This module implements the embedding handler for OpenRouter's embeddings API.
236
+ * OpenRouter provides access to multiple embedding providers through an OpenAI-compatible endpoint.
237
+ *
238
+ * @see {@link https://openrouter.ai/docs/api/reference/embeddings OpenRouter Embeddings API Reference}
239
+ * @module providers/openrouter/embed
240
+ */
241
+
242
+ /**
243
+ * OpenRouter embedding parameters.
244
+ * Passed through unchanged to the API.
260
245
  */
261
- type OpenRouterLLMParamsUnion = OpenRouterCompletionsParams | OpenRouterResponsesParams;
246
+ interface OpenRouterEmbedParams {
247
+ /** Output dimensions (model-dependent) */
248
+ dimensions?: number;
249
+ /** Encoding format: 'float' or 'base64' */
250
+ encoding_format?: 'float' | 'base64';
251
+ /** A unique identifier representing your end-user */
252
+ user?: string;
253
+ /** Input type hint for some models */
254
+ input_type?: string;
255
+ }
256
+
262
257
  /**
263
258
  * Configuration options for creating an OpenRouter model reference.
264
259
  *
@@ -297,30 +292,10 @@ interface OpenRouterProviderOptions {
297
292
  * const model = openrouter('anthropic/claude-3.5-sonnet', { api: 'completions' });
298
293
  * ```
299
294
  */
300
- interface OpenRouterProvider extends Provider<OpenRouterProviderOptions> {
301
- /**
302
- * Creates a model reference for the specified model ID.
303
- *
304
- * @param modelId - The OpenRouter model identifier in `provider/model` format
305
- * (e.g., 'openai/gpt-4o', 'anthropic/claude-3.5-sonnet',
306
- * 'meta-llama/llama-3.1-70b-instruct')
307
- * @param options - Optional configuration including API selection
308
- * @returns A model reference that can be passed to llm()
309
- */
310
- (modelId: string, options?: OpenRouterProviderOptions): ModelReference<OpenRouterProviderOptions>;
311
- /** Provider identifier. Always 'openrouter'. */
312
- readonly name: 'openrouter';
313
- /** Semantic version of this provider implementation. */
314
- readonly version: string;
315
- /**
316
- * Supported modalities for this provider.
317
- * OpenRouter supports LLM (text generation) and Embedding.
318
- */
319
- readonly modalities: {
320
- llm: LLMHandler<OpenRouterLLMParamsUnion>;
321
- embedding: EmbeddingHandler<OpenRouterEmbedParams>;
322
- };
323
- }
295
+ /**
296
+ * Type alias for the OpenRouter provider with its options.
297
+ */
298
+ type OpenRouterProvider = Provider<OpenRouterProviderOptions>;
324
299
  /**
325
300
  * OpenRouter provider singleton.
326
301
  *
@@ -1,21 +1,27 @@
1
+ import {
2
+ parseJsonResponse
3
+ } from "../chunk-I2VHCGQE.js";
1
4
  import {
2
5
  AssistantMessage,
6
+ createProvider,
7
+ generateId,
3
8
  isAssistantMessage,
4
9
  isToolResultMessage,
5
10
  isUserMessage
6
- } from "../chunk-UMKWXGO3.js";
11
+ } from "../chunk-M4BMM5IB.js";
7
12
  import {
8
13
  parseSSEStream
9
- } from "../chunk-Z7RBRCRN.js";
14
+ } from "../chunk-NWS5IKNR.js";
10
15
  import {
11
16
  resolveApiKey
12
- } from "../chunk-P5IRTEM5.js";
17
+ } from "../chunk-7WYBJPJJ.js";
13
18
  import {
14
19
  UPPError,
15
20
  doFetch,
16
21
  doStreamFetch,
17
- normalizeHttpError
18
- } from "../chunk-U3FZWV4U.js";
22
+ normalizeHttpError,
23
+ toError
24
+ } from "../chunk-RFWLEFAB.js";
19
25
 
20
26
  // src/providers/openrouter/transform.completions.ts
21
27
  function transformRequest(request, modelId) {
@@ -52,11 +58,21 @@ function transformRequest(request, modelId) {
52
58
  }
53
59
  function transformMessages(messages, system) {
54
60
  const result = [];
55
- if (system) {
56
- result.push({
57
- role: "system",
58
- content: system
59
- });
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
+ }
60
76
  }
61
77
  for (const message of messages) {
62
78
  if (isToolResultMessage(message)) {
@@ -71,6 +87,54 @@ function transformMessages(messages, system) {
71
87
  }
72
88
  return result;
73
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
+ }
74
138
  function filterValidContent(content) {
75
139
  return content.filter((c) => c && typeof c.type === "string");
76
140
  }
@@ -224,11 +288,12 @@ function transformResponse(data) {
224
288
  });
225
289
  }
226
290
  }
291
+ const responseId = data.id || generateId();
227
292
  const message = new AssistantMessage(
228
293
  content,
229
294
  toolCalls.length > 0 ? toolCalls : void 0,
230
295
  {
231
- id: data.id,
296
+ id: responseId,
232
297
  metadata: {
233
298
  openrouter: {
234
299
  model: data.model,
@@ -390,11 +455,12 @@ function buildResponseFromState(state) {
390
455
  arguments: args
391
456
  });
392
457
  }
458
+ const messageId = state.id || generateId();
393
459
  const message = new AssistantMessage(
394
460
  content,
395
461
  toolCalls.length > 0 ? toolCalls : void 0,
396
462
  {
397
- id: state.id,
463
+ id: messageId,
398
464
  metadata: {
399
465
  openrouter: {
400
466
  model: state.model,
@@ -497,7 +563,7 @@ function createCompletionsLLMHandler() {
497
563
  "openrouter",
498
564
  "llm"
499
565
  );
500
- const data = await response.json();
566
+ const data = await parseJsonResponse(response, "openrouter", "llm");
501
567
  return transformResponse(data);
502
568
  },
503
569
  stream(request) {
@@ -522,7 +588,8 @@ function createCompletionsLLMHandler() {
522
588
  body.stream_options = { include_usage: true };
523
589
  const headers = {
524
590
  "Content-Type": "application/json",
525
- Authorization: `Bearer ${apiKey}`
591
+ Authorization: `Bearer ${apiKey}`,
592
+ Accept: "text/event-stream"
526
593
  };
527
594
  if (request.config.headers) {
528
595
  for (const [key, value] of Object.entries(request.config.headers)) {
@@ -583,8 +650,9 @@ function createCompletionsLLMHandler() {
583
650
  }
584
651
  responseResolve(buildResponseFromState(state));
585
652
  } catch (error) {
586
- responseReject(error);
587
- throw error;
653
+ const err = toError(error);
654
+ responseReject(err);
655
+ throw err;
588
656
  }
589
657
  }
590
658
  return {
@@ -635,12 +703,23 @@ function transformRequest2(request, modelId) {
635
703
  }
636
704
  function transformInputItems(messages, system) {
637
705
  const result = [];
638
- if (system) {
639
- result.push({
640
- type: "message",
641
- role: "system",
642
- content: system
643
- });
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
+ }
644
723
  }
645
724
  for (const message of messages) {
646
725
  const items = transformMessage2(message);
@@ -654,6 +733,54 @@ function transformInputItems(messages, system) {
654
733
  }
655
734
  return result;
656
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
+ }
657
784
  function filterValidContent2(content) {
658
785
  return content.filter((c) => c && typeof c.type === "string");
659
786
  }
@@ -685,7 +812,7 @@ function transformMessage2(message) {
685
812
  text: c.text,
686
813
  annotations: []
687
814
  }));
688
- const messageId = message.id ?? `msg_${Date.now()}`;
815
+ const messageId = message.id || generateId();
689
816
  if (contentParts.length > 0) {
690
817
  items.push({
691
818
  type: "message",
@@ -831,17 +958,18 @@ function transformResponse2(data) {
831
958
  }
832
959
  }
833
960
  }
961
+ const responseId = data.id || generateId();
834
962
  const message = new AssistantMessage(
835
963
  content,
836
964
  toolCalls.length > 0 ? toolCalls : void 0,
837
965
  {
838
- id: data.id,
966
+ id: responseId,
839
967
  metadata: {
840
968
  openrouter: {
841
969
  model: data.model,
842
970
  status: data.status,
843
971
  // Store response_id for multi-turn tool calling
844
- response_id: data.id,
972
+ response_id: responseId,
845
973
  functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
846
974
  }
847
975
  }
@@ -1124,17 +1252,18 @@ function buildResponseFromState2(state) {
1124
1252
  });
1125
1253
  }
1126
1254
  }
1255
+ const responseId = state.id || generateId();
1127
1256
  const message = new AssistantMessage(
1128
1257
  content,
1129
1258
  toolCalls.length > 0 ? toolCalls : void 0,
1130
1259
  {
1131
- id: state.id,
1260
+ id: responseId,
1132
1261
  metadata: {
1133
1262
  openrouter: {
1134
1263
  model: state.model,
1135
1264
  status: state.status,
1136
1265
  // Store response_id for multi-turn tool calling
1137
- response_id: state.id,
1266
+ response_id: responseId,
1138
1267
  functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
1139
1268
  }
1140
1269
  }
@@ -1228,7 +1357,7 @@ function createResponsesLLMHandler() {
1228
1357
  "openrouter",
1229
1358
  "llm"
1230
1359
  );
1231
- const data = await response.json();
1360
+ const data = await parseJsonResponse(response, "openrouter", "llm");
1232
1361
  if (data.status === "failed" && data.error) {
1233
1362
  throw new UPPError(
1234
1363
  data.error.message,
@@ -1260,7 +1389,8 @@ function createResponsesLLMHandler() {
1260
1389
  body.stream = true;
1261
1390
  const headers = {
1262
1391
  "Content-Type": "application/json",
1263
- Authorization: `Bearer ${apiKey}`
1392
+ Authorization: `Bearer ${apiKey}`,
1393
+ Accept: "text/event-stream"
1264
1394
  };
1265
1395
  if (request.config.headers) {
1266
1396
  for (const [key, value] of Object.entries(request.config.headers)) {
@@ -1321,8 +1451,9 @@ function createResponsesLLMHandler() {
1321
1451
  }
1322
1452
  responseResolve(buildResponseFromState2(state));
1323
1453
  } catch (error) {
1324
- responseReject(error);
1325
- throw error;
1454
+ const err = toError(error);
1455
+ responseReject(err);
1456
+ throw err;
1326
1457
  }
1327
1458
  }
1328
1459
  return {
@@ -1431,7 +1562,7 @@ function createEmbeddingHandler() {
1431
1562
  body: JSON.stringify(body),
1432
1563
  signal: request.signal
1433
1564
  }, request.config, "openrouter", "embedding");
1434
- const data = await response.json();
1565
+ const data = await parseJsonResponse(response, "openrouter", "embedding");
1435
1566
  return {
1436
1567
  embeddings: data.data.map((d) => ({
1437
1568
  vector: d.embedding,
@@ -1448,46 +1579,22 @@ function createEmbeddingHandler() {
1448
1579
  }
1449
1580
 
1450
1581
  // src/providers/openrouter/index.ts
1451
- function createOpenRouterProvider() {
1452
- let currentApiMode = "completions";
1453
- const completionsHandler = createCompletionsLLMHandler();
1454
- const responsesHandler = createResponsesLLMHandler();
1455
- const embeddingHandler = createEmbeddingHandler();
1456
- const fn = function(modelId, options) {
1457
- const apiMode = options?.api ?? "completions";
1458
- currentApiMode = apiMode;
1459
- return { modelId, provider };
1460
- };
1461
- const modalities = {
1462
- get llm() {
1463
- return currentApiMode === "responses" ? responsesHandler : completionsHandler;
1464
- },
1465
- embedding: embeddingHandler
1466
- };
1467
- Object.defineProperties(fn, {
1468
- name: {
1469
- value: "openrouter",
1470
- writable: false,
1471
- configurable: true
1472
- },
1473
- version: {
1474
- value: "1.0.0",
1475
- writable: false,
1476
- configurable: true
1477
- },
1478
- modalities: {
1479
- value: modalities,
1480
- writable: false,
1481
- configurable: true
1482
- }
1483
- });
1484
- const provider = fn;
1485
- completionsHandler._setProvider?.(provider);
1486
- responsesHandler._setProvider?.(provider);
1487
- embeddingHandler._setProvider?.(provider);
1488
- return provider;
1489
- }
1490
- var openrouter = createOpenRouterProvider();
1582
+ var llmResolver = {
1583
+ handlers: {
1584
+ completions: createCompletionsLLMHandler(),
1585
+ responses: createResponsesLLMHandler()
1586
+ },
1587
+ defaultMode: "completions",
1588
+ getMode: (options) => options?.api ?? "completions"
1589
+ };
1590
+ var openrouter = createProvider({
1591
+ name: "openrouter",
1592
+ version: "1.0.0",
1593
+ handlers: {
1594
+ llm: llmResolver,
1595
+ embedding: createEmbeddingHandler()
1596
+ }
1597
+ });
1491
1598
  export {
1492
1599
  openrouter
1493
1600
  };