pi-web-ui 0.113.1 → 0.114.0
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/CHANGELOG.md +43 -1
- package/README.md +117 -246
- package/README.zh-CN.md +145 -217
- package/README.zh-TW.md +145 -217
- package/bin/device-hub-cli.mjs +577 -0
- package/bin/pi-device-hub.mjs +9 -0
- package/bin/pi-web-ui.mjs +47 -15
- package/dist/server/agent-service-daemon.js +102 -24
- package/dist/server/agent-service.js +2289 -1120
- package/dist/server/approval-rules.js +29 -3
- package/dist/server/ask-user-question-tool.js +8 -8
- package/dist/server/atomic-file.js +35 -14
- package/dist/server/attachment-store.js +71 -35
- package/dist/server/attachments.js +18 -8
- package/dist/server/canvas/canvas-agent-tool.js +65 -15
- package/dist/server/canvas/canvas-dispatch.js +61 -20
- package/dist/server/canvas/canvas-orchestrator.js +516 -199
- package/dist/server/canvas/canvas-scheduler.js +99 -0
- package/dist/server/canvas/canvas-store.js +107 -54
- package/dist/server/canvas/canvas-validation.js +131 -0
- package/dist/server/canvas/canvas-workspace.js +284 -0
- package/dist/server/client-id-utils.js +8 -0
- package/dist/server/client-state.js +104 -22
- package/dist/server/communicate-tool.js +76 -0
- package/dist/server/compact-context-tool.js +2 -3
- package/dist/server/compaction-instructions.js +75 -0
- package/dist/server/compaction-markers.js +7 -2
- package/dist/server/compaction-state.js +84 -0
- package/dist/server/composer-drafts.js +43 -40
- package/dist/server/context-evidence.js +346 -0
- package/dist/server/conversation-communication.js +368 -0
- package/dist/server/conversation-read-tool.js +3 -4
- package/dist/server/conversation-tool-names.js +14 -0
- package/dist/server/conversation-tool.js +57 -0
- package/dist/server/delegate-mode.js +17 -4
- package/dist/server/device-center/bot-catalog.js +88 -0
- package/dist/server/device-center/bot-execution.js +106 -0
- package/dist/server/device-center/bots.js +170 -0
- package/dist/server/device-center/collab-service.js +153 -0
- package/dist/server/device-center/collab-tool.js +184 -0
- package/dist/server/device-center/connector.js +332 -0
- package/dist/server/device-center/desktop-linux.js +372 -0
- package/dist/server/device-center/desktop-native.js +413 -0
- package/dist/server/device-center/desktop-page.js +232 -0
- package/dist/server/device-center/desktop-setup.js +30 -0
- package/dist/server/device-center/desktop.js +387 -0
- package/dist/server/device-center/device-portals.js +241 -0
- package/dist/server/device-center/execution-failure.js +70 -0
- package/dist/server/device-center/executor.js +371 -0
- package/dist/server/device-center/file-sync.js +493 -0
- package/dist/server/device-center/hub-auth.js +69 -0
- package/dist/server/device-center/hub-page.js +68 -0
- package/dist/server/device-center/hub-setup.js +214 -0
- package/dist/server/device-center/hub.js +829 -0
- package/dist/server/device-center/local-page.js +17 -0
- package/dist/server/device-center/local.js +476 -0
- package/dist/server/device-center/main.js +118 -0
- package/dist/server/device-center/native-desktop/macos.js +192 -0
- package/dist/server/device-center/native-desktop/windows.js +118 -0
- package/dist/server/device-center/path-portal.js +120 -0
- package/dist/server/device-center/relay.js +959 -0
- package/dist/server/device-center/runtime.js +253 -0
- package/dist/server/device-center/smoke-preview.js +3 -0
- package/dist/server/device-center/store.js +122 -0
- package/dist/server/device-center/task-error.js +14 -0
- package/dist/server/device-center/transfers.js +293 -0
- package/dist/server/device-center/workspaces.js +45 -0
- package/dist/server/dispatch-domain-handlers.js +83 -27
- package/dist/server/dsh/dsh-agent-service.js +277 -158
- package/dist/server/dsh/dsh-client.js +12 -2
- package/dist/server/durable/durable-agent-service.js +1 -1
- package/dist/server/evidence-tool-hooks.js +49 -0
- package/dist/server/file-save.js +95 -0
- package/dist/server/file-transfer-routes.js +97 -47
- package/dist/server/files-service.js +38 -26
- package/dist/server/goal-review-gate.js +16 -6
- package/dist/server/goal-service.js +54 -30
- package/dist/server/http-proxy.js +120 -0
- package/dist/server/index.js +229 -223
- package/dist/server/load-timing.js +31 -0
- package/dist/server/mcp-bridge.js +219 -94
- package/dist/server/mcp-hot-reload.js +31 -5
- package/dist/server/mcp-manager.js +192 -93
- package/dist/server/mcp-oauth-login.js +169 -0
- package/dist/server/mcp-oauth-ui.js +137 -0
- package/dist/server/mcp-oauth.js +395 -0
- package/dist/server/mcp-project-runtime.js +207 -0
- package/dist/server/message-window.js +74 -21
- package/dist/server/net-guard.js +122 -20
- package/dist/server/payload-limits.js +17 -0
- package/dist/server/pending-question.js +82 -0
- package/dist/server/plan-mode.js +338 -137
- package/dist/server/plugin-assets.js +8 -4
- package/dist/server/plugin-chat-attachments.js +48 -0
- package/dist/server/plugin-facilities.js +67 -220
- package/dist/server/plugin-installer.js +2 -1
- package/dist/server/plugin-removal.js +49 -0
- package/dist/server/plugin-secrets.js +179 -0
- package/dist/server/plugins.js +262 -92
- package/dist/server/preset-fields.js +49 -0
- package/dist/server/preset-plugin-dependencies.js +57 -0
- package/dist/server/preset-share.js +112 -72
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/queue-modes.js +10 -0
- package/dist/server/remote-ssh-service.js +160 -67
- package/dist/server/request-context.js +164 -0
- package/dist/server/sandbox-data.js +655 -0
- package/dist/server/sandbox-engines.js +7 -0
- package/dist/server/sandbox-runtime-gate.js +41 -0
- package/dist/server/sandbox-service.js +139 -698
- package/dist/server/sandbox-tool.js +21 -4
- package/dist/server/scheduler-tasks.js +63 -48
- package/dist/server/serialize.js +48 -19
- package/dist/server/session-branch.js +25 -3
- package/dist/server/session-parser.js +28 -18
- package/dist/server/session-scan.js +130 -0
- package/dist/server/session-search.js +51 -37
- package/dist/server/settings-service.js +267 -111
- package/dist/server/setup-config.js +58 -0
- package/dist/server/skill-maintenance.js +112 -24
- package/dist/server/subagent-helpers.js +4 -1
- package/dist/server/subagent-notify.js +4 -1
- package/dist/server/subagent-security-gate.js +52 -11
- package/dist/server/subagent-task.js +27 -0
- package/dist/server/subagents.js +217 -46
- package/dist/server/tabs.js +9 -1
- package/dist/server/terminals.js +467 -363
- package/dist/server/tool-guards.js +42 -6
- package/dist/server/tool-manager.js +47 -20
- package/dist/server/tool-overrides.js +13 -11
- package/dist/server/tool-prompt-overrides.js +32 -2
- package/dist/server/tool-prompts.js +1 -1
- package/dist/server/turn-timing.js +11 -0
- package/dist/server/universal/session-store.js +48 -6
- package/dist/server/universal/universal-service.js +1 -1
- package/dist/server/universal/universal-session.js +334 -231
- package/dist/server/uploads.js +64 -5
- package/dist/server/worktree-service.js +32 -24
- package/dist/server/ws-pending-queue.js +73 -27
- package/docs/device-center.md +142 -0
- package/docs/device-desktop.md +48 -0
- package/package.json +4 -1
- package/plugin-sdk/index.d.ts +28 -6
- package/plugin-sdk/ssh-host-trust.mjs +225 -0
- package/plugins/catalog.json +1 -1
- package/themes/aetheris.css +1 -107
- package/themes/ayu-light.css +5 -29
- package/themes/bases/flow-base.css +0 -6
- package/themes/bases/statusbar-flat-base.css +112 -0
- package/themes/bases/zhujian-base.css +1 -0
- package/themes/bases/zhupi-base.css +1 -0
- package/themes/catppuccin-latte.css +4 -27
- package/themes/catppuccin.css +4 -35
- package/themes/claude-code-dark.css +1 -0
- package/themes/codex.css +4 -28
- package/themes/cyberpunk.css +3 -61
- package/themes/dark-teal.css +0 -1
- package/themes/dazzle.css +3 -73
- package/themes/everforest-light.css +4 -27
- package/themes/geist.css +4 -28
- package/themes/gruvbox-light.css +4 -27
- package/themes/kanagawa-lotus.css +4 -27
- package/themes/md-preview.css +3 -213
- package/themes/mist.css +4 -24
- package/themes/nord.css +4 -35
- package/themes/one-dark.css +4 -37
- package/themes/paper.css +4 -16
- package/themes/rose-pine-dawn.css +4 -28
- package/themes/sakura.css +4 -24
- package/themes/solarized-light.css +4 -27
- package/themes/tokyo-night.css +3 -34
- package/themes/white.css +4 -26
- package/themes/zhujian-dark.css +1 -0
- package/themes/zhupi-dark.css +1 -0
- package/web/dist/.build-assets.json +1 -0
- package/web/dist/assets/BgTasksModal-CS7UNzcw.js +1 -0
- package/web/dist/assets/BgTasksModal-Dk-tp78W.js +1 -0
- package/web/dist/assets/CanvasStudio-BEJZE5BI.js +1579 -0
- package/web/dist/assets/CanvasStudio-CgUNHqos.js +1579 -0
- package/web/dist/assets/DeviceCenter-C8_CbxQ3.js +3 -0
- package/web/dist/assets/DeviceCenter-CVblvhNX.js +3 -0
- package/web/dist/assets/Dropdown-Oug0DDte.js +1 -0
- package/web/dist/assets/FilePreview-CvdX6nr6.js +2 -0
- package/web/dist/assets/FilePreview-kCZhfBAF.js +2 -0
- package/web/dist/assets/GlobalSearchModal-BLH63yYP.js +1 -0
- package/web/dist/assets/GlobalSearchModal-DAkvlvp6.js +1 -0
- package/web/dist/assets/GroupConversation-Cgo4xbpa.js +1 -0
- package/web/dist/assets/GroupConversation-DZ71jEi8.js +1 -0
- package/web/dist/assets/ModelConfigModal-BpWdFdQC.js +2 -0
- package/web/dist/assets/PiSetupModal-mkWPSxMj.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-VK_jqXYS.js +1 -0
- package/web/dist/assets/SCMPanel-BP1TUT-V.js +2 -0
- package/web/dist/assets/SCMPanel-BscPm9kf.js +2 -0
- package/web/dist/assets/SettingsModal-C8UgMjVM.js +20 -0
- package/web/dist/assets/SettingsModal-VynB7jNb.js +20 -0
- package/web/dist/assets/TerminalPanel-D6cPRFei.js +3 -0
- package/web/dist/assets/TerminalPanel-DL20uKgU.js +3 -0
- package/web/dist/assets/TuiOverlayModal-Dst3BmT8.js +1 -0
- package/web/dist/assets/TuiOverlayModal-NCkpNtTp.js +1 -0
- package/web/dist/assets/UniversalPathPicker-CfQFc_c3.js +1 -0
- package/web/dist/assets/UniversalPathPicker-DwxwNcFw.js +1 -0
- package/web/dist/assets/app-globals-CnO6KlEM.js +325 -0
- package/web/dist/assets/desktop-DPyTy4M3.js +1 -0
- package/web/dist/assets/es-DWMIuDU2.js +2 -0
- package/web/dist/assets/index-CPgAH5q7.js +83 -0
- package/web/dist/assets/index-Cje8iugL.js +83 -0
- package/web/dist/assets/index-DnKeiDbj.css +1 -0
- package/web/dist/assets/markdown-BggnuQ4H.js +40 -0
- package/web/dist/assets/markdown-C8LL_u4z.css +1 -0
- package/web/dist/assets/math-1uRJCsA9.js +257 -0
- package/web/dist/assets/math-BorAY7qD.css +1 -0
- package/web/dist/assets/plugin-host-CqA4yW6w.js +1 -0
- package/web/dist/assets/shortcut-stack-hhrDyRBn.js +1 -0
- package/web/dist/assets/theme-BqdAjEjF.js +1 -0
- package/web/dist/assets/uuid-BzDIGCwP.js +1 -0
- package/web/dist/index.html +15 -9
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/TerminalPanel-Ci55JtJL.js +0 -3
- package/web/dist/assets/index-ByiO9f-N.js +0 -2008
- package/web/dist/assets/index-ycGLh4a4.css +0 -1
- package/web/dist/assets/markdown-BJZlfvdP.js +0 -296
- package/web/dist/assets/markdown-DSNUiDzp.css +0 -1
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth 保护的远端 MCP 服务器的凭据(Issue #786)。
|
|
3
|
+
*
|
|
4
|
+
* pi 的内置 MCP 集成会把登录结果存进 `<agent-dir>/mcp-auth.json`,键为 `mcp__<服务器名>|<url>`
|
|
5
|
+
* (旧版本只写 URL 做键;那种状态这里同样接管)。桥接层与网页登录共用这份凭据。
|
|
6
|
+
*
|
|
7
|
+
* 本模块**复用 pi 的凭据库**,而不是再建一份:
|
|
8
|
+
* - 读出存好的 access token,随请求发 `Authorization: Bearer …`;
|
|
9
|
+
* - 临近过期或收到 401 时,用存好的 refresh token 换新的;
|
|
10
|
+
* - 刷新结果按 pi 的格式写回,并复用 pi 的刷新锁(两个进程不会同时刷新)。
|
|
11
|
+
*
|
|
12
|
+
* 网页登录(授权码 + PKCE + 浏览器回调)由 mcp-oauth-login.ts 处理;CLI 登录仍然兼容。
|
|
13
|
+
*/
|
|
14
|
+
import { createHash } from "node:crypto";
|
|
15
|
+
import { mkdirSync, readFileSync, rmSync, statSync, utimesSync } from "node:fs";
|
|
16
|
+
import { homedir } from "node:os";
|
|
17
|
+
import { dirname, join } from "node:path";
|
|
18
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
19
|
+
import { isDeepStrictEqual } from "node:util";
|
|
20
|
+
import { writeAtomicSync } from "./atomic-file.js";
|
|
21
|
+
/** 离过期这么近的 access token 先刷新再发出去(与 pi 同口径)。 */
|
|
22
|
+
export const MCP_REFRESH_SKEW_MS = 30_000;
|
|
23
|
+
/** 刷新锁被持有超过这个时间就视为遗弃(持锁进程被杀),后来者接管。 */
|
|
24
|
+
const REFRESH_LOCK_STALE_MS = 20_000;
|
|
25
|
+
/** 等别的进程刷新的最长时间;比一个过期锁的寿命更长。 */
|
|
26
|
+
const REFRESH_LOCK_WAIT_MS = 25_000;
|
|
27
|
+
const REFRESH_LOCK_RETRY_MS = 100;
|
|
28
|
+
/** 单次授权服务器请求的上限:不响应的服务器不能一直占着锁。 */
|
|
29
|
+
const REFRESH_REQUEST_TIMEOUT_MS = 20_000;
|
|
30
|
+
/** 凭据库锁只在「读-合并-写」这一小段持有;等别的进程的最长时间远小于刷新锁。 */
|
|
31
|
+
const STORE_LOCK_WAIT_MS = 2_000;
|
|
32
|
+
/** pi 的 proper-lockfile 默认把超过 10 s 没续期的锁视为遗弃,这里保持一致。 */
|
|
33
|
+
const STORE_LOCK_STALE_MS = 10_000;
|
|
34
|
+
const STORE_LOCK_RETRY_MS = 20;
|
|
35
|
+
/** 该 OAuth 服务器需要重新登录(没有可用的刷新令牌,或刷新被拒绝)。 */
|
|
36
|
+
export class McpOAuthSignInRequiredError extends Error {
|
|
37
|
+
constructor(message) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "McpOAuthSignInRequiredError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** 缺省 agent 目录,与 `getGlobalMcpPath` 的解析顺序一致。 */
|
|
43
|
+
export function defaultMcpAgentDir() {
|
|
44
|
+
return process.env.PI_CODING_AGENT_DIR || join(homedir(), ".pi", "agent");
|
|
45
|
+
}
|
|
46
|
+
/** pi 的 MCP 凭据库绝对路径。 */
|
|
47
|
+
export function getMcpAuthStorePath(options = {}) {
|
|
48
|
+
if (options.storePath)
|
|
49
|
+
return options.storePath;
|
|
50
|
+
return join(options.agentDir || defaultMcpAgentDir(), "mcp-auth.json");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 凭据库键里用的 URL。pi 走 `new URL()` 归一化:`…/mcp` 与 `…/mcp/` 仍是不同的键,但
|
|
54
|
+
* origin 上缺的斜杠会补成 `/`。
|
|
55
|
+
*/
|
|
56
|
+
function normalizeServerUrl(serverUrl) {
|
|
57
|
+
try {
|
|
58
|
+
return String(new URL(serverUrl));
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return serverUrl;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** pi 从服务器名推导出的命名空间:连字符换成下划线(`mcp__my_server`)。 */
|
|
65
|
+
function mcpNamespace(name) {
|
|
66
|
+
return `mcp__${name.replace(/-/g, "_")}`;
|
|
67
|
+
}
|
|
68
|
+
/** 一个服务器的存储键:名字 + URL,同一 URL 上的两个账号各存各的。 */
|
|
69
|
+
export function mcpAuthStoreKey(name, serverUrl) {
|
|
70
|
+
return `${mcpNamespace(name)}|${normalizeServerUrl(serverUrl)}`;
|
|
71
|
+
}
|
|
72
|
+
/** 旧版本 pi 写的键(只有 URL);照样读,下一次写入时被替换。 */
|
|
73
|
+
export function mcpAuthLegacyKey(serverUrl) {
|
|
74
|
+
return normalizeServerUrl(serverUrl);
|
|
75
|
+
}
|
|
76
|
+
/** 读整个凭据库;文件缺失或读不出来就当空库,绝不抛错。 */
|
|
77
|
+
export function readMcpAuthStore(options = {}) {
|
|
78
|
+
try {
|
|
79
|
+
const parsed = JSON.parse(readFileSync(getMcpAuthStorePath(options), "utf8"));
|
|
80
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
|
|
81
|
+
return parsed;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
/* missing or corrupt: behave like pi, an unauthenticated server */
|
|
85
|
+
}
|
|
86
|
+
return {};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 按 pi 的格式写回(2 空格 JSON + 换行,仅本人可读写)—— pi 与本模块共用这一个文件。
|
|
90
|
+
* 新建文件必须显式给 0600:默认的 0666 & umask 在常见 umask 下会写出 0644,凭据就变成可读了。
|
|
91
|
+
*/
|
|
92
|
+
export function writeMcpAuthStore(store, options = {}) {
|
|
93
|
+
writeAtomicSync(getMcpAuthStorePath(options), `${JSON.stringify(store, null, 2)}\n`, { mode: 0o600 });
|
|
94
|
+
}
|
|
95
|
+
/** 单个服务器已存的状态:优先新键,回落旧版 URL 键。 */
|
|
96
|
+
export function loadMcpAuthState(options) {
|
|
97
|
+
const key = mcpAuthStoreKey(options.name, options.serverUrl);
|
|
98
|
+
const legacyKey = mcpAuthLegacyKey(options.serverUrl);
|
|
99
|
+
const store = readMcpAuthStore(options);
|
|
100
|
+
return { key, legacyKey, state: store[key] ?? store[legacyKey] };
|
|
101
|
+
}
|
|
102
|
+
function isExpired(state, nowMs) {
|
|
103
|
+
const expireAt = state.tokensExpireAt;
|
|
104
|
+
if (typeof expireAt !== "number")
|
|
105
|
+
return false;
|
|
106
|
+
return expireAt - MCP_REFRESH_SKEW_MS <= nowMs;
|
|
107
|
+
}
|
|
108
|
+
function lockPathFor(storePath, key) {
|
|
109
|
+
const hash = createHash("sha256").update(key).digest("hex").slice(0, 16);
|
|
110
|
+
return `${storePath.replace(/mcp-auth\.json$/, "")}mcp-auth-refresh-${hash}.lock`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 用凭据库旁边的锁目录把跨进程刷新串行化 —— 名字与过期窗口都与 pi 的存储一致,因此 pi 与
|
|
114
|
+
* pi-web-ui 的刷新不会同时发生。
|
|
115
|
+
*/
|
|
116
|
+
async function withRefreshLock(storePath, key, fn, signal) {
|
|
117
|
+
const lockPath = lockPathFor(storePath, key);
|
|
118
|
+
mkdirSync(join(lockPath, ".."), { recursive: true, mode: 0o700 });
|
|
119
|
+
const deadline = Date.now() + REFRESH_LOCK_WAIT_MS;
|
|
120
|
+
for (;;) {
|
|
121
|
+
signal?.throwIfAborted();
|
|
122
|
+
try {
|
|
123
|
+
mkdirSync(lockPath);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
if (err.code !== "EEXIST")
|
|
128
|
+
throw err;
|
|
129
|
+
try {
|
|
130
|
+
if (Date.now() - statSync(lockPath).mtimeMs > REFRESH_LOCK_STALE_MS) {
|
|
131
|
+
rmSync(lockPath, { recursive: true, force: true });
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
continue; // the holder released it in the meantime
|
|
137
|
+
}
|
|
138
|
+
if (Date.now() > deadline)
|
|
139
|
+
throw new Error("Timed out waiting for the MCP refresh lock (another process is refreshing)");
|
|
140
|
+
await sleep(REFRESH_LOCK_RETRY_MS, undefined, { signal });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// 刷新期间持续续期锁,慢的授权服务器不会被别人当成遗弃锁接管。
|
|
144
|
+
const renewal = setInterval(() => {
|
|
145
|
+
try {
|
|
146
|
+
const now = new Date();
|
|
147
|
+
utimesSync(lockPath, now, now);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
/* released under us: nothing to renew */
|
|
151
|
+
}
|
|
152
|
+
}, Math.max(1_000, Math.floor(REFRESH_LOCK_STALE_MS / 4)));
|
|
153
|
+
renewal.unref?.();
|
|
154
|
+
try {
|
|
155
|
+
signal?.throwIfAborted();
|
|
156
|
+
return await fn();
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
clearInterval(renewal);
|
|
160
|
+
rmSync(lockPath, { recursive: true, force: true });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* 用 pi 自己的文件锁(`mcp-auth.json.lock`)保护整份凭据库的读-改-写。
|
|
165
|
+
*
|
|
166
|
+
* pi 的 `FileAuthStorageBackend.withLock` 对**每次**读写都用 proper-lockfile 锁住这个路径,
|
|
167
|
+
* 所以我们也用同名目录锁,两边互斥。两条约束:
|
|
168
|
+
* - 锁只覆盖读、合并、写这一小段,**绝不跨网络请求** —— pi 抢锁只重试 10 × 20 ms,
|
|
169
|
+
* 超时就抛错,会把 pi 自己的 MCP 调用拖挂;
|
|
170
|
+
* - mtime 超过 10 s 没续期就按遗弃处理(与 proper-lockfile 的默认 stale 一致),
|
|
171
|
+
* 崩溃留下的空锁目录不会永久堵住 pi。
|
|
172
|
+
*/
|
|
173
|
+
export async function withMcpAuthStoreLock(storePath, fn, signal) {
|
|
174
|
+
const lockPath = `${storePath}.lock`;
|
|
175
|
+
mkdirSync(dirname(storePath), { recursive: true, mode: 0o700 });
|
|
176
|
+
const deadline = Date.now() + STORE_LOCK_WAIT_MS;
|
|
177
|
+
for (;;) {
|
|
178
|
+
signal?.throwIfAborted();
|
|
179
|
+
try {
|
|
180
|
+
mkdirSync(lockPath, { mode: 0o700 });
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
catch (err) {
|
|
184
|
+
if (err.code !== "EEXIST")
|
|
185
|
+
throw err;
|
|
186
|
+
try {
|
|
187
|
+
if (Date.now() - statSync(lockPath).mtimeMs > STORE_LOCK_STALE_MS) {
|
|
188
|
+
rmSync(lockPath, { recursive: true, force: true });
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
continue; // the holder released it in the meantime
|
|
194
|
+
}
|
|
195
|
+
if (Date.now() > deadline)
|
|
196
|
+
throw new Error(`Timed out waiting for the MCP credential store lock (${lockPath})`);
|
|
197
|
+
await sleep(STORE_LOCK_RETRY_MS, undefined, { signal });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
signal?.throwIfAborted();
|
|
202
|
+
return fn();
|
|
203
|
+
}
|
|
204
|
+
finally {
|
|
205
|
+
rmSync(lockPath, { recursive: true, force: true });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function parseTokenResponse(value, fallbackRefreshToken) {
|
|
209
|
+
if (!value || typeof value !== "object") {
|
|
210
|
+
throw new McpOAuthSignInRequiredError("The authorization server answered the refresh with something that is not a JSON object");
|
|
211
|
+
}
|
|
212
|
+
const raw = value;
|
|
213
|
+
if (typeof raw.error === "string") {
|
|
214
|
+
const description = typeof raw.error_description === "string" ? raw.error_description : String(raw.error);
|
|
215
|
+
throw new McpOAuthSignInRequiredError(`The authorization server rejected the refresh token: ${description}`);
|
|
216
|
+
}
|
|
217
|
+
if (typeof raw.access_token !== "string" || !raw.access_token) {
|
|
218
|
+
throw new McpOAuthSignInRequiredError("The authorization server answered the refresh without an access_token");
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
access_token: raw.access_token,
|
|
222
|
+
// Servers that rotate refresh tokens send a new one; otherwise the grant keeps the old.
|
|
223
|
+
refresh_token: typeof raw.refresh_token === "string" && raw.refresh_token ? raw.refresh_token : fallbackRefreshToken,
|
|
224
|
+
token_type: typeof raw.token_type === "string" ? raw.token_type : undefined,
|
|
225
|
+
expires_in: typeof raw.expires_in === "number" ? raw.expires_in : undefined,
|
|
226
|
+
scope: typeof raw.scope === "string" ? raw.scope : undefined,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function selectClientAuthMethod(state) {
|
|
230
|
+
const information = state.clientInformation ?? {};
|
|
231
|
+
const hinted = information.token_endpoint_auth_method;
|
|
232
|
+
if (hinted === "client_secret_basic" || hinted === "client_secret_post" || hinted === "none")
|
|
233
|
+
return hinted;
|
|
234
|
+
return information.client_secret ? "client_secret_basic" : "none";
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* 用存好的 refresh token 换新的 access token(RFC 6749 §6),客户端认证方式跟随注册时的选择。
|
|
238
|
+
*/
|
|
239
|
+
async function requestRefreshToken(state, options) {
|
|
240
|
+
const tokenEndpoint = state.discovery?.authorizationServerMetadata?.token_endpoint;
|
|
241
|
+
const clientId = state.clientInformation?.client_id;
|
|
242
|
+
const refreshToken = state.tokens?.refresh_token;
|
|
243
|
+
if (!tokenEndpoint) {
|
|
244
|
+
throw new McpOAuthSignInRequiredError("The stored credentials have no token endpoint; sign in again");
|
|
245
|
+
}
|
|
246
|
+
if (!clientId) {
|
|
247
|
+
throw new McpOAuthSignInRequiredError("The stored credentials have no client_id; sign in again");
|
|
248
|
+
}
|
|
249
|
+
if (!refreshToken) {
|
|
250
|
+
throw new McpOAuthSignInRequiredError("The stored credentials have no refresh token; sign in again");
|
|
251
|
+
}
|
|
252
|
+
const params = new URLSearchParams({ grant_type: "refresh_token", refresh_token: refreshToken });
|
|
253
|
+
const resource = state.discovery?.resourceMetadata?.resource ?? options.serverUrl;
|
|
254
|
+
if (resource)
|
|
255
|
+
params.set("resource", resource);
|
|
256
|
+
const headers = {
|
|
257
|
+
Accept: "application/json",
|
|
258
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
259
|
+
};
|
|
260
|
+
const method = selectClientAuthMethod(state);
|
|
261
|
+
const secret = state.clientInformation?.client_secret;
|
|
262
|
+
if (method === "client_secret_basic" && secret) {
|
|
263
|
+
headers.Authorization = `Basic ${Buffer.from(`${clientId}:${secret}`).toString("base64")}`;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
params.set("client_id", clientId);
|
|
267
|
+
if (method === "client_secret_post" && secret)
|
|
268
|
+
params.set("client_secret", secret);
|
|
269
|
+
}
|
|
270
|
+
const doFetch = options.fetch ?? fetch;
|
|
271
|
+
const timeout = AbortSignal.timeout(REFRESH_REQUEST_TIMEOUT_MS);
|
|
272
|
+
const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
|
|
273
|
+
signal.throwIfAborted();
|
|
274
|
+
let response;
|
|
275
|
+
try {
|
|
276
|
+
response = await doFetch(tokenEndpoint, { method: "POST", headers, body: params.toString(), signal });
|
|
277
|
+
}
|
|
278
|
+
catch (err) {
|
|
279
|
+
signal.throwIfAborted();
|
|
280
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
281
|
+
throw new Error(`MCP token refresh request failed: ${detail}`);
|
|
282
|
+
}
|
|
283
|
+
const text = await response.text().catch((err) => {
|
|
284
|
+
signal.throwIfAborted();
|
|
285
|
+
throw err;
|
|
286
|
+
});
|
|
287
|
+
signal.throwIfAborted();
|
|
288
|
+
let parsed;
|
|
289
|
+
try {
|
|
290
|
+
parsed = JSON.parse(text);
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
parsed = undefined;
|
|
294
|
+
}
|
|
295
|
+
// OAuth servers may report errors with any status, so the body wins over the status code.
|
|
296
|
+
if (parsed && typeof parsed === "object" && typeof parsed.error === "string") {
|
|
297
|
+
return parseTokenResponse(parsed, refreshToken);
|
|
298
|
+
}
|
|
299
|
+
if (!response.ok) {
|
|
300
|
+
throw new McpOAuthSignInRequiredError(`MCP token refresh failed (HTTP ${response.status}): ${text.slice(0, 200)}`);
|
|
301
|
+
}
|
|
302
|
+
return parseTokenResponse(parsed, refreshToken);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* 强制刷新(401 之后,或 token 即将过期)并把结果落盘。返回新的 access token。
|
|
306
|
+
* 带 `staleToken` 时,若库里已被别的进程换掉就直接用那个 token,不再刷第二次 —— 刷新令牌轮换后
|
|
307
|
+
* 第二次尝试会失败。
|
|
308
|
+
*/
|
|
309
|
+
export async function refreshMcpAccessToken(options) {
|
|
310
|
+
const storePath = getMcpAuthStorePath(options);
|
|
311
|
+
const key = mcpAuthStoreKey(options.name, options.serverUrl);
|
|
312
|
+
const { signal } = options;
|
|
313
|
+
return withRefreshLock(storePath, key, async () => {
|
|
314
|
+
const legacyKey = mcpAuthLegacyKey(options.serverUrl);
|
|
315
|
+
const { state, sourceKey } = await withMcpAuthStoreLock(storePath, () => {
|
|
316
|
+
const store = readMcpAuthStore(options);
|
|
317
|
+
const sourceKey = store[key] ? key : legacyKey;
|
|
318
|
+
return { state: store[sourceKey], sourceKey };
|
|
319
|
+
}, signal);
|
|
320
|
+
if (!state?.tokens?.access_token && !state?.tokens?.refresh_token) {
|
|
321
|
+
throw new McpOAuthSignInRequiredError(`MCP server "${options.name}" needs an OAuth sign-in (no stored credentials found)`);
|
|
322
|
+
}
|
|
323
|
+
const storedToken = state?.tokens?.access_token;
|
|
324
|
+
if (options.staleToken && storedToken && storedToken !== options.staleToken) {
|
|
325
|
+
return storedToken; // refreshed by another process while we waited for the lock
|
|
326
|
+
}
|
|
327
|
+
if (!state)
|
|
328
|
+
return null;
|
|
329
|
+
// 网络期间仅持该服务器的刷新锁,不持整库锁;pi 仍可登录、注销或写入其它服务器。
|
|
330
|
+
const tokens = await requestRefreshToken(state, options);
|
|
331
|
+
const nowMs = (options.now ?? Date.now)();
|
|
332
|
+
const expiresAt = typeof tokens.expires_in === "number" ? nowMs + tokens.expires_in * 1000 : undefined;
|
|
333
|
+
return withMcpAuthStoreLock(storePath, () => {
|
|
334
|
+
// Compare the grant read before the network call under the SDK store lock.
|
|
335
|
+
// Logout/re-login/client changes win; never restore a missing entry.
|
|
336
|
+
signal?.throwIfAborted();
|
|
337
|
+
const fresh = readMcpAuthStore(options);
|
|
338
|
+
const currentKey = fresh[key] ? key : legacyKey;
|
|
339
|
+
const base = fresh[currentKey];
|
|
340
|
+
if (currentKey !== sourceKey || !base || !sameCredentials(base, state)) {
|
|
341
|
+
return base?.tokens?.access_token ?? null;
|
|
342
|
+
}
|
|
343
|
+
const next = {
|
|
344
|
+
...base,
|
|
345
|
+
tokens: { ...base.tokens, ...trimUndefined(tokens) },
|
|
346
|
+
};
|
|
347
|
+
// 授权服务器没给 `expires_in` 时必须删掉旧的绝对过期时间:否则它一直是「已过期」,
|
|
348
|
+
// 每个请求都会再走一次刷新(pi 的 provider 也是这么处理的)。
|
|
349
|
+
if (expiresAt === undefined)
|
|
350
|
+
delete next.tokensExpireAt;
|
|
351
|
+
else
|
|
352
|
+
next.tokensExpireAt = expiresAt;
|
|
353
|
+
fresh[key] = next;
|
|
354
|
+
if (sourceKey === legacyKey && legacyKey !== key)
|
|
355
|
+
delete fresh[legacyKey];
|
|
356
|
+
signal?.throwIfAborted();
|
|
357
|
+
writeMcpAuthStore(fresh, options);
|
|
358
|
+
return tokens.access_token;
|
|
359
|
+
}, signal);
|
|
360
|
+
}, signal);
|
|
361
|
+
}
|
|
362
|
+
function sameCredentials(a, b) {
|
|
363
|
+
return isDeepStrictEqual([a.tokens, a.tokensExpireAt, a.clientInformation, a.discovery], [b.tokens, b.tokensExpireAt, b.clientInformation, b.discovery]);
|
|
364
|
+
}
|
|
365
|
+
function trimUndefined(tokens) {
|
|
366
|
+
const out = {};
|
|
367
|
+
for (const [k, v] of Object.entries(tokens)) {
|
|
368
|
+
if (v !== undefined)
|
|
369
|
+
out[k] = v;
|
|
370
|
+
}
|
|
371
|
+
return out;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* 该发的 access token;这个服务器没有凭据(匿名服务器)时返回 `null`。
|
|
375
|
+
* 即将过期的会先刷新;刷新失败则返回旧 token,交给服务器自己的 401 定论。
|
|
376
|
+
*/
|
|
377
|
+
export async function getMcpAccessToken(options) {
|
|
378
|
+
options.signal?.throwIfAborted();
|
|
379
|
+
const { state } = loadMcpAuthState(options);
|
|
380
|
+
const token = state?.tokens?.access_token;
|
|
381
|
+
if (!state || !token)
|
|
382
|
+
return null;
|
|
383
|
+
if (!isExpired(state, (options.now ?? Date.now)()))
|
|
384
|
+
return token;
|
|
385
|
+
if (!state.tokens?.refresh_token)
|
|
386
|
+
return token;
|
|
387
|
+
try {
|
|
388
|
+
return await refreshMcpAccessToken({ ...options, staleToken: token });
|
|
389
|
+
}
|
|
390
|
+
catch {
|
|
391
|
+
options.signal?.throwIfAborted();
|
|
392
|
+
// A failed refresh may race logout/re-login too. Never reuse the captured grant.
|
|
393
|
+
return loadMcpAuthState(options).state?.tokens?.access_token ?? null;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { McpBridge } from "./mcp-bridge.js";
|
|
3
|
+
import { createMcpHotReload } from "./mcp-hot-reload.js";
|
|
4
|
+
/** Bridges never change cwd. Only unreferenced, idle entries may be automatically evicted. */
|
|
5
|
+
export class McpProjectRuntimeRegistry {
|
|
6
|
+
options;
|
|
7
|
+
projects = new Map();
|
|
8
|
+
pendingDisposals = new Set();
|
|
9
|
+
closed = false;
|
|
10
|
+
idleTimer;
|
|
11
|
+
publishing = false;
|
|
12
|
+
publish(cwd) {
|
|
13
|
+
const previous = this.publishing;
|
|
14
|
+
this.publishing = true;
|
|
15
|
+
try {
|
|
16
|
+
this.options.onToolsChanged?.(cwd);
|
|
17
|
+
}
|
|
18
|
+
finally {
|
|
19
|
+
this.publishing = previous;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.idleTimer = setInterval(() => {
|
|
25
|
+
void this.collectIdle().catch((err) => this.options.log?.("[mcp] idle cleanup failed", err));
|
|
26
|
+
}, options.idleCheckIntervalMs ?? 60_000);
|
|
27
|
+
this.idleTimer.unref();
|
|
28
|
+
}
|
|
29
|
+
now() {
|
|
30
|
+
return this.options.now?.() ?? Date.now();
|
|
31
|
+
}
|
|
32
|
+
/** Operations retain entries across awaits even before a conversation is published. */
|
|
33
|
+
retain(cwd) {
|
|
34
|
+
const entry = this.entry(cwd);
|
|
35
|
+
entry.busy++;
|
|
36
|
+
let released = false;
|
|
37
|
+
return () => {
|
|
38
|
+
if (released)
|
|
39
|
+
return;
|
|
40
|
+
released = true;
|
|
41
|
+
entry.busy--;
|
|
42
|
+
entry.lastAccess = this.now();
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async collectIdle() {
|
|
46
|
+
if (this.closed || !this.options.isProjectInUse)
|
|
47
|
+
return;
|
|
48
|
+
const cutoff = this.now() - (this.options.idleTimeoutMs ?? 300_000);
|
|
49
|
+
const candidates = [...this.projects.values()].sort((a, b) => a.lastAccess - b.lastAccess);
|
|
50
|
+
for (const entry of candidates) {
|
|
51
|
+
if (this.closed)
|
|
52
|
+
return;
|
|
53
|
+
if (this.projects.get(this.key(entry.cwd)) !== entry || entry.busy || entry.lastAccess > cutoff)
|
|
54
|
+
continue;
|
|
55
|
+
let inUse = true;
|
|
56
|
+
try {
|
|
57
|
+
inUse = this.options.isProjectInUse(entry.cwd);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
this.options.log?.("[mcp] ownership check failed; retaining project", entry.cwd, err);
|
|
61
|
+
}
|
|
62
|
+
if (!inUse)
|
|
63
|
+
await this.disposeProject(entry.cwd);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Read an already active bridge without creating a runtime or starting processes. */
|
|
67
|
+
peekBridge(cwd) {
|
|
68
|
+
return this.projects.get(this.key(cwd))?.bridge;
|
|
69
|
+
}
|
|
70
|
+
key(cwd) {
|
|
71
|
+
const path = resolve(cwd);
|
|
72
|
+
return process.platform === "win32" ? path.toLowerCase() : path;
|
|
73
|
+
}
|
|
74
|
+
entry(cwd) {
|
|
75
|
+
if (this.closed)
|
|
76
|
+
throw new Error("MCP project runtimes are closed");
|
|
77
|
+
const key = this.key(cwd);
|
|
78
|
+
const existing = this.projects.get(key);
|
|
79
|
+
if (existing) {
|
|
80
|
+
if (!this.publishing)
|
|
81
|
+
existing.lastAccess = this.now();
|
|
82
|
+
return existing;
|
|
83
|
+
}
|
|
84
|
+
const projectCwd = resolve(cwd);
|
|
85
|
+
let entry;
|
|
86
|
+
const current = () => !this.closed && !entry.closed && this.projects.get(key) === entry;
|
|
87
|
+
const changed = () => {
|
|
88
|
+
if (current())
|
|
89
|
+
this.publish(projectCwd);
|
|
90
|
+
};
|
|
91
|
+
const bridge = (this.options.createBridge?.(projectCwd, changed) ??
|
|
92
|
+
new McpBridge(this.options.dataDir, this.options.log, {
|
|
93
|
+
agentDir: this.options.agentDir,
|
|
94
|
+
cwd: projectCwd,
|
|
95
|
+
onToolsChanged: changed,
|
|
96
|
+
}));
|
|
97
|
+
const watcher = createMcpHotReload({
|
|
98
|
+
dataDir: this.options.dataDir,
|
|
99
|
+
agentDir: this.options.agentDir,
|
|
100
|
+
cwd: projectCwd,
|
|
101
|
+
reload: () => this.enqueue(entry),
|
|
102
|
+
onNotice: (level, text, textEn) => {
|
|
103
|
+
if (current())
|
|
104
|
+
this.options.onNotice?.(projectCwd, level, text, textEn);
|
|
105
|
+
},
|
|
106
|
+
log: this.options.log,
|
|
107
|
+
});
|
|
108
|
+
entry = {
|
|
109
|
+
cwd: projectCwd,
|
|
110
|
+
bridge,
|
|
111
|
+
watcher,
|
|
112
|
+
ready: Promise.resolve(),
|
|
113
|
+
tail: Promise.resolve(),
|
|
114
|
+
closed: false,
|
|
115
|
+
lastAccess: this.now(),
|
|
116
|
+
busy: 1,
|
|
117
|
+
};
|
|
118
|
+
this.projects.set(key, entry);
|
|
119
|
+
// Seed before load; apply afterwards catches edits made while servers were starting.
|
|
120
|
+
watcher.start();
|
|
121
|
+
entry.ready = Promise.resolve().then(async () => {
|
|
122
|
+
if (!current())
|
|
123
|
+
return;
|
|
124
|
+
try {
|
|
125
|
+
await bridge.load();
|
|
126
|
+
if (current())
|
|
127
|
+
changed();
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
entry.busy--;
|
|
131
|
+
if (!current())
|
|
132
|
+
bridge.dispose();
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
entry.tail = entry.ready;
|
|
136
|
+
void entry.ready
|
|
137
|
+
.then(() => (current() ? watcher.apply() : undefined))
|
|
138
|
+
.catch((err) => this.options.log?.("[mcp] project initialization failed", projectCwd, err));
|
|
139
|
+
return entry;
|
|
140
|
+
}
|
|
141
|
+
getBridge(cwd) {
|
|
142
|
+
return this.entry(cwd).bridge;
|
|
143
|
+
}
|
|
144
|
+
async prepare(cwd) {
|
|
145
|
+
const entry = this.entry(cwd);
|
|
146
|
+
await entry.ready;
|
|
147
|
+
if (this.closed || entry.closed)
|
|
148
|
+
throw new Error("MCP project runtime was disposed during preparation");
|
|
149
|
+
return entry.bridge;
|
|
150
|
+
}
|
|
151
|
+
getTools(cwd) {
|
|
152
|
+
if (this.closed)
|
|
153
|
+
return [];
|
|
154
|
+
return this.entry(cwd).bridge.getTools();
|
|
155
|
+
}
|
|
156
|
+
enqueue(entry) {
|
|
157
|
+
entry.busy++;
|
|
158
|
+
const run = entry.tail.then(async () => {
|
|
159
|
+
if (this.closed || entry.closed)
|
|
160
|
+
throw new Error("MCP project runtime is closed");
|
|
161
|
+
try {
|
|
162
|
+
const summary = await entry.bridge.reload();
|
|
163
|
+
if (!this.closed && !entry.closed)
|
|
164
|
+
this.publish(entry.cwd);
|
|
165
|
+
return summary;
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
if (this.closed || entry.closed)
|
|
169
|
+
entry.bridge.dispose();
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
const tracked = run.finally(() => {
|
|
173
|
+
entry.busy--;
|
|
174
|
+
});
|
|
175
|
+
entry.tail = tracked.catch(() => { });
|
|
176
|
+
return tracked;
|
|
177
|
+
}
|
|
178
|
+
/** Global configuration is shared on disk, while connections remain project-owned. */
|
|
179
|
+
async reload(cwd, scope = "project") {
|
|
180
|
+
const entry = this.entry(cwd);
|
|
181
|
+
await Promise.all((scope === "global" ? [...this.projects.values()] : [entry]).map((p) => this.enqueue(p)));
|
|
182
|
+
}
|
|
183
|
+
async disposeProject(cwd) {
|
|
184
|
+
const key = this.key(cwd);
|
|
185
|
+
const entry = this.projects.get(key);
|
|
186
|
+
if (!entry)
|
|
187
|
+
return;
|
|
188
|
+
this.projects.delete(key);
|
|
189
|
+
entry.closed = true;
|
|
190
|
+
entry.watcher.dispose();
|
|
191
|
+
entry.bridge.dispose();
|
|
192
|
+
const cleanup = entry.tail.catch(() => { }).then(() => entry.bridge.dispose());
|
|
193
|
+
this.pendingDisposals.add(cleanup);
|
|
194
|
+
try {
|
|
195
|
+
await cleanup;
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
this.pendingDisposals.delete(cleanup);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
async dispose() {
|
|
202
|
+
this.closed = true;
|
|
203
|
+
clearInterval(this.idleTimer);
|
|
204
|
+
await Promise.all([...this.projects.keys()].map((cwd) => this.disposeProject(cwd)));
|
|
205
|
+
await Promise.all(this.pendingDisposals);
|
|
206
|
+
}
|
|
207
|
+
}
|