toolroll 0.9.116 → 0.9.117
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 +36 -0
- package/README.md +43 -26
- package/dist/acp-agents.js +14 -1
- package/dist/acp-client.js +19 -1
- package/dist/agent-change-check.js +60 -0
- package/dist/agent-change-schema.js +19 -0
- package/dist/agent-detect.js +11 -1
- package/dist/agent-fence.js +39 -10
- package/dist/agent-network-request.js +108 -0
- package/dist/agent-requests.js +105 -0
- package/dist/approval-summary.js +3 -3
- package/dist/backup.js +17 -1
- package/dist/batch-checks.js +81 -18
- package/dist/browser/terminal.js +41 -41
- package/dist/browser/workspace.css +2 -2
- package/dist/browser/workspace.js +36 -36
- package/dist/builder.js +3 -1
- package/dist/candidate-materialize.js +119 -0
- package/dist/central-enrollment-contract.js +156 -0
- package/dist/central-recovery-stage.js +329 -0
- package/dist/chat-actions.js +101 -2
- package/dist/chat-channel.js +40 -0
- package/dist/check-only-standing.js +13 -0
- package/dist/check-only.js +25 -13
- package/dist/check-recovery-hold.js +199 -0
- package/dist/check-recovery-index-schema.js +3 -0
- package/dist/check-screenshots.js +83 -0
- package/dist/cli.js +125 -6
- package/dist/codex-limits.js +4 -0
- package/dist/connect-cli.js +257 -11
- package/dist/contracts/chat-actions.js +4 -0
- package/dist/contracts/console-api.js +14 -3
- package/dist/contracts/engine-api.js +331 -2
- package/dist/contracts/lead-tools.js +56 -2
- package/dist/contracts/provider.js +1 -1
- package/dist/contracts/route.js +1 -1
- package/dist/contracts/terminal-connection.js +50 -0
- package/dist/converse.js +80 -4
- package/dist/device-pair.js +280 -0
- package/dist/direct-chat-artifacts.js +347 -0
- package/dist/direct-chat-catalog.js +311 -0
- package/dist/direct-chat-fence.js +96 -0
- package/dist/direct-chat-git.js +343 -0
- package/dist/direct-chat-laptop.js +19 -0
- package/dist/direct-chat-schema.js +181 -0
- package/dist/direct-chat-ship.js +861 -0
- package/dist/direct-chat-workspace.js +389 -0
- package/dist/direct-chat.js +2107 -0
- package/dist/doctor.js +1 -1
- package/dist/easy-start.js +18 -0
- package/dist/egress.js +25 -4
- package/dist/engine-chat.js +454 -0
- package/dist/engine-reads.js +141 -13
- package/dist/exec.js +4 -3
- package/dist/flexible-agents.js +8 -16
- package/dist/flexible-session.js +91 -47
- package/dist/flexible-ship.js +87 -7
- package/dist/flexible-start.js +4 -1
- package/dist/flexible-thread.js +2 -3
- package/dist/flexible-turns.js +26 -5
- package/dist/flow-triggers.js +13 -4
- package/dist/flows-ui.js +54 -20
- package/dist/handoff-cli.js +630 -0
- package/dist/invoke.js +55 -13
- package/dist/lead-contract.js +5 -4
- package/dist/lead-doors.js +80 -1
- package/dist/lead-progress.js +9 -0
- package/dist/lead-tools.js +681 -10
- package/dist/lead.js +37 -9
- package/dist/live-bus.js +11 -0
- package/dist/live-terminal.js +132 -12
- package/dist/local-handoff.js +363 -0
- package/dist/local-owner-acts.js +264 -2
- package/dist/local-owner.js +16 -1
- package/dist/multiplayer-handoff.js +528 -0
- package/dist/multiplayer-presence.js +471 -0
- package/dist/names.js +17 -4
- package/dist/needs-you.js +10 -0
- package/dist/operate.js +280 -19
- package/dist/policy.js +2 -2
- package/dist/project-delete-ui.js +18 -11
- package/dist/project-delete.js +65 -4
- package/dist/project-tools.js +1 -1
- package/dist/prompt.js +1 -1
- package/dist/provider-connection.js +2 -0
- package/dist/provider-home.js +251 -8
- package/dist/provider.js +3 -2
- package/dist/publish.js +119 -64
- package/dist/pull-request-flow.js +35 -14
- package/dist/read-executor-worker.js +1 -1
- package/dist/read-executor.js +95 -1
- package/dist/restore-retirement.js +152 -0
- package/dist/result-follow-ups.js +116 -37
- package/dist/route-change.js +44 -4
- package/dist/scope.js +62 -5
- package/dist/serve.js +49 -5
- package/dist/server/chat.js +94 -13
- package/dist/server/direct-chat.js +583 -0
- package/dist/server/engine-api.js +219 -21
- package/dist/server/flows.js +43 -10
- package/dist/server/found-repos.js +25 -0
- package/dist/server/guards.js +20 -6
- package/dist/server/live.js +81 -23
- package/dist/server/pages.js +67 -13
- package/dist/server/people-tokens.js +47 -6
- package/dist/server/remote.js +29 -0
- package/dist/server/render-results.js +6 -2
- package/dist/server/render-settings.js +4 -2
- package/dist/server/render-tasks.js +18 -17
- package/dist/server/route-table.js +69 -2
- package/dist/server/settings.js +22 -7
- package/dist/server/task-views.js +15 -3
- package/dist/server/tasks.js +39 -2
- package/dist/server/terminals.js +185 -11
- package/dist/settings-proofs.js +1 -1
- package/dist/store.js +499 -56
- package/dist/subscription-chat.js +33 -6
- package/dist/subscription-usage.js +233 -0
- package/dist/task-live.js +2 -1
- package/dist/teardown-git.js +34 -4
- package/dist/terminal-connection.js +159 -0
- package/dist/tui/darwin-arm64/toolroll-tui +0 -0
- package/dist/tui/darwin-x64/toolroll-tui +0 -0
- package/dist/tui/linux-arm64/toolroll-tui +0 -0
- package/dist/tui/linux-x64/toolroll-tui +0 -0
- package/dist/tui-contract.js +1 -1
- package/dist/tui-launch.js +32 -1
- package/dist/workspace-identity-schema.js +21 -0
- package/package.json +3 -1
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { checkEngineResponse, ENGINE_API_VERSION, } from "../contracts/engine-api.js";
|
|
4
|
+
import { DirectChatHost, directChatPostureOf, importLegacyDirectChatJson, isDirectChatProvider, } from "../direct-chat.js";
|
|
5
|
+
import { listDirectChatSubscriptionModels } from "../direct-chat-catalog.js";
|
|
6
|
+
import { imageContentHeaders } from "../direct-chat-artifacts.js";
|
|
7
|
+
import { cancelDirectChatShip, executeDirectChatShip, listDirectChatShips, previewDirectChatShip, } from "../direct-chat-ship.js";
|
|
8
|
+
import { soloNow } from "../install-mode.js";
|
|
9
|
+
import { backgroundInstalled } from "../laptop-background.js";
|
|
10
|
+
import { localOwnerStandsNow } from "../local-owner.js";
|
|
11
|
+
import { projectName, rowVisible } from "../project.js";
|
|
12
|
+
import { handlersOf } from "./handler-registry.js";
|
|
13
|
+
import { respond } from "./http.js";
|
|
14
|
+
import { rereadBrowser } from "./live.js";
|
|
15
|
+
const THREAD_PATH = /^\/engine\/direct-chats\/([a-f0-9]{32})(?:\/(messages|stop|model|delete|attachments|apply-main-edits|repair|ship-preview|ship|ship-cancel))?$/;
|
|
16
|
+
const ARTIFACT_PATH = /^\/engine\/direct-chats\/([a-f0-9]{32})\/artifacts\/([a-f0-9]{32})$/;
|
|
17
|
+
const HEX32 = /^[0-9a-f]{32}$/;
|
|
18
|
+
export function createDirectChatHandlers(runtime, deps) {
|
|
19
|
+
const { store, liveCeiling, managedRepos } = runtime;
|
|
20
|
+
const dbFile = store.databaseFile();
|
|
21
|
+
const stateRoot = deps.seams?.stateRoot ?? (dbFile && dbFile !== ":memory:" ? dirname(dbFile) : join(tmpdir(), "toolroll-direct-chat"));
|
|
22
|
+
const host = new DirectChatHost(store, {
|
|
23
|
+
stateRoot,
|
|
24
|
+
...deps.seams,
|
|
25
|
+
onNotify: note => {
|
|
26
|
+
deps.seams?.onNotify?.(note);
|
|
27
|
+
deps.bus.publish({ revision: `dc:${note.threadId}:${Date.now()}` });
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
if (!deps.disableLegacyImport && !store.isDemo() && dbFile && dbFile !== ":memory:" && soloNow(store)) {
|
|
31
|
+
try {
|
|
32
|
+
const owners = store.handle
|
|
33
|
+
.prepare("SELECT name, generation FROM approver WHERE revoked_at IS NULL")
|
|
34
|
+
.all();
|
|
35
|
+
if (owners.length === 1 && owners[0]) {
|
|
36
|
+
importLegacyDirectChatJson({ store, owner: owners[0].name, accountGeneration: owners[0].generation });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
host.startPolling();
|
|
43
|
+
const json = (ctx, status, value) => respond(ctx.response, status, "application/json", JSON.stringify(value));
|
|
44
|
+
const checked = (ctx, status, name, value) => json(ctx, status, checkEngineResponse(name, value));
|
|
45
|
+
function admit(ctx, act) {
|
|
46
|
+
if (store.isDemo())
|
|
47
|
+
return { ok: false, status: 403, error: "Direct chat is unavailable in demo mode." };
|
|
48
|
+
if (ctx.who.via !== "cookie")
|
|
49
|
+
return { ok: false, status: 403, error: "Direct chat requires a local browser or desktop session." };
|
|
50
|
+
if (!runtime.fromThisComputer(ctx.request)) {
|
|
51
|
+
return { ok: false, status: 403, error: "Direct chat is only available from this computer over loopback." };
|
|
52
|
+
}
|
|
53
|
+
if (!soloNow(store)) {
|
|
54
|
+
return { ok: false, status: 403, error: "Direct chat is only available on solo local installs." };
|
|
55
|
+
}
|
|
56
|
+
const current = rereadBrowser(runtime, ctx.request, ctx.who);
|
|
57
|
+
if (current === null || current.role !== "approver") {
|
|
58
|
+
return { ok: false, status: 403, error: "Your sign-in changed. Sign in again." };
|
|
59
|
+
}
|
|
60
|
+
if (current.authority === "local-owner" && act !== null) {
|
|
61
|
+
const hash = runtime.sessions.hashOf(current.session);
|
|
62
|
+
if (current.session.localGeneration === null
|
|
63
|
+
|| hash === null
|
|
64
|
+
|| !localOwnerStandsNow(store, {
|
|
65
|
+
account: current.name,
|
|
66
|
+
localGeneration: current.session.localGeneration,
|
|
67
|
+
accountGeneration: current.session.generation,
|
|
68
|
+
transport: "browser",
|
|
69
|
+
act,
|
|
70
|
+
sessionHash: hash,
|
|
71
|
+
request: null,
|
|
72
|
+
})) {
|
|
73
|
+
return { ok: false, status: 403, error: "Local session no longer stands." };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { ok: true, who: current };
|
|
77
|
+
}
|
|
78
|
+
function resolveAdmittedRepo(owner, projectOrPath) {
|
|
79
|
+
const trimmed = projectOrPath.trim();
|
|
80
|
+
if (trimmed === "")
|
|
81
|
+
return null;
|
|
82
|
+
const repos = managedRepos();
|
|
83
|
+
const ceiling = liveCeiling();
|
|
84
|
+
for (const repo of repos) {
|
|
85
|
+
if (!rowVisible(ceiling, repo) || !store.accountCanAccess(owner, repo))
|
|
86
|
+
continue;
|
|
87
|
+
if (repo === trimmed || projectName(repo).toLowerCase() === trimmed.toLowerCase()) {
|
|
88
|
+
return repo;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
function canReachThread(owner, thread) {
|
|
94
|
+
if (thread.owner !== owner)
|
|
95
|
+
return false;
|
|
96
|
+
const ceiling = liveCeiling();
|
|
97
|
+
return rowVisible(ceiling, thread.repo) && store.accountCanAccess(owner, thread.repo);
|
|
98
|
+
}
|
|
99
|
+
function isBackgroundOn() {
|
|
100
|
+
if (!dbFile || dbFile === ":memory:")
|
|
101
|
+
return false;
|
|
102
|
+
try {
|
|
103
|
+
return backgroundInstalled();
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function toSummary(row) {
|
|
110
|
+
return {
|
|
111
|
+
id: row.id,
|
|
112
|
+
title: row.title,
|
|
113
|
+
project: row.project,
|
|
114
|
+
branch: row.branch,
|
|
115
|
+
provider: row.provider,
|
|
116
|
+
modelId: row.modelId,
|
|
117
|
+
effort: row.effort,
|
|
118
|
+
posture: directChatPostureOf(row.provider),
|
|
119
|
+
running: row.running,
|
|
120
|
+
lastStatus: row.lastStatus,
|
|
121
|
+
changedFilesCount: row.changedFilesCount,
|
|
122
|
+
mainDirtyFiles: row.mainDirtyFiles,
|
|
123
|
+
usage: row.usage,
|
|
124
|
+
createdAt: row.createdAt,
|
|
125
|
+
updatedAt: row.updatedAt,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function toArtifactView(row) {
|
|
129
|
+
return {
|
|
130
|
+
id: row.id,
|
|
131
|
+
kind: row.kind,
|
|
132
|
+
filename: row.filename,
|
|
133
|
+
mimeType: row.mimeType,
|
|
134
|
+
byteSize: row.byteSize,
|
|
135
|
+
url: `/engine/direct-chats/${row.threadId}/artifacts/${row.id}`,
|
|
136
|
+
sourcePath: row.sourcePath,
|
|
137
|
+
createdAt: row.createdAt,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function toShipSummary(row) {
|
|
141
|
+
return {
|
|
142
|
+
id: row.id,
|
|
143
|
+
seq: row.seq,
|
|
144
|
+
mode: row.mode,
|
|
145
|
+
branch: row.branch,
|
|
146
|
+
candidateSha: row.candidateSha,
|
|
147
|
+
state: row.state,
|
|
148
|
+
taskId: row.taskId,
|
|
149
|
+
prUrl: row.prUrl,
|
|
150
|
+
detail: row.detail,
|
|
151
|
+
createdAt: row.createdAt,
|
|
152
|
+
stateAt: row.stateAt,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async function list(ctx) {
|
|
156
|
+
const gate = admit(ctx, null);
|
|
157
|
+
if (!gate.ok) {
|
|
158
|
+
return checked(ctx, gate.status, "directChatsList", {
|
|
159
|
+
version: ENGINE_API_VERSION,
|
|
160
|
+
available: false,
|
|
161
|
+
problem: gate.error,
|
|
162
|
+
backgroundOn: false,
|
|
163
|
+
models: [],
|
|
164
|
+
threads: [],
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const projectFilter = ctx.url.searchParams.get("project");
|
|
168
|
+
const visibleThreads = host
|
|
169
|
+
.listThreads(gate.who.name, projectFilter)
|
|
170
|
+
.filter(t => canReachThread(gate.who.name, t))
|
|
171
|
+
.map(toSummary);
|
|
172
|
+
return checked(ctx, 200, "directChatsList", {
|
|
173
|
+
version: ENGINE_API_VERSION,
|
|
174
|
+
available: true,
|
|
175
|
+
problem: null,
|
|
176
|
+
backgroundOn: isBackgroundOn(),
|
|
177
|
+
models: listDirectChatSubscriptionModels(store),
|
|
178
|
+
threads: visibleThreads,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
async function detail(ctx) {
|
|
182
|
+
const gate = admit(ctx, null);
|
|
183
|
+
if (!gate.ok)
|
|
184
|
+
return checked(ctx, gate.status, "refusal", { error: gate.error });
|
|
185
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
186
|
+
const threadId = match?.[1] ?? "";
|
|
187
|
+
const found = host.getThread(gate.who.name, threadId);
|
|
188
|
+
if (found === null || !canReachThread(gate.who.name, found.thread)) {
|
|
189
|
+
return checked(ctx, 404, "refusal", { error: "Chat thread not found." });
|
|
190
|
+
}
|
|
191
|
+
if (admit(ctx, null).ok === false) {
|
|
192
|
+
return checked(ctx, 403, "refusal", { error: "Your sign-in changed. Sign in again." });
|
|
193
|
+
}
|
|
194
|
+
const lastStatus = found.turns.at(-1)?.status ?? null;
|
|
195
|
+
const ships = listDirectChatShips(store, found.thread.id).map(toShipSummary);
|
|
196
|
+
const diffScope = ctx.url.searchParams.get("scope");
|
|
197
|
+
const turnParam = ctx.url.searchParams.get("turn");
|
|
198
|
+
const responseDiff = (diffScope === "turn" || (turnParam !== null && HEX32.test(turnParam)))
|
|
199
|
+
? (host.readTurnDiff(gate.who.name, found.thread.id, turnParam) ?? { changedFiles: [], diffText: "", dirty: false })
|
|
200
|
+
: found.diff;
|
|
201
|
+
return checked(ctx, 200, "directChatDetail", {
|
|
202
|
+
version: ENGINE_API_VERSION,
|
|
203
|
+
room: `direct-chat:${found.thread.id}`,
|
|
204
|
+
thread: toSummary({
|
|
205
|
+
...found.thread,
|
|
206
|
+
running: found.running,
|
|
207
|
+
lastStatus,
|
|
208
|
+
changedFilesCount: found.diff.changedFiles.length,
|
|
209
|
+
}),
|
|
210
|
+
turns: found.turns.map(turn => ({
|
|
211
|
+
id: turn.id,
|
|
212
|
+
seq: turn.seq,
|
|
213
|
+
userPrompt: turn.userPrompt,
|
|
214
|
+
switchedFromProvider: turn.switchedFromProvider,
|
|
215
|
+
status: turn.status,
|
|
216
|
+
assistantText: turn.assistantText,
|
|
217
|
+
errorText: turn.errorText,
|
|
218
|
+
createdAt: turn.createdAt,
|
|
219
|
+
finishedAt: turn.finishedAt,
|
|
220
|
+
usage: turn.usage,
|
|
221
|
+
tools: turn.tools,
|
|
222
|
+
artifacts: turn.artifacts.map(toArtifactView),
|
|
223
|
+
})),
|
|
224
|
+
attachments: found.attachments.map(toArtifactView),
|
|
225
|
+
ships,
|
|
226
|
+
diff: responseDiff,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
async function start(ctx) {
|
|
230
|
+
const gate = admit(ctx, "direct-chat-start");
|
|
231
|
+
if (!gate.ok)
|
|
232
|
+
return checked(ctx, gate.status, "directChatStarted", { ok: false, error: gate.error });
|
|
233
|
+
const project = ctx.posted.get("project") ?? "";
|
|
234
|
+
const prompt = ctx.posted.get("prompt") ?? "";
|
|
235
|
+
const providerRaw = ctx.posted.get("provider") ?? "claude";
|
|
236
|
+
const modelId = ctx.posted.get("model") ?? "";
|
|
237
|
+
const effort = ctx.posted.get("effort") ?? "";
|
|
238
|
+
const requestId = ctx.posted.get("request") ?? "";
|
|
239
|
+
if (!isDirectChatProvider(providerRaw)) {
|
|
240
|
+
return checked(ctx, 400, "directChatStarted", { ok: false, error: "Choose Claude, Codex, or Gemini." });
|
|
241
|
+
}
|
|
242
|
+
if (!HEX32.test(requestId)) {
|
|
243
|
+
return checked(ctx, 400, "directChatStarted", { ok: false, error: "Missing 32-hex request id." });
|
|
244
|
+
}
|
|
245
|
+
const repo = resolveAdmittedRepo(gate.who.name, project);
|
|
246
|
+
if (repo === null) {
|
|
247
|
+
return checked(ctx, 404, "directChatStarted", { ok: false, error: "Choose a connected project first." });
|
|
248
|
+
}
|
|
249
|
+
const res = await host.createThread({
|
|
250
|
+
owner: gate.who.name,
|
|
251
|
+
localGeneration: gate.who.authority === "local-owner" ? gate.who.session.localGeneration : null,
|
|
252
|
+
accountGeneration: gate.who.session.generation,
|
|
253
|
+
requestId,
|
|
254
|
+
repo,
|
|
255
|
+
provider: providerRaw,
|
|
256
|
+
modelId,
|
|
257
|
+
...(effort !== "" ? { effort } : {}),
|
|
258
|
+
prompt,
|
|
259
|
+
});
|
|
260
|
+
if (!res.ok) {
|
|
261
|
+
return checked(ctx, res.status, "directChatStarted", { ok: false, error: res.error });
|
|
262
|
+
}
|
|
263
|
+
return checked(ctx, 200, "directChatStarted", {
|
|
264
|
+
ok: true,
|
|
265
|
+
threadId: res.thread.id,
|
|
266
|
+
turnId: res.turn.id,
|
|
267
|
+
room: `direct-chat:${res.thread.id}`,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
async function send(ctx) {
|
|
271
|
+
const gate = admit(ctx, "direct-chat-send");
|
|
272
|
+
if (!gate.ok)
|
|
273
|
+
return checked(ctx, gate.status, "directChatStarted", { ok: false, error: gate.error });
|
|
274
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
275
|
+
const threadId = match?.[1] ?? "";
|
|
276
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
277
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
278
|
+
return checked(ctx, 404, "directChatStarted", { ok: false, error: "Chat thread not found." });
|
|
279
|
+
}
|
|
280
|
+
const prompt = ctx.posted.get("prompt") ?? "";
|
|
281
|
+
const requestId = ctx.posted.get("request") ?? "";
|
|
282
|
+
const res = await host.sendMessage({
|
|
283
|
+
owner: gate.who.name,
|
|
284
|
+
threadId,
|
|
285
|
+
requestId,
|
|
286
|
+
prompt,
|
|
287
|
+
});
|
|
288
|
+
if (!res.ok) {
|
|
289
|
+
return checked(ctx, res.status, "directChatStarted", { ok: false, error: res.error });
|
|
290
|
+
}
|
|
291
|
+
return checked(ctx, 200, "directChatStarted", {
|
|
292
|
+
ok: true,
|
|
293
|
+
threadId,
|
|
294
|
+
turnId: res.turn.id,
|
|
295
|
+
room: `direct-chat:${threadId}`,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
async function stop(ctx) {
|
|
299
|
+
const gate = admit(ctx, "direct-chat-stop");
|
|
300
|
+
if (!gate.ok)
|
|
301
|
+
return checked(ctx, gate.status, "directChatMutated", { ok: false, error: gate.error });
|
|
302
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
303
|
+
const threadId = match?.[1] ?? "";
|
|
304
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
305
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
306
|
+
return checked(ctx, 404, "directChatMutated", { ok: false, error: "Chat thread not found." });
|
|
307
|
+
}
|
|
308
|
+
const res = host.stopThread(gate.who.name, threadId);
|
|
309
|
+
if (!res.ok)
|
|
310
|
+
return checked(ctx, res.status, "directChatMutated", { ok: false, error: res.error });
|
|
311
|
+
const said = res.stopped ? "Stopped." : res.stopping ? "Stopping…" : "Nothing was running.";
|
|
312
|
+
return checked(ctx, 200, "directChatMutated", { ok: true, said });
|
|
313
|
+
}
|
|
314
|
+
async function switchModel(ctx) {
|
|
315
|
+
const gate = admit(ctx, "direct-chat-manage");
|
|
316
|
+
if (!gate.ok)
|
|
317
|
+
return checked(ctx, gate.status, "directChatMutated", { ok: false, error: gate.error });
|
|
318
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
319
|
+
const threadId = match?.[1] ?? "";
|
|
320
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
321
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
322
|
+
return checked(ctx, 404, "directChatMutated", { ok: false, error: "Chat thread not found." });
|
|
323
|
+
}
|
|
324
|
+
const providerRaw = ctx.posted.get("provider") ?? "";
|
|
325
|
+
const modelId = ctx.posted.get("model") ?? "";
|
|
326
|
+
const effortParam = ctx.posted.has("effort") ? (ctx.posted.get("effort") ?? "") : undefined;
|
|
327
|
+
if (!isDirectChatProvider(providerRaw)) {
|
|
328
|
+
return checked(ctx, 400, "directChatMutated", { ok: false, error: "Choose Claude, Codex, or Gemini." });
|
|
329
|
+
}
|
|
330
|
+
const res = host.switchModel({
|
|
331
|
+
owner: gate.who.name,
|
|
332
|
+
threadId,
|
|
333
|
+
provider: providerRaw,
|
|
334
|
+
modelId,
|
|
335
|
+
...(effortParam !== undefined ? { effort: effortParam } : {}),
|
|
336
|
+
});
|
|
337
|
+
if (!res.ok)
|
|
338
|
+
return checked(ctx, res.status, "directChatMutated", { ok: false, error: res.error });
|
|
339
|
+
return checked(ctx, 200, "directChatMutated", { ok: true, said: "Model updated." });
|
|
340
|
+
}
|
|
341
|
+
async function remove(ctx) {
|
|
342
|
+
const gate = admit(ctx, "direct-chat-manage");
|
|
343
|
+
if (!gate.ok)
|
|
344
|
+
return checked(ctx, gate.status, "directChatMutated", { ok: false, error: gate.error });
|
|
345
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
346
|
+
const threadId = match?.[1] ?? "";
|
|
347
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
348
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
349
|
+
return checked(ctx, 404, "directChatMutated", { ok: false, error: "Chat thread not found." });
|
|
350
|
+
}
|
|
351
|
+
const discardChanges = ctx.posted.get("discard") === "1" || ctx.posted.get("discard") === "true";
|
|
352
|
+
const keepClone = ctx.posted.get("keepBranch") === "1" || ctx.posted.get("keepBranch") === "true";
|
|
353
|
+
const res = host.deleteThread({ owner: gate.who.name, threadId, discardChanges, keepClone });
|
|
354
|
+
if (!res.ok) {
|
|
355
|
+
return checked(ctx, res.status, "directChatMutated", {
|
|
356
|
+
ok: false,
|
|
357
|
+
error: res.error,
|
|
358
|
+
...(res.code ? { code: res.code } : {}),
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
return checked(ctx, 200, "directChatMutated", { ok: true, said: "Chat removed." });
|
|
362
|
+
}
|
|
363
|
+
async function artifact(ctx) {
|
|
364
|
+
const gate = admit(ctx, null);
|
|
365
|
+
if (!gate.ok)
|
|
366
|
+
return checked(ctx, gate.status, "refusal", { error: gate.error });
|
|
367
|
+
const match = ARTIFACT_PATH.exec(ctx.url.pathname);
|
|
368
|
+
const threadId = match?.[1] ?? "";
|
|
369
|
+
const artifactId = match?.[2] ?? "";
|
|
370
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
371
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
372
|
+
return checked(ctx, 404, "refusal", { error: "Artifact not found." });
|
|
373
|
+
}
|
|
374
|
+
const loaded = host.getArtifact(gate.who.name, threadId, artifactId);
|
|
375
|
+
if (loaded === null) {
|
|
376
|
+
return checked(ctx, 404, "refusal", { error: "Artifact not found." });
|
|
377
|
+
}
|
|
378
|
+
ctx.response.writeHead(200, {
|
|
379
|
+
...imageContentHeaders(loaded.row),
|
|
380
|
+
"content-length": String(loaded.bytes.length),
|
|
381
|
+
});
|
|
382
|
+
ctx.response.end(loaded.bytes);
|
|
383
|
+
}
|
|
384
|
+
async function attach(ctx) {
|
|
385
|
+
const gate = admit(ctx, "direct-chat-send");
|
|
386
|
+
if (!gate.ok)
|
|
387
|
+
return checked(ctx, gate.status, "directChatAttached", { ok: false, error: gate.error });
|
|
388
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
389
|
+
const threadId = match?.[1] ?? "";
|
|
390
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
391
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
392
|
+
return checked(ctx, 404, "directChatAttached", { ok: false, error: "Chat thread not found." });
|
|
393
|
+
}
|
|
394
|
+
const filename = ctx.posted.get("filename") ?? "attachment";
|
|
395
|
+
const mimeType = ctx.posted.get("mimeType") ?? undefined;
|
|
396
|
+
const dataBase64 = ctx.posted.get("dataBase64") ?? "";
|
|
397
|
+
if (dataBase64.trim() === "") {
|
|
398
|
+
return checked(ctx, 400, "directChatAttached", { ok: false, error: "Missing attachment payload." });
|
|
399
|
+
}
|
|
400
|
+
let bytes;
|
|
401
|
+
try {
|
|
402
|
+
bytes = Buffer.from(dataBase64, "base64");
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
return checked(ctx, 400, "directChatAttached", { ok: false, error: "Invalid base64 attachment payload." });
|
|
406
|
+
}
|
|
407
|
+
const saved = host.attachFile({
|
|
408
|
+
owner: gate.who.name,
|
|
409
|
+
threadId,
|
|
410
|
+
filename,
|
|
411
|
+
mimeType,
|
|
412
|
+
bytes,
|
|
413
|
+
});
|
|
414
|
+
if (!saved.ok) {
|
|
415
|
+
return checked(ctx, saved.status, "directChatAttached", { ok: false, error: saved.error });
|
|
416
|
+
}
|
|
417
|
+
return checked(ctx, 200, "directChatAttached", {
|
|
418
|
+
ok: true,
|
|
419
|
+
artifact: toArtifactView(saved.artifact),
|
|
420
|
+
cloneRelativePath: saved.cloneRelativePath,
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
async function applyMain(ctx) {
|
|
424
|
+
const gate = admit(ctx, "direct-chat-manage");
|
|
425
|
+
if (!gate.ok)
|
|
426
|
+
return checked(ctx, gate.status, "directChatMutated", { ok: false, error: gate.error });
|
|
427
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
428
|
+
const threadId = match?.[1] ?? "";
|
|
429
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
430
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
431
|
+
return checked(ctx, 404, "directChatMutated", { ok: false, error: "Chat thread not found." });
|
|
432
|
+
}
|
|
433
|
+
const res = host.applyMainEdits(gate.who.name, threadId);
|
|
434
|
+
if (!res.ok) {
|
|
435
|
+
return checked(ctx, res.status, "directChatMutated", { ok: false, error: res.error });
|
|
436
|
+
}
|
|
437
|
+
return checked(ctx, 200, "directChatMutated", { ok: true, said: res.said });
|
|
438
|
+
}
|
|
439
|
+
async function repair(ctx) {
|
|
440
|
+
const gate = admit(ctx, "direct-chat-manage");
|
|
441
|
+
if (!gate.ok)
|
|
442
|
+
return checked(ctx, gate.status, "directChatMutated", { ok: false, error: gate.error });
|
|
443
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
444
|
+
const threadId = match?.[1] ?? "";
|
|
445
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
446
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
447
|
+
return checked(ctx, 404, "directChatMutated", { ok: false, error: "Chat thread not found." });
|
|
448
|
+
}
|
|
449
|
+
const res = host.repairCloneGit(gate.who.name, threadId);
|
|
450
|
+
if (!res.ok) {
|
|
451
|
+
return checked(ctx, res.status, "directChatMutated", { ok: false, error: res.error });
|
|
452
|
+
}
|
|
453
|
+
return checked(ctx, 200, "directChatMutated", { ok: true, said: res.said });
|
|
454
|
+
}
|
|
455
|
+
async function shipPreview(ctx) {
|
|
456
|
+
const gate = admit(ctx, "read");
|
|
457
|
+
if (!gate.ok)
|
|
458
|
+
return checked(ctx, gate.status, "refusal", { error: gate.error });
|
|
459
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
460
|
+
const threadId = match?.[1] ?? "";
|
|
461
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
462
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
463
|
+
return checked(ctx, 404, "refusal", { error: "Chat thread not found." });
|
|
464
|
+
}
|
|
465
|
+
const modeParam = ctx.posted.get("mode") ?? ctx.url.searchParams.get("mode");
|
|
466
|
+
const mode = modeParam === "ship" || modeParam === "keep-branch" ? modeParam : undefined;
|
|
467
|
+
const title = ctx.posted.get("title") ?? ctx.url.searchParams.get("title") ?? undefined;
|
|
468
|
+
const goal = ctx.posted.get("goal") ?? ctx.url.searchParams.get("goal") ?? undefined;
|
|
469
|
+
const res = previewDirectChatShip(store, { stateRoot: host.stateRoot, owner: gate.who.name, threadId, mode, title, goal });
|
|
470
|
+
if (!res.ok)
|
|
471
|
+
return checked(ctx, res.status, "refusal", { error: res.error });
|
|
472
|
+
const { preview } = res;
|
|
473
|
+
return checked(ctx, 200, "directChatShipPreview", {
|
|
474
|
+
version: ENGINE_API_VERSION,
|
|
475
|
+
threadId: preview.threadId,
|
|
476
|
+
mode: preview.mode,
|
|
477
|
+
canAutoMerge: preview.canAutoMerge,
|
|
478
|
+
autoMergeReason: preview.autoMergeReason,
|
|
479
|
+
termsDigest: preview.termsDigest,
|
|
480
|
+
title: preview.title,
|
|
481
|
+
goal: preview.goal,
|
|
482
|
+
baseSha: preview.baseSha,
|
|
483
|
+
branch: preview.branch,
|
|
484
|
+
changedFiles: preview.changedFiles,
|
|
485
|
+
checkCommand: preview.checkCommand,
|
|
486
|
+
githubRepo: preview.githubRepo,
|
|
487
|
+
githubBase: preview.githubBase,
|
|
488
|
+
mergeSummary: preview.mergeSummary,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
async function ship(ctx) {
|
|
492
|
+
const gate = admit(ctx, "direct-chat-ship");
|
|
493
|
+
if (!gate.ok)
|
|
494
|
+
return checked(ctx, gate.status, "directChatShipped", { ok: false, error: gate.error });
|
|
495
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
496
|
+
const threadId = match?.[1] ?? "";
|
|
497
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
498
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
499
|
+
return checked(ctx, 404, "directChatShipped", { ok: false, error: "Chat thread not found." });
|
|
500
|
+
}
|
|
501
|
+
const modeRaw = ctx.posted.get("mode") ?? "keep-branch";
|
|
502
|
+
if (modeRaw !== "ship" && modeRaw !== "keep-branch") {
|
|
503
|
+
return checked(ctx, 400, "directChatShipped", { ok: false, error: "Choose ship or keep-branch." });
|
|
504
|
+
}
|
|
505
|
+
const termsDigest = ctx.posted.get("termsDigest") ?? "";
|
|
506
|
+
if (!HEX32.test(termsDigest)) {
|
|
507
|
+
return checked(ctx, 400, "directChatShipped", { ok: false, error: "Missing Ship terms digest." });
|
|
508
|
+
}
|
|
509
|
+
const title = ctx.posted.get("title") ?? undefined;
|
|
510
|
+
const goal = ctx.posted.get("goal") ?? undefined;
|
|
511
|
+
const password = ctx.posted.get("password") ?? undefined;
|
|
512
|
+
const localOwner = gate.who.authority === "local-owner" && gate.who.session.localGeneration !== null
|
|
513
|
+
? { localGeneration: gate.who.session.localGeneration }
|
|
514
|
+
: null;
|
|
515
|
+
const res = executeDirectChatShip(store, {
|
|
516
|
+
stateRoot: host.stateRoot,
|
|
517
|
+
owner: gate.who.name,
|
|
518
|
+
threadId,
|
|
519
|
+
mode: modeRaw,
|
|
520
|
+
termsDigest,
|
|
521
|
+
title,
|
|
522
|
+
goal,
|
|
523
|
+
password,
|
|
524
|
+
localOwner,
|
|
525
|
+
});
|
|
526
|
+
if (!res.ok) {
|
|
527
|
+
return checked(ctx, res.status, "directChatShipped", {
|
|
528
|
+
ok: false,
|
|
529
|
+
error: res.error,
|
|
530
|
+
...(res.code ? { code: res.code } : {}),
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
host.refreshDiffCache(thread);
|
|
534
|
+
deps.bus.publish({ revision: `dc:${threadId}:ship:${Date.now()}` });
|
|
535
|
+
const said = res.ship.mode === "keep-branch"
|
|
536
|
+
? `Saved branch ${res.ship.branch} (${res.ship.candidateSha.slice(0, 8)}).`
|
|
537
|
+
: `Shipped snapshot ${res.ship.candidateSha.slice(0, 8)} — verifying checks before auto-merge.`;
|
|
538
|
+
return checked(ctx, 200, "directChatShipped", {
|
|
539
|
+
ok: true,
|
|
540
|
+
ship: toShipSummary(res.ship),
|
|
541
|
+
said,
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
async function shipCancel(ctx) {
|
|
545
|
+
const gate = admit(ctx, "direct-chat-manage");
|
|
546
|
+
if (!gate.ok)
|
|
547
|
+
return checked(ctx, gate.status, "directChatMutated", { ok: false, error: gate.error });
|
|
548
|
+
const match = THREAD_PATH.exec(ctx.url.pathname);
|
|
549
|
+
const threadId = match?.[1] ?? "";
|
|
550
|
+
const thread = host.getThreadRecord(gate.who.name, threadId);
|
|
551
|
+
if (thread === null || !canReachThread(gate.who.name, thread)) {
|
|
552
|
+
return checked(ctx, 404, "directChatMutated", { ok: false, error: "Chat thread not found." });
|
|
553
|
+
}
|
|
554
|
+
const shipId = ctx.posted.get("shipId") ?? "";
|
|
555
|
+
const res = cancelDirectChatShip(store, { owner: gate.who.name, threadId, shipId });
|
|
556
|
+
if (!res.ok)
|
|
557
|
+
return checked(ctx, res.status, "directChatMutated", { ok: false, error: res.error });
|
|
558
|
+
deps.bus.publish({ revision: `dc:${threadId}:ship-cancel:${Date.now()}` });
|
|
559
|
+
return checked(ctx, 200, "directChatMutated", { ok: true, said: "Auto-merge cancelled." });
|
|
560
|
+
}
|
|
561
|
+
return {
|
|
562
|
+
host,
|
|
563
|
+
registrations: handlersOf("tasks", {
|
|
564
|
+
"direct-chat.list": list,
|
|
565
|
+
"direct-chat.detail": detail,
|
|
566
|
+
"direct-chat.start": start,
|
|
567
|
+
"direct-chat.send": send,
|
|
568
|
+
"direct-chat.stop": stop,
|
|
569
|
+
"direct-chat.model": switchModel,
|
|
570
|
+
"direct-chat.delete": remove,
|
|
571
|
+
"direct-chat.artifact": artifact,
|
|
572
|
+
"direct-chat.attach": attach,
|
|
573
|
+
"direct-chat.apply-main": applyMain,
|
|
574
|
+
"direct-chat.repair": repair,
|
|
575
|
+
"direct-chat.ship-preview": shipPreview,
|
|
576
|
+
"direct-chat.ship": ship,
|
|
577
|
+
"direct-chat.ship-cancel": shipCancel,
|
|
578
|
+
}),
|
|
579
|
+
close() {
|
|
580
|
+
host.close();
|
|
581
|
+
},
|
|
582
|
+
};
|
|
583
|
+
}
|