import type { ScopedInstruction } from "./instructions.ts"; export type ReportLanguage = "en" | "zh-CN"; function countMatches(value: string, pattern: RegExp): number { return value.match(pattern)?.length ?? 0; } export function detectReportLanguage( instructions: ScopedInstruction[], ): ReportLanguage { const root = instructions.find( (instruction) => instruction.path === "AGENTS.md", ); const content = root?.content ?? instructions.map((item) => item.content).join("\n"); const han = countMatches(content, /\p{Script=Han}/gu); const latin = countMatches(content, /\p{Script=Latin}/gu); const kana = countMatches( content, /[\p{Script=Hiragana}\p{Script=Katakana}]/gu, ); const hangul = countMatches(content, /\p{Script=Hangul}/gu); if (kana > 0 || hangul > 0) return "en"; return han >= 8 && han / Math.max(han + latin, 1) >= 0.2 ? "zh-CN" : "en"; }