const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
export function validateResponsePayload(payload, _config) {
    const errors = [];
    if (!payload.id || typeof payload.id !== 'string') {
        errors.push('响应缺少有效的id字段');
    }
    if (!payload.created || typeof payload.created !== 'number') {
        errors.push('响应缺少有效的created字段');
    }
    if (!payload.model || typeof payload.model !== 'string') {
        errors.push('响应缺少有效的model字段');
    }
    if (!Array.isArray(payload.choices) || payload.choices.length === 0) {
        errors.push('choices数组不能为空');
    }
    else {
        payload.choices.forEach((choice, idx) => {
            if (!isRecord(choice)) {
                errors.push(`choices[${idx}]必须是对象`);
                return;
            }
            if (!choice.message || typeof choice.message !== 'object') {
                errors.push(`choices[${idx}].message字段必须是对象`);
            }
            else if (Array.isArray(choice.message.tool_calls)) {
                choice.message.tool_calls.forEach((toolCall, tIdx) => {
                    if (!isRecord(toolCall)) {
                        errors.push(`choices[${idx}].message.tool_calls[${tIdx}]必须是对象`);
                        return;
                    }
                    if (!toolCall.function || typeof toolCall.function !== 'object') {
                        errors.push(`choices[${idx}].message.tool_calls[${tIdx}].function字段必须是对象`);
                        return;
                    }
                    const fn = toolCall.function;
                    if (typeof fn.name !== 'string') {
                        errors.push(`choices[${idx}].message.tool_calls[${tIdx}].function.name字段必须是字符串`);
                    }
                    if (typeof fn.arguments !== 'string') {
                        errors.push(`choices[${idx}].message.tool_calls[${tIdx}].function.arguments字段必须是字符串`);
                    }
                    else {
                        try {
                            JSON.parse(fn.arguments);
                        }
                        catch {
                            errors.push(`choices[${idx}].message.tool_calls[${tIdx}].function.arguments必须是有效JSON`);
                        }
                    }
                });
            }
        });
    }
    if (payload.usage && typeof payload.usage === 'object') {
        const usage = payload.usage;
        const promptTokens = usage.prompt_tokens;
        const completionTokens = usage.completion_tokens;
        const totalTokens = usage.total_tokens;
        if (!isNonNegativeNumber(promptTokens) ||
            !isNonNegativeNumber(completionTokens) ||
            !isNonNegativeNumber(totalTokens)) {
            errors.push('usage字段的token必须是非负数');
        }
        else if (promptTokens + completionTokens !== totalTokens) {
            errors.push('usage.total_tokens 应等于 prompt_tokens 与 completion_tokens 之和');
        }
    }
    if (errors.length) {
        const error = new Error(`GLM响应校验失败:\n${errors.join('\n')}`);
        error.code = 'compat_response_validation_failed';
        throw error;
    }
}
function isNonNegativeNumber(value) {
    return typeof value === 'number' && Number.isFinite(value) && value >= 0;
}
