bnpanel 0.6.5 → 0.6.7

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/index.js CHANGED
@@ -10,6 +10,8 @@ import path15 from "node:path";
10
10
 
11
11
  // src/host/app.js
12
12
  import { spawn as spawn2 } from "node:child_process";
13
+ import { once } from "node:events";
14
+ import { finished } from "node:stream/promises";
13
15
  import crypto8 from "node:crypto";
14
16
  import fs14 from "node:fs";
15
17
  import path14 from "node:path";
@@ -7122,6 +7124,8 @@ var EntitlementsSchema = external_exports.object({
7122
7124
  expiresAt: external_exports.string(),
7123
7125
  graceUntil: external_exports.string(),
7124
7126
  issuedAt: external_exports.string(),
7127
+ /** 代理归属码,激活许可证时由服务端根据 license.reseller_id 写入,面板据此区分代理。 */
7128
+ resellerCode: external_exports.string().optional(),
7125
7129
  /** Detached ed25519 signature over the other fields; verified offline by the panel. */
7126
7130
  signature: external_exports.string().optional()
7127
7131
  });
@@ -7309,7 +7313,7 @@ function listInstalled(paths) {
7309
7313
  function listVersions(paths, id) {
7310
7314
  const base = path3.join(paths.agents, id);
7311
7315
  if (!fs3.existsSync(base)) return [];
7312
- return fs3.readdirSync(base).filter((name2) => fs3.statSync(path3.join(base, name2)).isDirectory()).map((version) => {
7316
+ return fs3.readdirSync(base).filter((name2) => !name2.startsWith(".") && fs3.statSync(path3.join(base, name2)).isDirectory()).map((version) => {
7313
7317
  const dir = path3.join(base, version);
7314
7318
  return { version, dir, mtime: fs3.statSync(dir).mtimeMs };
7315
7319
  }).sort((a, b2) => b2.mtime - a.mtime);
@@ -7355,6 +7359,15 @@ function verifyNpmIntegrity(buf, integrity) {
7355
7359
 
7356
7360
  // ../panel-core/src/installer.ts
7357
7361
  var SKIP = /* @__PURE__ */ new Set([".git", "node_modules", ".DS_Store"]);
7362
+ function entryFileOf(dir, manifest) {
7363
+ const host = String(manifest.entry?.host || "").trim();
7364
+ const entry = path4.resolve(dir, host);
7365
+ const rel = path4.relative(dir, entry);
7366
+ if (!host || !rel || rel.startsWith("..") || path4.isAbsolute(rel)) {
7367
+ throw new Error(`manifest.json \u7684\u5165\u53E3\u4E0D\u5408\u6CD5\uFF1A${host || "(\u7A7A)"}`);
7368
+ }
7369
+ return entry;
7370
+ }
7358
7371
  async function installAgent(opts) {
7359
7372
  const { paths, publicKeys, source } = opts;
7360
7373
  const now = opts.now ?? (() => /* @__PURE__ */ new Date());
@@ -7382,16 +7395,39 @@ async function installAgent(opts) {
7382
7395
  }
7383
7396
  }
7384
7397
  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");
7398
+ const incoming = path4.join(path4.dirname(dest), `.installing-${process.pid}-${Date.now()}`);
7399
+ fs4.mkdirSync(path4.dirname(dest), { recursive: true });
7400
+ for (const name2 of fs4.readdirSync(path4.dirname(dest))) {
7401
+ if (!name2.startsWith(".old-")) continue;
7402
+ try {
7403
+ fs4.rmSync(path4.join(path4.dirname(dest), name2), { recursive: true, force: true });
7404
+ } catch {
7405
+ }
7406
+ }
7407
+ try {
7408
+ fs4.rmSync(incoming, { recursive: true, force: true });
7409
+ fs4.cpSync(staging, incoming, {
7410
+ recursive: true,
7411
+ filter: (src) => !SKIP.has(path4.basename(src))
7412
+ });
7413
+ fs4.writeFileSync(path4.join(incoming, "manifest.json"), JSON.stringify(manifest, null, 2));
7414
+ if (!verifyContentDigest(incoming, manifest.integrity.sha256)) {
7415
+ throw new Error("package content does not match the signed digest");
7416
+ }
7417
+ if (!fs4.existsSync(entryFileOf(incoming, manifest))) {
7418
+ throw new Error(`package is missing entry file "${manifest.entry?.host}"`);
7419
+ }
7420
+ if (fs4.existsSync(dest)) {
7421
+ const trash = path4.join(path4.dirname(dest), `.old-${process.pid}-${Date.now()}`);
7422
+ fs4.renameSync(dest, trash);
7423
+ try {
7424
+ fs4.rmSync(trash, { recursive: true, force: true });
7425
+ } catch {
7426
+ }
7427
+ }
7428
+ fs4.renameSync(incoming, dest);
7429
+ } finally {
7430
+ fs4.rmSync(incoming, { recursive: true, force: true });
7395
7431
  }
7396
7432
  const current = readCurrent(paths, manifest.id);
7397
7433
  writeCurrent(paths, manifest.id, {
@@ -7765,6 +7801,37 @@ var LicenseClient = class {
7765
7801
  if (body.state === "revoked") return { state: "revoked", notices: (body.notices ?? []).map((n) => n.body), product: cache.product };
7766
7802
  return { state: body.state, notices: (body.notices ?? []).map((n) => n.body), product: cache.product };
7767
7803
  }
7804
+ /** 在线能力(如诊断)用:令牌临近过期先续期,避免用了 24 小时后突然失效。 */
7805
+ async ensureToken(product) {
7806
+ const cache = this.readCache(product);
7807
+ if (!cache) return null;
7808
+ const expires = cache.tokenExpiresAt ? Date.parse(cache.tokenExpiresAt) : 0;
7809
+ if (expires && expires - Date.now() > 5 * 6e4) return cache.token;
7810
+ return await this.refreshToken(product) ?? cache.token;
7811
+ }
7812
+ async refreshToken(product) {
7813
+ const cache = this.readCache(product);
7814
+ if (!cache) return null;
7815
+ try {
7816
+ const res = await fetch(`${this.cfg.baseUrl}/v1/refresh`, {
7817
+ method: "POST",
7818
+ headers: { "content-type": "application/json", authorization: `Bearer ${cache.token}` },
7819
+ body: JSON.stringify({ device: this.device(), token: cache.token })
7820
+ });
7821
+ const body = await res.json();
7822
+ if (!res.ok || !body?.token) return null;
7823
+ const store = this.readStore();
7824
+ store.products[cache.product] = {
7825
+ ...cache,
7826
+ token: String(body.token),
7827
+ tokenExpiresAt: String(body.expiresAt ?? cache.tokenExpiresAt)
7828
+ };
7829
+ this.writeStore(store);
7830
+ return String(body.token);
7831
+ } catch {
7832
+ return null;
7833
+ }
7834
+ }
7768
7835
  statusFor(product) {
7769
7836
  const cache = this.readCache(product);
7770
7837
  if (!cache) {
@@ -7811,6 +7878,11 @@ var LicenseClient = class {
7811
7878
  canUseProduct(product) {
7812
7879
  return this.statusFor(product).canExport;
7813
7880
  }
7881
+ /** 从已缓存的签名 entitlements 中取代理归属码;未激活或官方直售时返回 null。 */
7882
+ resellerCode(product) {
7883
+ const cache = this.readCache(product);
7884
+ return cache?.entitlements?.resellerCode ?? null;
7885
+ }
7814
7886
  tokenFor(product) {
7815
7887
  return this.readCache(product)?.token ?? null;
7816
7888
  }
@@ -7880,6 +7952,11 @@ var Supervisor = class {
7880
7952
  if (this.running.has(agent.id)) return this.list().find((r) => r.id === agent.id);
7881
7953
  const port = await freePort();
7882
7954
  const entry = path7.resolve(agent.dir, agent.manifest.entry.host);
7955
+ if (!fs7.existsSync(entry)) {
7956
+ throw new Error(
7957
+ `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`
7958
+ );
7959
+ }
7883
7960
  const workspaceRoot = opts.workspaceRoot ? path7.isAbsolute(opts.workspaceRoot) ? opts.workspaceRoot : path7.resolve(opts.workspaceRoot) : this.paths.workspaces;
7884
7961
  const workspace = path7.join(workspaceRoot, agent.id);
7885
7962
  try {
@@ -8451,16 +8528,13 @@ function createChatBridge() {
8451
8528
  recent.unshift(entry);
8452
8529
  if (recent.length > 30) recent.length = 30;
8453
8530
  }
8454
- async function startSkillRun(opts) {
8455
- const skill = String(opts.skill || "").trim();
8531
+ async function openSession(opts) {
8456
8532
  const projectPath = path11.resolve(String(opts.projectPath || ""));
8457
8533
  const promptText = String(opts.promptText || "").trim();
8458
- const agentId = opts.agentId || skill;
8459
- if (!skill) return { ok: false, reason: "missing-skill" };
8534
+ const agentId = opts.agentId || "panel";
8535
+ const skill = opts.skill || "prompt";
8460
8536
  if (!promptText) return { ok: false, reason: "missing-prompt" };
8461
- if (!projectPath || !fs11.existsSync(projectPath)) {
8462
- return { ok: false, reason: "missing-project", error: `project path not found: ${projectPath}` };
8463
- }
8537
+ if (!projectPath) return { ok: false, reason: "missing-project", error: "project path is empty" };
8464
8538
  if (!available()) {
8465
8539
  const entry = {
8466
8540
  at: (/* @__PURE__ */ new Date()).toISOString(),
@@ -8471,7 +8545,7 @@ function createChatBridge() {
8471
8545
  error: "sessionController unavailable (not running inside DSH)",
8472
8546
  projectPath
8473
8547
  };
8474
- remember(entry);
8548
+ if (opts.rememberRun !== false) remember(entry);
8475
8549
  return { ok: false, reason: "chat-unavailable", ...entry };
8476
8550
  }
8477
8551
  try {
@@ -8506,8 +8580,8 @@ function createChatBridge() {
8506
8580
  ok: true,
8507
8581
  projectPath
8508
8582
  };
8509
- remember(entry);
8510
- return { ok: true, sessionId, skill, projectPath };
8583
+ if (opts.rememberRun !== false) remember(entry);
8584
+ return { ok: true, sessionId, projectPath };
8511
8585
  } catch (err) {
8512
8586
  const entry = {
8513
8587
  at: (/* @__PURE__ */ new Date()).toISOString(),
@@ -8518,14 +8592,28 @@ function createChatBridge() {
8518
8592
  error: err?.message || String(err),
8519
8593
  projectPath
8520
8594
  };
8521
- remember(entry);
8595
+ if (opts.rememberRun !== false) remember(entry);
8522
8596
  return { ok: false, reason: "kickoff-failed", error: entry.error, projectPath };
8523
8597
  }
8524
8598
  }
8599
+ async function startSkillRun(opts) {
8600
+ const skill = String(opts.skill || "").trim();
8601
+ if (!skill) return { ok: false, reason: "missing-skill" };
8602
+ const projectPath = path11.resolve(String(opts.projectPath || ""));
8603
+ if (!projectPath || !fs11.existsSync(projectPath)) {
8604
+ return { ok: false, reason: "missing-project", error: `project path not found: ${projectPath}` };
8605
+ }
8606
+ const result = await openSession({ ...opts, agentId: opts.agentId || skill, skill });
8607
+ return result.ok ? { ...result, skill } : result;
8608
+ }
8609
+ async function startPromptRun(opts) {
8610
+ return openSession({ ...opts, rememberRun: false });
8611
+ }
8525
8612
  return {
8526
8613
  setSessionController,
8527
8614
  available,
8528
8615
  startSkillRun,
8616
+ startPromptRun,
8529
8617
  listRecent,
8530
8618
  extractKickoffPrompt
8531
8619
  };
@@ -8774,7 +8862,29 @@ function resolveIconsDir() {
8774
8862
  return candidates[0];
8775
8863
  }
8776
8864
  var ICONS_DIR = resolveIconsDir();
8865
+ function resolveDiagnoseSkillDir(skillName = "account-diagnosis") {
8866
+ const here = path14.dirname(fileURLToPath2(import.meta.url));
8867
+ const candidates = [
8868
+ path14.join(here, "assets", "diagnose", skillName),
8869
+ path14.join(here, "..", "..", "assets", "diagnose", skillName)
8870
+ ];
8871
+ return candidates.find((dir) => fs14.existsSync(path14.join(dir, "SKILL.md"))) || null;
8872
+ }
8873
+ var DIAGNOSE_SKILL_DIRS = {
8874
+ "account-diagnosis": resolveDiagnoseSkillDir("account-diagnosis"),
8875
+ "creator-research": resolveDiagnoseSkillDir("creator-research"),
8876
+ "topic-planner": resolveDiagnoseSkillDir("topic-planner")
8877
+ };
8878
+ function seedDiagnoseSkill(projectDir, skillName = "account-diagnosis") {
8879
+ const sourceDir = DIAGNOSE_SKILL_DIRS[skillName];
8880
+ if (!sourceDir) throw new Error("\u8D26\u53F7\u589E\u957F\u5185\u6838\u7F3A\u5931\uFF0C\u8BF7\u66F4\u65B0\u9762\u677F");
8881
+ const destDir = path14.join(projectDir, ".agents", "skills", skillName);
8882
+ fs14.mkdirSync(path14.dirname(destDir), { recursive: true });
8883
+ fs14.cpSync(sourceDir, destDir, { recursive: true, force: true });
8884
+ return destDir;
8885
+ }
8777
8886
  var MAX_BODY = 256 * 1024;
8887
+ var GROWTH_LABELS = { research: "\u5BF9\u6807\u8C03\u7814", planner: "\u9009\u9898\u89C4\u5212", followup: "\u590D\u8BCA\u5BF9\u6BD4" };
8778
8888
  function send(res, status, body) {
8779
8889
  const data = JSON.stringify(body);
8780
8890
  res.writeHead(status, {
@@ -8809,12 +8919,17 @@ function createPanelApp(options = {}) {
8809
8919
  }
8810
8920
  const accountId = options.accountId ?? settings.accountId ?? "local-dev";
8811
8921
  const packageName = panelPackageName(options.packageName);
8812
- const resellerCode = panelResellerCode(options.resellerCode);
8813
- const buyPageUrl = () => withReseller(`${String(settings.licenseBaseUrl || "").replace(/\/$/, "")}/buy`, resellerCode);
8922
+ const configResellerCode = panelResellerCode(options.resellerCode);
8923
+ const resellerCode = () => {
8924
+ const fromLicense = license.resellerCode();
8925
+ if (fromLicense) return fromLicense;
8926
+ return configResellerCode;
8927
+ };
8928
+ const buyPageUrl = () => withReseller(`${String(settings.licenseBaseUrl || "").replace(/\/$/, "")}/buy`, resellerCode());
8814
8929
  const commerceUrl = (pathname) => {
8815
8930
  const base = String(settings.licenseBaseUrl || "").replace(/\/$/, "");
8816
8931
  if (!base) return "";
8817
- return withReseller(`${base}${pathname}`, resellerCode);
8932
+ return withReseller(`${base}${pathname}`, resellerCode());
8818
8933
  };
8819
8934
  const panelInstallHint = (updating) => updating ? `\u5728 DSH \u7684\u63D2\u4EF6\u5217\u8868\u91CC\u66F4\u65B0\u9762\u677F\uFF0C\u7136\u540E\u91CD\u542F` : `\u5728 DSH \u91CC\u6DFB\u52A0\u63D2\u4EF6\u5E76\u5B89\u88C5\u9762\u677F`;
8820
8935
  const license = new LicenseClient({
@@ -9203,6 +9318,8 @@ function createPanelApp(options = {}) {
9203
9318
  title: `\u5B89\u88C5 ${item.name || product}`,
9204
9319
  run: async (ctx) => {
9205
9320
  ctx.setProgress(5, "\u51C6\u5907\u4E0B\u8F7D");
9321
+ await supervisor.stop(product).catch(() => {
9322
+ });
9206
9323
  let result;
9207
9324
  try {
9208
9325
  ctx.setProgress(15, "\u6B63\u5728\u4E0B\u8F7D");
@@ -9405,6 +9522,54 @@ function createPanelApp(options = {}) {
9405
9522
  expectedSha256: sha && !/^0+$/.test(sha) ? sha : void 0
9406
9523
  });
9407
9524
  }
9525
+ function agentFilesBroken(agent) {
9526
+ if (!agent) return true;
9527
+ try {
9528
+ const host = agent.manifest?.entry?.host;
9529
+ return !host || !fs14.existsSync(path14.resolve(agent.dir, host));
9530
+ } catch {
9531
+ return true;
9532
+ }
9533
+ }
9534
+ const repairing = /* @__PURE__ */ new Set();
9535
+ function submitRepair(agent) {
9536
+ if (repairing.has(agent.id)) return null;
9537
+ repairing.add(agent.id);
9538
+ const product = productOf(agent) || agent.id;
9539
+ return queue.submit({
9540
+ agentId: agent.id,
9541
+ kind: "install",
9542
+ title: `\u4FEE\u590D ${agent.manifest?.name || agent.id} \u5B89\u88C5`,
9543
+ run: async (ctx) => {
9544
+ try {
9545
+ ctx.setProgress(10, "\u6B63\u5728\u91CD\u65B0\u4E0B\u8F7D\u5B89\u88C5\u5305");
9546
+ await supervisor.stop(agent.id).catch(() => {
9547
+ });
9548
+ const result = await installPublishedAgent(product, agent.version, void 0, void 0);
9549
+ ctx.setProgress(60, "\u6B63\u5728\u51C6\u5907\u8FD0\u884C\u73AF\u5883");
9550
+ await ensureAgentRuntimes(result.dir, result.manifest.runtime?.shared ?? [], ctx);
9551
+ const installed = listInstalled(paths).find((a) => a.id === agent.id);
9552
+ if (installed) syncAgentSkills(installed);
9553
+ ctx.setProgress(100, "\u5B89\u88C5\u5DF2\u4FEE\u590D");
9554
+ void reportInstalls(true);
9555
+ return { artifact: result.dir };
9556
+ } catch (err) {
9557
+ throw userFacingInstallError(err);
9558
+ } finally {
9559
+ repairing.delete(agent.id);
9560
+ }
9561
+ }
9562
+ });
9563
+ }
9564
+ function repairBrokenInstalls() {
9565
+ for (const agent of listInstalled(paths)) {
9566
+ if (!agentFilesBroken(agent)) continue;
9567
+ const busy = queue.list().some((t) => t.agentId === agent.id && (t.status === "queued" || t.status === "running"));
9568
+ if (busy) continue;
9569
+ console.warn("[biunow-panel] agent files missing:", agent.id, agent.version, agent.dir);
9570
+ submitRepair(agent);
9571
+ }
9572
+ }
9408
9573
  function submitUpdate(agent, next) {
9409
9574
  const product = productOf(agent) ?? next.product ?? settings.productSlug;
9410
9575
  return queue.submit({
@@ -9507,6 +9672,13 @@ function createPanelApp(options = {}) {
9507
9672
  err.code = "PERMISSIONS_REQUIRED";
9508
9673
  throw err;
9509
9674
  }
9675
+ if (agentFilesBroken(agent)) {
9676
+ submitRepair(agent);
9677
+ 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");
9678
+ err.status = 409;
9679
+ err.code = "AGENT_INSTALL_BROKEN";
9680
+ throw err;
9681
+ }
9510
9682
  const token = ensureCallbackToken(agent.id);
9511
9683
  const publicBase = callbackBaseUrl();
9512
9684
  const licensed = product ? license.canUseProduct(product) : license.canExport();
@@ -9577,6 +9749,7 @@ function createPanelApp(options = {}) {
9577
9749
  description: agent.manifest.description ?? null,
9578
9750
  previousVersion: agent.previousVersion,
9579
9751
  installedAt: agent.installedAt,
9752
+ broken: agentFilesBroken(agent),
9580
9753
  icon: agent.manifest.panel?.icon || `icon://${agent.id}`,
9581
9754
  accent: agent.manifest.panel?.accent ?? null,
9582
9755
  studio: agent.manifest.panel?.studio !== false,
@@ -9617,6 +9790,476 @@ function createPanelApp(options = {}) {
9617
9790
  queue.cancel(t.id);
9618
9791
  }
9619
9792
  }
9793
+ function memberInfo() {
9794
+ const rows = license.listProducts();
9795
+ const isOn = (status) => status && (status.state === "active" || status.state === "grace");
9796
+ const pick = rows.find((row) => row.product === "biupro" && isOn(row.status)) ?? null;
9797
+ const expiresAt = pick?.status?.expiresAt || null;
9798
+ const at2 = expiresAt ? Date.parse(expiresAt) : 0;
9799
+ return {
9800
+ active: !!isOn(pick?.status),
9801
+ product: pick?.product ?? null,
9802
+ expiresAt,
9803
+ daysLeft: at2 ? Math.max(0, Math.ceil((at2 - Date.now()) / 864e5)) : 0
9804
+ };
9805
+ }
9806
+ const DIAGNOSE_DIR = path14.join(paths.root, "diagnose");
9807
+ const GROWTH_DIR = path14.join(paths.root, "growth");
9808
+ const DIAGNOSE_MEDIA_DIR = path14.join(paths.root, "diagnose-media");
9809
+ const DIAGNOSE_SESSIONS_DIR = path14.join(paths.root, "diagnose-sessions");
9810
+ const DIAGNOSE_MAX_REPORTS = 60;
9811
+ const DIAGNOSE_MAX_UPLOAD = 300 * 1024 * 1024;
9812
+ const DIAGNOSE_IMAGE_EXT = /* @__PURE__ */ new Set([".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp", ".heic"]);
9813
+ const DIAGNOSE_VIDEO_EXT = /* @__PURE__ */ new Set([".mp4", ".mov", ".m4v", ".webm", ".avi", ".mkv"]);
9814
+ const DIAGNOSE_MEDIA_TYPES = /* @__PURE__ */ new Map([
9815
+ [".jpg", "image/jpeg"],
9816
+ [".jpeg", "image/jpeg"],
9817
+ [".png", "image/png"],
9818
+ [".webp", "image/webp"],
9819
+ [".gif", "image/gif"],
9820
+ [".bmp", "image/bmp"],
9821
+ [".heic", "image/heic"],
9822
+ [".mp4", "video/mp4"],
9823
+ [".m4v", "video/mp4"],
9824
+ [".mov", "video/quicktime"],
9825
+ [".webm", "video/webm"],
9826
+ [".avi", "video/x-msvideo"],
9827
+ [".mkv", "video/x-matroska"]
9828
+ ]);
9829
+ function assertDiagnoseMember() {
9830
+ const info = memberInfo();
9831
+ if (!info.active) throw Object.assign(new Error("\u53F7\u8109\u53CA\u589E\u957F\u80FD\u529B\u662F BiuPro \u4E13\u5C5E\u529F\u80FD\uFF0C\u8BF7\u5148\u5F00\u901A BiuPro"), { status: 403 });
9832
+ }
9833
+ async function saveDiagnoseUpload(req, query) {
9834
+ assertDiagnoseMember();
9835
+ const rawName = String(query.get("name") || "\u7D20\u6750").slice(0, 160);
9836
+ const ext = path14.extname(rawName).toLowerCase();
9837
+ const kind = DIAGNOSE_IMAGE_EXT.has(ext) ? "image" : DIAGNOSE_VIDEO_EXT.has(ext) ? "video" : "";
9838
+ if (!kind) throw Object.assign(new Error("\u53EA\u652F\u6301\u56FE\u7247\u6216\u89C6\u9891\uFF08jpg/png/webp/mp4/mov \u7B49\uFF09"), { status: 400 });
9839
+ const id = crypto8.randomUUID();
9840
+ const dir = path14.join(DIAGNOSE_MEDIA_DIR, id);
9841
+ fs14.mkdirSync(dir, { recursive: true });
9842
+ const file = path14.join(dir, `source${ext}`);
9843
+ const out = fs14.createWriteStream(file);
9844
+ let size = 0;
9845
+ try {
9846
+ for await (const chunk of req) {
9847
+ size += chunk.length;
9848
+ if (size > DIAGNOSE_MAX_UPLOAD) {
9849
+ throw Object.assign(new Error("\u6587\u4EF6\u592A\u5927\uFF0C\u5355\u4E2A\u7D20\u6750\u4E0A\u9650 300MB"), { status: 413 });
9850
+ }
9851
+ if (!out.write(chunk)) await once(out, "drain");
9852
+ }
9853
+ await new Promise((resolve, reject) => {
9854
+ out.once("error", reject);
9855
+ out.end(resolve);
9856
+ });
9857
+ await finished(out);
9858
+ } catch (error) {
9859
+ out.destroy();
9860
+ fs14.rmSync(dir, { recursive: true, force: true });
9861
+ throw error;
9862
+ }
9863
+ return {
9864
+ id,
9865
+ name: rawName,
9866
+ kind,
9867
+ size,
9868
+ mime: String(req.headers["content-type"] || "").split(";")[0] || DIAGNOSE_MEDIA_TYPES.get(ext) || "",
9869
+ url: `${basePath}/diagnose/media/${id}`,
9870
+ path: file
9871
+ };
9872
+ }
9873
+ function normalizeDiagnoseMedia(input) {
9874
+ const rows = Array.isArray(input) ? input : [];
9875
+ const out = [];
9876
+ for (const item of rows.slice(0, 12)) {
9877
+ const id = String(item?.id || "");
9878
+ if (!/^[0-9a-f-]{36}$/i.test(id)) continue;
9879
+ const dir = path14.join(DIAGNOSE_MEDIA_DIR, id);
9880
+ if (!fs14.existsSync(dir)) continue;
9881
+ const name2 = fs14.readdirSync(dir).find((entry) => entry.startsWith("source."));
9882
+ if (!name2) continue;
9883
+ const file = path14.join(dir, name2);
9884
+ const ext = path14.extname(name2).toLowerCase();
9885
+ out.push({
9886
+ id,
9887
+ name: String(item?.name || name2).slice(0, 120),
9888
+ kind: DIAGNOSE_IMAGE_EXT.has(ext) ? "image" : "video",
9889
+ size: Number(item?.size) || fs14.statSync(file).size,
9890
+ path: file
9891
+ });
9892
+ }
9893
+ return out;
9894
+ }
9895
+ function materializeDiagnoseMedia(projectPath, media) {
9896
+ if (!media.length) return [];
9897
+ const assetsDir = path14.join(projectPath, "assets");
9898
+ fs14.mkdirSync(assetsDir, { recursive: true });
9899
+ return media.map((item, index) => {
9900
+ const ext = path14.extname(item.path);
9901
+ const raw = String(item.name || path14.basename(item.path)).replace(/[^\w.\-]+/g, "_").slice(-60);
9902
+ const safe = raw || `media-${index + 1}${ext}`;
9903
+ const target = path14.join(assetsDir, `${String(index + 1).padStart(2, "0")}-${safe}`);
9904
+ if (!fs14.existsSync(target)) {
9905
+ try {
9906
+ fs14.linkSync(item.path, target);
9907
+ } catch {
9908
+ fs14.copyFileSync(item.path, target);
9909
+ }
9910
+ }
9911
+ return { ...item, path: target };
9912
+ });
9913
+ }
9914
+ function serveDiagnoseMedia(req, res, id) {
9915
+ assertDiagnoseMember();
9916
+ if (!/^[0-9a-f-]{36}$/i.test(id)) return send(res, 404, { error: "\u627E\u4E0D\u5230\u7D20\u6750" });
9917
+ const dir = path14.join(DIAGNOSE_MEDIA_DIR, id);
9918
+ if (!fs14.existsSync(dir)) return send(res, 404, { error: "\u627E\u4E0D\u5230\u7D20\u6750" });
9919
+ const name2 = fs14.readdirSync(dir).find((entry) => entry.startsWith("source."));
9920
+ if (!name2) return send(res, 404, { error: "\u627E\u4E0D\u5230\u7D20\u6750" });
9921
+ const file = path14.join(dir, name2);
9922
+ const type = DIAGNOSE_MEDIA_TYPES.get(path14.extname(name2).toLowerCase()) || "application/octet-stream";
9923
+ const stat = fs14.statSync(file);
9924
+ const headers = {
9925
+ "content-type": type,
9926
+ "accept-ranges": "bytes",
9927
+ "cache-control": "no-store"
9928
+ };
9929
+ const match = /^bytes=(\d+)-(\d*)$/.exec(String(req.headers.range || ""));
9930
+ if (match) {
9931
+ const start = Number(match[1]);
9932
+ const end = match[2] ? Math.min(Number(match[2]), stat.size - 1) : stat.size - 1;
9933
+ if (!Number.isFinite(start) || start > end || start >= stat.size) {
9934
+ res.writeHead(416, { "content-range": `bytes */${stat.size}` });
9935
+ return void res.end();
9936
+ }
9937
+ res.writeHead(206, { ...headers, "content-range": `bytes ${start}-${end}/${stat.size}`, "content-length": end - start + 1 });
9938
+ return void fs14.createReadStream(file, { start, end }).pipe(res);
9939
+ }
9940
+ res.writeHead(200, { ...headers, "content-length": stat.size });
9941
+ return void fs14.createReadStream(file).pipe(res);
9942
+ }
9943
+ function diagnoseReports() {
9944
+ if (!fs14.existsSync(DIAGNOSE_DIR)) return [];
9945
+ const rows = [];
9946
+ for (const name2 of fs14.readdirSync(DIAGNOSE_DIR)) {
9947
+ if (!/^[a-zA-Z0-9-]{8,64}\.json$/.test(name2)) continue;
9948
+ try {
9949
+ const item = JSON.parse(fs14.readFileSync(path14.join(DIAGNOSE_DIR, name2), "utf8"));
9950
+ if (item?.id && item?.snapshot) rows.push(item);
9951
+ } catch {
9952
+ }
9953
+ }
9954
+ return rows.sort((a, b2) => String(b2.createdAt || "").localeCompare(String(a.createdAt || "")));
9955
+ }
9956
+ function saveDiagnoseReport(snapshot, analysis, sessionId, extra = {}) {
9957
+ const text = String(analysis || "").slice(0, 2e4);
9958
+ const media = (Array.isArray(extra.media) ? extra.media : []).slice(0, 12).map((item) => ({
9959
+ id: String(item.id || ""),
9960
+ name: String(item.name || "").slice(0, 120),
9961
+ kind: item.kind === "video" ? "video" : "image",
9962
+ size: Number(item.size) || 0
9963
+ }));
9964
+ const record = {
9965
+ id: crypto8.randomUUID(),
9966
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
9967
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
9968
+ title: String(snapshot.title || snapshot.description || "\u672A\u547D\u540D\u8BCA\u65AD").slice(0, 80),
9969
+ snapshot,
9970
+ analysis: text,
9971
+ sessionId: String(sessionId || "").slice(0, 80),
9972
+ projectPath: String(extra.projectPath || "").slice(0, 400),
9973
+ media
9974
+ };
9975
+ const raw = JSON.stringify(record);
9976
+ if (Buffer.byteLength(raw) > 128 * 1024) {
9977
+ throw Object.assign(new Error("\u62A5\u544A\u592A\u5927\uFF0C\u4FDD\u5B58\u5931\u8D25"), { status: 413 });
9978
+ }
9979
+ fs14.mkdirSync(DIAGNOSE_DIR, { recursive: true });
9980
+ fs14.writeFileSync(path14.join(DIAGNOSE_DIR, `${record.id}.json`), raw, "utf8");
9981
+ for (const old of diagnoseReports().slice(DIAGNOSE_MAX_REPORTS)) {
9982
+ try {
9983
+ fs14.rmSync(path14.join(DIAGNOSE_DIR, `${old.id}.json`), { force: true });
9984
+ } catch {
9985
+ }
9986
+ }
9987
+ return record;
9988
+ }
9989
+ function diagnosePrompt(snapshot, opts = {}) {
9990
+ const data = snapshot || {};
9991
+ const stats = data.stats || {};
9992
+ const statLine = [
9993
+ stats.likes != null ? `\u70B9\u8D5E ${stats.likes}` : "",
9994
+ stats.comments != null ? `\u8BC4\u8BBA ${stats.comments}` : "",
9995
+ stats.collects != null ? `\u6536\u85CF ${stats.collects}` : "",
9996
+ stats.shares != null ? `\u8F6C\u53D1 ${stats.shares}` : "",
9997
+ stats.followers != null ? `\u7C89\u4E1D ${stats.followers}` : "",
9998
+ stats.works != null ? `\u4F5C\u54C1 ${stats.works}` : ""
9999
+ ].filter(Boolean).join(" \xB7 ");
10000
+ const media = Array.isArray(opts.media) ? opts.media : [];
10001
+ const projectPath = String(opts.projectPath || "");
10002
+ const skillPath = String(opts.skillPath || "");
10003
+ const hasVideo = media.some((item) => item.kind === "video");
10004
+ return [
10005
+ "\u8BF7\u6267\u884C\u4E00\u6B21\u300C\u6296\u97F3\u8D26\u53F7\u8BCA\u65AD\u300D\u3002\u8FD9\u662F\u8D26\u53F7\u7EA7\u6570\u636E\u8BCA\u65AD\uFF0C\u4E0D\u8981\u9000\u5316\u4E3A\u5355\u6761\u89C6\u9891\u70B9\u8BC4\u3002",
10006
+ projectPath ? `\u9879\u76EE\u76EE\u5F55\uFF1A${projectPath}` : "",
10007
+ skillPath ? `\u8BCA\u65AD\u6280\u80FD\u76EE\u5F55\uFF1A${skillPath}` : "",
10008
+ "\u5F00\u59CB\u524D\u5FC5\u987B\u5B8C\u6574\u9605\u8BFB\u6280\u80FD\u76EE\u5F55\u4E2D\u7684 SKILL.md\uFF0C\u5E76\u4E25\u683C\u9075\u5FAA\u5176\u4E2D\u7684\u6267\u884C\u6D41\u7A0B\u3001\u786C\u6027\u89C4\u5219\u548C\u8D28\u91CF\u81EA\u68C0\u6E05\u5355\u3002\u5FC5\u987B\u540C\u65F6\u8BFB\u53D6 references/diagnosis-framework.md\u3001references/prescription-library.md \u548C assets/report-template.md\uFF1B\u5904\u65B9\u53EA\u80FD\u4F7F\u7528\u8BE5\u5904\u65B9\u5E93\u4E2D\u53EF\u6EAF\u6E90\u7684\u6765\u6E90\uFF0C\u4E0D\u5F97\u81EA\u884C\u7F16\u9020\u51FA\u5904\u3002",
10009
+ "",
10010
+ media.length ? "\u7528\u6237\u4E0A\u4F20\u7684\u7D20\u6750\uFF1A" : "\u7528\u6237\u6CA1\u6709\u4E0A\u4F20\u7D20\u6750\uFF0C\u53EA\u80FD\u4F7F\u7528\u4E0B\u65B9\u8865\u5145\u4FE1\u606F\uFF1B\u4E0D\u8981\u56E0\u6B64\u7F16\u9020\u8D26\u53F7\u6570\u636E\u3002",
10011
+ ...media.map((item, index) => `${index + 1}. ${item.kind === "video" ? "\u89C6\u9891" : "\u56FE\u7247"}\uFF1A${item.path}`),
10012
+ media.some((item) => item.kind === "image") ? "\u540E\u53F0\u622A\u56FE\u5FC5\u987B\u9010\u5F20\u76F4\u63A5\u67E5\u770B\u3002\u5148\u63D0\u53D6\u4F5C\u54C1\u6807\u9898\u3001\u53D1\u5E03\u65F6\u95F4\u3001\u64AD\u653E\u3001\u70B9\u8D5E\u3001\u8BC4\u8BBA\u3001\u5206\u4EAB\u3001\u7C89\u4E1D\u7B49\u6240\u6709\u53EF\u8BFB\u6570\u636E\uFF0C\u518D\u9010\u9879\u6807\u6CE8\u300C\u4E8B\u5B9E / \u4F4E\u7F6E\u4FE1 / \u7F3A\u5931\u300D\uFF1B\u6A21\u7CCA\u8BFB\u6570\u4E0D\u5F97\u5F53\u4E8B\u5B9E\u3002" : "",
10013
+ hasVideo ? `\u89C6\u9891\u4E0D\u8981\u51ED\u7A7A\u731C\u5185\u5BB9\uFF1A\u5148\u7528 ffprobe \u8BFB\u65F6\u957F\uFF0C\u518D\u7528 ffmpeg \u62BD 6-9 \u5F20\u5173\u952E\u5E27\u5230\u9879\u76EE\u76EE\u5F55 frames/\uFF0C\u7136\u540E\u76F4\u63A5\u67E5\u770B\u8FD9\u4E9B\u5E27\u3002\u4F8B\u5982\uFF1Affmpeg -i "<\u89C6\u9891\u8DEF\u5F84>" -vf "fps=1/2,scale=720:-1" frames/f%03d.jpg\uFF1B\u955C\u5934\u53D8\u5316\u5927\u65F6\u53EF\u7528 -vf "select='gt(scene,0.3)',scale=720:-1" -vsync vfr\u3002\u9700\u8981\u53E3\u64AD\u65F6\u518D\u62BD\u97F3\u9891\uFF1Bffmpeg \u4E0D\u53EF\u7528\u5C31\u6362\u4F60\u80FD\u7528\u7684\u672C\u5730\u5DE5\u5177\uFF0C\u5E76\u8BF4\u660E\u3002\u89C6\u9891\u53EA\u80FD\u4F5C\u4E3A\u5185\u5BB9\u516C\u5F0F\u7B49\u7EF4\u5EA6\u7684\u8BC1\u636E\uFF0C\u4E0D\u80FD\u66FF\u4EE3\u8D26\u53F7\u540E\u53F0\u4F5C\u54C1\u6570\u636E\u3002` : "",
10014
+ "",
10015
+ data.author ? `\u4F5C\u8005 / \u8D26\u53F7\uFF1A${data.author}${data.authorId ? `\uFF08${data.authorId}\uFF09` : ""}` : "",
10016
+ data.music ? `\u914D\u4E50\uFF1A${data.music}` : "",
10017
+ statLine ? `\u5DF2\u63D0\u4F9B\u6570\u636E\uFF1A${statLine}` : "",
10018
+ data.notice ? `\u8BFB\u53D6\u63D0\u793A\uFF1A${data.notice}` : "",
10019
+ "",
10020
+ "\u7528\u6237\u8865\u5145\u7684\u6587\u6848\u3001\u6570\u636E\u6216\u8BCA\u65AD\u8BC9\u6C42\uFF1A",
10021
+ String(data.text || data.description || "").slice(0, 6e3) || "\uFF08\u65E0\uFF09",
10022
+ "",
10023
+ "\u4E25\u683C\u6309\u7167 assets/report-template.md \u8F93\u51FA\u5B8C\u6574\u8D26\u53F7\u8BCA\u65AD\u62A5\u544A\uFF0C\u4E0D\u9650\u5236\u4E3A\u7B80\u77ED\u70B9\u8BC4\u3002\u81F3\u5C11\u843D\u5B9E\uFF1A\u4E0D\u8D85\u8FC7 3 \u53E5\u7684\u4E00\u53E5\u8BDD\u7ED3\u8BBA\u3001\u6570\u636E\u4E8B\u5B9E\u4E0E\u7F6E\u4FE1\u5EA6\u3001\u57FA\u7EBF\u6307\u6807\u3001\u5C0F\u65F6\u7EA7\u53D1\u5E03\u65F6\u6BB5\u5206\u6790\u3001\u56DB\u7EF4\u8BCA\u65AD\u3001\u5185\u5BB9\u7C7B\u578B\u5224\u65AD\u3001\u56DB\u8981\u7D20\u9F50\u5168\u4E14\u6709\u51FA\u5904\u7684\u4F18\u5316\u5904\u65B9\u300130 \u5929\u53EF\u9A8C\u6536\u6307\u6807\u3001\u5F85\u8865\u8F93\u5165\u6E05\u5355\u3001\u8FED\u4EE3\u8BB0\u5F55\u3001\u65B9\u6CD5\u8BBA\u6765\u6E90\u6E05\u5355\u3002",
10024
+ "\u4F5C\u54C1\u5217\u8868\u4E0D\u8DB3 5 \u6761\u6216\u5B57\u6BB5\u4E0D\u5168\u65F6\u4ECD\u5148\u8BCA\u65AD\uFF0C\u4F46\u5FC5\u987B\u5728\u300A\u5F85\u8865\u8F93\u5165\u6E05\u5355\u300B\u8BF4\u660E\u5F71\u54CD\uFF1B\u6837\u672C\u4E0D\u8DB3\u4E0D\u5F97\u5F52\u56E0\uFF0C\u6309 SOP \u7ED9\u51FA\u63A7\u5236\u53D8\u91CF\u7684 A/B \u6D4B\u8BD5\u3002\u4E0D\u8981\u8F93\u51FA\u201C\u591A\u53D1\u4F18\u8D28\u5185\u5BB9\u201D\u7B49\u65E0\u51FA\u5904\u5EFA\u8BAE\u3002",
10025
+ "\u6807\u9898\u548C\u6587\u5B57\u8F6C\u5F55\u53EA\u80FD\u8BC1\u660E\u4E3B\u9898\uFF0C\u4E0D\u80FD\u8BC1\u660E\u53E3\u64AD\u3001\u5267\u60C5\u3001\u5B9E\u9645\u5F00\u573A\u6216\u955C\u5934\u8868\u73B0\uFF1B\u5355\u6761\u89C6\u9891\u4E5F\u4E0D\u80FD\u4EE3\u8868\u5168\u90E8\u4F5C\u54C1\u3002\u4F5C\u54C1\u5F62\u6001\u7F3A\u5931\u65F6\uFF0C\u5185\u5BB9\u7C7B\u578B\u53EA\u80FD\u6807\u4E3A\u300C\u6309\u4E3B\u9898\u6682\u5B9A / \u5F62\u6001\u5F85\u9A8C\u8BC1\u300D\uFF0C\u7C7B\u578B\u9650\u5B9A\u5904\u65B9\u53EA\u80FD\u6761\u4EF6\u9002\u7528\uFF0C\u5E76\u628A\u6240\u7F3A\u8BC1\u636E\u5217\u5165\u5F85\u8865\u8F93\u5165\u3002",
10026
+ "\u6807\u9898\u4E2D\u7684\u5E74\u9F84\u3001\u8EAB\u4EFD\u3001\u804C\u4E1A\u3001\u7ECF\u5386\u7B49\u5B57\u9762\u4FE1\u606F\u53EF\u6309\u7F6E\u4FE1\u5EA6\u5F15\u7528\uFF0C\u4F46\u4E0D\u5F97\u6269\u5C55\u6210\u672A\u63D0\u4F9B\u7684\u80CC\u666F\u6545\u4E8B\uFF1B\u5185\u5BB9\u7EBF\u53EA\u80FD\u6309\u5DF2\u6709\u8BC1\u636E\u79F0\u4E3A\u4E3B\u9898 / \u9009\u9898\u7EBF\uFF0C\u4E0D\u80FD\u51ED\u6807\u9898\u5224\u5B9A\u53D9\u4E8B\u5F62\u6001\uFF0C\u793A\u4F8B\u4E3B\u7EBF\u540C\u6837\u4E0D\u5F97\u65B0\u589E\u4EBA\u7269\u7ECF\u5386\u3002",
10027
+ "\u300C\u51E0\u4E4E\u4E3A 0\u300D\u300C\u7EA6\u300D\u7B49\u6982\u8FF0\u4E0D\u662F\u7CBE\u786E\u6570\u503C\u3002\u5047\u8BBE\u60C5\u666F\u8BA1\u7B97\u4E0D\u5F97\u5199\u6210\u4E0A\u754C\u3001\u4E0B\u754C\u6216\u9608\u503C\u5224\u5B9A\u8BC1\u636E\uFF1B\u4E92\u52A8\u7ED3\u679C\u4E0E\u4E92\u52A8\u8BBE\u8BA1\u539F\u56E0\u5FC5\u987B\u5206\u5F00\uFF0C\u8BC1\u636E\u4E0D\u8DB3\u5C31\u6807\u300C\u8BC1\u636E\u4E0D\u5B8C\u6574\u300D\u3002",
10028
+ "A/B \u6D4B\u8BD5\u6BCF\u7EC4 \u22653 \u6761\u4EC5\u7528\u4E8E\u65B9\u5411\u6027\u89C2\u5BDF\uFF0C\u4E0D\u5F97\u636E\u6B64\u8BC1\u660E\u300C\u65F6\u6BB5\u5047\u8BBE\u6210\u7ACB\u300D\u6216\u300C\u65E0\u65F6\u6BB5\u6548\u5E94\u300D\uFF1B\u8FBE\u5230\u6700\u4F4E\u6761\u6570\u4E5F\u4E0D\u7B49\u4E8E\u53EF\u4EE5\u5F52\u56E0\uFF0C\u65F6\u523B\u7CBE\u5EA6\u4E0D\u8DB3\u3001\u4E3B\u9898\u6DF7\u6742\u6216\u63A7\u5236\u53D8\u91CF\u4E0D\u6210\u7ACB\u65F6\u4ECD\u987B\u7EE7\u7EED\u6269\u6837\u3002",
10029
+ "\u6240\u6709\u6D4B\u8BD5\u4E0E\u680F\u76EE\u5FC5\u987B\u505A 30 \u5929\u4EA7\u80FD\u6838\u7B97\uFF1A\u6392\u671F\u6761\u6570\u4E0E\u66F4\u65B0\u9891\u7387\u4E00\u81F4\uFF0C\u4E0D\u540C\u5185\u5BB9\u7C7B\u578B\u4E0D\u5F97\u91CD\u590D\u8BA1\u6570\uFF1B\u5F53\u524D\u4EA7\u80FD\u653E\u4E0D\u4E0B\u65F6\uFF0C\u628A\u9A8C\u8BC1\u7A97\u53E3\u5EF6\u4F38\u5230 60 \u5929\uFF0C\u4E0D\u5F97\u4FDD\u7559\u4E92\u76F8\u51B2\u7A81\u7684\u6570\u91CF\u76EE\u6807\u3002",
10030
+ "\u5E73\u5747\u64AD\u653E\u65F6\u957F\u3001\u5B8C\u64AD\u7387\u300130 \u79D2\u7559\u5B58\u7B49\u5B57\u6BB5\u5404\u81EA\u72EC\u7ACB\uFF0C\u53EA\u8BB0\u5F55\u540E\u53F0\u5B9E\u9645\u63D0\u4F9B\u7684\u5B57\u6BB5\uFF0C\u4E0D\u5F97\u76F8\u4E92\u6362\u7B97\u6216\u865A\u6784\u6307\u6807\u3002\u81EA\u8BBE\u9636\u6BB5\u76EE\u6807\u5FC5\u987B\u6807\u660E\u4E3A\u8D26\u53F7\u7BA1\u7406\u76EE\u6807\uFF0C\u4E0D\u5F97\u5199\u6210\u5BF9\u9F50\u672A\u63D0\u4F9B\u7684\u5B98\u65B9\u6216\u540C\u7C7B\u53C2\u8003\u503C\u3002",
10031
+ "\u65B9\u6CD5\u8BBA\u6765\u6E90\u6E05\u5355\u53EA\u5217\u5904\u65B9\u5E93\u6765\u6E90\u6E05\u5355\u4E2D\u7684\u5B9E\u9645\u5F15\u7528\u9879\uFF1B\u4E0D\u5F97\u65B0\u589E\u300C\u6D41\u91CF\u6C60\u673A\u5236\u300D\u7B49\u65E0\u72EC\u7ACB\u51FA\u5904\u6761\u76EE\uFF0C\u4E5F\u4E0D\u5F97\u628A\u9AD8\u64AD\u653E\u89E3\u91CA\u6210\u5DF2\u8BC1\u5B9E\u7684\u5E73\u53F0\u673A\u5236\u3002",
10032
+ "\u4EA4\u4ED8\u524D\u9010\u9879\u6267\u884C SKILL.md \u7684\u8D28\u91CF\u81EA\u68C0\u6E05\u5355\uFF1B\u53D1\u73B0\u7F3A\u9879\u5148\u4FEE\u6B63\uFF0C\u518D\u4FDD\u5B58\u3002\u53EA\u6839\u636E\u7D20\u6750\u548C\u5DF2\u63D0\u4F9B\u4FE1\u606F\u5224\u65AD\uFF0C\u4E0D\u7F16\u9020\u753B\u9762\u3001\u6570\u5B57\u3001\u4F5C\u8005\u80CC\u666F\u6216\u5E73\u53F0\u673A\u5236\u3002",
10033
+ projectPath ? `\u5B8C\u6210\u540E\u628A\u6700\u7EC8\u5B8C\u6574\u62A5\u544A\u5199\u5165 ${projectPath}/report.md\uFF08UTF-8\u3001\u8986\u76D6\u5199\uFF09\uFF0C\u7136\u540E\u56DE\u590D\u300C\u5DF2\u5199\u5165 report.md\u300D\u3002` : "",
10034
+ media.length ? "\u4E0D\u8981\u4FEE\u6539\u6216\u5220\u9664\u7528\u6237\u4E0A\u4F20\u7684\u7D20\u6750\u3002" : ""
10035
+ ].join("\n").replace(/\n{3,}/g, "\n\n");
10036
+ }
10037
+ function growthError(message, status = 400) {
10038
+ return Object.assign(new Error(message), { status });
10039
+ }
10040
+ function growthKind(value) {
10041
+ const kind = String(value || "");
10042
+ if (!Object.hasOwn(GROWTH_LABELS, kind)) throw growthError("\u672A\u77E5\u7684\u8D26\u53F7\u589E\u957F\u529F\u80FD");
10043
+ return kind;
10044
+ }
10045
+ function growthRecordId(value) {
10046
+ const id = String(value || "");
10047
+ if (!/^[0-9a-f-]{36}$/i.test(id)) throw growthError("\u6863\u6848 ID \u65E0\u6548");
10048
+ return id;
10049
+ }
10050
+ function cleanGrowthInput(kind, raw) {
10051
+ const source = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
10052
+ const text = (key, max) => String(source[key] || "").slice(0, max);
10053
+ if (kind === "research") {
10054
+ return {
10055
+ accounts: text("accounts", 1e3),
10056
+ links: text("links", 4e3),
10057
+ positioning: text("positioning", 4e3),
10058
+ notes: text("notes", 6e3)
10059
+ };
10060
+ }
10061
+ if (kind === "planner") {
10062
+ const startDate = text("startDate", 10);
10063
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(startDate)) throw growthError("\u8BF7\u9009\u62E9\u6709\u6548\u7684\u5F00\u59CB\u65E5\u671F");
10064
+ const frequency = Number(source.frequency ?? 3);
10065
+ if (!Number.isInteger(frequency) || frequency < 1 || frequency > 14) throw growthError("\u6BCF\u5468\u53D1\u5E03\u4E0A\u9650\u987B\u4E3A 1\u201314 \u6761");
10066
+ return {
10067
+ diagnosisId: text("diagnosisId", 80),
10068
+ positioning: text("positioning", 4e3),
10069
+ startDate,
10070
+ frequency,
10071
+ notes: text("notes", 6e3)
10072
+ };
10073
+ }
10074
+ return { baselineId: text("baselineId", 80), currentId: text("currentId", 80), notes: text("notes", 6e3) };
10075
+ }
10076
+ function requireDiagnosisReport(id) {
10077
+ if (!/^[0-9a-f-]{36}$/i.test(String(id || ""))) throw growthError("\u4F53\u68C0\u62A5\u544A ID \u65E0\u6548");
10078
+ const report = diagnoseReports().find((item) => item.id === id);
10079
+ if (!report || !String(report.analysis || "").trim()) throw growthError("\u627E\u4E0D\u5230\u5DF2\u5B8C\u6210\u7684\u4F53\u68C0\u62A5\u544A\uFF0C\u8BF7\u5148\u56DE\u586B\u62A5\u544A");
10080
+ return report;
10081
+ }
10082
+ function growthLinksFromInput(kind, input) {
10083
+ if (kind === "planner") return { diagnosisId: input.diagnosisId || "" };
10084
+ if (kind === "followup") return { baselineId: input.baselineId || "", currentId: input.currentId || "" };
10085
+ return {};
10086
+ }
10087
+ function growthRecordTitle(kind, input) {
10088
+ if (kind === "research") return `${input.accounts.split(/[,,\n]/).map((x) => x.trim()).filter(Boolean)[0] || "\u5BF9\u6807\u8D26\u53F7"} \xB7 \u5BF9\u6807\u8C03\u7814`;
10089
+ if (kind === "planner") {
10090
+ const diagnosis = input.diagnosisId ? diagnoseReports().find((item) => item.id === input.diagnosisId) : null;
10091
+ return `${diagnosis?.title || input.positioning || "\u8D26\u53F7"} \xB7 \u9009\u9898\u89C4\u5212`.slice(0, 100);
10092
+ }
10093
+ const current = input.currentId ? diagnoseReports().find((item) => item.id === input.currentId) : null;
10094
+ return `${current?.title || "\u8D26\u53F7"} \xB7 \u590D\u8BCA\u5BF9\u6BD4`.slice(0, 100);
10095
+ }
10096
+ function growthRecords(kind) {
10097
+ const dir = path14.join(GROWTH_DIR, kind);
10098
+ if (!fs14.existsSync(dir)) return [];
10099
+ const rows = [];
10100
+ for (const name2 of fs14.readdirSync(dir)) {
10101
+ if (!/^[0-9a-f-]{36}\.json$/i.test(name2)) continue;
10102
+ try {
10103
+ const item = JSON.parse(fs14.readFileSync(path14.join(dir, name2), "utf8"));
10104
+ if (item?.id && item?.kind === kind) rows.push(item);
10105
+ } catch {
10106
+ }
10107
+ }
10108
+ return rows.sort((a, b2) => String(b2.createdAt || "").localeCompare(String(a.createdAt || "")));
10109
+ }
10110
+ function findGrowthRecord({ kind = "", id = "", sessionId = "" } = {}) {
10111
+ const kinds = kind ? [growthKind(kind)] : Object.keys(GROWTH_LABELS);
10112
+ for (const itemKind of kinds) {
10113
+ const hit = growthRecords(itemKind).find((item) => id && item.id === id || sessionId && item.sessionId === sessionId);
10114
+ if (hit) return hit;
10115
+ }
10116
+ return null;
10117
+ }
10118
+ function writeGrowthRecord(record) {
10119
+ const raw = JSON.stringify(record);
10120
+ if (Buffer.byteLength(raw) > 128 * 1024) throw growthError("\u62A5\u544A\u592A\u5927\uFF0C\u4FDD\u5B58\u5931\u8D25", 413);
10121
+ const dir = path14.join(GROWTH_DIR, record.kind);
10122
+ fs14.mkdirSync(dir, { recursive: true });
10123
+ const target = path14.join(dir, `${record.id}.json`);
10124
+ const temp = `${target}.tmp`;
10125
+ fs14.writeFileSync(temp, raw, "utf8");
10126
+ fs14.renameSync(temp, target);
10127
+ for (const old of growthRecords(record.kind).slice(60)) {
10128
+ try {
10129
+ fs14.rmSync(path14.join(dir, `${old.id}.json`), { force: true });
10130
+ } catch {
10131
+ }
10132
+ }
10133
+ return record;
10134
+ }
10135
+ function createGrowthRecord(kind, input, extra = {}) {
10136
+ const now = (/* @__PURE__ */ new Date()).toISOString();
10137
+ return writeGrowthRecord({
10138
+ id: crypto8.randomUUID(),
10139
+ kind,
10140
+ title: growthRecordTitle(kind, input),
10141
+ createdAt: now,
10142
+ updatedAt: now,
10143
+ input,
10144
+ analysis: String(extra.analysis || "").slice(0, 4e4),
10145
+ sessionId: String(extra.sessionId || "").slice(0, 100),
10146
+ projectPath: String(extra.projectPath || "").slice(0, 400),
10147
+ media: Array.isArray(extra.media) ? extra.media : [],
10148
+ links: growthLinksFromInput(kind, input)
10149
+ });
10150
+ }
10151
+ function diagnosisContext(report) {
10152
+ return [
10153
+ `# ${report.title || "\u8D26\u53F7\u4F53\u68C0"}`,
10154
+ `\u4F53\u68C0\u65F6\u95F4\uFF1A${report.createdAt || "\u672A\u8BB0\u5F55"}`,
10155
+ `\u8D26\u53F7\u5FEB\u7167\uFF1A${JSON.stringify(report.snapshot || {}).slice(0, 12e3)}`,
10156
+ "",
10157
+ String(report.analysis || "")
10158
+ ].join("\n");
10159
+ }
10160
+ function resolveGrowthContext(kind, input) {
10161
+ if (kind === "research") {
10162
+ if (!String(input.accounts || "").trim()) throw growthError("\u8BF7\u586B\u5199\u81F3\u5C11\u4E00\u4E2A\u5BF9\u6807\u8D26\u53F7\u540D");
10163
+ return {};
10164
+ }
10165
+ if (kind === "planner") {
10166
+ const diagnosis = input.diagnosisId ? requireDiagnosisReport(input.diagnosisId) : null;
10167
+ if (!diagnosis && !String(input.positioning || "").trim()) {
10168
+ throw growthError("\u8BF7\u9009\u62E9\u4F53\u68C0\u62A5\u544A\uFF0C\u6216\u586B\u5199\u8D26\u53F7\u5B9A\u4F4D");
10169
+ }
10170
+ return { diagnosis };
10171
+ }
10172
+ if (!input.baselineId || !input.currentId) throw growthError("\u8BF7\u9009\u62E9\u57FA\u7EBF\u4F53\u68C0\u548C\u672C\u6B21\u4F53\u68C0");
10173
+ if (input.baselineId === input.currentId) throw growthError("\u590D\u8BCA\u9700\u8981\u9009\u62E9\u4E24\u4EFD\u4E0D\u540C\u7684\u4F53\u68C0\u62A5\u544A");
10174
+ return {
10175
+ baseline: requireDiagnosisReport(input.baselineId),
10176
+ current: requireDiagnosisReport(input.currentId)
10177
+ };
10178
+ }
10179
+ function safeReportPath(projectPath) {
10180
+ const root = path14.resolve(DIAGNOSE_SESSIONS_DIR);
10181
+ const dir = path14.resolve(String(projectPath || ""));
10182
+ if (!dir.startsWith(root + path14.sep)) return null;
10183
+ return path14.join(dir, "report.md");
10184
+ }
10185
+ function growthPrompt(kind, input, media = [], linked = {}) {
10186
+ const projectPath = String(linked.projectPath || "");
10187
+ const skillPath = String(linked.skillPath || "");
10188
+ const common = [
10189
+ projectPath ? `\u9879\u76EE\u76EE\u5F55\uFF1A${projectPath}` : "",
10190
+ skillPath ? `\u6280\u80FD\u76EE\u5F55\uFF1A${skillPath}` : "",
10191
+ "\u5F00\u59CB\u524D\u5FC5\u987B\u5B8C\u6574\u9605\u8BFB\u4E0A\u8FF0\u6280\u80FD\u76EE\u5F55\u4E2D\u7684 SKILL.md\uFF0C\u4E25\u683C\u6267\u884C\u5176\u4E2D\u7684\u6D41\u7A0B\u3001\u786C\u6027\u89C4\u5219\u548C\u8D28\u91CF\u81EA\u68C0\u6E05\u5355\u3002",
10192
+ "\u53EA\u4F7F\u7528\u672C\u673A\u7D20\u6750\u3001\u7528\u6237\u8F93\u5165\u548C\u5B9E\u9645\u53EF\u8BBF\u95EE\u7684\u516C\u5F00\u4FE1\u606F\uFF1B\u4E0D\u5F97\u7F16\u9020\u6570\u636E\u3001\u4F5C\u54C1\u3001\u4EBA\u7269\u7ECF\u5386\u3001\u6765\u6E90\u6216\u5E73\u53F0\u673A\u5236\u3002",
10193
+ projectPath ? `\u5B8C\u6210\u540E\u628A\u6700\u7EC8\u5B8C\u6574\u62A5\u544A\u5199\u5165 ${projectPath}/report.md\uFF08UTF-8\u3001\u8986\u76D6\u5199\uFF09\uFF0C\u7136\u540E\u56DE\u590D\u300C\u5DF2\u5199\u5165 report.md\u300D\u3002` : "",
10194
+ media.length ? "\u4E0D\u8981\u4FEE\u6539\u6216\u5220\u9664\u7528\u6237\u4E0A\u4F20\u7684\u7D20\u6750\u3002" : ""
10195
+ ];
10196
+ if (kind === "research") {
10197
+ return [
10198
+ "\u8BF7\u6267\u884C\u4E00\u6B21\u72EC\u7ACB\u7684\u300C\u6296\u97F3\u5BF9\u6807\u8C03\u7814\u300D\uFF0C\u76EE\u6807\u662F\u63D0\u70BC\u53EF\u8FC1\u79FB\u7684\u5185\u5BB9\u7ED3\u6784\uFF0C\u4E0D\u662F\u5199\u8D26\u53F7\u7B80\u4ECB\u6216\u4E3B\u89C2\u5938\u8D5E\u3002",
10199
+ ...common.slice(0, 3),
10200
+ "\u540C\u65F6\u5B8C\u6574\u9605\u8BFB references/research-checklist.md\uFF0C\u5E76\u6309\u5176\u9010\u9879\u6838\u67E5\u3002",
10201
+ "",
10202
+ `\u5BF9\u6807\u8D26\u53F7\uFF1A${input.accounts}`,
10203
+ `\u4E3B\u9875\u6216\u4F5C\u54C1\u94FE\u63A5\uFF1A${input.links || "\uFF08\u672A\u63D0\u4F9B\uFF09"}`,
10204
+ `\u7528\u6237\u81EA\u5DF1\u7684\u8D26\u53F7\u5B9A\u4F4D\uFF1A${input.positioning || "\uFF08\u672A\u63D0\u4F9B\uFF1B\u53EF\u8FC1\u79FB\u6027\u53EA\u80FD\u6807\u4E3A\u5F85\u5224\u65AD\uFF09"}`,
10205
+ `\u8865\u5145\u8BF4\u660E\u4E0E\u771F\u5B9E\u8D44\u6E90\uFF1A${input.notes || "\uFF08\u65E0\uFF09"}`,
10206
+ "",
10207
+ media.length ? "\u7528\u6237\u4E0A\u4F20\u7684\u672C\u673A\u7D20\u6750\uFF1A" : "\u7528\u6237\u6CA1\u6709\u4E0A\u4F20\u622A\u56FE\u6216\u89C6\u9891\uFF1B\u4E0D\u5F97\u865A\u6784\u4F5C\u54C1\u5217\u8868\u548C\u4F5C\u54C1\u6570\u636E\u3002",
10208
+ ...media.map((item, index) => `${index + 1}. ${item.kind === "video" ? "\u89C6\u9891" : "\u56FE\u7247"}\uFF1A${item.path}`),
10209
+ media.some((item) => item.kind === "image") ? "\u6BCF\u5F20\u56FE\u7247\u90FD\u5FC5\u987B\u9010\u5F20\u76F4\u63A5\u67E5\u770B\uFF0C\u63D0\u53D6\u8D26\u53F7\u540D\u3001\u4F5C\u54C1\u6807\u9898\u3001\u53D1\u5E03\u65F6\u95F4\u548C\u53EF\u89C1\u4E92\u52A8\u6570\u636E\uFF0C\u5E76\u6807\u6CE8\u300C\u4E8B\u5B9E / \u4F4E\u7F6E\u4FE1 / \u7F3A\u5931\u300D\u3002" : "",
10210
+ media.some((item) => item.kind === "video") ? "\u6BCF\u4E2A\u89C6\u9891\u5148\u7528 ffprobe \u8BFB\u53D6\u65F6\u957F\uFF0C\u518D\u7528 ffmpeg \u62BD\u53D6 6\u20139 \u5F20\u5173\u952E\u5E27\u5230\u9879\u76EE\u76EE\u5F55 frames/ \u5E76\u9010\u5F20\u67E5\u770B\uFF1B\u9700\u8981\u53E3\u64AD\u8BC1\u636E\u65F6\u518D\u62BD\u97F3\u9891\u3002\u5DE5\u5177\u4E0D\u53EF\u7528\u65F6\u8BF4\u660E\u9650\u5236\uFF0C\u4E0D\u5F97\u731C\u753B\u9762\u3002" : "",
10211
+ "\u5982\u679C\u5177\u5907\u8054\u7F51\u6216\u7F51\u9875\u8BFB\u53D6\u80FD\u529B\uFF0C\u53EF\u8BBF\u95EE\u7528\u6237\u7ED9\u51FA\u7684\u94FE\u63A5\u5E76\u6838\u5B9E\u516C\u5F00\u62A5\u9053\u3001\u8BBF\u8C08\u548C\u672C\u4EBA\u516C\u5F00\u65B9\u6CD5\u8BBA\uFF0C\u9010\u6761\u9644 URL \u4E0E\u53D1\u5E03\u65E5\u671F\uFF1B\u6CA1\u6709\u8054\u7F51\u3001\u8BBF\u95EE\u5931\u8D25\u6216\u53EA\u6709\u8D26\u53F7\u540D\u65F6\uFF0C\u660E\u786E\u5199\u300C\u672C\u6B21\u672A\u6838\u5B9E\u516C\u5F00\u4FE1\u606F\u300D\uFF0C\u4E0D\u5F97\u5047\u88C5\u68C0\u7D22\u8FC7\u3002",
10212
+ "\u4E25\u683C\u533A\u5206\u300C\u516C\u5F00\u4FE1\u606F\uFF08\u9644\u6765\u6E90\u4E0E\u65F6\u95F4\uFF09/ \u4F5C\u54C1\u53EF\u89C1\u4E8B\u5B9E / \u63A8\u65AD / \u672A\u8BC1\u5B9E\u300D\u3002\u770B\u4E0D\u5230\u7684\u540E\u53F0\u6570\u636E\u3001\u6536\u5165\u3001\u56E2\u961F\u89C4\u6A21\u548C\u52A8\u673A\u4E00\u5F8B\u4E0D\u5F97\u63A8\u65AD\u6210\u4E8B\u5B9E\u3002",
10213
+ "\u5148\u6838\u5B9E\u672C\u4EBA\u662F\u5426\u6709\u516C\u5F00\u65B9\u6CD5\u8BBA\uFF1B\u6CA1\u6709\u53EF\u6EAF\u6E90\u65B9\u6CD5\u8BBA\u65F6\uFF0C\u660E\u786E\u964D\u7EA7\u4E3A\u300C\u4ECE\u4F5C\u54C1\u53CD\u63A8\u7684\u7ED3\u6784\u53C2\u8003\u300D\u3002\u8DE8\u5E73\u53F0\u5BF9\u8C61\u5FC5\u987B\u5148\u8BF4\u660E\u4E0E\u6296\u97F3\u7684\u5206\u53D1\u673A\u5236\u5DEE\u5F02\uFF0C\u4E0D\u5F97\u6362\u7B97\u6570\u636E\u8868\u73B0\u3002",
10214
+ "\u62A5\u544A\u5FC5\u987B\u5305\u542B\uFF1A\u4ED6\u662F\u8C01\u3001\u4F5C\u54C1\u6570\u636E\u8868\u3001\u4E94\u7EF4\u5185\u5BB9\u516C\u5F0F\uFF08\u8EAB\u4EFD\u53D9\u4E8B / \u9009\u9898\u6765\u6E90 / \u6807\u9898\u4E0E\u94A9\u5B50 / \u5F62\u5F0F\u4E0E\u8282\u594F / \u5206\u53D1\u4E0E\u53D8\u73B0\uFF0C\u6BCF\u7EF4\u9644\u4F5C\u54C1\u8BC1\u636E\u6216\u6807\u7F3A\u5931\uFF09\u3001\u53EF\u501F\u9274\u4E0E\u4E0D\u53EF\u7167\u642C\u6E05\u5355\u53CA\u8D44\u6E90\u7406\u7531\u3001\u4E0D\u8D85\u8FC7 3 \u6761\u4E0B\u5468\u53EF\u6267\u884C\u8FC1\u79FB\u52A8\u4F5C\u3001\u4FE1\u606F\u53EF\u9760\u6027\u8BF4\u660E\u3002",
10215
+ "\u8FC1\u79FB\u52A8\u4F5C\u5FC5\u987B\u5229\u7528\u7528\u6237\u73B0\u6709\u8D44\u6E90\uFF0C\u5199\u6E05\u7528\u4F55\u79CD\u66FF\u4EE3\u8DEF\u5F84\u8865\u8DB3\u5BF9\u65B9\u72EC\u6709\u8D44\u6E90\uFF1B\u4E0D\u5F97\u5EFA\u8BAE\u590D\u523B\u5177\u4F53\u9009\u9898\u3001\u6587\u6848\u6216\u4EBA\u8BBE\u3002\u8BC1\u636E\u4E0D\u8DB3\u7684\u7EF4\u5EA6\u4FDD\u7559\u7A7A\u7F3A\u5E76\u5217\u5165\u5F85\u8865\u7D20\u6750\u3002",
10216
+ ...common.slice(3)
10217
+ ].join("\n").replace(/\n{3,}/g, "\n\n");
10218
+ }
10219
+ if (kind === "planner") {
10220
+ const diagnosisFile = projectPath ? path14.join(projectPath, "inputs", "diagnosis-report.md") : "";
10221
+ return [
10222
+ "\u8BF7\u6267\u884C\u4E00\u6B21\u300C\u6296\u97F3\u9009\u9898\u89C4\u5212\u300D\uFF0C\u628A\u8D26\u53F7\u5B9A\u4F4D\u4E0E\u4F53\u68C0\u7ED3\u8BBA\u8F6C\u6210\u53EF\u62CD\u6444\u3001\u53EF\u9A8C\u6536\u7684\u771F\u5B9E\u5185\u5BB9\u65E5\u5386\u3002",
10223
+ ...common.slice(0, 3),
10224
+ "\u540C\u65F6\u5B8C\u6574\u9605\u8BFB references/formula-card.md\uFF1B\u5F15\u7528\u8BCA\u65AD\u5904\u65B9\u65F6\uFF0C\u9605\u8BFB\u76F8\u90BB account-diagnosis \u6280\u80FD\u7684 SKILL.md \u4E0E references/prescription-library.md\uFF0C\u4E0D\u5F97\u7F16\u9020\u65B9\u6CD5\u8BBA\u51FA\u5904\u3002",
10225
+ `\u8D26\u53F7\u5B9A\u4F4D\uFF1A${input.positioning || linked.diagnosis?.title || "\uFF08\u4EC5\u4F7F\u7528\u4F53\u68C0\u62A5\u544A\u4E2D\u7684\u5B9A\u4F4D\uFF0C\u7F3A\u5931\u65F6\u5148\u5217\u4E3A\u5F85\u8865\uFF09"}`,
10226
+ `\u5F00\u59CB\u65E5\u671F\uFF1A${input.startDate}`,
10227
+ `\u6BCF\u5468\u53D1\u5E03\u4E0A\u9650\uFF1A${input.frequency} \u6761\uFF08\u4E0D\u5F97\u8D85\u8FC7\uFF1B\u6570\u91CF\u4E0D\u8DB3\u4EE5\u8986\u76D6\u5168\u90E8\u7C7B\u578B\u65F6\u8BF4\u660E\u53D6\u820D\uFF0C\u4E0D\u5F97\u5077\u5077\u52A0\u91CF\uFF09`,
10228
+ `\u7528\u6237\u8865\u5145\u7684\u771F\u5B9E\u7D20\u6750\u3001\u65E5\u7A0B\u4E0E\u7EA6\u675F\uFF1A${input.notes || "\uFF08\u65E0\uFF1B\u4E0D\u5F97\u56E0\u6B64\u7F16\u9020\u53EF\u62CD\u4E8B\u4EF6\uFF09"}`,
10229
+ "",
10230
+ linked.diagnosis ? `\u5DF2\u5173\u8054\u4F53\u68C0\u62A5\u544A\uFF0C\u5168\u6587\u540C\u65F6\u4FDD\u5B58\u4E8E ${diagnosisFile}\uFF1B\u5FC5\u987B\u5148\u8BFB\u53D6\u8BE5\u6587\u4EF6\u3002\u4EE5\u4E0B\u4E3A\u540C\u4E00\u4EFD\u4F53\u68C0\u5168\u6587\uFF1A` : "\u672C\u6B21\u672A\u5173\u8054\u4F53\u68C0\u62A5\u544A\uFF0C\u53EA\u80FD\u4F9D\u636E\u4E0A\u65B9\u5B9A\u4F4D\u548C\u771F\u5B9E\u8865\u5145\u4FE1\u606F\u89C4\u5212\u3002",
10231
+ linked.diagnosis ? diagnosisContext(linked.diagnosis) : "",
10232
+ "",
10233
+ "\u4F53\u68C0\u4E2D\u7684\u7ED3\u8BBA\u987B\u843D\u5B9E\u5230\u9009\u9898\u8BBE\u8BA1\u903B\u8F91\uFF1A\u8BF4\u660E\u6BCF\u6761\u9009\u9898\u54CD\u5E94\u4E86\u54EA\u9879\u53D1\u73B0\u6216\u5904\u65B9\uFF1B\u4F53\u68C0\u540C\u6837\u8BC1\u636E\u4E0D\u8DB3\u7684\u7ED3\u8BBA\u4E0D\u5F97\u5F53\u6210\u4E8B\u5B9E\u653E\u5927\u3002",
10234
+ "\u6BCF\u6761\u9009\u9898\u5FC5\u987B\u7ED1\u5B9A\u7528\u6237\u771F\u5B9E\u5728\u505A\u7684\u4E8B\u3001\u771F\u5B9E\u53EF\u62CD\u4E8B\u4EF6\u6216\u771F\u5B9E\u516C\u5F00\u4FE1\u606F\uFF0C\u6CE8\u660E\u7D20\u6750\u6765\u6E90\uFF1B\u6CA1\u6709\u7D20\u6750\u5C31\u653E\u5165\u300C\u7D20\u6750\u5F85\u8865\u6E05\u5355\u300D\uFF0C\u4E0D\u5F97\u7F16\u9020\u7ECF\u5386\u3001\u4EA7\u54C1\u3001\u8BA2\u5355\u6216\u62CD\u6444\u6761\u4EF6\u3002",
10235
+ "\u8F93\u51FA\uFF1A\u5B9A\u4F4D\u590D\u8FF0\u4E0E\u672C\u5468\u7EA6\u675F\u3001\u4ECE\u5F00\u59CB\u65E5\u671F\u8D77\u7684\u4E00\u5468\u9009\u9898\u8868\uFF08\u65E5\u671F / \u6781\u7AEF\u5316\u6807\u9898 / 3 \u79D2\u94A9\u5B50 / \u5F62\u5F0F / \u7C7B\u578B / \u771F\u5B9E\u7D20\u6750\u6765\u6E90 / \u8BBE\u8BA1\u903B\u8F91\u4E0E\u51FA\u5904 / \u7ED3\u5C3E\u4E92\u52A8\u94A9\u5B50\uFF09\u3001\u9010\u6761\u516C\u5F0F\u5361\u3001\u672C\u5468\u6307\u6807\u4E0E\u68C0\u67E5\u70B9\u3001\u7D20\u6750\u5F85\u8865\u6E05\u5355\u3002",
10236
+ "\u81F3\u5C11\u5305\u542B 1 \u6761\u641C\u7D22\u9700\u6C42\u578B\u9009\u9898\uFF1B\u6709\u771F\u5B9E\u5728\u63A8\u8FDB\u7684\u4EA7\u54C1\u6216\u9879\u76EE\u65F6\u624D\u5B89\u6392\u4EA7\u54C1\u5C55\u793A\u578B\u3002\u6BCF\u6761\u516C\u5F0F\u5361\u5199\u5168\u6781\u7AEF\u5316\u6807\u9898\u30013 \u79D2\u94A9\u5B50\u3001\u5C71\u6D1E\u3001\u51B2\u7A81\u3001\u627F\u8BFA\u5151\u73B0\u3002\u6807\u660E A/B \u6D4B\u8BD5\u578B\u9009\u9898\uFF0C\u4F46\u4E0D\u5F97\u628A\u5C0F\u6837\u672C\u65B9\u5411\u89C2\u5BDF\u5199\u6210\u5DF2\u8BC1\u5B9E\u7ED3\u8BBA\u3002",
10237
+ "\u6392\u671F\u603B\u6761\u6570\u4E0D\u5F97\u8D85\u8FC7\u6BCF\u5468\u4E0A\u9650\uFF0C\u987B\u6838\u7B97\u7528\u6237\u4EA7\u80FD\uFF1B\u6240\u6709\u6307\u6807\u5206\u4E3A\u300C\u540E\u53F0\u5DF2\u6709\u57FA\u7EBF / \u81EA\u8BBE\u7BA1\u7406\u76EE\u6807 / \u5F85\u8865\u300D\uFF0C\u4E0D\u5F97\u865A\u6784\u540C\u7C7B\u6216\u5B98\u65B9\u53C2\u8003\u503C\u3002",
10238
+ ...common.slice(3)
10239
+ ].join("\n").replace(/\n{3,}/g, "\n\n");
10240
+ }
10241
+ const baselineFile = projectPath ? path14.join(projectPath, "inputs", "baseline-report.md") : "";
10242
+ const currentFile = projectPath ? path14.join(projectPath, "inputs", "current-report.md") : "";
10243
+ return [
10244
+ "\u8BF7\u6267\u884C\u4E00\u6B21\u300C\u8D26\u53F7\u590D\u8BCA\u5BF9\u6BD4\u300D\uFF1A\u5BF9\u7167\u6B64\u524D\u4E0E\u672C\u6B21\u4E24\u6B21\u4F53\u68C0\uFF0C\u5224\u65AD\u53D8\u5316\u3001\u5904\u65B9\u6267\u884C\u60C5\u51B5\u548C\u4E0B\u4E00\u6B65\uFF0C\u4E0D\u8981\u91CD\u65B0\u5199\u4E24\u4EFD\u4E92\u4E0D\u76F8\u5E72\u7684\u4F53\u68C0\u3002",
10245
+ ...common.slice(0, 3),
10246
+ "\u540C\u65F6\u9605\u8BFB references/diagnosis-framework.md \u4E0E references/prescription-library.md\uFF1B\u5904\u65B9\u53EA\u80FD\u6CBF\u7528\u53EF\u6EAF\u6E90\u6765\u6E90\uFF0C\u4E0D\u5F97\u81EA\u884C\u7F16\u9020\u51FA\u5904\u3002",
10247
+ `\u8865\u5145\u8BF4\u660E\uFF1A${input.notes || "\uFF08\u65E0\uFF09"}`,
10248
+ "",
10249
+ `\u57FA\u7EBF\u4F53\u68C0\u5168\u6587\u5DF2\u4FDD\u5B58\u4E8E ${baselineFile}\uFF0C\u672C\u6B21\u4F53\u68C0\u5168\u6587\u5DF2\u4FDD\u5B58\u4E8E ${currentFile}\uFF1B\u5FC5\u987B\u5148\u8BFB\u53D6\u4E24\u4EFD\u6587\u4EF6\u3002\u4EE5\u4E0B\u4F9D\u6B21\u5D4C\u5165\u76F8\u540C\u5168\u6587\uFF1A`,
10250
+ "===== \u57FA\u7EBF\u4F53\u68C0\uFF08\u6B64\u524D\uFF09 =====",
10251
+ diagnosisContext(linked.baseline),
10252
+ "===== \u672C\u6B21\u4F53\u68C0\uFF08\u6700\u8FD1\uFF09 =====",
10253
+ diagnosisContext(linked.current),
10254
+ "",
10255
+ "\u5148\u5217\u51FA\u4E24\u4EFD\u62A5\u544A\u7684\u4F53\u68C0\u65F6\u95F4\u3001\u6837\u672C\u8303\u56F4\u3001\u6570\u636E\u5B57\u6BB5\u548C\u7EDF\u8BA1\u53E3\u5F84\u3002\u53EA\u6BD4\u8F83\u4E24\u8FB9\u90FD\u5B9E\u9645\u5B58\u5728\u4E14\u5B9A\u4E49\u3001\u5355\u4F4D\u3001\u65F6\u95F4\u7A97\u53E3\u548C\u6837\u672C\u8303\u56F4\u4E00\u81F4\u7684\u6307\u6807\uFF1B\u5C55\u793A\u539F\u59CB\u503C\uFF0C\u518D\u8BA1\u7B97\u53D8\u5316\u503C\u6216\u53D8\u5316\u7387\u3002\u9664\u6570\u4E3A\u96F6\u6216\u4EFB\u4E00\u7AEF\u7F3A\u5931\u65F6\u4E0D\u7B97\u53D8\u5316\u7387\u3002",
10256
+ "\u4E0D\u540C\u5B57\u6BB5\u4E0D\u5F97\u4E92\u76F8\u6362\u7B97\uFF08\u5982\u5E73\u5747\u64AD\u653E\u65F6\u957F\u3001\u5B8C\u64AD\u7387\u300130 \u79D2\u7559\u5B58\uFF09\uFF1B\u4E0D\u540C\u6837\u672C\u91CF\u4E0D\u5F97\u76F4\u63A5\u62FC\u5E73\u5747\uFF0C\u4E0D\u540C\u53E3\u5F84\u4E0D\u5F97\u8BA1\u7B97\u3002\u65E0\u6CD5\u5BF9\u7167\u7684\u9879\u76EE\u7EDF\u4E00\u6807\u4E3A\u300C\u4E0D\u53EF\u6BD4\uFF1A\u7F3A\u5B57\u6BB5 / \u53E3\u5F84\u4E0D\u540C / \u6837\u672C\u4E0D\u540C\u300D\uFF0C\u4E0D\u5F97\u865A\u6784\u6570\u503C\u6216\u628A\u76F8\u5173\u6027\u5199\u6210\u56E0\u679C\u3002",
10257
+ "\u82E5\u62A5\u544A\u65E5\u671F\u6216\u57FA\u7EBF\uFF0F\u672C\u6B21\u987A\u5E8F\u770B\u8D77\u6765\u4E0D\u4E00\u81F4\uFF0C\u660E\u786E\u6307\u51FA\uFF0C\u4E0D\u5F97\u64C5\u81EA\u4EA4\u6362\u6216\u63A9\u76D6\u3002\u6807\u9898\u548C\u6587\u5B57\u8F6C\u5F55\u4ECD\u4E0D\u80FD\u8BC1\u660E\u672A\u63D0\u4F9B\u7684\u753B\u9762\u4E0E\u53E3\u64AD\u3002",
10258
+ "\u62A5\u544A\u5FC5\u987B\u5305\u542B\uFF1A\u6838\u5FC3\u7ED3\u8BBA\u3001\u540C\u53E3\u5F84\u6307\u6807\u5BF9\u7167\u8868\u3001\u5185\u5BB9\u7EBF\u4E0E\u53D1\u5E03\u6267\u884C\u53D8\u5316\u3001\u57FA\u7EBF\u5904\u65B9\u7684\u9010\u9879\u590D\u6838\uFF08\u4FDD\u7559 / \u505C\u6B62 / \u65B0\u589E\u53CA\u8BC1\u636E\uFF09\u3001\u4E0B\u4E00\u8F6E\u4E0D\u8D85\u8FC7 3 \u9879\u4F18\u5148\u52A8\u4F5C\u300130 \u5929\u9A8C\u6536\u6307\u6807\u4E0E\u68C0\u67E5\u70B9\u3001\u5F85\u8865\u8F93\u5165\u6E05\u5355\u3002",
10259
+ "\u4FDD\u7559\u3001\u505C\u6B62\u3001\u65B0\u589E\u90FD\u8981\u8BF4\u660E\u4F9D\u636E\uFF1B\u6CA1\u6709\u6267\u884C\u8BC1\u636E\u65F6\u6807\u300C\u6267\u884C\u5F85\u6838\u5B9E\u300D\uFF0C\u4E0D\u5F97\u5BA3\u79F0\u5904\u65B9\u6709\u6548\u6216\u65E0\u6548\u300230 \u5929\u76EE\u6807\u9700\u4E0E\u53D1\u5E03\u9891\u7387\u548C\u4EA7\u80FD\u4E00\u81F4\uFF0C\u81EA\u8BBE\u76EE\u6807\u987B\u660E\u786E\u6807\u6CE8\u3002",
10260
+ ...common.slice(3)
10261
+ ].join("\n").replace(/\n{3,}/g, "\n\n");
10262
+ }
9620
10263
  async function stateAsync() {
9621
10264
  await refreshTrialRuns();
9622
10265
  reconcileOrphanTasks();
@@ -9658,6 +10301,7 @@ function createPanelApp(options = {}) {
9658
10301
  store: storeView,
9659
10302
  license: license.status(),
9660
10303
  licenses: license.listProducts(),
10304
+ member: memberInfo(),
9661
10305
  buyUrl: buyPageUrl(),
9662
10306
  licenseBaseUrl: settings.licenseBaseUrl,
9663
10307
  trialRuns,
@@ -9699,6 +10343,7 @@ function createPanelApp(options = {}) {
9699
10343
  store: [],
9700
10344
  license: license.status(),
9701
10345
  licenses: license.listProducts(),
10346
+ member: memberInfo(),
9702
10347
  buyUrl: buyPageUrl(),
9703
10348
  licenseBaseUrl: settings.licenseBaseUrl,
9704
10349
  trialRuns,
@@ -9725,7 +10370,7 @@ function createPanelApp(options = {}) {
9725
10370
  }
9726
10371
  let campaignCache = { at: 0, items: [] };
9727
10372
  async function loadPublicCampaigns() {
9728
- if (resellerCode || license.canExport()) return [];
10373
+ if (resellerCode() || license.canExport()) return [];
9729
10374
  const now = Date.now();
9730
10375
  if (now - campaignCache.at < 6e4) return campaignCache.items;
9731
10376
  const base = String(settings.licenseBaseUrl || "").replace(/\/$/, "");
@@ -9897,6 +10542,7 @@ function createPanelApp(options = {}) {
9897
10542
  }
9898
10543
  async function handle(req, res, pathname) {
9899
10544
  const route = `${req.method} ${pathname}`;
10545
+ const query = new URL(req.url ?? "/", "http://local").searchParams;
9900
10546
  if (req.method === "GET" && pathname.startsWith("/icons/")) {
9901
10547
  const raw = pathname.slice("/icons/".length).replace(/[^a-zA-Z0-9._-]/g, "");
9902
10548
  const base = raw.replace(/\.(png|svg|webp)$/i, "") || "app";
@@ -9911,6 +10557,9 @@ function createPanelApp(options = {}) {
9911
10557
  res.writeHead(200, { "content-type": "image/svg+xml; charset=utf-8", "cache-control": "no-store" });
9912
10558
  return void res.end(placeholderIcon(base));
9913
10559
  }
10560
+ if (req.method === "GET" && pathname.startsWith("/diagnose/media/")) {
10561
+ return serveDiagnoseMedia(req, res, pathname.slice("/diagnose/media/".length).replace(/[^0-9a-f-]/gi, ""));
10562
+ }
9914
10563
  switch (route) {
9915
10564
  case "GET /state":
9916
10565
  return send(res, 200, await stateAsync());
@@ -9945,6 +10594,236 @@ function createPanelApp(options = {}) {
9945
10594
  });
9946
10595
  return void res.end(buf);
9947
10596
  }
10597
+ case "POST /diagnose/upload": {
10598
+ try {
10599
+ return send(res, 200, { media: await saveDiagnoseUpload(req, query) });
10600
+ } catch (error) {
10601
+ return send(res, error.status || 502, { error: error.message || "\u4E0A\u4F20\u5931\u8D25" });
10602
+ }
10603
+ }
10604
+ case "POST /diagnose/session": {
10605
+ try {
10606
+ assertDiagnoseMember();
10607
+ const body = await readJson(req);
10608
+ const media = normalizeDiagnoseMedia(body.media);
10609
+ if ((!body.snapshot || typeof body.snapshot !== "object") && !media.length) {
10610
+ return send(res, 400, { error: "\u8BF7\u5148\u4E0A\u4F20\u540E\u53F0\u622A\u56FE\u6216\u89C6\u9891" });
10611
+ }
10612
+ const snapshot = body.snapshot && typeof body.snapshot === "object" ? body.snapshot : { title: media[0].name, kind: "upload", media: media.map((item) => item.name) };
10613
+ const projectPath = path14.join(DIAGNOSE_SESSIONS_DIR, crypto8.randomUUID());
10614
+ fs14.mkdirSync(projectPath, { recursive: true });
10615
+ const sessionMedia = materializeDiagnoseMedia(projectPath, media);
10616
+ const skillPath = seedDiagnoseSkill(projectPath);
10617
+ const promptText = diagnosePrompt(snapshot, { media: sessionMedia, projectPath, skillPath });
10618
+ if (!dsh?.chat?.startPromptRun) {
10619
+ return send(res, 503, { error: "\u5F53\u524D\u73AF\u5883\u4E0D\u80FD\u76F4\u63A5\u5F00 DSH \u4F1A\u8BDD\uFF0C\u53EF\u4EE5\u590D\u5236\u63D0\u793A\u8BCD\u5230\u4EFB\u610F\u6A21\u578B", promptText });
10620
+ }
10621
+ const result = await dsh.chat.startPromptRun({
10622
+ agentId: "bn-diagnose",
10623
+ skill: "diagnose",
10624
+ title: "\u8D26\u53F7\u8BCA\u65AD \xB7 " + String(snapshot.title || "\u672A\u547D\u540D").slice(0, 40),
10625
+ promptText,
10626
+ projectPath
10627
+ });
10628
+ if (!result.ok) {
10629
+ return send(res, 502, { error: result.error || "\u5F00\u4E0D\u4E86 DSH \u4F1A\u8BDD\uFF0C\u53EF\u4EE5\u590D\u5236\u63D0\u793A\u8BCD\u5230\u4EFB\u610F\u6A21\u578B", promptText });
10630
+ }
10631
+ return send(res, 200, {
10632
+ ok: true,
10633
+ sessionId: result.sessionId,
10634
+ projectPath,
10635
+ media: media.map(({ path: _path, ...rest }) => rest),
10636
+ promptText
10637
+ });
10638
+ } catch (error) {
10639
+ return send(res, error.status || 502, { error: error.message || "\u5F00\u4E0D\u4E86 DSH \u4F1A\u8BDD" });
10640
+ }
10641
+ }
10642
+ case "GET /diagnose/session-report": {
10643
+ try {
10644
+ assertDiagnoseMember();
10645
+ const sessionId = String(query.get("sessionId") || "");
10646
+ if (!sessionId) return send(res, 400, { error: "\u7F3A\u5C11 sessionId" });
10647
+ const report = diagnoseReports().find((item) => item.sessionId === sessionId);
10648
+ if (!report) return send(res, 404, { error: "\u8FD8\u6CA1\u6709\u5B58\u6863", pending: true });
10649
+ const file = safeReportPath(report.projectPath);
10650
+ if (!file) return send(res, 400, { error: "\u62A5\u544A\u7F3A\u5C11\u6709\u6548\u7684\u4F1A\u8BDD\u76EE\u5F55" });
10651
+ if (!fs14.existsSync(file)) return send(res, 200, { pending: true, report });
10652
+ if (fs14.statSync(file).size > 256 * 1024) return send(res, 413, { error: "\u62A5\u544A\u6587\u4EF6\u592A\u5927" });
10653
+ const analysis = fs14.readFileSync(file, "utf8").slice(0, 2e4);
10654
+ if (analysis !== report.analysis) {
10655
+ report.analysis = analysis;
10656
+ report.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
10657
+ fs14.writeFileSync(path14.join(DIAGNOSE_DIR, `${report.id}.json`), JSON.stringify(report), "utf8");
10658
+ }
10659
+ return send(res, 200, { report });
10660
+ } catch (error) {
10661
+ return send(res, error.status || 500, { error: error.message || "\u8BFB\u53D6\u5931\u8D25" });
10662
+ }
10663
+ }
10664
+ case "GET /diagnose/reports":
10665
+ assertDiagnoseMember();
10666
+ return send(res, 200, { reports: diagnoseReports().slice(0, 50) });
10667
+ case "POST /diagnose/reports": {
10668
+ assertDiagnoseMember();
10669
+ const body = await readJson(req);
10670
+ if (!body.snapshot || typeof body.snapshot !== "object") {
10671
+ return send(res, 400, { error: "\u6CA1\u6709\u53EF\u4FDD\u5B58\u7684\u62A5\u544A" });
10672
+ }
10673
+ try {
10674
+ return send(res, 200, {
10675
+ report: saveDiagnoseReport(body.snapshot, body.analysis, body.sessionId, {
10676
+ projectPath: body.projectPath,
10677
+ media: body.media
10678
+ })
10679
+ });
10680
+ } catch (error) {
10681
+ return send(res, error.status || 500, { error: error.message || "\u4FDD\u5B58\u5931\u8D25" });
10682
+ }
10683
+ }
10684
+ case "POST /diagnose/reports/delete": {
10685
+ assertDiagnoseMember();
10686
+ const body = await readJson(req);
10687
+ const id = String(body.id || "");
10688
+ if (!/^[a-zA-Z0-9-]{8,64}$/.test(id)) return send(res, 400, { error: "\u62A5\u544A\u4E0D\u5B58\u5728" });
10689
+ fs14.rmSync(path14.join(DIAGNOSE_DIR, `${id}.json`), { force: true });
10690
+ return send(res, 200, { ok: true });
10691
+ }
10692
+ case "POST /growth/session": {
10693
+ let promptText = "";
10694
+ try {
10695
+ assertDiagnoseMember();
10696
+ const body = await readJson(req);
10697
+ const kind = growthKind(body.kind);
10698
+ const input = cleanGrowthInput(kind, body.input);
10699
+ const linked = resolveGrowthContext(kind, input);
10700
+ const projectPath = path14.join(DIAGNOSE_SESSIONS_DIR, crypto8.randomUUID());
10701
+ fs14.mkdirSync(projectPath, { recursive: true });
10702
+ const media = kind === "research" ? normalizeDiagnoseMedia(body.media) : [];
10703
+ const sessionMedia = materializeDiagnoseMedia(projectPath, media);
10704
+ let skillPath;
10705
+ if (kind === "research") {
10706
+ skillPath = seedDiagnoseSkill(projectPath, "creator-research");
10707
+ } else if (kind === "planner") {
10708
+ skillPath = seedDiagnoseSkill(projectPath, "topic-planner");
10709
+ seedDiagnoseSkill(projectPath, "account-diagnosis");
10710
+ if (linked.diagnosis) {
10711
+ const inputsDir = path14.join(projectPath, "inputs");
10712
+ fs14.mkdirSync(inputsDir, { recursive: true });
10713
+ fs14.writeFileSync(path14.join(inputsDir, "diagnosis-report.md"), diagnosisContext(linked.diagnosis), "utf8");
10714
+ }
10715
+ } else {
10716
+ skillPath = seedDiagnoseSkill(projectPath, "account-diagnosis");
10717
+ const inputsDir = path14.join(projectPath, "inputs");
10718
+ fs14.mkdirSync(inputsDir, { recursive: true });
10719
+ fs14.writeFileSync(path14.join(inputsDir, "baseline-report.md"), diagnosisContext(linked.baseline), "utf8");
10720
+ fs14.writeFileSync(path14.join(inputsDir, "current-report.md"), diagnosisContext(linked.current), "utf8");
10721
+ }
10722
+ promptText = growthPrompt(kind, input, sessionMedia, { ...linked, projectPath, skillPath });
10723
+ if (!dsh?.chat?.startPromptRun) {
10724
+ return send(res, 503, { error: "\u5F53\u524D\u73AF\u5883\u4E0D\u80FD\u76F4\u63A5\u5F00 DSH \u4F1A\u8BDD\uFF0C\u53EF\u4EE5\u590D\u5236\u63D0\u793A\u8BCD\u5230\u4EFB\u610F\u6A21\u578B", promptText });
10725
+ }
10726
+ const result = await dsh.chat.startPromptRun({
10727
+ agentId: "bn-diagnose",
10728
+ skill: "diagnose",
10729
+ title: `${GROWTH_LABELS[kind]} \xB7 ${growthRecordTitle(kind, input)}`.slice(0, 80),
10730
+ promptText,
10731
+ projectPath
10732
+ });
10733
+ if (!result.ok) {
10734
+ return send(res, 502, { error: result.error || "\u5F00\u4E0D\u4E86 DSH \u4F1A\u8BDD\uFF0C\u53EF\u4EE5\u590D\u5236\u63D0\u793A\u8BCD\u5230\u4EFB\u610F\u6A21\u578B", promptText });
10735
+ }
10736
+ const recordMedia = sessionMedia.map(({ path: _path, ...rest }) => rest);
10737
+ const record = createGrowthRecord(kind, input, {
10738
+ sessionId: result.sessionId,
10739
+ projectPath,
10740
+ media: recordMedia
10741
+ });
10742
+ return send(res, 200, {
10743
+ ok: true,
10744
+ sessionId: result.sessionId,
10745
+ projectPath,
10746
+ promptText,
10747
+ record,
10748
+ media: recordMedia
10749
+ });
10750
+ } catch (error) {
10751
+ return send(res, error.status || 502, { error: error.message || "\u5F00\u4E0D\u4E86 DSH \u4F1A\u8BDD", promptText });
10752
+ }
10753
+ }
10754
+ case "GET /growth/session-report": {
10755
+ try {
10756
+ assertDiagnoseMember();
10757
+ const sessionId = String(query.get("sessionId") || "");
10758
+ if (!sessionId) return send(res, 400, { error: "\u7F3A\u5C11 sessionId" });
10759
+ const report = findGrowthRecord({ sessionId });
10760
+ if (!report) return send(res, 404, { error: "\u8FD8\u6CA1\u6709\u5B58\u6863", pending: true });
10761
+ const file = safeReportPath(report.projectPath);
10762
+ if (!file) return send(res, 400, { error: "\u62A5\u544A\u7F3A\u5C11\u6709\u6548\u7684\u4F1A\u8BDD\u76EE\u5F55" });
10763
+ if (!fs14.existsSync(file)) return send(res, 200, { pending: true, report });
10764
+ if (fs14.statSync(file).size > 256 * 1024) return send(res, 413, { error: "\u62A5\u544A\u6587\u4EF6\u592A\u5927" });
10765
+ const analysis = fs14.readFileSync(file, "utf8").slice(0, 4e4);
10766
+ if (analysis !== report.analysis) {
10767
+ report.analysis = analysis;
10768
+ report.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
10769
+ writeGrowthRecord(report);
10770
+ }
10771
+ return send(res, 200, { report });
10772
+ } catch (error) {
10773
+ return send(res, error.status || 500, { error: error.message || "\u8BFB\u53D6\u5931\u8D25" });
10774
+ }
10775
+ }
10776
+ case "GET /growth/reports": {
10777
+ try {
10778
+ assertDiagnoseMember();
10779
+ const kind = growthKind(query.get("kind"));
10780
+ return send(res, 200, { reports: growthRecords(kind).slice(0, 60) });
10781
+ } catch (error) {
10782
+ return send(res, error.status || 500, { error: error.message || "\u8BFB\u53D6\u5931\u8D25" });
10783
+ }
10784
+ }
10785
+ case "POST /growth/reports": {
10786
+ try {
10787
+ assertDiagnoseMember();
10788
+ const body = await readJson(req);
10789
+ const kind = growthKind(body.kind);
10790
+ const input = cleanGrowthInput(kind, body.input);
10791
+ resolveGrowthContext(kind, input);
10792
+ const analysis = String(body.analysis || "").slice(0, 4e4);
10793
+ if (!analysis.trim()) return send(res, 400, { error: "\u6CA1\u6709\u53EF\u4FDD\u5B58\u7684\u62A5\u544A" });
10794
+ const existing = body.id ? findGrowthRecord({ kind, id: growthRecordId(body.id) }) : null;
10795
+ if (body.id && !existing) return send(res, 404, { error: "\u6863\u6848\u4E0D\u5B58\u5728" });
10796
+ const media = body.media === void 0 && existing ? existing.media : normalizeDiagnoseMedia(body.media).map(({ path: _path, ...rest }) => rest);
10797
+ const sessionId = String(body.sessionId ?? existing?.sessionId ?? "").slice(0, 100);
10798
+ const projectPath = String(body.projectPath ?? existing?.projectPath ?? "").slice(0, 400);
10799
+ const report = existing ? writeGrowthRecord({
10800
+ ...existing,
10801
+ title: growthRecordTitle(kind, input),
10802
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
10803
+ input,
10804
+ analysis,
10805
+ sessionId,
10806
+ projectPath,
10807
+ media,
10808
+ links: growthLinksFromInput(kind, input)
10809
+ }) : createGrowthRecord(kind, input, { analysis, sessionId, projectPath, media });
10810
+ return send(res, 200, { report });
10811
+ } catch (error) {
10812
+ return send(res, error.status || 500, { error: error.message || "\u4FDD\u5B58\u5931\u8D25" });
10813
+ }
10814
+ }
10815
+ case "POST /growth/reports/delete": {
10816
+ try {
10817
+ assertDiagnoseMember();
10818
+ const body = await readJson(req);
10819
+ const kind = growthKind(body.kind);
10820
+ const id = growthRecordId(body.id);
10821
+ fs14.rmSync(path14.join(GROWTH_DIR, kind, `${id}.json`), { force: true });
10822
+ return send(res, 200, { ok: true });
10823
+ } catch (error) {
10824
+ return send(res, error.status || 500, { error: error.message || "\u5220\u9664\u5931\u8D25" });
10825
+ }
10826
+ }
9948
10827
  case "GET /licenses":
9949
10828
  return send(res, 200, { licenses: license.listProducts(), aggregate: license.status() });
9950
10829
  case "POST /activate": {
@@ -10487,6 +11366,7 @@ function createPanelApp(options = {}) {
10487
11366
  void loadReleases();
10488
11367
  void refreshTrialRuns();
10489
11368
  void reportInstalls();
11369
+ setTimeout(() => repairBrokenInstalls(), 3e3).unref?.();
10490
11370
  const autoTimer = setInterval(() => void autoUpdateTick(), 6 * 36e5);
10491
11371
  if (typeof autoTimer.unref === "function") autoTimer.unref();
10492
11372
  function bindChatProduct(cwd) {