/**
 * SignalDock HTTP transport — provider-neutral agent messaging via SignalDock REST API.
 *
 * This transport enables CLEO agents to communicate through SignalDock's
 * infrastructure regardless of which AI coding tool hosts them.
 *
 * IMPORTANT: This is a TypeScript HTTP client. No SignalDock source code
 * has been copied. All interactions use the public REST API.
 *
 * @task T5671
 */
/**
 * SignalDock HTTP transport implementation.
 *
 * Communicates with a SignalDock server via its REST API to provide
 * provider-neutral inter-agent messaging with delivery guarantees.
 */
export class SignalDockTransport {
    name = 'signaldock';
    config;
    constructor(config) {
        this.config = config;
    }
    async register(name, agentClass, privacyTier) {
        const prefixedName = `${this.config.agentPrefix}${name}`;
        const body = {
            name: prefixedName,
            agentClass,
            privacyTier,
        };
        const response = await this.request('POST', '/agents', body);
        return {
            agentId: response.id,
            name: response.name,
            agentClass: response.agentClass,
            privacyTier: response.privacyTier,
        };
    }
    async deregister(agentId) {
        await this.request('DELETE', `/agents/${encodeURIComponent(agentId)}`);
    }
    async send(fromAgentId, toAgentId, content, conversationId) {
        let convId = conversationId;
        if (!convId) {
            const conversation = await this.createConversation([fromAgentId, toAgentId], 'private');
            convId = conversation.id;
        }
        const body = {
            conversationId: convId,
            fromAgentId,
            toAgentId,
            content,
            contentType: 'text',
        };
        const message = await this.request('POST', '/messages', body, fromAgentId);
        return {
            messageId: message.id,
            conversationId: message.conversationId,
            status: message.status === 'delivered' ? 'delivered' : 'pending',
        };
    }
    async poll(agentId, since) {
        const path = since
            ? `/messages/poll/new?since=${encodeURIComponent(since)}`
            : '/messages/poll/new';
        const messages = await this.request('GET', path, undefined, agentId);
        return messages;
    }
    async heartbeat(agentId) {
        await this.request('POST', `/agents/${encodeURIComponent(agentId)}/heartbeat`, undefined, agentId);
    }
    async createConversation(participants, visibility = 'private') {
        const body = {
            participants: [...participants].sort(),
            visibility,
        };
        return this.request('POST', '/conversations', body);
    }
    async getAgent(agentId) {
        try {
            return await this.request('GET', `/agents/${encodeURIComponent(agentId)}`);
        }
        catch {
            return null;
        }
    }
    /**
     * Make an HTTP request to the SignalDock API.
     */
    async request(method, path, body, agentId) {
        const url = `${this.config.endpoint}${path}`;
        const headers = {
            'Content-Type': 'application/json',
            Accept: 'application/json',
        };
        if (agentId) {
            headers['X-Agent-Id'] = agentId;
        }
        const init = {
            method,
            headers,
        };
        if (body !== undefined) {
            init.body = JSON.stringify(body);
        }
        const response = await fetch(url, init);
        if (!response.ok) {
            const text = await response.text().catch(() => '');
            throw new Error(`SignalDock API error: ${method} ${path} returned ${response.status}: ${text}`);
        }
        // DELETE may return no content
        if (response.status === 204) {
            return undefined;
        }
        const envelope = (await response.json());
        if (!envelope.success && envelope.error) {
            throw new Error(`SignalDock error [${envelope.error.code}]: ${envelope.error.message}`);
        }
        return envelope.data;
    }
}
//# sourceMappingURL=signaldock-transport.js.map