Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to dynamically pass the email address of users to HTTP headers of custom endpoints #4488

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions api/server/services/Endpoints/custom/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { fetchModels } = require('~/server/services/ModelService');
const { isUserProvided, sleep } = require('~/server/utils');
const getLogStores = require('~/cache/getLogStores');
const { OpenAIClient } = require('~/app');
const User = require('~/models/User');

const { PROXY } = process.env;

Expand All @@ -28,13 +29,31 @@ const initializeClient = async ({ req, res, endpointOption, optionsOnly, overrid
const CUSTOM_API_KEY = extractEnvVariable(endpointConfig.apiKey);
const CUSTOM_BASE_URL = extractEnvVariable(endpointConfig.baseURL);

const replaceUserEmail = async (metadata, userId) => { // function to replace ${USER_EMAIL} with the user's email
try {
const user = await User.findById(userId).exec(); // Fetch user by ID from database
return user?.email && metadata.includes('${USER_EMAIL}') // Check if user has an email and if metadata contains ${USER_EMAIL}
? metadata.replace('${USER_EMAIL}', user.email)
: metadata; // Return unchanged metadata if conditions aren't met
} catch {
throw new Error('User not found');
}
};

let resolvedHeaders = {};
if (endpointConfig.headers && typeof endpointConfig.headers === 'object') {
Object.keys(endpointConfig.headers).forEach((key) => {
resolvedHeaders[key] = extractEnvVariable(endpointConfig.headers[key]);
});
await Promise.all(Object.entries(endpointConfig.headers).map(async ([key, value]) => {
try {
resolvedHeaders[key] = value.includes('${USER_EMAIL}') // Check if header value contains ${USER_EMAIL}
? await replaceUserEmail(value, req.user.id) // Replace ${USER_EMAIL} with user's email if present
: extractEnvVariable(value);
} catch {
resolvedHeaders[key] = 'null'; // Fallback to 'null' if an error occurs during processing
}
})
);
}

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