File size: 11,216 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
 * Watches offloaded tool calls via ToolStream SSE (+ polling fallback).
 * Updates backgroundTasksStore with liveOutput and final status/result.
 */

import { message } from "antd";
import i18n from "../i18n";
import {
  extractOutputText,
  subscribeToolCallStream,
  toolCallsApi,
} from "../api/modules/toolCalls";
import { useBackgroundTasksStore } from "../stores/backgroundTasksStore";
import { resolveBackendSessionId } from "../utils/resolveBackendSessionId";

const POLL_INTERVAL_MS = 3000;
const LIVE_OUTPUT_MAX = 80_000;

type AbortFn = () => void;

const activeWatchers = new Map<string, AbortFn>();
const finalizedIds = new Set<string>();

function chunkToText(payload: unknown): string {
  if (!payload || typeof payload !== "object") return "";
  const p = payload as { data?: unknown; type?: string };
  const data = p.data;
  if (data == null) return "";
  if (typeof data === "string") return data;
  if (typeof data === "object") {
    const d = data as Record<string, unknown>;
    if (typeof d.text === "string") return d.text;
    if (typeof d.content === "string") return d.content;
    // ToolChunk / ToolResponse-like: content may be array of blocks
    if (Array.isArray(d.content)) {
      return d.content
        .map((b) =>
          b &&
          typeof b === "object" &&
          typeof (b as { text?: string }).text === "string"
            ? (b as { text: string }).text
            : "",
        )
        .filter(Boolean)
        .join("");
    }
    try {
      return JSON.stringify(data);
    } catch {
      return "";
    }
  }
  return String(data);
}

async function finalizeFromOutput(
  sessionId: string,
  toolCallId: string,
  fallbackLive: string,
  cancelled: boolean,
): Promise<void> {
  if (finalizedIds.has(toolCallId)) return;
  finalizedIds.add(toolCallId);

  const store = useBackgroundTasksStore.getState();
  let resultText = fallbackLive;
  let status: "done" | "cancelled" = cancelled ? "cancelled" : "done";

  try {
    const output = await toolCallsApi.getOutput(sessionId, toolCallId);
    const extracted = extractOutputText(output);
    if (extracted) resultText = extracted;
    if (
      output.final_state === "interrupted" ||
      output.final_state === "cancelled"
    ) {
      status = "cancelled";
    }
  } catch {
    // Cache miss / race — keep liveOutput
  }

  if (resultText.length > LIVE_OUTPUT_MAX) {
    resultText = resultText.slice(resultText.length - LIVE_OUTPUT_MAX);
  }

  const task = store.tasks.find((t) => t.toolCallId === toolCallId);
  // Skip toast if already terminal (e.g. user cancelled from panel)
  const alreadyTerminal =
    task?.status === "done" || task?.status === "cancelled";

  store.updateTask(toolCallId, {
    status,
    result: resultText || null,
  });

  if (alreadyTerminal) return;

  const toolName = task?.toolName || toolCallId;
  if (status === "cancelled") {
    message.info(
      i18n.t("tool.control.toast.bgCancelled", {
        tool: toolName,
        defaultValue: `Background tool cancelled: ${toolName}`,
      }),
    );
  } else {
    message.success(
      i18n.t("tool.control.toast.bgComplete", {
        tool: toolName,
        defaultValue: `Background tool complete: ${toolName}`,
      }),
    );
  }
}

function startPolling(sessionId: string, toolCallId: string): AbortFn {
  let stopped = false;
  const timer = setInterval(async () => {
    if (stopped) return;
    const finishPoll = async (cancelled: boolean) => {
      if (stopped) return;
      stopped = true;
      clearInterval(timer);
      const abort = activeWatchers.get(toolCallId);
      activeWatchers.delete(toolCallId);
      // Abort stream leg only; poll already stopped
      abort?.();
      const live =
        useBackgroundTasksStore
          .getState()
          .tasks.find((t) => t.toolCallId === toolCallId)?.liveOutput || "";
      await finalizeFromOutput(sessionId, toolCallId, live, cancelled);
    };
    try {
      const info = await toolCallsApi.getInfo(sessionId, toolCallId);
      if (info.status === "running" || info.status === "offloaded") {
        return;
      }
      const cancelled =
        info.end_state === "interrupted" || !!info.force_cancelled;
      await finishPoll(cancelled);
    } catch {
      // 404 after finalize — treat as completed and try getOutput once
      await finishPoll(false);
    }
  }, POLL_INTERVAL_MS);

  return () => {
    stopped = true;
    clearInterval(timer);
  };
}

/**
 * Register a task in the background queue and start the SSE/poll watcher.
 * Idempotent: safe for both manual offload and system auto-offload.
 *
 * sessionId may be empty on the first turn before window.currentSessionId is
 * set — we still enqueue the task (panel shows it) and resolve session later
 * for the watcher from window when possible.
 */
export function registerBackgroundTask(opts: {
  sessionId: string;
  toolCallId: string;
  toolName: string;
  startTime?: number;
  /** When true, skip SSE and hydrate /output immediately (fast bg finish). */
  alreadyCompleted?: boolean;
}): void {
  const {
    toolCallId,
    toolName,
    startTime = Date.now(),
    alreadyCompleted = false,
  } = opts;
  if (!toolCallId) return;

  const resolvedSessionId = resolveBackendSessionId(opts.sessionId);

  useBackgroundTasksStore.getState().addTask({
    toolCallId,
    toolName: toolName || toolCallId,
    sessionId: resolvedSessionId,
    startTime,
  });

  const backfillSessionId = (sid: string) => {
    useBackgroundTasksStore.setState((state) => ({
      tasks: state.tasks.map((t) =>
        t.toolCallId === toolCallId && !t.sessionId
          ? { ...t, sessionId: sid }
          : t,
      ),
    }));
  };

  if (alreadyCompleted) {
    const hydrate = (sid: string) => {
      if (!sid) return false;
      backfillSessionId(sid);
      void finalizeFromOutput(sid, toolCallId, "", false);
      return true;
    };
    if (!hydrate(resolvedSessionId)) {
      let attempts = 0;
      const timer = setInterval(() => {
        attempts += 1;
        const sid = resolveBackendSessionId();
        if (hydrate(sid) || attempts >= 20) {
          clearInterval(timer);
        }
      }, 250);
    }
    return;
  }

  // Watcher needs a session id for API paths; retry briefly if still empty.
  const startWatcher = (sid: string) => {
    if (!sid) return false;
    startBackgroundTaskWatcher(sid, toolCallId);
    // Back-fill sessionId on the task if it was empty at enqueue time.
    backfillSessionId(sid);
    return true;
  };

  if (!startWatcher(resolvedSessionId)) {
    let attempts = 0;
    const timer = setInterval(() => {
      attempts += 1;
      const sid = resolveBackendSessionId();
      if (startWatcher(sid) || attempts >= 20) {
        clearInterval(timer);
      }
    }, 250);
  }
}

/**
 * Start watching an offloaded tool call. Idempotent per toolCallId.
 */
export function startBackgroundTaskWatcher(
  sessionId: string,
  toolCallId: string,
): void {
  if (activeWatchers.has(toolCallId) || finalizedIds.has(toolCallId)) return;

  let settled = false;
  let pollAbort: AbortFn | null = null;
  let streamAbort: AbortFn = () => {};

  const abortAll = () => {
    streamAbort();
    pollAbort?.();
  };

  const settle = async (cancelled: boolean) => {
    if (settled) return;
    settled = true;
    activeWatchers.delete(toolCallId);
    abortAll();
    const live =
      useBackgroundTasksStore
        .getState()
        .tasks.find((t) => t.toolCallId === toolCallId)?.liveOutput || "";
    await finalizeFromOutput(sessionId, toolCallId, live, cancelled);
  };

  streamAbort = subscribeToolCallStream(sessionId, toolCallId, {
    onChunk: (payload) => {
      const text = chunkToText(payload);
      if (text) {
        useBackgroundTasksStore.getState().appendLiveOutput(toolCallId, text);
      }
    },
    onDone: () => {
      void settle(false);
    },
    onError: () => {
      if (settled || pollAbort) return;
      pollAbort = startPolling(sessionId, toolCallId);
    },
  });

  activeWatchers.set(toolCallId, abortAll);
}

/** Stop watcher without changing task status (e.g. user removed row). */
export function stopBackgroundTaskWatcher(toolCallId: string): void {
  const abort = activeWatchers.get(toolCallId);
  if (abort) {
    abort();
    activeWatchers.delete(toolCallId);
  }
}

/**
 * User cancelled from panel: stop stream, call cancel API, update store.
 * On API failure, resume the watcher so the task is not orphaned.
 */
export async function cancelBackgroundTask(
  sessionId: string,
  toolCallId: string,
): Promise<void> {
  const sid = (sessionId || "").trim();
  if (!sid) {
    message.error(
      i18n.t(
        "chat.backgroundTasks.cancelFailed",
        "Failed to cancel background task",
      ),
    );
    throw new Error("Missing backend session id for cancel");
  }
  stopBackgroundTaskWatcher(toolCallId);
  try {
    await toolCallsApi.cancel(sid, toolCallId);
  } catch (err) {
    finalizedIds.delete(toolCallId);
    startBackgroundTaskWatcher(sid, toolCallId);
    message.error(
      i18n.t(
        "chat.backgroundTasks.cancelFailed",
        "Failed to cancel background task",
      ),
    );
    throw err;
  }
  finalizedIds.add(toolCallId);
  const live =
    useBackgroundTasksStore
      .getState()
      .tasks.find((t) => t.toolCallId === toolCallId)?.liveOutput || "";
  useBackgroundTasksStore.getState().updateTask(toolCallId, {
    status: "cancelled",
    result: live || null,
  });
}

/**
 * Stop watchers and drop store rows that do not belong to the given session.
 * Call before hydrating a newly selected session to avoid leaking SSE/poll.
 * Pass an empty session id to tear down every tracked task (e.g. blank "new" chat).
 * Orphan rows with empty sessionId are always treated as stale on switch.
 */
export function stopBackgroundWatchersNotInSession(
  backendSessionId: string,
): void {
  const store = useBackgroundTasksStore.getState();
  const staleIds = !backendSessionId
    ? store.tasks.map((t) => t.toolCallId)
    : store.tasks
        .filter((t) => !t.sessionId || t.sessionId !== backendSessionId)
        .map((t) => t.toolCallId);
  for (const id of staleIds) {
    stopBackgroundTaskWatcher(id);
  }
  if (staleIds.length > 0) {
    store.removeTasks(staleIds);
  }
}

/**
 * Rehydrate the background task panel from the backend list of still-offloaded
 * tool calls. Idempotent with live registerBackgroundTask paths.
 */
export async function hydrateBackgroundTasksForSession(
  backendSessionId: string,
): Promise<void> {
  if (!backendSessionId) return;
  try {
    const { items } = await toolCallsApi.list(backendSessionId);
    for (const item of items) {
      if (item.status !== "offloaded") continue;
      const elapsedMs = Math.max(0, Math.round((item.elapsed || 0) * 1000));
      registerBackgroundTask({
        sessionId: item.session_id || backendSessionId,
        toolCallId: item.tool_call_id,
        toolName: item.tool_name || item.tool_call_id,
        startTime: Date.now() - elapsedMs,
      });
    }
  } catch (err) {
    console.error(
      "[hydrateBackgroundTasksForSession] list failed:",
      backendSessionId,
      err,
    );
  }
}