/**
 * Adapters for different agent platforms
 */
import { InstructionGateway } from '../engines/instruction-gateway.js';
import { resolveWorkspaceContext } from '../engines/workspace-resolver.js';
function extractInstructionPacket(message) {
    const payload = message.payload;
    const data = payload?.data;
    const packet = data?.instructionPacket;
    return packet && typeof packet === 'object' ? packet : undefined;
}
function familyForAdapter(type) {
    if (type === 'openclaw')
        return 'antigravity';
    return type;
}
class RenderedInstructionAdapter {
    name;
    type;
    connected = false;
    agents = [];
    gateway;
    gatewayRepoRoot;
    lastEnvelope;
    constructor(name, type) {
        this.name = name;
        this.type = type;
    }
    async connect() {
        this.connected = true;
        console.error(`[${this.name} Adapter] Connected`);
    }
    async disconnect() {
        this.connected = false;
        console.error(`[${this.name} Adapter] Disconnected`);
    }
    async send(message) {
        if (!this.connected) {
            throw new Error('Adapter not connected');
        }
        const packet = extractInstructionPacket(message);
        if (packet) {
            this.lastEnvelope = this.getGateway().renderEnvelope(packet, familyForAdapter(this.type));
            console.error(`[${this.name} Adapter] Rendered ${this.lastEnvelope.format} packet ${this.lastEnvelope.packetHash}`);
            return;
        }
        console.error(`[${this.name} Adapter] Sending generic message:`, message.type);
    }
    receive(message) {
        console.error(`[${this.name} Adapter] Received:`, message.type);
    }
    async spawnAgent(type, config) {
        const agentId = `${this.name}_${type}_${Date.now()}`;
        this.agents.push(agentId);
        console.error(`[${this.name} Adapter] Spawned agent: ${agentId}`);
        if (config && typeof config === 'object') {
            const maybePacket = config.instructionPacket;
            if (maybePacket) {
                this.lastEnvelope = this.getGateway().renderEnvelope(maybePacket, familyForAdapter(this.type));
            }
        }
        return agentId;
    }
    getGateway() {
        const repoRoot = resolveWorkspaceContext().repoRoot;
        if (!this.gateway || this.gatewayRepoRoot !== repoRoot) {
            this.gateway = new InstructionGateway(repoRoot);
            this.gatewayRepoRoot = repoRoot;
        }
        return this.gateway;
    }
    getLastEnvelope() {
        return this.lastEnvelope;
    }
}
export class OpenClawAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('openclaw', 'openclaw');
    }
}
export class HermesAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('hermes', 'hermes');
    }
}
export class NanoClawAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('nanoclaw', 'nanoclaw');
    }
}
export class PicoClawAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('picoclaw', 'picoclaw');
    }
}
export class ClaudeCodeAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('claude-code', 'claude-code');
    }
}
export class RufloAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('ruflo', 'ruflo');
    }
}
export class CodexAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('codex', 'codex');
    }
}
export class OpencodeAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('opencode', 'opencode');
    }
}
export class CursorAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('cursor', 'cursor');
    }
}
export class WindsurfAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('windsurf', 'windsurf');
    }
}
export class AiderAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('aider', 'aider');
    }
}
export class ContinueAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('continue', 'continue');
    }
}
export class ClineAdapter extends RenderedInstructionAdapter {
    constructor() {
        super('cline', 'cline');
    }
}
export class CustomAdapter extends RenderedInstructionAdapter {
    sendHandler;
    receiveHandler;
    constructor(name) {
        super(name, 'custom');
    }
    async send(message) {
        if (!this.connected) {
            throw new Error('Adapter not connected');
        }
        if (this.sendHandler) {
            await this.sendHandler(message);
            return;
        }
        await super.send(message);
    }
    receive(message) {
        if (this.receiveHandler) {
            this.receiveHandler(message);
            return;
        }
        super.receive(message);
    }
    setSendHandler(handler) {
        this.sendHandler = handler;
    }
    setReceiveHandler(handler) {
        this.receiveHandler = handler;
    }
}
import { MCPAdapter } from './adapters/mcp.js';
export function createAdapter(type, customName) {
    switch (type) {
        case 'openclaw':
            return new OpenClawAdapter();
        case 'hermes':
            return new HermesAdapter();
        case 'nanoclaw':
            return new NanoClawAdapter();
        case 'picoclaw':
            return new PicoClawAdapter();
        case 'claude-code':
            return new ClaudeCodeAdapter();
        case 'ruflo':
            return new RufloAdapter();
        case 'codex':
            return new CodexAdapter();
        case 'opencode':
            return new OpencodeAdapter();
        case 'cursor':
            return new CursorAdapter();
        case 'windsurf':
            return new WindsurfAdapter();
        case 'aider':
            return new AiderAdapter();
        case 'continue':
            return new ContinueAdapter();
        case 'cline':
            return new ClineAdapter();
        case 'mcp':
            return new MCPAdapter();
        case 'custom':
            return new CustomAdapter(customName ?? 'custom');
        default:
            throw new Error(`Unknown adapter type: ${type}`);
    }
}
