rowboat/apps/cli/src/app.ts

205 lines
6.9 KiB
TypeScript
Raw Normal View History

import { AgentState, streamAgent } from "./application/lib/agent.js";
2025-10-28 13:17:06 +05:30
import { StreamRenderer } from "./application/lib/stream-renderer.js";
2025-11-16 20:58:31 +05:30
import { stdin as input, stdout as output } from "node:process";
import fs from "fs";
import path from "path";
import { WorkDir } from "./application/config/config.js";
import { RunEvent } from "./application/entities/run-events.js";
2025-11-16 20:58:31 +05:30
import { createInterface, Interface } from "node:readline/promises";
import { ToolCallPart } from "./application/entities/message.js";
2025-11-16 20:58:31 +05:30
import { z } from "zod";
2025-10-28 13:17:06 +05:30
2025-11-18 20:38:14 +05:30
export async function updateState(agent: string, runId: string) {
const state = new AgentState(agent, runId);
// If running in a TTY, read run events from stdin line-by-line
if (!input.isTTY) {
return;
}
const rl = createInterface({ input, crlfDelay: Infinity });
try {
for await (const line of rl) {
if (line.trim() === "") {
continue;
}
const event = RunEvent.parse(JSON.parse(line));
state.ingestAndLog(event);
}
} finally {
rl.close();
}
}
2025-11-18 22:53:50 +05:30
function renderGreeting() {
const logo = `
$$\\ $$\\
$$ | $$ |
$$$$$$\\ $$$$$$\\ $$\\ $$\\ $$\\ $$$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\ $$\\ $$\\
$$ __$$\\ $$ __$$\\ $$ | $$ | $$ |$$ __$$\\ $$ __$$\\ \\____$$\\_$$ _| \\$$\\ $$ |
$$ | \\__|$$ / $$ |$$ | $$ | $$ |$$ | $$ |$$ / $$ | $$$$$$$ | $$ | \\$$$$ /
$$ | $$ | $$ |$$ | $$ | $$ |$$ | $$ |$$ | $$ |$$ __$$ | $$ |$$\\ $$ $$<
$$ | \\$$$$$$ |\\$$$$$\\$$$$ |$$$$$$$ |\\$$$$$$ |\\$$$$$$$ | \\$$$$ |$$ /\\$$\\
\\__| \\______/ \\_____\\____/ \\_______/ \\______/ \\_______| \\____/ \\__/ \\__|
`;
console.log(logo);
console.log("\nHow can i help you today?");
}
2025-11-15 01:51:22 +05:30
export async function app(opts: {
agent: string;
runId?: string;
input?: string;
2025-11-16 20:58:31 +05:30
noInteractive?: boolean;
2025-11-15 01:51:22 +05:30
}) {
2025-10-28 13:17:06 +05:30
const renderer = new StreamRenderer();
2025-11-18 20:38:14 +05:30
const state = new AgentState(opts.agent, opts.runId);
2025-11-16 20:58:31 +05:30
2025-11-18 22:53:50 +05:30
if (opts.agent === "copilot" && !opts.runId) {
renderGreeting();
}
2025-11-16 20:58:31 +05:30
// load existing and assemble state if required
let runId = opts.runId;
if (runId) {
console.error("loading run", runId);
let stream: fs.ReadStream | null = null;
let rl: Interface | null = null;
try {
const logFile = path.join(WorkDir, "runs", `${runId}.jsonl`);
stream = fs.createReadStream(logFile, { encoding: "utf8" });
rl = createInterface({ input: stream, crlfDelay: Infinity });
for await (const line of rl) {
if (line.trim() === "") {
continue;
}
const parsed = JSON.parse(line);
const event = RunEvent.parse(parsed);
state.ingest(event);
2025-11-16 20:58:31 +05:30
}
} finally {
stream?.close();
}
}
let rl: Interface | null = null;
if (!opts.noInteractive) {
rl = createInterface({ input, output });
}
2025-11-18 20:38:14 +05:30
let inputConsumed = false;
2025-11-16 20:58:31 +05:30
try {
while (true) {
// ask for pending tool permissions
for (const perm of Object.values(state.getPendingPermissions())) {
2025-11-18 20:38:14 +05:30
if (opts.noInteractive) {
return;
}
const response = await getToolCallPermission(perm.toolCall, rl!);
state.ingestAndLog({
type: "tool-permission-response",
response,
toolCallId: perm.toolCall.toolCallId,
subflow: perm.subflow,
});
2025-11-16 20:58:31 +05:30
}
// ask for pending human input
for (const ask of Object.values(state.getPendingAskHumans())) {
2025-11-18 20:38:14 +05:30
if (opts.noInteractive) {
return;
}
const response = await getAskHumanResponse(ask.query, rl!);
state.ingestAndLog({
type: "ask-human-response",
response,
toolCallId: ask.toolCallId,
subflow: ask.subflow,
});
2025-11-16 20:58:31 +05:30
}
// run one turn
for await (const event of streamAgent(state)) {
2025-11-16 20:58:31 +05:30
renderer.render(event);
if (event?.type === "error") {
process.exitCode = 1;
}
}
// if nothing pending, get user input
if (state.getPendingPermissions().length === 0 && state.getPendingAskHumans().length === 0) {
2025-11-18 20:38:14 +05:30
if (opts.input && !inputConsumed) {
state.ingestAndLog({
type: "message",
message: {
role: "user",
content: opts.input,
},
subflow: [],
});
inputConsumed = true;
continue;
}
if (opts.noInteractive) {
return;
}
const response = await getUserInput(rl!);
state.ingestAndLog({
type: "message",
message: {
role: "user",
content: response,
},
subflow: [],
});
2025-11-16 20:58:31 +05:30
}
2025-11-11 12:32:46 +05:30
}
2025-11-16 20:58:31 +05:30
} finally {
rl?.close();
2025-10-28 13:17:06 +05:30
}
2025-11-18 02:28:49 +05:30
}
async function getToolCallPermission(
call: z.infer<typeof ToolCallPart>,
rl: Interface,
): Promise<"approve" | "deny"> {
const question = `Do you want to allow running the following tool: ${call.toolName}?:
Tool name: ${call.toolName}
Tool arguments: ${JSON.stringify(call.arguments)}
Choices: y/n/a/d:
- y: approve
- n: deny
`;
const input = await rl.question(question);
if (input.toLowerCase() === "y") return "approve";
if (input.toLowerCase() === "n") return "deny";
return "deny";
}
async function getAskHumanResponse(
query: string,
rl: Interface,
): Promise<string> {
const input = await rl.question(`The agent is asking for your help with the following query:
Question: ${query}
Please respond to the question.
`);
return input;
}
async function getUserInput(
rl: Interface,
): Promise<string> {
const input = await rl.question("You: ");
if (["quit", "exit", "q"].includes(input.toLowerCase().trim())) {
console.error("Bye!");
process.exit(0);
2025-11-18 02:28:49 +05:30
}
return input;
2025-11-15 01:51:22 +05:30
}