File size: 2,550 Bytes
3d2098f
 
 
 
 
 
 
 
 
 
b389a82
 
 
 
 
 
 
3d2098f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from 'react'

// Collapsible "view / edit the exact prompt before it runs" panel.
//
// `value` is the current prompt text (null = use the backend default).
// `fetchPrompt` is an async () => string that assembles the default prompt
// (some steps fetch live data to build it, so it may take a moment).
// When the user edits the box, value diverges from the default — the run
// button sends `value`; if the panel was never opened, value stays null and
// the backend assembles the default itself.
export default function PromptPanel({ fetchPrompt, value, setValue, disabled, label = 'prompt', open: openProp, setOpen: setOpenProp }) {
  const [openState, setOpenState] = useState(false)
  // Controlled when the parent passes open/setOpen (e.g. to auto-reveal prompts
  // after a bulk generate); otherwise self-managed.
  const controlled = openProp !== undefined
  const open = controlled ? openProp : openState
  const setOpen = controlled ? setOpenProp : setOpenState
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState('')
  const [original, setOriginal] = useState(null)

  const load = async () => {
    setLoading(true)
    setError('')
    try {
      const p = await fetchPrompt()
      setValue(p)
      setOriginal(p)
    } catch (e) {
      setError(e.message)
    } finally {
      setLoading(false)
    }
  }

  const toggle = async () => {
    if (!open && value == null) await load()
    setOpen(!open)
  }

  const edited = value != null && original != null && value !== original

  return (
    <div className="prompt-panel">
      <button className="prompt-toggle" onClick={toggle} disabled={disabled}>
        {open ? '▾ Hide' : '▸ Show / edit'} the {label} prompt (advanced)
        {edited && <span className="edited-badge">● edited</span>}
      </button>
      {open && (
        <div>
          <p className="prompt-hint">
            This is the exact text sent to the model. Edit it to QC the output, then run. Your edits are used as-is.
          </p>
          {loading ? (
            <p className="muted"><span className="spinner" />Assembling prompt…</p>
          ) : error ? (
            <div className="error-box">{error}</div>
          ) : (
            <textarea className="prompt-area" value={value || ''} onChange={(e) => setValue(e.target.value)} />
          )}
          <button className="btn small secondary" onClick={load} disabled={loading}>
            ↺ Reset to default
          </button>
        </div>
      )}
    </div>
  )
}