/*
 * 文件定位：agent_interfaceAdapter / 外部接口统一信封。
 * 核心目的：让 CLI、TUI、Raxode、Raxos 或自定义应用用同一种 envelope 接走 runtime 事件、审批、状态和管理命令。
 * 边界：只定义和校验接口信封，不实现 TAP/CMP/MP/multiagent 具体策略。
 */
function hasText(value) {
    return typeof value === "string" && value.trim().length > 0;
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary, publicSafe: true },
        events: ["agentCore.interfaceEnvelope.rejected"],
    };
}
export function createInterfaceEnvelope(input) {
    if (!hasText(input.envelopeId)) {
        return failure("MISSING_ENVELOPE_ID", "interface envelope requires an envelopeId", "input");
    }
    if (!hasText(input.kind)) {
        return failure("MISSING_KIND", "interface envelope requires a kind", "input");
    }
    if (!hasText(input.surface)) {
        return failure("MISSING_SURFACE", "interface envelope requires a target surface", "input");
    }
    if (!hasText(input.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "interface envelope requires a runtimeId", "input");
    }
    if (input.publicSafe === false) {
        return failure("UNSAFE_PAYLOAD", "interface envelope payload must be public-safe before leaving agentCore", "safety");
    }
    return {
        ok: true,
        envelope: {
            envelopeId: input.envelopeId.trim(),
            kind: input.kind,
            surface: input.surface,
            runtimeId: input.runtimeId.trim(),
            sessionId: input.sessionId?.trim() || undefined,
            correlationId: input.correlationId?.trim() || undefined,
            payload: input.payload,
            publicSafe: true,
            createdAt: input.createdAt ?? new Date(0).toISOString(),
            metadata: input.metadata ?? {},
        },
        events: ["agentCore.interfaceEnvelope.created"],
    };
}
export function approvalInterfaceEnvelope(input) {
    return createInterfaceEnvelope({
        envelopeId: `approval:${input.approvalId}`,
        kind: "approval",
        surface: input.surface ?? "application",
        runtimeId: input.runtimeId,
        sessionId: input.sessionId,
        payload: input.payload,
        createdAt: input.createdAt,
        metadata: { approvalId: input.approvalId },
    });
}
export function eventInterfaceEnvelope(input) {
    return createInterfaceEnvelope({
        envelopeId: `event:${input.eventId}`,
        kind: "event",
        surface: input.surface ?? "application",
        runtimeId: input.runtimeId,
        sessionId: input.sessionId,
        payload: input.payload,
        createdAt: input.createdAt,
        metadata: { eventId: input.eventId, ...(input.metadata ?? {}) },
    });
}
export function stateInterfaceEnvelope(input) {
    return createInterfaceEnvelope({
        envelopeId: `state:${input.stateId}`,
        kind: "state",
        surface: input.surface ?? "application",
        runtimeId: input.runtimeId,
        sessionId: input.sessionId,
        payload: input.payload,
        createdAt: input.createdAt,
        metadata: { stateId: input.stateId, ...(input.metadata ?? {}) },
    });
}
export function managementInterfaceEnvelope(input) {
    return createInterfaceEnvelope({
        envelopeId: `management:${input.commandId}`,
        kind: "management",
        surface: input.surface ?? "application",
        runtimeId: input.runtimeId,
        sessionId: input.sessionId,
        payload: input.payload,
        createdAt: input.createdAt,
        metadata: { commandId: input.commandId, ...(input.metadata ?? {}) },
    });
}
export function repairInterfaceEnvelope(input) {
    return createInterfaceEnvelope({
        envelopeId: `repair:${input.repairId}`,
        kind: "repair",
        surface: input.surface ?? "application",
        runtimeId: input.runtimeId,
        sessionId: input.sessionId,
        payload: input.payload,
        createdAt: input.createdAt,
        metadata: { repairId: input.repairId, ...(input.metadata ?? {}) },
    });
}
