File size: 6,511 Bytes
f2e28ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { useEffect, useState } from 'react';
import clsx from 'clsx';

const DEFAULT_FORM = {
  mode: 'equal_split',
  modelId: '',
  seed: 42,
  maxRounds: 20,
  temperature: 0.2,
};

/**
 * Sidebar/header control that picks an inference mode and starts a live run.
 *
 * Props:
 *   - modes:        [{id,label,description,requires_credentials,...}]
 *   - status:       'idle' | 'connecting' | 'running' | 'done' | 'error'
 *   - lastError:    string | null
 *   - currentRunId: string | null
 *   - onStart(opts): kicks off a new run (parent handles SSE wiring)
 *   - onStop():     stops the current SSE subscription
 */
export default function RunControls({
  modes = [],
  status = 'idle',
  lastError = null,
  currentRunId = null,
  onStart,
  onStop,
}) {
  const [form, setForm] = useState(DEFAULT_FORM);
  const [open, setOpen] = useState(true);

  // Make sure the selected mode stays valid when the modes list refreshes.
  useEffect(() => {
    if (!modes.length) return;
    if (!modes.find(m => m.id === form.mode)) {
      setForm(f => ({ ...f, mode: modes[0].id }));
    }
  }, [modes, form.mode]);

  const selectedMode = modes.find(m => m.id === form.mode);
  const llmReady = selectedMode?.id !== 'llm' || selectedMode?.credentials_present;

  function update(key, value) {
    setForm(f => ({ ...f, [key]: value }));
  }

  function submit(e) {
    e.preventDefault();
    onStart?.({
      mode: form.mode,
      modelId: form.modelId || selectedMode?.default_model_id || null,
      seed: Number(form.seed) || 0,
      maxRounds: Number(form.maxRounds) || 20,
      temperature: Number(form.temperature) || 0,
    });
  }

  const isBusy = status === 'connecting' || status === 'running';

  return (
    <div className="run-controls card">
      <h3>
        Live inference
        <span className="meta">
          {currentRunId ? <code>{currentRunId}</code> : 'no run yet'}
        </span>
      </h3>

      <button
        type="button"
        className="run-controls__toggle"
        onClick={() => setOpen(o => !o)}
      >
        {open ? 'Hide controls' : 'Show controls'}
      </button>

      {open && (
        <form className="run-controls__form" onSubmit={submit}>
          <label className="run-controls__field">
            <span>Policy</span>
            <select
              value={form.mode}
              onChange={e => update('mode', e.target.value)}
              disabled={isBusy}
            >
              {modes.map(m => (
                <option key={m.id} value={m.id}>
                  {m.label}
                  {m.requires_credentials && !m.credentials_present
                    ? ' (no token)'
                    : ''}
                </option>
              ))}
            </select>
            {selectedMode?.description && (
              <small className="run-controls__hint">
                {selectedMode.description}
              </small>
            )}
          </label>

          {selectedMode?.id === 'llm' && (
            <label className="run-controls__field">
              <span>Model id</span>
              <input
                type="text"
                value={form.modelId}
                onChange={e => update('modelId', e.target.value)}
                placeholder={
                  selectedMode.default_model_id || 'mistralai/Mistral-7B-Instruct-v0.3'
                }
                disabled={isBusy}
              />
            </label>
          )}

          <div className="run-controls__row">
            <label className="run-controls__field">
              <span>Seed</span>
              <input
                type="number"
                min={0}
                value={form.seed}
                onChange={e => update('seed', e.target.value)}
                disabled={isBusy}
              />
            </label>
            <label className="run-controls__field">
              <span>Max rounds</span>
              <input
                type="number"
                min={1}
                max={50}
                value={form.maxRounds}
                onChange={e => update('maxRounds', e.target.value)}
                disabled={isBusy}
              />
            </label>
            {selectedMode?.id === 'llm' && (
              <label className="run-controls__field">
                <span>Temp</span>
                <input
                  type="number"
                  step="0.1"
                  min={0}
                  max={2}
                  value={form.temperature}
                  onChange={e => update('temperature', e.target.value)}
                  disabled={isBusy}
                />
              </label>
            )}
          </div>

          <div className="run-controls__actions">
            <button
              type="submit"
              className="primary"
              disabled={isBusy || !llmReady}
              title={
                !llmReady
                  ? 'Set HF_TOKEN and HF_MODEL_ID in the server .env first'
                  : undefined
              }
            >
              {status === 'connecting' && 'Starting…'}
              {status === 'running' && 'Running…'}
              {(status === 'idle' || status === 'done' || status === 'error') &&
                'Start run'}
            </button>
            {isBusy && (
              <button type="button" onClick={() => onStop?.()}>
                Stop stream
              </button>
            )}
          </div>

          <div
            className={clsx('run-controls__status', `is-${status}`)}
            role="status"
          >
            <span className="dot" />
            {statusLabel(status)}
            {lastError && (
              <em className="run-controls__error">{lastError}</em>
            )}
          </div>

          {selectedMode?.id === 'llm' && !selectedMode?.credentials_present && (
            <small className="run-controls__warn">
              The server is missing <code>HF_TOKEN</code> /{' '}
              <code>HF_MODEL_ID</code>. Set them in <code>.env</code> and
              restart <code>scripts/run_visualizer_server.py</code>.
            </small>
          )}
        </form>
      )}
    </div>
  );
}

function statusLabel(status) {
  switch (status) {
    case 'connecting':
      return 'Connecting to backend…';
    case 'running':
      return 'Streaming live rounds';
    case 'done':
      return 'Run complete';
    case 'error':
      return 'Run failed';
    default:
      return 'Idle';
  }
}