@openrouter/ai-sdk-provider 1.4.1 → 1.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.
package/dist/index.d.mts CHANGED
@@ -2,6 +2,8 @@ import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, La
2
2
  export { LanguageModelV2, LanguageModelV2Prompt } from '@ai-sdk/provider';
3
3
  import * as models from '@openrouter/sdk/models';
4
4
  import { z } from 'zod/v4';
5
+ import { EncodeOptions, DecodeOptions, JsonValue } from '@toon-format/toon';
6
+ export { DecodeOptions, EncodeOptions, JsonValue } from '@toon-format/toon';
5
7
 
6
8
  type OpenRouterChatModelId = string;
7
9
  type OpenRouterChatSettings = {
@@ -549,4 +551,84 @@ declare class OpenRouter {
549
551
  embedding(modelId: OpenRouterEmbeddingModelId, settings?: OpenRouterEmbeddingSettings): OpenRouterEmbeddingModel;
550
552
  }
551
553
 
552
- export { OpenRouter, type OpenRouterCompletionSettings, type OpenRouterEmbeddingModelId, type OpenRouterEmbeddingSettings, type OpenRouterProvider, type OpenRouterProviderOptions, type OpenRouterProviderSettings, type OpenRouterSharedSettings, type OpenRouterUsageAccounting, createOpenRouter, openrouter };
554
+ /**
555
+ * TOON (Token-Oriented Object Notation) helper utilities for token-efficient
556
+ * data encoding in LLM prompts.
557
+ *
558
+ * TOON achieves ~40% token reduction vs JSON for tabular data while maintaining
559
+ * high LLM comprehension accuracy.
560
+ *
561
+ * @see https://toonformat.dev
562
+ * @see https://github.com/toon-format/toon
563
+ *
564
+ * @example
565
+ * ```ts
566
+ * import { encodeToon, decodeToon } from '@openrouter/ai-sdk-provider';
567
+ *
568
+ * // Encode data to TOON format
569
+ * const toon = await encodeToon([
570
+ * { id: 1, name: 'Alice', score: 95 },
571
+ * { id: 2, name: 'Bob', score: 87 },
572
+ * ]);
573
+ * // Result: [2]{id,name,score}: 1,Alice,95 2,Bob,87
574
+ *
575
+ * // Decode TOON back to JSON
576
+ * const data = await decodeToon(toon);
577
+ * ```
578
+ */
579
+
580
+ type ToonEncodeOptions = EncodeOptions;
581
+ type ToonDecodeOptions = DecodeOptions;
582
+ /**
583
+ * Encodes a JavaScript value into TOON format string.
584
+ *
585
+ * TOON is particularly efficient for uniform arrays of objects (tabular data),
586
+ * achieving CSV-like compactness while preserving explicit structure.
587
+ *
588
+ * @param value - Any JavaScript value (objects, arrays, primitives)
589
+ * @param options - Optional encoding configuration
590
+ * @returns Promise resolving to TOON formatted string
591
+ *
592
+ * @example
593
+ * ```ts
594
+ * // Simple object
595
+ * await encodeToon({ name: 'Alice', age: 30 });
596
+ * // name: Alice
597
+ * // age: 30
598
+ *
599
+ * // Tabular array (most efficient)
600
+ * await encodeToon([
601
+ * { id: 1, name: 'Alice' },
602
+ * { id: 2, name: 'Bob' },
603
+ * ]);
604
+ * // [2]{id,name}: 1,Alice 2,Bob
605
+ *
606
+ * // With options
607
+ * await encodeToon(data, { indent: 4, keyFolding: 'safe' });
608
+ * ```
609
+ */
610
+ declare function encodeToon(value: unknown, options?: ToonEncodeOptions): Promise<string>;
611
+ /**
612
+ * Decodes a TOON format string into a JavaScript value.
613
+ *
614
+ * @param input - TOON formatted string
615
+ * @param options - Optional decoding configuration
616
+ * @returns Promise resolving to parsed JavaScript value
617
+ *
618
+ * @example
619
+ * ```ts
620
+ * // Decode simple object
621
+ * await decodeToon('name: Alice\nage: 30');
622
+ * // { name: 'Alice', age: 30 }
623
+ *
624
+ * // Decode tabular array
625
+ * await decodeToon('[2]{id,name}: 1,Alice 2,Bob');
626
+ * // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
627
+ *
628
+ * // With options
629
+ * await decodeToon(toonString, { strict: false, expandPaths: 'safe' });
630
+ * ```
631
+ */
632
+ declare function decodeToon(input: string, options?: ToonDecodeOptions): Promise<JsonValue>;
633
+
634
+ export { OpenRouter, type OpenRouterChatSettings, type OpenRouterCompletionSettings, type OpenRouterEmbeddingModelId, type OpenRouterEmbeddingSettings, type OpenRouterProvider, type OpenRouterProviderOptions, type OpenRouterProviderSettings, type OpenRouterSharedSettings, type OpenRouterUsageAccounting, type ToonDecodeOptions, type ToonEncodeOptions, createOpenRouter, decodeToon, encodeToon, openrouter };
package/dist/index.d.ts CHANGED
@@ -2,6 +2,8 @@ import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, La
2
2
  export { LanguageModelV2, LanguageModelV2Prompt } from '@ai-sdk/provider';
3
3
  import * as models from '@openrouter/sdk/models';
4
4
  import { z } from 'zod/v4';
5
+ import { EncodeOptions, DecodeOptions, JsonValue } from '@toon-format/toon';
6
+ export { DecodeOptions, EncodeOptions, JsonValue } from '@toon-format/toon';
5
7
 
6
8
  type OpenRouterChatModelId = string;
7
9
  type OpenRouterChatSettings = {
@@ -549,4 +551,84 @@ declare class OpenRouter {
549
551
  embedding(modelId: OpenRouterEmbeddingModelId, settings?: OpenRouterEmbeddingSettings): OpenRouterEmbeddingModel;
550
552
  }
551
553
 
552
- export { OpenRouter, type OpenRouterCompletionSettings, type OpenRouterEmbeddingModelId, type OpenRouterEmbeddingSettings, type OpenRouterProvider, type OpenRouterProviderOptions, type OpenRouterProviderSettings, type OpenRouterSharedSettings, type OpenRouterUsageAccounting, createOpenRouter, openrouter };
554
+ /**
555
+ * TOON (Token-Oriented Object Notation) helper utilities for token-efficient
556
+ * data encoding in LLM prompts.
557
+ *
558
+ * TOON achieves ~40% token reduction vs JSON for tabular data while maintaining
559
+ * high LLM comprehension accuracy.
560
+ *
561
+ * @see https://toonformat.dev
562
+ * @see https://github.com/toon-format/toon
563
+ *
564
+ * @example
565
+ * ```ts
566
+ * import { encodeToon, decodeToon } from '@openrouter/ai-sdk-provider';
567
+ *
568
+ * // Encode data to TOON format
569
+ * const toon = await encodeToon([
570
+ * { id: 1, name: 'Alice', score: 95 },
571
+ * { id: 2, name: 'Bob', score: 87 },
572
+ * ]);
573
+ * // Result: [2]{id,name,score}: 1,Alice,95 2,Bob,87
574
+ *
575
+ * // Decode TOON back to JSON
576
+ * const data = await decodeToon(toon);
577
+ * ```
578
+ */
579
+
580
+ type ToonEncodeOptions = EncodeOptions;
581
+ type ToonDecodeOptions = DecodeOptions;
582
+ /**
583
+ * Encodes a JavaScript value into TOON format string.
584
+ *
585
+ * TOON is particularly efficient for uniform arrays of objects (tabular data),
586
+ * achieving CSV-like compactness while preserving explicit structure.
587
+ *
588
+ * @param value - Any JavaScript value (objects, arrays, primitives)
589
+ * @param options - Optional encoding configuration
590
+ * @returns Promise resolving to TOON formatted string
591
+ *
592
+ * @example
593
+ * ```ts
594
+ * // Simple object
595
+ * await encodeToon({ name: 'Alice', age: 30 });
596
+ * // name: Alice
597
+ * // age: 30
598
+ *
599
+ * // Tabular array (most efficient)
600
+ * await encodeToon([
601
+ * { id: 1, name: 'Alice' },
602
+ * { id: 2, name: 'Bob' },
603
+ * ]);
604
+ * // [2]{id,name}: 1,Alice 2,Bob
605
+ *
606
+ * // With options
607
+ * await encodeToon(data, { indent: 4, keyFolding: 'safe' });
608
+ * ```
609
+ */
610
+ declare function encodeToon(value: unknown, options?: ToonEncodeOptions): Promise<string>;
611
+ /**
612
+ * Decodes a TOON format string into a JavaScript value.
613
+ *
614
+ * @param input - TOON formatted string
615
+ * @param options - Optional decoding configuration
616
+ * @returns Promise resolving to parsed JavaScript value
617
+ *
618
+ * @example
619
+ * ```ts
620
+ * // Decode simple object
621
+ * await decodeToon('name: Alice\nage: 30');
622
+ * // { name: 'Alice', age: 30 }
623
+ *
624
+ * // Decode tabular array
625
+ * await decodeToon('[2]{id,name}: 1,Alice 2,Bob');
626
+ * // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
627
+ *
628
+ * // With options
629
+ * await decodeToon(toonString, { strict: false, expandPaths: 'safe' });
630
+ * ```
631
+ */
632
+ declare function decodeToon(input: string, options?: ToonDecodeOptions): Promise<JsonValue>;
633
+
634
+ export { OpenRouter, type OpenRouterChatSettings, type OpenRouterCompletionSettings, type OpenRouterEmbeddingModelId, type OpenRouterEmbeddingSettings, type OpenRouterProvider, type OpenRouterProviderOptions, type OpenRouterProviderSettings, type OpenRouterSharedSettings, type OpenRouterUsageAccounting, type ToonDecodeOptions, type ToonEncodeOptions, createOpenRouter, decodeToon, encodeToon, openrouter };
package/dist/index.js CHANGED
@@ -49,6 +49,8 @@ var index_exports = {};
49
49
  __export(index_exports, {
50
50
  OpenRouter: () => OpenRouter,
51
51
  createOpenRouter: () => createOpenRouter,
52
+ decodeToon: () => decodeToon,
53
+ encodeToon: () => encodeToon,
52
54
  openrouter: () => openrouter
53
55
  });
54
56
  module.exports = __toCommonJS(index_exports);
@@ -1071,6 +1073,19 @@ function mapOpenRouterFinishReason(finishReason) {
1071
1073
  }
1072
1074
  }
1073
1075
 
1076
+ // src/types/openrouter-chat-completions-input.ts
1077
+ var OPENROUTER_AUDIO_FORMATS = [
1078
+ "wav",
1079
+ "mp3",
1080
+ "aiff",
1081
+ "aac",
1082
+ "ogg",
1083
+ "flac",
1084
+ "m4a",
1085
+ "pcm16",
1086
+ "pcm24"
1087
+ ];
1088
+
1074
1089
  // src/chat/is-url.ts
1075
1090
  function isUrl({
1076
1091
  url,
@@ -1112,6 +1127,34 @@ function getBase64FromDataUrl(dataUrl) {
1112
1127
  const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
1113
1128
  return match ? match[1] : dataUrl;
1114
1129
  }
1130
+ var MIME_TO_FORMAT = {
1131
+ // MP3 variants
1132
+ mpeg: "mp3",
1133
+ mp3: "mp3",
1134
+ // WAV variants
1135
+ "x-wav": "wav",
1136
+ wave: "wav",
1137
+ wav: "wav",
1138
+ // OGG variants
1139
+ ogg: "ogg",
1140
+ vorbis: "ogg",
1141
+ // AAC variants
1142
+ aac: "aac",
1143
+ "x-aac": "aac",
1144
+ // M4A variants
1145
+ m4a: "m4a",
1146
+ "x-m4a": "m4a",
1147
+ mp4: "m4a",
1148
+ // AIFF variants
1149
+ aiff: "aiff",
1150
+ "x-aiff": "aiff",
1151
+ // FLAC
1152
+ flac: "flac",
1153
+ "x-flac": "flac",
1154
+ // PCM variants
1155
+ pcm16: "pcm16",
1156
+ pcm24: "pcm24"
1157
+ };
1115
1158
  function getInputAudioData(part) {
1116
1159
  const fileData = getFileUrl({
1117
1160
  part,
@@ -1136,19 +1179,14 @@ Learn more: https://openrouter.ai/docs/features/multimodal/audio`
1136
1179
  }
1137
1180
  const data = getBase64FromDataUrl(fileData);
1138
1181
  const mediaType = part.mediaType || "audio/mpeg";
1139
- let format = mediaType.replace("audio/", "");
1140
- if (format === "mpeg" || format === "mp3") {
1141
- format = "mp3";
1142
- } else if (format === "x-wav" || format === "wave" || format === "wav") {
1143
- format = "wav";
1144
- }
1145
- if (format !== "mp3" && format !== "wav") {
1182
+ const rawFormat = mediaType.replace("audio/", "");
1183
+ const format = MIME_TO_FORMAT[rawFormat];
1184
+ if (format === void 0) {
1185
+ const supportedList = OPENROUTER_AUDIO_FORMATS.join(", ");
1146
1186
  throw new Error(
1147
1187
  `Unsupported audio format: "${mediaType}"
1148
1188
 
1149
- OpenRouter only supports MP3 and WAV audio formats.
1150
- \u2022 For MP3: use "audio/mpeg" or "audio/mp3"
1151
- \u2022 For WAV: use "audio/wav" or "audio/x-wav"
1189
+ OpenRouter supports the following audio formats: ${supportedList}
1152
1190
 
1153
1191
  Learn more: https://openrouter.ai/docs/features/multimodal/audio`
1154
1192
  );
@@ -2819,7 +2857,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
2819
2857
  }
2820
2858
 
2821
2859
  // src/version.ts
2822
- var VERSION = false ? "0.0.0-test" : "1.4.1";
2860
+ var VERSION = false ? "0.0.0-test" : "1.5.0";
2823
2861
 
2824
2862
  // src/provider.ts
2825
2863
  function createOpenRouter(options = {}) {
@@ -2887,10 +2925,31 @@ var openrouter = createOpenRouter({
2887
2925
  compatibility: "strict"
2888
2926
  // strict for OpenRouter API
2889
2927
  });
2928
+
2929
+ // src/toon/index.ts
2930
+ async function getToonModule() {
2931
+ try {
2932
+ return await import("@toon-format/toon");
2933
+ } catch (e) {
2934
+ throw new Error(
2935
+ "The @toon-format/toon package is required for TOON encoding/decoding. Install it with: npm install @toon-format/toon"
2936
+ );
2937
+ }
2938
+ }
2939
+ async function encodeToon(value, options) {
2940
+ const toon = await getToonModule();
2941
+ return toon.encode(value, options);
2942
+ }
2943
+ async function decodeToon(input, options) {
2944
+ const toon = await getToonModule();
2945
+ return toon.decode(input, options);
2946
+ }
2890
2947
  // Annotate the CommonJS export names for ESM import in node:
2891
2948
  0 && (module.exports = {
2892
2949
  OpenRouter,
2893
2950
  createOpenRouter,
2951
+ decodeToon,
2952
+ encodeToon,
2894
2953
  openrouter
2895
2954
  });
2896
2955
  //# sourceMappingURL=index.js.map