@openrouter/ai-sdk-provider 1.2.5 → 1.2.7

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.
@@ -1051,6 +1051,49 @@ function getBase64FromDataUrl(dataUrl) {
1051
1051
  const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
1052
1052
  return match ? match[1] : dataUrl;
1053
1053
  }
1054
+ function getInputAudioData(part) {
1055
+ const fileData = getFileUrl({
1056
+ part,
1057
+ defaultMediaType: "audio/mpeg"
1058
+ });
1059
+ if (isUrl({
1060
+ url: fileData,
1061
+ protocols: /* @__PURE__ */ new Set(["http:", "https:"])
1062
+ })) {
1063
+ throw new Error(
1064
+ `Audio files cannot be provided as URLs.
1065
+
1066
+ OpenRouter requires audio to be base64-encoded. Please:
1067
+ 1. Download the audio file locally
1068
+ 2. Read it as a Buffer or Uint8Array
1069
+ 3. Pass it as the data parameter
1070
+
1071
+ The AI SDK will automatically handle base64 encoding.
1072
+
1073
+ Learn more: https://openrouter.ai/docs/features/multimodal/audio`
1074
+ );
1075
+ }
1076
+ const data = getBase64FromDataUrl(fileData);
1077
+ const mediaType = part.mediaType || "audio/mpeg";
1078
+ let format = mediaType.replace("audio/", "");
1079
+ if (format === "mpeg" || format === "mp3") {
1080
+ format = "mp3";
1081
+ } else if (format === "x-wav" || format === "wave" || format === "wav") {
1082
+ format = "wav";
1083
+ }
1084
+ if (format !== "mp3" && format !== "wav") {
1085
+ throw new Error(
1086
+ `Unsupported audio format: "${mediaType}"
1087
+
1088
+ OpenRouter only supports MP3 and WAV audio formats.
1089
+ \u2022 For MP3: use "audio/mpeg" or "audio/mp3"
1090
+ \u2022 For WAV: use "audio/wav" or "audio/x-wav"
1091
+
1092
+ Learn more: https://openrouter.ai/docs/features/multimodal/audio`
1093
+ );
1094
+ }
1095
+ return { data, format };
1096
+ }
1054
1097
 
1055
1098
  // src/chat/convert-to-openrouter-chat-messages.ts
1056
1099
  function getCacheControl(providerMetadata) {
@@ -1092,7 +1135,7 @@ function convertToOpenRouterChatMessages(prompt) {
1092
1135
  const messageCacheControl = getCacheControl(providerOptions);
1093
1136
  const contentParts = content.map(
1094
1137
  (part) => {
1095
- var _a16, _b2, _c2, _d2, _e2, _f2;
1138
+ var _a16, _b2, _c2, _d2, _e2, _f2, _g;
1096
1139
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
1097
1140
  switch (part.type) {
1098
1141
  case "text":
@@ -1117,8 +1160,15 @@ function convertToOpenRouterChatMessages(prompt) {
1117
1160
  cache_control: cacheControl
1118
1161
  };
1119
1162
  }
1163
+ if ((_c2 = part.mediaType) == null ? void 0 : _c2.startsWith("audio/")) {
1164
+ return {
1165
+ type: "input_audio",
1166
+ input_audio: getInputAudioData(part),
1167
+ cache_control: cacheControl
1168
+ };
1169
+ }
1120
1170
  const fileName = String(
1121
- (_f2 = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f2 : ""
1171
+ (_g = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g : ""
1122
1172
  );
1123
1173
  const fileData = getFileUrl({
1124
1174
  part,
@@ -1359,7 +1409,7 @@ var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
1359
1409
  content: import_v46.z.array(
1360
1410
  import_v46.z.object({
1361
1411
  type: import_v46.z.string(),
1362
- text: import_v46.z.string()
1412
+ text: import_v46.z.string().optional()
1363
1413
  }).passthrough()
1364
1414
  ).optional()
1365
1415
  }).passthrough()
@@ -1442,7 +1492,7 @@ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
1442
1492
  content: import_v46.z.array(
1443
1493
  import_v46.z.object({
1444
1494
  type: import_v46.z.string(),
1445
- text: import_v46.z.string()
1495
+ text: import_v46.z.string().optional()
1446
1496
  }).passthrough()
1447
1497
  ).optional()
1448
1498
  }).passthrough()