|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | + |
| 5 | +export default function Home() { |
| 6 | + const [prompt, setPrompt] = useState(""); |
| 7 | + const [result, setResult] = useState<{ |
| 8 | + code: string; |
| 9 | + stdout: string; |
| 10 | + stderr: string; |
| 11 | + iterations: number; |
| 12 | + } | null>(null); |
| 13 | + const [status, setStatus] = useState<"idle" | "running" | "done" | "error">( |
| 14 | + "idle", |
| 15 | + ); |
| 16 | + const [error, setError] = useState(""); |
| 17 | + |
| 18 | + async function handleSubmit(e: React.FormEvent) { |
| 19 | + e.preventDefault(); |
| 20 | + if (!prompt.trim()) return; |
| 21 | + |
| 22 | + setStatus("running"); |
| 23 | + setResult(null); |
| 24 | + setError(""); |
| 25 | + |
| 26 | + try { |
| 27 | + const res = await fetch("/api/run", { |
| 28 | + method: "POST", |
| 29 | + headers: { "Content-Type": "application/json" }, |
| 30 | + body: JSON.stringify({ prompt }), |
| 31 | + }); |
| 32 | + |
| 33 | + if (!res.ok) { |
| 34 | + throw new Error(await res.text()); |
| 35 | + } |
| 36 | + |
| 37 | + const data = await res.json(); |
| 38 | + |
| 39 | + const pollResult = async (runId: string) => { |
| 40 | + const poll = await fetch(`/api/run?runId=${runId}`); |
| 41 | + const pollData = await poll.json(); |
| 42 | + |
| 43 | + if (pollData.status === "completed") { |
| 44 | + setResult(pollData.output); |
| 45 | + setStatus("done"); |
| 46 | + } else if (pollData.status === "failed") { |
| 47 | + setError(pollData.error ?? "Workflow failed"); |
| 48 | + setStatus("error"); |
| 49 | + } else { |
| 50 | + setTimeout(() => pollResult(runId), 1000); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + await pollResult(data.runId); |
| 55 | + } catch (err) { |
| 56 | + setError(err instanceof Error ? err.message : "Unknown error"); |
| 57 | + setStatus("error"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <div className="flex min-h-screen items-center justify-center font-sans"> |
| 63 | + <main className="flex w-full max-w-2xl flex-col gap-8 px-6 py-16"> |
| 64 | + <div className="flex flex-col gap-2"> |
| 65 | + <h1 className="text-2xl font-semibold tracking-tight"> |
| 66 | + Sandbox Code Runner |
| 67 | + </h1> |
| 68 | + <p className="text-sm text-zinc-400"> |
| 69 | + Describe a program and AI will generate and execute it in a sandbox. |
| 70 | + If it fails, it automatically retries with the error context. |
| 71 | + </p> |
| 72 | + </div> |
| 73 | + |
| 74 | + <form onSubmit={handleSubmit} className="flex flex-col gap-3"> |
| 75 | + <textarea |
| 76 | + value={prompt} |
| 77 | + onChange={(e) => setPrompt(e.target.value)} |
| 78 | + placeholder='e.g. "Write a program that computes the first 20 fibonacci numbers and prints them as a formatted table"' |
| 79 | + rows={3} |
| 80 | + className="w-full resize-none rounded-lg border border-zinc-800 bg-zinc-900 px-4 py-3 text-sm text-zinc-100 placeholder-zinc-600 outline-none focus:border-zinc-600" |
| 81 | + /> |
| 82 | + <button |
| 83 | + type="submit" |
| 84 | + disabled={status === "running" || !prompt.trim()} |
| 85 | + className="self-start rounded-lg bg-white px-4 py-2 text-sm font-medium text-black transition-colors hover:bg-zinc-200 disabled:cursor-not-allowed disabled:opacity-50" |
| 86 | + > |
| 87 | + {status === "running" ? "Running..." : "Run"} |
| 88 | + </button> |
| 89 | + </form> |
| 90 | + |
| 91 | + {status === "running" && ( |
| 92 | + <div className="flex items-center gap-2 text-sm text-zinc-400"> |
| 93 | + <div className="h-4 w-4 animate-spin rounded-full border-2 border-zinc-600 border-t-zinc-300" /> |
| 94 | + Generating and executing code in sandbox... |
| 95 | + </div> |
| 96 | + )} |
| 97 | + |
| 98 | + {error && ( |
| 99 | + <div className="rounded-lg border border-red-900 bg-red-950/50 px-4 py-3 text-sm text-red-400"> |
| 100 | + {error} |
| 101 | + </div> |
| 102 | + )} |
| 103 | + |
| 104 | + {result && ( |
| 105 | + <div className="flex flex-col gap-4"> |
| 106 | + <div className="flex flex-col gap-2"> |
| 107 | + <div className="flex items-center justify-between"> |
| 108 | + <h2 className="text-sm font-medium text-zinc-300"> |
| 109 | + Generated Code |
| 110 | + </h2> |
| 111 | + {result.iterations > 1 && ( |
| 112 | + <span className="text-xs text-zinc-500"> |
| 113 | + {result.iterations} attempt |
| 114 | + {result.iterations > 1 ? "s" : ""} |
| 115 | + </span> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + <pre className="overflow-x-auto rounded-lg border border-zinc-800 bg-zinc-900 p-4 font-mono text-sm text-zinc-300"> |
| 119 | + {result.code} |
| 120 | + </pre> |
| 121 | + </div> |
| 122 | + |
| 123 | + {result.stdout && ( |
| 124 | + <div className="flex flex-col gap-2"> |
| 125 | + <h2 className="text-sm font-medium text-zinc-300">Output</h2> |
| 126 | + <pre className="overflow-x-auto rounded-lg border border-zinc-800 bg-zinc-900 p-4 font-mono text-sm text-green-400"> |
| 127 | + {result.stdout} |
| 128 | + </pre> |
| 129 | + </div> |
| 130 | + )} |
| 131 | + |
| 132 | + {result.stderr && ( |
| 133 | + <div className="flex flex-col gap-2"> |
| 134 | + <h2 className="text-sm font-medium text-zinc-300">Stderr</h2> |
| 135 | + <pre className="overflow-x-auto rounded-lg border border-zinc-800 bg-zinc-900 p-4 font-mono text-sm text-yellow-400"> |
| 136 | + {result.stderr} |
| 137 | + </pre> |
| 138 | + </div> |
| 139 | + )} |
| 140 | + </div> |
| 141 | + )} |
| 142 | + </main> |
| 143 | + </div> |
| 144 | + ); |
| 145 | +} |
0 commit comments