import type { User } from '../context/AuthContext' const PERSONA_DRAFT_LS = 'persona_draft_v1' const ADVISOR_DRAFT_LS = 'advisor_draft_v1' function parseDraft(key: string): Record { try { const s = localStorage.getItem(key) if (!s) return {} const j = JSON.parse(s) as unknown return j && typeof j === 'object' && !Array.isArray(j) ? (j as Record) : {} } catch { return {} } } function draftLine(label: string, d: Record): string | null { const keys = Object.keys(d).filter((k) => { const v = d[k] if (v == null) return false if (typeof v === 'string') return v.trim() !== '' if (typeof v === 'object') return Object.keys(v as object).length > 0 return true }) if (!keys.length) return null return `${label}: ${JSON.stringify(d)}` } /** * Text appended to interactive Q&A system prompts so the model can greet by name * and use wizard draft fields the learner already entered (localStorage + account). */ export function buildLearnerContextForChat(mode: 'persona' | 'advisor', user: User | null): string { const lines: string[] = [] if (user) { const first = user.firstName?.trim() const last = user.lastName?.trim() const full = [first, last].filter(Boolean).join(' ').trim() if (full) lines.push(`Preferred name: ${full}`) if (user.email) lines.push(`Email (do not read aloud unless relevant): ${user.email}`) } const personaDraft = parseDraft(PERSONA_DRAFT_LS) const advisorDraft = parseDraft(ADVISOR_DRAFT_LS) if (mode === 'persona') { const p = draftLine('Persona wizard draft (fields already filled)', personaDraft) if (p) lines.push(p) const a = draftLine('Advisor wizard draft (related context)', advisorDraft) if (a) lines.push(a) } else { const a = draftLine('Advisor wizard draft (fields already filled)', advisorDraft) if (a) lines.push(a) const p = draftLine('Persona wizard draft (related context)', personaDraft) if (p) lines.push(p) } return lines.join('\n').trim() }