UNPKG

@monitoro/herd

Version:

Automate your browser, build AI web tools and MCP servers with Monitoro Herd

70 lines (69 loc) 2.75 kB
import { join } from "path"; import { ResourceProvider } from "./ResourceProvider.js"; import { getBuiltTrailPath, isTrailBuilt } from "./build.js"; import * as fs from "fs"; /** * Load a trail from a local directory * @param path Path to the trail directory * @returns Trail components: actions, URLs, selectors, and resources */ export async function loadTrail(path) { // Check if we have built files const useBuiltFiles = isTrailBuilt(path); // Check if we have direct built files (for downloaded trails) const hasDirectBuiltFiles = fs.existsSync(join(path, "actions.js")) && fs.existsSync(join(path, "urls.js")) && fs.existsSync(join(path, "selectors.js")); let basePath; let extension; if (useBuiltFiles) { // Built trail in .build directory basePath = getBuiltTrailPath(path); extension = '.js'; } else if (hasDirectBuiltFiles) { // Downloaded trail with JS files directly in the directory basePath = path; extension = '.js'; } else { // Source trail basePath = path; extension = '.ts'; } const actionsPath = join(basePath, `actions${extension}${extension === '.ts' ? '?ts=' + Date.now() : ''}`); const urlsPath = join(basePath, `urls${extension}${extension === '.ts' ? '?ts=' + Date.now() : ''}`); const selectorsPath = join(basePath, `selectors${extension}${extension === '.ts' ? '?ts=' + Date.now() : ''}`); try { let actions = (await import(actionsPath)); const urls = (await import(urlsPath)).default; const selectors = (await import(selectorsPath)).default; // Handle both default exports and named exports const actionClasses = actions.default || actions; actions = Object.values(actionClasses).reduce((acc, action) => { if (typeof action === 'function') { const instance = new action(); if (instance.manifest?.name) { acc[instance.manifest.name] = instance; } else { console.warn(`⚠️ Warning: Action class missing manifest.name`); } } return acc; }, {}); if (Object.keys(actions).length === 0) { console.warn(`⚠️ Warning: No valid actions found in ${actionsPath}`); } return { actions, urls, selectors, resources: new ResourceProvider(urls, selectors) }; } catch (error) { console.error(`❌ Error loading trail from ${path}:`, error); throw new Error(`Failed to load trail: ${error instanceof Error ? error.message : String(error)}`); } }