@openrouter/ai-sdk-provider 2.3.3 → 2.4.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
@@ -2417,6 +2417,31 @@ function createFinishReason(unified, raw) {
2417
2417
  return { unified, raw };
2418
2418
  }
2419
2419
 
2420
+ // src/utils/with-stream-error-handling.ts
2421
+ function withStreamErrorHandling(source, onError) {
2422
+ const reader = source.getReader();
2423
+ return new ReadableStream({
2424
+ async pull(controller) {
2425
+ try {
2426
+ const { done, value } = await reader.read();
2427
+ if (done) {
2428
+ controller.close();
2429
+ } else {
2430
+ controller.enqueue(value);
2431
+ }
2432
+ } catch (err) {
2433
+ onError(err);
2434
+ reader.cancel().catch(() => {
2435
+ });
2436
+ controller.close();
2437
+ }
2438
+ },
2439
+ cancel(reason) {
2440
+ reader.cancel(reason);
2441
+ }
2442
+ });
2443
+ }
2444
+
2420
2445
  // src/utils/reasoning-details-duplicate-tracker.ts
2421
2446
  var _seenKeys;
2422
2447
  var ReasoningDetailsDuplicateTracker = class {
@@ -2804,6 +2829,7 @@ function convertToOpenRouterChatMessages(prompt) {
2804
2829
  role: "tool",
2805
2830
  tool_call_id: toolResponse.toolCallId,
2806
2831
  content: content2,
2832
+ name: toolResponse.toolName,
2807
2833
  cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
2808
2834
  });
2809
2835
  }
@@ -3466,7 +3492,8 @@ var OpenRouterChatLanguageModel = class {
3466
3492
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3467
3493
  );
3468
3494
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3469
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3495
+ const mappedFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3496
+ const effectiveFinishReason = hasToolCalls && mappedFinishReason.unified === "other" ? createFinishReason("tool-calls", mappedFinishReason.raw) : mappedFinishReason;
3470
3497
  return {
3471
3498
  content,
3472
3499
  finishReason: effectiveFinishReason,
@@ -3530,6 +3557,10 @@ var OpenRouterChatLanguageModel = class {
3530
3557
  abortSignal: options.abortSignal,
3531
3558
  fetch: this.config.fetch
3532
3559
  });
3560
+ let streamError;
3561
+ const safeResponse = withStreamErrorHandling(response, (err) => {
3562
+ streamError = err;
3563
+ });
3533
3564
  const toolCalls = [];
3534
3565
  let finishReason = createFinishReason("other");
3535
3566
  const usage = {
@@ -3558,7 +3589,7 @@ var OpenRouterChatLanguageModel = class {
3558
3589
  let openrouterResponseId;
3559
3590
  let provider;
3560
3591
  return {
3561
- stream: response.pipeThrough(
3592
+ stream: safeResponse.pipeThrough(
3562
3593
  new TransformStream({
3563
3594
  transform(chunk, controller) {
3564
3595
  var _a17, _b17, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
@@ -3880,12 +3911,19 @@ var OpenRouterChatLanguageModel = class {
3880
3911
  flush(controller) {
3881
3912
  var _a17;
3882
3913
  const hasToolCalls = toolCalls.length > 0;
3914
+ if (streamError != null) {
3915
+ finishReason = createFinishReason("error");
3916
+ controller.enqueue({ type: "error", error: streamError });
3917
+ }
3883
3918
  const hasEncryptedReasoning = accumulatedReasoningDetails.some(
3884
3919
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3885
3920
  );
3886
3921
  if (hasToolCalls && hasEncryptedReasoning && finishReason.unified === "stop") {
3887
3922
  finishReason = createFinishReason("tool-calls", finishReason.raw);
3888
3923
  }
3924
+ if (hasToolCalls && finishReason.unified === "other") {
3925
+ finishReason = createFinishReason("tool-calls", finishReason.raw);
3926
+ }
3889
3927
  if (finishReason.unified === "tool-calls") {
3890
3928
  for (const toolCall of toolCalls) {
3891
3929
  if (toolCall && !toolCall.sent) {
@@ -4271,6 +4309,10 @@ var OpenRouterCompletionLanguageModel = class {
4271
4309
  abortSignal: options.abortSignal,
4272
4310
  fetch: this.config.fetch
4273
4311
  });
4312
+ let streamError;
4313
+ const safeResponse = withStreamErrorHandling(response, (err) => {
4314
+ streamError = err;
4315
+ });
4274
4316
  let finishReason = createFinishReason("other");
4275
4317
  const usage = {
4276
4318
  inputTokens: {
@@ -4290,7 +4332,7 @@ var OpenRouterCompletionLanguageModel = class {
4290
4332
  let provider;
4291
4333
  let rawUsage;
4292
4334
  return {
4293
- stream: response.pipeThrough(
4335
+ stream: safeResponse.pipeThrough(
4294
4336
  new TransformStream({
4295
4337
  transform(chunk, controller) {
4296
4338
  var _a16, _b16, _c, _d, _e;
@@ -4354,6 +4396,10 @@ var OpenRouterCompletionLanguageModel = class {
4354
4396
  }
4355
4397
  },
4356
4398
  flush(controller) {
4399
+ if (streamError != null) {
4400
+ finishReason = createFinishReason("error");
4401
+ controller.enqueue({ type: "error", error: streamError });
4402
+ }
4357
4403
  usage.raw = rawUsage;
4358
4404
  const openrouterMetadata = {
4359
4405
  usage: openrouterUsage
@@ -4467,17 +4513,19 @@ var OpenRouter = class {
4467
4513
  this.apiKey = options.apiKey;
4468
4514
  this.headers = options.headers;
4469
4515
  this.api_keys = options.api_keys;
4516
+ this.appName = options.appName;
4517
+ this.appUrl = options.appUrl;
4470
4518
  }
4471
4519
  get baseConfig() {
4472
4520
  return {
4473
4521
  baseURL: this.baseURL,
4474
- headers: () => __spreadValues(__spreadValues({
4522
+ headers: () => __spreadValues(__spreadValues(__spreadValues(__spreadValues({
4475
4523
  Authorization: `Bearer ${loadApiKey({
4476
4524
  apiKey: this.apiKey,
4477
4525
  environmentVariableName: "OPENROUTER_API_KEY",
4478
4526
  description: "OpenRouter"
4479
4527
  })}`
4480
- }, this.headers), this.api_keys && Object.keys(this.api_keys).length > 0 && {
4528
+ }, this.appName && { "X-OpenRouter-Title": this.appName }), this.appUrl && { "HTTP-Referer": this.appUrl }), this.headers), this.api_keys && Object.keys(this.api_keys).length > 0 && {
4481
4529
  "X-Provider-API-Keys": JSON.stringify(this.api_keys)
4482
4530
  })
4483
4531
  };
@@ -4714,7 +4762,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4714
4762
  }
4715
4763
 
4716
4764
  // src/version.ts
4717
- var VERSION2 = false ? "0.0.0-test" : "2.3.3";
4765
+ var VERSION2 = false ? "0.0.0-test" : "2.4.0";
4718
4766
 
4719
4767
  // src/provider.ts
4720
4768
  function createOpenRouter(options = {}) {
@@ -4722,13 +4770,13 @@ function createOpenRouter(options = {}) {
4722
4770
  const baseURL = (_b16 = withoutTrailingSlash((_a16 = options.baseURL) != null ? _a16 : options.baseUrl)) != null ? _b16 : "https://openrouter.ai/api/v1";
4723
4771
  const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
4724
4772
  const getHeaders = () => withUserAgentSuffix2(
4725
- __spreadValues(__spreadValues({
4773
+ __spreadValues(__spreadValues(__spreadValues(__spreadValues({
4726
4774
  Authorization: `Bearer ${loadApiKey({
4727
4775
  apiKey: options.apiKey,
4728
4776
  environmentVariableName: "OPENROUTER_API_KEY",
4729
4777
  description: "OpenRouter"
4730
4778
  })}`
4731
- }, options.headers), options.api_keys && Object.keys(options.api_keys).length > 0 && {
4779
+ }, options.appName && { "X-OpenRouter-Title": options.appName }), options.appUrl && { "HTTP-Referer": options.appUrl }), options.headers), options.api_keys && Object.keys(options.api_keys).length > 0 && {
4732
4780
  "X-Provider-API-Keys": JSON.stringify(options.api_keys)
4733
4781
  }),
4734
4782
  `ai-sdk/openrouter/${VERSION2}`