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
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
/** Pure deterministic chunk extractor for long reports/journals; never silences or drops boundaries. */
|
|
3
|
+
export function queryTaskReportChunk(taskId, reportText, offset = 0, limit = 8192, options) {
|
|
4
|
+
if (!Number.isSafeInteger(offset) || offset < 0) {
|
|
5
|
+
throw new RangeError("Offset must be a nonnegative safe integer");
|
|
6
|
+
}
|
|
7
|
+
if (!Number.isSafeInteger(limit) || limit < 1) {
|
|
8
|
+
throw new RangeError("Limit must be a positive safe integer");
|
|
9
|
+
}
|
|
10
|
+
const buf = Buffer.from(reportText, "utf8");
|
|
11
|
+
const totalBytes = buf.length;
|
|
12
|
+
let actualOffset = Math.min(offset, totalBytes);
|
|
13
|
+
// If actualOffset falls on a UTF-8 continuation byte, align backward to the code point start.
|
|
14
|
+
while (actualOffset > 0 && (buf[actualOffset] & 0xc0) === 0x80) {
|
|
15
|
+
actualOffset--;
|
|
16
|
+
}
|
|
17
|
+
const safeLimit = Math.max(1, Math.min(limit, 65536));
|
|
18
|
+
let end = Math.min(actualOffset + safeLimit, totalBytes);
|
|
19
|
+
// If end falls on a continuation byte, shrink backward to prevent exceeding safeLimit.
|
|
20
|
+
while (end > actualOffset && (buf[end] & 0xc0) === 0x80) {
|
|
21
|
+
end--;
|
|
22
|
+
}
|
|
23
|
+
// Fallback for single multi-byte code point exceeding safeLimit: advance to include it.
|
|
24
|
+
if (end === actualOffset && actualOffset < totalBytes) {
|
|
25
|
+
end++;
|
|
26
|
+
while (end < totalBytes && (buf[end] & 0xc0) === 0x80) {
|
|
27
|
+
end++;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const slice = buf.subarray(actualOffset, end);
|
|
31
|
+
const chunk = slice.toString("utf8");
|
|
32
|
+
const nextOffset = end < totalBytes ? end : null;
|
|
33
|
+
const sha256 = createHash("sha256").update(buf).digest("hex");
|
|
34
|
+
const digest = createHash("sha256")
|
|
35
|
+
.update(JSON.stringify([taskId, options?.attemptId ?? "", options?.revision ?? 0, totalBytes]))
|
|
36
|
+
.update(buf)
|
|
37
|
+
.digest("hex");
|
|
38
|
+
const isExcerpt = options?.source === "report_excerpt" || reportText.includes("[Report excerpt:") || totalBytes > safeLimit;
|
|
39
|
+
const complete = options?.complete ?? (!isExcerpt && nextOffset === null);
|
|
40
|
+
return {
|
|
41
|
+
taskId,
|
|
42
|
+
...(options?.attemptId ? { attemptId: options.attemptId } : {}),
|
|
43
|
+
...(options?.revision !== undefined ? { revision: options.revision } : {}),
|
|
44
|
+
totalBytes,
|
|
45
|
+
offset: actualOffset,
|
|
46
|
+
limit: safeLimit,
|
|
47
|
+
chunk,
|
|
48
|
+
hasMore: nextOffset !== null,
|
|
49
|
+
nextOffset,
|
|
50
|
+
isExcerpt,
|
|
51
|
+
complete,
|
|
52
|
+
source: options?.source ?? (isExcerpt ? "report_excerpt" : "full_report"),
|
|
53
|
+
...(options?.reportRef ? { reportRef: options.reportRef } : {}),
|
|
54
|
+
digest,
|
|
55
|
+
sha256,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** Deterministic aggregation of task state without calling any external LLM summary. */
|
|
59
|
+
export function buildDeterministicTaskSummary(taskState, options) {
|
|
60
|
+
const constraints = [];
|
|
61
|
+
const maxBytes = options?.maxConstraintBytes ?? 2000;
|
|
62
|
+
if (options?.userConstraints) {
|
|
63
|
+
options.userConstraints.forEach((rawText, idx) => {
|
|
64
|
+
if (typeof rawText === "string" && rawText.trim()) {
|
|
65
|
+
const trimmed = rawText.trim();
|
|
66
|
+
const bytes = Buffer.byteLength(trimmed, "utf8");
|
|
67
|
+
const ref = `user_instruction:${idx}`;
|
|
68
|
+
if (bytes > maxBytes) {
|
|
69
|
+
const boundedText = Buffer.from(trimmed, "utf8").subarray(0, maxBytes).toString("utf8");
|
|
70
|
+
constraints.push({
|
|
71
|
+
text: boundedText,
|
|
72
|
+
provenance: "user_instruction",
|
|
73
|
+
ref,
|
|
74
|
+
truncated: true,
|
|
75
|
+
originalBytes: bytes,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
constraints.push({
|
|
80
|
+
text: trimmed,
|
|
81
|
+
provenance: "user_instruction",
|
|
82
|
+
ref,
|
|
83
|
+
truncated: false,
|
|
84
|
+
originalBytes: bytes,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (taskState.reported?.constraints) {
|
|
91
|
+
taskState.reported.constraints.forEach((rawText, idx) => {
|
|
92
|
+
if (typeof rawText === "string" && rawText.trim()) {
|
|
93
|
+
const trimmed = rawText.trim();
|
|
94
|
+
const bytes = Buffer.byteLength(trimmed, "utf8");
|
|
95
|
+
const ref = `bot_reported:${taskState.reported.botId}:${idx}`;
|
|
96
|
+
if (bytes > maxBytes) {
|
|
97
|
+
const boundedText = Buffer.from(trimmed, "utf8").subarray(0, maxBytes).toString("utf8");
|
|
98
|
+
constraints.push({
|
|
99
|
+
text: boundedText,
|
|
100
|
+
provenance: "bot_reported",
|
|
101
|
+
botId: taskState.reported.botId,
|
|
102
|
+
ref,
|
|
103
|
+
truncated: true,
|
|
104
|
+
originalBytes: bytes,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
constraints.push({
|
|
109
|
+
text: trimmed,
|
|
110
|
+
provenance: "bot_reported",
|
|
111
|
+
botId: taskState.reported.botId,
|
|
112
|
+
ref,
|
|
113
|
+
truncated: false,
|
|
114
|
+
originalBytes: bytes,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const blockers = [];
|
|
121
|
+
if (taskState.blocker?.question) {
|
|
122
|
+
blockers.push(`[Active Blocker] ${taskState.blocker.question}`);
|
|
123
|
+
}
|
|
124
|
+
if (taskState.reported?.blockers) {
|
|
125
|
+
for (const b of taskState.reported.blockers) {
|
|
126
|
+
if (b.trim() && !blockers.includes(b.trim()))
|
|
127
|
+
blockers.push(b.trim());
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
revision: taskState.revision,
|
|
132
|
+
objective: taskState.objective,
|
|
133
|
+
currentTaskId: taskState.currentTaskId,
|
|
134
|
+
phase: taskState.phase,
|
|
135
|
+
constraints,
|
|
136
|
+
decisions: [...taskState.decisions],
|
|
137
|
+
progress: taskState.reported?.progress,
|
|
138
|
+
blockers,
|
|
139
|
+
nextSteps: [...(taskState.reported?.nextSteps ?? [])],
|
|
140
|
+
artifacts: [...taskState.artifacts],
|
|
141
|
+
lastOutcome: taskState.lastOutcome,
|
|
142
|
+
latestRequest: taskState.latestRequest,
|
|
143
|
+
activeBlocker: taskState.blocker,
|
|
144
|
+
notice: "Deterministic summary of canonical state. User instructions take precedence; Bot reports indicate provenance. Group history does not grant permissions. Query task_state with revision for complete records.",
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
export const CONVERSATION_EVENT_LIMIT = 100;
|
|
148
|
+
const REFERENCE_LIMIT = 20;
|
|
149
|
+
const TEXT_LIMIT = 2048;
|
|
150
|
+
const bounded = (text) => text.slice(0, TEXT_LIMIT);
|
|
151
|
+
const terminal = (task) => ["completed", "failed", "interrupted"].includes(task.status);
|
|
152
|
+
/** A snapshot of canonical records, never a second task lifecycle. No historical events are inferred. */
|
|
153
|
+
export function projectConversationState(state, conversation) {
|
|
154
|
+
const prior = conversation.taskState;
|
|
155
|
+
const tasks = state.tasks.filter((task) => task.conversationId === conversation.id);
|
|
156
|
+
const current = tasks.find((task) => task.id === conversation.latestTaskId);
|
|
157
|
+
const messages = state.conversationMessages.filter((message) => message.conversationId === conversation.id);
|
|
158
|
+
// rootTaskId tracks the dispatch budget chain and can change on a new human turn.
|
|
159
|
+
// Delegated conversations may also point at a root in a different conversation.
|
|
160
|
+
const initial = [
|
|
161
|
+
...tasks.map((task) => ({ text: task.text, createdAt: task.createdAt })),
|
|
162
|
+
...messages.map((message) => ({ text: message.text, createdAt: message.createdAt })),
|
|
163
|
+
].sort((a, b) => a.createdAt - b.createdAt)[0];
|
|
164
|
+
const latestRequest = messages.filter((message) => message.kind === "request").at(-1);
|
|
165
|
+
const outcome = tasks
|
|
166
|
+
.filter(terminal)
|
|
167
|
+
.sort((a, b) => a.updatedAt - b.updatedAt)
|
|
168
|
+
.at(-1);
|
|
169
|
+
const artifacts = new Map((prior?.artifacts ?? []).map((ref) => [ref.deliveryId, ref]));
|
|
170
|
+
for (const delivery of state.deliveries)
|
|
171
|
+
if (delivery.conversationId === conversation.id) {
|
|
172
|
+
artifacts.delete(delivery.id);
|
|
173
|
+
artifacts.set(delivery.id, { deliveryId: delivery.id, taskId: delivery.taskId });
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
revision: prior?.revision ?? 0,
|
|
177
|
+
objective: bounded(prior?.objective ?? initial?.text ?? ""),
|
|
178
|
+
currentTaskId: conversation.latestTaskId,
|
|
179
|
+
phase: conversation.closed
|
|
180
|
+
? "closed"
|
|
181
|
+
: conversation.attention
|
|
182
|
+
? "waiting"
|
|
183
|
+
: (current?.status ??
|
|
184
|
+
(prior?.currentTaskId === conversation.latestTaskId ? prior.phase : undefined) ??
|
|
185
|
+
"interrupted"),
|
|
186
|
+
...(latestRequest
|
|
187
|
+
? {
|
|
188
|
+
latestRequest: {
|
|
189
|
+
taskId: latestRequest.taskId,
|
|
190
|
+
sourceId: latestRequest.id,
|
|
191
|
+
text: bounded(latestRequest.text),
|
|
192
|
+
},
|
|
193
|
+
}
|
|
194
|
+
: prior?.latestRequest
|
|
195
|
+
? { latestRequest: prior.latestRequest }
|
|
196
|
+
: {}),
|
|
197
|
+
...(conversation.attention && !conversation.closed
|
|
198
|
+
? {
|
|
199
|
+
blocker: {
|
|
200
|
+
attentionId: conversation.attention.id,
|
|
201
|
+
taskId: conversation.attention.taskId,
|
|
202
|
+
question: bounded(conversation.attention.question),
|
|
203
|
+
},
|
|
204
|
+
}
|
|
205
|
+
: {}),
|
|
206
|
+
...(outcome
|
|
207
|
+
? {
|
|
208
|
+
lastOutcome: {
|
|
209
|
+
taskId: outcome.id,
|
|
210
|
+
status: outcome.status,
|
|
211
|
+
...(outcome.report ? { text: bounded(outcome.report) } : {}),
|
|
212
|
+
},
|
|
213
|
+
}
|
|
214
|
+
: prior?.lastOutcome
|
|
215
|
+
? { lastOutcome: prior.lastOutcome }
|
|
216
|
+
: {}),
|
|
217
|
+
decisions: (prior?.decisions ?? []).slice(-REFERENCE_LIMIT),
|
|
218
|
+
artifacts: [...artifacts.values()].slice(-REFERENCE_LIMIT),
|
|
219
|
+
...(prior?.reported ? { reported: prior.reported } : {}),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/** Run after every canonical mutation and before persistence, including restart recovery. */
|
|
223
|
+
export function reconcileConversationState(previous, next) {
|
|
224
|
+
const oldConversations = new Map(previous.conversations.map((conversation) => [conversation.id, conversation]));
|
|
225
|
+
const oldMessages = new Set(previous.conversationMessages.map((message) => message.id));
|
|
226
|
+
const oldTasks = new Map(previous.tasks.map((task) => [task.id, task]));
|
|
227
|
+
const oldDeliveries = new Set(previous.deliveries.map((delivery) => delivery.id));
|
|
228
|
+
const oldReviews = new Set(previous.reviews.map((review) => review.id));
|
|
229
|
+
for (const conversation of next.conversations) {
|
|
230
|
+
const before = oldConversations.get(conversation.id);
|
|
231
|
+
// Only reported fields may be explicitly changed; the rest is always derived here.
|
|
232
|
+
const reported = conversation.taskState?.reported;
|
|
233
|
+
conversation.taskState = before?.taskState;
|
|
234
|
+
const projected = projectConversationState(next, conversation);
|
|
235
|
+
if (reported)
|
|
236
|
+
projected.reported = structuredClone(reported);
|
|
237
|
+
conversation.taskState = projected;
|
|
238
|
+
const events = structuredClone(before?.events ?? []);
|
|
239
|
+
let sequence = Math.max(before?.taskState?.revision ?? 0, events.at(-1)?.sequence ?? 0);
|
|
240
|
+
const append = (type, sourceId, createdAt, taskId, text, discriminator = "") => {
|
|
241
|
+
const id = createHash("sha256")
|
|
242
|
+
.update(JSON.stringify([conversation.id, type, sourceId, discriminator]))
|
|
243
|
+
.digest("hex");
|
|
244
|
+
if (events.some((event) => event.id === id))
|
|
245
|
+
return;
|
|
246
|
+
events.push({
|
|
247
|
+
id,
|
|
248
|
+
sequence: ++sequence,
|
|
249
|
+
type,
|
|
250
|
+
sourceId,
|
|
251
|
+
createdAt,
|
|
252
|
+
...(taskId ? { taskId } : {}),
|
|
253
|
+
...(text !== undefined ? { text: bounded(text) } : {}),
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
const newMessages = next.conversationMessages.filter((message) => message.conversationId === conversation.id && !oldMessages.has(message.id));
|
|
257
|
+
for (const message of newMessages) {
|
|
258
|
+
// Own execution reports are represented by the canonical task status transition.
|
|
259
|
+
const task = next.tasks.find((task) => task.id === message.taskId);
|
|
260
|
+
if (message.kind === "result" && (task?.kind !== "result" || message.sourceBotId === task.botId))
|
|
261
|
+
continue;
|
|
262
|
+
const type = message.kind === "request"
|
|
263
|
+
? message.sourceBotId
|
|
264
|
+
? "bot_request"
|
|
265
|
+
: "user_message"
|
|
266
|
+
: message.kind === "reply"
|
|
267
|
+
? "bot_reply"
|
|
268
|
+
: message.kind === "result"
|
|
269
|
+
? "worker_result"
|
|
270
|
+
: message.sourceBotId
|
|
271
|
+
? "bot_request"
|
|
272
|
+
: "user_message";
|
|
273
|
+
append(type, message.id, message.createdAt, message.taskId, message.text);
|
|
274
|
+
}
|
|
275
|
+
for (const task of next.tasks) {
|
|
276
|
+
if (task.conversationId !== conversation.id || task.status === "queued")
|
|
277
|
+
continue;
|
|
278
|
+
const old = oldTasks.get(task.id);
|
|
279
|
+
if (old?.status === task.status && old.attemptId === task.attemptId)
|
|
280
|
+
continue;
|
|
281
|
+
const type = task.status === "running"
|
|
282
|
+
? "execution_started"
|
|
283
|
+
: task.status === "completed"
|
|
284
|
+
? "execution_completed"
|
|
285
|
+
: task.status === "failed"
|
|
286
|
+
? "execution_failed"
|
|
287
|
+
: "execution_interrupted";
|
|
288
|
+
append(type, task.id, task.updatedAt, task.id, task.report, task.attemptId);
|
|
289
|
+
}
|
|
290
|
+
if (conversation.attention && conversation.attention.id !== before?.attention?.id)
|
|
291
|
+
append("attention_requested", conversation.attention.id, conversation.attention.createdAt, conversation.attention.taskId, conversation.attention.question);
|
|
292
|
+
if (before?.attention && before.attention.id !== conversation.attention?.id) {
|
|
293
|
+
const attention = before.attention;
|
|
294
|
+
const humans = newMessages.filter((message) => !message.sourceBotId && message.kind === "message" && message.createdAt >= attention.createdAt);
|
|
295
|
+
// Ambiguous removal (close/cancel/replacement) must never invent a human decision.
|
|
296
|
+
const answer = !conversation.closed && !conversation.attention && humans.length === 1 ? humans[0] : undefined;
|
|
297
|
+
append("attention_resolved", attention.id, answer?.createdAt ?? conversation.updatedAt, answer?.taskId ?? attention.taskId);
|
|
298
|
+
if (answer && attention.kind === "decision")
|
|
299
|
+
projected.decisions = [
|
|
300
|
+
...projected.decisions,
|
|
301
|
+
{
|
|
302
|
+
attentionId: attention.id,
|
|
303
|
+
messageId: answer.id,
|
|
304
|
+
taskId: answer.taskId,
|
|
305
|
+
question: attention.question.slice(0, 2000),
|
|
306
|
+
answer: answer.text.slice(0, 2000),
|
|
307
|
+
...(attention.question.length > 2000 ? { questionTruncated: true } : {}),
|
|
308
|
+
...(answer.text.length > 2000 ? { answerTruncated: true } : {}),
|
|
309
|
+
},
|
|
310
|
+
].slice(-REFERENCE_LIMIT);
|
|
311
|
+
}
|
|
312
|
+
for (const delivery of next.deliveries)
|
|
313
|
+
if (delivery.conversationId === conversation.id && !oldDeliveries.has(delivery.id))
|
|
314
|
+
append("delivery_recorded", delivery.id, delivery.createdAt, delivery.taskId, delivery.summary);
|
|
315
|
+
for (const review of next.reviews)
|
|
316
|
+
if (review.conversationId === conversation.id && !oldReviews.has(review.id))
|
|
317
|
+
append("review_recorded", review.id, review.createdAt, next.deliveries.find((delivery) => delivery.id === review.deliveryId)?.taskId, review.summary);
|
|
318
|
+
if (conversation.closed && !before?.closed)
|
|
319
|
+
append("conversation_closed", conversation.id, conversation.updatedAt, conversation.latestTaskId);
|
|
320
|
+
if (reported && JSON.stringify(reported) !== JSON.stringify(before?.taskState?.reported))
|
|
321
|
+
append("task_updated", reported.taskId, reported.updatedAt, reported.taskId, reported.progress, JSON.stringify([sequence + 1, reported]));
|
|
322
|
+
projected.revision = sequence;
|
|
323
|
+
conversation.events = events.slice(-CONVERSATION_EVENT_LIMIT);
|
|
324
|
+
let bytes = 2;
|
|
325
|
+
for (let index = conversation.events.length - 1; index >= 0; index--) {
|
|
326
|
+
bytes += Buffer.byteLength(JSON.stringify(conversation.events[index])) + 1;
|
|
327
|
+
if (bytes > 64 * 1024) {
|
|
328
|
+
conversation.events = conversation.events.slice(index + 1);
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|