The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentSpeechModel, SpeechBenchmarkTier } from "@t3tools/contracts"; |
| 2 | import { recommendSpeechModels } from "@t3tools/shared/speech"; |
| 3 | |
| 4 | export type SpeechModelSort = "recommended" | "size" | "accuracy" | "speed"; |
| 5 | |
| 6 | const modelSortOrder = (model: EnvironmentSpeechModel) => { |
| 7 | if (model.active) return 0; |
| 8 | if (model.state === "installed") return 1; |
| 9 | if (model.state === "downloading" || model.state === "verifying") return 2; |
| 10 | return 3; |
| 11 | }; |
| 12 | |
| 13 | export function filterSpeechModels( |
| 14 | models: readonly EnvironmentSpeechModel[], |
| 15 | options: { |
| 16 | readonly search: string; |
| 17 | readonly languages: readonly string[]; |
| 18 | readonly downloadedOnly: boolean; |
| 19 | readonly sort: SpeechModelSort; |
| 20 | readonly accuracyLanguage: string; |
| 21 | readonly benchmarkTier: SpeechBenchmarkTier | undefined; |
| 22 | }, |
| 23 | recommendations = recommendSpeechModels(models, options.languages, options.benchmarkTier), |
| 24 | ) { |
| 25 | const query = options.search.trim().toLocaleLowerCase(); |
| 26 | const ranks = new Map([...recommendations.keys()].map((id, index) => [id, index])); |
| 27 | const score = (model: EnvironmentSpeechModel) => { |
| 28 | switch (options.sort) { |
| 29 | case "size": |
| 30 | return model.size; |
| 31 | case "accuracy": |
| 32 | return model.benchmark?.accuracy[options.accuracyLanguage]?.errorPercent ?? Infinity; |
| 33 | case "speed": |
| 34 | return options.benchmarkTier === undefined |
| 35 | ? Infinity |
| 36 | : (model.benchmark?.secondsPer30s[options.benchmarkTier] ?? Infinity); |
| 37 | case "recommended": |
| 38 | return modelSortOrder(model) * 4 + (ranks.get(model.id) ?? 3); |
| 39 | } |
| 40 | }; |
| 41 | return models |
| 42 | .filter( |
| 43 | (model) => |
| 44 | options.languages.every((language) => model.languages.includes(language)) && |
| 45 | (!options.downloadedOnly || model.state === "installed") && |
| 46 | (!query || (model.name + " " + model.description).toLocaleLowerCase().includes(query)), |
| 47 | ) |
| 48 | .toSorted( |
| 49 | (left, right) => score(left) - score(right) || modelSortOrder(left) - modelSortOrder(right), |
| 50 | ); |
| 51 | } |