spb/zyquo-router Public MIT
One local endpoint, every AI provider — a private OpenAI-compatible LLM gateway for your Mac (170 models, 12 providers).
Swift 95.7%
Python 2.3%
Shell 1.2%
Makefile 0.9%
1//2// SSEWriter.swift3// Zyquo Router4//5// Author: Simon-Pierre Boucher6// Mail: contact@spboucher.ai7//8// Spec-exact Server-Sent Events emission for /v1/chat/completions streaming:9// each event is `data: <json>\n\n`, the stream ends with `data: [DONE]\n\n`.10// Every write flushes; a write against a disconnected client throws, which11// cancels the calling route task and, with it, the upstream provider call.12//1314import Foundation15import NIOCore16import NIOHTTP11718struct SSEWriter {19 let outbound: NIOAsyncChannelOutboundWriter<HTTPServerResponsePart>2021 private static let encoder: JSONEncoder = {22 let encoder = JSONEncoder()23 encoder.outputFormatting = [.withoutEscapingSlashes]24 return encoder25 }()2627 /// Emits one `data:` event carrying an encodable payload (a chunk object).28 func send<T: Encodable>(_ payload: T) async throws {29 try await send(raw: Self.encoder.encode(payload))30 }3132 /// Emits one `data:` event carrying pre-serialized JSON (pass-through path).33 func send(raw json: Data) async throws {34 var buffer = ByteBuffer()35 buffer.writeString("data: ")36 buffer.writeBytes(json)37 buffer.writeString("\n\n")38 try await outbound.write(.body(.byteBuffer(buffer)))39 }4041 /// Emits an SSE comment line (keep-alive heartbeat).42 func sendHeartbeat() async throws {43 try await outbound.write(.body(.byteBuffer(ByteBuffer(string: ": keep-alive\n\n"))))44 }4546 /// Terminates the stream per the OpenAI contract.47 func sendDone() async throws {48 try await outbound.write(.body(.byteBuffer(ByteBuffer(string: "data: [DONE]\n\n"))))49 }50}51