Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

feat: add macOS file parsing utilities — PIDChecker, ClaudePaths, SessionFileParser, JSONLTailReader

libokai0efb7cfd5062
diff --git a/CodePulse-macOS/Services/SessionFileParser.swift b/CodePulse-macOS/Services/SessionFileParser.swift
new file mode 100644
index 00000000..9eef5dd8
--- /dev/null
+++ b/CodePulse-macOS/Services/SessionFileParser.swift
@@ -0,0 +1,98 @@
+import Foundation
+import CodePulseShared
+
+struct RawSessionFile: Codable {
+ let pid: Int
+ let sessionId: String
+ let cwd: String
+ let startedAt: Int64
+}
+
+struct SessionIndexEntry: Codable {
+ let sessionId: String
+ let firstPrompt: String?
+ let summary: String?
+ let messageCount: Int?
+ let gitBranch: String?
+ let projectPath: String?
+}
+
+struct SessionIndex: Codable {
+ let version: Int
+ let entries: [SessionIndexEntry]
+}
+
+struct RawTask: Codable {
+ let content: String?
+ let subject: String?
+ let status: String
+ let activeForm: String?
+ let id: String?
+ let description: String?
+}
+
+enum SessionFileParser {
+ static func parseSessionFiles() -> [RawSessionFile] {
+ let dir = ClaudePaths.sessionsDir
+ guard let files = try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)
+ .filter({ $0.pathExtension == "json" }) else { return [] }
+
+ return files.compactMap { url in
+ guard let data = try? Data(contentsOf: url),
+ let session = try? JSONDecoder().decode(RawSessionFile.self, from: data) else { return nil }
+ return session
+ }
+ }
+
+ static func parseTasks(sessionId: String) -> [TaskItem] {
+ let todoTasks = parseTodos(sessionId: sessionId)
+ if !todoTasks.isEmpty { return todoTasks }
+
+ // Fallback: check tasks directory for highwatermark
+ let dir = ClaudePaths.tasksPath(sessionId: sessionId)
+ let hwPath = dir.appendingPathComponent(".highwatermark")
+ guard let hwData = try? String(contentsOf: hwPath, encoding: .utf8),
+ let maxId = Int(hwData.trimmingCharacters(in: .whitespacesAndNewlines)) else {
+ return []
+ }
+
+ return (1...maxId).map {
+ TaskItem(id: "\($0)", content: "Task \($0)", status: .pending)
+ }
+ }
+
+ static func parseTodos(sessionId: String) -> [TaskItem] {
+ let dir = ClaudePaths.todosDir
+ guard let files = try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil) else { return [] }
+
+ let matching = files.filter { $0.lastPathComponent.hasPrefix(ClaudePaths.todosPattern(sessionId: sessionId)) }
+
+ for file in matching {
+ guard let data = try? Data(contentsOf: file),
+ let rawTasks = try? JSONDecoder().decode([RawTask].self, from: data),
+ !rawTasks.isEmpty else { continue }
+
+ return rawTasks.enumerated().map { index, raw in
+ let status: TaskStatus = switch raw.status {
+ case "completed": .completed
+ case "in_progress": .inProgress
+ default: .pending
+ }
+ return TaskItem(
+ id: raw.id ?? "\(index + 1)",
+ content: raw.subject ?? raw.content ?? "Task \(index + 1)",
+ status: status,
+ activeForm: raw.activeForm
+ )
+ }
+ }
+ return []
+ }
+
+ static func parseSessionIndex(cwd: String, sessionId: String) -> SessionIndexEntry? {
+ let path = ClaudePaths.sessionIndexPath(cwd: cwd)
+ guard let data = try? Data(contentsOf: path),
+ let index = try? JSONDecoder().decode(SessionIndex.self, from: data) else { return nil }
+ return index.entries.first { $0.sessionId == sessionId }
+ }
+}
diff --git a/CodePulse-macOS/Utilities/ClaudePaths.swift b/CodePulse-macOS/Utilities/ClaudePaths.swift
new file mode 100644
index 00000000..3411d4b1
--- /dev/null
+++ b/CodePulse-macOS/Utilities/ClaudePaths.swift
@@ -0,0 +1,36 @@
+import Foundation
+
+enum ClaudePaths {
+ static var claudeDir: URL {
+ FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".claude")
+ }
+
+ static var sessionsDir: URL { claudeDir.appendingPathComponent("sessions") }
+ static var tasksDir: URL { claudeDir.appendingPathComponent("tasks") }
+ static var todosDir: URL { claudeDir.appendingPathComponent("todos") }
+ static var projectsDir: URL { claudeDir.appendingPathComponent("projects") }
+
+ static func mangledCwd(_ cwd: String) -> String {
+ cwd.replacingOccurrences(of: "/", with: "-")
+ }
+
+ static func sessionIndexPath(cwd: String) -> URL {
+ projectsDir
+ .appendingPathComponent(mangledCwd(cwd))
+ .appendingPathComponent("sessions-index.json")
+ }
+
+ static func jsonlPath(cwd: String, sessionId: String) -> URL {
+ projectsDir
+ .appendingPathComponent(mangledCwd(cwd))
+ .appendingPathComponent("\(sessionId).jsonl")
+ }
+
+ static func tasksPath(sessionId: String) -> URL {
+ tasksDir.appendingPathComponent(sessionId)
+ }
+
+ static func todosPattern(sessionId: String) -> String {
+ "\(sessionId)-agent-"
+ }
+}
diff --git a/CodePulse-macOS/Utilities/JSONLTailReader.swift b/CodePulse-macOS/Utilities/JSONLTailReader.swift
new file mode 100644
index 00000000..4aa11d0a
--- /dev/null
+++ b/CodePulse-macOS/Utilities/JSONLTailReader.swift
@@ -0,0 +1,112 @@
+import Foundation
+
+struct JSONLUsage: Codable {
+ let inputTokens: Int?
+ let outputTokens: Int?
+ let cacheCreationInputTokens: Int?
+ let cacheReadInputTokens: Int?
+
+ enum CodingKeys: String, CodingKey {
+ case inputTokens = "input_tokens"
+ case outputTokens = "output_tokens"
+ case cacheCreationInputTokens = "cache_creation_input_tokens"
+ case cacheReadInputTokens = "cache_read_input_tokens"
+ }
+}
+
+struct JSONLMessage: Codable {
+ let role: String?
+ let model: String?
+ let usage: JSONLUsage?
+}
+
+struct JSONLEntry: Codable {
+ let type: String?
+ let message: JSONLMessage?
+ let timestamp: String?
+}
+
+struct JSONLSessionInfo {
+ var model: String = "Unknown"
+ var totalInputTokens: Int = 0
+ var totalOutputTokens: Int = 0
+ var totalCacheReadTokens: Int = 0
+ var lastUpdateTime: Date?
+
+ var contextPct: Int {
+ let contextWindowSize = modelContextSize
+ guard contextWindowSize > 0 else { return 0 }
+ let used = totalInputTokens + totalCacheReadTokens
+ return min(100, (used * 100) / contextWindowSize)
+ }
+
+ var costUSD: Double {
+ let (inputRate, outputRate) = modelPricing
+ let inputCost = Double(totalInputTokens) * inputRate / 1_000_000
+ let outputCost = Double(totalOutputTokens) * outputRate / 1_000_000
+ return inputCost + outputCost
+ }
+
+ private var modelContextSize: Int {
+ if model.contains("opus") { return 200_000 }
+ if model.contains("sonnet") { return 200_000 }
+ if model.contains("haiku") { return 200_000 }
+ return 200_000
+ }
+
+ private var modelPricing: (Double, Double) {
+ if model.contains("opus") { return (15.0, 75.0) }
+ if model.contains("sonnet") { return (3.0, 15.0) }
+ if model.contains("haiku") { return (0.25, 1.25) }
+ return (3.0, 15.0)
+ }
+}
+
+enum JSONLTailReader {
+ static func readTail(url: URL, lineCount: Int = 50) -> [String] {
+ guard let fileHandle = try? FileHandle(forReadingFrom: url) else { return [] }
+ defer { try? fileHandle.close() }
+
+ let fileSize = fileHandle.seekToEndOfFile()
+ guard fileSize > 0 else { return [] }
+
+ let chunkSize = min(fileSize, UInt64(lineCount * 500))
+ fileHandle.seek(toFileOffset: fileSize - chunkSize)
+
+ guard let data = try? fileHandle.readToEnd(),
+ let text = String(data: data, encoding: .utf8) else { return [] }
+
+ let lines = text.components(separatedBy: "\n").filter { !$0.isEmpty }
+ return Array(lines.suffix(lineCount))
+ }
+
+ static func extractInfo(url: URL) -> JSONLSessionInfo {
+ let lines = readTail(url: url)
+ var info = JSONLSessionInfo()
+
+ let decoder = JSONDecoder()
+ for line in lines {
+ guard let data = line.data(using: .utf8),
+ let entry = try? decoder.decode(JSONLEntry.self, from: data) else { continue }
+
+ if let message = entry.message {
+ if let model = message.model, !model.isEmpty {
+ info.model = model
+ }
+ if let usage = message.usage {
+ info.totalInputTokens += usage.inputTokens ?? 0
+ info.totalOutputTokens += usage.outputTokens ?? 0
+ info.totalCacheReadTokens += usage.cacheReadInputTokens ?? 0
+ }
+ }
+ }
+
+ return info
+ }
+
+ static func fileSize(url: URL) -> UInt64? {
+ guard let attrs = try? FileManager.default.attributesOfItem(atPath: url.path),
+ let size = attrs[.size] as? UInt64 else { return nil }
+ return size
+ }
+}
diff --git a/CodePulse-macOS/Utilities/PIDChecker.swift b/CodePulse-macOS/Utilities/PIDChecker.swift
new file mode 100644
index 00000000..98f017f3
--- /dev/null
+++ b/CodePulse-macOS/Utilities/PIDChecker.swift
@@ -0,0 +1,7 @@
+import Foundation
+
+enum PIDChecker {
+ static func isAlive(pid: Int) -> Bool {
+ kill(Int32(pid), 0) == 0
+ }
+}
diff --git a/CodePulse.xcodeproj/project.pbxproj b/CodePulse.xcodeproj/project.pbxproj
index c31de435..3d75eace 100644
--- a/CodePulse.xcodeproj/project.pbxproj
+++ b/CodePulse.xcodeproj/project.pbxproj
@@ -19,6 +19,10 @@
E36D87A2D2D0F1177F263772 /* CodePulseLiveActivityBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BCD1168005B123A7F9ACB37 /* CodePulseLiveActivityBundle.swift */; };
EB132AF39AB968D2DFA6151B /* CodePulseApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A67A3CD19408484AB11BB73 /* CodePulseApp.swift */; };
F4F54F91459EE6A15DD7B246 /* CodePulseIOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8453D8BE6EE838E1B9EA4588 /* CodePulseIOSApp.swift */; };
+ C1C1204B22A14AEC955654F0 /* PIDChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47E9EBDBE2D94F718632D454 /* PIDChecker.swift */; };
+ E3C8E587447D41AE9F8BF0CF /* ClaudePaths.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA494301693D443F8FD797D7 /* ClaudePaths.swift */; };
+ CE2560F773384F74989BD043 /* SessionFileParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6B15463A7EE485B8B6C304B /* SessionFileParser.swift */; };
+ 311FECFF65054BA2B657AF93 /* JSONLTailReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7582EF7C72A74D8F90599D0D /* JSONLTailReader.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -60,6 +64,10 @@
9580D32B5B29E049090D66C8 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
CD5A99B72DC7E1F01D755DF6 /* CodePulse.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CodePulse.entitlements; sourceTree = "<group>"; };
F033CA1408917B1655BC66D2 /* CodePulseShared */ = {isa = PBXFileReference; lastKnownFileType = folder; name = CodePulseShared; path = CodePulseShared; sourceTree = SOURCE_ROOT; };
+ 47E9EBDBE2D94F718632D454 /* PIDChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIDChecker.swift; sourceTree = "<group>"; };
+ CA494301693D443F8FD797D7 /* ClaudePaths.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClaudePaths.swift; sourceTree = "<group>"; };
+ B6B15463A7EE485B8B6C304B /* SessionFileParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionFileParser.swift; sourceTree = "<group>"; };
+ 7582EF7C72A74D8F90599D0D /* JSONLTailReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONLTailReader.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -143,6 +151,9 @@
3FCF1C54EE1B71553A70301E /* Utilities */ = {
isa = PBXGroup;
children = (
+ 47E9EBDBE2D94F718632D454 /* PIDChecker.swift */,
+ CA494301693D443F8FD797D7 /* ClaudePaths.swift */,
+ 7582EF7C72A74D8F90599D0D /* JSONLTailReader.swift */,
);
path = Utilities;
sourceTree = "<group>";
@@ -183,6 +194,7 @@
BF668ADCDAD9595E867B23D0 /* Services */ = {
isa = PBXGroup;
children = (
+ B6B15463A7EE485B8B6C304B /* SessionFileParser.swift */,
);
path = Services;
sourceTree = "<group>";
@@ -338,6 +350,10 @@
files = (
EB132AF39AB968D2DFA6151B /* CodePulseApp.swift in Sources */,
3037E7D95514810851C96989 /* ContentView.swift in Sources */,
+ C1C1204B22A14AEC955654F0 /* PIDChecker.swift in Sources */,
+ E3C8E587447D41AE9F8BF0CF /* ClaudePaths.swift in Sources */,
+ CE2560F773384F74989BD043 /* SessionFileParser.swift in Sources */,
+ 311FECFF65054BA2B657AF93 /* JSONLTailReader.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};