/*
 * 文件定位：Agent 模型适配层 / 模型抽象层。
 * 核心目的：承载 harness Receiver Interface 这一能力位点。
 * 能力要求1：需要把文件名表达的能力落实成清晰的类型、输入输出和最小行为。
 * 能力要求2：如果后续发现语义不足，应优先补接口契约，而不是把逻辑散落到相邻文件。
 * 边界：负责把任意厂商/格式抽象到 DSL 定义的能力形态，不直接做 agentCore 内部最终调用。
 * 对接：需要被 runtime.modelAdapter 拉起，并和 provider/carrier、PromptPack lowering、能力抽象链路接通。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const harnessReceiverInterfaceDescriptor = {
    capability: "harness-receiver-interface",
    route: "agent_modelAdapter.abstractionLayer",
    purpose: "receive provider or custom-format capability signals into a provider-neutral abstraction envelope",
    providerRawShapeExposed: false,
    networkCalled: false,
    unsafeSideEffects: false,
};
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function uniqueStrings(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter(Boolean))];
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary, safeForRuntimeInspection: true, providerRawShapeExposed: false },
        events: ["modelAdapter.harnessReceiverInterface.rejected"],
    };
}
function deniedScope(requestedScopes, allowedScopes) {
    const requested = uniqueStrings(requestedScopes);
    if (requested.length === 0) {
        return undefined;
    }
    const allowed = new Set(uniqueStrings(allowedScopes));
    return requested.find((scope) => !allowed.has(scope));
}
function isAllowedSource(source, allowedSources) {
    const allowed = uniqueStrings(allowedSources);
    return allowed.length === 0 || allowed.includes(source);
}
function normalizeInterfaces(items) {
    return (items ?? []).map((item, index) => ({
        interfaceId: item.interfaceId?.trim() || `harness-output:${index + 1}`,
        modality: item.modality?.trim() || "unknown",
        channels: uniqueStrings(item.channels),
        scopes: uniqueStrings(item.scopes),
    }));
}
function hasCapabilityMaterial(signal) {
    return (uniqueStrings(signal.capabilities).length > 0 ||
        normalizeInterfaces(signal.outputInterfaces).length > 0 ||
        uniqueStrings(signal.scopes).length > 0 ||
        uniqueStrings(signal.formatHints).length > 0);
}
export function receiveHarnessReceiverInterface(request) {
    if (request === undefined || isBlank(request.receiverId)) {
        return failure("MISSING_RECEIVER_ID", "harnessReceiverInterface requires a receiverId", "input");
    }
    if (isBlank(request.harnessId)) {
        return failure("MISSING_HARNESS_ID", "harnessReceiverInterface requires a harnessId", "input");
    }
    if (request.signal === undefined) {
        return failure("MISSING_SIGNAL", "harnessReceiverInterface requires a capability signal envelope", "input");
    }
    if (request.contract?.accepted === false) {
        return failure("CONTRACT_REJECTED", request.contract.reason ?? "harness receiver was rejected by contract surface", "contract");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "harness receiver was rejected by runtime governance", "governance");
    }
    const denied = deniedScope(request.requestedScopes, request.allowedScopes);
    if (denied !== undefined) {
        return failure("SCOPE_DENIED", `harness receiver scope ${denied} is outside runtime governance`, "scope");
    }
    const providerId = request.signal.providerId?.trim();
    if (isBlank(providerId)) {
        return failure("MISSING_PROVIDER_ID", "harness capability signal requires a providerId", "input");
    }
    const source = request.signal.source?.trim();
    if (isBlank(source)) {
        return failure("MISSING_SOURCE", "harness capability signal requires a source", "source");
    }
    if (!isAllowedSource(source ?? "", request.allowedSources)) {
        return failure("SOURCE_DENIED", `harness source ${source} is outside the receiver boundary`, "source");
    }
    if (!hasCapabilityMaterial(request.signal)) {
        return failure("EMPTY_CAPABILITY_SIGNAL", "harness capability signal must include capabilities, output interfaces, scopes, or format hints", "input");
    }
    return {
        ok: true,
        received: {
            kind: "harness-receiver-interface",
            receiverId: request.receiverId?.trim() ?? "",
            harnessId: request.harnessId?.trim() ?? "",
            runtimeId: request.runtimeId?.trim() || undefined,
            providerId: providerId ?? "",
            modelId: request.signal.modelId?.trim() || undefined,
            source: source ?? "",
            capabilities: uniqueStrings(request.signal.capabilities),
            outputInterfaces: normalizeInterfaces(request.signal.outputInterfaces),
            scopes: uniqueStrings(request.signal.scopes),
            formatHints: uniqueStrings(request.signal.formatHints),
            rawShapeRef: request.signal.rawShapeRef?.trim() || undefined,
            providerRawShapeExposed: false,
            networkCalled: false,
            providerCallPlanned: false,
            unsafeSideEffects: false,
        },
        events: ["modelAdapter.harnessReceiverInterface.received"],
    };
}
