The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 376 lines 12.3 KB
Raw
1 import { useEffect, useRef, type ReactNode } from "react";
2 import { highlightTS } from "../marketing/highlightTS";
3 import { useSpinner } from "./_terminal";
4
5 /*
6 * The two steps of the full-picture animation that run on your machine,
7 * before the pull request: an agent edits the Worker and the type checker
8 * catches the missing Layer, then the same test runs on emulated services
9 * and against the real cloud.
10 *
11 * Left: the editor. Right: the agent's transcript. Like the CI steps,
12 * every frame is derived from ms elapsed in the step.
13 */
14
15 export type LocalStepId = "edit" | "local";
16
17 // ---------- Edit: add an upload route, hit the type error, fix it ----------
18 const T_PROMPT_DONE = 900;
19 const T_EDIT1 = 1100;
20 const T_A = [1250, 2050] as const; // typing `const photos = yield* Photos;`
21 const T_B = [2150, 3250] as const; // typing the upload call
22 const T_CHECK1 = 3400;
23 const T_ERROR = 3650;
24 const T_EDIT2 = 5400;
25 const T_C = [5550, 6550] as const; // typing `.pipe(Effect.provide([PhotosR2]))`
26 const T_CHECK2 = 6700;
27 const T_OK = 6950;
28 export const EDIT_MS = 8800;
29
30 // ---------- Iterate locally: emulated test, then the real cloud ----------
31 // The live run replays the preview benchmark's measured phases
32 // (examples/cloudflare-preview-benchmark): deploy 13.35s, destroy 4.76s.
33 // It plays at LIVE_SPEED× so the step stays short, and says so.
34 export const LIVE_SPEED = 2;
35 const T_RUN1 = 300;
36 const T_EMU_UP = 1500;
37 const T_EMU_TEST = [1500, 1900] as const;
38 const T_RUN2 = 2700;
39 const T_DEPLOY = [2900, 2900 + 13350 / LIVE_SPEED] as const;
40 const T_LIVE_TEST = [T_DEPLOY[1], T_DEPLOY[1] + 700] as const;
41 const T_DESTROY = [T_LIVE_TEST[1], T_LIVE_TEST[1] + 4760 / LIVE_SPEED] as const;
42 const T_LIVE_DONE = T_DESTROY[1] + 150;
43 const T_PUSH = T_LIVE_DONE + 1100;
44 export const LOCAL_MS = T_PUSH + 1500;
45
46 const GREEN = "var(--alc-accent-bright)";
47 const RED = "var(--alc-danger)";
48
49 const PROMPT = "add photo uploads to the API";
50 const LINE_A = " const photos = yield* Photos;";
51 const LINE_B = " yield* photos.upload(request.url, yield* request.text);";
52 const PROVIDE = ".pipe(Effect.provide([PhotosR2]))";
53 const ERROR = "Type 'Photos' is not assignable to type 'PlatformServices | WorkerServices'.";
54
55 const TEST_FILE = [
56 "const { test, beforeAll, afterAll, deploy, destroy } = Test.make({",
57 " providers: Cloudflare.providers(),",
58 " dev: !!process.env.LOCAL, // emulated or real cloud",
59 "});",
60 "",
61 "const stack = beforeAll(deploy(Stack));",
62 "afterAll(destroy(Stack));",
63 "",
64 'test("PUT + GET round-trips through R2", Effect.gen(function* () {',
65 " const { url } = yield* stack;",
66 " const res = yield* HttpClient.get(`${url}/object/hello.txt`);",
67 ' expect(yield* res.text).toBe("hi!");',
68 "}));",
69 ];
70
71 const typed = (text: string, [a, b]: readonly [number, number], t: number) =>
72 text.slice(0, Math.round(Math.min(1, Math.max(0, (t - a) / (b - a))) * text.length));
73 const typing = ([a, b]: readonly [number, number], t: number) => t >= a && t < b;
74 /** Live seconds, scaled back up to real time. */
75 const liveSecs = (t: number, from: number) =>
76 `${((Math.max(0, t - from) * LIVE_SPEED) / 1000).toFixed(1)}s`;
77
78 type CodeLine = {
79 text: string;
80 added?: boolean;
81 caret?: boolean;
82 error?: boolean;
83 lit?: boolean;
84 };
85
86 function Chrome({
87 title,
88 badge,
89 children,
90 }: {
91 title: ReactNode;
92 badge?: { text: string; tone: "green" | "red" | "sky" };
93 children: ReactNode;
94 }) {
95 return (
96 <div className="prf-win">
97 <div className="prf-win__bar">
98 <span className="prf-dot" style={{ background: "var(--alc-dot-red)" }} />
99 <span className="prf-dot" style={{ background: "var(--alc-dot-yellow)" }} />
100 <span className="prf-dot" style={{ background: "var(--alc-dot-green)" }} />
101 <span className="prf-win__title">{title}</span>
102 {badge && <span className={`prf-badge prf-badge--${badge.tone}`}>{badge.text}</span>}
103 </div>
104 <div className="prf-win__body">{children}</div>
105 </div>
106 );
107 }
108
109 /* ------------------------------------------------------------------ */
110 /* Left: the editor */
111 /* ------------------------------------------------------------------ */
112
113 function apiLines(t: number): CodeLine[] {
114 const error = t >= T_ERROR && t < T_OK;
115 const lines: CodeLine[] = [
116 { text: "export default Cloudflare.Worker(" },
117 { text: ' "Api",' },
118 { text: " { main: import.meta.url }," },
119 { text: " Effect.gen(function* () {", error },
120 ];
121 if (t >= T_A[0])
122 lines.push({
123 text: typed(LINE_A, T_A, t),
124 added: true,
125 caret: typing(T_A, t),
126 });
127 lines.push(
128 { text: " return {" },
129 { text: " fetch: Effect.gen(function* () {" },
130 { text: " const request = yield* HttpServerRequest;" },
131 );
132 if (t >= T_B[0])
133 lines.push({
134 text: typed(LINE_B, T_B, t),
135 added: true,
136 caret: typing(T_B, t),
137 });
138 lines.push(
139 { text: " return HttpServerResponse.empty({ status: 201 });" },
140 { text: " })," },
141 { text: " };" },
142 {
143 text: ` })${typed(PROVIDE, T_C, t)},`,
144 added: t >= T_C[0],
145 caret: typing(T_C, t),
146 },
147 { text: ");" },
148 );
149 return lines;
150 }
151
152 function testLines(t: number): CodeLine[] {
153 // The `dev:` line decides where the run goes; the hooks are what the live
154 // run adds: a stage of its own, deployed first and destroyed after.
155 const emulated = t >= T_RUN1 && t < T_RUN2;
156 const live = t >= T_RUN2 && t < T_LIVE_DONE;
157 return TEST_FILE.map((text, i) => ({
158 text,
159 lit: (emulated && i === 2) || (live && (i === 5 || i === 6)),
160 }));
161 }
162
163 export function EditorPane({ id, t }: { id: LocalStepId; t: number }) {
164 const file = id === "edit" ? "src/api.ts" : "test/api.test.ts";
165 const lines = id === "edit" ? apiLines(t) : testLines(t);
166 const errorAt = lines.findIndex((l) => l.error);
167 return (
168 <Chrome title={<>my-app — {file}</>}>
169 <div className="prf-edtabs" aria-hidden>
170 <span className={`prf-edtab ${id === "edit" ? "is-active" : ""}`}>api.ts</span>
171 <span className={`prf-edtab ${id === "local" ? "is-active" : ""}`}>api.test.ts</span>
172 </div>
173 <div className="prf-code">
174 {lines.map((l, i) => (
175 <div key={`${file}-${i}`}>
176 <div className={`prf-code__line ${l.added ? "is-added" : ""} ${l.lit ? "is-lit" : ""}`}>
177 <span className="prf-code__n">{i + 1}</span>
178 <span
179 className={`prf-code__t ${l.error ? "is-error" : ""}`}
180 dangerouslySetInnerHTML={{
181 __html: highlightTS(l.text) || "&nbsp;",
182 }}
183 />
184 {l.caret && <span className="prf-caret" />}
185 </div>
186 {i === errorAt && (
187 <div className="prf-diag prf-enter">
188 <span style={{ color: RED }}>✗</span> {ERROR}
189 </div>
190 )}
191 </div>
192 ))}
193 </div>
194 </Chrome>
195 );
196 }
197
198 /* ------------------------------------------------------------------ */
199 /* Right: the agent */
200 /* ------------------------------------------------------------------ */
201
202 function Action({
203 title,
204 detail,
205 children,
206 }: {
207 title: ReactNode;
208 detail?: ReactNode;
209 children?: ReactNode;
210 }) {
211 return (
212 <div className="prf-act prf-enter">
213 <div className="prf-act__head">
214 <span className="prf-act__dot">●</span>
215 <span className="prf-strong">{title}</span>
216 {detail && <span className="prf-muted"> {detail}</span>}
217 </div>
218 {children && <div className="prf-act__out">{children}</div>}
219 </div>
220 );
221 }
222
223 function Row({
224 done,
225 spin,
226 tone = GREEN,
227 children,
228 }: {
229 done: boolean;
230 spin: string;
231 tone?: string;
232 children: ReactNode;
233 }) {
234 return (
235 <div className="prf-act__row">
236 <span className="prf-log__icon" style={{ color: tone }}>
237 {done ? "✓" : spin}
238 </span>
239 {children}
240 </div>
241 );
242 }
243
244 export function AgentPane({ id, t, paused }: { id: LocalStepId; t: number; paused: boolean }) {
245 const listRef = useRef<HTMLDivElement>(null);
246 useEffect(() => {
247 const el = listRef.current;
248 if (el) el.scrollTop = el.scrollHeight;
249 });
250 const busy =
251 id === "edit"
252 ? (t >= T_CHECK1 && t < T_ERROR) || (t >= T_CHECK2 && t < T_OK)
253 : (t >= T_RUN1 && t < T_EMU_TEST[1]) || (t >= T_RUN2 && t < T_DESTROY[1]);
254 const spin = useSpinner(busy && !paused);
255
256 const prompt =
257 id === "edit"
258 ? PROMPT.slice(0, Math.round(Math.min(1, t / T_PROMPT_DONE) * PROMPT.length))
259 : PROMPT;
260
261 const items: ReactNode[] = [
262 <div className="prf-act__prompt" key="prompt">
263 <span className="prf-muted">›</span> {prompt}
264 {id === "edit" && t < T_PROMPT_DONE && <span className="prf-caret" />}
265 </div>,
266 ];
267
268 if (id === "edit") {
269 if (t >= T_EDIT1) items.push(<Action key="e1" title="Edit" detail="src/api.ts · +2 lines" />);
270 if (t >= T_CHECK1)
271 items.push(
272 <Action key="c1" title="Type-check">
273 {t < T_ERROR ? (
274 <Row done={false} spin={spin}>
275 checking
276 </Row>
277 ) : (
278 <>
279 <div className="prf-act__row">
280 <span className="prf-log__icon" style={{ color: RED }}>
281 ✗
282 </span>
283 <span style={{ color: RED }}>1 error</span>
284 <span className="prf-muted"> · src/api.ts:4</span>
285 </div>
286 <div className="prf-act__msg">{ERROR}</div>
287 </>
288 )}
289 </Action>,
290 );
291 if (t >= T_EDIT2)
292 items.push(<Action key="e2" title="Edit" detail="src/api.ts · provide PhotosR2" />);
293 if (t >= T_CHECK2)
294 items.push(
295 <Action key="c2" title="Type-check">
296 <Row done={t >= T_OK} spin={spin}>
297 {t >= T_OK ? "no errors" : "checking"}
298 </Row>
299 </Action>,
300 );
301 } else {
302 items.push(<Action key="done-edit" title="Edit" detail="src/api.ts · type-checks" />);
303 if (t >= T_RUN1)
304 items.push(
305 <Action key="r1" title="Run" detail="LOCAL=1 pnpm test">
306 <Row done={t >= T_EMU_UP} spin={spin}>
307 {t >= T_EMU_UP ? "stack running on emulated services" : "starting emulated services"}
308 </Row>
309 {t >= T_EMU_TEST[0] && (
310 <Row done={t >= T_EMU_TEST[1]} spin={spin}>
311 PUT + GET round-trips through R2
312 </Row>
313 )}
314 {t >= T_EMU_TEST[1] && (
315 <div className="prf-act__row">
316 <span style={{ color: GREEN }}>1 pass</span>
317 <span className="prf-muted"> · 0 fail · nothing deployed</span>
318 </div>
319 )}
320 </Action>,
321 );
322 if (t >= T_RUN2)
323 items.push(
324 <Action key="r2" title="Run" detail="pnpm test">
325 {t >= T_DEPLOY[0] && (
326 <Row done={t >= T_DEPLOY[1]} spin={spin}>
327 {t >= T_DEPLOY[1] ? "deployed" : "deploying"} 3 resources
328 <span className="prf-muted">
329 {" · "}
330 {liveSecs(Math.min(t, T_DEPLOY[1]), T_DEPLOY[0])}
331 </span>
332 </Row>
333 )}
334 {t >= T_LIVE_TEST[0] && (
335 <Row done={t >= T_LIVE_TEST[1]} spin={spin}>
336 PUT + GET round-trips through R2
337 </Row>
338 )}
339 {t >= T_DESTROY[0] && (
340 <Row done={t >= T_DESTROY[1]} spin={spin} tone={RED}>
341 {t >= T_DESTROY[1] ? "destroyed" : "destroying"} 3 resources
342 <span className="prf-muted">
343 {" · "}
344 {liveSecs(Math.min(t, T_DESTROY[1]), T_DESTROY[0])}
345 </span>
346 </Row>
347 )}
348 {t < T_LIVE_DONE ? (
349 <div className="prf-act__row prf-muted">
350 {" "}(played at {LIVE_SPEED}× speed)
351 </div>
352 ) : (
353 <div className="prf-act__row">
354 <span style={{ color: GREEN }}>1 pass</span>
355 <span className="prf-muted"> · 0 fail · real cloud</span>
356 </div>
357 )}
358 </Action>,
359 );
360 if (t >= T_PUSH)
361 items.push(
362 <Action key="push" title="Push" detail="feature/photo-upload · open a pull request" />,
363 );
364 }
365
366 return (
367 <Chrome
368 title="agent · my-app"
369 badge={id === "edit" ? { text: "TYPE-CHECK", tone: "sky" } : { text: "TEST", tone: "green" }}
370 >
371 <div className="prf-agent" ref={listRef}>
372 {items}
373 </div>
374 </Chrome>
375 );
376 }