@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
63 lines (62 loc) • 1.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Agent = void 0;
class Agent {
constructor(config) {
this.name = config.name;
this.description = config.description;
this.version = config.version;
this.handler = config.handler;
this.validateInput = config.validateInput;
this.validateOutput = config.validateOutput;
this.metadata = {
name: this.name,
description: this.description,
version: this.version,
createdAt: new Date(),
updatedAt: new Date(),
};
}
async execute(input) {
try {
// Validate input if validator is provided
if (this.validateInput && !this.validateInput(input)) {
throw this.createError('INVALID_INPUT', 'Input validation failed');
}
// Execute the handler
const result = await this.handler(input);
// Validate output if validator is provided
if (this.validateOutput && !this.validateOutput(result)) {
throw this.createError('INVALID_OUTPUT', 'Output validation failed');
}
return result;
}
catch (error) {
if (error instanceof Error) {
throw this.createError('EXECUTION_ERROR', error.message, error);
}
throw this.createError('UNKNOWN_ERROR', 'An unknown error occurred');
}
}
getName() {
return this.name;
}
getDescription() {
return this.description;
}
getVersion() {
return this.version;
}
getMetadata() {
return { ...this.metadata };
}
createError(code, message, originalError) {
const error = new Error(message);
error.code = code;
if (originalError) {
error.details = originalError;
}
return error;
}
}
exports.Agent = Agent;