import type { BUN_SQLITE_COBBLE_BOT_SESSIONS } from "@/db/types/db"; import BunSQLite from "@moduletrace/bun-sqlite"; type Params = { /** * The user the conversation is with. */ user_id: number | string; bot_id: number | string; /** * Null or undefined: the bot's own chat. */ workspace_id?: number | string | null; }; /** * The active session of one user's conversation (with a bot in its own * chat or in one workspace), or null when it has none yet. */ export default async function grabActiveBotSession({ user_id, bot_id, workspace_id, }: Params): Promise { const res = await BunSQLite.sql({ sql: `SELECT * FROM bot_sessions WHERE user_id = ? AND bot_id = ? AND workspace_id IS ? AND status = 'active' ORDER BY id DESC LIMIT 1`, values: [user_id, bot_id, workspace_id ? Number(workspace_id) : null], }); return res.payload?.[0] || null; }