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:
@@ -1,11 +1,13 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import { BotNeedsYouStatusLine } from "@/src/dict/bot-queue-dict";
|
||||
import insertBotMessage from "../bots/insert-bot-message";
|
||||
import setBotFace from "../bots/set-bot-face";
|
||||
import countOpenBotJobs from "./count-open-bot-jobs";
|
||||
import grabLockedBotJob from "./grab-locked-bot-job";
|
||||
import relayJobToRequester from "./relay-job-to-requester";
|
||||
import jobSessionId from "./job-session-id";
|
||||
import grabJobUserId from "./grab-job-user-id";
|
||||
|
||||
type Params = {
|
||||
job_id: number | string;
|
||||
@@ -27,9 +29,9 @@ type Params = {
|
||||
|
||||
/**
|
||||
* Marks a job done, closes its task and sends the completion message back
|
||||
* to the user in the bot's conversation. Unless it has other jobs open,
|
||||
* the bot shows needs-you if asked, else done after a task and idle after
|
||||
* a reply. A handed-over job's result also goes to the bot that handed
|
||||
* to the user in the bot's conversation. Unless it has other jobs open
|
||||
* for that user, the bot shows that user needs-you if asked, else done
|
||||
* after a task and idle after a reply; nobody else's face changes. A handed-over job's result also goes to the bot that handed
|
||||
* it over (`relay`). Throws if this handler no longer holds the job.
|
||||
*/
|
||||
export default async function completeBotJob({
|
||||
@@ -78,18 +80,24 @@ export default async function completeBotJob({
|
||||
needs_you,
|
||||
});
|
||||
|
||||
const bot = needs_you
|
||||
? await setBotFace({
|
||||
bot_id: job.bot_id,
|
||||
face: "needs-you",
|
||||
status_line: "Waiting on you",
|
||||
})
|
||||
: (await countOpenBotJobs({ bot_id: job.bot_id }))
|
||||
? null
|
||||
: await setBotFace({
|
||||
const user_id = await grabJobUserId({ job });
|
||||
|
||||
const bot = !user_id
|
||||
? null
|
||||
: needs_you
|
||||
? await setBotFace({
|
||||
bot_id: job.bot_id,
|
||||
face: job.kind == "reply" ? "idle" : "done",
|
||||
});
|
||||
user_id,
|
||||
face: "needs-you",
|
||||
status_line: BotNeedsYouStatusLine,
|
||||
})
|
||||
: (await countOpenBotJobs({ bot_id: job.bot_id, user_id }))
|
||||
? null
|
||||
: await setBotFace({
|
||||
bot_id: job.bot_id,
|
||||
user_id,
|
||||
face: job.kind == "reply" ? "idle" : "done",
|
||||
});
|
||||
|
||||
const relay = silent ? null : await relayJobToRequester({ job, text: result });
|
||||
|
||||
|
||||
@@ -2,15 +2,20 @@ import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
|
||||
type Params = {
|
||||
bot_id: number | string;
|
||||
/**
|
||||
* Only jobs in this user's conversations.
|
||||
*/
|
||||
user_id: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
* How many of a bot's jobs are still pending or processing.
|
||||
* How many of a bot's jobs for one user are still pending or processing.
|
||||
*/
|
||||
export default async function countOpenBotJobs({ bot_id }: Params) {
|
||||
export default async function countOpenBotJobs({ bot_id, user_id }: Params) {
|
||||
const res = await BunSQLite.sql<{ open: number }>({
|
||||
sql: `SELECT COUNT(*) AS open FROM bots_queue WHERE bot_id = ? AND status IN ('pending', 'processing')`,
|
||||
values: [bot_id],
|
||||
sql: `SELECT COUNT(*) AS open FROM bots_queue WHERE bot_id = ? AND status IN ('pending', 'processing')
|
||||
AND session_id IN (SELECT id FROM bot_sessions WHERE user_id = ?)`,
|
||||
values: [bot_id, user_id],
|
||||
});
|
||||
|
||||
return Number(res.payload?.[0]?.open || 0);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE } from "@/db/types/db";
|
||||
import jobSessionId from "./job-session-id";
|
||||
|
||||
type Params = {
|
||||
job: Pick<BUN_SQLITE_COBBLE_BOTS_QUEUE, "session_id">;
|
||||
};
|
||||
|
||||
/**
|
||||
* The user a job works for: the one whose conversation it's in. Null once
|
||||
* that conversation is gone.
|
||||
*/
|
||||
export default async function grabJobUserId({ job }: Params) {
|
||||
const user_id = (
|
||||
await BunSQLite.sql<{ user_id: number }>({
|
||||
sql: `SELECT user_id FROM bot_sessions WHERE id = ? LIMIT 1`,
|
||||
values: [jobSessionId({ job })],
|
||||
})
|
||||
).payload?.[0]?.user_id;
|
||||
|
||||
return user_id ? Number(user_id) : null;
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import insertBotMessage from "../bots/insert-bot-message";
|
||||
import setBotFace from "../bots/set-bot-face";
|
||||
import relayJobToRequester from "./relay-job-to-requester";
|
||||
import jobSessionId from "./job-session-id";
|
||||
import grabJobUserId from "./grab-job-user-id";
|
||||
import { BotNeedsYouStatusLine } from "@/src/dict/bot-queue-dict";
|
||||
|
||||
type Params = {
|
||||
job: BUN_SQLITE_COBBLE_BOTS_QUEUE;
|
||||
@@ -13,7 +15,7 @@ type Params = {
|
||||
/**
|
||||
* Wraps up a job that is out of attempts: stamps `finished_at` (so it's
|
||||
* reported once), tells the user in the bot's conversation and sets the
|
||||
* bot to needs-you. Its task stays active, since the work isn't done. A
|
||||
* bot to needs-you for that user. Its task stays active, since the work isn't done. A
|
||||
* handed-over job's failure also goes to the bot that handed it over.
|
||||
*/
|
||||
export default async function notifyBotJobFailed({
|
||||
@@ -46,11 +48,16 @@ export default async function notifyBotJobFailed({
|
||||
needs_you: true,
|
||||
});
|
||||
|
||||
const bot = await setBotFace({
|
||||
bot_id: job.bot_id,
|
||||
face: "needs-you",
|
||||
status_line: "Waiting on you",
|
||||
});
|
||||
const user_id = await grabJobUserId({ job });
|
||||
|
||||
const bot = user_id
|
||||
? await setBotFace({
|
||||
bot_id: job.bot_id,
|
||||
user_id,
|
||||
face: "needs-you",
|
||||
status_line: BotNeedsYouStatusLine,
|
||||
})
|
||||
: null;
|
||||
|
||||
const relay = await relayJobToRequester({
|
||||
job,
|
||||
|
||||
Reference in New Issue
Block a user