File size: 6,101 Bytes
f56a29b | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | import type { StatelessChatRequest } from '@/lib/types/chat';
import { buildWhiteboardConflicts } from './whiteboard-conflicts';
// ==================== Element Summarization ====================
/**
* Strip HTML tags to extract plain text
*/
function stripHtml(html: string): string {
return html.replace(/<[^>]*>/g, '').trim();
}
/**
* Summarize a single PPT element into a one-line description
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- PPTElement variants have heterogeneous shapes
function summarizeElement(el: any): string {
const id = el.id ? `[id:${el.id}]` : '';
const pos = `at (${Math.round(el.left)},${Math.round(el.top)})`;
const size =
el.width != null && el.height != null
? ` size ${Math.round(el.width)}×${Math.round(el.height)}`
: el.width != null
? ` w=${Math.round(el.width)}`
: '';
switch (el.type) {
case 'text': {
const text = stripHtml(el.content || '').slice(0, 60);
const suffix = text.length >= 60 ? '...' : '';
return `${id} text${el.textType ? `[${el.textType}]` : ''}: "${text}${suffix}" ${pos}${size}`;
}
case 'image': {
const src = el.src?.startsWith('data:') ? '[embedded]' : el.src?.slice(0, 50) || 'unknown';
return `${id} image: ${src} ${pos}${size}`;
}
case 'shape': {
const shapeText = el.text?.content ? stripHtml(el.text.content).slice(0, 40) : '';
return `${id} shape${shapeText ? `: "${shapeText}"` : ''} ${pos}${size}`;
}
case 'chart':
return `${id} chart[${el.chartType}]: labels=[${(el.data?.labels || []).slice(0, 4).join(',')}] ${pos}${size}`;
case 'table': {
const rows = el.data?.length || 0;
const cols = el.data?.[0]?.length || 0;
return `${id} table: ${rows}x${cols} ${pos}${size}`;
}
case 'latex':
return `${id} latex: "${(el.latex || '').slice(0, 40)}" ${pos}${size}`;
case 'line': {
const lx = Math.round(el.left ?? 0);
const ly = Math.round(el.top ?? 0);
const sx = el.start?.[0] ?? 0;
const sy = el.start?.[1] ?? 0;
const ex = el.end?.[0] ?? 0;
const ey = el.end?.[1] ?? 0;
return `${id} line: (${lx + sx},${ly + sy}) → (${lx + ex},${ly + ey})`;
}
case 'code': {
const lang = el.language || 'unknown';
const lineCount = el.lines?.length || 0;
const codeFn = el.fileName ? ` "${el.fileName}"` : '';
const linePreview = (el.lines || [])
.slice(0, 10)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.map((l: any) => ` ${l.id}: ${l.content}`)
.join('\n');
const moreLines = lineCount > 10 ? `\n ... and ${lineCount - 10} more lines` : '';
return `${id} code${codeFn} (${lang}, ${lineCount} lines) ${pos}${size}\n${linePreview}${moreLines}`;
}
case 'video':
return `${id} video ${pos}${size}`;
case 'audio':
return `${id} audio ${pos}${size}`;
default:
return `${id} ${el.type || 'unknown'} ${pos}${size}`;
}
}
/**
* Summarize an array of elements into line descriptions
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- PPTElement variants have heterogeneous shapes
export function summarizeElements(elements: any[]): string {
if (elements.length === 0) return ' (empty)';
const lines = elements.map((el, i) => ` ${i + 1}. ${summarizeElement(el)}`);
return lines.join('\n');
}
// ==================== State Context ====================
/**
* Build context string from store state
*/
export function buildStateContext(storeState: StatelessChatRequest['storeState']): string {
const { stage, scenes, currentSceneId, mode, whiteboardOpen } = storeState;
const lines: string[] = [];
// Mode
lines.push(`Mode: ${mode}`);
// Whiteboard status
lines.push(
`Whiteboard: ${whiteboardOpen ? 'OPEN (slide canvas is hidden)' : 'closed (slide canvas is visible)'}`,
);
// Stage info
if (stage) {
lines.push(
`Course: ${stage.name || 'Untitled'}${stage.description ? ` - ${stage.description}` : ''}`,
);
}
// Scenes summary
lines.push(`Total scenes: ${scenes.length}`);
if (currentSceneId) {
const currentScene = scenes.find((s) => s.id === currentSceneId);
if (currentScene) {
lines.push(
`Current scene: "${currentScene.title}" (${currentScene.type}, id: ${currentSceneId})`,
);
// Slide scene: include element details
if (currentScene.content.type === 'slide') {
const elements = currentScene.content.canvas.elements;
lines.push(`Current slide elements (${elements.length}):\n${summarizeElements(elements)}`);
}
// Quiz scene: include question summary
if (currentScene.content.type === 'quiz') {
const questions = currentScene.content.questions;
const qSummary = questions
.slice(0, 5)
.map((q, i) => ` ${i + 1}. [${q.type}] ${q.question.slice(0, 80)}`)
.join('\n');
lines.push(
`Quiz questions (${questions.length}):\n${qSummary}${questions.length > 5 ? `\n ... and ${questions.length - 5} more` : ''}`,
);
}
}
} else if (scenes.length > 0) {
lines.push('No scene currently selected');
}
// List first few scenes
if (scenes.length > 0) {
const sceneSummary = scenes
.slice(0, 5)
.map((s, i) => ` ${i + 1}. ${s.title} (${s.type}, id: ${s.id})`)
.join('\n');
lines.push(
`Scenes:\n${sceneSummary}${scenes.length > 5 ? `\n ... and ${scenes.length - 5} more` : ''}`,
);
}
// Whiteboard content (last whiteboard in the stage)
if (stage?.whiteboard && stage.whiteboard.length > 0) {
const lastWb = stage.whiteboard[stage.whiteboard.length - 1];
const wbElements = lastWb.elements || [];
lines.push(
`Whiteboard (last of ${stage.whiteboard.length}, ${wbElements.length} elements):\n${summarizeElements(wbElements)}`,
);
const conflictsText = buildWhiteboardConflicts(wbElements);
if (conflictsText) lines.push(conflictsText);
}
return lines.join('\n');
}
|