File size: 7,290 Bytes
86f402d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useState } from 'react';
import { ToolCall } from '../types';
import './ToolCallCard.css';

interface ToolCallCardProps {
  toolCall: ToolCall;
}

/** One-line summary shown in the collapsed header so results are visible at a glance */
function CollapsedSummary({ toolCall }: { toolCall: ToolCall }) {
  const r = toolCall.result;
  if (!r) return null;

  if (toolCall.tool === 'analyze_image') {
    const name = r.full_name ?? r.diagnosis;
    const pct = r.confidence != null ? `${Math.round(r.confidence * 100)}%` : null;
    if (name) return (
      <span className="tool-header-summary">
        {name}{pct ? ` β€” ${pct}` : ''}
      </span>
    );
  }

  if (toolCall.tool === 'compare_images') {
    const key = r.status_label ?? 'STABLE';
    const cfg = STATUS_CONFIG[key] ?? { emoji: 'βšͺ', label: key };
    return (
      <span className="tool-header-summary">
        {cfg.emoji} {cfg.label}
      </span>
    );
  }

  return null;
}

export function ToolCallCard({ toolCall }: ToolCallCardProps) {
  // Auto-expand when the tool completes so results are immediately visible.
  // User can collapse manually afterwards.
  const [expanded, setExpanded] = useState(false);

  useEffect(() => {
    if (toolCall.status === 'complete') setExpanded(true);
  }, [toolCall.status]);

  const isLoading = toolCall.status === 'calling';
  const isError = toolCall.status === 'error';

  const icon = toolCall.tool === 'compare_images' ? 'πŸ”„' : 'πŸ”¬';
  const label = toolCall.tool.replace(/_/g, ' ');

  return (
    <div className={`tool-card ${isLoading ? 'loading' : ''} ${isError ? 'error' : ''}`}>
      <button
        className="tool-card-header"
        onClick={() => !isLoading && setExpanded(e => !e)}
        disabled={isLoading}
      >
        <span className="tool-icon">{icon}</span>
        <span className="tool-label">{label}</span>
        {isLoading ? (
          <span className="tool-status calling">
            <span className="spinner" /> running…
          </span>
        ) : isError ? (
          <span className="tool-status error-text">error</span>
        ) : (
          <>
            <span className="tool-status done">βœ“</span>
            {!expanded && <CollapsedSummary toolCall={toolCall} />}
          </>
        )}
        {!isLoading && (
          <span className="tool-chevron">{expanded ? 'β–²' : 'β–Ό'}</span>
        )}
      </button>

      {expanded && !isLoading && toolCall.result && (
        <div className="tool-card-body">
          {toolCall.tool === 'analyze_image' && (
            <AnalyzeImageResult result={toolCall.result} />
          )}
          {toolCall.tool === 'compare_images' && (
            <CompareImagesResult result={toolCall.result} />
          )}
          {toolCall.tool !== 'analyze_image' && toolCall.tool !== 'compare_images' && (
            <GenericResult result={toolCall.result} />
          )}
        </div>
      )}
    </div>
  );
}

/* ─── analyze_image renderer ─────────────────────────────────────────────── */

function AnalyzeImageResult({ result }: { result: ToolCall['result'] }) {
  if (!result) return null;

  const hasClassifier = result.diagnosis != null;
  const topPrediction = result.all_predictions?.[0];
  const otherPredictions = result.all_predictions?.slice(1) ?? [];
  const confidence = result.confidence ?? topPrediction?.probability ?? 0;
  const pct = Math.round(confidence * 100);
  const statusColor = pct >= 70 ? '#ef4444' : pct >= 40 ? '#f59e0b' : '#22c55e';

  return (
    <div className="analyze-result">
      <div className="analyze-top">
        {result.image_url && (
          <img
            src={result.image_url}
            alt="Analyzed lesion"
            className="analyze-thumb"
          />
        )}
        <div className="analyze-info">
          {hasClassifier ? (
            <>
              <p className="diagnosis-name">{result.full_name ?? result.diagnosis}</p>
              <p className="confidence-label" style={{ color: statusColor }}>
                Confidence: {pct}%
              </p>
              <div className="confidence-bar-track">
                <div
                  className="confidence-bar-fill"
                  style={{ width: `${pct}%`, background: statusColor }}
                />
              </div>
            </>
          ) : (
            <p className="diagnosis-name" style={{ color: 'var(--gray-500)', fontWeight: 400, fontSize: '0.875rem' }}>
              Visual assessment complete β€” classifier unavailable
            </p>
          )}
        </div>
      </div>

      {hasClassifier && otherPredictions.length > 0 && (
        <ul className="other-predictions">
          {otherPredictions.map(p => (
            <li key={p.class} className="prediction-row">
              <span className="pred-name">{p.full_name ?? p.class}</span>
              <span className="pred-pct">{Math.round(p.probability * 100)}%</span>
            </li>
          ))}
        </ul>
      )}
    </div>
  );
}

/* ─── compare_images renderer ────────────────────────────────────────────── */

const STATUS_CONFIG: Record<string, { label: string; color: string; emoji: string }> = {
  STABLE: { label: 'Stable', color: '#22c55e', emoji: '🟒' },
  MINOR_CHANGE: { label: 'Minor Change', color: '#f59e0b', emoji: '🟑' },
  SIGNIFICANT_CHANGE: { label: 'Significant Change', color: '#ef4444', emoji: 'πŸ”΄' },
  IMPROVED: { label: 'Improved', color: '#3b82f6', emoji: 'πŸ”΅' },
};

function CompareImagesResult({ result }: { result: ToolCall['result'] }) {
  if (!result) return null;

  const statusKey = result.status_label ?? 'STABLE';
  const status = STATUS_CONFIG[statusKey] ?? { label: statusKey, color: '#6b7280', emoji: 'βšͺ' };
  const featureChanges = Object.entries(result.feature_changes ?? {});

  return (
    <div className="compare-result">
      <div className="compare-status" style={{ color: status.color }}>
        <strong>Status: {status.label} {status.emoji}</strong>
      </div>

      {featureChanges.length > 0 && (
        <ul className="feature-changes">
          {featureChanges.map(([name, vals]) => {
            const delta = vals.curr - vals.prev;
            const sign = delta > 0 ? '+' : '';
            return (
              <li key={name} className="feature-row">
                <span className="feature-name">{name}</span>
                <span className="feature-delta" style={{ color: Math.abs(delta) > 0.1 ? '#f59e0b' : '#6b7280' }}>
                  {sign}{(delta * 100).toFixed(1)}%
                </span>
              </li>
            );
          })}
        </ul>
      )}

      {result.summary && (
        <p className="compare-summary">{result.summary}</p>
      )}
    </div>
  );
}

/* ─── Generic (unknown tool) renderer ───────────────────────────────────── */

function GenericResult({ result }: { result: ToolCall['result'] }) {
  return (
    <pre className="generic-result">
      {JSON.stringify(result, null, 2)}
    </pre>
  );
}