bnpanel 0.6.4 → 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.
Files changed (2) hide show
  1. package/index.js +166 -16
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -7309,7 +7309,7 @@ function listInstalled(paths) {
7309
7309
  function listVersions(paths, id) {
7310
7310
  const base = path3.join(paths.agents, id);
7311
7311
  if (!fs3.existsSync(base)) return [];
7312
- return fs3.readdirSync(base).filter((name2) => fs3.statSync(path3.join(base, name2)).isDirectory()).map((version) => {
7312
+ return fs3.readdirSync(base).filter((name2) => !name2.startsWith(".") && fs3.statSync(path3.join(base, name2)).isDirectory()).map((version) => {
7313
7313
  const dir = path3.join(base, version);
7314
7314
  return { version, dir, mtime: fs3.statSync(dir).mtimeMs };
7315
7315
  }).sort((a, b2) => b2.mtime - a.mtime);
@@ -7355,6 +7355,15 @@ function verifyNpmIntegrity(buf, integrity) {
7355
7355
 
7356
7356
  // ../panel-core/src/installer.ts
7357
7357
  var SKIP = /* @__PURE__ */ new Set([".git", "node_modules", ".DS_Store"]);
7358
+ function entryFileOf(dir, manifest) {
7359
+ const host = String(manifest.entry?.host || "").trim();
7360
+ const entry = path4.resolve(dir, host);
7361
+ const rel = path4.relative(dir, entry);
7362
+ if (!host || !rel || rel.startsWith("..") || path4.isAbsolute(rel)) {
7363
+ throw new Error(`manifest.json \u7684\u5165\u53E3\u4E0D\u5408\u6CD5\uFF1A${host || "(\u7A7A)"}`);
7364
+ }
7365
+ return entry;
7366
+ }
7358
7367
  async function installAgent(opts) {
7359
7368
  const { paths, publicKeys, source } = opts;
7360
7369
  const now = opts.now ?? (() => /* @__PURE__ */ new Date());
@@ -7382,16 +7391,39 @@ async function installAgent(opts) {
7382
7391
  }
7383
7392
  }
7384
7393
  const dest = agentDir(paths, manifest.id, manifest.version);
7385
- fs4.rmSync(dest, { recursive: true, force: true });
7386
- fs4.mkdirSync(dest, { recursive: true });
7387
- fs4.cpSync(staging, dest, {
7388
- recursive: true,
7389
- filter: (src) => !SKIP.has(path4.basename(src))
7390
- });
7391
- fs4.writeFileSync(path4.join(dest, "manifest.json"), JSON.stringify(manifest, null, 2));
7392
- if (!verifyContentDigest(dest, manifest.integrity.sha256)) {
7393
- fs4.rmSync(dest, { recursive: true, force: true });
7394
- throw new Error("package content does not match the signed digest");
7394
+ const incoming = path4.join(path4.dirname(dest), `.installing-${process.pid}-${Date.now()}`);
7395
+ fs4.mkdirSync(path4.dirname(dest), { recursive: true });
7396
+ for (const name2 of fs4.readdirSync(path4.dirname(dest))) {
7397
+ if (!name2.startsWith(".old-")) continue;
7398
+ try {
7399
+ fs4.rmSync(path4.join(path4.dirname(dest), name2), { recursive: true, force: true });
7400
+ } catch {
7401
+ }
7402
+ }
7403
+ try {
7404
+ fs4.rmSync(incoming, { recursive: true, force: true });
7405
+ fs4.cpSync(staging, incoming, {
7406
+ recursive: true,
7407
+ filter: (src) => !SKIP.has(path4.basename(src))
7408
+ });
7409
+ fs4.writeFileSync(path4.join(incoming, "manifest.json"), JSON.stringify(manifest, null, 2));
7410
+ if (!verifyContentDigest(incoming, manifest.integrity.sha256)) {
7411
+ throw new Error("package content does not match the signed digest");
7412
+ }
7413
+ if (!fs4.existsSync(entryFileOf(incoming, manifest))) {
7414
+ throw new Error(`package is missing entry file "${manifest.entry?.host}"`);
7415
+ }
7416
+ if (fs4.existsSync(dest)) {
7417
+ const trash = path4.join(path4.dirname(dest), `.old-${process.pid}-${Date.now()}`);
7418
+ fs4.renameSync(dest, trash);
7419
+ try {
7420
+ fs4.rmSync(trash, { recursive: true, force: true });
7421
+ } catch {
7422
+ }
7423
+ }
7424
+ fs4.renameSync(incoming, dest);
7425
+ } finally {
7426
+ fs4.rmSync(incoming, { recursive: true, force: true });
7395
7427
  }
7396
7428
  const current = readCurrent(paths, manifest.id);
7397
7429
  writeCurrent(paths, manifest.id, {
@@ -7852,6 +7884,16 @@ import fs7 from "node:fs";
7852
7884
  import net from "node:net";
7853
7885
  import path7 from "node:path";
7854
7886
  import { spawn } from "node:child_process";
7887
+ function resolveNodeRuntime(env = process.env) {
7888
+ const explicit = env.PANEL_NODE_BIN || env.NODE_BIN;
7889
+ if (explicit) return { command: explicit, env: {} };
7890
+ const npmNode = env.npm_node_execpath;
7891
+ if (npmNode && fs7.existsSync(npmNode)) return { command: npmNode, env: {} };
7892
+ if (process.versions.electron) {
7893
+ return { command: process.execPath, env: { ELECTRON_RUN_AS_NODE: "1" } };
7894
+ }
7895
+ return { command: process.execPath, env: {} };
7896
+ }
7855
7897
  var Supervisor = class {
7856
7898
  constructor(paths, spawnImpl = spawn) {
7857
7899
  this.paths = paths;
@@ -7870,9 +7912,24 @@ var Supervisor = class {
7870
7912
  if (this.running.has(agent.id)) return this.list().find((r) => r.id === agent.id);
7871
7913
  const port = await freePort();
7872
7914
  const entry = path7.resolve(agent.dir, agent.manifest.entry.host);
7915
+ if (!fs7.existsSync(entry)) {
7916
+ throw new Error(
7917
+ `Agent \u5B89\u88C5\u6587\u4EF6\u4E0D\u5B8C\u6574\uFF1A\u7F3A\u5C11 ${entry}\u3002\u8BF7\u5728\u9762\u677F\u4E2D\u91CD\u65B0\u5B89\u88C5\u300C${agent.manifest.name || agent.id}\u300D\u3002`
7918
+ );
7919
+ }
7873
7920
  const workspaceRoot = opts.workspaceRoot ? path7.isAbsolute(opts.workspaceRoot) ? opts.workspaceRoot : path7.resolve(opts.workspaceRoot) : this.paths.workspaces;
7874
7921
  const workspace = path7.join(workspaceRoot, agent.id);
7875
- fs7.mkdirSync(workspace, { recursive: true });
7922
+ try {
7923
+ fs7.mkdirSync(workspace, { recursive: true });
7924
+ const probe = path7.join(workspace, `.write-probe-${process.pid}`);
7925
+ fs7.writeFileSync(probe, "");
7926
+ fs7.rmSync(probe, { force: true });
7927
+ } catch (err) {
7928
+ const e = err;
7929
+ throw new Error(
7930
+ `\u5DE5\u4F5C\u76EE\u5F55\u4E0D\u53EF\u7528\uFF1A${workspace}\uFF08${e.code ?? "ERROR"}: ${e.message}\uFF09\u3002\u8BF7\u5728\u8BBE\u7F6E\u91CC\u6362\u6210\u672C\u673A\u53EF\u5199\u7684\u76EE\u5F55\u540E\u91CD\u8BD5\u3002`
7931
+ );
7932
+ }
7876
7933
  const env = {
7877
7934
  ...process.env,
7878
7935
  PANEL_PORT: String(port),
@@ -7889,15 +7946,47 @@ var Supervisor = class {
7889
7946
  ...opts.panelCallbackToken ? { PANEL_CALLBACK_TOKEN: opts.panelCallbackToken } : {},
7890
7947
  ...opts.extraEnv ?? {}
7891
7948
  };
7892
- const child = this.spawnImpl(process.execPath, [entry], { cwd: agent.dir, env });
7949
+ const node = resolveNodeRuntime();
7950
+ const child = this.spawnImpl(node.command, [entry], { cwd: agent.dir, env: { ...env, ...node.env } });
7893
7951
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
7894
7952
  const record = { id: agent.id, pid: child.pid ?? -1, port, url: `http://127.0.0.1:${port}`, startedAt, child, restarts: 0 };
7895
7953
  this.running.set(agent.id, record);
7954
+ let tail = "";
7955
+ let spawnError = null;
7956
+ const capture = (chunk) => {
7957
+ tail = (tail + chunk.toString()).slice(-1200);
7958
+ };
7959
+ child.stdout?.on("data", capture);
7960
+ child.stderr?.on("data", capture);
7961
+ child.once("error", (err) => {
7962
+ spawnError = err;
7963
+ });
7896
7964
  child.once("exit", () => {
7897
7965
  const current = this.running.get(agent.id);
7898
7966
  if (current?.child === child) this.running.delete(agent.id);
7899
7967
  });
7900
- await waitForPort(port, opts.timeoutMs ?? 2e4);
7968
+ const exitWatcher = new Promise((_2, reject) => {
7969
+ child.once("error", (err) => reject(new Error(`Agent \u8FDB\u7A0B\u542F\u52A8\u5931\u8D25\uFF1A${err.message}`)));
7970
+ child.once("exit", (code) => reject(new Error(`Agent \u8FDB\u7A0B\u542F\u52A8\u540E\u7ACB\u5373\u9000\u51FA\uFF08exitCode=${code}\uFF09`)));
7971
+ });
7972
+ exitWatcher.catch(() => {
7973
+ });
7974
+ try {
7975
+ const waiter = waitForPort(port, opts.timeoutMs ?? 2e4);
7976
+ waiter.catch(() => {
7977
+ });
7978
+ await Promise.race([waiter, exitWatcher]);
7979
+ } catch (err) {
7980
+ this.running.delete(agent.id);
7981
+ if (!child.killed) child.kill("SIGKILL");
7982
+ const lines = [`${err.message}`];
7983
+ if (spawnError) lines.push(`\u542F\u52A8\u5931\u8D25\uFF1A${spawnError.message}`);
7984
+ else if (child.exitCode !== null) lines.push(`Agent \u8FDB\u7A0B\u5DF2\u9000\u51FA\uFF0CexitCode=${child.exitCode}`);
7985
+ lines.push(`workspace=${workspace}`, `command=${node.command}`);
7986
+ if (tail.trim()) lines.push(`--- agent output ---
7987
+ ${tail.trim()}`);
7988
+ throw new Error(lines.join("\n"));
7989
+ }
7901
7990
  return this.list().find((r) => r.id === agent.id);
7902
7991
  }
7903
7992
  async stop(id) {
@@ -9151,6 +9240,8 @@ function createPanelApp(options = {}) {
9151
9240
  title: `\u5B89\u88C5 ${item.name || product}`,
9152
9241
  run: async (ctx) => {
9153
9242
  ctx.setProgress(5, "\u51C6\u5907\u4E0B\u8F7D");
9243
+ await supervisor.stop(product).catch(() => {
9244
+ });
9154
9245
  let result;
9155
9246
  try {
9156
9247
  ctx.setProgress(15, "\u6B63\u5728\u4E0B\u8F7D");
@@ -9353,6 +9444,54 @@ function createPanelApp(options = {}) {
9353
9444
  expectedSha256: sha && !/^0+$/.test(sha) ? sha : void 0
9354
9445
  });
9355
9446
  }
9447
+ function agentFilesBroken(agent) {
9448
+ if (!agent) return true;
9449
+ try {
9450
+ const host = agent.manifest?.entry?.host;
9451
+ return !host || !fs14.existsSync(path14.resolve(agent.dir, host));
9452
+ } catch {
9453
+ return true;
9454
+ }
9455
+ }
9456
+ const repairing = /* @__PURE__ */ new Set();
9457
+ function submitRepair(agent) {
9458
+ if (repairing.has(agent.id)) return null;
9459
+ repairing.add(agent.id);
9460
+ const product = productOf(agent) || agent.id;
9461
+ return queue.submit({
9462
+ agentId: agent.id,
9463
+ kind: "install",
9464
+ title: `\u4FEE\u590D ${agent.manifest?.name || agent.id} \u5B89\u88C5`,
9465
+ run: async (ctx) => {
9466
+ try {
9467
+ ctx.setProgress(10, "\u6B63\u5728\u91CD\u65B0\u4E0B\u8F7D\u5B89\u88C5\u5305");
9468
+ await supervisor.stop(agent.id).catch(() => {
9469
+ });
9470
+ const result = await installPublishedAgent(product, agent.version, void 0, void 0);
9471
+ ctx.setProgress(60, "\u6B63\u5728\u51C6\u5907\u8FD0\u884C\u73AF\u5883");
9472
+ await ensureAgentRuntimes(result.dir, result.manifest.runtime?.shared ?? [], ctx);
9473
+ const installed = listInstalled(paths).find((a) => a.id === agent.id);
9474
+ if (installed) syncAgentSkills(installed);
9475
+ ctx.setProgress(100, "\u5B89\u88C5\u5DF2\u4FEE\u590D");
9476
+ void reportInstalls(true);
9477
+ return { artifact: result.dir };
9478
+ } catch (err) {
9479
+ throw userFacingInstallError(err);
9480
+ } finally {
9481
+ repairing.delete(agent.id);
9482
+ }
9483
+ }
9484
+ });
9485
+ }
9486
+ function repairBrokenInstalls() {
9487
+ for (const agent of listInstalled(paths)) {
9488
+ if (!agentFilesBroken(agent)) continue;
9489
+ const busy = queue.list().some((t) => t.agentId === agent.id && (t.status === "queued" || t.status === "running"));
9490
+ if (busy) continue;
9491
+ console.warn("[biunow-panel] agent files missing:", agent.id, agent.version, agent.dir);
9492
+ submitRepair(agent);
9493
+ }
9494
+ }
9356
9495
  function submitUpdate(agent, next) {
9357
9496
  const product = productOf(agent) ?? next.product ?? settings.productSlug;
9358
9497
  return queue.submit({
@@ -9455,6 +9594,13 @@ function createPanelApp(options = {}) {
9455
9594
  err.code = "PERMISSIONS_REQUIRED";
9456
9595
  throw err;
9457
9596
  }
9597
+ if (agentFilesBroken(agent)) {
9598
+ submitRepair(agent);
9599
+ const err = new Error("\u5B89\u88C5\u6587\u4EF6\u4E0D\u5B8C\u6574\uFF0C\u5DF2\u5F00\u59CB\u81EA\u52A8\u4FEE\u590D\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5");
9600
+ err.status = 409;
9601
+ err.code = "AGENT_INSTALL_BROKEN";
9602
+ throw err;
9603
+ }
9458
9604
  const token = ensureCallbackToken(agent.id);
9459
9605
  const publicBase = callbackBaseUrl();
9460
9606
  const licensed = product ? license.canUseProduct(product) : license.canExport();
@@ -9525,6 +9671,7 @@ function createPanelApp(options = {}) {
9525
9671
  description: agent.manifest.description ?? null,
9526
9672
  previousVersion: agent.previousVersion,
9527
9673
  installedAt: agent.installedAt,
9674
+ broken: agentFilesBroken(agent),
9528
9675
  icon: agent.manifest.panel?.icon || `icon://${agent.id}`,
9529
9676
  accent: agent.manifest.panel?.accent ?? null,
9530
9677
  studio: agent.manifest.panel?.studio !== false,
@@ -9618,7 +9765,8 @@ function createPanelApp(options = {}) {
9618
9765
  settings: {
9619
9766
  autoInstallUpdates: !!settings.autoInstallUpdates,
9620
9767
  autoUpdate: settings.autoUpdate !== false,
9621
- autoKickoffInDsh: settings.autoKickoffInDsh !== false
9768
+ autoKickoffInDsh: settings.autoKickoffInDsh !== false,
9769
+ workspaceRoot: settings.workspaceRoot || null
9622
9770
  },
9623
9771
  panel: {
9624
9772
  name: packageName,
@@ -9657,7 +9805,8 @@ function createPanelApp(options = {}) {
9657
9805
  tasks: queue.list(),
9658
9806
  settings: {
9659
9807
  autoInstallUpdates: !!settings.autoInstallUpdates,
9660
- autoKickoffInDsh: settings.autoKickoffInDsh !== false
9808
+ autoKickoffInDsh: settings.autoKickoffInDsh !== false,
9809
+ workspaceRoot: settings.workspaceRoot || null
9661
9810
  },
9662
9811
  dsh: {
9663
9812
  skills: !!dsh?.skills?.available?.(),
@@ -10433,6 +10582,7 @@ function createPanelApp(options = {}) {
10433
10582
  void loadReleases();
10434
10583
  void refreshTrialRuns();
10435
10584
  void reportInstalls();
10585
+ setTimeout(() => repairBrokenInstalls(), 3e3).unref?.();
10436
10586
  const autoTimer = setInterval(() => void autoUpdateTick(), 6 * 36e5);
10437
10587
  if (typeof autoTimer.unref === "function") autoTimer.unref();
10438
10588
  function bindChatProduct(cwd) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bnpanel",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
4
4
  "type": "module",
5
5
  "description": "无需AI生图/生视频,本机低配置即可运行的‘解说、动效、风格化’视频创作工厂。",
6
6
  "license": "UNLICENSED",