Fix speech tools: add -parse-as-library for @main, handle async supportedLocales (macOS 26 may be async)

This commit is contained in:
aeroreyna
2026-07-13 17:08:59 -04:00
parent 6645ef3664
commit af2fd4303b
+20 -9
View File
@@ -181,6 +181,7 @@ async function buildAndRunTranscriber({ audioPath, locale = "en-US" }) {
fputsStderr(`Compiling transcriber for ${audioPath}...\n`);
const compileCmd = await execFileAsync("/usr/bin/swiftc", [
"-O",
"-parse-as-library",
swiftFile,
"-o", binFile,
"-framework", "AVFoundation",
@@ -258,21 +259,31 @@ export async function speechListLocales() {
const tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "speech-locale-"));
const swiftFile = path.join(tmpDir, "ListLocales.swift");
const binFile = path.join(tmpDir, "list_bin");
const swiftSrc = String.raw`
import Speech
const swiftSrc = `import Speech
import Foundation
@main
struct L {
static func main() async {
// Ensure we trigger asset inventory awareness
let isAvail = SpeechTranscriber.isAvailable
let supported: [String]
let installed: [String]
// These may be async in newer SDKs
// Try sync first, fallback to await if needed via Any
if #available(macOS 26.0, *) {
supported = await SpeechTranscriber.supportedLocales.map { $0.identifier }.sorted()
installed = await SpeechTranscriber.installedLocales.map { $0.identifier }.sorted()
} else {
supported = SpeechTranscriber.supportedLocales.map { $0.identifier }.sorted()
installed = SpeechTranscriber.installedLocales.map { $0.identifier }.sorted()
}
let payload: [String: Any] = [
"isAvailable": SpeechTranscriber.isAvailable,
"supported": SpeechTranscriber.supportedLocales.map{$0.identifier}.sorted(),
"installed": SpeechTranscriber.installedLocales.map{$0.identifier}.sorted(),
"isAvailable": isAvail,
"supported": supported,
"installed": installed,
"macOS": ProcessInfo.processInfo.operatingSystemVersionString,
"maxReserved": AssetInventory.maximumReservedLocales,
"reserved": AssetInventory.reservedLocales.map{$0.identifier}
"reserved": AssetInventory.reservedLocales.map { $0.identifier }
]
let d = try! JSONSerialization.data(withJSONObject: payload, options: [.prettyPrinted, .sortedKeys])
FileHandle.standardOutput.write(d)
@@ -281,13 +292,13 @@ struct L {
`;
await writeFile(swiftFile, swiftSrc, "utf8");
try {
await execFileAsync("/usr/bin/swiftc", ["-O", swiftFile, "-o", binFile, "-framework", "Speech"], { timeout: 30_000 });
await execFileAsync("/usr/bin/swiftc", ["-O", "-parse-as-library", swiftFile, "-o", binFile, "-framework", "Speech"], { timeout: 30_000 });
const { stdout } = await execFileAsync(binFile, [], { timeout: 15_000 });
await cleanup(tmpDir);
return JSON.parse(stdout);
} catch (e) {
await cleanup(tmpDir);
throw new Error(`List locales failed: ${e.stderr || e.message}`);
throw new Error(`List locales failed: ${e.stderr || e.message}\n${e.stdout || ""}`);
}
}