File size: 9,984 Bytes
86f402d 58a4476 86f402d 58a4476 86f402d 58a4476 86f402d 58a4476 86f402d 58a4476 86f402d 5241b71 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 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 | import { useEffect, useRef, useState } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { api } from '../services/api';
import { streamChatMessage } from '../services/streaming';
import { ToolCallCard } from '../components/ToolCallCard';
import { MessageContent } from '../components/MessageContent';
import { Patient, ChatMessage, ToolCall } from '../types';
import './ChatPage.css';
function formatTime(ts: string) {
return new Date(ts).toLocaleTimeString('en-US', {
hour: 'numeric',
minute: '2-digit',
});
}
export function ChatPage() {
const { patientId } = useParams<{ patientId: string }>();
const navigate = useNavigate();
const [patient, setPatient] = useState<Patient | null>(null);
const [messages, setMessages] = useState<ChatMessage[]>([]);
const [input, setInput] = useState('');
const [selectedImage, setSelectedImage] = useState<File | null>(null);
const [imagePreview, setImagePreview] = useState<string | null>(null);
const [isStreaming, setIsStreaming] = useState(false);
const messagesEndRef = useRef<HTMLDivElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
useEffect(() => {
if (!patientId) return;
api.getPatient(patientId).then(res => setPatient(res.patient));
api.getChatHistory(patientId).then(res => setMessages(res.messages ?? []));
}, [patientId]);
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);
const handleImageSelect = (file: File) => {
setSelectedImage(file);
setImagePreview(URL.createObjectURL(file));
};
const handleSend = async () => {
if ((!input.trim() && !selectedImage) || !patientId || isStreaming) return;
const userMsgId = `msg-${Date.now()}`;
const assistantMsgId = `msg-${Date.now() + 1}`;
const userMsg: ChatMessage = {
id: userMsgId,
role: 'user',
content: input,
timestamp: new Date().toISOString(),
image_url: imagePreview ?? undefined,
};
const assistantMsg: ChatMessage = {
id: assistantMsgId,
role: 'assistant',
content: '',
timestamp: new Date().toISOString(),
tool_calls: [],
};
setMessages(prev => [...prev, userMsg, assistantMsg]);
const imgToSend = selectedImage;
const contentToSend = input;
setInput('');
setSelectedImage(null);
setImagePreview(null);
setIsStreaming(true);
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
}
await streamChatMessage(patientId, contentToSend, imgToSend, {
onText: (chunk) => {
setMessages(prev =>
prev.map(m =>
m.id === assistantMsgId ? { ...m, content: m.content + chunk } : m
)
);
},
onToolStart: (tool, callId) => {
setMessages(prev =>
prev.map(m =>
m.id === assistantMsgId
? {
...m,
tool_calls: [
...(m.tool_calls ?? []),
{ id: callId, tool, status: 'calling' as const },
],
}
: m
)
);
},
onToolResult: (_tool, callId, result) => {
setMessages(prev =>
prev.map(m =>
m.id === assistantMsgId
? {
...m,
tool_calls: (m.tool_calls ?? []).map(tc =>
tc.id === callId
? { ...tc, status: 'complete' as const, result: result as ToolCall['result'] }
: tc
),
}
: m
)
);
},
onDone: () => setIsStreaming(false),
onError: (err) => {
setMessages(prev =>
prev.map(m =>
m.id === assistantMsgId ? { ...m, content: `[ERROR]${err}[/ERROR]` } : m
)
);
setIsStreaming(false);
},
});
};
const handleClear = async () => {
if (!patientId || !confirm('Clear chat history?')) return;
await api.clearChat(patientId);
setMessages([]);
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
const handleTextareaChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setInput(e.target.value);
e.target.style.height = 'auto';
e.target.style.height = `${Math.min(e.target.scrollHeight, 160)}px`;
};
return (
<div className="chat-page">
{/* Header */}
<header className="chat-header">
<button className="header-back-btn" onClick={() => navigate('/')} title="Back to patients">
<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z" />
</svg>
</button>
<div className="header-center">
<span className="header-app-name">SkinProAI</span>
{patient && <span className="header-patient-name">{patient.name}</span>}
</div>
<button className="header-clear-btn" onClick={handleClear} title="Clear history">
Clear
</button>
</header>
{/* Messages */}
<main className="chat-messages">
{messages.length === 0 && (
<div className="chat-empty">
<div className="chat-empty-icon">
<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2z" />
</svg>
</div>
<p>Send a message or attach a skin image to begin analysis.</p>
</div>
)}
{messages.map(msg => (
<div key={msg.id} className={`message-row ${msg.role}`}>
{msg.role === 'user' ? (
<div className="user-message">
<div className="user-bubble">
{msg.image_url && (
<img src={msg.image_url} className="message-image" alt="Attached" />
)}
{msg.content && <p className="bubble-text">{msg.content}</p>}
</div>
<span className="msg-time">{formatTime(msg.timestamp)}</span>
</div>
) : (
<div className="assistant-message">
{/* Tool call status lines */}
{(msg.tool_calls ?? []).map(tc => (
<ToolCallCard key={tc.id} toolCall={tc} />
))}
{/* Text content */}
{msg.content ? (
<div className="assistant-text">
<MessageContent text={msg.content} />
</div>
) : (!msg.tool_calls || msg.tool_calls.length === 0) && isStreaming ? (
<div className="thinking">
<span className="dot" />
<span className="dot" />
<span className="dot" />
</div>
) : null}
{(msg.content || (msg.tool_calls && msg.tool_calls.length > 0)) && (
<span className="msg-time">{formatTime(msg.timestamp)}</span>
)}
</div>
)}
</div>
))}
{/* Hint after analysis completes */}
{!isStreaming && messages.length > 0 && (() => {
const last = messages[messages.length - 1];
const hasAnalysis = last.role === 'assistant' &&
(last.tool_calls ?? []).some(tc => tc.tool === 'analyze_image');
if (!hasAnalysis) return null;
return (
<p className="post-analysis-hint">
Ask a follow-up question, add context, or upload an image to compare to previous.
</p>
);
})()}
<div ref={messagesEndRef} />
</main>
{/* Input bar */}
<footer className="chat-input-bar">
{imagePreview && (
<div className="image-preview-container">
<img src={imagePreview} alt="Preview" className="image-preview-thumb" />
<button
className="remove-image-btn"
onClick={() => { setSelectedImage(null); setImagePreview(null); }}
title="Remove image"
>
×
</button>
</div>
)}
<div className="input-row">
<button
className="attach-btn"
onClick={() => fileInputRef.current?.click()}
title="Attach image"
disabled={isStreaming}
>
<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M16.5 6v11.5c0 2.21-1.79 4-4 4s-4-1.79-4-4V5a2.5 2.5 0 015 0v10.5c0 .83-.67 1.5-1.5 1.5s-1.5-.67-1.5-1.5V6H10v9.5a2.5 2.5 0 005 0V5c0-2.21-1.79-4-4-4S7 2.79 7 5v12.5c0 3.04 2.46 5.5 5.5 5.5s5.5-2.46 5.5-5.5V6h-1.5z" />
</svg>
</button>
<input
ref={fileInputRef}
type="file"
accept="image/*"
style={{ display: 'none' }}
onChange={e => e.target.files?.[0] && handleImageSelect(e.target.files[0])}
/>
<textarea
ref={textareaRef}
className="chat-input"
placeholder="Type a message..."
value={input}
onChange={handleTextareaChange}
onKeyDown={handleKeyDown}
disabled={isStreaming}
rows={1}
/>
<button
className="send-btn"
onClick={handleSend}
disabled={isStreaming || (!input.trim() && !selectedImage)}
title="Send"
>
<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" />
</svg>
</button>
</div>
</footer>
</div>
);
}
|