Spaces:
Sleeping
Sleeping
File size: 12,140 Bytes
36fa5d5 198828b 79d7d6f 198828b a801d04 198828b a801d04 198828b 36fa5d5 a801d04 198828b 36fa5d5 a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 79d7d6f 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b 36fa5d5 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b 36fa5d5 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b 79d7d6f a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 198828b a801d04 | 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 | import React, { useState } from 'react';
import { Button } from './ui/button';
import {
Copy,
ThumbsUp,
ThumbsDown,
ChevronDown,
ChevronUp,
Check,
X,
} from 'lucide-react';
import { Badge } from './ui/badge';
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from './ui/collapsible';
import { Textarea } from './ui/textarea';
import type { Message as MessageType } from '../App';
import { toast } from 'sonner';
import clareAvatar from '../assets/dfe44dab3ad8cd93953eac4a3e68bd1a5f999653.png';
// ✅ Markdown rendering (NEW)
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
// ✅ NEW: call backend feedback API
import {
apiFeedback,
type User as ApiUser,
type LearningMode as ApiLearningMode,
type FileType as ApiFileType,
type FeedbackRating,
} from '../lib/api';
interface MessageProps {
message: MessageType;
showSenderInfo?: boolean; // For group chat mode
// ✅ NEW (recommended) — for logging feedback correctly
user?: ApiUser | null;
// context (optional but useful for metadata)
learningMode?: ApiLearningMode;
docType?: ApiFileType | string;
// optional: supply refs (if your message.references is not the same as backend refs)
refs?: string[];
/**
* Optional: provide the user question that led to this assistant message.
* Best practice: parent passes a function that finds previous user message.
*/
getContextUserText?: () => string;
}
// 反馈标签选项
const FEEDBACK_TAGS: Record<FeedbackRating, string[]> = {
not_helpful: [
'Code was incorrect',
"Shouldn't have used Memory",
"Don't like the personality",
"Don't like the style",
'Not factually correct',
],
helpful: [
'Accurate and helpful',
'Clear explanation',
'Good examples',
'Solved my problem',
'Well structured',
],
};
export function Message({
message,
showSenderInfo = false,
// NEW
user = null,
learningMode,
docType,
refs,
getContextUserText,
}: MessageProps) {
const [feedback, setFeedback] = useState<FeedbackRating | null>(null);
const [copied, setCopied] = useState(false);
const [referencesOpen, setReferencesOpen] = useState(false);
const [showFeedbackArea, setShowFeedbackArea] = useState(false);
// ✅ unify to backend enum
const [feedbackType, setFeedbackType] = useState<FeedbackRating | null>(null);
const [feedbackText, setFeedbackText] = useState('');
const [selectedTags, setSelectedTags] = useState<string[]>([]);
const [submitting, setSubmitting] = useState(false);
const isUser = message.role === 'user';
const handleCopy = async () => {
await navigator.clipboard.writeText(message.content);
setCopied(true);
toast.success('Message copied to clipboard');
setTimeout(() => setCopied(false), 2000);
};
const handleFeedbackClick = (type: FeedbackRating) => {
if (feedback === type) {
// clicked same state -> collapse + reset detail
setFeedback(null);
setShowFeedbackArea(false);
setFeedbackType(null);
setFeedbackText('');
setSelectedTags([]);
return;
}
// open feedback area
setFeedback(type);
setFeedbackType(type);
setShowFeedbackArea(true);
};
const handleFeedbackClose = () => {
setShowFeedbackArea(false);
setFeedbackType(null);
setFeedbackText('');
setSelectedTags([]);
// do NOT reset feedback (keeps thumbs state)
};
const handleTagToggle = (tag: string) => {
setSelectedTags((prev) =>
prev.includes(tag) ? prev.filter((t) => t !== tag) : [...prev, tag],
);
};
const handleFeedbackSubmit = async () => {
if (!feedbackType) return;
// If not logged in / no user, we cannot attribute feedback to a student_id
if (!user?.email) {
toast.error('Please login to submit feedback.');
return;
}
const assistantText = (message.content || '').trim();
const userText = (getContextUserText ? getContextUserText() : '').trim();
// refs: prefer explicit refs prop; fallback to message.references
const refsToSend =
refs && refs.length > 0 ? refs : (message.references ?? []);
setSubmitting(true);
try {
await apiFeedback({
user,
rating: feedbackType,
assistantMessageId: message.id, // recommended
assistantText,
userText,
tags: selectedTags,
comment: feedbackText,
refs: refsToSend,
learningMode,
docType,
timestampMs: Date.now(),
});
toast.success('Thanks — feedback recorded.');
handleFeedbackClose();
// keep thumbs state
} catch (e: any) {
console.error('[feedback] submit failed:', e);
toast.error(e?.message ? `Feedback failed: ${e.message}` : 'Feedback failed.');
} finally {
setSubmitting(false);
}
};
return (
<div className={`flex gap-3 ${isUser && !showSenderInfo ? 'justify-end' : 'justify-start'}`}>
{/* Avatar */}
{showSenderInfo && message.sender ? (
<div
className={`w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 ${
message.sender.isAI ? 'overflow-hidden bg-white' : 'bg-muted'
}`}
>
{message.sender.isAI ? (
<img src={clareAvatar} alt="Clare" className="w-full h-full object-cover" />
) : (
<span className="text-sm">
{message.sender.name
.split(' ')
.map((n) => n[0])
.join('')
.toUpperCase()}
</span>
)}
</div>
) : !isUser ? (
<div className="w-8 h-8 rounded-full overflow-hidden bg-white flex items-center justify-center flex-shrink-0">
<img src={clareAvatar} alt="Clare" className="w-full h-full object-cover" />
</div>
) : null}
<div className={`flex flex-col gap-2 max-w-[80%] ${isUser && !showSenderInfo ? 'items-end' : 'items-start'}`}>
{/* Sender name in group chat */}
{showSenderInfo && message.sender && (
<div className="flex items-center gap-2 px-1">
<span className="text-xs">{message.sender.name}</span>
{message.sender.isAI && (
<Badge variant="secondary" className="text-xs h-4 px-1">
AI
</Badge>
)}
</div>
)}
<div
className={`
rounded-2xl px-4 py-3
${isUser && !showSenderInfo ? 'bg-primary text-primary-foreground' : 'bg-muted'}
`}
>
{/* ✅ KEY FIX: assistant uses Markdown renderer */}
{isUser ? (
<p className="whitespace-pre-wrap">{message.content}</p>
) : (
<div className="prose prose-sm max-w-none dark:prose-invert">
<ReactMarkdown
remarkPlugins={[remarkGfm]}
components={{
// keep line breaks looking natural in chat bubbles
p: ({ children }) => <p className="whitespace-pre-wrap">{children}</p>,
// code blocks + inline code styling
code: ({ className, children }) => (
<code className={className ? className : 'px-1 py-0.5 rounded bg-black/5 dark:bg-white/10'}>
{children}
</code>
),
}}
>
{message.content}
</ReactMarkdown>
</div>
)}
</div>
{/* References */}
{message.references && message.references.length > 0 && (
<Collapsible open={referencesOpen} onOpenChange={setReferencesOpen}>
<CollapsibleTrigger asChild>
<Button variant="ghost" size="sm" className="gap-1 h-7 text-xs">
{referencesOpen ? <ChevronUp className="h-3 w-3" /> : <ChevronDown className="h-3 w-3" />}
{message.references.length} {message.references.length === 1 ? 'reference' : 'references'}
</Button>
</CollapsibleTrigger>
<CollapsibleContent className="space-y-1 mt-1">
{message.references.map((ref, index) => (
<Badge key={index} variant="outline" className="text-xs">
{ref}
</Badge>
))}
</CollapsibleContent>
</Collapsible>
)}
{/* Message Actions */}
<div className="flex items-center gap-1">
<Button variant="ghost" size="sm" className="h-7 gap-1" onClick={handleCopy}>
{copied ? (
<>
<Check className="h-3 w-3" />
<span className="text-xs">Copied</span>
</>
) : (
<>
<Copy className="h-3 w-3" />
<span className="text-xs">Copy</span>
</>
)}
</Button>
{!isUser && (
<>
<Button
variant="ghost"
size="sm"
className={`h-7 gap-1 ${
feedback === 'helpful' ? 'bg-green-100 text-green-600 dark:bg-green-900/20' : ''
}`}
onClick={() => handleFeedbackClick('helpful')}
>
<ThumbsUp className="h-3 w-3" />
<span className="text-xs">Helpful</span>
</Button>
<Button
variant="ghost"
size="sm"
className={`h-7 gap-1 ${
feedback === 'not_helpful' ? 'bg-red-100 text-red-600 dark:bg-red-900/20' : ''
}`}
onClick={() => handleFeedbackClick('not_helpful')}
>
<ThumbsDown className="h-3 w-3" />
<span className="text-xs">Not helpful</span>
</Button>
</>
)}
</div>
{/* Feedback Area */}
{!isUser && showFeedbackArea && feedbackType && (
<div className="w-full mt-2 bg-gray-50 dark:bg-gray-800/50 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
<div className="flex items-start justify-between mb-4">
<h4 className="text-sm font-medium text-gray-900 dark:text-gray-100">Tell us more:</h4>
<Button variant="ghost" size="sm" className="h-6 w-6 p-0" onClick={handleFeedbackClose}>
<X className="h-4 w-4" />
</Button>
</div>
{/* Tags */}
<div className="flex flex-wrap gap-2 mb-4">
{FEEDBACK_TAGS[feedbackType].map((tag) => (
<Button
key={tag}
variant={selectedTags.includes(tag) ? 'default' : 'outline'}
size="sm"
className="h-7 text-xs"
onClick={() => handleTagToggle(tag)}
>
{tag}
</Button>
))}
</div>
{/* Comment box */}
<Textarea
className="min-h-[60px] mb-4 bg-white dark:bg-gray-900"
value={feedbackText}
onChange={(e) => setFeedbackText(e.target.value)}
placeholder="Additional feedback (optional)..."
/>
{/* Submit */}
<div className="flex justify-end gap-2">
<Button variant="outline" size="sm" onClick={handleFeedbackClose} disabled={submitting}>
Cancel
</Button>
<Button
size="sm"
onClick={handleFeedbackSubmit}
disabled={submitting || (selectedTags.length === 0 && !feedbackText.trim())}
>
{submitting ? 'Submitting…' : 'Submit'}
</Button>
</div>
</div>
)}
</div>
{isUser && !showSenderInfo && (
<div className="w-8 h-8 rounded-full bg-muted flex items-center justify-center flex-shrink-0">
<span className="text-sm">👤</span>
</div>
)}
</div>
);
}
|