File size: 29,502 Bytes
8de92d1
 
8565da5
8de92d1
 
8565da5
 
 
 
 
 
 
 
 
 
 
ff1e468
5b7f9a5
 
 
 
 
 
 
 
ff1e468
27bba9c
8565da5
 
5b7f9a5
8565da5
 
 
 
 
 
 
5b7f9a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff1e468
 
1a43738
ff1e468
e40b300
 
 
 
 
 
ff1e468
 
8de92d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8565da5
 
b59ef58
8565da5
e40b300
 
b59ef58
8de92d1
8565da5
 
 
1a43738
8565da5
ff1e468
 
 
8de92d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff1e468
8de92d1
 
 
 
 
 
 
8565da5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8de92d1
 
 
 
 
 
 
 
ff1e468
 
 
 
 
1a43738
8de92d1
 
 
 
 
1a43738
 
 
 
 
 
 
 
 
 
ff1e468
 
 
 
 
 
 
 
8de92d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40b300
 
 
 
 
 
 
 
 
 
8de92d1
 
 
 
 
 
 
ff1e468
8de92d1
 
 
 
 
 
 
 
 
 
 
 
ff1e468
 
 
 
b59ef58
 
 
 
 
 
 
 
e40b300
8565da5
b59ef58
8565da5
b59ef58
 
 
5b7f9a5
 
8565da5
 
 
 
 
 
 
 
5b7f9a5
 
 
8565da5
5b7f9a5
 
8565da5
 
 
 
 
 
 
 
 
8de92d1
e40b300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7f9a5
8565da5
5b7f9a5
 
 
 
 
 
 
8de92d1
5b7f9a5
 
 
8de92d1
5b7f9a5
 
 
 
 
 
 
 
 
 
 
8de92d1
5b7f9a5
 
 
 
8de92d1
 
 
 
5b7f9a5
 
 
 
 
957e7cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7f9a5
 
 
 
 
957e7cc
 
 
 
8565da5
 
 
 
 
5b7f9a5
 
 
 
 
 
 
 
 
8de92d1
5b7f9a5
 
8de92d1
5b7f9a5
8de92d1
5b7f9a5
8de92d1
5b7f9a5
 
 
8de92d1
5b7f9a5
8de92d1
5b7f9a5
 
 
 
8de92d1
5b7f9a5
8de92d1
 
 
5b7f9a5
 
8de92d1
5b7f9a5
8565da5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad056e1
8565da5
 
 
 
 
 
 
ad056e1
8565da5
5b7f9a5
8565da5
 
 
 
 
 
 
8b6f89e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8565da5
 
 
 
 
ad056e1
8565da5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27bba9c
8565da5
5b7f9a5
8565da5
5b7f9a5
 
 
 
e40b300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7f9a5
e40b300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7f9a5
 
 
e40b300
5b7f9a5
 
e40b300
 
5b7f9a5
e40b300
5b7f9a5
 
e40b300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7f9a5
e40b300
 
 
 
 
 
 
 
 
8b6f89e
e40b300
 
 
8b6f89e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40b300
 
1a43738
 
 
 
 
 
e40b300
 
 
5b7f9a5
 
 
 
 
 
 
b59ef58
5b7f9a5
b59ef58
5b7f9a5
e40b300
5b7f9a5
 
 
 
 
8565da5
 
 
 
5b7f9a5
8565da5
 
 
 
 
 
 
 
 
5b7f9a5
 
 
 
8565da5
5b7f9a5
8565da5
 
 
 
 
 
 
8de92d1
 
 
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
"use client";

import { useState, useEffect, useCallback, useRef, useMemo } from "react";
import { Card } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import {
  Flag,
  Clock,
  Hash,
  ArrowRight,
  Bot,
  User,
  ChevronDown,
  ChevronUp,
  Info,
} from "lucide-react";
import { useInference } from "@/lib/inference";
import { Label } from "@/components/ui/label";
import { cn } from "@/lib/utils";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";

import { API_BASE } from "@/lib/constants";
import ForceDirectedGraph from "./force-directed-graph";
import qwen3Data from "../../results/qwen3.json"
// Simple Switch component since it's not available in the UI components
const Switch = ({
  checked,
  onCheckedChange,
  disabled,
  id,
}: {
  checked: boolean;
  onCheckedChange: (checked: boolean) => void;
  disabled?: boolean;
  id?: string;
}) => {
  return (
    <button
      id={id}
      type="button"
      role="switch"
      aria-checked={checked}
      data-state={checked ? "checked" : "unchecked"}
      disabled={disabled}
      onClick={() => onCheckedChange(!checked)}
      className={cn(
        "focus-visible:ring-ring/50 peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors focus-visible:outline-none focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-50",
        checked ? "bg-primary" : "bg-input"
      )}
    >
      <span
        data-state={checked ? "checked" : "unchecked"}
        className={cn(
          "pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform",
          checked ? "translate-x-4" : "translate-x-0"
        )}
      />
    </button>
  );
};

type Message = {
  role: "user" | "assistant" | "game" | "result" | "error";
  content: string;
  metadata?: {
    page?: string;
    links?: string[];
    status?: "playing" | "won" | "lost";
    path?: string[];
  };
};

const buildPrompt = (
  current: string,
  target: string,
  path_so_far: string[],
  links: string[]
) => {
  const formatted_links = links
    .map((link, index) => `${index + 1}. ${link}`)
    .join("\n");
  const path_so_far_str = path_so_far.join(" -> ");

  return `You are playing WikiRun, trying to navigate from one Wikipedia article to another using only links.

IMPORTANT: You MUST put your final answer in <answer>NUMBER</answer> tags, where NUMBER is the link number.
For example, if you want to choose link 3, output <answer>3</answer>.

Current article: ${current}
Target article: ${target}
You have ${links.length} link(s) to choose from:
${formatted_links}

Your path so far: ${path_so_far_str}

Think about which link is most likely to lead you toward the target article.
First, analyze each link briefly and how it connects to your goal, then select the most promising one.

Remember to format your final answer by explicitly writing out the xml number tags like this: <answer>NUMBER</answer>`;
};

interface GameComponentProps {
  player: "me" | "model";
  model?: string;
  maxHops: number;
  startPage: string;
  targetPage: string;
  onReset: () => void;
  maxTokens: number;
  maxLinks: number;
}

export default function GameComponent({
  player,
  model,
  maxHops,
  startPage,
  targetPage,
  onReset,
  maxTokens,
  maxLinks,
}: GameComponentProps) {
  const [currentPage, setCurrentPage] = useState<string>(startPage);
  const [currentPageLinks, setCurrentPageLinks] = useState<string[]>([]);
  const [linksLoading, setLinksLoading] = useState<boolean>(false);
  const [hops, setHops] = useState<number>(0);
  const [timeElapsed, setTimeElapsed] = useState<number>(0);
  const [visitedNodes, setVisitedNodes] = useState<string[]>([startPage]);
  const [gameStatus, setGameStatus] = useState<"playing" | "won" | "lost">(
    "playing"
  );
  const [continuousPlay, setContinuousPlay] = useState<boolean>(true);
  const [autoRunning, setAutoRunning] = useState<boolean>(true);
  const [convo, setConvo] = useState<Message[]>([]);
  const [expandedMessages, setExpandedMessages] = useState<
    Record<number | string, boolean>
  >({ game: false });
  const messagesEndRef = useRef<HTMLDivElement>(null);

  const {
    status: modelStatus,
    partialText,
    inference
  } = useInference({
    apiKey:
      window.localStorage.getItem("huggingface_access_token") || undefined,
  });

  const fetchCurrentPageLinks = useCallback(async () => {
    setLinksLoading(true);
    const response = await fetch(
      `${API_BASE}/get_article_with_links/${currentPage}`
    );
    const data = await response.json();
    setCurrentPageLinks(data.links.slice(0, maxLinks));
    setLinksLoading(false);
  }, [currentPage, maxLinks]);

  useEffect(() => {
    fetchCurrentPageLinks();
  }, [fetchCurrentPageLinks]);

  useEffect(() => {
    if (gameStatus === "playing") {
      const timer = setInterval(() => {
        setTimeElapsed((prev) => prev + 1);
      }, 1000);

      return () => clearInterval(timer);
    }
  }, [gameStatus]);

  // Check win condition
  useEffect(() => {
    if (currentPage === targetPage) {
      setGameStatus("won");
    } else if (hops >= maxHops) {
      setGameStatus("lost");
    }
  }, [currentPage, targetPage, hops, maxHops]);

  const handleLinkClick = (link: string) => {
    if (gameStatus !== "playing") return;

    setCurrentPage(link);
    setHops((prev) => prev + 1);
    setVisitedNodes((prev) => [...prev, link]);
  };

  const currentRuns = useMemo(() => {
    const q3runs = qwen3Data.runs.filter((run) => run.result === "win");

    if (visitedNodes.length === 0) {
      return q3runs;
    }

    return [
      {
        steps: [
          {
            type: "start",
            article: startPage,
          },
          ...visitedNodes.map((node) => ({ type: "move", article: node })),
        ],
        start_article: startPage,
        destination_article: targetPage,
      },
      ...q3runs,
    ];
  }, [visitedNodes, startPage, targetPage]);

  const makeModelMove = async () => {
    const prompt = buildPrompt(
      currentPage,
      targetPage,
      visitedNodes,
      currentPageLinks
    );

    pushConvo({
      role: "user",
      content: prompt,
    });

    const {status, result: modelResponse} = await inference({
      model: model,
      prompt,
      maxTokens: maxTokens,
    });

    if (status === "error") {
      pushConvo({
        role: "error",
        content: "Error during inference: " + modelResponse,
      });

      setAutoRunning(false);
      return;
    }

    pushConvo({
      role: "assistant",
      content: modelResponse,
    });

    console.log("Model response", modelResponse);

    const answer = modelResponse.match(/<answer>(.*?)<\/answer>/)?.[1];
    if (!answer) {
      console.error("No answer found in model response");
      return;
    }

    // try parsing the answer as an integer
    const answerInt = parseInt(answer);
    if (isNaN(answerInt)) {
      console.error("Invalid answer found in model response");
      return;
    }

    if (answerInt < 1 || answerInt > currentPageLinks.length) {
      console.error(
        "Selected link out of bounds",
        answerInt,
        "from ",
        currentPageLinks.length,
        "links"
      );
      return;
    }

    const selectedLink = currentPageLinks[answerInt - 1];

    // Add a game status message after each move
    pushConvo({
      role: "game",
      content: `Model selected link ${answerInt}: ${selectedLink}`,
      metadata: {
        page: currentPage,
        links: [...currentPageLinks],
      },
    });

    console.log(
      "Model picked selectedLink",
      selectedLink,
      "from ",
      currentPageLinks
    );

    handleLinkClick(selectedLink);
  };

  const handleGiveUp = () => {
    setGameStatus("lost");
  };

  const formatTime = (seconds: number) => {
    const mins = Math.floor(seconds / 60);
    const secs = seconds % 60;
    return `${mins}:${secs < 10 ? "0" : ""}${secs}`;
  };

  const pushConvo = (message: Message) => {
    setConvo((prev) => [...prev, message]);
  };

  const scrollToBottom = () => {
    messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
  };

  useEffect(() => {
    scrollToBottom();
  }, [convo, partialText]);

  const toggleMessageExpand = (index: number | string) => {
    setExpandedMessages((prev) => ({
      ...prev,
      [index]: !prev[index],
    }));
  };

  // Effect for continuous play mode
  useEffect(() => {
    if (
      continuousPlay &&
      autoRunning &&
      player === "model" &&
      gameStatus === "playing" &&
      modelStatus !== "thinking" &&
      !linksLoading
    ) {
      const timer = setTimeout(() => {
        makeModelMove();
      }, 1000);

      return () => clearTimeout(timer);
    }
  }, [
    continuousPlay,
    autoRunning,
    player,
    gameStatus,
    modelStatus,
    linksLoading,
    currentPage,
  ]);

  // Add a result message when the game ends
  useEffect(() => {
    if (gameStatus !== "playing" && convo.length > 0 && convo[convo.length - 1].role !== "result") {
      pushConvo({
        role: "result",
        content: gameStatus === "won" 
          ? `${model} successfully navigated from ${visitedNodes[0]} to ${targetPage} in ${hops} moves!`
          : `${model} failed to reach ${targetPage} within the ${maxHops} hop limit.`,
        metadata: {
          status: gameStatus,
          path: [...visitedNodes],
        },
      });
    }
  }, [gameStatus]);

  return (
    <div className="grid grid-cols-1 md:grid-cols-12 gap-2 h-[calc(100vh-200px)] grid-rows-[auto_1fr_1fr]">
      {/* Condensed Game Status Card */}
      <Card className="p-2 col-span-12 h-12 row-start-1">
        <div className="flex items-center justify-between h-full">
          <div className="flex items-center gap-4">
            <div className="flex items-center gap-1">
              <ArrowRight className="h-4 w-4 text-muted-foreground" />
              <span className="text-sm font-medium">{currentPage}</span>
            </div>
            <div className="flex items-center gap-1">
              <Flag className="h-4 w-4 text-muted-foreground" />
              <span className="text-sm font-medium">{targetPage}</span>
            </div>
            <div
              className="flex items-center gap-1 cursor-help relative group"
              title="Path history"
            >
              <Hash className="h-4 w-4 text-muted-foreground" />
              <span className="text-sm font-medium">
                {hops} / {maxHops}
              </span>
              <div className="invisible absolute bottom-full left-0 mb-2 p-2 bg-popover border rounded-md shadow-md text-xs max-w-[300px] z-50 group-hover:visible whitespace-pre-wrap">
                Path: {visitedNodes.join(" → ")}
              </div>
            </div>
            <div className="flex items-center gap-1">
              <Clock className="h-4 w-4 text-muted-foreground" />
              <span className="text-sm font-medium">
                {formatTime(timeElapsed)}
              </span>
            </div>
          </div>

          <div className="flex items-center gap-2">
            {gameStatus === "playing" && (
              <>
                {player === "model" && (
                  <>
                    {continuousPlay ? (
                      <Button
                        onClick={() => setAutoRunning(!autoRunning)}
                        size="sm"
                        className="h-8"
                      >
                        {autoRunning ? "Stop" : "Start"}
                      </Button>
                    ) : (
                      <Button
                        onClick={makeModelMove}
                        disabled={modelStatus === "thinking" || linksLoading}
                        size="sm"
                        className="h-8"
                      >
                        Next Move
                      </Button>
                    )}

                    <div className="flex items-center gap-1 ml-1">
                      <Switch
                        id="continuous-play"
                        checked={continuousPlay}
                        onCheckedChange={(checked) => {
                          setContinuousPlay(checked);
                          if (!checked) setAutoRunning(false);
                        }}
                        disabled={
                          modelStatus === "thinking" ||
                          linksLoading ||
                          (continuousPlay && autoRunning)
                        }
                      />
                      <Label htmlFor="continuous-play" className="text-xs">
                        Auto
                      </Label>
                    </div>
                  </>
                )}

                {player === "me" && (
                  <Button
                    onClick={handleGiveUp}
                    variant="destructive"
                    size="sm"
                    className="h-8"
                  >
                    Give Up
                  </Button>
                )}
              </>
            )}

            {gameStatus !== "playing" && (
              <Button
                onClick={onReset}
                variant="outline"
                size="sm"
                className="h-8"
              >
                New Game
              </Button>
            )}
          </div>
        </div>
      </Card>

      {/* Links panel - larger now */}
      {player === "me" && (
        <Card className="p-3 md:col-span-6 h-full overflow-hidden row-span-2 row-start-2">
          <h2 className="text-lg font-bold mb-2">
            Available Links
            <TooltipProvider>
              <Tooltip>
                <TooltipTrigger asChild>
                  <span className="ml-2 text-xs text-muted-foreground cursor-help inline-flex items-center">
                    <Info className="h-3 w-3 mr-1" />
                    Why are some links missing?
                  </span>
                </TooltipTrigger>
                <TooltipContent className="max-w-[300px] p-3">
                  <p>
                    We're playing on a pruned version of Simple Wikipedia so
                    that every path between articles is possible. See dataset
                    details{" "}
                    <a
                      href="https://huggingface.co/datasets/HuggingFaceTB/simplewiki-pruned-350k"
                      target="_blank"
                      rel="noopener noreferrer"
                      className="text-blue-600 underline hover:text-blue-800"
                    >
                      here
                    </a>
                    .
                  </p>
                </TooltipContent>
              </Tooltip>
            </TooltipProvider>
          </h2>

          {gameStatus === "playing" ? (
            <div className="flex flex-wrap content-start overflow-y-auto h-[calc(100%-2.5rem)]">
              {currentPageLinks
                .sort((a, b) => a.localeCompare(b))
                .map((link) => (
                  <Button
                    key={link}
                    variant="outline"
                    size="sm"
                    className="justify-start overflow-hidden text-ellipsis whitespace-nowrap w-[calc(33.333%-0.5rem)] m-[0.25rem]"
                    onClick={() => handleLinkClick(link)}
                  >
                    {link}
                  </Button>
                ))}
            </div>
          ) : (
            <div className="flex items-center justify-center h-[calc(100%-2.5rem)]">
              {gameStatus === "won" ? (
                <div className="bg-gradient-to-r from-green-50 to-emerald-50 border border-green-200 p-6 rounded-lg w-full shadow-sm">
                  <div className="flex flex-col items-center text-center">
                    <div className="mb-3 bg-green-100 p-3 rounded-full">
                      <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="text-green-600"><polyline points="20 6 9 17 4 12"></polyline></svg>
                    </div>
                    <h3 className="font-bold text-xl text-green-800 mb-2">
                      You won!
                    </h3>
                    <p className="text-green-700 mb-4">
                      You reached <span className="font-bold">{targetPage}</span> in <span className="font-bold">{hops}</span> {hops === 1 ? 'hop' : 'hops'} in {formatTime(timeElapsed)}
                    </p>
                    
                    <div className="bg-white rounded-md p-4 my-3 w-full max-w-md border border-green-100">
                      <h4 className="font-medium text-sm text-green-800 mb-2">Your Path:</h4>
                      <div className="flex flex-wrap items-center gap-2 justify-center text-sm">
                        {visitedNodes.map((node, index) => (
                          <div key={`path-${index}`} className="flex items-center">
                            <span className="bg-green-50 px-2 py-1 rounded border border-green-100 font-medium">{node}</span>
                            {index < visitedNodes.length - 1 && (
                              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="mx-1 text-green-400"><path d="M5 12h14"></path><path d="m12 5 7 7-7 7"></path></svg>
                            )}
                          </div>
                        ))}
                      </div>
                    </div>
                    
                    <div className="flex gap-2 mt-2">
                      <Button
                        onClick={onReset}
                        variant="outline"
                        size="sm"
                        className="bg-white"
                      >
                        New Game
                      </Button>
                    </div>
                  </div>
                </div>
              ) : (
                <div className="bg-red-100 text-red-800 p-4 rounded-md w-full">
                  <h3 className="font-bold">Game Over</h3>
                  <p>
                    You didn't reach {targetPage} within {maxHops} hops.
                  </p>
                  <Button
                    onClick={onReset}
                    variant="outline"
                    size="sm"
                    className="mt-2"
                  >
                    New Game
                  </Button>
                </div>
              )}
            </div>
          )}
        </Card>
      )}

      {/* Reasoning panel - spans full height on left side */}
      {player === "model" && (
        <Card className="p-3 md:col-span-6 h-full overflow-hidden row-span-2 row-start-2">
          <h2 className="text-lg font-bold mb-2">LLM Reasoning</h2>
          <div className="overflow-y-auto h-[calc(100%-2.5rem)] space-y-2 pr-2">
            {convo.map((message, index) => {
              const isExpanded = expandedMessages[index] || false;
              
              if (message.role === "user" || message.role === "assistant") {
                const isLongUserMessage =
                  message.role === "user" && message.content.length > 300;
                const shouldTruncate = isLongUserMessage && !isExpanded;
                
                return (
                  <div
                    key={`message-${index}`}
                    className={`p-2 rounded-lg text-xs ${
                      message.role === "assistant"
                        ? "bg-blue-50 border border-blue-100"
                        : "bg-gray-50 border border-gray-100"
                    }`}
                  >
                    <div className="flex items-center gap-1 mb-1 text-xs font-medium text-muted-foreground">
                      {message.role === "assistant" ? (
                        <>
                          <Bot className="h-3 w-3" />
                          <span>Assistant</span>
                        </>
                      ) : (
                        <>
                          <User className="h-3 w-3" />
                          <span>User</span>
                        </>
                      )}
                    </div>

                    <div>
                      <p className="whitespace-pre-wrap text-xs">
                        {shouldTruncate
                          ? message.content.substring(0, 300) + "..."
                          : message.content}
                      </p>

                      {isLongUserMessage && (
                        <Button
                          variant="ghost"
                          size="sm"
                          className="mt-1 h-5 text-xs flex items-center gap-1 text-muted-foreground hover:text-foreground"
                          onClick={() => toggleMessageExpand(index)}
                        >
                          {isExpanded ? (
                            <>
                              <ChevronUp className="h-3 w-3" /> Show less
                            </>
                          ) : (
                            <>
                              <ChevronDown className="h-3 w-3" /> Show more
                            </>
                          )}
                        </Button>
                      )}
                    </div>
                  </div>
                );
              } else if (message.role === "game") {
                // Game status block
                return (
                  <div 
                    key={`game-${index}`}
                    className="p-2 rounded-lg bg-yellow-50 border border-yellow-100 text-xs"
                  >
                    <div className="flex items-center justify-between mb-1">
                      <div className="flex items-center gap-1 text-xs font-medium text-muted-foreground">
                        <Info className="h-3 w-3" />
                        <span>Game Status</span>
                      </div>
                      <Button
                        variant="ghost"
                        size="sm"
                        className="h-5 text-xs flex items-center gap-1 text-muted-foreground hover:text-foreground p-0"
                        onClick={() => toggleMessageExpand(index)}
                      >
                        {expandedMessages[index] ? (
                          <ChevronUp className="h-3 w-3" />
                        ) : (
                          <ChevronDown className="h-3 w-3" />
                        )}
                      </Button>
                    </div>
                    
                    <div>
                      <p className="font-medium">{message.content}</p>
                      {message.metadata?.page && (
                        <p className="mt-1">Current page: {message.metadata.page}</p>
                      )}
                      {message.metadata?.links && (
                        <p className="mt-1">
                          Available links: {message.metadata.links.length} 
                          {!isExpanded && message.metadata.links.length > 0 && (
                            <span className="text-muted-foreground">
                              {" "}({message.metadata.links.slice(0, 3).join(", ")}
                              {message.metadata.links.length > 3 ? "..." : ""})
                            </span>
                          )}
                        </p>
                      )}
                      
                      {isExpanded && message.metadata?.links && (
                        <div className="mt-2 space-y-1">
                          {message.metadata.links.map((link, i) => (
                            <div key={i} className="text-xs text-muted-foreground">
                              {i+1}. {link}
                            </div>
                          ))}
                        </div>
                      )}
                    </div>
                  </div>
                );
              } else if (message.role === "result") {
                // Result block
                const isWon = message.metadata?.status === "won";
                return (
                  <div 
                    key={`result-${index}`}
                    className={`p-2 rounded-lg text-xs ${
                      isWon
                        ? "bg-gradient-to-r from-green-50 to-emerald-50 border border-green-200" 
                        : "bg-red-50 border border-red-100"
                    }`}
                  >
                    {isWon ? (
                      <div className="flex flex-col items-center text-center">
                        <div className="mb-2 bg-green-100 p-2 rounded-full">
                          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="text-green-600"><polyline points="20 6 9 17 4 12"></polyline></svg>
                        </div>
                        <h3 className="font-bold text-sm text-green-800 mb-1">{message.content}</h3>
                        
                        {message.metadata?.path && (
                          <div className="bg-white rounded p-2 my-2 w-full border border-green-100">
                            <h4 className="font-medium text-xs text-green-800 mb-1">Path:</h4>
                            <div className="flex flex-wrap items-center gap-1 justify-center text-xs">
                              {message.metadata.path.map((node, index) => (
                                <div key={`result-path-${index}`} className="flex items-center">
                                  <span className="bg-green-50 px-1.5 py-0.5 rounded border border-green-100 font-medium">{node}</span>
                                  {index < message.metadata.path.length - 1 && (
                                    <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="mx-1 text-green-400"><path d="M5 12h14"></path><path d="m12 5 7 7-7 7"></path></svg>
                                  )}
                                </div>
                              ))}
                            </div>
                          </div>
                        )}
                      </div>
                    ) : (
                      <div>
                        <div className="flex items-center gap-1 mb-1 text-xs font-medium text-muted-foreground">
                          <Flag className="h-3 w-3" />
                          <span>Game Over</span>
                        </div>
                        <p>{message.content}</p>
                        
                        {message.metadata?.path && (
                          <p className="mt-1 text-xs text-muted-foreground">
                            Path: {message.metadata.path.join(" → ")}
                          </p>
                        )}
                      </div>
                    )}
                  </div>
                );
              } else if (message.role === "error") {
                return (
                  <div className="p-2 rounded-lg bg-red-50 border border-red-100 text-xs">
                    <p>{message.content}</p>
                  </div>
                );
              }
              
              return null;
            })}

            {modelStatus === "thinking" && (
              <div className="p-2 rounded-lg bg-blue-50 border border-blue-100 text-xs">
                <div className="flex items-center gap-1 mb-1 text-xs font-medium text-muted-foreground">
                  <Bot className="h-3 w-3" />
                  <span className="animate-pulse">Thinking...</span>
                </div>
                <p className="whitespace-pre-wrap text-xs">{partialText}</p>
              </div>
            )}
            
            <div ref={messagesEndRef} />
          </div>
        </Card>
      )}

      {/* Wikipedia view - top right quadrant */}
      <Card className="p-3 md:col-span-6 h-full overflow-hidden row-start-2">
        <h2 className="text-lg font-bold mb-2">Wikipedia View</h2>
        <div className="relative w-full h-[calc(100%-2.5rem)] overflow-hidden">
          <iframe
            style={{
              transform: "scale(0.5, 0.5)",
              width: "calc(100% * 2)",
              height: "calc(100% * 2)",
              transformOrigin: "top left",
              position: "absolute",
              top: 0,
              left: 0,
            }}
            src={`https://simple.wikipedia.org/wiki/${currentPage.replace(
              " ",
              "_"
            )}`}
            className="border-0"
          />
        </div>
      </Card>

      {/* Force directed graph - bottom right quadrant */}
      <Card className="p-3 md:col-span-6 h-full overflow-hidden row-start-3">
        <ForceDirectedGraph runs={currentRuns} runId={0} />
      </Card>
    </div>
  );
}