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

49 lines
1.4 KiB
TypeScript
Raw Normal View History

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";
import { createSocket, getOpts } from "./util.js";
export function registerAgentCommands(program: Command): void {
program
.command("agent")
.description("Ask the TrustGraph agent a question")
.argument("<question>", "Question to ask")
.action(async (question: string, _opts, cmd) => {
const opts = getOpts(cmd);
const socket = await createSocket(opts);
try {
2026-04-05 22:44:45 -05:00
const flow = socket.flow(opts.flow);
2026-04-05 21:09:33 -05:00
2026-04-05 22:44:45 -05:00
await new Promise<void>((resolve, reject) => {
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");
resolve();
}
},
(err) => reject(new Error(err)),
);
});
2026-04-05 21:09:33 -05:00
} finally {
2026-04-05 22:44:45 -05:00
socket.close();
2026-04-05 21:09:33 -05:00
}
});
}