@openrouter/ai-sdk-provider 0.4.5 → 0.5.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.
@@ -78,7 +78,7 @@ function convertToOpenRouterChatMessages(prompt) {
78
78
  const messageCacheControl = getCacheControl(providerMetadata);
79
79
  const contentParts = content.map(
80
80
  (part) => {
81
- var _a2, _b2, _c2, _d;
81
+ var _a2, _b2, _c2, _d, _e, _f;
82
82
  switch (part.type) {
83
83
  case "text":
84
84
  return {
@@ -100,9 +100,14 @@ function convertToOpenRouterChatMessages(prompt) {
100
100
  };
101
101
  case "file":
102
102
  return {
103
- type: "text",
104
- text: part.data instanceof URL ? part.data.toString() : part.data,
105
- cache_control: (_d = getCacheControl(part.providerMetadata)) != null ? _d : messageCacheControl
103
+ type: "file",
104
+ file: {
105
+ filename: String(
106
+ (_e = (_d = part.providerMetadata) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.filename
107
+ ),
108
+ file_data: part.data instanceof Uint8Array ? `data:${part.mimeType};base64,${convertUint8ArrayToBase64(part.data)}` : `data:${part.mimeType};base64,${part.data}`
109
+ },
110
+ cache_control: (_f = getCacheControl(part.providerMetadata)) != null ? _f : messageCacheControl
106
111
  };
107
112
  default: {
108
113
  const _exhaustiveCheck = part;
@@ -254,7 +259,7 @@ var OpenRouterChatLanguageModel = class {
254
259
  }) {
255
260
  var _a;
256
261
  const type = mode.type;
257
- const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata["openrouter"]) != null ? _a : {};
262
+ const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata.openrouter) != null ? _a : {};
258
263
  const baseArgs = __spreadValues(__spreadValues(__spreadValues({
259
264
  // model id:
260
265
  model: this.modelId,
@@ -279,7 +284,8 @@ var OpenRouterChatLanguageModel = class {
279
284
  messages: convertToOpenRouterChatMessages(prompt),
280
285
  // OpenRouter specific settings:
281
286
  include_reasoning: this.settings.includeReasoning,
282
- reasoning: this.settings.reasoning
287
+ reasoning: this.settings.reasoning,
288
+ usage: this.settings.usage
283
289
  }, this.config.extraBody), this.settings.extraBody), extraCallingBody);
284
290
  switch (type) {
285
291
  case "regular": {
@@ -315,7 +321,7 @@ var OpenRouterChatLanguageModel = class {
315
321
  }
316
322
  }
317
323
  async doGenerate(options) {
318
- var _b, _c, _d, _e, _f, _g, _h;
324
+ var _b, _c, _d, _e, _f, _g, _h, _i, _j;
319
325
  const args = this.getArgs(options);
320
326
  const { responseHeaders, value: response } = await postJsonToApi({
321
327
  url: this.config.url({
@@ -336,14 +342,39 @@ var OpenRouterChatLanguageModel = class {
336
342
  if (!choice) {
337
343
  throw new Error("No choice in response");
338
344
  }
339
- return {
345
+ const usageInfo = response.usage ? {
346
+ promptTokens: (_b = response.usage.prompt_tokens) != null ? _b : 0,
347
+ completionTokens: (_c = response.usage.completion_tokens) != null ? _c : 0
348
+ } : {
349
+ promptTokens: 0,
350
+ completionTokens: 0
351
+ };
352
+ const providerMetadata = {};
353
+ if (response.usage && ((_d = this.settings.usage) == null ? void 0 : _d.include)) {
354
+ providerMetadata.openrouter = {
355
+ usage: {
356
+ promptTokens: response.usage.prompt_tokens,
357
+ promptTokensDetails: response.usage.prompt_tokens_details ? {
358
+ cachedTokens: (_e = response.usage.prompt_tokens_details.cached_tokens) != null ? _e : 0
359
+ } : void 0,
360
+ completionTokens: response.usage.completion_tokens,
361
+ completionTokensDetails: response.usage.completion_tokens_details ? {
362
+ reasoningTokens: (_f = response.usage.completion_tokens_details.reasoning_tokens) != null ? _f : 0
363
+ } : void 0,
364
+ cost: response.usage.cost,
365
+ totalTokens: (_g = response.usage.total_tokens) != null ? _g : 0
366
+ }
367
+ };
368
+ }
369
+ const hasProviderMetadata = Object.keys(providerMetadata).length > 0;
370
+ return __spreadValues({
340
371
  response: {
341
372
  id: response.id,
342
373
  modelId: response.model
343
374
  },
344
- text: (_b = choice.message.content) != null ? _b : void 0,
345
- reasoning: (_c = choice.message.reasoning) != null ? _c : void 0,
346
- toolCalls: (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
375
+ text: (_h = choice.message.content) != null ? _h : void 0,
376
+ reasoning: (_i = choice.message.reasoning) != null ? _i : void 0,
377
+ toolCalls: (_j = choice.message.tool_calls) == null ? void 0 : _j.map((toolCall) => {
347
378
  var _a2;
348
379
  return {
349
380
  toolCallType: "function",
@@ -353,17 +384,15 @@ var OpenRouterChatLanguageModel = class {
353
384
  };
354
385
  }),
355
386
  finishReason: mapOpenRouterFinishReason(choice.finish_reason),
356
- usage: {
357
- promptTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : 0,
358
- completionTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0
359
- },
387
+ usage: usageInfo,
360
388
  rawCall: { rawPrompt, rawSettings },
361
389
  rawResponse: { headers: responseHeaders },
362
390
  warnings: [],
363
391
  logprobs: mapOpenRouterChatLogProbsOutput(choice.logprobs)
364
- };
392
+ }, hasProviderMetadata ? { providerMetadata } : {});
365
393
  }
366
394
  async doStream(options) {
395
+ var _a, _c;
367
396
  const args = this.getArgs(options);
368
397
  const { responseHeaders, value: response } = await postJsonToApi({
369
398
  url: this.config.url({
@@ -374,7 +403,9 @@ var OpenRouterChatLanguageModel = class {
374
403
  body: __spreadProps(__spreadValues({}, args), {
375
404
  stream: true,
376
405
  // only include stream_options when in strict compatibility mode:
377
- stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0
406
+ stream_options: this.config.compatibility === "strict" ? __spreadValues({
407
+ include_usage: true
408
+ }, ((_a = this.settings.usage) == null ? void 0 : _a.include) ? { include_usage: true } : {}) : void 0
378
409
  }),
379
410
  failedResponseHandler: openrouterFailedResponseHandler,
380
411
  successfulResponseHandler: createEventSourceResponseHandler(
@@ -383,7 +414,7 @@ var OpenRouterChatLanguageModel = class {
383
414
  abortSignal: options.abortSignal,
384
415
  fetch: this.config.fetch
385
416
  });
386
- const _a = args, { messages: rawPrompt } = _a, rawSettings = __objRest(_a, ["messages"]);
417
+ const _b = args, { messages: rawPrompt } = _b, rawSettings = __objRest(_b, ["messages"]);
387
418
  const toolCalls = [];
388
419
  let finishReason = "other";
389
420
  let usage = {
@@ -391,11 +422,13 @@ var OpenRouterChatLanguageModel = class {
391
422
  completionTokens: Number.NaN
392
423
  };
393
424
  let logprobs;
425
+ const openrouterUsage = {};
426
+ const shouldIncludeUsageAccounting = !!((_c = this.settings.usage) == null ? void 0 : _c.include);
394
427
  return {
395
428
  stream: response.pipeThrough(
396
429
  new TransformStream({
397
430
  transform(chunk, controller) {
398
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
431
+ var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
399
432
  if (!chunk.success) {
400
433
  finishReason = "error";
401
434
  controller.enqueue({ type: "error", error: chunk.error });
@@ -424,6 +457,20 @@ var OpenRouterChatLanguageModel = class {
424
457
  promptTokens: value.usage.prompt_tokens,
425
458
  completionTokens: value.usage.completion_tokens
426
459
  };
460
+ openrouterUsage.promptTokens = value.usage.prompt_tokens;
461
+ if (value.usage.prompt_tokens_details) {
462
+ openrouterUsage.promptTokensDetails = {
463
+ cachedTokens: (_a2 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a2 : 0
464
+ };
465
+ }
466
+ openrouterUsage.completionTokens = value.usage.completion_tokens;
467
+ if (value.usage.completion_tokens_details) {
468
+ openrouterUsage.completionTokensDetails = {
469
+ reasoningTokens: (_b2 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b2 : 0
470
+ };
471
+ }
472
+ openrouterUsage.cost = value.usage.cost;
473
+ openrouterUsage.totalTokens = value.usage.total_tokens;
427
474
  }
428
475
  const choice = value.choices[0];
429
476
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -468,7 +515,7 @@ var OpenRouterChatLanguageModel = class {
468
515
  message: `Expected 'id' to be a string.`
469
516
  });
470
517
  }
471
- if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
518
+ if (((_c2 = toolCallDelta.function) == null ? void 0 : _c2.name) == null) {
472
519
  throw new InvalidResponseDataError({
473
520
  data: toolCallDelta,
474
521
  message: `Expected 'function.name' to be a string.`
@@ -479,7 +526,7 @@ var OpenRouterChatLanguageModel = class {
479
526
  type: "function",
480
527
  function: {
481
528
  name: toolCallDelta.function.name,
482
- arguments: (_b = toolCallDelta.function.arguments) != null ? _b : ""
529
+ arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
483
530
  },
484
531
  sent: false
485
532
  };
@@ -487,7 +534,7 @@ var OpenRouterChatLanguageModel = class {
487
534
  if (toolCall2 == null) {
488
535
  throw new Error("Tool call is missing");
489
536
  }
490
- if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
537
+ if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
491
538
  controller.enqueue({
492
539
  type: "tool-call-delta",
493
540
  toolCallType: "function",
@@ -498,7 +545,7 @@ var OpenRouterChatLanguageModel = class {
498
545
  controller.enqueue({
499
546
  type: "tool-call",
500
547
  toolCallType: "function",
501
- toolCallId: (_e = toolCall2.id) != null ? _e : generateId(),
548
+ toolCallId: (_g = toolCall2.id) != null ? _g : generateId(),
502
549
  toolName: toolCall2.function.name,
503
550
  args: toolCall2.function.arguments
504
551
  });
@@ -510,21 +557,21 @@ var OpenRouterChatLanguageModel = class {
510
557
  if (toolCall == null) {
511
558
  throw new Error("Tool call is missing");
512
559
  }
513
- if (((_f = toolCallDelta.function) == null ? void 0 : _f.arguments) != null) {
514
- toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
560
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
561
+ toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
515
562
  }
516
563
  controller.enqueue({
517
564
  type: "tool-call-delta",
518
565
  toolCallType: "function",
519
566
  toolCallId: toolCall.id,
520
567
  toolName: toolCall.function.name,
521
- argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
568
+ argsTextDelta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
522
569
  });
523
- if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson(toolCall.function.arguments)) {
570
+ if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
524
571
  controller.enqueue({
525
572
  type: "tool-call",
526
573
  toolCallType: "function",
527
- toolCallId: (_l = toolCall.id) != null ? _l : generateId(),
574
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
528
575
  toolName: toolCall.function.name,
529
576
  args: toolCall.function.arguments
530
577
  });
@@ -550,12 +597,19 @@ var OpenRouterChatLanguageModel = class {
550
597
  }
551
598
  }
552
599
  }
553
- controller.enqueue({
600
+ const providerMetadata = {};
601
+ if (shouldIncludeUsageAccounting && (openrouterUsage.totalTokens !== void 0 || openrouterUsage.cost !== void 0 || openrouterUsage.promptTokensDetails !== void 0 || openrouterUsage.completionTokensDetails !== void 0)) {
602
+ providerMetadata.openrouter = {
603
+ usage: openrouterUsage
604
+ };
605
+ }
606
+ const hasProviderMetadata = Object.keys(providerMetadata).length > 0 && shouldIncludeUsageAccounting;
607
+ controller.enqueue(__spreadValues({
554
608
  type: "finish",
555
609
  finishReason,
556
610
  logprobs,
557
611
  usage
558
- });
612
+ }, hasProviderMetadata ? { providerMetadata } : {}));
559
613
  }
560
614
  })
561
615
  ),
@@ -570,8 +624,15 @@ var OpenRouterChatCompletionBaseResponseSchema = z2.object({
570
624
  model: z2.string().optional(),
571
625
  usage: z2.object({
572
626
  prompt_tokens: z2.number(),
627
+ prompt_tokens_details: z2.object({
628
+ cached_tokens: z2.number()
629
+ }).optional(),
573
630
  completion_tokens: z2.number(),
574
- total_tokens: z2.number()
631
+ completion_tokens_details: z2.object({
632
+ reasoning_tokens: z2.number()
633
+ }).optional(),
634
+ total_tokens: z2.number(),
635
+ cost: z2.number().optional()
575
636
  }).nullish()
576
637
  });
577
638
  var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
@@ -668,14 +729,13 @@ function prepareToolsAndToolChoice(mode) {
668
729
  parameters: tool.parameters
669
730
  }
670
731
  };
671
- } else {
672
- return {
673
- type: "function",
674
- function: {
675
- name: tool.name
676
- }
677
- };
678
732
  }
733
+ return {
734
+ type: "function",
735
+ function: {
736
+ name: tool.name
737
+ }
738
+ };
679
739
  });
680
740
  const toolChoice = mode.toolChoice;
681
741
  if (toolChoice == null) {
@@ -872,7 +932,7 @@ var OpenRouterCompletionLanguageModel = class {
872
932
  }) {
873
933
  var _a, _b;
874
934
  const type = mode.type;
875
- const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata["openrouter"]) != null ? _a : {};
935
+ const extraCallingBody = (_a = providerMetadata == null ? void 0 : providerMetadata.openrouter) != null ? _a : {};
876
936
  const { prompt: completionPrompt } = convertToOpenRouterCompletionPrompt({
877
937
  prompt,
878
938
  inputFormat