// ACPAdapter — bridges ACP protocol to IM-Hub's AgentAdapter interface.
//
// P2-C: sessionId is now forwarded to the remote agent in the request body
// (as `session_id`) so multi-turn conversations preserve context across
// HTTP calls. Remote agents that don't recognize the field simply ignore
// it; this is a forward-compatible spec extension.
import { ACPClient } from './acp-client.js';
import { logger as rootLogger } from '../../../core/logger.js';
const log = rootLogger.child({ component: 'acp-adapter' });
export class ACPAdapter {
    name;
    aliases;
    kind = 'remote';
    client;
    endpoint;
    constructor(config) {
        this.name = config.name;
        this.aliases = config.aliases || [];
        this.endpoint = config.endpoint;
        this.client = new ACPClient(config);
    }
    describe() {
        try {
            const u = new URL(this.endpoint);
            return `host: ${u.host}`;
        }
        catch {
            return this.endpoint || undefined;
        }
    }
    async isAvailable() {
        return this.client.healthCheck();
    }
    async *sendPrompt(sessionId, prompt, history, _opts) {
        // Try streaming first, fall back to sync.
        // NOTE: Fallback creates a new task; if the streaming task was partially
        // processed, the agent may repeat work. Acceptable for v1.
        try {
            for await (const chunk of this.client.streamPrompt(prompt, history, sessionId)) {
                yield chunk;
            }
        }
        catch (streamError) {
            const errMsg = streamError instanceof Error ? streamError.message : String(streamError);
            log.warn({ agent: this.name, error: errMsg, sessionId }, 'ACP streaming failed, falling back to sync');
            const response = await this.client.sendPromptSync(prompt, history, sessionId);
            if (response)
                yield response;
        }
    }
    /** Get the agent manifest for display (not part of AgentAdapter interface) */
    async getManifest() {
        try {
            return await this.client.fetchManifest();
        }
        catch {
            return undefined;
        }
    }
}
//# sourceMappingURL=acp-adapter.js.map