"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" | "inputbox" id: string title: string message: string options?: Array<{ label: string; value: string }> default?: 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, }) setCurrentInteraction(null) setInteractionInput("") } }, [open, scriptPath, scriptName, params, sessionId]) 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() const handleWebInteraction = (interaction: WebInteraction) => { console.log("[v0] Received web interaction:", interaction) setCurrentInteraction(interaction) } 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) // Access the terminal instance to send the response const terminal = terminalRef.current if (terminal?.terminals?.[0]?.ws) { terminal.terminals[0].ws.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" || (currentInteraction.type === "inputbox" && (
setInteractionInput(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleInteractionResponse(interactionInput) } }} placeholder={currentInteraction.default || ""} />
))} {currentInteraction.type === "msgbox" && ( )}
)} ) }