|
| 1 | +import Foundation |
| 2 | + |
| 3 | +struct AgentInstructionEditBuffer { |
| 4 | + struct Entry { |
| 5 | + let expected: UUID |
| 6 | + let originalLabel: String |
| 7 | + let originalText: String |
| 8 | + var label: String |
| 9 | + var text: String |
| 10 | + |
| 11 | + var isDirty: Bool { !AgentBytes.same(label, originalLabel) || !AgentBytes.same(text, originalText) } |
| 12 | + } |
| 13 | + |
| 14 | + private(set) var drafts: [String: Entry] = [:] |
| 15 | + |
| 16 | + func entry(for selector: String, expected: UUID, label: String, text: String) -> Entry { |
| 17 | + drafts[selector] ?? Entry(expected: expected, originalLabel: label, originalText: text, label: label, text: text) |
| 18 | + } |
| 19 | + |
| 20 | + mutating func update(_ entry: Entry, for selector: String) { |
| 21 | + drafts[selector] = entry.isDirty ? entry : nil |
| 22 | + } |
| 23 | + |
| 24 | + mutating func revert(_ selector: String) { |
| 25 | + drafts[selector] = nil |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +struct AgentInstructionChoice: Identifiable { |
| 30 | + let id: String |
| 31 | + let label: String |
| 32 | + |
| 33 | + static func matching(_ options: [Self], query: String) -> [Self] { |
| 34 | + let query = query.trimmingCharacters(in: .whitespacesAndNewlines) |
| 35 | + return query.isEmpty ? options : options.filter { $0.label.localizedStandardContains(query) } |
| 36 | + } |
| 37 | + |
| 38 | + /// Enter in the filter is a choice only if it identifies one exact option. |
| 39 | + static func keyboardChoice(_ options: [Self], query: String) -> String? { |
| 40 | + let matches = matching(options, query: query) |
| 41 | + guard !query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, matches.count == 1 else { return nil } |
| 42 | + return matches[0].id |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +enum AgentPresetInstructions { |
| 47 | + private static let selectors: Set<String> = ["explain", "continue", "normalize"] |
| 48 | + |
| 49 | + static func read(at url: URL) throws -> [String: String] { |
| 50 | + let data: Data |
| 51 | + do { data = try Data(contentsOf: url) } |
| 52 | + catch let error as NSError where error.domain == NSCocoaErrorDomain && error.code == NSFileReadNoSuchFileError { |
| 53 | + return [:] |
| 54 | + } |
| 55 | + let saved = try JSONDecoder().decode([String: String].self, from: data) |
| 56 | + guard saved.allSatisfy({ selectors.contains($0.key) && AgentBytes.textValid($0.value) }) else { |
| 57 | + throw CocoaError(.fileReadCorruptFile) |
| 58 | + } |
| 59 | + return saved |
| 60 | + } |
| 61 | + |
| 62 | + static func write(_ text: String, for selector: String, expectedOverride: String?, at url: URL) throws -> [String: String] { |
| 63 | + guard selectors.contains(selector), AgentBytes.textValid(text) else { |
| 64 | + throw AgentCatalog.Failure.invalid |
| 65 | + } |
| 66 | + var current = try read(at: url) |
| 67 | + let matches = current[selector].map { old in expectedOverride.map { AgentBytes.same($0, old) } == true } |
| 68 | + ?? (expectedOverride == nil) |
| 69 | + guard matches else { throw AgentCatalog.Failure.conflict } |
| 70 | + current[selector] = text |
| 71 | + let payload = try JSONEncoder().encode(current) |
| 72 | + try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true) |
| 73 | + try payload.write(to: url, options: .atomic) |
| 74 | + return current |
| 75 | + } |
| 76 | +} |
0 commit comments