🎯 refactor: Centralize Agent Model Handling Across Conversation Lifecycle (#10956)

* refactor: Implement clearModelForNonEphemeralAgent utility for improved agent handling

- Introduced clearModelForNonEphemeralAgent function to manage model state for non-ephemeral agents across various components.
- Updated ModelSelectorContext to initialize model based on agent type.
- Enhanced useNavigateToConvo, useQueryParams, and useSelectMention hooks to clear model for non-ephemeral agents.
- Refactored buildDefaultConvo and endpoints utility to ensure proper handling of agent_id and model state.
- Improved overall conversation logic and state management for better performance and reliability.

* refactor: Enhance useNewConvo hook to improve agent conversation handling

- Added logic to skip access checks for existing agent conversations, utilizing localStorage to restore conversations after refresh.
- Improved handling of default endpoints for agents based on user access and existing conversation state, ensuring more reliable conversation initialization.

* refactor: Update ChatRoute and useAuthRedirect to include user roles

- Enhanced ChatRoute to utilize user roles for improved conversation state management.
- Modified useAuthRedirect to return user roles alongside authentication status, ensuring roles are available for conditional logic in ChatRoute.
- Adjusted conversation initialization logic to depend on the loaded roles, enhancing the reliability of the conversation setup.

* refactor: Update BaseClient to handle non-ephemeral agents in conversation logic

- Added a check for non-ephemeral agents in BaseClient, modifying the exceptions set to include 'model' when applicable.
- Enhanced conversation handling to improve flexibility based on agent type.

* test: Add mock for clearModelForNonEphemeralAgent in useQueryParams tests

- Introduced a mock for clearModelForNonEphemeralAgent to enhance testing of query parameters related to non-ephemeral agents.
- This addition supports improved test coverage and ensures proper handling of model state in relevant scenarios.

* refactor: Simplify mocks in useQueryParams tests for improved clarity

- Updated the mocking strategy for utilities in useQueryParams tests to use actual implementations where possible, while still suppressing test output for the logger.
- Enhanced the mock for tQueryParamsSchema to minimize complexity and avoid unnecessary validation during tests, improving test reliability and maintainability.

* refactor: Enhance agent identification logic in BaseClient for improved clarity

* chore: Import Constants in families.ts for enhanced functionality
This commit is contained in:
Danny Avila 2025-12-13 08:29:15 -05:00 committed by GitHub
parent 06719794f6
commit b5ab32c5ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 161 additions and 49 deletions

View file

@ -1,10 +1,12 @@
import {
Constants,
parseConvo,
EModelEndpoint,
isAssistantsEndpoint,
isAgentsEndpoint,
} from 'librechat-data-provider';
import type { TConversation, EndpointSchemaKey } from 'librechat-data-provider';
import { clearModelForNonEphemeralAgent } from './endpoints';
import { getLocalStorageItems } from './localStorage';
const buildDefaultConvo = ({
@ -66,10 +68,17 @@ const buildDefaultConvo = ({
// Ensures agent_id is always defined
const agentId = convo?.agent_id ?? '';
const defaultAgentId = lastConversationSetup?.agent_id ?? '';
if (isAgentsEndpoint(endpoint) && !defaultAgentId && agentId) {
if (
isAgentsEndpoint(endpoint) &&
agentId &&
(!defaultAgentId || defaultAgentId === Constants.EPHEMERAL_AGENT_ID)
) {
defaultConvo.agent_id = agentId;
}
// Clear model for non-ephemeral agents - agents use their configured model internally
clearModelForNonEphemeralAgent(defaultConvo);
defaultConvo.tools = lastConversationSetup?.tools ?? lastSelectedTools ?? defaultConvo.tools;
return defaultConvo;

View file

@ -11,6 +11,27 @@ import {
import type * as t from 'librechat-data-provider';
import type { LocalizeFunction, IconsRecord } from '~/common';
/**
* Clears model for non-ephemeral agent conversations.
* Agents use their configured model internally, so the conversation model should be undefined.
* Mutates the template in place.
*/
export function clearModelForNonEphemeralAgent<
T extends {
endpoint?: EModelEndpoint | string | null;
agent_id?: string | null;
model?: string | null;
},
>(template: T): void {
if (
isAgentsEndpoint(template.endpoint) &&
template.agent_id &&
template.agent_id !== Constants.EPHEMERAL_AGENT_ID
) {
template.model = undefined as T['model'];
}
}
export const getEntityName = ({
name = '',
localize,
@ -125,6 +146,18 @@ export function getConvoSwitchLogic(params: ConversationInitParams): InitiatedTe
conversationId: 'new',
};
// Reset agent_id if switching to a non-agents endpoint but template has a non-ephemeral agent_id
if (
!isAgentsEndpoint(newEndpoint) &&
template.agent_id &&
template.agent_id !== Constants.EPHEMERAL_AGENT_ID
) {
template.agent_id = Constants.EPHEMERAL_AGENT_ID;
}
// Clear model for non-ephemeral agents - agents use their configured model internally
clearModelForNonEphemeralAgent(template);
const isAssistantSwitch =
isAssistantsEndpoint(newEndpoint) &&
isAssistantsEndpoint(currentEndpoint) &&