import React, { useState, useRef, useEffect } from "react"; import { MdClose } from "react-icons/md"; import { scriptsApi } from "../api/scripts.api"; import { useTerminalStore } from "@/modules/terminal/store/useTerminalStore"; import { useAgentStore } from "@/app/providers/layout/store/agent.store"; interface RunScriptModalProps { scriptPath: string; scriptId: number; onClose: () => void; } export const RunScriptModal: React.FC = ({ scriptPath, scriptId, onClose, }) => { const [selectedAgentIdx, setSelectedAgentIdx] = useState(0); const [stdinValue, setStdinValue] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const inputRef = useRef(null); const agents = useAgentStore((s) => s.agents); const addJob = useTerminalStore((s) => s.addJob); const openTerminal = useTerminalStore((s) => s.openTerminal); const selectedAgent = agents[selectedAgentIdx]; useEffect(() => { inputRef.current?.focus(); }, []); const handleRun = async () => { if (!selectedAgent) { setError("No agents available"); return; } setLoading(true); setError(null); try { // 1. Запускаем скрипт const runResult = await scriptsApi.runScript(scriptId, { stdin: stdinValue, token: selectedAgent.token, }); // 2. Добавляем джоб в терминал addJob({ id: runResult.id, scriptPath, command: runResult.command, }); // 3. Открываем терминал openTerminal(); // 4. Ждём завершения по id const jobResult = await scriptsApi.waitJob(runResult.id); // 5. Обновляем существующий джоб (не создаём новый!) const terminalStore = useTerminalStore.getState(); terminalStore.updateJob(runResult.id, { command: jobResult.command, stdin: jobResult.stdin, status: jobResult.status, stdout: jobResult.stdout, stderr: jobResult.stderr, isRunning: false, }); onClose(); } catch (e: any) { console.error("Failed to run script:", e); setError(e?.response?.data?.detail || "Failed to run script"); } finally { setLoading(false); } }; return (
e.stopPropagation()} > {/* Header */}

Run Script

{/* Content */}
{/* Script path */}
{scriptPath}
{/* Agent selector */}
{/* Stdin (optional) */}