Files
open-gsio/packages/ai/providers/_ProviderRepository.ts
geoffsee c6e09644e2 **Refactor:** Restructure server package to streamline imports and improve file organization
- Moved `providers`, `services`, `models`, `lib`, and related files to `src` directory within `server` package.
- Adjusted imports across the codebase to reflect the new paths.
- Renamed several `.ts` files for consistency.
- Introduced an `index.ts` in the `ai/providers` package to export all providers.

This improves maintainability and aligns with the project's updated directory structure.
2025-06-24 20:46:15 -04:00

87 lines
3.0 KiB
TypeScript

export type GenericEnv = Record<string, any>;
export class ProviderRepository {
#providers: { name: string; key: string; endpoint: string }[] = [];
#env: Record<string, any>;
constructor(env: GenericEnv) {
this.#env = env;
this.setProviders(env);
}
static OPENAI_COMPAT_ENDPOINTS = {
xai: 'https://api.x.ai/v1',
groq: 'https://api.groq.com/openai/v1',
google: 'https://generativelanguage.googleapis.com/v1beta/openai',
fireworks: 'https://api.fireworks.ai/inference/v1',
cohere: 'https://api.cohere.ai/compatibility/v1',
cloudflare: 'https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/v1',
anthropic: 'https://api.anthropic.com/v1',
openai: 'https://api.openai.com/v1',
cerebras: 'https://api.cerebras.com/v1',
ollama: 'http://localhost:11434/v1',
mlx: 'http://localhost:10240/v1',
};
static async getModelFamily(model: any, env: Env) {
const allModels = await env.KV_STORAGE.get('supportedModels');
const models = JSON.parse(allModels);
const modelData = models.filter(m => m.id === model);
return modelData[0].provider;
}
static async getModelMeta(meta, env) {
const allModels = await env.KV_STORAGE.get('supportedModels');
const models = JSON.parse(allModels);
return models.filter(m => m.id === meta.model).pop();
}
getProviders(): { name: string; key: string; endpoint: string }[] {
return this.#providers;
}
setProviders(env: Record<string, any>) {
const envKeys = Object.keys(env);
for (let i = 0; i < envKeys.length; i++) {
if (envKeys[i].endsWith('KEY')) {
const detectedProvider = envKeys[i].split('_')[0].toLowerCase();
const detectedProviderValue = env[envKeys[i]];
if (detectedProviderValue) {
switch (detectedProvider) {
case 'anthropic':
this.#providers.push({
name: 'anthropic',
key: env.ANTHROPIC_API_KEY,
endpoint: ProviderRepository.OPENAI_COMPAT_ENDPOINTS['anthropic'],
});
break;
case 'gemini':
this.#providers.push({
name: 'google',
key: env.GEMINI_API_KEY,
endpoint: ProviderRepository.OPENAI_COMPAT_ENDPOINTS['google'],
});
break;
case 'cloudflare':
this.#providers.push({
name: 'cloudflare',
key: env.CLOUDFLARE_API_KEY,
endpoint: ProviderRepository.OPENAI_COMPAT_ENDPOINTS[detectedProvider].replace(
'{CLOUDFLARE_ACCOUNT_ID}',
env.CLOUDFLARE_ACCOUNT_ID,
),
});
break;
default:
this.#providers.push({
name: detectedProvider,
key: env[envKeys[i]],
endpoint: ProviderRepository.OPENAI_COMPAT_ENDPOINTS[detectedProvider],
});
}
}
}
}
}
}