trustgraph/ts/packages/cli/src/commands/flow.ts

100 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-04-05 21:09:33 -05:00
/**
* Flow management CLI commands.
*
* Python reference: trustgraph-cli/trustgraph/cli/start_flow.py, stop_flow.py, etc.
*/
import type { Command } from "commander";
2026-06-02 00:22:04 -05:00
import { Effect } from "effect";
import * as S from "effect/Schema";
import { cliCommandError, withSocket, writeJson } from "./util.js";
2026-04-05 21:09:33 -05:00
export function registerFlowCommands(program: Command): void {
const flow = program
.command("flow")
.description("Flow management");
flow
.command("list")
.description("List active flows")
2026-06-02 00:22:04 -05:00
.action((_opts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const flows = socket.flows();
2026-06-02 00:22:04 -05:00
const ids = yield* Effect.tryPromise({
try: () => flows.getFlows(),
catch: (error) => cliCommandError("flow.list", error),
});
yield* writeJson(ids);
}),
)),
);
2026-04-05 21:09:33 -05:00
flow
2026-04-05 22:44:45 -05:00
.command("get")
.description("Get a flow definition")
.argument("<id>", "Flow ID")
2026-06-02 00:22:04 -05:00
.action((id: string, _opts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const flows = socket.flows();
2026-06-02 00:22:04 -05:00
const def = yield* Effect.tryPromise({
try: () => flows.getFlow(id),
catch: (error) => cliCommandError("flow.get", error),
});
yield* writeJson(def);
}),
)),
);
2026-04-05 21:09:33 -05:00
flow
2026-04-05 22:44:45 -05:00
.command("start")
.description("Start a flow")
.argument("<id>", "Flow ID")
.requiredOption("-b, --blueprint <name>", "Blueprint name")
.option("-d, --description <text>", "Flow description", "")
.option("-p, --parameters <json>", "Parameters as JSON")
2026-06-02 00:22:04 -05:00
.action((id: string, cmdOpts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const flows = socket.flows();
2026-05-12 08:06:58 -05:00
const rawParameters = cmdOpts.parameters as string | undefined;
const params = rawParameters !== undefined && rawParameters.length > 0
2026-06-02 00:22:04 -05:00
? yield* S.decodeUnknownEffect(S.UnknownFromJsonString)(rawParameters).pipe(
Effect.flatMap(S.decodeUnknownEffect(S.Record(S.String, S.Unknown))),
Effect.mapError((error) => cliCommandError("flow.start.parameters", error)),
)
2026-04-05 22:44:45 -05:00
: undefined;
2026-06-02 00:22:04 -05:00
const resp = yield* Effect.tryPromise({
try: () =>
flows.startFlow(
id,
cmdOpts.blueprint as string,
cmdOpts.description as string,
params,
),
catch: (error) => cliCommandError("flow.start", error),
});
yield* writeJson(resp);
}),
)),
);
2026-04-05 21:09:33 -05:00
flow
2026-04-05 22:44:45 -05:00
.command("stop")
.description("Stop a flow")
.argument("<id>", "Flow ID")
2026-06-02 00:22:04 -05:00
.action((id: string, _opts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const flows = socket.flows();
2026-06-02 00:22:04 -05:00
const resp = yield* Effect.tryPromise({
try: () => flows.stopFlow(id),
catch: (error) => cliCommandError("flow.stop", error),
});
yield* writeJson(resp);
}),
)),
);
2026-04-05 21:09:33 -05:00
}