@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
193 lines (192 loc) • 6.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentPricingSDK = void 0;
exports.createAgentPricingSDK = createAgentPricingSDK;
class AgentPricingSDK {
constructor(agentId, options = {}) {
this.agentId = agentId;
this.baseUrl = options.baseUrl || 'https://developers.agentdao.com';
this.apiKey = options.apiKey;
this.headers = {
'Content-Type': 'application/json',
...options.headers
};
// Add API key to headers if provided
if (this.apiKey) {
this.headers['Authorization'] = `Bearer ${this.apiKey}`;
}
}
/**
* Set API key for authentication
*/
setApiKey(apiKey) {
this.apiKey = apiKey;
this.headers['Authorization'] = `Bearer ${apiKey}`;
}
/**
* Add custom headers
*/
setHeaders(headers) {
this.headers = { ...this.headers, ...headers };
}
/**
* Fetch the pricing configuration for this agent
*/
async getPricingConfig() {
try {
const response = await fetch(`${this.baseUrl}/api/agents/${this.agentId}/pricing-config`, {
headers: this.headers
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.error || `Failed to fetch pricing config: ${response.status} ${response.statusText}`);
}
const data = await response.json();
return data;
}
catch (error) {
console.error('Error fetching agent pricing configuration:', error);
throw error;
}
}
/**
* Get all pricing plans for this agent
*/
async getPricingPlans() {
const config = await this.getPricingConfig();
return config.pricing.pricing_plans;
}
/**
* Get all accepted tokens for this agent
*/
async getAcceptedTokens() {
const config = await this.getPricingConfig();
return config.pricing.accepted_tokens;
}
/**
* Get ESH token addresses for this agent
*/
async getEshTokenAddresses() {
const config = await this.getPricingConfig();
return config.pricing.esh_token_addresses;
}
/**
* Get ESH token metadata for this agent
*/
async getEshTokenMetadata() {
const config = await this.getPricingConfig();
return config.pricing.esh_token_metadata;
}
/**
* Check if this agent is available for hire
*/
async isAvailableForHire() {
const config = await this.getPricingConfig();
return config.pricing.is_available_for_hire;
}
/**
* Get usage limits for this agent
*/
async getUsageLimits() {
const config = await this.getPricingConfig();
return config.pricing.usage_limits;
}
/**
* Get rate limits for this agent
*/
async getRateLimits() {
const config = await this.getPricingConfig();
return config.pricing.rate_limits;
}
/**
* Check if a specific feature is enabled for this agent
*/
async hasFeature(featureName) {
const config = await this.getPricingConfig();
return config.features.features_enabled.includes(featureName);
}
/**
* Get all enabled features for this agent
*/
async getEnabledFeatures() {
const config = await this.getPricingConfig();
return config.features.features_enabled;
}
/**
* Get agent tags
*/
async getTags() {
const config = await this.getPricingConfig();
return config.features.tags;
}
/**
* Get agent categories
*/
async getCategories() {
const config = await this.getPricingConfig();
return config.features.categories;
}
/**
* Get supported languages
*/
async getSupportedLanguages() {
const config = await this.getPricingConfig();
return config.features.supported_languages;
}
/**
* Get integration information
*/
async getIntegrationInfo() {
const config = await this.getPricingConfig();
return config.integration;
}
/**
* Validate if a user can access the agent based on pricing plans
* This is a helper method that can be used for access control
*/
async validateAccess(userPlan, userToken) {
const config = await this.getPricingConfig();
// If agent is not available for hire, no access
if (!config.pricing.is_available_for_hire) {
return {
hasAccess: false,
reason: 'Agent is not available for hire'
};
}
// If no pricing plans, agent is free
if (Object.keys(config.pricing.pricing_plans).length === 0) {
return { hasAccess: true };
}
// If user has a plan, check if it's valid
if (userPlan && config.pricing.pricing_plans[userPlan]) {
return { hasAccess: true };
}
// If user has a token, check if it's accepted
if (userToken && config.pricing.accepted_tokens[userToken]) {
return { hasAccess: true };
}
// No valid access found
const availablePlans = Object.keys(config.pricing.pricing_plans);
const availableTokens = Object.keys(config.pricing.accepted_tokens);
return {
hasAccess: false,
reason: 'Valid plan or token required',
requiredPlan: availablePlans.length > 0 ? availablePlans[0] : undefined,
requiredToken: availableTokens.length > 0 ? availableTokens[0] : undefined
};
}
/**
* Get agent information
*/
async getAgentInfo() {
const config = await this.getPricingConfig();
return config.agent;
}
}
exports.AgentPricingSDK = AgentPricingSDK;
/**
* Factory function to create AgentPricingSDK instance
*/
function createAgentPricingSDK(agentId, options = {}) {
return new AgentPricingSDK(agentId, options);
}