command-doc.ts
This file
Path
packages/agent-kit/src/command-doc.ts
251 lines · 6.6 KiB · TypeScript
Last changed by
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252import { Brand } from "@repo/brand";import { Cli, Description, NeedsApproval, ReadOnly, Surfaces, TodayOf,} from "@repo/protocol/contracts";import type { Contracts } from "@repo/protocol/contracts";import { Array as Arr, Context, Order, Schema, SchemaAST } from "effect";
export const CommandDoc = Schema.Struct({ contract: Schema.optionalKey(Schema.NonEmptyString), flags: Schema.Array(Schema.NonEmptyString), needsApproval: Schema.Boolean, path: Schema.NonEmptyArray(Schema.NonEmptyString), positional: Schema.Array(Schema.NonEmptyString), readOnly: Schema.Boolean, source: Schema.Literals(["contract", "local"]), summary: Schema.NonEmptyString,});
export type CommandDoc = typeof CommandDoc.Type;
export const localCommandDocs: readonly CommandDoc[] = [ { flags: [ "--url", "--profile", "--token-stdin", "--paste", "--no-browser", "--no-git-config", "--json", ], needsApproval: false, path: ["login"], positional: [], readOnly: false, source: "local", summary: "Sign in (to the platform itself unless --url names another); git and this CLI then use its token.", }, { flags: ["--profile", "--json"], needsApproval: false, path: ["logout"], positional: [], readOnly: false, source: "local", summary: "Forget a profile's token, and stop git asking for a platform no profile is on.", }, { contract: "initProject", flags: [ "--project", "--import", "--branch", "--yes", "--dry-run", "--push", "--no-push", "--json", ], needsApproval: true, path: ["init"], positional: [], readOnly: false, source: "local", summary: "Make this repo, or a public HTTPS repo, a project: find its checks, write its config and push.", }, { contract: "openAsk", flags: [ "--files", "--discussion", "--project", "--yes", "--dry-run", "--json", ], needsApproval: true, path: ["ask", "open"], positional: ["ask"], readOnly: false, source: "local", summary: `Open an ask by landing its file and its check, read from the ask's own file under ${Brand.configDir}/asks/ and the path it names unless --files gives them, once the check fails on main by an assertion, twice in the same way; --discussion names the thread it comes from, and marks it asked.`, }, { flags: [ "--ask", "--project", "--base", "--offline", "--shape-only", "--lane", "--dir", "--json", ], needsApproval: false, path: ["check"], positional: [], readOnly: true, source: "local", summary: "Run the gate's checks on your answer locally, before you push.", }, { contract: "landing", flags: ["--project", "--json"], needsApproval: false, path: ["why"], positional: ["location"], readOnly: true, source: "local", summary: "Explain a line: the ask, the answer and the people that landed it.", }, { contract: "setEnv", flags: ["--project", "--acknowledge-short", "--yes", "--dry-run", "--json"], needsApproval: true, path: ["env", "set"], positional: ["key"], readOnly: false, source: "local", summary: "Set a sensitive env item's value from stdin; nobody can read it back.", }, { flags: ["--json"], needsApproval: false, path: ["hook"], positional: ["event"], readOnly: true, source: "local", summary: "Answer a harness hook (session-start, pre-tool-use, stop) with the gate's policy.", }, { flags: [], needsApproval: false, path: ["credential"], positional: ["operation"], readOnly: false, source: "local", summary: "Be git's credential helper: hand git the stored token for a platform.", }, { flags: [], needsApproval: false, path: ["mcp"], positional: [], readOnly: false, source: "local", summary: "Serve the platform's tools over MCP on stdio.", }, { flags: ["--json"], needsApproval: false, path: ["schema"], positional: ["command"], readOnly: true, source: "local", summary: "Print the JSON Schema of the payload a command sends.", }, { contract: "today", flags: ["--project", "--tui", "--json"], needsApproval: false, path: ["today"], positional: [], readOnly: true, source: "local", summary: `${Context.get(TodayOf.annotations, Description)} It only reads, unless --tui opens it full-screen to take decisions at a terminal.`, },];
const standIns = new Set( localCommandDocs.flatMap(({ contract, path }) => contract === undefined ? [] : [path.join(" ")] ));
const fieldsOf = (ast: SchemaAST.AST) => SchemaAST.isObjects(ast) ? ast.propertySignatures.map(({ name }) => String(name)) : [];
const requiredFieldsOf = (ast: SchemaAST.AST) => SchemaAST.isObjects(ast) ? ast.propertySignatures.flatMap(({ name, type }) => type.context?.isOptional === true ? [] : [String(name)] ) : [];
export const contractCommandDocs = ( group: typeof Contracts): readonly CommandDoc[] => [...group.requests.values()].flatMap((rpc) => { const { annotations } = rpc; const cli = Context.get(annotations, Cli); const name = cli.name ?? rpc._tag; const [first, ...rest] = name.split(" ");
if ( first === undefined || !Context.get(annotations, Surfaces).includes("cli") || standIns.has(name) ) { return []; }
const positional = cli.positional ?? []; const required = requiredFieldsOf(rpc.payloadSchema.ast); const readOnly = Context.get(annotations, ReadOnly); const needsApproval = Context.get(annotations, NeedsApproval);
const flags = fieldsOf(rpc.payloadSchema.ast) .filter((field) => !positional.includes(field)) .map((field) => `--${field}`);
return [ { contract: rpc._tag, flags: [ ...flags, "--json", ...(needsApproval ? ["--yes"] : []), ...(readOnly ? [] : ["--dry-run"]), ], needsApproval, path: [first, ...rest], // An optional positional (a reply's discussion) is taken, not required, so the row leaves it out. positional: positional.filter((field) => required.includes(field)), readOnly, source: "contract", summary: Context.get(annotations, Description), }, ]; });
const byWords = Order.mapInput(Order.String, (doc: CommandDoc) => doc.path.join(" "));
export const commandDocs = (group: typeof Contracts): readonly CommandDoc[] => Arr.sort([...contractCommandDocs(group), ...localCommandDocs], byWords);