|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | + |
| 5 | +const models = [ |
| 6 | + { value: "sonnet-4.5", label: "Sonnet 4.5" }, |
| 7 | + { value: "opus-4.5", label: "Opus 4.5" }, |
| 8 | + { value: "gemini-3-pro", label: "Gemini 3 Pro" }, |
| 9 | +]; |
| 10 | + |
| 11 | +type RunState = |
| 12 | + | { status: "idle" } |
| 13 | + | { status: "starting" } |
| 14 | + | { status: "running"; runId: string; publicAccessToken: string } |
| 15 | + | { status: "complete"; runId: string; publicAccessToken: string } |
| 16 | + | { status: "failed"; error: string }; |
| 17 | + |
| 18 | +export function useRunState() { |
| 19 | + const [runState, setRunState] = useState<RunState>({ status: "idle" }); |
| 20 | + |
| 21 | + async function startRun(prompt: string, model: string) { |
| 22 | + setRunState({ status: "starting" }); |
| 23 | + |
| 24 | + try { |
| 25 | + const res = await fetch("/api/trigger", { |
| 26 | + method: "POST", |
| 27 | + headers: { "Content-Type": "application/json" }, |
| 28 | + body: JSON.stringify({ prompt, model }), |
| 29 | + }); |
| 30 | + |
| 31 | + if (!res.ok) { |
| 32 | + const data = await res.json(); |
| 33 | + setRunState({ status: "failed", error: data.error ?? "Request failed" }); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const { runId, publicAccessToken } = await res.json(); |
| 38 | + setRunState({ status: "running", runId, publicAccessToken }); |
| 39 | + } catch (err) { |
| 40 | + setRunState({ status: "failed", error: err instanceof Error ? err.message : "Unknown error" }); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + function reset() { |
| 45 | + setRunState({ status: "idle" }); |
| 46 | + } |
| 47 | + |
| 48 | + function markComplete() { |
| 49 | + setRunState((prev) => { |
| 50 | + if (prev.status === "running") { |
| 51 | + return { status: "complete", runId: prev.runId, publicAccessToken: prev.publicAccessToken }; |
| 52 | + } |
| 53 | + return prev; |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + return { runState, startRun, reset, markComplete }; |
| 58 | +} |
| 59 | + |
| 60 | +export function ControlBar({ |
| 61 | + runState, |
| 62 | + onRun, |
| 63 | + onReset, |
| 64 | +}: { |
| 65 | + runState: RunState; |
| 66 | + onRun: (prompt: string, model: string) => void; |
| 67 | + onReset: () => void; |
| 68 | +}) { |
| 69 | + const [prompt, setPrompt] = useState("Create a TypeScript CLI tool that converts celsius to fahrenheit with input validation"); |
| 70 | + const [model, setModel] = useState("sonnet-4.5"); |
| 71 | + |
| 72 | + const isDisabled = runState.status === "starting" || runState.status === "running"; |
| 73 | + |
| 74 | + function handleSubmit(e: React.FormEvent<HTMLFormElement>) { |
| 75 | + e.preventDefault(); |
| 76 | + if (!prompt.trim() || isDisabled) return; |
| 77 | + onRun(prompt.trim(), model); |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <form onSubmit={handleSubmit} className="flex flex-col gap-3"> |
| 82 | + <div className="flex gap-3"> |
| 83 | + <input |
| 84 | + type="text" |
| 85 | + value={prompt} |
| 86 | + onChange={(e) => setPrompt(e.target.value)} |
| 87 | + disabled={isDisabled} |
| 88 | + placeholder="Describe what to create..." |
| 89 | + className="flex-1 bg-[var(--terminal-bg)] border border-[var(--terminal-border)] rounded-lg px-4 py-2.5 text-sm text-white placeholder:text-white/30 focus:outline-none focus:border-white/30 disabled:opacity-50 font-[family-name:var(--font-geist-mono)]" |
| 90 | + /> |
| 91 | + |
| 92 | + <select |
| 93 | + value={model} |
| 94 | + onChange={(e) => setModel(e.target.value)} |
| 95 | + disabled={isDisabled} |
| 96 | + className="bg-[var(--terminal-bg)] border border-[var(--terminal-border)] rounded-lg px-3 py-2.5 text-sm text-white/80 focus:outline-none focus:border-white/30 disabled:opacity-50" |
| 97 | + > |
| 98 | + {models.map((m) => ( |
| 99 | + <option key={m.value} value={m.value}>{m.label}</option> |
| 100 | + ))} |
| 101 | + </select> |
| 102 | + |
| 103 | + {runState.status === "complete" || runState.status === "failed" ? ( |
| 104 | + <button |
| 105 | + type="button" |
| 106 | + onClick={onReset} |
| 107 | + className="px-5 py-2.5 rounded-lg bg-white/10 text-white text-sm font-medium hover:bg-white/15 transition-colors" |
| 108 | + > |
| 109 | + New run |
| 110 | + </button> |
| 111 | + ) : ( |
| 112 | + <button |
| 113 | + type="submit" |
| 114 | + disabled={isDisabled || !prompt.trim()} |
| 115 | + className="px-5 py-2.5 rounded-lg bg-white text-black text-sm font-medium hover:bg-white/90 transition-colors disabled:opacity-30 disabled:cursor-not-allowed" |
| 116 | + > |
| 117 | + {runState.status === "starting" ? "Starting..." : "Run"} |
| 118 | + </button> |
| 119 | + )} |
| 120 | + </div> |
| 121 | + |
| 122 | + <div className="flex items-center gap-2"> |
| 123 | + <StatusDot status={runState.status} /> |
| 124 | + <span className="text-xs text-white/40"> |
| 125 | + {runState.status === "idle" && "Ready"} |
| 126 | + {runState.status === "starting" && "Triggering task..."} |
| 127 | + {runState.status === "running" && "Agent is working..."} |
| 128 | + {runState.status === "complete" && "Complete"} |
| 129 | + {runState.status === "failed" && `Failed: ${runState.error}`} |
| 130 | + </span> |
| 131 | + </div> |
| 132 | + </form> |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +function StatusDot({ status }: { status: RunState["status"] }) { |
| 137 | + const color = |
| 138 | + status === "running" || status === "starting" |
| 139 | + ? "bg-yellow-400 animate-pulse" |
| 140 | + : status === "complete" |
| 141 | + ? "bg-green-400" |
| 142 | + : status === "failed" |
| 143 | + ? "bg-red-400" |
| 144 | + : "bg-white/20"; |
| 145 | + |
| 146 | + return <div className={`w-2 h-2 rounded-full ${color}`} />; |
| 147 | +} |
0 commit comments