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.
This commit is contained in:
1 parent
7ca5c710ec
commit
26b493e7e5
93 files changed
+3732
-218
No files matched your search
+81
@@ -0,0 +1,81 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
import Foundation
|
||||
|
||||
/// A serialized `URLProtocol` stub for contract tests. Test suites using it must be `.serialized`.
|
||||
final class StubURLProtocol: URLProtocol, @unchecked Sendable {
|
||||
struct Stub: Sendable {
|
||||
var status = 200
|
||||
var headers: [String: String] = ["Content-Type": "application/json"]
|
||||
var chunks: [Data] = []
|
||||
var error: URLError?
|
||||
var hangAfterChunks = false
|
||||
}
|
||||
|
||||
struct Snapshot: Sendable {
|
||||
let count: Int
|
||||
let stopped: Bool
|
||||
let lastRequest: URLRequest?
|
||||
}
|
||||
|
||||
private static let lock = NSLock()
|
||||
private nonisolated(unsafe) static var stub: Stub?
|
||||
private nonisolated(unsafe) static var requestCount = 0
|
||||
private nonisolated(unsafe) static var wasStopped = false
|
||||
private nonisolated(unsafe) static var lastRequestValue: URLRequest?
|
||||
|
||||
static func reset(_ stub: Stub? = nil) {
|
||||
lock.lock()
|
||||
self.stub = stub
|
||||
requestCount = 0
|
||||
wasStopped = false
|
||||
lastRequestValue = nil
|
||||
lock.unlock()
|
||||
}
|
||||
|
||||
static func snapshot() -> Snapshot {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return Snapshot(count: requestCount, stopped: wasStopped, lastRequest: lastRequestValue)
|
||||
}
|
||||
|
||||
private static func beginRequest(_ request: URLRequest) -> Stub? {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
requestCount += 1
|
||||
lastRequestValue = request
|
||||
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(request) else {
|
||||
client?.urlProtocol(self, didFailWithError: URLError(.unknown))
|
||||
return
|
||||
}
|
||||
if let error = stub.error {
|
||||
client?.urlProtocol(self, didFailWithError: error)
|
||||
return
|
||||
}
|
||||
guard let url = request.url,
|
||||
let response = HTTPURLResponse(
|
||||
url: url,
|
||||
statusCode: stub.status,
|
||||
httpVersion: "HTTP/1.1",
|
||||
headerFields: stub.headers
|
||||
)
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user