@cognipeer/agent-sdk 0.8.2 → 0.8.3

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.cjs CHANGED
@@ -8600,6 +8600,78 @@ function resolveBudget(reasoning, effortBudget) {
8600
8600
  return void 0;
8601
8601
  }
8602
8602
 
8603
+ // src/providers/utils/media.ts
8604
+ var EXTENSION_MIME = {
8605
+ jpg: "image/jpeg",
8606
+ jpeg: "image/jpeg",
8607
+ png: "image/png",
8608
+ gif: "image/gif",
8609
+ webp: "image/webp",
8610
+ svg: "image/svg+xml",
8611
+ bmp: "image/bmp",
8612
+ tif: "image/tiff",
8613
+ tiff: "image/tiff",
8614
+ heic: "image/heic",
8615
+ heif: "image/heif",
8616
+ pdf: "application/pdf",
8617
+ csv: "text/csv",
8618
+ txt: "text/plain",
8619
+ md: "text/markdown",
8620
+ html: "text/html",
8621
+ doc: "application/msword",
8622
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
8623
+ xls: "application/vnd.ms-excel",
8624
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
8625
+ ppt: "application/vnd.ms-powerpoint",
8626
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
8627
+ mp3: "audio/mpeg",
8628
+ mpga: "audio/mpeg",
8629
+ wav: "audio/wav",
8630
+ ogg: "audio/ogg",
8631
+ flac: "audio/flac",
8632
+ aac: "audio/aac",
8633
+ m4a: "audio/mp4",
8634
+ webm: "audio/webm",
8635
+ mp4: "video/mp4",
8636
+ mov: "video/quicktime",
8637
+ avi: "video/x-msvideo"
8638
+ };
8639
+ function inferMediaTypeFromUrl(urlOrName) {
8640
+ if (!urlOrName) return void 0;
8641
+ const cleaned = urlOrName.split("?")[0].split("#")[0];
8642
+ const ext = cleaned.split(".").pop()?.toLowerCase();
8643
+ if (!ext || ext === cleaned.toLowerCase()) return void 0;
8644
+ return EXTENSION_MIME[ext];
8645
+ }
8646
+ function audioMimeToOpenAIFormat(mediaType) {
8647
+ const m = (mediaType ?? "").toLowerCase();
8648
+ if (m.includes("wav")) return "wav";
8649
+ return "mp3";
8650
+ }
8651
+ var BEDROCK_DOC_FORMATS = {
8652
+ "application/pdf": "pdf",
8653
+ "text/csv": "csv",
8654
+ "application/msword": "doc",
8655
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
8656
+ "application/vnd.ms-excel": "xls",
8657
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
8658
+ "text/html": "html",
8659
+ "text/plain": "txt",
8660
+ "text/markdown": "md"
8661
+ };
8662
+ function documentMimeToBedrockFormat(mediaType, fileName) {
8663
+ if (mediaType && BEDROCK_DOC_FORMATS[mediaType.toLowerCase()]) {
8664
+ return BEDROCK_DOC_FORMATS[mediaType.toLowerCase()];
8665
+ }
8666
+ const inferred = inferMediaTypeFromUrl(fileName);
8667
+ if (inferred && BEDROCK_DOC_FORMATS[inferred]) return BEDROCK_DOC_FORMATS[inferred];
8668
+ return void 0;
8669
+ }
8670
+ function sanitizeBedrockDocName(name, fallback = "document") {
8671
+ const cleaned = (name ?? "").replace(/\.[^.]+$/, "").replace(/[^a-zA-Z0-9\s\-()[\]]/g, " ").replace(/\s+/g, " ").trim();
8672
+ return cleaned || fallback;
8673
+ }
8674
+
8603
8675
  // src/providers/openai.ts
8604
8676
  var OpenAIProvider = class extends BaseProvider {
8605
8677
  constructor(config) {
@@ -8822,6 +8894,27 @@ var OpenAIProvider = class extends BaseProvider {
8822
8894
  const url = src.type === "url" ? src.url : `data:${src.mediaType};base64,${src.data}`;
8823
8895
  return { type: "input_image", image_url: url };
8824
8896
  }
8897
+ if (part.type === "file") {
8898
+ const src = part.source;
8899
+ if (src.type === "base64") {
8900
+ return {
8901
+ type: "input_file",
8902
+ filename: part.fileName ?? "file",
8903
+ file_data: `data:${src.mediaType};base64,${src.data}`
8904
+ };
8905
+ }
8906
+ return { type: "input_file", file_url: src.url };
8907
+ }
8908
+ if (part.type === "audio") {
8909
+ const src = part.source;
8910
+ if (src.type === "base64") {
8911
+ return {
8912
+ type: "input_audio",
8913
+ input_audio: { data: src.data, format: audioMimeToOpenAIFormat(src.mediaType) }
8914
+ };
8915
+ }
8916
+ return { type: textType, text: `[audio: ${src.url}]` };
8917
+ }
8825
8918
  return { type: textType, text: "" };
8826
8919
  });
8827
8920
  return { role, content };
@@ -8906,6 +8999,29 @@ var OpenAIProvider = class extends BaseProvider {
8906
8999
  const url = src.type === "url" ? src.url : `data:${src.mediaType};base64,${src.data}`;
8907
9000
  return { type: "image_url", image_url: { url } };
8908
9001
  }
9002
+ if (part.type === "file") {
9003
+ const src = part.source;
9004
+ if (src.type === "base64") {
9005
+ return {
9006
+ type: "file",
9007
+ file: {
9008
+ filename: part.fileName ?? "file",
9009
+ file_data: `data:${src.mediaType};base64,${src.data}`
9010
+ }
9011
+ };
9012
+ }
9013
+ return { type: "text", text: `[file: ${src.url}]` };
9014
+ }
9015
+ if (part.type === "audio") {
9016
+ const src = part.source;
9017
+ if (src.type === "base64") {
9018
+ return {
9019
+ type: "input_audio",
9020
+ input_audio: { data: src.data, format: audioMimeToOpenAIFormat(src.mediaType) }
9021
+ };
9022
+ }
9023
+ return { type: "text", text: `[audio: ${src.url}]` };
9024
+ }
8909
9025
  return part;
8910
9026
  });
8911
9027
  }
@@ -9303,6 +9419,22 @@ var AnthropicProvider = class extends BaseProvider {
9303
9419
  source: { type: "url", url: src.url }
9304
9420
  };
9305
9421
  }
9422
+ if (part.type === "file") {
9423
+ const src = part.source;
9424
+ const title = part.fileName ? { title: part.fileName } : {};
9425
+ if (src.type === "base64") {
9426
+ return {
9427
+ type: "document",
9428
+ source: { type: "base64", media_type: src.mediaType, data: src.data },
9429
+ ...title
9430
+ };
9431
+ }
9432
+ return { type: "document", source: { type: "url", url: src.url }, ...title };
9433
+ }
9434
+ if (part.type === "audio") {
9435
+ const src = part.source;
9436
+ return { type: "text", text: src.type === "url" ? `[audio: ${src.url}]` : "[audio attachment omitted: not supported by this model]" };
9437
+ }
9306
9438
  return part;
9307
9439
  });
9308
9440
  }
@@ -9770,6 +9902,24 @@ var BedrockProvider = class extends BaseProvider {
9770
9902
  }
9771
9903
  });
9772
9904
  }
9905
+ } else if (part.type === "file") {
9906
+ const src = part.source;
9907
+ const format = documentMimeToBedrockFormat(src.mediaType, part.fileName);
9908
+ if (src.type === "base64" && format) {
9909
+ content.push({
9910
+ document: {
9911
+ format,
9912
+ name: sanitizeBedrockDocName(part.fileName),
9913
+ source: { bytes: src.data }
9914
+ }
9915
+ });
9916
+ } else {
9917
+ const ref = src.type === "url" ? src.url : part.fileName ?? "attachment";
9918
+ content.push({ text: `[file: ${ref}]` });
9919
+ }
9920
+ } else if (part.type === "audio") {
9921
+ const src = part.source;
9922
+ content.push({ text: src.type === "url" ? `[audio: ${src.url}]` : "[audio attachment omitted: not supported by this model]" });
9773
9923
  }
9774
9924
  }
9775
9925
  }
@@ -10113,7 +10263,25 @@ var VertexProvider = class extends BaseProvider {
10113
10263
  });
10114
10264
  } else {
10115
10265
  parts.push({
10116
- fileData: { mimeType: "image/jpeg", fileUri: src.url }
10266
+ fileData: {
10267
+ mimeType: src.mediaType ?? inferMediaTypeFromUrl(src.url) ?? "image/jpeg",
10268
+ fileUri: src.url
10269
+ }
10270
+ });
10271
+ }
10272
+ } else if (part.type === "file" || part.type === "audio") {
10273
+ const src = part.source;
10274
+ const fallbackMime = part.type === "audio" ? "audio/mpeg" : "application/pdf";
10275
+ if (src.type === "base64") {
10276
+ parts.push({
10277
+ inlineData: { mimeType: src.mediaType ?? fallbackMime, data: src.data }
10278
+ });
10279
+ } else {
10280
+ parts.push({
10281
+ fileData: {
10282
+ mimeType: src.mediaType ?? inferMediaTypeFromUrl(src.url) ?? inferMediaTypeFromUrl(part.fileName) ?? fallbackMime,
10283
+ fileUri: src.url
10284
+ }
10117
10285
  });
10118
10286
  }
10119
10287
  }
@@ -10458,9 +10626,50 @@ function convertContent(content) {
10458
10626
  }
10459
10627
  }
10460
10628
  }
10629
+ if (part.type === "file" || part.type === "document") {
10630
+ const source = extractBinarySource(part);
10631
+ if (source) {
10632
+ const fileName = part.metadata?.filename ?? part.metadata?.fileName ?? part.fileName ?? part.filename ?? part.name ?? part.file?.filename;
10633
+ return { type: "file", source, ...fileName ? { fileName } : {} };
10634
+ }
10635
+ }
10636
+ if (part.type === "audio" || part.type === "input_audio") {
10637
+ const ia = part.input_audio;
10638
+ if (ia?.data) {
10639
+ const mediaType = ia.format === "wav" ? "audio/wav" : "audio/mpeg";
10640
+ return { type: "audio", source: { type: "base64", mediaType, data: ia.data } };
10641
+ }
10642
+ const source = extractBinarySource(part);
10643
+ if (source) return { type: "audio", source };
10644
+ }
10461
10645
  return { type: "text", text: part.text ?? part.content ?? JSON.stringify(part) };
10462
10646
  });
10463
10647
  }
10648
+ function extractBinarySource(part) {
10649
+ const src = part.source;
10650
+ if (src?.type === "base64" && src.data) {
10651
+ return { type: "base64", mediaType: src.mediaType ?? src.media_type ?? src.mime_type ?? "application/octet-stream", data: src.data };
10652
+ }
10653
+ if (src?.type === "url" && src.url) {
10654
+ const mediaType2 = src.mediaType ?? src.media_type ?? src.mime_type;
10655
+ return { type: "url", url: src.url, ...mediaType2 ? { mediaType: mediaType2 } : {} };
10656
+ }
10657
+ const mediaType = part.mime_type ?? part.mimeType ?? part.media_type ?? part.mediaType;
10658
+ if (part.source_type === "base64" && part.data) {
10659
+ return { type: "base64", mediaType: mediaType ?? "application/octet-stream", data: part.data };
10660
+ }
10661
+ if (part.source_type === "url" && part.url) {
10662
+ return { type: "url", url: part.url, ...mediaType ? { mediaType } : {} };
10663
+ }
10664
+ const value = part.data ?? part.url ?? part.file?.file_data;
10665
+ if (typeof value === "string" && value) {
10666
+ const match = value.match(/^data:([^;]+);base64,(.+)$/);
10667
+ if (match) return { type: "base64", mediaType: mediaType ?? match[1], data: match[2] };
10668
+ if (/^https?:\/\//.test(value)) return { type: "url", url: value, ...mediaType ? { mediaType } : {} };
10669
+ if (mediaType) return { type: "base64", mediaType, data: value };
10670
+ }
10671
+ return void 0;
10672
+ }
10464
10673
  function isZodSchema(obj) {
10465
10674
  return obj != null && typeof obj === "object" && typeof obj.safeParse === "function" && "_def" in obj;
10466
10675
  }