MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / useTaskPoller

Function useTaskPoller

apps/kimi-web/src/composables/client/useTaskPoller.ts:22–265  ·  view source on GitHub ↗
(
  rawState: ExtendedState,
  activeAppTasks: ComputedRef<AppTask[]>,
)

Source from the content-addressed store, hash-verified

20}
21
22export 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 });

Callers 1

Calls 2

startTaskOutputPollingFunction · 0.85
stopTaskOutputPollingFunction · 0.85

Tested by

no test coverage detected