( rawState: ExtendedState, activeAppTasks: ComputedRef<AppTask[]>, )
| 20 | } |
| 21 | |
| 22 | export function useTaskPoller( |
| 23 | rawState: ExtendedState, |
| 24 | activeAppTasks: ComputedRef<AppTask[]>, |
| 25 | ): UseTaskPoller { |
| 26 | let taskOutputPollTimer: ReturnType<typeof setInterval> | null = null; |
| 27 | let lastPolledSessionId: string | undefined; |
| 28 | const fetchedTerminalTaskOutputIds = new Set<string>(); |
| 29 | |
| 30 | async function loadTasksForSession(sessionId: string): Promise<void> { |
| 31 | try { |
| 32 | const api = getKimiWebApi(); |
| 33 | const taskList = await api.listTasks(sessionId); |
| 34 | rawState.tasksBySession = { |
| 35 | ...rawState.tasksBySession, |
| 36 | // Keep WS-delivered swarm subagents that REST /tasks omits (see keepLiveSubagents). |
| 37 | [sessionId]: keepLiveSubagents(taskList, rawState.tasksBySession[sessionId] ?? []), |
| 38 | }; |
| 39 | // Completed tasks may have real terminal output that never streamed over |
| 40 | // WS. Fetch it once now so the rows are expandable when the session opens. |
| 41 | await fetchTerminalTaskOutputs(sessionId, taskList); |
| 42 | } catch { |
| 43 | // Tasks are side data; old/stale sessions may fail without blocking messages. |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Fetch the final output snapshot for terminal tasks that lack real streamed |
| 49 | * outputLines. Called once after loading the task list so already-completed |
| 50 | * tasks are clickable immediately. |
| 51 | */ |
| 52 | async function fetchTerminalTaskOutputs( |
| 53 | sessionId: string, |
| 54 | taskList?: AppTask[], |
| 55 | ): Promise<void> { |
| 56 | if (rawState.activeSessionId !== sessionId) return; |
| 57 | |
| 58 | const tasks = taskList ?? rawState.tasksBySession[sessionId] ?? []; |
| 59 | const api = getKimiWebApi(); |
| 60 | const outputByTaskId = new Map<string, { preview: string; bytes?: number }>(); |
| 61 | |
| 62 | await Promise.all( |
| 63 | tasks.map(async (task) => { |
| 64 | const isTerminal = |
| 65 | task.status === 'completed' || task.status === 'failed' || task.status === 'cancelled'; |
| 66 | if (!isTerminal) return; |
| 67 | if (fetchedTerminalTaskOutputIds.has(task.id)) return; |
| 68 | if ((task.outputLines?.length ?? 0) > 0) return; |
| 69 | |
| 70 | try { |
| 71 | const withOutput = await api.getTask(sessionId, task.id, { |
| 72 | withOutput: true, |
| 73 | outputBytes: TASK_OUTPUT_FINAL_BYTES, |
| 74 | }); |
| 75 | if (withOutput.outputPreview !== undefined) { |
| 76 | outputByTaskId.set(task.id, { |
| 77 | preview: withOutput.outputPreview, |
| 78 | bytes: withOutput.outputBytes, |
| 79 | }); |
no test coverage detected