Spaces:
Configuration error
Configuration error
Commit Β·
921b162
1
Parent(s): eb69de5
commit initial 09-12-2025 018
Browse files31 unmodified lines3233343536373839404142434445464748495051525354555639575859606162636442656667686919 unmodified lines8990916892939495104 unmodified lines2002012021792032042052062072082092102112122132142152162172182192202212226 unmodified lines229230231232233234193235236237196197238239240241242243244245246247248249250251200252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311118 unmodified lines43043143233033133233333433533633733833934034134243343443543643743829 unmodified lines4684694703783793803813824714724734748 unmodified lines48348448539748648748848949049149249349449549649749849950050150250350450550650750850939951051151251351451551651751851940440540640740852052152252331 unmodified lines
const RUNNABLE_LANGS = ["python", "javascript", "java"];
// Utility: detect whether output likely requests inputfunction outputLooksForInput(output) { if (!output) return false; const o = output.toString(); // common prompt words / patterns and trailing prompt char const patterns = [ /enter.*:/i, /input.*:/i, /please enter/i, /scanner/i, // java Scanner exceptions or prompts /press enter/i, /: $/, // ends with colon + space (e.g. "Enter number: ") /:\n$/, // ends with colon + newline /> $/, // trailing > ]; return patterns.some((p) => p.test(o));}
// =================== APP ===================function App() { const [tree, setTree] = useState(loadTree()); const [activePath, setActivePath] = useState("main.py"); // selected file or folder path const [output, setOutput] = useState(""); // Terminal-ish state const [terminalLines, setTerminalLines] = useState([]); // array of strings shown in terminal const [terminalInput, setTerminalInput] = useState(""); // current typed input in terminal prompt const [accumStdin, setAccumStdin] = useState(""); // accumulated stdin passed to backend const [awaitingInput, setAwaitingInput] = useState(false); // true if program waiting for input const [output, setOutput] = useState(""); // last raw output (kept for compatibility) const [prompt, setPrompt] = useState(""); const [explanation, setExplanation] = useState(""); const [stdin, setStdin] = useState(""); // other states const [stdin, setStdin] = useState(""); // legacy single-run input (kept for compatibility) const [problems, setProblems] = useState([]); const [theme, setTheme] = useState("vs-dark"); const [searchOpen, setSearchOpen] = useState(false);19 unmodified lines LANGUAGE_OPTIONS.find((l) => currentNode?.name?.endsWith(l.ext)) || LANGUAGE_OPTIONS[0];
// ---------- Tree utilities ---------- // ---------- TREE helpers ---------- const collectFolderPaths = (node, acc = []) => { if (!node) return acc; if (node.type === "folder") acc.push(node.path || "");104 unmodified lines e.target.value = ""; };
// ---------- Run & Agent (with progress flags) ---------- // ---------- Terminal/Run logic (interactive) ---------- // We will accumulate stdin in `accumStdin`. On initial run we clear it. // When backend returns output containing cues that input is requested, set awaitingInput=true. // When user types into terminal prompt and presses Enter, append that input to accumStdin + "\n", // then re-run the program with updated accumStdin. Repeat until no input cues.
const resetTerminal = () => { setTerminalLines([]); setTerminalInput(""); setAccumStdin(""); setAwaitingInput(false); };
const appendTerminal = (text) => { setTerminalLines((prev) => [...prev, text]); };
const handleRun = async () => { const node = getNodeByPath(tree, activePath); if (!node || node.type !== "file") {6 unmodified lines return; }
// clear terminal state and start a fresh run resetTerminal(); setIsRunning(true); setOutput(""); // clear previous setProblems([]); setOutput(""); try { const res = await runCode(node.content, selectedLang, stdin); setOutput(res.output || ""); // call backend with current accumStdin (empty on first run) const res = await runCode(node.content, selectedLang, accumStdin || stdin || ""); const out = res.output ?? ""; setOutput(out); appendTerminal(out); setProblems(res.error ? parseProblems(res.output) : []);
if (outputLooksForInput(out)) { // program likely wants input: show prompt setAwaitingInput(true); } else { setAwaitingInput(false); } } catch (err) { setOutput(String(err)); const e = String(err); setOutput(e); appendTerminal(e); setAwaitingInput(false); } finally { setIsRunning(false); } };
// Called when user types input into terminal and presses Enter const sendTerminalInput = async () => { if (!awaitingInput) return; const userText = terminalInput; // echo user input in terminal appendTerminal(`> ${userText}`); const newAccum = (accumStdin || "") + userText + "\n"; setAccumStdin(newAccum); setTerminalInput(""); setIsRunning(true); try { // re-run program with updated stdin const node = getNodeByPath(tree, activePath); if (!node || node.type !== "file") { appendTerminal("No file selected."); setAwaitingInput(false); return; } const selectedLang = LANGUAGE_OPTIONS.find((l) => node.name.endsWith(l.ext))?.id; const res = await runCode(node.content, selectedLang, newAccum); const out = res.output ?? ""; setOutput(out); appendTerminal(out); setProblems(res.error ? parseProblems(res.output) : []); // detect if program still waiting if (outputLooksForInput(out)) { setAwaitingInput(true); } else { setAwaitingInput(false); } } catch (err) { appendTerminal(String(err)); setAwaitingInput(false); } finally { setIsRunning(false); } };
// Allow pressing Enter to send input const onTerminalKeyDown = (e) => { if (e.key === "Enter") { e.preventDefault(); if (!awaitingInput) return; sendTerminalInput(); } };
// ---------- Agent functions ---------- const handleAskFix = async () => { const node = getNodeByPath(tree, activePath); if (!node || node.type !== "file") {118 unmodified lines
<div className="ide-menubar-right"> <button onClick={handleSearchToggle} disabled={anyLoading}>π Search</button>
<button onClick={handleRun} disabled={isRunning || anyLoading}> {isRunning ? "β³ Running..." : "βΆ Run"} </button>
<button onClick={handleAskFix} disabled={isFixing || anyLoading}> {isFixing ? "β³ Fixing..." : "π€ Fix"} </button>
<button onClick={handleExplainSelection} disabled={isExplaining || anyLoading}> {isExplaining ? "β³ Explaining..." : "π Explain"} </button>
<button onClick={handleRun} disabled={isRunning || anyLoading}>{isRunning ? "β³ Running..." : "βΆ Run"}</button> <button onClick={handleAskFix} disabled={isFixing || anyLoading}>{isFixing ? "β³ Fixing..." : "π€ Fix"}</button> <button onClick={handleExplainSelection} disabled={isExplaining || anyLoading}>{isExplaining ? "β³ Explaining..." : "π Explain"}</button> <button onClick={() => setTheme((t) => (t === "vs-dark" ? "light" : "vs-dark"))} disabled={anyLoading}> {theme === "vs-dark" ? "βοΈ" : "π"} </button>29 unmodified lines onChange={updateActiveFileContent} onMount={(editor) => (editorRef.current = editor)} onBlur={() => fetchAiSuggestions(currentNode?.content)} options={{ minimap: { enabled: true }, fontSize: 14, scrollBeyondLastLine: false, }} options={{ minimap: { enabled: true }, fontSize: 14, scrollBeyondLastLine: false }} /> </div>
8 unmodified lines
{/* Bottom panels */} <div className="ide-panels"> <pre className="ide-output">{output}</pre> {/* Terminal output */} <div className="ide-output-terminal" style={{ marginBottom: 8 }}> <div style={{ fontSize: 12, color: "#ccc", marginBottom: 6 }}>Terminal</div> <div className="terminal-content" style={{ background: "#000", color: "#0f0", padding: 8, borderRadius: 6, maxHeight: 220, overflowY: "auto", whiteSpace: "pre-wrap", fontFamily: "Consolas, monospace" }}> {terminalLines.length === 0 ? <div style={{ color: "#999" }}>[Program output will appear here]</div> : terminalLines.map((ln, i) => <div key={i}>{ln}</div>)} {awaitingInput && <div style={{ color: "#fff" }}> </div>} </div>
{/* Terminal input (shown only when program asks input) */} {awaitingInput ? ( <div style={{ display: "flex", gap: 6, marginTop: 6 }}> <input className="ide-input-box" placeholder="Type input and press Enter..." value={terminalInput} onChange={(e) => setTerminalInput(e.target.value)} onKeyDown={onTerminalKeyDown} disabled={!awaitingInput || isRunning} /> <button onClick={sendTerminalInput} disabled={!awaitingInput || isRunning} className="ide-button">Send</button> </div> ) : ( // If not awaiting input, show legacy single-run input + hint to run for interactive programs <div style={{ display: "flex", gap: 6, marginTop: 6 }}> <input className="ide-input-box" placeholder="Program input..." value={stdin} onChange={(e) => setStdin(e.target.value)} /> <input className="ide-input-box" placeholder="(Optional) Program input for single-run" value={stdin} onChange={(e) => setStdin(e.target.value)} /> <div style={{ alignSelf: "center", color: "#999", fontSize: 12 }}>Press Run β to execute</div> </div> )} </div>
{/* Problems */} {problems.length > 0 && ( <div className="ide-problems-panel"> <div>π¨ Problems ({problems.length})</div> {problems.map((p, i) => ( <div key={i}> {p.path}:{p.line} β {p.message} </div> ))} {problems.map((p, i) => <div key={i}>{p.path}:{p.line} β {p.message}</div>)} </div> )} </div>src/App.js+145-33