| 94 | } |
| 95 | |
| 96 | export function createMockMessageGenerator(onMessage: (msg: WSMessage) => void): () => void { |
| 97 | const intervals: number[] = []; |
| 98 | const agents = [...INITIAL_AGENTS]; |
| 99 | const agentMap = new Map(agents.map((a) => [a.id, { ...a }])); |
| 100 | |
| 101 | const emitAgentActivity = () => { |
| 102 | const agent = pick(agents); |
| 103 | const state = agentMap.get(agent.id)!; |
| 104 | const layerStages = LAYER_META[agent.layer].stages; |
| 105 | const roll = Math.random(); |
| 106 | |
| 107 | if (roll < 0.5 && state.status !== 'working') { |
| 108 | const stage = pick(layerStages); |
| 109 | state.status = 'working'; |
| 110 | state.currentStage = stage; |
| 111 | state.currentTask = pick(TASK_DETAILS[stage] || ['处理中...']); |
| 112 | state.stageProgress[stage] = 'running'; |
| 113 | } else if (roll < 0.75 && state.currentStage) { |
| 114 | state.stageProgress[state.currentStage] = 'completed'; |
| 115 | state.status = 'done'; |
| 116 | state.currentTask = ''; |
| 117 | |
| 118 | const repo = stageToRepo(state.currentStage); |
| 119 | if (repo) { |
| 120 | const outputs = STAGE_META[state.currentStage].outputs; |
| 121 | const file = pick(outputs); |
| 122 | const artifact: Artifact = { |
| 123 | id: uid(), |
| 124 | repoId: repo, |
| 125 | projectId: agent.runId, |
| 126 | filename: file, |
| 127 | producedBy: agent.name, |
| 128 | timestamp: Date.now(), |
| 129 | size: `${(Math.random() * 100 + 1).toFixed(1)} KB`, |
| 130 | status: 'fresh', |
| 131 | }; |
| 132 | onMessage({ type: 'artifact_produced', payload: artifact }); |
| 133 | } |
| 134 | |
| 135 | const nextStage = state.currentStage; |
| 136 | state.currentStage = null; |
| 137 | onMessage({ |
| 138 | type: 'stage_update', |
| 139 | payload: { agentId: agent.id, stage: nextStage, status: 'completed' }, |
| 140 | }); |
| 141 | } else if (roll < 0.85 && state.currentStage) { |
| 142 | state.stageProgress[state.currentStage] = 'failed'; |
| 143 | state.status = 'error'; |
| 144 | state.currentTask = '执行失败,等待重试...'; |
| 145 | } else { |
| 146 | state.status = 'idle'; |
| 147 | state.currentStage = null; |
| 148 | state.currentTask = ''; |
| 149 | } |
| 150 | |
| 151 | agentMap.set(agent.id, { ...state }); |
| 152 | onMessage({ type: 'agent_update', payload: { ...state } }); |
| 153 | |