-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathindex.ts
More file actions
788 lines (716 loc) · 22.3 KB
/
index.ts
File metadata and controls
788 lines (716 loc) · 22.3 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
#!/usr/bin/env node
import { createInterface } from "readline";
import { Agent } from "@mariozechner/pi-agent-core";
import type { AgentEvent, AgentTool } from "@mariozechner/pi-agent-core";
import { loadAgent } from "./loader.js";
import { createBuiltinTools } from "./tools/index.js";
import { createSandboxContext } from "./sandbox.js";
import type { SandboxContext, SandboxConfig } from "./sandbox.js";
import { expandSkillCommand, refreshSkills } from "./skills.js";
import { loadHooksConfig, runHooks, wrapToolWithHooks } from "./hooks.js";
import type { HooksConfig } from "./hooks.js";
import { loadDeclarativeTools } from "./tool-loader.js";
import { toAgentTool } from "./tool-utils.js";
import { AuditLogger, isAuditEnabled } from "./audit.js";
import { formatComplianceWarnings } from "./compliance.js";
import { readFile, mkdir, writeFile, stat, access } from "fs/promises";
import { existsSync, readFileSync } from "fs";
import { join, resolve } from "path";
import { execFileSync } from "child_process";
import { initLocalSession } from "./session.js";
import type { LocalSession } from "./session.js";
import { startVoiceServer } from "./voice/server.js";
import { handlePluginCommand } from "./plugin-cli.js";
// ANSI helpers
const dim = (s: string) => `\x1b[2m${s}\x1b[0m`;
const bold = (s: string) => `\x1b[1m${s}\x1b[0m`;
const red = (s: string) => `\x1b[31m${s}\x1b[0m`;
const green = (s: string) => `\x1b[32m${s}\x1b[0m`;
interface ParsedArgs {
model?: string;
dir: string;
prompt?: string;
env?: string;
sandbox?: boolean;
sandboxRepo?: string;
sandboxToken?: string;
repo?: string;
pat?: string;
session?: string;
voice?: string;
}
function parseArgs(argv: string[]): ParsedArgs {
const args = argv.slice(2);
let model: string | undefined;
let dir = process.cwd();
let prompt: string | undefined;
let env: string | undefined;
let sandbox = false;
let sandboxRepo: string | undefined;
let sandboxToken: string | undefined;
let repo: string | undefined;
let pat: string | undefined;
let session: string | undefined;
let voice: string | undefined;
for (let i = 0; i < args.length; i++) {
switch (args[i]) {
case "--model":
case "-m":
model = args[++i];
break;
case "--dir":
case "-d":
dir = args[++i];
break;
case "--prompt":
case "-p":
prompt = args[++i];
break;
case "--env":
case "-e":
env = args[++i];
break;
case "--sandbox":
case "-s":
sandbox = true;
break;
case "--sandbox-repo":
sandboxRepo = args[++i];
break;
case "--sandbox-token":
sandboxToken = args[++i];
break;
case "--repo":
case "-r":
repo = args[++i];
break;
case "--pat":
pat = args[++i];
break;
case "--session":
session = args[++i];
break;
case "--voice":
case "-v":
// Accept optional backend name: --voice, --voice openai, --voice gemini
if (args[i + 1] && !args[i + 1].startsWith("-")) {
voice = args[++i];
} else {
voice = "openai";
}
break;
default:
if (!args[i].startsWith("-")) {
prompt = args[i];
}
break;
}
}
return { model, dir, prompt, env, sandbox, sandboxRepo, sandboxToken, repo, pat, session, voice };
}
function handleEvent(
event: AgentEvent,
hooksConfig: HooksConfig | null,
agentDir: string,
sessionId: string,
auditLogger?: AuditLogger,
): void {
switch (event.type) {
case "message_update": {
const e = event.assistantMessageEvent;
if (e.type === "text_delta") {
process.stdout.write(e.delta);
}
break;
}
case "message_end": {
process.stdout.write("\n");
// Fire post_response hooks (non-blocking)
if (hooksConfig?.hooks.post_response) {
runHooks(hooksConfig.hooks.post_response, agentDir, {
event: "post_response",
session_id: sessionId,
}).catch(() => {});
}
auditLogger?.logResponse().catch(() => {});
break;
}
case "tool_execution_start":
process.stdout.write(dim(`\n▶ ${event.toolName}(${summarizeArgs(event.args)})\n`));
auditLogger?.logToolUse(event.toolName, event.args || {}).catch(() => {});
break;
case "tool_execution_end": {
if (event.isError) {
process.stdout.write(red(`✗ ${event.toolName} failed\n`));
} else {
const result = event.result;
const text = result?.content?.[0]?.text || "";
const preview = text.length > 200 ? text.slice(0, 200) + "…" : text;
if (preview) {
process.stdout.write(dim(preview) + "\n");
}
}
break;
}
case "agent_end":
if (event.messages && event.messages.length > 0) {
const last = event.messages[event.messages.length - 1];
if (last.role === "assistant" && last.errorMessage) {
process.stdout.write(red(`\nError: ${last.errorMessage}\n`));
}
}
break;
}
}
function summarizeArgs(args: any): string {
if (!args) return "";
const entries = Object.entries(args);
if (entries.length === 0) return "";
return entries
.map(([k, v]) => {
const str = typeof v === "string" ? v : JSON.stringify(v);
const short = str.length > 60 ? str.slice(0, 60) + "…" : str;
return `${k}: ${short}`;
})
.join(", ");
}
function askQuestion(question: string): Promise<string> {
const rl = createInterface({ input: process.stdin, output: process.stdout });
return new Promise((res) => {
rl.question(question, (answer) => {
rl.close();
res(answer.trim());
});
});
}
function isGitRepo(dir: string): boolean {
try {
execFileSync("git", ["rev-parse", "--is-inside-work-tree"], { cwd: dir, stdio: "pipe" });
return true;
} catch {
return false;
}
}
async function fileExists(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
async function ensureRepo(dir: string, model?: string): Promise<string> {
const absDir = resolve(dir);
// Create directory if it doesn't exist
if (!(await fileExists(absDir))) {
console.log(dim(`Creating directory: ${absDir}`));
await mkdir(absDir, { recursive: true });
}
// Git init if not a repo
if (!isGitRepo(absDir)) {
console.log(dim("Initializing git repository..."));
execFileSync("git", ["init"], { cwd: absDir, stdio: "pipe" });
// Create .gitignore
const gitignorePath = join(absDir, ".gitignore");
if (!(await fileExists(gitignorePath))) {
await writeFile(gitignorePath, "node_modules/\ndist/\n.gitagent/\n", "utf-8");
}
// Initial commit so memory saves work
execFileSync("git", ["add", "-A"], { cwd: absDir, stdio: "pipe" });
execFileSync("git", ["commit", "-m", "Initial commit", "--allow-empty"], {
cwd: absDir,
stdio: "pipe",
});
}
// Scaffold agent.yaml if missing
const agentYamlPath = join(absDir, "agent.yaml");
if (!(await fileExists(agentYamlPath))) {
const defaultModel = model || "openai:gpt-4o-mini";
const agentName = absDir.split("/").pop() || "my-agent";
const yaml = [
'spec_version: "0.1.0"',
`name: ${agentName}`,
"version: 0.1.0",
`description: Gitclaw agent for ${agentName}`,
"model:",
` preferred: "${defaultModel}"`,
" fallback: []",
"tools: [cli, read, write, memory]",
"runtime:",
" max_turns: 50",
"",
].join("\n");
await writeFile(agentYamlPath, yaml, "utf-8");
console.log(dim(`Created agent.yaml (model: ${defaultModel})`));
}
// Scaffold workspace directory
const workspaceDir = join(absDir, "workspace");
if (!(await fileExists(workspaceDir))) {
await mkdir(workspaceDir, { recursive: true });
}
// Scaffold memory if missing
const memoryDir = join(absDir, "memory");
const memoryFile = join(memoryDir, "MEMORY.md");
if (!(await fileExists(memoryFile))) {
await mkdir(memoryDir, { recursive: true });
await writeFile(memoryFile, "# Memory\n", "utf-8");
}
// Scaffold SOUL.md if missing
const soulPath = join(absDir, "SOUL.md");
if (!(await fileExists(soulPath))) {
await writeFile(soulPath, [
"# Identity",
"",
"You are a helpful AI agent. You live inside a git repository.",
"You can run commands, read and write files, and remember things.",
"Be concise and action-oriented.",
"",
].join("\n"), "utf-8");
}
// Stage new scaffolded files
try {
execFileSync("git", ["add", "-A"], { cwd: absDir, stdio: "pipe" });
try {
execFileSync("git", ["diff", "--cached", "--quiet"], { cwd: absDir, stdio: "pipe" });
} catch {
// There are staged changes — commit them
execFileSync("git", ["commit", "-m", "Scaffold gitclaw agent"], { cwd: absDir, stdio: "pipe" });
}
} catch {
// ok if nothing to commit
}
return absDir;
}
async function main(): Promise<void> {
// Handle plugin subcommand: gitclaw plugin <install|list|remove|...>
if (process.argv[2] === "plugin") {
const allArgs = process.argv.slice(3);
let agentDir = process.cwd();
const pluginArgs: string[] = [];
for (let i = 0; i < allArgs.length; i++) {
if ((allArgs[i] === "--dir" || allArgs[i] === "-d") && allArgs[i + 1]) {
agentDir = allArgs[++i];
} else {
pluginArgs.push(allArgs[i]);
}
}
await handlePluginCommand(resolve(agentDir), pluginArgs);
return;
}
const { model, dir: rawDir, prompt, env, sandbox: useSandbox, sandboxRepo, sandboxToken, repo, pat, session: sessionBranch, voice } = parseArgs(process.argv);
// If --repo is given, derive a default dir from the repo URL (skip interactive prompt)
let dir = rawDir;
let localSession: LocalSession | undefined;
if (repo) {
// Validate mutually exclusive flags
if (useSandbox) {
console.error(red("Error: --repo and --sandbox are mutually exclusive"));
process.exit(1);
}
const token = pat || process.env.GITHUB_TOKEN || process.env.GIT_TOKEN;
if (!token) {
console.error(red("Error: --pat, GITHUB_TOKEN, or GIT_TOKEN is required with --repo"));
process.exit(1);
}
// Default dir: /tmp/gitclaw/<repo-name> if no --dir given
if (dir === process.cwd()) {
const repoName = repo.split("/").pop()?.replace(/\.git$/, "") || "repo";
dir = resolve(`/tmp/gitclaw/${repoName}`);
}
localSession = initLocalSession({
url: repo,
token,
dir,
session: sessionBranch,
});
dir = localSession.dir;
console.log(dim(`Local session: ${localSession.branch} (${localSession.dir})`));
} else if (dir === process.cwd() && !prompt) {
// No --repo: interactive prompt for dir
const answer = await askQuestion(green("? ") + bold("Repository path") + dim(" (. for current dir)") + green(": "));
if (answer) {
dir = resolve(answer === "." ? process.cwd() : answer);
}
}
// Create sandbox context if --sandbox flag is set
let sandboxCtx: SandboxContext | undefined;
if (useSandbox) {
const sandboxConfig: SandboxConfig = {
provider: "e2b",
repository: sandboxRepo,
token: sandboxToken,
};
sandboxCtx = await createSandboxContext(sandboxConfig, resolve(dir));
console.log(dim("Starting sandbox VM..."));
await sandboxCtx.gitMachine.start();
console.log(dim(`Sandbox ready (repo: ${sandboxCtx.repoPath})`));
}
// Ensure the target is a valid gitclaw repo (skip in sandbox/local-repo mode)
if (localSession) {
// Already cloned and scaffolded by initLocalSession
} else if (!useSandbox) {
dir = await ensureRepo(dir, model);
} else {
dir = resolve(dir);
}
// Load .env from agent directory so API keys are available before voice init
const envPath = resolve(dir, ".env");
if (existsSync(envPath)) {
const envContent = readFileSync(envPath, "utf-8");
for (const line of envContent.split("\n")) {
const eq = line.indexOf("=");
if (eq <= 0) continue;
const key = line.slice(0, eq).trim();
const val = line.slice(eq + 1).trim();
if (!process.env[key]) {
process.env[key] = val;
}
}
}
// Voice mode
if (voice) {
let adapterBackend: "openai-realtime" | "gemini-live";
let apiKey: string | undefined;
if (voice === "gemini") {
adapterBackend = "gemini-live";
apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
console.error(red("Error: GEMINI_API_KEY is required for --voice gemini"));
process.exit(1);
}
} else {
adapterBackend = "openai-realtime";
apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
console.error(red("Error: OPENAI_API_KEY is required for --voice mode"));
process.exit(1);
}
}
const cleanup = await startVoiceServer({
adapter: adapterBackend,
adapterConfig: { apiKey },
agentDir: dir,
model,
env,
});
let stopping = false;
process.on("SIGINT", () => {
if (stopping) {
// Second Ctrl+C — force exit immediately
process.exit(1);
}
stopping = true;
console.log("\nDisconnecting...");
cleanup().finally(() => process.exit(0));
});
// Keep process alive
return;
}
let loaded;
try {
loaded = await loadAgent(dir, model, env);
} catch (err: any) {
console.error(red(`Error: ${err.message}`));
process.exit(1);
}
const { systemPrompt, manifest, skills, sessionId, agentDir, gitagentDir, complianceWarnings } = loaded;
// Show compliance warnings
if (complianceWarnings.length > 0) {
const yellow = (s: string) => `\x1b[33m${s}\x1b[0m`;
console.log(yellow("Compliance warnings:"));
console.log(yellow(formatComplianceWarnings(complianceWarnings)));
}
// Initialize audit logger
const auditEnabled = isAuditEnabled(manifest.compliance);
const auditLogger = new AuditLogger(gitagentDir, sessionId, auditEnabled);
if (auditEnabled) {
await auditLogger.logSessionStart();
}
// Load hooks config (agent + plugin hooks merged)
const { mergeHooksConfigs } = await import("./plugins.js");
const agentHooksConfig = await loadHooksConfig(agentDir);
const hooksConfig = mergeHooksConfigs(agentHooksConfig, loaded.plugins);
// Run on_session_start hooks
if (hooksConfig?.hooks.on_session_start) {
try {
const result = await runHooks(hooksConfig.hooks.on_session_start, agentDir, {
event: "on_session_start",
session_id: sessionId,
agent: manifest.name,
});
if (result.action === "block") {
console.error(red(`Session blocked by hook: ${result.reason || "no reason given"}`));
process.exit(1);
}
} catch (err: any) {
console.error(red(`Hook error: ${err.message}`));
}
}
// Map provider to expected env var
const apiKeyEnvVars: Record<string, string> = {
anthropic: "ANTHROPIC_API_KEY",
openai: "OPENAI_API_KEY",
google: "GOOGLE_API_KEY",
xai: "XAI_API_KEY",
groq: "GROQ_API_KEY",
mistral: "MISTRAL_API_KEY",
};
const provider = loaded.model.provider;
const envVar = apiKeyEnvVars[provider];
if (envVar && !process.env[envVar]) {
console.error(red(`Error: ${envVar} environment variable is not set.`));
console.error(dim(`Set it with: export ${envVar}=your-key-here`));
process.exit(1);
}
// Collect plugin memory layers
const pluginMemoryLayers = loaded.plugins.flatMap((p) => p.memoryLayers);
// Build tools — built-in + declarative
let tools: AgentTool<any>[] = createBuiltinTools({
dir,
timeout: manifest.runtime.timeout,
sandbox: sandboxCtx,
gitagentDir,
pluginMemoryLayers: pluginMemoryLayers.length > 0 ? pluginMemoryLayers : undefined,
});
// Load declarative tools from tools/*.yaml (Phase 2.2)
const declarativeTools = await loadDeclarativeTools(agentDir);
tools = [...tools, ...declarativeTools];
// Plugin tools (declarative + programmatic) — check for collisions with existing tools
const existingToolNames = new Set(tools.map((t) => t.name));
for (const plugin of loaded.plugins) {
const pluginTools = [
...plugin.tools,
...plugin.programmaticTools.map(toAgentTool),
];
for (const t of pluginTools) {
if (existingToolNames.has(t.name)) {
console.warn(`[plugin:${plugin.manifest.id}] Tool "${t.name}" collides with existing tool — skipping`);
} else {
tools.push(t);
existingToolNames.add(t.name);
}
}
}
// Wrap with hooks if configured
if (hooksConfig) {
tools = tools.map((t) => wrapToolWithHooks(t, hooksConfig, agentDir, sessionId));
}
// Build model options from manifest constraints
const modelOptions: Record<string, any> = {};
if (manifest.model.constraints) {
const c = manifest.model.constraints;
if (c.temperature !== undefined) modelOptions.temperature = c.temperature;
if (c.max_tokens !== undefined) modelOptions.maxTokens = c.max_tokens;
if (c.top_p !== undefined) modelOptions.topP = c.top_p;
if (c.top_k !== undefined) modelOptions.topK = c.top_k;
if (c.stop_sequences !== undefined) modelOptions.stopSequences = c.stop_sequences;
}
const agent = new Agent({
initialState: {
systemPrompt,
model: loaded.model,
tools,
...modelOptions,
},
});
agent.subscribe((event) => handleEvent(event, hooksConfig, agentDir, sessionId, auditLogger));
console.log(bold(`${manifest.name} v${manifest.version}`));
console.log(dim(`Model: ${loaded.model.provider}:${loaded.model.id}`));
const allToolNames = tools.map((t) => t.name);
console.log(dim(`Tools: ${allToolNames.join(", ")}`));
if (skills.length > 0) {
console.log(dim(`Skills: ${skills.map((s) => s.name).join(", ")}`));
}
if (loaded.workflows.length > 0) {
console.log(dim(`Workflows: ${loaded.workflows.map((w) => w.name).join(", ")}`));
}
if (loaded.subAgents.length > 0) {
console.log(dim(`Agents: ${loaded.subAgents.map((a) => a.name).join(", ")}`));
}
if (loaded.plugins.length > 0) {
console.log(dim(`Plugins: ${loaded.plugins.map((p) => p.manifest.id).join(", ")}`));
}
console.log(dim('Type /skills to list skills, /plugins to list plugins, /memory to view memory, /quit to exit\n'));
// Single-shot mode
if (prompt) {
try {
await agent.prompt(prompt);
} catch (err: any) {
auditLogger?.logError(err.message).catch(() => {});
// Fire on_error hooks
if (hooksConfig?.hooks.on_error) {
runHooks(hooksConfig.hooks.on_error, agentDir, {
event: "on_error",
session_id: sessionId,
error: err.message,
}).catch(() => {});
}
throw err;
} finally {
if (localSession) {
console.log(dim("Finalizing session..."));
localSession.finalize();
}
if (sandboxCtx) {
console.log(dim("Stopping sandbox..."));
await sandboxCtx.gitMachine.stop();
}
}
return;
}
// REPL mode
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
const ask = (): void => {
rl.question(green("→ "), async (input) => {
const trimmed = input.trim();
if (!trimmed) {
ask();
return;
}
if (trimmed === "/quit" || trimmed === "/exit") {
rl.close();
if (localSession) {
console.log(dim("Finalizing session..."));
localSession.finalize();
}
await stopSandbox();
process.exit(0);
}
if (trimmed === "/memory") {
try {
const mem = await readFile(join(dir, "memory/MEMORY.md"), "utf-8");
console.log(dim("--- memory ---"));
console.log(mem.trim() || "(empty)");
console.log(dim("--- end ---"));
} catch {
console.log(dim("(no memory file)"));
}
ask();
return;
}
if (trimmed === "/skills") {
// Refresh skills to pick up any newly learned ones
const currentSkills = await refreshSkills(dir);
if (currentSkills.length === 0) {
console.log(dim("No skills installed."));
} else {
for (const s of currentSkills) {
const conf = s.confidence !== undefined ? dim(` [confidence: ${s.confidence}]`) : "";
console.log(` ${bold(s.name)} — ${dim(s.description)}${conf}`);
}
}
ask();
return;
}
if (trimmed === "/tasks") {
try {
const tasksRaw = await readFile(join(gitagentDir, "learning", "tasks.json"), "utf-8");
const tasksData = JSON.parse(tasksRaw);
const active = (tasksData.tasks || []).filter((t: any) => t.status === "active");
if (active.length === 0) {
console.log(dim("No active tasks."));
} else {
for (const t of active) {
console.log(` ${bold(t.id.slice(0, 8))} — ${t.objective} (${t.steps.length} steps, attempt #${t.attempts})`);
}
}
} catch {
console.log(dim("No tasks recorded yet."));
}
ask();
return;
}
if (trimmed === "/learned") {
const currentSkills = await refreshSkills(dir);
const learned = currentSkills.filter((s) => s.confidence !== undefined);
if (learned.length === 0) {
console.log(dim("No learned skills yet."));
} else {
for (const s of learned) {
const usage = s.usage_count ?? 0;
const ratio = `${s.success_count ?? 0}/${(s.success_count ?? 0) + (s.failure_count ?? 0)}`;
console.log(` ${bold(s.name)} — confidence: ${s.confidence}, usage: ${usage}, success: ${ratio}`);
}
}
ask();
return;
}
if (trimmed === "/plugins") {
if (loaded.plugins.length === 0) {
console.log(dim("No plugins loaded."));
} else {
for (const p of loaded.plugins) {
const toolCount = p.tools.length + p.programmaticTools.length;
const info = [
toolCount > 0 ? `${toolCount} tools` : null,
p.skills.length > 0 ? `${p.skills.length} skills` : null,
p.hooks ? "hooks" : null,
p.promptAddition ? "prompt" : null,
].filter(Boolean).join(", ");
console.log(` ${bold(p.manifest.id)} v${p.manifest.version} — ${dim(p.manifest.description)}`);
if (info) console.log(` ${dim(`provides: ${info}`)}`);
}
}
ask();
return;
}
// Skill expansion: /skill:name [args]
let promptText = trimmed;
if (trimmed.startsWith("/skill:")) {
const result = await expandSkillCommand(trimmed, skills);
if (result) {
console.log(dim(`▶ loading skill: ${result.skillName}`));
promptText = result.expanded;
} else {
const requested = trimmed.match(/^\/skill:([a-z0-9-]*)/)?.[1] || "?";
console.error(red(`Unknown skill: ${requested}`));
ask();
return;
}
}
try {
await agent.prompt(promptText);
} catch (err: any) {
console.error(red(`Error: ${err.message}`));
auditLogger?.logError(err.message).catch(() => {});
// Fire on_error hooks
if (hooksConfig?.hooks.on_error) {
runHooks(hooksConfig.hooks.on_error, agentDir, {
event: "on_error",
session_id: sessionId,
error: err.message,
}).catch(() => {});
}
}
ask();
});
};
// Sandbox cleanup helper
const stopSandbox = async () => {
if (sandboxCtx) {
console.log(dim("Stopping sandbox..."));
await sandboxCtx.gitMachine.stop();
}
};
// Handle Ctrl+C during streaming
rl.on("SIGINT", () => {
if (agent.state.isStreaming) {
agent.abort();
} else {
console.log("\nBye!");
rl.close();
if (localSession) {
try { localSession.finalize(); } catch { /* best-effort */ }
}
stopSandbox().finally(() => process.exit(0));
}
});
ask();
}
main().catch((err) => {
console.error(red(`Fatal: ${err.message}`));
process.exit(1);
});