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.
122 lines
4.4 KiB
Swift
122 lines
4.4 KiB
Swift
import Foundation
|
|
|
|
/// The details block returned by `/api/tags` and `/api/show`.
|
|
public struct ModelDetailsInfo: Sendable, Equatable, Decodable {
|
|
/// The parent model tag, when reported.
|
|
public let parentModel: String?
|
|
/// The format, normally `gguf`.
|
|
public let format: String?
|
|
/// The model family, e.g. `hunyuan-dense`.
|
|
public let family: String?
|
|
/// All reported families.
|
|
public let families: [String]?
|
|
/// The parameter size string, e.g. `7.5B`.
|
|
public let parameterSize: String?
|
|
/// The quantization level, e.g. `Q4_K_M`.
|
|
public let quantizationLevel: String?
|
|
/// The base context length from `/api/tags` details, when present (the adapter uses 8192).
|
|
public let contextLength: Int?
|
|
/// The embedding length from `/api/tags` details, when present.
|
|
public let embeddingLength: Int?
|
|
/// The runner name, when reported.
|
|
public let runner: String?
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case parentModel = "parent_model"
|
|
case format
|
|
case family
|
|
case families
|
|
case parameterSize = "parameter_size"
|
|
case quantizationLevel = "quantization_level"
|
|
case contextLength = "context_length"
|
|
case embeddingLength = "embedding_length"
|
|
case runner
|
|
}
|
|
}
|
|
|
|
/// One parameter line from `/api/show`'s `parameters` string.
|
|
public struct ModelParameter: Sendable, Equatable {
|
|
/// The parameter name, e.g. `stop`.
|
|
public let name: String
|
|
/// The parameter value with surrounding quotes removed.
|
|
public let value: String
|
|
|
|
/// Creates a parameter.
|
|
public init(name: String, value: String) {
|
|
self.name = name
|
|
self.value = value
|
|
}
|
|
|
|
/// Parses the newline-separated `name value` lines from `/api/show`.
|
|
public static func parse(_ text: String) -> [ModelParameter] {
|
|
text.split(separator: "\n").compactMap { line in
|
|
let trimmed = line.trimmingCharacters(in: .whitespaces)
|
|
guard let separator = trimmed.firstIndex(where: { $0 == " " || $0 == "\t" }) else {
|
|
return nil
|
|
}
|
|
let name = String(trimmed[..<separator])
|
|
var value = String(trimmed[separator...]).trimmingCharacters(in: .whitespaces)
|
|
if value.hasPrefix("\""), value.hasSuffix("\""), value.count >= 2 {
|
|
value = String(value.dropFirst().dropLast())
|
|
}
|
|
return ModelParameter(name: name, value: value)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The fields from `POST /api/show` that v1 uses.
|
|
public struct ModelDetails: Sendable, Equatable, Decodable {
|
|
/// Capabilities such as `completion`; empty when omitted.
|
|
public let capabilities: [String]
|
|
/// Family and quantization details.
|
|
public let details: ModelDetailsInfo?
|
|
/// The raw parameter lines; parse them with `ModelParameter.parse`.
|
|
public let parameters: String?
|
|
/// The chat template.
|
|
public let template: String?
|
|
/// The server-reported licence; `null` for the current GGUF.
|
|
public let license: String?
|
|
|
|
/// Creates model details.
|
|
public init(
|
|
capabilities: [String] = [],
|
|
details: ModelDetailsInfo? = nil,
|
|
parameters: String? = nil,
|
|
template: String? = nil,
|
|
license: String? = nil
|
|
) {
|
|
self.capabilities = capabilities
|
|
self.details = details
|
|
self.parameters = parameters
|
|
self.template = template
|
|
self.license = license
|
|
}
|
|
|
|
/// The parsed parameter lines, preserving repeated keys such as `stop`.
|
|
public var parsedParameters: [ModelParameter] {
|
|
ModelParameter.parse(parameters ?? "")
|
|
}
|
|
|
|
/// True when the model reports text completion.
|
|
public var supportsCompletion: Bool {
|
|
capabilities.contains("completion")
|
|
}
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case capabilities
|
|
case details
|
|
case parameters
|
|
case template
|
|
case license
|
|
}
|
|
|
|
public init(from decoder: any Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
capabilities = try container.decodeIfPresent([String].self, forKey: .capabilities) ?? []
|
|
details = try container.decodeIfPresent(ModelDetailsInfo.self, forKey: .details)
|
|
parameters = try container.decodeIfPresent(String.self, forKey: .parameters)
|
|
template = try container.decodeIfPresent(String.self, forKey: .template)
|
|
license = try container.decodeIfPresent(String.self, forKey: .license)
|
|
}
|
|
}
|