Skip to content

Commit 6b7f8ff

Browse files
committed
Edit saved agent instructions inline and filter the profile chooser
1 parent b24c4e5 commit 6b7f8ff

8 files changed

Lines changed: 338 additions & 115 deletions

‎Sources/App/AppState.swift‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,26 @@ final class AppState: ObservableObject {
9292
let agentCatalog = AgentCatalog(url: URL(fileURLWithPath: Secrets.appSupportDir)
9393
.appendingPathComponent("agent-instructions-v1.json"))
9494
let agentAccess = AgentSessionController()
95+
private static let agentPresetURL = URL(fileURLWithPath: Secrets.appSupportDir)
96+
.appendingPathComponent("agent-preset-instructions-v1.json")
97+
@Published private(set) var agentPresetInstructions = (try? AgentPresetInstructions.read(at: AppState.agentPresetURL)) ?? [:]
98+
99+
func saveAgentPresetInstruction(_ selector: String, expected: UUID, text: String) throws {
100+
guard LocalInstructionProfile.Name(rawValue: selector) != nil,
101+
agentSnapshot(selector)?.generation == expected else { throw AgentCatalog.Failure.conflict }
102+
let saved = try AgentPresetInstructions.write(text, for: selector,
103+
expectedOverride: agentPresetInstructions[selector],
104+
at: Self.agentPresetURL)
105+
agentPresetInstructions = saved
106+
invalidatePreset(selector)
107+
}
108+
95109
private var presetSnapshots: [String: AgentInstructionSnapshot] = [:]
96110
@Published var sharedInstructionProfile: LocalInstructionProfile.Name?
97111

98112
func agentSnapshot(_ selector: String) -> AgentInstructionSnapshot? {
99113
if let builtin = LocalInstructionProfile.Name(rawValue: selector) {
100-
let text = instructionText(builtin)
114+
let text = agentPresetInstructions[selector] ?? instructionText(builtin)
101115
if let old = presetSnapshots[selector], AgentBytes.same(old.text, text) { return old }
102116
let snapshot = AgentInstructionSnapshot(selector: selector, generation: UUID(), text: text)
103117
presetSnapshots[selector] = snapshot
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)