Spaces:
Running
Running
File size: 1,432 Bytes
bf8b26e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import type { LoaderFunction } from '@remix-run/cloudflare';
import { LLMManager } from '~/lib/modules/llm/manager';
import { getApiKeysFromCookie } from '~/lib/api/cookies';
export const loader: LoaderFunction = async ({ context, request }) => {
// Get API keys from cookie
const cookieHeader = request.headers.get('Cookie');
const apiKeysFromCookie = getApiKeysFromCookie(cookieHeader);
// Initialize the LLM manager to access environment variables
const llmManager = LLMManager.getInstance(context?.cloudflare?.env as any);
// Get all provider instances to find their API token keys
const providers = llmManager.getAllProviders();
// Create a comprehensive API keys object
const apiKeys: Record<string, string> = { ...apiKeysFromCookie };
// For each provider, check all possible sources for API keys
for (const provider of providers) {
if (!provider.config.apiTokenKey) {
continue;
}
const envVarName = provider.config.apiTokenKey;
// Skip if we already have this provider's key from cookies
if (apiKeys[provider.name]) {
continue;
}
// Check environment variables in order of precedence
const envValue =
(context?.cloudflare?.env as Record<string, any>)?.[envVarName] ||
process.env[envVarName] ||
llmManager.env[envVarName];
if (envValue) {
apiKeys[provider.name] = envValue;
}
}
return Response.json(apiKeys);
};
|