Files
schihei 26b493e7e5 feat(m1): Ollama client, catalog, streaming chat and CLI (T1.1-T1.6)
Add endpoint policy and redirect-safe ephemeral HTTP client, error mapping, catalog cache and version checks, streaming chat with phase timeouts/cancellation, provisional Hy-MT3 prompt/language/request factory, warm-up policy, and sprachmittler-cli models/show/translate. Recapture the 7B fixtures; defer the 1.8B VERIFY items; document ADR-016. Also fix the signature checker's pipefail SIGPIPE issue.
2026-11-26 13:04:22 +01:00

45 lines
1.1 KiB
Swift

import Foundation
/// Schedules one timeout at a time. Scheduling a new phase cancels the previous timer.
actor ChatTimeoutController {
private let sleeper: any AsyncSleeper
private var generation = 0
private var task: Task<Void, Never>?
private var handler: (@Sendable (TimeoutPhase) -> Void)?
init(sleeper: any AsyncSleeper) {
self.sleeper = sleeper
}
func schedule(
_ phase: TimeoutPhase,
after duration: Duration,
onTimeout: @escaping @Sendable (TimeoutPhase) -> Void
) {
generation += 1
let expected = generation
task?.cancel()
handler = onTimeout
task = Task { [weak self, sleeper] in
do {
try await sleeper.sleep(for: duration)
} catch {
return
}
await self?.fire(expected, phase)
}
}
private func fire(_ expected: Int, _ phase: TimeoutPhase) {
guard expected == generation else { return }
handler?(phase)
}
func cancel() {
generation += 1
task?.cancel()
task = nil
handler = nil
}
}