@whyshu/argent-engine 0.8.0-rc.9 → 0.13.27

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 (106) hide show
  1. package/MANIFEST.json +102 -40
  2. package/bin/runtime-install.mjs +92 -0
  3. package/engine/VERSION +1 -1
  4. package/engine/configs/billing-actions.json +30 -0
  5. package/engine/configs/mcp-tools.snapshot.json +47 -0
  6. package/engine/core/contracts/argent.contracts.json +2 -2
  7. package/engine/core/lib/account-token.mjs +37 -0
  8. package/engine/core/lib/redact.mjs +34 -0
  9. package/engine/core/mcp/billing-upload.mjs +77 -0
  10. package/engine/core/mcp/erp-server.mjs +300 -0
  11. package/engine/core/mcp/gateway.mjs +333 -0
  12. package/engine/core/mcp/meter.mjs +101 -0
  13. package/engine/core/mcp/server.mjs +245 -0
  14. package/engine/core/mcp/vault.mjs +78 -0
  15. package/engine/scripts/appointment-worker.mjs +456 -35
  16. package/engine/scripts/appointment_grab.py +1389 -264
  17. package/engine/scripts/appointment_rules.py +118 -0
  18. package/engine/scripts/argent-timers.mjs +227 -0
  19. package/engine/scripts/argent-web-preflight.mjs +123 -0
  20. package/engine/scripts/argent-web-verify.mjs +105 -0
  21. package/engine/scripts/billing-upload.mjs +26 -0
  22. package/engine/scripts/build-engine-package.mjs +7 -1
  23. package/engine/scripts/capability-packs.lock.json +323 -300
  24. package/engine/scripts/check-version-consistency.mjs +14 -1
  25. package/engine/scripts/collect-intel.mjs +108 -0
  26. package/engine/scripts/daily-report-timer-plist.sh +18 -4
  27. package/engine/scripts/daily-report-timers.sh +36 -3
  28. package/engine/scripts/daily-report.mjs +78 -2
  29. package/engine/scripts/dsh-web-doctor.mjs +84 -0
  30. package/engine/scripts/engine-manifest.mjs +90 -0
  31. package/engine/scripts/engine-runtime-install.mjs +92 -0
  32. package/engine/scripts/erp-ui-install.mjs +127 -0
  33. package/engine/scripts/erp-ui-preflight.mjs +104 -0
  34. package/engine/scripts/erp-ui-verify.mjs +135 -0
  35. package/engine/scripts/flow-requirements.json +122 -99
  36. package/engine/scripts/full-exec-worker.mjs +251 -0
  37. package/engine/scripts/gen-preset-rows.mjs +1 -1
  38. package/engine/scripts/invoice_lip_runner.py +1 -0
  39. package/engine/scripts/invoice_runner.py +17 -6
  40. package/engine/scripts/invoice_stageb.py +21 -4
  41. package/engine/scripts/lib/bootcheck-wiring.mjs +7 -5
  42. package/engine/scripts/lib/intel-card.mjs +43 -0
  43. package/engine/scripts/lib/invoice_names.py +23 -0
  44. package/engine/scripts/lib/ml-ads-core.mjs +100 -18
  45. package/engine/scripts/lib/ml-ads-rules.mjs +275 -0
  46. package/engine/scripts/lib/ml-bridge-auth.mjs +50 -0
  47. package/engine/scripts/lib/ml-category-core.mjs +120 -0
  48. package/engine/scripts/lib/ml-insight-core.mjs +621 -0
  49. package/engine/scripts/lib/ml-insight-facts.mjs +424 -0
  50. package/engine/scripts/lib/ml-items.mjs +40 -0
  51. package/engine/scripts/lib/ml-listing-core.mjs +342 -0
  52. package/engine/scripts/lib/ml-market-core.mjs +774 -0
  53. package/engine/scripts/lib/plugin-packages.mjs +13 -0
  54. package/engine/scripts/lib/webdriver_ipc.py +61 -1
  55. package/engine/scripts/line_pack.py +196 -0
  56. package/engine/scripts/mcp-gateway-switch.mjs +100 -0
  57. package/engine/scripts/migrate-providers.mjs +3 -3
  58. package/engine/scripts/ml-ads-advice.mjs +140 -0
  59. package/engine/scripts/ml-ads.mjs +40 -0
  60. package/engine/scripts/ml-category-baseline.mjs +174 -0
  61. package/engine/scripts/ml-insight-diagnose.mjs +247 -0
  62. package/engine/scripts/ml-insight-run.sh +183 -0
  63. package/engine/scripts/ml-insight-timers.sh +224 -0
  64. package/engine/scripts/ml-listing-audit.mjs +170 -0
  65. package/engine/scripts/ml-market-matrix.mjs +84 -0
  66. package/engine/scripts/ml-market-snapshot.mjs +261 -0
  67. package/engine/scripts/ml-messages.mjs +145 -0
  68. package/engine/scripts/npm-publish-argent.mjs +14 -0
  69. package/engine/scripts/ops-codefix-pr.mjs +87 -0
  70. package/engine/scripts/ops-codefix-queue.mjs +30 -0
  71. package/engine/scripts/ops-digest.mjs +98 -0
  72. package/engine/scripts/ops-verify.mjs +67 -0
  73. package/engine/scripts/orchestrator.py +410 -20
  74. package/engine/scripts/orchestrator_br.py +146 -4
  75. package/engine/scripts/plugin-bootcheck.mjs +50 -1
  76. package/engine/scripts/publish-engine.mjs +111 -0
  77. package/engine/scripts/restart-dsh-with-tunnel.sh +129 -0
  78. package/engine/scripts/skuflow_invoice_runner.py +43 -3
  79. package/engine/scripts/v0.9-acceptance.mjs +107 -0
  80. package/engine/scripts/vertical-lint.mjs +12 -1
  81. package/engine/scripts/web-switch.mjs +121 -0
  82. package/engine/scripts/wenyao_fill_template.py +52 -15
  83. package/engine/scripts/wenyao_run.py +2 -1
  84. package/engine/skills/README.md +3 -3
  85. package/engine/skills/bilibili-upload/SKILL.md +109 -0
  86. package/engine/skills/douyin-upload/SKILL.md +74 -0
  87. package/engine/skills/internal.json +28 -0
  88. package/engine/skills/kuaishou-upload/SKILL.md +109 -0
  89. package/engine/skills/ml-appointment-safety/SKILL.md +42 -0
  90. package/engine/skills/ml-full-appointment/SKILL.md +23 -0
  91. package/engine/skills/ml-invoice/SKILL.md +2 -2
  92. package/engine/skills/ml-invoice/references/template-fill-contract.md +1 -1
  93. package/engine/skills/ml-label-recovery/SKILL.md +43 -0
  94. package/engine/skills/ml-listing-audit/SKILL.md +66 -0
  95. package/engine/skills/ml-store-daily-report/SKILL.md +68 -0
  96. package/engine/skills/ml-store-daily-report/scripts/generate.js +35 -0
  97. package/engine/skills/ml-store-insight/SKILL.md +81 -0
  98. package/engine/skills/tencent-upload/SKILL.md +60 -0
  99. package/engine/skills/weibo-upload/SKILL.md +106 -0
  100. package/engine/skills/whyshu-social-ops/SKILL.md +136 -0
  101. package/engine/skills/xiaohongshu-upload/SKILL.md +122 -0
  102. package/engine/version.mjs +1 -1
  103. package/engine/verticals/README.md +7 -0
  104. package/engine/verticals/_engine-words.baseline.txt +27 -59
  105. package/engine/verticals/mercadolibre/manifest.json +132 -6
  106. package/package.json +5 -1
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * erp-ui-install.mjs —— 把 `@whyshu/argent-erp-ui` 装进某个 dsh profile(v0.9.0)
4
+ *
5
+ * 为什么需要脚本:手工改 profile 有三处必错点(2026-11-02 实测过两次):
6
+ * ① `pnpm install` 会把 `node_modules/@whyshu/*` 的**软链重建成新目录**(桌面插件曾是软链,重建后
7
+ * "改代码不生效"但没有任何报错);
8
+ * ② bundles 数组漏加 ⇒ 包装了但**根本不会被加载**;
9
+ * ③ 不备份 ⇒ 出问题只能靠记忆回滚。
10
+ *
11
+ * 用法:
12
+ * node scripts/erp-ui-install.mjs # 默认 dry-run(只打印将做什么)
13
+ * node scripts/erp-ui-install.mjs --apply # 真正写入 + pnpm install
14
+ * node scripts/erp-ui-install.mjs --apply --profile desktop
15
+ * node scripts/erp-ui-install.mjs --rollback <备份目录>
16
+ *
17
+ * 安全:只动 profile 的 package.json / node_modules;⛔ 不重启 dsh(由用户手动)。
18
+ */
19
+ import { cpSync, existsSync, lstatSync, mkdirSync, readFileSync, readlinkSync, realpathSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs'
20
+ import { homedir } from 'node:os'
21
+ import { join, resolve } from 'node:path'
22
+ import { spawnSync } from 'node:child_process'
23
+
24
+ const REPO = resolve(import.meta.dirname, '..')
25
+ const PKG = '@whyshu/argent-erp-ui'
26
+ const PKG_DIR = join(REPO, 'plugins', 'argent-erp-ui')
27
+ const args = process.argv.slice(2)
28
+ const apply = args.includes('--apply')
29
+ const profileArg = args.includes('--profile') ? args[args.indexOf('--profile') + 1] : 'desktop'
30
+ const rollbackIdx = args.indexOf('--rollback')
31
+
32
+ const dshHome = process.env.DSH_HOME || join(homedir(), '.dsh')
33
+ const profileDir = join(dshHome, 'profiles', profileArg)
34
+ const pkgFile = join(profileDir, 'package.json')
35
+ const ts = new Date().toISOString().replace(/[-:T]/g, '').slice(0, 14)
36
+
37
+ function resolvePnpm() {
38
+ const cands = [process.env.ARGENT_PNPM, '/opt/homebrew/bin/pnpm', join(homedir(), 'Library', 'pnpm', 'pnpm')].filter(Boolean)
39
+ for (const c of cands) if (existsSync(c)) return c
40
+ const which = spawnSync('pnpm', ['--version'], { encoding: 'utf8' })
41
+ return which.status === 0 ? 'pnpm' : ''
42
+ }
43
+
44
+ function fail(msg) { console.error(`❌ ${msg}`); process.exit(1) }
45
+
46
+ if (rollbackIdx >= 0) {
47
+ const dir = args[rollbackIdx + 1]
48
+ if (!dir || !existsSync(dir)) fail(`找不到备份目录:${dir}`)
49
+ cpSync(join(dir, 'package.json'), pkgFile)
50
+ const patchBak = join(dir, 'cordis.patch.yml')
51
+ if (existsSync(patchBak)) cpSync(patchBak, join(profileDir, 'cordis.patch.yml'))
52
+ console.log(`✅ 已回滚 package.json${existsSync(patchBak) ? ' + cordis.patch.yml' : ''}(来自 ${dir})`)
53
+ console.log(' 下一步:重启 DSH Desktop(由用户手动)')
54
+ process.exit(0)
55
+ }
56
+
57
+ if (!existsSync(pkgFile)) fail(`profile 不存在:${pkgFile}`)
58
+ if (!existsSync(join(PKG_DIR, 'package.json'))) fail(`插件不存在:${PKG_DIR}`)
59
+
60
+ const before = JSON.parse(readFileSync(pkgFile, 'utf8'))
61
+ const deps = before.dependencies || {}
62
+ const bundles = (before.dsh && before.dsh.profile && before.dsh.profile.bundles) || []
63
+ const needDep = deps[PKG] !== `file:${PKG_DIR}`
64
+ const needBundle = !bundles.includes(PKG)
65
+
66
+ console.log(`profile: ${profileDir}`)
67
+ console.log(` 依赖 ${PKG}: ${needDep ? '需要添加' : '已存在'}`)
68
+ console.log(` bundles: ${needBundle ? '需要追加' : '已包含'}`)
69
+
70
+ const pnpm = resolvePnpm()
71
+ if (!pnpm) console.log(' ⚠️ 找不到 pnpm:--apply 时只能改 manifest,需你手动 pnpm install')
72
+
73
+ // 现有 @whyshu 软链(pnpm install 可能重建)
74
+ const nmWhyshu = join(profileDir, 'node_modules', '@whyshu')
75
+ if (existsSync(nmWhyshu)) {
76
+ for (const d of ['argent-desktop', 'argent-erp-ui']) {
77
+ const p = join(nmWhyshu, d)
78
+ if (!existsSync(p)) continue
79
+ const st = lstatSync(p)
80
+ console.log(` node_modules/@whyshu/${d}: ${st.isSymbolicLink() ? `symlink → ${readlinkSync(p)}` : `目录(真实路径 ${realpathSync(p)})`}`)
81
+ }
82
+ }
83
+
84
+ if (!apply) {
85
+ console.log('\n[dry-run] 未写入。要执行:node scripts/erp-ui-install.mjs --apply')
86
+ process.exit(0)
87
+ }
88
+
89
+ const bakDir = join(profileDir, `backup-erpui-${ts}`)
90
+ mkdirSync(bakDir, { recursive: true })
91
+ cpSync(pkgFile, join(bakDir, 'package.json'))
92
+ if (existsSync(join(profileDir, 'cordis.patch.yml'))) cpSync(join(profileDir, 'cordis.patch.yml'), join(bakDir, 'cordis.patch.yml'))
93
+ console.log(`\n备份:${bakDir}`)
94
+
95
+ const after = JSON.parse(JSON.stringify(before))
96
+ after.dependencies = { ...(after.dependencies || {}), [PKG]: `file:${PKG_DIR}` }
97
+ after.dsh = after.dsh || {}
98
+ after.dsh.profile = after.dsh.profile || {}
99
+ const b = after.dsh.profile.bundles || []
100
+ if (!b.includes(PKG)) b.push(PKG)
101
+ after.dsh.profile.bundles = b
102
+ writeFileSync(pkgFile, JSON.stringify(after, null, 2) + '\n')
103
+ console.log('✅ package.json 已更新')
104
+
105
+ if (pnpm) {
106
+ console.log(`\n$ ${pnpm} install --ignore-scripts(在 ${profileDir})`)
107
+ const r = spawnSync(pnpm, ['install', '--ignore-scripts'], { cwd: profileDir, stdio: 'inherit' })
108
+ if (r.status !== 0) fail(`pnpm install 失败(status=${r.status})`)
109
+ } else {
110
+ console.log('⚠️ 跳过 pnpm install(未找到 pnpm)——请手动执行后再重启')
111
+ }
112
+
113
+ // install 后核软链(桌面插件历史上被重建过)
114
+ const desktopLink = join(nmWhyshu, 'argent-desktop')
115
+ const repoDesktop = join(REPO, 'plugins', 'argent-desktop')
116
+ if (existsSync(desktopLink) && !lstatSync(desktopLink).isSymbolicLink()) {
117
+ const real = realpathSync(desktopLink)
118
+ if (real !== realpathSync(repoDesktop)) {
119
+ console.log(`⚠️ @whyshu/argent-desktop 已不是软链(${real})⇒ 恢复软链,否则改仓库代码不生效`)
120
+ unlinkSync(desktopLink)
121
+ symlinkSync(repoDesktop, desktopLink)
122
+ console.log('✅ 软链已恢复')
123
+ }
124
+ }
125
+
126
+ console.log(`\n完成。回滚:node scripts/erp-ui-install.mjs --rollback ${bakDir}`)
127
+ console.log('生效需要重启 DSH Desktop(⛔ 本脚本不重启,由用户手动)')
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * erp-ui-preflight.mjs —— 重启**前**的结构自检(v0.9.0)
4
+ *
5
+ * 为什么需要:改完 profile 到重启之间,任何一环错都只会表现为"重启后看不到面板",且没有报错。
6
+ * 本脚本把"能提前发现"的都查一遍,避免拿一次重启去试错。
7
+ *
8
+ * 查什么(全部离线、不碰网络):
9
+ * 1. profile package.json:依赖存在 / bundles 含该包 / JSON 合法
10
+ * 2. node_modules 软链指向仓库且 package.json 可解析
11
+ * 3. cordis.patch.yml 含 gateway insert(且旧的 engine/ml 两条已并掉)
12
+ * 4. 客户端 bundle 能被 loader 装载并注册 3 个 slot(用 vm 桩,不需要浏览器)
13
+ * 5. 网关 insert 指向的文件存在
14
+ *
15
+ * 退出码:0 全通 | 1 有 FAIL
16
+ */
17
+ import { readFileSync, existsSync, lstatSync, readlinkSync, realpathSync } from 'node:fs'
18
+ import { homedir } from 'node:os'
19
+ import { join, resolve } from 'node:path'
20
+
21
+ const REPO = resolve(import.meta.dirname, '..')
22
+ const profileArg = process.argv.includes('--profile') ? process.argv[process.argv.indexOf('--profile') + 1] : 'desktop'
23
+ const profileDir = join(process.env.DSH_HOME || join(homedir(), '.dsh'), 'profiles', profileArg)
24
+ const PKG = '@whyshu/argent-erp-ui'
25
+
26
+ let fails = 0
27
+ const ok = (c, msg) => { console.log(`${c ? '✅' : '❌'} ${msg}`); if (!c) fails++ }
28
+
29
+ // 1) package.json
30
+ let pkg = null
31
+ try { pkg = JSON.parse(readFileSync(join(profileDir, 'package.json'), 'utf8')) } catch (e) { ok(false, `package.json 读不了:${e.message}`) }
32
+ if (pkg) {
33
+ ok(Boolean(pkg.dependencies && pkg.dependencies[PKG]), `依赖 ${PKG} 已登记`)
34
+ const bundles = (pkg.dsh && pkg.dsh.profile && pkg.dsh.profile.bundles) || []
35
+ ok(bundles.includes(PKG), `bundles 含 ${PKG}`)
36
+ ok(Array.isArray(bundles) && bundles.length > 0, `bundles 非空(${bundles.length} 条)`)
37
+ }
38
+
39
+ // 2) node_modules
40
+ const nm = join(profileDir, 'node_modules', '@whyshu', 'argent-erp-ui')
41
+ if (!existsSync(nm)) ok(false, `node_modules 缺 ${PKG}`)
42
+ else {
43
+ const st = lstatSync(nm)
44
+ ok(st.isSymbolicLink(), `node_modules/${PKG} 是软链(改仓库代码即生效)`)
45
+ if (st.isSymbolicLink()) {
46
+ const target = realpathSync(nm)
47
+ ok(target === realpathSync(join(REPO, 'plugins', 'argent-erp-ui')), `软链指向仓库(${target})`)
48
+ }
49
+ ok(existsSync(join(nm, 'package.json')), '包 package.json 可解析')
50
+ }
51
+
52
+ // 3) patch
53
+ const patch = join(profileDir, 'cordis.patch.yml')
54
+ if (!existsSync(patch)) ok(false, '缺 cordis.patch.yml')
55
+ else {
56
+ const txt = readFileSync(patch, 'utf8')
57
+ // MCP 形态二选一:单条网关(target)或旧的 engine+ml 两条(兼容态,同样能用)
58
+ const hasGateway = txt.includes('mcp-argent-gateway')
59
+ ok(txt.includes('mcp-argent-engine') || hasGateway, `patch 至少有一条 argent MCP(${hasGateway ? '网关' : 'engine/ml'})`)
60
+ ok(!(hasGateway && txt.includes('mcp-argent-engine')), '网关与旧 engine 条目不同时存在(不重复工具)')
61
+ if (hasGateway) ok(txt.includes('core/mcp/gateway.mjs'), 'patch 指向 gateway.mjs')
62
+ }
63
+
64
+ // 4) bundle 装载(vm 桩)
65
+ const bundle = join(REPO, 'plugins', 'argent-erp-ui', 'dist', 'client.js')
66
+ if (!existsSync(bundle)) ok(false, '缺 dist/client.js')
67
+ else {
68
+ const src = readFileSync(bundle, 'utf8')
69
+ const regs = []
70
+ let mod = null
71
+ const reactStub = { createElement: () => null, useState: (v) => [v, () => {}], useEffect: () => {}, useCallback: (f) => f }
72
+ const windowStub = { __ModuleLoader__: { load: ({ factory }) => { mod = factory((n) => (n === 'react' ? reactStub : {})) } } }
73
+ try {
74
+ new Function('window', src)(windowStub)
75
+ ok(Boolean(mod && typeof mod.apply === 'function'), 'bundle 可装载(导出 apply)')
76
+ mod.apply({ effect: (f) => f(), slots: { inject: (n, f) => f(), register: (m) => regs.push(m.name) } })
77
+ const need = ['sidebar.right.pane.tab', 'sidebar.right.pane.tab.title', 'sidebar.toggle.badge']
78
+ ok(need.every((n) => regs.includes(n)), `注册了 3 个 slot(${regs.length})`)
79
+ } catch (e) { ok(false, `bundle 装载失败:${e.message}`) }
80
+ }
81
+
82
+ // 4.5) 插件不得跨目录 import 仓库 core/lib(复制安装时会 ERR_MODULE_NOT_FOUND ⇒ web boot 失败)
83
+ {
84
+ const srcFiles = ['src/index.js', 'src/data.mjs']
85
+ let bad = []
86
+ for (const f of srcFiles) {
87
+ const fp = join(REPO, 'plugins', 'argent-erp-ui', f)
88
+ if (!existsSync(fp)) continue
89
+ const t = readFileSync(fp, 'utf8')
90
+ const m = t.match(/from\s+['"](\.\.\/\.\.\/\.\.[^'"]+)['"]/)
91
+ if (m) bad.push(`${f} → ${m[1]}`)
92
+ }
93
+ ok(bad.length === 0, bad.length ? `插件存在跨目录 import(复制安装会崩):${bad.join('; ')}` : '插件不依赖仓库相对路径(复制安装安全)')
94
+ for (const v of ['vendor/account-token.mjs', 'vendor/redact.mjs']) {
95
+ ok(existsSync(join(REPO, 'plugins', 'argent-erp-ui', 'src', v)), `vendor 副本存在:${v}`)
96
+ }
97
+ }
98
+
99
+ // 5) gateway 文件
100
+ ok(existsSync(join(REPO, 'core', 'mcp', 'gateway.mjs')), 'core/mcp/gateway.mjs 存在')
101
+ ok(existsSync(join(REPO, 'core', 'mcp', 'erp-server.mjs')), 'core/mcp/erp-server.mjs 存在')
102
+
103
+ console.log(fails ? `\n❌ ${fails} 项未通过 —— 先修,别拿重启试错` : '\n✅ 结构自检全通:可以重启 DSH Desktop,之后跑 scripts/erp-ui-verify.mjs')
104
+ process.exit(fails ? 1 : 0)
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * erp-ui-verify.mjs —— 重启后 30 秒验收:面板端点是否真的挂上了(v0.9.0)
4
+ *
5
+ * 为什么需要:改完 profile 不重启,dsh 里**什么都看不到也没有报错**(路由 404/401 落到宿主兜底)。
6
+ * 判据只能来自"真打一次端点"。
7
+ *
8
+ * 用法:
9
+ * node scripts/erp-ui-verify.mjs # 默认打 http://127.0.0.1:55866
10
+ * node scripts/erp-ui-verify.mjs --base http://127.0.0.1:55866
11
+ *
12
+ * 退出码:0 全通 | 1 端点未就绪(多半是没重启) | 2 端点存在但数据取不到(token/网络/权限)
13
+ */
14
+ import { execSync } from 'node:child_process'
15
+ import { statSync } from 'node:fs'
16
+ import { join } from 'node:path'
17
+
18
+ const args = process.argv.slice(2)
19
+
20
+ /** 自动发现 dsh 桌面进程监听的端口(每次启动都会变,别让用户自己查) */
21
+ function discoverPorts() {
22
+ const ports = new Set()
23
+ try {
24
+ const out = execSync('lsof -nP -iTCP -sTCP:LISTEN 2>/dev/null || true', { encoding: 'utf8', timeout: 8000 })
25
+ for (const line of out.split('\n')) {
26
+ if (!/DeepSeek|dsh/i.test(line)) continue
27
+ const m = line.match(/:(\d{4,5})\s+\(LISTEN\)/)
28
+ if (m) ports.add(Number(m[1]))
29
+ }
30
+ } catch { /* 忽略 */ }
31
+ return [...ports].sort((a, b) => a - b)
32
+ }
33
+
34
+ async function pickBase() {
35
+ const explicit = args.includes('--base') ? args[args.indexOf('--base') + 1] : process.env.DSH_WEB_BASE
36
+ if (explicit) return explicit.replace(/\/+$/, '')
37
+ const ports = discoverPorts()
38
+ if (!ports.length) return 'http://127.0.0.1:55866'
39
+ for (const port of ports) {
40
+ const b = `http://127.0.0.1:${port}`
41
+ try {
42
+ const r = await fetch(`${b}/api/argent-desktop/health`, { signal: AbortSignal.timeout(1500) })
43
+ if (r.status > 0) return b // 该端口能应答(200/401 都算"是 dsh 服务")
44
+ } catch { /* 试下一个 */ }
45
+ }
46
+ return `http://127.0.0.1:${ports[0]}`
47
+ }
48
+
49
+ /** App 启动时间 vs 客户端 bundle 修改时间:bundle 更新更晚 ⇒ 你跑的是旧版(不重启就不生效) */
50
+ function bundleStaleness() {
51
+ try {
52
+ // pgrep -f 在这里匹配不到(实测返回空),用 ps + grep 更可靠
53
+ const pid = execSync("ps -eo pid,command | grep dsh-desktop-host | grep -v grep | awk '{print $1}' | head -1", { encoding: 'utf8' }).trim()
54
+ if (!pid) return null
55
+ const started = execSync(`ps -p ${pid} -o lstart=`, { encoding: 'utf8' }).trim()
56
+ const startMs = Date.parse(started)
57
+ const bundle = statSync(join(process.cwd(), 'plugins', 'argent-erp-ui', 'dist', 'client.js')).mtimeMs
58
+ if (Number.isNaN(startMs)) return null
59
+ return { started, startMs, bundleMs: bundle, stale: bundle > startMs }
60
+ } catch { return null }
61
+ }
62
+
63
+ const base = await pickBase()
64
+
65
+ const get = async (path) => {
66
+ const r = await fetch(`${base}${path}`, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(15000) })
67
+ const text = await r.text()
68
+ let body = null
69
+ try { body = JSON.parse(text) } catch { body = { raw: text.slice(0, 120) } }
70
+ return { status: r.status, body }
71
+ }
72
+
73
+ const line = (ok, msg) => console.log(`${ok ? '✅' : '❌'} ${msg}`)
74
+
75
+ ;(async () => {
76
+ console.log(`base: ${base}\n`)
77
+ const fresh = bundleStaleness()
78
+ if (fresh && fresh.stale) {
79
+ console.log(`⚠️ 客户端 bundle 比 App 启动时间新(bundle ${new Date(fresh.bundleMs).toLocaleString()} > 启动 ${new Date(fresh.startMs).toLocaleString()})`)
80
+ console.log(' ⇒ 你看到的仍是旧版面板;**退出 App 再打开**后才生效\n')
81
+ }
82
+ let h
83
+ try { h = await get('/api/argent-erp/health') } catch (e) {
84
+ line(false, `连不上 dsh 服务:${e.message}`)
85
+ console.log('\n⇒ 先确认 DSH Desktop 在运行;端口不是 55866 时用 --base 指定')
86
+ process.exit(1)
87
+ }
88
+ if (h.status !== 200 || !h.body || h.body.ok !== true) {
89
+ line(false, `/api/argent-erp/health → HTTP ${h.status}(路由未注册)`)
90
+ console.log('\n⇒ 插件尚未加载:**重启 DSH Desktop**(⛔ 不能由 agent 重启)')
91
+ console.log(' 回滚:node scripts/erp-ui-install.mjs --rollback ~/.dsh/profiles/desktop/backup-erpui-<ts>')
92
+ process.exit(1)
93
+ }
94
+ line(true, `/health 200(version=${h.body.version})`)
95
+ if (h.body.clientPingAt) console.log(` ✅ 客户端 bundle 已加载(ping ${h.body.clientPingAt})`)
96
+ else console.log(' ⚠️ 客户端 bundle 尚未 ping:面板不会出现(bundle 未加载,或还没触发)')
97
+
98
+ let failed = 0
99
+ for (const p of ['/api/argent-erp/whoami', '/api/argent-erp/overview', '/api/argent-erp/timeline', '/api/argent-erp/badge']) {
100
+ const r = await get(p)
101
+ const ok = r.status === 200 && r.body && r.body.ok === true
102
+ line(ok, `${p} → ${r.status}${ok ? '' : `(${(r.body && (r.body.message || r.body.error)) || '无 body'})`}`)
103
+ if (!ok) failed++
104
+ if (ok && p.endsWith('/timeline')) {
105
+ const tl = r.body.data || {}
106
+ console.log(` 时间线:${tl.total} 个流程,${(tl.days || []).length} 天`)
107
+ }
108
+ if (ok && p.endsWith('/overview')) {
109
+ const k = r.body.data.kpis || {}
110
+ console.log(` KPI:待处理 ${k.pending} · 在办 ${k.inProgress} · 今日完成 ${k.doneToday} · 执行机 ${k.agentsOnline}/${k.agentsTotal}`)
111
+ console.log(` 待办 ${(r.body.data.workItems || []).length} 条 · 凭据来源 ${r.body.data.authSource || '?'}`)
112
+ }
113
+ }
114
+ // 端到端:拿时间线第一条 → 打开它的 flow 详情(面板点击走的正是这条路径)
115
+ try {
116
+ const tl = await get('/api/argent-erp/timeline?days=30')
117
+ const ev = tl.body && tl.body.data && tl.body.data.days && tl.body.data.days[0] && tl.body.data.days[0].events[0]
118
+ if (ev && ev.id) {
119
+ const fd = await get(`/api/argent-erp/flow?id=${encodeURIComponent(ev.id)}`)
120
+ const nodes = fd.body && fd.body.data && fd.body.data.nodes
121
+ line(fd.status === 200 && Array.isArray(nodes) && nodes.length > 0, `/flow?id=${ev.id.slice(0, 8)}… → ${fd.status}(${Array.isArray(nodes) ? nodes.length : 0} 个节点)`)
122
+ } else {
123
+ console.log(' (时间线暂无数据,跳过 /flow 端到端)')
124
+ }
125
+ } catch (e) {
126
+ line(false, `/flow 端到端失败:${e.message}`)
127
+ failed++
128
+ }
129
+
130
+ if (failed) {
131
+ console.log('\n⇒ 端点已挂但取数失败:检查账号 token(~/.dsh/.credentials.yaml 的 WHYSHU_API_KEY)与网络')
132
+ process.exit(2)
133
+ }
134
+ console.log('\n全部通过。侧栏应出现「ERP」tab;有待办时侧栏开关上会出现红色角标。')
135
+ })()