@blockrun/clawrouter 0.11.2 → 0.11.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.js CHANGED
@@ -4934,6 +4934,27 @@ async function proxyPartnerRequest(req, res, apiBase, payFetch) {
4934
4934
  }).catch(() => {
4935
4935
  });
4936
4936
  }
4937
+ async function uploadDataUriToHost(dataUri) {
4938
+ const match = dataUri.match(/^data:(image\/\w+);base64,(.+)$/);
4939
+ if (!match) throw new Error("Invalid data URI format");
4940
+ const [, mimeType, b64Data] = match;
4941
+ const ext = mimeType === "image/jpeg" ? "jpg" : mimeType.split("/")[1] ?? "png";
4942
+ const buffer = Buffer.from(b64Data, "base64");
4943
+ const blob = new Blob([buffer], { type: mimeType });
4944
+ const form = new FormData();
4945
+ form.append("file", blob, `image.${ext}`);
4946
+ const resp = await fetch("https://telegra.ph/upload", {
4947
+ method: "POST",
4948
+ body: form
4949
+ });
4950
+ if (!resp.ok) throw new Error(`telegra.ph upload failed: HTTP ${resp.status}`);
4951
+ const result = await resp.json();
4952
+ if (Array.isArray(result) && result[0]?.src) {
4953
+ return `https://telegra.ph${result[0].src}`;
4954
+ }
4955
+ const errMsg = !Array.isArray(result) && result.error ? result.error : "unknown error";
4956
+ throw new Error(`telegra.ph upload failed: ${errMsg}`);
4957
+ }
4937
4958
  async function startProxy(options) {
4938
4959
  const apiBase = options.apiBase ?? BLOCKRUN_API;
4939
4960
  const listenPort = options.port ?? getProxyPort();
@@ -5551,7 +5572,19 @@ async function proxyRequest(req, res, apiBase, payFetch, options, routerOpts, de
5551
5572
  } else {
5552
5573
  const lines = [];
5553
5574
  for (const img of images) {
5554
- if (img.url) lines.push(img.url);
5575
+ if (img.url) {
5576
+ if (img.url.startsWith("data:")) {
5577
+ try {
5578
+ const hostedUrl = await uploadDataUriToHost(img.url);
5579
+ lines.push(hostedUrl);
5580
+ } catch (uploadErr) {
5581
+ console.error(`[ClawRouter] /imagegen: failed to upload data URI: ${uploadErr instanceof Error ? uploadErr.message : String(uploadErr)}`);
5582
+ lines.push("Image generated but upload failed. Try again or use --model dall-e-3.");
5583
+ }
5584
+ } else {
5585
+ lines.push(img.url);
5586
+ }
5587
+ }
5555
5588
  if (img.revised_prompt) lines.push(`Revised prompt: ${img.revised_prompt}`);
5556
5589
  }
5557
5590
  lines.push("", `Model: ${imageModel} | Size: ${imageSize}`);