File size: 12,867 Bytes
2043afa
 
1c495e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2043afa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982afcc
2043afa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { useEffect, useMemo, useRef, useState } from "react";

function resolveApiBase() {
  const explicitBase = import.meta.env.VITE_API_BASE;
  if (explicitBase) return explicitBase.replace(/\/$/, "");

  const host = window.location.hostname;
  const isLocal =
    host === "localhost" || host === "127.0.0.1" || host === "0.0.0.0";

  // In local Vite dev, backend runs on :7860. In Spaces/prod, serve same-origin.
  if (isLocal && window.location.port === "5173") {
    return "http://localhost:7860";
  }
  return window.location.origin.replace(/\/$/, "");
}

const API_BASE = resolveApiBase();
const WS_URL = `${API_BASE.replace(/^http/, "ws")}/ws`;
const TASKS = ["easy_screening", "budgeted_screening", "complex_tradeoff"];

async function apiPost(path, body) {
  const res = await fetch(`${API_BASE}${path}`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
  if (!res.ok) {
    const msg = await res.text();
    throw new Error(msg || `HTTP ${res.status}`);
  }
  return res.json();
}

export default function App() {
  const [taskId, setTaskId] = useState("budgeted_screening");
  const [obs, setObs] = useState(null);
  const [log, setLog] = useState([]);
  const [loading, setLoading] = useState(false);
  const [action, setAction] = useState({
    action_type: "query_ddi",
    drug_id_1: "",
    drug_id_2: "",
    target_drug_id: "",
    intervention_type: "stop",
    proposed_new_drug_id: "",
    rationale: "",
  });

  const medIds = useMemo(
    () => (obs?.current_medications || []).map((m) => m.drug_id),
    [obs]
  );
  const hasValidEpisode = Boolean(obs?.episode_id) && (obs?.current_medications?.length || 0) > 0;
  const isDone = Boolean(obs?.done);
  const finalScore =
    typeof obs?.metadata?.grader_score === "number" ? obs.metadata.grader_score : null;
  const noBudgetsLeft =
    hasValidEpisode &&
    (obs?.remaining_query_budget ?? 0) <= 0 &&
    (obs?.remaining_intervention_budget ?? 0) <= 0;
  const wsRef = useRef(null);
  const pendingRef = useRef([]);

  const wsEnsure = async () => {
    if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) return wsRef.current;
    if (wsRef.current && wsRef.current.readyState === WebSocket.CONNECTING) {
      await new Promise((r) => setTimeout(r, 80));
      return wsEnsure();
    }

    const ws = new WebSocket(WS_URL);
    wsRef.current = ws;

    ws.onmessage = (evt) => {
      try {
        const msg = JSON.parse(evt.data);
        const pending = pendingRef.current.shift();
        if (pending) pending.resolve(msg);
      } catch (e) {
        const pending = pendingRef.current.shift();
        if (pending) pending.reject(e);
      }
    };
    ws.onerror = (err) => {
      const pending = pendingRef.current.shift();
      if (pending) pending.reject(err);
    };
    ws.onclose = () => {
      wsRef.current = null;
    };

    await new Promise((resolve, reject) => {
      const t = setTimeout(() => reject(new Error("WebSocket connect timeout")), 2500);
      ws.onopen = () => {
        clearTimeout(t);
        resolve();
      };
    });
    return ws;
  };

  const wsSend = async (type, data) => {
    const ws = await wsEnsure();
    return await new Promise((resolve, reject) => {
      pendingRef.current.push({ resolve, reject });
      ws.send(JSON.stringify({ type, data }));
    });
  };

  useEffect(() => {
    return () => {
      try {
        wsRef.current?.close();
      } catch {
        // ignore
      }
    };
  }, []);

  const appendLog = (text) => {
    setLog((prev) => [`${new Date().toLocaleTimeString()}  ${text}`, ...prev].slice(0, 20));
  };

  const normalizeObsFromWs = (packetData) => {
    const observation = packetData?.observation || {};
    const mergedMetadata = {
      ...(observation?.metadata || {}),
      ...(packetData?.info || {}),
    };
    return {
      ...observation,
      done: Boolean(packetData?.done ?? observation?.done ?? false),
      reward: packetData?.reward ?? observation?.reward ?? null,
      metadata: mergedMetadata,
    };
  };

  const handleReset = async () => {
    setLoading(true);
    try {
      const msg = await wsSend("reset", { task_id: taskId });
      const data = msg?.data || {};
      const normalized = normalizeObsFromWs(data);
      setObs(normalized);
      const ids = (normalized?.current_medications || []).map((m) => m.drug_id);
      setAction((prev) => ({
        ...prev,
        drug_id_1: ids[0] || "",
        drug_id_2: ids[1] || "",
        target_drug_id: ids[0] || "",
      }));
      appendLog(`Reset task=${taskId}`);
    } catch (err) {
      appendLog(`Reset failed: ${err.message}`);
    } finally {
      setLoading(false);
    }
  };

  const buildActionPayload = () => {
    if (noBudgetsLeft) {
      return { action_type: "finish_review" };
    }
    if (action.action_type === "query_ddi") {
      return {
        action_type: "query_ddi",
        drug_id_1: action.drug_id_1,
        drug_id_2: action.drug_id_2,
      };
    }
    if (action.action_type === "propose_intervention") {
      return {
        action_type: "propose_intervention",
        target_drug_id: action.target_drug_id,
        intervention_type: action.intervention_type,
        proposed_new_drug_id: action.proposed_new_drug_id || undefined,
        rationale: action.rationale || undefined,
      };
    }
    return { action_type: "finish_review" };
  };

  const isActionValid = () => {
    if (!hasValidEpisode) return false;
    if (isDone) return false;
    if (noBudgetsLeft) return true;
    if (action.action_type === "query_ddi") {
      return Boolean(action.drug_id_1 && action.drug_id_2);
    }
    if (action.action_type === "propose_intervention") {
      return Boolean(action.target_drug_id && action.intervention_type);
    }
    return true;
  };

  const handleStep = async (overrideAction = null) => {
    if (!hasValidEpisode) {
      appendLog("Run Reset Episode before stepping.");
      return;
    }
    setLoading(true);
    try {
      const payload = overrideAction || buildActionPayload();
      const msg = await wsSend("step", payload);
      const data = msg?.data || {};
      const normalized = normalizeObsFromWs(data);
      setObs(normalized);
      appendLog(`Step: ${payload.action_type} -> reward=${data.reward ?? 0}`);
    } catch (err) {
      appendLog(`Step failed: ${err.message}`);
    } finally {
      setLoading(false);
    }
  };

  const askAi = async () => {
    if (!hasValidEpisode) {
      appendLog("Run Reset Episode before asking AI.");
      return;
    }
    setLoading(true);
    try {
      const data = await apiPost("/agent/suggest", { observation: obs });
      appendLog(`AI suggestion: ${data.action.action_type}`);
      await handleStep(data.action);
    } catch (err) {
      appendLog(`AI suggestion failed: ${err.message}`);
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="shell">
      <div className="bg-orb orb-a" />
      <div className="bg-orb orb-b" />

      <div className="container">
      <header className="topbar glass">
        <div className="title-wrap">
          <h1>Polypharmacy Control Center</h1>
          <p>Metaverse Clinical Ops Console</p>
        </div>
        <div className={`status-chip ${hasValidEpisode ? "live" : "idle"}`}>
          {hasValidEpisode ? "Session Live" : "Waiting for reset"}
        </div>
        <div className="actions">
          <select value={taskId} onChange={(e) => setTaskId(e.target.value)}>
            {TASKS.map((t) => (
              <option key={t} value={t}>
                {t}
              </option>
            ))}
          </select>
          <button onClick={handleReset} disabled={loading}>
            Reset Episode
          </button>
          <button className="secondary" onClick={askAi} disabled={!hasValidEpisode || isDone || loading}>
            Ask AI + Auto Step
          </button>
        </div>
      </header>

      <main className="layout">
        <section className="panel glass panel-wide">
          <h2>Episode</h2>
          {hasValidEpisode ? (
            <div className="kpi-grid">
              <div><span>Episode</span><strong>{obs.episode_id}</strong></div>
              <div><span>Task</span><strong>{obs.task_id}</strong></div>
              <div><span>Age / Sex</span><strong>{obs.age} / {obs.sex}</strong></div>
              <div><span>Step</span><strong>{obs.step_index}</strong></div>
              <div><span>Query budget</span><strong>{obs.remaining_query_budget}</strong></div>
              <div><span>Intervention budget</span><strong>{obs.remaining_intervention_budget}</strong></div>
            </div>
          ) : (
            <p className="muted">Start with Reset Episode. Until then, step actions are blocked.</p>
          )}
          {noBudgetsLeft && (
            <p className="muted budget-note">Query and intervention budgets are exhausted. Finish review to get final score.</p>
          )}
          {isDone && (
            <p className="muted budget-note">
              Episode complete
              {finalScore !== null ? ` • final score: ${finalScore.toFixed(3)}` : ""}.
              Click Reset Episode to start a new case.
            </p>
          )}
        </section>

        <section className="panel glass">
          <h2>Action Console</h2>
          <div className="action-row">
            <label>Action type</label>
            <select
              value={action.action_type}
              onChange={(e) => setAction((a) => ({ ...a, action_type: e.target.value }))}
            >
              <option value="query_ddi">query_ddi</option>
              <option value="propose_intervention">propose_intervention</option>
              <option value="finish_review">finish_review</option>
            </select>
          </div>

          {action.action_type === "query_ddi" && (
            <div className="stack stack-two">
              <input
                placeholder="drug_id_1"
                value={action.drug_id_1}
                onChange={(e) => setAction((a) => ({ ...a, drug_id_1: e.target.value }))}
              />
              <input
                placeholder="drug_id_2"
                value={action.drug_id_2}
                onChange={(e) => setAction((a) => ({ ...a, drug_id_2: e.target.value }))}
              />
            </div>
          )}

          {action.action_type === "propose_intervention" && (
            <div className="stack">
              <select
                value={action.target_drug_id}
                onChange={(e) => setAction((a) => ({ ...a, target_drug_id: e.target.value }))}
              >
                <option value="">Select target drug</option>
                {medIds.map((id) => (
                  <option key={id} value={id}>
                    {id}
                  </option>
                ))}
              </select>
              <select
                value={action.intervention_type}
                onChange={(e) => setAction((a) => ({ ...a, intervention_type: e.target.value }))}
              >
                <option value="stop">stop</option>
                <option value="dose_reduce">dose_reduce</option>
                <option value="substitute">substitute</option>
                <option value="add_monitoring">add_monitoring</option>
              </select>
              <input
                placeholder="proposed_new_drug_id (optional)"
                value={action.proposed_new_drug_id}
                onChange={(e) =>
                  setAction((a) => ({ ...a, proposed_new_drug_id: e.target.value }))
                }
              />
              <input
                placeholder="rationale (optional)"
                value={action.rationale}
                onChange={(e) => setAction((a) => ({ ...a, rationale: e.target.value }))}
              />
            </div>
          )}
          <button onClick={() => handleStep()} disabled={!isActionValid() || loading}>
            {noBudgetsLeft ? "Finish Review" : "Submit Step"}
          </button>
        </section>

        <section className="panel glass">
          <h2>Current Medications</h2>
          <div className="med-grid">
            {(obs?.current_medications || []).map((m) => (
              <div key={m.drug_id} className="med-card">
                <strong>{m.drug_id}</strong>
                <p>{m.generic_name}</p>
                <small>{m.dose_mg} mg • {m.atc_class}</small>
              </div>
            ))}
          </div>
        </section>

        <section className="panel glass">
          <h2>Event Log</h2>
          <div className="logs">
            {log.map((line, idx) => (
              <div key={idx}>{line}</div>
            ))}
          </div>
        </section>
      </main>
      </div>
    </div>
  );
}