File size: 2,130 Bytes
d8808e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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<string, unknown> {
  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<string, unknown>) : {}
  } catch {
    return {}
  }
}

function draftLine(label: string, d: Record<string, unknown>): 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()
}