File size: 2,548 Bytes
575fb61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e648dca
 
 
 
 
 
 
575fb61
 
 
 
e648dca
575fb61
 
 
 
 
e648dca
575fb61
 
 
 
 
 
 
e648dca
575fb61
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
// Persona-prompt viewer/editor for the Settings page. Shows the system prompt that
// writes each hero (name / about / quote / voice design) and lets you edit it. A saved
// edit is stored locally (personaPrompts.js → setPersonaSystem) and used by the persona
// panel on the next "Recruit hero"; Reset restores the built-in default.
import {
  PERSONA_SYSTEM_DEFAULT, getPersonaSystem, setPersonaSystem, resetPersonaSystem, isPersonaSystemCustom,
} from '/web/personaPrompts.js'

function el(tag, props = {}, kids = []) {
  const n = document.createElement(tag)
  for (const [k, v] of Object.entries(props)) {
    if (k === 'class') n.className = v
    else if (k.startsWith('on') && typeof v === 'function') n.addEventListener(k.slice(2), v)
    else if (v != null) n.setAttribute(k, v)
  }
  for (const kid of [].concat(kids)) if (kid != null) n.append(kid)
  return n
}

export function mountPersonaPromptBar(host) {
  const ta = el('textarea', { class: 'persona-prompt-edit', rows: 12, spellcheck: 'false' })
  const saveBtn = el('button', { class: 'persona-go persona-prompt-save', type: 'button' }, 'Save')
  const resetBtn = el('button', { class: 'persona-ico persona-prompt-reset', type: 'button' }, 'Reset to default')
  const note = el('div', { class: 'persona-status persona-prompt-note' })

  ta.value = getPersonaSystem()

  function refreshNote() {
    note.textContent = isPersonaSystemCustom() ? 'Using your custom prompt.' : 'Using the built-in default.'
  }
  // Badge the Save button while the textarea differs from the prompt in effect —
  // i.e. there are unsaved edits. Cleared once saved (or the edit matches default).
  function refreshEdited() {
    saveBtn.classList.toggle('badged', ta.value.trim() !== getPersonaSystem().trim())
  }
  refreshNote(); refreshEdited()
  ta.addEventListener('input', refreshEdited)

  saveBtn.addEventListener('click', () => {
    setPersonaSystem(ta.value)
    ta.value = getPersonaSystem() // reflect (a default-equal edit clears the override)
    refreshNote(); refreshEdited()
    saveBtn.textContent = '✓ Saved'; setTimeout(() => { saveBtn.textContent = 'Save' }, 1400)
  })
  resetBtn.addEventListener('click', () => {
    resetPersonaSystem()
    ta.value = PERSONA_SYSTEM_DEFAULT
    refreshNote(); refreshEdited()
  })

  host.append(el('div', { class: 'persona-prompt-bar' }, [
    ta,
    el('div', { class: 'persona-prompt-actions' }, [saveBtn, resetBtn]),
    note,
  ]))
  return { refresh: () => { ta.value = getPersonaSystem(); refreshNote(); refreshEdited() } }
}