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:
schihei committed 2026-11-26 13:04:22 +01:00
1 parent 7ca5c710ec
commit 26b493e7e5
93 files changed
+3732 -218

No files matched your search

@@ -0,0 +1,57 @@
import Foundation
import SprachmittlerOllama
/// A catalog fake backed by fixed values.
public struct FakeOllamaCatalog: OllamaCatalog, Sendable {
/// The reported server version.
public let version: String
/// The installed models.
public let models: [InstalledModel]
/// Details by model tag.
public let details: [String: ModelDetails]
/// The loaded models.
public let loaded: [LoadedModel]
/// Creates a catalog fake.
public init(
version: String = "0.40.18",
models: [InstalledModel] = [],
details: [String: ModelDetails] = [:],
loaded: [LoadedModel] = []
) {
self.version = version
self.models = models
self.details = details
self.loaded = loaded
}
public func serverVersion() async throws -> String {
version
}
public func installedModels() async throws -> [InstalledModel] {
models
}
public func show(model: String) async throws -> ModelDetails {
guard let details = details[model] else {
throw OllamaError.modelNotInstalled(model)
}
return details
}
public func loadedModels() async throws -> [LoadedModel] {
loaded
}
public func refresh() async {}
public func checkMinimumVersion() async throws {
guard let parsed = OllamaVersion(version), parsed >= .minimumSupported else {
throw OllamaError.unsupportedVersion(
found: version,
required: OllamaVersion.minimumSupported.description
)
}
}
}
@@ -0,0 +1,29 @@
import Foundation
import SprachmittlerOllama
/// A chat fake that emits a fixed event list and records the request through a caller closure.
public struct FakeOllamaChat: OllamaChatting, Sendable {
/// The events emitted for every chat call.
public let events: [ChatEvent]
/// Called synchronously with each request.
public let onRequest: @Sendable (ChatRequest) -> Void
/// Creates a fake chat.
public init(
events: [ChatEvent] = [.done(reason: "stop", metrics: .empty)],
onRequest: @escaping @Sendable (ChatRequest) -> Void = { _ in }
) {
self.events = events
self.onRequest = onRequest
}
public func chat(_ request: ChatRequest) -> AsyncThrowingStream<ChatEvent, any Error> {
onRequest(request)
return AsyncThrowingStream { continuation in
for event in events {
continuation.yield(event)
}
continuation.finish()
}
}
}
@@ -0,0 +1,26 @@
import Foundation
import SprachmittlerOllama
/// A transport fake for catalog and chat tests. It records every request and answers through a
/// caller-supplied handler.
public actor FakeOllamaHTTPTransport: OllamaHTTPTransport {
/// The reply function.
public typealias Handler = @Sendable (OllamaHTTPRequest) throws -> OllamaHTTPResponse
private let handler: Handler
/// Every request seen, in order.
public private(set) var requests: [OllamaHTTPRequest] = []
/// The model argument supplied with each request, in order.
public private(set) var models: [String?] = []
/// Creates a fake with the given reply handler.
public init(handler: @escaping Handler) {
self.handler = handler
}
public func send(_ request: OllamaHTTPRequest, model: String?) async throws -> OllamaHTTPResponse {
requests.append(request)
models.append(model)
return try handler(request)
}
}
@@ -0,0 +1,64 @@
import Foundation
import SprachmittlerOllama
/// A manual sleeper for timeout tests: no real time passes. Tests wait for a pending duration
/// and advance it explicitly.
public actor ManualSleeper: AsyncSleeper {
private struct Waiter {
let duration: Duration
let continuation: CheckedContinuation<Void, any Error>
}
private var waiters: [UUID: Waiter] = [:]
private var observers: [Duration: [CheckedContinuation<Void, Never>]] = [:]
/// Creates a manual sleeper.
public init() {}
public func sleep(for duration: Duration) async throws {
if Task.isCancelled {
throw CancellationError()
}
let id = UUID()
try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
waiters[id] = Waiter(duration: duration, continuation: continuation)
let pending = observers.removeValue(forKey: duration) ?? []
pending.forEach { $0.resume() }
}
} onCancel: {
Task {
await self.cancel(id)
}
}
}
/// Suspends until a sleep for `duration` is waiting.
public func waitUntilPending(_ duration: Duration) async {
if waiters.values.contains(where: { $0.duration == duration }) {
return
}
await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
observers[duration, default: []].append(continuation)
}
}
/// Resumes every sleep waiting for `duration`.
public func advance(matching duration: Duration) {
let matching = waiters.filter { $0.value.duration == duration }
for (id, waiter) in matching {
waiters.removeValue(forKey: id)
waiter.continuation.resume()
}
}
/// The number of sleepers currently waiting for `duration`.
public func pendingCount(_ duration: Duration) -> Int {
waiters.values.filter { $0.duration == duration }.count
}
private func cancel(_ id: UUID) {
guard let waiter = waiters.removeValue(forKey: id) else { return }
waiter.continuation.resume(throwing: CancellationError())
}
}
@@ -1,6 +0,0 @@
// Placeholder so the target compiles. Replace during T0.1+ (see docs/IMPLEMENTATION_PLAN.md).
/// Namespace for the SprachmittlerTestSupport module.
public enum SprachmittlerTestSupportModule {
public static let moduleName = "SprachmittlerTestSupport"
}