mirror of
https://github.com/hanzoai/bot.git
synced 2026-10-30 17:24:26 +00:00
Three coordinated things:
1. Resolve the committed upstream/main merge conflicts. 94 files were
sitting with raw <<<<<<< HEAD ... >>>>>>> upstream/main blocks. Every
conflict had HanzoBot/Bot on HEAD and OpenClaw on upstream. Resolved
by keeping HEAD everywhere — 540 conflict blocks.
2. Rewrite remaining OpenClaw text refs.
OPENCLAW → HANZO_BOT
OpenClaw → HanzoBot (PascalCase type identifiers)
openclaw → bot (codebase convention — botToken,
botUsername etc.; avoids invalid
hyphenated JS identifiers)
openclaw[_]/openclaw[A-Z] → bot[_]/bot[A-Z] (compound)
ai.openclaw.x → ai.hanzo.bot.x (JVM package path)
3. Delete or rename openclaw-named file paths (116 of them). Dead
duplicates with a Bot-named canonical from the partial migration:
deleted. Otherwise renamed.
ai/openclaw/** → deleted (ai/hanzo/bot/** canonical)
Sources/OpenClaw*/ → deleted (Sources/Bot* canonical)
Tests/OpenClawIPCTests/ → deleted (Tests/BotIPCTests canonical)
OpenClawKit/ → deleted (BotKit canonical)
openclaw-tools.*.ts → deleted (bot-tools.*.ts canonical)
openclaw-root.ts etc. → deleted (bot-root.ts canonical)
types.openclaw.ts → deleted (types.bot.ts canonical)
extensions/*/openclaw.plugin.json → renamed hanzo-bot.plugin.json
docs/start/openclaw.md → renamed hanzo-bot.md (+ zh-CN)
docs/assets/openclaw-*.png, whatsapp-openclaw*.jpg → deleted
scripts/.../openclaw-* → deleted (bot/hanzo-bot parallels)
openclaw.mjs → deleted (hanzo-bot.mjs is package.bin)
Then patched 65 broken JS/TS import strings where hanzo-bot had ended
up inside an import path ('./types.hanzo-bot.js' → './types.bot.js'
etc., since the on-disk filename uses the brand-neutral bot- prefix).
Pre-existing lint debt cleaned up to get oxlint --type-aware to 0/0:
removed the dead i18n test that referenced a path no longer existing;
defined the missing DIDConfig and WalletConfig types in types.base.ts
(they were imported but never declared); dropped unused imports;
collapsed redundant type assertion and a tautological meta.bot lookup
that the openclaw→bot rename made redundant.
The a2ui.bundle.js generated artifact is kept at its pre-rebrand
contents — it gets regenerated by 'pnpm canvas:a2ui:bundle' from
sources I cannot rebuild in this commit; the source side is clean.
Verified: 0 occurrences of openclaw (case-insensitive) in tree source
content, 0 file or directory paths with openclaw in the name; oxlint
--type-aware src test reports 0/0.
186 lines
7.3 KiB
Swift
186 lines
7.3 KiB
Swift
import BotKit
|
|
import Foundation
|
|
import Network
|
|
import Observation
|
|
|
|
@MainActor
|
|
@Observable
|
|
final class GatewayDiscoveryModel {
|
|
struct DebugLogEntry: Identifiable, Equatable {
|
|
var id = UUID()
|
|
var ts: Date
|
|
var message: String
|
|
}
|
|
|
|
struct DiscoveredGateway: Identifiable, Equatable {
|
|
var id: String { self.stableID }
|
|
var name: String
|
|
var endpoint: NWEndpoint
|
|
var stableID: String
|
|
var debugID: String
|
|
var lanHost: String?
|
|
var tailnetDns: String?
|
|
var gatewayPort: Int?
|
|
var canvasPort: Int?
|
|
var tlsEnabled: Bool
|
|
var tlsFingerprintSha256: String?
|
|
var cliPath: String?
|
|
}
|
|
|
|
var gateways: [DiscoveredGateway] = []
|
|
var statusText: String = "Idle"
|
|
private(set) var debugLog: [DebugLogEntry] = []
|
|
|
|
private var browsers: [String: NWBrowser] = [:]
|
|
private var gatewaysByDomain: [String: [DiscoveredGateway]] = [:]
|
|
private var statesByDomain: [String: NWBrowser.State] = [:]
|
|
private var debugLoggingEnabled = false
|
|
private var lastStableIDs = Set<String>()
|
|
|
|
func setDebugLoggingEnabled(_ enabled: Bool) {
|
|
let wasEnabled = self.debugLoggingEnabled
|
|
self.debugLoggingEnabled = enabled
|
|
if !enabled {
|
|
self.debugLog = []
|
|
} else if !wasEnabled {
|
|
self.appendDebugLog("debug logging enabled")
|
|
self.appendDebugLog("snapshot: status=\(self.statusText) gateways=\(self.gateways.count)")
|
|
}
|
|
}
|
|
|
|
func start() {
|
|
if !self.browsers.isEmpty { return }
|
|
self.appendDebugLog("start()")
|
|
|
|
for domain in HanzoBotBonjour.gatewayServiceDomains {
|
|
let params = NWParameters.tcp
|
|
params.includePeerToPeer = true
|
|
let browser = NWBrowser(
|
|
for: .bonjour(type: HanzoBotBonjour.gatewayServiceType, domain: domain),
|
|
using: params)
|
|
|
|
browser.stateUpdateHandler = { [weak self] state in
|
|
Task { @MainActor in
|
|
guard let self else { return }
|
|
self.statesByDomain[domain] = state
|
|
self.updateStatusText()
|
|
self.appendDebugLog("state[\(domain)]: \(Self.prettyState(state))")
|
|
},
|
|
onResults: { [weak self] results in
|
|
guard let self else { return }
|
|
self.gatewaysByDomain[domain] = results.compactMap { result -> DiscoveredGateway? in
|
|
switch result.endpoint {
|
|
case let .service(name, _, _, _):
|
|
let decodedName = BonjourEscapes.decode(name)
|
|
let txt = result.endpoint.txtRecord?.dictionary ?? [:]
|
|
let advertisedName = txt["displayName"]
|
|
let prettyAdvertised = advertisedName
|
|
.map(Self.prettifyInstanceName)
|
|
.flatMap { $0.isEmpty ? nil : $0 }
|
|
let prettyName = prettyAdvertised ?? Self.prettifyInstanceName(decodedName)
|
|
return DiscoveredGateway(
|
|
name: prettyName,
|
|
endpoint: result.endpoint,
|
|
stableID: GatewayEndpointID.stableID(result.endpoint),
|
|
debugID: GatewayEndpointID.prettyDescription(result.endpoint),
|
|
lanHost: Self.txtValue(txt, key: "lanHost"),
|
|
tailnetDns: Self.txtValue(txt, key: "tailnetDns"),
|
|
gatewayPort: Self.txtIntValue(txt, key: "gatewayPort"),
|
|
canvasPort: Self.txtIntValue(txt, key: "canvasPort"),
|
|
tlsEnabled: Self.txtBoolValue(txt, key: "gatewayTls"),
|
|
tlsFingerprintSha256: Self.txtValue(txt, key: "gatewayTlsSha256"),
|
|
cliPath: Self.txtValue(txt, key: "cliPath"))
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }
|
|
self.recomputeGateways()
|
|
})
|
|
|
|
self.browsers[domain] = browser
|
|
browser.start(queue: DispatchQueue(label: "ai.hanzo.bot.ios.gateway-discovery.\(domain)"))
|
|
}
|
|
}
|
|
|
|
func stop() {
|
|
self.appendDebugLog("stop()")
|
|
for browser in self.browsers.values {
|
|
browser.cancel()
|
|
}
|
|
self.browsers = [:]
|
|
self.gatewaysByDomain = [:]
|
|
self.statesByDomain = [:]
|
|
self.gateways = []
|
|
self.statusText = "Stopped"
|
|
}
|
|
|
|
private func recomputeGateways() {
|
|
let next = self.gatewaysByDomain.values
|
|
.flatMap(\.self)
|
|
.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }
|
|
|
|
let nextIDs = Set(next.map(\.stableID))
|
|
let added = nextIDs.subtracting(self.lastStableIDs)
|
|
let removed = self.lastStableIDs.subtracting(nextIDs)
|
|
if !added.isEmpty || !removed.isEmpty {
|
|
self.appendDebugLog("results: total=\(next.count) added=\(added.count) removed=\(removed.count)")
|
|
}
|
|
self.lastStableIDs = nextIDs
|
|
self.gateways = next
|
|
}
|
|
|
|
private func updateStatusText() {
|
|
self.statusText = GatewayDiscoveryStatusText.make(
|
|
states: Array(self.statesByDomain.values),
|
|
hasBrowsers: !self.browsers.isEmpty)
|
|
}
|
|
|
|
private static func prettyState(_ state: NWBrowser.State) -> String {
|
|
switch state {
|
|
case .setup:
|
|
"setup"
|
|
case .ready:
|
|
"ready"
|
|
case let .failed(err):
|
|
"failed (\(err))"
|
|
case .cancelled:
|
|
"cancelled"
|
|
case let .waiting(err):
|
|
"waiting (\(err))"
|
|
@unknown default:
|
|
"unknown"
|
|
}
|
|
}
|
|
|
|
private func appendDebugLog(_ message: String) {
|
|
guard self.debugLoggingEnabled else { return }
|
|
self.debugLog.append(DebugLogEntry(ts: Date(), message: message))
|
|
if self.debugLog.count > 200 {
|
|
self.debugLog.removeFirst(self.debugLog.count - 200)
|
|
}
|
|
}
|
|
|
|
private static func prettifyInstanceName(_ decodedName: String) -> String {
|
|
let normalized = decodedName.split(whereSeparator: \.isWhitespace).joined(separator: " ")
|
|
let stripped = normalized.replacingOccurrences(of: " (HanzoBot)", with: "")
|
|
.replacingOccurrences(of: #"\s+\(\d+\)$"#, with: "", options: .regularExpression)
|
|
return stripped.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
}
|
|
|
|
private static func txtValue(_ dict: [String: String], key: String) -> String? {
|
|
let raw = dict[key]?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
|
return raw.isEmpty ? nil : raw
|
|
}
|
|
|
|
private static func txtIntValue(_ dict: [String: String], key: String) -> Int? {
|
|
guard let raw = self.txtValue(dict, key: key) else { return nil }
|
|
return Int(raw)
|
|
}
|
|
|
|
private static func txtBoolValue(_ dict: [String: String], key: String) -> Bool {
|
|
guard let raw = self.txtValue(dict, key: key)?.lowercased() else { return false }
|
|
return raw == "1" || raw == "true" || raw == "yes"
|
|
}
|
|
}
|