Skip to content

Commit 2dfb523

Browse files
author
Gopal Sharma
committed
feat: Add conversation ID support to custom endpoint headers
- Add LIBRECHAT_CONVERSATION_ID to customUserVars when provided - Pass conversation ID to header resolution for dynamic headers - Add comprehensive test coverage Enables custom endpoints to access conversation context using {{LIBRECHAT_CONVERSATION_ID}} placeholder.
1 parent faaba30 commit 2dfb523

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

api/server/services/Endpoints/custom/initialize.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ const initializeClient = async ({ req, res, endpointOption, optionsOnly, overrid
2828
const CUSTOM_API_KEY = extractEnvVariable(endpointConfig.apiKey);
2929
const CUSTOM_BASE_URL = extractEnvVariable(endpointConfig.baseURL);
3030

31-
let resolvedHeaders = resolveHeaders(endpointConfig.headers, req.user);
31+
const customUserVars = {};
32+
customUserVars.LIBRECHAT_CONVERSATION_ID = req.body.conversationId;
33+
34+
let resolvedHeaders = resolveHeaders(endpointConfig.headers, req.user, customUserVars);
3235

3336
if (CUSTOM_API_KEY.match(envVarRegex)) {
3437
throw new Error(`Missing API Key for ${endpoint}.`);

api/server/services/Endpoints/custom/initialize.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ describe('custom/initializeClient', () => {
6464
jest.clearAllMocks();
6565
});
6666

67+
it('calls resolveHeaders with conversation ID when provided', async () => {
68+
const { resolveHeaders } = require('@librechat/api');
69+
const requestWithConversationId = {
70+
...mockRequest,
71+
body: { ...mockRequest.body, conversationId: 'existing-conversation-123' },
72+
};
73+
await initializeClient({ req: requestWithConversationId, res: mockResponse, optionsOnly: true });
74+
expect(resolveHeaders).toHaveBeenCalledWith(
75+
{ 'x-user': '{{LIBRECHAT_USER_ID}}', 'x-email': '{{LIBRECHAT_USER_EMAIL}}' },
76+
{ id: 'user-123', email: '[email protected]' },
77+
{ LIBRECHAT_CONVERSATION_ID: 'existing-conversation-123' },
78+
);
79+
});
80+
6781
it('calls resolveHeaders with headers and user', async () => {
6882
const { resolveHeaders } = require('@librechat/api');
6983
await initializeClient({ req: mockRequest, res: mockResponse, optionsOnly: true });

0 commit comments

Comments
 (0)