The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { deck, loopDeck } from "../deck.ts"; |
| 2 | import { schedule } from "../remotion/scene/schedule.ts"; |
| 3 | import { introTimeline, type IntroJson } from "../shared/intro.ts"; |
| 4 | import { VIDEO, type DeckItem, type SceneCapture } from "../shared/types.ts"; |
| 5 | |
| 6 | /** One deck item, loaded and ready for a <Player>. */ |
| 7 | export interface LiveItem { |
| 8 | item: DeckItem; |
| 9 | durationInFrames: number; |
| 10 | inputProps: Record<string, unknown>; |
| 11 | } |
| 12 | |
| 13 | /** One press of →: frames `[from, to)` of an item. */ |
| 14 | export interface LiveStep { |
| 15 | item: number; |
| 16 | from: number; |
| 17 | to: number; |
| 18 | title: string; |
| 19 | notes: string; |
| 20 | } |
| 21 | |
| 22 | const fetchJson = async <T>(file: string): Promise<T | undefined> => { |
| 23 | const response = await fetch(`/${file}?t=${Date.now()}`); |
| 24 | return response.ok ? ((await response.json()) as T) : undefined; |
| 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * Clip times where the recorded terminal starts printing again after at least |
| 29 | * 1.5s of silence: natural places to stop and talk (e.g. a plan waiting for approval). |
| 30 | */ |
| 31 | const terminalPauses = async (id: string): Promise<number[]> => { |
| 32 | const response = await fetch(`/${id}/terminal.cast?t=${Date.now()}`); |
| 33 | if (!response.ok) return []; |
| 34 | const pauses: number[] = []; |
| 35 | let previous = 0; |
| 36 | for (const line of (await response.text()).split("\n").slice(1)) { |
| 37 | if (!line.startsWith("[")) continue; |
| 38 | const [time] = JSON.parse(line) as [number]; |
| 39 | if (time - previous > 1.5) pauses.push(time); |
| 40 | previous = time; |
| 41 | } |
| 42 | return pauses; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * Loads every deck item the same way render.ts does, splits it into |
| 47 | * presenter steps, and skips scenes that haven't been captured yet. |
| 48 | */ |
| 49 | export const loadDeck = async (name = "loop") => { |
| 50 | const items: LiveItem[] = []; |
| 51 | const steps: LiveStep[] = []; |
| 52 | for (const item of name === "talk" ? deck : loopDeck) { |
| 53 | const index = items.length; |
| 54 | if (item.kind === "slide") { |
| 55 | const frames = Math.round((item.seconds ?? 2) * VIDEO.fps); |
| 56 | items.push({ |
| 57 | item, |
| 58 | durationInFrames: frames, |
| 59 | inputProps: { layout: item.layout, props: item.props }, |
| 60 | }); |
| 61 | steps.push({ item: index, from: 0, to: frames, title: item.title, notes: item.notes }); |
| 62 | } else if (item.kind === "intro") { |
| 63 | const intro = await fetchJson<IntroJson>(`${item.id}/intro.json`); |
| 64 | if (!intro) continue; |
| 65 | const ranges = introTimeline(intro.steps); |
| 66 | items.push({ |
| 67 | item, |
| 68 | durationInFrames: Math.max(1, ranges.at(-1)?.to ?? 1), |
| 69 | inputProps: { intro, source: item.id }, |
| 70 | }); |
| 71 | ranges.forEach((range, k) => |
| 72 | steps.push({ |
| 73 | item: index, |
| 74 | ...range, |
| 75 | title: intro.steps[k]!.title, |
| 76 | notes: intro.steps[k]!.notes, |
| 77 | }), |
| 78 | ); |
| 79 | } else { |
| 80 | const capture = await fetchJson<SceneCapture>(`${item.id}/scene.json`); |
| 81 | if (!capture) continue; |
| 82 | if (capture.terminal) capture.terminal.pauses ??= await terminalPauses(item.id); |
| 83 | const plan = await schedule(capture, VIDEO.fps); |
| 84 | items.push({ |
| 85 | item, |
| 86 | durationInFrames: plan.durationInFrames, |
| 87 | inputProps: { id: item.id, capture, plan }, |
| 88 | }); |
| 89 | for (const step of plan.steps) steps.push({ item: index, ...step }); |
| 90 | } |
| 91 | } |
| 92 | return { items, steps }; |
| 93 | }; |