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:
@@ -9,7 +9,7 @@ type Params = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Cancels one of the user's pending or running jobs. It's marked failed
|
||||
* Cancels a pending or running job in one of the user's conversations. It's marked failed
|
||||
* with `BotJobCancelledError`; a running handler finds out on its next
|
||||
* `updateBotJob` (which returns false) and stops. Its task is closed.
|
||||
*/
|
||||
@@ -19,7 +19,7 @@ export default async function cancelBotJob({ user_id, job_id }: Params) {
|
||||
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS_QUEUE>({
|
||||
sql: `UPDATE bots_queue SET status = 'failed', last_error = ?, locked_at = NULL, locked_by = NULL, finished_at = ?, updated_at = ?
|
||||
WHERE id = ? AND status IN ('pending', 'processing')
|
||||
AND bot_id IN (SELECT id FROM bots WHERE user_id = ?)
|
||||
AND session_id IN (SELECT id FROM bot_sessions WHERE user_id = ?)
|
||||
RETURNING *`,
|
||||
values: [BotJobCancelledError, now, now, job_id, user_id],
|
||||
});
|
||||
|
||||
@@ -115,7 +115,7 @@ export default async function enqueueBotJob({
|
||||
}
|
||||
|
||||
if (computer_id) {
|
||||
const computers = await grabBotComputers({ bot_id: bot.id, user_id, workspace_id });
|
||||
const computers = await grabBotComputers({ bot_id: bot.id, workspace_id });
|
||||
|
||||
if (!computers.some((c) => String(c.id) == String(computer_id))) {
|
||||
throw new AppError(`This bot has no access to that computer.`);
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE } from "@/db/types/db";
|
||||
import ensureBotSession from "../bot-sessions/ensure-bot-session";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
|
||||
type Params = {
|
||||
job: Pick<BUN_SQLITE_COBBLE_BOTS_QUEUE, "bot_id" | "session_id" | "workspace_id">;
|
||||
job: Pick<BUN_SQLITE_COBBLE_BOTS_QUEUE, "session_id">;
|
||||
};
|
||||
|
||||
/**
|
||||
* The session a job's result goes in: its own, or (for a job queued
|
||||
* without one) the active session of its bot's conversation there.
|
||||
* The session a job's result goes in. Every job is queued in one
|
||||
* (`enqueueBotJob`), which says whose conversation it is.
|
||||
*/
|
||||
export default async function jobSessionId({ job }: Params) {
|
||||
if (job.session_id) return Number(job.session_id);
|
||||
export default function jobSessionId({ job }: Params) {
|
||||
if (!job.session_id) {
|
||||
throw new AppError(`This job has no conversation.`);
|
||||
}
|
||||
|
||||
const session = await ensureBotSession({
|
||||
bot_id: job.bot_id!,
|
||||
workspace_id: job.workspace_id || null,
|
||||
});
|
||||
|
||||
return Number(session.id);
|
||||
return Number(job.session_id);
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ type Params = {
|
||||
/**
|
||||
* Reports a handed-over job back to the bot that handed it over
|
||||
* (`requested_by_bot_id`, the coordinator): a message in the conversation
|
||||
* it was handed over from (the active session there, which may have moved
|
||||
* on since), with the worker's report, flagged needs-you when the job
|
||||
* it was handed over from (that user's active session there, which may
|
||||
* have moved on since), with the worker's report, flagged needs-you when the job
|
||||
* failed. Returns the message for the live push, or null when nobody
|
||||
* handed the job over.
|
||||
*/
|
||||
@@ -48,14 +48,16 @@ export default async function relayJobToRequester({ job, text, failed }: Params)
|
||||
const requested_from = job.requested_by_session_id
|
||||
? (
|
||||
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOT_SESSIONS>({
|
||||
sql: `SELECT workspace_id FROM bot_sessions WHERE id = ? AND bot_id = ? LIMIT 1`,
|
||||
sql: `SELECT user_id, workspace_id FROM bot_sessions WHERE id = ? AND bot_id = ? LIMIT 1`,
|
||||
values: [job.requested_by_session_id, requester.id],
|
||||
})
|
||||
).payload?.[0]
|
||||
: null;
|
||||
|
||||
const workspace_id = requested_from ? requested_from.workspace_id || null : job.workspace_id || null;
|
||||
const session = await ensureBotSession({ bot_id: requester.id, workspace_id });
|
||||
if (!requested_from?.user_id) return null;
|
||||
|
||||
const workspace_id = requested_from.workspace_id || null;
|
||||
const session = await ensureBotSession({ user_id: requested_from.user_id, bot_id: requester.id, workspace_id });
|
||||
|
||||
const name = worker?.name || "A crew member";
|
||||
const title = job.title || "the job";
|
||||
|
||||
Reference in New Issue
Block a user