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,75 @@
|
|
|
1
|
+
/** Additional focus for Pi's existing initial/update summary prompts; never replaces their format. */
|
|
2
|
+
export const COMPACTION_RETENTION_INSTRUCTIONS = `Preserve the SDK's default structured checkpoint format. Within its existing sections, always retain the following categories when present; mark missing information as unknown rather than inventing it:
|
|
3
|
+
- Goal: the active user goal, scope, and completion criteria.
|
|
4
|
+
- Constraints & Preferences: explicit user requirements, prohibitions, permissions, and corrections that still apply.
|
|
5
|
+
- Critical Context: verified facts and their evidence (observed outputs, exact paths, symbols, errors, and relevant references). Label hypotheses, inferences, and unverified claims explicitly; never promote them to facts.
|
|
6
|
+
- Progress / Done and In Progress: actual changes, affected files, decisions and rationale, and what remains incomplete. Distinguish proposed work from applied changes.
|
|
7
|
+
- Progress / Critical Context: validation commands, observed results, failures, and checks not run; never infer success from a proposed command or an absent error.
|
|
8
|
+
- Blocked / Next Steps: unresolved questions, blockers, and concrete next actions needed to continue.
|
|
9
|
+
Preserve all context-evidence:<sha256> references verbatim, including references in earlier summaries, with their associated claim and verification status. A reference alone does not prove a claim; do not invent, rewrite, or expand its contents.
|
|
10
|
+
Treat any supplied summary as candidate notes to verify against the conversation, not as authoritative evidence. Condense redundant exploration without dropping the retained categories or still-applicable user constraints.`;
|
|
11
|
+
/** Append without changing caller instructions; safe when both tool and adapter apply it. */
|
|
12
|
+
export function appendCompactionInstructions(instructions) {
|
|
13
|
+
if (instructions?.includes(COMPACTION_RETENTION_INSTRUCTIONS))
|
|
14
|
+
return instructions;
|
|
15
|
+
return instructions ? `${instructions}\n\n${COMPACTION_RETENTION_INSTRUCTIONS}` : COMPACTION_RETENTION_INSTRUCTIONS;
|
|
16
|
+
}
|
|
17
|
+
const installedWrappers = new WeakSet();
|
|
18
|
+
/**
|
|
19
|
+
* Compatibility adapter for Pi's shared manual/auto default summarization path.
|
|
20
|
+
* No public hook currently accepts instructions for automatic compaction:
|
|
21
|
+
* session_before_compact can cancel or supply a finished summary instead.
|
|
22
|
+
* Install once on each new SDK session, before it can compact. False means the
|
|
23
|
+
* private five-argument method is unavailable/incompatible; leave it untouched.
|
|
24
|
+
* Extension-provided summaries bypass this method and are intentionally unaffected.
|
|
25
|
+
* Reinstallation keeps the first installation's callback; install with options upfront.
|
|
26
|
+
*/
|
|
27
|
+
export function installCompactionInstructions(session, options) {
|
|
28
|
+
if (!session || typeof session !== "object")
|
|
29
|
+
return false;
|
|
30
|
+
try {
|
|
31
|
+
// Inspect data descriptors only: do not invoke an unknown SDK accessor.
|
|
32
|
+
let owner = session;
|
|
33
|
+
let descriptor;
|
|
34
|
+
while (owner && !descriptor) {
|
|
35
|
+
descriptor = Object.getOwnPropertyDescriptor(owner, "_runDefaultCompaction");
|
|
36
|
+
if (!descriptor)
|
|
37
|
+
owner = Object.getPrototypeOf(owner);
|
|
38
|
+
}
|
|
39
|
+
const original = descriptor?.value;
|
|
40
|
+
if (typeof original !== "function")
|
|
41
|
+
return false;
|
|
42
|
+
if (installedWrappers.has(original))
|
|
43
|
+
return true;
|
|
44
|
+
if (original.length !== 5)
|
|
45
|
+
return false;
|
|
46
|
+
const getEvidenceIndex = options?.getEvidenceIndex;
|
|
47
|
+
function wrapped(preparation, model, instructions, signal, reason, ...rest) {
|
|
48
|
+
// Unexpected future argument types pass through unchanged.
|
|
49
|
+
const result = original.call(this, preparation, model, typeof instructions === "string" || instructions === undefined
|
|
50
|
+
? appendCompactionInstructions(instructions)
|
|
51
|
+
: instructions, signal, reason, ...rest);
|
|
52
|
+
if (!getEvidenceIndex)
|
|
53
|
+
return result;
|
|
54
|
+
return Promise.resolve(result).then(async (compaction) => {
|
|
55
|
+
if (!compaction || typeof compaction !== "object" || !("summary" in compaction))
|
|
56
|
+
return compaction;
|
|
57
|
+
if (typeof compaction.summary !== "string")
|
|
58
|
+
return compaction;
|
|
59
|
+
const index = await getEvidenceIndex();
|
|
60
|
+
if (!index.trim())
|
|
61
|
+
return compaction;
|
|
62
|
+
return { ...compaction, summary: `${compaction.summary}\n\n${index}` };
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
Object.defineProperty(session, "_runDefaultCompaction", {
|
|
66
|
+
...(owner === session ? descriptor : { configurable: true, writable: true, enumerable: false }),
|
|
67
|
+
value: wrapped,
|
|
68
|
+
});
|
|
69
|
+
installedWrappers.add(wrapped);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -261,13 +261,16 @@ export function repairSessionTranscript(raw) {
|
|
|
261
261
|
for (const s of live)
|
|
262
262
|
if (s.finalId !== null)
|
|
263
263
|
byId.set(s.finalId, s);
|
|
264
|
+
// 每条已完成路径均已到根、悬空引用或已截断的环,后续无需重复回溯。
|
|
265
|
+
// 保留 live 的遍历顺序与路径内的重复检测,确保断环位置与原实现一致。
|
|
266
|
+
const completed = new Set();
|
|
264
267
|
for (const s of live) {
|
|
265
|
-
if (!s.entry)
|
|
268
|
+
if (!s.entry || completed.has(s.finalId))
|
|
266
269
|
continue;
|
|
267
270
|
const seen = new Set([s.finalId]);
|
|
268
271
|
let prev = s;
|
|
269
272
|
let cur = parentIdOf(s.entry);
|
|
270
|
-
while (cur !== null) {
|
|
273
|
+
while (cur !== null && !completed.has(cur)) {
|
|
271
274
|
if (seen.has(cur)) {
|
|
272
275
|
prev.entry.parentId = null;
|
|
273
276
|
prev.dirty = true;
|
|
@@ -281,6 +284,8 @@ export function repairSessionTranscript(raw) {
|
|
|
281
284
|
prev = next;
|
|
282
285
|
cur = parentIdOf(next.entry);
|
|
283
286
|
}
|
|
287
|
+
for (const id of seen)
|
|
288
|
+
completed.add(id);
|
|
284
289
|
}
|
|
285
290
|
}
|
|
286
291
|
// 第 4 步:分支拓扑归置(issue #567)。
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-owned compaction recovery state. Never reads or rewrites SDK transcripts.
|
|
3
|
+
*
|
|
4
|
+
* Integration:
|
|
5
|
+
* - Capture the session file at compaction_start; pass undefined for ephemeral or
|
|
6
|
+
* !sessionManager.isPersisted() sessions, even if the SDK exposes a file name.
|
|
7
|
+
* - mark(file, { reason, startedAt }) must succeed before claiming durable state.
|
|
8
|
+
* - On compaction_end, append COMPACTION_DONE_TYPE via the captured session's
|
|
9
|
+
* SessionManager.appendCustomEntry(), then finish(file). This module does not
|
|
10
|
+
* append SDK entries. Keep the old compaction-markers repair for old files.
|
|
11
|
+
* - Call consumeInterrupted(file) only when restoring a session, before starting
|
|
12
|
+
* new work. Emit the interruption notice only when it returns a state.
|
|
13
|
+
*
|
|
14
|
+
* All operations are synchronous within the host's single-writer data directory.
|
|
15
|
+
* I/O and invalid-state errors propagate; callers must report persistence errors.
|
|
16
|
+
* A successful consume removes the state before returning (at most one notice).
|
|
17
|
+
*/
|
|
18
|
+
import { readFileSync, rmSync } from "node:fs";
|
|
19
|
+
import { writeJsonAtomicSync } from "./atomic-file.js";
|
|
20
|
+
function sidecar(file) {
|
|
21
|
+
return file ? `${file}.compaction-state.json` : undefined;
|
|
22
|
+
}
|
|
23
|
+
function isState(value) {
|
|
24
|
+
if (typeof value !== "object" || value === null)
|
|
25
|
+
return false;
|
|
26
|
+
const state = value;
|
|
27
|
+
return (typeof state.reason === "string" &&
|
|
28
|
+
state.reason.length > 0 &&
|
|
29
|
+
typeof state.startedAt === "number" &&
|
|
30
|
+
Number.isFinite(state.startedAt) &&
|
|
31
|
+
state.startedAt >= 0);
|
|
32
|
+
}
|
|
33
|
+
/** Atomically persist pending state; false means this session has no durable file. */
|
|
34
|
+
export function mark(file, state) {
|
|
35
|
+
const path = sidecar(file);
|
|
36
|
+
if (!path)
|
|
37
|
+
return false;
|
|
38
|
+
if (!isState(state))
|
|
39
|
+
throw new Error("Invalid compaction state");
|
|
40
|
+
writeJsonAtomicSync(path, {
|
|
41
|
+
version: 1,
|
|
42
|
+
reason: state.reason,
|
|
43
|
+
startedAt: state.startedAt,
|
|
44
|
+
});
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
/** Clear pending state after the caller records the outcome through the SDK. */
|
|
48
|
+
export function finish(file) {
|
|
49
|
+
const path = sidecar(file);
|
|
50
|
+
if (!path)
|
|
51
|
+
return false;
|
|
52
|
+
try {
|
|
53
|
+
rmSync(path);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (error.code === "ENOENT")
|
|
58
|
+
return false;
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Consume once on restore. A failed read/delete never reports successful recovery. */
|
|
63
|
+
export function consumeInterrupted(file) {
|
|
64
|
+
const path = sidecar(file);
|
|
65
|
+
if (!path)
|
|
66
|
+
return undefined;
|
|
67
|
+
let raw;
|
|
68
|
+
try {
|
|
69
|
+
raw = readFileSync(path, "utf8");
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (error.code === "ENOENT")
|
|
73
|
+
return undefined;
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
const state = JSON.parse(raw);
|
|
77
|
+
if (!isState(state) || state.version !== 1) {
|
|
78
|
+
throw new Error(`Invalid compaction state: ${path}`);
|
|
79
|
+
}
|
|
80
|
+
// Do not return a notice if another consumer has already removed the state.
|
|
81
|
+
if (!finish(file))
|
|
82
|
+
return undefined;
|
|
83
|
+
return { reason: state.reason, startedAt: state.startedAt };
|
|
84
|
+
}
|
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
* 不依赖转录文件落盘(SDK 的 _persist 门控要等首轮 assistant 才写盘)。
|
|
10
10
|
* - 每会话只留最新一条(last-write-wins,按 ts,比 marker-store 的扫描模式更轻)。
|
|
11
11
|
* - 与 per-client 的 client-state.json 不同:按 sessionId 全局存,跨标签页可见。
|
|
12
|
-
* -
|
|
12
|
+
* - 同文件实例共享状态;写盘成功后提交内存,失败抛给调用者反馈。
|
|
13
13
|
*/
|
|
14
14
|
import { readFileSync, renameSync } from "node:fs";
|
|
15
|
-
import { basename } from "node:path";
|
|
15
|
+
import { basename, resolve } from "node:path";
|
|
16
16
|
import { writeJsonAtomicSync } from "./atomic-file.js";
|
|
17
17
|
/** 单条草稿上限字符数:超出截断再存(快照全量下发时携带,太大浪费流量)。 */
|
|
18
18
|
export const DRAFT_TEXT_MAX = 20_000;
|
|
@@ -42,38 +42,45 @@ function isValidDraft(d) {
|
|
|
42
42
|
const o = d;
|
|
43
43
|
return typeof o.text === "string" && typeof o.ts === "number" && typeof o.updatedAt === "number";
|
|
44
44
|
}
|
|
45
|
-
|
|
46
|
-
* 模块级(非实例级):同进程每个 ClientSession 各有一个 ComposerDraftsStore
|
|
47
|
-
* 实例(agent-service.ts:1922),跨 tab 的 prompt-clear 与陈旧写落在不同
|
|
48
|
-
* 实例上,水位必须跨实例可见。进程重启即失 —— 重启后文件态本身已干净
|
|
49
|
-
* (clear 落盘删 key),late-save 无处可达。
|
|
50
|
-
* ponytail: 内存水位,TTL/持久化按需加(当前重启语义已够用)。 */
|
|
51
|
-
const clearedAt = new Map();
|
|
45
|
+
const draftFiles = new Map();
|
|
52
46
|
export class ComposerDraftsStore {
|
|
53
47
|
filePath;
|
|
54
|
-
|
|
48
|
+
state;
|
|
55
49
|
constructor(filePath) {
|
|
56
|
-
this.filePath = filePath;
|
|
50
|
+
this.filePath = resolve(filePath);
|
|
51
|
+
const key = process.platform === "win32" ? this.filePath.toLowerCase() : this.filePath;
|
|
52
|
+
let state = draftFiles.get(key);
|
|
53
|
+
if (!state) {
|
|
54
|
+
state = { cache: null, clearedAt: new Map() };
|
|
55
|
+
draftFiles.set(key, state);
|
|
56
|
+
}
|
|
57
|
+
this.state = state;
|
|
57
58
|
}
|
|
58
59
|
load() {
|
|
59
|
-
if (this.cache)
|
|
60
|
-
return this.cache;
|
|
60
|
+
if (this.state.cache)
|
|
61
|
+
return this.state.cache;
|
|
61
62
|
let raw = {};
|
|
62
63
|
try {
|
|
63
64
|
raw = JSON.parse(readFileSync(this.filePath, "utf8"));
|
|
64
65
|
}
|
|
65
|
-
catch {
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (!(error instanceof SyntaxError) && error.code !== "ENOENT")
|
|
68
|
+
throw error;
|
|
66
69
|
// 坏文件改名留存而非直接覆盖:先有留存,后续 save 落盘的才是新数据
|
|
67
70
|
//(否则一次解析失败 → 空表 → 下次 save 把全部草稿的原始记录抹掉)。
|
|
68
71
|
try {
|
|
69
72
|
renameSync(this.filePath, `${this.filePath}.corrupt-${Date.now()}`);
|
|
70
73
|
console.warn(`[composer-drafts] 草稿文件解析失败,已改名留存:${this.filePath}(丢草稿可重打,不留坏副本)`);
|
|
71
74
|
}
|
|
72
|
-
catch {
|
|
73
|
-
//
|
|
75
|
+
catch (renameError) {
|
|
76
|
+
// Never overwrite a corrupt original unless its recovery copy is secured.
|
|
77
|
+
if (error.code !== "ENOENT")
|
|
78
|
+
throw renameError;
|
|
74
79
|
}
|
|
75
80
|
raw = {};
|
|
76
81
|
}
|
|
82
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
83
|
+
throw new Error("Invalid composer drafts file");
|
|
77
84
|
const now = Date.now();
|
|
78
85
|
const kept = {};
|
|
79
86
|
let pruned = false;
|
|
@@ -88,21 +95,16 @@ export class ComposerDraftsStore {
|
|
|
88
95
|
}
|
|
89
96
|
kept[k] = v;
|
|
90
97
|
}
|
|
91
|
-
this.cache = kept;
|
|
92
98
|
// 读到过期/脏条目才回写:正常加载不碰磁盘。
|
|
93
99
|
if (pruned)
|
|
94
|
-
this.persist();
|
|
95
|
-
|
|
100
|
+
this.persist(kept);
|
|
101
|
+
else
|
|
102
|
+
this.state.cache = kept;
|
|
103
|
+
return kept;
|
|
96
104
|
}
|
|
97
|
-
persist() {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
writeJsonAtomicSync(this.filePath, this.cache);
|
|
102
|
-
}
|
|
103
|
-
catch {
|
|
104
|
-
// best-effort:草稿丢了可以重打,server 绝不能因此崩。
|
|
105
|
-
}
|
|
105
|
+
persist(next) {
|
|
106
|
+
writeJsonAtomicSync(this.filePath, next);
|
|
107
|
+
this.state.cache = next;
|
|
106
108
|
}
|
|
107
109
|
/** 读一条草稿({text, ts};没有返回 undefined)。 */
|
|
108
110
|
get(sessionId) {
|
|
@@ -117,42 +119,43 @@ export class ComposerDraftsStore {
|
|
|
117
119
|
* (防抖延迟 / 跨 tab 陈旧写,ts <= clearTs)直接丢弃,不复活存储
|
|
118
120
|
* (reload-resurrect,见 clearedAt)。*/
|
|
119
121
|
save(sessionId, text, ts) {
|
|
120
|
-
if (!sessionId)
|
|
122
|
+
if (!sessionId || !Number.isFinite(ts))
|
|
121
123
|
return;
|
|
122
|
-
const cut = clearedAt.get(sessionId);
|
|
124
|
+
const cut = this.state.clearedAt.get(sessionId);
|
|
123
125
|
if (cut !== undefined && ts <= cut)
|
|
124
126
|
return;
|
|
125
|
-
const map = this.load();
|
|
127
|
+
const map = { ...this.load() };
|
|
128
|
+
const prev = map[sessionId];
|
|
129
|
+
if (prev && prev.ts > ts)
|
|
130
|
+
return;
|
|
126
131
|
const norm = normalizeDraftText(text);
|
|
127
132
|
if (norm === undefined) {
|
|
128
133
|
if (map[sessionId] !== undefined) {
|
|
129
134
|
delete map[sessionId];
|
|
130
|
-
this.persist();
|
|
135
|
+
this.persist(map);
|
|
131
136
|
}
|
|
137
|
+
this.state.clearedAt.set(sessionId, ts);
|
|
132
138
|
return;
|
|
133
139
|
}
|
|
134
|
-
const prev = map[sessionId];
|
|
135
|
-
if (prev && prev.ts > ts)
|
|
136
|
-
return;
|
|
137
140
|
if (prev && prev.ts === ts && prev.text === norm)
|
|
138
141
|
return;
|
|
139
142
|
map[sessionId] = { text: norm, ts, updatedAt: Date.now() };
|
|
140
|
-
this.persist();
|
|
143
|
+
this.persist(map);
|
|
141
144
|
}
|
|
142
145
|
/** 发送成功 / 会话删除后清掉(有 key 才写盘)。同时记录 clear 时间戳
|
|
143
146
|
* (max(旧条目 ts, Date.now())):之后 ts <= 该水位的 save 一律丢弃,
|
|
144
147
|
* 拦住 prompt() 清掉之后才 landing 的旧 `draft_update`。
|
|
145
|
-
*
|
|
148
|
+
* 同文件共享:同进程多 ClientSession(多 tab)共用同一水位。 */
|
|
146
149
|
clear(sessionId) {
|
|
147
150
|
if (!sessionId)
|
|
148
151
|
return;
|
|
149
|
-
const map = this.load();
|
|
152
|
+
const map = { ...this.load() };
|
|
150
153
|
const prevTs = map[sessionId]?.ts ?? 0;
|
|
151
|
-
clearedAt.set(sessionId, Math.max(prevTs, Date.now()));
|
|
152
154
|
if (map[sessionId] !== undefined) {
|
|
153
155
|
delete map[sessionId];
|
|
154
|
-
this.persist();
|
|
156
|
+
this.persist(map);
|
|
155
157
|
}
|
|
158
|
+
this.state.clearedAt.set(sessionId, Math.max(this.state.clearedAt.get(sessionId) ?? 0, prevTs, Date.now()));
|
|
156
159
|
}
|
|
157
160
|
/** delete_session 用:按转录文件路径反解 sessionId 并清掉(形状对不上就跳过)。 */
|
|
158
161
|
pruneSessionFile(sessionPath) {
|