A worker asks for another bot's help with ask_crew. The coordinator reads the request, asks the right bot (in a workspace, only its members), and sends the answer back into the asking bot's conversation, waking it. Each exchange is kept as a read-only chatroom: listed in the main rail after the workspaces, or in the workspace's rail for requests made there, with live status rows for each bot while it's open and a Stop button. Room sessions are hidden from every conversation list, other bots' words reach the model fenced as data, and turns, nesting and open rooms are capped. Closed rooms are pruned after a retention period and past a cap per place (Settings > General). Co-Authored-By: Claude Opus 5.5 <[email protected]>
71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
import BunSQLite from "@moduletrace/bun-sqlite";
|
|
import { AppData } from "@/src/data/app-data";
|
|
import type { BotSessionListItem } from "@/src/types";
|
|
|
|
type Params = {
|
|
/**
|
|
* The user the conversation is with.
|
|
*/
|
|
user_id: number | string;
|
|
bot_id: number | string;
|
|
workspace_id?: number | string | null;
|
|
/**
|
|
* Matches the session's title, summary or any of its messages.
|
|
*/
|
|
query?: string;
|
|
page?: number;
|
|
/**
|
|
* How many to return; a page by default.
|
|
*/
|
|
limit?: number;
|
|
};
|
|
|
|
/**
|
|
* A user's conversation's sessions for the sessions modal, latest activity first,
|
|
* each with its message count and latest message. Empty sessions are left
|
|
* out unless active.
|
|
*/
|
|
export default async function grabBotSessions({
|
|
user_id,
|
|
bot_id,
|
|
workspace_id,
|
|
query,
|
|
page = 1,
|
|
limit,
|
|
}: Params): Promise<BotSessionListItem[]> {
|
|
const search = query?.trim();
|
|
const like = search ? `%${search.replace(/[\\%_]/g, (c) => `\\${c}`)}%` : null;
|
|
const page_size = limit || AppData.BotSessionsPageSize;
|
|
|
|
let sql = ``;
|
|
|
|
sql += `SELECT s.*,\n`;
|
|
sql += ` (SELECT COUNT(*) FROM bot_messages m WHERE m.session_id = s.id) AS message_count,\n`;
|
|
sql += ` (SELECT m.text FROM bot_messages m WHERE m.session_id = s.id ORDER BY m.created_at DESC, m.id DESC LIMIT 1) AS last_message_text\n`;
|
|
sql += `FROM bot_sessions s\n`;
|
|
sql += `WHERE s.user_id = ? AND s.bot_id = ? AND s.workspace_id IS ? AND s.chatroom_id IS NULL\n`;
|
|
sql += `AND (s.status = 'active' OR EXISTS (SELECT 1 FROM bot_messages m WHERE m.session_id = s.id))\n`;
|
|
|
|
if (like) {
|
|
sql += `AND (s.title LIKE ? ESCAPE '\\' OR s.summary LIKE ? ESCAPE '\\'\n`;
|
|
sql += ` OR EXISTS (SELECT 1 FROM bot_messages m WHERE m.session_id = s.id AND m.text LIKE ? ESCAPE '\\'))\n`;
|
|
}
|
|
|
|
sql += `ORDER BY s.status = 'active' DESC, s.last_activity_at DESC, s.id DESC\n`;
|
|
sql += `LIMIT ? OFFSET ?\n`;
|
|
|
|
const res = await BunSQLite.sql<BotSessionListItem>({
|
|
sql,
|
|
values: [
|
|
user_id,
|
|
bot_id,
|
|
workspace_id ? Number(workspace_id) : null,
|
|
...(like ? [like, like, like] : []),
|
|
page_size,
|
|
(Math.max(1, page) - 1) * page_size,
|
|
],
|
|
});
|
|
|
|
return res.payload || [];
|
|
}
|