feat(cli): attach terminal clients to the selected Desktop [skip ci]
NAN-104 requires terminal chat and browser attachment without granting a client ownership of Desktop's gateway. Resolve bounded short-lived credentials over authenticated current-user IPC, pin both Desktop and gateway identities, and reject replacement instances or incompatible clients before any mutation. Reuse the existing TUI artifact resolver behind a narrow Desktop-only entrypoint, keep credentials out of argv/environment/files, and preserve Python-mode reconnect and gateway leases. Add protocol, real-gateway, and anonymous IPC regression coverage; matching native-host and Windows acceptance remain release gates.
This commit is contained in:
1 parent
b373a95f5c
commit
48e7c4f697
20 files changed
+797
-52
No files matched your search
@@ -192,6 +192,8 @@ type OutboundEvent =
|
||||
}
|
||||
|
||||
export interface ClientOptions {
|
||||
expectedGatewayId?: string
|
||||
reconnect?: boolean
|
||||
url?: string
|
||||
resolveConnection?: () => Promise<GatewayConnection>
|
||||
checkHealth?: () => Promise<GatewayHealthStatus>
|
||||
@@ -1072,6 +1074,8 @@ export function sanitizeConnectionFailure(error: unknown): string {
|
||||
}
|
||||
|
||||
export class NanobotClient {
|
||||
private identityVerified = false
|
||||
private handshakeTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private socket: WebSocket | null = null
|
||||
private chatId = ""
|
||||
private workspaceScope?: WorkspaceScopePayload
|
||||
@@ -1115,6 +1119,7 @@ export class NanobotClient {
|
||||
private async open(): Promise<void> {
|
||||
if (this.socket || this.opening || this.closedByClient) return
|
||||
this.opening = true
|
||||
this.identityVerified = !this.options.expectedGatewayId
|
||||
this.nextRetryAt = 0
|
||||
this.connectionAttempt += 1
|
||||
this.reportConnectionProgress()
|
||||
@@ -1154,6 +1159,9 @@ export class NanobotClient {
|
||||
}
|
||||
let opened = false
|
||||
this.socket = socket
|
||||
if (this.options.expectedGatewayId) {
|
||||
this.handshakeTimer = setTimeout(() => this.desktopFailure(), 8_000)
|
||||
}
|
||||
socket.addEventListener("open", () => {
|
||||
if (this.socket !== socket) return
|
||||
opened = true
|
||||
@@ -1172,6 +1180,7 @@ export class NanobotClient {
|
||||
})
|
||||
socket.addEventListener("error", () => {
|
||||
if (this.socket !== socket) return
|
||||
if (this.options.reconnect === false) { this.desktopFailure(); return }
|
||||
this.lastFailure = "connection failed"
|
||||
this.reportRetryState()
|
||||
})
|
||||
@@ -1183,6 +1192,7 @@ export class NanobotClient {
|
||||
this.options.onStatus("closed")
|
||||
return
|
||||
}
|
||||
if (this.options.reconnect === false) { this.desktopFailure(); return }
|
||||
if (opened) {
|
||||
this.connectionAttempt = 0
|
||||
this.reconnectAttempt = 0
|
||||
@@ -1195,6 +1205,8 @@ export class NanobotClient {
|
||||
}
|
||||
|
||||
close(): void {
|
||||
if (this.handshakeTimer) clearTimeout(this.handshakeTimer)
|
||||
this.handshakeTimer = null
|
||||
this.closedByClient = true
|
||||
if (this.reconnectTimer) clearTimeout(this.reconnectTimer)
|
||||
this.reconnectTimer = null
|
||||
@@ -1271,6 +1283,9 @@ export class NanobotClient {
|
||||
payload: Record<string, unknown> = {},
|
||||
timeoutMs = 20_000,
|
||||
): Promise<T> {
|
||||
if (this.options.expectedGatewayId && !this.identityVerified) {
|
||||
return Promise.reject(new Error("Desktop identity not verified"))
|
||||
}
|
||||
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
|
||||
return Promise.reject(new Error("gateway connection is not open"))
|
||||
}
|
||||
@@ -1314,10 +1329,21 @@ export class NanobotClient {
|
||||
try {
|
||||
value = JSON.parse(raw) as unknown
|
||||
} catch {
|
||||
if (!this.identityVerified && this.options.expectedGatewayId) { this.desktopFailure(); return }
|
||||
this.options.onStatus("error", "gateway sent invalid JSON")
|
||||
return
|
||||
}
|
||||
const response = decodeWebUIResponse(value)
|
||||
if (!this.identityVerified) {
|
||||
if (!isRecord(value) || value.event !== "ready" || !isRecord(value.terminal)
|
||||
|| value.terminal.protocolVersion !== 1 || value.terminal.gatewayId !== this.options.expectedGatewayId) {
|
||||
this.desktopFailure()
|
||||
return
|
||||
}
|
||||
this.identityVerified = true
|
||||
if (this.handshakeTimer) clearTimeout(this.handshakeTimer)
|
||||
this.handshakeTimer = null
|
||||
}
|
||||
if (response === null) {
|
||||
this.options.onStatus("error", "gateway sent an invalid event")
|
||||
return
|
||||
@@ -1371,6 +1397,7 @@ export class NanobotClient {
|
||||
}
|
||||
|
||||
private async checkHealthAndScheduleReconnect(): Promise<void> {
|
||||
if (this.options.reconnect === false) { this.desktopFailure(); return }
|
||||
if (this.options.checkHealth) {
|
||||
try {
|
||||
this.healthStatus = await this.options.checkHealth()
|
||||
@@ -1393,6 +1420,11 @@ export class NanobotClient {
|
||||
}
|
||||
}
|
||||
|
||||
private desktopFailure(): void {
|
||||
this.close()
|
||||
this.options.onStatus("error", "Desktop disconnected or is incompatible; reconnect from the terminal", this.connectionInfo())
|
||||
}
|
||||
|
||||
private reportConnectionProgress(): void {
|
||||
if (this.connectedOnce) {
|
||||
this.options.onStatus("reconnecting", this.lastFailure || undefined, this.connectionInfo())
|
||||
@@ -1429,6 +1461,7 @@ export class NanobotClient {
|
||||
}
|
||||
|
||||
private write(event: OutboundEvent): void {
|
||||
if (this.options.expectedGatewayId && !this.identityVerified) throw new Error("Desktop identity not verified")
|
||||
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
|
||||
throw new Error("gateway connection is not open")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user