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(windows): when your own AI service can't be reached, subtitles no longer wait a few seconds on every joined sentence before Google translates it

wquguruf61c0f171378
diff --git a/windows/src/Cicada.Core/Translator.cs b/windows/src/Cicada.Core/Translator.cs
index dc196b4..2cdb23f 100644
--- a/windows/src/Cicada.Core/Translator.cs
+++ b/windows/src/Cicada.Core/Translator.cs
@@ -164,11 +164,26 @@ public sealed class Translator(LingClient ling, HttpMessageHandler? google = nul
public enum Kind { Draft, Sentence }
+ long lastUnreachableTry = DateTime.MinValue.Ticks;
+
+ /// A text service that answers nothing (a home server switched off, its hear model served
+ /// elsewhere) costs every sentence its timeout before Google gets it, so subtitles run seconds
+ /// late. While the check says unreachable, one sentence in 15 s still goes to it, so a server
+ /// that is back is noticed without waiting for a check. (Only the hear address unreachable:
+ /// text is fine, it goes on being tried.)
+ bool LingWorthTrying()
+ {
+ if (ling.Health.Failure is not { Why: AICheck.Reason.Unreachable, Step: not AICheck.StepId.Hear }) return true;
+ var last = Interlocked.Read(ref lastUnreachableTry);
+ var now = DateTime.UtcNow.Ticks;
+ return now - last >= TimeSpan.TicksPerSecond * 15 && Interlocked.CompareExchange(ref lastUnreachableTry, now, last) == last;
+ }
+
/// The AI service (finished sentences only) → Google. Null when both fail.
public async Task<string?> Translate(string text, string source, string target, Kind kind, Priority priority = Priority.Translate)
{
if (source == target) return text;
- if (kind != Kind.Draft && ling.Usable)
+ if (kind != Kind.Draft && ling.Usable && LingWorthTrying())
{
try
{
diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.cs
index 082c1ab..06ae1f7 100644
--- a/windows/tests/Cicada.Tests/Program.cs
+++ b/windows/tests/Cicada.Tests/Program.cs
@@ -449,6 +449,25 @@ static class Tests
Equal("Done.", Translator.CleanTranslation("<think>x</think>Done.", "en"), "leaked reasoning");
}
+ /// While the check says the service is unreachable, a sentence doesn't wait on its timeout
+ /// every time: one in 15 s still tries it, the rest go straight to Google.
+ public static async Task AnUnreachableServiceIsTriedOnceIn15Seconds()
+ {
+ var fake = new Fake(_ => Reply("你好"));
+ var ling = Client(fake);
+ var translator = new Translator(ling, new Fake(_ => new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)));
+ Equal("你好", await translator.Translate("Hello", "en", "zh-CN", Translator.Kind.Sentence), "a healthy service translates");
+ ling.SimulateCheck = AICheck.Reason.Unreachable;
+ await ling.Check();
+ await translator.Translate("Hello", "en", "zh-CN", Translator.Kind.Sentence);
+ await translator.Translate("Hello", "en", "zh-CN", Translator.Kind.Sentence);
+ Equal(2, fake.Calls.Count, "one try while unreachable, the next sentence skips it");
+ ling.SimulateCheck = AICheck.Reason.Deaf;
+ await ling.Check();
+ await translator.Translate("Hello", "en", "zh-CN", Translator.Kind.Sentence);
+ Equal(3, fake.Calls.Count, "tried again once the check says it answers");
+ }
+
public static void JoinsCutSentences()
{
var last = new TranscriptLine { Src = "We finished the migration and", SrcLang = "en" };