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,85 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type {
|
||||
BUN_SQLITE_COBBLE_COMPUTERS,
|
||||
BunSQLiteTables,
|
||||
} from "@/db/types/db";
|
||||
import { MainComputerName } from "@/src/dict/computer-dict";
|
||||
import ensureMainComputerContainer from "./ensure-main-computer-container";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
/**
|
||||
* The owner's coordinator (from `ensureCoordinatorBot`). It always has
|
||||
* access to the main computer.
|
||||
*/
|
||||
coordinator_id: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the id of the user's main computer, recording it (and its
|
||||
* container) first if missing, and makes sure the coordinator has access
|
||||
* to it. Safe to call on every visit.
|
||||
*/
|
||||
export default async function ensureMainComputer({
|
||||
user_id,
|
||||
coordinator_id,
|
||||
}: Params) {
|
||||
const computer_id = await grabOrCreateMainComputer({ user_id });
|
||||
|
||||
await BunSQLite.sql({
|
||||
sql: `INSERT OR IGNORE INTO bot_computers (bot_id, computer_id, created_at, updated_at) VALUES (?, ?, ?, ?)`,
|
||||
values: [coordinator_id, computer_id, Date.now(), Date.now()],
|
||||
});
|
||||
|
||||
return computer_id;
|
||||
}
|
||||
|
||||
async function grabOrCreateMainComputer({
|
||||
user_id,
|
||||
}: Pick<Params, "user_id">) {
|
||||
const existing = (
|
||||
await BunSQLite.select<
|
||||
BUN_SQLITE_COBBLE_COMPUTERS,
|
||||
(typeof BunSQLiteTables)[number]
|
||||
>({
|
||||
table: "computers",
|
||||
query: {
|
||||
query: {
|
||||
user_id: { value: user_id },
|
||||
kind: { value: "main" },
|
||||
},
|
||||
order: { field: "id", strategy: "ASC" },
|
||||
limit: 1,
|
||||
},
|
||||
})
|
||||
).payload?.[0];
|
||||
|
||||
if (existing?.id) {
|
||||
return existing.id;
|
||||
}
|
||||
|
||||
const container_id = await ensureMainComputerContainer();
|
||||
|
||||
const res = await BunSQLite.insert<
|
||||
BUN_SQLITE_COBBLE_COMPUTERS,
|
||||
(typeof BunSQLiteTables)[number]
|
||||
>({
|
||||
table: "computers",
|
||||
data: [
|
||||
{
|
||||
user_id: Number(user_id),
|
||||
container_id: Number(container_id),
|
||||
name: MainComputerName,
|
||||
kind: "main",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const computer_id = res.singleRes?.id || res.postInsertReturn?.insertId;
|
||||
|
||||
if (!res.success || !computer_id) {
|
||||
throw new Error(`Couldn't create the main computer: ${res.msg}`);
|
||||
}
|
||||
|
||||
return computer_id;
|
||||
}
|
||||
Reference in New Issue
Block a user