Chat UI: accept canonical main session key alias

This commit is contained in:
Mariano Belinky
2026-02-18 19:17:08 +00:00
committed by mbelinky
parent 6e7f1a6a1b
commit 2c22b2c3c3
2 changed files with 69 additions and 1 deletions

View File

@@ -447,7 +447,10 @@ public final class OpenClawChatViewModel {
// even when this view currently uses an alias key (for example "main").
// Never drop events for our own pending run on key mismatch, or the UI can stay
// stuck at "thinking" until the user reopens and forces a history reload.
if let sessionKey = chat.sessionKey, sessionKey != self.sessionKey, !isOurRun {
if let sessionKey = chat.sessionKey,
!Self.matchesCurrentSessionKey(incoming: sessionKey, current: self.sessionKey),
!isOurRun
{
return
}
if !isOurRun {
@@ -481,6 +484,21 @@ public final class OpenClawChatViewModel {
}
}
private static func matchesCurrentSessionKey(incoming: String, current: String) -> Bool {
let incomingNormalized = incoming.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
let currentNormalized = current.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
if incomingNormalized == currentNormalized {
return true
}
// Common alias pair in operator clients: UI uses "main" while gateway emits canonical.
if (incomingNormalized == "agent:main:main" && currentNormalized == "main") ||
(incomingNormalized == "main" && currentNormalized == "agent:main:main")
{
return true
}
return false
}
private func handleAgentEvent(_ evt: OpenClawAgentEventPayload) {
if let sessionId, evt.runId != sessionId {
return

View File

@@ -261,6 +261,56 @@ extension TestChatTransportState {
}
}
@Test func acceptsCanonicalSessionKeyEventsForExternalRuns() async throws {
let now = Date().timeIntervalSince1970 * 1000
let history1 = OpenClawChatHistoryPayload(
sessionKey: "main",
sessionId: "sess-main",
messages: [
AnyCodable([
"role": "user",
"content": [["type": "text", "text": "first"]],
"timestamp": now,
]),
],
thinkingLevel: "off")
let history2 = OpenClawChatHistoryPayload(
sessionKey: "main",
sessionId: "sess-main",
messages: [
AnyCodable([
"role": "user",
"content": [["type": "text", "text": "first"]],
"timestamp": now,
]),
AnyCodable([
"role": "assistant",
"content": [["type": "text", "text": "from external run"]],
"timestamp": now + 1,
]),
],
thinkingLevel: "off")
let transport = TestChatTransport(historyResponses: [history1, history2])
let vm = await MainActor.run { OpenClawChatViewModel(sessionKey: "main", transport: transport) }
await MainActor.run { vm.load() }
try await waitUntil("bootstrap") { await MainActor.run { vm.messages.count == 1 } }
transport.emit(
.chat(
OpenClawChatEventPayload(
runId: "external-run",
sessionKey: "agent:main:main",
state: "final",
message: nil,
errorMessage: nil)))
try await waitUntil("history refresh after canonical external event") {
await MainActor.run { vm.messages.count == 2 }
}
}
@Test func preservesMessageIDsAcrossHistoryRefreshes() async throws {
let now = Date().timeIntervalSince1970 * 1000
let history1 = OpenClawChatHistoryPayload(