/// Detects a likely model preamble without rewriting it (`docs/TRANSLATION_OUTPUT.md` §6). public struct PreambleDetector: Sendable { /// The maximum first-line length for a preamble. public static let maximumFirstLineLength = 80 /// Creates a detector. public init() {} /// Returns the suspicious first line, or nil. /// /// The heuristic is deliberately simple: a first non-empty line of at most 80 characters /// ending in `:` or `:`, followed by a blank line and at least one more non-empty line. public func firstLine(in text: String) -> String? { let lines = text.components(separatedBy: "\n") guard let firstIndex = lines.firstIndex(where: { !$0.trimmingCharacters(in: .whitespaces).isEmpty }) else { return nil } let line = lines[firstIndex].trimmingCharacters(in: .whitespaces) guard line.count <= Self.maximumFirstLineLength, line.hasSuffix(":") || line.hasSuffix(":"), firstIndex + 1 < lines.count, lines[firstIndex + 1].trimmingCharacters(in: .whitespaces).isEmpty else { return nil } let hasMoreText = lines[(firstIndex + 2)...].contains { !$0.trimmingCharacters(in: .whitespaces).isEmpty } return hasMoreText ? line : nil } }