"use client" import { useState, useEffect, useRef } from "react" import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { X, CheckCircle2, XCircle, Loader2 } from "lucide-react" import { TerminalPanel } from "./terminal-panel" import { API_PORT } from "@/lib/api-config" interface WebInteraction { type: "yesno" | "menu" | "msgbox" | "input" id: string title: string message: string options?: Array<{ label: string; value: string }> } interface ScriptTerminalModalProps { open: boolean onClose: () => void scriptPath: string scriptName: string params?: Record title: string description: string } export function ScriptTerminalModal({ open, onClose, scriptPath, scriptName, params = {}, title, description, }: ScriptTerminalModalProps) { const [sessionId] = useState(() => Math.random().toString(36).substring(7)) const [isComplete, setIsComplete] = useState(false) const [exitCode, setExitCode] = useState(null) const [currentInteraction, setCurrentInteraction] = useState(null) const [interactionInput, setInteractionInput] = useState("") const wsRef = useRef(null) const terminalRef = useRef(null) useEffect(() => { if (!open) return let ws: WebSocket | null = null const wsUrl = getScriptWebSocketUrl() const connectWebSocket = () => { try { ws = new WebSocket(wsUrl) wsRef.current = ws ws.onopen = () => { console.log("[v0] WebSocket connected, sending init message") const initMessage = JSON.stringify({ script_path: scriptPath, params: params, }) ws?.send(initMessage) } ws.onmessage = (event) => { try { const data = JSON.parse(event.data) // Detectar interacciones híbridas if (data.type === "interaction") { console.log("[v0] Interaction detected:", data) setCurrentInteraction(data as WebInteraction) return } // Detectar finalización if (data.type === "exit") { console.log("[v0] Script completed with exit code:", data.code) setIsComplete(true) setExitCode(data.code || 0) return } // Detectar errores if (data.type === "error") { console.error("[v0] Script error:", data.message) return } } catch (e) { // No es JSON, es output normal - TerminalPanel lo manejará } } ws.onerror = (error) => { console.error("[v0] WebSocket error:", error) } ws.onclose = () => { console.log("[v0] WebSocket closed") wsRef.current = null } } catch (error) { console.error("[v0] Failed to create WebSocket:", error) } } connectWebSocket() return () => { if (ws) { ws.close() wsRef.current = null } } }, [open, sessionId, scriptPath, params]) const getScriptWebSocketUrl = (): string => { if (typeof window === "undefined") { return `ws://localhost:${API_PORT}/ws/script/${sessionId}` } const { hostname, protocol } = window.location const wsProtocol = protocol === "https:" ? "wss:" : "ws:" return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sessionId}` } const handleInteractionResponse = (value: string) => { if (!wsRef.current || !currentInteraction) return const response = JSON.stringify({ type: "interaction_response", id: currentInteraction.id, value: value, }) console.log("[v0] Sending interaction response:", response) wsRef.current.send(response) setCurrentInteraction(null) setInteractionInput("") } return ( <> {title} {/* Header */}
{isComplete ? ( exitCode === 0 ? ( ) : ( ) ) : ( )}

{title}

{description &&

{description}

}
{/* Footer */}
Session ID: {sessionId}
{isComplete && }
{currentInteraction && ( setCurrentInteraction(null)}> {currentInteraction.title}

{currentInteraction.message}

{currentInteraction.type === "yesno" && (
)} {currentInteraction.type === "menu" && currentInteraction.options && (
{currentInteraction.options.map((option) => ( ))}
)} {currentInteraction.type === "input" && (
setInteractionInput(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleInteractionResponse(interactionInput) } }} />
)} {currentInteraction.type === "msgbox" && ( )}
)} ) }