2026-04-05 21:09:33 -05:00
|
|
|
/**
|
|
|
|
|
* Agent CLI commands.
|
|
|
|
|
*
|
|
|
|
|
* Python reference: trustgraph-cli/trustgraph/cli/invoke_agent.py
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type { Command } from "commander";
|
2026-06-02 00:22:04 -05:00
|
|
|
import { Effect } from "effect";
|
|
|
|
|
import { cliCommandError, withSocket } from "./util.js";
|
2026-04-05 21:09:33 -05:00
|
|
|
|
|
|
|
|
export function registerAgentCommands(program: Command): void {
|
|
|
|
|
program
|
|
|
|
|
.command("agent")
|
|
|
|
|
.description("Ask the TrustGraph agent a question")
|
|
|
|
|
.argument("<question>", "Question to ask")
|
2026-06-02 00:22:04 -05:00
|
|
|
.action((question: string, _opts, cmd) =>
|
|
|
|
|
Effect.runPromise(withSocket(cmd, (socket, opts) =>
|
|
|
|
|
Effect.gen(function* () {
|
2026-04-05 22:44:45 -05:00
|
|
|
const flow = socket.flow(opts.flow);
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-02 00:22:04 -05:00
|
|
|
yield* Effect.callback<void, ReturnType<typeof cliCommandError>>((resume) => {
|
2026-04-05 22:44:45 -05:00
|
|
|
flow.agent(
|
|
|
|
|
question,
|
|
|
|
|
(chunk) => {
|
|
|
|
|
// think — show thought process
|
2026-05-12 08:06:58 -05:00
|
|
|
if (chunk.length > 0) process.stderr.write(chunk);
|
2026-04-05 22:44:45 -05:00
|
|
|
},
|
|
|
|
|
(chunk) => {
|
|
|
|
|
// observe — show observations
|
2026-05-12 08:06:58 -05:00
|
|
|
if (chunk.length > 0) process.stderr.write(chunk);
|
2026-04-05 22:44:45 -05:00
|
|
|
},
|
|
|
|
|
(chunk, complete) => {
|
|
|
|
|
// answer — print to stdout
|
2026-05-12 08:06:58 -05:00
|
|
|
if (chunk.length > 0) process.stdout.write(chunk);
|
2026-04-05 22:44:45 -05:00
|
|
|
if (complete) {
|
|
|
|
|
process.stdout.write("\n");
|
2026-06-02 00:22:04 -05:00
|
|
|
resume(Effect.void);
|
2026-04-05 22:44:45 -05:00
|
|
|
}
|
|
|
|
|
},
|
2026-06-02 00:22:04 -05:00
|
|
|
(err) => resume(Effect.fail(cliCommandError("agent", err))),
|
2026-04-05 22:44:45 -05:00
|
|
|
);
|
|
|
|
|
});
|
2026-06-02 00:22:04 -05:00
|
|
|
}),
|
|
|
|
|
)),
|
|
|
|
|
);
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|