| | |
| | | let eventSource = null |
| | | const baseURL = import.meta.env.VITE_API_BASE_URL || '' |
| | | |
| | | // 统一的ID比较,避免数字/字符串不一致导致SSE更新被忽略 |
| | | const isSameId = (a, b) => { |
| | | if (a == null || b == null) return false |
| | | return String(a) === String(b) |
| | | } |
| | | |
| | | const fetchTasks = async () => { |
| | | try { |
| | | loading.value = true |
| | |
| | | // 监听步骤更新 |
| | | eventSource.addEventListener('stepUpdate', (event) => { |
| | | try { |
| | | const data = JSON.parse(event.data) |
| | | const raw = JSON.parse(event.data) |
| | | // 后端全量监听时的数据结构为 { taskId, step: { ... } },需要展开 step |
| | | const data = raw?.step ? { ...raw.step, taskId: raw.taskId || raw.step.taskId } : raw |
| | | // 如果数据中包含 taskId,检查是否匹配当前查看的任务 |
| | | if (data.taskId && data.taskId === currentTaskId.value) { |
| | | if (data.taskId && isSameId(data.taskId, currentTaskId.value)) { |
| | | updateStepFromSSE(data) |
| | | } else if (!data.taskId) { |
| | | // 如果没有 taskId,可能是步骤数据直接传递 |
| | |
| | | return |
| | | } |
| | | |
| | | const taskIndex = tasks.value.findIndex(t => t.taskId === data.taskId) |
| | | const taskIndex = tasks.value.findIndex(t => isSameId(t.taskId, data.taskId)) |
| | | if (taskIndex >= 0) { |
| | | // 更新任务 - 保留原有字段,只更新SSE传来的字段 |
| | | const existingTask = tasks.value[taskIndex] |
| | |
| | | if (!data) return |
| | | |
| | | // 如果数据中包含 taskId,检查是否匹配当前查看的任务 |
| | | if (data.taskId && data.taskId !== currentTaskId.value) { |
| | | if (data.taskId && !isSameId(data.taskId, currentTaskId.value)) { |
| | | return |
| | | } |
| | | |