mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-10 17:36:09 +00:00
The shared chat session sheet (iOS chat + macOS webchat) gains server-backed search (sessions.list search param, 250ms debounce, cancellation-safe local fallback when offline), an Active/Archived scope, swipe/context actions for pin/rename/archive with optimistic updates and rollback, pin indicators, and restore-on-open for archived rows. Archiving the open session switches back to the main session so the composer never points at a send-rejecting session. OpenClawChatTransport consolidates the two list requirements into canonical listSessions(limit:search:archived:) with forwarding sugars (unreleased internal API; all in-repo conformers updated: iOS gateway transport, demo and fixture transports, previews, macOS webchat, test fakes). macOS webchat also implements patchSession, giving the sheet's controls a live transport there. OpenClawChatSessionEntry becomes var-based and gains pinnedAt/archivedAt; the two full-init rebuild blocks in ChatViewModel collapse to copy-mutation (the pattern that silently dropped newly added fields), preserving main's model-identity/thinking-metadata semantics. A shared session list organizer mirrors gateway ordering (pinnedAt desc, updatedAt desc, key) for cached and offline lists, and pinned sessions survive the 24h recency cutoff in the session picker. Refs #100712
369 lines
14 KiB
Swift
369 lines
14 KiB
Swift
import Foundation
|
|
|
|
public enum OpenClawChatTransportEvent: Sendable {
|
|
case health(ok: Bool)
|
|
case tick
|
|
case chat(OpenClawChatEventPayload)
|
|
case sessionMessage(OpenClawSessionMessageEventPayload)
|
|
case agent(OpenClawAgentEventPayload)
|
|
case seqGap
|
|
}
|
|
|
|
/// One immutable transport route used by an entire outbox flush. Route-aware
|
|
/// transports bind both sends and confirmation reads to the same connection;
|
|
/// a gateway switch then cancels the old work instead of retargeting it.
|
|
public struct OpenClawChatTransportRouteLease: Sendable {
|
|
public typealias SendMessage = @Sendable (
|
|
_ sessionKey: String,
|
|
_ message: String,
|
|
_ thinking: String,
|
|
_ idempotencyKey: String,
|
|
_ attachments: [OpenClawChatAttachmentPayload]) async throws -> OpenClawChatSendResponse
|
|
public typealias RequestHistory = @Sendable (String) async throws -> OpenClawChatHistoryPayload
|
|
public typealias SendTargetedMessage = @Sendable (
|
|
_ sessionKey: String,
|
|
_ agentID: String?,
|
|
_ message: String,
|
|
_ thinking: String,
|
|
_ idempotencyKey: String,
|
|
_ attachments: [OpenClawChatAttachmentPayload]) async throws -> OpenClawChatSendResponse
|
|
public typealias RequestTargetedHistory = @Sendable (
|
|
_ sessionKey: String,
|
|
_ agentID: String?) async throws -> OpenClawChatHistoryPayload
|
|
|
|
private let sendTargetedMessageImpl: SendTargetedMessage
|
|
private let requestTargetedHistoryImpl: RequestTargetedHistory
|
|
public let sessionRoutingContract: String?
|
|
|
|
public init(
|
|
sendMessage: @escaping SendMessage,
|
|
requestHistory: @escaping RequestHistory,
|
|
sessionRoutingContract: String? = nil)
|
|
{
|
|
self.sessionRoutingContract = sessionRoutingContract
|
|
self.sendTargetedMessageImpl = { sessionKey, _, message, thinking, idempotencyKey, attachments in
|
|
try await sendMessage(sessionKey, message, thinking, idempotencyKey, attachments)
|
|
}
|
|
self.requestTargetedHistoryImpl = { sessionKey, _ in
|
|
try await requestHistory(sessionKey)
|
|
}
|
|
}
|
|
|
|
public init(
|
|
sendTargetedMessage: @escaping SendTargetedMessage,
|
|
requestTargetedHistory: @escaping RequestTargetedHistory,
|
|
sessionRoutingContract: String? = nil)
|
|
{
|
|
self.sessionRoutingContract = sessionRoutingContract
|
|
self.sendTargetedMessageImpl = sendTargetedMessage
|
|
self.requestTargetedHistoryImpl = requestTargetedHistory
|
|
}
|
|
|
|
public func sendMessage(
|
|
sessionKey: String,
|
|
agentID: String? = nil,
|
|
message: String,
|
|
thinking: String,
|
|
idempotencyKey: String,
|
|
attachments: [OpenClawChatAttachmentPayload]) async throws -> OpenClawChatSendResponse
|
|
{
|
|
try await self.sendTargetedMessageImpl(
|
|
sessionKey,
|
|
agentID,
|
|
message,
|
|
thinking,
|
|
idempotencyKey,
|
|
attachments)
|
|
}
|
|
|
|
public func requestHistory(
|
|
sessionKey: String,
|
|
agentID: String? = nil) async throws -> OpenClawChatHistoryPayload
|
|
{
|
|
try await self.requestTargetedHistoryImpl(sessionKey, agentID)
|
|
}
|
|
}
|
|
|
|
public enum OpenClawChatTransportRouteLeaseResult: Sendable {
|
|
case available(OpenClawChatTransportRouteLease)
|
|
case unavailable(reason: String?)
|
|
}
|
|
|
|
/// The transport rejected a send before it reached its request channel. This
|
|
/// is the only failure class safe for automatic outbox retry.
|
|
public enum OpenClawChatTransportSendError: Error, Sendable {
|
|
case notDispatched
|
|
}
|
|
|
|
public enum OpenClawChatTransportUpgradeMessage {
|
|
public static let routingContract =
|
|
"Update the gateway before sending queued messages. This version requires safe delivery routing."
|
|
}
|
|
|
|
public protocol OpenClawChatTransport: Sendable {
|
|
func createSession(
|
|
key: String,
|
|
label: String?,
|
|
parentSessionKey: String?,
|
|
worktree: Bool?) async throws -> OpenClawChatCreateSessionResponse
|
|
|
|
func requestHistory(sessionKey: String) async throws -> OpenClawChatHistoryPayload
|
|
func listModels() async throws -> [OpenClawChatModelChoice]
|
|
var supportsSlashCommandCatalog: Bool { get }
|
|
func listCommands(sessionKey: String) async throws -> [OpenClawChatCommandChoice]
|
|
func sendMessage(
|
|
sessionKey: String,
|
|
message: String,
|
|
thinking: String,
|
|
idempotencyKey: String,
|
|
attachments: [OpenClawChatAttachmentPayload]) async throws -> OpenClawChatSendResponse
|
|
func sendMessage(
|
|
sessionKey: String,
|
|
agentID: String?,
|
|
expectedSessionRoutingContract: String?,
|
|
message: String,
|
|
thinking: String,
|
|
idempotencyKey: String,
|
|
attachments: [OpenClawChatAttachmentPayload]) async throws -> OpenClawChatSendResponse
|
|
|
|
/// Captures the current route for a durable outbox flush. Implementations
|
|
/// backed by a mutable gateway must override this with route-checked calls.
|
|
func acquireOutboxRouteLease() async -> OpenClawChatTransportRouteLeaseResult
|
|
var outboxRequiresSessionRoutingContract: Bool { get }
|
|
|
|
func abortRun(sessionKey: String, runId: String) async throws
|
|
func listSessions(
|
|
limit: Int?,
|
|
search: String?,
|
|
archived: Bool) async throws -> OpenClawChatSessionsListResponse
|
|
func patchSession(
|
|
key: String,
|
|
label: String??,
|
|
category: String??,
|
|
pinned: Bool?,
|
|
archived: Bool?,
|
|
unread: Bool?) async throws
|
|
func deleteSession(key: String) async throws
|
|
func forkSession(parentKey: String) async throws -> String
|
|
func setSessionModel(sessionKey: String, model: String?) async throws
|
|
func setSessionThinking(sessionKey: String, thinkingLevel: String) async throws
|
|
|
|
func requestHealth(timeoutMs: Int) async throws -> Bool
|
|
func waitForRunCompletion(runId: String, timeoutMs: Int) async -> Bool
|
|
func events() -> AsyncStream<OpenClawChatTransportEvent>
|
|
|
|
func setActiveSessionKey(_ sessionKey: String) async throws
|
|
func resetSession(sessionKey: String) async throws
|
|
func compactSession(sessionKey: String) async throws
|
|
}
|
|
|
|
extension OpenClawChatTransport {
|
|
public var outboxRequiresSessionRoutingContract: Bool {
|
|
false
|
|
}
|
|
|
|
public func acquireOutboxRouteLease() async -> OpenClawChatTransportRouteLeaseResult {
|
|
let transport = self
|
|
return .available(OpenClawChatTransportRouteLease(
|
|
sendMessage: { sessionKey, message, thinking, idempotencyKey, attachments in
|
|
try await transport.sendMessage(
|
|
sessionKey: sessionKey,
|
|
message: message,
|
|
thinking: thinking,
|
|
idempotencyKey: idempotencyKey,
|
|
attachments: attachments)
|
|
},
|
|
requestHistory: { sessionKey in
|
|
try await transport.requestHistory(sessionKey: sessionKey)
|
|
}))
|
|
}
|
|
|
|
public func sendMessage(
|
|
sessionKey: String,
|
|
agentID _: String?,
|
|
expectedSessionRoutingContract _: String?,
|
|
message: String,
|
|
thinking: String,
|
|
idempotencyKey: String,
|
|
attachments: [OpenClawChatAttachmentPayload]) async throws -> OpenClawChatSendResponse
|
|
{
|
|
try await self.sendMessage(
|
|
sessionKey: sessionKey,
|
|
message: message,
|
|
thinking: thinking,
|
|
idempotencyKey: idempotencyKey,
|
|
attachments: attachments)
|
|
}
|
|
|
|
public func createSession(
|
|
key _: String,
|
|
label _: String?,
|
|
parentSessionKey _: String?,
|
|
worktree _: Bool?) async throws -> OpenClawChatCreateSessionResponse
|
|
{
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.create not supported by this transport"])
|
|
}
|
|
|
|
public func setActiveSessionKey(_: String) async throws {}
|
|
|
|
public func waitForRunCompletion(runId _: String, timeoutMs _: Int) async -> Bool {
|
|
false
|
|
}
|
|
|
|
public func resetSession(sessionKey _: String) async throws {
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.reset not supported by this transport"])
|
|
}
|
|
|
|
public func compactSession(sessionKey _: String) async throws {
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.compact not supported by this transport"])
|
|
}
|
|
|
|
public func abortRun(sessionKey _: String, runId _: String) async throws {
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "chat.abort not supported by this transport"])
|
|
}
|
|
|
|
public func listSessions(
|
|
limit _: Int?,
|
|
search _: String?,
|
|
archived _: Bool) async throws -> OpenClawChatSessionsListResponse
|
|
{
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.list not supported by this transport"])
|
|
}
|
|
|
|
/// Conveniences for callers that only page a list. Transports must
|
|
/// implement the canonical `listSessions(limit:search:archived:)`
|
|
/// requirement; same-name methods on a conformer are shadowed by these
|
|
/// sugars and never called through the protocol.
|
|
public func listSessions(limit: Int?) async throws -> OpenClawChatSessionsListResponse {
|
|
try await self.listSessions(limit: limit, search: nil, archived: false)
|
|
}
|
|
|
|
public func listSessions(limit: Int?, archived: Bool) async throws -> OpenClawChatSessionsListResponse {
|
|
try await self.listSessions(limit: limit, search: nil, archived: archived)
|
|
}
|
|
|
|
public func patchSession(
|
|
key _: String,
|
|
label _: String?? = nil,
|
|
category _: String?? = nil,
|
|
pinned _: Bool? = nil,
|
|
archived _: Bool? = nil,
|
|
unread _: Bool? = nil) async throws
|
|
{
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.patch not supported by this transport"])
|
|
}
|
|
|
|
public func deleteSession(key _: String) async throws {
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.delete not supported by this transport"])
|
|
}
|
|
|
|
public func forkSession(parentKey _: String) async throws -> String {
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.create fork not supported by this transport"])
|
|
}
|
|
|
|
public func listModels() async throws -> [OpenClawChatModelChoice] {
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "models.list not supported by this transport"])
|
|
}
|
|
|
|
public var supportsSlashCommandCatalog: Bool {
|
|
false
|
|
}
|
|
|
|
public func listCommands(sessionKey _: String) async throws -> [OpenClawChatCommandChoice] {
|
|
[]
|
|
}
|
|
|
|
public func setSessionModel(sessionKey _: String, model _: String?) async throws {
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.patch(model) not supported by this transport"])
|
|
}
|
|
|
|
public func setSessionThinking(sessionKey _: String, thinkingLevel _: String) async throws {
|
|
throw NSError(
|
|
domain: "OpenClawChatTransport",
|
|
code: 0,
|
|
userInfo: [NSLocalizedDescriptionKey: "sessions.patch(thinkingLevel) not supported by this transport"])
|
|
}
|
|
}
|
|
|
|
public enum OpenClawChatSessionRoutingContract {
|
|
public static let changedErrorReason = "session-routing-changed"
|
|
|
|
public struct Components: Equatable, Sendable {
|
|
public let scope: String
|
|
public let mainKey: String
|
|
public let defaultAgentID: String
|
|
}
|
|
|
|
/// Live sends may proceed before routing identity is available. Queued
|
|
/// replay acquires a separate route lease and never uses a nil contract.
|
|
public static func expectedValue(
|
|
_ contract: String?,
|
|
serverSupportsGuard: Bool) -> String?
|
|
{
|
|
guard serverSupportsGuard else { return nil }
|
|
return self.normalize(contract)
|
|
}
|
|
|
|
public static func make(
|
|
scope: String?,
|
|
mainKey: String?,
|
|
defaultAgentID: String?) -> String?
|
|
{
|
|
let normalizedScope = self.normalize(scope)
|
|
let normalizedMainKey = self.normalize(mainKey)
|
|
let normalizedDefaultAgentID = self.normalize(defaultAgentID)
|
|
guard let normalizedScope, let normalizedMainKey, let normalizedDefaultAgentID else { return nil }
|
|
return "\(normalizedScope)|\(normalizedMainKey)|\(normalizedDefaultAgentID)"
|
|
}
|
|
|
|
/// Scope and agent ids cannot contain `|`; parse from both ends so an
|
|
/// older custom main key containing the delimiter still round-trips.
|
|
public static func parse(_ contract: String?) -> Components? {
|
|
guard let normalized = self.normalize(contract),
|
|
let firstSeparator = normalized.firstIndex(of: "|"),
|
|
let lastSeparator = normalized.lastIndex(of: "|"),
|
|
firstSeparator != lastSeparator
|
|
else { return nil }
|
|
let scope = String(normalized[..<firstSeparator])
|
|
let mainKey = String(normalized[normalized.index(after: firstSeparator)..<lastSeparator])
|
|
let defaultAgentID = String(normalized[normalized.index(after: lastSeparator)...])
|
|
guard !scope.isEmpty, !mainKey.isEmpty, !defaultAgentID.isEmpty else { return nil }
|
|
return Components(scope: scope, mainKey: mainKey, defaultAgentID: defaultAgentID)
|
|
}
|
|
|
|
private static func normalize(_ value: String?) -> String? {
|
|
let normalized = value?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
|
|
return normalized?.isEmpty == false ? normalized : nil
|
|
}
|
|
}
|