Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

9.9 KB
Raw Blame
import { useEffect, useRef } from 'react';
import toast from '../components/ui/Toast';
import socket from '../services/socket';

/**
 * Subscribe to server-side AI operation status events and render them as
 * live-updating toasts so the user can see what AI providers/models are
 * being called, when a model is being loaded into memory, and when each
 * call finishes.
 *
 * Phases (from server/services/aiStatusEvents.js):
 *   start          — request kicked off
 *   model:loading  — provider auto-loading a model (slow, always show)
 *   model:loaded   — model is in memory and ready
 *   complete       — call finished
 *   error          — call failed
 *
 * UX rules:
 *   - Each op id maps to one toast; phases mutate the same toast.
 *   - "Silent" ops (callers that didn't pass `op`/`opLabel`) only render
 *     toasts when something user-visible happens: model loading, model loaded,
 *     errors, or calls that take longer than a couple seconds.
 *   - Non-silent ops render from the start so the user sees feedback for
 *     explicit actions (Generate Summary, etc.) even on fast calls.
 *   - *Background*-op errors are coalesced per-provider within a short rolling
 *     window (see COALESCE below). An unattended job that fans out one AI call
 *     per record (e.g. the multi-goal check-in via `Promise.all` in
 *     server/services/goalCheckIn.js, which passes `background: true`) can
 *     produce N systematic failures at once — each with a unique op id, so
 *     Toast's content dedupe can't collapse them. Instead of N stacked red
 *     toasts from a job the user never watched start, they get ONE toast that
 *     counts the failures. The `background` flag is explicit provenance from the
 *     unattended caller — NOT inferred from `silent`, because user-triggered
 *     op-less actions (generateGoalPhases/decomposeGoal/checkInGoal) are silent
 *     too and must keep toasting individually. Every user-triggered op — silent
 *     or not — reports its failure immediately, individually, with the real
 *     reason; that is load-bearing for #2733's single-voice Go-deeper design.
 */
export function useAIStatusNotifications() {
  // Per-op state: { silent, opened, slowTimer? }
  const opsRef = useRef(new Map());
  // Per-provider background-error coalescing: providerKey -> { toastId, count, firstReason, seen, timer }
  const bgErrorsRef = useRef(new Map());
  // Monotonic window counter so each fresh coalescing window gets a unique toast
  // id (see coalesceSilentError) — a reused id would let a prior window's pending
  // auto-dismiss timer remove a successor window's toast.
  const windowSeqRef = useRef(0);

  useEffect(() => {
    const SLOW_CALL_MS = 2500;
    // Rolling window over which concurrent background-op failures from the same
    // provider collapse into a single counted toast. A burst from `Promise.all`
    // arrives near-simultaneously, well inside this window; genuinely
    // spaced-out failures (> window apart) each show their real reason again.
    const BG_ERROR_WINDOW_MS = 4000;

    const phaseIcon = {
      start: '🤖',
      'model:loading': '📦',
      'model:loaded': '✅',
      complete: '✓',
      error: '✕'
    };

    // Collapse a burst of background-op failures from one provider into a single
    // toast keyed by provider. The first failure in the window shows the real
    // reason; subsequent DISTINCT ops update the SAME toast with a running count,
    // keeping that first reason visible so the toast stays actionable.
    const coalesceBackgroundError = (event) => {
      const key = event.providerId || event.providerName || '__unknown_provider__';
      const providerLabel = event.providerName || event.providerId || 'AI provider';
      const reason = event.message || `${providerLabel} background AI call failed`;

      // The hook owns the coalesced toast's whole lifetime: render it with
      // `duration: Infinity` so Toast schedules NO auto-dismiss timer of its own
      // (it never cancels those, and each update's stale timer would otherwise
      // dismiss the toast mid-window), and dismiss it ourselves once the rolling
      // window lapses — BG_ERROR_WINDOW_MS after the LAST failure, since the
      // timer is reset on every update below.
      const armWindowExpiry = (k, toastId) =>
        setTimeout(() => {
          toast.dismiss(toastId);
          bgErrorsRef.current.delete(k);
        }, BG_ERROR_WINDOW_MS);

      const entry = bgErrorsRef.current.get(key);

      if (!entry) {
        // Fresh window. Give the toast a window-unique id so that even if a
        // dismissal ever races a brand-new window (e.g. rapid lapse/re-open),
        // it can only ever target its own window's toast, never a successor's.
        windowSeqRef.current += 1;
        const toastId = `ai-bg-error::${key}::${windowSeqRef.current}`;
        const fresh = { toastId, count: 1, firstReason: reason, seen: new Set([event.id]), timer: undefined };
        fresh.timer = armWindowExpiry(key, toastId);
        bgErrorsRef.current.set(key, fresh);
        toast.error(reason, { id: toastId, duration: Infinity, icon: '✕' });
        return;
      }

      // Count DISTINCT operations, not error events: one failed call can emit
      // more than one error event with the same op id (an LM Studio model-load
      // failure emits an error phase, then callProviderAISimple emits the final
      // error too), which would otherwise report a single failure as two. A
      // repeat id just keeps the window alive.
      if (entry.seen.has(event.id)) {
        if (entry.timer) clearTimeout(entry.timer);
        entry.timer = armWindowExpiry(key, entry.toastId);
        return;
      }
      entry.seen.add(event.id);
      entry.count += 1;
      if (entry.timer) clearTimeout(entry.timer);
      entry.timer = armWindowExpiry(key, entry.toastId);
      // Retain the first real reason — the count alone doesn't tell the user
      // whether it was an expired key, a timeout, or a bad response, and these
      // events can all land before the browser paints, so the counted toast is
      // often the only one the user ever sees.
      toast.error(`${providerLabel} failed on ${entry.count} background AI calls — ${entry.firstReason}`, {
        id: entry.toastId, duration: Infinity, icon: '✕'
      });
    };

    const showLoading = (event) => {
      const state = opsRef.current.get(event.id) || { silent: false };
      // Once a toast is opened, the deferred slow-call timer is no longer needed —
      // letting it fire later would re-show a stale start message and clobber
      // the model:loading/model:loaded toast we just opened.
      if (state.slowTimer) {
        clearTimeout(state.slowTimer);
        state.slowTimer = undefined;
      }
      state.opened = true;
      opsRef.current.set(event.id, state);
      toast.loading(event.message, { id: event.id, icon: phaseIcon[event.phase] || '🤖' });
    };

    const handleStatus = (event) => {
      if (event.localOnly) return;
      const state = opsRef.current.get(event.id) || { silent: !!event.silent, opened: false };
      opsRef.current.set(event.id, state);

      if (event.phase === 'start') {
        if (!state.silent) showLoading(event);
        // For silent ops, defer toast until something user-visible happens or
        // until the call exceeds SLOW_CALL_MS.
        else if (!state.opened) {
          state.slowTimer = setTimeout(() => {
            const cur = opsRef.current.get(event.id);
            if (cur && !cur.opened) {
              showLoading({ id: event.id, message: event.message, phase: 'start' });
            }
          }, SLOW_CALL_MS);
        }
        return;
      }

      if (event.phase === 'model:loading' || event.phase === 'model:loaded') {
        // Always surface model load events regardless of silent flag — these
        // are the ones the user is most likely to be waiting on.
        showLoading(event);
        return;
      }

      if (event.phase === 'complete') {
        if (state.slowTimer) clearTimeout(state.slowTimer);
        if (state.opened) {
          // Update the existing toast to a brief success that auto-dismisses.
          toast.success(event.message, { id: event.id, duration: 2500 });
        }
        opsRef.current.delete(event.id);
        return;
      }

      if (event.phase === 'error') {
        if (state.slowTimer) clearTimeout(state.slowTimer);
        opsRef.current.delete(event.id);
        // Only UNATTENDED background jobs coalesce per-provider, so a systematic
        // failure yields one counted toast, not N — and this holds for SLOW
        // failures too (an unreachable endpoint / timeout, where every parallel
        // op has already opened its own spinner): dismiss that orphan-prone
        // Infinity-duration spinner first, then coalesce, so the goal-check-in
        // flood collapses whether the provider fails fast or slow. Everything
        // the user triggered — INCLUDING op-less "silent" actions — toasts
        // immediately, individually, with the real reason (updating its own
        // spinner in place if it had one). Load-bearing and never aggregated.
        if (event.background) {
          if (state.opened) toast.dismiss(event.id);
          coalesceBackgroundError(event);
        } else {
          toast.error(event.message || 'AI call failed', { id: event.id, duration: 6000, icon: '✕' });
        }
      }
    };

    socket.on('ai:status', handleStatus);
    return () => {
      socket.off('ai:status', handleStatus);
      for (const s of opsRef.current.values()) {
        if (s.slowTimer) clearTimeout(s.slowTimer);
      }
      opsRef.current.clear();
      for (const e of bgErrorsRef.current.values()) {
        if (e.timer) clearTimeout(e.timer);
        // These toasts render with duration: Infinity, so nothing else will
        // dismiss them once we drop their window timer — clear them explicitly.
        toast.dismiss(e.toastId);
      }
      bgErrorsRef.current.clear();
    };
  }, []);
}