File size: 2,301 Bytes
bc7e9cd
ebb12a0
ec75a88
db9635c
 
 
 
ebb12a0
 
9434357
 
db9635c
 
 
 
 
 
bc7e9cd
ebb12a0
 
 
 
 
bc7e9cd
 
 
 
db9635c
bc7e9cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db9635c
 
 
 
 
 
 
 
bc7e9cd
 
 
 
 
 
ebb12a0
 
 
db9635c
ebb12a0
 
 
 
 
bc7e9cd
ebb12a0
 
ec75a88
 
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
import { DynamicTool } from "@langchain/core/tools";
import { consoleBuffer } from "./console-buffer";

interface ObserveConsoleOptions {
  tailSize?: number;
}

export const observeConsoleTool = new DynamicTool({
  name: "observe_console",
  description:
    "Read console messages and game state - use to check for errors after making changes",
  func: async (input: string | ObserveConsoleOptions = {}) => {
    const options: ObserveConsoleOptions =
      typeof input === "string" ? {} : input;
    const tailSize = options.tailSize || 10;

    const messages = consoleBuffer.getRecentMessages(undefined, tailSize);
    const allMessages = consoleBuffer.getAllMessages();
    const gameState = consoleBuffer.getGameStateFromMessages();

    consoleBuffer.markAsRead();

    if (messages.length === 0) {
      const debugInfo = `No new console messages since last check.
Total messages in buffer: ${allMessages.length}
Game State Analysis:
- Loading: ${gameState.isLoading}
- Reloading: ${gameState.isReloading}
- Ready: ${gameState.isReady}
- Has Error: ${gameState.hasError}
${gameState.lastError ? `- Last Error: ${gameState.lastError}` : ""}

Last 5 messages in buffer:
${allMessages
  .slice(-5)
  .map(
    (msg) =>
      `[${new Date(msg.timestamp).toISOString()}] [${msg.type}] ${msg.message}`,
  )
  .join("\n")}`;
      return debugInfo;
    }

    const totalNew = consoleBuffer.getRecentMessages().length;
    const displayedCount = messages.length;
    const headerInfo =
      totalNew > displayedCount
        ? `Console Messages (showing last ${displayedCount} of ${totalNew} new):`
        : `Console Messages (${displayedCount} new):`;

    let output = `${headerInfo}\n`;
    output += messages
      .map(
        (msg) =>
          `[${new Date(msg.timestamp).toISOString()}] [${msg.type}] ${msg.message}`,
      )
      .join("\n");

    output += "\n\nGame State:";
    output += `\n- Loading: ${gameState.isLoading}`;
    output += `\n- Reloading: ${gameState.isReloading}`;
    output += `\n- Ready: ${gameState.isReady}`;
    output += `\n- Has Error: ${gameState.hasError}`;
    if (gameState.lastError) {
      output += `\n- Last Error: ${gameState.lastError}`;
    }
    output += `\n\nTotal buffer size: ${allMessages.length} messages`;

    return output;
  },
});