import { EMBED_SYSTEM_PROMPT, BANNER_SYSTEM_PROMPT, buildEmbedContext, buildDataFilesContext, buildThemeContext, } from "./embed-system-prompt.js"; import { embedTools } from "./embed-tools.js"; import { streamChatResponse } from "./stream-handler.js"; import type { Request, Response } from "express"; export async function handleEmbedChat(req: Request, res: Response) { const { context } = req.body; const contextBlock = buildEmbedContext(context?.embedHtml); const isBanner = Boolean(context?.isBanner); const isScratch = Boolean(context?.isScratch); const dataFilesBlock = buildDataFilesContext(context?.dataFiles); const themeBlock = buildThemeContext(context?.theme); const parts = [EMBED_SYSTEM_PROMPT]; if (isBanner) parts.push(BANNER_SYSTEM_PROMPT); if (isScratch) { parts.push( "## New chart\n\nThis chart is BRAND NEW - it has an auto-generated " + "placeholder filename. Your VERY FIRST `createEmbed` call MUST include " + "a descriptive `filename` slug (kebab-case, no extension, 2-4 words) " + "that reflects what the chart represents. The client will rename the " + "underlying file and keep the document in sync.", ); } if (themeBlock) parts.push(themeBlock); if (contextBlock) parts.push(`## Current chart\n\n${contextBlock}`); if (dataFilesBlock) parts.push(`## Attached data\n\n${dataFilesBlock}`); const systemPrompt = parts.join("\n\n"); return streamChatResponse(req, res, { systemPrompt, tools: embedTools, logPrefix: "embed-chat", }); }