/*
 * 文件定位：Agent 模型适配层 / 真实上游调用层 / OpenAI 官方调用面。
 * 核心目的：承接 OpenAI 上游的 v1 audio voice consents 真实调用面。
 * 能力要求1：需要把对应 endpoint 的请求参数、响应形态、错误形态和能力信号整理成可适配对象。
 * 能力要求2：鉴权/API 登录后续会接入，但这里要为“上游能力变得实际可用”预留位置。
 * 能力要求3：不把该 provider 的字段形状提升为 Praxis 统一语义，只作为 actualInvocationLayer 的现实入口。
 * 边界：只处理上游实际调用面，不在这里定义 agentCore 使用 AI 的统一方式。
 * 对接：需要被 runtime.modelAdapter 拉起，并和 provider/carrier、PromptPack lowering、能力抽象链路接通。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const openAIAudioVoiceConsentsDescriptor = {
    provider: "openai",
    layer: "actualInvocationLayer",
    endpoint: "/v1/audio/voice_consents",
    method: "POST",
    modality: "audio",
    operation: "voice-consent-create",
    requestShape: "multipart-form-data",
    providerRawShapePromoted: false,
    unsafeSideEffects: false,
};
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function isPlainRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function cleanList(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter(Boolean))];
}
function cleanTrace(trace) {
    const cleaned = {};
    for (const [rawKey, rawValue] of Object.entries(trace ?? {})) {
        const key = rawKey.trim();
        const value = rawValue?.trim();
        if (key.length > 0 && value !== undefined && value.length > 0) {
            cleaned[key] = value;
        }
    }
    return cleaned;
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: {
            code,
            message,
            boundary,
            safeForRuntimeInspection: true,
            providerRawDetailExposed: false,
        },
        events: ["modelAdapter.openai.audio.voiceConsents.rejected"],
    };
}
function classifyProviderError(error) {
    if (error.timedOut === true || error.code === "timeout") {
        return failure("PROVIDER_TIMEOUT", "OpenAI voice consent request timed out", "provider");
    }
    if (error.authFailed === true || error.status === 401 || error.status === 403) {
        return failure("PROVIDER_AUTH_FAILED", "OpenAI voice consent authentication was rejected", "provider");
    }
    if (error.rateLimited === true || error.status === 429) {
        return failure("PROVIDER_RATE_LIMITED", "OpenAI voice consent creation was rate limited", "provider");
    }
    if (error.endpointAvailable === false || error.status === 404) {
        return failure("PROVIDER_ENDPOINT_UNAVAILABLE", "OpenAI voice consent endpoint is unavailable", "provider");
    }
    if (error.code === "response_drift") {
        return failure("PROVIDER_RESPONSE_DRIFT", "OpenAI voice consent response shape drifted from the expected envelope", "provider");
    }
    return failure("PROVIDER_REJECTED", error.message ?? "OpenAI voice consent provider rejected the request", "provider");
}
export function createOpenAIAudioVoiceConsentInvocation(request = {}) {
    if (isBlank(request.name)) {
        return failure("MISSING_NAME", "OpenAI voice consent invocation requires a consent name", "input");
    }
    if (isBlank(request.language)) {
        return failure("MISSING_LANGUAGE", "OpenAI voice consent invocation requires a BCP 47 language tag", "input");
    }
    if (request.recording === undefined || !isPlainRecord(request.recording)) {
        return failure("MISSING_RECORDING", "OpenAI voice consent invocation requires a consent recording handle", "input");
    }
    if (request.body !== undefined && !isPlainRecord(request.body)) {
        return failure("INVALID_REQUEST_BODY", "OpenAI voice consent body must be a plain provider request record", "input");
    }
    if (request.timeoutMs !== undefined && (!Number.isFinite(request.timeoutMs) || request.timeoutMs <= 0)) {
        return failure("INVALID_TIMEOUT", "OpenAI voice consent timeout must be a positive number", "input");
    }
    if (request.dryRun === false) {
        return failure("REAL_PROVIDER_CALL_NOT_ALLOWED", "first-round OpenAI voice consent invocation only builds dry-run or mock envelopes", "governance");
    }
    if (request.contract?.accepted === false) {
        return failure("CONTRACT_REJECTED", request.contract.reason ?? "OpenAI voice consent invocation was rejected by contract surface", "contract");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "OpenAI voice consent invocation was rejected by runtime governance", "governance");
    }
    const requestedScopes = cleanList(request.requestedScopes);
    const allowedScopes = cleanList(request.allowedScopes);
    const deniedScopes = requestedScopes.filter((scope) => allowedScopes.length > 0 && !allowedScopes.includes(scope));
    if (deniedScopes.length > 0) {
        return failure("SCOPE_DENIED", `OpenAI voice consent scope ${deniedScopes[0]} is outside governance`, "scope");
    }
    if (request.providerError !== undefined) {
        return classifyProviderError(request.providerError);
    }
    const name = request.name?.trim() ?? "";
    const language = request.language?.trim() ?? "";
    const requestId = request.requestId?.trim() || `openai:audio:voice_consents:${name}`;
    const envelope = {
        provider: "openai",
        endpoint: "/v1/audio/voice_consents",
        method: "POST",
        requestId,
        requestShape: "multipart-form-data",
        providerFields: {
            name,
            language,
            recording: request.recording,
        },
        body: request.body ?? {},
        auth: {
            apiKeyRef: request.apiKeyRef?.trim() || undefined,
            organizationId: request.organizationId?.trim() || undefined,
            projectId: request.projectId?.trim() || undefined,
            materialPresent: !isBlank(request.apiKeyRef),
        },
        runtime: {
            timeoutMs: request.timeoutMs,
            requestedScopes,
            dryRun: true,
            unsafeSideEffects: false,
        },
        trace: cleanTrace(request.trace),
    };
    return {
        ok: true,
        request: envelope,
        response: request.providerResponse === undefined
            ? undefined
            : {
                provider: "openai",
                endpoint: "/v1/audio/voice_consents",
                raw: request.providerResponse,
                received: true,
                providerRawShapePromoted: false,
            },
        capability: openAIAudioVoiceConsentsDescriptor,
        events: ["modelAdapter.openai.audio.voiceConsents.enveloped"],
    };
}
