@agentdao/core
Version:
Core functionality, skills, and ready-made UI components for AgentDAO - Web3 subscriptions, content generation, social media, help support, live chat, RSS fetching, web search, and agent pricing integration
187 lines (186 loc) ⢠8.08 kB
JavaScript
;
/**
* Environment Configuration Utility
*
* This utility helps users configure their API keys and provides helpful error messages
* when required keys are missing.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateSkillConfig = exports.getSetupInstructions = exports.isDemoMode = exports.getApiKey = exports.hasApiKey = exports.EnvConfigManager = void 0;
class EnvConfigManager {
constructor() {
this.config = {};
this.loadEnvironmentVariables();
}
loadEnvironmentVariables() {
// Check for environment variables (from user's .env file)
this.config = {
openai: {
apiKey: process.env.OPENAI_API_KEY || process.env.NEXT_PUBLIC_OPENAI_API_KEY,
model: process.env.OPENAI_MODEL || process.env.NEXT_PUBLIC_OPENAI_MODEL || 'gpt-4'
},
google: {
apiKey: process.env.GOOGLE_SEARCH_API_KEY || process.env.NEXT_PUBLIC_GOOGLE_SEARCH_API_KEY,
searchEngineId: process.env.GOOGLE_SEARCH_ENGINE_ID || process.env.NEXT_PUBLIC_GOOGLE_SEARCH_ENGINE_ID
},
bing: {
apiKey: process.env.BING_SEARCH_API_KEY || process.env.NEXT_PUBLIC_BING_SEARCH_API_KEY
},
newsapi: {
apiKey: process.env.NEWS_API_KEY || process.env.NEXT_PUBLIC_NEWS_API_KEY
},
database: {
endpoint: process.env.DATABASE_URL || process.env.NEXT_PUBLIC_DATABASE_URL,
apiKey: process.env.DATABASE_API_KEY || process.env.NEXT_PUBLIC_DATABASE_API_KEY
},
twitter: {
apiKey: process.env.TWITTER_API_KEY || process.env.NEXT_PUBLIC_TWITTER_API_KEY,
apiSecret: process.env.TWITTER_API_SECRET || process.env.NEXT_PUBLIC_TWITTER_API_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN || process.env.NEXT_PUBLIC_TWITTER_ACCESS_TOKEN,
accessTokenSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET || process.env.NEXT_PUBLIC_TWITTER_ACCESS_TOKEN_SECRET
},
unsplash: {
accessKey: process.env.UNSPLASH_ACCESS_KEY || process.env.NEXT_PUBLIC_UNSPLASH_ACCESS_KEY
},
blockchain: {
ethereumRpcUrl: process.env.ETHEREUM_RPC_URL || process.env.NEXT_PUBLIC_ETHEREUM_RPC_URL,
polygonRpcUrl: process.env.POLYGON_RPC_URL || process.env.NEXT_PUBLIC_POLYGON_RPC_URL,
arbitrumRpcUrl: process.env.ARBITRUM_RPC_URL || process.env.NEXT_PUBLIC_ARBITRUM_RPC_URL
},
demoMode: (process.env.DEMO_MODE === 'true') || (process.env.NEXT_PUBLIC_DEMO_MODE === 'true')
};
}
/**
* Get configuration for a specific service
*/
getServiceConfig(service) {
return this.config[service];
}
/**
* Check if a required API key is available
*/
hasApiKey(service, keyName) {
const serviceConfig = this.config[service];
if (!serviceConfig)
return false;
return !!serviceConfig[keyName];
}
/**
* Get API key with helpful error message if missing
*/
getApiKey(service, keyName) {
if (!this.hasApiKey(service, keyName)) {
throw new Error(`${service.toUpperCase()}_${keyName.toUpperCase()} environment variable is required but not set.\n` +
`Please add it to your .env file:\n` +
`${service.toUpperCase()}_${keyName.toUpperCase()}=your-api-key-here`);
}
return this.config[service][keyName];
}
/**
* Check if demo mode is enabled
*/
isDemoMode() {
return this.config.demoMode || false;
}
/**
* Get all missing API keys for a specific skill
*/
getMissingKeysForSkill(skillName) {
const requiredKeys = {
'WebSearchSkill': [
{ service: 'openai', key: 'apiKey' }
// Note: Google/Bing keys are optional - DuckDuckGo is used as fallback
],
'ContentGeneratorSkill': [
{ service: 'openai', key: 'apiKey' }
],
'SocialMediaSkill': [
{ service: 'twitter', key: 'apiKey' },
{ service: 'twitter', key: 'apiSecret' },
{ service: 'twitter', key: 'accessToken' },
{ service: 'twitter', key: 'accessTokenSecret' }
],
'PhotoSkill': [
{ service: 'unsplash', key: 'accessKey' }
],
'HelpSupportSkill': [
{ service: 'openai', key: 'apiKey' }
],
'LiveChatSkill': [
{ service: 'openai', key: 'apiKey' }
]
};
const skillKeys = requiredKeys[skillName] || [];
const missingKeys = [];
for (const { service, key } of skillKeys) {
if (!this.hasApiKey(service, key)) {
missingKeys.push(`${service.toUpperCase()}_${key.toUpperCase()}`);
}
}
return missingKeys;
}
/**
* Get setup instructions for missing API keys
*/
getSetupInstructions(skillName) {
const missingKeys = this.getMissingKeysForSkill(skillName);
if (missingKeys.length === 0) {
return `ā
All required API keys for ${skillName} are configured!`;
}
let instructions = `š§ Setup required for ${skillName}:\n\n`;
instructions += `Add the following to your .env file:\n\n`;
for (const key of missingKeys) {
instructions += `${key}=your-${key.toLowerCase().replace(/_/g, '-')}-here\n`;
}
instructions += `\nš How to get these API keys:\n`;
if (missingKeys.includes('OPENAI_API_KEY')) {
instructions += `⢠OpenAI API Key: https://platform.openai.com/api-keys\n`;
}
if (missingKeys.includes('GOOGLE_SEARCH_API_KEY')) {
instructions += `⢠Google Search API Key: https://console.cloud.google.com/apis/credentials\n`;
instructions += `⢠Google Search Engine ID: https://programmablesearchengine.google.com/controlpanel/all\n`;
}
if (missingKeys.includes('BING_SEARCH_API_KEY')) {
instructions += `⢠Bing Search API Key: https://www.microsoft.com/en-us/bing/apis/bing-web-search-api\n`;
}
if (missingKeys.includes('NEWS_API_KEY')) {
instructions += `⢠NewsAPI Key: https://newsapi.org/register\n`;
}
if (missingKeys.includes('TWITTER_API_KEY')) {
instructions += `⢠Twitter API Keys: https://developer.twitter.com/en/portal/dashboard\n`;
}
if (missingKeys.includes('UNSPLASH_ACCESS_KEY')) {
instructions += `⢠Unsplash Access Key: https://unsplash.com/developers\n`;
}
return instructions;
}
/**
* Validate configuration for a skill
*/
validateSkillConfig(skillName) {
const missingKeys = this.getMissingKeysForSkill(skillName);
const valid = missingKeys.length === 0;
const instructions = this.getSetupInstructions(skillName);
return { valid, missingKeys, instructions };
}
/**
* Get all configuration
*/
getAllConfig() {
return { ...this.config };
}
}
exports.EnvConfigManager = EnvConfigManager;
// Create singleton instance
const envConfig = new EnvConfigManager();
// Export utility functions
const hasApiKey = (service, keyName) => envConfig.hasApiKey(service, keyName);
exports.hasApiKey = hasApiKey;
const getApiKey = (service, keyName) => envConfig.getApiKey(service, keyName);
exports.getApiKey = getApiKey;
const isDemoMode = () => envConfig.isDemoMode();
exports.isDemoMode = isDemoMode;
const getSetupInstructions = (skillName) => envConfig.getSetupInstructions(skillName);
exports.getSetupInstructions = getSetupInstructions;
const validateSkillConfig = (skillName) => envConfig.validateSkillConfig(skillName);
exports.validateSkillConfig = validateSkillConfig;