Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Services/GenerationRunner.swift3// Description: Follows an analysis job to its end: applies its events to a RunState and reconnects from the last event seen when the stream drops.4import Foundation56/// The job runs on the server whatever happens to this viewer. When the connection drops, the runner asks for the7/// stream again from the first event it has not applied, with backoff, until the job is finished or no longer in the8/// server's memory. It never restarts the analysis itself.9public struct GenerationRunner: Sendable {10 public enum Phase: Sendable, Equatable {11 case connecting12 case streaming13 /// the stream dropped; attempt n is scheduled14 case reconnecting(attempt: Int)15 case finished16 /// the viewer gave up; the job may still be running on the server17 case lost(String)18 }1920 public struct Update: Sendable {21 public var state: RunState22 public var phase: Phase23 public init(state: RunState, phase: Phase) { self.state = state; self.phase = phase }24 }2526 public var api: VQuantAPI27 public var maxAttempts = 828 public var backoff: @Sendable (Int) -> Duration = { n in .milliseconds(min(15_000, 500 * (1 << min(n, 5)))) }29 /// Every frame the run applies, once and in order (a frame replayed after a reconnection is not repeated). The app30 /// feeds the events backend 6 added — routing, usage, provenance, the research workspace — to the shared kit with it.31 public var onFrame: (@Sendable (SSEFrame) -> Void)?3233 public init(api: VQuantAPI) { self.api = api }3435 /// Starts a new analysis and follows it.36 public func start(_ request: ChatRequest) -> AsyncStream<Update> { run(initial: RunState()) { try await api.startAnalysis(request) } }3738 /// Follows an existing job, from the beginning or from a known state.39 public func attach(jobId: String, state: RunState = RunState()) -> AsyncStream<Update> {40 var s = state; s.jobId = jobId41 return run(initial: s, open: nil)42 }4344 private func run(initial: RunState, open: (@Sendable () async throws -> AsyncThrowingStream<String, Error>)?) -> AsyncStream<Update> {45 AsyncStream(bufferingPolicy: .bufferingNewest(1)) { continuation in46 let task = Task {47 var state = initial48 var attempt = 049 var first = open50 continuation.yield(Update(state: state, phase: .connecting))51 while !Task.isCancelled {52 do {53 let lines: AsyncThrowingStream<String, Error>54 if let f = first { first = nil; lines = try await f() }55 else if let id = state.jobId { lines = try await api.watchGeneration(id, from: state.nextEventIndex) }56 else { continuation.yield(Update(state: state, phase: .lost("The analysis could not be started."))); break }57 var parser = SSEParser()58 var lastYield = ContinuousClock.now59 continuation.yield(Update(state: state, phase: .streaming))60 for try await line in lines {61 guard let frame = parser.feed(line) else { continue }62 let before = state.nextEventIndex63 if frame.id.map({ $0 >= before }) ?? true { onFrame?(frame) }64 state.apply(frame)65 if state.nextEventIndex > before { attempt = 0 }66 // text arrives in small chunks: coalesce to at most ~30 updates per second67 let now = ContinuousClock.now68 if state.finished || now - lastYield > .milliseconds(33) || !isText(frame) { continuation.yield(Update(state: state, phase: .streaming)); lastYield = now }69 }70 if state.finished { continuation.yield(Update(state: state, phase: .finished)); break }71 // the stream ended without a `done` event: the connection was cut72 } catch is CancellationError { break }73 catch let e as APIError where e.kind == .notFound {74 // the server no longer holds the job's events: its answer, if any, is in the conversation75 continuation.yield(Update(state: state, phase: .lost("This analysis is no longer in the server's memory. Its answer, if it finished, is saved in the conversation.")))76 break77 } catch let e as APIError where e.kind != .network && state.jobId == nil {78 state.error = e.message; state.finished = true79 continuation.yield(Update(state: state, phase: .lost(e.message)))80 break81 } catch let e as APIError where e.kind == .unauthorized {82 continuation.yield(Update(state: state, phase: .lost(e.message)))83 break84 } catch {}85 guard state.jobId != nil else { continuation.yield(Update(state: state, phase: .lost("The connection dropped before the server confirmed the analysis. Check Jobs: it may be running."))); break }86 attempt += 187 if attempt > maxAttempts { continuation.yield(Update(state: state, phase: .lost("The connection to the server could not be restored. The analysis continues on the server; reopen it from Jobs."))); break }88 continuation.yield(Update(state: state, phase: .reconnecting(attempt: attempt)))89 try? await Task.sleep(for: backoff(attempt))90 }91 continuation.finish()92 }93 continuation.onTermination = { _ in task.cancel() }94 }95 }9697 private func isText(_ f: SSEFrame) -> Bool { let t = f.json["type"]?.stringValue; return t == "text" || t == "thinking" }98}99