Repository navigation
Expand file tree
/
Copy pathcli.tsx
More file actions
113 lines (109 loc) · 3.29 KB
/
Copy pathcli.tsx
File metadata and controls
113 lines (109 loc) · 3.29 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
111
112
113
/** Copyright (C) 2026 Theros <https://github.com/therosin> GPL-3.0-or-later */
import { dirname } from "@std/path";
import { render } from "@deno-ink/core";
import { type AgentZoo, buildAgentZoo, loadConfig } from "@agent-zoo/core";
import { StartupScreen } from "./packages/cli/components/StartupScreen.tsx";
import { ZooDashboard } from "./packages/cli/components/ZooDashboard.tsx";
import { formatActivity } from "./packages/cli/activity_format.ts";
import { ActivityLog } from "./packages/cli/ActivityLog.ts";
import { createCli } from "./cli_command.ts";
export async function runCli(args: string[]): Promise<void> {
await createCli(runStandalone).parse(args);
}
export async function runStandalone(
configPath: string,
useUi = true,
task?: string,
): Promise<void> {
const interactive = useUi && Deno.stdout.isTerminal() &&
Deno.stdin.isTerminal();
let startup: Awaited<ReturnType<typeof render>> | undefined;
let zoo: AgentZoo | undefined;
let activityLog: ActivityLog | undefined;
let interruptCount = 0;
const stop = () => {
interruptCount++;
if (interruptCount > 1) {
console.error("Second Ctrl+C received; forcing exit.");
Deno.exit(130);
}
if (!interactive) {
console.error("Stopping gracefully. Press Ctrl+C again to force exit.");
}
void zoo?.stop("SIGINT").catch((error) => {
console.error("Graceful shutdown failed:", error);
});
};
try {
startup = interactive
? await render(
<StartupScreen configPath={configPath} />,
{ fullScreen: true },
)
: undefined;
Deno.addSignalListener("SIGINT", stop);
const config = await loadConfig(configPath);
if (interruptCount > 0) return;
const built = await buildAgentZoo(config, {
baseDir: dirname(configPath),
taskPrompt: task,
runtimeProject: configPath,
});
zoo = built.zoo;
if (interruptCount > 0) {
await zoo.stop("SIGINT");
return;
}
activityLog = await ActivityLog.create(zoo, built.models, configPath);
if (interruptCount > 0) {
await zoo.stop("SIGINT");
return;
}
if (interactive && startup) {
startup.rerender(
<ZooDashboard
zoo={zoo}
configPath={configPath}
logPath={activityLog.path}
objective={task ?? config.taskPrompt}
/>,
);
} else {
attachLineReporter(zoo, configPath, activityLog.path);
}
await zoo.run();
} finally {
try {
Deno.removeSignalListener("SIGINT", stop);
} catch {
// The listener is registered after the first successful render.
}
try {
await zoo?.stop("CLI shutdown");
} finally {
try {
await activityLog?.close();
} finally {
startup?.unmount();
}
}
}
}
function attachLineReporter(
zoo: AgentZoo,
configPath: string,
logPath: string,
): void {
console.log(`AgentZoo config: ${configPath}`);
console.log(`Full activity log: ${logPath}`);
zoo.addEventListener("activity", (event) => {
const activity = (event as CustomEvent).detail;
const display = formatActivity(activity, 120);
console.log(
`[${
new Date(activity.at).toISOString()
}] ${activity.kind} ${display.headline}`,
);
for (const line of display.details) console.log(` ${line}`);
});
}