Skip to content

Commit 94052b0

Browse files
committed
fix(macos): preserve session kind metadata
1 parent 7e4eeb9 commit 94052b0

4 files changed

Lines changed: 30 additions & 91 deletions

File tree

‎apps/macos/Sources/OpenClaw/SessionData.swift‎

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,6 @@ import Foundation
22
import OpenClawChatUI
33
import SwiftUI
44

5-
struct GatewaySessionDefaultsRecord: Codable {
6-
let model: String?
7-
let contextTokens: Int?
8-
}
9-
10-
struct GatewaySessionEntryRecord: Codable {
11-
let key: String
12-
let displayName: String?
13-
let provider: String?
14-
let subject: String?
15-
let room: String?
16-
let space: String?
17-
let updatedAt: Double?
18-
let sessionId: String?
19-
let systemSent: Bool?
20-
let abortedLastRun: Bool?
21-
let thinkingLevel: String?
22-
let verboseLevel: String?
23-
let inputTokens: Int?
24-
let outputTokens: Int?
25-
let totalTokens: Int?
26-
let model: String?
27-
let contextTokens: Int?
28-
}
29-
30-
struct GatewaySessionsListResponse: Codable {
31-
let ts: Double?
32-
let path: String
33-
let count: Int
34-
let defaults: GatewaySessionDefaultsRecord?
35-
let sessions: [GatewaySessionEntryRecord]
36-
}
37-
385
struct SessionTokenStats {
396
let input: Int
407
let output: Int
@@ -63,10 +30,6 @@ struct SessionRow: Identifiable {
6330
let key: String
6431
let kind: SessionKind
6532
let displayName: String?
66-
let provider: String?
67-
let subject: String?
68-
let room: String?
69-
let space: String?
7033
let updatedAt: Date?
7134
let sessionId: String?
7235
let thinkingLevel: String?
@@ -94,19 +57,12 @@ struct SessionRow: Identifiable {
9457
}
9558
}
9659

97-
enum SessionKind {
60+
enum SessionKind: String {
9861
case cron, direct, group, global, unknown
9962

100-
static func from(key: String) -> SessionKind {
101-
if key == "global" { return .global }
102-
let parts = key.lowercased().split(separator: ":").filter { !$0.isEmpty }
103-
if parts.first == "cron" { return .cron }
104-
if parts.count >= 3, parts[0] == "agent", parts[2] == "cron" { return .cron }
105-
if key.hasPrefix("group:") { return .group }
106-
if key.contains(":group:") { return .group }
107-
if key.contains(":channel:") { return .group }
108-
if key == "unknown" { return .unknown }
109-
return .direct
63+
static func from(_ entry: OpenClawChatSessionEntry) -> SessionKind {
64+
if entry.classification == cron.rawValue { return .cron }
65+
return entry.kind.flatMap(Self.init(rawValue:)) ?? .unknown
11066
}
11167

11268
var label: String {
@@ -143,10 +99,6 @@ extension SessionRow {
14399
144100
kind: .direct,
145101
displayName: nil,
146-
provider: nil,
147-
subject: nil,
148-
room: nil,
149-
space: nil,
150102
updatedAt: Date().addingTimeInterval(-90),
151103
sessionId: "sess-direct-1234",
152104
thinkingLevel: "low",
@@ -160,10 +112,6 @@ extension SessionRow {
160112
key: "discord:channel:release-squad",
161113
kind: .group,
162114
displayName: "discord:#release-squad",
163-
provider: "discord",
164-
subject: nil,
165-
room: "#release-squad",
166-
space: nil,
167115
updatedAt: Date().addingTimeInterval(-3600),
168116
sessionId: "sess-group-4321",
169117
thinkingLevel: "medium",
@@ -177,10 +125,6 @@ extension SessionRow {
177125
key: "global",
178126
kind: .global,
179127
displayName: nil,
180-
provider: nil,
181-
subject: nil,
182-
room: nil,
183-
space: nil,
184128
updatedAt: Date().addingTimeInterval(-86400),
185129
sessionId: nil,
186130
thinkingLevel: nil,
@@ -248,12 +192,15 @@ enum SessionLoader {
248192
throw SessionLoadError.gatewayUnavailable(msg)
249193
}
250194

251-
let decoded: GatewaySessionsListResponse
195+
let decoded: OpenClawChatSessionsListResponse
252196
do {
253-
decoded = try JSONDecoder().decode(GatewaySessionsListResponse.self, from: data)
197+
decoded = try JSONDecoder().decode(OpenClawChatSessionsListResponse.self, from: data)
254198
} catch {
255199
throw SessionLoadError.decodeFailed(error.localizedDescription)
256200
}
201+
guard let storePath = decoded.path else {
202+
throw SessionLoadError.decodeFailed("Missing session store path.")
203+
}
257204

258205
let defaults = SessionDefaults(
259206
model: decoded.defaults?.model ?? self.fallbackModel,
@@ -270,12 +217,8 @@ enum SessionLoader {
270217
return SessionRow(
271218
id: entry.key,
272219
key: entry.key,
273-
kind: SessionKind.from(key: entry.key),
220+
kind: SessionKind.from(entry),
274221
displayName: entry.displayName,
275-
provider: entry.provider,
276-
subject: entry.subject,
277-
room: entry.room,
278-
space: entry.space,
279222
updatedAt: updated,
280223
sessionId: entry.sessionId,
281224
thinkingLevel: entry.thinkingLevel,
@@ -290,7 +233,7 @@ enum SessionLoader {
290233
model: model)
291234
}.sorted { ($0.updatedAt ?? .distantPast) > ($1.updatedAt ?? .distantPast) }
292235

293-
return SessionStoreSnapshot(storePath: decoded.path, defaults: defaults, rows: rows)
236+
return SessionStoreSnapshot(storePath: storePath, defaults: defaults, rows: rows)
294237
}
295238

296239
private static func standardize(_ path: String) -> String {

‎apps/macos/Tests/OpenClawIPCTests/MenuSessionsInjectorTests.swift‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ struct MenuSessionsInjectorTests {
5151
key: "main",
5252
kind: .direct,
5353
displayName: nil,
54-
provider: nil,
55-
subject: nil,
56-
room: nil,
57-
space: nil,
5854
updatedAt: Date(),
5955
sessionId: "s1",
6056
thinkingLevel: "low",
@@ -68,10 +64,6 @@ struct MenuSessionsInjectorTests {
6864
key: "discord:group:alpha",
6965
kind: .group,
7066
displayName: nil,
71-
provider: nil,
72-
subject: nil,
73-
room: nil,
74-
space: nil,
7567
updatedAt: Date(timeIntervalSinceNow: -60),
7668
sessionId: "s2",
7769
thinkingLevel: "high",

‎apps/macos/Tests/OpenClawIPCTests/QuickChatRecentsTests.swift‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ struct QuickChatRecentsTests {
5858
key: key,
5959
kind: .direct,
6060
displayName: displayName,
61-
provider: nil,
62-
subject: nil,
63-
room: nil,
64-
space: nil,
6561
updatedAt: updatedAt,
6662
sessionId: nil,
6763
thinkingLevel: nil,

‎apps/macos/Tests/OpenClawIPCTests/SessionDataTests.swift‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import Foundation
2+
import OpenClawChatUI
23
import Testing
34
@testable import OpenClaw
45

56
struct SessionDataTests {
6-
@Test func `session kind from key detects common kinds`() {
7-
#expect(SessionKind.from(key: "global") == .global)
8-
#expect(SessionKind.from(key: "cron:daily") == .cron)
9-
#expect(SessionKind.from(key: "agent:main:cron:daily") == .cron)
10-
#expect(SessionKind.from(key: "discord:group:engineering") == .group)
11-
#expect(SessionKind.from(key: "unknown") == .unknown)
12-
#expect(SessionKind.from(key: "[email protected]") == .direct)
7+
@Test func `session kinds follow authoritative gateway metadata`() throws {
8+
let response = try JSONDecoder().decode(
9+
OpenClawChatSessionsListResponse.self,
10+
from: Data("""
11+
{"path":"synthetic.sqlite","sessions":[
12+
{"key":"provider-owned-room-key","kind":"group","classification":"group"},
13+
{"key":"opaque-scheduled-task","kind":"direct","classification":"cron"},
14+
{"key":"future-session","kind":"future"},
15+
{"key":"missing-session-kind"},
16+
{"key":"opaque-direct","kind":"direct"},
17+
{"key":"opaque-global","kind":"global"},
18+
{"key":"opaque-unknown","kind":"unknown"}
19+
]}
20+
""".utf8))
21+
22+
#expect(response.sessions.map(SessionKind.from) == [
23+
.group, .cron, .unknown, .unknown, .direct, .global, .unknown,
24+
])
1325
}
1426

1527
@Test func `session token stats format K tokens rounds as expected`() {
@@ -29,10 +41,6 @@ struct SessionDataTests {
2941
3042
kind: .direct,
3143
displayName: nil,
32-
provider: nil,
33-
subject: nil,
34-
room: nil,
35-
space: nil,
3644
updatedAt: Date(),
3745
sessionId: nil,
3846
thinkingLevel: "high",

0 commit comments

Comments
 (0)