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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { DynamicStructuredTool } from "@langchain/core/tools";
import { z } from "zod";
import type { WebSocket } from "ws";
import { consoleBuffer } from "./console-buffer";
import { virtualFileSystem } from "../services/virtual-fs";

interface EditorWebSocketConnection {
  send: (message: {
    type: string;
    payload: Record<string, unknown>;
    timestamp: number;
  }) => void;
}

let wsConnection: EditorWebSocketConnection | null = null;

export function setMCPWebSocketConnection(ws: WebSocket) {
  wsConnection = {
    send: (message: {
      type: string;
      payload: Record<string, unknown>;
      timestamp: number;
    }) => {
      if (ws && ws.readyState === ws.OPEN) {
        ws.send(JSON.stringify(message));
      }
    },
  };
}

/**
 * Standardized tool response builder
 */
function buildToolResponse({
  success,
  action,
  error,
  gameState,
  consoleOutput,
}: {
  success: boolean;
  action: string;
  error?: string;
  gameState?: ReturnType<typeof consoleBuffer.getGameStateFromMessages>;
  consoleOutput?: string[];
}): string {
  const sections: string[] = [];

  // Status section
  if (success) {
    sections.push(`✅ ${action} completed successfully.`);
  } else {
    sections.push(`❌ ${action} failed.`);
  }

  // Error section
  if (error) {
    sections.push(`\nError: ${error}`);
  }

  // Game state section
  if (gameState) {
    if (gameState.isReady) {
      sections.push("\n🎮 Game Status: Running");
    } else if (gameState.hasError) {
      sections.push(`\n🎮 Game Status: Error\n   ${gameState.lastError}`);
    } else if (gameState.isLoading) {
      sections.push("\n🎮 Game Status: Loading...");
    } else {
      sections.push("\n🎮 Game Status: Unknown");
    }
  }

  // Console output section
  if (consoleOutput && consoleOutput.length > 0) {
    sections.push("\n📝 Console Output:");
    sections.push(consoleOutput.join("\n"));
  }

  return sections.join("\n");
}

/**
 * Wait for game state with improved console capture
 */
async function waitForGameState(
  toolName: string,
  maxWaitTime: number = 5000,
): Promise<{
  gameState: ReturnType<typeof consoleBuffer.getGameStateFromMessages>;
  consoleOutput: string[];
}> {
  // Set execution context for console messages
  consoleBuffer.setExecutionContext(toolName);
  consoleBuffer.clearToolMessages();

  const startTime = Date.now();

  // Initial wait for game to process changes (increased to match GameCanvas reload delay)
  await new Promise((resolve) => setTimeout(resolve, 2000));

  while (Date.now() - startTime < maxWaitTime) {
    const gameState = consoleBuffer.getGameStateFromMessages();

    // Check if we have a definitive state (not reloading, and either ready or error)
    if (!gameState.isReloading && (gameState.isReady || gameState.hasError)) {
      const messages = consoleBuffer.getMessagesSinceLastTool();
      const consoleOutput = messages
        .slice(-30)
        .map((m) => `[${m.type}] ${m.message}`);

      // Clear context
      consoleBuffer.setExecutionContext(null);
      consoleBuffer.markAsRead();

      return { gameState, consoleOutput };
    }

    // If still reloading, wait longer
    if (gameState.isReloading) {
      await new Promise((resolve) => setTimeout(resolve, 200));
    } else {
      await new Promise((resolve) => setTimeout(resolve, 100));
    }
  }

  // Timeout - return current state
  const gameState = consoleBuffer.getGameStateFromMessages();
  const messages = consoleBuffer.getMessagesSinceLastTool();
  const consoleOutput = messages
    .slice(-30)
    .map((m) => `[${m.type}] ${m.message}`);

  // Clear context
  consoleBuffer.setExecutionContext(null);
  consoleBuffer.markAsRead();

  return { gameState, consoleOutput };
}

/**
 * MCPClientManager provides MCP-style tools for editor operations
 * Currently uses local implementation, can be extended to use actual MCP servers
 */
export class MCPClientManager {
  private tools: DynamicStructuredTool[] = [];
  private initialized = false;

  async initialize(): Promise<void> {
    if (this.initialized) {
      return;
    }

    this.tools = this.createEditorTools();
    this.initialized = true;
  }

  private createEditorTools(): DynamicStructuredTool[] {
    const tools: DynamicStructuredTool[] = [];

    tools.push(
      new DynamicStructuredTool({
        name: "read_editor",
        description:
          "Read the complete editor content - use for initial exploration or when search returns no results",
        schema: z.object({}),
        func: async () => {
          const file = virtualFileSystem.getGameFile();
          return `Current editor content (html):\n${file.content}`;
        },
      }),
    );

    tools.push(
      new DynamicStructuredTool({
        name: "read_editor_lines",
        description:
          "Read specific lines from the editor - use AFTER search_editor to examine found code sections in detail",
        schema: z.object({
          startLine: z
            .number()
            .min(1)
            .describe("The starting line number (1-indexed)"),
          endLine: z
            .number()
            .min(1)
            .optional()
            .describe(
              "The ending line number (inclusive). If not provided, only the start line is returned",
            ),
        }),
        func: async (input: { startLine: number; endLine?: number }) => {
          const result = virtualFileSystem.getLines(
            input.startLine,
            input.endLine,
          );

          if (result.error) {
            return `Error: ${result.error}`;
          }

          return result.content;
        },
      }),
    );

    tools.push(
      new DynamicStructuredTool({
        name: "search_editor",
        description:
          "Search for code elements and get line numbers - use FIRST to locate specific functions, classes, or components before reading or editing",
        schema: z.object({
          query: z.string().describe("Text or regex pattern to search for"),
          mode: z
            .enum(["text", "regex"])
            .optional()
            .describe(
              "Search mode: 'text' for literal text search, 'regex' for pattern matching (default: text)",
            ),
          contextLines: z
            .number()
            .min(0)
            .max(5)
            .optional()
            .describe(
              "Number of context lines before/after match (default: 2, max: 5)",
            ),
        }),
        func: async (input: {
          query: string;
          mode?: "text" | "regex";
          contextLines?: number;
        }) => {
          const mode = input.mode || "text";
          const results = virtualFileSystem.searchContent(input.query, mode);

          if (results.length === 0) {
            return `No matches found for "${input.query}" in editor content`;
          }

          const totalMatches = results.length;
          const displayMatches = results.slice(0, 10);

          let output = `Found ${totalMatches} match${totalMatches > 1 ? "es" : ""} for "${input.query}":\n\n`;

          displayMatches.forEach((match, index) => {
            if (index > 0) output += "\n---\n\n";
            output += match.context.join("\n");
          });

          if (totalMatches > 10) {
            output += `\n\n(Showing first 10 of ${totalMatches} matches. Use more specific search terms to narrow results)`;
          }

          return output;
        },
      }),
    );

    tools.push(
      new DynamicStructuredTool({
        name: "edit_editor",
        description:
          "Replace specific text in the editor - use for SMALL, targeted changes (max ~20 lines). For large changes, use multiple edit_editor calls with plan_tasks",
        schema: z.object({
          oldText: z
            .string()
            .describe(
              "The exact text to find and replace (keep small - max ~20 lines)",
            ),
          newText: z.string().describe("The text to replace it with"),
        }),
        func: async (input: { oldText: string; newText: string }) => {
          const currentContent = virtualFileSystem.getGameFile().content;
          if (!currentContent.includes(input.oldText)) {
            const shortPreview =
              input.oldText.substring(0, 50) +
              (input.oldText.length > 50 ? "..." : "");

            return buildToolResponse({
              success: false,
              action: "Text replacement",
              error: `Text not found: "${shortPreview}". This might be due to a previous edit already modifying this text. Please verify the current content with read_editor or search_editor.`,
            });
          }

          const result = virtualFileSystem.editContent(
            input.oldText,
            input.newText,
          );

          if (!result.success) {
            return buildToolResponse({
              success: false,
              action: "Text replacement",
              error: result.error,
            });
          }

          const file = virtualFileSystem.getGameFile();
          this.syncEditorContent(file.content);

          // Wait for game to reload and capture console
          const { gameState, consoleOutput } =
            await waitForGameState("edit_editor");

          return buildToolResponse({
            success: true,
            action: "Text replacement",
            gameState,
            consoleOutput,
          });
        },
      }),
    );

    tools.push(
      new DynamicStructuredTool({
        name: "write_editor",
        description:
          "Replace entire editor content - use ONLY for creating new files or complete rewrites. For modifications, use edit_editor with plan_tasks instead",
        schema: z.object({
          content: z
            .string()
            .describe("The complete code content to write to the editor"),
        }),
        func: async (input: { content: string }) => {
          virtualFileSystem.updateGameContent(input.content);
          this.syncEditorContent(input.content);

          // Wait for game to reload and capture console
          const { gameState, consoleOutput } =
            await waitForGameState("write_editor");

          return buildToolResponse({
            success: true,
            action: "Editor content update",
            gameState,
            consoleOutput,
          });
        },
      }),
    );

    tools.push(...this.createContext7Tools());

    return tools;
  }

  private createContext7Tools(): DynamicStructuredTool[] {
    const tools: DynamicStructuredTool[] = [];
    const apiKey = process.env.CONTEXT7_API_KEY;

    if (!apiKey) {
      console.warn(
        "CONTEXT7_API_KEY not set, Context7 tools will not be available",
      );
      return tools;
    }

    tools.push(
      new DynamicStructuredTool({
        name: "resolve_library_id",
        description: "Resolve a library name to Context7-compatible library ID",
        schema: z.object({
          libraryName: z
            .string()
            .describe("The name of the library to resolve"),
        }),
        func: async (input: { libraryName: string }) => {
          try {
            const response = await globalThis.fetch(
              "https://mcp.context7.com/mcp",
              {
                method: "POST",
                headers: {
                  "Content-Type": "application/json",
                  Accept: "application/json, text/event-stream",
                  CONTEXT7_API_KEY: apiKey,
                },
                body: JSON.stringify({
                  jsonrpc: "2.0",
                  id: Math.floor(Math.random() * 10000),
                  method: "tools/call",
                  params: {
                    name: "resolve-library-id",
                    arguments: input,
                  },
                }),
              },
            );

            if (!response.ok) {
              throw new Error(
                `HTTP ${response.status}: ${response.statusText}`,
              );
            }

            const text = await response.text();

            const lines = text.split("\n");
            let jsonData = null;

            for (const line of lines) {
              if (line.startsWith("data: ")) {
                try {
                  jsonData = JSON.parse(line.substring(6));
                  break;
                } catch {
                  // Continue looking for valid JSON
                }
              }
            }

            if (!jsonData) {
              throw new Error("No valid JSON data found in response");
            }

            if (jsonData.error) {
              throw new Error(
                jsonData.error.message || JSON.stringify(jsonData.error),
              );
            }

            return JSON.stringify(jsonData.result, null, 2);
          } catch (error) {
            return `Error resolving library ID for "${input.libraryName}": ${error instanceof Error ? error.message : String(error)}`;
          }
        },
      }),
    );

    tools.push(
      new DynamicStructuredTool({
        name: "get_library_docs",
        description:
          "Fetch up-to-date documentation for a Context7-compatible library",
        schema: z.object({
          context7CompatibleLibraryID: z
            .string()
            .describe("The Context7 library ID (e.g., '/greensock/gsap')"),
          tokens: z
            .number()
            .optional()
            .describe("Maximum tokens to retrieve (default: 5000)"),
          topic: z
            .string()
            .optional()
            .describe("Specific topic to focus on (e.g., 'animations')"),
        }),
        func: async (input: {
          context7CompatibleLibraryID: string;
          tokens?: number;
          topic?: string;
        }) => {
          try {
            const response = await globalThis.fetch(
              "https://mcp.context7.com/mcp",
              {
                method: "POST",
                headers: {
                  "Content-Type": "application/json",
                  Accept: "application/json, text/event-stream",
                  CONTEXT7_API_KEY: apiKey,
                },
                body: JSON.stringify({
                  jsonrpc: "2.0",
                  id: Math.floor(Math.random() * 10000),
                  method: "tools/call",
                  params: {
                    name: "get-library-docs",
                    arguments: {
                      context7CompatibleLibraryID:
                        input.context7CompatibleLibraryID,
                      tokens: input.tokens || 5000,
                      topic: input.topic,
                    },
                  },
                }),
              },
            );

            if (!response.ok) {
              throw new Error(
                `HTTP ${response.status}: ${response.statusText}`,
              );
            }

            const text = await response.text();

            const lines = text.split("\n");
            let jsonData = null;

            for (const line of lines) {
              if (line.startsWith("data: ")) {
                try {
                  jsonData = JSON.parse(line.substring(6));
                  break;
                } catch {
                  // Continue looking for valid JSON
                }
              }
            }

            if (!jsonData) {
              throw new Error("No valid JSON data found in response");
            }

            if (jsonData.error) {
              throw new Error(
                jsonData.error.message || JSON.stringify(jsonData.error),
              );
            }

            return JSON.stringify(jsonData.result, null, 2);
          } catch (error) {
            return `Error fetching docs for "${input.context7CompatibleLibraryID}": ${error instanceof Error ? error.message : String(error)}`;
          }
        },
      }),
    );

    return tools;
  }

  private syncEditorContent(content: string): void {
    if (wsConnection) {
      wsConnection.send({
        type: "editor_update",
        payload: { content },
        timestamp: Date.now(),
      });
    }
  }

  getTools(): DynamicStructuredTool[] {
    return this.tools;
  }

  async cleanup(): Promise<void> {
    this.initialized = false;
  }
}

export const mcpClientManager = new MCPClientManager();