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.
82 lines
2.3 KiB
Swift
82 lines
2.3 KiB
Swift
import Foundation
|
|
|
|
/// A chat-only URLProtocol stub. It has its own static state, so the chat suite can run in
|
|
/// parallel with the HTTP transport suite.
|
|
final class ChatStubURLProtocol: URLProtocol, @unchecked Sendable {
|
|
struct Stub: Sendable {
|
|
var chunks: [Data] = []
|
|
var hangAfterChunks = false
|
|
}
|
|
|
|
private static let lock = NSLock()
|
|
private nonisolated(unsafe) static var stub: Stub?
|
|
private nonisolated(unsafe) static var wasStopped = false
|
|
private nonisolated(unsafe) static var wasStarted = false
|
|
|
|
static func reset(_ stub: Stub? = nil) {
|
|
lock.lock()
|
|
self.stub = stub
|
|
wasStopped = false
|
|
wasStarted = false
|
|
lock.unlock()
|
|
}
|
|
|
|
static func stopped() -> Bool {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
return wasStopped
|
|
}
|
|
|
|
static func started() -> Bool {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
return wasStarted
|
|
}
|
|
|
|
private static func beginRequest() -> Stub? {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
wasStarted = true
|
|
return stub
|
|
}
|
|
|
|
override static func canInit(with request: URLRequest) -> Bool {
|
|
true
|
|
}
|
|
|
|
override static func canonicalRequest(for request: URLRequest) -> URLRequest {
|
|
request
|
|
}
|
|
|
|
override func startLoading() {
|
|
guard let stub = Self.beginRequest() else {
|
|
client?.urlProtocol(self, didFailWithError: URLError(.unknown))
|
|
return
|
|
}
|
|
guard let url = request.url,
|
|
let response = HTTPURLResponse(
|
|
url: url,
|
|
statusCode: 200,
|
|
httpVersion: "HTTP/1.1",
|
|
headerFields: ["Content-Type": "application/x-ndjson"]
|
|
)
|
|
else {
|
|
client?.urlProtocol(self, didFailWithError: URLError(.badURL))
|
|
return
|
|
}
|
|
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
|
|
for chunk in stub.chunks {
|
|
client?.urlProtocol(self, didLoad: chunk)
|
|
}
|
|
if !stub.hangAfterChunks {
|
|
client?.urlProtocolDidFinishLoading(self)
|
|
}
|
|
}
|
|
|
|
override func stopLoading() {
|
|
Self.lock.lock()
|
|
Self.wasStopped = true
|
|
Self.lock.unlock()
|
|
}
|
|
}
|