* 示例4: 高级用法 - 自定义管理器配置
()
| 165 | * 示例4: 高级用法 - 自定义管理器配置 |
| 166 | */ |
| 167 | async function demonstrateAdvancedUsage() { |
| 168 | console.log('=== 示例4: 高级用法 - 自定义管理器配置 ==='); |
| 169 | |
| 170 | try { |
| 171 | // 创建带有自定义配置的 Agent |
| 172 | const agent = new Agent({ |
| 173 | debug: true, |
| 174 | llm: { |
| 175 | provider: 'qwen', |
| 176 | apiKey: process.env.QWEN_API_KEY || 'your-api-key', |
| 177 | }, |
| 178 | components: { |
| 179 | debug: true, |
| 180 | autoInit: false, // 禁用自动初始化 |
| 181 | }, |
| 182 | tools: { |
| 183 | enabled: true, |
| 184 | includeBuiltinTools: true, |
| 185 | excludeTools: ['git_status'], // 排除特定工具 |
| 186 | includeCategories: ['utility', 'text'], // 只包含特定类别 |
| 187 | }, |
| 188 | }); |
| 189 | |
| 190 | await agent.init(); |
| 191 | |
| 192 | // 手动管理组件 |
| 193 | const componentManager = agent.getComponentManager(); |
| 194 | |
| 195 | // 监听组件事件 |
| 196 | componentManager.on('componentRegistered', event => { |
| 197 | console.log(`组件已注册: ${event.id}`); |
| 198 | }); |
| 199 | |
| 200 | componentManager.on('componentInitialized', event => { |
| 201 | console.log(`组件已初始化: ${event.id}`); |
| 202 | }); |
| 203 | |
| 204 | // 运行时添加新组件 |
| 205 | const customComponent = new ToolComponent('custom-tools', { |
| 206 | debug: true, |
| 207 | includeBuiltinTools: false, |
| 208 | }); |
| 209 | |
| 210 | await componentManager.registerComponent(customComponent); |
| 211 | |
| 212 | // 重启特定组件 |
| 213 | const restartResult = await componentManager.restartComponent('tools'); |
| 214 | console.log('组件重启结果:', restartResult); |
| 215 | |
| 216 | // 获取详细状态信息 |
| 217 | const detailedStatus = agent.getStatus(); |
| 218 | console.log('详细状态:', JSON.stringify(detailedStatus, null, 2)); |
| 219 | |
| 220 | // 批量操作 |
| 221 | const componentIds = componentManager.getComponentIds(); |
| 222 | console.log('所有组件ID:', componentIds); |
| 223 | |
| 224 | // 按类型搜索组件 |
no test coverage detected