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]>
29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
import { AppData } from "@/src/data/app-data";
|
|
import AppError from "@/src/utils/app-error";
|
|
|
|
type Params = {
|
|
title?: string;
|
|
html?: string;
|
|
};
|
|
|
|
/**
|
|
* A graphic's title and HTML as they're stored: trimmed, the title one
|
|
* line, the HTML within `AppData.BotGraphicMaxChars`. Throws an `AppError`
|
|
* the bot can act on.
|
|
*/
|
|
export default function checkGraphicInput({ title, html }: Params) {
|
|
const final_title = title?.replace(/\s+/g, " ").trim().slice(0, 200);
|
|
const final_html = html?.trim();
|
|
|
|
if (title !== undefined && !final_title) throw new AppError(`Give the graphic a title.`);
|
|
if (html !== undefined && !final_html) throw new AppError(`The graphic's HTML is empty.`);
|
|
|
|
if (final_html && final_html.length > AppData.BotGraphicMaxChars) {
|
|
throw new AppError(
|
|
`The HTML is ${final_html.length} characters; a graphic holds at most ${AppData.BotGraphicMaxChars}. Make it leaner: draw with SVG or a chart library instead of inlining data or images.`,
|
|
);
|
|
}
|
|
|
|
return { title: final_title, html: final_html };
|
|
}
|