@ordewell/web 0.12.1 → 0.12.3
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/dist/server/main.js +316 -57
- package/dist/server/main.js.map +1 -1
- package/package.json +2 -2
package/dist/server/main.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
var hono = require('hono');
|
|
5
|
+
var core = require('@ordewell/core');
|
|
5
6
|
var child_process = require('child_process');
|
|
6
7
|
var crypto = require('crypto');
|
|
7
8
|
var path = require('path');
|
|
8
|
-
var core = require('@ordewell/core');
|
|
9
9
|
var fs = require('fs');
|
|
10
10
|
var ws = require('ws');
|
|
11
11
|
var http = require('http');
|
|
@@ -31,6 +31,228 @@ function _interopNamespace(e) {
|
|
|
31
31
|
var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
32
32
|
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
|
|
33
33
|
|
|
34
|
+
var InvalidBodyError = class extends Error {
|
|
35
|
+
constructor(message) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.name = "InvalidBodyError";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var ChildNotFoundError = class extends Error {
|
|
41
|
+
constructor() {
|
|
42
|
+
super("This tree has no such child plan.");
|
|
43
|
+
this.name = "ChildNotFoundError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
async function jsonObject(c) {
|
|
47
|
+
let parsed;
|
|
48
|
+
try {
|
|
49
|
+
parsed = await c.req.json();
|
|
50
|
+
} catch {
|
|
51
|
+
throw new InvalidBodyError("The request body must be valid JSON.");
|
|
52
|
+
}
|
|
53
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
54
|
+
throw new InvalidBodyError("The request body must be a JSON object.");
|
|
55
|
+
}
|
|
56
|
+
return parsed;
|
|
57
|
+
}
|
|
58
|
+
function classify(err) {
|
|
59
|
+
if (err instanceof InvalidBodyError) return { status: 400, code: "refused" };
|
|
60
|
+
if (err instanceof core.SessionNotFoundError) return { status: 404, code: "session_not_found" };
|
|
61
|
+
if (err instanceof ChildNotFoundError) return { status: 404, code: "child_not_found" };
|
|
62
|
+
if (err instanceof core.SessionHeldError) return { status: 409, code: "session_held" };
|
|
63
|
+
if (err instanceof core.NoPlanError) return { status: 400, code: "no_plan" };
|
|
64
|
+
if (err instanceof core.AlreadyExecutingError) return { status: 409, code: "already_executing" };
|
|
65
|
+
if (err instanceof core.WorkspaceNotAProjectError) return { status: 400, code: "workspace_not_a_project" };
|
|
66
|
+
if (err instanceof core.WorkspaceNotFoundError) return { status: 400, code: "workspace_not_found" };
|
|
67
|
+
if (err instanceof core.PlannerNotReadyError) return { status: 409, code: "planner_not_ready" };
|
|
68
|
+
if (err instanceof core.PlannerTurnStoppedError) return { status: 409, code: "planner_turn_stopped" };
|
|
69
|
+
if (err instanceof core.PlannerTurnDiscardedError) return { status: 409, code: "planner_turn_discarded" };
|
|
70
|
+
if (err instanceof core.ConversationBusyError) return { status: 409, code: "conversation_busy" };
|
|
71
|
+
if (err instanceof core.ConversationEditError || err instanceof core.PlanEditError || err instanceof core.TaskControlError) {
|
|
72
|
+
return { status: 400, code: "refused" };
|
|
73
|
+
}
|
|
74
|
+
return void 0;
|
|
75
|
+
}
|
|
76
|
+
function refuse(c, status, message, code) {
|
|
77
|
+
const body = code ? { error: message, code } : { error: message };
|
|
78
|
+
return c.json(body, status);
|
|
79
|
+
}
|
|
80
|
+
function failure(c, err, label, { fallback = 500, message = "Internal error" } = {}) {
|
|
81
|
+
const classified = classify(err);
|
|
82
|
+
const status = classified?.status ?? fallback;
|
|
83
|
+
if (status >= 500) console.error(`[web] ${label} failed:`, err);
|
|
84
|
+
const body = {
|
|
85
|
+
error: err instanceof Error && err.message || message,
|
|
86
|
+
...classified ? { code: classified.code } : {},
|
|
87
|
+
...err instanceof core.WorkspaceNotAProjectError ? { workspace: err.workspace } : {},
|
|
88
|
+
...err instanceof core.PlannerNotReadyError ? { plannerNotReady: err.reason } : {},
|
|
89
|
+
...err instanceof core.SessionHeldError ? { sessionHeld: err.held } : {}
|
|
90
|
+
};
|
|
91
|
+
return c.json(body, status);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// server/routes/children.ts
|
|
95
|
+
var ABOVE_THIS_PLAN = "A raise or a force is the plan above this one to give: this request sets neither.";
|
|
96
|
+
var CONTROL_REFUSALS = {
|
|
97
|
+
view_only: { status: 409, code: "child_view_only" },
|
|
98
|
+
refused: { status: 409, code: "child_refused" },
|
|
99
|
+
no_task: { status: 404, code: "child_task_not_found" },
|
|
100
|
+
not_waiting: { status: 409, code: "checkpoint_not_waiting" }
|
|
101
|
+
};
|
|
102
|
+
function childrenRoute(pool2) {
|
|
103
|
+
const router = new hono.Hono();
|
|
104
|
+
const childOf = async (c) => {
|
|
105
|
+
const child = await pool2.session(c.req.param("id")).children.find(c.req.param("childId"));
|
|
106
|
+
if (!child) throw new ChildNotFoundError();
|
|
107
|
+
return child;
|
|
108
|
+
};
|
|
109
|
+
const position = (c) => pool2.childPosition(c.req.param("id"), c.req.param("childId"));
|
|
110
|
+
const controlled = (c, outcome) => {
|
|
111
|
+
if (!outcome.ok) return refuse(c, CONTROL_REFUSALS[outcome.code].status, outcome.message, CONTROL_REFUSALS[outcome.code].code);
|
|
112
|
+
return c.json({ message: outcome.message, ...outcome.id ? { id: outcome.id } : {} });
|
|
113
|
+
};
|
|
114
|
+
const asksAboveItsPlan = (body) => "raiseTokens" in body || "force" in body;
|
|
115
|
+
router.get("/:id/children", async (c) => {
|
|
116
|
+
try {
|
|
117
|
+
return c.json({ children: await pool2.session(c.req.param("id")).children.list() });
|
|
118
|
+
} catch (err) {
|
|
119
|
+
return failure(c, err, "list child plans");
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
router.get("/:id/children/:childId", async (c) => {
|
|
123
|
+
try {
|
|
124
|
+
const child = await childOf(c);
|
|
125
|
+
return c.json({ ...child.view(), position: position(c) });
|
|
126
|
+
} catch (err) {
|
|
127
|
+
return failure(c, err, "read child plan");
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
router.get("/:id/children/:childId/tasks/:taskId/log", async (c) => {
|
|
131
|
+
try {
|
|
132
|
+
const child = await childOf(c);
|
|
133
|
+
return c.json({ attempts: core.listTaskLogAttempts(child.logLocation(), c.req.param("taskId")), position: position(c) });
|
|
134
|
+
} catch (err) {
|
|
135
|
+
return failure(c, err, "list child task log");
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
router.get("/:id/children/:childId/tasks/:taskId/log/:attempt", async (c) => {
|
|
139
|
+
try {
|
|
140
|
+
const child = await childOf(c);
|
|
141
|
+
const attempt = Number(c.req.param("attempt"));
|
|
142
|
+
if (!Number.isInteger(attempt) || attempt < 1) return refuse(c, 400, "attempt must be a positive integer");
|
|
143
|
+
return c.json({ attempt, events: core.taskLogForSurface(core.readTaskLog(child.logLocation(), c.req.param("taskId"), attempt)), position: position(c) });
|
|
144
|
+
} catch (err) {
|
|
145
|
+
return failure(c, err, "read child task log");
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
router.post("/:id/children/:childId/message", async (c) => {
|
|
149
|
+
try {
|
|
150
|
+
const child = await childOf(c);
|
|
151
|
+
const body = await jsonObject(c);
|
|
152
|
+
if (asksAboveItsPlan(body)) return refuse(c, 400, ABOVE_THIS_PLAN, "refused");
|
|
153
|
+
const { message } = body;
|
|
154
|
+
if (typeof message !== "string" || !message.trim()) return refuse(c, 400, "message must be a non-empty string");
|
|
155
|
+
const outcome = child.message(message);
|
|
156
|
+
if (!outcome.ok) return refuse(c, 409, outcome.message, outcome.code === "view_only" ? "child_view_only" : "child_refused");
|
|
157
|
+
return c.json({ status: outcome.status });
|
|
158
|
+
} catch (err) {
|
|
159
|
+
return failure(c, err, "message child plan");
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
router.post("/:id/children/:childId/tasks/:taskId/message", async (c) => {
|
|
163
|
+
try {
|
|
164
|
+
const child = await childOf(c);
|
|
165
|
+
const body = await jsonObject(c);
|
|
166
|
+
if (asksAboveItsPlan(body)) return refuse(c, 400, ABOVE_THIS_PLAN, "refused");
|
|
167
|
+
if (typeof body.message !== "string" || !body.message.trim()) return refuse(c, 400, "message must be a non-empty string", "refused");
|
|
168
|
+
return controlled(c, await child.control(c.req.param("taskId"), { kind: "message", text: body.message }));
|
|
169
|
+
} catch (err) {
|
|
170
|
+
return failure(c, err, "message child task");
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
for (const answer of ["approve", "reject"]) {
|
|
174
|
+
router.post(`/:id/children/:childId/tasks/:taskId/checkpoint/${answer}`, async (c) => {
|
|
175
|
+
try {
|
|
176
|
+
const child = await childOf(c);
|
|
177
|
+
const body = await jsonObject(c);
|
|
178
|
+
if (asksAboveItsPlan(body)) return refuse(c, 400, ABOVE_THIS_PLAN, "refused");
|
|
179
|
+
const { question } = body;
|
|
180
|
+
if (typeof question !== "string" || !question.trim()) return refuse(c, 400, "question must be the words the task is waiting on", "refused");
|
|
181
|
+
if (body.reason !== void 0 && typeof body.reason !== "string") return refuse(c, 400, "reason must be a string", "refused");
|
|
182
|
+
const reason = body.reason?.trim();
|
|
183
|
+
if (answer === "reject" && !reason) return refuse(c, 400, "A rejection needs a reason: it is all the runner has to go on.", "refused");
|
|
184
|
+
return controlled(c, await child.control(c.req.param("taskId"), { kind: "checkpoint", answer, question, ...reason ? { reason } : {} }));
|
|
185
|
+
} catch (err) {
|
|
186
|
+
return failure(c, err, "answer child checkpoint");
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
return router;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// server/pool/childStreams.ts
|
|
194
|
+
var ChildStreams = class {
|
|
195
|
+
constructor(stream) {
|
|
196
|
+
this.stream = stream;
|
|
197
|
+
}
|
|
198
|
+
stream;
|
|
199
|
+
watched = /* @__PURE__ */ new Map();
|
|
200
|
+
sequence = /* @__PURE__ */ new Map();
|
|
201
|
+
/** Snapshot and watching in one turn, as a root's socket has them: no frame can fall between. */
|
|
202
|
+
join(rootId, childId, child, ws) {
|
|
203
|
+
if (ws.readyState !== ws.OPEN) return;
|
|
204
|
+
const key = keyOf(rootId, childId);
|
|
205
|
+
const view = child.view();
|
|
206
|
+
ws.send(JSON.stringify({ type: "connected", sessionId: childId }));
|
|
207
|
+
ws.send(JSON.stringify({
|
|
208
|
+
type: "run_snapshot",
|
|
209
|
+
sessionId: childId,
|
|
210
|
+
view: view.run,
|
|
211
|
+
approvals: [],
|
|
212
|
+
...this.position(rootId, childId)
|
|
213
|
+
}));
|
|
214
|
+
if (view.plannerCompaction) ws.send(JSON.stringify(view.plannerCompaction));
|
|
215
|
+
const watching = this.watched.get(key);
|
|
216
|
+
if (watching) {
|
|
217
|
+
watching.sockets.add(ws);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const sockets = /* @__PURE__ */ new Set([ws]);
|
|
221
|
+
this.watched.set(key, { sockets, stop: child.watch((frame) => this.send(key, sockets, frame)) });
|
|
222
|
+
}
|
|
223
|
+
leave(rootId, childId, ws) {
|
|
224
|
+
const key = keyOf(rootId, childId);
|
|
225
|
+
const watching = this.watched.get(key);
|
|
226
|
+
if (!watching) return;
|
|
227
|
+
watching.sockets.delete(ws);
|
|
228
|
+
if (watching.sockets.size === 0) this.drop(key, watching);
|
|
229
|
+
}
|
|
230
|
+
position(rootId, childId) {
|
|
231
|
+
return { stream: this.stream, seq: this.sequence.get(keyOf(rootId, childId)) ?? 0 };
|
|
232
|
+
}
|
|
233
|
+
closeRoot(rootId, code) {
|
|
234
|
+
for (const [key, watching] of this.watched) {
|
|
235
|
+
if (!key.startsWith(keyOf(rootId, ""))) continue;
|
|
236
|
+
this.drop(key, watching);
|
|
237
|
+
for (const ws of watching.sockets) {
|
|
238
|
+
if (ws.readyState === ws.OPEN) ws.close(code, "Child plan closed");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
send(key, sockets, frame) {
|
|
243
|
+
const seq = (this.sequence.get(key) ?? 0) + 1;
|
|
244
|
+
this.sequence.set(key, seq);
|
|
245
|
+
const payload = JSON.stringify({ ...frame, stream: this.stream, seq });
|
|
246
|
+
for (const ws of sockets) {
|
|
247
|
+
if (ws.readyState === ws.OPEN) ws.send(payload);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
drop(key, watching) {
|
|
251
|
+
watching.stop();
|
|
252
|
+
this.watched.delete(key);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
var keyOf = (rootId, childId) => `${rootId}\0${childId}`;
|
|
34
256
|
var WebConfig = class extends core.BaseConfig {
|
|
35
257
|
_enabledRunnersOverride;
|
|
36
258
|
_providerModelLists;
|
|
@@ -408,6 +630,7 @@ var OrchestratorPool = class {
|
|
|
408
630
|
*/
|
|
409
631
|
stream = crypto.randomUUID();
|
|
410
632
|
sequence = /* @__PURE__ */ new Map();
|
|
633
|
+
childStreams = new ChildStreams(this.stream);
|
|
411
634
|
registry = new core.RunnerRegistry();
|
|
412
635
|
modelResolver;
|
|
413
636
|
/** A host that supplied its own resolver answers for every workspace, as a supplied probe does. */
|
|
@@ -442,9 +665,11 @@ var OrchestratorPool = class {
|
|
|
442
665
|
codexSandbox;
|
|
443
666
|
leaseHost;
|
|
444
667
|
isolationPreview;
|
|
668
|
+
suppliedDockerCli;
|
|
445
669
|
constructor(deps = {}) {
|
|
446
670
|
const pluginNotice = core.removedPluginNotice();
|
|
447
671
|
if (pluginNotice) console.warn(pluginNotice);
|
|
672
|
+
this.suppliedDockerCli = deps.dockerCli;
|
|
448
673
|
this.runner = deps.runner ?? new core.StructuredRunner();
|
|
449
674
|
this.suppliedRunnerInstallation = deps.runnerInstallation;
|
|
450
675
|
this.runnerInstallation = deps.runnerInstallation ?? new core.RunnerInstallation(this.registry, void 0, void 0, {
|
|
@@ -513,6 +738,8 @@ var OrchestratorPool = class {
|
|
|
513
738
|
if (!this.clients.has(sessionId)) this.clients.set(sessionId, /* @__PURE__ */ new Set());
|
|
514
739
|
this.clients.get(sessionId).add(ws);
|
|
515
740
|
this.connectAccountLimits(sessionId, ws);
|
|
741
|
+
const session = this.sessions.get(sessionId);
|
|
742
|
+
if (session) void this.tellTree(sessionId, session, [ws]);
|
|
516
743
|
}
|
|
517
744
|
unsubscribe(sessionId, ws) {
|
|
518
745
|
this.clients.get(sessionId)?.delete(ws);
|
|
@@ -555,6 +782,31 @@ var OrchestratorPool = class {
|
|
|
555
782
|
this.sessions.set(sessionId, session);
|
|
556
783
|
this.sessionWorkspaces.set(sessionId, workspace);
|
|
557
784
|
for (const ws of this.clients.get(sessionId) ?? []) this.connectAccountLimits(sessionId, ws);
|
|
785
|
+
session.children.onChange(() => void this.tellTree(sessionId, session));
|
|
786
|
+
}
|
|
787
|
+
/** The tree as it stands. A failed read tells nothing: the list route is where a surface hears why. */
|
|
788
|
+
async tellTree(sessionId, session, sockets) {
|
|
789
|
+
const children = await session.children.list().catch(() => []);
|
|
790
|
+
if (children.length === 0 || this.sessions.get(sessionId) !== session) return;
|
|
791
|
+
const payload = JSON.stringify({ type: "child_tree", sessionId, children });
|
|
792
|
+
for (const ws of sockets ?? this.clients.get(sessionId) ?? []) {
|
|
793
|
+
if (ws.readyState === ws.OPEN) ws.send(payload);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
/** False for a child this root does not hold, or a root no longer loaded (ADR-0055 D20). */
|
|
797
|
+
async watchChild(rootId, childId, ws) {
|
|
798
|
+
const session = this.sessions.get(rootId);
|
|
799
|
+
const child = await session?.children.find(childId);
|
|
800
|
+
if (!child || this.sessions.get(rootId) !== session) return false;
|
|
801
|
+
this.childStreams.join(rootId, childId, child, ws);
|
|
802
|
+
return true;
|
|
803
|
+
}
|
|
804
|
+
unwatchChild(rootId, childId, ws) {
|
|
805
|
+
this.childStreams.leave(rootId, childId, ws);
|
|
806
|
+
}
|
|
807
|
+
/** What a read of the child taken now accounts for on its socket. */
|
|
808
|
+
childPosition(rootId, childId) {
|
|
809
|
+
return this.childStreams.position(rootId, childId);
|
|
558
810
|
}
|
|
559
811
|
broadcast(sessionId, msg) {
|
|
560
812
|
const workspace = this.sessionWorkspaces.get(sessionId);
|
|
@@ -738,6 +990,19 @@ var OrchestratorPool = class {
|
|
|
738
990
|
if (sessionId) this.sessions.get(sessionId)?.applyRunSettings(changes);
|
|
739
991
|
return this.getSettings(sessionId, workspace);
|
|
740
992
|
}
|
|
993
|
+
/** The `containers` setting as a surface shows it: `prove` starts a throwaway container to measure (ADR-0059 L3). */
|
|
994
|
+
containerStatus(prove, workspace) {
|
|
995
|
+
const { worktreeSetupCommand: setupCommand } = this.plannerConfig(void 0, workspace);
|
|
996
|
+
return core.containerStatus(this.settingsService.getAll().containers, { prove, ...this.docker(), ...setupCommand ? { setupCommand } : {} });
|
|
997
|
+
}
|
|
998
|
+
/** The way a surface changes that setting, and where it then stands; a running Session keeps what it opened with. */
|
|
999
|
+
async changeContainers(change, workspace) {
|
|
1000
|
+
const refused = await core.changeContainers(change, { settings: this.settingsService, ...this.docker() });
|
|
1001
|
+
return { refused, status: await this.containerStatus(false, workspace) };
|
|
1002
|
+
}
|
|
1003
|
+
docker() {
|
|
1004
|
+
return this.suppliedDockerCli ? { cli: this.suppliedDockerCli } : {};
|
|
1005
|
+
}
|
|
741
1006
|
/**
|
|
742
1007
|
* The workspace whose runner catalog a settings read or write is judged
|
|
743
1008
|
* against: an open session's own, else the one the client says it is in.
|
|
@@ -1163,6 +1428,7 @@ var OrchestratorPool = class {
|
|
|
1163
1428
|
this.sessions.delete(sessionId);
|
|
1164
1429
|
this.sessionWorkspaces.delete(sessionId);
|
|
1165
1430
|
this.releaseUnusedAccountLimits();
|
|
1431
|
+
this.childStreams.closeRoot(sessionId, options?.notifyClients ? 1001 : 1012);
|
|
1166
1432
|
if (!options?.notifyClients) return;
|
|
1167
1433
|
const clients = this.clients.get(sessionId);
|
|
1168
1434
|
if (!clients) return;
|
|
@@ -1198,7 +1464,7 @@ async function removeSession(sessionId, workspace, stop, close, reclaim) {
|
|
|
1198
1464
|
await stop();
|
|
1199
1465
|
const { refused } = await core.discardChildRuns(sessionId, workspace, core.createWorktreeIsolation({ config: new WebConfig() }));
|
|
1200
1466
|
if (refused.length > 0) throw new Error(`The session was kept: ${refused.map(({ worktree, reason }) => `${worktree} (${reason})`).join(", ")}`);
|
|
1201
|
-
close();
|
|
1467
|
+
await close();
|
|
1202
1468
|
await reclaim();
|
|
1203
1469
|
return core.deleteSession(sessionId, workspace);
|
|
1204
1470
|
}
|
|
@@ -1222,60 +1488,6 @@ function stringField(value) {
|
|
|
1222
1488
|
function workspaceKey(workspace) {
|
|
1223
1489
|
return path.resolve(workspace ?? process.cwd());
|
|
1224
1490
|
}
|
|
1225
|
-
var InvalidBodyError = class extends Error {
|
|
1226
|
-
constructor(message) {
|
|
1227
|
-
super(message);
|
|
1228
|
-
this.name = "InvalidBodyError";
|
|
1229
|
-
}
|
|
1230
|
-
};
|
|
1231
|
-
async function jsonObject(c) {
|
|
1232
|
-
let parsed;
|
|
1233
|
-
try {
|
|
1234
|
-
parsed = await c.req.json();
|
|
1235
|
-
} catch {
|
|
1236
|
-
throw new InvalidBodyError("The request body must be valid JSON.");
|
|
1237
|
-
}
|
|
1238
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
1239
|
-
throw new InvalidBodyError("The request body must be a JSON object.");
|
|
1240
|
-
}
|
|
1241
|
-
return parsed;
|
|
1242
|
-
}
|
|
1243
|
-
function classify(err) {
|
|
1244
|
-
if (err instanceof InvalidBodyError) return { status: 400, code: "refused" };
|
|
1245
|
-
if (err instanceof core.SessionNotFoundError) return { status: 404, code: "session_not_found" };
|
|
1246
|
-
if (err instanceof core.SessionHeldError) return { status: 409, code: "session_held" };
|
|
1247
|
-
if (err instanceof core.NoPlanError) return { status: 400, code: "no_plan" };
|
|
1248
|
-
if (err instanceof core.AlreadyExecutingError) return { status: 409, code: "already_executing" };
|
|
1249
|
-
if (err instanceof core.WorkspaceNotAProjectError) return { status: 400, code: "workspace_not_a_project" };
|
|
1250
|
-
if (err instanceof core.WorkspaceNotFoundError) return { status: 400, code: "workspace_not_found" };
|
|
1251
|
-
if (err instanceof core.PlannerNotReadyError) return { status: 409, code: "planner_not_ready" };
|
|
1252
|
-
if (err instanceof core.PlannerTurnStoppedError) return { status: 409, code: "planner_turn_stopped" };
|
|
1253
|
-
if (err instanceof core.PlannerTurnDiscardedError) return { status: 409, code: "planner_turn_discarded" };
|
|
1254
|
-
if (err instanceof core.ConversationBusyError) return { status: 409, code: "conversation_busy" };
|
|
1255
|
-
if (err instanceof core.ConversationEditError || err instanceof core.PlanEditError || err instanceof core.TaskControlError) {
|
|
1256
|
-
return { status: 400, code: "refused" };
|
|
1257
|
-
}
|
|
1258
|
-
return void 0;
|
|
1259
|
-
}
|
|
1260
|
-
function refuse(c, status, message, code) {
|
|
1261
|
-
const body = code ? { error: message, code } : { error: message };
|
|
1262
|
-
return c.json(body, status);
|
|
1263
|
-
}
|
|
1264
|
-
function failure(c, err, label, { fallback = 500, message = "Internal error" } = {}) {
|
|
1265
|
-
const classified = classify(err);
|
|
1266
|
-
const status = classified?.status ?? fallback;
|
|
1267
|
-
if (status >= 500) console.error(`[web] ${label} failed:`, err);
|
|
1268
|
-
const body = {
|
|
1269
|
-
error: err instanceof Error && err.message || message,
|
|
1270
|
-
...classified ? { code: classified.code } : {},
|
|
1271
|
-
...err instanceof core.WorkspaceNotAProjectError ? { workspace: err.workspace } : {},
|
|
1272
|
-
...err instanceof core.PlannerNotReadyError ? { plannerNotReady: err.reason } : {},
|
|
1273
|
-
...err instanceof core.SessionHeldError ? { sessionHeld: err.held } : {}
|
|
1274
|
-
};
|
|
1275
|
-
return c.json(body, status);
|
|
1276
|
-
}
|
|
1277
|
-
|
|
1278
|
-
// server/routes/sessions.ts
|
|
1279
1491
|
function sessionsRoute(pool2) {
|
|
1280
1492
|
const router = new hono.Hono();
|
|
1281
1493
|
router.get("/", (c) => {
|
|
@@ -1333,7 +1545,7 @@ function sessionsRoute(pool2) {
|
|
|
1333
1545
|
const held = pool2.hasSession(id);
|
|
1334
1546
|
let removed;
|
|
1335
1547
|
try {
|
|
1336
|
-
removed = await removeSession(id, ws, () => pool2.stopTree(id), () => pool2.
|
|
1548
|
+
removed = await removeSession(id, ws, () => pool2.stopTree(id), () => pool2.close(id), () => pool2.reclaim(id));
|
|
1337
1549
|
} catch (err) {
|
|
1338
1550
|
return c.json({ ok: false, message: err instanceof Error ? err.message : String(err) });
|
|
1339
1551
|
}
|
|
@@ -1746,8 +1958,39 @@ function approvalsRoute(pool2) {
|
|
|
1746
1958
|
});
|
|
1747
1959
|
return router;
|
|
1748
1960
|
}
|
|
1961
|
+
function containerChange(body) {
|
|
1962
|
+
const change = Object.fromEntries(Object.entries(body).filter(([key]) => key !== "workspace"));
|
|
1963
|
+
if (Object.keys(change).length !== 1) return null;
|
|
1964
|
+
if (typeof change.on === "boolean") return { on: change.on };
|
|
1965
|
+
if (typeof change.image === "string") return { image: change.image };
|
|
1966
|
+
return "login" in change ? { login: change.login } : null;
|
|
1967
|
+
}
|
|
1749
1968
|
function settingsRoute(pool2) {
|
|
1750
1969
|
const router = new hono.Hono();
|
|
1970
|
+
router.use("/containers", async (c, next) => {
|
|
1971
|
+
if (!core.containersOffered()) return refuse(c, 404, "This build does not offer containers");
|
|
1972
|
+
return next();
|
|
1973
|
+
});
|
|
1974
|
+
router.get("/containers", async (c) => {
|
|
1975
|
+
try {
|
|
1976
|
+
const status = await pool2.containerStatus(c.req.query("prove") === "1", c.req.query("workspace") || void 0);
|
|
1977
|
+
return c.json(status);
|
|
1978
|
+
} catch (err) {
|
|
1979
|
+
return failure(c, err, "containers status");
|
|
1980
|
+
}
|
|
1981
|
+
});
|
|
1982
|
+
router.patch("/containers", async (c) => {
|
|
1983
|
+
try {
|
|
1984
|
+
const body = await jsonObject(c);
|
|
1985
|
+
const change = containerChange(body);
|
|
1986
|
+
if (!change) return refuse(c, 400, "One of on, image or login is required");
|
|
1987
|
+
const { workspace } = body;
|
|
1988
|
+
const result = await pool2.changeContainers(change, typeof workspace === "string" ? workspace || void 0 : void 0);
|
|
1989
|
+
return c.json(result);
|
|
1990
|
+
} catch (err) {
|
|
1991
|
+
return failure(c, err, "containers change", { fallback: 400 });
|
|
1992
|
+
}
|
|
1993
|
+
});
|
|
1751
1994
|
router.get("/", async (c) => {
|
|
1752
1995
|
await core.installedPlannerDetected();
|
|
1753
1996
|
const sessionId = c.req.query("sessionId") || void 0;
|
|
@@ -1797,6 +2040,16 @@ function commandsRoute() {
|
|
|
1797
2040
|
});
|
|
1798
2041
|
return router;
|
|
1799
2042
|
}
|
|
2043
|
+
var CHILD_PATH = /^\/ws\/session\/([^/]+)\/child\/([^/]+)$/;
|
|
2044
|
+
function joinChild(pool2, ws, rootId, childId) {
|
|
2045
|
+
const leave = () => pool2.unwatchChild(rootId, childId, ws);
|
|
2046
|
+
ws.on("close", leave);
|
|
2047
|
+
ws.on("error", leave);
|
|
2048
|
+
const refuse3 = () => ws.close(4404, "No such child plan in this tree");
|
|
2049
|
+
void pool2.watchChild(rootId, childId, ws).then((watching) => {
|
|
2050
|
+
if (!watching) refuse3();
|
|
2051
|
+
}, refuse3);
|
|
2052
|
+
}
|
|
1800
2053
|
function createWsHandler(pool2) {
|
|
1801
2054
|
const wss = new ws.WebSocketServer({
|
|
1802
2055
|
noServer: true,
|
|
@@ -1807,6 +2060,11 @@ function createWsHandler(pool2) {
|
|
|
1807
2060
|
});
|
|
1808
2061
|
wss.on("connection", (ws, request) => {
|
|
1809
2062
|
const url = new URL(request.url ?? "", `http://${request.headers.host}`);
|
|
2063
|
+
const child = CHILD_PATH.exec(url.pathname);
|
|
2064
|
+
if (child) {
|
|
2065
|
+
joinChild(pool2, ws, child[1], child[2]);
|
|
2066
|
+
return;
|
|
2067
|
+
}
|
|
1810
2068
|
const sessionId = url.pathname.split("/").pop() || "";
|
|
1811
2069
|
ws.on("close", () => {
|
|
1812
2070
|
pool2.unsubscribe(sessionId, ws);
|
|
@@ -1910,6 +2168,7 @@ function createApp(pool2, admission) {
|
|
|
1910
2168
|
});
|
|
1911
2169
|
app.get("/api/server", (c) => c.json({ pid: process.pid, port: admission.port }));
|
|
1912
2170
|
app.route("/api/sessions", sessionsRoute(pool2));
|
|
2171
|
+
app.route("/api/sessions", childrenRoute(pool2));
|
|
1913
2172
|
app.route("/api/plans", plansRoute(pool2));
|
|
1914
2173
|
app.route("/api/runners", runnersRoute(pool2));
|
|
1915
2174
|
app.route("/api/models", modelsRoute(pool2));
|