mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-23 20:00:15 +01:00
feat: add support for request body placeholders in custom endpoint headers
- Add {{LIBRECHAT_BODY_*}} placeholders for conversationId, parentMessageId, messageId
- Update tests to reflect new body placeholder functionality
This commit is contained in:
parent
3508839d6d
commit
eec10bf745
5 changed files with 58 additions and 34 deletions
|
|
@ -28,23 +28,7 @@ const initializeClient = async ({ req, res, endpointOption, optionsOnly, overrid
|
|||
const CUSTOM_API_KEY = extractEnvVariable(endpointConfig.apiKey);
|
||||
const CUSTOM_BASE_URL = extractEnvVariable(endpointConfig.baseURL);
|
||||
|
||||
const customUserVars = {};
|
||||
if (req.body.conversationId) {
|
||||
customUserVars.LIBRECHAT_CONVERSATION_ID = req.body.conversationId;
|
||||
}
|
||||
|
||||
let resolvedHeaders = resolveHeaders(endpointConfig.headers, req.user, customUserVars);
|
||||
|
||||
// Filter out headers with unresolved placeholders
|
||||
const filteredHeaders = {};
|
||||
for (const [key, value] of Object.entries(resolvedHeaders)) {
|
||||
if (typeof value === 'string' && value.includes('{{') && value.includes('}}')) {
|
||||
continue;
|
||||
}
|
||||
filteredHeaders[key] = value;
|
||||
}
|
||||
|
||||
resolvedHeaders = filteredHeaders;
|
||||
let resolvedHeaders = resolveHeaders(endpointConfig.headers, req.user, undefined, req.body);
|
||||
|
||||
if (CUSTOM_API_KEY.match(envVarRegex)) {
|
||||
throw new Error(`Missing API Key for ${endpoint}.`);
|
||||
|
|
|
|||
|
|
@ -64,26 +64,14 @@ describe('custom/initializeClient', () => {
|
|||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('calls resolveHeaders with conversation ID when provided', async () => {
|
||||
const { resolveHeaders } = require('@librechat/api');
|
||||
const requestWithConversationId = {
|
||||
...mockRequest,
|
||||
body: { ...mockRequest.body, conversationId: 'existing-conversation-123' },
|
||||
};
|
||||
await initializeClient({ req: requestWithConversationId, res: mockResponse, optionsOnly: true });
|
||||
expect(resolveHeaders).toHaveBeenCalledWith(
|
||||
{ 'x-user': '{{LIBRECHAT_USER_ID}}', 'x-email': '{{LIBRECHAT_USER_EMAIL}}' },
|
||||
{ id: 'user-123', email: 'test@example.com' },
|
||||
{ LIBRECHAT_CONVERSATION_ID: 'existing-conversation-123' },
|
||||
);
|
||||
});
|
||||
|
||||
it('calls resolveHeaders with headers and user', async () => {
|
||||
it('calls resolveHeaders with headers, user, and body for body placeholder support', async () => {
|
||||
const { resolveHeaders } = require('@librechat/api');
|
||||
await initializeClient({ req: mockRequest, res: mockResponse, optionsOnly: true });
|
||||
expect(resolveHeaders).toHaveBeenCalledWith(
|
||||
{ 'x-user': '{{LIBRECHAT_USER_ID}}', 'x-email': '{{LIBRECHAT_USER_EMAIL}}' },
|
||||
{ id: 'user-123', email: 'test@example.com' },
|
||||
undefined, // customUserVars
|
||||
{ endpoint: 'test-endpoint' }, // body - supports {{LIBRECHAT_BODY_*}} placeholders
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue