Add workspaces, computers, bot memory and the bots queue
- Schema: workspaces (+ workspace_bots, workspace_computers), containers, computers, bot_computers, bots.memory, workspaces.system_prompt, and bots_queue as a bun-sqlite isQueue table - Main computer (and its container record) ensured with the coordinator on signup, seed and /admin/bots; the coordinator's access to it is locked - Effective computer access: direct grants plus workspace computers - Full bot prompt: core instructions, workspace prompts, then memory; bots can rewrite their memory and system prompt - Queue helpers on the native BunSQLite.queue API, plus a handler that heartbeats, serializes per computer, recovers after restarts, reports lost jobs, supports cancelling and pushes outcomes over the WebSocket - Enable SQLite foreign keys at startup so schema cascades fire - Prune finished jobs daily Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE } from "@/db/types/db";
|
||||
import { BotJobCancelledError } from "@/src/dict/bot-queue-dict";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
job_id: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Cancels one of the user's pending or running jobs. It's marked failed
|
||||
* with `BotJobCancelledError`; a running handler finds out on its next
|
||||
* `updateBotJob` (which returns false) and stops. Its task is closed.
|
||||
*/
|
||||
export default async function cancelBotJob({ user_id, job_id }: Params) {
|
||||
const now = Date.now();
|
||||
|
||||
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS_QUEUE>({
|
||||
sql: `UPDATE bots_queue SET status = 'failed', last_error = ?, locked_at = NULL, locked_by = NULL, finished_at = ?, updated_at = ?
|
||||
WHERE id = ? AND status IN ('pending', 'processing')
|
||||
AND bot_id IN (SELECT id FROM bots WHERE user_id = ?)
|
||||
RETURNING *`,
|
||||
values: [BotJobCancelledError, now, now, job_id, user_id],
|
||||
});
|
||||
|
||||
const job = res.payload?.[0];
|
||||
|
||||
if (!job?.id) {
|
||||
throw new AppError(`No pending or running job to cancel.`);
|
||||
}
|
||||
|
||||
if (job.task_id) {
|
||||
await BunSQLite.sql({
|
||||
sql: `UPDATE bot_tasks SET status = 'done', updated_at = ? WHERE id = ?`,
|
||||
values: [now, job.task_id],
|
||||
});
|
||||
}
|
||||
|
||||
return job;
|
||||
}
|
||||
Reference in New Issue
Block a user