pi-web-ui 0.114.3 → 0.116.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 +62 -1
- package/bin/pi-web-ui.mjs +90 -8
- package/dist/gh-comment-781.md +5 -0
- package/dist/server/agent-service-daemon.js +522 -32
- package/dist/server/agent-service.js +954 -402
- package/dist/server/canvas/canvas-dispatch.js +13 -1
- package/dist/server/client-state.js +42 -2
- package/dist/server/communicate-tool.js +2 -0
- package/dist/server/conversation-communication.js +14 -0
- package/dist/server/conversation-identity.js +27 -0
- package/dist/server/device-center/approval-summary.js +31 -0
- package/dist/server/device-center/bot-catalog.js +21 -7
- package/dist/server/device-center/bot-execution.js +100 -9
- package/dist/server/device-center/bot-presets.js +46 -0
- package/dist/server/device-center/collab-service.js +362 -13
- package/dist/server/device-center/collab-tool.js +824 -13
- package/dist/server/device-center/connector.js +359 -23
- package/dist/server/device-center/desktop-page.js +22 -16
- package/dist/server/device-center/device-integration.js +595 -0
- package/dist/server/device-center/execution-guides.js +32 -0
- package/dist/server/device-center/execution-instructions.js +20 -0
- package/dist/server/device-center/execution-settings.js +213 -0
- package/dist/server/device-center/execution-system-prompt.js +38 -0
- package/dist/server/device-center/execution-workspace.js +20 -0
- package/dist/server/device-center/executor.js +392 -40
- package/dist/server/device-center/file-sync-paths.js +171 -0
- package/dist/server/device-center/file-sync.js +186 -96
- package/dist/server/device-center/group-context-delta.js +37 -0
- package/dist/server/device-center/group-context-message.js +210 -0
- package/dist/server/device-center/group-context.js +179 -0
- package/dist/server/device-center/group-events.js +136 -0
- package/dist/server/device-center/group-plan-guard.js +138 -0
- package/dist/server/device-center/hub-conversations.js +233 -0
- package/dist/server/device-center/hub-deliveries.js +277 -0
- package/dist/server/device-center/hub-integration.js +451 -0
- package/dist/server/device-center/hub-locks.js +320 -0
- package/dist/server/device-center/hub-page.js +13 -33
- package/dist/server/device-center/hub-webhooks.js +165 -0
- package/dist/server/device-center/hub.js +2379 -107
- package/dist/server/device-center/human-interaction.js +232 -0
- package/dist/server/device-center/integration-errors.js +25 -0
- package/dist/server/device-center/local-hub.js +20 -1
- package/dist/server/device-center/local-page.js +17 -0
- package/dist/server/device-center/local.js +430 -41
- package/dist/server/device-center/main.js +11 -1
- package/dist/server/device-center/partition-log.js +293 -0
- package/dist/server/device-center/path-portal.js +11 -3
- package/dist/server/device-center/relay.js +364 -35
- package/dist/server/device-center/report-metadata.js +66 -0
- package/dist/server/device-center/report-upload.js +63 -0
- package/dist/server/device-center/runtime.js +598 -41
- package/dist/server/device-center/smoke-preview.js +3 -0
- package/dist/server/device-center/store.js +203 -41
- package/dist/server/device-center/task-context-budget.js +161 -0
- package/dist/server/device-center/task-journal.js +385 -0
- package/dist/server/device-center/task-plan.js +33 -0
- package/dist/server/device-center/task-progress.js +141 -0
- package/dist/server/device-center/task-recovery.js +452 -0
- package/dist/server/device-center/task-report-outbox.js +216 -0
- package/dist/server/device-center/task-report.js +384 -0
- package/dist/server/device-center/task-scheduler.js +445 -0
- package/dist/server/device-center/task-state.js +333 -0
- package/dist/server/device-center/transfer-stream.js +1006 -0
- package/dist/server/device-center/transfers.js +841 -8
- package/dist/server/dispatch-domain-handlers.js +35 -6
- package/dist/server/dsh/dsh-agent-service.js +6 -0
- package/dist/server/durable/durable-capabilities.js +11 -0
- package/dist/server/engine-probe.js +112 -1
- package/dist/server/global-loader.js +119 -19
- package/dist/server/goal-service.js +10 -5
- package/dist/server/index.js +222 -29
- package/dist/server/managed.js +2 -0
- package/dist/server/markers/builtins/services.js +123 -0
- package/dist/server/markers/builtins/todo.js +189 -0
- package/dist/server/markers-list-tool.js +34 -0
- package/dist/server/message-deletion.js +40 -0
- package/dist/server/message-window.js +9 -0
- package/dist/server/model-admin.js +48 -6
- package/dist/server/model-enrich.js +10 -1
- package/dist/server/orchestrator.js +109 -0
- package/dist/server/patch-executable-lookup.js +396 -0
- package/dist/server/payload-limits.js +2 -0
- package/dist/server/plugin-llm.js +100 -9
- package/dist/server/plugins.js +65 -7
- package/dist/server/preset-fields.js +24 -0
- package/dist/server/preset-share.js +2 -0
- package/dist/server/prompt-base-store.js +85 -0
- package/dist/server/prompt-composer.js +44 -2
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/protocol.js +2 -2
- package/dist/server/reasoning-registry.js +207 -0
- package/dist/server/remote-ssh-service.js +22 -9
- package/dist/server/resolve-global-sdk.js +2 -0
- package/dist/server/session-search.js +1 -1
- package/dist/server/settings-service.js +90 -39
- package/dist/server/skill-content-filler.js +29 -8
- package/dist/server/skill-tool.js +12 -3
- package/dist/server/startup-banner.js +35 -0
- package/dist/server/subagent-helpers.js +4 -1
- package/dist/server/subagents.js +52 -37
- package/dist/server/system-prompt-soul.js +41 -0
- package/dist/server/tool-host-factories.js +1 -0
- package/dist/server/tool-manager.js +17 -9
- package/dist/server/unauthorized-page.js +319 -0
- package/dist/server/univer-service.js +448 -0
- package/dist/server/universal/drivers/dsh-driver.js +379 -0
- package/dist/server/universal/universal-session.js +5 -0
- package/dist/server/webui-context.js +25 -5
- package/dist/test-artifact.md +9 -0
- package/dist/test-preview-modal.md +11 -0
- package/dist/test-report.html +20 -0
- package/docs/README.md +94 -0
- package/docs/architecture-plugins.md +946 -0
- package/docs/device-center.md +106 -12
- package/docs/dsh-engine.md +10 -10
- package/extensions/webui.ts +43 -1
- package/package.json +2 -1
- package/themes/aetheris.css +5 -0
- package/themes/ayu-light.css +6 -0
- package/themes/bases/flow-base.css +68 -68
- package/themes/catppuccin-latte.css +6 -0
- package/themes/catppuccin.css +6 -0
- package/themes/claude-code-dark.css +5 -0
- package/themes/codex.css +6 -0
- package/themes/cyberpunk.css +6 -0
- package/themes/dark-teal.css +6 -1
- package/themes/dazzle.css +6 -0
- package/themes/everforest-light.css +6 -0
- package/themes/flow-light.css +8 -1
- package/themes/flow.css +6 -1
- package/themes/geist.css +6 -0
- package/themes/gruvbox-light.css +6 -0
- package/themes/kanagawa-lotus.css +6 -0
- package/themes/md-preview.css +6 -0
- package/themes/mist.css +6 -0
- package/themes/nord.css +6 -0
- package/themes/one-dark.css +6 -0
- package/themes/paper.css +6 -0
- package/themes/rose-pine-dawn.css +6 -0
- package/themes/sakura.css +6 -0
- package/themes/solarized-light.css +6 -0
- package/themes/tokyo-night.css +6 -0
- package/themes/translucent.css +6 -0
- package/themes/transparent.css +6 -0
- package/themes/vs-code-dark.css +5 -0
- package/themes/wallpaper-flow.css +5 -1
- package/themes/wallpaper-obsidian.css +6 -0
- package/themes/white.css +6 -0
- package/themes/zhujian-dark.css +6 -1
- package/themes/zhujian.css +6 -1
- package/themes/zhupi-dark.css +6 -1
- package/themes/zhupi.css +6 -1
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/BgTasksModal-BDoOpj-F.js +1 -0
- package/web/dist/assets/BgTasksModal-C0i3O1FT.js +1 -0
- package/web/dist/assets/{CanvasStudio-BSg9ghPV.js → CanvasStudio-B8thymBN.js} +3 -3
- package/web/dist/assets/{CanvasStudio-DxW7C3Sz.js → CanvasStudio-zSxyIx1R.js} +3 -3
- package/web/dist/assets/DeviceCenter-DfAHKcid.js +3 -0
- package/web/dist/assets/DeviceCenter-ydvOTlzE.js +3 -0
- package/web/dist/assets/DeviceExecutionSettings-q5Ik8oXg.js +1 -0
- package/web/dist/assets/DeviceGroups-CI14_SRb.js +1 -0
- package/web/dist/assets/DeviceGroups-DvsB5IEU.js +1 -0
- package/web/dist/assets/Dropdown-C4LIhshN.js +1 -0
- package/web/dist/assets/FilePreview-BgGongmv.js +2 -0
- package/web/dist/assets/FilePreview-D4NrhF-U.js +2 -0
- package/web/dist/assets/GlobalSearchModal-BWgU8yPd.js +1 -0
- package/web/dist/assets/GlobalSearchModal-F7fNimKk.js +1 -0
- package/web/dist/assets/GroupConversation-Dh71Pdb-.js +17 -0
- package/web/dist/assets/GroupConversation-ZK3nRmNm.js +17 -0
- package/web/dist/assets/HintTip-c6d0TJoh.js +1 -0
- package/web/dist/assets/ModelConfigModal-YVEgyy34.js +2 -0
- package/web/dist/assets/ModelConfigModal-vWtg3srp.js +2 -0
- package/web/dist/assets/PiSetupModal-BmMrrgB9.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-BGSBIKD7.js +1 -0
- package/web/dist/assets/SCMPanel-B8_EDxNr.js +2 -0
- package/web/dist/assets/SCMPanel-BU58qk3T.js +2 -0
- package/web/dist/assets/SettingsModal-CMUybq3J.js +32 -0
- package/web/dist/assets/SettingsModal-OBmGTh-5.js +32 -0
- package/web/dist/assets/TerminalPanel-0CYvD72B.js +3 -0
- package/web/dist/assets/TerminalPanel-DDavKvFr.js +3 -0
- package/web/dist/assets/TuiOverlayModal-BsA-QTvN.js +1 -0
- package/web/dist/assets/TuiOverlayModal-DzoVQBuG.js +1 -0
- package/web/dist/assets/app-globals-CZxzGvIo.js +123 -0
- package/web/dist/assets/canvas-client-CUnXlQYX.js +1 -0
- package/web/dist/assets/{desktop-7EvUHhKQ.js → desktop-aAKpvKJV.js} +1 -1
- package/web/dist/assets/group-view-state-CGCzYF8k.js +1 -0
- package/web/dist/assets/index-C6DwztqE.css +1 -0
- package/web/dist/assets/index-CuN802Sj.js +93 -0
- package/web/dist/assets/index-EH5Fw9fa.js +93 -0
- package/web/dist/assets/plugin-host-BRWib9DF.js +1 -0
- package/web/dist/assets/remote-ssh-client-CfYSMEZg.js +1 -0
- package/web/dist/assets/theme-D2l_mLNo.js +1 -0
- package/web/dist/index.html +10 -9
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/BgTasksModal-BvnUHjFe.js +0 -1
- package/web/dist/assets/BgTasksModal-DSC8ncW3.js +0 -1
- package/web/dist/assets/DeviceCenter-Bmgr-Bwm.js +0 -3
- package/web/dist/assets/DeviceCenter-Cl9MBXaH.js +0 -3
- package/web/dist/assets/DeviceGroups-aeLD2msl.js +0 -1
- package/web/dist/assets/Dropdown-D1Jl35dw.js +0 -1
- package/web/dist/assets/FilePreview-6CqoDF_P.js +0 -2
- package/web/dist/assets/FilePreview-CJa27G_Q.js +0 -2
- package/web/dist/assets/GlobalSearchModal-CJX35rZL.js +0 -1
- package/web/dist/assets/GlobalSearchModal-CMW0xz0P.js +0 -1
- package/web/dist/assets/GroupConversation-Cq4daBFb.js +0 -1
- package/web/dist/assets/GroupConversation-DoELa83U.js +0 -1
- package/web/dist/assets/ModelConfigModal-Bp7tfKYh.js +0 -2
- package/web/dist/assets/PiSetupModal-DD8srwBJ.js +0 -1
- package/web/dist/assets/ProviderOAuthControls-C6tfXb88.js +0 -1
- package/web/dist/assets/SCMPanel-CghgsyBN.js +0 -2
- package/web/dist/assets/SCMPanel-D8-8aqwC.js +0 -2
- package/web/dist/assets/SettingsModal-BbBe2rYo.js +0 -20
- package/web/dist/assets/SettingsModal-qETiVPKj.js +0 -20
- package/web/dist/assets/TerminalPanel-Da-Xs4Wk.js +0 -3
- package/web/dist/assets/TerminalPanel-DnS4tFQO.js +0 -3
- package/web/dist/assets/TuiOverlayModal-CGyGkKYl.js +0 -1
- package/web/dist/assets/TuiOverlayModal-CpBKjG6x.js +0 -1
- package/web/dist/assets/app-globals-DcyO0UPi.js +0 -1
- package/web/dist/assets/fi-NyN-ZrgD.js +0 -325
- package/web/dist/assets/index-DK3F5ZJo.js +0 -85
- package/web/dist/assets/index-DQMii2he.js +0 -85
- package/web/dist/assets/index-vwBnwzkr.css +0 -1
- package/web/dist/assets/plugin-host-BCnX3P8U.js +0 -1
- package/web/dist/assets/theme-D2DMH6tH.js +0 -1
|
@@ -1,8 +1,33 @@
|
|
|
1
|
-
import { resolve } from "node:path";
|
|
1
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { readFileSync, statSync } from "node:fs";
|
|
4
|
+
import { writeJsonAtomicSync } from "../atomic-file.js";
|
|
2
5
|
import { createDeviceCollabService } from "./collab-service.js";
|
|
3
6
|
import { createDeviceTaskExecutor } from "./executor.js";
|
|
4
7
|
import { createDeviceFileSync } from "./file-sync.js";
|
|
5
8
|
import { DeviceTaskError } from "./task-error.js";
|
|
9
|
+
import { TaskRecoveryManager } from "./task-recovery.js";
|
|
10
|
+
import { createTaskReportOutbox } from "./task-report-outbox.js";
|
|
11
|
+
import { reportUploadMetadata } from "./report-metadata.js";
|
|
12
|
+
import { DeviceTaskScheduler } from "./task-scheduler.js";
|
|
13
|
+
import { loadExecutionSettings, saveExecutionSettings } from "./execution-settings.js";
|
|
14
|
+
import { executionRoot, executionRootsOverlap } from "./execution-workspace.js";
|
|
15
|
+
import { devicePrepareIntegration, deviceVerifyIntegration, deviceConfirmIntegration, deviceCancelIntegration, } from "./device-integration.js";
|
|
16
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
17
|
+
function samePath(a, b) {
|
|
18
|
+
if (!a || !b)
|
|
19
|
+
return false;
|
|
20
|
+
const fold = (p) => (process.platform === "win32" ? resolve(p).toLowerCase() : resolve(p));
|
|
21
|
+
return fold(a) === fold(b);
|
|
22
|
+
}
|
|
23
|
+
/** The task settled, but its executor has not confirmed that execution stopped. */
|
|
24
|
+
export class DeviceTaskStopUnconfirmedError extends Error {
|
|
25
|
+
stopUnconfirmed = true;
|
|
26
|
+
constructor(cause) {
|
|
27
|
+
super(cause instanceof Error ? cause.message : "Task executor stop unconfirmed", { cause });
|
|
28
|
+
this.name = "DeviceTaskStopUnconfirmedError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
6
31
|
export function createDeviceRuntime(options) {
|
|
7
32
|
const { config, bots } = options;
|
|
8
33
|
const sessions = new Map();
|
|
@@ -10,21 +35,95 @@ export function createDeviceRuntime(options) {
|
|
|
10
35
|
const receipts = new Map();
|
|
11
36
|
const operations = new Set();
|
|
12
37
|
let closed = false;
|
|
13
|
-
let blocked;
|
|
14
38
|
let generation = new AbortController();
|
|
15
|
-
|
|
39
|
+
const active = new Set();
|
|
40
|
+
const transferOperations = new Set();
|
|
41
|
+
const recovery = new TaskRecoveryManager({
|
|
42
|
+
dataDir: options.dataDir,
|
|
43
|
+
deviceId: config.deviceId,
|
|
44
|
+
hubUrl: config.hubUrl,
|
|
45
|
+
});
|
|
46
|
+
const reportOutbox = createTaskReportOutbox(options.dataDir, config.deviceId, config.hubUrl);
|
|
47
|
+
const schedulerFile = options.dataDir ? join(options.dataDir, "task-scheduler-state.json") : undefined;
|
|
48
|
+
let schedulerSaveTimer;
|
|
49
|
+
let schedulerStatePending = false;
|
|
50
|
+
function loadSchedulerState() {
|
|
51
|
+
if (!schedulerFile)
|
|
52
|
+
return undefined;
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(readFileSync(schedulerFile, "utf8"));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Corrupt or missing fairness state only means a restart starts with neutral fairness.
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function flushSchedulerState() {
|
|
62
|
+
if (!schedulerFile || !schedulerStatePending)
|
|
63
|
+
return;
|
|
64
|
+
schedulerStatePending = false;
|
|
65
|
+
try {
|
|
66
|
+
writeJsonAtomicSync(schedulerFile, scheduler.snapshotState());
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
/* fairness state is best-effort; never fail dispatch on persistence */
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function scheduleSchedulerStateSave() {
|
|
73
|
+
if (!schedulerFile)
|
|
74
|
+
return;
|
|
75
|
+
schedulerStatePending = true;
|
|
76
|
+
if (schedulerSaveTimer)
|
|
77
|
+
return;
|
|
78
|
+
schedulerSaveTimer = setTimeout(() => {
|
|
79
|
+
schedulerSaveTimer = undefined;
|
|
80
|
+
flushSchedulerState();
|
|
81
|
+
}, 250);
|
|
82
|
+
schedulerSaveTimer.unref?.();
|
|
83
|
+
}
|
|
84
|
+
const scheduler = new DeviceTaskScheduler({
|
|
85
|
+
initialSettings: loadExecutionSettings(options.dataDir),
|
|
86
|
+
persistedState: loadSchedulerState(),
|
|
87
|
+
onChange: scheduleSchedulerStateSave,
|
|
88
|
+
});
|
|
89
|
+
const conflicts = (a, b) => a.botId === b.botId || executionRootsOverlap(a.root, b.root);
|
|
90
|
+
function drain() {
|
|
91
|
+
scheduler.drain();
|
|
92
|
+
}
|
|
93
|
+
const transcriptFile = options.dataDir ? join(options.dataDir, "bot-execution-conversations.json") : undefined;
|
|
94
|
+
let transcripts = Object.create(null);
|
|
95
|
+
let transcriptLoadError;
|
|
96
|
+
if (transcriptFile) {
|
|
97
|
+
try {
|
|
98
|
+
const stored = JSON.parse(readFileSync(transcriptFile, "utf8"));
|
|
99
|
+
if (!stored || typeof stored !== "object" || Array.isArray(stored))
|
|
100
|
+
throw new Error("Invalid transcript registry");
|
|
101
|
+
for (const [id, entry] of Object.entries(stored)) {
|
|
102
|
+
const value = entry;
|
|
103
|
+
if (!UUID.test(id) ||
|
|
104
|
+
!value ||
|
|
105
|
+
typeof value.binding !== "string" ||
|
|
106
|
+
typeof value.path !== "string" ||
|
|
107
|
+
!isAbsolute(value.path))
|
|
108
|
+
throw new Error("Invalid transcript registry");
|
|
109
|
+
}
|
|
110
|
+
transcripts = Object.assign(Object.create(null), stored);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error.code !== "ENOENT")
|
|
114
|
+
transcriptLoadError = error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
16
117
|
function enabled(signal) {
|
|
17
118
|
signal?.throwIfAborted();
|
|
18
119
|
if (closed)
|
|
19
120
|
throw new Error("Device runtime closed");
|
|
20
|
-
if (blocked)
|
|
21
|
-
throw new DeviceTaskError("群任务停止状态不确定,项目已隔离。请在本机确认后重启服务。 / Task stop unconfirmed; workspace blocked until service restart.");
|
|
22
121
|
if (!config.allowExecution)
|
|
23
122
|
throw new DeviceTaskError("目标设备未开启群任务执行权限。 / Local task execution is disabled.");
|
|
24
123
|
}
|
|
25
124
|
function track(operation) {
|
|
26
125
|
operations.add(operation);
|
|
27
|
-
void operation.
|
|
126
|
+
void operation.catch(() => { }).finally(() => operations.delete(operation));
|
|
28
127
|
return operation;
|
|
29
128
|
}
|
|
30
129
|
async function sweep() {
|
|
@@ -45,6 +144,8 @@ export function createDeviceRuntime(options) {
|
|
|
45
144
|
const lease = await bots.acquire(botId);
|
|
46
145
|
try {
|
|
47
146
|
enabled(signal);
|
|
147
|
+
if ([...active].some((owner) => owner.blocked && conflicts(owner, { botId, root: executionRoot(lease.root) })))
|
|
148
|
+
throw new DeviceTaskError("Task stop unconfirmed; workspace blocked until service restart");
|
|
48
149
|
return lease;
|
|
49
150
|
}
|
|
50
151
|
catch (error) {
|
|
@@ -56,14 +157,28 @@ export function createDeviceRuntime(options) {
|
|
|
56
157
|
const key = JSON.stringify([roomId, botId, lease.revision]);
|
|
57
158
|
let entry = files.get(key);
|
|
58
159
|
if (!entry) {
|
|
160
|
+
if (!options.dataDir)
|
|
161
|
+
throw new Error("File synchronization storage unavailable");
|
|
162
|
+
const canonicalRoot = resolve(lease.root);
|
|
163
|
+
const scopeToken = createHash("sha256")
|
|
164
|
+
.update(JSON.stringify([config.hubUrl, config.deviceId, roomId, botId, lease.revision, canonicalRoot]))
|
|
165
|
+
.digest("hex")
|
|
166
|
+
.slice(0, 32);
|
|
167
|
+
const scopedDataDir = join(options.dataDir, "scoped-transfers", scopeToken);
|
|
168
|
+
const currentSettings = scheduler.getSettings();
|
|
59
169
|
entry = {
|
|
60
170
|
roomId,
|
|
61
171
|
botId,
|
|
62
172
|
revision: lease.revision,
|
|
63
173
|
files: createDeviceFileSync({
|
|
64
174
|
root: lease.root,
|
|
65
|
-
dataDir:
|
|
175
|
+
dataDir: scopedDataDir,
|
|
66
176
|
allowReceive: config.allowExecution,
|
|
177
|
+
limits: {
|
|
178
|
+
bytes: currentSettings.fileSyncMaxBytes,
|
|
179
|
+
files: currentSettings.fileSyncMaxFiles,
|
|
180
|
+
stagedBytes: currentSettings.fileSyncStagedBytes,
|
|
181
|
+
},
|
|
67
182
|
}),
|
|
68
183
|
};
|
|
69
184
|
files.set(key, entry);
|
|
@@ -72,6 +187,10 @@ export function createDeviceRuntime(options) {
|
|
|
72
187
|
}
|
|
73
188
|
const service = createDeviceCollabService({
|
|
74
189
|
getConfig: () => (closed ? undefined : config),
|
|
190
|
+
getExecutionContext: (context) => {
|
|
191
|
+
const owner = context?.sessionId ? sessions.get(context.sessionId) : undefined;
|
|
192
|
+
return owner && samePath(context?.cwd, owner.root) ? owner : undefined;
|
|
193
|
+
},
|
|
75
194
|
getParentTask: (context) => (context?.sessionId ? sessions.get(context.sessionId)?.taskId : undefined),
|
|
76
195
|
getSourceBot: (context) => (context?.sessionId ? sessions.get(context.sessionId)?.botId : undefined),
|
|
77
196
|
async workspaceResolver({ roomId, sourceBotId, context, signal }) {
|
|
@@ -95,24 +214,121 @@ export function createDeviceRuntime(options) {
|
|
|
95
214
|
});
|
|
96
215
|
const collabService = {
|
|
97
216
|
execute(action, args, signal, context) {
|
|
98
|
-
if (action === "send_task"
|
|
99
|
-
|
|
217
|
+
if (action === "send_task") {
|
|
218
|
+
const owner = context?.sessionId ? sessions.get(context.sessionId) : undefined;
|
|
219
|
+
if (!owner || (args.roomId !== undefined && owner.roomId !== args.roomId))
|
|
220
|
+
return Promise.reject(new Error("Task session is not bound to this room"));
|
|
221
|
+
}
|
|
100
222
|
const commandSignal = AbortSignal.any([generation.signal, ...(signal ? [signal] : [])]);
|
|
101
223
|
return track(service.execute(action, args, commandSignal, context));
|
|
102
224
|
},
|
|
103
225
|
};
|
|
104
|
-
function onTask(task, signal) {
|
|
226
|
+
function onTask(task, signal, callbacks) {
|
|
105
227
|
try {
|
|
106
228
|
enabled(signal);
|
|
107
229
|
}
|
|
108
230
|
catch (error) {
|
|
109
231
|
return Promise.reject(error);
|
|
110
232
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
233
|
+
const taskSignal = signal ?? new AbortController().signal;
|
|
234
|
+
const root = executionRoot(bots.get(task.botId)?.cwd ?? options.dataDir ?? process.cwd());
|
|
235
|
+
return track(new Promise((resolve, reject) => {
|
|
236
|
+
let queued = null;
|
|
237
|
+
const onAbort = () => {
|
|
238
|
+
if (queued)
|
|
239
|
+
scheduler.removeWaiting(queued);
|
|
240
|
+
taskSignal.removeEventListener("abort", onAbort);
|
|
241
|
+
reject(taskSignal.reason ?? new DeviceTaskError("Task cancelled"));
|
|
242
|
+
scheduler.drain();
|
|
243
|
+
};
|
|
244
|
+
try {
|
|
245
|
+
queued = scheduler.enqueue({
|
|
246
|
+
id: task.id,
|
|
247
|
+
attemptId: task.attemptId,
|
|
248
|
+
roomId: task.roomId,
|
|
249
|
+
botId: task.botId,
|
|
250
|
+
botRevision: String(task.botRevision),
|
|
251
|
+
root,
|
|
252
|
+
}, {
|
|
253
|
+
start() {
|
|
254
|
+
taskSignal.removeEventListener("abort", onAbort);
|
|
255
|
+
void runTask(task, root, taskSignal, callbacks).then(resolve, reject).finally(drain);
|
|
256
|
+
},
|
|
257
|
+
reject(error) {
|
|
258
|
+
taskSignal.removeEventListener("abort", onAbort);
|
|
259
|
+
reject(error);
|
|
260
|
+
},
|
|
261
|
+
cancel() {
|
|
262
|
+
taskSignal.removeEventListener("abort", onAbort);
|
|
263
|
+
reject(taskSignal.reason ?? new DeviceTaskError("Task cancelled"));
|
|
264
|
+
scheduler.drain();
|
|
265
|
+
},
|
|
266
|
+
}, taskSignal);
|
|
267
|
+
}
|
|
268
|
+
catch (err) {
|
|
269
|
+
reject(err);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
taskSignal.addEventListener("abort", onAbort, { once: true });
|
|
273
|
+
if (taskSignal.aborted)
|
|
274
|
+
onAbort();
|
|
275
|
+
}));
|
|
276
|
+
}
|
|
277
|
+
function runTask(task, root, signal, callbacks) {
|
|
278
|
+
try {
|
|
279
|
+
enabled(signal);
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
return Promise.reject(error);
|
|
283
|
+
}
|
|
284
|
+
if (typeof task.leaseToken !== "string" || !task.leaseToken) {
|
|
285
|
+
return Promise.reject(new DeviceTaskError("Hub task lease grant required"));
|
|
286
|
+
}
|
|
287
|
+
const capacityCheck = recovery.hasOutboxCapacity();
|
|
288
|
+
if (!capacityCheck.available) {
|
|
289
|
+
return Promise.reject(new DeviceTaskError(capacityCheck.reason ?? "Recovery outbox capacity reached"));
|
|
290
|
+
}
|
|
291
|
+
const owner = {
|
|
292
|
+
controller: new AbortController(),
|
|
293
|
+
botId: task.botId,
|
|
294
|
+
root,
|
|
295
|
+
taskId: task.id,
|
|
296
|
+
attemptId: task.attemptId,
|
|
297
|
+
};
|
|
298
|
+
// Transactional Admission: reverse cleanup on any failure before execution starts
|
|
299
|
+
let leaseRecorded = false;
|
|
300
|
+
let activeItem;
|
|
301
|
+
try {
|
|
302
|
+
active.add(owner);
|
|
303
|
+
recovery.recordTaskStart({
|
|
304
|
+
taskId: task.id,
|
|
305
|
+
attemptId: task.attemptId,
|
|
306
|
+
botId: task.botId,
|
|
307
|
+
deviceId: config.deviceId,
|
|
308
|
+
roomId: task.roomId,
|
|
309
|
+
leaseToken: task.leaseToken,
|
|
310
|
+
leaseGeneration: task.leaseGeneration ?? 1,
|
|
311
|
+
conversationId: task.conversationId,
|
|
312
|
+
});
|
|
313
|
+
leaseRecorded = true;
|
|
314
|
+
activeItem = scheduler.registerActive({
|
|
315
|
+
id: task.id,
|
|
316
|
+
attemptId: task.attemptId,
|
|
317
|
+
roomId: task.roomId,
|
|
318
|
+
botId: task.botId,
|
|
319
|
+
botRevision: String(task.botRevision),
|
|
320
|
+
root,
|
|
321
|
+
}, owner.controller);
|
|
322
|
+
}
|
|
323
|
+
catch (admissionError) {
|
|
324
|
+
if (leaseRecorded) {
|
|
325
|
+
recovery.removeActiveLease(task.id, task.attemptId);
|
|
326
|
+
}
|
|
327
|
+
active.delete(owner);
|
|
328
|
+
return Promise.reject(admissionError);
|
|
329
|
+
}
|
|
330
|
+
callbacks?.onStarted?.();
|
|
331
|
+
const taskSignal = AbortSignal.any([owner.controller.signal, ...(signal ? [signal] : [])]);
|
|
116
332
|
const operation = (async () => {
|
|
117
333
|
let lease;
|
|
118
334
|
let stopping;
|
|
@@ -127,8 +343,10 @@ export function createDeviceRuntime(options) {
|
|
|
127
343
|
await stopping;
|
|
128
344
|
}
|
|
129
345
|
catch (error) {
|
|
130
|
-
|
|
131
|
-
|
|
346
|
+
const failure = error instanceof DeviceTaskStopUnconfirmedError ? error : new DeviceTaskStopUnconfirmedError(error);
|
|
347
|
+
owner.blocked = failure;
|
|
348
|
+
scheduler.markBlocked(task.id, task.attemptId, failure);
|
|
349
|
+
throw failure;
|
|
132
350
|
}
|
|
133
351
|
};
|
|
134
352
|
try {
|
|
@@ -138,43 +356,217 @@ export function createDeviceRuntime(options) {
|
|
|
138
356
|
if (task.deviceId !== config.deviceId || !task.botId)
|
|
139
357
|
throw new Error("Task Bot route mismatch");
|
|
140
358
|
lease = await acquire(task.botId, taskSignal);
|
|
141
|
-
if (task.botRevision !== lease.bot.revision)
|
|
142
|
-
throw new Error("Task Bot revision mismatch");
|
|
359
|
+
if (task.botRevision !== lease.bot.revision || executionRoot(lease.root) !== root)
|
|
360
|
+
throw new Error("Task Bot revision or workspace mismatch");
|
|
143
361
|
}
|
|
144
362
|
catch (error) {
|
|
145
363
|
if (taskSignal.aborted)
|
|
146
364
|
throw error;
|
|
147
365
|
throw new DeviceTaskError("目标 Bot 不可用、配置已变更或正在忙碌。 / Target Bot unavailable or busy.");
|
|
148
366
|
}
|
|
367
|
+
let binding;
|
|
368
|
+
let resumePath;
|
|
369
|
+
let transcriptPath;
|
|
370
|
+
let boundSession;
|
|
371
|
+
if (task.resume !== undefined && typeof task.resume !== "boolean")
|
|
372
|
+
throw new DeviceTaskError("Invalid resume flag");
|
|
373
|
+
if (task.resume && !task.conversationId)
|
|
374
|
+
throw new DeviceTaskError("Resume conversation address required");
|
|
375
|
+
if (task.conversationId !== undefined) {
|
|
376
|
+
if (!UUID.test(task.conversationId) || !transcriptFile || transcriptLoadError)
|
|
377
|
+
throw new DeviceTaskError("Persistent conversation storage unavailable or invalid address");
|
|
378
|
+
binding = createHash("sha256")
|
|
379
|
+
.update(JSON.stringify([
|
|
380
|
+
config.hubUrl,
|
|
381
|
+
config.deviceId,
|
|
382
|
+
task.roomId,
|
|
383
|
+
lease.bot.id,
|
|
384
|
+
lease.bot.revision,
|
|
385
|
+
resolve(lease.root),
|
|
386
|
+
]))
|
|
387
|
+
.digest("hex");
|
|
388
|
+
const stored = transcripts[task.conversationId];
|
|
389
|
+
if (task.resume) {
|
|
390
|
+
if (!stored || stored.binding !== binding)
|
|
391
|
+
throw new DeviceTaskError("Conversation unavailable or configuration changed");
|
|
392
|
+
try {
|
|
393
|
+
if (!statSync(stored.path).isFile())
|
|
394
|
+
throw new Error();
|
|
395
|
+
}
|
|
396
|
+
catch {
|
|
397
|
+
throw new DeviceTaskError("Conversation transcript unavailable");
|
|
398
|
+
}
|
|
399
|
+
resumePath = stored.path;
|
|
400
|
+
}
|
|
401
|
+
else if (stored)
|
|
402
|
+
throw new DeviceTaskError("Conversation already exists; explicit resume required");
|
|
403
|
+
}
|
|
149
404
|
owner.executor = (options.executorFactory ?? createDeviceTaskExecutor)({
|
|
405
|
+
...callbacks,
|
|
406
|
+
onActivity() {
|
|
407
|
+
recovery.recordActivity(task.id, task.attemptId);
|
|
408
|
+
callbacks?.onActivity?.();
|
|
409
|
+
},
|
|
410
|
+
onProgress(progress) {
|
|
411
|
+
recovery.recordActivity(task.id, task.attemptId);
|
|
412
|
+
callbacks?.onProgress?.(progress);
|
|
413
|
+
},
|
|
414
|
+
onInteractions(items) {
|
|
415
|
+
recovery.recordActivity(task.id, task.attemptId);
|
|
416
|
+
callbacks?.onInteractions?.(items);
|
|
417
|
+
},
|
|
418
|
+
onFullReport(taskId, attemptId, fullReport) {
|
|
419
|
+
recovery.recordActivity(taskId, attemptId);
|
|
420
|
+
if (taskId !== task.id || attemptId !== task.attemptId)
|
|
421
|
+
throw new Error("Report task binding mismatch");
|
|
422
|
+
// Persist the original before notifying transport or settling the task.
|
|
423
|
+
reportOutbox.capture(task, fullReport);
|
|
424
|
+
return callbacks?.onFullReport?.(taskId, attemptId, fullReport);
|
|
425
|
+
},
|
|
426
|
+
onPlanReady(taskId, plan) {
|
|
427
|
+
taskSignal.throwIfAborted();
|
|
428
|
+
if (taskId !== task.id)
|
|
429
|
+
return Promise.resolve(false);
|
|
430
|
+
recovery.recordActivity(taskId, task.attemptId);
|
|
431
|
+
return callbacks?.onPlanReady?.(taskId, plan) ?? Promise.resolve(false);
|
|
432
|
+
},
|
|
433
|
+
onToolLifecycle(lifecycle) {
|
|
434
|
+
recovery.recordActivity(task.id, task.attemptId);
|
|
435
|
+
callbacks?.onToolLifecycle?.(lifecycle);
|
|
436
|
+
},
|
|
437
|
+
registerInteractionAnswer(answer) {
|
|
438
|
+
owner.answer = answer;
|
|
439
|
+
},
|
|
150
440
|
localOrigin: options.localOrigin,
|
|
151
441
|
localAuthToken: options.localAuthToken,
|
|
152
442
|
cwd: lease.root,
|
|
153
443
|
bot: lease.bot,
|
|
154
444
|
allowExecution: config.allowExecution,
|
|
445
|
+
resumePath,
|
|
446
|
+
onSessionPath(taskId, path) {
|
|
447
|
+
taskSignal.throwIfAborted();
|
|
448
|
+
if (taskId !== task.id ||
|
|
449
|
+
!boundSession ||
|
|
450
|
+
sessions.get(boundSession)?.taskId !== task.id ||
|
|
451
|
+
!isAbsolute(path) ||
|
|
452
|
+
(resumePath && !samePath(path, resumePath)))
|
|
453
|
+
throw new Error("Transcript ownership mismatch");
|
|
454
|
+
if (task.conversationId) {
|
|
455
|
+
const currentBot = bots.get(task.botId);
|
|
456
|
+
if (!binding ||
|
|
457
|
+
!currentBot?.enabled ||
|
|
458
|
+
currentBot.revision !== lease.bot.revision ||
|
|
459
|
+
resolve(currentBot.cwd) !== resolve(lease.root))
|
|
460
|
+
throw new DeviceTaskError("Conversation transcript ownership changed");
|
|
461
|
+
try {
|
|
462
|
+
// Fresh SDK sessions allocate a path before the first message creates
|
|
463
|
+
// the file. Bind it now so failed first turns remain recoverable.
|
|
464
|
+
// Existing resume paths must still refer to a real transcript.
|
|
465
|
+
if (resumePath && !statSync(path).isFile())
|
|
466
|
+
throw new Error();
|
|
467
|
+
const next = { ...transcripts, [task.conversationId]: { binding, path } };
|
|
468
|
+
writeJsonAtomicSync(transcriptFile, next);
|
|
469
|
+
transcripts = next;
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
throw new DeviceTaskError("Conversation transcript persistence failed");
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
transcriptPath = path;
|
|
476
|
+
},
|
|
155
477
|
onSession(taskId, sessionId) {
|
|
478
|
+
if (taskId !== task.id)
|
|
479
|
+
throw new Error("Task session ownership mismatch");
|
|
480
|
+
boundSession = sessionId;
|
|
156
481
|
for (const [id, bound] of sessions)
|
|
157
482
|
if (bound.taskId === taskId && bound.roomId === task.roomId)
|
|
158
483
|
sessions.delete(id);
|
|
159
484
|
if (sessionId && !taskSignal.aborted)
|
|
160
|
-
sessions.set(sessionId, {
|
|
485
|
+
sessions.set(sessionId, {
|
|
486
|
+
taskId,
|
|
487
|
+
...(task.taskState ? { taskState: structuredClone(task.taskState) } : {}),
|
|
488
|
+
roomId: task.roomId,
|
|
489
|
+
root: lease.root,
|
|
490
|
+
botId: task.botId,
|
|
491
|
+
conversationId: task.conversationId,
|
|
492
|
+
sourceConversationId: task.sourceConversationId,
|
|
493
|
+
requestId: task.requestId,
|
|
494
|
+
kind: task.kind,
|
|
495
|
+
});
|
|
161
496
|
},
|
|
162
497
|
});
|
|
163
498
|
taskSignal.addEventListener("abort", stop, { once: true });
|
|
164
499
|
taskSignal.throwIfAborted();
|
|
165
500
|
const result = await owner.executor.run(task);
|
|
501
|
+
await confirmStop();
|
|
166
502
|
taskSignal.throwIfAborted();
|
|
503
|
+
if (task.conversationId) {
|
|
504
|
+
const currentBot = bots.get(task.botId);
|
|
505
|
+
if (!transcriptPath ||
|
|
506
|
+
!binding ||
|
|
507
|
+
!currentBot?.enabled ||
|
|
508
|
+
currentBot.revision !== lease.bot.revision ||
|
|
509
|
+
resolve(currentBot.cwd) !== resolve(lease.root))
|
|
510
|
+
throw new DeviceTaskError("Conversation transcript capture failed");
|
|
511
|
+
try {
|
|
512
|
+
if (!statSync(transcriptPath).isFile())
|
|
513
|
+
throw new Error();
|
|
514
|
+
}
|
|
515
|
+
catch {
|
|
516
|
+
throw new DeviceTaskError("Conversation transcript unavailable after execution");
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
// Commit to durable outbox ONLY after all outcomes and transcripts are verified
|
|
520
|
+
recovery.recordTaskFinish({
|
|
521
|
+
id: task.id,
|
|
522
|
+
attemptId: task.attemptId,
|
|
523
|
+
botId: task.botId,
|
|
524
|
+
deviceId: config.deviceId,
|
|
525
|
+
roomId: task.roomId,
|
|
526
|
+
}, {
|
|
527
|
+
status: "completed",
|
|
528
|
+
report: result,
|
|
529
|
+
reportMetadata: reportUploadMetadata(reportOutbox.get(task) ?? reportOutbox.markMissing(task)),
|
|
530
|
+
});
|
|
167
531
|
return result;
|
|
168
532
|
}
|
|
533
|
+
catch (error) {
|
|
534
|
+
const isUnconfirmed = !!owner.blocked ||
|
|
535
|
+
error instanceof DeviceTaskStopUnconfirmedError ||
|
|
536
|
+
(error && typeof error === "object" && error.stopUnconfirmed === true);
|
|
537
|
+
try {
|
|
538
|
+
recovery.recordTaskFinish({
|
|
539
|
+
id: task.id,
|
|
540
|
+
attemptId: task.attemptId,
|
|
541
|
+
botId: task.botId,
|
|
542
|
+
deviceId: config.deviceId,
|
|
543
|
+
roomId: task.roomId,
|
|
544
|
+
}, {
|
|
545
|
+
status: isUnconfirmed ? "interrupted" : "failed",
|
|
546
|
+
error,
|
|
547
|
+
reportMetadata: reportUploadMetadata(reportOutbox.get(task) ?? reportOutbox.markMissing(task)),
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
catch { }
|
|
551
|
+
throw error;
|
|
552
|
+
}
|
|
169
553
|
finally {
|
|
170
554
|
taskSignal.removeEventListener("abort", stop);
|
|
171
|
-
|
|
555
|
+
let stoppedCleanly = false;
|
|
556
|
+
try {
|
|
557
|
+
await confirmStop();
|
|
558
|
+
stoppedCleanly = true;
|
|
559
|
+
}
|
|
560
|
+
catch { }
|
|
172
561
|
for (const [id, bound] of sessions)
|
|
173
562
|
if (bound.taskId === task.id && bound.roomId === task.roomId)
|
|
174
563
|
sessions.delete(id);
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
564
|
+
if (!owner.blocked && stoppedCleanly) {
|
|
565
|
+
lease?.release();
|
|
566
|
+
if (activeItem)
|
|
567
|
+
scheduler.releaseActive(activeItem);
|
|
568
|
+
active.delete(owner);
|
|
569
|
+
}
|
|
178
570
|
}
|
|
179
571
|
})();
|
|
180
572
|
owner.done = operation;
|
|
@@ -182,19 +574,76 @@ export function createDeviceRuntime(options) {
|
|
|
182
574
|
}
|
|
183
575
|
function onCommand(action, payload, signal) {
|
|
184
576
|
const commandSignal = AbortSignal.any([signal, generation.signal]);
|
|
185
|
-
|
|
577
|
+
if (action === "transfer_fence") {
|
|
578
|
+
return track((async () => {
|
|
579
|
+
enabled(commandSignal);
|
|
580
|
+
await Promise.allSettled(Array.from(operations));
|
|
581
|
+
return { ok: true, settled: true, remaining: 0 };
|
|
582
|
+
})());
|
|
583
|
+
}
|
|
584
|
+
const operation = (async () => {
|
|
186
585
|
enabled(commandSignal);
|
|
187
586
|
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
188
587
|
throw new Error("Invalid transfer command");
|
|
189
588
|
const command = payload;
|
|
190
|
-
if (typeof command.roomId !== "string"
|
|
589
|
+
if (typeof command.roomId !== "string" && !action.startsWith("integration_"))
|
|
191
590
|
throw new Error("Room required");
|
|
192
591
|
if (typeof command.targetBotId !== "string" || !command.targetBotId)
|
|
193
592
|
throw new Error("Target Bot required");
|
|
194
593
|
const lease = await acquire(command.targetBotId, commandSignal);
|
|
195
594
|
try {
|
|
196
|
-
if (
|
|
595
|
+
if (!action.startsWith("integration_")
|
|
596
|
+
? command.targetBotRevision !== lease.bot.revision
|
|
597
|
+
: command.targetBotRevision !== undefined && command.targetBotRevision !== lease.bot.revision)
|
|
197
598
|
throw new Error("Transfer Bot revision mismatch");
|
|
599
|
+
if (action === "integration_prepare") {
|
|
600
|
+
return await devicePrepareIntegration({
|
|
601
|
+
integrationId: command.integrationId,
|
|
602
|
+
targetBotId: lease.bot.id,
|
|
603
|
+
targetBotRevision: lease.bot.revision,
|
|
604
|
+
repoRoot: lease.root,
|
|
605
|
+
dataDir: options.dataDir ?? process.cwd(),
|
|
606
|
+
targetBranch: command.targetBranch,
|
|
607
|
+
commit: command.commit,
|
|
608
|
+
}, { signal: commandSignal });
|
|
609
|
+
}
|
|
610
|
+
if (action === "integration_verify") {
|
|
611
|
+
return await deviceVerifyIntegration({
|
|
612
|
+
integrationId: command.integrationId,
|
|
613
|
+
targetBotId: lease.bot.id,
|
|
614
|
+
targetBotRevision: lease.bot.revision,
|
|
615
|
+
repoRoot: lease.root,
|
|
616
|
+
dataDir: options.dataDir ?? process.cwd(),
|
|
617
|
+
commands: command.commands,
|
|
618
|
+
}, { signal: commandSignal });
|
|
619
|
+
}
|
|
620
|
+
if (action === "integration_confirm") {
|
|
621
|
+
return await deviceConfirmIntegration({
|
|
622
|
+
integrationId: command.integrationId,
|
|
623
|
+
targetBotId: lease.bot.id,
|
|
624
|
+
targetBotRevision: lease.bot.revision,
|
|
625
|
+
repoRoot: lease.root,
|
|
626
|
+
dataDir: options.dataDir ?? process.cwd(),
|
|
627
|
+
deliveryId: command.deliveryId,
|
|
628
|
+
commit: command.commit,
|
|
629
|
+
expectedTargetSha: command.expectedTargetSha,
|
|
630
|
+
}, { signal: commandSignal });
|
|
631
|
+
}
|
|
632
|
+
if (action === "integration_cancel") {
|
|
633
|
+
return await deviceCancelIntegration({
|
|
634
|
+
integrationId: command.integrationId,
|
|
635
|
+
targetBotId: lease.bot.id,
|
|
636
|
+
targetBotRevision: lease.bot.revision,
|
|
637
|
+
repoRoot: lease.root,
|
|
638
|
+
dataDir: options.dataDir ?? process.cwd(),
|
|
639
|
+
}, { signal: commandSignal });
|
|
640
|
+
}
|
|
641
|
+
if (action === "transfer_fence") {
|
|
642
|
+
const snapshot = Array.from(transferOperations);
|
|
643
|
+
await Promise.allSettled(snapshot);
|
|
644
|
+
commandSignal.throwIfAborted();
|
|
645
|
+
return { ok: true };
|
|
646
|
+
}
|
|
198
647
|
if (action === "transfer_stage") {
|
|
199
648
|
const entry = roomFiles(command.roomId, command.targetBotId, lease);
|
|
200
649
|
const preview = await entry.files.stage(command.bundle, { signal: commandSignal });
|
|
@@ -202,49 +651,157 @@ export function createDeviceRuntime(options) {
|
|
|
202
651
|
receipts.set(preview.id, entry);
|
|
203
652
|
return preview;
|
|
204
653
|
}
|
|
654
|
+
if (action === "transfer_init") {
|
|
655
|
+
const entry = roomFiles(command.roomId, command.targetBotId, lease);
|
|
656
|
+
const res = await entry.files.initChunkedTransfer(command.manifest, { signal: commandSignal });
|
|
657
|
+
commandSignal.throwIfAborted();
|
|
658
|
+
receipts.set(res.transferId, entry);
|
|
659
|
+
return res;
|
|
660
|
+
}
|
|
661
|
+
if (action === "transfer_prepare_export") {
|
|
662
|
+
const entry = roomFiles(command.roomId, command.targetBotId, lease);
|
|
663
|
+
return await entry.files.prepareChunked({
|
|
664
|
+
paths: command.paths,
|
|
665
|
+
recursive: command.recursive === true,
|
|
666
|
+
transferId: command.transferId,
|
|
667
|
+
signal: commandSignal,
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
if (action === "transfer_read_chunk") {
|
|
671
|
+
const entry = roomFiles(command.roomId, command.targetBotId, lease);
|
|
672
|
+
const res = await entry.files.readChunk({
|
|
673
|
+
path: command.path,
|
|
674
|
+
offset: command.offset,
|
|
675
|
+
size: command.size,
|
|
676
|
+
expectedFileSize: typeof command.expectedFileSize === "number"
|
|
677
|
+
? command.expectedFileSize
|
|
678
|
+
: typeof command.expectedSize === "number"
|
|
679
|
+
? command.expectedSize
|
|
680
|
+
: undefined,
|
|
681
|
+
expectedChunkSha256: typeof command.expectedChunkSha256 === "string"
|
|
682
|
+
? command.expectedChunkSha256
|
|
683
|
+
: typeof command.expectedSha256 === "string"
|
|
684
|
+
? command.expectedSha256
|
|
685
|
+
: undefined,
|
|
686
|
+
signal: commandSignal,
|
|
687
|
+
});
|
|
688
|
+
return { data: res.buffer.toString("base64"), sha256: res.sha256 };
|
|
689
|
+
}
|
|
205
690
|
const receipt = typeof command.transferId === "string" ? receipts.get(command.transferId) : undefined;
|
|
691
|
+
if (action === "transfer_abort") {
|
|
692
|
+
if (receipt) {
|
|
693
|
+
if (receipt.roomId !== command.roomId ||
|
|
694
|
+
receipt.botId !== command.targetBotId ||
|
|
695
|
+
receipt.revision !== lease.revision) {
|
|
696
|
+
throw new Error("Transfer workspace changed or transfer unavailable");
|
|
697
|
+
}
|
|
698
|
+
receipts.delete(command.transferId);
|
|
699
|
+
const res = await receipt.files.abortChunkedTransfer(command.transferId);
|
|
700
|
+
return { ...res, status: "cancelled" };
|
|
701
|
+
}
|
|
702
|
+
if (typeof command.transferId === "string") {
|
|
703
|
+
const entry = roomFiles(command.roomId, command.targetBotId, lease);
|
|
704
|
+
const res = await entry.files.abortChunkedTransfer(command.transferId);
|
|
705
|
+
return { ...res, status: "cancelled" };
|
|
706
|
+
}
|
|
707
|
+
return { ok: true, status: "cancelled" };
|
|
708
|
+
}
|
|
206
709
|
if (!receipt ||
|
|
207
710
|
receipt.roomId !== command.roomId ||
|
|
208
711
|
receipt.botId !== command.targetBotId ||
|
|
209
712
|
receipt.revision !== lease.revision)
|
|
210
713
|
throw new Error("Transfer workspace changed or transfer unavailable");
|
|
211
|
-
if (action === "
|
|
212
|
-
return await receipt.files.
|
|
714
|
+
if (action === "transfer_chunk")
|
|
715
|
+
return await receipt.files.writeTransferChunk(command.chunk, { signal: commandSignal });
|
|
716
|
+
if (action === "transfer_status")
|
|
717
|
+
return await receipt.files.getTransferStatus(command.transferId);
|
|
718
|
+
if (action === "transfer_preview") {
|
|
719
|
+
try {
|
|
720
|
+
return await receipt.files.previewChunked(command.transferId, { signal: commandSignal });
|
|
721
|
+
}
|
|
722
|
+
catch {
|
|
723
|
+
return await receipt.files.preview(command.transferId, { signal: commandSignal });
|
|
724
|
+
}
|
|
725
|
+
}
|
|
213
726
|
if (action === "transfer_apply" &&
|
|
214
727
|
typeof command.revision === "string" &&
|
|
215
728
|
Array.isArray(command.overwritePaths) &&
|
|
216
|
-
command.overwritePaths.every((path) => typeof path === "string"))
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
729
|
+
command.overwritePaths.every((path) => typeof path === "string")) {
|
|
730
|
+
const decisions = Object.fromEntries(command.overwritePaths.map((path) => [path, "overwrite"]));
|
|
731
|
+
try {
|
|
732
|
+
return await receipt.files.applyChunked({
|
|
733
|
+
transferId: command.transferId,
|
|
734
|
+
revision: command.revision,
|
|
735
|
+
decisions,
|
|
736
|
+
signal: commandSignal,
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
catch {
|
|
740
|
+
return await receipt.files.apply({
|
|
741
|
+
id: command.transferId,
|
|
742
|
+
revision: command.revision,
|
|
743
|
+
decisions,
|
|
744
|
+
signal: commandSignal,
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
}
|
|
223
748
|
throw new Error("Unsupported device command");
|
|
224
749
|
}
|
|
225
750
|
finally {
|
|
226
751
|
lease.release();
|
|
227
752
|
}
|
|
228
|
-
})()
|
|
753
|
+
})();
|
|
754
|
+
if (action.startsWith("transfer_") && action !== "transfer_fence") {
|
|
755
|
+
transferOperations.add(operation);
|
|
756
|
+
void operation.then(() => transferOperations.delete(operation), () => transferOperations.delete(operation));
|
|
757
|
+
}
|
|
758
|
+
return track(operation);
|
|
229
759
|
}
|
|
230
|
-
async function onDisconnect() {
|
|
760
|
+
async function onDisconnect(optionsDisconnect) {
|
|
231
761
|
const previous = generation;
|
|
232
762
|
generation = new AbortController();
|
|
233
|
-
|
|
234
|
-
|
|
763
|
+
if (optionsDisconnect?.abortTasks === true) {
|
|
764
|
+
previous.abort();
|
|
765
|
+
for (const owner of active)
|
|
766
|
+
owner.controller.abort();
|
|
767
|
+
}
|
|
235
768
|
await Promise.allSettled(operations);
|
|
236
769
|
await sweep();
|
|
770
|
+
const blocked = [...active].find((owner) => owner.blocked)?.blocked;
|
|
237
771
|
if (blocked)
|
|
238
772
|
throw blocked;
|
|
239
773
|
}
|
|
240
774
|
return {
|
|
241
775
|
onTask,
|
|
776
|
+
recovery,
|
|
777
|
+
reportOutbox,
|
|
778
|
+
getSchedulerSnapshot() {
|
|
779
|
+
return scheduler.getSnapshot();
|
|
780
|
+
},
|
|
781
|
+
updateExecutionSettings(next) {
|
|
782
|
+
if (options.dataDir) {
|
|
783
|
+
const saved = saveExecutionSettings(options.dataDir, next);
|
|
784
|
+
return scheduler.updateSettings(saved);
|
|
785
|
+
}
|
|
786
|
+
return scheduler.updateSettings(next);
|
|
787
|
+
},
|
|
788
|
+
cancelScheduledTask(taskId, attemptId) {
|
|
789
|
+
return scheduler.cancelTask(taskId, attemptId);
|
|
790
|
+
},
|
|
791
|
+
answerTaskInteraction(taskId, attemptId, answer) {
|
|
792
|
+
const owner = [...active].find((item) => item.taskId === taskId && item.attemptId === attemptId);
|
|
793
|
+
return !!owner && !owner.controller.signal.aborted && !owner.blocked && !!owner.answer?.(answer);
|
|
794
|
+
},
|
|
242
795
|
onCommand,
|
|
243
796
|
collabService,
|
|
244
797
|
onDisconnect,
|
|
245
798
|
async close() {
|
|
246
799
|
closed = true;
|
|
247
|
-
|
|
800
|
+
if (schedulerSaveTimer)
|
|
801
|
+
clearTimeout(schedulerSaveTimer);
|
|
802
|
+
schedulerSaveTimer = undefined;
|
|
803
|
+
flushSchedulerState();
|
|
804
|
+
await onDisconnect({ abortTasks: true });
|
|
248
805
|
await Promise.allSettled([...files.values()].map((entry) => entry.files.close()));
|
|
249
806
|
files.clear();
|
|
250
807
|
receipts.clear();
|