Update script-terminal-modal.tsx

This commit is contained in:
MacRimi
2025-12-06 21:37:43 +01:00
parent 4378a5843c
commit 310f972c7f

View File

@@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input" import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label" import { Label } from "@/components/ui/label"
import { CheckCircle2, XCircle, Loader2, Activity, GripHorizontal } from "lucide-react" import { CheckCircle2, XCircle, Loader2, Activity, GripHorizontal } from "lucide-react"
import { fetchApi, API_PORT } from "@/lib/api-config" import { API_PORT } from "@/lib/api-config"
import { useIsMobile } from "@/hooks/use-mobile" import { useIsMobile } from "@/hooks/use-mobile"
interface WebInteraction { interface WebInteraction {
@@ -66,41 +66,21 @@ export function ScriptTerminalModal({
return return
} }
const executeScript = async () => { // Generate session ID on client
try { const generateSessionId = () => {
console.log("[v0] Executing script:", scriptPath) return Math.random().toString(36).substring(2, 8)
const response = await fetchApi<{ success: boolean; session_id: string }>("/api/scripts/execute", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
script_name: scriptName,
script_relative_path: scriptPath,
params: params,
}),
})
if (response.success && response.session_id) {
console.log("[v0] Script started with session:", response.session_id)
setSessionId(response.session_id)
} else {
console.error("[v0] Failed to start script")
}
} catch (error) {
console.error("[v0] Error executing script:", error)
}
} }
const newSessionId = generateSessionId()
console.log("[v0] Generated session ID:", newSessionId)
setIsComplete(false) setIsComplete(false)
setExitCode(null) setExitCode(null)
setInteractionInput("") setInteractionInput("")
setCurrentInteraction(null) setCurrentInteraction(null)
setIsConnected(false) setIsConnected(false)
setIsWaitingNextInteraction(false) setIsWaitingNextInteraction(false)
setSessionId(null) setSessionId(newSessionId)
executeScript()
return () => { return () => {
if (checkConnectionInterval.current) { if (checkConnectionInterval.current) {
@@ -189,43 +169,61 @@ export function ScriptTerminalModal({
const ws = new WebSocket(wsUrl) const ws = new WebSocket(wsUrl)
ws.onopen = () => { ws.onopen = () => {
console.log("[v0] WebSocket connected") console.log("[v0] WebSocket connected, sending init data...")
setIsConnected(true) setIsConnected(true)
try {
ws.send( // Send initial script data to start execution
JSON.stringify({ const initData = {
type: "resize", script_path: scriptPath,
cols: term.cols, params: {
rows: term.rows, EXECUTION_MODE: "web",
}), ...params,
) },
} catch (err) {
console.warn("[v0] Failed to send initial resize:", err)
} }
console.log("[v0] Sending init data:", initData)
ws.send(JSON.stringify(initData))
// Send terminal size
setTimeout(() => {
try {
ws.send(
JSON.stringify({
type: "resize",
cols: term.cols,
rows: term.rows,
}),
)
} catch (err) {
console.warn("[v0] Failed to send resize:", err)
}
}, 100)
} }
ws.onmessage = (event) => { ws.onmessage = (event) => {
// Try to parse as JSON for web interactions
try { try {
const msg = JSON.parse(event.data) const msg = JSON.parse(event.data)
if (msg.type === "interaction") { if (msg.type === "web_interaction") {
console.log("[v0] Received web interaction:", msg.interaction)
setIsWaitingNextInteraction(false) setIsWaitingNextInteraction(false)
if (waitingTimeoutRef.current) { if (waitingTimeoutRef.current) {
clearTimeout(waitingTimeoutRef.current) clearTimeout(waitingTimeoutRef.current)
} }
setCurrentInteraction({ setCurrentInteraction({
type: msg.interaction_type, type: msg.interaction.type,
id: msg.id, id: msg.interaction.id,
title: msg.title || "", title: msg.interaction.title || "",
message: msg.message || "", message: msg.interaction.message || "",
options: msg.options, options: msg.interaction.options,
default: msg.default, default: msg.interaction.default,
}) })
} else if (msg.type === "complete") { } else if (msg.type === "error") {
setIsComplete(true) term.writeln(`\x1b[31m${msg.message}\x1b[0m`)
setExitCode(msg.exit_code)
} }
} catch { } catch {
// Not JSON, write as raw terminal output
term.write(event.data) term.write(event.data)
setIsWaitingNextInteraction(false) setIsWaitingNextInteraction(false)
if (waitingTimeoutRef.current) { if (waitingTimeoutRef.current) {
@@ -234,14 +232,22 @@ export function ScriptTerminalModal({
} }
} }
ws.onerror = () => { ws.onerror = (error) => {
console.error("[v0] WebSocket error:", error)
setIsConnected(false) setIsConnected(false)
term.writeln("\x1b[31mWebSocket error occurred\x1b[0m") term.writeln("\x1b[31mWebSocket error occurred\x1b[0m")
} }
ws.onclose = () => { ws.onclose = (event) => {
console.log("[v0] WebSocket closed:", event.code, event.reason)
setIsConnected(false) setIsConnected(false)
term.writeln("\x1b[33mConnection closed\x1b[0m") term.writeln("\x1b[33mConnection closed\x1b[0m")
// Mark as complete when connection closes
if (!isComplete) {
setIsComplete(true)
setExitCode(event.code === 1000 ? 0 : 1)
}
} }
term.onData((data) => { term.onData((data) => {
@@ -260,7 +266,7 @@ export function ScriptTerminalModal({
} }
initTerminal() initTerminal()
}, [open, sessionId]) }, [open, sessionId, scriptPath, params])
useEffect(() => { useEffect(() => {
if (!terminalContainerRef.current || !terminalRef.current || !fitAddonRef.current) { if (!terminalContainerRef.current || !terminalRef.current || !fitAddonRef.current) {