🗨️ refactor: Optimize Prompt Queries

feat: Refactor prompt and prompt group schemas; move types to separate file

feat: Implement paginated access to prompt groups with filtering and public visibility

refactor: Add PromptGroups context provider and integrate it into relevant components

refactor: Optimize filter change handling and query invalidation in usePromptGroupsNav hook

refactor: Simplify context usage in FilterPrompts and GroupSidePanel components
This commit is contained in:
Danny Avila 2025-08-11 22:06:15 -04:00
parent 53c31b85d0
commit dcd96c29c5
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
22 changed files with 583 additions and 259 deletions

View file

@ -1 +1,2 @@
export * from './content';
export * from './prompts';

View file

@ -0,0 +1,150 @@
import { SystemCategories } from 'librechat-data-provider';
import type { IPromptGroupDocument as IPromptGroup } from '@librechat/data-schemas';
import type { Types } from 'mongoose';
import type { PromptGroupsListResponse } from '~/types';
/**
* Formats prompt groups for the paginated /groups endpoint response
*/
export function formatPromptGroupsResponse({
promptGroups = [],
pageNumber,
pageSize,
actualLimit,
hasMore = false,
after = null,
}: {
promptGroups: IPromptGroup[];
pageNumber?: string;
pageSize?: string;
actualLimit?: string | number;
hasMore?: boolean;
after?: string | null;
}): PromptGroupsListResponse {
const effectivePageSize = parseInt(pageSize || '') || parseInt(String(actualLimit || '')) || 10;
const totalPages =
promptGroups.length > 0 ? Math.ceil(promptGroups.length / effectivePageSize).toString() : '0';
return {
promptGroups,
pageNumber: pageNumber || '1',
pageSize: pageSize || String(actualLimit) || '10',
pages: totalPages,
has_more: hasMore,
after,
};
}
/**
* Creates an empty response for the paginated /groups endpoint
*/
export function createEmptyPromptGroupsResponse({
pageNumber,
pageSize,
actualLimit,
}: {
pageNumber?: string;
pageSize?: string;
actualLimit?: string | number;
}): PromptGroupsListResponse {
return {
promptGroups: [],
pageNumber: pageNumber || '1',
pageSize: pageSize || String(actualLimit) || '10',
pages: '0',
has_more: false,
after: null,
};
}
/**
* Marks prompt groups as public based on the publicly accessible IDs
*/
export function markPublicPromptGroups(
promptGroups: IPromptGroup[],
publiclyAccessibleIds: Types.ObjectId[],
): IPromptGroup[] {
if (!promptGroups.length) {
return [];
}
return promptGroups.map((group) => {
const isPublic = publiclyAccessibleIds.some((id) => id.equals(group._id?.toString()));
return isPublic ? ({ ...group, isPublic: true } as IPromptGroup) : group;
});
}
/**
* Builds filter object for prompt group queries
*/
export function buildPromptGroupFilter({
name,
category,
...otherFilters
}: {
name?: string;
category?: string;
[key: string]: string | number | boolean | RegExp | undefined;
}): {
filter: Record<string, string | number | boolean | RegExp | undefined>;
searchShared: boolean;
searchSharedOnly: boolean;
} {
const filter: Record<string, string | number | boolean | RegExp | undefined> = {
...otherFilters,
};
let searchShared = true;
let searchSharedOnly = false;
// Handle name filter - convert to regex for case-insensitive search
if (name) {
const escapeRegExp = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
filter.name = new RegExp(escapeRegExp(name), 'i');
}
// Handle category filters with special system categories
if (category === SystemCategories.MY_PROMPTS) {
searchShared = false;
} else if (category === SystemCategories.NO_CATEGORY) {
filter.category = '';
} else if (category === SystemCategories.SHARED_PROMPTS) {
searchSharedOnly = true;
} else if (category) {
filter.category = category;
}
return { filter, searchShared, searchSharedOnly };
}
/**
* Filters accessible IDs based on shared/public prompts logic
*/
export async function filterAccessibleIdsBySharedLogic({
accessibleIds,
searchShared,
searchSharedOnly,
publicPromptGroupIds,
}: {
accessibleIds: Types.ObjectId[];
searchShared: boolean;
searchSharedOnly: boolean;
publicPromptGroupIds?: Types.ObjectId[];
}): Promise<Types.ObjectId[]> {
const publicIdStrings = new Set((publicPromptGroupIds || []).map((id) => id.toString()));
if (!searchShared) {
// For MY_PROMPTS - exclude public prompts to show only user's own prompts
return accessibleIds.filter((id) => !publicIdStrings.has(id.toString()));
}
if (searchSharedOnly) {
// Handle SHARED_PROMPTS filter - only return public prompts that user has access to
if (!publicPromptGroupIds?.length) {
return [];
}
const accessibleIdStrings = new Set(accessibleIds.map((id) => id.toString()));
return publicPromptGroupIds.filter((id) => accessibleIdStrings.has(id.toString()));
}
return [...accessibleIds, ...(publicPromptGroupIds || [])];
}

View file

@ -4,5 +4,6 @@ export * from './error';
export * from './google';
export * from './mistral';
export * from './openai';
export * from './prompts';
export * from './run';
export * from './zod';

View file

@ -0,0 +1,24 @@
import type { IPromptGroup as IPromptGroup } from '@librechat/data-schemas';
import type { Types } from 'mongoose';
export interface PromptGroupsListResponse {
promptGroups: IPromptGroup[];
pageNumber: string;
pageSize: string;
pages: string;
has_more: boolean;
after: string | null;
}
export interface PromptGroupsAllResponse {
data: IPromptGroup[];
}
export interface AccessiblePromptGroupsResult {
object: 'list';
data: IPromptGroup[];
first_id: Types.ObjectId | null;
last_id: Types.ObjectId | null;
has_more: boolean;
after: string | null;
}