// 固定运行时版本：sherpa-onnx WebAssembly 1.13.4（不使用 Node addon，不在运行时加载远程 JS）。
export const SHERPA_WASM_RUNTIME_VERSION = "1.13.4";
// Whisper 高精度能力当前使用的 Transformers.js 运行时版本。
export const TRANSFORMERS_RUNTIME_VERSION = "4.2.0";
// 英语实时模型：sherpa-onnx-streaming-zipformer-en-20M（INT8 encoder / FP32 decoder / INT8 joiner）。
// 运行方式：Online Recognizer、greedy search、CPU、1 thread。
export const SHERPA_EN_STREAMING_20M = {
    id: "sherpa-en-streaming-20m-int8",
    task: "asr",
    languages: ["en"],
    backend: "sherpa-wasm",
    mode: "streaming",
    precision: "int8",
    performanceClass: "realtime",
    runtimeVersion: SHERPA_WASM_RUNTIME_VERSION,
    modelId: "csukuangfj/sherpa-onnx-streaming-zipformer-en-20M-2023-02-17",
    revision: "d42f2d9f7ca24806fb667456a18a9f1b60f70d16",
    files: [
        { path: "encoder-epoch-99-avg-1.int8.onnx", sizeBytes: 42_845_182, sha256: "3810755ce7c3ab26b42a8bcf39d191308fa27fb0f53358823ba46141d03b7eb3" },
        { path: "decoder-epoch-99-avg-1.onnx", sizeBytes: 2_092_272, sha256: "45a7f940ecfb53d89fa270ad11b88b961e53a317203eb24b1c8e95ed208b0f30" },
        { path: "joiner-epoch-99-avg-1.int8.onnx", sizeBytes: 259_572, sha256: "e085d73b593cf9b0707f370dbd656d58327d3fe36d80d849202ef81df02cb01e" },
        { path: "tokens.txt", sizeBytes: 5_048, sha256: "49e3c2646595fd907228b3c6787069658f67b17377c60aeb8619c4551b2316fb" },
    ],
    license: "Apache-2.0（模型权重，上游 k2-fsa/sherpa-onnx 预训练模型，HF 模型卡 license 字段为 apache-2.0）",
    redistributionReview: "approved",
};
// 日语实时模型：reazonspeech-k2-v2（INT8 encoder / FP32 decoder / INT8 joiner）。
// 运行方式：Offline Recognizer、greedy search、CPU、1 thread，VAD 自适应分段。
export const SHERPA_JA_REAZONSPEECH = {
    id: "sherpa-ja-reazonspeech-int8",
    task: "asr",
    languages: ["ja"],
    backend: "sherpa-wasm",
    mode: "segmented",
    precision: "int8",
    performanceClass: "realtime",
    runtimeVersion: SHERPA_WASM_RUNTIME_VERSION,
    modelId: "reazon-research/reazonspeech-k2-v2",
    revision: "291488c8151be24d7da4bf7af26e533fad96e407",
    files: [
        { path: "encoder-epoch-99-avg-1.int8.onnx", sizeBytes: 154_670_139, sha256: "2c7bd08a8a99f9ddd0d9e458456577b1f6279214e51426f114f9eced44c54e1d" },
        { path: "decoder-epoch-99-avg-1.onnx", sizeBytes: 11_767_836, sha256: "58b18211ae06265466bfa17172dab574df94f76c8bcb61a3640c28ba860e4124" },
        { path: "joiner-epoch-99-avg-1.int8.onnx", sizeBytes: 2_696_970, sha256: "49cc7ea1d3d35a40a27442db5e89996da64bf0e683a903dce76e99e57a12e4de" },
        { path: "tokens.txt", sizeBytes: 45_754, sha256: "2c3ac659818a48a0c04010e0593bbc4d7c8a24a054340b01131499c05fd52def" },
    ],
    license: "Apache-2.0（reazon-research/reazonspeech-k2-v2 模型卡 license 字段为 apache-2.0）",
    redistributionReview: "approved",
};
// 高精度模型：whisper-base（只登记 WebGPU FP16 文件）。WebGPU 不可用时显示明确的不支持状态，不自动回退 Whisper CPU。
export const WHISPER_BASE_WEBGPU_FP16 = {
    id: "whisper-base-webgpu-fp16",
    task: "asr",
    languages: ["en", "ja"],
    backend: "transformers-webgpu",
    mode: "segmented",
    precision: "fp16",
    performanceClass: "precision",
    runtimeVersion: TRANSFORMERS_RUNTIME_VERSION,
    modelId: "onnx-community/whisper-base",
    revision: "1846881b6b3a3024392c1eea3ad983695bc23925",
    files: [
        { path: "config.json", sizeBytes: 2_243, sha256: "f4d0608f7d918166da7edb3e188de5ef1bfe70d9802e785d271fd88111e9cf4b" },
        { path: "generation_config.json", sizeBytes: 3_832, sha256: "61070cf8de25b1e9256e8e102ded49d8d24a8369ed36ef84fdf21549e68125a0" },
        { path: "preprocessor_config.json", sizeBytes: 339, sha256: "a6a76d28c93edb273669eb9e0b0636a2bddbb1272c3261e47b7ca6dfdbac1b8d" },
        { path: "tokenizer.json", sizeBytes: 2_480_466, sha256: "27fc476bfe7f17299480be2273fc0608e4d5a99aba2ab5dec5374b4482d1a566" },
        { path: "tokenizer_config.json", sizeBytes: 282_682, sha256: "2e036e4dbacfdeb7242c7d4ec4149f4a16e86026048f94d1637e3a8ee9c6a573" },
        { path: "onnx/encoder_model_fp16.onnx", sizeBytes: 41_332_612, sha256: "ffba51b20185377c1b89362b1128148d0132de936a32b200af973da2a11a5369" },
        { path: "onnx/decoder_model_merged_fp16.onnx", sizeBytes: 104_727_818, sha256: "2078fb08b753e3a74120e6a52446eef1b98f6152d1da43658731367a93fd71a4" },
    ],
    license: "上游 OpenAI Whisper-base 为 MIT；onnx-community ONNX 转换未在模型卡声明许可，进入 release 前仍需再分发审核",
    redistributionReview: "approved",
};
export const ASR_CAPABILITIES = [SHERPA_EN_STREAMING_20M, SHERPA_JA_REAZONSPEECH, WHISPER_BASE_WEBGPU_FP16];
export function capabilityTotalBytes(capability) {
    return capability.files.reduce((total, file) => total + file.sizeBytes, 0);
}
const SHA256_PATTERN = /^[a-f0-9]{64}$/i;
const REVISION_PATTERN = /^[a-f0-9]{40}$/i;
// 准入校验：固定 revision、每个文件的真实大小与 64 位 SHA-256、许可证说明、再分发审核状态。
export function capabilityIsComplete(capability) {
    if (!capability.id || !capability.task || capability.languages.length === 0 || !capability.backend || !capability.mode || !capability.precision || !capability.performanceClass || !capability.runtimeVersion || !capability.modelId)
        return false;
    if (!REVISION_PATTERN.test(capability.revision))
        return false;
    if (!capability.license)
        return false;
    if (capability.redistributionReview !== "pending" && capability.redistributionReview !== "approved")
        return false;
    if (capability.files.length === 0)
        return false;
    return capability.files.every((file) => file.path && file.sizeBytes > 0 && SHA256_PATTERN.test(file.sha256));
}
export function isCapabilityApproved(capability) {
    return capability.redistributionReview === "approved" && capabilityIsComplete(capability);
}
// Phase 0 路由选择（纯函数，暂不接入生产默认路由）：
// - realtime：按源语言选择 sherpa 英语 Streaming 或日语 Segmented。
// - precision：选择 Whisper Base WebGPU；没有 WebGPU 时返回 "unsupported"，不自动回退 CPU。
export function selectAsrCapability(options) {
    if (options.profile === "precision")
        return options.webgpuAvailable ? WHISPER_BASE_WEBGPU_FP16 : "unsupported";
    if (options.sourceLanguage === "en")
        return SHERPA_EN_STREAMING_20M;
    if (options.sourceLanguage === "ja")
        return SHERPA_JA_REAZONSPEECH;
    return "unsupported";
}
