Files
nanobot/tui/src/index.ts
T
Xubin Ren 48e7c4f697 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.
2026-09-08 10:04:15 +08:00

82 lines
3.3 KiB
TypeScript

import { NanobotTui, sessionExitMessage, type AppOptions } from "./app"
import { desktopConnectionSource } from "./desktop"
if (process.argv.slice(2).join(" ") === "--desktop-protocol") {
process.stdout.write("1\n")
process.exit(0)
}
const desktop = desktopConnectionSource()
// Keep in sync with _TUI_DETACH_EXIT_CODE in nanobot/cli/tui_launcher.py.
const TUI_DETACH_EXIT_CODE = 90
function themePreference(): AppOptions["theme"] {
const value = process.env.NANOBOT_TUI_THEME?.trim() || "auto"
if (value === "auto" || value === "dark" || value === "light") return value
throw new Error("NANOBOT_TUI_THEME must be auto, dark, or light")
}
const workspace = process.env.NANOBOT_TUI_WORKSPACE?.trim() || ""
const bootstrapUrl = process.env.NANOBOT_TUI_BOOTSTRAP_URL?.trim() || ""
const wsUrl = process.env.NANOBOT_TUI_WS_URL?.trim() || ""
const healthUrl = process.env.NANOBOT_TUI_HEALTH_URL?.trim() || ""
const gatewayStopCommand = process.env.NANOBOT_TUI_GATEWAY_STOP_COMMAND?.trim()
|| "nanobot gateway stop"
if (!desktop && !bootstrapUrl && !wsUrl) {
throw new Error("NANOBOT_TUI_BOOTSTRAP_URL or NANOBOT_TUI_WS_URL is required")
}
const options: AppOptions = {
...(desktop ? { resolveConnection: desktop.resolve, desktopGatewayId: desktop.gatewayId } : bootstrapUrl
? {
bootstrapUrl,
bootstrapSecret: process.env.NANOBOT_TUI_BOOTSTRAP_SECRET?.trim() || "",
healthUrl: healthUrl || undefined,
}
: { wsUrl }),
apiUrl: process.env.NANOBOT_TUI_API_URL?.trim() || "",
apiToken: process.env.NANOBOT_TUI_API_TOKEN?.trim() || "",
chatId: process.env.NANOBOT_TUI_CHAT_ID?.trim() || undefined,
model: process.env.NANOBOT_TUI_MODEL?.trim() || "unknown model",
modelPreset: process.env.NANOBOT_TUI_MODEL_PRESET?.trim() || "default",
workspace,
version: process.env.NANOBOT_TUI_VERSION?.trim() || "dev",
access: process.env.NANOBOT_TUI_ACCESS?.trim() || "workspace access",
theme: themePreference(),
onDetach: (chatId) => {
process.exitCode = TUI_DETACH_EXIT_CODE
process.stdout.write("Detached; the agent continues in the background.\n")
if (!desktop && chatId) process.stdout.write(sessionExitMessage(chatId))
process.stdout.write(desktop ? "Desktop remains running. Reconnect with nanobot.\n" : `Stop it with: ${gatewayStopCommand}\n`)
},
onExit: (chatId) => {
process.stdout.write(desktop ? "Disconnected from Desktop; its backend remains running.\n" : sessionExitMessage(chatId))
},
}
let app: NanobotTui | undefined
let shuttingDown = false
const shutdown = (code = 0) => {
if (shuttingDown) return
shuttingDown = true
app?.stop()
process.exitCode = code
}
for (const signal of ["SIGHUP", "SIGINT", "SIGTERM"] as const) {
process.once(signal, () => shutdown())
}
process.once("exit", () => app?.stop())
process.once("uncaughtException", (error) => {
shutdown(1)
process.stderr.write(desktop ? "Desktop terminal connection failed. No backend was started.\n" : `${error instanceof Error ? error.stack || error.message : String(error)}\n`)
})
process.once("unhandledRejection", (error) => {
shutdown(1)
process.stderr.write(desktop ? "Desktop terminal connection failed. No backend was started.\n" : `${error instanceof Error ? error.stack || error.message : String(error)}\n`)
})
app = await NanobotTui.create(options)
if (shuttingDown) app.stop()
else await app.start()