File size: 15,786 Bytes
ee3eb53 009a97a ee3eb53 21d3506 28269c5 ee3eb53 ef945f2 ee3eb53 a94de35 ef945f2 a94de35 ef945f2 a94de35 ef945f2 a94de35 ef945f2 a94de35 ef945f2 a94de35 ef945f2 a94de35 ef945f2 a94de35 ef945f2 ee3eb53 ef945f2 ee3eb53 ef945f2 ee3eb53 ef945f2 3beb9d7 a94de35 ef945f2 a94de35 ef945f2 5a8c8b7 ef945f2 5a8c8b7 ef945f2 5a8c8b7 ef945f2 21d3506 ee3eb53 ef945f2 ee3eb53 | 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 | import React, { useEffect, useRef } from 'react';
import ReactMarkdown from 'react-markdown';
import type { ChatMessage } from '../../types/chat.types.ts';
import { Loader2, AlertCircle, RotateCcw } from 'lucide-react';
import AudioPlayer from './AudioPlayer.tsx';
import TranscriptToggle from './TranscriptToggle.tsx';
type MessageListProps = {
messages: ChatMessage[];
isLoading?: boolean;
error?: string | null;
onRetry?: () => void;
};
/**
* Helper to determine if a message should use the specialized detection stance UI
*/
const isSpecializedStanceUI = (message: ChatMessage): boolean => {
const toolLower = message.tool?.toLowerCase() || '';
if (!toolLower.includes('detect') || !toolLower.includes('stance')) return false;
if (message.role === 'user') {
return message.content.includes('**Topic:**');
}
return message.role === 'assistant';
};
const MessageList = ({ messages, isLoading = false, error = null, onRetry }: MessageListProps) => {
const messagesEndRef = useRef<HTMLDivElement>(null);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
};
useEffect(() => {
scrollToBottom();
}, [messages, isLoading]);
const formatTime = (date: Date) => {
return date.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
});
};
const getToolClasses = (message: ChatMessage) => {
if (message.role !== 'assistant' || !message.tool) return '';
const toolLower = message.tool.toLowerCase();
// Match tool names flexibly (case-insensitive, handles variations)
if (toolLower.includes('detect') && toolLower.includes('stance')) {
return 'border-l-4 border-blue-400 pl-3 bg-blue-50/30 dark:bg-blue-900/10';
}
if (toolLower.includes('generate') && toolLower.includes('argument')) {
return 'border-l-4 border-purple-400 pl-3 bg-purple-50/30 dark:bg-purple-900/10';
}
if (toolLower.includes('extract') && toolLower.includes('topic')) {
return 'border-l-4 border-emerald-400 pl-3 bg-emerald-50/30 dark:bg-emerald-900/10';
}
// Default styling for other tools
return 'border-l-4 border-teal-400 pl-3 bg-teal-50/30 dark:bg-teal-900/10';
};
const getToolLabel = (tool: string | null | undefined): string => {
if (!tool) return '';
const toolLower = tool.toLowerCase();
if (toolLower.includes('detect') && toolLower.includes('stance')) {
return 'Detect Stance';
}
if (toolLower.includes('generate') && toolLower.includes('argument')) {
return 'Generate Argument';
}
if (toolLower.includes('extract') && toolLower.includes('topic')) {
return 'Extract Topic';
}
// Return formatted tool name (capitalize first letter of each word)
return tool.split(/[\s_-]+/).map(word =>
word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
).join(' ');
};
return (
<div className="flex-1 overflow-y-auto px-4 py-6 space-y-4">
{messages.length === 0 && !isLoading && (
<div className="flex flex-col items-center justify-center h-full text-center">
<div className="mb-4">
<div className="w-16 h-16 bg-gradient-to-br from-teal-400 to-blue-500 rounded-full flex items-center justify-center">
<svg className="w-8 h-8 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z" />
</svg>
</div>
</div>
<h3 className="text-lg font-medium text-gray-900 dark:text-gray-100 mb-2">
Start a conversation
</h3>
<p className="text-sm text-gray-500 dark:text-gray-400 max-w-md">
Ask me anything! I'm powered by Meta's Llama 4 Scout model and can help with a wide range of topics.
</p>
</div>
)}
{messages.map((message, index) => {
const showSpecializedStance = isSpecializedStanceUI(message);
return (
<div
key={message.id}
className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
>
<div
className={`max-w-3xl px-4 py-3 rounded-2xl ${message.role === 'user'
? 'bg-teal-500 text-white ml-12'
: 'bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-gray-100 mr-12'
}${getToolClasses(message) ? ' ' + getToolClasses(message) : ''}`}
>
{message.audioUrl ? (
<div>
<AudioPlayer
src={message.audioUrl}
autoPlay={message.role === 'assistant' && index === messages.length - 1}
/>
<TranscriptToggle content={message.content} />
</div>
) : (
<div className="space-y-3">
{message.tool && message.role === 'assistant' && (
<div className="mb-1 text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
{getToolLabel(message.tool)}
</div>
)}
{showSpecializedStance ? (
<div className="space-y-3">
{message.role === 'user' ? (
<div className="space-y-2">
{message.content.split('\n').map((line, idx) => {
if (line.startsWith('**Topic:**')) {
const topicText = line.replace('**Topic:**', '').trim();
return (
<div key={idx}>
<span className="text-xs font-semibold opacity-90">Topic: </span>
<span className="text-sm">{topicText}</span>
</div>
);
} else if (line.startsWith('**Argument:**')) {
const argumentText = line.replace('**Argument:**', '').trim();
return (
<div key={idx}>
<span className="text-xs font-semibold opacity-90">Argument: </span>
<span className="text-sm">{argumentText}</span>
</div>
);
}
return null;
})}
</div>
) : (
<div className="space-y-3">
{message.content.split('\n').map((line, idx) => {
if (line.startsWith('**Stance:**')) {
const stanceText = line.replace('**Stance:**', '').trim();
const isPro = stanceText.includes('PRO') || stanceText.toLowerCase().includes('positive');
return (
<div key={idx} className="flex items-center gap-2">
<span className="text-xs font-semibold text-gray-600 dark:text-gray-400">Stance:</span>
<span className={`px-2.5 py-1 rounded-full text-xs font-semibold ${isPro
? 'bg-emerald-100 text-emerald-800 dark:bg-emerald-900/40 dark:text-emerald-200'
: 'bg-rose-100 text-rose-800 dark:bg-rose-900/40 dark:text-rose-200'
}`}>
{stanceText}
</span>
</div>
);
} else if (line.startsWith('**Confidence:**')) {
const confidenceText = line.replace('**Confidence:**', '').trim();
const confidenceValue = parseFloat(confidenceText.replace('%', '')) || 0;
const getConfidenceColor = (value: number) => {
if (value >= 80) return 'bg-emerald-500';
if (value >= 60) return 'bg-yellow-500';
return 'bg-orange-500';
};
return (
<div key={idx} className="space-y-2">
<div className="flex items-center justify-between gap-2">
<span className="text-xs font-semibold text-gray-600 dark:text-gray-400">Confidence:</span>
<span className="text-sm font-medium text-gray-700 dark:text-gray-300">{confidenceText}</span>
</div>
<div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5 overflow-hidden">
<div
className={`h-full transition-all duration-500 ${getConfidenceColor(confidenceValue)}`}
style={{ width: `${Math.min(confidenceValue, 100)}%` }}
/>
</div>
</div>
);
} else if (line.startsWith('**Explanation:**')) {
const explanationParts = message.content.split('**Explanation:**\n');
const explanationText = explanationParts.length > 1 ? explanationParts[1] : '';
return (
<div key={idx} className="pt-2 border-t border-gray-200 dark:border-gray-700">
<div className="text-xs font-semibold text-gray-600 dark:text-gray-400 mb-1">Explanation:</div>
<div className="text-sm text-gray-700 dark:text-gray-300 whitespace-pre-wrap leading-relaxed">
{explanationText}
</div>
</div>
);
}
return null;
})}
</div>
)}
</div>
) : (
<div className="text-sm leading-relaxed break-words">
<ReactMarkdown
components={{
p: ({ node, ...props }) => (
<p {...props} className="whitespace-pre-wrap mb-2 last:mb-0" />
),
ul: ({ node, ...props }) => (
<ul {...props} className="list-disc ml-5 space-y-1 mb-2 last:mb-0" />
),
ol: ({ node, ...props }) => (
<ol {...props} className="list-decimal ml-5 space-y-1 mb-2 last:mb-0" />
),
li: ({ node, ...props }) => (
<li {...props} className="ml-1" />
),
}}
>
{typeof message.content === 'string' ? message.content : ''}
</ReactMarkdown>
</div>
)}
{/* Process pipeline for extract topic tool */}
{message.role === 'assistant' &&
message.tool?.toLowerCase().includes('extract') &&
message.tool?.toLowerCase().includes('topic') &&
message.process && (
<div className="mt-3 pt-3 border-t border-gray-200 dark:border-gray-700">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-[10px] uppercase tracking-wide text-gray-400 dark:text-gray-500 font-medium">
Process:
</span>
{message.process.split('→').map((step, sIdx, steps) => (
<React.Fragment key={sIdx}>
<span className="px-2 py-0.5 text-[10px] bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 rounded font-medium">
{step.trim()}
</span>
{sIdx < steps.length - 1 && (
<span className="text-gray-400 dark:text-gray-600 text-xs">→</span>
)}
</React.Fragment>
))}
</div>
</div>
)}
{/* Stance info for generate argument tool */}
{message.role === 'assistant' &&
message.tool?.toLowerCase().includes('generate') &&
message.tool?.toLowerCase().includes('argument') &&
message.stance && (
<div className="mt-2 flex gap-2">
<span className={`px-2.5 py-0.5 text-[10px] rounded-full font-semibold uppercase tracking-wider ${message.stance === 'positive'
? 'bg-emerald-500 text-white'
: 'bg-rose-500 text-white'
}`}>
{message.stance} stance
</span>
</div>
)}
</div>
)}
<div
className={`text-[10px] mt-2 opacity-60 ${message.role === 'user' ? 'text-right opacity-80' : 'text-left'
}`}
>
{formatTime(message.timestamp)}
</div>
</div>
</div>
);
})}
{isLoading && (
<div className="flex justify-start">
<div className="bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-gray-100 mr-12 max-w-3xl px-4 py-3 rounded-2xl">
<div className="flex items-center space-x-2">
<Loader2 className="w-4 h-4 animate-spin" />
<span className="text-sm">Thinking...</span>
</div>
</div>
</div>
)}
{error && (
<div className="flex justify-start">
<div className="bg-red-50 dark:bg-red-900/20 text-red-900 dark:text-red-100 mr-12 max-w-3xl px-4 py-3 rounded-2xl border border-red-200 dark:border-red-800">
<div className="flex items-start space-x-2">
<AlertCircle className="w-5 h-5 text-red-500 dark:text-red-400 flex-shrink-0 mt-0.5" />
<div className="flex-1">
<p className="text-sm font-medium">Error</p>
<p className="text-sm opacity-90">{error}</p>
{onRetry && (
<button
onClick={onRetry}
className="mt-2 flex items-center space-x-1 text-sm text-red-600 dark:text-red-400 hover:text-red-700 dark:hover:text-red-300 transition-colors"
>
<RotateCcw className="w-3 h-3" />
<span>Retry</span>
</button>
)}
</div>
</div>
</div>
</div>
)}
<div ref={messagesEndRef} />
</div>
);
};
export default MessageList;
|