mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(mobile): keep on-device speech text revisions rising across sessions on iOS (#39481)
This commit is contained in:
@@ -16,6 +16,8 @@ final class OnDeviceSpeechController: @unchecked Sendable {
|
||||
private var finishing: OnDeviceSpeechSession?
|
||||
// Bumped whenever the live session changes so a stale session's events and start result are dropped.
|
||||
private var generation = 0
|
||||
// Rises across sessions too, per the contract, so a reader can drop a previous session's late text.
|
||||
private var revision = 0
|
||||
private let emit: @Sendable (String, [String: Any]) -> Void
|
||||
|
||||
init(emit: @escaping @Sendable (String, [String: Any]) -> Void) {
|
||||
@@ -62,10 +64,8 @@ final class OnDeviceSpeechController: @unchecked Sendable {
|
||||
let session = OnDeviceSpeechSession(
|
||||
locale: identifier,
|
||||
inputFormat: format,
|
||||
onText: { [weak self] revision, committed, tentative in
|
||||
self?.emitIfCurrent(
|
||||
mine, ON_SPEECH_TEXT_EVENT_NAME,
|
||||
["revision": revision, "committed": committed, "tentative": tentative])
|
||||
onText: { [weak self] committed, tentative in
|
||||
self?.emitTextIfCurrent(mine, committed: committed, tentative: tentative)
|
||||
},
|
||||
onFailure: { [weak self] error in
|
||||
self?.fail(mine, error)
|
||||
@@ -131,11 +131,11 @@ final class OnDeviceSpeechController: @unchecked Sendable {
|
||||
}
|
||||
|
||||
// Emits under the lock (sendEvent only schedules) so a cancel or start can't slip in after the check.
|
||||
private func emitIfCurrent(_ generation: Int, _ name: String, _ body: [String: Any]) {
|
||||
private func emitTextIfCurrent(_ generation: Int, committed: String, tentative: String) {
|
||||
lock.withLock {
|
||||
if self.generation == generation {
|
||||
emit(name, body)
|
||||
}
|
||||
guard self.generation == generation else { return }
|
||||
revision += 1
|
||||
emit(ON_SPEECH_TEXT_EVENT_NAME, ["revision": revision, "committed": committed, "tentative": tentative])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ enum OnDeviceTranscriber {
|
||||
|
||||
// Collects results until the analyzer finishes; resolves with every finalized word.
|
||||
func collect(
|
||||
onText: @escaping @Sendable (_ revision: Int, _ committed: String, _ tentative: String) -> Void,
|
||||
onText: @escaping @Sendable (_ committed: String, _ tentative: String) -> Void,
|
||||
onFailure: @escaping @Sendable (Error) -> Void
|
||||
) -> Task<String, Error> {
|
||||
switch self {
|
||||
@@ -77,21 +77,19 @@ enum OnDeviceTranscriber {
|
||||
private static func collect<Results: AsyncSequence & Sendable>(
|
||||
_ results: Results,
|
||||
text: @escaping @Sendable (Results.Element) -> AttributedString,
|
||||
onText: @escaping @Sendable (Int, String, String) -> Void,
|
||||
onText: @escaping @Sendable (String, String) -> Void,
|
||||
onFailure: @escaping @Sendable (Error) -> Void
|
||||
) -> Task<String, Error> where Results.Element: SpeechModuleResult {
|
||||
Task {
|
||||
var committed = ""
|
||||
var revision = 0
|
||||
do {
|
||||
for try await result in results {
|
||||
let words = String(text(result).characters)
|
||||
revision += 1
|
||||
if result.isFinal {
|
||||
committed += words
|
||||
onText(revision, committed, "")
|
||||
onText(committed, "")
|
||||
} else {
|
||||
onText(revision, committed, words)
|
||||
onText(committed, words)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
@@ -171,7 +169,7 @@ final class OnDeviceSpeechSession: @unchecked Sendable {
|
||||
init(
|
||||
locale identifier: String,
|
||||
inputFormat: AVAudioFormat,
|
||||
onText: @escaping @Sendable (_ revision: Int, _ committed: String, _ tentative: String) -> Void,
|
||||
onText: @escaping @Sendable (_ committed: String, _ tentative: String) -> Void,
|
||||
onFailure: @escaping @Sendable (Error) -> Void
|
||||
) {
|
||||
self.inputFormat = inputFormat
|
||||
@@ -225,7 +223,7 @@ final class OnDeviceSpeechSession: @unchecked Sendable {
|
||||
_ stream: AsyncStream<AVAudioPCMBuffer>,
|
||||
locale: Locale,
|
||||
inputFormat: AVAudioFormat,
|
||||
onText: @escaping @Sendable (Int, String, String) -> Void,
|
||||
onText: @escaping @Sendable (String, String) -> Void,
|
||||
onFailure: @escaping @Sendable (Error) -> Void
|
||||
) async throws -> String {
|
||||
try Task.checkCancellation()
|
||||
|
||||
Reference in New Issue
Block a user