Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

13.2 KB
Raw Blame
import type { CreatableAcceptanceSubjectType } from '@lobechat/types';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import type { ReplicaSyncResult } from '@/libs/replica';
import { useClientDataSWR } from '@/libs/swr';
import { verifyKeys } from '@/libs/swr/keys';
import type {
  AcceptanceBySubject,
  AcceptanceListFilter,
  AcceptanceListItem,
  AcceptanceListQuery,
  VerifyReportBundle,
} from '@/services/verify';
import { verifyService } from '@/services/verify';
import { useDocumentStore } from '@/store/document';
import { useVerifyStore, verifySelectors } from '@/store/verify';
import type { AcceptanceListParams, AcceptancePageParams } from '@/store/verify/projection';

const VERIFY_REPORT_SWR_CONFIG = {
  revalidateIfStale: false,
  revalidateOnFocus: false,
  revalidateOnReconnect: false,
} as const;

/**
 * Poll until the aggregate exists AND stops moving.
 *
 * Discovery was the only case handled before, so a task page left open during
 * a goal loop kept rendering whatever state it first saw — "awaiting
 * verification" straight through verifying, delivery and acceptance — until a
 * focus or remount happened to refetch.
 */
const TERMINAL_ACCEPTANCE_STATUSES = new Set(['accepted', 'closed']);

/**
 * Most subjects never get an acceptance, so a missing one is checked every 10s rather than
 * every 2s, which kept every open task page reading it around the clock.
 */
export const ACCEPTANCE_ABSENT_REFRESH_MS = 10_000;

/**
 * Readers on one task page (state row, acceptance section, result portal) poll the same subject
 * on their own timers, and may mount seconds apart; `useClientDataSWR` dedupes nothing by
 * default. Just under the 5s live interval, so every reader shares one read per interval, and a
 * read left by the slower absent poll delays the first live one by less than one interval.
 */
export const ACCEPTANCE_POLL_DEDUPE_MS = 4500;

export const getAcceptanceBySubjectRefreshInterval = (acceptance: unknown) => {
  if (!acceptance) return ACCEPTANCE_ABSENT_REFRESH_MS;
  const status = (acceptance as { status?: string }).status;
  return status && TERMINAL_ACCEPTANCE_STATUSES.has(status) ? 0 : 5000;
};

/**
 * One Agent Run's verify card, read from the verify store's replica of
 * `verifyRunMap`: a settled run paints from IndexedDB on reload, then refreshes.
 */
const useVerifyRun = (operationId: string | null) => {
  const useFetchVerifyRun = useVerifyStore((s) => s.useFetchVerifyRun);
  const run = useVerifyStore(verifySelectors.verifyRun(operationId));
  const sync = useFetchVerifyRun(operationId);

  return { mutate: sync.revalidate, run };
};

/** Plan + rollup status for one Agent Run. Pass null operationId to skip. */
export const useVerifyState = (operationId: string | null) => {
  const { mutate, run } = useVerifyRun(operationId);
  return { data: run?.state, mutate };
};

/** Per-item check results for one Agent Run. Pass null operationId to skip. */
export const useVerifyResults = (operationId: string | null) => {
  const { mutate, run } = useVerifyRun(operationId);
  return { data: run?.results, mutate };
};

/**
 * What a replica-backed read hands the views that used to read an SWR response:
 * `data` is the store entry (`undefined` until hydrated or fetched), `mutate`
 * re-runs the network sync.
 */
export interface VerifyReplicaRead<T> {
  data: T | undefined;
  error: unknown;
  /** Nothing to show yet, and no failure either. */
  isLoading: boolean;
  isValidating: boolean;
  mutate: () => Promise<unknown>;
}

const toReplicaRead = <T>(
  data: T | undefined,
  sync: ReplicaSyncResult,
  enabled: boolean,
): VerifyReplicaRead<T> => ({
  data,
  error: sync.error,
  isLoading: enabled && data === undefined && sync.error == null,
  isValidating: sync.isValidating,
  mutate: sync.revalidate,
});

/**
 * Full standalone report bundle (run + report + results + evidence) by
 * verifyRunId. `data` is `null` once the server answered there is no such run;
 * a finished report paints from IndexedDB on reload, then refreshes.
 */
export const useVerifyReportBundle = (
  verifyRunId: string | null,
): VerifyReplicaRead<VerifyReportBundle | null> => {
  const useFetchVerifyReportBundle = useVerifyStore((s) => s.useFetchVerifyReportBundle);
  const data = useVerifyStore(verifySelectors.verifyReportBundle(verifyRunId));
  const sync = useFetchVerifyReportBundle(verifyRunId);
  return toReplicaRead(data, sync, !!verifyRunId);
};

export { useAcceptanceBundle } from './Viewer/useAcceptanceBundle';

/** What `useAcceptanceBySubject` hands its readers — the former SWR response shape. */
export interface AcceptanceBySubjectSync {
  /** The attachment from the replica view; `undefined` until hydrated or fetched. */
  data: AcceptanceBySubject | undefined;
  error: unknown;
  /** Nothing to show yet, and a hydration or fetch is still outstanding. */
  isLoading: boolean;
  isValidating: boolean;
  /** Re-read the attachment and resolve with the freshly read value. */
  mutate: () => Promise<AcceptanceBySubject | undefined>;
}

/**
 * The optional acceptance aggregate attached to a task/topic/document subject,
 * read from the verify store's replica of `acceptanceBySubjectMap`. A subject
 * can mount before its first Verify Run creates the aggregate, so the sync
 * checks for one and then keeps it live until it settles.
 */
export const useAcceptanceBySubject = (
  subjectType: CreatableAcceptanceSubjectType,
  subjectId: string | null,
): AcceptanceBySubjectSync => {
  const useFetchAcceptanceBySubject = useVerifyStore((s) => s.useFetchAcceptanceBySubject);
  const key = subjectId ? `${subjectType}:${subjectId}` : undefined;
  const data = useVerifyStore((s) => (key ? s.acceptanceBySubjectMap[key] : undefined));

  const sync = useFetchAcceptanceBySubject(subjectType, subjectId, {
    dedupingInterval: ACCEPTANCE_POLL_DEDUPE_MS,
    refreshInterval: getAcceptanceBySubjectRefreshInterval(data),
  });

  // A subject without an acceptance polls every 10s. Once a request for this key
  // has settled, "no acceptance" is a known answer: later polls must not flip the
  // caller back to its loading placeholder (that swapped the task's Acceptance
  // trigger for a spinner on every tick and made the section jump).
  const validatingKeyRef = useRef<string | undefined>(undefined);
  const [settledKey, setSettledKey] = useState<string>();
  useEffect(() => {
    if (!key) return;
    if (sync.isValidating) {
      validatingKeyRef.current = key;
      return;
    }
    if (validatingKeyRef.current === key) setSettledKey(key);
  }, [key, sync.isValidating]);

  return {
    data,
    error: sync.error,
    // A read with no subject has nothing to load — never report it as loading.
    isLoading:
      !!subjectId &&
      !data &&
      !sync.error &&
      (!sync.isHydrated || (sync.isValidating && settledKey !== key)),
    isValidating: sync.isValidating,
    mutate: async () => {
      await sync.revalidate();
      return key ? useVerifyStore.getState().acceptanceBySubjectMap[key] : undefined;
    },
  };
};

const EMPTY_ITEMS: AcceptanceListItem[] = [];

/**
 * The caller's recent acceptance aggregates (with subject headers), read from
 * the verify store's replica of `acceptanceListMap`. `limit` widens the recency
 * window for surfaces that must reach further back than the panel does (the
 * merge target picker); `fresh` skips the shared dedupe window on mount.
 *
 * Every write revalidates all list entries together
 * (`useVerifyStore.getState().refreshAcceptanceLists()`).
 */
export const useAcceptanceList = (
  enabled: boolean,
  options?: AcceptanceListQuery & {
    fresh?: boolean;
    limit?: number;
    q?: string;
  },
) => {
  const params: AcceptanceListParams | null = enabled
    ? {
        filter: options?.filter,
        limit: options?.limit,
        projectId: options?.projectId,
        q: options?.q,
        scope: options?.scope,
        source: options?.source,
      }
    : null;
  const useFetchAcceptanceList = useVerifyStore((s) => s.useFetchAcceptanceList);
  const sync = useFetchAcceptanceList(params, { fresh: options?.fresh });
  const entry = useVerifyStore(verifySelectors.acceptanceList(params));
  const confirmed = useVerifyStore(verifySelectors.isAcceptanceListConfirmed(params));

  // A restored empty list is only the last session's answer: until the server
  // confirms it, "no acceptances" is still loading (not onboarding or an empty state).
  const data = entry && (entry.length > 0 || confirmed) ? entry : undefined;
  // A failed background refresh keeps the last list on screen.
  const error = data ? undefined : sync.error;

  return {
    data,
    error,
    isLoading: enabled && !data && !error,
    mutate: sync.revalidate,
  };
};

/** Rounds, evidence files and bytes a delete would purge. Pass null to skip. */
export const useAcceptancePurgePreview = (acceptanceId: string | null) =>
  useClientDataSWR(
    acceptanceId ? verifyKeys.acceptancePurgePreview(acceptanceId) : null,
    () => verifyService.getAcceptancePurgePreview([acceptanceId!]),
    VERIFY_REPORT_SWR_CONFIG,
  );

/**
 * The panel's scroll feed: keyset pages, newest first, with the in-progress /
 * completed split applied server-side (see `acceptance.listPage`), read from
 * the verify store's paged replica (`acceptancePageMap`). The head page
 * survives a reload, so the panel paints its last rows before the network.
 *
 * A `null` filter disables the feed entirely — that is how the panel hands off
 * to the flat search read. Switching to a feed collapses it back to its head
 * page rather than refetching as many pages as an earlier visit had open.
 */
export const useAcceptanceListInfinite = (
  filter: AcceptanceListFilter | null,
  query: Omit<AcceptanceListQuery, 'filter'> = {},
) => {
  const { projectId, scope, source } = query;
  const params = useMemo<AcceptancePageParams | null>(
    () => (filter ? { filter, projectId, scope, source } : null),
    [filter, projectId, scope, source],
  );
  const useFetchAcceptancePage = useVerifyStore((s) => s.useFetchAcceptancePage);
  const sync = useFetchAcceptancePage(params);
  const entry = useVerifyStore(verifySelectors.acceptancePage(params));
  const confirmed = useVerifyStore(verifySelectors.isAcceptancePageConfirmed(params));

  useEffect(() => {
    if (params) useVerifyStore.getState().collapseAcceptancePage(params);
  }, [params]);

  const loadMore = useCallback(() => {
    if (params) void useVerifyStore.getState().loadMoreAcceptancePage(params);
  }, [params]);

  // A restored empty head is not a settled "no acceptances" until the server says so.
  const settled = !!entry && (entry.items.length > 0 || confirmed);
  const items = settled ? entry.items : EMPTY_ITEMS;
  // A failed background refresh keeps the rows on screen; only a feed with
  // nothing to show reads as failed.
  const error = settled ? undefined : sync.error;
  const loadMoreError = settled ? entry.loadMoreError : undefined;

  return {
    error,
    // Pause the sentinel while a page error is showing so it cannot hot-loop
    // the failed page; the panel offers a manual retry instead.
    hasMore: settled && entry.hasMore && !loadMoreError,
    isLoadingInitial: !!params && !settled && !error,
    isLoadingMore: settled && !!entry.isLoadingMore,
    items,
    loadMore,
    loadMoreError,
    mutate: sync.revalidate,
  };
};

/** Model / token / latency for an LLM verifier judgment. Pass null to skip. */
export const useVerifierTracing = (tracingId: string | null | undefined) => {
  const useFetchVerifierTracing = useVerifyStore((s) => s.useFetchVerifierTracing);
  const data = useVerifyStore(verifySelectors.verifierTracing(tracingId));
  const sync = useFetchVerifierTracing(tracingId);
  return toReplicaRead(data, sync, !!tracingId);
};

/**
 * The criterion's original judging rule, stored in its instruction document.
 * Shares the document detail replica, so it paints from the same persisted row
 * as every other view of that document.
 */
export const useVerifyInstruction = (documentId: string | null | undefined) => {
  const useFetchDocumentDetail = useDocumentStore((s) => s.useFetchDocumentDetail);
  return useFetchDocumentDetail(documentId || undefined);
};

/** The workspace's reusable rubric templates (delivery-standard groups). */
export const useRubrics = (enabled = true) => {
  const useFetchVerifyRubrics = useVerifyStore((s) => s.useFetchVerifyRubrics);
  const data = useVerifyStore(verifySelectors.verifyRubrics);
  const sync = useFetchVerifyRubrics(enabled);
  return toReplicaRead(data, sync, enabled);
};

/** The criteria a rubric groups, in rubric order. Pass null to skip. */
export const useRubricCriteria = (rubricId: string | null | undefined) => {
  const useFetchVerifyRubricCriteria = useVerifyStore((s) => s.useFetchVerifyRubricCriteria);
  const data = useVerifyStore(verifySelectors.verifyRubricCriteria(rubricId));
  const sync = useFetchVerifyRubricCriteria(rubricId);
  return toReplicaRead(data, sync, !!rubricId);
};

/** The persisted criteria a Goal is accepted against, in its order. Empty ids skip. */
export const useGoalCriteria = (criteriaIds: string[]) => {
  const useFetchGoalCriteria = useVerifyStore((s) => s.useFetchGoalCriteria);
  const data = useVerifyStore(verifySelectors.goalCriteria(criteriaIds));
  const sync = useFetchGoalCriteria(criteriaIds);
  return toReplicaRead(data, sync, criteriaIds.length > 0);
};