File size: 2,687 Bytes
dd99b77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66ed511
7ad6991
 
 
dd99b77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { consoleStore } from "../stores/console";
import { agentService } from "./agent";

type ConsoleMethod = "log" | "warn" | "error" | "info";

export class ConsoleSyncService {
  private isSetup = false;
  private originalConsole: Record<ConsoleMethod, (...args: unknown[]) => void> =
    {} as Record<ConsoleMethod, (...args: unknown[]) => void>;

  setup(): void {
    if (this.isSetup) return;

    this.originalConsole = {
      log: console.log.bind(console),
      warn: console.warn.bind(console),
      error: console.error.bind(console),
      info: console.info.bind(console),
    };

    const interceptConsole = (method: ConsoleMethod) => {
      const original = this.originalConsole[method];
      console[method] = (...args: unknown[]) => {
        original(...args);

        const firstArg = args[0];
        if (typeof firstArg === "string") {
          if (
            firstArg.includes("[vite]") ||
            firstArg.includes("[VibeGame] Console forwarding") ||
            firstArg.includes("[DEBUG]") ||
            firstArg.includes("hot updated:") ||
            firstArg.includes(
              "using deprecated parameters for the initialization function",
            )
          ) {
            return;
          }
        }

        const message = args
          .map((arg) => {
            if (arg instanceof Error) {
              return arg.message;
            } else if (typeof arg === "object") {
              try {
                return JSON.stringify(arg, null, 2);
              } catch {
                return String(arg);
              }
            }
            return String(arg);
          })
          .join(" ");

        const messageId = `${Date.now()}-${Math.random()}`;
        consoleStore.addMessage(method === "log" ? "info" : method, message);

        agentService.sendRawMessage({
          type: "console_sync",
          payload: {
            id: messageId,
            type: method === "log" ? "info" : method,
            message: message,
            timestamp: Date.now(),
          },
          timestamp: Date.now(),
        });
      };
    };

    (["log", "warn", "error", "info"] as ConsoleMethod[]).forEach(
      interceptConsole,
    );
    this.isSetup = true;
  }

  teardown(): void {
    if (!this.isSetup) return;

    console.log = this.originalConsole.log;
    console.warn = this.originalConsole.warn;
    console.error = this.originalConsole.error;
    console.info = this.originalConsole.info;

    this.originalConsole = {} as Record<
      ConsoleMethod,
      (...args: unknown[]) => void
    >;
    this.isSetup = false;
  }
}

export const consoleSyncService = new ConsoleSyncService();