Share one crew across the install, keep user memory per user

Bots, workspaces and computers now belong to the install. Users reach
workers only through bot_users (assigned by a super admin or admin) and
workspace_users; the coordinator and main computer are one per install,
and only the super admin sees the coordinator. Conversations are per
user (bot_sessions.user_id), jobs run for the session's user, and live
updates go only to who should see them.

A bot's core memory is now general knowledge shared by every user it
works for; what it learns about a user lives in bot_user_memories.

One-off scripts move existing installs over: move-bot-memory-to-owners
(run first), then share-crew.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-19 11:37:14 +01:00
co-authored by Claude Opus 5.5
parent 4dd34ed251
commit e45d54d30b
133 changed files with 1854 additions and 436 deletions
@@ -0,0 +1,20 @@
import AppError from "@/src/utils/app-error";
import grabCrewRole from "./grab-crew-role";
type Params = {
user_id: number | string;
};
/**
* Throws unless the user manages the crew (a super admin or an admin).
* Returns their role.
*/
export default async function assertCrewManager({ user_id }: Params) {
const role = await grabCrewRole({ user_id });
if (role == "member") {
throw new AppError(`Only an admin can do that.`);
}
return role;
}
@@ -0,0 +1,30 @@
import BunSQLite from "@moduletrace/bun-sqlite";
type Params = {
bot_id: number | string;
};
/**
* Every user who sees a bot (`grabUserBot`), for live updates about it:
* the super admin, and for a worker the admins, the users it's assigned
* to and the members of its workspaces.
*/
export default async function grabBotAudience({ bot_id }: Params): Promise<number[]> {
const res = await BunSQLite.sql<{ user_id: number }>({
sql: `SELECT user_id FROM user_types WHERE user_type = 'super_admin'
UNION
SELECT ut.user_id FROM user_types ut JOIN bots b ON b.id = ? AND b.kind != 'coordinator'
WHERE ut.user_type = 'admin'
UNION
SELECT bu.user_id FROM bot_users bu JOIN bots b ON b.id = bu.bot_id AND b.kind != 'coordinator'
WHERE bu.bot_id = ?
UNION
SELECT wu.user_id FROM workspace_users wu
JOIN workspace_bots wb ON wb.workspace_id = wu.workspace_id AND wb.bot_id = ?
JOIN workspaces w ON w.id = wu.workspace_id AND w.archived = 0
JOIN bots b ON b.id = wb.bot_id AND b.kind != 'coordinator'`,
values: [bot_id, bot_id, bot_id],
});
return (res.payload || []).map((row) => Number(row.user_id)).filter(Boolean);
}
@@ -0,0 +1,26 @@
import BunSQLite from "@moduletrace/bun-sqlite";
type Params = {
computer_id: number | string;
};
/**
* Every user who sees a computer (`computersSql`), for live updates about
* it: super admins and admins, and the users whose workspaces or assigned
* workers use it.
*/
export default async function grabComputerAudience({ computer_id }: Params): Promise<number[]> {
const res = await BunSQLite.sql<{ user_id: number }>({
sql: `SELECT user_id FROM user_types WHERE user_type IN ('super_admin', 'admin')
UNION
SELECT wu.user_id FROM workspace_users wu
JOIN workspace_computers wc ON wc.workspace_id = wu.workspace_id AND wc.computer_id = ?
UNION
SELECT bu.user_id FROM bot_users bu
JOIN bot_computers bc ON bc.bot_id = bu.bot_id AND bc.computer_id = ?
JOIN bots b ON b.id = bu.bot_id AND b.kind != 'coordinator'`,
values: [computer_id, computer_id],
});
return (res.payload || []).map((row) => Number(row.user_id)).filter(Boolean);
}
@@ -0,0 +1,19 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS, BUN_SQLITE_COBBLE_WORKSPACES } from "@/db/types/db";
/**
* Every worker and every workspace (not archived), for picking what a
* user may reach. Never the coordinator: it's the super admin's alone.
*/
export default async function grabCrewOptions() {
const [bots, workspaces] = await Promise.all([
BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
sql: `SELECT id, name, role FROM bots WHERE kind = 'worker' ORDER BY name COLLATE NOCASE ASC, id ASC`,
}),
BunSQLite.sql<BUN_SQLITE_COBBLE_WORKSPACES>({
sql: `SELECT id, name FROM workspaces WHERE archived = 0 ORDER BY name COLLATE NOCASE ASC, id ASC`,
}),
]);
return { crew_bots: bots.payload || [], crew_workspaces: workspaces.payload || [] };
}
@@ -0,0 +1,13 @@
import crewRole from "@/src/utils/rbac/crew-role";
import grabUserTypes from "../users/grab-user-types";
type Params = {
user_id: number | string;
};
/**
* A user's `crewRole`, read from their user types.
*/
export default async function grabCrewRole({ user_id }: Params) {
return crewRole({ user_types: await grabUserTypes({ user_id }) });
}
@@ -0,0 +1,41 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS, BUN_SQLITE_COBBLE_MEDIA, BUN_SQLITE_COBBLE_WORKSPACES } from "@/db/types/db";
import grabUserBot from "../bots/grab-user-bot";
import grabUserWorkspace from "../workspaces/grab-user-workspace";
type Params = {
media_id: number | string;
user_id: number | string;
};
/**
* A bot's or workspace's picture (private media, uploaded by whoever set
* it) for a user who reaches that bot or workspace, or null. Bots and
* workspaces belong to the install, so their pictures show to everyone
* who sees them.
*/
export default async function grabSharedPictureMedia({ media_id, user_id }: Params) {
const [bot, workspace] = await Promise.all([
BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
sql: `SELECT id FROM bots WHERE image_media_id = ? LIMIT 1`,
values: [media_id],
}).then((res) => res.payload?.[0]),
BunSQLite.sql<BUN_SQLITE_COBBLE_WORKSPACES>({
sql: `SELECT id FROM workspaces WHERE image_media_id = ? LIMIT 1`,
values: [media_id],
}).then((res) => res.payload?.[0]),
]);
const allowed =
(bot?.id && (await grabUserBot({ bot_id: bot.id, user_id }))?.id) ||
(workspace?.id && (await grabUserWorkspace({ workspace_id: workspace.id, user_id }))?.id);
if (!allowed) return null;
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_MEDIA>({
sql: `SELECT * FROM media WHERE id = ? LIMIT 1`,
values: [media_id],
});
return res.payload?.[0] || null;
}
@@ -0,0 +1,49 @@
import BunSQLite from "@moduletrace/bun-sqlite";
type Params = {
user_id: number | string;
/**
* Every worker assigned to them, replacing the old list. The
* coordinator is never assigned. Left out: unchanged.
*/
bot_ids?: number[];
/**
* Every workspace they're in, replacing the old list. Left out:
* unchanged.
*/
workspace_ids?: number[];
};
/**
* Sets which workers a user chats with in their own chat (`bot_users`)
* and which workspaces they work in (`workspace_users`). The caller checks
* the actor manages the crew. Ids that aren't a worker or a workspace are
* skipped.
*/
export default async function setUserAccess({ user_id, bot_ids, workspace_ids }: Params) {
const now = Date.now();
if (bot_ids) {
await BunSQLite.sql({ sql: `DELETE FROM bot_users WHERE user_id = ?`, values: [user_id] });
for (const bot_id of new Set(bot_ids)) {
await BunSQLite.sql({
sql: `INSERT OR IGNORE INTO bot_users (created_at, updated_at, bot_id, user_id)
SELECT ?, ?, id, ? FROM bots WHERE id = ? AND kind = 'worker'`,
values: [now, now, user_id, bot_id],
});
}
}
if (workspace_ids) {
await BunSQLite.sql({ sql: `DELETE FROM workspace_users WHERE user_id = ?`, values: [user_id] });
for (const workspace_id of new Set(workspace_ids)) {
await BunSQLite.sql({
sql: `INSERT OR IGNORE INTO workspace_users (created_at, updated_at, workspace_id, user_id)
SELECT ?, ?, id, ? FROM workspaces WHERE id = ?`,
values: [now, now, user_id, workspace_id],
});
}
}
}