feat(ui): 添加子 Agent 进度显示功能
当 build agent 调用 guide/explore 等子 agent 时, 用户可以在 web 页面实时看到子 agent 的执行进度 实现方案: - Core: 使用 EventEmitter 模式发射子 agent 事件 - Server: 订阅事件并转发到 WebSocket - UI: 处理事件并渲染 SubagentProgress 组件 新增文件: - packages/core/src/agent/events.ts - packages/ui/src/components/SubagentProgress.tsx 修改文件: - core: executor.ts, manager.ts, types.ts, task.ts - server: adapter.ts, types.ts - ui: useChat.ts, types.ts - web: Chat.tsx
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
import type { ToolWithMetadata } from '../types.js';
|
||||
import type { AgentConfig } from '../../types/index.js';
|
||||
import type { ImageData } from '../../agent/types.js';
|
||||
import { agentRegistry, AgentExecutor } from '../../agent/index.js';
|
||||
import { agentRegistry, AgentExecutor, agentEventEmitter } from '../../agent/index.js';
|
||||
import { toolRegistry } from '../registry.js';
|
||||
import { SessionManager } from '../../session/index.js';
|
||||
import { getAgentManager } from '../../agent/manager.js';
|
||||
import { loadVisionConfig } from '../../utils/config.js';
|
||||
|
||||
/**
|
||||
* 生成短 ID(8 字符)
|
||||
*/
|
||||
function generateShortId(): string {
|
||||
return Math.random().toString(36).slice(2, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* 模型预设映射
|
||||
*/
|
||||
@@ -231,15 +238,41 @@ export const taskTool: ToolWithMetadata = {
|
||||
`${description} (@${agent.name})`
|
||||
);
|
||||
|
||||
// 生成子 Agent 实例 ID
|
||||
const agentId = generateShortId();
|
||||
const startTime = Date.now();
|
||||
|
||||
// 发射子 Agent 开始事件
|
||||
agentEventEmitter.emit({
|
||||
type: 'subagent:start',
|
||||
sessionId: parentSessionId,
|
||||
agentId,
|
||||
agentName: agent.name,
|
||||
description,
|
||||
});
|
||||
|
||||
// 创建执行器
|
||||
const executor = new AgentExecutor(agent, effectiveConfig, toolRegistry);
|
||||
|
||||
// 执行任务
|
||||
// 执行任务(启用事件发射)
|
||||
const result = await executor.execute(prompt, {
|
||||
parentSessionId,
|
||||
workdir: process.cwd(),
|
||||
images,
|
||||
onStream: undefined, // 子任务不使用流式输出
|
||||
sessionId: parentSessionId,
|
||||
agentId,
|
||||
emitEvents: true, // 启用子 Agent 事件发射
|
||||
});
|
||||
|
||||
// 发射子 Agent 结束事件
|
||||
agentEventEmitter.emit({
|
||||
type: 'subagent:end',
|
||||
sessionId: parentSessionId,
|
||||
agentId,
|
||||
agentName: agent.name,
|
||||
success: result.success,
|
||||
duration: Date.now() - startTime,
|
||||
error: result.error,
|
||||
});
|
||||
|
||||
// 保存子会话
|
||||
@@ -255,6 +288,7 @@ export const taskTool: ToolWithMetadata = {
|
||||
output: result.text,
|
||||
metadata: {
|
||||
agent: agent.name,
|
||||
agentId,
|
||||
sessionId: childSession.id,
|
||||
steps: result.steps,
|
||||
mode: 'sync',
|
||||
@@ -267,6 +301,7 @@ export const taskTool: ToolWithMetadata = {
|
||||
error: result.error || '子任务执行失败',
|
||||
metadata: {
|
||||
agent: agent.name,
|
||||
agentId,
|
||||
sessionId: childSession.id,
|
||||
steps: result.steps,
|
||||
mode: 'sync',
|
||||
|
||||
Reference in New Issue
Block a user