1inch-agent-kit
Version:
AI Agent Kit for 1inch - Connect any LLM to 1inch DeFi protocols
156 lines • 6.67 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const logger_1 = require("../utils/logger");
// Determine the correct functions root path
// Check if we're running in development mode by looking for ts-node in process.argv
const isDevelopment = process.argv.some(arg => arg.includes('ts-node')) ||
process.argv.some(arg => arg.includes('ts-node-esm')) ||
process.argv.some(arg => arg.includes('tsx'));
// More robust path resolution
const getFunctionsRoot = () => {
if (isDevelopment) {
// When running with ts-node, resolve from current working directory
return path_1.default.resolve(process.cwd(), "src/functions");
}
else {
// When running compiled code, resolve from __dirname
return path_1.default.resolve(__dirname, "../functions");
}
};
const FUNCTIONS_ROOT = getFunctionsRoot();
class Registry {
constructor() {
this.defs = [];
this.handlers = {};
this.initialized = false;
}
/** Scan every subfolder under functions */
async init() {
if (this.initialized)
return;
logger_1.logger.info("Initializing function registry...");
logger_1.logger.debug(`Functions root: ${FUNCTIONS_ROOT}`);
logger_1.logger.debug(`Development mode: ${isDevelopment}`);
logger_1.logger.debug(`Current working directory: ${process.cwd()}`);
logger_1.logger.debug(`Process argv: ${process.argv.join(' ')}`);
try {
const entries = await promises_1.default.readdir(FUNCTIONS_ROOT, { withFileTypes: true });
logger_1.logger.debug(`Found entries: ${entries.map(e => e.name).join(', ')}`);
for (const e of entries) {
if (!e.isDirectory())
continue;
const fnName = e.name;
logger_1.logger.debug(`Loading function: ${fnName}`);
const fnDir = path_1.default.join(FUNCTIONS_ROOT, fnName);
const schemaPath = path_1.default.join(fnDir, "schema.json");
// In development (ts-node), look for .ts files, in production look for .js files
const codePath = isDevelopment
? path_1.default.join(fnDir, "index.ts")
: path_1.default.join(fnDir, "index.js");
logger_1.logger.debug(`Using code path: ${codePath}`);
// Check if the code file exists
if (!(await this.fileExists(codePath))) {
logger_1.logger.warn(`Code file not found: ${codePath}, skipping function: ${fnName}`);
continue;
}
// 1) load JSON schema
const raw = await promises_1.default.readFile(schemaPath, "utf-8");
const schema = JSON.parse(raw);
this.defs.push(schema);
logger_1.logger.debug(`Loaded schema for: ${schema.name}`);
// 2) dynamic import of the code file
const mod = await Promise.resolve(`${codePath}`).then(s => __importStar(require(s)));
// assume named export matches folder name, else default export
const handler = mod[fnName] ?? mod.default;
if (typeof handler !== "function") {
throw new Error(`No function export found in ${codePath}`);
}
this.handlers[schema.name] = handler;
logger_1.logger.debug(`Loaded handler for: ${schema.name}`);
}
this.initialized = true;
logger_1.logger.info(`Registry initialized with ${this.defs.length} functions`);
}
catch (error) {
logger_1.logger.error("Failed to initialize registry:", error);
throw error;
}
}
async fileExists(filePath) {
try {
await promises_1.default.access(filePath);
return true;
}
catch {
return false;
}
}
getFunctionDefinitions() {
if (!this.initialized) {
throw new Error("Registry not initialized. Call init() first.");
}
return this.defs;
}
async callFunction(name, args) {
if (!this.initialized) {
throw new Error("Registry not initialized. Call init() first.");
}
const fn = this.handlers[name];
if (!fn)
throw new Error(`Function "${name}" not registered`);
logger_1.logger.debug(`Calling function: ${name} with args:`, args);
return await fn(args);
}
getAvailableFunctions() {
if (!this.initialized) {
throw new Error("Registry not initialized. Call init() first.");
}
return this.defs.map(d => d.name);
}
hasFunction(name) {
if (!this.initialized) {
throw new Error("Registry not initialized. Call init() first.");
}
return this.handlers.hasOwnProperty(name);
}
}
exports.default = new Registry();
//# sourceMappingURL=registry.js.map