Merge pull request #816 from thecodacus/env-file-fix

fix: fixed model not loading/working, even after baseUrl set in .env file
This commit is contained in:
Anirban Kar 2024-12-18 21:58:32 +05:30 committed by GitHub
commit 50e6778784
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 326 additions and 117 deletions

View File

@ -119,6 +119,9 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
useEffect(() => { useEffect(() => {
// Load API keys from cookies on component mount // Load API keys from cookies on component mount
let parsedApiKeys: Record<string, string> | undefined = {};
try { try {
const storedApiKeys = Cookies.get('apiKeys'); const storedApiKeys = Cookies.get('apiKeys');
@ -127,6 +130,7 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
if (typeof parsedKeys === 'object' && parsedKeys !== null) { if (typeof parsedKeys === 'object' && parsedKeys !== null) {
setApiKeys(parsedKeys); setApiKeys(parsedKeys);
parsedApiKeys = parsedKeys;
} }
} }
} catch (error) { } catch (error) {
@ -155,7 +159,7 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
Cookies.remove('providers'); Cookies.remove('providers');
} }
initializeModelList(providerSettings).then((modelList) => { initializeModelList({ apiKeys: parsedApiKeys, providerSettings }).then((modelList) => {
setModelList(modelList); setModelList(modelList);
}); });

View File

@ -2,6 +2,7 @@ import React, { useCallback, useEffect, useState } from 'react';
import { useSettings } from '~/lib/hooks/useSettings'; import { useSettings } from '~/lib/hooks/useSettings';
import commit from '~/commit.json'; import commit from '~/commit.json';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
import { providerBaseUrlEnvKeys } from '~/utils/constants';
interface ProviderStatus { interface ProviderStatus {
name: string; name: string;
@ -236,7 +237,7 @@ const checkProviderStatus = async (url: string | null, providerName: string): Pr
} }
// Try different endpoints based on provider // Try different endpoints based on provider
const checkUrls = [`${url}/api/health`, `${url}/v1/models`]; const checkUrls = [`${url}/api/health`, url.endsWith('v1') ? `${url}/models` : `${url}/v1/models`];
console.log(`[Debug] Checking additional endpoints:`, checkUrls); console.log(`[Debug] Checking additional endpoints:`, checkUrls);
const results = await Promise.all( const results = await Promise.all(
@ -321,14 +322,16 @@ export default function DebugTab() {
.filter(([, provider]) => LOCAL_PROVIDERS.includes(provider.name)) .filter(([, provider]) => LOCAL_PROVIDERS.includes(provider.name))
.map(async ([, provider]) => { .map(async ([, provider]) => {
const envVarName = const envVarName =
provider.name.toLowerCase() === 'ollama' providerBaseUrlEnvKeys[provider.name].baseUrlKey || `REACT_APP_${provider.name.toUpperCase()}_URL`;
? 'OLLAMA_API_BASE_URL'
: provider.name.toLowerCase() === 'lmstudio'
? 'LMSTUDIO_API_BASE_URL'
: `REACT_APP_${provider.name.toUpperCase()}_URL`;
// Access environment variables through import.meta.env // Access environment variables through import.meta.env
const url = import.meta.env[envVarName] || provider.settings.baseUrl || null; // Ensure baseUrl is used let settingsUrl = provider.settings.baseUrl;
if (settingsUrl && settingsUrl.trim().length === 0) {
settingsUrl = undefined;
}
const url = settingsUrl || import.meta.env[envVarName] || null; // Ensure baseUrl is used
console.log(`[Debug] Using URL for ${provider.name}:`, url, `(from ${envVarName})`); console.log(`[Debug] Using URL for ${provider.name}:`, url, `(from ${envVarName})`);
const status = await checkProviderStatus(url, provider.name); const status = await checkProviderStatus(url, provider.name);

View File

@ -7,6 +7,7 @@ import { logStore } from '~/lib/stores/logs';
// Import a default fallback icon // Import a default fallback icon
import DefaultIcon from '/icons/Default.svg'; // Adjust the path as necessary import DefaultIcon from '/icons/Default.svg'; // Adjust the path as necessary
import { providerBaseUrlEnvKeys } from '~/utils/constants';
export default function ProvidersTab() { export default function ProvidersTab() {
const { providers, updateProviderSettings, isLocalModel } = useSettings(); const { providers, updateProviderSettings, isLocalModel } = useSettings();
@ -47,60 +48,77 @@ export default function ProvidersTab() {
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor" className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
/> />
</div> </div>
{filteredProviders.map((provider) => ( {filteredProviders.map((provider) => {
<div const envBaseUrlKey = providerBaseUrlEnvKeys[provider.name].baseUrlKey;
key={provider.name} const envBaseUrl = envBaseUrlKey ? import.meta.env[envBaseUrlKey] : undefined;
className="flex flex-col mb-2 provider-item hover:bg-bolt-elements-bg-depth-3 p-4 rounded-lg border border-bolt-elements-borderColor "
>
<div className="flex items-center justify-between mb-2">
<div className="flex items-center gap-2">
<img
src={`/icons/${provider.name}.svg`} // Attempt to load the specific icon
onError={(e) => {
// Fallback to default icon on error
e.currentTarget.src = DefaultIcon;
}}
alt={`${provider.name} icon`}
className="w-6 h-6 dark:invert"
/>
<span className="text-bolt-elements-textPrimary">{provider.name}</span>
</div>
<Switch
className="ml-auto"
checked={provider.settings.enabled}
onCheckedChange={(enabled) => {
updateProviderSettings(provider.name, { ...provider.settings, enabled });
if (enabled) { return (
logStore.logProvider(`Provider ${provider.name} enabled`, { provider: provider.name }); <div
} else { key={provider.name}
logStore.logProvider(`Provider ${provider.name} disabled`, { provider: provider.name }); className="flex flex-col mb-2 provider-item hover:bg-bolt-elements-bg-depth-3 p-4 rounded-lg border border-bolt-elements-borderColor "
} >
}} <div className="flex items-center justify-between mb-2">
/> <div className="flex items-center gap-2">
</div> <img
{/* Base URL input for configurable providers */} src={`/icons/${provider.name}.svg`} // Attempt to load the specific icon
{URL_CONFIGURABLE_PROVIDERS.includes(provider.name) && provider.settings.enabled && ( onError={(e) => {
<div className="mt-2"> // Fallback to default icon on error
<label className="block text-sm text-bolt-elements-textSecondary mb-1">Base URL:</label> e.currentTarget.src = DefaultIcon;
<input }}
type="text" alt={`${provider.name} icon`}
value={provider.settings.baseUrl || ''} className="w-6 h-6 dark:invert"
onChange={(e) => { />
const newBaseUrl = e.target.value; <span className="text-bolt-elements-textPrimary">{provider.name}</span>
updateProviderSettings(provider.name, { ...provider.settings, baseUrl: newBaseUrl }); </div>
logStore.logProvider(`Base URL updated for ${provider.name}`, { <Switch
provider: provider.name, className="ml-auto"
baseUrl: newBaseUrl, checked={provider.settings.enabled}
}); onCheckedChange={(enabled) => {
updateProviderSettings(provider.name, { ...provider.settings, enabled });
if (enabled) {
logStore.logProvider(`Provider ${provider.name} enabled`, { provider: provider.name });
} else {
logStore.logProvider(`Provider ${provider.name} disabled`, { provider: provider.name });
}
}} }}
placeholder={`Enter ${provider.name} base URL`}
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
/> />
</div> </div>
)} {/* Base URL input for configurable providers */}
</div> {URL_CONFIGURABLE_PROVIDERS.includes(provider.name) && provider.settings.enabled && (
))} <div className="mt-2">
{envBaseUrl && (
<label className="block text-xs text-bolt-elements-textSecondary text-green-300 mb-2">
Set On (.env) : {envBaseUrl}
</label>
)}
<label className="block text-sm text-bolt-elements-textSecondary mb-2">
{envBaseUrl ? 'Override Base Url' : 'Base URL '}:{' '}
</label>
<input
type="text"
value={provider.settings.baseUrl || ''}
onChange={(e) => {
let newBaseUrl: string | undefined = e.target.value;
if (newBaseUrl && newBaseUrl.trim().length === 0) {
newBaseUrl = undefined;
}
updateProviderSettings(provider.name, { ...provider.settings, baseUrl: newBaseUrl });
logStore.logProvider(`Base URL updated for ${provider.name}`, {
provider: provider.name,
baseUrl: newBaseUrl,
});
}}
placeholder={`Enter ${provider.name} base URL`}
className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
/>
</div>
)}
</div>
);
})}
</div> </div>
); );
} }

View File

@ -14,7 +14,7 @@ export default async function handleRequest(
remixContext: EntryContext, remixContext: EntryContext,
_loadContext: AppLoadContext, _loadContext: AppLoadContext,
) { ) {
await initializeModelList(); await initializeModelList({});
const readable = await renderToReadableStream(<RemixServer context={remixContext} url={request.url} />, { const readable = await renderToReadableStream(<RemixServer context={remixContext} url={request.url} />, {
signal: request.signal, signal: request.signal,

View File

@ -3,6 +3,8 @@
* Preventing TS checks with files presented in the video for a better presentation. * Preventing TS checks with files presented in the video for a better presentation.
*/ */
import { env } from 'node:process'; import { env } from 'node:process';
import type { IProviderSetting } from '~/types/model';
import { getProviderBaseUrlAndKey } from '~/utils/constants';
export function getAPIKey(cloudflareEnv: Env, provider: string, userApiKeys?: Record<string, string>) { export function getAPIKey(cloudflareEnv: Env, provider: string, userApiKeys?: Record<string, string>) {
/** /**
@ -15,7 +17,20 @@ export function getAPIKey(cloudflareEnv: Env, provider: string, userApiKeys?: Re
return userApiKeys[provider]; return userApiKeys[provider];
} }
// Fall back to environment variables const { apiKey } = getProviderBaseUrlAndKey({
provider,
apiKeys: userApiKeys,
providerSettings: undefined,
serverEnv: cloudflareEnv as any,
defaultBaseUrlKey: '',
defaultApiTokenKey: '',
});
if (apiKey) {
return apiKey;
}
// Fall back to hardcoded environment variables names
switch (provider) { switch (provider) {
case 'Anthropic': case 'Anthropic':
return env.ANTHROPIC_API_KEY || cloudflareEnv.ANTHROPIC_API_KEY; return env.ANTHROPIC_API_KEY || cloudflareEnv.ANTHROPIC_API_KEY;
@ -50,16 +65,43 @@ export function getAPIKey(cloudflareEnv: Env, provider: string, userApiKeys?: Re
} }
} }
export function getBaseURL(cloudflareEnv: Env, provider: string) { export function getBaseURL(cloudflareEnv: Env, provider: string, providerSettings?: Record<string, IProviderSetting>) {
const { baseUrl } = getProviderBaseUrlAndKey({
provider,
apiKeys: {},
providerSettings,
serverEnv: cloudflareEnv as any,
defaultBaseUrlKey: '',
defaultApiTokenKey: '',
});
if (baseUrl) {
return baseUrl;
}
let settingBaseUrl = providerSettings?.[provider].baseUrl;
if (settingBaseUrl && settingBaseUrl.length == 0) {
settingBaseUrl = undefined;
}
switch (provider) { switch (provider) {
case 'Together': case 'Together':
return env.TOGETHER_API_BASE_URL || cloudflareEnv.TOGETHER_API_BASE_URL || 'https://api.together.xyz/v1'; return (
settingBaseUrl ||
env.TOGETHER_API_BASE_URL ||
cloudflareEnv.TOGETHER_API_BASE_URL ||
'https://api.together.xyz/v1'
);
case 'OpenAILike': case 'OpenAILike':
return env.OPENAI_LIKE_API_BASE_URL || cloudflareEnv.OPENAI_LIKE_API_BASE_URL; return settingBaseUrl || env.OPENAI_LIKE_API_BASE_URL || cloudflareEnv.OPENAI_LIKE_API_BASE_URL;
case 'LMStudio': case 'LMStudio':
return env.LMSTUDIO_API_BASE_URL || cloudflareEnv.LMSTUDIO_API_BASE_URL || 'http://localhost:1234'; return (
settingBaseUrl || env.LMSTUDIO_API_BASE_URL || cloudflareEnv.LMSTUDIO_API_BASE_URL || 'http://localhost:1234'
);
case 'Ollama': { case 'Ollama': {
let baseUrl = env.OLLAMA_API_BASE_URL || cloudflareEnv.OLLAMA_API_BASE_URL || 'http://localhost:11434'; let baseUrl =
settingBaseUrl || env.OLLAMA_API_BASE_URL || cloudflareEnv.OLLAMA_API_BASE_URL || 'http://localhost:11434';
if (env.RUNNING_IN_DOCKER === 'true') { if (env.RUNNING_IN_DOCKER === 'true') {
baseUrl = baseUrl.replace('localhost', 'host.docker.internal'); baseUrl = baseUrl.replace('localhost', 'host.docker.internal');

View File

@ -140,7 +140,7 @@ export function getPerplexityModel(apiKey: OptionalApiKey, model: string) {
export function getModel( export function getModel(
provider: string, provider: string,
model: string, model: string,
env: Env, serverEnv: Env,
apiKeys?: Record<string, string>, apiKeys?: Record<string, string>,
providerSettings?: Record<string, IProviderSetting>, providerSettings?: Record<string, IProviderSetting>,
) { ) {
@ -148,9 +148,12 @@ export function getModel(
* let apiKey; // Declare first * let apiKey; // Declare first
* let baseURL; * let baseURL;
*/ */
// console.log({provider,model});
const apiKey = getAPIKey(env, provider, apiKeys); // Then assign const apiKey = getAPIKey(serverEnv, provider, apiKeys); // Then assign
const baseURL = providerSettings?.[provider].baseUrl || getBaseURL(env, provider); const baseURL = getBaseURL(serverEnv, provider, providerSettings);
// console.log({apiKey,baseURL});
switch (provider) { switch (provider) {
case 'Anthropic': case 'Anthropic':

View File

@ -151,10 +151,13 @@ export async function streamText(props: {
providerSettings?: Record<string, IProviderSetting>; providerSettings?: Record<string, IProviderSetting>;
promptId?: string; promptId?: string;
}) { }) {
const { messages, env, options, apiKeys, files, providerSettings, promptId } = props; const { messages, env: serverEnv, options, apiKeys, files, providerSettings, promptId } = props;
// console.log({serverEnv});
let currentModel = DEFAULT_MODEL; let currentModel = DEFAULT_MODEL;
let currentProvider = DEFAULT_PROVIDER.name; let currentProvider = DEFAULT_PROVIDER.name;
const MODEL_LIST = await getModelList(apiKeys || {}, providerSettings); const MODEL_LIST = await getModelList({ apiKeys, providerSettings, serverEnv: serverEnv as any });
const processedMessages = messages.map((message) => { const processedMessages = messages.map((message) => {
if (message.role === 'user') { if (message.role === 'user') {
const { model, provider, content } = extractPropertiesFromMessage(message); const { model, provider, content } = extractPropertiesFromMessage(message);
@ -196,7 +199,7 @@ export async function streamText(props: {
} }
return _streamText({ return _streamText({
model: getModel(currentProvider, currentModel, env, apiKeys, providerSettings) as any, model: getModel(currentProvider, currentModel, serverEnv, apiKeys, providerSettings) as any,
system: systemPrompt, system: systemPrompt,
maxTokens: dynamicMaxTokens, maxTokens: dynamicMaxTokens,
messages: convertToCoreMessages(processedMessages as any), messages: convertToCoreMessages(processedMessages as any),

View File

@ -92,6 +92,7 @@ export function useEditChatDescription({
} }
const lengthValid = trimmedDesc.length > 0 && trimmedDesc.length <= 100; const lengthValid = trimmedDesc.length > 0 && trimmedDesc.length <= 100;
// Allow letters, numbers, spaces, and common punctuation but exclude characters that could cause issues // Allow letters, numbers, spaces, and common punctuation but exclude characters that could cause issues
const characterValid = /^[a-zA-Z0-9\s\-_.,!?()[\]{}'"]+$/.test(trimmedDesc); const characterValid = /^[a-zA-Z0-9\s\-_.,!?()[\]{}'"]+$/.test(trimmedDesc);

View File

@ -3,7 +3,12 @@ import type { ModelInfo } from '~/utils/types';
export type ProviderInfo = { export type ProviderInfo = {
staticModels: ModelInfo[]; staticModels: ModelInfo[];
name: string; name: string;
getDynamicModels?: (apiKeys?: Record<string, string>, providerSettings?: IProviderSetting) => Promise<ModelInfo[]>; getDynamicModels?: (
providerName: string,
apiKeys?: Record<string, string>,
providerSettings?: IProviderSetting,
serverEnv?: Record<string, string>,
) => Promise<ModelInfo[]>;
getApiKeyLink?: string; getApiKeyLink?: string;
labelForGetApiKey?: string; labelForGetApiKey?: string;
icon?: string; icon?: string;

View File

@ -220,7 +220,6 @@ const PROVIDER_LIST: ProviderInfo[] = [
], ],
getApiKeyLink: 'https://huggingface.co/settings/tokens', getApiKeyLink: 'https://huggingface.co/settings/tokens',
}, },
{ {
name: 'OpenAI', name: 'OpenAI',
staticModels: [ staticModels: [
@ -319,44 +318,130 @@ const PROVIDER_LIST: ProviderInfo[] = [
}, },
]; ];
export const providerBaseUrlEnvKeys: Record<string, { baseUrlKey?: string; apiTokenKey?: string }> = {
Anthropic: {
apiTokenKey: 'ANTHROPIC_API_KEY',
},
OpenAI: {
apiTokenKey: 'OPENAI_API_KEY',
},
Groq: {
apiTokenKey: 'GROQ_API_KEY',
},
HuggingFace: {
apiTokenKey: 'HuggingFace_API_KEY',
},
OpenRouter: {
apiTokenKey: 'OPEN_ROUTER_API_KEY',
},
Google: {
apiTokenKey: 'GOOGLE_GENERATIVE_AI_API_KEY',
},
OpenAILike: {
baseUrlKey: 'OPENAI_LIKE_API_BASE_URL',
apiTokenKey: 'OPENAI_LIKE_API_KEY',
},
Together: {
baseUrlKey: 'TOGETHER_API_BASE_URL',
apiTokenKey: 'TOGETHER_API_KEY',
},
Deepseek: {
apiTokenKey: 'DEEPSEEK_API_KEY',
},
Mistral: {
apiTokenKey: 'MISTRAL_API_KEY',
},
LMStudio: {
baseUrlKey: 'LMSTUDIO_API_BASE_URL',
},
xAI: {
apiTokenKey: 'XAI_API_KEY',
},
Cohere: {
apiTokenKey: 'COHERE_API_KEY',
},
Perplexity: {
apiTokenKey: 'PERPLEXITY_API_KEY',
},
Ollama: {
baseUrlKey: 'OLLAMA_API_BASE_URL',
},
};
export const getProviderBaseUrlAndKey = (options: {
provider: string;
apiKeys?: Record<string, string>;
providerSettings?: IProviderSetting;
serverEnv?: Record<string, string>;
defaultBaseUrlKey: string;
defaultApiTokenKey: string;
}) => {
const { provider, apiKeys, providerSettings, serverEnv, defaultBaseUrlKey, defaultApiTokenKey } = options;
let settingsBaseUrl = providerSettings?.baseUrl;
if (settingsBaseUrl && settingsBaseUrl.length == 0) {
settingsBaseUrl = undefined;
}
const baseUrlKey = providerBaseUrlEnvKeys[provider]?.baseUrlKey || defaultBaseUrlKey;
const baseUrl = settingsBaseUrl || serverEnv?.[baseUrlKey] || process.env[baseUrlKey] || import.meta.env[baseUrlKey];
const apiTokenKey = providerBaseUrlEnvKeys[provider]?.apiTokenKey || defaultApiTokenKey;
const apiKey =
apiKeys?.[provider] || serverEnv?.[apiTokenKey] || process.env[apiTokenKey] || import.meta.env[apiTokenKey];
return {
baseUrl,
apiKey,
};
};
export const DEFAULT_PROVIDER = PROVIDER_LIST[0]; export const DEFAULT_PROVIDER = PROVIDER_LIST[0];
const staticModels: ModelInfo[] = PROVIDER_LIST.map((p) => p.staticModels).flat(); const staticModels: ModelInfo[] = PROVIDER_LIST.map((p) => p.staticModels).flat();
export let MODEL_LIST: ModelInfo[] = [...staticModels]; export let MODEL_LIST: ModelInfo[] = [...staticModels];
export async function getModelList( export async function getModelList(options: {
apiKeys: Record<string, string>, apiKeys?: Record<string, string>;
providerSettings?: Record<string, IProviderSetting>, providerSettings?: Record<string, IProviderSetting>;
) { serverEnv?: Record<string, string>;
}) {
const { apiKeys, providerSettings, serverEnv } = options;
MODEL_LIST = [ MODEL_LIST = [
...( ...(
await Promise.all( await Promise.all(
PROVIDER_LIST.filter( PROVIDER_LIST.filter(
(p): p is ProviderInfo & { getDynamicModels: () => Promise<ModelInfo[]> } => !!p.getDynamicModels, (p): p is ProviderInfo & { getDynamicModels: () => Promise<ModelInfo[]> } => !!p.getDynamicModels,
).map((p) => p.getDynamicModels(apiKeys, providerSettings?.[p.name])), ).map((p) => p.getDynamicModels(p.name, apiKeys, providerSettings?.[p.name], serverEnv)),
) )
).flat(), ).flat(),
...staticModels, ...staticModels,
]; ];
return MODEL_LIST; return MODEL_LIST;
} }
async function getTogetherModels(apiKeys?: Record<string, string>, settings?: IProviderSetting): Promise<ModelInfo[]> { async function getTogetherModels(
name: string,
apiKeys?: Record<string, string>,
settings?: IProviderSetting,
serverEnv: Record<string, string> = {},
): Promise<ModelInfo[]> {
try { try {
const baseUrl = settings?.baseUrl || import.meta.env.TOGETHER_API_BASE_URL || ''; const { baseUrl, apiKey } = getProviderBaseUrlAndKey({
const provider = 'Together'; provider: name,
apiKeys,
providerSettings: settings,
serverEnv,
defaultBaseUrlKey: 'TOGETHER_API_BASE_URL',
defaultApiTokenKey: 'TOGETHER_API_KEY',
});
if (!baseUrl) { if (!baseUrl) {
return []; return [];
} }
let apiKey = import.meta.env.OPENAI_LIKE_API_KEY ?? '';
if (apiKeys && apiKeys[provider]) {
apiKey = apiKeys[provider];
}
if (!apiKey) { if (!apiKey) {
return []; return [];
} }
@ -374,7 +459,7 @@ async function getTogetherModels(apiKeys?: Record<string, string>, settings?: IP
label: `${m.display_name} - in:$${m.pricing.input.toFixed( label: `${m.display_name} - in:$${m.pricing.input.toFixed(
2, 2,
)} out:$${m.pricing.output.toFixed(2)} - context ${Math.floor(m.context_length / 1000)}k`, )} out:$${m.pricing.output.toFixed(2)} - context ${Math.floor(m.context_length / 1000)}k`,
provider, provider: name,
maxTokenAllowed: 8000, maxTokenAllowed: 8000,
})); }));
} catch (e) { } catch (e) {
@ -383,24 +468,40 @@ async function getTogetherModels(apiKeys?: Record<string, string>, settings?: IP
} }
} }
const getOllamaBaseUrl = (settings?: IProviderSetting) => { const getOllamaBaseUrl = (name: string, settings?: IProviderSetting, serverEnv: Record<string, string> = {}) => {
const defaultBaseUrl = settings?.baseUrl || import.meta.env.OLLAMA_API_BASE_URL || 'http://localhost:11434'; const { baseUrl } = getProviderBaseUrlAndKey({
provider: name,
providerSettings: settings,
serverEnv,
defaultBaseUrlKey: 'OLLAMA_API_BASE_URL',
defaultApiTokenKey: '',
});
// Check if we're in the browser // Check if we're in the browser
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
// Frontend always uses localhost // Frontend always uses localhost
return defaultBaseUrl; return baseUrl;
} }
// Backend: Check if we're running in Docker // Backend: Check if we're running in Docker
const isDocker = process.env.RUNNING_IN_DOCKER === 'true'; const isDocker = process.env.RUNNING_IN_DOCKER === 'true';
return isDocker ? defaultBaseUrl.replace('localhost', 'host.docker.internal') : defaultBaseUrl; return isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
}; };
async function getOllamaModels(apiKeys?: Record<string, string>, settings?: IProviderSetting): Promise<ModelInfo[]> { async function getOllamaModels(
name: string,
_apiKeys?: Record<string, string>,
settings?: IProviderSetting,
serverEnv: Record<string, string> = {},
): Promise<ModelInfo[]> {
try { try {
const baseUrl = getOllamaBaseUrl(settings); const baseUrl = getOllamaBaseUrl(name, settings, serverEnv);
if (!baseUrl) {
return [];
}
const response = await fetch(`${baseUrl}/api/tags`); const response = await fetch(`${baseUrl}/api/tags`);
const data = (await response.json()) as OllamaApiResponse; const data = (await response.json()) as OllamaApiResponse;
@ -419,22 +520,25 @@ async function getOllamaModels(apiKeys?: Record<string, string>, settings?: IPro
} }
async function getOpenAILikeModels( async function getOpenAILikeModels(
name: string,
apiKeys?: Record<string, string>, apiKeys?: Record<string, string>,
settings?: IProviderSetting, settings?: IProviderSetting,
serverEnv: Record<string, string> = {},
): Promise<ModelInfo[]> { ): Promise<ModelInfo[]> {
try { try {
const baseUrl = settings?.baseUrl || import.meta.env.OPENAI_LIKE_API_BASE_URL || ''; const { baseUrl, apiKey } = getProviderBaseUrlAndKey({
provider: name,
apiKeys,
providerSettings: settings,
serverEnv,
defaultBaseUrlKey: 'OPENAI_LIKE_API_BASE_URL',
defaultApiTokenKey: 'OPENAI_LIKE_API_KEY',
});
if (!baseUrl) { if (!baseUrl) {
return []; return [];
} }
let apiKey = '';
if (apiKeys && apiKeys.OpenAILike) {
apiKey = apiKeys.OpenAILike;
}
const response = await fetch(`${baseUrl}/models`, { const response = await fetch(`${baseUrl}/models`, {
headers: { headers: {
Authorization: `Bearer ${apiKey}`, Authorization: `Bearer ${apiKey}`,
@ -445,7 +549,7 @@ async function getOpenAILikeModels(
return res.data.map((model: any) => ({ return res.data.map((model: any) => ({
name: model.id, name: model.id,
label: model.id, label: model.id,
provider: 'OpenAILike', provider: name,
})); }));
} catch (e) { } catch (e) {
console.error('Error getting OpenAILike models:', e); console.error('Error getting OpenAILike models:', e);
@ -486,9 +590,26 @@ async function getOpenRouterModels(): Promise<ModelInfo[]> {
})); }));
} }
async function getLMStudioModels(_apiKeys?: Record<string, string>, settings?: IProviderSetting): Promise<ModelInfo[]> { async function getLMStudioModels(
name: string,
apiKeys?: Record<string, string>,
settings?: IProviderSetting,
serverEnv: Record<string, string> = {},
): Promise<ModelInfo[]> {
try { try {
const baseUrl = settings?.baseUrl || import.meta.env.LMSTUDIO_API_BASE_URL || 'http://localhost:1234'; const { baseUrl } = getProviderBaseUrlAndKey({
provider: name,
apiKeys,
providerSettings: settings,
serverEnv,
defaultBaseUrlKey: 'LMSTUDIO_API_BASE_URL',
defaultApiTokenKey: '',
});
if (!baseUrl) {
return [];
}
const response = await fetch(`${baseUrl}/v1/models`); const response = await fetch(`${baseUrl}/v1/models`);
const data = (await response.json()) as any; const data = (await response.json()) as any;
@ -503,29 +624,37 @@ async function getLMStudioModels(_apiKeys?: Record<string, string>, settings?: I
} }
} }
async function initializeModelList(providerSettings?: Record<string, IProviderSetting>): Promise<ModelInfo[]> { async function initializeModelList(options: {
let apiKeys: Record<string, string> = {}; env?: Record<string, string>;
providerSettings?: Record<string, IProviderSetting>;
apiKeys?: Record<string, string>;
}): Promise<ModelInfo[]> {
const { providerSettings, apiKeys: providedApiKeys, env } = options;
let apiKeys: Record<string, string> = providedApiKeys || {};
try { if (!providedApiKeys) {
const storedApiKeys = Cookies.get('apiKeys'); try {
const storedApiKeys = Cookies.get('apiKeys');
if (storedApiKeys) { if (storedApiKeys) {
const parsedKeys = JSON.parse(storedApiKeys); const parsedKeys = JSON.parse(storedApiKeys);
if (typeof parsedKeys === 'object' && parsedKeys !== null) { if (typeof parsedKeys === 'object' && parsedKeys !== null) {
apiKeys = parsedKeys; apiKeys = parsedKeys;
}
} }
} catch (error: any) {
logStore.logError('Failed to fetch API keys from cookies', error);
logger.warn(`Failed to fetch apikeys from cookies: ${error?.message}`);
} }
} catch (error: any) {
logStore.logError('Failed to fetch API keys from cookies', error);
logger.warn(`Failed to fetch apikeys from cookies: ${error?.message}`);
} }
MODEL_LIST = [ MODEL_LIST = [
...( ...(
await Promise.all( await Promise.all(
PROVIDER_LIST.filter( PROVIDER_LIST.filter(
(p): p is ProviderInfo & { getDynamicModels: () => Promise<ModelInfo[]> } => !!p.getDynamicModels, (p): p is ProviderInfo & { getDynamicModels: () => Promise<ModelInfo[]> } => !!p.getDynamicModels,
).map((p) => p.getDynamicModels(apiKeys, providerSettings?.[p.name])), ).map((p) => p.getDynamicModels(p.name, apiKeys, providerSettings?.[p.name], env)),
) )
).flat(), ).flat(),
...staticModels, ...staticModels,
@ -534,6 +663,7 @@ async function initializeModelList(providerSettings?: Record<string, IProviderSe
return MODEL_LIST; return MODEL_LIST;
} }
// initializeModelList({})
export { export {
getOllamaModels, getOllamaModels,
getOpenAILikeModels, getOpenAILikeModels,

View File

@ -28,7 +28,7 @@ export default defineConfig((config) => {
chrome129IssuePlugin(), chrome129IssuePlugin(),
config.mode === 'production' && optimizeCssModules({ apply: 'build' }), config.mode === 'production' && optimizeCssModules({ apply: 'build' }),
], ],
envPrefix: ["VITE_", "OPENAI_LIKE_API_", "OLLAMA_API_BASE_URL", "LMSTUDIO_API_BASE_URL","TOGETHER_API_BASE_URL"], envPrefix: ["VITE_","OPENAI_LIKE_API_BASE_URL", "OLLAMA_API_BASE_URL", "LMSTUDIO_API_BASE_URL","TOGETHER_API_BASE_URL"],
css: { css: {
preprocessorOptions: { preprocessorOptions: {
scss: { scss: {