Files
cobble/src/functions/backend/db/computers/ensure-main-computer-container.ts
T
tbenandClaude Opus 5.5 d49f524fe8 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]>
2026-10-18 18:33:54 +01:00

61 lines
1.7 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import type {
BUN_SQLITE_COBBLE_CONTAINERS,
BunSQLiteTables,
} from "@/db/types/db";
import { AppData } from "@/src/data/app-data";
/**
* Returns the id of the main computer's container record, creating it if
* missing. The container itself is created at install, named
* `AppData.MainComputerContainerName` on the `AppData.DockerNetworkName`
* network; this only records it. Safe to call on every visit.
*/
export default async function ensureMainComputerContainer() {
const name = AppData.MainComputerContainerName;
const existing = (
await BunSQLite.select<
BUN_SQLITE_COBBLE_CONTAINERS,
(typeof BunSQLiteTables)[number]
>({
table: "containers",
query: {
query: {
name: { value: name },
},
limit: 1,
},
})
).payload?.[0];
if (existing?.id) {
return existing.id;
}
const res = await BunSQLite.insert<
BUN_SQLITE_COBBLE_CONTAINERS,
(typeof BunSQLiteTables)[number]
>({
table: "containers",
data: [
{
name,
purpose: "computer",
network: AppData.DockerNetworkName,
hostname: name,
port: AppData.ComputerPort,
status: "unknown",
},
],
});
const container_id = res.singleRes?.id || res.postInsertReturn?.insertId;
if (!res.success || !container_id) {
throw new Error(`Couldn't record the main computer's container: ${res.msg}`);
}
return container_id;
}