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; }, });