LibreChat/client/src/utils/buildDefaultConvo.ts
Danny Avila 467df0f07a
🎭 feat: Override Custom Endpoint Schema with Specified Params Endpoint (#11788)
* 🔧 refactor: Simplify payload parsing and enhance getSaveOptions logic

- Removed unused bedrockInputSchema from payloadParser, streamlining the function.
- Updated payloadParser to handle optional chaining for model parameters.
- Enhanced getSaveOptions to ensure runOptions defaults to an empty object if parsing fails, improving robustness.
- Adjusted the assignment of maxContextTokens to use the instance variable for consistency.

* 🔧 fix: Update maxContextTokens assignment logic in initializeAgent function

- Enhanced the maxContextTokens assignment to allow for user-defined values, ensuring it defaults to a calculated value only when not provided or invalid. This change improves flexibility in agent initialization.

* 🧪 test: Add unit tests for initializeAgent function

- Introduced comprehensive unit tests for the initializeAgent function, focusing on maxContextTokens behavior.
- Tests cover scenarios for user-defined values, fallback calculations, and edge cases such as zero and negative values, enhancing overall test coverage and reliability of agent initialization logic.

* refactor: default params Endpoint Configuration Handling

- Integrated `getEndpointsConfig` to fetch endpoint configurations, allowing for dynamic handling of `defaultParamsEndpoint`.
- Updated `buildEndpointOption` to pass `defaultParamsEndpoint` to `parseCompactConvo`, ensuring correct parameter handling based on endpoint type.
- Added comprehensive unit tests for `buildDefaultConvo` and `cleanupPreset` to validate behavior with `defaultParamsEndpoint`, covering various scenarios and edge cases.
- Refactored related hooks and utility functions to support the new configuration structure, improving overall flexibility and maintainability.

* refactor: Centralize defaultParamsEndpoint retrieval

- Introduced `getDefaultParamsEndpoint` function to streamline the retrieval of `defaultParamsEndpoint` across various hooks and middleware.
- Updated multiple files to utilize the new function, enhancing code consistency and maintainability.
- Removed redundant logic for fetching `defaultParamsEndpoint`, simplifying the codebase.
2026-02-13 23:04:51 -05:00

90 lines
2.5 KiB
TypeScript

import {
parseConvo,
EModelEndpoint,
isAgentsEndpoint,
isEphemeralAgentId,
isAssistantsEndpoint,
} from 'librechat-data-provider';
import type { TConversation, EndpointSchemaKey } from 'librechat-data-provider';
import { clearModelForNonEphemeralAgent } from './endpoints';
import { getLocalStorageItems } from './localStorage';
const buildDefaultConvo = ({
models,
conversation,
endpoint = null,
lastConversationSetup,
defaultParamsEndpoint,
}: {
models: string[];
conversation: TConversation;
endpoint?: EModelEndpoint | null;
lastConversationSetup: TConversation | null;
defaultParamsEndpoint?: string | null;
}): TConversation => {
const { lastSelectedModel, lastSelectedTools } = getLocalStorageItems();
const endpointType = lastConversationSetup?.endpointType ?? conversation.endpointType;
if (!endpoint) {
return {
...conversation,
endpointType,
endpoint,
};
}
const availableModels = models;
const model = lastConversationSetup?.model ?? lastSelectedModel?.[endpoint] ?? '';
let possibleModels: string[];
if (availableModels.includes(model)) {
possibleModels = [model, ...availableModels];
} else {
possibleModels = [...availableModels];
}
const convo = parseConvo({
endpoint: endpoint as EndpointSchemaKey,
endpointType: endpointType as EndpointSchemaKey,
conversation: lastConversationSetup,
possibleValues: {
models: possibleModels,
},
defaultParamsEndpoint,
});
const defaultConvo = {
...conversation,
...convo,
endpointType,
endpoint,
};
// Ensures assistant_id is always defined
const assistantId = convo?.assistant_id ?? conversation?.assistant_id ?? '';
const defaultAssistantId = lastConversationSetup?.assistant_id ?? '';
if (isAssistantsEndpoint(endpoint) && !defaultAssistantId && assistantId) {
defaultConvo.assistant_id = assistantId;
}
// Ensures agent_id is always defined
const agentId = convo?.agent_id ?? '';
const defaultAgentId = lastConversationSetup?.agent_id ?? '';
if (
isAgentsEndpoint(endpoint) &&
agentId &&
(!defaultAgentId || isEphemeralAgentId(defaultAgentId))
) {
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;
};
export default buildDefaultConvo;