behemoth-cli
Version:
🌍 BEHEMOTH CLIv3.760.4 - Level 50+ POST-SINGULARITY Intelligence Trading AI
187 lines • 6.26 kB
JavaScript
import Groq from 'groq-sdk';
import OpenAI from 'openai';
import { MultiProviderConfigManager, PROVIDERS } from '../utils/multi-provider-config.js';
class GroqProvider {
name = 'groq';
client;
constructor(apiKey) {
this.client = new Groq({ apiKey });
}
async createChatCompletion(request) {
const response = await this.client.chat.completions.create({
model: request.model,
messages: request.messages,
temperature: request.temperature,
max_tokens: request.max_tokens,
tools: request.tools,
tool_choice: request.tool_choice,
stream: false
});
return response;
}
async *createStreamingChatCompletion(request) {
const stream = await this.client.chat.completions.create({
model: request.model,
messages: request.messages,
temperature: request.temperature,
max_tokens: request.max_tokens,
tools: request.tools,
tool_choice: request.tool_choice,
stream: true
});
for await (const chunk of stream) {
yield chunk;
}
}
}
class OpenRouterProvider {
name = 'openrouter';
client;
constructor(apiKey) {
this.client = new OpenAI({
apiKey,
baseURL: 'https://openrouter.ai/api/v1',
defaultHeaders: {
'HTTP-Referer': 'https://github.com/fr3k/behemoth-cli',
'X-Title': 'BEHEMOTH CLI v2'
}
});
}
async createChatCompletion(request) {
const response = await this.client.chat.completions.create({
model: request.model,
messages: request.messages,
temperature: request.temperature,
max_tokens: request.max_tokens,
tools: request.tools,
tool_choice: request.tool_choice,
stream: false
});
return response;
}
async *createStreamingChatCompletion(request) {
const stream = await this.client.chat.completions.create({
model: request.model,
messages: request.messages,
temperature: request.temperature,
max_tokens: request.max_tokens,
tools: request.tools,
tool_choice: request.tool_choice,
stream: true
});
for await (const chunk of stream) {
yield chunk;
}
}
}
class DeepSeekProvider {
name = 'deepseek';
client;
constructor(apiKey) {
this.client = new OpenAI({
apiKey,
baseURL: 'https://api.deepseek.com',
});
}
async createChatCompletion(request) {
const response = await this.client.chat.completions.create({
model: request.model,
messages: request.messages,
temperature: request.temperature,
max_tokens: request.max_tokens,
tools: request.tools,
tool_choice: request.tool_choice,
stream: false
});
return response;
}
async *createStreamingChatCompletion(request) {
const stream = await this.client.chat.completions.create({
model: request.model,
messages: request.messages,
temperature: request.temperature,
max_tokens: request.max_tokens,
tools: request.tools,
tool_choice: request.tool_choice,
stream: true
});
for await (const chunk of stream) {
yield chunk;
}
}
}
export class ProviderClientFactory {
configManager;
clientCache = new Map();
constructor() {
this.configManager = new MultiProviderConfigManager();
}
createProvider(providerName) {
// Check cache first
const cacheKey = providerName;
if (this.clientCache.has(cacheKey)) {
return this.clientCache.get(cacheKey);
}
// Get API key for provider
const apiKey = this.configManager.getProviderApiKey(providerName);
if (!apiKey) {
throw new Error(`No API key configured for provider: ${providerName}`);
}
// Create provider instance
let provider;
switch (providerName) {
case 'groq':
provider = new GroqProvider(apiKey);
break;
case 'openrouter':
provider = new OpenRouterProvider(apiKey);
break;
case 'deepseek':
provider = new DeepSeekProvider(apiKey);
break;
default:
throw new Error(`Unsupported provider: ${providerName}`);
}
// Cache and return
this.clientCache.set(cacheKey, provider);
return provider;
}
rotateApiKey(providerName) {
// Remove from cache to force recreation with new key
this.clientCache.delete(providerName);
// Rotate the API key
const newApiKey = this.configManager.rotateProviderApiKey(providerName);
if (!newApiKey) {
return null;
}
// Create new provider with rotated key
return this.createProvider(providerName);
}
isProviderAvailable(providerName) {
return this.configManager.isProviderConfigured(providerName);
}
getAvailableProviders() {
return this.configManager.getEnabledProviders().filter(provider => this.configManager.isProviderConfigured(provider));
}
validateProvider(providerName) {
if (!PROVIDERS[providerName]) {
return { valid: false, error: `Unknown provider: ${providerName}` };
}
if (!this.configManager.isProviderConfigured(providerName)) {
return { valid: false, error: `Provider ${providerName} is not configured` };
}
return { valid: true };
}
clearCache() {
this.clientCache.clear();
}
getCacheStats() {
return {
providers: Array.from(this.clientCache.keys()),
size: this.clientCache.size
};
}
}
// Singleton instance
export const providerClientFactory = new ProviderClientFactory();
//# sourceMappingURL=provider-clients.js.map