contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
82 lines (81 loc) • 2.97 kB
JavaScript
import { FileService } from "../fileService.js";
import { PROVIDER_CAPABILITIES, } from "./types.js";
export class BaseLLM {
constructor(providerConfig) {
this.providerConfig = providerConfig;
this.config = {};
this.fileService = new FileService();
this.storageKey = `./.config/llm_config/${providerConfig.name.toLowerCase()}.json`;
// Don't call loadConfig in constructor - it will be called when needed
}
async executePrompts(prompts, options) {
return Promise.all(prompts.map((prompt) => this.executePrompt(prompt, options)));
}
configure(config) {
this.config = config;
this.fileService.saveFile({
path: this.storageKey,
content: JSON.stringify(config),
});
}
getConfig() {
return this.config;
}
async isConfigured() {
// Ensure config is loaded before checking
await this.loadConfig();
return this.providerConfig.configFields
.filter((field) => field.required)
.every((field) => Boolean(this.config[field.name]));
}
getCapabilities() {
const capabilities = PROVIDER_CAPABILITIES[this.providerConfig.name];
if (!capabilities) {
// Default capabilities for unknown providers
return {
supportsConversation: false,
supportsSystemMessages: false,
supportsToolCalls: false,
customFormat: 'standard'
};
}
return capabilities;
}
supportsConversation() {
return this.getCapabilities().supportsConversation;
}
async loadConfig() {
const savedConfig = await this.fileService
.readFile(this.storageKey)
.then((data) => {
try {
this.config = JSON.parse(data);
// Process environment variable references
this.resolveEnvironmentVariables();
}
catch (error) {
console.error("Failed to load LLM config");
this.config = {};
}
})
.catch((error) => {
console.error("Failed to load LLM config");
this.config = {};
});
}
resolveEnvironmentVariables() {
for (const [key, value] of Object.entries(this.config)) {
if (typeof value === 'string' && value.startsWith('env:')) {
const envVarName = value.substring(4); // Remove 'env:' prefix
const envValue = process.env[envVarName];
if (envValue) {
this.config[key] = envValue;
}
else {
console.warn(`⚠️ Environment variable ${envVarName} not found for ${this.providerConfig.name}.${key}`);
// Keep the original env: reference so user knows it's missing
}
}
}
}
}