/*
 * 文件定位：Agent 模型适配层 / agentCore 内部桥接层。
 * 核心目的：测试模型适配桥接层暴露给 agentCore 的实际调用接口是否可用。
 * 能力要求1：需要覆盖从抽象层结果到 agentCore 内部调用入口的最小连通性。
 * 能力要求2：它用于防止 provider 已接入但 agentCore 仍不能稳定调用的假接通。
 * 边界：负责进入 agentCore 前的最后适配，不重新处理上游 endpoint 细节。
 * 对接：需要被 runtime.modelAdapter 拉起，并和 provider/carrier、PromptPack lowering、能力抽象链路接通。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const applicationInterfaceTestDescriptor = {
    capability: "application-interface-test",
    route: "agent_modelAdapter.bridgingLayer",
    purpose: "probe the direct agentCore-facing model adapter interface without reaching provider APIs",
    providerPayloadCreated: false,
    unsafeSideEffects: false,
};
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function cleanList(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 },
        events: ["modelAdapter.applicationInterfaceTest.rejected"],
    };
}
function resolveScopes(requestedScopes, allowedScopes) {
    const requested = cleanList(requestedScopes);
    const allowed = cleanList(allowedScopes);
    if (requested.length === 0) {
        return [];
    }
    const denied = requested.filter((scope) => !allowed.includes(scope));
    if (denied.length > 0) {
        return failure("SCOPE_DENIED", `application interface probe scope ${denied[0]} is outside runtime governance`, "scope");
    }
    return requested;
}
function interfaceGaps(adapter, expectedCapabilityIds, expectedFormatIds) {
    const gaps = [];
    const capabilities = new Map(adapter.capabilities.map((capability) => [capability.capabilityId, capability]));
    const formats = new Map(adapter.formats.map((format) => [format.formatId, format]));
    for (const capabilityId of cleanList(expectedCapabilityIds)) {
        const capability = capabilities.get(capabilityId);
        if (capability === undefined) {
            gaps.push({ kind: "capability", id: capabilityId, reason: "missing" });
        }
        else if (!capability.available) {
            gaps.push({ kind: "capability", id: capabilityId, reason: "unavailable" });
        }
    }
    for (const formatId of cleanList(expectedFormatIds)) {
        const format = formats.get(formatId);
        if (format === undefined) {
            gaps.push({ kind: "format", id: formatId, reason: "missing" });
        }
        else if (!format.available) {
            gaps.push({ kind: "format", id: formatId, reason: "unavailable" });
        }
    }
    return gaps;
}
function defaultSampleInput(probeId) {
    return {
        invocationId: `${probeId}:dry-run`,
        prompt: "agentCore application interface dry-run probe",
        metadata: { probe: true },
    };
}
export async function runApplicationModelInterfaceProbe(request) {
    if (request === undefined || isBlank(request.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "application interface probe requires runtimeId", "input");
    }
    const runtimeId = (request.runtimeId ?? "").trim();
    const probeId = request.probeId?.trim();
    if (!probeId) {
        return failure("MISSING_PROBE_ID", "application interface probe requires probeId", "input");
    }
    if (request.adapter === undefined) {
        return failure("MISSING_ADAPTER", "application interface probe requires an application model adapter", "input");
    }
    if (request.adapter.runtimeId !== runtimeId) {
        return failure("RUNTIME_MISMATCH", "application interface probe adapter belongs to a different runtime", "contract");
    }
    if (request.adapter.readiness !== "ready") {
        return failure("ADAPTER_NOT_READY", "application interface probe requires a ready adapter", "compatibility");
    }
    const acceptedScopes = resolveScopes(request.requestedScopes, request.allowedScopes);
    if ("ok" in acceptedScopes) {
        return acceptedScopes;
    }
    const gaps = interfaceGaps(request.adapter, request.expectedCapabilityIds, request.expectedFormatIds);
    const shouldExecuteDryRun = request.executeDryRun !== false;
    const invocation = shouldExecuteDryRun
        ? await request.adapter.invoke(request.sampleInput ?? defaultSampleInput(probeId))
        : undefined;
    if (invocation !== undefined && !invocation.ok) {
        return failure("DRY_RUN_FAILED", invocation.error.message, invocation.error.boundary);
    }
    const interfaceUsable = gaps.length === 0 && (invocation === undefined || invocation.ok);
    return {
        ok: true,
        report: {
            kind: "agentCore.modelAdapter.applicationInterfaceProbe",
            runtimeId,
            probeId,
            adapterId: request.adapter.adapterId,
            interfaceUsable,
            gaps,
            acceptedScopes,
            dryRun: {
                executed: shouldExecuteDryRun,
                ok: invocation?.ok ?? false,
                providerPayloadCreated: false,
                unsafeSideEffects: false,
            },
            providerPayloadCreated: false,
            unsafeSideEffects: false,
        },
        invocation,
        events: [
            interfaceUsable
                ? "modelAdapter.applicationInterfaceTest.interfaceUsable"
                : "modelAdapter.applicationInterfaceTest.gapDetected",
        ],
    };
}
