puckeditor-plugin-ai 0.6.5 → 0.6.6

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.js CHANGED
@@ -323,6 +323,23 @@ function filesToAttachedImages(files) {
323
323
  return results;
324
324
  });
325
325
  }
326
+ function mediaTypeFromDataUrl(dataUrl) {
327
+ if (!dataUrl.startsWith("data:")) return "application/octet-stream";
328
+ const rest = dataUrl.slice(5);
329
+ const semi = rest.indexOf(";");
330
+ const comma = rest.indexOf(",");
331
+ const end = semi === -1 ? comma : semi;
332
+ if (end <= 0) return "application/octet-stream";
333
+ return rest.slice(0, end) || "application/octet-stream";
334
+ }
335
+ function attachedImageToFileUIPart(img) {
336
+ return {
337
+ type: "file",
338
+ mediaType: mediaTypeFromDataUrl(img.dataUrl),
339
+ url: img.dataUrl,
340
+ filename: img.name
341
+ };
342
+ }
326
343
  var DEFAULT_AI_SETTINGS = {
327
344
  thinkingLevel: "none",
328
345
  urlContext: false,
@@ -1574,8 +1591,12 @@ function Chat({
1574
1591
  setError("");
1575
1592
  setPromptValue("");
1576
1593
  pendingSendImagesRef.current = attachedImages.map((img) => img.dataUrl);
1594
+ const fileParts = attachedImages.map(attachedImageToFileUIPart);
1577
1595
  setAttachedImages([]);
1578
- sendMessage({ text }).catch((e) => {
1596
+ const sendPayload = {};
1597
+ if (text) sendPayload.text = text;
1598
+ if (fileParts.length > 0) sendPayload.files = fileParts;
1599
+ sendMessage(sendPayload).catch((e) => {
1579
1600
  console.error(e);
1580
1601
  });
1581
1602
  };