File size: 6,961 Bytes
aea470f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * DebugFixBanner.tsx — Live Auto-Debug Toast (S60)
 *
 * Mostra all'utente cosa sta facendo il loop agentLoop → debugLoop in tempo reale.
 * Appare automaticamente quando un tool run_code / execute_shell fallisce e
 * l'agente tenta di auto-ripararlo.
 *
 * Stati:
 *   fixing   → pillola arancio animata "🔧 Auto-debug in corso…"
 *   fixed    → pillola verde "✅ Codice corretto automaticamente!" (sparisce dopo 4s)
 *   failed   → pillola rossa "❌ Auto-debug: impossibile correggere" (sparisce dopo 3s)
 *
 * iPhone-safe: solo CSS transitions, zero Worker/SharedArrayBuffer.
 * Non mostra StreamChunk — filtra solo live_debug_* events.
 */

import { memo, useState, useEffect, useCallback, useRef } from "react";
import type * as React from "react"; // FIX11: React namespace types
import { Wrench, CheckCircle, XCircle } from "lucide-react";
import { useAnyEvent } from "@/core/events/useEventStream";
import type { RuntimeEvent } from "@/core/events/EventTypes";
import { Z_INDEX } from "@/lib/zindex";

// ─── Tipi stato ───────────────────────────────────────────────────────────────

type DebugState =
  | { phase: "idle" }
  | { phase: "fixing";  taskId: string; attempt: number; toolName: string }
  | { phase: "fixed";   taskId: string; attempts: number; snippet?: string }
  | { phase: "failed";  taskId: string; reason: string };

// ─── Colori e icone per fase ──────────────────────────────────────────────────

function stateStyle(phase: DebugState["phase"]): React.CSSProperties {
  if (phase === "fixing") return {
    background: "linear-gradient(135deg, rgba(251,146,60,0.18), rgba(245,158,11,0.12))",
    border:     "1px solid rgba(251,146,60,0.4)",
    color:      "#fb923c",
  };
  if (phase === "fixed") return {
    background: "linear-gradient(135deg, rgba(74,222,128,0.18), rgba(59,130,246,0.12))",
    border:     "1px solid rgba(74,222,128,0.4)",
    color:      "#4ade80",
  };
  return {
    background: "linear-gradient(135deg, rgba(248,113,113,0.18), rgba(239,68,68,0.10))",
    border:     "1px solid rgba(248,113,113,0.35)",
    color:      "#f87171",
  };
}

// ─── Pillola principale ───────────────────────────────────────────────────────

function DebugPill({ state }: { state: Exclude<DebugState, { phase: "idle" }> }) {
  const { phase } = state;
  const style = stateStyle(phase);

  const icon =
    phase === "fixing" ? <Wrench size={13} style={{ flexShrink: 0, animation: "debug-spin 1.6s linear infinite" }} /> :
    phase === "fixed"  ? <CheckCircle size={13} style={{ flexShrink: 0 }} /> :
                         <XCircle size={13} style={{ flexShrink: 0 }} />;

  const label =
    phase === "fixing"
      ? `Auto-debug ${state.toolName}: tentativo ${state.attempt}…`
      : phase === "fixed"
      ? `Codice corretto in ${state.attempts} tentativ${state.attempts === 1 ? "o" : "i"}`
      : `Auto-debug non riuscito`;

  const sub =
    phase === "fixed" && (state as { phase: "fixed"; snippet?: string }).snippet
      ? (state as { phase: "fixed"; snippet?: string }).snippet!.slice(0, 80)
      : phase === "failed"
      ? (state as { phase: "failed"; reason: string }).reason
      : "";

  return (
    <div style={{
      display: "flex", alignItems: "flex-start", gap: 8,
      padding: "8px 14px", borderRadius: 12,
      boxShadow: "0 4px 20px rgba(0,0,0,0.4)",
      maxWidth: 360,
      ...style,
    }}>
      {/* icona */}
      <span style={{ paddingTop: 1 }}>{icon}</span>

      {/* testo */}
      <div style={{ display: "flex", flexDirection: "column", gap: 2, minWidth: 0 }}>
        <span style={{
          fontSize: "0.72rem", fontWeight: 700, letterSpacing: "0.02em",
          whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis",
        }}>
          {label}
        </span>
        {sub && (
          <span style={{
            fontSize: "0.63rem", opacity: 0.75,
            overflow: "hidden", textOverflow: "ellipsis",
            whiteSpace: "nowrap", fontFamily: "ui-monospace,'SF Mono',monospace",
          }}>
            {sub}
          </span>
        )}
      </div>
    </div>
  );
}

// ─── Banner principale ────────────────────────────────────────────────────────

const DebugFixBanner = memo(function DebugFixBanner() {
  const [state, setState] = useState<DebugState>({ phase: "idle" });
  const hideTimer = useRef<ReturnType<typeof setTimeout> | null>(null);

  const clearHide = () => {
    if (hideTimer.current !== null) { clearTimeout(hideTimer.current); hideTimer.current = null; }
  };

  const scheduleHide = useCallback((ms: number) => {
    clearHide();
    hideTimer.current = setTimeout(() => setState({ phase: "idle" }), ms);
  }, []);

  useEffect(() => () => clearHide(), []);

  useAnyEvent(useCallback((ev: RuntimeEvent) => {
    if (ev.kind === "live_debug_start") {
      clearHide();
      setState({ phase: "fixing", taskId: ev.taskId, attempt: ev.attempt, toolName: ev.toolName });
    } else if (ev.kind === "live_debug_fix") {
      setState(prev =>
        prev.phase === "fixing"
          ? { ...prev, attempt: ev.attempt }
          : prev
      );
    } else if (ev.kind === "live_debug_success") {
      setState({ phase: "fixed", taskId: ev.taskId, attempts: ev.attempts });
      scheduleHide(4500);
    } else if (ev.kind === "live_debug_fail") {
      setState({ phase: "failed", taskId: ev.taskId, reason: ev.reason });
      scheduleHide(3000);
    }
  }, [scheduleHide]));

  if (state.phase === "idle") return null;

  return (
    <>
      {/* Keyframe per l'icona rotante */}
      <style>{`
        @keyframes debug-spin {
          0%   { transform: rotate(0deg); }
          25%  { transform: rotate(-15deg); }
          75%  { transform: rotate(15deg); }
          100% { transform: rotate(0deg); }
        }
        .debug-banner-enter {
          animation: debug-banner-in 0.25s ease-out forwards;
        }
        @keyframes debug-banner-in {
          from { opacity: 0; transform: translateY(8px) scale(0.96); }
          to   { opacity: 1; transform: translateY(0)   scale(1); }
        }
      `}</style>

      <div
        className="debug-banner-enter"
        style={{
          position:  "fixed",
          bottom:    72,
          left:      "50%",
          transform: "translateX(-50%)",
          zIndex:    Z_INDEX.SIDEBAR_PANEL + 5,
          pointerEvents: "none",
        }}
      >
        <DebugPill state={state} />
      </div>
    </>
  );
}
);
export default DebugFixBanner;