-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcmd-agent.ts
More file actions
149 lines (127 loc) · 4.41 KB
/
cmd-agent.ts
File metadata and controls
149 lines (127 loc) · 4.41 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
/**
* Agent command - Interactive AI agent for codebase Q&A
*/
import { Command } from "commander";
import * as readline from "readline";
import { CLIAgent, type Provider } from "../clients/cli-agent.js";
import { MultiIndexRunner } from "../clients/multi-index-runner.js";
import { CompositeStoreReader, parseIndexSpecs } from "../stores/index.js";
import { buildClientUserAgent } from "../core/utils.js";
const PROVIDER_DEFAULTS: Record<Provider, string> = {
openai: "gpt-5-mini",
anthropic: "claude-haiku-4-5",
google: "gemini-3-flash-preview",
augment: "claude-sonnet-4-5",
};
export const agentCommand = new Command("agent")
.description("Interactive AI agent for codebase Q&A")
.requiredOption(
"-i, --index <specs...>",
"Index spec(s): name, path:/path, or s3://bucket/key"
)
.requiredOption(
"--provider <name>",
"LLM provider (openai, anthropic, google, augment)"
)
.option("--search-only", "Disable listFiles/readFile tools (search only)")
.option("--model <name>", "Model to use (defaults based on provider)")
.option("--max-steps <n>", "Maximum agent steps", (val) => parseInt(val, 10), 10)
.option("-v, --verbose", "Show tool calls")
.argument("[query]", "Initial query to ask")
.option("--print", "Non-interactive mode: print response and exit")
.action(async (query, options) => {
try {
// Validate provider
const provider = options.provider as Provider;
if (!["openai", "anthropic", "google", "augment"].includes(provider)) {
console.error(
`Unknown provider: ${provider}. Use: openai, anthropic, google, or augment`
);
process.exit(1);
}
// Get model (use provider default if not specified)
const model = options.model ?? PROVIDER_DEFAULTS[provider];
// Parse index specs and create composite store
const specs = parseIndexSpecs(options.index);
const store = await CompositeStoreReader.fromSpecs(specs);
// Create multi-index runner
// Build User-Agent for analytics tracking
const clientUserAgent = buildClientUserAgent("cli");
const runner = await MultiIndexRunner.create({
store,
searchOnly: options.searchOnly,
clientUserAgent,
});
console.log("\x1b[1;36mContext Connectors Minimal Agent\x1b[0m");
console.log();
// Display connected indexes
console.log(`\x1b[36mConnected to ${runner.indexes.length} index(es):\x1b[0m`);
for (const idx of runner.indexes) {
console.log(` - ${idx.name} (${idx.type}://${idx.identifier})`);
}
console.log(`\x1b[36mUsing: ${provider}/${model}\x1b[0m`);
console.log();
// Create and initialize agent with multi-index runner
const agent = new CLIAgent({
runner,
provider,
model,
maxSteps: options.maxSteps,
verbose: options.verbose,
clientUserAgent,
});
await agent.initialize();
// Non-interactive mode (--print)
if (options.print) {
if (!query) {
console.error("Error: query is required in non-interactive mode (--print)");
process.exit(1);
}
await agent.ask(query);
return;
}
// Interactive mode
// If initial query provided, ask it first
if (query) {
console.log();
await agent.ask(query);
console.log();
}
console.log("Ask questions about your codebase. Type 'exit' to quit.\n");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const prompt = () => {
rl.question("\x1b[32m> \x1b[0m", async (input) => {
const query = input.trim();
if (query.toLowerCase() === "exit" || query.toLowerCase() === "quit") {
rl.close();
return;
}
if (query.toLowerCase() === "reset") {
agent.reset();
console.log("Conversation reset.\n");
prompt();
return;
}
if (!query) {
prompt();
return;
}
try {
console.log();
await agent.ask(query);
console.log();
} catch (error) {
console.error("\x1b[31mError:\x1b[0m", error);
}
prompt();
});
};
prompt();
} catch (error) {
console.error("Agent failed:", error);
process.exit(1);
}
});