Branchwell

Jobley / app · jobley.ai / Sources / JobleyKit / AgentSettings.swift

Raw 2.0 KB on 31b31c099e453aea6c2ecdc8181ae49a445ca531
1import Foundation
2
3/// "Let agents make changes" and the log of every change an agent made. The app and the MCP process
4/// (the same executable run with --mcp) share the standard defaults and this log file.
5public enum AgentSettings {
6 public static let allowWritesKey = "jobley.agents.allowWrites"
7
8 public static func allowsWrites(_ defaults: UserDefaults = .standard) -> Bool { defaults.bool(forKey: allowWritesKey) }
9 public static func setAllowsWrites(_ value: Bool, _ defaults: UserDefaults = .standard) { defaults.set(value, forKey: allowWritesKey) }
10}
11
12public struct AgentLogEntry: Codable, Hashable, Identifiable, Sendable {
13 public var id = UUID()
14 public var at: Date
15 public var agent: String
16 public var tool: String
17 public var jobID: String
18 public var summary: String
19}
20
21public enum AgentLog {
22 public static var fileURL: URL {
23 if let path = ProcessInfo.processInfo.environment["JOBLEY_AGENT_LOG"] { return URL(fileURLWithPath: path) }
24 let folder = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appending(path: "Jobley")
25 return folder.appending(path: "agent-log.json")
26 }
27
28 public static func entries(at url: URL = fileURL) -> [AgentLogEntry] {
29 guard let data = try? Data(contentsOf: url) else { return [] }
30 let decoder = JSONDecoder(); decoder.dateDecodingStrategy = .iso8601
31 return (try? decoder.decode([AgentLogEntry].self, from: data)) ?? []
32 }
33
34 /// Newest first, at most 500 entries.
35 public static func append(_ entry: AgentLogEntry, at url: URL = fileURL) {
36 var all = entries(at: url)
37 all.insert(entry, at: 0)
38 if all.count > 500 { all.removeLast(all.count - 500) }
39 let encoder = JSONEncoder(); encoder.dateEncodingStrategy = .iso8601
40 try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
41 if let data = try? encoder.encode(all) { try? data.write(to: url, options: .atomic) }
42 }
43}