codex-supervisor-mcp 0.5.9 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +4 -3
- package/README.md +3 -2
- package/package.json +2 -1
- package/skills/codex-supervisor/SKILL.md +13 -1
- package/src/codex-runner.js +6 -0
- package/src/side-chat.js +440 -0
- package/src/task-store.js +105 -0
- package/src/web-app.js +110 -6
- package/web/dist/assets/index-CqG_XY9n.js +154 -0
- package/web/dist/assets/index-DI1hLMxE.css +2 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-DPTvsjxI.js +0 -85
- package/web/dist/assets/index-DnCjDsN6.css +0 -2
package/src/web-app.js
CHANGED
|
@@ -22,6 +22,7 @@ import { readTaskChanges } from "./worktree.js";
|
|
|
22
22
|
import { ACTIVE_STATUSES } from "./status.js";
|
|
23
23
|
import { supervisorRoot } from "./paths.js";
|
|
24
24
|
import { checkForUpdate } from "./update-check.js";
|
|
25
|
+
import { attachToTurn, endSideSession, runSideTurn, sideSessionState, stopSideTurn } from "./side-chat.js";
|
|
25
26
|
|
|
26
27
|
const here = fileURLToPath(new URL(".", import.meta.url));
|
|
27
28
|
const distDir = resolve(here, "..", "web", "dist");
|
|
@@ -157,7 +158,20 @@ function sessionRow(key, tasks, meta = null) {
|
|
|
157
158
|
};
|
|
158
159
|
}
|
|
159
160
|
|
|
160
|
-
|
|
161
|
+
// Pagination for the lists: `limit` rows after `offset`, newest activity
|
|
162
|
+
// first for sessions, dispatch order for workers. The page says whether
|
|
163
|
+
// there is more so the board loads the rest as the user scrolls.
|
|
164
|
+
function pageParams(url, defaultLimit) {
|
|
165
|
+
const limit = Math.min(200, Math.max(1, Number(url.searchParams.get("limit") ?? defaultLimit) || defaultLimit));
|
|
166
|
+
const offset = Math.max(0, Number(url.searchParams.get("offset") ?? 0) || 0);
|
|
167
|
+
return { limit, offset };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function paginate(items, { limit, offset }) {
|
|
171
|
+
return { items: items.slice(offset, offset + limit), total: items.length, offset, limit, has_more: offset + limit < items.length };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function overview({ limit, offset } = { limit: 20, offset: 0 }) {
|
|
161
175
|
await reconcileDetachedActiveTasks();
|
|
162
176
|
const tasks = await readTasks();
|
|
163
177
|
const metas = new Map((await readSessions()).map((row) => [row.id, row]));
|
|
@@ -171,23 +185,33 @@ async function overview() {
|
|
|
171
185
|
if (a.active_count !== b.active_count) return b.active_count - a.active_count;
|
|
172
186
|
return String(b.last_activity_at ?? "").localeCompare(String(a.last_activity_at ?? ""));
|
|
173
187
|
});
|
|
188
|
+
const page = paginate(sessions, { limit, offset });
|
|
174
189
|
return {
|
|
175
190
|
generated_at: new Date().toISOString(),
|
|
176
191
|
store: supervisorRoot,
|
|
177
192
|
total_workers: tasks.length,
|
|
178
193
|
active_workers: tasks.filter((task) => ACTIVE_STATUSES.has(task.status)).length,
|
|
179
|
-
sessions
|
|
194
|
+
total_sessions: sessions.length,
|
|
195
|
+
offset: page.offset,
|
|
196
|
+
limit: page.limit,
|
|
197
|
+
has_more: page.has_more,
|
|
198
|
+
sessions: page.items
|
|
180
199
|
};
|
|
181
200
|
}
|
|
182
201
|
|
|
183
|
-
async function session(key) {
|
|
202
|
+
async function session(key, { limit, offset } = { limit: 40, offset: 0 }) {
|
|
184
203
|
await reconcileDetachedActiveTasks();
|
|
185
204
|
const tasks = (await readTasks()).filter((task) => sessionKey(task) === key);
|
|
186
205
|
if (tasks.length === 0) return null;
|
|
187
206
|
const meta = (await readSessions()).find((row) => row.id === key) ?? null;
|
|
207
|
+
const ordered = [...tasks].sort((a, b) => String(a.created_at ?? "").localeCompare(String(b.created_at ?? "")));
|
|
208
|
+
const page = paginate(ordered, { limit, offset });
|
|
188
209
|
return {
|
|
189
210
|
...sessionRow(key, tasks, meta),
|
|
190
|
-
|
|
211
|
+
offset: page.offset,
|
|
212
|
+
limit: page.limit,
|
|
213
|
+
has_more: page.has_more,
|
|
214
|
+
workers: page.items.map(workerRow)
|
|
191
215
|
};
|
|
192
216
|
}
|
|
193
217
|
|
|
@@ -229,6 +253,48 @@ async function events(taskId, url) {
|
|
|
229
253
|
return { task_id: taskId, events: await readTaskEvents(taskId, limit, kinds) };
|
|
230
254
|
}
|
|
231
255
|
|
|
256
|
+
// Read a JSON request body, capped so a stray client cannot fill memory.
|
|
257
|
+
function readJson(req, limit = 2 * 1024 * 1024) {
|
|
258
|
+
return new Promise((resolve, reject) => {
|
|
259
|
+
let size = 0;
|
|
260
|
+
const chunks = [];
|
|
261
|
+
req.on("data", (chunk) => {
|
|
262
|
+
size += chunk.length;
|
|
263
|
+
if (size > limit) {
|
|
264
|
+
reject(Object.assign(new Error("request body too large"), { code: "too_large" }));
|
|
265
|
+
req.destroy();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
chunks.push(chunk);
|
|
269
|
+
});
|
|
270
|
+
req.on("end", () => {
|
|
271
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
272
|
+
if (!text.trim()) return resolve({});
|
|
273
|
+
try {
|
|
274
|
+
resolve(JSON.parse(text));
|
|
275
|
+
} catch (error) {
|
|
276
|
+
reject(Object.assign(new Error(`invalid JSON body: ${error.message}`), { code: "bad_json" }));
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
req.on("error", reject);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// The last user message of an AI SDK useChat request, as plain text.
|
|
284
|
+
function lastUserText(body) {
|
|
285
|
+
if (typeof body?.text === "string") return body.text;
|
|
286
|
+
const messages = Array.isArray(body?.messages) ? body.messages : [];
|
|
287
|
+
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
|
288
|
+
const m = messages[i];
|
|
289
|
+
if (m?.role !== "user") continue;
|
|
290
|
+
const parts = Array.isArray(m.parts) ? m.parts : [];
|
|
291
|
+
const text = parts.filter((p) => p?.type === "text").map((p) => p.text ?? "").join("\n").trim();
|
|
292
|
+
if (text) return text;
|
|
293
|
+
if (typeof m.content === "string" && m.content.trim()) return m.content.trim();
|
|
294
|
+
}
|
|
295
|
+
return "";
|
|
296
|
+
}
|
|
297
|
+
|
|
232
298
|
function json(res, status, body) {
|
|
233
299
|
const text = JSON.stringify(body);
|
|
234
300
|
res.writeHead(status, {
|
|
@@ -265,14 +331,52 @@ async function route(req, res) {
|
|
|
265
331
|
const url = new URL(req.url ?? "/", `http://${host}`);
|
|
266
332
|
const parts = url.pathname.split("/").filter(Boolean);
|
|
267
333
|
if (parts[0] !== "api") return serveStatic(res, url.pathname);
|
|
334
|
+
|
|
335
|
+
// The side chat is the one place the board does something: it forks the
|
|
336
|
+
// worker's Codex thread for a read-only side question.
|
|
337
|
+
if (parts[1] === "workers" && parts.length >= 4 && parts[3] === "chat") {
|
|
338
|
+
const taskId = decodeURIComponent(parts[2]);
|
|
339
|
+
const task = await getTask(taskId);
|
|
340
|
+
if (!task) return json(res, 404, { error: "task_not_found" });
|
|
341
|
+
const action = parts[4] ?? null;
|
|
342
|
+
// GET .../chat/<chatId>/stream is what the AI SDK client calls to
|
|
343
|
+
// resume: replay the running turn, or 204 when nothing is running.
|
|
344
|
+
if (req.method === "GET" && parts.length === 6 && parts[5] === "stream") {
|
|
345
|
+
attachToTurn(taskId, res);
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
if (req.method === "GET" && !action) return json(res, 200, { task_id: taskId, can_ask: Boolean(task.thread_id), worker_status: task.status, ...(await sideSessionState(taskId)) });
|
|
349
|
+
if (req.method === "DELETE" && !action) return json(res, 200, { task_id: taskId, ...(await endSideSession(taskId)) });
|
|
350
|
+
if (req.method === "POST" && action === "stop") return json(res, 200, { task_id: taskId, ...stopSideTurn(taskId) });
|
|
351
|
+
if (req.method === "POST" && !action) {
|
|
352
|
+
let body;
|
|
353
|
+
try {
|
|
354
|
+
body = await readJson(req);
|
|
355
|
+
} catch (error) {
|
|
356
|
+
return json(res, 400, { error: error.code ?? "bad_request", message: error.message });
|
|
357
|
+
}
|
|
358
|
+
const text = lastUserText(body);
|
|
359
|
+
if (!text) return json(res, 400, { error: "empty_question", message: "send a user message with text" });
|
|
360
|
+
try {
|
|
361
|
+
await runSideTurn({ task, text, res });
|
|
362
|
+
} catch (error) {
|
|
363
|
+
if (res.headersSent) return undefined;
|
|
364
|
+
const status = error.code === "busy" ? 409 : error.code === "no_thread" ? 400 : error.code === "codex_missing" ? 503 : 500;
|
|
365
|
+
return json(res, status, { error: error.code ?? "side_chat_failed", message: error.message });
|
|
366
|
+
}
|
|
367
|
+
return undefined;
|
|
368
|
+
}
|
|
369
|
+
return json(res, 405, { error: "method_not_allowed" });
|
|
370
|
+
}
|
|
371
|
+
|
|
268
372
|
if (req.method !== "GET") return json(res, 405, { error: "method_not_allowed" });
|
|
269
373
|
|
|
270
|
-
if (parts[1] === "overview" && parts.length === 2) return json(res, 200, await overview());
|
|
374
|
+
if (parts[1] === "overview" && parts.length === 2) return json(res, 200, await overview(pageParams(url, 20)));
|
|
271
375
|
if (parts[1] === "version" && parts.length === 2) {
|
|
272
376
|
return json(res, 200, await checkForUpdate({ force: url.searchParams.get("force") === "1" }));
|
|
273
377
|
}
|
|
274
378
|
if (parts[1] === "sessions" && parts.length === 3) {
|
|
275
|
-
const body = await session(decodeURIComponent(parts[2]));
|
|
379
|
+
const body = await session(decodeURIComponent(parts[2]), pageParams(url, 40));
|
|
276
380
|
return body ? json(res, 200, body) : json(res, 404, { error: "session_not_found" });
|
|
277
381
|
}
|
|
278
382
|
if (parts[1] === "workers" && parts.length === 3) {
|