import { getPolicyMemoryManager } from './policy-memory.js';
import { writeFileSync, mkdirSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { execFileSync } from 'node:child_process';
export class PolicyToolRegistry {
    _memory = null;
    _toolDir = null;
    get memory() {
        if (!this._memory) {
            this._memory = getPolicyMemoryManager();
        }
        return this._memory;
    }
    get toolDir() {
        if (!this._toolDir) {
            this._toolDir = join(process.cwd(), '.policy-tools');
            mkdirSync(this._toolDir, { recursive: true });
        }
        return this._toolDir;
    }
    async getExecutableTool(policyId) {
        const policy = await this.memory.getPolicy(policyId);
        if (!policy?.executableTools || policy.executableTools.length === 0) {
            return null;
        }
        for (const toolName of policy.executableTools) {
            const toolPath = join(this.toolDir, `${policyId}_${toolName}.py`);
            if (existsSync(toolPath)) {
                return toolPath;
            }
        }
        return null;
    }
    async storeToolCode(policyId, toolName, pythonCode) {
        await this.memory.storeExecutablePolicy(policyId, pythonCode, toolName);
        const filePath = join(this.toolDir, `${policyId}_${toolName}.py`);
        writeFileSync(filePath, pythonCode);
        return filePath;
    }
    async callPolicyTool(policyId, operation, args) {
        const toolPath = await this.getExecutableTool(policyId);
        if (!toolPath) {
            throw new Error(`No executable tool found for policy ${policyId}`);
        }
        // Use execFileSync with argument array to prevent shell injection
        const result = execFileSync('python3', [toolPath, '--operation', operation, '--args', JSON.stringify(args)], {
            encoding: 'utf-8',
            timeout: 30000,
        });
        try {
            const parsed = JSON.parse(result);
            return parsed ?? { raw: result.trim() };
        }
        catch {
            return { raw: result.trim() };
        }
    }
}
// Lazy singleton
let _instance = null;
export function getPolicyToolRegistry() {
    if (!_instance) {
        _instance = new PolicyToolRegistry();
    }
    return _instance;
}
//# sourceMappingURL=policy-tools.js.map