File size: 22,953 Bytes
6cdce85 |
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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
'use client';
import { useState, useRef, useEffect, useCallback } from 'react';
import { Sparkles, Send, Loader2, Trash2, ChevronLeft, Copy, Check, Play } from 'lucide-react';
import ReactMarkdown from 'react-markdown';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { clsx } from 'clsx';
import type { CodingProblem } from '@/types';
import { postProcessResponse } from '@/lib/utils/response';
import { LoadingStatus } from '../Chat/LoadingStatus';
interface AIHelperProps {
problem: CodingProblem | null;
userCode: string;
isCollapsed: boolean;
onToggleCollapse: () => void;
onApplyCode?: (code: string) => void;
}
interface HelperMessage {
id: string;
role: 'user' | 'assistant';
content: string;
timestamp: Date;
}
// Custom matte dark theme - matching Chat component
const customTheme: { [key: string]: React.CSSProperties } = {
'code[class*="language-"]': {
color: '#d4d4d8',
background: 'none',
fontFamily: "'JetBrains Mono', Consolas, Monaco, monospace",
fontSize: '0.8rem',
textAlign: 'left',
whiteSpace: 'pre',
wordSpacing: 'normal',
wordBreak: 'normal',
wordWrap: 'normal',
lineHeight: '1.5',
tabSize: 4,
},
'pre[class*="language-"]': {
color: '#d4d4d8',
background: '#18181b',
fontFamily: "'JetBrains Mono', Consolas, Monaco, monospace",
fontSize: '0.8rem',
textAlign: 'left',
whiteSpace: 'pre',
wordSpacing: 'normal',
wordBreak: 'normal',
wordWrap: 'normal',
lineHeight: '1.5',
tabSize: 4,
padding: '0.75rem',
margin: '0',
overflow: 'auto',
borderRadius: '0.375rem',
},
comment: { color: '#71717a' },
prolog: { color: '#71717a' },
doctype: { color: '#71717a' },
punctuation: { color: '#a1a1aa' },
property: { color: '#f0abfc' },
tag: { color: '#f0abfc' },
boolean: { color: '#c4b5fd' },
number: { color: '#c4b5fd' },
constant: { color: '#c4b5fd' },
symbol: { color: '#c4b5fd' },
selector: { color: '#86efac' },
string: { color: '#86efac' },
char: { color: '#86efac' },
builtin: { color: '#86efac' },
operator: { color: '#f0abfc' },
variable: { color: '#d4d4d8' },
function: { color: '#93c5fd' },
'class-name': { color: '#93c5fd' },
keyword: { color: '#c4b5fd' },
regex: { color: '#fcd34d' },
important: { color: '#fcd34d', fontWeight: 'bold' },
};
const HELPER_PROMPT = `You are a helpful coding assistant for quantum computing practice problems using Qiskit.
Your role is to:
1. Provide hints and guidance without giving away the complete solution
2. Explain quantum computing concepts when asked
3. Help debug code issues
4. Suggest improvements to the user's approach
Guidelines:
- Be encouraging and educational
- Give progressively more detailed hints if the user is stuck
- Focus on teaching, not just solving
- Reference Qiskit 2.0 best practices
- Keep responses concise and focused
Current problem context will be provided. Help the user learn while they solve the problem themselves.`;
function getSolvePrompt(problemType: 'function_completion' | 'code_generation') {
if (problemType === 'function_completion') {
return `You are a quantum computing expert using Qiskit.
Your task is to provide ONLY the code lines that complete the function body. Do NOT include the function signature/definition - just the implementation lines that go inside the function.
Guidelines:
- Provide ONLY the implementation code (the lines after the function definition)
- Do NOT repeat the function signature like "def function_name(...):"
- Include proper indentation for the function body
- Use Qiskit 2.0 best practices
- Add brief comments for complex steps
Example: If the function is:
\`\`\`python
def create_bell_state():
"""Create a Bell state circuit."""
pass
\`\`\`
You should respond with ONLY:
\`\`\`python
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
return qc
\`\`\`
Format your response with ONLY the implementation code in a Python code block.`;
}
return `You are a quantum computing expert using Qiskit.
Your task is to provide a complete, working solution for the given problem.
Guidelines:
- Provide a complete, executable Python solution
- Include all necessary imports
- Use Qiskit 2.0 best practices
- Include brief comments explaining key steps
- Make sure the solution passes the provided tests
Format your response with the complete code in a Python code block.`;
}
function looksLikeCode(text: string): boolean {
const codeIndicators = [
/^from\s+/m,
/^import\s+/m,
/^def\s+/m,
/^class\s+/m,
/^\s*return\s+/m,
/QuantumCircuit/,
/Parameter\(/,
/\.\w+\([^)]*\)/m,
/^\s{4}/m, // Indented code
/qc\.\w+/,
/circuit\.\w+/,
];
return codeIndicators.some((p) => p.test(text));
}
interface CodeBlockProps {
code: string;
language: string;
onCopy: () => void;
onApply?: () => void;
copied: boolean;
}
function CodeBlock({ code, language, onCopy, onApply, copied }: CodeBlockProps) {
return (
<div className="relative group my-2">
{/* Action buttons - always visible for better discoverability */}
<div className="absolute right-2 top-2 flex items-center gap-1.5 z-10">
<span className="text-[10px] text-zinc-500 bg-zinc-900/80 px-1.5 py-0.5 rounded">
{language || 'python'}
</span>
<button
onClick={onCopy}
className="p-1 rounded bg-zinc-800/90 hover:bg-zinc-700 transition-colors"
title="Copy code"
>
{copied ? (
<Check className="w-3 h-3 text-emerald-400" />
) : (
<Copy className="w-3 h-3 text-zinc-400" />
)}
</button>
{onApply && (
<button
onClick={onApply}
className="flex items-center gap-1 px-1.5 py-1 rounded bg-teal-700/80 hover:bg-teal-600 text-teal-100 transition-colors text-[10px] font-medium"
title="Apply code to editor"
>
<Play className="w-3 h-3" />
Apply
</button>
)}
</div>
<SyntaxHighlighter
style={customTheme}
language={language || 'python'}
PreTag="div"
customStyle={{
margin: 0,
borderRadius: '0.375rem',
background: '#18181b',
padding: '0.75rem',
paddingTop: '2rem', // Space for buttons
fontSize: '0.8rem',
border: '1px solid #27272a',
lineHeight: '1.5',
}}
codeTagProps={{
style: {
background: 'none',
padding: 0,
},
}}
wrapLongLines={false}
>
{code}
</SyntaxHighlighter>
</div>
);
}
export function AIHelper({
problem,
userCode,
isCollapsed,
onToggleCollapse,
onApplyCode,
}: AIHelperProps) {
const [messages, setMessages] = useState<HelperMessage[]>([]);
const [input, setInput] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [hasStartedStreaming, setHasStartedStreaming] = useState(false);
const [copiedId, setCopiedId] = useState<string | null>(null);
const abortControllerRef = useRef<AbortController | null>(null);
const messagesContainerRef = useRef<HTMLDivElement>(null);
const scrollToBottom = useCallback(() => {
// Scroll only within the messages container, not the whole page
if (messagesContainerRef.current) {
messagesContainerRef.current.scrollTop = messagesContainerRef.current.scrollHeight;
}
}, []);
useEffect(() => {
// Only scroll within the AI Helper panel, not the whole page
requestAnimationFrame(() => {
scrollToBottom();
});
}, [messages, scrollToBottom]);
useEffect(() => {
setMessages([]);
}, [problem?.id]);
// Fetch image as base64 for multimodal problems
const fetchImageBase64 = async (imageUrl: string): Promise<string | null> => {
try {
const response = await fetch(imageUrl);
const blob = await response.blob();
return new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => {
const base64 = reader.result as string;
const base64Data = base64.split(',')[1] || base64;
resolve(base64Data);
};
reader.onerror = () => resolve(null);
reader.readAsDataURL(blob);
});
} catch {
return null;
}
};
const handleSendMessage = async (customMessage?: string, isSolveRequest = false) => {
const messageText = customMessage || input.trim();
if (!messageText || isLoading || !problem) return;
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
abortControllerRef.current = new AbortController();
const userMessage: HelperMessage = {
id: crypto.randomUUID(),
role: 'user',
content: messageText,
timestamp: new Date(),
};
const assistantId = crypto.randomUUID();
const loadingMessage: HelperMessage = {
id: assistantId,
role: 'assistant',
content: '',
timestamp: new Date(),
};
setMessages((prev) => [...prev, userMessage, loadingMessage]);
setInput('');
setIsLoading(true);
setHasStartedStreaming(false);
try {
// Build context message with problem info
let contextMessage = `Problem: ${problem.question}`;
if (!isSolveRequest && userCode) {
contextMessage += `\n\nUser's current code:\n\`\`\`python\n${userCode || '# No code written yet'}\n\`\`\``;
}
contextMessage += `\n\nUser's request: ${messageText}`;
// Select appropriate system prompt
const systemPrompt = isSolveRequest
? getSolvePrompt(problem.type as 'function_completion' | 'code_generation')
: HELPER_PROMPT;
// Build messages array
const apiMessages: Array<{
role: 'system' | 'user' | 'assistant';
content: string | Array<{ type: string; text?: string; image_url?: { url: string } }>;
}> = [
{ role: 'system', content: systemPrompt },
...messages.map((m) => ({
role: m.role as 'user' | 'assistant',
content: m.content,
})),
];
// Handle multimodal problems - include image if available
if (problem.imageUrl && problem.hasImage) {
const imageBase64 = await fetchImageBase64(problem.imageUrl);
if (imageBase64) {
apiMessages.push({
role: 'user',
content: [
{ type: 'text', text: contextMessage },
{
type: 'image_url',
image_url: { url: `data:image/jpeg;base64,${imageBase64}` },
},
],
});
} else {
apiMessages.push({ role: 'user', content: contextMessage });
}
} else {
apiMessages.push({ role: 'user', content: contextMessage });
}
const response = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: apiMessages,
stream: true,
}),
signal: abortControllerRef.current.signal,
});
if (!response.ok) {
const data = await response.json();
throw new Error(data.error || 'Request failed');
}
const reader = response.body?.getReader();
if (!reader) throw new Error('No response body');
const decoder = new TextDecoder();
let buffer = '';
let fullContent = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || !trimmed.startsWith('data: ')) continue;
const jsonStr = trimmed.slice(6);
try {
const data = JSON.parse(jsonStr);
if (data.content) {
// First content received - streaming has started
if (fullContent === '') {
setHasStartedStreaming(true);
}
fullContent += data.content;
// Use postProcessResponse like ChatInterface does for proper formatting
const processedContent = postProcessResponse(fullContent);
setMessages((prev) =>
prev.map((m) =>
m.id === assistantId ? { ...m, content: processedContent } : m
)
);
}
} catch {
continue;
}
}
}
// Apply final post-processing
const finalContent = postProcessResponse(fullContent);
setMessages((prev) =>
prev.map((m) =>
m.id === assistantId ? { ...m, content: finalContent } : m
)
);
} catch (error) {
if ((error as Error).name === 'AbortError') return;
setMessages((prev) =>
prev.map((m) =>
m.id === assistantId
? { ...m, content: `Error: ${error instanceof Error ? error.message : 'Failed'}` }
: m
)
);
} finally {
setIsLoading(false);
setHasStartedStreaming(false);
abortControllerRef.current = null;
}
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSendMessage();
}
};
const clearMessages = () => {
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
setMessages([]);
};
// Extract code blocks from message content, preserving indentation
const extractCodeBlocks = (content: string): string[] => {
const codeBlockRegex = /```(?:python)?\n?([\s\S]*?)```/g;
const blocks: string[] = [];
let match;
while ((match = codeBlockRegex.exec(content)) !== null) {
// Preserve indentation - only trim trailing newlines, not leading whitespace
const code = match[1].replace(/\n+$/, '');
blocks.push(code);
}
return blocks;
};
const handleCopyCode = (code: string, messageId: string) => {
navigator.clipboard.writeText(code);
setCopiedId(messageId);
setTimeout(() => setCopiedId(null), 2000);
};
const handleApplyCode = (code: string) => {
if (onApplyCode) {
onApplyCode(code);
}
};
// Process content to add code blocks where needed
const processContent = useCallback((content: string): string => {
// If already has code blocks, return as-is
if (content.includes('```')) {
return content;
}
// If it looks like code, wrap it
if (looksLikeCode(content)) {
return '```python\n' + content + '\n```';
}
return content;
}, []);
// Collapsed view
if (isCollapsed) {
return (
<button
onClick={onToggleCollapse}
className="h-full w-full flex flex-col items-center justify-center gap-2 bg-zinc-900/95 border-l border-zinc-800/80 hover:bg-zinc-800/50 transition-colors cursor-pointer"
title="Expand AI Helper"
>
<Sparkles className="w-5 h-5 text-teal-500" />
<span className="text-xs text-zinc-500 [writing-mode:vertical-lr] rotate-180 font-medium">
AI Helper
</span>
</button>
);
}
// Expanded view
return (
<div className="h-full flex flex-col bg-zinc-900/95 border-l border-zinc-800/80">
<div className="flex items-center justify-between px-4 py-3 border-b border-zinc-800/80 flex-shrink-0">
<div className="flex items-center gap-2">
<Sparkles className="w-4 h-4 text-teal-500" />
<h3 className="font-semibold text-zinc-200 text-sm">AI Helper</h3>
</div>
<div className="flex items-center gap-1">
{messages.length > 0 && (
<button
onClick={clearMessages}
className="p-1.5 rounded-md hover:bg-zinc-800/50 transition-colors"
title="Clear chat"
>
<Trash2 className="w-3.5 h-3.5 text-zinc-500" />
</button>
)}
<button
onClick={onToggleCollapse}
className="p-1.5 rounded-md hover:bg-zinc-800/50 transition-colors"
title="Collapse"
>
<ChevronLeft className="w-4 h-4 text-zinc-500 rotate-180" />
</button>
</div>
</div>
<div ref={messagesContainerRef} className="flex-1 overflow-y-auto p-3 space-y-3 min-h-0">
{messages.length === 0 ? (
<div className="flex flex-col items-center justify-center h-full text-center px-4">
<Sparkles className="w-8 h-8 text-teal-500/50 mb-3" />
<p className="text-sm text-zinc-500 mb-4">
Need help? Ask for hints or get the solution.
</p>
{problem && (
<div className="space-y-2 w-full">
{[
{ label: 'Give me a hint', isSolve: false },
{ label: 'Explain the concept', isSolve: false },
{ label: 'Solve it', isSolve: true },
].map(({ label, isSolve }) => (
<button
key={label}
onClick={() => handleSendMessage(label, isSolve)}
className={clsx(
'w-full text-left px-3 py-2 rounded-md text-xs transition-colors',
isSolve
? 'bg-teal-900/40 hover:bg-teal-800/50 text-teal-300 hover:text-teal-200 border border-teal-700/30'
: 'bg-zinc-800/60 hover:bg-zinc-800 text-zinc-400 hover:text-zinc-200'
)}
>
{label}
</button>
))}
</div>
)}
</div>
) : (
messages.map((message) => {
const processedContent = processContent(message.content);
const codeBlocks = extractCodeBlocks(processedContent);
const hasCode = codeBlocks.length > 0;
return (
<div
key={message.id}
className={clsx(
'flex flex-col',
message.role === 'user' ? 'items-end' : 'items-start'
)}
>
<div
className={clsx(
'max-w-[95%] rounded-lg px-3 py-2 text-sm',
message.role === 'user'
? 'bg-teal-700/60 text-white'
: 'bg-zinc-800/80 text-zinc-300'
)}
>
{message.role === 'assistant' && !message.content ? (
<LoadingStatus
isLoading={isLoading}
hasStartedStreaming={hasStartedStreaming}
/>
) : (
<ReactMarkdown
components={{
code({ className, children, ...props }) {
const match = /language-(\w+)/.exec(className || '');
const code = String(children).replace(/\n$/, '');
const isBlock = match || code.includes('\n') || looksLikeCode(code);
if (isBlock) {
return (
<CodeBlock
code={code}
language={match?.[1] || 'python'}
onCopy={() => handleCopyCode(code, message.id)}
onApply={onApplyCode ? () => handleApplyCode(code) : undefined}
copied={copiedId === message.id}
/>
);
}
return (
<code className="bg-zinc-700/50 px-1 py-0.5 rounded text-xs" {...props}>
{children}
</code>
);
},
pre({ children }) {
return <>{children}</>;
},
p({ children }) {
return <p className="mb-2 last:mb-0">{children}</p>;
},
ul({ children }) {
return <ul className="list-disc ml-4 mb-2 space-y-1">{children}</ul>;
},
ol({ children }) {
return <ol className="list-decimal ml-4 mb-2 space-y-1">{children}</ol>;
},
li({ children }) {
return <li className="text-zinc-300">{children}</li>;
},
strong({ children }) {
return <strong className="font-semibold text-zinc-200">{children}</strong>;
},
}}
>
{processedContent}
</ReactMarkdown>
)}
</div>
</div>
);
})
)}
</div>
{problem && (
<div className="p-3 border-t border-zinc-800/80 flex-shrink-0">
<div className="flex items-end gap-2">
<textarea
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Ask for help..."
disabled={isLoading}
rows={1}
className="flex-1 bg-zinc-800/60 border border-zinc-700/50 rounded-lg px-3 py-2 text-sm text-zinc-200 placeholder:text-zinc-500 resize-none focus:outline-none focus:ring-1 focus:ring-teal-600/50 min-h-[40px] max-h-[100px]"
style={{ height: 'auto' }}
onInput={(e) => {
const target = e.target as HTMLTextAreaElement;
target.style.height = 'auto';
target.style.height = `${Math.min(target.scrollHeight, 100)}px`;
}}
/>
<button
onClick={() => handleSendMessage()}
disabled={!input.trim() || isLoading}
className={clsx(
'p-2 rounded-lg transition-all',
input.trim() && !isLoading
? 'bg-teal-600 hover:bg-teal-500 text-white'
: 'bg-zinc-800 text-zinc-500'
)}
>
{isLoading ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Send className="w-4 h-4" />
)}
</button>
</div>
</div>
)}
</div>
);
}
|