donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
178 lines • 6.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GptConfigsManager = void 0;
const GptClientFactory_1 = require("../clients/GptClientFactory");
const InvalidParamValueException_1 = require("../exceptions/InvalidParamValueException");
const MiscUtils_1 = require("../utils/MiscUtils");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const Logger_1 = require("../utils/Logger");
const envVars_1 = require("../envVars");
/**
* This class manages GPT configs and is responsible for validation around
* that context.
*/
class GptConfigsManager {
constructor(persistence) {
this.persistence = persistence;
}
static async create(persistence) {
const instance = new GptConfigsManager(persistence);
await instance.initializeDefaultConfigurations();
return instance;
}
/**
* Attempt to save the given GPT config. Where possible, a zero-cost
* request is attempted to be made with the GPT config before saving it.
* If the zero-cost request is attempted and it fails, this method throws.
*/
async set(name, config) {
await this.verifyGptConfigOrThrow(name, config);
await this.persistence.set(name, config);
}
/**
* Fetch a GPT config by name.
*/
async get(name) {
return this.persistence.get(name);
}
/**
* Fetch all GPT configs.
*/
async getAll() {
return this.persistence.getAll();
}
/**
* Delete a GPT config by name.
*/
async delete(name) {
return this.persistence.delete(name);
}
/**
* Verifies the given GPT config and name or throws if they are invalid.
*/
async verifyGptConfigOrThrow(name, config) {
if (!name || name.length > 64) {
throw new InvalidParamValueException_1.InvalidParamValueException('name', name, 'the GPT configuration name must be a non-blank string 64 characters or fewer');
}
// Creating a GPT client performs validation on its own.
await GptClientFactory_1.GptClientFactory.createFromGptConfig(config);
}
/**
* Inspects environment variables and legacy GPT platform keys files to
* initialize default GPT configurations.
*/
async initializeDefaultConfigurations() {
const apiKeysFilepath = path_1.default.join(MiscUtils_1.MiscUtils.baseWorkingDirectory(), '.gptplatforms');
let apiKeysData = {
ANTHROPIC: null,
GOOGLE: null,
OPENAI: null,
};
try {
const content = await fs_1.promises.readFile(apiKeysFilepath, 'utf-8');
// API key data has the JSON format of...
// {
// "ANTHROPIC": <key>,
// ...
// }
apiKeysData = JSON.parse(content);
}
catch (_error) {
// This likely just means the legacy API keys file has already been migrated.
}
// Perform overrides if environment variables are present.
const anthropicApiKeyFromEnv = process.env[envVars_1.ENV_VAR_NAMES.ANTHROPIC_API_KEY];
if (anthropicApiKeyFromEnv) {
apiKeysData.ANTHROPIC = anthropicApiKeyFromEnv;
}
const googleApiKeyFromEnv = process.env[envVars_1.ENV_VAR_NAMES.GOOGLE_GENERATIVE_AI_API_KEY];
if (googleApiKeyFromEnv) {
apiKeysData.GOOGLE = googleApiKeyFromEnv;
}
const openAiApiKeyFromEnv = process.env[envVars_1.ENV_VAR_NAMES.OPENAI_API_KEY];
if (openAiApiKeyFromEnv) {
apiKeysData.OPENAI = openAiApiKeyFromEnv;
}
// Setup Anthropic GPT configurations.
if (apiKeysData.ANTHROPIC) {
for (const modelName of [
'claude-3-7-sonnet-latest',
'claude-3-5-sonnet-latest',
]) {
try {
await this.get(modelName);
}
catch (_error) {
if (apiKeysData.ANTHROPIC) {
try {
// Nothing found, so port it.
await this.set(modelName, {
type: 'ANTHROPIC',
apiKey: apiKeysData.ANTHROPIC,
modelName: modelName,
});
}
catch (error) {
Logger_1.appLogger.warn(`Failed to create default GPT configuration for model: ${modelName}`, error);
}
}
}
}
}
// Setup Google GPT configurations.
if (apiKeysData.GOOGLE) {
for (const modelName of ['gemini-2.0-flash', 'gemini-1.5-pro']) {
try {
await this.get(modelName);
}
catch (_error) {
try {
// Nothing found, so port it.
await this.set(modelName, {
type: 'GOOGLE_GEMINI',
apiKey: apiKeysData.GOOGLE,
modelName: modelName,
});
}
catch (error) {
Logger_1.appLogger.warn(`Failed to create default GPT configuration for model: ${modelName}`, error);
}
}
}
}
// Setup OpenAI GPT configurations.
if (apiKeysData.OPENAI) {
for (const modelName of ['gpt-4o', 'gpt-4o-mini']) {
try {
await this.get(modelName);
}
catch (_error) {
try {
// Nothing found, so port it.
await this.set(modelName, {
type: 'OPENAI',
apiKey: apiKeysData.OPENAI,
modelName: modelName,
});
}
catch (error) {
Logger_1.appLogger.warn(`Failed to create default GPT configuration for model: ${modelName}`, error);
}
}
}
}
// Remove the legacy API keys file if it exists.
try {
await fs_1.promises.rm(apiKeysFilepath);
}
catch (_error) {
// No-op.
}
}
}
exports.GptConfigsManager = GptConfigsManager;
//# sourceMappingURL=GptConfigsManager.js.map