// Copyright 2024-2026 Tymofii Pidlisnyi. Apache-2.0 license. See LICENSE.
/**
 * Google A2A Adapter — primitive mappings between A2A Agent Cards and APS passports.
 *
 * A2A pattern: Agent Card (discovery) → Task (work) → Artifact (output)
 * APS pattern: Passport (identity) → Delegation (scope) → Receipt (proof)
 *
 * This module is a pure mapping layer. Runtime governance (the stateful
 * lifecycle hook) lives in the gateway.
 */
import { verifyPassport } from '../verification/verify.js';
/** Derive APS-compatible scopes from an A2A Agent Card. */
export function deriveA2AScopes(card) {
    const scopes = [];
    if (card.skills) {
        for (const skill of card.skills) {
            scopes.push(`a2a:skill:${skill.id}`);
        }
    }
    if (card.capabilities?.streaming)
        scopes.push('a2a:streaming');
    if (card.capabilities?.pushNotifications)
        scopes.push('a2a:push');
    if (scopes.length === 0)
        scopes.push('a2a:task:execute');
    return scopes;
}
/** Convert APS passport to A2A Agent Card */
export function passportToA2ACard(passport, opts) {
    const p = passport.passport;
    const card = {
        name: p.agentName || p.agentId,
        description: p.mission,
        url: opts?.url,
        version: '1.0',
        capabilities: opts?.capabilities || {},
        securitySchemes: {
            aps_ed25519: {
                type: 'ed25519',
                publicKey: p.publicKey,
                agentId: p.agentId,
            },
        },
    };
    // Map delegation scope to skills
    if (opts?.skills) {
        card.skills = opts.skills;
    }
    else if (opts?.delegation) {
        card.skills = opts.delegation.scope.map(s => ({
            id: s,
            name: s.replace(/:/g, ' ').replace(/\b\w/g, c => c.toUpperCase()),
        }));
    }
    if (p.ownerAlias) {
        card.provider = { organization: p.ownerAlias };
    }
    return card;
}
/** Convert A2A Agent Card to APS passport metadata */
export function a2aCardToPassportMeta(card) {
    return {
        agentId: card.name.toLowerCase().replace(/[^a-z0-9-]/g, '-'),
        metadata: {
            a2a_name: card.name,
            a2a_description: card.description,
            a2a_url: card.url,
            a2a_provider: card.provider,
            a2a_version: card.version,
            a2a_capabilities: card.capabilities,
            a2a_skill_count: card.skills?.length || 0,
        },
    };
}
/** Verify an A2A agent has valid APS identity */
export function verifyA2AIdentity(card, passport) {
    const errors = [];
    const pc = verifyPassport(passport);
    if (!pc.valid)
        errors.push(...pc.errors);
    const p = passport.passport;
    if (card.name !== p.agentName && card.name !== p.agentId) {
        errors.push(`Card name "${card.name}" does not match passport agentName "${p.agentName}" or agentId "${p.agentId}"`);
    }
    const schemePubKey = card.securitySchemes?.aps_ed25519?.publicKey;
    if (schemePubKey && schemePubKey !== p.publicKey) {
        errors.push('Card security scheme publicKey does not match passport publicKey');
    }
    return { valid: errors.length === 0, errors };
}
/** Extract delegation scope from A2A skills */
export function a2aSkillsToScope(skills) {
    if (!skills || skills.length === 0)
        return [];
    return skills.map(s => `a2a:${s.id}`);
}
/** Embed APS trust signal in Agent Card extensions */
export function embedA2ATrustSignal(card, passport, trustEndpoint) {
    return {
        ...card,
        extensions: {
            ...card.extensions,
            aps_trust: {
                agentId: passport.passport.agentId,
                publicKey: passport.passport.publicKey,
                trustEndpoint: trustEndpoint || `https://gateway.aeoess.com/api/v1/public/trust/${passport.passport.agentId}`,
                protocol: 'agent-passport-system',
            },
        },
    };
}
//# sourceMappingURL=a2a.js.map