@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.
@@ -2214,9 +2214,58 @@ var OpenRouterErrorResponseSchema = z2.object({
2214
2214
  param: z2.any().nullable().optional().default(null)
2215
2215
  }).passthrough()
2216
2216
  }).passthrough();
2217
+ function extractErrorMessage(data) {
2218
+ const error = data.error;
2219
+ const metadata = error.metadata;
2220
+ if (!metadata) {
2221
+ return data.error.message;
2222
+ }
2223
+ const parts = [];
2224
+ if (typeof metadata.provider_name === "string" && metadata.provider_name) {
2225
+ parts.push(`[${metadata.provider_name}]`);
2226
+ }
2227
+ const raw = metadata.raw;
2228
+ const rawMessage = extractRawMessage(raw);
2229
+ if (rawMessage && rawMessage !== data.error.message) {
2230
+ parts.push(rawMessage);
2231
+ } else {
2232
+ parts.push(data.error.message);
2233
+ }
2234
+ return parts.join(" ");
2235
+ }
2236
+ function extractRawMessage(raw) {
2237
+ if (typeof raw === "string") {
2238
+ try {
2239
+ const parsed = JSON.parse(raw);
2240
+ if (typeof parsed === "object" && parsed !== null) {
2241
+ return extractRawMessage(parsed);
2242
+ }
2243
+ return raw;
2244
+ } catch (e) {
2245
+ return raw;
2246
+ }
2247
+ }
2248
+ if (typeof raw !== "object" || raw === null) {
2249
+ return void 0;
2250
+ }
2251
+ const obj = raw;
2252
+ for (const field of ["message", "error", "detail", "details", "msg"]) {
2253
+ const value = obj[field];
2254
+ if (typeof value === "string" && value.length > 0) {
2255
+ return value;
2256
+ }
2257
+ if (typeof value === "object" && value !== null) {
2258
+ const nested = extractRawMessage(value);
2259
+ if (nested) {
2260
+ return nested;
2261
+ }
2262
+ }
2263
+ }
2264
+ return void 0;
2265
+ }
2217
2266
  var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
2218
2267
  errorSchema: OpenRouterErrorResponseSchema,
2219
- errorToMessage: (data) => data.error.message
2268
+ errorToMessage: extractErrorMessage
2220
2269
  });
2221
2270
 
2222
2271
  // src/schemas/provider-metadata.ts
@@ -3062,7 +3111,9 @@ var OpenRouterChatLanguageModel = class {
3062
3111
  // Provider routing settings:
3063
3112
  provider: this.settings.provider,
3064
3113
  // Debug settings:
3065
- debug: this.settings.debug
3114
+ debug: this.settings.debug,
3115
+ // Anthropic automatic caching:
3116
+ cache_control: this.settings.cache_control
3066
3117
  }, this.config.extraBody), this.settings.extraBody);
3067
3118
  if (tools && tools.length > 0) {
3068
3119
  const mappedTools = tools.filter(
@@ -3083,10 +3134,11 @@ var OpenRouterChatLanguageModel = class {
3083
3134
  return baseArgs;
3084
3135
  }
3085
3136
  async doGenerate(options) {
3086
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
3137
+ var _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
3087
3138
  const providerOptions = options.providerOptions || {};
3088
3139
  const openrouterOptions = providerOptions.openrouter || {};
3089
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
3140
+ const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
3141
+ const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
3090
3142
  const { value: responseValue, responseHeaders } = await postJsonToApi({
3091
3143
  url: this.config.url({
3092
3144
  path: "/chat/completions",
@@ -3123,7 +3175,7 @@ var OpenRouterChatLanguageModel = class {
3123
3175
  });
3124
3176
  }
3125
3177
  const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
3126
- const reasoningDetails = (_a16 = choice.message.reasoning_details) != null ? _a16 : [];
3178
+ const reasoningDetails = (_b16 = choice.message.reasoning_details) != null ? _b16 : [];
3127
3179
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
3128
3180
  switch (detail.type) {
3129
3181
  case "reasoning.text" /* Text */: {
@@ -3192,9 +3244,9 @@ var OpenRouterChatLanguageModel = class {
3192
3244
  for (const toolCall of choice.message.tool_calls) {
3193
3245
  content.push({
3194
3246
  type: "tool-call",
3195
- toolCallId: (_b16 = toolCall.id) != null ? _b16 : generateId(),
3247
+ toolCallId: (_c = toolCall.id) != null ? _c : generateId(),
3196
3248
  toolName: toolCall.function.name,
3197
- input: (_c = toolCall.function.arguments) != null ? _c : "{}",
3249
+ input: (_d = toolCall.function.arguments) != null ? _d : "{}",
3198
3250
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3199
3251
  openrouter: {
3200
3252
  reasoning_details: reasoningDetails
@@ -3221,19 +3273,19 @@ var OpenRouterChatLanguageModel = class {
3221
3273
  sourceType: "url",
3222
3274
  id: annotation.url_citation.url,
3223
3275
  url: annotation.url_citation.url,
3224
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3276
+ title: (_e = annotation.url_citation.title) != null ? _e : "",
3225
3277
  providerMetadata: {
3226
3278
  openrouter: {
3227
- content: (_e = annotation.url_citation.content) != null ? _e : "",
3228
- startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3229
- endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3279
+ content: (_f = annotation.url_citation.content) != null ? _f : "",
3280
+ startIndex: (_g = annotation.url_citation.start_index) != null ? _g : 0,
3281
+ endIndex: (_h = annotation.url_citation.end_index) != null ? _h : 0
3230
3282
  }
3231
3283
  }
3232
3284
  });
3233
3285
  }
3234
3286
  }
3235
3287
  }
3236
- const fileAnnotations = (_h = choice.message.annotations) == null ? void 0 : _h.filter(
3288
+ const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3237
3289
  (a) => a.type === "file"
3238
3290
  );
3239
3291
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3241,7 +3293,7 @@ var OpenRouterChatLanguageModel = class {
3241
3293
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3242
3294
  );
3243
3295
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3244
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_i = choice.finish_reason) != null ? _i : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3296
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3245
3297
  return {
3246
3298
  content,
3247
3299
  finishReason: effectiveFinishReason,
@@ -3249,22 +3301,22 @@ var OpenRouterChatLanguageModel = class {
3249
3301
  warnings: [],
3250
3302
  providerMetadata: {
3251
3303
  openrouter: OpenRouterProviderMetadataSchema.parse({
3252
- provider: (_j = response.provider) != null ? _j : "",
3253
- reasoning_details: (_k = choice.message.reasoning_details) != null ? _k : [],
3304
+ provider: (_k = response.provider) != null ? _k : "",
3305
+ reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3254
3306
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3255
3307
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3256
- promptTokens: (_l = usageInfo.inputTokens.total) != null ? _l : 0,
3257
- completionTokens: (_m = usageInfo.outputTokens.total) != null ? _m : 0,
3258
- totalTokens: ((_n = usageInfo.inputTokens.total) != null ? _n : 0) + ((_o = usageInfo.outputTokens.total) != null ? _o : 0)
3259
- }, ((_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 ? {
3308
+ promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3309
+ completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3310
+ totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0)
3311
+ }, ((_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 ? {
3260
3312
  promptTokensDetails: {
3261
3313
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3262
3314
  }
3263
- } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
3315
+ } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3264
3316
  completionTokensDetails: {
3265
3317
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3266
3318
  }
3267
- } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
3319
+ } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3268
3320
  costDetails: {
3269
3321
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3270
3322
  }
@@ -3280,10 +3332,11 @@ var OpenRouterChatLanguageModel = class {
3280
3332
  };
3281
3333
  }
3282
3334
  async doStream(options) {
3283
- var _a16;
3335
+ var _b16;
3284
3336
  const providerOptions = options.providerOptions || {};
3285
3337
  const openrouterOptions = providerOptions.openrouter || {};
3286
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
3338
+ const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
3339
+ const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
3287
3340
  const { value: response, responseHeaders } = await postJsonToApi({
3288
3341
  url: this.config.url({
3289
3342
  path: "/chat/completions",
@@ -3295,7 +3348,7 @@ var OpenRouterChatLanguageModel = class {
3295
3348
  // only include stream_options when in strict compatibility mode:
3296
3349
  stream_options: this.config.compatibility === "strict" ? __spreadValues({
3297
3350
  include_usage: true
3298
- }, ((_a16 = this.settings.usage) == null ? void 0 : _a16.include) ? { include_usage: true } : {}) : void 0
3351
+ }, ((_b16 = this.settings.usage) == null ? void 0 : _b16.include) ? { include_usage: true } : {}) : void 0
3299
3352
  }),
3300
3353
  failedResponseHandler: openrouterFailedResponseHandler,
3301
3354
  successfulResponseHandler: createEventSourceResponseHandler(
@@ -3335,7 +3388,7 @@ var OpenRouterChatLanguageModel = class {
3335
3388
  stream: response.pipeThrough(
3336
3389
  new TransformStream({
3337
3390
  transform(chunk, controller) {
3338
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3391
+ var _a17, _b17, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3339
3392
  if (options.includeRawChunks) {
3340
3393
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3341
3394
  }
@@ -3372,7 +3425,7 @@ var OpenRouterChatLanguageModel = class {
3372
3425
  Object.assign(usage.outputTokens, computed.outputTokens);
3373
3426
  rawUsage = value.usage;
3374
3427
  const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
3375
- const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
3428
+ const completionTokens = (_b17 = value.usage.completion_tokens) != null ? _b17 : 0;
3376
3429
  openrouterUsage.promptTokens = promptTokens;
3377
3430
  if (value.usage.prompt_tokens_details) {
3378
3431
  openrouterUsage.promptTokensDetails = {