# agument-code **Repository Path**: meepo_vip/agument-code ## Basic Information - **Project Name**: agument-code - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-13 - **Last Updated**: 2025-08-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Augment Code 核心业务逻辑还原 ## 项目概述 本项目基于对 `augment-code/out/extension.js` 压缩代码的分析,还原了 Augment Code 扩展的核心业务逻辑,并集成了完整的工具系统。 ## 核心架构 ### 1. 工具系统 (`libs/tools`) 工具系统是整个扩展的核心,提供了模块化、可扩展的工具执行框架: - **ToolRegistry**: 工具注册表,管理工具的注册、查找、启用/禁用 - **ToolExecutionEngine**: 工具执行引擎,负责工具的验证、执行、超时控制 - **ToolManager**: 工具管理器,统一管理工具注册和执行 - **ToolHandler**: 工具处理器接口,每个工具的具体实现 ### 2. 核心扩展类 (`src/core-extension.ts`) `AugmentCoreExtension` 是主要的扩展类,实现了 `ModelProvider` 接口: - 集成 Anthropic API 和工具系统 - 处理工具调用和结果格式化 - 管理请求生命周期和取消机制 - 提供完整的错误处理和日志记录 ### 3. 工具执行协调器 (`src/tool-execution-coordinator.ts`) 提供高级的工具执行流程控制: - **ToolExecutionCoordinator**: 管理工具执行队列、优先级、并发控制 - **ToolExecutionFlowManager**: 支持工具链和并行执行 ### 4. 错误处理和日志系统 (`src/error-handling.ts`) 完善的错误处理机制: - 自定义错误类型和错误码 - 结构化日志记录 - 错误回调和监听机制 ## 已实现的工具 ### 文件操作工具 - `read-file`: 读取文件内容 - `save-file`: 保存文件内容 - `str-replace-editor`: 编辑文件(字符串替换) ### 进程管理工具 - `launch-process`: 启动系统进程 - `kill-process`: 终止进程 - `read-process`: 读取进程输出 - `write-process`: 向进程写入输入 - `list-processes`: 列出所有进程 ### 记忆管理工具 (新增) - `remember`: 创建记忆项 - `memory-retrieval`: 检索记忆 ## 快速开始 ### 1. 安装依赖 ```bash npm install ``` ### 2. 初始化扩展 ```typescript import { AugmentCoreExtension } from './src/core-extension'; import { Logger, LogLevel } from './src/error-handling'; // 创建日志记录器 const logger = new Logger(LogLevel.INFO); // 创建扩展实例 const extension = new AugmentCoreExtension( 'your-anthropic-api-key', 'claude-3-5-sonnet-20241022', logger ); // 初始化 await extension.initialize(); ``` ### 3. 使用工具 ```typescript // 检查可用工具 const tools = extension.getAvailableTools(); console.log('Available tools:', tools); // 执行工具(通过 generate 方法) const request = { requestMessage: 'Please read the file /path/to/file.txt', requestId: 'req-123' }; for await (const node of extension.generate(request)) { console.log('Response node:', node); } ``` ### 4. 工具链执行 ```typescript import { ToolExecutionFlowManager } from './src/tool-execution-coordinator'; const flowManager = new ToolExecutionFlowManager(extension.toolManager, logger); // 执行工具链 const tools = [ { name: 'read-file', parameters: { path: '/input.txt' } }, { name: 'save-file', parameters: { path: '/output.txt', content: 'processed' } } ]; const context = { workspaceRoot: process.cwd(), environment: process.env, timestamp: new Date(), permissions: ['file:read', 'file:write'], logger }; const results = await flowManager.executeToolChain(tools, context); ``` ## 配置 ### 工具配置 可以通过 `ToolManager` 配置工具: ```typescript // 启用/禁用工具 extension.setToolEnabled('launch-process', false); // 检查工具状态 const isAvailable = extension.isToolAvailable('read-file'); // 获取工具统计 const stats = extension.getToolStats(); ``` ### 日志配置 ```typescript import { Logger, LogLevel } from './src/error-handling'; const logger = new Logger(LogLevel.DEBUG); // 添加自定义日志回调 logger.addLogCallback((entry) => { // 自定义日志处理 console.log(`[${entry.level}] ${entry.message}`); }); ``` ### 错误处理配置 ```typescript import { ErrorHandler, ErrorCode } from './src/error-handling'; const errorHandler = new ErrorHandler(logger); // 注册错误回调 errorHandler.onError(ErrorCode.TOOL_EXECUTION_FAILED, (error) => { console.error('Tool execution failed:', error); }); ``` ## 测试 项目包含完整的测试套件: ```bash # 运行所有测试 npm test # 运行特定测试 npm test -- core-extension.test.ts npm test -- memory-management.test.ts npm test -- integration.test.ts ``` ### 测试覆盖 - **单元测试**: 测试各个组件的独立功能 - **集成测试**: 测试组件间的交互和完整工作流程 - **错误处理测试**: 验证错误场景的处理 ## 扩展开发 ### 添加新工具 1. 在 `libs/tools/src/core/tool-definitions.ts` 中定义工具: ```typescript export const MY_TOOLS: Record = { 'my-tool': { name: 'my-tool', description: 'My custom tool', category: ToolCategory.CUSTOM, parameters: [ { name: 'input', type: 'string', required: true, description: 'Input parameter' } ], timeout: 5000, isAsync: true } }; ``` 2. 实现工具处理器: ```typescript export class MyToolHandler implements ToolHandler { async execute(parameters: Record, context: ToolExecutionContext): Promise { // 实现工具逻辑 return { success: true, data: { result: 'success' } }; } validate(parameters: Record): ValidationResult { // 实现参数验证 return { isValid: true, errors: [] }; } } ``` 3. 在 `ToolManager` 中注册工具: ```typescript this.registry.register( 'my-tool', MY_TOOLS['my-tool'], new MyToolHandler() ); ``` ### 自定义错误处理 ```typescript import { AugmentError, ErrorCode } from './src/error-handling'; // 创建自定义错误 const customError = new AugmentError( ErrorCode.CUSTOM_ERROR, 'Custom error message', { details: 'additional info' }, { context: 'error context' } ); // 处理错误 const handledError = errorHandler.handleError(customError); ``` ## 性能优化 ### 并发控制 工具执行协调器支持并发控制: ```typescript const coordinator = new ToolExecutionCoordinator(toolManager, logger); // 设置最大并发数 coordinator.setMaxConcurrentExecutions(3); // 批量执行 const requests = [/* ... */]; const results = await coordinator.executeBatch(requests); ``` ### 内存管理 记忆管理工具使用文件存储,支持大量记忆: ```typescript // 记忆存储在 .augment-memories.json 文件中 // 支持搜索和分页 const memories = await memoryRetrievalHandler.execute({ query: 'search term', limit: 50 }, context); ``` ## 故障排除 ### 常见问题 1. **工具执行超时** - 检查网络连接 - 增加超时时间 - 检查工具参数 2. **文件访问权限** - 确保有足够的文件系统权限 - 检查工作目录设置 3. **API 密钥问题** - 验证 Anthropic API 密钥 - 检查 API 配额和限制 ### 调试 启用调试日志: ```typescript const logger = new Logger(LogLevel.DEBUG); ``` 查看工具执行统计: ```typescript const stats = extension.getToolStats(); console.log('Tool statistics:', stats); ``` ## 贡献 欢迎贡献代码!请遵循以下步骤: 1. Fork 项目 2. 创建功能分支 3. 编写测试 4. 提交代码 5. 创建 Pull Request ## 许可证 MIT License