pi-web-ui 0.114.2 → 0.115.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 +55 -1
- package/bin/pi-web-ui.mjs +69 -7
- package/dist/server/agent-service-daemon.js +440 -32
- package/dist/server/agent-service.js +878 -383
- package/dist/server/canvas/canvas-dispatch.js +13 -1
- package/dist/server/client-state.js +25 -0
- package/dist/server/communicate-tool.js +2 -0
- package/dist/server/context-usage.js +105 -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 +72 -9
- package/dist/server/device-center/bot-presets.js +46 -0
- package/dist/server/device-center/collab-service.js +328 -10
- package/dist/server/device-center/collab-tool.js +738 -12
- 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 +19 -0
- package/dist/server/device-center/execution-settings.js +100 -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 +389 -40
- package/dist/server/device-center/file-sync-paths.js +171 -0
- package/dist/server/device-center/file-sync.js +180 -91
- package/dist/server/device-center/group-context-delta.js +37 -0
- package/dist/server/device-center/group-context-message.js +208 -0
- package/dist/server/device-center/group-context.js +109 -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 +178 -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.js +1544 -80
- 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 +18 -0
- package/dist/server/device-center/local.js +356 -40
- package/dist/server/device-center/main.js +11 -1
- 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 +549 -41
- package/dist/server/device-center/store.js +66 -3
- 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 +400 -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 +815 -8
- package/dist/server/dispatch-domain-handlers.js +21 -3
- package/dist/server/dsh/dsh-agent-service.js +6 -0
- package/dist/server/goal-service.js +10 -5
- package/dist/server/index.js +89 -13
- package/dist/server/message-deletion.js +40 -0
- package/dist/server/message-window.js +9 -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 +1 -0
- 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 +5 -2
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/protocol.js +2 -2
- 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/subagent-helpers.js +4 -1
- package/dist/server/subagents.js +50 -36
- package/dist/server/tool-host-factories.js +1 -0
- package/dist/server/universal/universal-session.js +5 -0
- package/dist/server/webui-context.js +25 -5
- package/docs/README.md +94 -0
- package/docs/device-center.md +100 -12
- package/docs/dsh-engine.md +10 -10
- package/extensions/webui.ts +43 -1
- package/package.json +1 -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-BYzPgmoq.js +1 -0
- package/web/dist/assets/BgTasksModal-ni0VTmAQ.js +1 -0
- package/web/dist/assets/{CanvasStudio-B3W6b_rA.js → CanvasStudio-BCUeEf25.js} +3 -3
- package/web/dist/assets/{CanvasStudio-Csv1Sj1-.js → CanvasStudio-DNmDNBod.js} +3 -3
- package/web/dist/assets/DeviceCenter-BcBhRgL6.js +3 -0
- package/web/dist/assets/DeviceCenter-CmL5FZmt.js +3 -0
- package/web/dist/assets/DeviceGroups-CLdpdnI7.js +1 -0
- package/web/dist/assets/DeviceGroups-CgBaDPrD.js +1 -0
- package/web/dist/assets/Dropdown-CGXD0DfT.js +1 -0
- package/web/dist/assets/FilePreview-B8iqnBjy.js +2 -0
- package/web/dist/assets/FilePreview-BqviZKCN.js +2 -0
- package/web/dist/assets/GlobalSearchModal-mevF-ySi.js +1 -0
- package/web/dist/assets/GlobalSearchModal-qL99FN_F.js +1 -0
- package/web/dist/assets/GroupConversation-B88Yy-lt.js +3 -0
- package/web/dist/assets/GroupConversation-BwP5NVOE.js +3 -0
- package/web/dist/assets/HintTip-LHwkS2jV.js +1 -0
- package/web/dist/assets/ModelConfigModal-Do3Fe3jH.js +2 -0
- package/web/dist/assets/PiSetupModal-DE5-gEtI.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-6UMARucA.js +1 -0
- package/web/dist/assets/SCMPanel-Hi6QqC1Q.js +2 -0
- package/web/dist/assets/SCMPanel-ezY_ZtCu.js +2 -0
- package/web/dist/assets/SettingsModal-6HgeEsJ8.js +20 -0
- package/web/dist/assets/SettingsModal-CF-7MwqS.js +20 -0
- package/web/dist/assets/TerminalPanel-CTp_BZ4S.js +3 -0
- package/web/dist/assets/TerminalPanel-CkYxC-Gh.js +3 -0
- package/web/dist/assets/TuiOverlayModal-1cRLP8IF.js +1 -0
- package/web/dist/assets/TuiOverlayModal-2xlptqWX.js +1 -0
- package/web/dist/assets/app-globals-DZNa9v8u.js +325 -0
- package/web/dist/assets/canvas-client-Bye9nSFO.js +1 -0
- package/web/dist/assets/{desktop-YguLbrX1.js → desktop-kjBoE3Nu.js} +1 -1
- package/web/dist/assets/group-view-state-CGCzYF8k.js +1 -0
- package/web/dist/assets/index-CsFs_HMb.css +1 -0
- package/web/dist/assets/index-DHeeHS_l.js +85 -0
- package/web/dist/assets/index-DTZch8uz.js +85 -0
- package/web/dist/assets/plugin-host-C8mNgghx.js +1 -0
- package/web/dist/assets/remote-ssh-client-CeP3CLUW.js +1 -0
- package/web/dist/assets/theme-BXGpyTMh.js +1 -0
- package/web/dist/index.html +10 -9
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/BgTasksModal-BIlQ89vr.js +0 -1
- package/web/dist/assets/BgTasksModal-Ck2sjlPM.js +0 -1
- package/web/dist/assets/DeviceCenter-BQdDcO8A.js +0 -3
- package/web/dist/assets/DeviceCenter-CHRKL0Vy.js +0 -3
- package/web/dist/assets/DeviceGroups-D2rogxEN.js +0 -1
- package/web/dist/assets/Dropdown-ZLS04O8p.js +0 -1
- package/web/dist/assets/FilePreview-B9xoU6ON.js +0 -2
- package/web/dist/assets/FilePreview-DUQpVZkH.js +0 -2
- package/web/dist/assets/GlobalSearchModal-BXEGvUZq.js +0 -1
- package/web/dist/assets/GlobalSearchModal-CnVPmfOI.js +0 -1
- package/web/dist/assets/GroupConversation-CyIpQzDd.js +0 -1
- package/web/dist/assets/GroupConversation-DgYV3uYS.js +0 -1
- package/web/dist/assets/ModelConfigModal-DDZccDYt.js +0 -2
- package/web/dist/assets/PiSetupModal-DHbqfx8T.js +0 -1
- package/web/dist/assets/ProviderOAuthControls-C-MK36Dk.js +0 -1
- package/web/dist/assets/SCMPanel-imYI2Ecw.js +0 -2
- package/web/dist/assets/SCMPanel-sVZcV7dR.js +0 -2
- package/web/dist/assets/SettingsModal-Xw1Yoi3_.js +0 -20
- package/web/dist/assets/SettingsModal-mb4e34-0.js +0 -20
- package/web/dist/assets/TerminalPanel-CLkJJvxg.js +0 -3
- package/web/dist/assets/TerminalPanel-DpcIellm.js +0 -3
- package/web/dist/assets/TuiOverlayModal-4-fFAZe7.js +0 -1
- package/web/dist/assets/TuiOverlayModal-DCu01lks.js +0 -1
- package/web/dist/assets/app-globals-DcyO0UPi.js +0 -1
- package/web/dist/assets/fi-DaSO0Q-m.js +0 -325
- package/web/dist/assets/index-Cdh3sibd.js +0 -85
- package/web/dist/assets/index-DDZMv6LO.css +0 -1
- package/web/dist/assets/index-DWw_SxRq.js +0 -85
- package/web/dist/assets/plugin-host-BRBg6zmQ.js +0 -1
- package/web/dist/assets/theme-DDl-Oy1b.js +0 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { isTerminal, taskPending } from "./store.js";
|
|
3
|
+
export function trimConversationHistory(state, roomId, limit = 500) {
|
|
4
|
+
const protectedIds = new Set();
|
|
5
|
+
for (const task of state.tasks.filter((t) => t.roomId === roomId && taskPending(t))) {
|
|
6
|
+
protectedIds.add(task.id);
|
|
7
|
+
if (task.rootTaskId)
|
|
8
|
+
protectedIds.add(task.rootTaskId);
|
|
9
|
+
let ancestor = task.parentTaskId;
|
|
10
|
+
while (ancestor && !protectedIds.has(ancestor)) {
|
|
11
|
+
protectedIds.add(ancestor);
|
|
12
|
+
ancestor = state.tasks.find((t) => t.id === ancestor && t.roomId === roomId)?.parentTaskId;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const terminal = state.tasks.filter((t) => t.roomId === roomId && !taskPending(t) && !protectedIds.has(t.id));
|
|
16
|
+
const remove = new Set(terminal.slice(0, -limit).map((t) => t.id));
|
|
17
|
+
state.tasks = state.tasks.filter((t) => !remove.has(t.id));
|
|
18
|
+
state.conversationMessages = state.conversationMessages.filter((m) => !remove.has(m.taskId));
|
|
19
|
+
}
|
|
20
|
+
export function conversationPage(state, conversationId, before) {
|
|
21
|
+
const all = state.conversationMessages.filter((m) => m.conversationId === conversationId);
|
|
22
|
+
const end = before === undefined ? all.length : all.findIndex((m) => m.id === before);
|
|
23
|
+
if (end < 0)
|
|
24
|
+
throw Object.assign(new Error("Invalid conversation cursor"), { status: 400 });
|
|
25
|
+
const messages = [];
|
|
26
|
+
const tasks = [];
|
|
27
|
+
let bytes = 0;
|
|
28
|
+
let truncated = false;
|
|
29
|
+
for (let i = end - 1; i >= 0 && messages.length < 100; i--) {
|
|
30
|
+
let message = all[i];
|
|
31
|
+
let task = state.tasks.find((t) => t.id === message.taskId);
|
|
32
|
+
const taskId = task?.id;
|
|
33
|
+
const includeTask = !!taskId && !tasks.some((t) => t.id === taskId);
|
|
34
|
+
let size = Buffer.byteLength(JSON.stringify({ message, task: includeTask ? task : undefined }));
|
|
35
|
+
if (!messages.length && size > 60 * 1024) {
|
|
36
|
+
message = { ...message, text: message.text.slice(0, 1024) };
|
|
37
|
+
if (task)
|
|
38
|
+
task = { ...task, text: task.text.slice(0, 1024), report: task.report?.slice(0, 1024), handoff: undefined };
|
|
39
|
+
truncated = true;
|
|
40
|
+
size = Buffer.byteLength(JSON.stringify({ message, task: includeTask ? task : undefined }));
|
|
41
|
+
}
|
|
42
|
+
if (messages.length && bytes + size > 60 * 1024)
|
|
43
|
+
break;
|
|
44
|
+
bytes += size;
|
|
45
|
+
messages.unshift(message);
|
|
46
|
+
if (includeTask && task)
|
|
47
|
+
tasks.unshift(task);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
messages,
|
|
51
|
+
tasks,
|
|
52
|
+
hasMore: end > messages.length,
|
|
53
|
+
truncated: truncated || end > messages.length,
|
|
54
|
+
nextBefore: end > messages.length ? messages[0]?.id : undefined,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** Persisted insertion order is the FIFO; timestamps can collide and latestTaskId is not a queue. */
|
|
58
|
+
export function nextConversationTask(state, conversationId) {
|
|
59
|
+
const conversation = state.conversations.find((entry) => entry.id === conversationId);
|
|
60
|
+
if (!conversation || conversation.closed)
|
|
61
|
+
return;
|
|
62
|
+
const pending = state.tasks.filter((task) => task.conversationId === conversationId && taskPending(task));
|
|
63
|
+
if (pending.some((task) => task.status !== "queued" || task.cancelRequested))
|
|
64
|
+
return;
|
|
65
|
+
return pending[0];
|
|
66
|
+
}
|
|
67
|
+
/** A queued continuation must not replace the identity of the turn still executing. */
|
|
68
|
+
export function advanceConversationTask(state, conversation, task) {
|
|
69
|
+
const current = state.tasks.find((entry) => entry.id === conversation.latestTaskId);
|
|
70
|
+
if (current && current.id !== task.id && taskPending(current))
|
|
71
|
+
return;
|
|
72
|
+
conversation.latestTaskId = task.id;
|
|
73
|
+
}
|
|
74
|
+
export function conversationStatus(state, conversation) {
|
|
75
|
+
if (conversation.closed)
|
|
76
|
+
return "closed";
|
|
77
|
+
return state.tasks.find((task) => task.id === conversation.latestTaskId)?.status ?? "interrupted";
|
|
78
|
+
}
|
|
79
|
+
/** Establish the address in the same transaction that commits the initial task. */
|
|
80
|
+
export function establishConversation(state, task) {
|
|
81
|
+
const conversation = {
|
|
82
|
+
id: randomUUID(),
|
|
83
|
+
accountId: task.accountId,
|
|
84
|
+
roomId: task.roomId,
|
|
85
|
+
botId: task.botId,
|
|
86
|
+
deviceId: task.deviceId,
|
|
87
|
+
botRevision: task.botRevision,
|
|
88
|
+
...(task.sourceBotId ? { sourceBotId: task.sourceBotId } : {}),
|
|
89
|
+
...(task.sourceConversationId ? { sourceConversationId: task.sourceConversationId } : {}),
|
|
90
|
+
...(task.parentTaskId ? { parentTaskId: task.parentTaskId } : {}),
|
|
91
|
+
name: task.text.slice(0, 80),
|
|
92
|
+
rootTaskId: task.rootTaskId ?? task.id,
|
|
93
|
+
dispatchDepth: task.dispatchDepth ?? 0,
|
|
94
|
+
latestTaskId: task.id,
|
|
95
|
+
createdAt: task.createdAt,
|
|
96
|
+
updatedAt: task.updatedAt,
|
|
97
|
+
};
|
|
98
|
+
task.conversationId = conversation.id;
|
|
99
|
+
task.resume = false;
|
|
100
|
+
state.conversations.push(conversation);
|
|
101
|
+
return conversation;
|
|
102
|
+
}
|
|
103
|
+
export function recordConversationTurn(state, task, key, fingerprint) {
|
|
104
|
+
if (!task.conversationId)
|
|
105
|
+
return;
|
|
106
|
+
state.conversationMessages.push({
|
|
107
|
+
id: randomUUID(),
|
|
108
|
+
conversationId: task.conversationId,
|
|
109
|
+
taskId: task.id,
|
|
110
|
+
kind: task.kind ?? "message",
|
|
111
|
+
text: task.text,
|
|
112
|
+
sourceBotId: task.sourceBotId,
|
|
113
|
+
requestId: task.requestId,
|
|
114
|
+
idempotencyKey: key,
|
|
115
|
+
fingerprint,
|
|
116
|
+
createdAt: task.createdAt,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/** A terminal child result creates one durable continuation. Its task is the delivery receipt. */
|
|
120
|
+
export function queueParentResult(state, child) {
|
|
121
|
+
if (child.cancelRequested ||
|
|
122
|
+
!isTerminal(child.status) ||
|
|
123
|
+
child.kind === "result" ||
|
|
124
|
+
child.kind === "reply" ||
|
|
125
|
+
!child.parentTaskId ||
|
|
126
|
+
!child.report)
|
|
127
|
+
return;
|
|
128
|
+
if (state.tasks.some((task) => task.resultOfTaskId === child.id))
|
|
129
|
+
return;
|
|
130
|
+
if (child.kind === "request" &&
|
|
131
|
+
state.tasks.some((task) => task.kind === "reply" && task.requestId === child.requestId && task.roomId === child.roomId))
|
|
132
|
+
return;
|
|
133
|
+
const parent = state.tasks.find((task) => task.id === child.parentTaskId);
|
|
134
|
+
const conversation = state.conversations.find((entry) => entry.id === parent?.conversationId);
|
|
135
|
+
const root = state.tasks.find((task) => task.id === child.rootTaskId);
|
|
136
|
+
const bot = conversation &&
|
|
137
|
+
state.bots.find((entry) => entry.id === conversation.botId && entry.enabled && entry.accountId === child.accountId);
|
|
138
|
+
if (!parent ||
|
|
139
|
+
!conversation ||
|
|
140
|
+
conversation.closed ||
|
|
141
|
+
!root ||
|
|
142
|
+
!bot ||
|
|
143
|
+
bot.deviceId !== conversation.deviceId ||
|
|
144
|
+
bot.revision !== conversation.botRevision ||
|
|
145
|
+
!state.rooms.some((room) => room.id === child.roomId && room.botIds.includes(bot.id)) ||
|
|
146
|
+
(root.dispatchCount ?? 0) >= 16 ||
|
|
147
|
+
(child.dispatchDepth ?? 0) >= 8)
|
|
148
|
+
return;
|
|
149
|
+
root.dispatchCount = (root.dispatchCount ?? 0) + 1;
|
|
150
|
+
const now = Date.now();
|
|
151
|
+
const task = {
|
|
152
|
+
id: randomUUID(),
|
|
153
|
+
attemptId: randomUUID(),
|
|
154
|
+
accountId: child.accountId,
|
|
155
|
+
roomId: child.roomId,
|
|
156
|
+
botId: bot.id,
|
|
157
|
+
botRevision: bot.revision,
|
|
158
|
+
deviceId: bot.deviceId,
|
|
159
|
+
sourceBotId: child.botId,
|
|
160
|
+
conversationId: conversation.id,
|
|
161
|
+
resume: true,
|
|
162
|
+
kind: "result",
|
|
163
|
+
resultOfTaskId: child.id,
|
|
164
|
+
requestId: child.requestId,
|
|
165
|
+
parentTaskId: child.id,
|
|
166
|
+
rootTaskId: root.id,
|
|
167
|
+
dispatchDepth: (child.dispatchDepth ?? 0) + 1,
|
|
168
|
+
text: child.report,
|
|
169
|
+
status: "queued",
|
|
170
|
+
createdAt: now,
|
|
171
|
+
updatedAt: now,
|
|
172
|
+
};
|
|
173
|
+
state.tasks.push(task);
|
|
174
|
+
advanceConversationTask(state, conversation, task);
|
|
175
|
+
conversation.updatedAt = now;
|
|
176
|
+
recordConversationTurn(state, task);
|
|
177
|
+
return task;
|
|
178
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { hashSecret } from "./store.js";
|
|
3
|
+
function fail(status, message) {
|
|
4
|
+
throw Object.assign(new Error(message), { status });
|
|
5
|
+
}
|
|
6
|
+
function text(value, max, name) {
|
|
7
|
+
if (typeof value !== "string" ||
|
|
8
|
+
!value.trim() ||
|
|
9
|
+
value.length > max ||
|
|
10
|
+
/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(value))
|
|
11
|
+
fail(400, `Invalid ${name}`);
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function object(value, keys) {
|
|
15
|
+
if (!value ||
|
|
16
|
+
typeof value !== "object" ||
|
|
17
|
+
Array.isArray(value) ||
|
|
18
|
+
Object.keys(value).some((key) => !keys.includes(key)))
|
|
19
|
+
fail(400, "Invalid fields");
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
function sha(value) {
|
|
23
|
+
const result = text(value, 64, "commit");
|
|
24
|
+
if (!/^(?:[a-f\d]{40}|[a-f\d]{64})$/i.test(result))
|
|
25
|
+
fail(400, "Full commit SHA required");
|
|
26
|
+
return result.toLowerCase();
|
|
27
|
+
}
|
|
28
|
+
function repository(value) {
|
|
29
|
+
const result = text(value, 2048, "repository");
|
|
30
|
+
if (/[\r\n\t]/.test(result))
|
|
31
|
+
fail(400, "Invalid repository");
|
|
32
|
+
if (result.includes("://")) {
|
|
33
|
+
let url;
|
|
34
|
+
try {
|
|
35
|
+
url = new URL(result);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return fail(400, "Invalid repository URL");
|
|
39
|
+
}
|
|
40
|
+
if (!["https:", "http:", "ssh:", "git:"].includes(url.protocol) ||
|
|
41
|
+
url.password ||
|
|
42
|
+
(url.username && (url.protocol !== "ssh:" || url.username !== "git")) ||
|
|
43
|
+
url.search ||
|
|
44
|
+
url.hash)
|
|
45
|
+
fail(400, "Repository URL credentials or query are not allowed");
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
function binding(state, roomId, conversationId, actor, body) {
|
|
50
|
+
const room = state.rooms.find((r) => r.id === roomId && r.accountId === actor.accountId);
|
|
51
|
+
const conversation = state.conversations.find((c) => c.id === conversationId && c.roomId === roomId && c.accountId === actor.accountId);
|
|
52
|
+
if (!room || !conversation)
|
|
53
|
+
fail(404, "Conversation missing");
|
|
54
|
+
if (actor.bot) {
|
|
55
|
+
const bot = state.bots.find((b) => b.id === actor.bot.id &&
|
|
56
|
+
b.accountId === actor.accountId &&
|
|
57
|
+
b.enabled &&
|
|
58
|
+
b.deviceId === actor.bot.deviceId &&
|
|
59
|
+
b.revision === actor.bot.revision);
|
|
60
|
+
if (!bot ||
|
|
61
|
+
!room.botIds.includes(bot.id) ||
|
|
62
|
+
!state.devices.some((d) => d.id === bot.deviceId && d.accountId === actor.accountId))
|
|
63
|
+
fail(403, "Bot is not a room member");
|
|
64
|
+
const parent = state.tasks.find((t) => t.id === body.parentTaskId &&
|
|
65
|
+
t.accountId === actor.accountId &&
|
|
66
|
+
t.roomId === roomId &&
|
|
67
|
+
t.botId === bot.id &&
|
|
68
|
+
t.deviceId === bot.deviceId &&
|
|
69
|
+
t.botRevision === bot.revision);
|
|
70
|
+
const source = state.conversations.find((c) => c.id === parent?.conversationId &&
|
|
71
|
+
c.accountId === actor.accountId &&
|
|
72
|
+
c.roomId === roomId &&
|
|
73
|
+
c.botId === bot.id &&
|
|
74
|
+
c.deviceId === bot.deviceId &&
|
|
75
|
+
c.botRevision === bot.revision &&
|
|
76
|
+
c.latestTaskId === parent?.id &&
|
|
77
|
+
!c.closed);
|
|
78
|
+
if (!parent || !source || body.sourceBotId !== bot.id)
|
|
79
|
+
fail(403, "Current bound Bot task required");
|
|
80
|
+
}
|
|
81
|
+
return conversation;
|
|
82
|
+
}
|
|
83
|
+
function append(state, kind, conversationId, key, actor, payload, create) {
|
|
84
|
+
const fingerprint = hashSecret(JSON.stringify({
|
|
85
|
+
actor: actor.bot ? { type: "bot", id: actor.bot.id } : { type: "human", id: actor.accountId },
|
|
86
|
+
payload,
|
|
87
|
+
}));
|
|
88
|
+
const prior = state.deliveryReceipts.find((r) => r.conversationId === conversationId && r.kind === kind && r.key === key);
|
|
89
|
+
const records = kind === "delivery" ? state.deliveries : state.reviews;
|
|
90
|
+
if (prior) {
|
|
91
|
+
if (prior.fingerprint !== fingerprint)
|
|
92
|
+
fail(409, "Idempotency key reused");
|
|
93
|
+
const record = records.find((r) => r.id === prior.recordId);
|
|
94
|
+
if (!record)
|
|
95
|
+
fail(409, "Idempotency record missing");
|
|
96
|
+
return record;
|
|
97
|
+
}
|
|
98
|
+
if (actor.bot &&
|
|
99
|
+
!state.tasks.some((t) => t.id === payload.taskId &&
|
|
100
|
+
t.botId === actor.bot.id &&
|
|
101
|
+
t.accountId === actor.accountId &&
|
|
102
|
+
t.status === "running" &&
|
|
103
|
+
!t.cancelRequested))
|
|
104
|
+
fail(409, "New records require a running Bot task");
|
|
105
|
+
const record = create();
|
|
106
|
+
const history = [...state.deliveries, ...state.reviews].filter((r) => r.conversationId === conversationId);
|
|
107
|
+
if (history.length >= 200 ||
|
|
108
|
+
Buffer.byteLength(JSON.stringify([...history, record])) > 256 * 1024 ||
|
|
109
|
+
state.deliveries.length + state.reviews.length >= 20000)
|
|
110
|
+
fail(409, "Delivery history capacity reached");
|
|
111
|
+
records.push(record);
|
|
112
|
+
state.deliveryReceipts.push({ conversationId, kind, key, fingerprint, recordId: record.id });
|
|
113
|
+
return record;
|
|
114
|
+
}
|
|
115
|
+
export function addDelivery(state, roomId, conversationId, actor, input) {
|
|
116
|
+
const body = object(input, [
|
|
117
|
+
"sourceBotId",
|
|
118
|
+
"parentTaskId",
|
|
119
|
+
"idempotencyKey",
|
|
120
|
+
"repository",
|
|
121
|
+
"baseCommit",
|
|
122
|
+
"commit",
|
|
123
|
+
"branch",
|
|
124
|
+
"summary",
|
|
125
|
+
"checks",
|
|
126
|
+
"limitations",
|
|
127
|
+
]);
|
|
128
|
+
if (!actor.bot)
|
|
129
|
+
fail(403, "Bot delivery required");
|
|
130
|
+
const conversation = binding(state, roomId, conversationId, actor, body);
|
|
131
|
+
if (conversation.botId !== actor.bot.id || conversation.latestTaskId !== body.parentTaskId || conversation.closed)
|
|
132
|
+
fail(403, "Only the current development Bot task may deliver");
|
|
133
|
+
const key = text(body.idempotencyKey, 128, "idempotencyKey");
|
|
134
|
+
if (!Array.isArray(body.checks) || body.checks.length > 32)
|
|
135
|
+
fail(400, "Invalid checks");
|
|
136
|
+
const checks = body.checks.map((value) => {
|
|
137
|
+
const check = object(value, ["command", "status", "result", "exitCode"]);
|
|
138
|
+
if (!["passed", "failed", "not_run"].includes(check.status))
|
|
139
|
+
fail(400, "Invalid check status");
|
|
140
|
+
if (check.exitCode !== undefined &&
|
|
141
|
+
(!Number.isSafeInteger(check.exitCode) || Math.abs(check.exitCode) > 2147483647))
|
|
142
|
+
fail(400, "Invalid exit code");
|
|
143
|
+
if (check.exitCode !== undefined &&
|
|
144
|
+
((check.status === "passed" && check.exitCode !== 0) ||
|
|
145
|
+
(check.status === "failed" && check.exitCode === 0) ||
|
|
146
|
+
check.status === "not_run"))
|
|
147
|
+
fail(400, "Check status and exit code disagree");
|
|
148
|
+
return {
|
|
149
|
+
command: text(check.command, 2048, "check command"),
|
|
150
|
+
status: check.status,
|
|
151
|
+
...(check.result === undefined ? {} : { result: text(check.result, 4096, "check result") }),
|
|
152
|
+
...(check.exitCode === undefined ? {} : { exitCode: check.exitCode }),
|
|
153
|
+
source: "bot_reported",
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
const limits = body.limitations ?? [];
|
|
157
|
+
if (!Array.isArray(limits) || limits.length > 32)
|
|
158
|
+
fail(400, "Invalid limitations");
|
|
159
|
+
const payload = {
|
|
160
|
+
taskId: text(body.parentTaskId, 128, "parentTaskId"),
|
|
161
|
+
repository: repository(body.repository),
|
|
162
|
+
baseCommit: sha(body.baseCommit),
|
|
163
|
+
commit: sha(body.commit),
|
|
164
|
+
...(body.branch === undefined ? {} : { branch: text(body.branch, 256, "branch") }),
|
|
165
|
+
summary: text(body.summary, 8192, "summary"),
|
|
166
|
+
checks,
|
|
167
|
+
limitations: limits.map((s) => text(s, 2048, "limitation")),
|
|
168
|
+
};
|
|
169
|
+
if (Buffer.byteLength(JSON.stringify(payload)) > 24 * 1024)
|
|
170
|
+
fail(413, "Delivery budget exceeded");
|
|
171
|
+
return append(state, "delivery", conversationId, key, actor, payload, () => ({
|
|
172
|
+
...payload,
|
|
173
|
+
id: randomUUID(),
|
|
174
|
+
accountId: actor.accountId,
|
|
175
|
+
roomId,
|
|
176
|
+
conversationId,
|
|
177
|
+
botId: actor.bot.id,
|
|
178
|
+
createdAt: Date.now(),
|
|
179
|
+
}));
|
|
180
|
+
}
|
|
181
|
+
export function addReview(state, roomId, conversationId, actor, input) {
|
|
182
|
+
const body = object(input, [
|
|
183
|
+
"sourceBotId",
|
|
184
|
+
"parentTaskId",
|
|
185
|
+
"idempotencyKey",
|
|
186
|
+
"deliveryId",
|
|
187
|
+
"commit",
|
|
188
|
+
"verdict",
|
|
189
|
+
"summary",
|
|
190
|
+
]);
|
|
191
|
+
binding(state, roomId, conversationId, actor, body);
|
|
192
|
+
if (!actor.bot && (body.sourceBotId !== undefined || body.parentTaskId !== undefined))
|
|
193
|
+
fail(400, "Human review cannot claim Bot identity");
|
|
194
|
+
const key = text(body.idempotencyKey, 128, "idempotencyKey");
|
|
195
|
+
const deliveryId = text(body.deliveryId, 128, "deliveryId");
|
|
196
|
+
const commit = sha(body.commit);
|
|
197
|
+
if (!["accepted", "changes_requested"].includes(body.verdict))
|
|
198
|
+
fail(400, "Invalid verdict");
|
|
199
|
+
const payload = {
|
|
200
|
+
deliveryId,
|
|
201
|
+
commit,
|
|
202
|
+
verdict: body.verdict,
|
|
203
|
+
summary: text(body.summary, 8192, "summary"),
|
|
204
|
+
...(actor.bot ? { taskId: text(body.parentTaskId, 128, "parentTaskId") } : {}),
|
|
205
|
+
};
|
|
206
|
+
const delivery = state.deliveries.find((d) => d.id === deliveryId &&
|
|
207
|
+
d.conversationId === conversationId &&
|
|
208
|
+
d.roomId === roomId &&
|
|
209
|
+
d.accountId === actor.accountId);
|
|
210
|
+
if (!delivery || delivery.commit !== commit)
|
|
211
|
+
fail(400, "Delivery and commit must match");
|
|
212
|
+
if (actor.bot?.id === delivery.botId)
|
|
213
|
+
fail(403, "Self review is forbidden");
|
|
214
|
+
return append(state, "review", conversationId, key, actor, payload, () => {
|
|
215
|
+
if (state.deliveries.filter((d) => d.conversationId === conversationId).at(-1)?.id !== deliveryId)
|
|
216
|
+
fail(409, "Only the latest delivery may be reviewed");
|
|
217
|
+
return {
|
|
218
|
+
id: randomUUID(),
|
|
219
|
+
accountId: actor.accountId,
|
|
220
|
+
roomId,
|
|
221
|
+
conversationId,
|
|
222
|
+
deliveryId,
|
|
223
|
+
commit,
|
|
224
|
+
verdict: payload.verdict,
|
|
225
|
+
summary: payload.summary,
|
|
226
|
+
reviewer: actor.bot ? { type: "bot", id: actor.bot.id } : { type: "human", id: actor.accountId },
|
|
227
|
+
createdAt: Date.now(),
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
export function deliveryHistory(state, conversationId) {
|
|
232
|
+
return {
|
|
233
|
+
deliveries: state.deliveries.filter((d) => d.conversationId === conversationId),
|
|
234
|
+
reviews: state.reviews.filter((r) => r.conversationId === conversationId),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
export function deliverySummary(state, conversationId, compact = false) {
|
|
238
|
+
const { deliveries, reviews } = deliveryHistory(state, conversationId);
|
|
239
|
+
let latestDelivery = deliveries.at(-1);
|
|
240
|
+
let deliverySummaryTruncated = false;
|
|
241
|
+
if (compact && latestDelivery) {
|
|
242
|
+
const original = latestDelivery;
|
|
243
|
+
latestDelivery = {
|
|
244
|
+
...original,
|
|
245
|
+
summary: original.summary.slice(0, 512),
|
|
246
|
+
checks: original.checks.map(({ result: _result, command, ...check }) => ({
|
|
247
|
+
...check,
|
|
248
|
+
command: command.slice(0, 256),
|
|
249
|
+
})),
|
|
250
|
+
limitations: original.limitations.slice(0, 3).map((s) => s.slice(0, 256)),
|
|
251
|
+
};
|
|
252
|
+
deliverySummaryTruncated = JSON.stringify(original) !== JSON.stringify(latestDelivery);
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
deliverySummaryTruncated,
|
|
256
|
+
latestDelivery,
|
|
257
|
+
latestReview: (() => {
|
|
258
|
+
const review = reviews.filter((r) => r.deliveryId === latestDelivery?.id).at(-1);
|
|
259
|
+
return compact && review ? { ...review, summary: review.summary.slice(0, 512) } : review;
|
|
260
|
+
})(),
|
|
261
|
+
deliveryCount: deliveries.length,
|
|
262
|
+
reviewCount: reviews.length,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
export function getAcceptedDeliveryForIntegration(state, conversationId, deliveryId) {
|
|
266
|
+
const deliveries = state.deliveries.filter((d) => d.conversationId === conversationId);
|
|
267
|
+
const latest = deliveries.at(-1);
|
|
268
|
+
if (!latest || latest.id !== deliveryId) {
|
|
269
|
+
fail(400, "Only the latest delivery can be integrated");
|
|
270
|
+
}
|
|
271
|
+
const reviews = state.reviews.filter((r) => r.deliveryId === deliveryId);
|
|
272
|
+
const latestReview = reviews.at(-1);
|
|
273
|
+
if (!latestReview || latestReview.verdict !== "accepted" || latestReview.commit !== latest.commit) {
|
|
274
|
+
fail(400, "Delivery has no accepted review for its commit");
|
|
275
|
+
}
|
|
276
|
+
return { delivery: latest, review: latestReview };
|
|
277
|
+
}
|