Skip to content
Myr

run.ts

This file

Path

apps/cli/src/tui/run.ts

170 lines · 4.8 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

History of this file

Take it

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 1–18why, one step back
1import { Brand } from "@repo/brand";
2import { Client } from "@repo/protocol/contracts";3import type { Decision } from "@repo/protocol/domain/decision";4import type { ProjectName } from "@repo/protocol/ids";5import { Refusal } from "@repo/protocol/refusal";6import { explain } from "@repo/protocol/render";7import type { Scope } from "effect";8import {9 Array as Arr,10 Context,11 Effect,12 Option,13 Predicate,14 Runtime,15 Schema,16 Stdio,17} from "effect";18

Wire `today --tui` into the CLI, and keep the decision under the cursor on screenoscabrielbefore adoption368c432

line 19why, one step back
19import { CurrentMode, Policy } from "../policy.ts";

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 20–62why, one step back
20import { placeOf, reduce, refreshed, start } from "./today.ts";
21import type { DecideCall, TodayViewState } from "./today.ts";2223const instead = `${Brand.cli} today --json`;2425export class TuiRefused extends Schema.TaggedError<TuiRefused>()("TuiRefused", {26 next: Schema.NonEmptyString.pipe(27 Schema.withConstructorDefault(Effect.succeed(instead))28 ),29 reason: Schema.String,30}) {31 override readonly [Runtime.errorExitCode] = 2;3233 override get message() {34 return `${Brand.cli} today --tui needs a person at a terminal, but ${this.reason}: run ${this.next} instead`;35 }36}3738export const causeText = (cause: unknown) =>39 Predicate.hasProperty(cause, "message") && Predicate.isString(cause.message)40 ? cause.message41 : String(cause);4243export interface Screen {44 readonly show: (state: TodayViewState) => Effect.Effect<void>;45 readonly key: Effect.Effect<string>;46 readonly forget: Effect.Effect<void>;47}4849/** Opens the terminal through OpenTUI, loading it only now. */50const terminal: Effect.Effect<Screen, TuiRefused, Scope.Scope> =51 Effect.tryPromise({52 catch: (cause) =>53 new TuiRefused({54 reason: `the terminal could not be opened (${causeText(cause)})`,55 }),56 try: () => import("./screen.ts"),57 }).pipe(Effect.flatMap(({ openTerminal }) => openTerminal));5859export const OpenScreen = Context.Reference<60 Effect.Effect<Screen, TuiRefused, Scope.Scope>61>("cli/tui/OpenScreen", { defaultValue: () => terminal });62

Wire `today --tui` into the CLI, and keep the decision under the cursor on screenoscabrielbefore adoption368c432

lines 63–70why, one step back
63const invocation = Effect.flatMap(
64 Effect.serviceOption(Policy),65 Option.match({66 onNone: () => Effect.provide(Effect.service(Policy), CurrentMode),67 onSome: Effect.succeed,68 })69);70

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

line 71why, one step back
71const aPersonAtATerminal = Effect.gen(function* () {

Wire `today --tui` into the CLI, and keep the decision under the cursor on screenoscabrielbefore adoption368c432

line 72why, one step back
72 const { mode, reason } = yield* invocation;

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 73–170why, one step back
73
74 const typing = yield* Effect.flatMap(75 Stdio.Stdio,76 (stdio) => stdio.stdinIsTerminal77 );7879 // The policy's reason first: an agent at a terminal is still an agent.80 const reasons = [81 ...(mode === "human" ? [] : [reason]),82 ...(typing ? [] : ["stdin is not a terminal"]),83 ];8485 yield* Option.match(Arr.head(reasons), {86 onNone: () => Effect.void,87 onSome: (why) => Effect.fail(new TuiRefused({ reason: why })),88 });89});9091const isRefusal = Schema.is(Refusal);9293export const payloadFor = (project: Option.Option<ProjectName>) =>94 Option.match(project, {95 onNone: () => ({}),96 onSome: (name) => ({ project: name }),97 });9899const decidedLine = (call: DecideCall, decision: Decision) =>100 `Decided: ${decision.decided?.choice ?? call.choice} on ${placeOf(call.project, call.decision)}`;101102export const runTodayTui = (input: {103 readonly project: Option.Option<ProjectName>;104}) =>105 Effect.gen(function* () {106 yield* aPersonAtATerminal;107108 const client = yield* Client;109110 const payload = payloadFor(input.project);111112 const first = start(yield* client.today(payload));113114 const decide = (screen: Screen, state: TodayViewState, call: DecideCall) =>115 Effect.gen(function* () {116 yield* screen.show({117 ...state,118 notice: [119 `Sending ${call.choice} on ${placeOf(call.project, call.decision)}…`,120 ],121 });122123 const notice = yield* client.decide(call).pipe(124 Effect.map(({ decision }) => [decidedLine(call, decision)]),125 Effect.catchIf(isRefusal, (refusal) =>126 Effect.succeed(explain([refusal]))127 )128 );129130 const after = refreshed(state, yield* client.today(payload), notice);131132 yield* screen.show(after);133 yield* screen.forget;134135 return after;136 });137138 const session = (139 screen: Screen,140 state: TodayViewState141 ): Effect.Effect<void, Effect.Error<ReturnType<typeof decide>>> =>142 Effect.flatMap(screen.key, (key) => {143 const step = reduce(state, key);144145 if (step.quit) {146 return Effect.void;147 }148149 return screen.show(step.state).pipe(150 Effect.andThen(151 Option.match(step.call, {152 onNone: () => Effect.succeed(step.state),153 onSome: (call) => decide(screen, step.state, call),154 })155 ),156 Effect.flatMap((next) => session(screen, next))157 );158 });159160 const open = yield* OpenScreen;161162 yield* Effect.scoped(163 Effect.gen(function* () {164 const screen = yield* open;165166 yield* screen.show(first);167 yield* session(screen, first);168 })169 );170 });