Add computer switching, crew reporting, job steps and markdown chat

- Bots have a current computer (bots.computer_id), switched from the chat
  header beside the model button or by clicking a computer the bot names
  ([computer:ID]); tasks run on it by default
- search_computers runs one read-only command on all of a bot's computers;
  chat replies know their computers (compose-computers-note)
- The coordinator can use every computer and grants or revokes crew access
  with assign_computer
- Delegated jobs record requested_by_bot_id; the worker's result or failure
  is reported back in the coordinator's conversation
- bots_queue_updates records each model turn of a job, pushed live and shown
  in the working-on panel
- Live relay: every process pushes new messages, job steps and bot changes
  from the database to its own sockets, so work done elsewhere shows live
- Bot messages render as markdown (react-markdown + remark-gfm, no raw HTML)
  with twui components; rail previews are plain text
- Fix header button overflow in the model and computer selects
- Bump @moduletrace/twui to 0.1.2

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-19 04:44:35 +01:00
co-authored by Claude Opus 5.5
parent 6a2665dae9
commit 7b580d782e
64 changed files with 1792 additions and 86 deletions
@@ -4,6 +4,7 @@ import insertBotMessage from "../bots/insert-bot-message";
import setBotFace from "../bots/set-bot-face";
import countOpenBotJobs from "./count-open-bot-jobs";
import grabLockedBotJob from "./grab-locked-bot-job";
import relayJobToRequester from "./relay-job-to-requester";
type Params = {
job_id: number | string;
@@ -27,8 +28,8 @@ type Params = {
* Marks a job done, closes its task and sends the completion message back
* to the user in the bot's conversation. Unless it has other jobs open,
* the bot shows needs-you if asked, else done after a task and idle after
* a reply. Throws if this handler no longer holds
* the job.
* a reply. A handed-over job's result also goes to the bot that handed
* it over (`relay`). Throws if this handler no longer holds the job.
*/
export default async function completeBotJob({
job_id,
@@ -87,5 +88,7 @@ export default async function completeBotJob({
face: job.kind == "reply" ? "idle" : "done",
});
return { message, bot };
const relay = silent ? null : await relayJobToRequester({ job, text: result });
return { message, bot, relay };
}
@@ -41,6 +41,11 @@ type Params = {
* Milliseconds to hold the job back before it can be claimed.
*/
delay?: number;
/**
* The user's bot handing this job over (the coordinator). It gets the
* result or failure in its own conversation (`relayJobToRequester`).
*/
requested_by_bot_id?: number | string;
};
/**
@@ -60,6 +65,7 @@ export default async function enqueueBotJob({
task_id,
computer_id,
delay,
requested_by_bot_id,
}: Params) {
const bot = await grabUserBot({ bot_id, user_id });
@@ -67,6 +73,10 @@ export default async function enqueueBotJob({
throw new AppError(`Bot not found.`);
}
if (requested_by_bot_id && !(await grabUserBot({ bot_id: requested_by_bot_id, user_id }))?.id) {
throw new AppError(`Bot not found.`);
}
if (message_id && !(await belongsToBot({ table: "bot_messages", id: message_id, bot_id: bot.id }))) {
throw new AppError(`Message not found.`);
}
@@ -103,6 +113,7 @@ export default async function enqueueBotJob({
message_id: message_id ? Number(message_id) : undefined,
task_id: final_task_id ? Number(final_task_id) : undefined,
computer_id: computer_id ? Number(computer_id) : undefined,
requested_by_bot_id: requested_by_bot_id ? Number(requested_by_bot_id) : undefined,
},
],
delay,
@@ -0,0 +1,28 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES } from "@/db/types/db";
import { AppData } from "@/src/data/app-data";
type Params = {
task_id: number | string;
};
/**
* The latest steps of the newest job working on a task, oldest first,
* capped at `AppData.BotJobUpdatesShown`.
*/
export default async function grabBotJobUpdates({
task_id,
}: Params): Promise<BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES[]> {
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES>({
sql: `SELECT * FROM (
SELECT u.* FROM bots_queue_updates u
WHERE u.bots_queue_id = (
SELECT id FROM bots_queue WHERE task_id = ? ORDER BY id DESC LIMIT 1
)
ORDER BY u.created_at DESC, u.id DESC LIMIT ?
) ORDER BY created_at ASC, id ASC`,
values: [task_id, AppData.BotJobUpdatesShown],
});
return res.payload || [];
}
@@ -0,0 +1,47 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES, BunSQLiteTables } from "@/db/types/db";
import type { BotJobUpdateKind } from "@/src/dict/bot-queue-dict";
type Params = {
job_id: number;
bot_id: number;
kind: BotJobUpdateKind;
text: string;
metadata?: Record<string, unknown>;
};
/**
* Records one step of a running job (`bots_queue_updates`) and returns the
* row, for the live push.
*/
export default async function insertBotJobUpdate({
job_id,
bot_id,
kind,
text,
metadata,
}: Params): Promise<BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES> {
const row: BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES = {
bots_queue_id: job_id,
bot_id,
kind,
text,
metadata: metadata ? JSON.stringify(metadata) : undefined,
};
const res = await BunSQLite.insert<
BUN_SQLITE_COBBLE_BOTS_QUEUE_UPDATES,
(typeof BunSQLiteTables)[number]
>({
table: "bots_queue_updates",
data: [row],
});
const id = res.postInsertReturn?.insertId;
if (!res.success || !id) {
throw new Error(`Failed to save the job update: ${res.msg}`);
}
return { ...row, id, created_at: Date.now() };
}
@@ -2,6 +2,7 @@ import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE } from "@/db/types/db";
import insertBotMessage from "../bots/insert-bot-message";
import setBotFace from "../bots/set-bot-face";
import relayJobToRequester from "./relay-job-to-requester";
type Params = {
job: BUN_SQLITE_COBBLE_BOTS_QUEUE;
@@ -11,7 +12,8 @@ type Params = {
/**
* Wraps up a job that is out of attempts: stamps `finished_at` (so it's
* reported once), tells the user in the bot's conversation and sets the
* bot to needs-you. Its task stays active, since the work isn't done.
* bot to needs-you. Its task stays active, since the work isn't done. A
* handed-over job's failure also goes to the bot that handed it over.
*/
export default async function notifyBotJobFailed({
job,
@@ -47,5 +49,11 @@ export default async function notifyBotJobFailed({
status_line: "Waiting on you",
});
return { message, bot };
const relay = await relayJobToRequester({
job,
text: failure_message || job.last_error || "",
failed: true,
});
return { message, bot, relay };
}
@@ -0,0 +1,53 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS, BUN_SQLITE_COBBLE_BOTS_QUEUE } from "@/db/types/db";
import insertBotMessage from "../bots/insert-bot-message";
type Params = {
job: BUN_SQLITE_COBBLE_BOTS_QUEUE;
/**
* The worker's report, or why it failed (markdown).
*/
text: string;
failed?: boolean;
};
/**
* Reports a handed-over job back to the bot that handed it over
* (`requested_by_bot_id`, the coordinator): a message in its conversation
* with the worker's report, flagged needs-you when the job failed. Returns
* the message for the live push, or null when nobody handed the job over.
*/
export default async function relayJobToRequester({ job, text, failed }: Params) {
if (!job.requested_by_bot_id || !job.bot_id || job.requested_by_bot_id == job.bot_id) {
return null;
}
const worker = (
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
sql: `SELECT name FROM bots WHERE id = ? LIMIT 1`,
values: [job.bot_id],
})
).payload?.[0];
const requester = (
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
sql: `SELECT id FROM bots WHERE id = ? LIMIT 1`,
values: [job.requested_by_bot_id],
})
).payload?.[0];
if (!requester?.id) return null;
const name = worker?.name || "A crew member";
const title = job.title || "the job";
const message = await insertBotMessage({
bot_id: requester.id,
text: failed
? `${name} couldn't finish **${title}**. ${text}`.trim()
: `${name} finished **${title}**. Their report:\n\n${text}`,
needs_you: failed,
});
return { bot_id: requester.id, message };
}
@@ -4,13 +4,15 @@ import type {
} from "@/db/types/db";
import type { BotTaskWithSteps } from "@/src/types";
import BunSQLite from "@moduletrace/bun-sqlite";
import grabBotJobUpdates from "../bots-queue/grab-bot-job-updates";
type Params = {
bot_id: number | string;
};
/**
* The bot's most recent active task and its steps, for the working-on panel.
* The bot's most recent active task, its steps and what its job has done
* so far, for the working-on panel.
*/
export default async function grabActiveBotTask({
bot_id,
@@ -31,5 +33,9 @@ export default async function grabActiveBotTask({
values: [task.id],
});
return { ...task, steps: steps_res.payload || [] };
return {
...task,
steps: steps_res.payload || [],
updates: await grabBotJobUpdates({ task_id: task.id }),
};
}
@@ -9,7 +9,10 @@ type Params = {
/**
* Every computer a bot can use: the ones it was given directly
* (`bot_computers`) plus the ones assigned to any workspace it's in
* (`workspace_computers`). Only the owner's computers, each once.
* (`workspace_computers`); the coordinator can use all of them. Only the
* owner's computers, each once. The
* one it works on now (`bots.computer_id`) comes first, else its main one,
* so `computers[0]` is where its tasks run by default.
*/
export default async function grabBotComputers({
bot_id,
@@ -19,7 +22,8 @@ export default async function grabBotComputers({
sql: `SELECT c.* FROM computers c
WHERE c.user_id = ?
AND (
c.id IN (SELECT computer_id FROM bot_computers WHERE bot_id = ?)
(SELECT kind FROM bots WHERE id = ? AND user_id = c.user_id) = 'coordinator'
OR c.id IN (SELECT computer_id FROM bot_computers WHERE bot_id = ?)
OR c.id IN (
SELECT wc.computer_id FROM workspace_computers wc
JOIN workspace_bots wb ON wb.workspace_id = wc.workspace_id
@@ -27,8 +31,9 @@ export default async function grabBotComputers({
WHERE wb.bot_id = ? AND w.archived = 0
)
)
ORDER BY c.kind = 'main' DESC, c.id ASC`,
values: [user_id, bot_id, bot_id],
ORDER BY c.id = (SELECT computer_id FROM bots WHERE id = ?) DESC,
c.kind = 'main' DESC, c.id ASC`,
values: [user_id, bot_id, bot_id, bot_id, bot_id],
});
return res.payload || [];
@@ -0,0 +1,40 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import AppError from "@/src/utils/app-error";
import grabUserBot from "../bots/grab-user-bot";
import grabBotComputers from "./grab-bot-computers";
type Params = {
bot_id: number | string;
user_id: number | string;
computer_id: number | string;
};
/**
* Switches the computer a bot works on (`bots.computer_id`) to one of its
* own (`grabBotComputers`). Returns its computers, the new one first.
*/
export default async function setBotComputer({ bot_id, user_id, computer_id }: Params) {
const bot = await grabUserBot({ bot_id, user_id });
if (!bot?.id) {
throw new AppError(`Bot not found.`);
}
const computers = await grabBotComputers({ bot_id: bot.id, user_id });
const computer = computers.find((c) => String(c.id) == String(computer_id));
if (!computer?.id) {
throw new AppError(`${bot.name || "This bot"} has no access to that computer.`);
}
await BunSQLite.sql({
sql: `UPDATE bots SET computer_id = ?, updated_at = ? WHERE id = ?`,
values: [computer.id, Date.now(), bot.id],
});
return {
bot,
computer,
computers: [computer, ...computers.filter((c) => c.id != computer.id)],
};
}