* 示例2: 直接使用组件管理器
()
| 61 | * 示例2: 直接使用组件管理器 |
| 62 | */ |
| 63 | async function demonstrateComponentManager() { |
| 64 | console.log('=== 示例2: 直接使用组件管理器 ==='); |
| 65 | |
| 66 | try { |
| 67 | // 创建独立的组件管理器 |
| 68 | const componentManager = new ComponentManager({ |
| 69 | debug: true, |
| 70 | autoInit: true, |
| 71 | }); |
| 72 | |
| 73 | // 创建并注册工具组件 |
| 74 | const toolManager = await createToolManager(); |
| 75 | const toolComponent = new ToolComponent('tools', { |
| 76 | debug: true, |
| 77 | includeBuiltinTools: true, |
| 78 | }); |
| 79 | |
| 80 | await componentManager.registerComponent(toolComponent); |
| 81 | |
| 82 | // 初始化组件管理器 |
| 83 | await componentManager.init(); |
| 84 | |
| 85 | // 获取组件状态 |
| 86 | console.log('组件管理器状态:', componentManager.getStatus()); |
| 87 | |
| 88 | // 获取工具组件并使用 |
| 89 | const tools = componentManager.getComponent<ToolComponent>('tools'); |
| 90 | if (tools) { |
| 91 | const availableTools = tools.getTools(); |
| 92 | console.log(`可用工具数量: ${availableTools.length}`); |
| 93 | console.log( |
| 94 | '前3个工具:', |
| 95 | availableTools.slice(0, 3).map(t => t.name) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | // 检查健康状态 |
| 100 | const health = await componentManager.getHealthStatus(); |
| 101 | console.log('健康状态:', health.healthy ? '良好' : '异常'); |
| 102 | |
| 103 | // 销毁 |
| 104 | await componentManager.destroy(); |
| 105 | console.log('组件管理器已销毁\n'); |
| 106 | } catch (error) { |
| 107 | console.error('组件管理器示例失败:', error); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * 示例3: 使用完整的 Agent(推荐方式) |
no test coverage detected