LibreChat/client/src/data-provider/PullRequest/queries.ts
Marco Beretta caeb786e97
🔀 feat: Show the Conversation Pull Request in the Sidebar List (#16815)
* feat: Show the Conversation Pull Request in the Sidebar List

Add a batch lookup route and a sidebar row mark: the state icon with the CI dot, opening the same card beside the row on hover. The row keeps the conversation's own title.

* fix: Address Review Findings on the Sidebar Pull Request Batch and Row Mark

Cap the whole batch with a configurable deadline, make the row mark a keyboard-reachable button that opens the card on focus, refresh rows when the window regains focus through the shared batch, and show a retryable failure state instead of treating a failed first lookup as no pull request.

* fix: Send Sidebar Pull Request Batches One at a Time

Queue every batch request behind the one before it, so an oversized list or an overlapping flush cannot multiply the server's configured lookup concurrency, and bound how long one request may hold the queue so a request that never answers cannot stop the rest.

* fix: Keep the Batch Request Timeout Above the Server Deadline

Default the client's request timeout to just past the longest batchTimeoutSeconds a deployment may configure, so a queued request never starts while the previous request's server-side lookups are still running.

* fix: Keep Batch Lookups Inside the Concurrency Limit and Per-Entry Failures Per Entry

Share one lookup limiter across every batch request so lookups that outlive a request's deadline keep their slot, answer a missing token only for the conversations that needed it, and point the row at its pull request description only while that text is in the page.

* fix: Bound Batch Reads, Scope Concurrency Per Credential, and Gate the Sidebar on the Batch Version

Start the batch deadline when the request arrives and bound the config and lane reads, count lookup slots per credential, advertise a batch route version and ask only a server that has it, take no row space for an absent mark, show a failed refresh over a cached empty answer, keep a malformed response from stalling the request queue, and repair the lane recording tests that still used a mock removed on dev.

* fix: Move Capability Resolution Into the Package and Harden the Batch Route and Queue

Resolve the pull request startup capabilities in packages/api so the route stays wiring only, bound the config and lane reads by the configured deadline, merge identical lanes before taking limiter slots, fall back to the single route when a replica has no batch route, and dispose the batch queue when the signed-in user changes.

* fix: Abort the Batch Fallback With Its Request So the Queue Never Overlaps It

Give the batch fetcher the request's abort signal and pass it to every single-route call, abort a request that times out or is disposed, and stop the fallback workers from starting more calls once it has.

* fix: Forward the Abort Signal to the Batch POST

Pass the request's abort signal through the batch fetcher, the data-provider call and Axios, so a batch request that times out or is disposed is cancelled instead of continuing beside the next one, and settle a request promptly when it is aborted.

* fix: Close the Pull Request Lookup Follow-Ups and Cancel Abandoned Sidebar Rows
2026-10-07 12:36:57 +02:00

130 lines
5.2 KiB
TypeScript

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 { useGetStartupConfig } from '../Endpoints';
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 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.
*/
export const endPullRequestSession = (): void => {
rowBatcher?.batcher.dispose();
rowBatcher = undefined;
};
/**
* 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,
},
);
};