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:
@@ -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 };
|
||||
}
|
||||
Reference in New Issue
Block a user