@blockrun/clawrouter 0.12.24 → 0.12.26

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
@@ -1637,6 +1637,9 @@ var blockrunProvider = {
1637
1637
  // src/proxy.ts
1638
1638
  import { createServer } from "http";
1639
1639
  import { finished } from "stream";
1640
+ import { homedir as homedir4 } from "os";
1641
+ import { join as join5 } from "path";
1642
+ import { mkdir as mkdir3, writeFile as writeFile2, readFile, stat as fsStat } from "fs/promises";
1640
1643
  import { createPublicClient as createPublicClient2, http as http2 } from "viem";
1641
1644
  import { base as base2 } from "viem/chains";
1642
1645
  import { privateKeyToAccount as privateKeyToAccount3 } from "viem/accounts";
@@ -5475,6 +5478,7 @@ ${lines.join("\n")}`;
5475
5478
  // src/proxy.ts
5476
5479
  var BLOCKRUN_API = "https://blockrun.ai/api";
5477
5480
  var BLOCKRUN_SOLANA_API = "https://sol.blockrun.ai/api";
5481
+ var IMAGE_DIR = join5(homedir4(), ".openclaw", "blockrun", "images");
5478
5482
  var AUTO_MODEL = "blockrun/auto";
5479
5483
  var ROUTING_PROFILES = /* @__PURE__ */ new Set([
5480
5484
  "blockrun/free",
@@ -6256,6 +6260,81 @@ async function startProxy(options) {
6256
6260
  res.end(JSON.stringify({ object: "list", data: models }));
6257
6261
  return;
6258
6262
  }
6263
+ if (req.url?.startsWith("/images/") && req.method === "GET") {
6264
+ const filename = req.url.slice("/images/".length).split("?")[0].replace(/[^a-zA-Z0-9._-]/g, "");
6265
+ if (!filename) {
6266
+ res.writeHead(400);
6267
+ res.end("Bad request");
6268
+ return;
6269
+ }
6270
+ const filePath = join5(IMAGE_DIR, filename);
6271
+ try {
6272
+ const s = await fsStat(filePath);
6273
+ if (!s.isFile()) throw new Error("not a file");
6274
+ const ext = filename.split(".").pop()?.toLowerCase() ?? "png";
6275
+ const mime = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", webp: "image/webp", gif: "image/gif" };
6276
+ const data = await readFile(filePath);
6277
+ res.writeHead(200, { "Content-Type": mime[ext] ?? "application/octet-stream", "Content-Length": data.length });
6278
+ res.end(data);
6279
+ } catch {
6280
+ res.writeHead(404, { "Content-Type": "application/json" });
6281
+ res.end(JSON.stringify({ error: "Image not found" }));
6282
+ }
6283
+ return;
6284
+ }
6285
+ if (req.url === "/v1/images/generations" && req.method === "POST") {
6286
+ const chunks = [];
6287
+ for await (const chunk of req) {
6288
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
6289
+ }
6290
+ const reqBody = Buffer.concat(chunks);
6291
+ try {
6292
+ const upstream = await payFetch(`${apiBase}/v1/images/generations`, {
6293
+ method: "POST",
6294
+ headers: { "content-type": "application/json", "user-agent": USER_AGENT },
6295
+ body: reqBody
6296
+ });
6297
+ const text = await upstream.text();
6298
+ if (!upstream.ok) {
6299
+ res.writeHead(upstream.status, { "Content-Type": "application/json" });
6300
+ res.end(text);
6301
+ return;
6302
+ }
6303
+ let result;
6304
+ try {
6305
+ result = JSON.parse(text);
6306
+ } catch {
6307
+ res.writeHead(200, { "Content-Type": "application/json" });
6308
+ res.end(text);
6309
+ return;
6310
+ }
6311
+ if (result.data?.length) {
6312
+ await mkdir3(IMAGE_DIR, { recursive: true });
6313
+ const port2 = server.address()?.port ?? 8402;
6314
+ for (const img of result.data) {
6315
+ const m = img.url?.match(/^data:(image\/\w+);base64,(.+)$/);
6316
+ if (m) {
6317
+ const [, mimeType, b64] = m;
6318
+ const ext = mimeType === "image/jpeg" ? "jpg" : mimeType.split("/")[1] ?? "png";
6319
+ const filename = `${Date.now()}-${Math.random().toString(36).slice(2, 10)}.${ext}`;
6320
+ await writeFile2(join5(IMAGE_DIR, filename), Buffer.from(b64, "base64"));
6321
+ img.url = `http://localhost:${port2}/images/${filename}`;
6322
+ console.log(`[ClawRouter] Image saved \u2192 ${img.url}`);
6323
+ }
6324
+ }
6325
+ }
6326
+ res.writeHead(200, { "Content-Type": "application/json" });
6327
+ res.end(JSON.stringify(result));
6328
+ } catch (err) {
6329
+ const msg = err instanceof Error ? err.message : String(err);
6330
+ console.error(`[ClawRouter] Image generation error: ${msg}`);
6331
+ if (!res.headersSent) {
6332
+ res.writeHead(502, { "Content-Type": "application/json" });
6333
+ res.end(JSON.stringify({ error: "Image generation failed", details: msg }));
6334
+ }
6335
+ }
6336
+ return;
6337
+ }
6259
6338
  if (req.url?.match(/^\/v1\/(?:x|partner)\//)) {
6260
6339
  try {
6261
6340
  await proxyPartnerRequest(req, res, apiBase, payFetch);
@@ -7665,8 +7744,8 @@ import {
7665
7744
  copyFileSync,
7666
7745
  renameSync
7667
7746
  } from "fs";
7668
- import { homedir as homedir5 } from "os";
7669
- import { join as join6 } from "path";
7747
+ import { homedir as homedir6 } from "os";
7748
+ import { join as join7 } from "path";
7670
7749
  import { privateKeyToAccount as privateKeyToAccount4 } from "viem/accounts";
7671
7750
 
7672
7751
  // src/partners/registry.ts
@@ -7770,8 +7849,8 @@ init_solana_balance();
7770
7849
  // src/spend-control.ts
7771
7850
  import * as fs from "fs";
7772
7851
  import * as path from "path";
7773
- import { homedir as homedir4 } from "os";
7774
- var WALLET_DIR2 = path.join(homedir4(), ".openclaw", "blockrun");
7852
+ import { homedir as homedir5 } from "os";
7853
+ var WALLET_DIR2 = path.join(homedir5(), ".openclaw", "blockrun");
7775
7854
  var HOUR_MS = 60 * 60 * 1e3;
7776
7855
  var DAY_MS = 24 * HOUR_MS;
7777
7856
  var FileSpendControlStorage = class {
@@ -8097,8 +8176,8 @@ function isGatewayMode() {
8097
8176
  return args.includes("gateway");
8098
8177
  }
8099
8178
  function injectModelsConfig(logger) {
8100
- const configDir = join6(homedir5(), ".openclaw");
8101
- const configPath = join6(configDir, "openclaw.json");
8179
+ const configDir = join7(homedir6(), ".openclaw");
8180
+ const configPath = join7(configDir, "openclaw.json");
8102
8181
  let config = {};
8103
8182
  let needsWrite = false;
8104
8183
  if (!existsSync2(configDir)) {
@@ -8259,7 +8338,7 @@ function injectModelsConfig(logger) {
8259
8338
  }
8260
8339
  }
8261
8340
  function injectAuthProfile(logger) {
8262
- const agentsDir = join6(homedir5(), ".openclaw", "agents");
8341
+ const agentsDir = join7(homedir6(), ".openclaw", "agents");
8263
8342
  if (!existsSync2(agentsDir)) {
8264
8343
  try {
8265
8344
  mkdirSync2(agentsDir, { recursive: true });
@@ -8276,8 +8355,8 @@ function injectAuthProfile(logger) {
8276
8355
  agents = ["main", ...agents];
8277
8356
  }
8278
8357
  for (const agentId of agents) {
8279
- const authDir = join6(agentsDir, agentId, "agent");
8280
- const authPath = join6(authDir, "auth-profiles.json");
8358
+ const authDir = join7(agentsDir, agentId, "agent");
8359
+ const authPath = join7(authDir, "auth-profiles.json");
8281
8360
  if (!existsSync2(authDir)) {
8282
8361
  try {
8283
8362
  mkdirSync2(authDir, { recursive: true });