Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

6.2 KB
Raw Blame
import {
  AgentEventType,
  AgentConversation,
  AgentConversationStatus,
  WebsocketClientEvent,
} from '@activepieces/shared';
import { useQueries, useQueryClient } from '@tanstack/react-query';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { useSocket } from '@/components/providers/socket-provider';

import { chatApi } from './chat-api';

export function useConversationIndicators({
  conversations,
  activeId,
  scope,
  loaded,
}: {
  conversations: AgentConversation[];
  activeId: string | null;
  scope: string;
  loaded: boolean;
}) {
  const socket = useSocket();
  const queryClient = useQueryClient();
  const [lastSeen, setLastSeen] = useState<Record<string, string>>(() =>
    readLastSeen(),
  );

  const conversationsRef = useRef(conversations);
  conversationsRef.current = conversations;
  const firstLoadBaselineRef = useRef(new Map<string, string>());

  useEffect(() => {
    if (!loaded) return;
    const baseline =
      firstLoadBaselineRef.current.get(scope) ??
      newestUpdated({ conversations });
    firstLoadBaselineRef.current.set(scope, baseline);
    setLastSeen((prev) => {
      const next = seedLastSeen({ prev, conversations, baseline });
      if (next === prev) return prev;
      writeLastSeen(next);
      return next;
    });
  }, [conversations, loaded, scope]);

  const markRead = useCallback((id: string) => {
    const conv = conversationsRef.current.find((c) => c.id === id);
    const seenValue = conv?.updated ?? new Date().toISOString();
    setLastSeen((prev) => {
      if (prev[id] === seenValue) return prev;
      const next = { ...prev, [id]: seenValue };
      writeLastSeen(next);
      return next;
    });
  }, []);

  const activeUpdated = activeId
    ? conversations.find((c) => c.id === activeId)?.updated
    : undefined;
  useEffect(() => {
    if (activeId) markRead(activeId);
  }, [activeId, activeUpdated, markRead]);

  useEffect(() => {
    const knownStreaming = new Set<string>();

    const invalidateList = () => {
      void queryClient.invalidateQueries({ queryKey: ['chat-conversations'] });
    };

    const handler = (event: SocketEvent) => {
      const convId = event.conversationId;
      if (!convId) return;
      switch (event.type) {
        case AgentEventType.FINISHED:
        case AgentEventType.ERROR:
          knownStreaming.delete(convId);
          invalidateList();
          break;
        case AgentEventType.ACTION_PREVIEW:
        case AgentEventType.ACTION_RECEIPT:
          void queryClient.invalidateQueries({
            queryKey: ['chat-pending-gate', convId],
          });
          invalidateList();
          break;
        case AgentEventType.CHUNK:
        case AgentEventType.TITLE_UPDATE:
          if (!knownStreaming.has(convId)) {
            knownStreaming.add(convId);
            invalidateList();
          }
          break;
        default:
          break;
      }
    };

    socket.on(WebsocketClientEvent.CHAT_MESSAGE_CHUNK, handler);
    const reconnectHandler = () => {
      socket.off(WebsocketClientEvent.CHAT_MESSAGE_CHUNK, handler);
      socket.on(WebsocketClientEvent.CHAT_MESSAGE_CHUNK, handler);
    };
    socket.on('connect', reconnectHandler);

    return () => {
      socket.off(WebsocketClientEvent.CHAT_MESSAGE_CHUNK, handler);
      socket.off('connect', reconnectHandler);
    };
  }, [socket, queryClient]);

  const streamingIds = useMemo(
    () =>
      conversations
        .filter((c) => c.status === AgentConversationStatus.STREAMING)
        .map((c) => c.id),
    [conversations],
  );

  const gateQueries = useQueries({
    queries: streamingIds.map((id) => ({
      queryKey: ['chat-pending-gate', id],
      queryFn: () => chatApi.getPendingGate(id),
      refetchInterval: GATE_REFETCH_INTERVAL_MS,
    })),
  });

  const waitingSet = useMemo(() => {
    const set = new Set<string>();
    streamingIds.forEach((id, index) => {
      if (gateQueries[index]?.data) set.add(id);
    });
    return set;
  }, [streamingIds, gateQueries]);

  const getIndicator = useCallback(
    (conv: AgentConversation): ConversationIndicatorState | null => {
      if (conv.status === AgentConversationStatus.STREAMING) {
        return waitingSet.has(conv.id) ? 'waiting' : 'working';
      }
      if (conv.id !== activeId) {
        const seen = lastSeen[conv.id];
        if (seen !== undefined && conv.updated > seen) return 'unread';
      }
      return null;
    },
    [waitingSet, activeId, lastSeen],
  );

  return { getIndicator, markRead };
}

function seedLastSeen({
  prev,
  conversations,
  baseline,
}: {
  prev: Record<string, string>;
  conversations: Pick<AgentConversation, 'id' | 'updated'>[];
  baseline: string;
}): Record<string, string> {
  const unseen = conversations.filter((conv) => prev[conv.id] === undefined);
  if (unseen.length === 0) return prev;
  const seeded = unseen.map((conv) => [
    conv.id,
    conv.updated > baseline ? NEVER_SEEN : conv.updated,
  ]);
  return { ...prev, ...Object.fromEntries(seeded) };
}

function newestUpdated({
  conversations,
}: {
  conversations: Pick<AgentConversation, 'updated'>[];
}): string {
  return conversations.reduce(
    (newest, conv) => (conv.updated > newest ? conv.updated : newest),
    NEVER_SEEN,
  );
}

function readLastSeen(): Record<string, string> {
  try {
    const raw = localStorage.getItem(LAST_SEEN_STORAGE_KEY);
    if (!raw) return {};
    const parsed: unknown = JSON.parse(raw);
    if (typeof parsed !== 'object' || parsed === null) return {};
    const result: Record<string, string> = {};
    for (const [key, value] of Object.entries(parsed)) {
      if (typeof value === 'string') result[key] = value;
    }
    return result;
  } catch {
    return {};
  }
}

function writeLastSeen(value: Record<string, string>): void {
  try {
    localStorage.setItem(LAST_SEEN_STORAGE_KEY, JSON.stringify(value));
  } catch {
    // localStorage may be unavailable (private mode / quota) — unread falls back to in-memory.
  }
}

const LAST_SEEN_STORAGE_KEY = 'chat-conversation-last-seen';
const NEVER_SEEN = '';
const GATE_REFETCH_INTERVAL_MS = 3000;

type SocketEvent = {
  conversationId: string;
  runId?: string;
  type: string;
  data: unknown;
};

export type ConversationIndicatorState = 'working' | 'waiting' | 'unread';

export const conversationIndicatorUtils = { seedLastSeen };