Files
cobble/src/functions/backend/db/bot-graphics/grab-bot-graphic-view.ts
T
tbenandClaude Opus 5.5 bea656ade8 Add bot graphics
Bots can now show a concept as a visual: create_graphic (with
update_graphic, read_graphic and list_graphics, on every run, no computer
needed) stores one HTML document in bot_graphics, per user like the
conversation, and the chat renders its [graphic:ID] ref as an inline card
sized to fit, with a large view and a page of its own (no rail).

Graphic HTML is treated as hostile: it is only served at /graphics/<id>
with a CSP sandbox allowing scripts under an origin of its own and no
network, loading only the vendored Chart.js and Mermaid from
/graphics-lib/. The document carries the app's theme vars and scrollbars,
reports its height to the thread, and a frame that navigates away is
closed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 08:11:15 +01:00

39 lines
1.4 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import type { BotGraphicView } from "@/src/types";
import { viewerGraphicsSql } from "@/src/utils/bot-graphic-sql";
type Params = {
graphic_id: number | string;
user_id: number | string;
};
/**
* A graphic as its card and page name it (never its HTML, which only
* `/graphics/<id>` serves), for the user it was made for; null otherwise.
*/
export default async function grabBotGraphicView({ graphic_id, user_id }: Params): Promise<BotGraphicView | null> {
const reach = viewerGraphicsSql({ alias: "g", user_id });
const row = (
await BunSQLite.sql<BotGraphicView & { bot_name: string | null }>({
sql: `SELECT g.id, g.title, g.version, g.bot_id, g.workspace_id, g.session_id, g.updated_at, b.name AS bot_name
FROM bot_graphics g LEFT JOIN bots b ON b.id = g.bot_id
WHERE g.id = ? AND ${reach.sql} LIMIT 1`,
values: [Number(graphic_id), ...reach.values],
})
).payload?.[0];
if (!row) return null;
return {
id: Number(row.id),
title: row.title || `Graphic ${row.id}`,
version: Number(row.version) || 1,
bot_id: Number(row.bot_id),
bot_name: row.bot_name || "A bot",
workspace_id: row.workspace_id ? Number(row.workspace_id) : null,
session_id: row.session_id ? Number(row.session_id) : null,
updated_at: Number(row.updated_at) || 0,
};
}