@aiti-ai/ext-shared 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +36 -2
- package/agent-accounts.test.mjs +227 -0
- package/agent-accounts.ts +324 -0
- package/agent-capability-contract.test.mjs +129 -0
- package/agent-interaction.test.mjs +194 -0
- package/agent-interaction.ts +340 -0
- package/agent-launch.test.mjs +262 -0
- package/agent-spawn-env.test.mjs +169 -0
- package/agent-spawn-env.ts +548 -0
- package/aiti-editor-inject.mjs +49 -0
- package/backend-queue.test.mjs +336 -0
- package/backend-queue.ts +469 -0
- package/blob-save-route.ts +128 -0
- package/cbc-model-catalog.ts +86 -0
- package/check-client.mjs +273 -0
- package/check-ext-globals.mjs +144 -0
- package/controlled-login-dialog.mjs +270 -0
- package/controlled-login.mjs +398 -0
- package/controlled-login.test.mjs +466 -0
- package/convert-saveas.test.mjs +39 -0
- package/convert-saveas.ts +170 -0
- package/copy-client-bundle.mjs +39 -0
- package/cursor-cli-store.ts +377 -0
- package/dedupe-fetch.test.mjs +71 -0
- package/dedupe-fetch.ts +58 -0
- package/deploy-commit.mjs +52 -0
- package/dev-pack.mjs +246 -0
- package/dock-icons.ts +127 -0
- package/esbuild-bin.test.mjs +53 -0
- package/esbuild-bin.ts +66 -0
- package/exec-async.test.mjs +86 -0
- package/exec-async.ts +179 -0
- package/ext-display-names.ts +91 -0
- package/ext-entry-contracts.test.mjs +131 -0
- package/ext-float-dock-core.test.mjs +348 -0
- package/ext-float-dock-core.ts +783 -0
- package/ext-float-dock-lite.ts +52 -0
- package/ext-float-dock.test.mjs +286 -0
- package/ext-float-dock.ts +740 -0
- package/ext-icons-deploy.mjs +24 -0
- package/ext-lazy.test.mjs +186 -0
- package/ext-lazy.tsx +183 -0
- package/ext-model-state.test.mjs +272 -0
- package/ext-model-state.ts +324 -0
- package/ext-protocol.mjs +113 -0
- package/ext-protocol.test.mjs +88 -0
- package/ext-session-usage.test.mjs +219 -0
- package/ext-session-usage.ts +507 -0
- package/external-cli-governance.ts +153 -0
- package/git-ops-client/api.mjs +56 -0
- package/git-ops-client/branch-panel.tsx +987 -0
- package/git-ops-client/i18n.mjs +328 -0
- package/host-tools-allow.test.mjs +46 -0
- package/host-tools-allow.ts +84 -0
- package/icons/alert.svg +1 -0
- package/icons/bookmark.svg +1 -0
- package/icons/bot.svg +1 -0
- package/icons/chart-column.svg +1 -0
- package/icons/check-circle.svg +1 -0
- package/icons/clipboard-list.svg +1 -0
- package/icons/clock.svg +1 -0
- package/icons/coffee.svg +1 -0
- package/icons/edit.svg +1 -0
- package/icons/eye.svg +1 -0
- package/icons/git-branch.svg +1 -0
- package/icons/git-commit-horizontal.svg +1 -0
- package/icons/globe.svg +1 -0
- package/icons/history.svg +1 -0
- package/icons/import.svg +1 -0
- package/icons/inbox.svg +1 -0
- package/icons/key-round.svg +1 -0
- package/icons/layout-grid.svg +1 -0
- package/icons/mail.svg +1 -0
- package/icons/message-circle.svg +1 -0
- package/icons/message-square.svg +1 -0
- package/icons/meter-vertical.svg +1 -0
- package/icons/mic.svg +1 -0
- package/icons/monitor.svg +1 -0
- package/icons/pen-line.svg +1 -0
- package/icons/plug.svg +1 -0
- package/icons/radio-dot.svg +1 -0
- package/icons/radio.svg +1 -0
- package/icons/refresh.svg +1 -0
- package/icons/rocket.svg +1 -0
- package/icons/search.svg +1 -0
- package/icons/shield.svg +1 -0
- package/icons/sparkles.svg +1 -0
- package/icons/square-kanban.svg +1 -0
- package/icons/terminal.svg +1 -0
- package/icons/users.svg +1 -0
- package/icons/volume-2.svg +1 -0
- package/icons/wand.svg +1 -0
- package/icons/wrench.svg +1 -0
- package/icons/x-circle.svg +1 -0
- package/icons/x.svg +1 -0
- package/ignore-semantics.ts +76 -0
- package/lazy-loading-metrics.mjs +61 -0
- package/new-session-cwd.test.mjs +389 -0
- package/new-session-cwd.ts +253 -0
- package/open-viewer-window.ts +65 -0
- package/overlay-back-client.test.mjs +123 -0
- package/overlay-back-client.ts +87 -0
- package/package.json +65 -4
- package/pi-fork-rpc/README.md +12 -0
- package/pi-fork-rpc/cli-resolve.test.mjs +42 -0
- package/pi-fork-rpc/cli-resolve.ts +79 -0
- package/pi-fork-rpc/remote-queue.test.mjs +100 -0
- package/pi-fork-rpc/remote-queue.ts +140 -0
- package/pi-fork-rpc/spec.ts +106 -0
- package/pi-fork-rpc/wire.test.mjs +260 -0
- package/pi-fork-rpc/wire.ts +367 -0
- package/popup-anchor.ts +45 -0
- package/provider-priority-contract.test.mjs +55 -0
- package/reconcile-deps.mjs +41 -0
- package/reload-instances.mjs +60 -0
- package/seed-tool.test.mjs +95 -0
- package/seed-tool.ts +88 -0
- package/session-recycle.ts +109 -0
- package/shared-copy-sync.test.mjs +115 -0
- package/soffice-locate.ts +171 -0
- package/soffice-panel.ts +239 -0
- package/sync-dir.mjs +91 -0
- package/sync-dir.test.mjs +101 -0
- package/sync-gate.test.mjs +93 -0
- package/sync-mirror.test.mjs +85 -0
- package/task-list.test.mjs +91 -0
- package/task-list.ts +118 -0
- package/test-rm-rf.mjs +33 -0
- package/ui-dialogs.test.mjs +198 -0
- package/ui-dialogs.ts +310 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 aiti-ai contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @aiti-ai/ext-shared
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> aiti-agent 扩展的服务端共享层:agent-spawn-env(spawn env 治理 / resolveCliLaunch)、seed-tool(历史回放种子)、cursor-cli-store(Cursor CLI 会话读取器)、exec-async、git-ops-client 等共享模块。扩展 package.json 声明依赖后由 extman reconcile 统一装进共享根 node_modules,jiti walk-up 解析。
|
|
4
|
+
|
|
5
|
+
## 来源 · Origin
|
|
6
|
+
|
|
7
|
+
本包是 **aiti** 生态的组成部分——以 [pi](https://pi.dev) 为引擎的个人 AI 开发工作台与本地调试注入生态:**aiti-agent**(自托管 Web 工作台,基于开源项目 [pi-web](https://github.com/aiti-ai/pi-web) 构建,浏览器为主操作面)+ 各开发工具内的调试注入(aiti-devtools)+ 丰富的扩展体系。
|
|
8
|
+
|
|
9
|
+
This package is part of the **aiti** ecosystem — a personal AI development workbench powered by [pi](https://pi.dev): **aiti-agent** (a self-hosted web workbench built on the open-source [pi-web](https://github.com/aiti-ai/pi-web)) plus debug injection into mainstream dev tools (aiti-devtools) and a rich extension system.
|
|
10
|
+
|
|
11
|
+
- 工作台 / workbench: <https://github.com/aiti-ai/pi-web>
|
|
12
|
+
- 生态仓库 / monorepo: <https://github.com/aiti-ai/aiti-ai>
|
|
13
|
+
- 官网 / site: <https://aiti.pw>
|
|
14
|
+
|
|
15
|
+
## 功能定位(中文)
|
|
16
|
+
|
|
17
|
+
aiti-agent 扩展的服务端共享层:agent-spawn-env(spawn env 治理 / resolveCliLaunch)、seed-tool(历史回放种子)、cursor-cli-store(Cursor CLI 会话读取器)、exec-async、git-ops-client 等共享模块。扩展 package.json 声明依赖后由 extman reconcile 统一装进共享根 node_modules,jiti walk-up 解析。
|
|
18
|
+
|
|
19
|
+
## Positioning (English)
|
|
20
|
+
|
|
21
|
+
Server-side shared layer for aiti-agent extensions: agent-spawn-env (spawn env governance / resolveCliLaunch), seed-tool (history-replay seeds), cursor-cli-store (Cursor CLI session reader), exec-async, git-ops-client and more. Declared in each extension's package.json and installed into the shared node_modules root by extman reconcile (resolved via jiti walk-up).
|
|
22
|
+
|
|
23
|
+
## 安装 / 部署
|
|
24
|
+
|
|
25
|
+
随 aiti-agent 扩展体系部署(extman 安装 / deploy 脚本拷贝到 `~/.pi/web/extensions/`),一般不单独 npm 安装;作为依赖被引用时经 extman reconcile 自动装入共享 node_modules。
|
|
26
|
+
|
|
27
|
+
Deployed as part of the aiti-agent extension system (extman install / deploy scripts copy it to `~/.pi/web/extensions/`); usually not installed standalone.
|
|
28
|
+
|
|
29
|
+
## 文档 · Documentation
|
|
30
|
+
|
|
31
|
+
- 中文完整文档(aiti-docs): <https://aiti.pw/guide/ext-services.html>
|
|
32
|
+
- English docs: <https://aiti.pw/en/guide/ext-services.html>
|
|
33
|
+
- 扩展注册表 / registry: <https://aiti.pw/extensions.html> · machine-readable <https://aiti.pw/extensions.json>
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
MIT
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-accounts 共享引擎测试(单文件存储 / 快照 / 切换 / 自动保险 / 登出)。
|
|
3
|
+
*
|
|
4
|
+
* Run: node --test pi-web-ext/_shared/agent-accounts.test.mjs
|
|
5
|
+
*/
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import test from "node:test";
|
|
8
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
|
|
12
|
+
const {
|
|
13
|
+
emptyStore, readAccountsStore, writeAccountsStore,
|
|
14
|
+
sanitizeAlias, autoAlias,
|
|
15
|
+
currentSignature, hasAnySlotContent, isCurrentCovered,
|
|
16
|
+
listAccounts, saveAccount, switchAccount, restoreAccount, removeAccount, clearSlots,
|
|
17
|
+
} = await import("./agent-accounts.ts");
|
|
18
|
+
|
|
19
|
+
function tmpDir(tag) {
|
|
20
|
+
return mkdtempSync(join(tmpdir(), `agent-accounts-${tag}-`));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function write(p, s) {
|
|
24
|
+
mkdirSync(join(p, ".."), { recursive: true });
|
|
25
|
+
writeFileSync(p, s);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
test("sanitizeAlias:合法/非法(路径穿越、保留前缀、超长、空白)", () => {
|
|
29
|
+
assert.equal(sanitizeAlias("主账号"), "主账号");
|
|
30
|
+
assert.equal(sanitizeAlias("work-2"), "work-2");
|
|
31
|
+
assert.equal(sanitizeAlias(" Team A "), "Team A");
|
|
32
|
+
assert.equal(sanitizeAlias("_auto-before-switch"), null, "保留前缀拒绝");
|
|
33
|
+
assert.equal(sanitizeAlias("../evil"), null);
|
|
34
|
+
assert.equal(sanitizeAlias("a/b"), null);
|
|
35
|
+
assert.equal(sanitizeAlias("a\\b"), null);
|
|
36
|
+
assert.equal(sanitizeAlias(""), null);
|
|
37
|
+
assert.equal(sanitizeAlias("x".repeat(33)), null);
|
|
38
|
+
assert.equal(sanitizeAlias(".hidden"), null, "首字符非字母数字中文");
|
|
39
|
+
assert.equal(sanitizeAlias("a..b"), null);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("store 读写:缺失/坏 JSON → 空 store;写入可读回(原子)", () => {
|
|
43
|
+
const dir = tmpDir("store");
|
|
44
|
+
const file = join(dir, "agent-accounts.json");
|
|
45
|
+
assert.deepEqual(readAccountsStore(file), emptyStore(), "缺失 → 空");
|
|
46
|
+
write(file, "{broken json");
|
|
47
|
+
assert.deepEqual(readAccountsStore(file), emptyStore(), "坏 JSON → 空");
|
|
48
|
+
const st = emptyStore();
|
|
49
|
+
st.accounts.claude = { x: { savedAt: 1, files: {} } };
|
|
50
|
+
writeAccountsStore(file, st);
|
|
51
|
+
const back = readAccountsStore(file);
|
|
52
|
+
assert.equal(back.accounts.claude.x.savedAt, 1);
|
|
53
|
+
rmSync(dir, { recursive: true, force: true });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("saveAccount:无内容抛错;有内容记录签名/current/note", () => {
|
|
57
|
+
const dir = tmpDir("save");
|
|
58
|
+
const slots = [
|
|
59
|
+
{ key: "k1", label: "凭据 1", path: join(dir, "a", "one.json") },
|
|
60
|
+
{ key: "k2", label: "凭据 2", path: join(dir, "a", "two.json") },
|
|
61
|
+
];
|
|
62
|
+
const store = emptyStore();
|
|
63
|
+
assert.throws(() => saveAccount(store, "claude", "acc", slots), /无登录态可备份/);
|
|
64
|
+
|
|
65
|
+
write(slots[0].path, '{"token":"aaa"}');
|
|
66
|
+
const r = saveAccount(store, "claude", "acc", slots, { note: "user@x", account: { user: "李永", uin: "330116798388", expiresAt: 1797048848890 } });
|
|
67
|
+
assert.equal(r.overwritten, false);
|
|
68
|
+
assert.equal(r.entry.alias, "acc");
|
|
69
|
+
assert.equal(r.entry.slotCount, 1, "只收了有内容的槽位");
|
|
70
|
+
assert.equal(r.entry.note, "user@x");
|
|
71
|
+
assert.equal(r.entry.user, "李永", "账号展示元数据透出");
|
|
72
|
+
assert.equal(r.entry.uin, "330116798388");
|
|
73
|
+
assert.equal(r.entry.expiresAt, 1797048848890);
|
|
74
|
+
assert.equal(r.entry.auto, false);
|
|
75
|
+
const list = listAccounts(store, "claude", slots);
|
|
76
|
+
assert.equal(list.length, 1);
|
|
77
|
+
assert.equal(list[0].current, true, "刚刚从当前状态存下 → current");
|
|
78
|
+
|
|
79
|
+
// 覆盖同名档
|
|
80
|
+
write(slots[0].path, '{"token":"bbb"}');
|
|
81
|
+
const r2 = saveAccount(store, "claude", "acc", slots);
|
|
82
|
+
assert.equal(r2.overwritten, true);
|
|
83
|
+
rmSync(dir, { recursive: true, force: true });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("switchAccount:恢复内容 + 缺槽位删除 + current 变化", () => {
|
|
87
|
+
const dir = tmpDir("switch");
|
|
88
|
+
const slots = [
|
|
89
|
+
{ key: "one", label: "one", path: join(dir, "one.json") },
|
|
90
|
+
{ key: "two", label: "two", path: join(dir, "two.json") },
|
|
91
|
+
];
|
|
92
|
+
const store = emptyStore();
|
|
93
|
+
write(slots[0].path, "AAA");
|
|
94
|
+
write(slots[1].path, "B1");
|
|
95
|
+
saveAccount(store, "codex", "甲", slots);
|
|
96
|
+
|
|
97
|
+
write(slots[0].path, "CCC");
|
|
98
|
+
rmSync(slots[1].path, { force: true });
|
|
99
|
+
saveAccount(store, "codex", "乙", slots);
|
|
100
|
+
|
|
101
|
+
const r = switchAccount(store, "codex", "甲", slots);
|
|
102
|
+
assert.equal(readFileSync(slots[0].path, "utf8"), "AAA");
|
|
103
|
+
assert.equal(readFileSync(slots[1].path, "utf8"), "B1", "档里有的槽位写回");
|
|
104
|
+
assert.equal(r.entry.current, true);
|
|
105
|
+
const list = listAccounts(store, "codex", slots);
|
|
106
|
+
assert.equal(list.find((e) => e.alias === "甲").current, true);
|
|
107
|
+
assert.equal(list.find((e) => e.alias === "乙").current, false);
|
|
108
|
+
|
|
109
|
+
// 切回乙:甲多了 two.json,但乙档没有 → 删除,保证干净
|
|
110
|
+
switchAccount(store, "codex", "乙", slots);
|
|
111
|
+
assert.equal(readFileSync(slots[0].path, "utf8"), "CCC");
|
|
112
|
+
assert.equal(existsSync(slots[1].path), false, "目标档没有的槽位 → 删除当前文件");
|
|
113
|
+
rmSync(dir, { recursive: true, force: true });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("switchAccount:当前未存档 → 自动保险 _auto-before-switch(含未命名的新登录)", () => {
|
|
117
|
+
const dir = tmpDir("auto");
|
|
118
|
+
const slots = [{ key: "c", label: "cred", path: join(dir, "cred.json") }];
|
|
119
|
+
const store = emptyStore();
|
|
120
|
+
write(slots[0].path, "OLD");
|
|
121
|
+
saveAccount(store, "cursor", "老号", slots);
|
|
122
|
+
|
|
123
|
+
write(slots[0].path, "NEW-UNSAVED");
|
|
124
|
+
assert.equal(isCurrentCovered(store, "cursor", slots), false, "当前状态未被任何档覆盖");
|
|
125
|
+
const r = switchAccount(store, "cursor", "老号", slots);
|
|
126
|
+
assert.equal(r.autoSaved, true);
|
|
127
|
+
const auto = store.accounts.cursor[autoAlias("switch")];
|
|
128
|
+
assert.ok(auto, "自动保险档存在");
|
|
129
|
+
assert.equal(auto.files.c.data, "NEW-UNSAVED", "保险档保存的是切换前内容");
|
|
130
|
+
assert.equal(auto.auto, true);
|
|
131
|
+
assert.equal(readFileSync(slots[0].path, "utf8"), "OLD");
|
|
132
|
+
rmSync(dir, { recursive: true, force: true });
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("isCurrentCovered:有档匹配 → 不生成保险档;无内容 → false", () => {
|
|
136
|
+
const dir = tmpDir("covered");
|
|
137
|
+
const slots = [{ key: "c", label: "cred", path: join(dir, "cred.json") }];
|
|
138
|
+
const store = emptyStore();
|
|
139
|
+
assert.equal(hasAnySlotContent(slots), false);
|
|
140
|
+
assert.equal(isCurrentCovered(store, "zcode", slots), false);
|
|
141
|
+
write(slots[0].path, "X");
|
|
142
|
+
saveAccount(store, "zcode", "号", slots);
|
|
143
|
+
assert.equal(isCurrentCovered(store, "zcode", slots), true);
|
|
144
|
+
const r = switchAccount(store, "zcode", "号", slots);
|
|
145
|
+
assert.equal(r.autoSaved, false, "已覆盖 → 不重复保险");
|
|
146
|
+
assert.equal(store.accounts.zcode[autoAlias("switch")], undefined);
|
|
147
|
+
rmSync(dir, { recursive: true, force: true });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("switchAccount:目标档不存在抛错;removeAccount 删除含自动档", () => {
|
|
151
|
+
const dir = tmpDir("rm");
|
|
152
|
+
const slots = [{ key: "c", label: "cred", path: join(dir, "cred.json") }];
|
|
153
|
+
const store = emptyStore();
|
|
154
|
+
assert.throws(() => switchAccount(store, "claude", "没有", slots), /账号档不存在/);
|
|
155
|
+
write(slots[0].path, "X");
|
|
156
|
+
saveAccount(store, "claude", "A", slots);
|
|
157
|
+
saveAccount(store, "claude", "B", slots);
|
|
158
|
+
saveAccount(store, "claude", autoAlias("logout"), slots, { auto: true });
|
|
159
|
+
assert.equal(removeAccount(store, "claude", autoAlias("logout")), true);
|
|
160
|
+
assert.equal(removeAccount(store, "claude", "不存在"), false);
|
|
161
|
+
assert.equal(removeAccount(store, "claude", "B"), true);
|
|
162
|
+
const list = listAccounts(store, "claude", slots);
|
|
163
|
+
assert.deepEqual(list.map((e) => e.alias), ["A"]);
|
|
164
|
+
rmSync(dir, { recursive: true, force: true });
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("clearSlots:删除现有文件(登出)并返回路径;不存在不报错", () => {
|
|
168
|
+
const dir = tmpDir("clear");
|
|
169
|
+
const slots = [
|
|
170
|
+
{ key: "a", label: "a", path: join(dir, "a.json") },
|
|
171
|
+
{ key: "b", label: "b", path: join(dir, "b.json") },
|
|
172
|
+
];
|
|
173
|
+
write(slots[0].path, "A");
|
|
174
|
+
const removed = clearSlots(slots);
|
|
175
|
+
assert.deepEqual(removed, [slots[0].path]);
|
|
176
|
+
assert.equal(existsSync(slots[0].path), false);
|
|
177
|
+
assert.deepEqual(clearSlots(slots), [], "再清不报错");
|
|
178
|
+
rmSync(dir, { recursive: true, force: true });
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("二进制内容 base64 往返 + restoreAccount 直接恢复", () => {
|
|
182
|
+
const dir = tmpDir("b64");
|
|
183
|
+
const p = join(dir, "user.bin");
|
|
184
|
+
const slots = [{ key: "bin", label: "bin", path: p }];
|
|
185
|
+
const raw = Buffer.from([0x00, 0xff, 0x10, 0x80, 0x01]);
|
|
186
|
+
mkdirSync(dir, { recursive: true });
|
|
187
|
+
writeFileSync(p, raw);
|
|
188
|
+
const store = emptyStore();
|
|
189
|
+
saveAccount(store, "qoder", "档", slots);
|
|
190
|
+
assert.equal(store.accounts.qoder["档"].files.bin.b64, true, "非 UTF-8 → b64");
|
|
191
|
+
rmSync(p, { force: true });
|
|
192
|
+
restoreAccount(store, "qoder", "档", slots);
|
|
193
|
+
assert.deepEqual(readFileSync(p), raw, "字节级往返一致");
|
|
194
|
+
rmSync(dir, { recursive: true, force: true });
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("currentSignature:内容变化签名变化;空槽位集合签名稳定", () => {
|
|
198
|
+
const dir = tmpDir("sig");
|
|
199
|
+
const slots = [{ key: "c", label: "c", path: join(dir, "c.json") }];
|
|
200
|
+
const empty1 = currentSignature(slots);
|
|
201
|
+
const empty2 = currentSignature([]);
|
|
202
|
+
assert.equal(empty1, empty2, "无内容 = 空签名");
|
|
203
|
+
write(slots[0].path, "1");
|
|
204
|
+
const s1 = currentSignature(slots);
|
|
205
|
+
write(slots[0].path, "2");
|
|
206
|
+
const s2 = currentSignature(slots);
|
|
207
|
+
assert.notEqual(s1, s2);
|
|
208
|
+
assert.notEqual(s1, empty1);
|
|
209
|
+
rmSync(dir, { recursive: true, force: true });
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("identity:token 自动刷新(内容轮换)仍判 current;同身份不重复保险", () => {
|
|
213
|
+
const dir = tmpDir("identity");
|
|
214
|
+
const slots = [{ key: "c", label: "c", path: join(dir, "cred.json") }];
|
|
215
|
+
const store = emptyStore();
|
|
216
|
+
write(slots[0].path, "TOKEN-1");
|
|
217
|
+
saveAccount(store, "codebuddy", "甲", slots, { identity: "李永|123" });
|
|
218
|
+
write(slots[0].path, "TOKEN-2"); // CLI 后台刷新 token
|
|
219
|
+
const list = listAccounts(store, "codebuddy", slots, { identity: "李永|123" });
|
|
220
|
+
assert.equal(list[0].current, true, "同身份 → 仍标当前(不再因 token 轮换掉标记)");
|
|
221
|
+
assert.equal(isCurrentCovered(store, "codebuddy", slots, "李永|123"), true);
|
|
222
|
+
assert.equal(isCurrentCovered(store, "codebuddy", slots, "别人|456"), false, "不同身份 → 未覆盖");
|
|
223
|
+
const r = switchAccount(store, "codebuddy", "甲", slots, { identity: "李永|123" });
|
|
224
|
+
assert.equal(r.autoSaved, false, "同身份切向自身 → 不生成保险档");
|
|
225
|
+
assert.equal(store.accounts.codebuddy[autoAlias("switch")], undefined);
|
|
226
|
+
rmSync(dir, { recursive: true, force: true });
|
|
227
|
+
});
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-accounts —— 多 agent 登录账号档(别名备份 / 一键切换 / 自动保险)共享引擎
|
|
3
|
+
* (纯模块,可测;权威源在本文件,调用方:aiti-agent-manager)。
|
|
4
|
+
*
|
|
5
|
+
* 设计(2026-09-13 拍板):
|
|
6
|
+
* - **单文件存储**:所有 agent、所有账号档收在一个 JSON 文件里
|
|
7
|
+
* (默认 `<agent-manager configDir>/agent-accounts.json`,0600)——aiti-configs
|
|
8
|
+
* 备份时一条 encrypt pattern(agent-accounts.json)即可全量加密,不散落多文件。
|
|
9
|
+
* - **slot**:每 agent 的登录凭据 = 一组「槽位」(key / label / 当前机器绝对路径),
|
|
10
|
+
* 由调用方逐机器解析;快照只存 key + 内容,切回时按当前机器的路径写回(跨机备份可用)。
|
|
11
|
+
* - **自动保险档**:switch / login / logout 等覆盖性操作前,当前登录若未被任何档覆盖 →
|
|
12
|
+
* 自动存 `_auto-before-<kind>`——永不因切换 / 重新登录丢账号,也无须用户先手动备份。
|
|
13
|
+
* - **current 判定**:内容签名(sha256)比对,不存额外状态,文件被外部改动也能感知。
|
|
14
|
+
* - **登出**:`clearSlots` 删除当前凭据文件(先经调用方自动保险);feature/env 注入的
|
|
15
|
+
* 凭据不在文件层,天然不受影响(调用方可提示)。
|
|
16
|
+
*
|
|
17
|
+
* 存储结构:
|
|
18
|
+
* { version: 1,
|
|
19
|
+
* accounts: { <agent>: { <alias>: {
|
|
20
|
+
* savedAt: number, auto?: boolean, note?: string,
|
|
21
|
+
* files: { <slotKey>: { data: string, b64?: boolean, label?: string } } } } } }
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { createHash } from "node:crypto";
|
|
25
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
26
|
+
import { dirname } from "node:path";
|
|
27
|
+
|
|
28
|
+
export const ACCOUNTS_STORE_VERSION = 1;
|
|
29
|
+
/** 单文件体积防御(正常几十 KB;超限说明误收大文件,拒绝写入并报错)。 */
|
|
30
|
+
export const MAX_STORE_BYTES = 4 * 1024 * 1024;
|
|
31
|
+
/** 自动保险档前缀(用户手动别名禁以此开头)。 */
|
|
32
|
+
export const AUTO_ALIAS_PREFIX = "_auto-";
|
|
33
|
+
|
|
34
|
+
/** 一个凭据槽位(key 稳定逻辑标识,写入快照/切回时与当前机器路径解耦)。 */
|
|
35
|
+
export interface AccountSlot {
|
|
36
|
+
key: string;
|
|
37
|
+
label: string;
|
|
38
|
+
/** 当前机器绝对路径(不存在 = 该槽位暂无内容)。 */
|
|
39
|
+
path: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface StoredFile {
|
|
43
|
+
data: string;
|
|
44
|
+
/** true = data 为 base64(原文件非 UTF-8 文本);缺省 = 纯文本 */
|
|
45
|
+
b64?: boolean;
|
|
46
|
+
label?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface StoredAccount {
|
|
50
|
+
savedAt: number;
|
|
51
|
+
auto?: boolean;
|
|
52
|
+
note?: string;
|
|
53
|
+
/** 账号身份指纹(调用方构造,如 "李永|330116798388")——token 自动刷新后仍能标记「当前」 */
|
|
54
|
+
identity?: string;
|
|
55
|
+
/** 账号展示元数据(user/uin/expiresAt;展示用,不含 token) */
|
|
56
|
+
account?: AccountDisplay;
|
|
57
|
+
files: Record<string, StoredFile>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 账号展示元数据(各家 /login/status 提取;字段可缺)。 */
|
|
61
|
+
export interface AccountDisplay {
|
|
62
|
+
user?: string;
|
|
63
|
+
uin?: string;
|
|
64
|
+
/** 凭据有效期(ms 时间戳;token 过期时间) */
|
|
65
|
+
expiresAt?: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface AccountsStore {
|
|
69
|
+
version: number;
|
|
70
|
+
accounts: Record<string, Record<string, StoredAccount>>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 列表项(面板展示用;不含文件内容)。 */
|
|
74
|
+
export interface AccountEntry {
|
|
75
|
+
alias: string;
|
|
76
|
+
savedAt: number;
|
|
77
|
+
auto: boolean;
|
|
78
|
+
note?: string;
|
|
79
|
+
/** 账号展示元数据(user/uin/expiresAt;旧档缺省时由调用方按身份回填) */
|
|
80
|
+
user?: string;
|
|
81
|
+
uin?: string;
|
|
82
|
+
expiresAt?: number;
|
|
83
|
+
slotCount: number;
|
|
84
|
+
/** 当前机器上已有内容的槽位数(档存了但本机没这个槽位时 < slotCount) */
|
|
85
|
+
presentCount: number;
|
|
86
|
+
current: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function emptyStore(): AccountsStore {
|
|
90
|
+
return { version: ACCOUNTS_STORE_VERSION, accounts: {} };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 读单文件存储(缺失/坏 JSON/版本不符 → 空 store,不抛)。 */
|
|
94
|
+
export function readAccountsStore(file: string): AccountsStore {
|
|
95
|
+
try {
|
|
96
|
+
const j = JSON.parse(readFileSync(file, "utf8")) as AccountsStore;
|
|
97
|
+
if (!j || typeof j !== "object" || !j.accounts || typeof j.accounts !== "object") return emptyStore();
|
|
98
|
+
return { version: ACCOUNTS_STORE_VERSION, accounts: j.accounts };
|
|
99
|
+
} catch {
|
|
100
|
+
return emptyStore();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 原子写单文件存储(临时文件 + rename;0600;体积防御)。 */
|
|
105
|
+
export function writeAccountsStore(file: string, store: AccountsStore): void {
|
|
106
|
+
const body = JSON.stringify({ version: ACCOUNTS_STORE_VERSION, accounts: store.accounts }, null, 1);
|
|
107
|
+
if (Buffer.byteLength(body) > MAX_STORE_BYTES) {
|
|
108
|
+
throw new Error(`账号档文件超限(>${Math.round(MAX_STORE_BYTES / 1024 / 1024)}MB)——检查槽位是否误收大文件`);
|
|
109
|
+
}
|
|
110
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
111
|
+
const tmp = `${file}.tmp-${process.pid}`;
|
|
112
|
+
writeFileSync(tmp, body, { mode: 0o600 });
|
|
113
|
+
try { chmodSync(tmp, 0o600); } catch { /* 非 POSIX 容错 */ }
|
|
114
|
+
renameSync(tmp, file);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 用户输入别名净化:1-32 字符,中文/字母/数字/._- 空格,首字符非符号,禁保留前缀与路径分隔。 */
|
|
118
|
+
export function sanitizeAlias(alias: unknown): string | null {
|
|
119
|
+
const a = String(alias ?? "").trim();
|
|
120
|
+
if (!a || a.length > 32) return null;
|
|
121
|
+
if (a.startsWith(AUTO_ALIAS_PREFIX)) return null;
|
|
122
|
+
if (!/^[\p{L}\p{N}][\p{L}\p{N}._ -]*$/u.test(a)) return null;
|
|
123
|
+
if (a.includes("..") || a.includes("/") || a.includes("\\")) return null;
|
|
124
|
+
return a;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 自动保险档别名。 */
|
|
128
|
+
export function autoAlias(kind: "switch" | "login" | "logout"): string {
|
|
129
|
+
return `${AUTO_ALIAS_PREFIX}before-${kind}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 槽位当前内容(文件不存在/空文件 → null;非 UTF-8 二进制 → base64)。 */
|
|
133
|
+
function readSlotData(slot: AccountSlot): StoredFile | null {
|
|
134
|
+
try {
|
|
135
|
+
if (!existsSync(slot.path)) return null;
|
|
136
|
+
const st = statSync(slot.path);
|
|
137
|
+
if (!st.isFile() || st.size === 0) return null;
|
|
138
|
+
const buf = readFileSync(slot.path);
|
|
139
|
+
const text = buf.toString("utf8");
|
|
140
|
+
const lossless = Buffer.compare(Buffer.from(text, "utf8"), buf) === 0 && !text.includes("\u0000");
|
|
141
|
+
return lossless ? { data: text, label: slot.label } : { data: buf.toString("base64"), b64: true, label: slot.label };
|
|
142
|
+
} catch {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function storedToBuffer(f: StoredFile): Buffer {
|
|
148
|
+
return f.b64 ? Buffer.from(f.data, "base64") : Buffer.from(f.data, "utf8");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 槽位集合的内容签名(只含当前存在的内容;key 排序稳定)。 */
|
|
152
|
+
export function currentSignature(slots: AccountSlot[]): string {
|
|
153
|
+
const h = createHash("sha256");
|
|
154
|
+
for (const s of [...slots].sort((a, b) => a.key.localeCompare(b.key))) {
|
|
155
|
+
const f = readSlotData(s);
|
|
156
|
+
if (!f) continue;
|
|
157
|
+
h.update(s.key).update("\u0000").update(f.b64 ? "b64:" : "").update(f.data).update("\u0000");
|
|
158
|
+
}
|
|
159
|
+
return h.digest("hex").slice(0, 16);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function accountSignature(acct: StoredAccount): string {
|
|
163
|
+
const h = createHash("sha256");
|
|
164
|
+
for (const key of Object.keys(acct.files).sort()) {
|
|
165
|
+
const f = acct.files[key];
|
|
166
|
+
h.update(key).update("\u0000").update(f.b64 ? "b64:" : "").update(f.data).update("\u0000");
|
|
167
|
+
}
|
|
168
|
+
return h.digest("hex").slice(0, 16);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** 当前槽位是否已有内容(有 = 可备份 / 需保险)。 */
|
|
172
|
+
export function hasAnySlotContent(slots: AccountSlot[]): boolean {
|
|
173
|
+
return slots.some((s) => readSlotData(s) !== null);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 当前登录是否已被某个档覆盖(用于自动保险前的判断;identity 相同也算覆盖)。 */
|
|
177
|
+
export function isCurrentCovered(store: AccountsStore, agent: string, slots: AccountSlot[], identity?: string): boolean {
|
|
178
|
+
const sig = currentSignature(slots);
|
|
179
|
+
if (sig === currentSignature([])) return false; // 当前无内容
|
|
180
|
+
return Object.values(store.accounts[agent] ?? {}).some((a) =>
|
|
181
|
+
accountSignature(a) === sig || Boolean(identity && a.identity && a.identity === identity));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 列出某 agent 的账号档(savedAt 倒序)。
|
|
186
|
+
* current = 内容签名一致,**或** identity 相同(token 自动刷新导致内容轮换时仍认得同一账号)。
|
|
187
|
+
*/
|
|
188
|
+
export function listAccounts(
|
|
189
|
+
store: AccountsStore,
|
|
190
|
+
agent: string,
|
|
191
|
+
slots: AccountSlot[],
|
|
192
|
+
opts: { identity?: string } = {},
|
|
193
|
+
): AccountEntry[] {
|
|
194
|
+
const cur = currentSignature(slots);
|
|
195
|
+
const map = store.accounts[agent] ?? {};
|
|
196
|
+
const presentKeys = new Set(slots.map((s) => s.key));
|
|
197
|
+
return Object.entries(map)
|
|
198
|
+
.map(([alias, acct]) => ({
|
|
199
|
+
alias,
|
|
200
|
+
savedAt: acct.savedAt,
|
|
201
|
+
auto: acct.auto === true || alias.startsWith(AUTO_ALIAS_PREFIX),
|
|
202
|
+
...(acct.note ? { note: acct.note } : {}),
|
|
203
|
+
...(acct.account?.user ? { user: acct.account.user } : {}),
|
|
204
|
+
...(acct.account?.uin ? { uin: acct.account.uin } : {}),
|
|
205
|
+
...(typeof acct.account?.expiresAt === "number" && Number.isFinite(acct.account.expiresAt) ? { expiresAt: acct.account.expiresAt } : {}),
|
|
206
|
+
slotCount: Object.keys(acct.files).length,
|
|
207
|
+
presentCount: Object.keys(acct.files).filter((k) => presentKeys.has(k)).length,
|
|
208
|
+
current: Object.keys(acct.files).length > 0
|
|
209
|
+
&& (accountSignature(acct) === cur || Boolean(opts.identity && acct.identity && acct.identity === opts.identity)),
|
|
210
|
+
}))
|
|
211
|
+
.sort((a, b) => b.savedAt - a.savedAt);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 备份当前登录为账号档(覆盖同名档)。
|
|
216
|
+
* @throws 当前无任何槽位内容时(没有可备份的登录态)
|
|
217
|
+
*/
|
|
218
|
+
export function saveAccount(
|
|
219
|
+
store: AccountsStore,
|
|
220
|
+
agent: string,
|
|
221
|
+
alias: string,
|
|
222
|
+
slots: AccountSlot[],
|
|
223
|
+
opts: { auto?: boolean; note?: string; identity?: string; account?: AccountDisplay; now?: number } = {},
|
|
224
|
+
): { entry: AccountEntry; overwritten: boolean } {
|
|
225
|
+
const clean = opts.auto ? String(alias) : sanitizeAlias(alias);
|
|
226
|
+
if (!clean || clean.includes("/") || clean.includes("\\") || clean.includes("..")) {
|
|
227
|
+
throw new Error("别名非法:1-32 字符,可用中文/字母/数字/./_/-/空格,且不得以 _auto- 开头");
|
|
228
|
+
}
|
|
229
|
+
const files: Record<string, StoredFile> = {};
|
|
230
|
+
for (const s of slots) {
|
|
231
|
+
const f = readSlotData(s);
|
|
232
|
+
if (f) files[s.key] = f;
|
|
233
|
+
}
|
|
234
|
+
if (!Object.keys(files).length) throw new Error("当前无登录态可备份(未检测到任何凭据文件)");
|
|
235
|
+
const prev = store.accounts[agent]?.[clean];
|
|
236
|
+
const account = opts.account && (opts.account.user || opts.account.uin || typeof opts.account.expiresAt === "number") ? opts.account : undefined;
|
|
237
|
+
store.accounts[agent] = store.accounts[agent] ?? {};
|
|
238
|
+
store.accounts[agent][clean] = {
|
|
239
|
+
savedAt: opts.now ?? Date.now(),
|
|
240
|
+
...(opts.auto ? { auto: true } : {}),
|
|
241
|
+
...(opts.note ? { note: opts.note } : {}),
|
|
242
|
+
...(opts.identity ? { identity: opts.identity } : {}),
|
|
243
|
+
...(account ? { account } : {}),
|
|
244
|
+
files,
|
|
245
|
+
};
|
|
246
|
+
const entries = listAccounts(store, agent, slots, { identity: opts.identity });
|
|
247
|
+
const entry = entries.find((e) => e.alias === clean)!;
|
|
248
|
+
return { entry, overwritten: Boolean(prev) };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** 把账号档内容恢复到槽位路径(档里没有的槽位 → 删除当前文件,保证切换后状态干净)。 */
|
|
252
|
+
export function restoreAccount(store: AccountsStore, agent: string, alias: string, slots: AccountSlot[]): StoredAccount {
|
|
253
|
+
const acct = store.accounts[agent]?.[alias];
|
|
254
|
+
if (!acct) throw new Error(`账号档不存在:${agent} / ${alias}`);
|
|
255
|
+
for (const s of slots) {
|
|
256
|
+
const f = acct.files[s.key];
|
|
257
|
+
if (f) {
|
|
258
|
+
mkdirSync(dirname(s.path), { recursive: true, mode: 0o700 });
|
|
259
|
+
writeFileSync(s.path, storedToBuffer(f), { mode: 0o600 });
|
|
260
|
+
try { chmodSync(s.path, 0o600); } catch { /* 非 POSIX 容错 */ }
|
|
261
|
+
} else {
|
|
262
|
+
try { rmSync(s.path, { force: true }); } catch { /* 已不在 */ }
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return acct;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 切换到账号档:当前登录未被任何档覆盖时先自动存 `_auto-before-switch`,再恢复目标档。
|
|
270
|
+
* @returns autoSaved = 是否生成了自动保险档
|
|
271
|
+
*/
|
|
272
|
+
export function switchAccount(
|
|
273
|
+
store: AccountsStore,
|
|
274
|
+
agent: string,
|
|
275
|
+
alias: string,
|
|
276
|
+
slots: AccountSlot[],
|
|
277
|
+
opts: { now?: number; note?: string; identity?: string; account?: AccountDisplay } = {},
|
|
278
|
+
): { entry: AccountEntry; autoSaved: boolean } {
|
|
279
|
+
const clean = String(alias ?? "").trim();
|
|
280
|
+
if (!store.accounts[agent]?.[clean]) throw new Error(`账号档不存在:${clean}`);
|
|
281
|
+
let autoSaved = false;
|
|
282
|
+
if (hasAnySlotContent(slots) && !isCurrentCovered(store, agent, slots, opts.identity)) {
|
|
283
|
+
try {
|
|
284
|
+
saveAccount(store, agent, autoAlias("switch"), slots, {
|
|
285
|
+
auto: true, now: opts.now,
|
|
286
|
+
...(opts.identity ? { identity: opts.identity } : {}),
|
|
287
|
+
...(opts.account ? { account: opts.account } : {}),
|
|
288
|
+
});
|
|
289
|
+
autoSaved = true;
|
|
290
|
+
} catch { /* 保险失败不阻断切换 */ }
|
|
291
|
+
}
|
|
292
|
+
restoreAccount(store, agent, clean, slots);
|
|
293
|
+
const entries = listAccounts(store, agent, slots, { identity: opts.identity });
|
|
294
|
+
const entry = entries.find((e) => e.alias === clean)!;
|
|
295
|
+
return { entry, autoSaved };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** 删除账号档(含自动保险档)。 */
|
|
299
|
+
export function removeAccount(store: AccountsStore, agent: string, alias: string): boolean {
|
|
300
|
+
const clean = String(alias ?? "").trim();
|
|
301
|
+
if (!clean || clean.includes("/") || clean.includes("\\") || clean.includes("..")) throw new Error("别名非法");
|
|
302
|
+
const bag = store.accounts[agent];
|
|
303
|
+
if (!bag?.[clean]) return false;
|
|
304
|
+
delete bag[clean];
|
|
305
|
+
if (!Object.keys(bag).length) delete store.accounts[agent];
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* 登出(清空全部槽位文件)。调用方应先确保当前登录已存档(自动保险 `_auto-before-logout`)。
|
|
311
|
+
* @returns 实际删除的文件路径
|
|
312
|
+
*/
|
|
313
|
+
export function clearSlots(slots: AccountSlot[]): string[] {
|
|
314
|
+
const removed: string[] = [];
|
|
315
|
+
for (const s of slots) {
|
|
316
|
+
try {
|
|
317
|
+
if (!existsSync(s.path)) continue;
|
|
318
|
+
rmSync(s.path, { force: true });
|
|
319
|
+
removed.push(s.path);
|
|
320
|
+
} catch { /* 删失败留给下次 */ }
|
|
321
|
+
}
|
|
322
|
+
return removed;
|
|
323
|
+
}
|
|
324
|
+
|