@openrouter/ai-sdk-provider 2.2.4 → 2.3.0

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/dist/index.mjs CHANGED
@@ -2249,9 +2249,58 @@ var OpenRouterErrorResponseSchema = z2.object({
2249
2249
  param: z2.any().nullable().optional().default(null)
2250
2250
  }).passthrough()
2251
2251
  }).passthrough();
2252
+ function extractErrorMessage(data) {
2253
+ const error = data.error;
2254
+ const metadata = error.metadata;
2255
+ if (!metadata) {
2256
+ return data.error.message;
2257
+ }
2258
+ const parts = [];
2259
+ if (typeof metadata.provider_name === "string" && metadata.provider_name) {
2260
+ parts.push(`[${metadata.provider_name}]`);
2261
+ }
2262
+ const raw = metadata.raw;
2263
+ const rawMessage = extractRawMessage(raw);
2264
+ if (rawMessage && rawMessage !== data.error.message) {
2265
+ parts.push(rawMessage);
2266
+ } else {
2267
+ parts.push(data.error.message);
2268
+ }
2269
+ return parts.join(" ");
2270
+ }
2271
+ function extractRawMessage(raw) {
2272
+ if (typeof raw === "string") {
2273
+ try {
2274
+ const parsed = JSON.parse(raw);
2275
+ if (typeof parsed === "object" && parsed !== null) {
2276
+ return extractRawMessage(parsed);
2277
+ }
2278
+ return raw;
2279
+ } catch (e) {
2280
+ return raw;
2281
+ }
2282
+ }
2283
+ if (typeof raw !== "object" || raw === null) {
2284
+ return void 0;
2285
+ }
2286
+ const obj = raw;
2287
+ for (const field of ["message", "error", "detail", "details", "msg"]) {
2288
+ const value = obj[field];
2289
+ if (typeof value === "string" && value.length > 0) {
2290
+ return value;
2291
+ }
2292
+ if (typeof value === "object" && value !== null) {
2293
+ const nested = extractRawMessage(value);
2294
+ if (nested) {
2295
+ return nested;
2296
+ }
2297
+ }
2298
+ }
2299
+ return void 0;
2300
+ }
2252
2301
  var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
2253
2302
  errorSchema: OpenRouterErrorResponseSchema,
2254
- errorToMessage: (data) => data.error.message
2303
+ errorToMessage: extractErrorMessage
2255
2304
  });
2256
2305
 
2257
2306
  // src/schemas/provider-metadata.ts
@@ -3097,7 +3146,9 @@ var OpenRouterChatLanguageModel = class {
3097
3146
  // Provider routing settings:
3098
3147
  provider: this.settings.provider,
3099
3148
  // Debug settings:
3100
- debug: this.settings.debug
3149
+ debug: this.settings.debug,
3150
+ // Anthropic automatic caching:
3151
+ cache_control: this.settings.cache_control
3101
3152
  }, this.config.extraBody), this.settings.extraBody);
3102
3153
  if (tools && tools.length > 0) {
3103
3154
  const mappedTools = tools.filter(
@@ -3118,10 +3169,11 @@ var OpenRouterChatLanguageModel = class {
3118
3169
  return baseArgs;
3119
3170
  }
3120
3171
  async doGenerate(options) {
3121
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
3172
+ var _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
3122
3173
  const providerOptions = options.providerOptions || {};
3123
3174
  const openrouterOptions = providerOptions.openrouter || {};
3124
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
3175
+ const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
3176
+ const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
3125
3177
  const { value: responseValue, responseHeaders } = await postJsonToApi({
3126
3178
  url: this.config.url({
3127
3179
  path: "/chat/completions",
@@ -3158,7 +3210,7 @@ var OpenRouterChatLanguageModel = class {
3158
3210
  });
3159
3211
  }
3160
3212
  const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
3161
- const reasoningDetails = (_a16 = choice.message.reasoning_details) != null ? _a16 : [];
3213
+ const reasoningDetails = (_b16 = choice.message.reasoning_details) != null ? _b16 : [];
3162
3214
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
3163
3215
  switch (detail.type) {
3164
3216
  case "reasoning.text" /* Text */: {
@@ -3227,9 +3279,9 @@ var OpenRouterChatLanguageModel = class {
3227
3279
  for (const toolCall of choice.message.tool_calls) {
3228
3280
  content.push({
3229
3281
  type: "tool-call",
3230
- toolCallId: (_b16 = toolCall.id) != null ? _b16 : generateId(),
3282
+ toolCallId: (_c = toolCall.id) != null ? _c : generateId(),
3231
3283
  toolName: toolCall.function.name,
3232
- input: (_c = toolCall.function.arguments) != null ? _c : "{}",
3284
+ input: (_d = toolCall.function.arguments) != null ? _d : "{}",
3233
3285
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3234
3286
  openrouter: {
3235
3287
  reasoning_details: reasoningDetails
@@ -3256,19 +3308,19 @@ var OpenRouterChatLanguageModel = class {
3256
3308
  sourceType: "url",
3257
3309
  id: annotation.url_citation.url,
3258
3310
  url: annotation.url_citation.url,
3259
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3311
+ title: (_e = annotation.url_citation.title) != null ? _e : "",
3260
3312
  providerMetadata: {
3261
3313
  openrouter: {
3262
- content: (_e = annotation.url_citation.content) != null ? _e : "",
3263
- startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3264
- endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3314
+ content: (_f = annotation.url_citation.content) != null ? _f : "",
3315
+ startIndex: (_g = annotation.url_citation.start_index) != null ? _g : 0,
3316
+ endIndex: (_h = annotation.url_citation.end_index) != null ? _h : 0
3265
3317
  }
3266
3318
  }
3267
3319
  });
3268
3320
  }
3269
3321
  }
3270
3322
  }
3271
- const fileAnnotations = (_h = choice.message.annotations) == null ? void 0 : _h.filter(
3323
+ const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3272
3324
  (a) => a.type === "file"
3273
3325
  );
3274
3326
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3276,7 +3328,7 @@ var OpenRouterChatLanguageModel = class {
3276
3328
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3277
3329
  );
3278
3330
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3279
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_i = choice.finish_reason) != null ? _i : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3331
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3280
3332
  return {
3281
3333
  content,
3282
3334
  finishReason: effectiveFinishReason,
@@ -3284,22 +3336,22 @@ var OpenRouterChatLanguageModel = class {
3284
3336
  warnings: [],
3285
3337
  providerMetadata: {
3286
3338
  openrouter: OpenRouterProviderMetadataSchema.parse({
3287
- provider: (_j = response.provider) != null ? _j : "",
3288
- reasoning_details: (_k = choice.message.reasoning_details) != null ? _k : [],
3339
+ provider: (_k = response.provider) != null ? _k : "",
3340
+ reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3289
3341
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3290
3342
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3291
- promptTokens: (_l = usageInfo.inputTokens.total) != null ? _l : 0,
3292
- completionTokens: (_m = usageInfo.outputTokens.total) != null ? _m : 0,
3293
- totalTokens: ((_n = usageInfo.inputTokens.total) != null ? _n : 0) + ((_o = usageInfo.outputTokens.total) != null ? _o : 0)
3294
- }, ((_p = response.usage) == null ? void 0 : _p.cost) != null ? { cost: response.usage.cost } : {}), ((_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? {
3343
+ promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3344
+ completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3345
+ totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0)
3346
+ }, ((_q = response.usage) == null ? void 0 : _q.cost) != null ? { cost: response.usage.cost } : {}), ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
3295
3347
  promptTokensDetails: {
3296
3348
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3297
3349
  }
3298
- } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
3350
+ } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3299
3351
  completionTokensDetails: {
3300
3352
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3301
3353
  }
3302
- } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
3354
+ } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3303
3355
  costDetails: {
3304
3356
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3305
3357
  }
@@ -3315,10 +3367,11 @@ var OpenRouterChatLanguageModel = class {
3315
3367
  };
3316
3368
  }
3317
3369
  async doStream(options) {
3318
- var _a16;
3370
+ var _b16;
3319
3371
  const providerOptions = options.providerOptions || {};
3320
3372
  const openrouterOptions = providerOptions.openrouter || {};
3321
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
3373
+ const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
3374
+ const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
3322
3375
  const { value: response, responseHeaders } = await postJsonToApi({
3323
3376
  url: this.config.url({
3324
3377
  path: "/chat/completions",
@@ -3330,7 +3383,7 @@ var OpenRouterChatLanguageModel = class {
3330
3383
  // only include stream_options when in strict compatibility mode:
3331
3384
  stream_options: this.config.compatibility === "strict" ? __spreadValues({
3332
3385
  include_usage: true
3333
- }, ((_a16 = this.settings.usage) == null ? void 0 : _a16.include) ? { include_usage: true } : {}) : void 0
3386
+ }, ((_b16 = this.settings.usage) == null ? void 0 : _b16.include) ? { include_usage: true } : {}) : void 0
3334
3387
  }),
3335
3388
  failedResponseHandler: openrouterFailedResponseHandler,
3336
3389
  successfulResponseHandler: createEventSourceResponseHandler(
@@ -3370,7 +3423,7 @@ var OpenRouterChatLanguageModel = class {
3370
3423
  stream: response.pipeThrough(
3371
3424
  new TransformStream({
3372
3425
  transform(chunk, controller) {
3373
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3426
+ var _a17, _b17, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3374
3427
  if (options.includeRawChunks) {
3375
3428
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3376
3429
  }
@@ -3407,7 +3460,7 @@ var OpenRouterChatLanguageModel = class {
3407
3460
  Object.assign(usage.outputTokens, computed.outputTokens);
3408
3461
  rawUsage = value.usage;
3409
3462
  const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
3410
- const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
3463
+ const completionTokens = (_b17 = value.usage.completion_tokens) != null ? _b17 : 0;
3411
3464
  openrouterUsage.promptTokens = promptTokens;
3412
3465
  if (value.usage.prompt_tokens_details) {
3413
3466
  openrouterUsage.promptTokensDetails = {
@@ -4523,7 +4576,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4523
4576
  }
4524
4577
 
4525
4578
  // src/version.ts
4526
- var VERSION2 = false ? "0.0.0-test" : "2.2.4";
4579
+ var VERSION2 = false ? "0.0.0-test" : "2.3.0";
4527
4580
 
4528
4581
  // src/provider.ts
4529
4582
  function createOpenRouter(options = {}) {