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:
@@ -2,6 +2,7 @@ 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";
|
||||
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
@@ -18,6 +19,8 @@ export default async function addBotToWorkspace({
|
||||
workspace_id,
|
||||
bot_id,
|
||||
}: Params) {
|
||||
await assertCrewManager({ user_id });
|
||||
|
||||
const bot = await grabUserBot({ bot_id, user_id });
|
||||
const workspace = await grabUserWorkspace({ workspace_id, user_id });
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ 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";
|
||||
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
@@ -19,6 +20,8 @@ export default async function addComputerToWorkspace({
|
||||
workspace_id,
|
||||
computer_id,
|
||||
}: Params) {
|
||||
await assertCrewManager({ user_id });
|
||||
|
||||
const computer = await grabUserComputer({ computer_id, user_id });
|
||||
const workspace = await grabUserWorkspace({ workspace_id, user_id });
|
||||
|
||||
|
||||
@@ -4,8 +4,13 @@ import type {
|
||||
BunSQLiteTables,
|
||||
} from "@/db/types/db";
|
||||
import assertPromptLength from "@/src/utils/assert-prompt-length";
|
||||
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
|
||||
|
||||
type Params = {
|
||||
/**
|
||||
* Who creates it (a super admin or admin). Workspaces belong to the
|
||||
* install.
|
||||
*/
|
||||
user_id: number | string;
|
||||
name: string;
|
||||
description?: string;
|
||||
@@ -13,7 +18,8 @@ type Params = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a workspace owned by the user and returns its id.
|
||||
* Creates a workspace and returns its id. Members reach it once they're
|
||||
* added to it (`workspace_users`).
|
||||
*/
|
||||
export default async function createWorkspace({
|
||||
user_id,
|
||||
@@ -21,6 +27,8 @@ export default async function createWorkspace({
|
||||
description,
|
||||
system_prompt,
|
||||
}: Params) {
|
||||
await assertCrewManager({ user_id });
|
||||
|
||||
assertPromptLength({ label: "System prompt", text: system_prompt });
|
||||
|
||||
const res = await BunSQLite.insert<
|
||||
|
||||
@@ -2,6 +2,7 @@ import deleteMedia from "@/src/functions/backend/db/media/delete-media";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import grabUserWorkspace from "./grab-user-workspace";
|
||||
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
@@ -13,6 +14,8 @@ type Params = {
|
||||
* links and its models. Its bots and computers stay.
|
||||
*/
|
||||
export default async function deleteWorkspace({ user_id, workspace_id }: Params) {
|
||||
await assertCrewManager({ user_id });
|
||||
|
||||
const workspace = await grabUserWorkspace({ workspace_id, user_id });
|
||||
|
||||
if (!workspace?.id) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import type { BUN_SQLITE_COBBLE_WORKSPACES } from "@/db/types/db";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import workspacesSql from "@/src/utils/crew-access/workspaces-sql";
|
||||
import grabCrewRole from "../access/grab-crew-role";
|
||||
|
||||
type Params = {
|
||||
workspace_id: number | string;
|
||||
@@ -7,15 +9,17 @@ type Params = {
|
||||
};
|
||||
|
||||
/**
|
||||
* A workspace only if it belongs to the given owner.
|
||||
* A workspace only if the user works in it (`workspacesSql`).
|
||||
*/
|
||||
export default async function grabUserWorkspace({
|
||||
workspace_id,
|
||||
user_id,
|
||||
}: Params): Promise<BUN_SQLITE_COBBLE_WORKSPACES | null> {
|
||||
const access = workspacesSql({ role: await grabCrewRole({ user_id }), user_id });
|
||||
|
||||
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],
|
||||
sql: `SELECT w.* FROM workspaces w WHERE w.id = ? AND ${access.sql} LIMIT 1`,
|
||||
values: [workspace_id, ...access.values],
|
||||
});
|
||||
|
||||
return res.payload?.[0] || null;
|
||||
|
||||
@@ -1,25 +1,30 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { WorkspaceWithActivity } from "@/src/types";
|
||||
import workspacesSql from "@/src/utils/crew-access/workspaces-sql";
|
||||
import grabCrewRole from "../access/grab-crew-role";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* The user's workspaces for the rail, not archived, oldest first, each
|
||||
* unread while any conversation in it is.
|
||||
* The workspaces the user works in (`workspacesSql`), for the rail: not
|
||||
* archived, oldest first, each unread while any of the user's
|
||||
* conversations in it is.
|
||||
*/
|
||||
export default async function grabUserWorkspaces({
|
||||
user_id,
|
||||
}: Params): Promise<WorkspaceWithActivity[]> {
|
||||
const access = workspacesSql({ role: await grabCrewRole({ user_id }), user_id });
|
||||
|
||||
const res = await BunSQLite.sql<WorkspaceWithActivity>({
|
||||
sql: `SELECT w.*,
|
||||
COALESCE((SELECT MAX(s.unread) FROM bot_sessions s WHERE s.workspace_id = w.id), 0) AS unread,
|
||||
(SELECT MAX(s.last_activity_at) FROM bot_sessions s WHERE s.workspace_id = w.id) AS last_activity_at
|
||||
COALESCE((SELECT MAX(s.unread) FROM bot_sessions s WHERE s.workspace_id = w.id AND s.user_id = ?), 0) AS unread,
|
||||
(SELECT MAX(s.last_activity_at) FROM bot_sessions s WHERE s.workspace_id = w.id AND s.user_id = ?) AS last_activity_at
|
||||
FROM workspaces w
|
||||
WHERE w.user_id = ? AND w.archived = 0
|
||||
WHERE w.archived = 0 AND ${access.sql}
|
||||
ORDER BY w.id ASC`,
|
||||
values: [user_id],
|
||||
values: [user_id, user_id, ...access.values],
|
||||
});
|
||||
|
||||
return res.payload || [];
|
||||
|
||||
@@ -4,6 +4,9 @@ import type {
|
||||
BUN_SQLITE_COBBLE_WORKSPACES,
|
||||
} from "@/db/types/db";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import workspaceBotsSql from "@/src/utils/crew-access/workspace-bots-sql";
|
||||
import workspacesSql from "@/src/utils/crew-access/workspaces-sql";
|
||||
import grabCrewRole from "../access/grab-crew-role";
|
||||
|
||||
type Params = {
|
||||
workspace_id: number | string;
|
||||
@@ -22,29 +25,37 @@ export type WorkspaceBotMembership = {
|
||||
};
|
||||
|
||||
/**
|
||||
* One of the user's bots in one of their (non-archived) workspaces: a
|
||||
* member, or the coordinator, which is in all of them. Null otherwise.
|
||||
* A bot the user chats with in a workspace they're in (not archived): one
|
||||
* of its workers, or for the super admin the coordinator, which is in all
|
||||
* of them (`workspaceBotsSql`). Null otherwise.
|
||||
*/
|
||||
export default async function grabWorkspaceBot({
|
||||
workspace_id,
|
||||
bot_id,
|
||||
user_id,
|
||||
}: Params): Promise<WorkspaceBotMembership | null> {
|
||||
const role = await grabCrewRole({ user_id });
|
||||
const workspace_access = workspacesSql({ role, user_id });
|
||||
|
||||
const workspace = (
|
||||
await BunSQLite.sql<BUN_SQLITE_COBBLE_WORKSPACES>({
|
||||
sql: `SELECT * FROM workspaces WHERE id = ? AND user_id = ? AND archived = 0 LIMIT 1`,
|
||||
values: [workspace_id, user_id],
|
||||
sql: `SELECT w.* FROM workspaces w WHERE w.id = ? AND w.archived = 0 AND ${workspace_access.sql} LIMIT 1`,
|
||||
values: [workspace_id, ...workspace_access.values],
|
||||
})
|
||||
).payload?.[0];
|
||||
|
||||
if (!workspace?.id) return null;
|
||||
|
||||
const bot_access = workspaceBotsSql({ role, workspace_id: workspace.id });
|
||||
|
||||
const bot = (
|
||||
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
|
||||
sql: `SELECT * FROM bots WHERE id = ? AND user_id = ? LIMIT 1`,
|
||||
values: [bot_id, user_id],
|
||||
sql: `SELECT b.* FROM bots b WHERE b.id = ? AND ${bot_access.sql} LIMIT 1`,
|
||||
values: [bot_id, ...bot_access.values],
|
||||
})
|
||||
).payload?.[0];
|
||||
|
||||
if (!workspace?.id || !bot?.id) return null;
|
||||
if (!bot?.id) return null;
|
||||
|
||||
const membership =
|
||||
(
|
||||
@@ -54,7 +65,5 @@ export default async function grabWorkspaceBot({
|
||||
})
|
||||
).payload?.[0] || null;
|
||||
|
||||
if (!membership && bot.kind != "coordinator") return null;
|
||||
|
||||
return { workspace, bot, membership };
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ export default async function grabWorkspaceComputers({
|
||||
const res = await BunSQLite.sql<ComputerWithStatus & { metadata?: string | null }>({
|
||||
sql: `SELECT c.*, co.status AS status, co.metadata AS metadata FROM computers c
|
||||
JOIN workspace_computers wc ON wc.computer_id = c.id AND wc.workspace_id = ?
|
||||
JOIN workspaces w ON w.id = wc.workspace_id AND w.user_id = c.user_id
|
||||
JOIN workspaces w ON w.id = wc.workspace_id
|
||||
LEFT JOIN containers co ON co.id = c.container_id
|
||||
ORDER BY c.id = w.computer_id DESC, wc.id ASC`,
|
||||
values: [workspace_id],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import grabUserWorkspace from "./grab-user-workspace";
|
||||
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
@@ -16,6 +17,8 @@ export default async function removeBotFromWorkspace({
|
||||
workspace_id,
|
||||
bot_id,
|
||||
}: Params) {
|
||||
await assertCrewManager({ user_id });
|
||||
|
||||
const workspace = await grabUserWorkspace({ workspace_id, user_id });
|
||||
|
||||
if (!workspace?.id) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import grabUserWorkspace from "./grab-user-workspace";
|
||||
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
@@ -18,6 +19,8 @@ export default async function removeComputerFromWorkspace({
|
||||
workspace_id,
|
||||
computer_id,
|
||||
}: Params) {
|
||||
await assertCrewManager({ user_id });
|
||||
|
||||
const workspace = await grabUserWorkspace({ workspace_id, user_id });
|
||||
|
||||
if (!workspace?.id) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import AppError from "@/src/utils/app-error";
|
||||
import assertPromptLength from "@/src/utils/assert-prompt-length";
|
||||
import grabUserWorkspace from "./grab-user-workspace";
|
||||
import _ from "lodash";
|
||||
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
@@ -29,6 +30,8 @@ export default async function updateWorkspace({
|
||||
workspace_id,
|
||||
data,
|
||||
}: Params) {
|
||||
await assertCrewManager({ user_id });
|
||||
|
||||
assertPromptLength({ label: "System prompt", text: data.system_prompt });
|
||||
|
||||
const workspace = await grabUserWorkspace({ workspace_id, user_id });
|
||||
|
||||
Reference in New Issue
Block a user