@providerprotocol/ai 0.0.11 → 0.0.13

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 (104) hide show
  1. package/dist/anthropic/index.d.ts +51 -15
  2. package/dist/anthropic/index.js +54 -19
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-SUNYWHTH.js → chunk-MOU4U3PO.js} +55 -3
  5. package/dist/chunk-MOU4U3PO.js.map +1 -0
  6. package/dist/{chunk-Y6Q7JCNP.js → chunk-MSR5P65T.js} +1 -1
  7. package/dist/chunk-MSR5P65T.js.map +1 -0
  8. package/dist/{chunk-W4BB4BG2.js → chunk-SVYROCLD.js} +31 -11
  9. package/dist/chunk-SVYROCLD.js.map +1 -0
  10. package/dist/chunk-U4JJC2YX.js +234 -0
  11. package/dist/chunk-U4JJC2YX.js.map +1 -0
  12. package/dist/{chunk-X5G4EHL7.js → chunk-Z7RBRCRN.js} +1 -1
  13. package/dist/chunk-Z7RBRCRN.js.map +1 -0
  14. package/dist/google/index.d.ts +376 -7
  15. package/dist/google/index.js +127 -15
  16. package/dist/google/index.js.map +1 -1
  17. package/dist/http/index.d.ts +222 -25
  18. package/dist/http/index.js +3 -3
  19. package/dist/index.d.ts +1482 -198
  20. package/dist/index.js +233 -49
  21. package/dist/index.js.map +1 -1
  22. package/dist/ollama/index.d.ts +92 -20
  23. package/dist/ollama/index.js +17 -7
  24. package/dist/ollama/index.js.map +1 -1
  25. package/dist/openai/index.d.ts +340 -61
  26. package/dist/openai/index.js +57 -15
  27. package/dist/openai/index.js.map +1 -1
  28. package/dist/openrouter/index.d.ts +107 -51
  29. package/dist/openrouter/index.js +36 -8
  30. package/dist/openrouter/index.js.map +1 -1
  31. package/dist/provider-mKkz7Q9U.d.ts +488 -0
  32. package/dist/retry-Dh70lgr0.d.ts +508 -0
  33. package/dist/xai/index.d.ts +97 -22
  34. package/dist/xai/index.js +55 -19
  35. package/dist/xai/index.js.map +1 -1
  36. package/package.json +8 -12
  37. package/dist/chunk-CUCRF5W6.js +0 -136
  38. package/dist/chunk-CUCRF5W6.js.map +0 -1
  39. package/dist/chunk-SUNYWHTH.js.map +0 -1
  40. package/dist/chunk-W4BB4BG2.js.map +0 -1
  41. package/dist/chunk-X5G4EHL7.js.map +0 -1
  42. package/dist/chunk-Y6Q7JCNP.js.map +0 -1
  43. package/dist/provider-CUJWjgNl.d.ts +0 -192
  44. package/dist/retry-I2661_rv.d.ts +0 -118
  45. package/src/anthropic/index.ts +0 -3
  46. package/src/core/image.ts +0 -188
  47. package/src/core/llm.ts +0 -650
  48. package/src/core/provider.ts +0 -92
  49. package/src/google/index.ts +0 -3
  50. package/src/http/errors.ts +0 -112
  51. package/src/http/fetch.ts +0 -210
  52. package/src/http/index.ts +0 -31
  53. package/src/http/keys.ts +0 -136
  54. package/src/http/retry.ts +0 -205
  55. package/src/http/sse.ts +0 -136
  56. package/src/index.ts +0 -32
  57. package/src/ollama/index.ts +0 -3
  58. package/src/openai/index.ts +0 -39
  59. package/src/openrouter/index.ts +0 -11
  60. package/src/providers/anthropic/index.ts +0 -17
  61. package/src/providers/anthropic/llm.ts +0 -196
  62. package/src/providers/anthropic/transform.ts +0 -434
  63. package/src/providers/anthropic/types.ts +0 -213
  64. package/src/providers/google/index.ts +0 -17
  65. package/src/providers/google/llm.ts +0 -203
  66. package/src/providers/google/transform.ts +0 -447
  67. package/src/providers/google/types.ts +0 -214
  68. package/src/providers/ollama/index.ts +0 -43
  69. package/src/providers/ollama/llm.ts +0 -272
  70. package/src/providers/ollama/transform.ts +0 -434
  71. package/src/providers/ollama/types.ts +0 -260
  72. package/src/providers/openai/index.ts +0 -186
  73. package/src/providers/openai/llm.completions.ts +0 -201
  74. package/src/providers/openai/llm.responses.ts +0 -211
  75. package/src/providers/openai/transform.completions.ts +0 -561
  76. package/src/providers/openai/transform.responses.ts +0 -708
  77. package/src/providers/openai/types.ts +0 -1249
  78. package/src/providers/openrouter/index.ts +0 -177
  79. package/src/providers/openrouter/llm.completions.ts +0 -201
  80. package/src/providers/openrouter/llm.responses.ts +0 -211
  81. package/src/providers/openrouter/transform.completions.ts +0 -538
  82. package/src/providers/openrouter/transform.responses.ts +0 -742
  83. package/src/providers/openrouter/types.ts +0 -717
  84. package/src/providers/xai/index.ts +0 -223
  85. package/src/providers/xai/llm.completions.ts +0 -201
  86. package/src/providers/xai/llm.messages.ts +0 -195
  87. package/src/providers/xai/llm.responses.ts +0 -211
  88. package/src/providers/xai/transform.completions.ts +0 -565
  89. package/src/providers/xai/transform.messages.ts +0 -448
  90. package/src/providers/xai/transform.responses.ts +0 -678
  91. package/src/providers/xai/types.ts +0 -938
  92. package/src/types/content.ts +0 -133
  93. package/src/types/errors.ts +0 -85
  94. package/src/types/index.ts +0 -105
  95. package/src/types/llm.ts +0 -211
  96. package/src/types/messages.ts +0 -205
  97. package/src/types/provider.ts +0 -195
  98. package/src/types/schema.ts +0 -58
  99. package/src/types/stream.ts +0 -188
  100. package/src/types/thread.ts +0 -226
  101. package/src/types/tool.ts +0 -88
  102. package/src/types/turn.ts +0 -118
  103. package/src/utils/id.ts +0 -28
  104. package/src/xai/index.ts +0 -41
@@ -1,8 +1,22 @@
1
- import { b as Provider, M as ModelReference, a as LLMHandler } from '../provider-CUJWjgNl.js';
1
+ import { b as Provider, M as ModelReference, a as LLMHandler } from '../provider-mKkz7Q9U.js';
2
2
 
3
3
  /**
4
- * OpenRouter Chat Completions API parameters
5
- * These are passed through to the /api/v1/chat/completions endpoint
4
+ * OpenRouter-specific types for the Unified Provider Protocol.
5
+ *
6
+ * This module defines types for both the Chat Completions API and the
7
+ * Responses API (beta), including request/response formats, streaming
8
+ * events, and OpenRouter-specific features like model routing.
9
+ *
10
+ * @module types
11
+ */
12
+ /**
13
+ * Parameters for OpenRouter's Chat Completions API.
14
+ *
15
+ * These parameters are passed through to the `/api/v1/chat/completions` endpoint.
16
+ * Includes standard OpenAI-compatible parameters plus OpenRouter-specific features
17
+ * like model routing and provider preferences.
18
+ *
19
+ * @see {@link https://openrouter.ai/docs/api-reference/chat-completions | OpenRouter Chat Completions API}
6
20
  */
7
21
  interface OpenRouterCompletionsParams {
8
22
  /** Maximum number of tokens to generate */
@@ -74,8 +88,13 @@ interface OpenRouterCompletionsParams {
74
88
  };
75
89
  }
76
90
  /**
77
- * OpenRouter Responses API parameters (Beta)
78
- * These are passed through to the /api/v1/responses endpoint
91
+ * Parameters for OpenRouter's Responses API (beta).
92
+ *
93
+ * These parameters are passed through to the `/api/v1/responses` endpoint.
94
+ * The Responses API uses a different structure than Chat Completions and
95
+ * supports features like reasoning configuration.
96
+ *
97
+ * @see {@link https://openrouter.ai/docs/api-reference/responses | OpenRouter Responses API}
79
98
  */
80
99
  interface OpenRouterResponsesParams {
81
100
  /** Maximum output tokens */
@@ -94,49 +113,58 @@ interface OpenRouterResponsesParams {
94
113
  };
95
114
  }
96
115
  /**
97
- * API mode for OpenRouter provider
116
+ * API mode selection for OpenRouter provider.
117
+ *
118
+ * - `'completions'`: Chat Completions API (stable, recommended)
119
+ * - `'responses'`: Responses API (beta)
98
120
  */
99
121
  type OpenRouterAPIMode = 'completions' | 'responses';
100
122
  /**
101
- * Model options when creating a model reference
123
+ * Options for creating an OpenRouter model reference.
102
124
  */
103
125
  interface OpenRouterModelOptions {
104
- /** Which API to use */
126
+ /** Which API to use for this model. */
105
127
  api?: OpenRouterAPIMode;
106
128
  }
107
129
  /**
108
- * Model reference with OpenRouter-specific options
130
+ * Model reference with OpenRouter-specific options.
109
131
  */
110
132
  interface OpenRouterModelReference {
133
+ /** The model identifier in `provider/model` format. */
111
134
  modelId: string;
135
+ /** Optional API selection. */
112
136
  options?: OpenRouterModelOptions;
113
137
  }
114
138
  /**
115
- * OpenRouter provider configuration
139
+ * Configuration for the OpenRouter provider.
116
140
  */
117
141
  interface OpenRouterConfig {
118
- /** Which API to use: 'completions' (default) or 'responses' (beta) */
142
+ /** Which API to use: 'completions' (default) or 'responses' (beta). */
119
143
  api?: 'completions' | 'responses';
120
144
  }
121
145
  /**
122
- * Provider routing preferences
146
+ * Provider routing preferences for OpenRouter.
147
+ *
148
+ * Controls how OpenRouter selects and routes requests to upstream providers.
149
+ *
150
+ * @see {@link https://openrouter.ai/docs/guides/routing/provider-selection | Provider Selection}
123
151
  */
124
152
  interface OpenRouterProviderPreferences {
125
- /** Allow fallback to other providers */
153
+ /** Allow fallback to other providers if the primary is unavailable. */
126
154
  allow_fallbacks?: boolean;
127
- /** Require specific parameters to be supported */
155
+ /** Require that the provider supports all specified parameters. */
128
156
  require_parameters?: boolean;
129
- /** Data collection policy */
157
+ /** Data collection policy: 'allow' or 'deny'. */
130
158
  data_collection?: 'allow' | 'deny';
131
- /** Order of provider preference */
159
+ /** Ordered list of preferred provider slugs. */
132
160
  order?: string[];
133
- /** Ignore specific providers */
161
+ /** List of provider slugs to exclude from routing. */
134
162
  ignore?: string[];
135
- /** Quantization preferences */
163
+ /** Preferred quantization levels (e.g., 'fp16', 'int8'). */
136
164
  quantizations?: string[];
137
165
  }
138
166
  /**
139
- * Response format
167
+ * Response format configuration for structured output.
140
168
  */
141
169
  type OpenRouterResponseFormat = {
142
170
  type: 'text';
@@ -145,103 +173,131 @@ type OpenRouterResponseFormat = {
145
173
  } | {
146
174
  type: 'json_schema';
147
175
  json_schema: {
176
+ /** Schema name for identification. */
148
177
  name: string;
178
+ /** Optional schema description. */
149
179
  description?: string;
180
+ /** JSON Schema definition. */
150
181
  schema: Record<string, unknown>;
182
+ /** Enable strict schema validation. */
151
183
  strict?: boolean;
152
184
  };
153
185
  };
154
186
 
155
- /** Union type for modalities interface */
187
+ /**
188
+ * Union type for both Completions and Responses API parameter types.
189
+ * Used internally to type the modalities handler.
190
+ */
156
191
  type OpenRouterLLMParamsUnion = OpenRouterCompletionsParams | OpenRouterResponsesParams;
157
192
  /**
158
- * OpenRouter provider options
193
+ * Configuration options for creating an OpenRouter model reference.
194
+ *
195
+ * OpenRouter supports two distinct APIs:
196
+ * - Chat Completions API: Stable, production-ready, supports most models
197
+ * - Responses API: Beta, supports advanced features like reasoning
159
198
  */
160
199
  interface OpenRouterProviderOptions {
161
200
  /**
162
- * Which API to use:
163
- * - 'completions': Chat Completions API (default, recommended)
164
- * - 'responses': Responses API (beta)
201
+ * Which OpenRouter API to use.
202
+ *
203
+ * - `'completions'`: Chat Completions API (default, recommended for production)
204
+ * - `'responses'`: Responses API (beta, supports reasoning models)
165
205
  */
166
206
  api?: 'completions' | 'responses';
167
207
  }
168
208
  /**
169
- * OpenRouter provider with configurable API mode
209
+ * OpenRouter provider interface with configurable API mode.
210
+ *
211
+ * OpenRouter is a unified API that provides access to hundreds of AI models
212
+ * through a single endpoint, including models from OpenAI, Anthropic, Google,
213
+ * Meta, Mistral, and many others.
170
214
  *
171
- * @example
172
- * // Using the Chat Completions API (default)
215
+ * @example Using the Chat Completions API (default)
216
+ * ```typescript
173
217
  * const model = openrouter('openai/gpt-4o');
218
+ * ```
174
219
  *
175
- * @example
176
- * // Using the Responses API (beta)
220
+ * @example Using the Responses API (beta)
221
+ * ```typescript
177
222
  * const model = openrouter('openai/gpt-4o', { api: 'responses' });
223
+ * ```
178
224
  *
179
- * @example
180
- * // Explicit Completions API
225
+ * @example Using Anthropic models
226
+ * ```typescript
181
227
  * const model = openrouter('anthropic/claude-3.5-sonnet', { api: 'completions' });
228
+ * ```
182
229
  */
183
230
  interface OpenRouterProvider extends Provider<OpenRouterProviderOptions> {
184
231
  /**
185
- * Create a model reference
186
- * @param modelId - The model identifier (e.g., 'openai/gpt-4o', 'anthropic/claude-3.5-sonnet', 'meta-llama/llama-3.1-70b-instruct')
187
- * @param options - Provider options including API selection
232
+ * Creates a model reference for the specified model ID.
233
+ *
234
+ * @param modelId - The OpenRouter model identifier in `provider/model` format
235
+ * (e.g., 'openai/gpt-4o', 'anthropic/claude-3.5-sonnet',
236
+ * 'meta-llama/llama-3.1-70b-instruct')
237
+ * @param options - Optional configuration including API selection
238
+ * @returns A model reference that can be passed to llm()
188
239
  */
189
240
  (modelId: string, options?: OpenRouterProviderOptions): ModelReference<OpenRouterProviderOptions>;
190
- /** Provider name */
241
+ /** Provider identifier. Always 'openrouter'. */
191
242
  readonly name: 'openrouter';
192
- /** Provider version */
243
+ /** Semantic version of this provider implementation. */
193
244
  readonly version: string;
194
- /** Supported modalities */
245
+ /**
246
+ * Supported modalities for this provider.
247
+ * OpenRouter currently only supports LLM (text generation).
248
+ */
195
249
  readonly modalities: {
196
250
  llm: LLMHandler<OpenRouterLLMParamsUnion>;
197
251
  };
198
252
  }
199
253
  /**
200
- * OpenRouter provider
201
- *
202
- * Supports both the Chat Completions API (default) and Responses API (beta).
254
+ * OpenRouter provider singleton.
203
255
  *
204
256
  * OpenRouter is a unified API that provides access to hundreds of AI models
205
257
  * through a single endpoint, including models from OpenAI, Anthropic, Google,
206
258
  * Meta, Mistral, and many others.
207
259
  *
208
- * @example
209
- * ```ts
260
+ * Supports both the Chat Completions API (default) and Responses API (beta).
261
+ *
262
+ * @example Basic usage with Chat Completions API
263
+ * ```typescript
210
264
  * import { openrouter } from './providers/openrouter';
211
265
  * import { llm } from './core/llm';
212
266
  *
213
- * // Using Chat Completions API (default, recommended)
214
267
  * const model = llm({
215
268
  * model: openrouter('openai/gpt-4o'),
216
269
  * params: { max_tokens: 1000 }
217
270
  * });
218
271
  *
219
- * // Using Responses API (beta)
272
+ * const turn = await model.generate('Hello!');
273
+ * console.log(turn.response.text);
274
+ * ```
275
+ *
276
+ * @example Using the Responses API (beta)
277
+ * ```typescript
220
278
  * const betaModel = llm({
221
279
  * model: openrouter('openai/gpt-4o', { api: 'responses' }),
222
280
  * params: { max_output_tokens: 1000 }
223
281
  * });
282
+ * ```
224
283
  *
225
- * // Using OpenRouter-specific features
284
+ * @example Model routing and fallback configuration
285
+ * ```typescript
226
286
  * const routedModel = llm({
227
287
  * model: openrouter('openai/gpt-4o'),
228
288
  * params: {
229
289
  * max_tokens: 1000,
230
- * // Fallback routing
231
290
  * models: ['openai/gpt-4o', 'anthropic/claude-3.5-sonnet'],
232
291
  * route: 'fallback',
233
- * // Provider preferences
234
292
  * provider: {
235
293
  * allow_fallbacks: true,
236
294
  * require_parameters: true,
237
295
  * },
238
296
  * }
239
297
  * });
240
- *
241
- * // Generate
242
- * const turn = await model.generate('Hello!');
243
- * console.log(turn.response.text);
244
298
  * ```
299
+ *
300
+ * @see {@link https://openrouter.ai/docs | OpenRouter Documentation}
245
301
  */
246
302
  declare const openrouter: OpenRouterProvider;
247
303
 
@@ -3,17 +3,17 @@ import {
3
3
  isAssistantMessage,
4
4
  isToolResultMessage,
5
5
  isUserMessage
6
- } from "../chunk-W4BB4BG2.js";
6
+ } from "../chunk-SVYROCLD.js";
7
7
  import {
8
8
  parseSSEStream
9
- } from "../chunk-X5G4EHL7.js";
9
+ } from "../chunk-Z7RBRCRN.js";
10
10
  import {
11
11
  UPPError,
12
12
  doFetch,
13
13
  doStreamFetch,
14
14
  normalizeHttpError,
15
15
  resolveApiKey
16
- } from "../chunk-SUNYWHTH.js";
16
+ } from "../chunk-MOU4U3PO.js";
17
17
 
18
18
  // src/providers/openrouter/transform.completions.ts
19
19
  function transformRequest(request, modelId) {
@@ -72,9 +72,25 @@ function transformMessages(messages, system) {
72
72
  function filterValidContent(content) {
73
73
  return content.filter((c) => c && typeof c.type === "string");
74
74
  }
75
+ function extractCacheControl(message) {
76
+ const openrouterMeta = message.metadata?.openrouter;
77
+ return openrouterMeta?.cache_control;
78
+ }
75
79
  function transformMessage(message) {
76
80
  if (isUserMessage(message)) {
77
81
  const validContent = filterValidContent(message.content);
82
+ const cacheControl = extractCacheControl(message);
83
+ if (cacheControl) {
84
+ const content = validContent.map(transformContentBlock);
85
+ for (let i = content.length - 1; i >= 0; i--) {
86
+ const block = content[i];
87
+ if (block?.type === "text") {
88
+ content[i] = { type: "text", text: block.text, cache_control: cacheControl };
89
+ break;
90
+ }
91
+ }
92
+ return { role: "user", content };
93
+ }
78
94
  if (validContent.length === 1 && validContent[0]?.type === "text") {
79
95
  return {
80
96
  role: "user",
@@ -215,7 +231,9 @@ function transformResponse(data) {
215
231
  const usage = {
216
232
  inputTokens: data.usage.prompt_tokens,
217
233
  outputTokens: data.usage.completion_tokens,
218
- totalTokens: data.usage.total_tokens
234
+ totalTokens: data.usage.total_tokens,
235
+ cacheReadTokens: data.usage.prompt_tokens_details?.cached_tokens ?? 0,
236
+ cacheWriteTokens: 0
219
237
  };
220
238
  let stopReason = "end_turn";
221
239
  switch (choice.finish_reason) {
@@ -247,7 +265,8 @@ function createStreamState() {
247
265
  toolCalls: /* @__PURE__ */ new Map(),
248
266
  finishReason: null,
249
267
  inputTokens: 0,
250
- outputTokens: 0
268
+ outputTokens: 0,
269
+ cacheReadTokens: 0
251
270
  };
252
271
  }
253
272
  function transformStreamEvent(chunk, state) {
@@ -305,6 +324,7 @@ function transformStreamEvent(chunk, state) {
305
324
  if (chunk.usage) {
306
325
  state.inputTokens = chunk.usage.prompt_tokens;
307
326
  state.outputTokens = chunk.usage.completion_tokens;
327
+ state.cacheReadTokens = chunk.usage.prompt_tokens_details?.cached_tokens ?? 0;
308
328
  }
309
329
  return events;
310
330
  }
@@ -349,7 +369,9 @@ function buildResponseFromState(state) {
349
369
  const usage = {
350
370
  inputTokens: state.inputTokens,
351
371
  outputTokens: state.outputTokens,
352
- totalTokens: state.inputTokens + state.outputTokens
372
+ totalTokens: state.inputTokens + state.outputTokens,
373
+ cacheReadTokens: state.cacheReadTokens,
374
+ cacheWriteTokens: 0
353
375
  };
354
376
  let stopReason = "end_turn";
355
377
  switch (state.finishReason) {
@@ -765,7 +787,9 @@ function transformResponse2(data) {
765
787
  const usage = {
766
788
  inputTokens: data.usage.input_tokens,
767
789
  outputTokens: data.usage.output_tokens,
768
- totalTokens: data.usage.total_tokens
790
+ totalTokens: data.usage.total_tokens,
791
+ cacheReadTokens: data.usage.input_tokens_details?.cached_tokens ?? 0,
792
+ cacheWriteTokens: 0
769
793
  };
770
794
  let stopReason = "end_turn";
771
795
  if (data.status === "completed") {
@@ -794,6 +818,7 @@ function createStreamState2() {
794
818
  status: "in_progress",
795
819
  inputTokens: 0,
796
820
  outputTokens: 0,
821
+ cacheReadTokens: 0,
797
822
  hadRefusal: false
798
823
  };
799
824
  }
@@ -814,6 +839,7 @@ function transformStreamEvent2(event, state) {
814
839
  if (event.response?.usage) {
815
840
  state.inputTokens = event.response.usage.input_tokens;
816
841
  state.outputTokens = event.response.usage.output_tokens;
842
+ state.cacheReadTokens = event.response.usage.input_tokens_details?.cached_tokens ?? 0;
817
843
  }
818
844
  if (event.response?.output) {
819
845
  for (let i = 0; i < event.response.output.length; i++) {
@@ -1039,7 +1065,9 @@ function buildResponseFromState2(state) {
1039
1065
  const usage = {
1040
1066
  inputTokens: state.inputTokens,
1041
1067
  outputTokens: state.outputTokens,
1042
- totalTokens: state.inputTokens + state.outputTokens
1068
+ totalTokens: state.inputTokens + state.outputTokens,
1069
+ cacheReadTokens: state.cacheReadTokens,
1070
+ cacheWriteTokens: 0
1043
1071
  };
1044
1072
  let stopReason = "end_turn";
1045
1073
  if (state.status === "completed") {