Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

6.2 KB
Raw Blame
import { useRecoilValue } from 'recoil';
import { useQuery } from '@tanstack/react-query';
import {
  Constants,
  QueryKeys,
  dataService,
  PULL_REQUEST_BATCH_VERSION,
} from 'librechat-data-provider';
import type { TConversationPullRequestResponse } from 'librechat-data-provider';
import type { UseQueryOptions } from '@tanstack/react-query';
import { createBatchFetcher, createPullRequestBatcher } from './batch';
import { resetPullRequestCredentials } from './connection';
import { useGetStartupConfig } from '../Endpoints';
import { stopPullRequestSignIns } from './signIn';
import store from '~/store';

const SETTLED_REFRESH_MS = 60_000;
/** Checks and conflict state change while a pull request is being worked on. */
const ACTIVE_REFRESH_MS = 20_000;

/**
 * Poll faster only while something can still change. A closed or merged pull request has no
 * mergeability left to settle, but its checks can still be running, so those keep the active
 * interval until they finish; only then does polling stop.
 */
export const pullRequestRefetchInterval = (
  response: TConversationPullRequestResponse | undefined,
): number | false => {
  const pr = response?.pullRequest;
  if (pr == null) return SETTLED_REFRESH_MS;
  if (pr.checks === 'running') return ACTIVE_REFRESH_MS;
  if (pr.state !== 'open') return false;
  return pr.mergeable === 'unknown' ? ACTIVE_REFRESH_MS : SETTLED_REFRESH_MS;
};

const isSavedConversation = (conversationId: string) =>
  conversationId !== '' &&
  conversationId !== Constants.NEW_CONVO &&
  conversationId !== Constants.PENDING_CONVO;

/**
 * Asks only when the deployment advertises the feature and the conversation is a saved one, so a
 * default installation never calls the endpoint. The server answers no pull request for a chat
 * that has no code lane, which keeps a non-code chat to one cheap, slow poll.
 */
export const useConversationPullRequestQuery = (
  conversationId: string,
  config?: UseQueryOptions<TConversationPullRequestResponse>,
) => {
  const { data: startupConfig } = useGetStartupConfig();
  return useQuery<TConversationPullRequestResponse>(
    [QueryKeys.conversationPullRequest, conversationId],
    () => dataService.getConversationPullRequest(conversationId),
    {
      enabled: startupConfig?.pullRequestsEnabled === true && isSavedConversation(conversationId),
      staleTime: 15_000,
      retry: false,
      refetchOnWindowFocus: true,
      refetchInterval: pullRequestRefetchInterval,
      refetchIntervalInBackground: false,
      ...config,
    },
  );
};

/** What the server last advertised, for a fallback that has no batch limiter to lean on. */
let advertisedConcurrency: number | undefined;

const fetchRows = createBatchFetcher({
  fetchMany: (conversationIds, signal) =>
    dataService.getConversationPullRequests(conversationIds, { signal }),
  fetchOne: (conversationId, signal) =>
    dataService.getConversationPullRequest(conversationId, { signal }),
  concurrency: () => advertisedConcurrency,
});

type RowBatcher = ReturnType<typeof createPullRequestBatcher>;
let rowBatcher: { userId: string; batcher: RowBatcher } | undefined;

/**
 * The batcher for the signed-in user: every sidebar row mounted in the same moment shares one
 * request. It belongs to one identity, so when the user changes the old one is disposed, and ids
 * it still held are neither sent under the next account's authorization nor made to wait ahead
 * of that account's own rows.
 */
const batcherFor = (userId: string): RowBatcher => {
  if (rowBatcher?.userId === userId) return rowBatcher.batcher;
  rowBatcher?.batcher.dispose();
  const batcher = createPullRequestBatcher({ fetchMany: fetchRows });
  rowBatcher = { userId, batcher };
  return batcher;
};

/**
 * Ends the row queue, any account sign-in watch and the record of which credential cached pull
 * requests were fetched with, with the session. Called wherever the session's client state is cleared, so
 * signing back in as the same account starts a fresh queue and neither waits behind, nor sends,
 * anything the previous session left.
 */
/** The account the pull request session belongs to. */
let sessionOwner: string | undefined;

/**
 * Records who signed in, and ends the pull request session when it is a different account than
 * the last one: an account switch without a logout passes no teardown, and the sign-in watch,
 * credential record and queued lookups would otherwise carry over to the next account.
 */
export const setPullRequestSessionOwner = (userId?: string | null): void => {
  if (!userId) {
    return;
  }
  if (sessionOwner !== undefined && sessionOwner !== userId) {
    endPullRequestSession();
  }
  sessionOwner = userId;
};

export const endPullRequestSession = (): void => {
  rowBatcher?.batcher.dispose();
  rowBatcher = undefined;
  /** A sign-in watch left running would ask for, and cache, the next user's connection. */
  stopPullRequestSignIns();
  resetPullRequestCredentials();
};

/**
 * The pull request of a sidebar row. It shares its cache entry with the header's query, so the
 * open conversation is never fetched twice, but it never polls: a list of rows would otherwise
 * poll GitHub once per row. It refreshes when the window regains focus and a row's answer is
 * older than a minute; every mounted row asks in the same moment, so the batcher turns that
 * into one request for the whole list rather than one per row.
 */
export const useRowPullRequestQuery = (conversationId: string) => {
  const { data: startupConfig } = useGetStartupConfig();
  const userId = useRecoilValue(store.user)?.id ?? '';
  return useQuery<TConversationPullRequestResponse>(
    [QueryKeys.conversationPullRequest, conversationId],
    ({ signal }) => {
      advertisedConcurrency = startupConfig?.pullRequestsMaxConcurrentLookups;
      return batcherFor(userId).load(conversationId, signal);
    },
    {
      /** The batch route is newer than the flag: ask only a server that says it has it. */
      enabled:
        startupConfig?.pullRequestsEnabled === true &&
        startupConfig.pullRequestsBatchVersion === PULL_REQUEST_BATCH_VERSION &&
        userId !== '' &&
        isSavedConversation(conversationId),
      staleTime: SETTLED_REFRESH_MS,
      retry: false,
      refetchOnWindowFocus: true,
    },
  );
};