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:
2026-10-18 18:33:54 +01:00
co-authored by Claude Opus 5.5
parent 694c83346f
commit d49f524fe8
72 changed files with 2961 additions and 31 deletions
@@ -0,0 +1,32 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import AppError from "@/src/utils/app-error";
import grabUserBot from "../bots/grab-user-bot";
import grabUserWorkspace from "./grab-user-workspace";
type Params = {
user_id: number | string;
workspace_id: number | string;
bot_id: number | string;
};
/**
* Assigns one of the user's bots to one of their workspaces. Assigning a
* bot that is already there is a no-op.
*/
export default async function addBotToWorkspace({
user_id,
workspace_id,
bot_id,
}: Params) {
const bot = await grabUserBot({ bot_id, user_id });
const workspace = await grabUserWorkspace({ workspace_id, user_id });
if (!bot?.id || !workspace?.id) {
throw new AppError(`Bot or workspace not found.`);
}
await BunSQLite.sql({
sql: `INSERT OR IGNORE INTO workspace_bots (workspace_id, bot_id, created_at, updated_at) VALUES (?, ?, ?, ?)`,
values: [workspace.id, bot.id, Date.now(), Date.now()],
});
}
@@ -0,0 +1,32 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import AppError from "@/src/utils/app-error";
import grabUserComputer from "../computers/grab-user-computer";
import grabUserWorkspace from "./grab-user-workspace";
type Params = {
user_id: number | string;
workspace_id: number | string;
computer_id: number | string;
};
/**
* Assigns one of the user's computers to one of their workspaces, so every
* bot in the workspace gets access to it. Assigning it twice is a no-op.
*/
export default async function addComputerToWorkspace({
user_id,
workspace_id,
computer_id,
}: Params) {
const computer = await grabUserComputer({ computer_id, user_id });
const workspace = await grabUserWorkspace({ workspace_id, user_id });
if (!computer?.id || !workspace?.id) {
throw new AppError(`Computer or workspace not found.`);
}
await BunSQLite.sql({
sql: `INSERT OR IGNORE INTO workspace_computers (workspace_id, computer_id, created_at, updated_at) VALUES (?, ?, ?, ?)`,
values: [workspace.id, computer.id, Date.now(), Date.now()],
});
}
@@ -0,0 +1,49 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type {
BUN_SQLITE_COBBLE_WORKSPACES,
BunSQLiteTables,
} from "@/db/types/db";
import assertPromptLength from "@/src/utils/assert-prompt-length";
type Params = {
user_id: number | string;
name: string;
description?: string;
system_prompt?: string;
};
/**
* Creates a workspace owned by the user and returns its id.
*/
export default async function createWorkspace({
user_id,
name,
description,
system_prompt,
}: Params) {
assertPromptLength({ label: "System prompt", text: system_prompt });
const res = await BunSQLite.insert<
BUN_SQLITE_COBBLE_WORKSPACES,
(typeof BunSQLiteTables)[number]
>({
table: "workspaces",
data: [
{
user_id: Number(user_id),
name,
description,
system_prompt,
archived: 0,
},
],
});
const workspace_id = res.singleRes?.id || res.postInsertReturn?.insertId;
if (!res.success || !workspace_id) {
throw new Error(`Couldn't create the workspace: ${res.msg}`);
}
return workspace_id;
}
@@ -0,0 +1,22 @@
import type { BUN_SQLITE_COBBLE_WORKSPACES } from "@/db/types/db";
import BunSQLite from "@moduletrace/bun-sqlite";
type Params = {
workspace_id: number | string;
user_id: number | string;
};
/**
* A workspace only if it belongs to the given owner.
*/
export default async function grabUserWorkspace({
workspace_id,
user_id,
}: Params): Promise<BUN_SQLITE_COBBLE_WORKSPACES | null> {
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_WORKSPACES>({
sql: `SELECT * FROM workspaces WHERE id = ? AND user_id = ? LIMIT 1`,
values: [workspace_id, user_id],
});
return res.payload?.[0] || null;
}
@@ -0,0 +1,29 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import AppError from "@/src/utils/app-error";
import grabUserWorkspace from "./grab-user-workspace";
type Params = {
user_id: number | string;
workspace_id: number | string;
bot_id: number | string;
};
/**
* Removes a bot from one of the user's workspaces.
*/
export default async function removeBotFromWorkspace({
user_id,
workspace_id,
bot_id,
}: Params) {
const workspace = await grabUserWorkspace({ workspace_id, user_id });
if (!workspace?.id) {
throw new AppError(`Workspace not found.`);
}
await BunSQLite.sql({
sql: `DELETE FROM workspace_bots WHERE workspace_id = ? AND bot_id = ?`,
values: [workspace.id, bot_id],
});
}
@@ -0,0 +1,30 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import AppError from "@/src/utils/app-error";
import grabUserWorkspace from "./grab-user-workspace";
type Params = {
user_id: number | string;
workspace_id: number | string;
computer_id: number | string;
};
/**
* Takes a computer off one of the user's workspaces. Its bots keep any
* access they were given directly.
*/
export default async function removeComputerFromWorkspace({
user_id,
workspace_id,
computer_id,
}: Params) {
const workspace = await grabUserWorkspace({ workspace_id, user_id });
if (!workspace?.id) {
throw new AppError(`Workspace not found.`);
}
await BunSQLite.sql({
sql: `DELETE FROM workspace_computers WHERE workspace_id = ? AND computer_id = ?`,
values: [workspace.id, computer_id],
});
}
@@ -0,0 +1,64 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type {
BUN_SQLITE_COBBLE_WORKSPACES,
BunSQLiteTables,
} from "@/db/types/db";
import AppError from "@/src/utils/app-error";
import assertPromptLength from "@/src/utils/assert-prompt-length";
import grabUserWorkspace from "./grab-user-workspace";
import _ from "lodash";
type Params = {
user_id: number | string;
workspace_id: number | string;
/**
* Only the fields given are changed.
*/
data: Pick<
BUN_SQLITE_COBBLE_WORKSPACES,
"name" | "description" | "system_prompt" | "archived"
>;
};
/**
* Updates one of the user's workspaces. Its `system_prompt` is shared
* context for every bot in it.
*/
export default async function updateWorkspace({
user_id,
workspace_id,
data,
}: Params) {
assertPromptLength({ label: "System prompt", text: data.system_prompt });
const workspace = await grabUserWorkspace({ workspace_id, user_id });
if (!workspace?.id) {
throw new AppError(`Workspace not found.`);
}
const res = await BunSQLite.update<
BUN_SQLITE_COBBLE_WORKSPACES,
(typeof BunSQLiteTables)[number]
>({
table: "workspaces",
targetId: workspace.id,
/**
* `BunSQLite.update` writes every key it gets, so a field left out
* here would be set to null.
*/
data: _.omitBy(
{
name: data.name,
description: data.description,
system_prompt: data.system_prompt,
archived: data.archived,
},
_.isUndefined,
),
});
if (!res.success) {
throw new Error(`Couldn't update the workspace: ${res.msg}`);
}
}