File size: 10,785 Bytes
53ba2ed
 
 
6a059ac
53ba2ed
 
 
6a059ac
7285ff3
53ba2ed
b94a1a8
53ba2ed
 
 
 
 
 
 
 
 
 
 
6a059ac
 
53ba2ed
 
 
 
 
 
 
 
7285ff3
 
 
53ba2ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7285ff3
53ba2ed
 
3024450
53ba2ed
3024450
 
 
53ba2ed
3024450
 
53ba2ed
6a059ac
 
ea0f7e0
 
 
 
 
 
 
 
 
 
53ba2ed
 
 
 
d23fef9
bf73afc
 
 
d23fef9
53ba2ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a059ac
c8d750f
 
 
 
 
 
 
53ba2ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a059ac
 
53ba2ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a059ac
 
 
 
 
 
 
d23fef9
 
 
 
 
 
 
 
 
 
 
497bd10
d23fef9
 
 
 
 
 
 
 
53ba2ed
 
 
 
 
 
 
 
7285ff3
53ba2ed
 
 
 
 
 
 
 
 
 
 
 
7285ff3
53ba2ed
7285ff3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53ba2ed
 
 
 
 
 
 
6a059ac
 
53ba2ed
 
 
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
import { useState, useEffect, useCallback } from 'react';
import { Activity } from 'lucide-react';
import Welcome from './Welcome';
import Tutorial from './Tutorial';
import { PatientPanel } from './PatientPanel';
import { AgentPanel } from './AgentPanel';
import { BriefPanel } from './BriefPanel';
import { ApiKeyModal } from './ApiKeyModal';
import ConsultPanel from './ConsultPanel';

const API = import.meta.env.VITE_API_BASE || '';

const INITIAL_STATUSES = {
  triage: 'idle', contraindication: 'idle',
  labs: 'idle', differentials: 'idle', action_protocol: 'idle',
};

// Staggered agent activation for visual drama
const AGENT_ORDER = ['triage', 'contraindication', 'labs', 'differentials', 'action_protocol'];

export default function App() {
  const [showWelcome, setShowWelcome] = useState(true);
  const [showTutorial, setShowTutorial] = useState(false);
  const [showApiModal, setShowApiModal] = useState(false);
  const [patients, setPatients] = useState([]);
  const [patientsLoading, setPatientsLoading] = useState(false);
  const [selected, setSelected] = useState(null);
  const [running, setRunning] = useState(false);
  const [agentStatuses, setAgentStatuses] = useState(INITIAL_STATUSES);
  const [agentResults, setAgentResults] = useState({});
  const [briefData, setBriefData] = useState(null);

  const [activeTab, setActiveTab] = useState('orchestrate');
  const [focus, setFocus] = useState('General');

  // Load patients
  const fetchPatients = useCallback(async () => {
    setPatientsLoading(true);
    try {
      const res = await fetch(`${API}/patients`);
      const data = await res.json();
      setPatients(data.patients || []);
    } catch {
      setPatients([]);
    } finally {
      setPatientsLoading(false);
    }
  }, []);

  useEffect(() => { fetchPatients(); }, [fetchPatients]);

  const handleSelectPatient = (patient) => {
    setSelected(patient);
    setBriefData(null);
    setAgentStatuses(INITIAL_STATUSES);
    setAgentResults({});
    setActiveTab('orchestrate');
  };

  // Upload patient JSON/PDF (Multi-file support)
  const handleUpload = async (e) => {
    const fileList = Array.from(e.target.files);
    if (fileList.length === 0) return;
    
    const form = new FormData();
    fileList.forEach(f => form.append('files', f)); // 'files' is plural now
    
    try {
      const geminiKey = localStorage.getItem('gemini_key');
      const headers = geminiKey ? { 'x-gemini-key': geminiKey } : {};
      const res = await fetch(`${API}/patients/upload`, { method: 'POST', body: form, headers });
      if (!res.ok) {
        const err = await res.json();
        alert(`Upload failed: ${err.detail || 'Internal Server Error'}`);
      } else {
        fetchPatients();
      }
    } catch (err) {
      alert(`Network error during upload: ${err.message}`);
    }
    e.target.value = '';
  };

  // Run 5-agent orchestration with staggered UI animation
  const runOrchestration = async (targetPatient = null) => {
    // If targetPatient is an event object from a click handler, ignore it
    const isEvent = targetPatient && targetPatient.nativeEvent;
    const p = (targetPatient && !isEvent) ? targetPatient : selected;
    if (!p || running) return;
    setRunning(true);
    setBriefData(null);
    setAgentResults({});

    // Light up agents one by one with a delay for UX drama
    const activateAgents = async () => {
      for (let i = 0; i < AGENT_ORDER.length; i++) {
        const key = AGENT_ORDER[i];
        setAgentStatuses(prev => ({ ...prev, [key]: 'working' }));
        await new Promise(r => setTimeout(r, 180));
      }
    };
    activateAgents(); // fire and don't await — let it animate while API call runs

    try {
      const geminiKey = localStorage.getItem('gemini_key');
      const headers = { 'Content-Type': 'application/json' };
      if (geminiKey) headers['x-gemini-key'] = geminiKey;

      const res = await fetch(`${API}/orchestrate/${p.id}`, { 
        method: 'POST',
        headers 
      });
      if (!res.ok) throw new Error('API error');
      const data = await res.json();

      // Mark all complete with slight stagger
      for (let i = 0; i < AGENT_ORDER.length; i++) {
        const key = AGENT_ORDER[i];
        await new Promise(r => setTimeout(r, 120));
        setAgentStatuses(prev => ({ ...prev, [key]: 'complete' }));
        setAgentResults(prev => ({ ...prev, [key]: data.agents?.[key] }));
      }

      setBriefData(data);
      // Update the patient risk in sidebar from triage
      if (data.agents?.triage?.severity) {
        const sev = data.agents.triage.severity;
        const risk = sev <= 2 ? 'critical' : sev === 3 ? 'moderate' : 'stable';
        setSelected(prev => prev ? { ...prev, risk_level: risk } : prev);
      }
    } catch {
      AGENT_ORDER.forEach(key => {
        setAgentStatuses(prev => ({ ...prev, [key]: 'error' }));
      });
    } finally {
      setRunning(false);
    }
  };

  if (showWelcome) return <Welcome onStart={() => { setShowWelcome(false); setShowTutorial(true); }} />;
  if (showTutorial) return <Tutorial onComplete={() => setShowTutorial(false)} />;

  return (
    <div style={{ display: 'flex', flexDirection: 'column', height: '100vh', overflow: 'hidden' }}>
      {/* Header */}
      <header style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        padding: '0 20px', height: 48, flexShrink: 0,
        background: 'var(--bg-surface)', borderBottom: '1px solid var(--border-subtle)',
        zIndex: 10,
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <div style={{ animation: 'heartbeat 1.4s infinite', color: 'var(--red-critical)' }}>
            <Activity size={20} />
          </div>
          <span style={{ fontWeight: 800, fontSize: 15, letterSpacing: '-0.02em' }}>CodeBlue Context</span>
          <span style={{
            fontSize: 10, fontWeight: 600, padding: '2px 8px', borderRadius: 99,
            background: 'rgba(255,45,85,0.15)', color: 'var(--red-critical)',
            border: '1px solid rgba(255,45,85,0.3)', letterSpacing: '0.05em',
          }}>
            EMERGENCY INTELLIGENCE
          </span>
        </div>

        <div style={{ display: 'flex', alignItems: 'center', gap: 16, fontSize: 11, color: 'var(--text-tertiary)' }}>
          <button 
            onClick={() => setShowApiModal(true)}
            className="btn-ghost"
            style={{ color: 'var(--purple-ai)', borderColor: 'rgba(191,90,242,0.3)', padding: '6px 12px' }}
          >
            🔑 Provide API Key
          </button>
          <button 
            className="btn-demo"
            onClick={() => {
              const demoPatient = patients.find(p => p.id === 'CB-001') || patients[0];
              if (demoPatient) {
                handleSelectPatient(demoPatient);
                setTimeout(() => runOrchestration(demoPatient), 50); // slight delay to let UI select it
              }
            }}
            style={{
              padding: '6px 14px', borderRadius: 6, fontWeight: 700, cursor: 'pointer',
              background: 'var(--blue-accent)', color: '#fff', border: 'none',
              transition: 'all 0.2s',
              display: 'flex', alignItems: 'center', gap: '6px'
            }}
            onMouseOver={(e) => e.currentTarget.style.filter = 'brightness(1.1)'}
            onMouseOut={(e) => e.currentTarget.style.filter = 'brightness(1)'}
          >
            <Activity size={14} /> Run Live Demo
          </button>
          <span>
            <span style={{ color: 'var(--green-safe)', fontWeight: 700 }}>●</span> System Online
          </span>
          <span>{patients.length} patients</span>
        </div>
      </header>

      {/* 3-column layout */}
      <div style={{ flex: 1, display: 'grid', gridTemplateColumns: '240px 420px 1fr', overflow: 'hidden' }}>

        {/* Left: Patient List */}
        <div style={{ borderRight: '1px solid var(--border-subtle)', overflow: 'hidden', display: 'flex', flexDirection: 'column' }}>
          <PatientPanel
            patients={patients}
            selected={selected}
            onSelect={handleSelectPatient}
            onUpload={handleUpload}
            loading={patientsLoading}
          />
        </div>

        {/* Center: Tabs + Content */}
        <div style={{ borderRight: '1px solid var(--border-subtle)', overflow: 'hidden', display: 'flex', flexDirection: 'column' }}>
          <div style={{ display: 'flex', background: 'var(--bg-base)', padding: '4px 4px 0', borderBottom: '1px solid var(--border-subtle)' }}>
            <button 
              onClick={() => setActiveTab('orchestrate')}
              style={{
                flex: 1, padding: '10px', fontSize: 11, fontWeight: 700, cursor: 'pointer', border: 'none',
                background: activeTab === 'orchestrate' ? 'var(--bg-surface)' : 'transparent',
                color: activeTab === 'orchestrate' ? 'var(--blue-accent)' : 'var(--text-secondary)',
                borderRadius: '8px 8px 0 0', borderBottom: activeTab === 'orchestrate' ? '2px solid var(--blue-accent)' : 'none'
              }}
            >
              ORCHESTRATION
            </button>
            <button 
              onClick={() => setActiveTab('consult')}
              style={{
                flex: 1, padding: '10px', fontSize: 11, fontWeight: 700, cursor: 'pointer', border: 'none',
                background: activeTab === 'consult' ? 'var(--bg-surface)' : 'transparent',
                color: activeTab === 'consult' ? 'var(--purple-ai)' : 'var(--text-secondary)',
                borderRadius: '8px 8px 0 0', borderBottom: activeTab === 'consult' ? '2px solid var(--purple-ai)' : 'none'
              }}
            >
              ADVANCED CONSULT
            </button>
          </div>
          <div style={{ flex: 1, overflow: 'hidden' }}>
            {activeTab === 'orchestrate' ? (
              <AgentPanel
                patient={selected}
                agentStatuses={agentStatuses}
                agentResults={agentResults}
                onRun={runOrchestration}
                running={running}
                focus={focus}
                setFocus={setFocus}
              />
            ) : (
              <ConsultPanel 
                patientId={selected?.id} 
                previousResults={agentResults} 
              />
            )}
          </div>
        </div>

        {/* Right: Emergency Brief */}
        <div style={{ overflow: 'hidden', display: 'flex', flexDirection: 'column' }}>
          <BriefPanel data={briefData} patient={selected} />
        </div>
      </div>
      
      {showApiModal && <ApiKeyModal onClose={() => setShowApiModal(false)} />}
    </div>
  );
}