weaver-e9-assistant-cli 1.1.4 → 1.1.11

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/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # weaver-e9-assistant
2
+
@@ -72,7 +72,7 @@ async function launchSystemBrowser(chromium, headless, args) {
72
72
  const missingCore = /Executable doesn't exist|executablePath.*(exist|found)|not found.*browser|download.*chromium|Please run the following command|playwright install/i.test(msg);
73
73
  if (!missingCore)
74
74
  throw new Error(`无法启动浏览器(系统浏览器与 Playwright 自带内核均不可用):${msg}`);
75
- console.log(' [AUTH] 系统未检测到 Chrome/Edge/Brave,需下载 Playwright chromium 内核(约 120-200 MiB,仅此情况才需要)...');
75
+ console.error(' [AUTH] 系统未检测到 Chrome/Edge/Brave,需下载 Playwright chromium 内核(约 120-200 MiB,仅此情况才需要)...');
76
76
  const dl = await runCmd('npx playwright install chromium', 300000);
77
77
  if (dl.code !== 0) {
78
78
  throw new Error(`chromium 内核下载失败(exit ${dl.code},网络可能拦截)。可手动 \`npx playwright install chromium\`,或改用 \`e9 auth cookie-login\`。`);
@@ -189,7 +189,7 @@ export async function ensurePlaywright() {
189
189
  if (!online) {
190
190
  throw new Error('playwright 未安装且当前环境无法联网安装。请先 `npm install playwright`,或改用 `e9 auth cookie-login` 手动导入 cookie(无需浏览器)。');
191
191
  }
192
- console.log(' [AUTH] playwright 未安装,联网自动安装模块(chromium 内核按需懒下载,通常无需)...');
192
+ console.error(' [AUTH] playwright 未安装,联网自动安装模块(chromium 内核按需懒下载,通常无需)...');
193
193
  const r1 = await runCmd('npm install playwright', 120000);
194
194
  if (r1.code !== 0) {
195
195
  throw new Error(`playwright 模块安装失败(npm exit ${r1.code})。可手动 \`npm install playwright\`,或改用 \`e9 auth cookie-login\`。`);
@@ -278,7 +278,7 @@ export async function loginForCookie(baseUrl, username, password, opts = {}) {
278
278
  ]);
279
279
  }
280
280
  else if (!headless) {
281
- console.log(' 请在浏览器登录页输入账密并点登录(最长 600s)...');
281
+ console.error(' 请在浏览器登录页输入账密并点登录(最长 600s)...');
282
282
  }
283
283
  // 轮询 loginidweaver。autoFill 未拿到 cookie 时原地转手动登录(复用同一浏览器会话,不重开)
284
284
  const t0 = Date.now();
@@ -306,7 +306,7 @@ export async function loginForCookie(baseUrl, username, password, opts = {}) {
306
306
  portaled = true;
307
307
  }
308
308
  if (!headless && !prompted && Date.now() - t0 > 30000) {
309
- console.log(' ⏳ 仍在等待 loginidweaver cookie...');
309
+ console.error(' ⏳ 仍在等待 loginidweaver cookie...');
310
310
  prompted = true;
311
311
  }
312
312
  await sleep(200);
@@ -0,0 +1,73 @@
1
+ /**
2
+ * 统一缓存 I/O 模块(横切共享)。
3
+ *
4
+ * 为 node_map、cube_catalog、subordinate_scope 三类环境级单文件缓存提供
5
+ * 一致的读取(含 TTL 检查)、写入和 read-modify-write 原语。
6
+ *
7
+ * 设计原则:
8
+ * - fail-soft:任何读写错误返回 null/void,不抛异常
9
+ * - TTL 检查统一读 raw.fetchedAt || raw.updatedAt
10
+ * - 不负责业务校验(如 selfId 匹配、reliable 标志)
11
+ */
12
+
13
+ import fs from 'node:fs';
14
+ import path from 'node:path';
15
+
16
+ /**
17
+ * 读缓存文件并做 TTL 检查。
18
+ *
19
+ * @param {string} filePath - 缓存文件绝对路径
20
+ * @param {number} [ttlMs] - 过期毫秒数;null/undefined 时跳过 TTL 检查
21
+ * @returns {object|null} 解析后的 JSON 对象;文件缺失/损坏/过期时返回 null
22
+ */
23
+ export function cacheGet(filePath, ttlMs) {
24
+ try {
25
+ if (!fs.existsSync(filePath)) return null;
26
+ const raw = JSON.parse(fs.readFileSync(filePath, 'utf8'));
27
+ if (!raw || typeof raw !== 'object') return null;
28
+ if (ttlMs != null) {
29
+ const ts = raw.fetchedAt || raw.updatedAt;
30
+ if (ts && (Date.now() - new Date(ts).getTime() > ttlMs)) return null;
31
+ }
32
+ return raw;
33
+ } catch {
34
+ return null;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * 写缓存(创建父目录 + JSON 序列化,fail-soft)。
40
+ *
41
+ * @param {string} filePath - 缓存文件绝对路径
42
+ * @param {*} value - 要写入的值(会经 JSON.stringify)
43
+ */
44
+ export function cacheSet(filePath, value) {
45
+ try {
46
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
47
+ fs.writeFileSync(filePath, JSON.stringify(value, null, 2) + '\n', 'utf8');
48
+ } catch { /* 写失败不阻断业务 */ }
49
+ }
50
+
51
+ /**
52
+ * read-modify-write:读取当前值 → 经 fn 变换 → 写回。
53
+ *
54
+ * @param {string} filePath - 缓存文件绝对路径
55
+ * @param {Function} fn - (currentValue|null) => newValue;返回 null/undefined 时中止不写
56
+ * @returns {*|null} 写入的新值;中止或出错返回 null
57
+ */
58
+ export function cacheUpdate(filePath, fn) {
59
+ try {
60
+ let current = null;
61
+ if (fs.existsSync(filePath)) {
62
+ const raw = JSON.parse(fs.readFileSync(filePath, 'utf8'));
63
+ if (raw && typeof raw === 'object') current = raw;
64
+ }
65
+ const next = fn(current);
66
+ if (next === undefined || next === null) return null;
67
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
68
+ fs.writeFileSync(filePath, JSON.stringify(next, null, 2) + '\n', 'utf8');
69
+ return next;
70
+ } catch {
71
+ return null;
72
+ }
73
+ }