返回 presentation-ai
template-serializer.ts
根目录 / src / components / notebook / presentation / utils / template-serializer.ts
1 import { type PlateSlide } from "./parser";
2 import { serializeSlideToXml } from "./slide-serializer";
3 import { TEMPLATE_DEFINITIONS } from "./templates";
4
5 /**
6 * Serialize selected templates to a format suitable for inclusion in the AI prompt.
7 * This function takes template IDs and returns formatted XML examples for the LLM.
8 * Templates are numbered and include their category for AI guidance.
9 *
10 * @param templateIds Array of template IDs from TEMPLATE_DEFINITIONS
11 * @returns Formatted string with XML examples for each template
12 */
13 export function serializeTemplatesForPrompt(templateIds: string[]): string {
14 const templates = TEMPLATE_DEFINITIONS.filter((t) =>
15 templateIds.includes(t.id),
16 );
17
18 if (templates.length === 0) {
19 return "";
20 }
21
22 const serialized = templates.map((template, index) => {
23 // Create a minimal slide with the template content
24 const slide: PlateSlide = {
25 id: "example",
26 ...template.template,
27 content: template.template.content ?? [],
28 };
29
30 const xml = serializeSlideToXml(slide);
31
32 // Include usage hint based on category
33 const usageHint = getCategoryUsageHint(template.categoryId);
34
35 return `### ${index + 1}. ${template.name}
36 **Use for**: ${usageHint}
37 \`\`\`xml
38 ${xml}
39 \`\`\``;
40 });
41
42 return serialized.join("\n\n");
43 }
44
45 /**
46 * Get usage hint for a template category
47 */
48 function getCategoryUsageHint(categoryId: string): string {
49 const hints: Record<string, string> = {
50 basic: "Simple text layouts and general purpose content",
51 boxes: "Grouped information tiles, feature highlights",
52 bullets: "Key points, list-based content",
53 "card-layouts": "Accent layouts with prominent visuals",
54 charts: "Data visualization, metrics, statistics",
55 circles: "Cyclic processes, interconnected workflows",
56 images: "Image-heavy slides, galleries, team photos",
57 numbers: "KPIs, metrics, ratings, progress indicators",
58 pyramids: "Hierarchies, funnels, importance levels",
59 sequence: "Timelines, arrows, step-by-step flows",
60 steps: "Progressive advancement, staircases",
61 };
62 return hints[categoryId] || "General purpose layouts";
63 }
64
65 /**
66 * Serialize template hints for per-outline overrides.
67 * Maps outline indices to template names for AI guidance.
68 *
69 * @param overrides Record of outline ID to template ID mappings
70 * @param templateIds Array of selected template IDs (to look up names)
71 * @returns Object mapping outline indices to template names
72 */
73 export function serializeTemplateHintsForPrompt(
74 overrides: Record<string, string | null>,
75 templateIds: string[],
76 ): Record<number, string> {
77 const templates = TEMPLATE_DEFINITIONS.filter((t) =>
78 templateIds.includes(t.id),
79 );
80
81 const hints: Record<number, string> = {};
82 let index = 0;
83
84 for (const [_outlineId, templateId] of Object.entries(overrides)) {
85 if (templateId) {
86 const template = templates.find((t) => t.id === templateId);
87 if (template) {
88 hints[index] = template.name;
89 }
90 }
91 index++;
92 }
93
94 return hints;
95 }
96
96 lines TYPESCRIPT