@blockrun/clawrouter 0.11.2 → 0.11.4

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/cli.js CHANGED
@@ -4784,6 +4784,27 @@ async function proxyPartnerRequest(req, res, apiBase, payFetch) {
4784
4784
  }).catch(() => {
4785
4785
  });
4786
4786
  }
4787
+ async function uploadDataUriToHost(dataUri) {
4788
+ const match = dataUri.match(/^data:(image\/\w+);base64,(.+)$/);
4789
+ if (!match) throw new Error("Invalid data URI format");
4790
+ const [, mimeType, b64Data] = match;
4791
+ const ext = mimeType === "image/jpeg" ? "jpg" : mimeType.split("/")[1] ?? "png";
4792
+ const buffer = Buffer.from(b64Data, "base64");
4793
+ const blob = new Blob([buffer], { type: mimeType });
4794
+ const form = new FormData();
4795
+ form.append("reqtype", "fileupload");
4796
+ form.append("fileToUpload", blob, `image.${ext}`);
4797
+ const resp = await fetch("https://catbox.moe/user/api.php", {
4798
+ method: "POST",
4799
+ body: form
4800
+ });
4801
+ if (!resp.ok) throw new Error(`catbox.moe upload failed: HTTP ${resp.status}`);
4802
+ const result = await resp.text();
4803
+ if (result.startsWith("https://")) {
4804
+ return result.trim();
4805
+ }
4806
+ throw new Error(`catbox.moe upload failed: ${result}`);
4807
+ }
4787
4808
  async function startProxy(options) {
4788
4809
  const apiBase = options.apiBase ?? BLOCKRUN_API;
4789
4810
  const listenPort = options.port ?? getProxyPort();
@@ -5401,7 +5422,19 @@ async function proxyRequest(req, res, apiBase, payFetch, options, routerOpts, de
5401
5422
  } else {
5402
5423
  const lines = [];
5403
5424
  for (const img of images) {
5404
- if (img.url) lines.push(img.url);
5425
+ if (img.url) {
5426
+ if (img.url.startsWith("data:")) {
5427
+ try {
5428
+ const hostedUrl = await uploadDataUriToHost(img.url);
5429
+ lines.push(hostedUrl);
5430
+ } catch (uploadErr) {
5431
+ console.error(`[ClawRouter] /imagegen: failed to upload data URI: ${uploadErr instanceof Error ? uploadErr.message : String(uploadErr)}`);
5432
+ lines.push("Image generated but upload failed. Try again or use --model dall-e-3.");
5433
+ }
5434
+ } else {
5435
+ lines.push(img.url);
5436
+ }
5437
+ }
5405
5438
  if (img.revised_prompt) lines.push(`Revised prompt: ${img.revised_prompt}`);
5406
5439
  }
5407
5440
  lines.push("", `Model: ${imageModel} | Size: ${imageSize}`);