知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.
Swift androidlinuxlive-captionsmacosmeeting-notesmenubar-appsimultaneous-interpretationspeech-recognitionsubtitlesswiftswiftuitranslationwindows
fix(windows): when the AI service keeps answering a meeting's audio with a server error, the notes window says so instead of leaving the subtitles silently empty or blaming the network
wqugurudbe59edc5eda
diff --git a/windows/src/Cicada.Core/ListenEngine.cs b/windows/src/Cicada.Core/ListenEngine.csindex bd68a0e..f6dae7c 100644--- a/windows/src/Cicada.Core/ListenEngine.cs+++ b/windows/src/Cicada.Core/ListenEngine.cs@@ -48,6 +48,11 @@ public sealed class ListenEnginepublic LingException.Kind? Problem { get; private set; }/// The HTTP status behind Problem == Http.public int ProblemStatus { get; private set; }+ /// Stretches in a row the service answered with a 5xx; said from the third (ListenEngine.swift's+ /// cloudFailures): one isn't worth a bar, but a meeting with no subtitles and no reason is worse.+ int serverErrors;+ public const int ServerErrorsShown = 3;+ public static bool IsServerError(LingException.Kind kind, int status) => kind == LingException.Kind.Http && status >= 500;/// A stretch that couldn't be heard, in words: (title, what to do, whether 设置 fixes it). Ling/// keeps its own words; another service is named. A hear model that refuses audio (400, 404,@@ -60,7 +65,9 @@ public sealed class ListenEnginereturn (Strings.LF("%@ 听不了声音。", hear ?? service), Strings.L("在 设置 › AI 服务 里换一个"), true);return kind switch{- LingException.Kind.RateLimited => ling ? (Strings.L("Ling 太忙"), Strings.L("稍后会自动重试。"), false) : (null, Strings.LF("%@ 太忙,稍后再试", service), false),+ // It answered, with an error (a gateway's 502): like busy, it passes by itself; not the network.+ LingException.Kind.Http when status >= 500 => (null, Strings.LF("%@ 暂时出错(HTTP %lld),稍后再试。", service, status), false),+ LingException.Kind.RateLimited => ling ?(Strings.L("Ling 太忙"), Strings.L("稍后会自动重试。"), false) : (null, Strings.LF("%@ 太忙,稍后再试", service), false),LingException.Kind.InvalidKey => (ling ? Strings.L("Ling key 无效") : Strings.LF("%@ 的 key 无效", service), Strings.L("去设置里换一个 key。"), true),LingException.Kind.NoKey => (ling ? Strings.L("Ling key 无效") : Strings.LF("需要 %@ 的 key 才能听", service), Strings.L("去设置里换一个 key。"), true),_ => (ling ? Strings.L("连不上 Ling") : Strings.LF("连不上 %@", service), Strings.L("这段时间的话没能识别。网络恢复后自动继续。"), false),@@ -148,7 +155,7 @@ public sealed class ListenEngine{done.Clear(); segments.Clear(); inFlight.Clear(); prints.Clear(); clusterer = new(); emitted.Clear();nextToDeliver = 1; lineCount = RoomSide ? RoomIDs : 0; last = null; lastEnd = 0;- hint = null; candidate = null; candidateVotes = 0; Problem = null;+ hint = null; candidate = null; candidateVotes = 0; Problem = null; serverErrors = 0;}offset = at;segmenter = new Segmenter(room ? new VoiceGate() : null);@@ -267,12 +274,18 @@ public sealed class ListenEnginetry{heard = await translator.Hear(wav, Target, lang);+ Interlocked.Exchange(ref serverErrors, 0);Problem = null;}catch (LingException e){- Problem = e.Failure is LingException.Kind.Hijacked or LingException.Kind.Parse ? null : e.Failure;- ProblemStatus = e.Status;+ var server = IsServerError(e.Failure, e.Status);+ if (!server) Interlocked.Exchange(ref serverErrors, 0);+ if (!server || Interlocked.Increment(ref serverErrors) >= ServerErrorsShown)+ {+ Problem = e.Failure is LingException.Kind.Hijacked or LingException.Kind.Parse ? null : e.Failure;+ ProblemStatus = e.Status;+ }Log.Debug($"listen: segment {seg.Index} failed {e.Message}");}Log.Debug($"listen: segment {seg.Index} {seg.Seconds:F1}s -> {(heard is null ? "nothing" : heard.Lang)} in {(DateTime.UtcNow - started).TotalMilliseconds:F0} ms");diff --git a/windows/src/Cicada/NotesWindow.cs b/windows/src/Cicada/NotesWindow.csindex 4dad238..5aa0981 100644--- a/windows/src/Cicada/NotesWindow.cs+++ b/windows/src/Cicada/NotesWindow.cs@@ -275,7 +275,8 @@ sealed class NotesWindow : Window// Can't reach it: what this computer's proxy looks like says what to do.var offline = problem is LingException.Kind.Network or LingException.Kind.Timeout;if (offline && ling.Health.ProxyLine(ling.ServiceName, ling.ServiceHost) is { } proxy) body += "\n" + proxy;- if (problem == LingException.Kind.RateLimited) Add(Ui.InfoBar(Ui.Severity.Warning, title, body));+ // Busy or erring (a 5xx): it passes by itself, nothing to press.+ if (problem == LingException.Kind.RateLimited || ListenEngine.IsServerError(problem, app.Listen.ProblemStatus)) Add(Ui.InfoBar(Ui.Severity.Warning, title, body));else if (settings || offline) Add(Ui.InfoBar(Ui.Severity.Error, title, body, Ui.Button(L("设置"), app.ShowAISettings, height: 32)));else Add(Ui.InfoBar(Ui.Severity.Error, title, body, Ui.Button(L("重试"), () => { app.Listen.ClearProblem(); _ = app.Ling.Check(); }, height: 32)));}diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.csindex fe525ac..73165b8 100644--- a/windows/tests/Cicada.Tests/Program.cs+++ b/windows/tests/Cicada.Tests/Program.cs@@ -2180,6 +2180,50 @@ static class TestsCheck(ListenEngine.Words(LingException.Kind.Network, 0, "ZenMux").Title!.Contains("ZenMux"), "named");Equal("连不上 Ling", ListenEngine.Words(LingException.Kind.Timeout, 0, "Ling").Title, "Ling keeps its words");Check(!ListenEngine.Words(LingException.Kind.Http, 400, "Ling").Settings, "Ling's 400 is the network bar");+ // A gateway's 502 in front of the server: the service erring, not the network, nothing in settings.+ Equal((null, "ZenMux 暂时出错(HTTP 502),稍后再试。", false), ListenEngine.Words(LingException.Kind.Http, 502, "ZenMux"), "a 5xx is the service's trouble");+ Equal("Ling 暂时出错(HTTP 503),稍后再试。", ListenEngine.Words(LingException.Kind.Http, 503, "Ling").Body, "Ling's too");+ }++ public static async Task ServerErrorsInARowAreSaidAndClearOnTheNextLine()+ {+ // Every stretch answered 502 by a gateway: no subtitles, so from the third in a row the bar+ // says the service is erring; the next stretch heard clears it. Two blips say nothing.+ var calls = 0;+ var seen = new List<(LingException.Kind? kind, int status, int calls)>();+ var fake = new Fake(body =>+ {+ if (!body.Contains("Ming-3.0-audio-flash")) return Reply("{}");+ return Interlocked.Increment(ref calls) <= 3 ? new HttpResponseMessage(HttpStatusCode.BadGateway)+ : Reply("LANG: en\nORIGINAL: Back again.\nTRANSLATION: 回来了。");+ });+ var old = LingClient.Spacing; LingClient.Spacing = 0.05;+ try+ {+ var ling = Client(fake);+ var store = new SessionStore();+ var engine = new ListenEngine(ling, new Translator(ling), store, new NotesEngine(ling, store), new AppSettings { Target = "zh-CN" });+ engine.Changed += () => { lock (seen) seen.Add((engine.Problem, engine.ProblemStatus, Volatile.Read(ref calls))); };+ var path = Path.Combine(Path.GetTempPath(), $"cicada-502-{Environment.ProcessId}.wav");+ short[] stretches = [.. Tone(1), .. Silence(3), .. Tone(1), .. Silence(3), .. Tone(1), .. Silence(3), .. Tone(1), .. Silence(3)];+ File.WriteAllBytes(path, Wav.Bytes(stretches));+ // Four times real time: each reply is back before the next stretch goes, as in a meeting.+ var src = new FileAudioSource(path, speed: 4);+ var ended = new TaskCompletionSource();+ src.Ended += _ => ended.TrySetResult();+ engine.Start(src);+ await ended.Task;+ await Task.Delay(300);+ await engine.Stop();+ Equal(4, calls, "four stretches sent");+ List<(LingException.Kind? kind, int status, int calls)> shown;+ lock (seen) shown = seen.Where(p => p.kind is not null).ToList();+ Check(shown.Count > 0 && shown.All(p => p.kind == LingException.Kind.Http && p.status == 502), "three 502s in a row are said");+ Check(shown.All(p => p.calls >= 3), "not after one or two");+ Equal(null, engine.Problem, "the next stretch heard clears it");+ Equal("Back again.", store.Get(engine.SessionId!)!.Lines.Single().Src, "and its line is there");+ }+ finally { LingClient.Spacing = old; }}public static void FinalNotesKeepRetryingForAboutAMinute()