知了 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: with OpenRouter's default model, notes no longer fail when the host it picks refuses JSON mode
wqugurud60a59f6f420
diff --git a/Sources/Cicada/Ling.swift b/Sources/Cicada/Ling.swiftindex c096ce2..a7a7783 100644--- a/Sources/Cicada/Ling.swift+++ b/Sources/Cicada/Ling.swift@@ -154,6 +154,15 @@ final class LingClient: ObservableObject {}static let models = AIProvider.ling.textModels.map(\.id)++ /// A chat reply whose text holds a JSON object (a fence or a sentence around it is fine).+ static func answersJSON(_ root: [String: Any]?) -> Bool {+ let message = ((root?["choices"] as? [[String: Any]])?.first?["message"]) as? [String: Any]+ guard let text = message?["content"] as? String,+ let a = text.firstIndex(of: "{"), let b = text.lastIndex(of: "}"), a < b,+ let data = String(text[a...b]).data(using: .utf8) else { return false }+ return (try? JSONSerialization.jsonObject(with: data)) is [String: Any]+ }private static let hijackPrefixes = ["根据现有检索证据", "暂未找到足够可靠的信息", "根据检索"]@Published private(set) var keyState: KeyState@@ -334,7 +343,11 @@ final class LingClient: ObservableObject {["role": "user", "content": "Reply."]]]c.shape(&json, maxTokens: 20, templateKwargs: !noTemplateKwargs)do { _ = try await postChat(json, key: key, timeout: 30) } catch Failure.http(let code) {- r.fail(.text, .noJSON, code: code)+ // JSON mode refused (some hosts of a model do): meetings then ask without it, so+ // what counts is whether the model still answers with a JSON object.+ json["response_format"] = nil+ let plain = try? await postChat(json, key: key, timeout: 30)+ if !Self.answersJSON(plain) { r.fail(.text, .noJSON, code: code) }}if r.failure == nil, c.transcribes {r.hear = await checkHearing(c, key: key)@@ -524,7 +537,16 @@ final class LingClient: ObservableObject {onQueued?(queued)let started = Date()do {- let root = try await postChat(body, key: key, timeout: timeout)+ let root: [String: Any]+ do {+ root = try await postChat(body, key: key, timeout: timeout)+ } catch Failure.http(400) where body["response_format"] != nil {+ // Some hosts of a model refuse JSON mode (on OpenRouter, Novita for Ling 3.1 Flash)+ // while the model answers JSON anyway: the prompt asks for it, the parsers find it.+ debugLog("ling: \(priority) JSON mode refused (400), asking again without it")+ body["response_format"] = nil+ root = try await postChat(body, key: key, timeout: timeout)+ }guard let choice = (root["choices"] as? [[String: Any]])?.first,let message = choice["message"] as? [String: Any],let text = message["content"] as? String else { throw Failure.parse }diff --git a/Tests/CicadaTests/LingTests.swift b/Tests/CicadaTests/LingTests.swiftindex 0967dba..c58784a 100644--- a/Tests/CicadaTests/LingTests.swift+++ b/Tests/CicadaTests/LingTests.swift@@ -82,12 +82,11 @@ final class LingStub: URLProtocol, @unchecked Sendable {@Test func aRefusalForSomethingElseKeepsThinkingOff() async throws {LingStub.reset(200, "")- // No JSON mode on this server: 400 with or without the kwargs.+ // No JSON mode on this server: 400 with or without the kwargs. Asked again without JSON mode.LingStub.respond = { body in body["response_format"] != nil ? (400, "{}") : (200, LingStub.chatReply("好")) }let ling = customClient()- await #expect(throws: LingClient.Failure.http(400)) {- try await ling.chat(content: "x", instruction: "Notes.", priority: .translate, json: true)- }+ #expect(try await ling.chat(content: "x", instruction: "Notes.", priority: .translate, json: true) == "好")+ #expect(LingStub.seen.last?.body["response_format"] == nil && LingStub.seen.last?.body["chat_template_kwargs"] != nil)_ = try await ling.chat(content: "y", instruction: "Translate.", priority: .translate)#expect(LingStub.seen.last?.body["chat_template_kwargs"] != nil)}@@ -236,6 +235,14 @@ final class LingStub: URLProtocol, @unchecked Sendable {#expect(c.outcome == "ok" && c.chars == 4 && c.firstAudio != nil && c.promptTokens == 0)}+ @Test func aJSONObjectInPlainTextCountsAsJSON() {+ func reply(_ t: String) -> [String: Any] { ["choices": [["message": ["content": t]]]] }+ #expect(LingClient.answersJSON(reply("{\"ok\": true}")))+ #expect(LingClient.answersJSON(reply("```json\n{\"ok\": true}\n```")))+ #expect(!LingClient.answersJSON(reply("ok")))+ #expect(!LingClient.answersJSON(nil))+ }+@Test func rejectedKeyIsMarkedInvalid() async {let ling = client()LingStub.reset(401, "{}")diff --git a/site/changelog.json b/site/changelog.jsonindex 362c454..8050c68 100644--- a/site/changelog.json+++ b/site/changelog.json@@ -104,6 +104,14 @@],"title": "用 OpenRouter 的声音同传时,说完一句后说话键不再卡在“正在说给对方听…”,之后按下也能接着说。","text": "OpenRouter 送来的声音有时停在半个采样上,知了一直等它播完,还占满一个 CPU 核。"+ },+ {+ "kind": "fixed",+ "platforms": [+ "mac"+ ],+ "title": "用 OpenRouter 的默认模型时,纪要不再因为服务商不支持 JSON 格式而写不出来,设置里也不再说这个模型不能用。",+ "text": "OpenRouter 有时把 Ling 3.1 Flash 交给不支持 JSON 格式的服务商;现在被拒时会换个问法再问一次。"}]},