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 * as Effect from "effect/Effect"; |
| 2 | import * as Match from "effect/Match"; |
| 3 | import * as Alchemist from "../../Alchemist/index.ts"; |
| 4 | import type { Event, PlanningPhase } from "../../Events.ts"; |
| 5 | import { Interaction } from "../../Interaction.ts"; |
| 6 | import * as Report from "../../Report.ts"; |
| 7 | import { isPromptCancellation } from "./errors.ts"; |
| 8 | |
| 9 | export const renderPlanning = |
| 10 | (options: { operation: string; stage: string; computingLabel?: string; readyLabel?: string }) => |
| 11 | <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 12 | Effect.gen(function* () { |
| 13 | const phaseLabel: Record<PlanningPhase, string> = { |
| 14 | "importing-module": "Importing stack module", |
| 15 | "resolving-services": "Resolving stack services", |
| 16 | "loading-state": "Loading stack state", |
| 17 | "computing-plan": options.computingLabel ?? "Computing plan", |
| 18 | "plan-ready": options.readyLabel ?? "Plan ready", |
| 19 | }; |
| 20 | const cli = yield* Report.Cli; |
| 21 | const planning = yield* cli.startPlanningSession( |
| 22 | phaseLabel["importing-module"], |
| 23 | options.stage, |
| 24 | `${options.operation} · ${options.stage}`, |
| 25 | ); |
| 26 | const interactive = (yield* Interaction).terminal.input; |
| 27 | const inFlight = new Set<string>(); |
| 28 | let done = 0; |
| 29 | let total = 0; |
| 30 | const showCounter = (fallback: string) => |
| 31 | Effect.suspend(() => { |
| 32 | const current: string = inFlight.values().next().value ?? fallback; |
| 33 | const rest = inFlight.size > 1 ? ` +${inFlight.size - 1}` : ""; |
| 34 | return planning.update( |
| 35 | phaseLabel["computing-plan"], |
| 36 | `${done}/${total} · ${current}${rest}`, |
| 37 | ); |
| 38 | }); |
| 39 | const completed = (event: { fqn: string; completed: number; total: number }) => { |
| 40 | if (!interactive) return Effect.void; |
| 41 | inFlight.delete(event.fqn); |
| 42 | done = event.completed; |
| 43 | total = event.total; |
| 44 | return showCounter(event.fqn); |
| 45 | }; |
| 46 | return yield* effect.pipe( |
| 47 | Alchemist.onEvent( |
| 48 | Match.type<Event>().pipe( |
| 49 | Match.discriminators("type")({ |
| 50 | "plan.phase": (event) => |
| 51 | event.phase === "plan-ready" |
| 52 | ? planning.succeed(phaseLabel["plan-ready"]) |
| 53 | : planning.update(phaseLabel[event.phase], options.stage, { |
| 54 | spinning: event.phase !== "importing-module", |
| 55 | }), |
| 56 | "state.bootstrap.started": (event) => |
| 57 | planning.update(`Bootstrapping state store '${event.store}'`, options.stage), |
| 58 | "state.bootstrap.completed": () => |
| 59 | planning.update(phaseLabel["loading-state"], options.stage), |
| 60 | "plan.resource.started": (event) => { |
| 61 | if (!interactive) return Effect.void; |
| 62 | inFlight.add(event.fqn); |
| 63 | total = Math.max(total, event.total); |
| 64 | return showCounter(event.fqn); |
| 65 | }, |
| 66 | "plan.resource.completed": completed, |
| 67 | "plan.action.completed": completed, |
| 68 | }), |
| 69 | Match.orElse(() => Effect.void), |
| 70 | ), |
| 71 | ), |
| 72 | // A prompt dismissed mid-plan (e.g. a provider asking the user to |
| 73 | // resolve an ambiguity) is a cancellation, not a planning failure. |
| 74 | Effect.tapError((error) => |
| 75 | planning.fail(isPromptCancellation(error) ? "Planning was canceled" : "Planning failed"), |
| 76 | ), |
| 77 | Effect.ensuring(planning.close), |
| 78 | ); |
| 79 | }); |