Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(android): when the AI service keeps answering a meeting's audio with a server error, the banner says so instead of leaving the subtitles silently empty

wquguru34b0199c33db
diff --git a/android/cli/src/main/kotlin/cicada/cli/ProviderTests.kt b/android/cli/src/main/kotlin/cicada/cli/ProviderTests.kt
index 179def3..2d350ad 100644
--- a/android/cli/src/main/kotlin/cicada/cli/ProviderTests.kt
+++ b/android/cli/src/main/kotlin/cicada/cli/ProviderTests.kt
@@ -435,6 +435,9 @@ object ProviderTests {
check(ListenEngine.problem(LingException(LingException.Kind.Network), "ZenMux").second!!.contains("ZenMux"), "named")
equal("network" to "连不上 Ling,检查网络", ListenEngine.problem(LingException(LingException.Kind.Timeout), "Ling"), "Ling's own words")
equal(null, ListenEngine.problem(LingException(LingException.Kind.Http, 400), "Ling").first, "Ling's 400 is not the model")
+ // A gateway's 502 in front of the server: the service erring, not the network or the key.
+ equal("busy" to "ZenMux 暂时出错(HTTP 502),稍后再试。", ListenEngine.problem(LingException(LingException.Kind.Http, 502), "ZenMux"), "a 5xx is the service's trouble")
+ equal("busy" to "Ling 暂时出错(HTTP 503),稍后再试。", ListenEngine.problem(LingException(LingException.Kind.Http, 503), "Ling"), "Ling's too")
}
fun finalNotesRetry() {
diff --git a/android/cli/src/main/kotlin/cicada/cli/Tests.kt b/android/cli/src/main/kotlin/cicada/cli/Tests.kt
index a51be40..efe087b 100644
--- a/android/cli/src/main/kotlin/cicada/cli/Tests.kt
+++ b/android/cli/src/main/kotlin/cicada/cli/Tests.kt
@@ -80,6 +80,7 @@ object Tests {
"lingMarksABadKey" to ::lingMarksABadKey,
"lingGateSpacesStartsAndServesPriorityFirst" to ::lingGate,
"listenEngineEndToEndWithAFakeMing" to ::listenEndToEnd,
+ "listenSaysServerErrorsInARowAndClearsThem" to ::listenServerErrors,
) + ProviderTests.tests + AICheckTests.tests
var passed = 0; var failed = 0
for ((name, test) in tests) {
@@ -885,6 +886,41 @@ object Tests {
} finally { LingClient.spacing = old }
}
+ fun listenServerErrors() {
+ // A gateway answering 502 to every stretch: no subtitles, so from the third in a row the banner
+ // says the service is erring (not after one or two); the next stretch heard clears it.
+ val calls = java.util.concurrent.atomic.AtomicInteger()
+ val fake = Fake { body ->
+ if (!body.contains("Ming-3.0-audio-flash")) reply("{}")
+ else if (calls.incrementAndGet() <= 3) Transport.Response(502, "Bad Gateway")
+ else reply("LANG: en\nORIGINAL: Back again.\nTRANSLATION: 回来了。")
+ }
+ val old = LingClient.spacing
+ LingClient.spacing = 0.05
+ try {
+ val ling = client(fake)
+ val store = SessionStore()
+ val engine = ListenEngine(ling, Translator(ling), store, NotesEngine(ling, store), AppSettings(target = "zh-CN"))
+ val seen = java.util.Collections.synchronizedList(mutableListOf<Pair<String?, Int>>())
+ engine.onChange { seen.add(engine.problem to calls.get()) }
+ // Four times real time: each reply is back before the next stretch goes, as in a meeting.
+ val src = PcmAudioSource({ concat(*Array(4) { concat(tone(1.0), silence(3.0)) }) }, speed = 4.0)
+ val ended = CountDownLatch(1)
+ src.onEnded = { ended.countDown() }
+ engine.start(src, "file")
+ ended.await()
+ Thread.sleep(300)
+ engine.stop()
+ equal(4, calls.get(), "four stretches sent")
+ val said = synchronized(seen) { seen.filter { it.first != null } }
+ check(said.isNotEmpty(), "three 502s in a row are said")
+ check(said.all { it.second >= 3 }, "not after one or two")
+ equal(setOf("Ling 暂时出错(HTTP 502),稍后再试。"), said.map { it.first }.toSet(), "in the service's words")
+ equal(null, engine.problem, "the next stretch heard clears it")
+ equal("Back again.", store.get(engine.sessionId!!)!!.lines.joinToString("|") { it.src }, "its line is there")
+ } finally { LingClient.spacing = old }
+ }
+
fun speaksOnlyTranslations() {
fun me(src: String, dst: String?, from: String, to: String) =
ListenEngine.speakable(TranscriptLine(speaker = "me", src = src, dst = dst, srcLang = from, dstLang = to))
diff --git a/android/core/src/main/kotlin/cicada/ListenEngine.kt b/android/core/src/main/kotlin/cicada/ListenEngine.kt
index 7d39542..071b6f0 100644
--- a/android/core/src/main/kotlin/cicada/ListenEngine.kt
+++ b/android/core/src/main/kotlin/cicada/ListenEngine.kt
@@ -47,6 +47,8 @@ class ListenEngine(
@Volatile var problem: String? = null; private set
/// What kind it is ("key", "busy", "network"), for the screens to pick an icon by.
@Volatile var problemKind: String? = null; private set
+ /// Stretches in a row the service answered with a 5xx (under `gate`).
+ private var serverErrors = 0
@Volatile var level = 0.0; private set
/// Seconds of audio heard this meeting (the capture is alive while this grows).
@Volatile var heardSeconds = 0.0; private set
@@ -144,7 +146,7 @@ class ListenEngine(
synchronized(gate) {
done.clear(); segments.clear(); inFlight.clear()
nextToDeliver = 1; lineCount = 0; last = null; lastEnd = 0.0
- hint = null; candidate = null; candidateVotes = 0; problem = null; problemKind = null
+ hint = null; candidate = null; candidateVotes = 0; problem = null; problemKind = null; serverErrors = 0
}
holding = false; saying = false; muted = false; sayProblem = null; saidCount = 0
heardSeconds = 0.0; peak = 0.0
@@ -284,10 +286,19 @@ class ListenEngine(
heard = translator.hear(clip, asked, lang, seg.seconds)?.let { h ->
if (settings.targetFor(h.lang) == asked) h else h.copy(translation = null)
}
+ synchronized(gate) { serverErrors = 0 }
problem = null; problemKind = null
} catch (e: LingException) {
- val (kind, text) = problem(e, ling.hearName)
- problemKind = kind; problem = text
+ // A server error (a gateway's 502) is said from the third in a row, as on the Mac: one
+ // isn't worth a banner, but a meeting with no subtitles and no reason is worse.
+ val shown = synchronized(gate) {
+ serverErrors = if (serverError(e)) serverErrors + 1 else 0
+ !serverError(e) || serverErrors >= SERVER_ERRORS_SHOWN
+ }
+ if (shown) {
+ val (kind, text) = problem(e, ling.hearName)
+ problemKind = kind; problem = text
+ }
Log.d("listen: segment ${seg.index} failed ${e.message}")
} catch (e: Exception) {
Log.d("listen: segment ${seg.index} failed ${e.javaClass.simpleName}")
@@ -364,12 +375,19 @@ class ListenEngine(
}
companion object {
+ /// Server errors in a row before listening says so (ListenEngine.swift's cloudFailures).
+ const val SERVER_ERRORS_SHOWN = 3
+
+ fun serverError(e: LingException) = e.kind == LingException.Kind.Http && e.status >= 500
+
/// (kind, what to say) for a stretch that couldn't be heard. Ling keeps its own words; another
/// service is named. A hear model that refuses audio (400 / 404) is a key-like problem: 设置 fixes it.
fun problem(e: LingException, service: String): Pair<String?, String?> {
val ling = service == "Ling"
if (e.kind == LingException.Kind.Http && (e.status == 400 || e.status == 404) && !ling)
return "key" to Strings.L("这个模型听不了声音,换一个支持音频输入的")
+ // It answered, with an error: like busy, it passes by itself (AICheck's serverError words).
+ if (serverError(e)) return "busy" to Strings.LF("%@ 暂时出错(HTTP %lld),稍后再试。", Strings.L(service), e.status)
val kind = when (e.kind) {
LingException.Kind.NoKey, LingException.Kind.InvalidKey -> "key"
LingException.Kind.RateLimited -> "busy"