UNPKG

donobu

Version:

Create browser automations with an LLM agent and replay them as Playwright scripts.

150 lines 6.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GptConfigsApi = void 0; const v4_1 = require("zod/v4"); const GptConfig_1 = require("../models/GptConfig"); const Logger_1 = require("../utils/Logger"); /** * API controller for managing GPT configurations used by Donobu flows. * * The GptConfigsApi provides comprehensive management of GPT configurations, which define * the connection parameters, authentication credentials, and model settings for various * AI providers. These configurations are referenced by agents and used during flow execution * to connect to external AI services. * * **Security Features:** * - Automatic redaction of sensitive fields (API keys, secret keys) in responses * - Validation of configurations before storage * - Cascade cleanup when configurations are deleted * * **Key Features:** * - Automatic cleanup of dependent agent mappings on deletion * - Comprehensive error handling and validation * - Support for multiple AI provider types and authentication methods */ class GptConfigsApi { constructor(gptConfigsManager, agentsManager) { this.gptConfigsManager = gptConfigsManager; this.agentsManager = agentsManager; } /** * Creates or updates a GPT configuration. * * This endpoint performs comprehensive validation of the GPT configuration before * storage, including attempting a request to the GPT to verify connectivity and * authentication. The configuration is validated against the provider's requirements * and tested for functionality before being saved. * * **Validation Process:** * 1. Schema validation against the configuration type * 2. Credential validation via test request to the provider * 3. Model availability verification (where supported) * 4. Persistent storage of the validated configuration * * **Security:** * - Sensitive fields are automatically redacted in the response * - Original credentials are securely stored but never returned */ async set(req, res) { const name = GptConfigsApi.GPT_CONFIG_NAME_SCHEMA.parse(req.params.name); const parsedBody = GptConfig_1.GptConfigInputSchema.parse(req.body); await this.gptConfigsManager.set(name, parsedBody); res.json(GptConfigsApi.redactSensativeData(parsedBody)); } /** * Retrieves a specific GPT configuration by name. * * Returns the requested GPT configuration with sensitive fields automatically * redacted for security. The configuration includes all provider-specific * settings and model parameters needed for AI service connectivity. */ async get(req, res) { const name = String(req.params.name); const config = GptConfigsApi.redactSensativeData(await this.gptConfigsManager.get(name)); res.json(config); } /** * Retrieves all stored GPT configurations. * * Returns a complete collection of all GPT configurations with sensitive * fields redacted. This endpoint is useful for administrative interfaces * and configuration management tools. The response maps configuration * names to their respective settings. */ async getAll(_req, res) { const configs = await this.gptConfigsManager.getAll(); const redactedConfigs = new Map(Array.from(configs.entries()).map(([key, value]) => [ key, GptConfigsApi.redactSensativeData(value), ])); res.json(Object.fromEntries(redactedConfigs)); } /** * Deletes a GPT configuration and cleans up dependent references. * * This endpoint permanently removes a GPT configuration and performs * cascade cleanup to maintain system integrity: * * **Cleanup Process:** * 1. Removes the configuration from storage * 2. Identifies all agents referencing this configuration * 3. Unlinks the configuration from all dependent agents */ async delete(req, res) { const name = String(req.params.name); await this.gptConfigsManager.delete(name); try { // If this GPT config is associated with an agent, delete its reference. const agentNameToConfigNameMap = await this.agentsManager.getAll(); for (const [agentName, configName] of agentNameToConfigNameMap) { if (configName === name) { try { await this.agentsManager.set(agentName, null); } catch (error) { Logger_1.appLogger.warn(`Failed to unset Donobu ${agentName} agent configuration from '${name}'`, error); } } } } catch (error) { Logger_1.appLogger.warn(`Failed to unset Donobu agent configurations from '${name}'`, error); } res.json({}); } /** * Redacts sensative data from the given GPT configuration so that the record * can be safely returned through the API. */ static redactSensativeData(config) { const configCopy = { ...config }; for (const redactedFieldName of GptConfigsApi.REDACTED_FIELD_NAMES) { if (redactedFieldName in configCopy) { // Use type assertion to tell TypeScript this is allowed configCopy[redactedFieldName] = '****************'; } } return configCopy; } } exports.GptConfigsApi = GptConfigsApi; /** * Zod schema for validating GPT config names from URL parameters. * Ensures names are non-empty strings with reasonable length constraints. */ GptConfigsApi.GPT_CONFIG_NAME_SCHEMA = v4_1.z .string() .min(1, 'GPT config name cannot be empty') .max(100, 'GPT config name cannot exceed 100 characters') .regex(/^[a-zA-Z0-9_\s-]+$/, 'GPT config name can only contain alphanumeric characters, spaces, underscores, and hyphens'); /** * Field names that contain sensitive data and should be redacted in API responses. * These fields are automatically replaced with asterisks to prevent credential exposure. */ GptConfigsApi.REDACTED_FIELD_NAMES = [ 'apiKey', 'credentials', 'key', 'secretAccessKey', ]; //# sourceMappingURL=GptConfigsApi.js.map