Spaces:
Runtime error
Runtime error
Commit
·
b59ef58
1
Parent(s):
ff1e468
better trace
Browse files
src/components/game-component.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
-
import { useState, useEffect, useCallback } from "react";
|
| 4 |
import { Card } from "@/components/ui/card";
|
| 5 |
import { Button } from "@/components/ui/button";
|
| 6 |
-
import { Flag, Clock, Hash, ArrowRight, Bot } from "lucide-react";
|
| 7 |
import { useInference } from "@/lib/inference";
|
| 8 |
|
| 9 |
import { API_BASE } from "@/lib/constants";
|
|
@@ -74,9 +74,11 @@ export default function GameComponent({
|
|
| 74 |
"playing"
|
| 75 |
);
|
| 76 |
|
| 77 |
-
const [convo, setConvo] = useState([]);
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
const { status: modelStatus, partialText,
|
| 80 |
apiKey:
|
| 81 |
window.localStorage.getItem("huggingface_access_token") || undefined,
|
| 82 |
});
|
|
@@ -198,8 +200,23 @@ export default function GameComponent({
|
|
| 198 |
setConvo((prev) => [...prev, message]);
|
| 199 |
};
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
return (
|
| 202 |
-
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
| 203 |
<Card className="p-4 flex col-span-2">
|
| 204 |
<h2 className="text-xl font-bold">Game Status</h2>
|
| 205 |
<div className="grid grid-cols-4 gap-4 mb-4">
|
|
@@ -245,7 +262,7 @@ export default function GameComponent({
|
|
| 245 |
)}
|
| 246 |
</Card>
|
| 247 |
{/* Left pane - Current page and available links */}
|
| 248 |
-
<Card className="p-4 flex flex-col">
|
| 249 |
<h2 className="text-xl font-bold">Available Links</h2>
|
| 250 |
<div className="flex justify-between items-center mb-4">
|
| 251 |
{gameStatus !== "playing" && (
|
|
@@ -286,13 +303,6 @@ export default function GameComponent({
|
|
| 286 |
</>
|
| 287 |
)}
|
| 288 |
|
| 289 |
-
{player === "model" && modelStatus === "thinking" && gameStatus === "playing" && (
|
| 290 |
-
<div className="flex items-center gap-2 text-sm animate-pulse mb-4">
|
| 291 |
-
<Bot className="h-4 w-4" />
|
| 292 |
-
<span>{model} is thinking...</span>
|
| 293 |
-
</div>
|
| 294 |
-
)}
|
| 295 |
-
|
| 296 |
{gameStatus === "playing" && player === "me" && (
|
| 297 |
<Button
|
| 298 |
onClick={handleGiveUp}
|
|
@@ -326,24 +336,74 @@ export default function GameComponent({
|
|
| 326 |
)}
|
| 327 |
</Card>
|
| 328 |
|
| 329 |
-
<Card className="p-4 flex flex-col
|
| 330 |
-
<h2 className="text-xl font-bold">LLM Reasoning</h2>
|
| 331 |
-
|
| 332 |
-
convo.map((message, index) =>
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
</div>
|
| 338 |
-
)
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
{ modelStatus === "thinking" && (
|
| 342 |
-
<div className="flex items-center gap-2 text-sm animate-pulse mb-4">
|
| 343 |
-
<Bot className="h-4 w-4" />
|
| 344 |
-
<p>{partialText}</p>
|
| 345 |
-
</div>
|
| 346 |
-
)}
|
| 347 |
</Card>
|
| 348 |
|
| 349 |
{/* <Card className="p-4 flex flex-col max-h-[500px] overflow-y-auto">
|
|
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
+
import { useState, useEffect, useCallback, useRef } from "react";
|
| 4 |
import { Card } from "@/components/ui/card";
|
| 5 |
import { Button } from "@/components/ui/button";
|
| 6 |
+
import { Flag, Clock, Hash, ArrowRight, Bot, User, ChevronDown, ChevronUp } from "lucide-react";
|
| 7 |
import { useInference } from "@/lib/inference";
|
| 8 |
|
| 9 |
import { API_BASE } from "@/lib/constants";
|
|
|
|
| 74 |
"playing"
|
| 75 |
);
|
| 76 |
|
| 77 |
+
const [convo, setConvo] = useState<Message[]>([]);
|
| 78 |
+
const [expandedMessages, setExpandedMessages] = useState<Record<number, boolean>>({});
|
| 79 |
+
const messagesEndRef = useRef<HTMLDivElement>(null);
|
| 80 |
|
| 81 |
+
const { status: modelStatus, partialText, inference } = useInference({
|
| 82 |
apiKey:
|
| 83 |
window.localStorage.getItem("huggingface_access_token") || undefined,
|
| 84 |
});
|
|
|
|
| 200 |
setConvo((prev) => [...prev, message]);
|
| 201 |
};
|
| 202 |
|
| 203 |
+
const scrollToBottom = () => {
|
| 204 |
+
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
| 205 |
+
};
|
| 206 |
+
|
| 207 |
+
useEffect(() => {
|
| 208 |
+
scrollToBottom();
|
| 209 |
+
}, [convo, partialText]);
|
| 210 |
+
|
| 211 |
+
const toggleMessageExpand = (index: number) => {
|
| 212 |
+
setExpandedMessages(prev => ({
|
| 213 |
+
...prev,
|
| 214 |
+
[index]: !prev[index]
|
| 215 |
+
}));
|
| 216 |
+
};
|
| 217 |
+
|
| 218 |
return (
|
| 219 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 h-[calc(100vh-200px)]">
|
| 220 |
<Card className="p-4 flex col-span-2">
|
| 221 |
<h2 className="text-xl font-bold">Game Status</h2>
|
| 222 |
<div className="grid grid-cols-4 gap-4 mb-4">
|
|
|
|
| 262 |
)}
|
| 263 |
</Card>
|
| 264 |
{/* Left pane - Current page and available links */}
|
| 265 |
+
<Card className="p-4 flex flex-col h-full overflow-hidden">
|
| 266 |
<h2 className="text-xl font-bold">Available Links</h2>
|
| 267 |
<div className="flex justify-between items-center mb-4">
|
| 268 |
{gameStatus !== "playing" && (
|
|
|
|
| 303 |
</>
|
| 304 |
)}
|
| 305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
{gameStatus === "playing" && player === "me" && (
|
| 307 |
<Button
|
| 308 |
onClick={handleGiveUp}
|
|
|
|
| 336 |
)}
|
| 337 |
</Card>
|
| 338 |
|
| 339 |
+
<Card className="p-4 flex flex-col h-full overflow-hidden">
|
| 340 |
+
<h2 className="text-xl font-bold mb-4">LLM Reasoning</h2>
|
| 341 |
+
<div className="flex-1 overflow-y-auto space-y-4 pr-2">
|
| 342 |
+
{convo.map((message, index) => {
|
| 343 |
+
const isExpanded = expandedMessages[index] || false;
|
| 344 |
+
const isLongUserMessage = message.role === "user" && message.content.length > 300;
|
| 345 |
+
const shouldTruncate = isLongUserMessage && !isExpanded;
|
| 346 |
+
|
| 347 |
+
return (
|
| 348 |
+
<div
|
| 349 |
+
key={index}
|
| 350 |
+
className={`p-3 rounded-lg ${
|
| 351 |
+
message.role === "assistant"
|
| 352 |
+
? "bg-blue-50 border border-blue-100"
|
| 353 |
+
: "bg-gray-50 border border-gray-100"
|
| 354 |
+
}`}
|
| 355 |
+
>
|
| 356 |
+
<div className="flex items-center gap-2 mb-1 text-sm font-medium text-muted-foreground">
|
| 357 |
+
{message.role === "assistant" ? (
|
| 358 |
+
<>
|
| 359 |
+
<Bot className="h-4 w-4" />
|
| 360 |
+
<span>Assistant</span>
|
| 361 |
+
</>
|
| 362 |
+
) : (
|
| 363 |
+
<>
|
| 364 |
+
<User className="h-4 w-4" />
|
| 365 |
+
<span>User</span>
|
| 366 |
+
</>
|
| 367 |
+
)}
|
| 368 |
+
</div>
|
| 369 |
+
|
| 370 |
+
<div>
|
| 371 |
+
<p className="whitespace-pre-wrap text-sm">
|
| 372 |
+
{shouldTruncate
|
| 373 |
+
? message.content.substring(0, 300) + "..."
|
| 374 |
+
: message.content
|
| 375 |
+
}
|
| 376 |
+
</p>
|
| 377 |
+
|
| 378 |
+
{isLongUserMessage && (
|
| 379 |
+
<Button
|
| 380 |
+
variant="ghost"
|
| 381 |
+
size="sm"
|
| 382 |
+
className="mt-1 h-6 text-xs flex items-center gap-1 text-muted-foreground hover:text-foreground"
|
| 383 |
+
onClick={() => toggleMessageExpand(index)}
|
| 384 |
+
>
|
| 385 |
+
{isExpanded
|
| 386 |
+
? <><ChevronUp className="h-3 w-3" /> Show less</>
|
| 387 |
+
: <><ChevronDown className="h-3 w-3" /> Show more</>
|
| 388 |
+
}
|
| 389 |
+
</Button>
|
| 390 |
+
)}
|
| 391 |
+
</div>
|
| 392 |
+
</div>
|
| 393 |
+
);
|
| 394 |
+
})}
|
| 395 |
+
|
| 396 |
+
{modelStatus === "thinking" && (
|
| 397 |
+
<div className="p-3 rounded-lg bg-blue-50 border border-blue-100">
|
| 398 |
+
<div className="flex items-center gap-2 mb-1 text-sm font-medium text-muted-foreground">
|
| 399 |
+
<Bot className="h-4 w-4" />
|
| 400 |
+
<span className="animate-pulse">Thinking...</span>
|
| 401 |
+
</div>
|
| 402 |
+
<p className="whitespace-pre-wrap text-sm">{partialText}</p>
|
| 403 |
</div>
|
| 404 |
+
)}
|
| 405 |
+
<div ref={messagesEndRef} />
|
| 406 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
</Card>
|
| 408 |
|
| 409 |
{/* <Card className="p-4 flex flex-col max-h-[500px] overflow-y-auto">
|