Files
cobble/src/functions/backend/db/bots-queue/grab-running-bot-job-face.ts
T
tbenandClaude Opus 5.5 96d3227bd0 Name the workspace in a bot's task status line
A task running in a workspace shows "Working on a task in <name>
workspace", so the bot's own chat and the main rail say where the work
is. Outside a workspace it stays "Working on a task".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 19:22:25 +01:00

43 lines
1.5 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import type { BotFace } from "@/src/dict/bot-dict";
import { BotReplyJobTitle } from "@/src/dict/bot-queue-dict";
import taskStatusLine from "@/src/utils/task-status-line";
type Params = {
bot_id: number | string;
/**
* Only jobs in this user's conversations.
*/
user_id: number | string;
};
/**
* The face a bot should show one user while other jobs for them are
* still running: thinking while a reply runs (it's short, and its chat
* needs the row), else working on a task (in its workspace, if it runs in
* one). Null when none is running.
*/
export default async function grabRunningBotJobFace({
bot_id,
user_id,
}: Params): Promise<{ face: BotFace; status_line: string } | null> {
const jobs = (
await BunSQLite.sql<{ kind: string; workspace_name: string | null }>({
sql: `SELECT q.kind, w.name AS workspace_name FROM bots_queue q
LEFT JOIN workspaces w ON w.id = q.workspace_id
WHERE q.bot_id = ? AND q.status = 'processing'
AND q.session_id IN (SELECT id FROM bot_sessions WHERE user_id = ?)
ORDER BY q.id`,
values: [bot_id, user_id],
})
).payload || [];
if (jobs.some((job) => job.kind == "reply")) return { face: "working", status_line: BotReplyJobTitle };
const task = jobs.find((job) => job.kind == "task");
if (task) return { face: "working", status_line: taskStatusLine({ workspace_name: task.workspace_name }) };
return null;
}