Update script-terminal-modal.tsx

This commit is contained in:
MacRimi
2025-12-10 21:57:16 +01:00
parent 2eb7cb1687
commit d8af31ba5b

View File

@@ -92,14 +92,12 @@ export function ScriptTerminalModal({
}, []) }, [])
const initializeTerminal = async () => { const initializeTerminal = async () => {
console.log("[v0] Loading xterm modules...")
const [TerminalClass, FitAddonClass] = await Promise.all([ const [TerminalClass, FitAddonClass] = await Promise.all([
import("xterm").then((mod) => mod.Terminal), import("xterm").then((mod) => mod.Terminal),
import("xterm-addon-fit").then((mod) => mod.FitAddon), import("xterm-addon-fit").then((mod) => mod.FitAddon),
import("xterm/css/xterm.css"), import("xterm/css/xterm.css"),
]) ])
console.log("[v0] Creating terminal instance...")
const fontSize = window.innerWidth < 768 ? 12 : 16 const fontSize = window.innerWidth < 768 ? 12 : 16
const term = new TerminalClass({ const term = new TerminalClass({
@@ -139,7 +137,6 @@ export function ScriptTerminalModal({
const fitAddon = new FitAddonClass() const fitAddon = new FitAddonClass()
term.loadAddon(fitAddon) term.loadAddon(fitAddon)
console.log("[v0] Opening terminal in container...")
if (terminalContainerRef.current) { if (terminalContainerRef.current) {
term.open(terminalContainerRef.current) term.open(terminalContainerRef.current)
} }
@@ -148,16 +145,12 @@ export function ScriptTerminalModal({
fitAddonRef.current = fitAddon fitAddonRef.current = fitAddon
setTimeout(() => { setTimeout(() => {
try { if (fitAddonRef.current && termRef.current) {
fitAddon.fit() fitAddonRef.current.fit()
console.log("[v0] Terminal fitted, cols:", term.cols, "rows:", term.rows)
} catch (err) {
console.log("[v0] Fit error:", err)
} }
}, 50) }, 100)
const wsUrl = getScriptWebSocketUrl(sessionIdRef.current) const wsUrl = getScriptWebSocketUrl(sessionIdRef.current)
console.log("[v0] Connecting to WebSocket:", wsUrl)
const ws = new WebSocket(wsUrl) const ws = new WebSocket(wsUrl)
wsRef.current = ws wsRef.current = ws
@@ -174,11 +167,9 @@ export function ScriptTerminalModal({
ws.send(JSON.stringify(initMessage)) ws.send(JSON.stringify(initMessage))
setTimeout(() => { setTimeout(() => {
try { if (fitAddonRef.current && termRef.current && ws.readyState === WebSocket.OPEN) {
fitAddon.fit() const cols = termRef.current.cols
const cols = term.cols const rows = termRef.current.rows
const rows = term.rows
console.log("[v0] Sending resize:", { cols, rows })
ws.send( ws.send(
JSON.stringify({ JSON.stringify({
type: "resize", type: "resize",
@@ -186,19 +177,15 @@ export function ScriptTerminalModal({
rows: rows, rows: rows,
}), }),
) )
} catch (err) {
console.log("[v0] Resize error:", err)
} }
}, 100) }, 100)
} }
ws.onmessage = (event) => { ws.onmessage = (event) => {
console.log("[v0] WebSocket message received:", event.data.substring(0, 100))
try { try {
const msg = JSON.parse(event.data) const msg = JSON.parse(event.data)
if (msg.type === "web_interaction" && msg.interaction) { if (msg.type === "web_interaction" && msg.interaction) {
console.log("[v0] Web interaction detected:", msg.interaction.type)
setIsWaitingNextInteraction(false) setIsWaitingNextInteraction(false)
if (waitingTimeoutRef.current) { if (waitingTimeoutRef.current) {
clearTimeout(waitingTimeoutRef.current) clearTimeout(waitingTimeoutRef.current)
@@ -215,7 +202,6 @@ export function ScriptTerminalModal({
} }
if (msg.type === "error") { if (msg.type === "error") {
console.log("[v0] Error message:", msg.message)
term.writeln(`\x1b[31m${msg.message}\x1b[0m`) term.writeln(`\x1b[31m${msg.message}\x1b[0m`)
return return
} }
@@ -253,11 +239,11 @@ export function ScriptTerminalModal({
}) })
checkConnectionInterval.current = setInterval(() => { checkConnectionInterval.current = setInterval(() => {
if (ws) { if (wsRef.current) {
setConnectionStatus( setConnectionStatus(
ws.readyState === WebSocket.OPEN wsRef.current.readyState === WebSocket.OPEN
? "online" ? "online"
: ws.readyState === WebSocket.CONNECTING : wsRef.current.readyState === WebSocket.CONNECTING
? "connecting" ? "connecting"
: "offline", : "offline",
) )
@@ -269,19 +255,15 @@ export function ScriptTerminalModal({
const resizeObserver = new ResizeObserver(() => { const resizeObserver = new ResizeObserver(() => {
if (resizeTimeout) clearTimeout(resizeTimeout) if (resizeTimeout) clearTimeout(resizeTimeout)
resizeTimeout = setTimeout(() => { resizeTimeout = setTimeout(() => {
if (fitAddon && term && ws?.readyState === WebSocket.OPEN) { if (fitAddonRef.current && termRef.current && wsRef.current?.readyState === WebSocket.OPEN) {
try { fitAddonRef.current.fit()
fitAddon.fit() wsRef.current.send(
ws.send(
JSON.stringify({ JSON.stringify({
type: "resize", type: "resize",
cols: term.cols, cols: termRef.current.cols,
rows: term.rows, rows: termRef.current.rows,
}), }),
) )
} catch (err) {
// Ignore
}
} }
}, 100) }, 100)
}) })
@@ -372,10 +354,8 @@ export function ScriptTerminalModal({
const wsProtocol = protocol === "https:" ? "wss:" : "ws:" const wsProtocol = protocol === "https:" ? "wss:" : "ws:"
if (isStandardPort) { if (isStandardPort) {
// When using standard port (proxy scenario), don't add port number
return `${wsProtocol}//${hostname}/ws/script/${sid}` return `${wsProtocol}//${hostname}/ws/script/${sid}`
} else { } else {
// Development or custom port, use API_PORT
return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sid}` return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sid}`
} }
} }
@@ -439,7 +419,6 @@ export function ScriptTerminalModal({
setModalHeight(newHeight) setModalHeight(newHeight)
if (fitAddonRef.current && termRef.current && wsRef.current?.readyState === WebSocket.OPEN) { if (fitAddonRef.current && termRef.current && wsRef.current?.readyState === WebSocket.OPEN) {
try {
setTimeout(() => { setTimeout(() => {
fitAddonRef.current.fit() fitAddonRef.current.fit()
wsRef.current?.send( wsRef.current?.send(
@@ -450,9 +429,6 @@ export function ScriptTerminalModal({
}), }),
) )
}, 10) }, 10)
} catch (err) {
// Ignore
}
} }
} }
@@ -462,7 +438,6 @@ export function ScriptTerminalModal({
localStorage.setItem("scriptModalHeight", modalHeight.toString()) localStorage.setItem("scriptModalHeight", modalHeight.toString())
if (fitAddonRef.current && termRef.current && wsRef.current?.readyState === WebSocket.OPEN) { if (fitAddonRef.current && termRef.current && wsRef.current?.readyState === WebSocket.OPEN) {
try {
setTimeout(() => { setTimeout(() => {
fitAddonRef.current.fit() fitAddonRef.current.fit()
wsRef.current?.send( wsRef.current?.send(
@@ -473,9 +448,6 @@ export function ScriptTerminalModal({
}), }),
) )
}, 50) }, 50)
} catch (err) {
// Ignore
}
} }
document.removeEventListener("mousemove", handleMove) document.removeEventListener("mousemove", handleMove)