Skip to content
Myr

surfaces.ts

This file

Path

apps/cli/src/surfaces.ts

274 lines · 7.6 KiB · TypeScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–37why, one step back
1import { commandDocs } from "@repo/agent-kit/command-doc";
2import { platformSkill } from "@repo/agent-kit/platform";3import { Brand } from "@repo/brand";4import { commandName, toCommands } from "@repo/protocol/cli";5import type { CommandRpc, Defaults } from "@repo/protocol/cli";6import {7 Client,8 Contracts,9 surfaced,10 Surfaces,11} from "@repo/protocol/contracts";12import { toToolkit } from "@repo/protocol/mcp";13import {14 briefResources,15 skillIn,16 skillPrompt,17} from "@repo/protocol/mcp-extras";18import type { Skill } from "@repo/protocol/mcp-extras";19import {20 Context,21 Effect,22 JsonSchema,23 Layer,24 Option,25 Predicate,26 Schema,27 SchemaAST,28 SchemaRepresentation,29} from "effect";30import { McpProtocol, McpServer } from "effect/ai";31import { Argument, Command, Flag } from "effect/cli";32import type { ChildProcessSpawner } from "effect/process";3334import { local } from "./local/index.ts";35import { Output } from "./output.ts";36import { refusalsIn, views } from "./render.ts";37import { repoContext } from "./repo-context.ts";

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

line 38why, one step back
38import { today } from "./tui/command.ts";

Merge build/1104 into mainoscabrielbefore adoption82cc398

line 39why, one step back
39import { cliVersion } from "./version.ts";

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 40–66why, one step back
40
41export const WorkingDirectory = Context.Reference<string>(42 "cli/WorkingDirectory",43 { defaultValue: () => "." }44);4546export const fromRepoContext: Defaults<ChildProcessSpawner.ChildProcessSpawner> =47 {48 fields: { project: "the project of the repo you stand in" },49 values: Effect.gen(function* () {50 const here = yield* Effect.option(repoContext(yield* WorkingDirectory));5152 return Option.match(53 Option.flatMap(here, ({ repo }) => repo),54 {55 onNone: () => ({}),56 onSome: ({ project }) => ({ project }),57 }58 );59 }),60 };6162export interface CommandEntry {63 readonly path: readonly [string, ...string[]];64 readonly command: Command.Command.Any;65}66

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

line 67why, one step back
67/** `env set` reads the value from stdin so it never passes through an argument list or a transcript. */

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

line 68why, one step back
68const wrapped = ["initProject", "setEnv", "today"];

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 69–221why, one step back
69
70const projected = toCommands(Contracts, Effect.service(Client), {71 defaults: fromRepoContext,72 exclude: wrapped,73 output: Effect.service(Output),74 refusalsIn,75 views,76});7778const groups = new Map([79 ["env", "Read a project's env shape, or set a write-only value."],80]);8182export const merge = (83 all: readonly CommandEntry[]84): readonly Command.Command.Any[] => {85 const words = [...new Set(all.map(({ path: [word] }) => word))];8687 return words.map((word) => {88 const here = all.filter(({ path: [first] }) => first === word);89 const own = here.find(({ path }) => path.length === 1)?.command;9091 const below = here.flatMap(({ command, path: [, next, ...rest] }) =>92 next === undefined93 ? []94 : [{ command, path: [next, ...rest] } satisfies CommandEntry]95 );9697 if (below.length === 0 && own !== undefined) {98 return own;99 }100101 const parent =102 own ??103 Command.make(word).pipe(104 Command.withDescription(groups.get(word) ?? `Commands under ${word}.`)105 );106107 return Command.withSubcommands(parent, merge(below));108 });109};110111const spoken = (words: readonly string[]) => words.join(" ");112113export class UnknownCommand extends Schema.TaggedError<UnknownCommand>()(114 "UnknownCommand",115 { words: Schema.String }116) {117 override get message() {118 return `${Brand.cli} has no command "${this.words}" that takes a payload; run ${Brand.cli} --help to list them`;119 }120}121122const byWords = new Map(123 [...Contracts.requests.values()].flatMap((rpc) =>124 Context.get(rpc.annotations, Surfaces).includes("cli")125 ? [[commandName(rpc), rpc] as const]126 : []127 )128);129130const isWriteOnly = (ast: SchemaAST.AST) =>131 SchemaAST.resolveAt<{ readonly id?: unknown }>("representation")(132 SchemaAST.toType(ast)133 )?.id === "effect/schema/Redacted";134135const WRITE_ONLY =136 "Write-only: the platform keeps it and never returns or prints it, and no MCP tool takes it.";137138const decodeJson = Schema.decodeUnknownEffect(Schema.Json);139140export const payloadJsonSchema = (rpc: CommandRpc) => {141 const payload = rpc.payloadSchema;142143 const { definitions, schemas } =144 SchemaRepresentation.toJsonSchemaMultiDocument(145 SchemaRepresentation.toRepresentations([Schema.toCodecJson(payload).ast])146 );147148 const [root] = schemas;149150 const writeOnly = SchemaAST.isObjects(payload.ast)151 ? payload.ast.propertySignatures.flatMap(({ name, type }) =>152 Predicate.isString(name) && isWriteOnly(type) ? [name] : []153 )154 : [];155156 const properties = Predicate.isObject(root.properties)157 ? Object.fromEntries(158 Object.entries(root.properties).map(([name, field]) => [159 name,160 writeOnly.includes(name) && Predicate.isObject(field)161 ? { ...field, description: WRITE_ONLY, writeOnly: true }162 : field,163 ])164 )165 : undefined;166167 const document = Object.fromEntries([168 ["$schema", JsonSchema.META_SCHEMA_URI_DRAFT_2020_12],169 ...Object.entries(root),170 ...(properties === undefined ? [] : [["properties", properties] as const]),171 ...(Object.keys(definitions).length === 0172 ? []173 : [["$defs", definitions] as const]),174 ]);175176 return decodeJson(document).pipe(Effect.orDie);177};178179export const schema = Command.make(180 "schema",181 {182 json: Flag.Boolean("json").pipe(183 Flag.withDefault(false),184 Flag.withDescription(185 "Print one versioned JSON envelope on stdout, and nothing else there"186 )187 ),188 words: Argument.String("command").pipe(189 Argument.variadic({ min: 1 }),190 Argument.withDescription("The command's words: claim, ask open, env set")191 ),192 },193 ({ json, words }) =>194 Effect.gen(function* () {195 const rpc = byWords.get(spoken(words));196197 if (rpc === undefined) {198 return yield* new UnknownCommand({ words: spoken(words) });199 }200201 const document = yield* payloadJsonSchema(rpc);202 const text = JSON.stringify(document, null, 2);203 const output = yield* Output;204205 return yield* output.success({206 command: "schema",207 data: document,208 json,209 view: { markdown: text },210 });211 })212).pipe(213 Command.withDescription(214 "Print the JSON Schema of the payload a command sends."215 )216);217218export const entries: readonly CommandEntry[] = [219 ...projected,220 ...local,221 { command: schema, path: ["schema"] },

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

line 222why, one step back
222 { command: today, path: ["today"] },

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 223–227why, one step back
223];
224225type Every =226 | (typeof projected)[number]["command"]227 | (typeof local)[number]["command"]

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

lines 228–229why, one step back
228 | typeof schema
229 | typeof today;

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

line 230why, one step back
230

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

line 231why, one step back
231/** Merging erases each command's own type, so the tree states the union. */

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 232–263why, one step back
232export type TreeCommand = Command.Command<
233 string,234 never,235 unknown,236 Command.Error<Every>,237 Command.Services<Every>238>;239240export const commands: readonly TreeCommand[] = merge(entries);241242const projection = toToolkit(surfaced("mcp"), Effect.service(Client));243244const skill = skillIn(platformSkill({ commands: commandDocs(Contracts) }));245246const server = (made: Skill) =>247 Layer.mergeAll(248 McpServer.toolkit(projection.toolkit).pipe(249 Layer.provideMerge(projection.layer)250 ),251 briefResources,252 skillPrompt(made)253 ).pipe(254 Layer.provide(255 McpServer.layerStdio({256 name: Brand.name,257 protocols: [258 McpProtocol.v2026_07_28,259 McpProtocol.v2025_11_25,260 McpProtocol.v2025_06_18,261 McpProtocol.v2025_03_26,262 McpProtocol.v2024_11_05,263 ],

Merge build/1104 into mainoscabrielbefore adoption82cc398

line 264why, one step back
264 version: cliVersion,

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 265–268why, one step back
265 })
266 )267 );268

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

line 269why, one step back
269/** The skill is made before the server is built: it reads stdin at once and could miss a first `prompts/get`. */

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 270–274why, one step back
270export const mcp = Command.make("mcp", {}, () =>
271 Effect.flatMap(skill, (made) => Layer.launch(server(made)))272).pipe(273 Command.withDescription("Serve the platform's tools over MCP on stdio.")274);