mac: fix gateway hello types
This commit is contained in:
@@ -54,50 +54,55 @@ private actor GatewayChannelActor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func sendHello() async throws {
|
private func sendHello() async throws {
|
||||||
let hello: [String: Any] = [
|
let hello = Hello(
|
||||||
"type": "hello",
|
type: "hello",
|
||||||
"minProtocol": GATEWAY_PROTOCOL_VERSION,
|
minprotocol: GATEWAY_PROTOCOL_VERSION,
|
||||||
"maxProtocol": GATEWAY_PROTOCOL_VERSION,
|
maxprotocol: GATEWAY_PROTOCOL_VERSION,
|
||||||
"client": [
|
client: [
|
||||||
"name": "clawdis-mac",
|
"name": ClawdisProtocol.AnyCodable("clawdis-mac"),
|
||||||
"version": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "dev",
|
"version": ClawdisProtocol.AnyCodable(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "dev"),
|
||||||
"platform": "macos",
|
"platform": ClawdisProtocol.AnyCodable("macos"),
|
||||||
"mode": "app",
|
"mode": ClawdisProtocol.AnyCodable("app"),
|
||||||
"instanceId": Host.current().localizedName ?? UUID().uuidString,
|
"instanceId": ClawdisProtocol.AnyCodable(Host.current().localizedName ?? UUID().uuidString),
|
||||||
],
|
],
|
||||||
"caps": [],
|
caps: [],
|
||||||
"auth": self.token != nil ? ["token": self.token!] : [:],
|
auth: self.token.map { ["token": ClawdisProtocol.AnyCodable($0)] },
|
||||||
]
|
locale: nil,
|
||||||
let data = try JSONSerialization.data(withJSONObject: hello)
|
useragent: nil)
|
||||||
|
let data = try JSONEncoder().encode(hello)
|
||||||
try await self.task?.send(.data(data))
|
try await self.task?.send(.data(data))
|
||||||
// wait for hello-ok
|
guard let msg = try await task?.receive() else {
|
||||||
if let msg = try await task?.receive() {
|
throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "hello failed (no response)"])
|
||||||
if try await self.handleHelloResponse(msg) { return }
|
|
||||||
}
|
}
|
||||||
throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "hello failed"])
|
try await self.handleHelloResponse(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func handleHelloResponse(_ msg: URLSessionWebSocketTask.Message) async throws -> Bool {
|
private func handleHelloResponse(_ msg: URLSessionWebSocketTask.Message) async throws {
|
||||||
let data: Data? = switch msg {
|
let data: Data? = switch msg {
|
||||||
case let .data(d): d
|
case let .data(d): d
|
||||||
case let .string(s): s.data(using: .utf8)
|
case let .string(s): s.data(using: .utf8)
|
||||||
@unknown default: nil
|
@unknown default: nil
|
||||||
}
|
}
|
||||||
guard let data else { return false }
|
guard let data else {
|
||||||
guard let obj = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "hello failed (empty response)"])
|
||||||
let type = obj["type"] as? String else { return false }
|
}
|
||||||
if type == "hello-ok" {
|
let decoder = JSONDecoder()
|
||||||
if let policy = obj["policy"] as? [String: Any],
|
if let ok = try? decoder.decode(HelloOk.self, from: data) {
|
||||||
let tick = policy["tickIntervalMs"] as? Double
|
if let tick = ok.policy["tickIntervalMs"]?.value as? Double {
|
||||||
{
|
|
||||||
self.tickIntervalMs = tick
|
self.tickIntervalMs = tick
|
||||||
|
} else if let tick = ok.policy["tickIntervalMs"]?.value as? Int {
|
||||||
|
self.tickIntervalMs = Double(tick)
|
||||||
}
|
}
|
||||||
self.lastTick = Date()
|
self.lastTick = Date()
|
||||||
Task { await self.watchTicks() }
|
Task { await self.watchTicks() }
|
||||||
NotificationCenter.default.post(name: .gatewaySnapshot, object: nil, userInfo: obj)
|
let frame = GatewayFrame.helloOk(ok)
|
||||||
return true
|
NotificationCenter.default.post(name: .gatewaySnapshot, object: frame)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return false
|
if let err = try? decoder.decode(HelloError.self, from: data) {
|
||||||
|
throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "hello-error: \(err.reason)"])
|
||||||
|
}
|
||||||
|
throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "hello failed (unexpected response)"])
|
||||||
}
|
}
|
||||||
|
|
||||||
private func listen() {
|
private func listen() {
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ enum GatewayEnvironment {
|
|||||||
let gatewayLabel = gatewayBin != nil ? "global" : "local"
|
let gatewayLabel = gatewayBin != nil ? "global" : "local"
|
||||||
let gatewayVersionText = installed?.description ?? "unknown"
|
let gatewayVersionText = installed?.description ?? "unknown"
|
||||||
let localPathHint = gatewayBin == nil && projectEntrypoint != nil
|
let localPathHint = gatewayBin == nil && projectEntrypoint != nil
|
||||||
? " (local: \(projectEntrypoint?.path ?? "unknown"))"
|
? " (local: \(projectEntrypoint ?? "unknown"))"
|
||||||
: ""
|
: ""
|
||||||
return GatewayEnvironmentStatus(
|
return GatewayEnvironmentStatus(
|
||||||
kind: .ok,
|
kind: .ok,
|
||||||
|
|||||||
Reference in New Issue
Block a user