/**
 * UNIVERSAL CAPABILITY FRAMEWORK
 *
 * A unified framework for all AGI capabilities that promotes code reuse,
 * consistent patterns, and cross-module integration.
 *
 * KEY PRINCIPLES:
 * 1. Single Source of Truth - Common utilities used by all capabilities
 * 2. Dependency Injection - Capabilities declare dependencies
 * 3. Event-Driven Architecture - Cross-module communication via events
 * 4. Pluggable Architecture - Capabilities can be registered/unregistered at runtime
 * 5. Type Safety - Full TypeScript support with generic types
 */
import { EventEmitter } from 'events';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as crypto from 'crypto';
import { execSync } from 'child_process';
// ============================================================================
// UNIVERSAL FRAMEWORK CORE
// ============================================================================
export class UniversalCapabilityFramework extends EventEmitter {
    config;
    capabilities = new Map();
    dependencyGraph = {
        nodes: new Map(),
        edges: new Map(),
        topologicalOrder: [],
        hasCycles: false
    };
    sharedUtilities;
    eventBus;
    contextManager;
    toolRegistry;
    constructor(config = {}) {
        super();
        this.config = {
            rootDir: config.rootDir || process.cwd(),
            debug: config.debug || false,
            enableEvents: config.enableEvents || true,
            enableDependencyResolution: config.enableDependencyResolution || true,
            sharedDataDir: config.sharedDataDir || path.join(os.tmpdir(), 'agi-universal-framework'),
            pluginPatterns: config.pluginPatterns || ['**/*.ts', '**/*.js']
        };
        this.sharedUtilities = new SharedUniversalUtilities(this.config);
        this.eventBus = new EventEmitter();
        this.contextManager = new ContextManager();
        this.toolRegistry = new ToolRegistry();
        this.initializeFramework();
    }
    initializeFramework() {
        // Create shared directories
        fs.mkdirSync(this.config.sharedDataDir, { recursive: true });
        // Initialize shared utilities
        this.sharedUtilities = new SharedUniversalUtilities(this.config);
        // Initialize core systems
        this.contextManager = new ContextManager();
        this.toolRegistry = new ToolRegistry();
        this.log('info', 'Universal Framework initialized', {
            rootDir: this.config.rootDir,
            sharedDataDir: this.config.sharedDataDir
        });
    }
    /**
     * Register a capability with the framework
     */
    async registerCapability(module, metadata) {
        const registration = {
            module,
            metadata,
            instance: null,
            status: 'registered',
            dependencies: metadata.dependencies || [],
            dependents: []
        };
        this.capabilities.set(metadata.id, registration);
        this.updateDependencyGraph();
        this.log('info', `Capability registered: ${metadata.id}`, {
            version: metadata.version,
            dependencies: metadata.dependencies
        });
        return registration;
    }
    /**
     * Activate a capability (resolve dependencies and initialize)
     */
    async activateCapability(capabilityId) {
        const registration = this.capabilities.get(capabilityId);
        if (!registration) {
            throw new Error(`Capability not found: ${capabilityId}`);
        }
        if (registration.status === 'active') {
            return registration;
        }
        // Resolve and activate dependencies
        if (this.config.enableDependencyResolution) {
            await this.activateDependencies(capabilityId);
        }
        // Initialize capability instance
        try {
            // Create context for capability
            const context = {
                profile: 'default',
                workspaceContext: null,
                workingDir: this.config.rootDir,
                env: process.env
            };
            // Create capability instance
            const result = await registration.module.create(context);
            registration.instance = result;
            registration.status = 'active';
            this.log('info', `Capability activated: ${capabilityId}`);
            this.emit('capability:activated', {
                capabilityId,
                registration
            });
        }
        catch (error) {
            registration.status = 'error';
            this.log('error', `Failed to activate capability ${capabilityId}`, error);
            throw error;
        }
        return registration;
    }
    /**
     * Activate all dependencies for a capability
     */
    async activateDependencies(capabilityId) {
        const registration = this.capabilities.get(capabilityId);
        if (!registration)
            return;
        for (const depId of registration.dependencies) {
            const depRegistration = this.capabilities.get(depId);
            if (!depRegistration) {
                throw new Error(`Dependency not found: ${depId} required by ${capabilityId}`);
            }
            if (depRegistration.status !== 'active') {
                await this.activateCapability(depId);
            }
        }
    }
    /**
     * Get a capability instance
     */
    getCapability(capabilityId) {
        const registration = this.capabilities.get(capabilityId);
        if (!registration || registration.status !== 'active') {
            return null;
        }
        return registration.instance;
    }
    /**
     * Execute a cross-capability operation
     */
    async executeOperation(operation, parameters, capabilities) {
        const operationId = crypto.randomUUID();
        const startTime = Date.now();
        this.log('info', `Starting cross-capability operation: ${operation}`, {
            operationId,
            capabilities
        });
        this.emit('operation:started', {
            operationId,
            operation,
            parameters,
            capabilities
        });
        // Activate required capabilities
        for (const capabilityId of capabilities) {
            await this.activateCapability(capabilityId);
        }
        // Execute operation across capabilities
        const results = {};
        for (const capabilityId of capabilities) {
            const capability = this.getCapability(capabilityId);
            if (capability && typeof capability.execute === 'function') {
                try {
                    results[capabilityId] = await capability.execute({
                        operation,
                        parameters,
                        operationId
                    });
                }
                catch (error) {
                    results[capabilityId] = {
                        error: error instanceof Error ? error.message : String(error)
                    };
                }
            }
        }
        const duration = Date.now() - startTime;
        this.log('info', `Completed cross-capability operation: ${operation}`, {
            operationId,
            duration,
            success: true
        });
        this.emit('operation:completed', {
            operationId,
            operation,
            parameters,
            results,
            duration
        });
        return results;
    }
    /**
     * Update dependency graph
     */
    updateDependencyGraph() {
        this.dependencyGraph.nodes = this.capabilities;
        this.dependencyGraph.edges = new Map();
        // Build edges
        for (const [id, registration] of this.capabilities) {
            const edges = new Set();
            for (const depId of registration.dependencies) {
                edges.add(depId);
            }
            this.dependencyGraph.edges.set(id, edges);
        }
        // Detect cycles
        this.dependencyGraph.hasCycles = this.detectCycles();
        // Calculate topological order
        this.dependencyGraph.topologicalOrder = this.topologicalSort();
    }
    detectCycles() {
        const visited = new Set();
        const recStack = new Set();
        const dfs = (nodeId) => {
            if (recStack.has(nodeId))
                return true;
            if (visited.has(nodeId))
                return false;
            visited.add(nodeId);
            recStack.add(nodeId);
            const edges = this.dependencyGraph.edges.get(nodeId) || new Set();
            for (const neighbor of edges) {
                if (dfs(neighbor))
                    return true;
            }
            recStack.delete(nodeId);
            return false;
        };
        for (const nodeId of this.dependencyGraph.nodes.keys()) {
            if (dfs(nodeId))
                return true;
        }
        return false;
    }
    topologicalSort() {
        const visited = new Set();
        const order = [];
        const dfs = (nodeId) => {
            if (visited.has(nodeId))
                return;
            visited.add(nodeId);
            const edges = this.dependencyGraph.edges.get(nodeId) || new Set();
            for (const neighbor of edges) {
                dfs(neighbor);
            }
            order.unshift(nodeId);
        };
        for (const nodeId of this.dependencyGraph.nodes.keys()) {
            dfs(nodeId);
        }
        return order;
    }
    /**
     * Logging utility
     */
    log(level, message, data) {
        if (!this.config.debug && level === 'info')
            return;
        const timestamp = new Date().toISOString();
        const logEntry = {
            timestamp,
            level,
            message,
            data
        };
        console.log(`[${timestamp}] [${level.toUpperCase()}] ${message}`);
        if (data && this.config.debug) {
            console.log(JSON.stringify(data, null, 2));
        }
        this.emit('log', logEntry);
    }
    // ============================================================================
    // PUBLIC API
    // ============================================================================
    /**
     * Get framework configuration
     */
    getConfig() {
        return { ...this.config };
    }
    /**
     * List all registered capabilities
     */
    listCapabilities() {
        return Array.from(this.capabilities.values());
    }
    /**
     * Get dependency graph
     */
    getDependencyGraph() {
        return { ...this.dependencyGraph };
    }
    /**
     * Get shared utilities
     */
    getSharedUtilities() {
        return this.sharedUtilities;
    }
    /**
     * Get event bus
     */
    getEventBus() {
        return this.eventBus;
    }
    /**
     * Get tool registry
     */
    getToolRegistry() {
        return this.toolRegistry;
    }
}
// ============================================================================
// SHARED UNIVERSAL UTILITIES
// ============================================================================
export class SharedUniversalUtilities {
    config;
    constructor(config) {
        this.config = config;
    }
    /**
     * Generate unique operation ID
     */
    generateOperationId(prefix = 'op') {
        const timestamp = Date.now();
        const random = crypto.randomBytes(8).toString('hex');
        return `${prefix}_${timestamp}_${random}`;
    }
    /**
     * Create shared data directory for operation
     */
    createOperationDir(operationId) {
        const dirPath = path.join(this.config.sharedDataDir, 'operations', operationId);
        fs.mkdirSync(dirPath, { recursive: true });
        return dirPath;
    }
    /**
     * Save evidence/data to shared storage
     */
    saveToSharedStorage(operationId, fileName, data) {
        const opDir = this.createOperationDir(operationId);
        const filePath = path.join(opDir, fileName);
        const content = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
        fs.writeFileSync(filePath, content, 'utf8');
        return filePath;
    }
    /**
     * Read from shared storage
     */
    readFromSharedStorage(operationId, fileName) {
        const filePath = path.join(this.config.sharedDataDir, 'operations', operationId, fileName);
        if (!fs.existsSync(filePath))
            return null;
        return fs.readFileSync(filePath, 'utf8');
    }
    /**
     * Merge multiple objects (deep merge)
     */
    deepMerge(target, ...sources) {
        const result = { ...target };
        for (const source of sources) {
            for (const key in source) {
                if (source[key] !== null && typeof source[key] === 'object' && !Array.isArray(source[key])) {
                    if (!result[key] || typeof result[key] !== 'object') {
                        result[key] = {};
                    }
                    result[key] = this.deepMerge(result[key], source[key]);
                }
                else {
                    result[key] = source[key];
                }
            }
        }
        return result;
    }
    /**
     * Validate configuration against schema
     */
    validateConfig(config, schema) {
        const errors = [];
        const validateObject = (obj, sch, path = '') => {
            if (sch.type === 'object' && sch.properties) {
                for (const [key, propSchema] of Object.entries(sch.properties)) {
                    const fullPath = path ? `${path}.${key}` : key;
                    const typedPropSchema = propSchema;
                    if (typedPropSchema.required && obj[key] === undefined) {
                        errors.push(`Missing required property: ${fullPath}`);
                    }
                    else if (obj[key] !== undefined) {
                        validateObject(obj[key], propSchema, fullPath);
                    }
                }
            }
        };
        validateObject(config, schema);
        return {
            valid: errors.length === 0,
            errors
        };
    }
    /**
     * Execute with retry logic
     */
    async executeWithRetry(operation, maxRetries = 3, delay = 1000) {
        let lastError;
        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            try {
                return await operation();
            }
            catch (error) {
                lastError = error;
                if (attempt < maxRetries) {
                    await new Promise(resolve => setTimeout(resolve, delay * attempt));
                }
            }
        }
        throw lastError;
    }
    /**
     * Create standardized tool definition
     */
    createToolDefinition(name, description, parameters, handler) {
        return {
            name,
            description,
            parameters,
            handler
        };
    }
}
// ============================================================================
// CONTEXT MANAGER
// ============================================================================
export class ContextManager {
    contexts = new Map();
    defaultContext = {};
    /**
     * Set context value
     */
    setContext(key, value) {
        this.contexts.set(key, value);
    }
    /**
     * Get context value
     */
    getContext(key) {
        return this.contexts.get(key);
    }
    /**
     * Clear context
     */
    clearContext(key) {
        return this.contexts.delete(key);
    }
    /**
     * Get all contexts
     */
    getAllContexts() {
        return Object.fromEntries(this.contexts.entries());
    }
    /**
     * Save context to file
     */
    saveContexts(filePath) {
        const data = this.getAllContexts();
        fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
    }
    /**
     * Load contexts from file
     */
    loadContexts(filePath) {
        if (!fs.existsSync(filePath))
            return;
        const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
        for (const [key, value] of Object.entries(data)) {
            this.contexts.set(key, value);
        }
    }
}
// ============================================================================
// TOOL REGISTRY
// ============================================================================
export class ToolRegistry {
    tools = new Map();
    toolSuites = new Map();
    /**
     * Register a tool
     */
    registerTool(tool) {
        this.tools.set(tool.name, tool);
    }
    /**
     * Register a tool suite
     */
    registerToolSuite(suite) {
        this.toolSuites.set(suite.id, suite);
        for (const tool of suite.tools) {
            this.registerTool(tool);
        }
    }
    /**
     * Get a tool by name
     */
    getTool(name) {
        return this.tools.get(name);
    }
    /**
     * Get a tool suite by ID
     */
    getToolSuite(id) {
        return this.toolSuites.get(id);
    }
    /**
     * List all tools
     */
    listTools() {
        return Array.from(this.tools.values());
    }
    /**
     * List all tool suites
     */
    listToolSuites() {
        return Array.from(this.toolSuites.values());
    }
    /**
     * Search tools by criteria
     */
    searchTools(criteria) {
        return Array.from(this.tools.values()).filter(criteria);
    }
}
// ============================================================================
// UNIVERSAL CAPABILITY BASE CLASS
// ============================================================================
export class UniversalCapabilityModule {
    framework;
    utilities;
    contextManager;
    toolRegistry;
    config = {};
    constructor(framework, config = {}) {
        this.framework = framework;
        this.utilities = framework.getSharedUtilities();
        this.contextManager = new ContextManager();
        this.toolRegistry = framework.getToolRegistry();
        this.config = config;
    }
    /**
     * Initialize capability (called after constructor)
     */
    async initialize() {
        // Default implementation does nothing
    }
    /**
     * Cleanup capability (called before disposal)
     */
    async cleanup() {
        // Default implementation does nothing
    }
    /**
     * Execute an operation (common interface for all capabilities)
     */
    async execute(params) {
        throw new Error(`Execute method not implemented for capability: ${this.id}`);
    }
    /**
     * Validate configuration
     */
    validateConfig(schema) {
        return this.utilities.validateConfig(this.config, schema);
    }
    /**
     * Log with capability context
     */
    log(level, message, data) {
        const logData = {
            capabilityId: this.id,
            ...data
        };
        this.framework.emit('log', {
            timestamp: Date.now(),
            level,
            message,
            data: logData
        });
    }
    /**
     * Emit framework event
     */
    emitEvent(type, data) {
        this.framework.emit(`capability:${this.id}:${type}`, {
            timestamp: Date.now(),
            source: this.id,
            data
        });
    }
    /**
     * Create fallback file tool for filesystem capability
     */
    createFallbackFileTool() {
        return this.utilities.createToolDefinition('read_file_fallback', 'Fallback file reading tool', {
            type: 'object',
            properties: {
                path: { type: 'string', description: 'Path to file' }
            },
            required: ['path']
        }, async (args) => {
            try {
                return fs.readFileSync(args.path, 'utf8');
            }
            catch (error) {
                return `Error reading file: ${error.message}`;
            }
        });
    }
}
// ============================================================================
// UNIVERSAL CAPABILITY FACTORY
// ============================================================================
export class UniversalCapabilityFactory {
    static capabilityRegistry = new Map();
    /**
     * Register a capability class
     */
    static registerCapability(id, constructor) {
        this.capabilityRegistry.set(id, constructor);
    }
    /**
     * Create capability instance
     */
    static createCapability(id, framework, config = {}) {
        const Constructor = this.capabilityRegistry.get(id);
        if (!Constructor)
            return null;
        return new Constructor(framework, config);
    }
    /**
     * List all registered capability types
     */
    static listCapabilityTypes() {
        return Array.from(this.capabilityRegistry.keys());
    }
}
// ============================================================================
// PRE-BUILT UNIVERSAL CAPABILITIES
// ============================================================================
/**
 * Universal Filesystem Capability
 */
export class UniversalFilesystemCapability extends UniversalCapabilityModule {
    id = 'capability.universal-filesystem';
    metadata = {
        id: this.id,
        version: '1.0.0',
        description: 'Universal filesystem operations with cross-platform support',
        author: 'AGI Core Team',
        dependencies: [],
        provides: ['filesystem.read', 'filesystem.write', 'filesystem.list', 'filesystem.search'],
        requires: [],
        category: 'core',
        tags: ['filesystem', 'io', 'storage']
    };
    async create(context) {
        try {
            const { createFileTools } = await import('../tools/fileTools.js');
            return {
                id: 'filesystem.universal',
                description: 'Universal filesystem access with enhanced capabilities',
                toolSuite: {
                    id: 'fs-universal',
                    description: 'Universal filesystem operations',
                    tools: createFileTools(context.workingDir)
                },
                metadata: {
                    workingDir: context.workingDir,
                    platform: os.platform(),
                    capabilities: this.metadata.provides
                }
            };
        }
        catch (error) {
            // Fallback to simple implementation if module not available
            return {
                id: 'filesystem.universal-fallback',
                description: 'Universal filesystem access (fallback mode)',
                toolSuite: {
                    id: 'fs-universal-fallback',
                    description: 'Universal filesystem operations (fallback)',
                    tools: [this.createFallbackFileTool()]
                },
                metadata: {
                    workingDir: context.workingDir,
                    platform: os.platform(),
                    capabilities: this.metadata.provides,
                    fallback: true
                }
            };
        }
    }
    async execute(params) {
        const opId = params.operationId || this.utilities.generateOperationId('fs');
        switch (params.operation) {
            case 'read':
                return this.readFile(params.parameters.path);
            case 'write':
                return this.writeFile(params.parameters.path, params.parameters.content);
            case 'list':
                return this.listDirectory(params.parameters.path, params.parameters.recursive);
            case 'search':
                return this.searchFiles(params.parameters.pattern, params.parameters.path);
            default:
                throw new Error(`Unknown filesystem operation: ${params.operation}`);
        }
    }
    readFile(filePath) {
        return fs.readFileSync(filePath, 'utf8');
    }
    writeFile(filePath, content) {
        fs.writeFileSync(filePath, content, 'utf8');
    }
    listDirectory(dirPath, recursive = false) {
        if (!fs.existsSync(dirPath))
            return [];
        const items = fs.readdirSync(dirPath, { withFileTypes: true });
        const result = [];
        for (const item of items) {
            const fullPath = path.join(dirPath, item.name);
            result.push(fullPath);
            if (recursive && item.isDirectory()) {
                result.push(...this.listDirectory(fullPath, true));
            }
        }
        return result;
    }
    searchFiles(pattern, basePath) {
        const files = [];
        const searchDir = (dir) => {
            try {
                const items = fs.readdirSync(dir);
                for (const item of items) {
                    const fullPath = path.join(dir, item);
                    const stat = fs.statSync(fullPath);
                    if (stat.isDirectory()) {
                        searchDir(fullPath);
                    }
                    else if (item.includes(pattern) || fullPath.includes(pattern)) {
                        files.push(fullPath);
                    }
                }
            }
            catch (error) {
                // Skip directories we can't read
            }
        };
        searchDir(basePath);
        return files;
    }
}
/**
 * Universal Bash Capability
 */
export class UniversalBashCapability extends UniversalCapabilityModule {
    id = 'capability.universal-bash';
    metadata = {
        id: this.id,
        version: '1.0.0',
        description: 'Universal bash/shell execution with cross-platform support',
        author: 'AGI Core Team',
        dependencies: ['capability.universal-filesystem'],
        provides: ['bash.execute', 'bash.script', 'bash.pipeline', 'bash.background'],
        requires: [],
        category: 'core',
        tags: ['bash', 'shell', 'execution']
    };
    async create(context) {
        try {
            const { createBashTools } = await import('../tools/bashTools.js');
            return {
                id: 'bash.universal',
                description: 'Universal bash execution with enhanced capabilities',
                toolSuite: {
                    id: 'bash-universal',
                    description: 'Universal bash/shell operations',
                    tools: createBashTools(context.workingDir)
                },
                metadata: {
                    workingDir: context.workingDir,
                    platform: os.platform(),
                    shell: process.env.SHELL || 'bash',
                    capabilities: this.metadata.provides
                }
            };
        }
        catch (error) {
            // Fallback to simple implementation
            return {
                id: 'bash.universal-fallback',
                description: 'Universal bash execution (fallback mode)',
                toolSuite: {
                    id: 'bash-universal-fallback',
                    description: 'Universal bash/shell operations (fallback)',
                    tools: [this.createFallbackBashTool()]
                },
                metadata: {
                    workingDir: context.workingDir,
                    platform: os.platform(),
                    capabilities: this.metadata.provides,
                    fallback: true
                }
            };
        }
    }
    createFallbackBashTool() {
        return this.utilities.createToolDefinition('execute_command_fallback', 'Fallback command execution tool', {
            type: 'object',
            properties: {
                command: { type: 'string', description: 'Command to execute' },
                cwd: { type: 'string', description: 'Working directory', default: process.cwd() }
            },
            required: ['command']
        }, async (args) => {
            try {
                const result = execSync(args.command, {
                    cwd: args.cwd || process.cwd(),
                    encoding: 'utf8',
                    timeout: 30000
                });
                return result.trim();
            }
            catch (error) {
                return `Error executing command: ${error.message}`;
            }
        });
    }
    async execute(params) {
        const opId = params.operationId || this.utilities.generateOperationId('bash');
        try {
            const result = execSync(params.parameters.command, {
                cwd: params.parameters.cwd || this.config.workingDir || process.cwd(),
                encoding: 'utf8',
                timeout: params.parameters.timeout || 30000
            });
            return {
                success: true,
                output: result.trim(),
                exitCode: 0
            };
        }
        catch (error) {
            return {
                success: false,
                output: error.stderr?.toString() || error.message,
                exitCode: error.status || 1
            };
        }
    }
}
/**
 * Universal Search Capability
 */
export class UniversalSearchCapability extends UniversalCapabilityModule {
    id = 'capability.universal-search';
    metadata = {
        id: this.id,
        version: '1.0.0',
        description: 'Universal search across files, content, and definitions',
        author: 'AGI Core Team',
        dependencies: ['capability.universal-filesystem'],
        provides: ['search.files', 'search.content', 'search.definitions', 'search.pattern'],
        requires: [],
        category: 'core',
        tags: ['search', 'find', 'grep', 'pattern']
    };
    async create(context) {
        try {
            const { createSearchTools } = await import('../tools/searchTools.js');
            return {
                id: 'search.universal',
                description: 'Universal search across files and content',
                toolSuite: {
                    id: 'search-universal',
                    description: 'Universal search operations',
                    tools: createSearchTools(context.workingDir)
                },
                metadata: {
                    workingDir: context.workingDir,
                    capabilities: this.metadata.provides
                }
            };
        }
        catch (error) {
            // Fallback to simple implementation
            return {
                id: 'search.universal-fallback',
                description: 'Universal search (fallback mode)',
                toolSuite: {
                    id: 'search-universal-fallback',
                    description: 'Universal search operations (fallback)',
                    tools: [this.createFallbackSearchTool()]
                },
                metadata: {
                    workingDir: context.workingDir,
                    capabilities: this.metadata.provides,
                    fallback: true
                }
            };
        }
    }
    createFallbackSearchTool() {
        return this.utilities.createToolDefinition('search_fallback', 'Fallback search tool', {
            type: 'object',
            properties: {
                pattern: { type: 'string', description: 'Search pattern' },
                path: { type: 'string', description: 'Search directory', default: process.cwd() }
            },
            required: ['pattern']
        }, async (args) => {
            const searchDir = args.path || process.cwd();
            const files = [];
            const search = (dir) => {
                try {
                    const items = fs.readdirSync(dir);
                    for (const item of items) {
                        const fullPath = path.join(dir, item);
                        const stat = fs.statSync(fullPath);
                        if (stat.isDirectory()) {
                            search(fullPath);
                        }
                        else if (item.includes(args.pattern) || fullPath.includes(args.pattern)) {
                            files.push(fullPath);
                        }
                    }
                }
                catch (error) {
                    // Skip unreadable directories
                }
            };
            search(searchDir);
            return files.length > 0 ? files.join('\n') : 'No matches found';
        });
    }
    async execute(params) {
        const opId = params.operationId || this.utilities.generateOperationId('search');
        // Implementation would use the search tools
        // For now, return a placeholder
        return {
            operation: params.operation,
            parameters: params.parameters,
            results: []
        };
    }
}
/**
 * Universal Edit Capability
 */
export class UniversalEditCapability extends UniversalCapabilityModule {
    id = 'capability.universal-edit';
    metadata = {
        id: this.id,
        version: '1.0.0',
        description: 'Universal file editing with exact string replacement',
        author: 'AGI Core Team',
        dependencies: ['capability.universal-filesystem'],
        provides: ['edit.file', 'edit.replace', 'edit.create', 'edit.delete'],
        requires: [],
        category: 'core',
        tags: ['edit', 'modify', 'file', 'code']
    };
    async create(context) {
        try {
            const { createEditTools } = await import('../tools/editTools.js');
            return {
                id: 'edit.universal',
                description: 'Universal file editing capabilities',
                toolSuite: {
                    id: 'edit-universal',
                    description: 'Universal edit operations',
                    tools: createEditTools(context.workingDir || process.cwd())
                },
                metadata: {
                    workingDir: context.workingDir,
                    capabilities: this.metadata.provides
                }
            };
        }
        catch (error) {
            // Fallback to simple implementation
            return {
                id: 'edit.universal-fallback',
                description: 'Universal file editing (fallback mode)',
                toolSuite: {
                    id: 'edit-universal-fallback',
                    description: 'Universal edit operations (fallback)',
                    tools: [this.createFallbackEditTool()]
                },
                metadata: {
                    workingDir: context.workingDir,
                    capabilities: this.metadata.provides,
                    fallback: true
                }
            };
        }
    }
    createFallbackEditTool() {
        return this.utilities.createToolDefinition('edit_file_fallback', 'Fallback file editing tool', {
            type: 'object',
            properties: {
                file_path: { type: 'string', description: 'Path to file' },
                old_string: { type: 'string', description: 'Text to replace' },
                new_string: { type: 'string', description: 'New text', default: '' }
            },
            required: ['file_path', 'old_string']
        }, async (args) => {
            try {
                const content = fs.readFileSync(args.file_path, 'utf8');
                const newContent = content.replace(args.old_string, args.new_string || '');
                fs.writeFileSync(args.file_path, newContent, 'utf8');
                return `File ${args.file_path} updated successfully`;
            }
            catch (error) {
                return `Error editing file: ${error.message}`;
            }
        });
    }
    async execute(params) {
        const opId = params.operationId || this.utilities.generateOperationId('edit');
        // Implementation would use the edit tools
        // For now, return a placeholder
        return {
            operation: params.operation,
            parameters: params.parameters,
            success: true
        };
    }
}
// ============================================================================
// UNIVERSAL FRAMEWORK EXPORTS
// ============================================================================
export default UniversalCapabilityFramework;
// Note: Individual capability classes are already exported at their definitions
// (UniversalFilesystemCapability, UniversalBashCapability, UniversalSearchCapability, UniversalEditCapability)
//# sourceMappingURL=universalCapabilityFramework.js.map