"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 terminalRef = useRef(null) useEffect(() => { if (open) { console.log("[v0] ScriptTerminalModal opened with:", { scriptPath, scriptName, params, sessionId, }) } }, [open, scriptPath, scriptName, params, sessionId]) useEffect(() => { if (!open) return // We'll pass initMessage prop to TerminalPanel instead return () => { // Cleanup if needed } }, [open]) 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 wsUrl = getScriptWebSocketUrl() console.log("[v0] ScriptTerminalModal WebSocket URL:", wsUrl) console.log("[v0] ScriptTerminalModal initMessage:", { script_path: scriptPath, params: params, }) const handleInteractionResponse = (value: string) => { if (!terminalRef.current || !currentInteraction) return const response = JSON.stringify({ type: "interaction_response", id: currentInteraction.id, value: value, }) console.log("[v0] Sending interaction response:", response) terminalRef.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" && ( )}
)} ) }