🦾 refactor: filter Model Specs based on user access to Agents (#9433)

This commit is contained in:
Danny Avila 2025-09-03 02:59:57 -04:00 committed by GitHub
parent 122ff416ac
commit 45da421e7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 4 deletions

View file

@ -1,6 +1,6 @@
import debounce from 'lodash/debounce'; import debounce from 'lodash/debounce';
import React, { createContext, useContext, useState, useMemo } from 'react'; import React, { createContext, useContext, useState, useMemo } from 'react';
import { isAgentsEndpoint, isAssistantsEndpoint } from 'librechat-data-provider'; import { EModelEndpoint, isAgentsEndpoint, isAssistantsEndpoint } from 'librechat-data-provider';
import type * as t from 'librechat-data-provider'; import type * as t from 'librechat-data-provider';
import type { Endpoint, SelectedValues } from '~/common'; import type { Endpoint, SelectedValues } from '~/common';
import { import {
@ -59,7 +59,25 @@ export function ModelSelectorProvider({ children, startupConfig }: ModelSelector
const { data: endpointsConfig } = useGetEndpointsQuery(); const { data: endpointsConfig } = useGetEndpointsQuery();
const { endpoint, model, spec, agent_id, assistant_id, newConversation } = const { endpoint, model, spec, agent_id, assistant_id, newConversation } =
useModelSelectorChatContext(); useModelSelectorChatContext();
const modelSpecs = useMemo(() => startupConfig?.modelSpecs?.list ?? [], [startupConfig]); const modelSpecs = useMemo(() => {
const specs = startupConfig?.modelSpecs?.list ?? [];
if (!agentsMap) {
return specs;
}
/**
* Filter modelSpecs to only include agents the user has access to.
* Use agentsMap which already contains permission-filtered agents (consistent with other components).
*/
return specs.filter((spec) => {
if (spec.preset?.endpoint === EModelEndpoint.agents && spec.preset?.agent_id) {
return spec.preset.agent_id in agentsMap;
}
/** Keep non-agent modelSpecs */
return true;
});
}, [startupConfig, agentsMap]);
const permissionLevel = useAgentDefaultPermissionLevel(); const permissionLevel = useAgentDefaultPermissionLevel();
const { data: agents = null } = useListAgentsQuery( const { data: agents = null } = useListAgentsQuery(
{ requiredPermission: permissionLevel }, { requiredPermission: permissionLevel },

View file

@ -3,12 +3,12 @@ import { useGetModelsQuery } from 'librechat-data-provider/react-query';
import { import {
Permissions, Permissions,
alternateName, alternateName,
PermissionBits,
EModelEndpoint, EModelEndpoint,
PermissionTypes, PermissionTypes,
isAgentsEndpoint, isAgentsEndpoint,
getConfigDefaults, getConfigDefaults,
isAssistantsEndpoint, isAssistantsEndpoint,
PermissionBits,
} from 'librechat-data-provider'; } from 'librechat-data-provider';
import type { TAssistantsMap, TEndpointsConfig } from 'librechat-data-provider'; import type { TAssistantsMap, TEndpointsConfig } from 'librechat-data-provider';
import type { MentionOption } from '~/common'; import type { MentionOption } from '~/common';
@ -19,6 +19,7 @@ import {
useGetStartupConfig, useGetStartupConfig,
} from '~/data-provider'; } from '~/data-provider';
import useAssistantListMap from '~/hooks/Assistants/useAssistantListMap'; import useAssistantListMap from '~/hooks/Assistants/useAssistantListMap';
import { useAgentsMapContext } from '~/Providers/AgentsMapContext';
import { mapEndpoints, getPresetTitle } from '~/utils'; import { mapEndpoints, getPresetTitle } from '~/utils';
import { EndpointIcon } from '~/components/Endpoints'; import { EndpointIcon } from '~/components/Endpoints';
import useHasAccess from '~/hooks/Roles/useHasAccess'; import useHasAccess from '~/hooks/Roles/useHasAccess';
@ -62,6 +63,7 @@ export default function useMentions({
permission: Permissions.USE, permission: Permissions.USE,
}); });
const agentsMap = useAgentsMapContext();
const { data: presets } = useGetPresetsQuery(); const { data: presets } = useGetPresetsQuery();
const { data: modelsConfig } = useGetModelsQuery(); const { data: modelsConfig } = useGetModelsQuery();
const { data: startupConfig } = useGetStartupConfig(); const { data: startupConfig } = useGetStartupConfig();
@ -129,7 +131,24 @@ export default function useMentions({
[listMap, assistantMap, endpointsConfig], [listMap, assistantMap, endpointsConfig],
); );
const modelSpecs = useMemo(() => startupConfig?.modelSpecs?.list ?? [], [startupConfig]); const modelSpecs = useMemo(() => {
const specs = startupConfig?.modelSpecs?.list ?? [];
if (!agentsMap) {
return specs;
}
/**
* Filter modelSpecs to only include agents the user has access to.
* Use agentsMap which already contains permission-filtered agents (consistent with other components).
*/
return specs.filter((spec) => {
if (spec.preset?.endpoint === EModelEndpoint.agents && spec.preset?.agent_id) {
return spec.preset.agent_id in agentsMap;
}
/** Keep non-agent modelSpecs */
return true;
});
}, [startupConfig, agentsMap]);
const options: MentionOption[] = useMemo(() => { const options: MentionOption[] = useMemo(() => {
let validEndpoints = endpoints; let validEndpoints = endpoints;