fix
This commit is contained in:
@@ -399,8 +399,8 @@
|
||||
// 初始化阶段
|
||||
this.initStages();
|
||||
|
||||
// 连接 SSE
|
||||
this.connectSSE(data.task_id);
|
||||
// 开始轮询任务事件
|
||||
this.connectPolling(data.task_id);
|
||||
|
||||
} catch (error) {
|
||||
console.error('启动失败:', error);
|
||||
@@ -423,88 +423,118 @@
|
||||
},
|
||||
|
||||
/**
|
||||
* 连接 SSE
|
||||
* 轮询任务事件(替代 SSE)
|
||||
*/
|
||||
connectSSE(taskId) {
|
||||
const url = `/api/v1/sdlc/stream/${taskId}`;
|
||||
|
||||
connectPolling(taskId) {
|
||||
this.connectionStatus = 'connecting';
|
||||
this.addLog('system', 'SSE', `连接到:${url}`);
|
||||
this.addLog('system', 'POLL', `开始轮询任务:${taskId}`);
|
||||
|
||||
this.eventSource = new EventSource(url);
|
||||
let lastIndex = 0;
|
||||
let pollCount = 0;
|
||||
const maxPolls = 600; // 最多轮询 600 次 (10 分钟)
|
||||
|
||||
// 连接成功
|
||||
this.eventSource.onopen = () => {
|
||||
this.connectionStatus = 'connected';
|
||||
this.addLog('system', 'SSE', '连接成功');
|
||||
const poll = () => {
|
||||
if (pollCount >= maxPolls) {
|
||||
this.addLog('system', 'POLL', '轮询超时');
|
||||
this.isProcessing = false;
|
||||
this.connectionStatus = 'disconnected';
|
||||
return;
|
||||
}
|
||||
|
||||
fetch(`/api/v1/sdlc/poll/${taskId}?last_index=${lastIndex}`)
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
const { events, has_more, status } = data;
|
||||
|
||||
// 处理新事件
|
||||
events.forEach(event => {
|
||||
lastIndex++;
|
||||
this.handleEvent(event);
|
||||
});
|
||||
|
||||
// 检查是否继续轮询
|
||||
if (status === 'completed' || status === 'failed') {
|
||||
this.isProcessing = false;
|
||||
this.connectionStatus = 'disconnected';
|
||||
this.addLog('system', 'POLL', `任务完成,状态:${status}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (has_more || events.length > 0) {
|
||||
pollCount++;
|
||||
setTimeout(poll, 500); // 每 500ms 轮询一次
|
||||
} else if (status === 'processing') {
|
||||
pollCount++;
|
||||
setTimeout(poll, 1000); // 无新事件时 1 秒后再试
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('轮询失败:', err);
|
||||
this.addLog('error', 'POLL', err.message);
|
||||
pollCount++;
|
||||
setTimeout(poll, 2000);
|
||||
});
|
||||
};
|
||||
|
||||
// PM 阶段
|
||||
this.eventSource.addEventListener('pm_start', (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.updateStageStatus('pm', 'processing');
|
||||
this.addLog('pm_start', 'PM Agent', '开始需求分析...');
|
||||
});
|
||||
// 开始轮询
|
||||
setTimeout(poll, 500);
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理单个事件
|
||||
*/
|
||||
handleEvent(event) {
|
||||
const eventType = event.event;
|
||||
const data = event.data;
|
||||
|
||||
this.eventSource.addEventListener('pm_complete', (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.updateStageStatus('pm', 'completed');
|
||||
this.addLog('pm_complete', 'PM Agent', '需求分析完成');
|
||||
this.addResult('📋 软件需求规格说明书 (SRS)', data.content, data.timestamp);
|
||||
});
|
||||
|
||||
// QA 阶段
|
||||
this.eventSource.addEventListener('qa_start', (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.updateStageStatus('qa', 'processing');
|
||||
this.addLog('qa_start', 'QA Agent', '开始测试用例设计...');
|
||||
});
|
||||
|
||||
this.eventSource.addEventListener('qa_complete', (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.updateStageStatus('qa', 'completed');
|
||||
this.addLog('qa_complete', 'QA Agent', '测试用例设计完成');
|
||||
this.addResult('🧪 测试方案与用例', data.content, data.timestamp);
|
||||
});
|
||||
|
||||
// Dev 阶段
|
||||
this.eventSource.addEventListener('dev_start', (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.updateStageStatus('dev', 'processing');
|
||||
this.addLog('dev_start', 'Dev Agent', '开始代码实现...');
|
||||
});
|
||||
|
||||
this.eventSource.addEventListener('dev_complete', (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.updateStageStatus('dev', 'completed');
|
||||
this.addLog('dev_complete', 'Dev Agent', '代码实现完成');
|
||||
this.addResult('💻 代码实现', data.content, data.timestamp);
|
||||
});
|
||||
|
||||
// 最终结果
|
||||
this.eventSource.addEventListener('final_result', (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.updateStageStatus('final', 'completed');
|
||||
this.addLog('final_result', 'System', 'SDLC 流程完成');
|
||||
this.isProcessing = false;
|
||||
this.connectionStatus = 'disconnected';
|
||||
});
|
||||
|
||||
// 错误处理
|
||||
this.eventSource.addEventListener('error', (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
this.addLog('error', 'Error', data.error || '未知错误');
|
||||
this.isProcessing = false;
|
||||
this.connectionStatus = 'disconnected';
|
||||
alert(`执行错误:${data.error}`);
|
||||
});
|
||||
|
||||
// 连接错误
|
||||
this.eventSource.onerror = () => {
|
||||
this.addLog('system', 'SSE', '连接断开');
|
||||
this.connectionStatus = 'disconnected';
|
||||
this.eventSource.close();
|
||||
};
|
||||
switch(eventType) {
|
||||
case 'task_started':
|
||||
this.addLog('task_started', 'System', data.message || '任务已启动');
|
||||
break;
|
||||
|
||||
case 'pm_start':
|
||||
this.updateStageStatus('pm', 'processing');
|
||||
this.addLog('pm_start', 'PM Agent', '开始需求分析...');
|
||||
break;
|
||||
|
||||
case 'pm_complete':
|
||||
this.updateStageStatus('pm', 'completed');
|
||||
this.addLog('pm_complete', 'PM Agent', '需求分析完成');
|
||||
this.addResult('📋 软件需求规格说明书 (SRS)', data.content, data.timestamp);
|
||||
break;
|
||||
|
||||
case 'qa_start':
|
||||
this.updateStageStatus('qa', 'processing');
|
||||
this.addLog('qa_start', 'QA Agent', '开始测试用例设计...');
|
||||
break;
|
||||
|
||||
case 'qa_complete':
|
||||
this.updateStageStatus('qa', 'completed');
|
||||
this.addLog('qa_complete', 'QA Agent', '测试用例设计完成');
|
||||
this.addResult('🧪 测试方案与用例', data.content, data.timestamp);
|
||||
break;
|
||||
|
||||
case 'dev_start':
|
||||
this.updateStageStatus('dev', 'processing');
|
||||
this.addLog('dev_start', 'Dev Agent', '开始代码实现...');
|
||||
break;
|
||||
|
||||
case 'dev_complete':
|
||||
this.updateStageStatus('dev', 'completed');
|
||||
this.addLog('dev_complete', 'Dev Agent', '代码实现完成');
|
||||
this.addResult('💻 代码实现', data.content, data.timestamp);
|
||||
break;
|
||||
|
||||
case 'final_result':
|
||||
this.updateStageStatus('final', 'completed');
|
||||
this.addLog('final_result', 'System', 'SDLC 流程完成');
|
||||
break;
|
||||
|
||||
case 'error':
|
||||
this.addLog('error', 'Error', data.error || '未知错误');
|
||||
alert(`执行错误:${data.error}`);
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user