/** Prompt instructions for interactive reply blocks, generated once per mode from the shared catalog. */ import { createLibrary, defineComponent } from '@openuidev/lang-core'; import { GENUI_COMPONENTS, GENUI_EXAMPLES, GENUI_EXPLAIN_RULES, GENUI_GROUPS, GENUI_PREAMBLE, GENUI_ROOT, GENUI_RULES } from '../../shared/genui.ts'; /** Components that quiz the student; Explain leaves them out. */ const PRACTICE = new Set(['Question', 'Flashcards', 'FlashCard']); const cached = new Map(); /** * `chat`: the assistant and share-link chats. `explain`: Explain with AI, which gives information * only (no practice questions or flashcards). */ export function genUiPrompt(mode: 'chat' | 'explain' = 'chat'): string { let prompt = cached.get(mode); if (prompt === undefined) { const explain = mode === 'explain'; const library = createLibrary({ root: GENUI_ROOT, components: GENUI_COMPONENTS.filter((c) => !explain || !PRACTICE.has(c.name)).map((c) => defineComponent({ ...c, component: null as never })), componentGroups: GENUI_GROUPS.map((g) => (explain ? { ...g, components: g.components.filter((n) => !PRACTICE.has(n)) } : g)).filter((g) => g.components.length > 0), }); const preamble = explain ? GENUI_PREAMBLE.replace(/\n- Teaching a concept the student can practise:[^\n]*/, '') : GENUI_PREAMBLE; prompt = `\n\n${library.prompt({ preamble, additionalRules: explain ? [...GENUI_RULES, ...GENUI_EXPLAIN_RULES] : GENUI_RULES, examples: explain ? GENUI_EXAMPLES.filter((e) => !/Question\(|Flashcards\(/.test(e)) : GENUI_EXAMPLES, bindings: true, })}`; cached.set(mode, prompt); } return prompt; }