Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🎯 refactor: Custom Endpoint Request-based Header Resolution (#9344)

Danny Avila43add11b056c
diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.js
index 804fe34fac..1bf9e07bd8 100644
--- a/api/server/controllers/agents/client.js
+++ b/api/server/controllers/agents/client.js
@@ -7,6 +7,7 @@ const {
createRun,
Tokenizer,
checkAccess,
+ resolveHeaders,
getBalanceConfig,
memoryInstructions,
formatContentStrings,
@@ -879,6 +880,16 @@ class AgentClient extends BaseClient {
memoryPromise = this.runMemory(messages);
}
+ /** Resolve request-based headers for Custom Endpoints. Note: if this is added to
+ * non-custom endpoints, needs consideration of varying provider header configs.
+ */
+ if (agent.model_parameters?.configuration?.defaultHeaders != null) {
+ agent.model_parameters.configuration.defaultHeaders = resolveHeaders({
+ headers: agent.model_parameters.configuration.defaultHeaders,
+ body: config.configurable.requestBody,
+ });
+ }
+
run = await createRun({
agent,
req: this.options.req,
@@ -1181,6 +1192,20 @@ class AgentClient extends BaseClient {
clientOptions.json = true;
}
+ /** Resolve request-based headers for Custom Endpoints. Note: if this is added to
+ * non-custom endpoints, needs consideration of varying provider header configs.
+ */
+ if (clientOptions?.configuration?.defaultHeaders != null) {
+ clientOptions.configuration.defaultHeaders = resolveHeaders({
+ headers: clientOptions.configuration.defaultHeaders,
+ body: {
+ messageId: this.responseMessageId,
+ conversationId: this.conversationId,
+ parentMessageId: this.parentMessageId,
+ },
+ });
+ }
+
try {
const titleResult = await this.run.generateTitle({
provider,
diff --git a/api/server/services/Endpoints/custom/initialize.js b/api/server/services/Endpoints/custom/initialize.js
index 4a1026636f..e2a092ad55 100644
--- a/api/server/services/Endpoints/custom/initialize.js
+++ b/api/server/services/Endpoints/custom/initialize.js
@@ -36,10 +36,12 @@ const initializeClient = async ({ req, res, endpointOption, optionsOnly, overrid
const CUSTOM_API_KEY = extractEnvVariable(endpointConfig.apiKey);
const CUSTOM_BASE_URL = extractEnvVariable(endpointConfig.baseURL);
+ /** Intentionally excludes passing `body`, i.e. `req.body`, as
+ * values may not be accurate until `AgentClient` is initialized
+ */
let resolvedHeaders = resolveHeaders({
headers: endpointConfig.headers,
user: req.user,
- body: req.body,
});
if (CUSTOM_API_KEY.match(envVarRegex)) {
diff --git a/api/server/services/Endpoints/custom/initialize.spec.js b/api/server/services/Endpoints/custom/initialize.spec.js
index 38d65a158a..8b4a1303ee 100644
--- a/api/server/services/Endpoints/custom/initialize.spec.js
+++ b/api/server/services/Endpoints/custom/initialize.spec.js
@@ -76,7 +76,10 @@ describe('custom/initializeClient', () => {
expect(resolveHeaders).toHaveBeenCalledWith({
headers: { 'x-user': '{{LIBRECHAT_USER_ID}}', 'x-email': '{{LIBRECHAT_USER_EMAIL}}' },
user: { id: 'user-123', email: '[email protected]', role: 'user' },
+ /**
+ * Note: Request-based Header Resolution is deferred until right before LLM request is made
body: { endpoint: 'test-endpoint' }, // body - supports {{LIBRECHAT_BODY_*}} placeholders
+ */
});
});