SPB Git

Repository Public

AnalysisStream.swift 13.1 KB 270 lines swift

1//2//  VQuant iOS3//  File: Core/Streaming/AnalysisStream.swift4//  Description: SSE parser, the events of an analysis job, and the reducer that folds them into what the screen shows.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//910import Foundation11import os1213struct SSEEvent: Equatable, Sendable {14    var event: String15    var data: String16    /// The backend numbers the events of a job; the last index seen is what a reconnection resumes from.17    var id: Int?18}1920/// Byte-level SSE parser: LF and CRLF, multi-line `data:`, comments (heartbeats), events split at any byte.21struct SSEParser: Sendable {22    enum Output: Equatable, Sendable { case event(SSEEvent), heartbeat }23    private var line: [UInt8] = []24    private var event = "message"25    private var data: [String] = []26    private var id: Int?27    private var sawField = false2829    init() { line.reserveCapacity(4096) }3031    mutating func feed(_ byte: UInt8) -> Output? {32        if byte != 0x0A { line.append(byte); return nil }33        if line.last == 0x0D { line.removeLast() }34        defer { line.removeAll(keepingCapacity: true) }35        if line.isEmpty { return dispatch() }36        if line[0] == 0x3A { return .heartbeat }37        let text = String(decoding: line, as: UTF8.self)38        let field: Substring, value: Substring39        if let colon = text.firstIndex(of: ":") {40            field = text[..<colon]41            var v = text[text.index(after: colon)...]42            if v.first == " " { v = v.dropFirst() }43            value = v44        } else { field = Substring(text); value = "" }45        sawField = true46        switch field {47        case "event": event = String(value)48        case "data": data.append(String(value))49        case "id": id = Int(value.trimmingCharacters(in: .whitespaces))50        default: break51        }52        return nil53    }5455    mutating func feed<S: Sequence>(_ bytes: S) -> [Output] where S.Element == UInt8 {56        var out: [Output] = []57        for b in bytes { if let o = feed(b) { out.append(o) } }58        return out59    }6061    private mutating func dispatch() -> Output? {62        defer { event = "message"; data.removeAll(keepingCapacity: true); id = nil; sawField = false }63        guard sawField, !data.isEmpty else { return nil }64        return .event(SSEEvent(event: event, data: data.joined(separator: "\n"), id: id))65    }66}6768/// What the agent loop writes to the job (`server/routes.ts`, `server/services/agents.ts`).69enum AnalysisEvent: Equatable, Sendable {70    case job(id: String, sessionId: String, query: String)71    case status(String)72    case text(String)73    case thinking(String)74    case plan([String])75    case toolStart(tool: String, toolId: String, input: JSONValue)76    case toolComplete(tool: String, toolId: String, durationMs: Double?, empty: Bool)77    case toolError(tool: String, toolId: String, error: String)78    case toolResult(tool: String, result: JSONValue)79    case pythonCode(String)80    case figures(FigureBatch)81    case agentStart(agentId: String, name: String, kind: String?)82    case agentTool(agentId: String, tool: String, status: String)83    case agentDone(agentId: String, ok: Bool, tools: Int, preview: String)84    case sources([SourceLink])85    case followups([String])86    case error(String)87    case done(sessionId: String?)88    /// An event type this version does not know: kept so nothing is silently dropped from the audit view.89    case other(type: String)9091    init(sse: SSEEvent) {92        guard let data = sse.data.data(using: .utf8), let v = try? JSONDecoder().decode(JSONValue.self, from: data), let type = v["type"]?.stringValue else {93            self = .other(type: "unreadable"); return94        }95        func s(_ k: String) -> String { v[k]?.stringValue ?? "" }96        switch type {97        case "job": self = .job(id: s("id"), sessionId: s("sessionId"), query: s("query"))98        case "status": self = .status(s("message"))99        case "text": self = .text(s("content"))100        case "thinking": self = .thinking(s("content"))101        case "plan":102            let steps = (v["steps"]?.arrayValue ?? []).map { $0.stringValue ?? $0["title"]?.stringValue ?? $0["step"]?.stringValue ?? $0.prettyPrinted(limit: 200) }103            self = .plan(steps)104        case "tool_start": self = .toolStart(tool: s("tool"), toolId: s("toolId"), input: v["input"] ?? .null)105        case "tool_complete": self = .toolComplete(tool: s("tool"), toolId: s("toolId"), durationMs: v["duration"]?.doubleValue, empty: v["empty"]?.boolValue ?? false)106        case "tool_error": self = .toolError(tool: s("tool"), toolId: s("toolId"), error: s("error"))107        case "tool_result": self = .toolResult(tool: s("tool"), result: v["result"] ?? .null)108        case "python_code": self = .pythonCode(s("code"))109        case "custom_python_figures":110            let strings: (String) -> [String] = { k in (v[k]?.arrayValue ?? []).compactMap(\.stringValue) }111            self = .figures(FigureBatch(id: v["figureId"]?.stringValue ?? UUID().uuidString, figures: strings("figures"), figureUrls: strings("figureUrls"),112                                        output: v["output"]?.stringValue, description: v["description"]?.stringValue))113        case "agent_start": self = .agentStart(agentId: s("agentId"), name: s("name"), kind: v["kind"]?.stringValue)114        case "agent_tool": self = .agentTool(agentId: s("agentId"), tool: s("tool"), status: s("status"))115        case "agent_done": self = .agentDone(agentId: s("agentId"), ok: v["ok"]?.boolValue ?? true, tools: v["tools"]?.intValue ?? 0, preview: s("preview"))116        case "sources":117            let list = (v["sources"]?.arrayValue ?? []).compactMap { o -> SourceLink? in118                guard let url = o["url"]?.stringValue else { return nil }119                return SourceLink(title: o["title"]?.stringValue ?? url, url: url, snippet: o["snippet"]?.stringValue, rank: o["rank"]?.intValue)120            }121            self = .sources(list)122        case "followups": self = .followups((v["questions"]?.arrayValue ?? []).compactMap(\.stringValue))123        case "error": self = .error(s("error"))124        case "done": self = .done(sessionId: v["sessionId"]?.stringValue)125        default: self = .other(type: type)126        }127    }128}129130struct StreamItem: Sendable, Equatable {131    var index: Int?132    var event: AnalysisEvent133    /// The event's JSON as received. The app hands it to the shared research reducer (VQuantKit), which reads the134    /// event types backend 6 added; nil in hand-built test items.135    var raw: String? = nil136}137138enum SSEStream {139    /// The server sends a comment every 20 s; beyond this silence the connection is considered dead and the caller re-attaches.140    static let silenceLimit: Duration = .seconds(60)141142    static func items(from bytes: URLSession.AsyncBytes, silenceLimit: Duration = SSEStream.silenceLimit) -> AsyncThrowingStream<StreamItem, Error> {143        AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in144            let lastActivity = OSAllocatedUnfairLock(initialState: ContinuousClock.now)145            let reader = Task.detached(priority: .userInitiated) {146                var parser = SSEParser()147                do {148                    for try await byte in bytes {149                        guard let out = parser.feed(byte) else { continue }150                        lastActivity.withLock { $0 = .now }151                        if case .event(let e) = out { continuation.yield(StreamItem(index: e.id, event: AnalysisEvent(sse: e), raw: e.data)) }152                    }153                    continuation.finish()154                } catch { continuation.finish(throwing: error) }155            }156            let watchdog = Task.detached(priority: .utility) {157                while !Task.isCancelled {158                    try? await Task.sleep(for: .seconds(5))159                    if ContinuousClock.now - lastActivity.withLock({ $0 }) > silenceLimit {160                        reader.cancel()161                        continuation.finish(throwing: URLError(.timedOut))162                        return163                    }164                }165            }166            continuation.onTermination = { _ in reader.cancel(); watchdog.cancel() }167        }168    }169}170171/// The visible state of an analysis, rebuilt from its events. Pure and deterministic: replaying the same events172/// from index 0 always gives the same state, which is what makes reconnection safe.173struct AnalysisRun: Equatable, Sendable {174    struct Step: Equatable, Sendable, Identifiable {175        enum Kind: Equatable, Sendable { case status, tool, agent, plan }176        enum State: Equatable, Sendable { case running, done, failed }177        var id: String178        var kind: Kind179        var title: String180        var detail: String?181        var state: State182        var durationMs: Double?183        var input: JSONValue?184    }185186    var jobId: String?187    var sessionId: String?188    var query = ""189    var answer = ""190    var status = ""191    var steps: [Step] = []192    var toolResults: [ToolRecord] = []193    var pythonCode: [String] = []194    var figures: [FigureBatch] = []195    var sources: [SourceLink] = []196    var followups: [String] = []197    var error: String?198    var isFinished = false199    /// Index of the next event to ask the server for.200    var nextIndex = 0201    var unknownEvents: [String] = []202203    var toolCount: Int { steps.filter { $0.kind == .tool }.count }204205    /// Event types of backend 6 that this reducer leaves to the research view (they are not "unknown").206    static let nextEvents: Set<String> = ["research_workspace", "dataset", "research_file", "code_execution", "package_install", "research_run", "experiment",207                                          "research_note", "figure_lint", "approval_request", "approval_resolved", "quality_gate", "budget", "model_routed",208                                          "quant_job", "provenance", "budget_applied", "code_execution_start"]209210    mutating func apply(_ item: StreamItem) {211        if let i = item.index {212            if i < nextIndex { return }                         // already applied (overlap after a reconnection)213            nextIndex = i + 1214        }215        switch item.event {216        case .job(let id, let sessionId, let query):217            jobId = id; self.sessionId = sessionId; if self.query.isEmpty { self.query = query }218        case .status(let m):219            status = m220        case .text(let t):221            answer += t222        case .thinking:223            if status.isEmpty { status = "Réflexion" }224        case .plan(let list):225            steps.append(Step(id: "plan-\(steps.count)", kind: .plan, title: "Plan de recherche", detail: list.enumerated().map { "\($0.offset + 1). \($0.element)" }.joined(separator: "\n"), state: .done))226        case .toolStart(let tool, let toolId, let input):227            let id = toolId.isEmpty ? "tool-\(steps.count)" : toolId228            if !steps.contains(where: { $0.id == id }) { steps.append(Step(id: id, kind: .tool, title: tool, state: .running, input: input)) }229            status = "Outil : \(tool)"230        case .toolComplete(_, let toolId, let duration, let empty):231            if let i = steps.lastIndex(where: { $0.id == toolId }) { steps[i].state = .done; steps[i].durationMs = duration; if empty { steps[i].detail = "Aucune donnée renvoyée" } }232        case .toolError(_, let toolId, let message):233            if let i = steps.lastIndex(where: { $0.id == toolId }) { steps[i].state = .failed; steps[i].detail = message }234        case .toolResult(let tool, let result):235            toolResults.append(ToolRecord(tool: tool, result: result))236        case .pythonCode(let code):237            if !code.isEmpty { pythonCode.append(code) }238        case .figures(let batch):239            figures.append(batch)240        case .agentStart(let agentId, let name, _):241            steps.append(Step(id: "agent-\(agentId)", kind: .agent, title: name.isEmpty ? "Sous-analyste" : name, state: .running))242            status = "Sous-analyste : \(name)"243        case .agentTool(let agentId, let tool, let state):244            if let i = steps.lastIndex(where: { $0.id == "agent-\(agentId)" }), state == "running" { steps[i].detail = "Outil : \(tool)" }245        case .agentDone(let agentId, let ok, let tools, let preview):246            if let i = steps.lastIndex(where: { $0.id == "agent-\(agentId)" }) {247                steps[i].state = ok ? .done : .failed248                steps[i].detail = "\(tools) outil\(tools > 1 ? "s" : "")" + (preview.isEmpty ? "" : " · \(preview)")249            }250        case .sources(let s):251            sources = s252        case .followups(let f):253            followups = f254        case .error(let message):255            error = message256        case .done(let sid):257            if let sid, !sid.isEmpty { sessionId = sid }258            isFinished = true259            for i in steps.indices where steps[i].state == .running { steps[i].state = error == nil ? .done : .failed }260        case .other(let type):261            if !["tools", "workspace", "unreadable"].contains(type), !Self.nextEvents.contains(type), !unknownEvents.contains(type) { unknownEvents.append(type) }262        }263    }264265    /// The exchange this run produced, in the shape the conversation stores.266    var exchange: Exchange {267        Exchange(question: query, answer: answer, toolResults: toolResults, sources: sources, pythonCode: pythonCode.last, figures: figures)268    }269}270
Files main
Menu

Theme