feat: 添加 LSP 代码诊断功能

- 实现 LSP 客户端,支持与语言服务器通信
- 支持多种语言: TypeScript, Python, Go, Rust, C/C++ 等
- write_file/edit_file 工具集成 LSP 诊断,写入后自动检查代码错误
- 添加 CLI 命令管理语言服务器 (ai-assist lsp list/install/info)
- 智能等待机制:首次启动 LSP 等待 2s,后续仅需 300ms
- 将 read_file/write_file/edit_file 设为核心工具,确保文件操作使用正确的工具
- 更新系统提示词,引导 AI 使用文件工具而非 bash 命令
This commit is contained in:
2025-12-11 00:01:58 +08:00
parent 1e0ecc2de7
commit 929f6f7850
12 changed files with 1430 additions and 5 deletions
+48
View File
@@ -7,6 +7,13 @@ import { loadConfig, initConfig } from './utils/config.js';
import { toolRegistry, todoManager } from './tools/index.js';
import { getPermissionManager, promptPermission } from './permission/index.js';
import { SessionManager } from './session/index.js';
import { initLSP, shutdownLSP } from './lsp/index.js';
import {
printServerList,
installServer,
installAllServers,
showServerInfo,
} from './lsp/cli.js';
const program = new Command();
@@ -23,6 +30,43 @@ program
await initConfig();
});
// LSP 命令组
const lspCommand = program
.command('lsp')
.description('语言服务器管理');
lspCommand
.command('list')
.description('列出所有语言服务器及其安装状态')
.action(() => {
printServerList();
});
lspCommand
.command('install [servers...]')
.description('安装指定的语言服务器')
.option('-a, --all', '安装所有语言服务器')
.action(async (servers: string[], options: { all?: boolean }) => {
if (options.all) {
await installAllServers();
} else if (servers.length === 0) {
console.log('用法: ai-assist lsp install <server> [server2] ...');
console.log(' ai-assist lsp install --all');
console.log('\n运行 "ai-assist lsp list" 查看可用的服务器');
} else {
for (const server of servers) {
await installServer(server);
}
}
});
lspCommand
.command('info <server>')
.description('显示语言服务器详细信息')
.action((server: string) => {
showServerInfo(server);
});
// 初始化权限系统
function setupPermissions(): void {
const permissionManager = getPermissionManager();
@@ -61,6 +105,9 @@ program.action(async () => {
const config = loadConfig();
const agent = new Agent(config);
// 初始化 LSP 系统
initLSP(process.cwd());
// 设置工具注册表(支持动态工具发现)
agent.setRegistry(toolRegistry);
@@ -84,6 +131,7 @@ program.action(async () => {
// 优雅退出
process.on('SIGINT', async () => {
console.log('\n\n👋 再见!');
await shutdownLSP();
await sessionManager.close();
ui.close();
process.exit(0);