mirror of
https://github.com/hanzoai/bot.git
synced 2026-10-30 17:24:26 +00:00
Squash merge of 1,462 upstream commits with openclaw→hanzo-bot naming transforms applied. Conflicts resolved favoring our main branch to preserve existing customizations. Key upstream additions: - iOS Security Stack (concurrency locks, runtime guards, TTS fallback) - Discord CDN hostname allowlisting for SSRF protection - Discord mention handling improvements - Various bug fixes and stability improvements
65 lines
2.1 KiB
Swift
65 lines
2.1 KiB
Swift
import Foundation
|
|
import BotKit
|
|
import OSLog
|
|
|
|
@MainActor
|
|
final class VoiceWakeGlobalSettingsSync {
|
|
static let shared = VoiceWakeGlobalSettingsSync()
|
|
|
|
private let logger = Logger(subsystem: "ai.bot", category: "voicewake.sync")
|
|
private var task: Task<Void, Never>?
|
|
|
|
private struct VoiceWakePayload: Codable, Equatable {
|
|
let triggers: [String]
|
|
}
|
|
|
|
func start() {
|
|
SimpleTaskSupport.start(task: &self.task) { [weak self] in
|
|
guard let self else { return }
|
|
while !Task.isCancelled {
|
|
do {
|
|
try await GatewayConnection.shared.refresh()
|
|
} catch {
|
|
// Not configured / not reachable yet.
|
|
}
|
|
|
|
await self.refreshFromGateway()
|
|
|
|
let stream = await GatewayConnection.shared.subscribe(bufferingNewest: 200)
|
|
for await push in stream {
|
|
if Task.isCancelled { return }
|
|
await self.handle(push: push)
|
|
}
|
|
|
|
// If the stream finishes (gateway shutdown / reconnect), loop and resubscribe.
|
|
try? await Task.sleep(nanoseconds: 600_000_000)
|
|
}
|
|
}
|
|
}
|
|
|
|
func stop() {
|
|
SimpleTaskSupport.stop(task: &self.task)
|
|
}
|
|
|
|
private func refreshFromGateway() async {
|
|
do {
|
|
let triggers = try await GatewayConnection.shared.voiceWakeGetTriggers()
|
|
AppStateStore.shared.applyGlobalVoiceWakeTriggers(triggers)
|
|
} catch {
|
|
// Best-effort only.
|
|
}
|
|
}
|
|
|
|
func handle(push: GatewayPush) async {
|
|
guard case let .event(evt) = push else { return }
|
|
guard evt.event == "voicewake.changed" else { return }
|
|
guard let payload = evt.payload else { return }
|
|
do {
|
|
let decoded = try GatewayPayloadDecoding.decode(payload, as: VoiceWakePayload.self)
|
|
AppStateStore.shared.applyGlobalVoiceWakeTriggers(decoded.triggers)
|
|
} catch {
|
|
self.logger.error("failed to decode voicewake.changed: \(error.localizedDescription, privacy: .public)")
|
|
}
|
|
}
|
|
}
|