Repository navigation
Expand file tree
/
Copy pathruntime.ts
More file actions
110 lines (97 loc) · 4.15 KB
/
Copy pathruntime.ts
File metadata and controls
110 lines (97 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Effect } from "effect";
import { Command } from "effect/cli";
import type { CommandError } from "../cli/error.ts";
import * as Otel from "./otel.ts";
import * as Spec from "./spec.ts";
/**
* Binds handlers to a {@link Spec} tree. Handlers are loaded through dynamic
* imports so that starting the process -- or printing `--help` -- never pulls in
* the harness, its sandbox drivers, or the model catalog.
*/
/** Recovers the parsed input type of a spec node, for typing its handler. */
export type Input<Value> =
Value extends Spec.Node<infer _Name, infer Spec, infer _Commands>
? Input<Spec>
: Value extends Command.Command<infer _Name, infer Parsed, infer _Context, infer _Error, infer _Requirements>
? Parsed
: never;
/**
* A handler may yield an ancestor's spec to read its shared flags, which shows up
* as a `CommandContext` requirement; `withSubcommands` discharges it when the tree
* is assembled.
*/
type Requirements = Command.Environment | Command.CommandContext<string>;
type Run<Parsed> = (input: Parsed) => Effect.Effect<void, CommandError, Requirements>;
type Loader<Node extends Spec.Any> = () => Promise<{ readonly default: Run<Input<Node>> }>;
/**
* The handler registry for a spec tree: a loader per leaf, mirroring the shape of
* the tree. A parent with children takes its own handler under `$`.
*/
export type Handlers<Node extends Spec.Any> = keyof Node["commands"] extends never
? Loader<Node>
: { readonly $?: Loader<Node> } & { readonly [Key in keyof Node["commands"]]: Handlers<Node["commands"][Key]> };
interface LazyHandler {
readonly spec: Command.Command.Any;
// biome-ignore lint/suspicious/noExplicitAny: input is recovered per-command by `Input`
readonly load: () => Promise<{ readonly default: Run<any> }>;
}
type LazyHandlers = LazyHandler["load"] | { readonly [key: string]: LazyHandlers | undefined };
type ProvidedCommand = Command.Command<string, unknown, unknown, never, Command.Environment>;
/** Type-checks a handler against the spec node it implements. */
export function handler<const Node extends Spec.Any>(_node: Node, run: Run<Input<Node>>) {
return run;
}
export function handlers<const Root extends Spec.Any>(
root: Root,
handlers: Handlers<Root>,
): ReadonlyArray<LazyHandler> {
const result: LazyHandler[] = [];
function add(node: Spec.Any, value: LazyHandlers) {
if (typeof value === "function") {
result.push({ spec: node.spec, load: value });
return;
}
if (value.$ !== undefined) result.push({ spec: node.spec, load: value.$ as LazyHandler["load"] });
for (const [name, child] of Object.entries(node.commands)) {
const nested = value[name];
if (nested !== undefined) add(child, nested);
}
}
add(root, handlers as LazyHandlers);
return result;
}
export function run(root: Spec.Any, handlers: ReadonlyArray<LazyHandler>, options: { readonly version: string }) {
return Command.run(provide(root, handlers, { version: options.version, path: [] }), options);
}
function provide(
node: Spec.Any,
handlers: ReadonlyArray<LazyHandler>,
context: { readonly version: string; readonly path: ReadonlyArray<string> },
): ProvidedCommand {
const found = handlers.find((entry) => entry.spec === node.spec);
const command = context.path.length === 0 ? node.name : context.path.join(" ");
const spec = found
? node.spec.pipe(
Command.withHandler((input) =>
Effect.promise(found.load).pipe(
Effect.flatMap((module) => module.default(input)),
// Rendered here, so a handler's own failure prints as `error:` instead of reaching the
// runtime's logger with a stack. Imported on failure: the renderer pulls in the harness.
Effect.catch((error) =>
Effect.promise(() => import("../cli/error.ts")).pipe(
Effect.flatMap((module) => module.reportFailure(error)),
),
),
Effect.provide(Otel.layer({ version: context.version, command })),
),
),
)
: node.spec;
const children = Object.values(node.commands);
if (children.length === 0) return spec as ProvidedCommand;
return spec.pipe(
Command.withSubcommands(
children.map((child) => provide(child, handlers, { ...context, path: [...context.path, child.name] })),
),
) as ProvidedCommand;
}