Initial commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { Command } from 'commander';
|
||||
import { Agent } from './core/agent.js';
|
||||
import { TerminalUI } from './ui/terminal.js';
|
||||
import { loadConfig, initConfig } from './utils/config.js';
|
||||
import {
|
||||
bashTool,
|
||||
readFileTool,
|
||||
writeFileTool,
|
||||
listDirTool,
|
||||
searchFilesTool,
|
||||
} from './tools/index.js';
|
||||
|
||||
const program = new Command();
|
||||
|
||||
program
|
||||
.name('ai-assist')
|
||||
.description('AI Terminal Assistant - 终端中的 AI 编程助手')
|
||||
.version('1.0.0');
|
||||
|
||||
// 初始化命令
|
||||
program
|
||||
.command('init')
|
||||
.description('初始化配置(设置 API Key 等)')
|
||||
.action(async () => {
|
||||
await initConfig();
|
||||
});
|
||||
|
||||
// 单次查询命令
|
||||
program
|
||||
.command('ask <question>')
|
||||
.description('单次提问(不进入交互模式)')
|
||||
.action(async (question: string) => {
|
||||
const config = loadConfig();
|
||||
const agent = new Agent(config);
|
||||
|
||||
// 注册工具
|
||||
agent.registerTool(bashTool);
|
||||
agent.registerTool(readFileTool);
|
||||
agent.registerTool(writeFileTool);
|
||||
agent.registerTool(listDirTool);
|
||||
agent.registerTool(searchFilesTool);
|
||||
|
||||
try {
|
||||
const response = await agent.chat(question, (text) => {
|
||||
process.stdout.write(text);
|
||||
});
|
||||
console.log('');
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'错误:',
|
||||
error instanceof Error ? error.message : String(error)
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
// 默认:交互模式
|
||||
program.action(async () => {
|
||||
const config = loadConfig();
|
||||
const agent = new Agent(config);
|
||||
|
||||
// 注册所有工具
|
||||
agent.registerTool(bashTool);
|
||||
agent.registerTool(readFileTool);
|
||||
agent.registerTool(writeFileTool);
|
||||
agent.registerTool(listDirTool);
|
||||
agent.registerTool(searchFilesTool);
|
||||
|
||||
// 启动终端 UI
|
||||
const ui = new TerminalUI(agent);
|
||||
|
||||
// 优雅退出
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n\n👋 再见!');
|
||||
ui.close();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
await ui.start();
|
||||
});
|
||||
|
||||
program.parse();
|
||||
Reference in New Issue
Block a user