File size: 1,664 Bytes
391c43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Runtime Error Buffer
 *
 * Accumulates JS runtime errors (uncaught exceptions, console.error calls)
 * captured from the preview iframe via postMessage.
 *
 * Errors are NOT injected between orchestrator iterations (they're usually
 * transient — e.g. new JS running against old HTML mid-rewrite). Instead,
 * they're only drained when the AI signals completion (status --complete).
 * The preview clears the buffer on each recompilation so only errors from
 * the latest compilation are present at drain time.
 */

let pending: string[] = [];

export function pushRuntimeError(error: string): void {
  pending.push(error);
  if (typeof window !== 'undefined') {
    window.dispatchEvent(new CustomEvent('runtimeErrorsChanged', {
      detail: { count: pending.length }
    }));
  }
}

export function peekRuntimeErrors(): string[] {
  return [...new Set(pending)];
}

/**
 * Drain runtime errors, returning deduplicated list and clearing the buffer.
 */
export function drainRuntimeErrors(): string[] {
  const errors = [...new Set(pending)];
  pending = [];
  return errors;
}

/**
 * Clear pending errors. Called by the preview on each recompilation
 * so only errors from the latest compilation are retained.
 */
export function clearRuntimeErrors(): void {
  pending = [];
}

/**
 * Reset all state. Called at the start of each generation
 * so errors from a previous generation don't carry over.
 */
export function resetRuntimeErrors(): void {
  pending = [];
}

export function formatRuntimeErrors(errors: string[]): string {
  return 'Runtime errors detected in the preview. Fix these issues:\n\n'
    + errors.map(e => `  - ${e}`).join('\n');
}