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.

feat(ios): use OpenRouter, ZenMux, OpenAI or your own service instead of Ling, as on the Mac

wquguru59652c956007
diff --git a/ios/Resources/Info.plist b/ios/Resources/Info.plist
index 6f91278..7ab0379 100644
--- a/ios/Resources/Info.plist
+++ b/ios/Resources/Info.plist
@@ -60,6 +60,13 @@
<false/>
<key>MinimumOSVersion</key>
<string>26.0</string>
+ <key>NSAppTransportSecurity</key>
+ <dict>
+ <key>NSAllowsLocalNetworking</key>
+ <true/>
+ </dict>
+ <key>NSLocalNetworkUsageDescription</key>
+ <string>Only to reach the AI server you set up on your local network. Cicada doesn't look for or connect to other devices.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Cicada listens to the people in the room to show live translated subtitles and notes.</string>
<key>NSSpeechRecognitionUsageDescription</key>
diff --git a/ios/Sources/Cicada/AIServicePage.swift b/ios/Sources/Cicada/AIServicePage.swift
new file mode 100644
index 0000000..f5f1a57
--- /dev/null
+++ b/ios/Sources/Cicada/AIServicePage.swift
@@ -0,0 +1,206 @@
+import SwiftUI
+
+/// 设置 › AI 服务, as on the Mac and Android: Ling (recommended), OpenRouter, ZenMux, OpenAI or the
+/// user's own OpenAI-compatible service. The settings are the Mac's (`aiProvider`, `aiModels`,
+/// `aiVoiceModels`, `aiCustomBase`, Ling's model in `aiModel`), each service with its own key file.
+/// The custom service here is the Mac's plain one: one address, a text model and optionally a speech
+/// model; recognition stays on the phone, notes use the same server.
+enum AIPrefs {
+ static let provider = "aiProvider"
+ static let models = "aiModels"
+ static let voiceModels = "aiVoiceModels"
+ static let customBase = "aiCustomBase"
+
+ private static var d: UserDefaults { .standard }
+ static var current: AIProvider { AIProvider.named(d.string(forKey: provider) ?? "ling") }
+ static var modelMap: [String: String] { d.dictionary(forKey: models) as? [String: String] ?? [:] }
+ static var voiceMap: [String: String] { d.dictionary(forKey: voiceModels) as? [String: String] ?? [:] }
+
+ /// What `LingClient` calls with, as the Mac's `AppSettings.aiConfig`.
+ static var config: AIConfig {
+ let p = current
+ switch p.id {
+ case .ling:
+ // Ming speaks 面对面 with the same key: without the voice model, every turn fell back to the phone's voice.
+ return AIConfig(provider: p, base: p.base, textModel: d.string(forKey: Pref.model) ?? p.textModels.first?.id ?? "",
+ voiceModel: p.voiceModels.first?.id, voiceBase: p.base)
+ case .custom:
+ let base = AIProvider.customBase(d.string(forKey: customBase) ?? "")
+ let voice = (voiceMap["custom"] ?? "").trimmingCharacters(in: .whitespaces)
+ return AIConfig(provider: p, base: base, textModel: modelMap["custom"] ?? "",
+ voiceModel: voice.isEmpty ? nil : voice, voiceBase: base)
+ default:
+ return AIConfig(provider: p, base: p.base, textModel: modelMap[p.id.rawValue] ?? p.textModels.first?.id ?? "",
+ voiceModel: p.hasVoices ? voiceMap[p.id.rawValue] ?? p.voiceModels.first?.id : nil, voiceBase: p.base)
+ }
+ }
+
+ /// 面对面's voices follow the service: Ming's with Ling, the speech model's with the others.
+ static func applyVoices() {
+ let c = config
+ switch c.provider.id {
+ case .ling: VoiceCatalog.service = .ming
+ default:
+ VoiceCatalog.service = c.hasVoices ? .cloud(label: c.provider.id == .custom ? L("自定义服务") : c.provider.name,
+ voices: c.provider.voices) : .none
+ }
+ }
+
+ static func setModel(_ id: String, for p: AIProvider) {
+ if p.id == .ling { d.set(id, forKey: Pref.model); return }
+ var m = modelMap
+ m[p.id.rawValue] = id
+ d.set(m, forKey: models)
+ }
+
+ static func setVoiceModel(_ id: String, for p: AIProvider) {
+ var m = voiceMap
+ m[p.id.rawValue] = id
+ d.set(m, forKey: voiceModels)
+ }
+}
+
+struct AIServicePage: View {
+ @EnvironmentObject var model: AppModel
+ @EnvironmentObject var ling: LingClient
+ @AppStorage(AIPrefs.provider) private var provider = "ling"
+ @AppStorage(AIPrefs.customBase) private var base = ""
+ @State private var customModel = AIPrefs.modelMap["custom"] ?? ""
+ @State private var customVoice = AIPrefs.voiceMap["custom"] ?? ""
+ @State private var tick = 0
+
+ private var p: AIProvider { AIProvider.named(provider) }
+
+ var body: some View {
+ ScrollView {
+ VStack(alignment: .leading, spacing: 0) {
+ Card {
+ ForEach(Array(AIProvider.all.enumerated()), id: \.element.id) { i, s in
+ if i > 0 { Hairline() }
+ Button { pick(s) } label: { serviceRow(s) }.buttonStyle(RowPress())
+ .accessibilityAddTraits(s.id == p.id ? .isSelected : [])
+ }
+ }
+ .padding(.top, 12)
+
+ if p.id == .custom {
+ SectionLabel(text: L("地址"))
+ Card {
+ TextField(text: $base) { Text(verbatim: "https://…/v1 · 192.168.1.20:8000/v1") }
+ .textInputAutocapitalization(.never).autocorrectionDisabled().keyboardType(.URL)
+ .padding(.horizontal, 16).frame(minHeight: 50)
+ .onSubmit { model.aiChanged() }
+ }
+ if !base.isEmpty, AIProvider.customBase(base) == nil {
+ Text("地址要用 https;这台手机和局域网里的服务可以用 http。")
+ .font(.footnote).foregroundStyle(Ink.accentText).padding(.horizontal, 4).padding(.top, 8)
+ }
+ }
+
+ SectionLabel(text: p.id == .custom ? L("Key(局域网里的服务可以不填)") : L("\(p.name) 的 key"))
+ Card {
+ NavigationLink { KeyPage() } label: {
+ HStack(spacing: 8) {
+ // No key: the status beside it says so; this shows what a key looks like.
+ Text(verbatim: ling.key.map { $0.isEmpty ? L("不用 key") : AIKey.masked($0) } ?? "sk-…")
+ .font(.body.monospaced()).foregroundStyle(ling.key == nil ? Ink.secondary : Ink.ink).lineLimit(1)
+ Spacer()
+ KeyStatus(state: ling.keyState)
+ Chevron()
+ }
+ .padding(.horizontal, 16).frame(minHeight: 50).contentShape(Rectangle())
+ }
+ .buttonStyle(RowPress())
+ }
+
+ SectionLabel(text: L("模型"))
+ Card {
+ if p.id == .custom {
+ field(L("翻译和纪要"), $customModel) { AIPrefs.setModel(customModel.trimmingCharacters(in: .whitespaces), for: p) }
+ Hairline()
+ field(L("声音模型"), $customVoice) { AIPrefs.setVoiceModel(customVoice.trimmingCharacters(in: .whitespaces), for: p) }
+ } else {
+ menu(L("翻译和纪要"), options: p.id == .ling ? LingClient.models : p.textModels.map(\.id), current: AIPrefs.config.textModel) {
+ AIPrefs.setModel($0, for: p)
+ }
+ if p.hasVoices && p.id != .ling, let v = AIPrefs.config.voiceModel {
+ Hairline()
+ menu(L("声音"), options: p.voiceModels.map(\.id), current: v) { AIPrefs.setVoiceModel($0, for: p) }
+ }
+ }
+ }
+ Text(p.clones ? L("填入 Ling key:声音更自然,还能用你自己的声音") : L("只有 Ling 能用你自己的声音"))
+ .font(.footnote).foregroundStyle(Ink.secondary).padding(.horizontal, 4).padding(.top, 10)
+ }
+ .padding(.horizontal, 16).padding(.bottom, 120)
+ .frame(maxWidth: 640).frame(maxWidth: .infinity)
+ .id(tick)
+ }
+ .background(Ink.ground)
+ .navigationTitle(Text("AI 服务"))
+ .navigationBarTitleDisplayMode(.inline)
+ }
+
+ private func serviceRow(_ s: AIProvider) -> some View {
+ HStack(spacing: 8) {
+ Text(verbatim: s.name).foregroundStyle(Ink.ink)
+ if s.id == .ling {
+ Text("推荐").font(.caption2.weight(.semibold)).foregroundStyle(Ink.accentText)
+ .padding(.horizontal, 7).padding(.vertical, 2).background(Ink.accentWash, in: Capsule())
+ }
+ Spacer()
+ if s.id == p.id { Image(systemName: "checkmark").font(.body.weight(.semibold)).foregroundStyle(Ink.accentText) }
+ }
+ .padding(.horizontal, 16).frame(minHeight: 50).contentShape(Rectangle())
+ }
+
+ private func menu(_ title: String, options: [String], current: String, set: @escaping (String) -> Void) -> some View {
+ HStack(spacing: 8) {
+ Text(verbatim: title).foregroundStyle(Ink.ink)
+ Spacer(minLength: 8)
+ Menu {
+ ForEach(options, id: \.self) { o in
+ Button { set(o); model.aiChanged(); tick += 1 } label: {
+ if o == current { Label(o, systemImage: "checkmark") } else { Text(verbatim: o) }
+ }
+ }
+ } label: {
+ HStack(spacing: 4) {
+ Text(verbatim: current).lineLimit(1).truncationMode(.head)
+ Image(systemName: "chevron.up.chevron.down").font(.caption2.weight(.semibold))
+ }
+ .foregroundStyle(Ink.secondary)
+ }
+ }
+ .padding(.horizontal, 16).frame(minHeight: 50)
+ }
+
+ private func field(_ title: String, _ text: Binding<String>, commit: @escaping () -> Void) -> some View {
+ HStack(spacing: 8) {
+ Text(verbatim: title).foregroundStyle(Ink.ink)
+ TextField(text: text) { Text("模型名") }
+ .multilineTextAlignment(.trailing).foregroundStyle(Ink.secondary)
+ .textInputAutocapitalization(.never).autocorrectionDisabled()
+ .onSubmit { commit(); model.aiChanged() }
+ }
+ .padding(.horizontal, 16).frame(minHeight: 50)
+ }
+
+ private func pick(_ s: AIProvider) {
+ guard s.id != p.id else { return }
+ provider = s.id.rawValue
+ model.aiChanged()
+ tick += 1
+ }
+}
+
+extension AppModel {
+ /// The service, its address or a model changed: LingClient checks again and 面对面 takes the
+ /// service's voices.
+ func aiChanged() {
+ AIPrefs.applyVoices()
+ ling.reconfigure()
+ debugLog("ai: service \(AIPrefs.config.provider.id.rawValue) model \(AIPrefs.config.textModel) voice \(AIPrefs.config.voiceModel ?? "-")")
+ objectWillChange.send()
+ }
+}
diff --git a/ios/Sources/Cicada/LiveViews.swift b/ios/Sources/Cicada/LiveViews.swift
index 1a26835..5ec928c 100644
--- a/ios/Sources/Cicada/LiveViews.swift
+++ b/ios/Sources/Cicada/LiveViews.swift
@@ -250,7 +250,9 @@ struct TranslateDownCard: View {
Image(systemName: "exclamationmark.triangle.fill").foregroundStyle(Ink.accent).padding(.top, 2)
VStack(alignment: .leading, spacing: 3) {
Text("连不上翻译,新字幕暂时只有原文").font(.subheadline.weight(.semibold)).foregroundStyle(Ink.ink)
- Text(model.ling.hasKey ? L("网络连不上,连上后会自动继续") : L("填 Ling key 后不用代理也能翻译,还会整理出纪要。"))
+ Text(model.ling.hasKey ? L("网络连不上,连上后会自动继续")
+ : model.ling.provider.id == .ling ? L("填 Ling key 后不用代理也能翻译,还会整理出纪要。")
+ : L("填好 \(model.ling.provider.name) 的 key 后用它翻译,还会整理出纪要。"))
.font(.footnote).foregroundStyle(Ink.secondary).fixedSize(horizontal: false, vertical: true)
}
Spacer(minLength: 0)
diff --git a/ios/Sources/Cicada/VoicesPage.swift b/ios/Sources/Cicada/VoicesPage.swift
index dce1045..b6deed8 100644
--- a/ios/Sources/Cicada/VoicesPage.swift
+++ b/ios/Sources/Cicada/VoicesPage.swift
@@ -10,11 +10,16 @@ enum SpeakVoices {
/// The voice for a language: the one picked, while it can still be used (Ming and the clone need
/// the key, the clone its recording), else the catalog's default. `mine`: read for me (their
/// words in my language), where my own voice would make no sense.
- static func chosenID(for language: String, hasKey: Bool, mine: Bool = false) -> String? {
+ /// `clones`: the service can speak in the user's own voice (Ling only). A voice of another
+ /// service (Ming's picked under Ling, then ZenMux chosen) gives way to the default.
+ static func chosenID(for language: String, hasKey: Bool, mine: Bool = false, clones: Bool = true) -> String? {
if let id = all[VoiceCatalog.key(for: language)] {
- let clone = id == VoiceCatalog.cloneOption.id
if id.hasPrefix("system:") { return id }
- if hasKey, !clone || (!mine && VoiceReference.exists) { return id }
+ if hasKey, id == VoiceCatalog.cloneOption.id {
+ if clones, !mine, VoiceReference.exists { return id }
+ } else if hasKey, VoiceCatalog.remote(for: language).contains(where: { $0.id == id }) {
+ return id
+ }
}
return VoiceCatalog.defaultID(for: language, hasKey: hasKey)
}
@@ -47,8 +52,10 @@ struct VoicesPage: View {
ForEach(languages, id: \.self) { lang in
section(lang)
}
- Text(ling.hasKey ? L("Ling 不可用时自动换成手机的声音") : L("填入 Ling key 后可用 Ming 声音和你自己的声音"))
- .font(.footnote).foregroundStyle(Ink.secondary).padding(.horizontal, 4).padding(.top, 10)
+ if ling.provider.id == .ling {
+ Text(ling.hasKey ? L("Ling 不可用时自动换成手机的声音") : L("填入 Ling key 后可用 Ming 声音和你自己的声音"))
+ .font(.footnote).foregroundStyle(Ink.secondary).padding(.horizontal, 4).padding(.top, 10)
+ }
}
.padding(.horizontal, 16).padding(.top, 8).padding(.bottom, 120)
.frame(maxWidth: 640).frame(maxWidth: .infinity)
@@ -80,7 +87,7 @@ struct VoicesPage: View {
.fixedSize(horizontal: false, vertical: true)
}
}
- if !ling.hasKey {
+ if !ling.clones {
Text("只有 Ling 能用你自己的声音").font(.footnote).foregroundStyle(Ink.secondary)
} else if hasOwn {
HStack(spacing: 10) {
@@ -115,8 +122,8 @@ struct VoicesPage: View {
let options = VoiceCatalog.options(for: lang)
let mine = VoiceCatalog.key(for: lang) == VoiceCatalog.key(for: face.mine.code) && lang != face.theirs.code
// My own voice only for what is read to the other person (这是我自己的声音。只在我说话时用它替我翻译。).
- let rows = (!mine && hasOwn && ling.hasKey ? [VoiceCatalog.cloneOption] : []) + options
- let current = SpeakVoices.chosenID(for: lang, hasKey: ling.hasKey, mine: mine)
+ let rows = (!mine && hasOwn && ling.clones ? [VoiceCatalog.cloneOption] : []) + options
+ let current = SpeakVoices.chosenID(for: lang, hasKey: ling.hasKey, mine: mine, clones: ling.clones)
return VStack(alignment: .leading, spacing: 6) {
Text(verbatim: name(lang) + " · " + (mine ? L("对方说\(Pref.name(of: face.theirs))时用") : L("对方会听到这个")))
.font(.footnote).foregroundStyle(Ink.secondary).padding(.horizontal, 4).padding(.top, 14)