File size: 1,564 Bytes
f6678ab
 
 
 
8fc8501
27abbe5
f6678ab
 
 
 
 
 
 
 
 
8fc8501
 
27abbe5
f6678ab
 
 
8fc8501
 
 
 
 
 
 
 
 
27abbe5
f6678ab
8fc8501
f6678ab
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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",
  });
}