Give every user their own bot face
A bot's face and status line live in bot_user_faces, one row per bot and user, set by jobs for the user whose conversation they're in. Needs-you and working only show to that user; every bot read overlays the viewer's face (botFaceSql) and pushes go to the user a face change is for. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -14,6 +14,7 @@ import BotJobFatalError from "./bot-job-fatal-error";
|
||||
import describeJobStep from "./describe-job-step";
|
||||
import pushToSessionUser from "./push-to-session-user";
|
||||
import pushBotNotice from "./push-bot-notice";
|
||||
import grabJobUserId from "@/src/functions/backend/db/bots-queue/grab-job-user-id";
|
||||
import runBotJob from "./run-bot-job";
|
||||
import parseJobJson from "./parse-job-json";
|
||||
import type { BotJobUpdateData } from "@/src/types";
|
||||
@@ -58,6 +59,17 @@ export default async function processBotJob({
|
||||
|
||||
const job_id = job.id;
|
||||
const bot_id = job.bot_id;
|
||||
|
||||
/**
|
||||
* Its face changes only for the user it works for.
|
||||
*/
|
||||
const user_id = await grabJobUserId({ job }).catch(() => null);
|
||||
|
||||
const setFace = async (face: Omit<Parameters<typeof setBotFace>[0], "bot_id" | "user_id">) => {
|
||||
if (!user_id) return;
|
||||
|
||||
await pushBotNotice({ bot_id, bot: await setBotFace({ bot_id, user_id, ...face }) });
|
||||
};
|
||||
const controller = new AbortController();
|
||||
|
||||
const update: BotJobContext["update"] = async (params) => {
|
||||
@@ -106,13 +118,9 @@ export default async function processBotJob({
|
||||
* its computer.
|
||||
*/
|
||||
const run = async () => {
|
||||
await pushBotNotice({
|
||||
bot_id,
|
||||
bot: await setBotFace({
|
||||
bot_id,
|
||||
face: "working",
|
||||
status_line: job.kind == "reply" ? BotReplyJobTitle : BotTaskStatusLine,
|
||||
}),
|
||||
await setFace({
|
||||
face: "working",
|
||||
status_line: job.kind == "reply" ? BotReplyJobTitle : BotTaskStatusLine,
|
||||
});
|
||||
|
||||
return await runner({
|
||||
@@ -159,14 +167,7 @@ export default async function processBotJob({
|
||||
refund: true,
|
||||
});
|
||||
|
||||
await pushBotNotice({
|
||||
bot_id,
|
||||
bot: await setBotFace({
|
||||
bot_id,
|
||||
face: "asleep",
|
||||
status_line: BotWaitingStatusLine,
|
||||
}),
|
||||
});
|
||||
await setFace({ face: "asleep", status_line: BotWaitingStatusLine });
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import type {
|
||||
BUN_SQLITE_COBBLE_BOT_MESSAGES,
|
||||
BUN_SQLITE_COBBLE_BOTS,
|
||||
} from "@/db/types/db";
|
||||
import type { BUN_SQLITE_COBBLE_BOT_MESSAGES } from "@/db/types/db";
|
||||
import type { BotFaceChange } from "@/src/functions/backend/db/bots/set-bot-face";
|
||||
import pushBotUpdated from "./push-bot-updated";
|
||||
import pushToSessionUser from "./push-to-session-user";
|
||||
|
||||
type Params = {
|
||||
bot_id: number | string;
|
||||
message?: BUN_SQLITE_COBBLE_BOT_MESSAGES | null;
|
||||
bot?: Pick<BUN_SQLITE_COBBLE_BOTS, "id" | "face" | "status_line" | "computer_id"> | null;
|
||||
/**
|
||||
* A face change (`setBotFace`), pushed to the user it's for.
|
||||
*/
|
||||
bot?: BotFaceChange | null;
|
||||
/**
|
||||
* A job's result reported to the bot that handed it over
|
||||
* (`relayJobToRequester`): a message in that bot's conversation.
|
||||
@@ -18,8 +19,8 @@ type Params = {
|
||||
|
||||
/**
|
||||
* 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
|
||||
* conversation it's in, and a face change to the user it's for
|
||||
* (`pushBotUpdated`). A relayed report
|
||||
* goes to the conversation it was posted in.
|
||||
*/
|
||||
export default async function pushBotNotice({ bot_id, message, bot, relay }: Params) {
|
||||
@@ -38,6 +39,6 @@ export default async function pushBotNotice({ bot_id, message, bot, relay }: Par
|
||||
}
|
||||
|
||||
if (bot) {
|
||||
await pushBotUpdated({ bot: { ...bot, id: bot.id || Number(bot_id) } });
|
||||
await pushBotUpdated({ bot_id: bot.id || bot_id, user_id: bot.user_id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,48 @@
|
||||
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 grabBotFace from "@/src/functions/backend/db/bots/grab-bot-face";
|
||||
import { sendToUser } from "@/src/websocket/(functions)/send-to-user";
|
||||
|
||||
type Params = {
|
||||
bot: Pick<BUN_SQLITE_COBBLE_BOTS, "id" | "face" | "status_line" | "computer_id">;
|
||||
bot_id: number | string;
|
||||
/**
|
||||
* Only these users (the connected ones, for the live relay); else
|
||||
* everyone who sees the bot.
|
||||
* Only this user: a face change, which is theirs alone. Else everyone
|
||||
* who sees the bot.
|
||||
*/
|
||||
user_id?: number | null;
|
||||
/**
|
||||
* Only these users (the connected ones, for the live relay).
|
||||
*/
|
||||
users?: Set<string>;
|
||||
/**
|
||||
* The computer the bot is on, when the push knows it (a switch, or the
|
||||
* live relay). Null means its main one.
|
||||
*/
|
||||
computer_id?: number | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Pushes `server:bot-updated` to one user, or everyone who sees the bot
|
||||
* (`grabBotAudience`), each with their own face, status line and active
|
||||
* task with it, never another user's.
|
||||
*/
|
||||
export default async function pushBotUpdated({ bot, users }: Params) {
|
||||
if (!bot.id) return;
|
||||
export default async function pushBotUpdated({ bot_id, user_id, users, computer_id }: Params) {
|
||||
if (!bot_id) return;
|
||||
|
||||
for (const user_id of await grabBotAudience({ bot_id: bot.id })) {
|
||||
if (users && !users.has(String(user_id))) continue;
|
||||
const audience = user_id ? [user_id] : await grabBotAudience({ bot_id });
|
||||
|
||||
for (const viewer_id of audience) {
|
||||
if (users && !users.has(String(viewer_id))) continue;
|
||||
|
||||
sendToUser({
|
||||
user_id,
|
||||
user_id: viewer_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 }),
|
||||
id: Number(bot_id),
|
||||
...(await grabBotFace({ bot_id, user_id: viewer_id })),
|
||||
...(computer_id !== undefined ? { computer_id: computer_id || null } : {}),
|
||||
task: await grabActiveBotTask({ bot_id, user_id: viewer_id }),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
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 grabJobUserId from "@/src/functions/backend/db/bots-queue/grab-job-user-id";
|
||||
import HarnessError from "@/src/harness/harness-error";
|
||||
import BotJobFatalError from "./bot-job-fatal-error";
|
||||
import runBotReply from "./run-bot-reply";
|
||||
@@ -26,19 +24,13 @@ const runBotJob: BotJobRunner = async (context) => {
|
||||
throw new BotJobFatalError(`The bot for this job no longer exists.`);
|
||||
}
|
||||
|
||||
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];
|
||||
const user_id = await grabJobUserId({ job });
|
||||
|
||||
if (!session?.user_id) {
|
||||
if (!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"
|
||||
|
||||
Reference in New Issue
Block a user