fix: bridge gateway anycodable payloads
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
|
import ClawdbotKit
|
||||||
import ClawdbotProtocol
|
import ClawdbotProtocol
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
// Prefer the ClawdbotKit wrapper to keep gateway request payloads consistent.
|
||||||
|
typealias AnyCodable = ClawdbotKit.AnyCodable
|
||||||
|
typealias InstanceIdentity = ClawdbotKit.InstanceIdentity
|
||||||
|
|
||||||
extension AnyCodable {
|
extension AnyCodable {
|
||||||
var stringValue: String? { self.value as? String }
|
var stringValue: String? { self.value as? String }
|
||||||
var boolValue: Bool? { self.value as? Bool }
|
var boolValue: Bool? { self.value as? Bool }
|
||||||
@@ -20,3 +25,23 @@ extension AnyCodable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension ClawdbotProtocol.AnyCodable {
|
||||||
|
var stringValue: String? { self.value as? String }
|
||||||
|
var boolValue: Bool? { self.value as? Bool }
|
||||||
|
var intValue: Int? { self.value as? Int }
|
||||||
|
var doubleValue: Double? { self.value as? Double }
|
||||||
|
var dictionaryValue: [String: ClawdbotProtocol.AnyCodable]? { self.value as? [String: ClawdbotProtocol.AnyCodable] }
|
||||||
|
var arrayValue: [ClawdbotProtocol.AnyCodable]? { self.value as? [ClawdbotProtocol.AnyCodable] }
|
||||||
|
|
||||||
|
var foundationValue: Any {
|
||||||
|
switch self.value {
|
||||||
|
case let dict as [String: ClawdbotProtocol.AnyCodable]:
|
||||||
|
dict.mapValues { $0.foundationValue }
|
||||||
|
case let array as [ClawdbotProtocol.AnyCodable]:
|
||||||
|
array.map(\.foundationValue)
|
||||||
|
default:
|
||||||
|
self.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -346,7 +346,7 @@ final class ControlChannel {
|
|||||||
let phase = event.data["phase"]?.value as? String ?? ""
|
let phase = event.data["phase"]?.value as? String ?? ""
|
||||||
let name = event.data["name"]?.value as? String
|
let name = event.data["name"]?.value as? String
|
||||||
let meta = event.data["meta"]?.value as? String
|
let meta = event.data["meta"]?.value as? String
|
||||||
let args = event.data["args"]?.value as? [String: AnyCodable]
|
let args = Self.bridgeToProtocolArgs(event.data["args"])
|
||||||
WorkActivityStore.shared.handleTool(
|
WorkActivityStore.shared.handleTool(
|
||||||
sessionKey: sessionKey,
|
sessionKey: sessionKey,
|
||||||
phase: phase,
|
phase: phase,
|
||||||
@@ -357,6 +357,27 @@ final class ControlChannel {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static func bridgeToProtocolArgs(
|
||||||
|
_ value: AnyCodable?) -> [String: ClawdbotProtocol.AnyCodable]?
|
||||||
|
{
|
||||||
|
guard let value else { return nil }
|
||||||
|
if let dict = value.value as? [String: ClawdbotProtocol.AnyCodable] {
|
||||||
|
return dict
|
||||||
|
}
|
||||||
|
if let dict = value.value as? [String: AnyCodable],
|
||||||
|
let data = try? JSONEncoder().encode(dict),
|
||||||
|
let decoded = try? JSONDecoder().decode([String: ClawdbotProtocol.AnyCodable].self, from: data)
|
||||||
|
{
|
||||||
|
return decoded
|
||||||
|
}
|
||||||
|
if let data = try? JSONEncoder().encode(value),
|
||||||
|
let decoded = try? JSONDecoder().decode([String: ClawdbotProtocol.AnyCodable].self, from: data)
|
||||||
|
{
|
||||||
|
return decoded
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Notification.Name {
|
extension Notification.Name {
|
||||||
|
|||||||
@@ -244,9 +244,9 @@ actor GatewayConnection {
|
|||||||
return trimmed.isEmpty ? nil : trimmed
|
return trimmed.isEmpty ? nil : trimmed
|
||||||
}
|
}
|
||||||
|
|
||||||
private func sessionDefaultString(_ defaults: [String: AnyCodable]?, key: String) -> String {
|
private func sessionDefaultString(_ defaults: [String: ClawdbotProtocol.AnyCodable]?, key: String) -> String {
|
||||||
(defaults?[key]?.stringValue ?? "")
|
let raw = defaults?[key]?.value as? String
|
||||||
.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
return (raw ?? "").trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cachedMainSessionKey() -> String? {
|
func cachedMainSessionKey() -> String? {
|
||||||
|
|||||||
@@ -29,5 +29,10 @@ public struct GatewayDecodingError: LocalizedError, Sendable {
|
|||||||
public let method: String
|
public let method: String
|
||||||
public let message: String
|
public let message: String
|
||||||
|
|
||||||
|
public init(method: String, message: String) {
|
||||||
|
self.method = method
|
||||||
|
self.message = message
|
||||||
|
}
|
||||||
|
|
||||||
public var errorDescription: String? { "\(self.method): \(self.message)" }
|
public var errorDescription: String? { "\(self.method): \(self.message)" }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user