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,10 +9,10 @@ import { logError } from "@/src/functions/backend/logger";
|
||||
import createKeyLock from "@/src/utils/create-key-lock";
|
||||
import HarnessError from "@/src/harness/harness-error";
|
||||
import deferBotJob from "@/src/functions/backend/db/bots-queue/defer-bot-job";
|
||||
import { BotWaitingStatusLine } from "@/src/dict/bot-queue-dict";
|
||||
import { BotReplyJobTitle, BotTaskStatusLine, BotWaitingStatusLine } from "@/src/dict/bot-queue-dict";
|
||||
import BotJobFatalError from "./bot-job-fatal-error";
|
||||
import describeJobStep from "./describe-job-step";
|
||||
import pushToBotOwner from "./push-to-bot-owner";
|
||||
import pushToSessionUser from "./push-to-session-user";
|
||||
import pushBotNotice from "./push-bot-notice";
|
||||
import runBotJob from "./run-bot-job";
|
||||
import parseJobJson from "./parse-job-json";
|
||||
@@ -80,8 +80,8 @@ export default async function processBotJob({
|
||||
|
||||
const row = await insertBotJobUpdate({ job_id, bot_id, ...described });
|
||||
|
||||
await pushToBotOwner({
|
||||
bot_id,
|
||||
await pushToSessionUser({
|
||||
session_id: job.session_id,
|
||||
data: {
|
||||
event: "server:bot-job-update",
|
||||
data: {
|
||||
@@ -111,7 +111,7 @@ export default async function processBotJob({
|
||||
bot: await setBotFace({
|
||||
bot_id,
|
||||
face: "working",
|
||||
status_line: job.title || "",
|
||||
status_line: job.kind == "reply" ? BotReplyJobTitle : BotTaskStatusLine,
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@ import type {
|
||||
BUN_SQLITE_COBBLE_BOT_MESSAGES,
|
||||
BUN_SQLITE_COBBLE_BOTS,
|
||||
} from "@/db/types/db";
|
||||
import grabActiveBotTask from "@/src/functions/backend/db/bots/grab-active-bot-task";
|
||||
import pushToBotOwner from "./push-to-bot-owner";
|
||||
import pushBotUpdated from "./push-bot-updated";
|
||||
import pushToSessionUser from "./push-to-session-user";
|
||||
|
||||
type Params = {
|
||||
bot_id: number | string;
|
||||
@@ -17,32 +17,27 @@ type Params = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Pushes a new bot message and/or face change to the owner's open tabs.
|
||||
* A face change carries the bot's active task, for the working-on panel.
|
||||
* A relayed report goes to the conversation it was posted in.
|
||||
* Pushes a new bot message to the open tabs of the user whose
|
||||
* conversation it's in, and a face change to everyone who sees the bot
|
||||
* (`pushBotUpdated`, each with their own active task). A relayed report
|
||||
* goes to the conversation it was posted in.
|
||||
*/
|
||||
export default async function pushBotNotice({ bot_id, message, bot, relay }: Params) {
|
||||
if (message) {
|
||||
await pushToBotOwner({
|
||||
bot_id,
|
||||
await pushToSessionUser({
|
||||
session_id: message.session_id,
|
||||
data: { event: "server:bot-message", data: message },
|
||||
});
|
||||
}
|
||||
|
||||
if (relay) {
|
||||
await pushToBotOwner({
|
||||
bot_id: relay.bot_id,
|
||||
await pushToSessionUser({
|
||||
session_id: relay.message.session_id,
|
||||
data: { event: "server:bot-message", data: relay.message },
|
||||
});
|
||||
}
|
||||
|
||||
if (bot) {
|
||||
await pushToBotOwner({
|
||||
bot_id,
|
||||
data: {
|
||||
event: "server:bot-updated",
|
||||
data: { ...bot, task: await grabActiveBotTask({ bot_id }) },
|
||||
},
|
||||
});
|
||||
await pushBotUpdated({ bot: { ...bot, id: bot.id || Number(bot_id) } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
|
||||
import grabBotAudience from "@/src/functions/backend/db/access/grab-bot-audience";
|
||||
import grabActiveBotTask from "@/src/functions/backend/db/bots/grab-active-bot-task";
|
||||
import { sendToUser } from "@/src/websocket/(functions)/send-to-user";
|
||||
|
||||
type Params = {
|
||||
bot: Pick<BUN_SQLITE_COBBLE_BOTS, "id" | "face" | "status_line" | "computer_id">;
|
||||
/**
|
||||
* Only these users (the connected ones, for the live relay); else
|
||||
* everyone who sees the bot.
|
||||
*/
|
||||
users?: Set<string>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pushes `server:bot-updated` (face, status line, computer) to everyone
|
||||
* who sees the bot (`grabBotAudience`), each with their own active task
|
||||
* with it, never another user's.
|
||||
*/
|
||||
export default async function pushBotUpdated({ bot, users }: Params) {
|
||||
if (!bot.id) return;
|
||||
|
||||
for (const user_id of await grabBotAudience({ bot_id: bot.id })) {
|
||||
if (users && !users.has(String(user_id))) continue;
|
||||
|
||||
sendToUser({
|
||||
user_id,
|
||||
data: {
|
||||
event: "server:bot-updated",
|
||||
data: {
|
||||
id: bot.id,
|
||||
face: bot.face,
|
||||
status_line: bot.status_line || "",
|
||||
...(bot.computer_id !== undefined ? { computer_id: bot.computer_id || null } : {}),
|
||||
task: await grabActiveBotTask({ bot_id: bot.id, user_id }),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
|
||||
import type { WebSocketDataType } from "@/src/types";
|
||||
import { sendToUser } from "@/src/websocket/(functions)/send-to-user";
|
||||
|
||||
type Params = {
|
||||
bot_id: number | string;
|
||||
data: WebSocketDataType;
|
||||
};
|
||||
|
||||
/**
|
||||
* Live update to the bot owner's open tabs. Best effort: nothing is queued
|
||||
* for offline users, who see the change on their next page load.
|
||||
*/
|
||||
export default async function pushToBotOwner({ bot_id, data }: Params) {
|
||||
const bot = (
|
||||
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
|
||||
sql: `SELECT user_id FROM bots WHERE id = ? LIMIT 1`,
|
||||
values: [bot_id],
|
||||
})
|
||||
).payload?.[0];
|
||||
|
||||
if (!bot?.user_id) return;
|
||||
|
||||
sendToUser({ user_id: bot.user_id, data });
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_COBBLE_BOT_SESSIONS } from "@/db/types/db";
|
||||
import type { WebSocketDataType } from "@/src/types";
|
||||
import { sendToUser } from "@/src/websocket/(functions)/send-to-user";
|
||||
|
||||
type Params = {
|
||||
session_id?: number | string | null;
|
||||
data: WebSocketDataType;
|
||||
};
|
||||
|
||||
/**
|
||||
* Live update to the open tabs of the user a session belongs to: only
|
||||
* they see that conversation. Best effort: nothing is queued for offline
|
||||
* users, who see the change on their next page load.
|
||||
*/
|
||||
export default async function pushToSessionUser({ session_id, data }: Params) {
|
||||
if (!session_id) return;
|
||||
|
||||
const session = (
|
||||
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOT_SESSIONS>({
|
||||
sql: `SELECT user_id FROM bot_sessions WHERE id = ? LIMIT 1`,
|
||||
values: [session_id],
|
||||
})
|
||||
).payload?.[0];
|
||||
|
||||
if (!session?.user_id) return;
|
||||
|
||||
sendToUser({ user_id: session.user_id, data });
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_COBBLE_BOT_SESSIONS } from "@/db/types/db";
|
||||
import grabBotById from "@/src/functions/backend/db/bots/grab-bot-by-id";
|
||||
import jobSessionId from "@/src/functions/backend/db/bots-queue/job-session-id";
|
||||
import HarnessError from "@/src/harness/harness-error";
|
||||
import BotJobFatalError from "./bot-job-fatal-error";
|
||||
import runBotReply from "./run-bot-reply";
|
||||
@@ -9,6 +12,9 @@ import type { BotJobRunner } from "./types";
|
||||
* Does a job's actual work on the harness: a `reply` answers the user in
|
||||
* chat (`runBotReply`), a `task` works through its brief (`runBotTask`).
|
||||
*
|
||||
* It works for the user whose conversation the job is in: their memory,
|
||||
* and only what they can reach.
|
||||
*
|
||||
* Harness failures that waiting can fix (rate limits, outages) are thrown
|
||||
* as they are, so the queue retries with backoff; the rest are fatal.
|
||||
*/
|
||||
@@ -16,16 +22,28 @@ const runBotJob: BotJobRunner = async (context) => {
|
||||
const { job } = context;
|
||||
const bot = job.bot_id ? await grabBotById({ bot_id: job.bot_id }) : null;
|
||||
|
||||
if (!bot?.id || !bot.user_id) {
|
||||
if (!bot?.id) {
|
||||
throw new BotJobFatalError(`The bot for this job no longer exists.`);
|
||||
}
|
||||
|
||||
const owned = { ...bot, id: bot.id, user_id: bot.user_id };
|
||||
const session = (
|
||||
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOT_SESSIONS>({
|
||||
sql: `SELECT user_id FROM bot_sessions WHERE id = ? LIMIT 1`,
|
||||
values: [jobSessionId({ job })],
|
||||
})
|
||||
).payload?.[0];
|
||||
|
||||
if (!session?.user_id) {
|
||||
throw new BotJobFatalError(`The conversation for this job no longer exists.`);
|
||||
}
|
||||
|
||||
const owned = { ...bot, id: bot.id };
|
||||
const user_id = session.user_id;
|
||||
|
||||
try {
|
||||
return job.kind == "reply"
|
||||
? await runBotReply({ ...context, bot: owned })
|
||||
: await runBotTask({ ...context, bot: owned });
|
||||
? await runBotReply({ ...context, bot: owned, user_id })
|
||||
: await runBotTask({ ...context, bot: owned, user_id });
|
||||
} catch (error: unknown) {
|
||||
if (
|
||||
error instanceof HarnessError &&
|
||||
|
||||
@@ -20,7 +20,11 @@ import withTakeOverRefs from "./with-take-over-refs";
|
||||
import type { BotJobContext, BotJobOutcome } from "./types";
|
||||
|
||||
type Params = BotJobContext & {
|
||||
bot: BUN_SQLITE_COBBLE_BOTS & { id: number; user_id: number };
|
||||
bot: BUN_SQLITE_COBBLE_BOTS & { id: number };
|
||||
/**
|
||||
* The user whose conversation the job is in.
|
||||
*/
|
||||
user_id: number;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -35,6 +39,7 @@ type Params = BotJobContext & {
|
||||
*/
|
||||
export default async function runBotReply({
|
||||
bot,
|
||||
user_id,
|
||||
job,
|
||||
signal,
|
||||
report,
|
||||
@@ -51,8 +56,8 @@ export default async function runBotReply({
|
||||
return { result: "", silent: true };
|
||||
}
|
||||
|
||||
const prompt = await grabBotPrompt({ bot_id: bot.id, user_id: bot.user_id, workspace_id });
|
||||
const computers = await grabBotComputers({ bot_id: bot.id, user_id: bot.user_id, workspace_id });
|
||||
const prompt = await grabBotPrompt({ bot_id: bot.id, user_id, workspace_id });
|
||||
const computers = await grabBotComputers({ bot_id: bot.id, workspace_id });
|
||||
|
||||
/**
|
||||
* A summary that can't be written now (no model free) is left out
|
||||
@@ -69,7 +74,7 @@ export default async function runBotReply({
|
||||
|
||||
const tools = await grabBotChatTools({
|
||||
bot,
|
||||
user_id: bot.user_id,
|
||||
user_id,
|
||||
computers,
|
||||
session_id,
|
||||
workspace_id,
|
||||
|
||||
@@ -14,7 +14,11 @@ import withTakeOverRefs from "./with-take-over-refs";
|
||||
import type { BotJobContext, BotJobOutcome } from "./types";
|
||||
|
||||
type Params = BotJobContext & {
|
||||
bot: { id: number; user_id: number };
|
||||
bot: { id: number };
|
||||
/**
|
||||
* The user whose conversation the job is in.
|
||||
*/
|
||||
user_id: number;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -28,6 +32,7 @@ type Params = BotJobContext & {
|
||||
*/
|
||||
export default async function runBotTask({
|
||||
bot,
|
||||
user_id,
|
||||
job,
|
||||
params,
|
||||
signal,
|
||||
@@ -46,8 +51,8 @@ export default async function runBotTask({
|
||||
|
||||
try {
|
||||
const session_id = await jobSessionId({ job });
|
||||
const prompt = await grabBotPrompt({ bot_id: bot.id, user_id: bot.user_id, workspace_id });
|
||||
const computers = await grabBotComputers({ bot_id: bot.id, user_id: bot.user_id, workspace_id });
|
||||
const prompt = await grabBotPrompt({ bot_id: bot.id, user_id, workspace_id });
|
||||
const computers = await grabBotComputers({ bot_id: bot.id, workspace_id });
|
||||
const carry_over = await grabSessionCarryOver({
|
||||
session_id,
|
||||
bot_id: bot.id,
|
||||
@@ -97,7 +102,7 @@ export default async function runBotTask({
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
...grabBotTools({ user_id: bot.user_id, bot_id: bot.id, workspace_id, computers }),
|
||||
...grabBotTools({ user_id, bot_id: bot.id, workspace_id, computers }),
|
||||
...(computers.length
|
||||
? [
|
||||
searchComputersTool({ computers, workspace_id }),
|
||||
|
||||
Reference in New Issue
Block a user