pi-web-ui 0.113.1 → 0.114.1
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 +71 -1
- package/README.md +187 -316
- package/README.zh-CN.md +227 -299
- package/README.zh-TW.md +227 -299
- package/bin/device-hub-cli.mjs +577 -0
- package/bin/frp-manager.mjs +1573 -1573
- package/bin/pi-device-hub.mjs +9 -0
- package/bin/pi-web-ui.mjs +4081 -4041
- package/deploy/nginx-subpath.conf +100 -100
- package/deploy/pi-web-ui.service +36 -36
- package/dist/server/agent-service-daemon.js +102 -24
- package/dist/server/agent-service.js +2419 -1174
- 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 +69 -13
- package/dist/server/canvas/canvas-agent-tool.js +68 -18
- package/dist/server/canvas/canvas-cards.js +1 -1
- package/dist/server/canvas/canvas-dispatch.js +61 -20
- package/dist/server/canvas/canvas-orchestrator.js +519 -202
- package/dist/server/canvas/canvas-scheduled-task.js +45 -0
- 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 +110 -24
- 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 +351 -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 +830 -0
- package/dist/server/device-center/local-hub.js +92 -0
- package/dist/server/device-center/local.js +529 -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 +996 -0
- package/dist/server/device-center/runtime.js +253 -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 +84 -27
- package/dist/server/dsh/dsh-agent-service.js +282 -159
- package/dist/server/dsh/dsh-client.js +12 -2
- package/dist/server/dsh/runtime/cordis.yml +1 -1
- package/dist/server/dsh/runtime/custom-prompt.mjs +33 -33
- package/dist/server/dsh/runtime/goal-rpc.mjs +1309 -1309
- package/dist/server/dsh/runtime/launcher.mjs +235 -235
- package/dist/server/dsh/runtime/override.patch.yml +89 -89
- package/dist/server/dsh/runtime/runtime-root.mjs +405 -405
- package/dist/server/durable/durable-agent-service.js +1 -1
- package/dist/server/engine-probe.js +36 -6
- package/dist/server/eval-tool.js +135 -135
- 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/global-loader.js +40 -2
- 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 +285 -255
- 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/patch-bash-process.js +125 -0
- package/dist/server/patch-tool.js +9 -9
- package/dist/server/patch-turn-end-boundary.js +3 -3
- package/dist/server/payload-limits.js +17 -0
- package/dist/server/pending-question.js +82 -0
- package/dist/server/persona-chain.js +8 -8
- 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 +288 -94
- package/dist/server/preset-fields.js +56 -0
- package/dist/server/preset-plugin-dependencies.js +57 -0
- package/dist/server/preset-share.js +113 -72
- package/dist/server/prompt-templates.js +106 -0
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/queue-modes.js +10 -0
- package/dist/server/remote-ssh-service.js +199 -106
- package/dist/server/request-context.js +164 -0
- package/dist/server/resolve-global-sdk.js +2 -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/schedule-agent-tool.js +39 -21
- package/dist/server/scheduler-tasks.js +106 -55
- package/dist/server/scm-commitmsg.js +10 -10
- 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 +273 -111
- package/dist/server/setup-config.js +58 -0
- package/dist/server/setup-page.js +620 -620
- 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/text-quote.js +9 -0
- package/dist/server/tool-guards.js +42 -6
- package/dist/server/tool-manager.js +48 -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/drivers/claude-code.js +5 -0
- package/dist/server/universal/drivers/codex.js +336 -0
- package/dist/server/universal/index.js +1 -0
- package/dist/server/universal/session-store.js +48 -6
- package/dist/server/universal/universal-attachments.js +24 -1
- package/dist/server/universal/universal-service.js +1 -1
- package/dist/server/universal/universal-session.js +357 -231
- package/dist/server/uploads.js +64 -5
- package/dist/server/vision-bridge.js +18 -18
- package/dist/server/worktree-service.js +32 -24
- package/dist/server/ws-pending-queue.js +73 -27
- package/docs/device-center.md +148 -0
- package/docs/device-desktop.md +48 -0
- package/docs/dsh-engine.md +493 -493
- package/extensions/webui.ts +335 -335
- package/package.json +5 -2
- package/plugin-sdk/README.md +174 -174
- package/plugin-sdk/client-utils.mjs +279 -279
- package/plugin-sdk/import-check.mjs +210 -210
- package/plugin-sdk/index.d.ts +698 -676
- package/plugin-sdk/index.mjs +696 -696
- package/plugin-sdk/package.json +13 -13
- package/plugin-sdk/ssh-host-trust.mjs +225 -0
- package/plugins/catalog.json +11 -1
- package/themes/aetheris.css +501 -607
- package/themes/ayu-light.css +116 -140
- package/themes/bases/flow-base.css +1357 -1363
- package/themes/bases/obsidian-base.css +895 -895
- package/themes/bases/statusbar-flat-base.css +112 -0
- package/themes/bases/zhujian-base.css +688 -687
- package/themes/bases/zhupi-base.css +660 -659
- package/themes/catppuccin-latte.css +119 -142
- package/themes/catppuccin.css +116 -147
- package/themes/claude-code-dark.css +147 -146
- package/themes/codex.css +114 -138
- package/themes/cyberpunk.css +50 -108
- package/themes/dark-teal.css +264 -265
- package/themes/dazzle.css +38 -108
- package/themes/everforest-light.css +115 -138
- package/themes/flow-light.css +164 -164
- package/themes/flow.css +161 -161
- package/themes/geist.css +114 -138
- package/themes/gruvbox-light.css +115 -138
- package/themes/kanagawa-lotus.css +115 -138
- package/themes/md-preview.css +24 -234
- package/themes/mist.css +166 -186
- package/themes/nord.css +107 -138
- package/themes/one-dark.css +106 -139
- package/themes/paper.css +174 -186
- package/themes/rose-pine-dawn.css +114 -138
- package/themes/sakura.css +166 -186
- package/themes/solarized-light.css +114 -137
- package/themes/tokyo-night.css +115 -146
- package/themes/translucent.css +85 -85
- package/themes/transparent.css +116 -116
- package/themes/vs-code-dark.css +147 -147
- package/themes/wallpaper-flow.css +382 -382
- package/themes/wallpaper-obsidian.css +74 -74
- package/themes/white.css +164 -186
- package/themes/zhujian-dark.css +115 -114
- package/themes/zhujian.css +114 -114
- package/themes/zhupi-dark.css +115 -114
- package/themes/zhupi.css +114 -114
- package/web/dist/.build-assets.json +1 -0
- package/web/dist/assets/BgTasksModal-BcXJ20RO.js +1 -0
- package/web/dist/assets/BgTasksModal-DjQmiKnE.js +1 -0
- package/web/dist/assets/CanvasStudio-PI9pid0Y.js +1579 -0
- package/web/dist/assets/CanvasStudio-nf5-HxLM.js +1579 -0
- package/web/dist/assets/DeviceCenter-CYttJw2a.js +3 -0
- package/web/dist/assets/DeviceCenter-DPe4sz74.js +3 -0
- package/web/dist/assets/DeviceGroups-D2rogxEN.js +1 -0
- package/web/dist/assets/Dropdown-ZLS04O8p.js +1 -0
- package/web/dist/assets/FilePreview-C9Ut4Lq6.js +2 -0
- package/web/dist/assets/FilePreview-CvK3ZzX5.js +2 -0
- package/web/dist/assets/GlobalSearchModal-C5e-OCw2.js +1 -0
- package/web/dist/assets/GlobalSearchModal-H7pFK2UC.js +1 -0
- package/web/dist/assets/GroupConversation-C2wNM0dQ.js +1 -0
- package/web/dist/assets/GroupConversation-ChN3TWQK.js +1 -0
- package/web/dist/assets/ModelConfigModal-DDZccDYt.js +2 -0
- package/web/dist/assets/PiSetupModal-DHbqfx8T.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-C-MK36Dk.js +1 -0
- package/web/dist/assets/SCMPanel-buTGziDB.js +2 -0
- package/web/dist/assets/SCMPanel-kx-SCL2s.js +2 -0
- package/web/dist/assets/SettingsModal-BsUF8jqr.js +20 -0
- package/web/dist/assets/SettingsModal-D357Ky7P.js +20 -0
- package/web/dist/assets/TerminalPanel-BBuM_Sez.js +3 -0
- package/web/dist/assets/TerminalPanel-D6x6xz8Q.js +3 -0
- package/web/dist/assets/TuiOverlayModal-CdbGV3Za.js +1 -0
- package/web/dist/assets/TuiOverlayModal-d6x9u25n.js +1 -0
- package/web/dist/assets/app-globals-DcyO0UPi.js +1 -0
- package/web/dist/assets/desktop-YguLbrX1.js +1 -0
- package/web/dist/assets/es-DWMIuDU2.js +2 -0
- package/web/dist/assets/fi-DaSO0Q-m.js +325 -0
- package/web/dist/assets/index-BPvpea8t.js +85 -0
- package/web/dist/assets/index-D6u8xH0o.js +85 -0
- package/web/dist/assets/index-DDZMv6LO.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-BRBg6zmQ.js +1 -0
- package/web/dist/assets/shortcut-stack-hhrDyRBn.js +1 -0
- package/web/dist/assets/theme-DDl-Oy1b.js +1 -0
- package/web/dist/assets/uuid-BzDIGCwP.js +1 -0
- package/web/dist/index.html +42 -35
- package/web/dist/sw.js +179 -179
- package/web/public/sw.js +179 -179
- package/dist/gh-comment-781.md +0 -5
- package/dist/server/durable/durable-capabilities.js +0 -11
- package/dist/server/markers/builtins/services.js +0 -123
- package/dist/server/markers/builtins/todo.js +0 -189
- package/dist/server/markers-list-tool.js +0 -34
- package/dist/server/orchestrator.js +0 -109
- package/dist/server/system-prompt-soul.js +0 -41
- package/dist/server/univer-service.js +0 -448
- package/dist/server/universal/drivers/dsh-driver.js +0 -379
- package/dist/test-artifact.md +0 -9
- package/dist/test-preview-modal.md +0 -11
- package/dist/test-report.html +0 -20
- 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,92 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { hostname } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { createDeviceHub } from "./hub.js";
|
|
6
|
+
import { createDeviceRelay } from "./relay.js";
|
|
7
|
+
import { hashSecret } from "./store.js";
|
|
8
|
+
/** Private loopback Hub: uses the same authenticated Bot, room and task paths as a remote Hub. */
|
|
9
|
+
export async function createLocalDeviceHub(dataDir) {
|
|
10
|
+
const server = createServer();
|
|
11
|
+
await new Promise((resolve, reject) => {
|
|
12
|
+
server.once("error", reject);
|
|
13
|
+
server.listen(0, "127.0.0.1", () => {
|
|
14
|
+
server.off("error", reject);
|
|
15
|
+
resolve();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
const address = server.address();
|
|
19
|
+
if (!address || typeof address === "string")
|
|
20
|
+
throw new Error("Local Hub address unavailable");
|
|
21
|
+
const hubUrl = `http://127.0.0.1:${address.port}`;
|
|
22
|
+
let hub;
|
|
23
|
+
let relay;
|
|
24
|
+
try {
|
|
25
|
+
hub = await createDeviceHub({
|
|
26
|
+
dataDir: join(dataDir, "local-device-hub"),
|
|
27
|
+
publicUrl: hubUrl,
|
|
28
|
+
server,
|
|
29
|
+
isOnline: (id) => relay?.isOnline(id) ?? false,
|
|
30
|
+
});
|
|
31
|
+
const credential = randomBytes(32).toString("hex");
|
|
32
|
+
const uiCredential = randomBytes(32).toString("hex");
|
|
33
|
+
// Stable local identity and durable rooms; credentials are rotated on every process start.
|
|
34
|
+
const accountId = "local-account";
|
|
35
|
+
const deviceId = "local-device";
|
|
36
|
+
await hub.store.transact((draft) => {
|
|
37
|
+
if (!draft.accounts.some((account) => account.id === accountId))
|
|
38
|
+
draft.accounts.push({ id: accountId, username: "local", passwordHash: "", createdAt: Date.now() });
|
|
39
|
+
const device = {
|
|
40
|
+
id: deviceId,
|
|
41
|
+
accountId,
|
|
42
|
+
name: hostname(),
|
|
43
|
+
credentialHash: hashSecret(credential),
|
|
44
|
+
uiCredentialHash: hashSecret(uiCredential),
|
|
45
|
+
createdAt: Date.now(),
|
|
46
|
+
};
|
|
47
|
+
const previous = draft.devices.find((entry) => entry.id === deviceId);
|
|
48
|
+
if (previous)
|
|
49
|
+
Object.assign(previous, device, { createdAt: previous.createdAt });
|
|
50
|
+
else
|
|
51
|
+
draft.devices.push(device);
|
|
52
|
+
});
|
|
53
|
+
relay = createDeviceRelay({
|
|
54
|
+
server,
|
|
55
|
+
publicUrl: hubUrl,
|
|
56
|
+
getAccount: hub.getRelayAccount,
|
|
57
|
+
authenticateDevice: (id, token) => hub.store.authenticateDevice(id, token),
|
|
58
|
+
ownsDevice: (owner, id) => hub.store.getDeviceOwner(id) === owner,
|
|
59
|
+
onTaskEvent: async (id, event) => {
|
|
60
|
+
await hub.acceptTaskEvent(id, event);
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
hub.setRelay({
|
|
64
|
+
onTask: relay.sendTask,
|
|
65
|
+
onRevoke: relay.revoke,
|
|
66
|
+
handleRequest: relay.handleRequest,
|
|
67
|
+
});
|
|
68
|
+
const config = {
|
|
69
|
+
hubUrl,
|
|
70
|
+
name: hostname(),
|
|
71
|
+
deviceId,
|
|
72
|
+
credential,
|
|
73
|
+
uiCredential,
|
|
74
|
+
allowExecution: true,
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
config,
|
|
78
|
+
async close() {
|
|
79
|
+
relay?.close();
|
|
80
|
+
await hub?.close();
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
relay?.close();
|
|
86
|
+
if (hub)
|
|
87
|
+
await hub.close();
|
|
88
|
+
else
|
|
89
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { hostname } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { getLocalBotOptions } from "./bot-catalog.js";
|
|
6
|
+
import { isIP } from "node:net";
|
|
7
|
+
import { installHubSetupRoutes } from "./hub-setup.js";
|
|
8
|
+
import { DeviceWorkspaceError } from "./workspaces.js";
|
|
9
|
+
import { createLocalDeviceHub } from "./local-hub.js";
|
|
10
|
+
import { createLocalBotRegistry } from "./bots.js";
|
|
11
|
+
/** Hub enrollment accepts HTTP(S) origins, including public IP addresses. */
|
|
12
|
+
export function normalizeHubUrl(input) {
|
|
13
|
+
if (typeof input !== "string" || input.length > 2048)
|
|
14
|
+
throw new Error("Invalid hub URL");
|
|
15
|
+
const url = new URL(input);
|
|
16
|
+
if (!["https:", "http:"].includes(url.protocol) ||
|
|
17
|
+
url.username ||
|
|
18
|
+
Reflect.get(url, "pass" + "word") ||
|
|
19
|
+
url.search ||
|
|
20
|
+
url.hash ||
|
|
21
|
+
url.pathname !== "/")
|
|
22
|
+
throw new Error("Hub must use HTTP(S), without a path, query, fragment or credentials");
|
|
23
|
+
return url.origin;
|
|
24
|
+
}
|
|
25
|
+
function localOnly(req, res, next) {
|
|
26
|
+
const peer = req.socket.remoteAddress?.replace(/^::ffff:/, "");
|
|
27
|
+
if (!(peer === "::1" || peer?.startsWith("127."))) {
|
|
28
|
+
res.status(403).json({ code: "LOCAL_ONLY", error: "Configure devices from this computer's loopback address" });
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
let host;
|
|
32
|
+
try {
|
|
33
|
+
host = new URL(`http://${req.headers.host}`).hostname.replace(/^\[|\]$/g, "");
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
res.status(403).json({ code: "LOCAL_ONLY", error: "Invalid host" });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!(host === "localhost" || host === "::1" || (isIP(host) === 4 && host.startsWith("127.")))) {
|
|
40
|
+
res.status(403).json({ code: "LOCAL_ONLY", error: "Use localhost to configure device access" });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (!["GET", "HEAD"].includes(req.method)) {
|
|
44
|
+
try {
|
|
45
|
+
const origin = new URL(req.headers.origin ?? "");
|
|
46
|
+
if (!["http:", "https:"].includes(origin.protocol) || origin.host !== req.headers.host)
|
|
47
|
+
throw new Error();
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
res.status(403).json({ code: "LOCAL_ONLY", error: "Same-origin request required" });
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
res.setHeader("Cache-Control", "no-store");
|
|
55
|
+
next();
|
|
56
|
+
}
|
|
57
|
+
export async function installLocalDeviceCenter(options) {
|
|
58
|
+
const file = join(options.dataDir, "device-center.json");
|
|
59
|
+
const bots = options.bots ?? (await createLocalBotRegistry(options.dataDir));
|
|
60
|
+
let config;
|
|
61
|
+
let connection;
|
|
62
|
+
let localHub;
|
|
63
|
+
let localStarting;
|
|
64
|
+
const activeConfig = () => config ?? localHub?.config;
|
|
65
|
+
async function ensureLocalHub() {
|
|
66
|
+
if (config || localHub || closed)
|
|
67
|
+
return;
|
|
68
|
+
if (!localStarting) {
|
|
69
|
+
localStarting = (async () => {
|
|
70
|
+
const hub = await createLocalDeviceHub(options.dataDir);
|
|
71
|
+
try {
|
|
72
|
+
const { uiCredential: _uiCredential, ...agentConfig } = hub.config;
|
|
73
|
+
localHub = hub;
|
|
74
|
+
connection = await options.connect(agentConfig, syncBots);
|
|
75
|
+
await syncBots();
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
await connection?.close();
|
|
79
|
+
connection = undefined;
|
|
80
|
+
localHub = undefined;
|
|
81
|
+
await hub.close();
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
})().finally(() => {
|
|
85
|
+
localStarting = undefined;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
await localStarting;
|
|
89
|
+
}
|
|
90
|
+
let pending;
|
|
91
|
+
let generation = 0;
|
|
92
|
+
let closed = false;
|
|
93
|
+
let serial = Promise.resolve();
|
|
94
|
+
const serialize = (work) => {
|
|
95
|
+
const result = serial.then(work);
|
|
96
|
+
serial = result.catch(() => undefined);
|
|
97
|
+
return result;
|
|
98
|
+
};
|
|
99
|
+
async function persist(next) {
|
|
100
|
+
await mkdir(options.dataDir, { recursive: true });
|
|
101
|
+
const temp = `${file}.${randomUUID()}.tmp`;
|
|
102
|
+
try {
|
|
103
|
+
await writeFile(temp, JSON.stringify(next ?? null), { mode: 0o600, flag: "wx" });
|
|
104
|
+
await rename(temp, file);
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
await rm(temp, { force: true });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async function reconnect() {
|
|
111
|
+
await localStarting;
|
|
112
|
+
await connection?.close();
|
|
113
|
+
connection = undefined;
|
|
114
|
+
await localHub?.close();
|
|
115
|
+
localHub = undefined;
|
|
116
|
+
await bots.setScope(config ? JSON.stringify([config.hubUrl, config.deviceId]) : "unpaired");
|
|
117
|
+
if (config && !closed) {
|
|
118
|
+
const { uiCredential: _uiCredential, ...agentConfig } = config;
|
|
119
|
+
connection = await options.connect(agentConfig, syncBots);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function syncBots() {
|
|
123
|
+
const captured = activeConfig();
|
|
124
|
+
if (!captured || closed)
|
|
125
|
+
return;
|
|
126
|
+
const response = await fetch(`${captured.hubUrl}/api/agent/bots`, {
|
|
127
|
+
method: "PUT",
|
|
128
|
+
redirect: "error",
|
|
129
|
+
signal: AbortSignal.timeout(15000),
|
|
130
|
+
headers: {
|
|
131
|
+
"Content-Type": "application/json",
|
|
132
|
+
"X-Device-Id": captured.deviceId,
|
|
133
|
+
Authorization: `Bearer ${captured.credential}`,
|
|
134
|
+
},
|
|
135
|
+
body: JSON.stringify({
|
|
136
|
+
bots: bots
|
|
137
|
+
.list()
|
|
138
|
+
.map(({ id, name, description, enabled, revision }) => ({ id, name, description, enabled, revision })),
|
|
139
|
+
}),
|
|
140
|
+
});
|
|
141
|
+
await response.body?.cancel();
|
|
142
|
+
if (!response.ok)
|
|
143
|
+
throw new Error("Bot configuration saved locally, but Hub synchronization failed; retry after reconnecting");
|
|
144
|
+
if (activeConfig() !== captured || closed)
|
|
145
|
+
throw new Error("Device binding changed during Bot synchronization");
|
|
146
|
+
}
|
|
147
|
+
async function hubRequest(hubUrl, path, body) {
|
|
148
|
+
const response = await fetch(`${hubUrl}${path}`, {
|
|
149
|
+
method: "POST",
|
|
150
|
+
redirect: "error",
|
|
151
|
+
signal: AbortSignal.timeout(15_000),
|
|
152
|
+
headers: { "Content-Type": "application/json", Origin: hubUrl },
|
|
153
|
+
body: JSON.stringify(body),
|
|
154
|
+
});
|
|
155
|
+
if (!response.body)
|
|
156
|
+
throw new Error("Empty hub response");
|
|
157
|
+
const reader = response.body.getReader();
|
|
158
|
+
const chunks = [];
|
|
159
|
+
let bytes = 0;
|
|
160
|
+
try {
|
|
161
|
+
for (;;) {
|
|
162
|
+
const chunk = await reader.read();
|
|
163
|
+
if (chunk.done)
|
|
164
|
+
break;
|
|
165
|
+
bytes += chunk.value.byteLength;
|
|
166
|
+
if (bytes > 32768)
|
|
167
|
+
throw new Error("Hub response exceeds limit");
|
|
168
|
+
chunks.push(chunk.value);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
await reader.cancel();
|
|
173
|
+
}
|
|
174
|
+
const result = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
175
|
+
if (!response.ok)
|
|
176
|
+
throw new Error(typeof result.error === "string" ? result.error : `Hub returned ${response.status}`);
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
const value = JSON.parse(await readFile(file, "utf8"));
|
|
181
|
+
if (value !== null) {
|
|
182
|
+
if (!value ||
|
|
183
|
+
typeof value.deviceId !== "string" ||
|
|
184
|
+
typeof value.credential !== "string" ||
|
|
185
|
+
typeof value.name !== "string" ||
|
|
186
|
+
typeof value.allowExecution !== "boolean" ||
|
|
187
|
+
(value.uiCredential !== undefined && typeof value.uiCredential !== "string"))
|
|
188
|
+
throw new Error("Invalid saved device configuration");
|
|
189
|
+
config = { ...value, hubUrl: normalizeHubUrl(value.hubUrl) };
|
|
190
|
+
await reconnect();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
if (error.code !== "ENOENT")
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
// Existing bookmarks now enter the native application view.
|
|
198
|
+
options.app.get("/devices", localOnly, (_req, res) => {
|
|
199
|
+
res.redirect("./?view=devices");
|
|
200
|
+
});
|
|
201
|
+
options.app.use("/api/device-center", localOnly);
|
|
202
|
+
const hubSetup = installHubSetupRoutes(options.app, options.dataDir);
|
|
203
|
+
options.app.get("/api/device-center/status", (_req, res) => {
|
|
204
|
+
res.json({
|
|
205
|
+
name: config?.name ?? hostname(),
|
|
206
|
+
hubUrl: config?.hubUrl,
|
|
207
|
+
deviceId: config?.deviceId,
|
|
208
|
+
allowExecution: config?.allowExecution ?? false,
|
|
209
|
+
connected: connection?.isConnected?.() ?? false,
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
// Account chat discovery is safe to relay; local settings remain device-only.
|
|
213
|
+
options.app.get("/api/device-center/chat-status", async (_req, res) => {
|
|
214
|
+
try {
|
|
215
|
+
await serialize(ensureLocalHub);
|
|
216
|
+
const current = activeConfig();
|
|
217
|
+
res.json({ hubUrl: current?.hubUrl, deviceId: current?.deviceId, ...(!config ? { local: true } : {}) });
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
res.status(503).json({ code: "LOCAL_HUB_UNAVAILABLE", error: "Local collaboration could not start" });
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
const activeRequests = new Set();
|
|
224
|
+
let inFlight = 0;
|
|
225
|
+
function userRoute(method, path, fields) {
|
|
226
|
+
options.app[method](`/api/device-center/${path}`, async (req, res) => {
|
|
227
|
+
try {
|
|
228
|
+
await serialize(ensureLocalHub);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return res.status(503).json({ code: "LOCAL_HUB_UNAVAILABLE", error: "Local collaboration could not start" });
|
|
232
|
+
}
|
|
233
|
+
const captured = activeConfig();
|
|
234
|
+
const capturedGeneration = generation;
|
|
235
|
+
if (!captured)
|
|
236
|
+
return res.status(409).json({ code: "NOT_PAIRED", error: "Pair this computer first" });
|
|
237
|
+
if (!captured.uiCredential)
|
|
238
|
+
return res
|
|
239
|
+
.status(409)
|
|
240
|
+
.json({ code: "REPAIR_REQUIRED", error: "Pair this computer again to enable group chat" });
|
|
241
|
+
if (closed || inFlight >= 8)
|
|
242
|
+
return res.status(429).json({ code: "BUSY", error: "Device center busy" });
|
|
243
|
+
const body = req.body ?? {};
|
|
244
|
+
if (method === "post" &&
|
|
245
|
+
(!body ||
|
|
246
|
+
typeof body !== "object" ||
|
|
247
|
+
Array.isArray(body) ||
|
|
248
|
+
Object.keys(body).some((key) => !fields.includes(key)) ||
|
|
249
|
+
Buffer.byteLength(JSON.stringify(body)) > 32768))
|
|
250
|
+
return res.status(400).json({ code: "INVALID_BODY", error: "Invalid group request" });
|
|
251
|
+
const controller = new AbortController();
|
|
252
|
+
activeRequests.add(controller);
|
|
253
|
+
inFlight++;
|
|
254
|
+
const cancel = () => {
|
|
255
|
+
if (!res.writableEnded)
|
|
256
|
+
controller.abort();
|
|
257
|
+
};
|
|
258
|
+
req.once("aborted", cancel);
|
|
259
|
+
res.once("close", cancel);
|
|
260
|
+
try {
|
|
261
|
+
const suffix = path.replace(":id", encodeURIComponent(String(req.params.id)));
|
|
262
|
+
const response = await fetch(`${captured.hubUrl}/api/device-user/${suffix}`, {
|
|
263
|
+
method: method.toUpperCase(),
|
|
264
|
+
redirect: "error",
|
|
265
|
+
signal: AbortSignal.any([controller.signal, AbortSignal.timeout(15000)]),
|
|
266
|
+
headers: {
|
|
267
|
+
"Content-Type": "application/json",
|
|
268
|
+
"X-Device-Id": captured.deviceId,
|
|
269
|
+
Authorization: `Bearer ${captured.uiCredential}`,
|
|
270
|
+
},
|
|
271
|
+
...(method === "post" ? { body: JSON.stringify(body) } : {}),
|
|
272
|
+
});
|
|
273
|
+
if (!response.body)
|
|
274
|
+
throw new Error("Empty response");
|
|
275
|
+
const reader = response.body.getReader();
|
|
276
|
+
const chunks = [];
|
|
277
|
+
let bytes = 0;
|
|
278
|
+
try {
|
|
279
|
+
for (;;) {
|
|
280
|
+
const chunk = await reader.read();
|
|
281
|
+
if (chunk.done)
|
|
282
|
+
break;
|
|
283
|
+
bytes += chunk.value.byteLength;
|
|
284
|
+
if (bytes > 2 * 1024 * 1024)
|
|
285
|
+
throw new Error("Response exceeds limit");
|
|
286
|
+
chunks.push(chunk.value);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
finally {
|
|
290
|
+
await reader.cancel();
|
|
291
|
+
}
|
|
292
|
+
if (closed || activeConfig() !== captured || generation !== capturedGeneration)
|
|
293
|
+
return res.status(409).json({ code: "DEVICE_CHANGED", error: "Device binding changed; refresh" });
|
|
294
|
+
if (!response.ok) {
|
|
295
|
+
const failure = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
296
|
+
if (response.status === 409 && failure?.code === "ACTIVE_TASKS")
|
|
297
|
+
return res.status(409).json({
|
|
298
|
+
code: "ACTIVE_TASKS",
|
|
299
|
+
error: "Stop active tasks before removing members or deleting this group",
|
|
300
|
+
});
|
|
301
|
+
const status = [400, 401, 403, 404, 409, 429].includes(response.status) ? response.status : 502;
|
|
302
|
+
return res.status(status === 401 ? 409 : status).json({
|
|
303
|
+
code: status === 401 ? "REPAIR_REQUIRED" : status === 403 ? "HUB_FORBIDDEN" : "HUB_REJECTED",
|
|
304
|
+
error: status === 401 ? "Pair this computer again" : "Hub rejected group request",
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
const data = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
308
|
+
res.status(response.status).json(data);
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
if (!res.destroyed && (closed || activeConfig() !== captured || generation !== capturedGeneration))
|
|
312
|
+
return res.status(409).json({ code: "DEVICE_CHANGED", error: "Device binding changed; refresh" });
|
|
313
|
+
if (!res.destroyed)
|
|
314
|
+
res.status(502).json({ code: "HUB_UNAVAILABLE", error: "Hub request failed or exceeded response limit" });
|
|
315
|
+
}
|
|
316
|
+
finally {
|
|
317
|
+
inFlight--;
|
|
318
|
+
activeRequests.delete(controller);
|
|
319
|
+
req.off("aborted", cancel);
|
|
320
|
+
res.off("close", cancel);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
options.app.get("/api/device-center/local-bots", (_req, res) => {
|
|
325
|
+
res.json({ bots: bots.list() });
|
|
326
|
+
});
|
|
327
|
+
function botMutation(remove) {
|
|
328
|
+
return (req, res) => {
|
|
329
|
+
void serialize(async () => {
|
|
330
|
+
if (closed)
|
|
331
|
+
throw new DeviceWorkspaceError(409, "CLOSED", "Device center is shutting down");
|
|
332
|
+
const input = req.body;
|
|
333
|
+
const fields = remove
|
|
334
|
+
? ["revision"]
|
|
335
|
+
: [
|
|
336
|
+
"id",
|
|
337
|
+
"revision",
|
|
338
|
+
"name",
|
|
339
|
+
"description",
|
|
340
|
+
"systemPrompt",
|
|
341
|
+
"cwd",
|
|
342
|
+
"model",
|
|
343
|
+
"tools",
|
|
344
|
+
"skills",
|
|
345
|
+
"mcpServers",
|
|
346
|
+
"enabled",
|
|
347
|
+
];
|
|
348
|
+
if (!input ||
|
|
349
|
+
typeof input !== "object" ||
|
|
350
|
+
Array.isArray(input) ||
|
|
351
|
+
Object.keys(input).some((key) => !fields.includes(key)) ||
|
|
352
|
+
Buffer.byteLength(JSON.stringify(input)) > 128 * 1024)
|
|
353
|
+
throw new DeviceWorkspaceError(400, "BOT_INVALID", "Invalid Bot configuration");
|
|
354
|
+
const result = remove
|
|
355
|
+
? (await bots.remove(String(req.params.id), input.revision), { removed: true })
|
|
356
|
+
: { bot: await bots.upsert(input) };
|
|
357
|
+
try {
|
|
358
|
+
await syncBots();
|
|
359
|
+
return { ...result, synced: true };
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
return { ...result, synced: false };
|
|
363
|
+
}
|
|
364
|
+
}).then((data) => res.json(data), (error) => {
|
|
365
|
+
const known = error instanceof DeviceWorkspaceError;
|
|
366
|
+
res
|
|
367
|
+
.status(known ? error.status : 500)
|
|
368
|
+
.json({ code: known ? error.code : "BOT_FAILED", error: known ? error.message : "Bot operation failed" });
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
options.app.get("/api/device-center/local-bot-options", async (req, res) => {
|
|
373
|
+
if (req.headers.origin || req.headers["sec-fetch-site"] === "cross-site") {
|
|
374
|
+
try {
|
|
375
|
+
const origin = new URL(req.headers.origin ?? "");
|
|
376
|
+
if (!["http:", "https:"].includes(origin.protocol) || origin.host !== req.headers.host)
|
|
377
|
+
throw new Error();
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
res.status(403).json({ code: "LOCAL_ONLY", error: "Same-origin request required" });
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
res.setHeader("Cache-Control", "no-store");
|
|
385
|
+
const controller = new AbortController();
|
|
386
|
+
const cancel = () => {
|
|
387
|
+
if (!res.writableEnded)
|
|
388
|
+
controller.abort();
|
|
389
|
+
};
|
|
390
|
+
req.once("aborted", cancel);
|
|
391
|
+
res.once("close", cancel);
|
|
392
|
+
try {
|
|
393
|
+
const data = await getLocalBotOptions(options.botCatalog ?? { defaultCwd: process.cwd(), dataDir: options.dataDir }, req.query.cwd, controller.signal);
|
|
394
|
+
if (!controller.signal.aborted)
|
|
395
|
+
res.json(data);
|
|
396
|
+
}
|
|
397
|
+
catch (error) {
|
|
398
|
+
if (!controller.signal.aborted) {
|
|
399
|
+
const known = error instanceof DeviceWorkspaceError;
|
|
400
|
+
res.status(known ? error.status : 500).json({
|
|
401
|
+
code: known ? error.code : "BOT_CATALOG_FAILED",
|
|
402
|
+
error: known ? error.message : "Bot catalog unavailable",
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
finally {
|
|
407
|
+
req.off("aborted", cancel);
|
|
408
|
+
res.off("close", cancel);
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
options.app.post("/api/device-center/local-bots", botMutation(false));
|
|
412
|
+
options.app.post("/api/device-center/local-bots/:id/delete", botMutation(true));
|
|
413
|
+
userRoute("get", "bots", []);
|
|
414
|
+
userRoute("get", "devices", []);
|
|
415
|
+
userRoute("post", "devices/:id/open", []);
|
|
416
|
+
userRoute("get", "rooms", []);
|
|
417
|
+
userRoute("get", "rooms/:id", []);
|
|
418
|
+
userRoute("post", "rooms", ["name", "botIds"]);
|
|
419
|
+
userRoute("post", "rooms/:id/rename", ["name"]);
|
|
420
|
+
userRoute("post", "rooms/:id/members", ["botIds"]);
|
|
421
|
+
userRoute("post", "rooms/:id/delete", []);
|
|
422
|
+
userRoute("post", "rooms/:id/messages", [
|
|
423
|
+
"text",
|
|
424
|
+
"targetBotIds",
|
|
425
|
+
"sourceBotId",
|
|
426
|
+
"idempotencyKey",
|
|
427
|
+
"handoff",
|
|
428
|
+
"parentTaskId",
|
|
429
|
+
]);
|
|
430
|
+
function route(path, work) {
|
|
431
|
+
options.app.post(`/api/device-center/${path}`, (req, res) => {
|
|
432
|
+
void serialize(async () => {
|
|
433
|
+
if (closed)
|
|
434
|
+
throw new Error("Device center is shutting down");
|
|
435
|
+
return work(req);
|
|
436
|
+
}).then((data) => res.json(data), (error) => res.status(400).json({ error: error.message }));
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
route("pair", async (req) => {
|
|
440
|
+
if (config)
|
|
441
|
+
throw new Error("Disconnect the existing device before pairing again");
|
|
442
|
+
const hubUrl = normalizeHubUrl(req.body?.hubUrl);
|
|
443
|
+
const name = req.body?.name;
|
|
444
|
+
if (typeof name !== "string" || !name.trim() || name.length > 80)
|
|
445
|
+
throw new Error("Device name must contain 1–80 characters");
|
|
446
|
+
const result = await hubRequest(hubUrl, "/api/pair/start", { name: name.trim() });
|
|
447
|
+
if (typeof result.code !== "string" || typeof result.pollToken !== "string")
|
|
448
|
+
throw new Error("Invalid pairing response");
|
|
449
|
+
const expiresAt = typeof result.expiresAt === "number" ? result.expiresAt : Date.parse(result.expiresAt);
|
|
450
|
+
if (!Number.isFinite(expiresAt) || expiresAt <= Date.now() || expiresAt > Date.now() + 15 * 60_000)
|
|
451
|
+
throw new Error("Invalid pairing expiry");
|
|
452
|
+
pending = { hubUrl, name: name.trim(), pollToken: result.pollToken, expiresAt, generation: ++generation };
|
|
453
|
+
return { code: result.code, expiresAt, hubUrl };
|
|
454
|
+
});
|
|
455
|
+
route("poll", async () => {
|
|
456
|
+
const pair = pending;
|
|
457
|
+
if (!pair || pair.expiresAt < Date.now()) {
|
|
458
|
+
pending = undefined;
|
|
459
|
+
throw new Error("Pairing expired; request a new code");
|
|
460
|
+
}
|
|
461
|
+
const result = await hubRequest(pair.hubUrl, "/api/pair/poll", { pollToken: pair.pollToken });
|
|
462
|
+
if (result.pending)
|
|
463
|
+
return { pending: true };
|
|
464
|
+
if (closed || pair.generation !== generation)
|
|
465
|
+
throw new Error("Pairing cancelled");
|
|
466
|
+
if (typeof result.deviceId !== "string" ||
|
|
467
|
+
typeof result.credential !== "string" ||
|
|
468
|
+
typeof result.uiCredential !== "string" ||
|
|
469
|
+
!result.uiCredential)
|
|
470
|
+
throw new Error("Invalid device credentials");
|
|
471
|
+
const next = {
|
|
472
|
+
hubUrl: pair.hubUrl,
|
|
473
|
+
name: pair.name,
|
|
474
|
+
deviceId: result.deviceId,
|
|
475
|
+
credential: result.credential,
|
|
476
|
+
uiCredential: result.uiCredential,
|
|
477
|
+
allowExecution: false,
|
|
478
|
+
};
|
|
479
|
+
await persist(next);
|
|
480
|
+
config = next;
|
|
481
|
+
pending = undefined;
|
|
482
|
+
await reconnect();
|
|
483
|
+
return { paired: true };
|
|
484
|
+
});
|
|
485
|
+
route("execution", async (req) => {
|
|
486
|
+
if (!config)
|
|
487
|
+
throw new Error("Pair this computer first");
|
|
488
|
+
if (typeof req.body?.allowExecution !== "boolean")
|
|
489
|
+
throw new Error("Invalid execution permission");
|
|
490
|
+
const next = { ...config, allowExecution: req.body.allowExecution };
|
|
491
|
+
await persist(next);
|
|
492
|
+
config = next;
|
|
493
|
+
await reconnect();
|
|
494
|
+
return { saved: true };
|
|
495
|
+
});
|
|
496
|
+
route("disconnect", async () => {
|
|
497
|
+
await persist(undefined);
|
|
498
|
+
generation++;
|
|
499
|
+
for (const controller of activeRequests)
|
|
500
|
+
controller.abort();
|
|
501
|
+
pending = undefined;
|
|
502
|
+
config = undefined;
|
|
503
|
+
await reconnect();
|
|
504
|
+
return { disconnected: true };
|
|
505
|
+
});
|
|
506
|
+
return {
|
|
507
|
+
syncBots,
|
|
508
|
+
getConfig() {
|
|
509
|
+
const current = activeConfig();
|
|
510
|
+
if (!current)
|
|
511
|
+
return undefined;
|
|
512
|
+
const { uiCredential: _uiCredential, ...agentConfig } = current;
|
|
513
|
+
return agentConfig;
|
|
514
|
+
},
|
|
515
|
+
async close() {
|
|
516
|
+
closed = true;
|
|
517
|
+
await hubSetup.close();
|
|
518
|
+
generation++;
|
|
519
|
+
for (const controller of activeRequests)
|
|
520
|
+
controller.abort();
|
|
521
|
+
await serial;
|
|
522
|
+
await localStarting;
|
|
523
|
+
await connection?.close();
|
|
524
|
+
connection = undefined;
|
|
525
|
+
await localHub?.close();
|
|
526
|
+
localHub = undefined;
|
|
527
|
+
},
|
|
528
|
+
};
|
|
529
|
+
}
|