Compare commits

..
Author SHA1 Message Date
Peter Steinberger 110079d99d fix: guard nodes status duration parsing (#1354) (thanks @vignesh07) 2026-01-21 05:07:27 +00:00
Peter Steinberger 34a126a6d7 fix: allow mobile node client ids (#1354) (thanks @vignesh07) 2026-01-21 05:07:26 +00:00
Vignesh Natarajan 31462f64d8 fix: allow clawdbot-ios gateway client id
The iOS app currently identifies as clientId=clawdbot-ios when
connecting in node mode. Add this ID to the allowed gateway client
IDs so the handshake schema accepts it.

Also fixes a TS strictness issue in auto-reply status formatting
(parts filter) that caused
> clawdbot@2026.1.20 build /Users/vignesh/clawd/clawdbot-upstream
> tsc -p tsconfig.json && node --import tsx scripts/canvas-a2ui-copy.ts && node --import tsx scripts/copy-hook-metadata.ts && node --import tsx scripts/write-build-info.ts

[copy-hook-metadata] Copied boot-md/HOOK.md
[copy-hook-metadata] Copied command-logger/HOOK.md
[copy-hook-metadata] Copied session-memory/HOOK.md
[copy-hook-metadata] Copied soul-evil/HOOK.md
[copy-hook-metadata] Done to fail.
2026-01-21 05:07:26 +00:00
Peter Steinberger de0a488985 refactor: unify gateway connectivity state 2026-01-21 05:01:32 +00:00
Peter Steinberger 15f16de651 docs: update nodes list/status flags 2026-01-21 04:52:54 +00:00
Peter Steinberger fa7df1976d feat: theme hooks/skills/plugins output 2026-01-21 04:48:34 +00:00
Peter Steinberger 2cd62f94a5 feat: tableize device/directory outputs 2026-01-21 04:48:33 +00:00
Peter Steinberger a74c19feed docs: unify cli help examples 2026-01-21 04:48:33 +00:00
Peter Steinberger 1ad4a7194e fix: allow node exec fallback and defer node approvals 2026-01-21 04:46:50 +00:00
Peter Steinberger beec504ebd feat: filter nodes list/status 2026-01-21 04:39:15 +00:00
Peter Steinberger fe1133e2c5 Merge pull request #1348 from vignesh07/feat/tui-input-history
feat(tui): add input history (↑/↓) for submitted messages
2026-01-21 04:37:49 +00:00
Peter Steinberger 6f37f1d8ff fix: record tui input history (#1348) (thanks @vignesh07) 2026-01-21 04:37:22 +00:00
Peter Steinberger 57700f33a9 fix: record node last-connect by instance id 2026-01-21 04:32:53 +00:00
Vignesh Natarajan 2700794228 feat(tui): add input history for submitted messages (WIP)
Record submitted inputs in the editor history so up/down arrow
can recall previous messages.

Adds a small helper to wire submit handling and unit tests for
routing/recording behavior.

No PR yet (per request).
2026-01-21 04:31:33 +00:00
dependabot[bot] 416894c642 chore(deps): bump tar in the npm_and_yarn group across 1 directory
Bumps the npm_and_yarn group with 1 update in the / directory: [tar](https://github.com/isaacs/node-tar).


Updates `tar` from 7.5.3 to 7.5.4
- [Release notes](https://github.com/isaacs/node-tar/releases)
- [Changelog](https://github.com/isaacs/node-tar/blob/main/CHANGELOG.md)
- [Commits](https://github.com/isaacs/node-tar/compare/v7.5.3...v7.5.4)

---
updated-dependencies:
- dependency-name: tar
  dependency-version: 7.5.4
  dependency-type: direct:production
  dependency-group: npm_and_yarn
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-01-21 04:22:46 +00:00
Peter Steinberger db88378ae3 fix: normalize node invoke result payloadJSON 2026-01-21 04:17:35 +00:00
Peter Steinberger e97b4973bb docs: clarify node host exec flow 2026-01-21 04:14:34 +00:00
Peter Steinberger 832dfb02fe fix: omit null payloadJSON in node invoke results 2026-01-21 04:13:47 +00:00
Peter Steinberger 15e3a2a395 fix: sanitize node invoke result params 2026-01-21 04:13:47 +00:00
Peter Steinberger 8c472c210f docs: colorize update help 2026-01-21 04:08:50 +00:00
Peter Steinberger 833bbcd166 fix: show subcommand help on --help 2026-01-21 04:08:50 +00:00
Peter Steinberger d7440baef6 docs: clarify update help 2026-01-21 04:08:50 +00:00
Peter Steinberger 58b131919f feat: use tsgo for dev/watch builds 2026-01-21 04:06:09 +00:00
Peter Steinberger 186e86660a Merge pull request #1350 from Jackten/fix/fallback-authprofile-provider-scope
test(auto-reply): regression for authProfileId across provider fallback
2026-01-21 03:53:07 +00:00
Peter Steinberger 18d47b47d2 Merge pull request #1349 from siddhantjain/fix/immediate-callback-answer
fix(telegram): answer callback queries immediately to prevent retries
2026-01-21 03:51:50 +00:00
Peter Steinberger eb1e2c7a3b fix: suppress node warnings unless verbose 2026-01-21 03:47:50 +00:00
Peter Steinberger 6ea4cb0012 fix: suppress npm fund prompts 2026-01-21 03:47:50 +00:00
Peter Steinberger 184f5a5fc3 fix: suppress update deprecation warnings 2026-01-21 03:47:50 +00:00
Peter Steinberger 4ad359ffcd feat: add non-interactive update option 2026-01-21 03:47:50 +00:00
Peter Steinberger 38cc2a3288 fix: guard media status parts filter 2026-01-21 03:43:42 +00:00
Peter Steinberger 28c49db494 fix: default exec security to allowlist 2026-01-21 03:40:27 +00:00
Peter Steinberger 026e6c4df4 fix: restore bundled plugin discovery 2026-01-21 03:40:05 +00:00
Peter Steinberger 841dfc693e style: flatten approvals allowlist age 2026-01-21 03:36:54 +00:00
Peter Steinberger f38278d919 style: collapse approvals allowlist age 2026-01-21 03:36:54 +00:00
Peter Steinberger 9545edcb49 style: tighten approvals allowlist age 2026-01-21 03:36:54 +00:00
Peter Steinberger f3554a3ad8 feat: render approvals get as table 2026-01-21 03:36:54 +00:00
Peter Steinberger b30359e9cd fix(macos): ignore launchd token in remote mode 2026-01-21 03:34:51 +00:00
Peter Steinberger d3898ee8df test(macos): cover gateway host resolution 2026-01-21 03:34:51 +00:00
Peter Steinberger d1c2fc4bc8 fix: hide empty status rows 2026-01-21 03:32:16 +00:00
Peter Steinberger a5a3ab958f fix: skip bundled plugin discovery in tests 2026-01-21 03:29:03 +00:00
Peter Steinberger 165861e78d feat: add approvals allowlist examples 2026-01-21 03:27:50 +00:00
Peter Steinberger e7c355ee85 feat: group service help examples by submenu 2026-01-21 03:27:50 +00:00
Peter Steinberger 052a58f2f7 style: tighten nodes pending row 2026-01-21 03:27:50 +00:00
Peter Steinberger 5ff56ffb4e style: compress nodes pending age 2026-01-21 03:27:50 +00:00
Peter Steinberger 9a3dd626a1 style: format nodes pending row 2026-01-21 03:27:50 +00:00
Peter Steinberger aae4b2952f feat: polish nodes cli output 2026-01-21 03:27:50 +00:00
Peter Steinberger aec622fe63 chore: remove fresh dist log 2026-01-21 03:13:50 +00:00
Peter Steinberger e6287270d9 feat: render nodes status as table 2026-01-21 03:11:27 +00:00
Vultr-Clawd Admin c05a7b5390 test(auto-reply): drop auth profile on provider fallback 2026-01-20 23:07:33 -04:00
Siddhant Jain 020fecef5c fix(telegram): answer callback queries immediately to prevent retries
Telegram retries callback queries if they aren't acknowledged quickly.
Previously, answerCallbackQuery was called in a finally block AFTER
processing, which could take several seconds for agent responses.

This change moves answerCallbackQuery to immediately after basic
validation, before any processing begins. This prevents Telegram
from sending duplicate callbacks while the agent is thinking.

Fixes duplicate callback handling when agent processing is slow.
2026-01-21 03:04:28 +00:00
84 changed files with 2456 additions and 812 deletions

No files matched your search

+4
View File
@@ -6,6 +6,7 @@ Docs: https://docs.clawd.bot
### Changes ### Changes
- Deps: update workspace + memory-lancedb dependencies. - Deps: update workspace + memory-lancedb dependencies.
- Dev: use tsgo for dev/watch builds by default; set `CLAWDBOT_TS_COMPILER=tsc` to opt out.
- Repo: remove the Peekaboo git submodule now that the SPM release is used. - Repo: remove the Peekaboo git submodule now that the SPM release is used.
- Update: sync plugin sources on channel switches and update npm-installed plugins during `clawdbot update`. - Update: sync plugin sources on channel switches and update npm-installed plugins during `clawdbot update`.
- Plugins: share npm plugin update logic between `clawdbot update` and `clawdbot plugins update`. - Plugins: share npm plugin update logic between `clawdbot update` and `clawdbot plugins update`.
@@ -22,6 +23,7 @@ Docs: https://docs.clawd.bot
- Skills: add download installs with OS-filtered install options; add local sherpa-onnx-tts skill. - Skills: add download installs with OS-filtered install options; add local sherpa-onnx-tts skill.
- Docs: clarify WhatsApp voice notes and Windows WSL portproxy LAN access notes. - Docs: clarify WhatsApp voice notes and Windows WSL portproxy LAN access notes.
- UI: add copy-as-markdown with error feedback and drop legacy list view. (#1345) — thanks @bradleypriest. - UI: add copy-as-markdown with error feedback and drop legacy list view. (#1345) — thanks @bradleypriest.
- TUI: add input history (up/down) for submitted messages. (#1348) — thanks @vignesh07.
### Fixes ### Fixes
- Discovery: shorten Bonjour DNS-SD service type to `_clawdbot-gw._tcp` and update discovery clients/docs. - Discovery: shorten Bonjour DNS-SD service type to `_clawdbot-gw._tcp` and update discovery clients/docs.
- Agents: preserve subagent announce thread/topic routing + queued replies across channels. (#1241) — thanks @gnarco. - Agents: preserve subagent announce thread/topic routing + queued replies across channels. (#1241) — thanks @gnarco.
@@ -32,11 +34,13 @@ Docs: https://docs.clawd.bot
- CLI: keep `clawdbot logs` output resilient to broken pipes while preserving progress output. - CLI: keep `clawdbot logs` output resilient to broken pipes while preserving progress output.
- Model catalog: avoid caching import failures, log transient discovery errors, and keep partial results. (#1332) — thanks @dougvk. - Model catalog: avoid caching import failures, log transient discovery errors, and keep partial results. (#1332) — thanks @dougvk.
- Doctor: clarify plugin auto-enable hint text in the startup banner. - Doctor: clarify plugin auto-enable hint text in the startup banner.
- Gateway: allow mobile node client ids for iOS + Android handshake validation. (#1354) — thanks @vignesh07.
- Gateway: clarify unauthorized handshake responses with token/password mismatch guidance. - Gateway: clarify unauthorized handshake responses with token/password mismatch guidance.
- Gateway: clarify connect/validation errors for gateway params. (#1347) — thanks @vignesh07. - Gateway: clarify connect/validation errors for gateway params. (#1347) — thanks @vignesh07.
- Gateway: preserve restart wake routing + thread replies across restarts. (#1337) — thanks @John-Rood. - Gateway: preserve restart wake routing + thread replies across restarts. (#1337) — thanks @John-Rood.
- Gateway: reschedule per-agent heartbeats on config hot reload without restarting the runner. - Gateway: reschedule per-agent heartbeats on config hot reload without restarting the runner.
- Config: log invalid config issues once per run and keep invalid-config errors stackless. - Config: log invalid config issues once per run and keep invalid-config errors stackless.
- Exec: default gateway/node exec security to allowlist when unset (sandbox stays deny).
- UI: keep config form enums typed, preserve empty strings, protect sensitive defaults, and deepen config search. (#1315) — thanks @MaudeBot. - UI: keep config form enums typed, preserve empty strings, protect sensitive defaults, and deepen config search. (#1315) — thanks @MaudeBot.
- UI: preserve ordered list numbering in chat markdown. (#1341) — thanks @bradleypriest. - UI: preserve ordered list numbering in chat markdown. (#1341) — thanks @bradleypriest.
- UI: allow Control UI to read gatewayUrl from URL params for remote WebSocket targets. (#1342) — thanks @ameno-. - UI: allow Control UI to read gatewayUrl from URL params for remote WebSocket targets. (#1342) — thanks @ameno-.
@@ -529,7 +529,7 @@ class NodeRuntime(context: Context) {
caps = buildCapabilities(), caps = buildCapabilities(),
commands = buildInvokeCommands(), commands = buildInvokeCommands(),
permissions = emptyMap(), permissions = emptyMap(),
client = buildClientInfo(clientId = "node-host", clientMode = "node"), client = buildClientInfo(clientId = "clawdbot-android", clientMode = "node"),
userAgent = buildUserAgent(), userAgent = buildUserAgent(),
) )
} }
@@ -263,7 +263,8 @@ extension ConfigSettings {
let subsections = self.resolveSubsections(for: section) let subsections = self.resolveSubsections(for: section)
let resolved: (ConfigSchemaNode, ConfigPath) = { let resolved: (ConfigSchemaNode, ConfigPath) = {
if case let .key(key) = subsection, if case let .key(key) = subsection,
let match = subsections.first(where: { $0.key == key }) { let match = subsections.first(where: { $0.key == key })
{
return (match.node, match.path) return (match.node, match.path)
} }
return (self.resolvedSchemaNode(section.node), defaultPath) return (self.resolvedSchemaNode(section.node), defaultPath)
@@ -87,15 +87,7 @@ final class ControlChannel {
func configure() async { func configure() async {
self.logger.info("control channel configure mode=local") self.logger.info("control channel configure mode=local")
self.state = .connecting await self.refreshEndpoint(reason: "configure")
do {
try await GatewayConnection.shared.refresh()
self.state = .connected
PresenceReporter.shared.sendImmediate(reason: "connect")
} catch {
let message = self.friendlyGatewayMessage(error)
self.state = .degraded(message)
}
} }
func configure(mode: Mode = .local) async throws { func configure(mode: Mode = .local) async throws {
@@ -111,7 +103,7 @@ final class ControlChannel {
"target=\(target, privacy: .public) identitySet=\(idSet, privacy: .public)") "target=\(target, privacy: .public) identitySet=\(idSet, privacy: .public)")
self.state = .connecting self.state = .connecting
_ = try await GatewayEndpointStore.shared.ensureRemoteControlTunnel() _ = try await GatewayEndpointStore.shared.ensureRemoteControlTunnel()
await self.configure() await self.refreshEndpoint(reason: "configure")
} catch { } catch {
self.state = .degraded(error.localizedDescription) self.state = .degraded(error.localizedDescription)
throw error throw error
@@ -119,6 +111,19 @@ final class ControlChannel {
} }
} }
func refreshEndpoint(reason: String) async {
self.logger.info("control channel refresh endpoint reason=\(reason, privacy: .public)")
self.state = .connecting
do {
try await self.establishGatewayConnection()
self.state = .connected
PresenceReporter.shared.sendImmediate(reason: "connect")
} catch {
let message = self.friendlyGatewayMessage(error)
self.state = .degraded(message)
}
}
func disconnect() async { func disconnect() async {
await GatewayConnection.shared.shutdown() await GatewayConnection.shared.shutdown()
self.state = .disconnected self.state = .disconnected
@@ -275,18 +280,28 @@ final class ControlChannel {
} }
} }
do { await self.refreshEndpoint(reason: "recovery:\(reasonText)")
try await GatewayConnection.shared.refresh() if case .connected = self.state {
self.logger.info("control channel recovery finished") self.logger.info("control channel recovery finished")
} catch { } else if case let .degraded(message) = self.state {
self.logger.error( self.logger.error("control channel recovery failed \(message, privacy: .public)")
"control channel recovery failed \(error.localizedDescription, privacy: .public)")
} }
self.recoveryTask = nil self.recoveryTask = nil
} }
} }
private func establishGatewayConnection(timeoutMs: Int = 5000) async throws {
try await GatewayConnection.shared.refresh()
let ok = try await GatewayConnection.shared.healthOK(timeoutMs: timeoutMs)
if ok == false {
throw NSError(
domain: "Gateway",
code: 0,
userInfo: [NSLocalizedDescriptionKey: "gateway health not ok"])
}
}
func sendSystemEvent(_ text: String, params: [String: AnyHashable] = [:]) async throws { func sendSystemEvent(_ text: String, params: [String: AnyHashable] = [:]) async throws {
var merged = params var merged = params
merged["text"] = AnyHashable(text) merged["text"] = AnyHashable(text)
@@ -319,7 +319,7 @@ private enum ExecHostExecutor {
security: context.security, security: context.security,
allowlistMatch: context.allowlistMatch, allowlistMatch: context.allowlistMatch,
skillAllow: context.skillAllow), skillAllow: context.skillAllow),
approvalDecision == nil approvalDecision == nil
{ {
let decision = ExecApprovalsPromptPresenter.prompt( let decision = ExecApprovalsPromptPresenter.prompt(
ExecApprovalPromptRequest( ExecApprovalPromptRequest(
@@ -0,0 +1,63 @@
import Foundation
import Observation
import OSLog
@MainActor
@Observable
final class GatewayConnectivityCoordinator {
static let shared = GatewayConnectivityCoordinator()
private let logger = Logger(subsystem: "com.clawdbot", category: "gateway.connectivity")
private var endpointTask: Task<Void, Never>?
private var lastResolvedURL: URL?
private(set) var endpointState: GatewayEndpointState?
private(set) var resolvedURL: URL?
private(set) var resolvedMode: AppState.ConnectionMode?
private(set) var resolvedHostLabel: String?
private init() {
self.start()
}
func start() {
guard self.endpointTask == nil else { return }
self.endpointTask = Task { [weak self] in
guard let self else { return }
let stream = await GatewayEndpointStore.shared.subscribe()
for await state in stream {
await MainActor.run { self.handleEndpointState(state) }
}
}
}
var localEndpointHostLabel: String? {
guard self.resolvedMode == .local, let url = self.resolvedURL else { return nil }
return Self.hostLabel(for: url)
}
private func handleEndpointState(_ state: GatewayEndpointState) {
self.endpointState = state
switch state {
case let .ready(mode, url, _, _):
self.resolvedMode = mode
self.resolvedURL = url
self.resolvedHostLabel = Self.hostLabel(for: url)
let urlChanged = self.lastResolvedURL?.absoluteString != url.absoluteString
if urlChanged {
self.lastResolvedURL = url
Task { await ControlChannel.shared.refreshEndpoint(reason: "endpoint changed") }
}
case let .connecting(mode, _):
self.resolvedMode = mode
case let .unavailable(mode, _):
self.resolvedMode = mode
}
}
private static func hostLabel(for url: URL) -> String {
let host = url.host ?? url.absoluteString
if let port = url.port { return "\(host):\(port)" }
return host
}
}
@@ -68,6 +68,7 @@ actor GatewayEndpointStore {
env: ProcessInfo.processInfo.environment) env: ProcessInfo.processInfo.environment)
let customBindHost = GatewayEndpointStore.resolveGatewayCustomBindHost(root: root) let customBindHost = GatewayEndpointStore.resolveGatewayCustomBindHost(root: root)
let tailscaleIP = await MainActor.run { TailscaleService.shared.tailscaleIP } let tailscaleIP = await MainActor.run { TailscaleService.shared.tailscaleIP }
?? TailscaleService.fallbackTailnetIPv4()
return GatewayEndpointStore.resolveLocalGatewayHost( return GatewayEndpointStore.resolveLocalGatewayHost(
bindMode: bind, bindMode: bind,
customBindHost: customBindHost, customBindHost: customBindHost,
@@ -172,6 +173,10 @@ actor GatewayEndpointStore {
return configToken return configToken
} }
if isRemote {
return nil
}
if let token = launchdSnapshot?.token?.trimmingCharacters(in: .whitespacesAndNewlines), if let token = launchdSnapshot?.token?.trimmingCharacters(in: .whitespacesAndNewlines),
!token.isEmpty !token.isEmpty
{ {
@@ -483,6 +488,7 @@ actor GatewayEndpointStore {
guard currentHost == "127.0.0.1" || currentHost == "localhost" else { return nil } guard currentHost == "127.0.0.1" || currentHost == "localhost" else { return nil }
let tailscaleIP = await MainActor.run { TailscaleService.shared.tailscaleIP } let tailscaleIP = await MainActor.run { TailscaleService.shared.tailscaleIP }
?? TailscaleService.fallbackTailnetIPv4()
guard let tailscaleIP, !tailscaleIP.isEmpty else { return nil } guard let tailscaleIP, !tailscaleIP.isEmpty else { return nil }
let scheme = GatewayEndpointStore.resolveGatewayScheme( let scheme = GatewayEndpointStore.resolveGatewayScheme(
@@ -634,11 +640,12 @@ extension GatewayEndpointStore {
static func _testResolveLocalGatewayHost( static func _testResolveLocalGatewayHost(
bindMode: String?, bindMode: String?,
tailscaleIP: String?) -> String tailscaleIP: String?,
customBindHost: String? = nil) -> String
{ {
self.resolveLocalGatewayHost( self.resolveLocalGatewayHost(
bindMode: bindMode, bindMode: bindMode,
customBindHost: nil, customBindHost: customBindHost,
tailscaleIP: tailscaleIP) tailscaleIP: tailscaleIP)
} }
} }
@@ -235,8 +235,8 @@ final class HealthStore {
let lower = error.lowercased() let lower = error.lowercased()
if lower.contains("connection refused") { if lower.contains("connection refused") {
let port = GatewayEnvironment.gatewayPort() let port = GatewayEnvironment.gatewayPort()
return "The gateway control port (127.0.0.1:\(port)) isn’t listening — " + let host = GatewayConnectivityCoordinator.shared.localEndpointHostLabel ?? "127.0.0.1:\(port)"
"restart Clawdbot to bring it back." return "The gateway control port (\(host)) isn’t listening — restart Clawdbot to bring it back."
} }
if lower.contains("timeout") { if lower.contains("timeout") {
return "Timed out waiting for the control server; the gateway may be crashed or still starting." return "Timed out waiting for the control server; the gateway may be crashed or still starting."
@@ -13,6 +13,7 @@ struct ClawdbotApp: App {
private let gatewayManager = GatewayProcessManager.shared private let gatewayManager = GatewayProcessManager.shared
private let controlChannel = ControlChannel.shared private let controlChannel = ControlChannel.shared
private let activityStore = WorkActivityStore.shared private let activityStore = WorkActivityStore.shared
private let connectivityCoordinator = GatewayConnectivityCoordinator.shared
@State private var statusItem: NSStatusItem? @State private var statusItem: NSStatusItem?
@State private var isMenuPresented = false @State private var isMenuPresented = false
@State private var isPanelVisible = false @State private var isPanelVisible = false
@@ -469,7 +469,7 @@ extension MenuSessionsInjector {
} }
case .local: case .local:
platform = "local" platform = "local"
host = "127.0.0.1:\(port)" host = GatewayConnectivityCoordinator.shared.localEndpointHostLabel ?? "127.0.0.1:\(port)"
case .unconfigured: case .unconfigured:
platform = nil platform = nil
host = nil host = nil
@@ -103,6 +103,7 @@ final class TailscaleService {
} }
func checkTailscaleStatus() async { func checkTailscaleStatus() async {
let previousIP = self.tailscaleIP
self.isInstalled = self.checkAppInstallation() self.isInstalled = self.checkAppInstallation()
if !self.isInstalled { if !self.isInstalled {
self.isRunning = false self.isRunning = false
@@ -147,6 +148,10 @@ final class TailscaleService {
self.statusError = nil self.statusError = nil
self.logger.info("Tailscale interface IP detected (fallback) ip=\(fallback, privacy: .public)") self.logger.info("Tailscale interface IP detected (fallback) ip=\(fallback, privacy: .public)")
} }
if previousIP != self.tailscaleIP {
await GatewayEndpointStore.shared.refresh()
}
} }
func openTailscaleApp() { func openTailscaleApp() {
@@ -214,4 +219,8 @@ final class TailscaleService {
return nil return nil
} }
nonisolated static func fallbackTailnetIPv4() -> String? {
Self.detectTailnetIPv4()
}
} }
@@ -10,7 +10,7 @@ struct ConnectOptions {
var token: String? var token: String?
var password: String? var password: String?
var mode: String? var mode: String?
var timeoutMs: Int = 15_000 var timeoutMs: Int = 15000
var json: Bool = false var json: Bool = false
var probe: Bool = false var probe: Bool = false
var clientId: String = "clawdbot-macos" var clientId: String = "clawdbot-macos"
@@ -22,53 +22,43 @@ struct ConnectOptions {
static func parse(_ args: [String]) -> ConnectOptions { static func parse(_ args: [String]) -> ConnectOptions {
var opts = ConnectOptions() var opts = ConnectOptions()
let flagHandlers: [String: (inout ConnectOptions) -> Void] = [
"-h": { $0.help = true },
"--help": { $0.help = true },
"--json": { $0.json = true },
"--probe": { $0.probe = true },
]
let valueHandlers: [String: (inout ConnectOptions, String) -> Void] = [
"--url": { $0.url = $1 },
"--token": { $0.token = $1 },
"--password": { $0.password = $1 },
"--mode": { $0.mode = $1 },
"--timeout": { opts, raw in
if let parsed = Int(raw.trimmingCharacters(in: .whitespacesAndNewlines)) {
opts.timeoutMs = max(250, parsed)
}
},
"--client-id": { $0.clientId = $1 },
"--client-mode": { $0.clientMode = $1 },
"--display-name": { $0.displayName = $1 },
"--role": { $0.role = $1 },
"--scopes": { opts, raw in
opts.scopes = raw.split(separator: ",").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
},
]
var i = 0 var i = 0
while i < args.count { while i < args.count {
let arg = args[i] let arg = args[i]
switch arg { if let handler = flagHandlers[arg] {
case "-h", "--help": handler(&opts)
opts.help = true i += 1
case "--json": continue
opts.json = true }
case "--probe": if let handler = valueHandlers[arg], let value = self.nextValue(args, index: &i) {
opts.probe = true handler(&opts, value)
case "--url": i += 1
opts.url = self.nextValue(args, index: &i) continue
case "--token":
opts.token = self.nextValue(args, index: &i)
case "--password":
opts.password = self.nextValue(args, index: &i)
case "--mode":
if let value = self.nextValue(args, index: &i) {
opts.mode = value
}
case "--timeout":
if let raw = self.nextValue(args, index: &i),
let parsed = Int(raw.trimmingCharacters(in: .whitespacesAndNewlines))
{
opts.timeoutMs = max(250, parsed)
}
case "--client-id":
if let value = self.nextValue(args, index: &i) {
opts.clientId = value
}
case "--client-mode":
if let value = self.nextValue(args, index: &i) {
opts.clientMode = value
}
case "--display-name":
opts.displayName = self.nextValue(args, index: &i)
case "--role":
if let value = self.nextValue(args, index: &i) {
opts.role = value
}
case "--scopes":
if let value = self.nextValue(args, index: &i) {
opts.scopes = value.split(separator: ",").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
}
default:
break
} }
i += 1 i += 1
} }
@@ -257,8 +247,12 @@ private func resolveGatewayEndpoint(opts: ConnectOptions, config: GatewayConfig)
if resolvedMode == "remote" { if resolvedMode == "remote" {
guard let raw = config.remoteUrl?.trimmingCharacters(in: .whitespacesAndNewlines), guard let raw = config.remoteUrl?.trimmingCharacters(in: .whitespacesAndNewlines),
!raw.isEmpty else { !raw.isEmpty
throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "gateway.remote.url is missing"]) else {
throw NSError(
domain: "Gateway",
code: 1,
userInfo: [NSLocalizedDescriptionKey: "gateway.remote.url is missing"])
} }
guard let url = URL(string: raw) else { guard let url = URL(string: raw) else {
throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "invalid url: \(raw)"]) throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "invalid url: \(raw)"])
@@ -273,7 +267,10 @@ private func resolveGatewayEndpoint(opts: ConnectOptions, config: GatewayConfig)
let port = config.port ?? 18789 let port = config.port ?? 18789
let host = resolveLocalHost(bind: config.bind) let host = resolveLocalHost(bind: config.bind)
guard let url = URL(string: "ws://\(host):\(port)") else { guard let url = URL(string: "ws://\(host):\(port)") else {
throw NSError(domain: "Gateway", code: 1, userInfo: [NSLocalizedDescriptionKey: "invalid url: ws://\(host):\(port)"]) throw NSError(
domain: "Gateway",
code: 1,
userInfo: [NSLocalizedDescriptionKey: "invalid url: ws://\(host):\(port)"])
} }
return GatewayEndpoint( return GatewayEndpoint(
url: url, url: url,
@@ -283,7 +280,7 @@ private func resolveGatewayEndpoint(opts: ConnectOptions, config: GatewayConfig)
} }
private func bestEffortEndpoint(opts: ConnectOptions, config: GatewayConfig) -> GatewayEndpoint? { private func bestEffortEndpoint(opts: ConnectOptions, config: GatewayConfig) -> GatewayEndpoint? {
return try? resolveGatewayEndpoint(opts: opts, config: config) try? resolveGatewayEndpoint(opts: opts, config: config)
} }
private func resolvedToken(opts: ConnectOptions, mode: String, config: GatewayConfig) -> String? { private func resolvedToken(opts: ConnectOptions, mode: String, config: GatewayConfig) -> String? {
@@ -3,6 +3,8 @@ import SwiftUI
import Testing import Testing
@testable import Clawdbot @testable import Clawdbot
private typealias SnapshotAnyCodable = Clawdbot.AnyCodable
@Suite(.serialized) @Suite(.serialized)
@MainActor @MainActor
struct ChannelsSettingsSmokeTests { struct ChannelsSettingsSmokeTests {
@@ -17,8 +19,11 @@ struct ChannelsSettingsSmokeTests {
"signal": "Signal", "signal": "Signal",
"imessage": "iMessage", "imessage": "iMessage",
], ],
channelDetailLabels: nil,
channelSystemImages: nil,
channelMeta: nil,
channels: [ channels: [
"whatsapp": AnyCodable([ "whatsapp": SnapshotAnyCodable([
"configured": true, "configured": true,
"linked": true, "linked": true,
"authAgeMs": 86_400_000, "authAgeMs": 86_400_000,
@@ -37,7 +42,7 @@ struct ChannelsSettingsSmokeTests {
"lastEventAt": 1_700_000_060_000, "lastEventAt": 1_700_000_060_000,
"lastError": "needs login", "lastError": "needs login",
]), ]),
"telegram": AnyCodable([ "telegram": SnapshotAnyCodable([
"configured": true, "configured": true,
"tokenSource": "env", "tokenSource": "env",
"running": true, "running": true,
@@ -52,7 +57,7 @@ struct ChannelsSettingsSmokeTests {
], ],
"lastProbeAt": 1_700_000_050_000, "lastProbeAt": 1_700_000_050_000,
]), ]),
"signal": AnyCodable([ "signal": SnapshotAnyCodable([
"configured": true, "configured": true,
"baseUrl": "http://127.0.0.1:8080", "baseUrl": "http://127.0.0.1:8080",
"running": true, "running": true,
@@ -65,7 +70,7 @@ struct ChannelsSettingsSmokeTests {
], ],
"lastProbeAt": 1_700_000_050_000, "lastProbeAt": 1_700_000_050_000,
]), ]),
"imessage": AnyCodable([ "imessage": SnapshotAnyCodable([
"configured": false, "configured": false,
"running": false, "running": false,
"lastError": "not configured", "lastError": "not configured",
@@ -100,15 +105,18 @@ struct ChannelsSettingsSmokeTests {
"signal": "Signal", "signal": "Signal",
"imessage": "iMessage", "imessage": "iMessage",
], ],
channelDetailLabels: nil,
channelSystemImages: nil,
channelMeta: nil,
channels: [ channels: [
"whatsapp": AnyCodable([ "whatsapp": SnapshotAnyCodable([
"configured": false, "configured": false,
"linked": false, "linked": false,
"running": false, "running": false,
"connected": false, "connected": false,
"reconnectAttempts": 0, "reconnectAttempts": 0,
]), ]),
"telegram": AnyCodable([ "telegram": SnapshotAnyCodable([
"configured": false, "configured": false,
"running": false, "running": false,
"lastError": "bot missing", "lastError": "bot missing",
@@ -120,7 +128,7 @@ struct ChannelsSettingsSmokeTests {
], ],
"lastProbeAt": 1_700_000_100_000, "lastProbeAt": 1_700_000_100_000,
]), ]),
"signal": AnyCodable([ "signal": SnapshotAnyCodable([
"configured": false, "configured": false,
"baseUrl": "http://127.0.0.1:8080", "baseUrl": "http://127.0.0.1:8080",
"running": false, "running": false,
@@ -133,7 +141,7 @@ struct ChannelsSettingsSmokeTests {
], ],
"lastProbeAt": 1_700_000_200_000, "lastProbeAt": 1_700_000_200_000,
]), ]),
"imessage": AnyCodable([ "imessage": SnapshotAnyCodable([
"configured": false, "configured": false,
"running": false, "running": false,
"lastError": "not configured", "lastError": "not configured",
@@ -11,16 +11,19 @@ struct CronJobEditorSmokeTests {
} }
@Test func cronJobEditorBuildsBodyForNewJob() { @Test func cronJobEditorBuildsBodyForNewJob() {
let channelsStore = ChannelsStore(isPreview: true)
let view = CronJobEditor( let view = CronJobEditor(
job: nil, job: nil,
isSaving: .constant(false), isSaving: .constant(false),
error: .constant(nil), error: .constant(nil),
channelsStore: channelsStore,
onCancel: {}, onCancel: {},
onSave: { _ in }) onSave: { _ in })
_ = view.body _ = view.body
} }
@Test func cronJobEditorBuildsBodyForExistingJob() { @Test func cronJobEditorBuildsBodyForExistingJob() {
let channelsStore = ChannelsStore(isPreview: true)
let job = CronJob( let job = CronJob(
id: "job-1", id: "job-1",
agentId: "ops", agentId: "ops",
@@ -54,31 +57,36 @@ struct CronJobEditorSmokeTests {
job: job, job: job,
isSaving: .constant(false), isSaving: .constant(false),
error: .constant(nil), error: .constant(nil),
channelsStore: channelsStore,
onCancel: {}, onCancel: {},
onSave: { _ in }) onSave: { _ in })
_ = view.body _ = view.body
} }
@Test func cronJobEditorExercisesBuilders() { @Test func cronJobEditorExercisesBuilders() {
let channelsStore = ChannelsStore(isPreview: true)
var view = CronJobEditor( var view = CronJobEditor(
job: nil, job: nil,
isSaving: .constant(false), isSaving: .constant(false),
error: .constant(nil), error: .constant(nil),
channelsStore: channelsStore,
onCancel: {}, onCancel: {},
onSave: { _ in }) onSave: { _ in })
view.exerciseForTesting() view.exerciseForTesting()
} }
@Test func cronJobEditorIncludesDeleteAfterRunForAtSchedule() throws { @Test func cronJobEditorIncludesDeleteAfterRunForAtSchedule() throws {
let channelsStore = ChannelsStore(isPreview: true)
let view = CronJobEditor( let view = CronJobEditor(
job: nil, job: nil,
isSaving: .constant(false), isSaving: .constant(false),
error: .constant(nil), error: .constant(nil),
channelsStore: channelsStore,
onCancel: {}, onCancel: {},
onSave: { _ in }) onSave: { _ in })
var root: [String: Any] = [:] var root: [String: Any] = [:]
view.applyDeleteAfterRun(to: &root, scheduleKind: .at, deleteAfterRun: true) view.applyDeleteAfterRun(to: &root, scheduleKind: CronJobEditor.ScheduleKind.at, deleteAfterRun: true)
let raw = root["deleteAfterRun"] as? Bool let raw = root["deleteAfterRun"] as? Bool
#expect(raw == true) #expect(raw == true)
} }
@@ -1,3 +1,4 @@
import ClawdbotKit
import Foundation import Foundation
import os import os
import Testing import Testing
@@ -1,3 +1,4 @@
import ClawdbotKit
import Foundation import Foundation
import os import os
import Testing import Testing
@@ -1,3 +1,4 @@
import ClawdbotKit
import Foundation import Foundation
import os import os
import Testing import Testing
@@ -1,3 +1,4 @@
import ClawdbotKit
import Foundation import Foundation
import os import os
import Testing import Testing
@@ -1,3 +1,4 @@
import ClawdbotKit
import Foundation import Foundation
import Testing import Testing
@testable import Clawdbot @testable import Clawdbot
@@ -139,4 +139,40 @@ import Testing
let resolved = ConnectionModeResolver.resolve(root: root, defaults: defaults) let resolved = ConnectionModeResolver.resolve(root: root, defaults: defaults)
#expect(resolved.mode == .remote) #expect(resolved.mode == .remote)
} }
@Test func resolveLocalGatewayHostPrefersTailnetForAuto() {
let host = GatewayEndpointStore._testResolveLocalGatewayHost(
bindMode: "auto",
tailscaleIP: "100.64.1.2")
#expect(host == "100.64.1.2")
}
@Test func resolveLocalGatewayHostFallsBackToLoopbackForAuto() {
let host = GatewayEndpointStore._testResolveLocalGatewayHost(
bindMode: "auto",
tailscaleIP: nil)
#expect(host == "127.0.0.1")
}
@Test func resolveLocalGatewayHostPrefersTailnetForTailnetMode() {
let host = GatewayEndpointStore._testResolveLocalGatewayHost(
bindMode: "tailnet",
tailscaleIP: "100.64.1.5")
#expect(host == "100.64.1.5")
}
@Test func resolveLocalGatewayHostFallsBackToLoopbackForTailnetMode() {
let host = GatewayEndpointStore._testResolveLocalGatewayHost(
bindMode: "tailnet",
tailscaleIP: nil)
#expect(host == "127.0.0.1")
}
@Test func resolveLocalGatewayHostUsesCustomBindHost() {
let host = GatewayEndpointStore._testResolveLocalGatewayHost(
bindMode: "custom",
tailscaleIP: "100.64.1.9",
customBindHost: "192.168.1.10")
#expect(host == "192.168.1.10")
}
} }
@@ -7,15 +7,17 @@ import Testing
@Suite(.serialized) @Suite(.serialized)
struct LowCoverageHelperTests { struct LowCoverageHelperTests {
private typealias ProtoAnyCodable = ClawdbotProtocol.AnyCodable
@Test func anyCodableHelperAccessors() throws { @Test func anyCodableHelperAccessors() throws {
let payload: [String: AnyCodable] = [ let payload: [String: ProtoAnyCodable] = [
"title": AnyCodable("Hello"), "title": ProtoAnyCodable("Hello"),
"flag": AnyCodable(true), "flag": ProtoAnyCodable(true),
"count": AnyCodable(3), "count": ProtoAnyCodable(3),
"ratio": AnyCodable(1.25), "ratio": ProtoAnyCodable(1.25),
"list": AnyCodable([AnyCodable("a"), AnyCodable(2)]), "list": ProtoAnyCodable([ProtoAnyCodable("a"), ProtoAnyCodable(2)]),
] ]
let any = AnyCodable(payload) let any = ProtoAnyCodable(payload)
let dict = try #require(any.dictionaryValue) let dict = try #require(any.dictionaryValue)
#expect(dict["title"]?.stringValue == "Hello") #expect(dict["title"]?.stringValue == "Hello")
#expect(dict["flag"]?.boolValue == true) #expect(dict["flag"]?.boolValue == true)
@@ -76,31 +78,27 @@ struct LowCoverageHelperTests {
#expect(result.stderr.contains("stderr-1999")) #expect(result.stderr.contains("stderr-1999"))
} }
@Test func pairedNodesStorePersists() async throws { @Test func nodeInfoCodableRoundTrip() throws {
let dir = FileManager().temporaryDirectory let info = NodeInfo(
.appendingPathComponent("paired-\(UUID().uuidString)", isDirectory: true)
try FileManager().createDirectory(at: dir, withIntermediateDirectories: true)
let url = dir.appendingPathComponent("nodes.json")
let store = PairedNodesStore(fileURL: url)
await store.load()
#expect(await store.all().isEmpty)
let node = PairedNode(
nodeId: "node-1", nodeId: "node-1",
displayName: "Node One", displayName: "Node One",
platform: "macOS", platform: "macOS",
version: "1.0", version: "1.0",
coreVersion: "1.0-core",
uiVersion: "1.0-ui",
deviceFamily: "Mac", deviceFamily: "Mac",
modelIdentifier: "MacBookPro", modelIdentifier: "MacBookPro",
token: "token", remoteIp: "192.168.1.2",
createdAtMs: 1, caps: ["chat"],
lastSeenAtMs: nil) commands: ["send"],
try await store.upsert(node) permissions: ["send": true],
#expect(await store.find(nodeId: "node-1")?.displayName == "Node One") paired: true,
connected: false)
try await store.touchSeen(nodeId: "node-1") let data = try JSONEncoder().encode(info)
let updated = await store.find(nodeId: "node-1") let decoded = try JSONDecoder().decode(NodeInfo.self, from: data)
#expect(updated?.lastSeenAtMs != nil) #expect(decoded.nodeId == "node-1")
#expect(decoded.isPaired == true)
#expect(decoded.isConnected == false)
} }
@Test @MainActor func presenceReporterHelpers() { @Test @MainActor func presenceReporterHelpers() {
@@ -21,6 +21,7 @@ import Testing
features: [:], features: [:],
snapshot: snapshot, snapshot: snapshot,
canvashosturl: nil, canvashosturl: nil,
auth: nil,
policy: [:]) policy: [:])
let mapped = MacGatewayChatTransport.mapPushToTransportEvent(.snapshot(hello)) let mapped = MacGatewayChatTransport.mapPushToTransportEvent(.snapshot(hello))
@@ -3,13 +3,15 @@ import SwiftUI
import Testing import Testing
@testable import Clawdbot @testable import Clawdbot
private typealias ProtoAnyCodable = ClawdbotProtocol.AnyCodable
@Suite(.serialized) @Suite(.serialized)
@MainActor @MainActor
struct OnboardingWizardStepViewTests { struct OnboardingWizardStepViewTests {
@Test func noteStepBuilds() { @Test func noteStepBuilds() {
let step = WizardStep( let step = WizardStep(
id: "step-1", id: "step-1",
type: AnyCodable("note"), type: ProtoAnyCodable("note"),
title: "Welcome", title: "Welcome",
message: "Hello", message: "Hello",
options: nil, options: nil,
@@ -22,17 +24,17 @@ struct OnboardingWizardStepViewTests {
} }
@Test func selectStepBuilds() { @Test func selectStepBuilds() {
let options: [[String: AnyCodable]] = [ let options: [[String: ProtoAnyCodable]] = [
["value": AnyCodable("local"), "label": AnyCodable("Local"), "hint": AnyCodable("This Mac")], ["value": ProtoAnyCodable("local"), "label": ProtoAnyCodable("Local"), "hint": ProtoAnyCodable("This Mac")],
["value": AnyCodable("remote"), "label": AnyCodable("Remote")], ["value": ProtoAnyCodable("remote"), "label": ProtoAnyCodable("Remote")],
] ]
let step = WizardStep( let step = WizardStep(
id: "step-2", id: "step-2",
type: AnyCodable("select"), type: ProtoAnyCodable("select"),
title: "Mode", title: "Mode",
message: "Choose a mode", message: "Choose a mode",
options: options, options: options,
initialvalue: AnyCodable("local"), initialvalue: ProtoAnyCodable("local"),
placeholder: nil, placeholder: nil,
sensitive: nil, sensitive: nil,
executor: nil) executor: nil)
@@ -15,6 +15,9 @@ extension URLSessionWebSocketTask: WebSocketTasking {}
public struct WebSocketTaskBox: @unchecked Sendable { public struct WebSocketTaskBox: @unchecked Sendable {
public let task: any WebSocketTasking public let task: any WebSocketTasking
public init(task: any WebSocketTasking) {
self.task = task
}
public var state: URLSessionTask.State { self.task.state } public var state: URLSessionTask.State { self.task.state }
@@ -180,8 +180,10 @@ public actor GatewayNodeSession {
"id": AnyCodable(request.id), "id": AnyCodable(request.id),
"nodeId": AnyCodable(request.nodeId), "nodeId": AnyCodable(request.nodeId),
"ok": AnyCodable(response.ok), "ok": AnyCodable(response.ok),
"payloadJSON": AnyCodable(response.payloadJSON ?? NSNull()),
] ]
if let payloadJSON = response.payloadJSON {
params["payloadJSON"] = AnyCodable(payloadJSON)
}
if let error = response.error { if let error = response.error {
params["error"] = AnyCodable([ params["error"] = AnyCodable([
"code": AnyCodable(error.code.rawValue), "code": AnyCodable(error.code.rawValue),
+2 -2
View File
@@ -825,9 +825,9 @@ Common options:
- `--url`, `--token`, `--timeout`, `--json` - `--url`, `--token`, `--timeout`, `--json`
Subcommands: Subcommands:
- `nodes status` - `nodes status [--connected] [--last-connected <duration>]`
- `nodes describe --node <id|name|ip>` - `nodes describe --node <id|name|ip>`
- `nodes list` - `nodes list [--connected] [--last-connected <duration>]`
- `nodes pending` - `nodes pending`
- `nodes approve <requestId>` - `nodes approve <requestId>`
- `nodes reject <requestId>` - `nodes reject <requestId>`
+6
View File
@@ -18,12 +18,18 @@ Related:
```bash ```bash
clawdbot nodes list clawdbot nodes list
clawdbot nodes list --connected
clawdbot nodes list --last-connected 24h
clawdbot nodes pending clawdbot nodes pending
clawdbot nodes approve <requestId> clawdbot nodes approve <requestId>
clawdbot nodes status clawdbot nodes status
clawdbot nodes status --connected
clawdbot nodes status --last-connected 24h
``` ```
`nodes list` prints pending/paired tables. Paired rows include the most recent connect age (Last Connect). `nodes list` prints pending/paired tables. Paired rows include the most recent connect age (Last Connect).
Use `--connected` to only show currently-connected nodes. Use `--last-connected <duration>` to
filter to nodes that connected within a duration (e.g. `24h`, `7d`).
## Invoke / run ## Invoke / run
+78
View File
@@ -34,6 +34,81 @@ clawdbot nodes rename --node <idOrNameOrIp> --name "Kitchen iPad"
Notes: Notes:
- `nodes rename` stores a display name override in the gateway pairing store. - `nodes rename` stores a display name override in the gateway pairing store.
## Remote node host (system.run)
Use a **node host** when your Gateway runs on one machine and you want commands
to execute on another. The model still talks to the **gateway**; the gateway
forwards `exec` calls to the **node host** when `host=node` is selected.
### What runs where
- **Gateway host**: receives messages, runs the model, routes tool calls.
- **Node host**: executes `system.run`/`system.which` on the node machine.
- **Approvals**: enforced on the node host via `~/.clawdbot/exec-approvals.json`.
### Start a node host (foreground)
On the node machine:
```bash
clawdbot node start --host <gateway-host> --port 18789 --display-name "Build Node"
```
### Start a node host (service)
```bash
clawdbot node service install --host <gateway-host> --port 18789 --display-name "Build Node"
clawdbot node service start
```
### Pair + name
On the gateway host:
```bash
clawdbot nodes pending
clawdbot nodes approve <requestId>
clawdbot nodes list
```
Naming options:
- `--display-name` on `clawdbot node start/service install` (persists in `~/.clawdbot/node.json` on the node).
- `clawdbot nodes rename --node <id|name|ip> --name "Build Node"` (gateway override).
### Allowlist the commands
Exec approvals are **per node host**. Add allowlist entries from the gateway:
```bash
clawdbot approvals allowlist add --node <id|name|ip> "/usr/bin/uname"
clawdbot approvals allowlist add --node <id|name|ip> "/usr/bin/sw_vers"
```
Approvals live on the node host at `~/.clawdbot/exec-approvals.json`.
### Point exec at the node
Configure defaults (gateway config):
```bash
clawdbot config set tools.exec.host node
clawdbot config set tools.exec.security allowlist
clawdbot config set tools.exec.node "<id-or-name>"
```
Or per session:
```
/exec host=node security=allowlist node=<id-or-name>
```
Once set, any `exec` call with `host=node` runs on the node host (subject to the
node allowlist/approvals).
Related:
- [Node host CLI](/cli/node)
- [Exec tool](/tools/exec)
- [Exec approvals](/tools/exec-approvals)
## Invoking commands ## Invoking commands
Low-level (raw RPC): Low-level (raw RPC):
@@ -214,6 +289,9 @@ Notes:
- The node host stores its node id + pairing token in `~/.clawdbot/node.json`. - The node host stores its node id + pairing token in `~/.clawdbot/node.json`.
- Exec approvals are enforced locally via `~/.clawdbot/exec-approvals.json` - Exec approvals are enforced locally via `~/.clawdbot/exec-approvals.json`
(see [Exec approvals](/tools/exec-approvals)). (see [Exec approvals](/tools/exec-approvals)).
- On macOS, the headless node host prefers the companion app exec host when reachable and falls
back to local execution if the app is unavailable. Set `CLAWDBOT_NODE_EXEC_HOST=app` to require
the app, or `CLAWDBOT_NODE_EXEC_FALLBACK=0` to disable fallback.
- Add `--tls` / `--tls-fingerprint` when the bridge requires TLS. - Add `--tls` / `--tls-fingerprint` when the bridge requires TLS.
## Mac node mode ## Mac node mode
+1 -1
View File
@@ -39,7 +39,7 @@ Notes:
- `tools.exec.notifyOnExit` (default: true): when true, backgrounded exec sessions enqueue a system event and request a heartbeat on exit. - `tools.exec.notifyOnExit` (default: true): when true, backgrounded exec sessions enqueue a system event and request a heartbeat on exit.
- `tools.exec.host` (default: `sandbox`) - `tools.exec.host` (default: `sandbox`)
- `tools.exec.security` (default: `deny`) - `tools.exec.security` (default: `deny` for sandbox, `allowlist` for gateway + node when unset)
- `tools.exec.ask` (default: `on-miss`) - `tools.exec.ask` (default: `on-miss`)
- `tools.exec.node` (default: unset) - `tools.exec.node` (default: unset)
- `tools.exec.pathPrepend`: list of directories to prepend to `PATH` for exec runs. - `tools.exec.pathPrepend`: list of directories to prepend to `PATH` for exec runs.
+2 -1
View File
@@ -210,6 +210,7 @@
"@types/proper-lockfile": "^4.1.4", "@types/proper-lockfile": "^4.1.4",
"@types/qrcode-terminal": "^0.12.2", "@types/qrcode-terminal": "^0.12.2",
"@types/ws": "^8.18.1", "@types/ws": "^8.18.1",
"@typescript/native-preview": "7.0.0-dev.20260120.1",
"@vitest/coverage-v8": "^4.0.17", "@vitest/coverage-v8": "^4.0.17",
"docx-preview": "^0.3.7", "docx-preview": "^0.3.7",
"lit": "^3.3.2", "lit": "^3.3.2",
@@ -231,7 +232,7 @@
"overrides": { "overrides": {
"@sinclair/typebox": "0.34.47", "@sinclair/typebox": "0.34.47",
"hono": "4.11.4", "hono": "4.11.4",
"tar": "7.5.3" "tar": "7.5.4"
}, },
"patchedDependencies": { "patchedDependencies": {
"@mariozechner/pi-ai@0.49.2": "patches/@mariozechner__pi-ai@0.49.2.patch" "@mariozechner/pi-ai@0.49.2": "patches/@mariozechner__pi-ai@0.49.2.patch"
+80 -8
View File
@@ -7,7 +7,7 @@ settings:
overrides: overrides:
'@sinclair/typebox': 0.34.47 '@sinclair/typebox': 0.34.47
hono: 4.11.4 hono: 4.11.4
tar: 7.5.3 tar: 7.5.4
patchedDependencies: patchedDependencies:
'@mariozechner/pi-ai@0.49.2': '@mariozechner/pi-ai@0.49.2':
@@ -151,8 +151,8 @@ importers:
specifier: 0.1.7-alpha.2 specifier: 0.1.7-alpha.2
version: 0.1.7-alpha.2 version: 0.1.7-alpha.2
tar: tar:
specifier: 7.5.3 specifier: 7.5.4
version: 7.5.3 version: 7.5.4
tslog: tslog:
specifier: ^4.10.2 specifier: ^4.10.2
version: 4.10.2 version: 4.10.2
@@ -202,6 +202,9 @@ importers:
'@types/ws': '@types/ws':
specifier: ^8.18.1 specifier: ^8.18.1
version: 8.18.1 version: 8.18.1
'@typescript/native-preview':
specifier: 7.0.0-dev.20260120.1
version: 7.0.0-dev.20260120.1
'@vitest/coverage-v8': '@vitest/coverage-v8':
specifier: ^4.0.17 specifier: ^4.0.17
version: 4.0.17(@vitest/browser@4.0.17(vite@7.3.1(@types/node@25.0.9)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0)(yaml@2.8.2))(vitest@4.0.17))(vitest@4.0.17) version: 4.0.17(@vitest/browser@4.0.17(vite@7.3.1(@types/node@25.0.9)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0)(yaml@2.8.2))(vitest@4.0.17))(vitest@4.0.17)
@@ -2530,6 +2533,45 @@ packages:
'@types/ws@8.18.1': '@types/ws@8.18.1':
resolution: {integrity: sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==} resolution: {integrity: sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==}
'@typescript/native-preview-darwin-arm64@7.0.0-dev.20260120.1':
resolution: {integrity: sha512-r3pWFuR2H7mn6ScwpH5jJljKQqKto0npVuJSk6pRwFwexpTyxOGmJTZJ1V0AWiisaNxU2+CNAqWFJSJYIE/QTg==}
cpu: [arm64]
os: [darwin]
'@typescript/native-preview-darwin-x64@7.0.0-dev.20260120.1':
resolution: {integrity: sha512-cuC1+wLbUP+Ip2UT94G134fqRdp5w3b3dhcCO6/FQ4yXxvRNyv/WK+upHBUFDaeSOeHgDTyO9/QFYUWwC4If1A==}
cpu: [x64]
os: [darwin]
'@typescript/native-preview-linux-arm64@7.0.0-dev.20260120.1':
resolution: {integrity: sha512-zZGvEGY7wcHYefMZ87KNmvjN3NLIhsCMHEpHZiGCS3khKf+8z6ZsanrzCjOTodvL01VPyBzHxV1EtkSxAcLiQg==}
cpu: [arm64]
os: [linux]
'@typescript/native-preview-linux-arm@7.0.0-dev.20260120.1':
resolution: {integrity: sha512-vN6OYVySol/kQZjJGmAzd6L30SyVlCgmCXS8WjUYtE5clN0YrzQHop16RK29fYZHMxpkOniVBtRPxUYQANZBlQ==}
cpu: [arm]
os: [linux]
'@typescript/native-preview-linux-x64@7.0.0-dev.20260120.1':
resolution: {integrity: sha512-JBfNhWd/asd5MDeS3VgRvE24pGKBkmvLub6tsux6ypr+Yhy+o0WaAEzVpmlRYZUqss2ai5tvOu4dzPBXzZAtFw==}
cpu: [x64]
os: [linux]
'@typescript/native-preview-win32-arm64@7.0.0-dev.20260120.1':
resolution: {integrity: sha512-tTndRtYCq2xwgE0VkTi9ACNiJaV43+PqvBqCxk8ceYi3X36Ve+CCnwlZfZJ4k9NxZthtrAwF/kUmpC9iIYbq1w==}
cpu: [arm64]
os: [win32]
'@typescript/native-preview-win32-x64@7.0.0-dev.20260120.1':
resolution: {integrity: sha512-oZia7hFL6k9pVepfonuPI86Jmyz6WlJKR57tWCDwRNmpA7odxuTq1PbvcYgy1z4+wHF1nnKKJY0PMAiq6ac18w==}
cpu: [x64]
os: [win32]
'@typescript/native-preview@7.0.0-dev.20260120.1':
resolution: {integrity: sha512-nnEf37C9ue7OBRnF2zmV/OCBmV5Y7T/K4mCHa+nxgiXcF/1w8sA0cgdFl+gHQ0mysqUJ+Bu5btAMeWgpLyjrgg==}
hasBin: true
'@typespec/ts-http-runtime@0.3.2': '@typespec/ts-http-runtime@0.3.2':
resolution: {integrity: sha512-IlqQ/Gv22xUC1r/WQm4StLkYQmaaTsXAhUVsNE0+xiyf0yRFiH5++q78U3bw6bLKDCTmh0uqKB9eG9+Bt75Dkg==} resolution: {integrity: sha512-IlqQ/Gv22xUC1r/WQm4StLkYQmaaTsXAhUVsNE0+xiyf0yRFiH5++q78U3bw6bLKDCTmh0uqKB9eG9+Bt75Dkg==}
engines: {node: '>=20.0.0'} engines: {node: '>=20.0.0'}
@@ -4863,10 +4905,9 @@ packages:
tailwindcss@4.1.17: tailwindcss@4.1.17:
resolution: {integrity: sha512-j9Ee2YjuQqYT9bbRTfTZht9W/ytp5H+jJpZKiYdP/bpnXARAuELt9ofP0lPnmHjbga7SNQIxdTAXCmtKVYjN+Q==} resolution: {integrity: sha512-j9Ee2YjuQqYT9bbRTfTZht9W/ytp5H+jJpZKiYdP/bpnXARAuELt9ofP0lPnmHjbga7SNQIxdTAXCmtKVYjN+Q==}
tar@7.5.3: tar@7.5.4:
resolution: {integrity: sha512-ENg5JUHUm2rDD7IvKNFGzyElLXNjachNLp6RaGf4+JOgxXHkqA+gq81ZAMCUmtMtqBsoU62lcp6S27g1LCYGGQ==} resolution: {integrity: sha512-AN04xbWGrSTDmVwlI4/GTlIIwMFk/XEv7uL8aa57zuvRy6s4hdBed+lVq2fAZ89XDa7Us3ANXcE3Tvqvja1kTA==}
engines: {node: '>=18'} engines: {node: '>=18'}
deprecated: Old versions of tar are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exhorbitant rates) by contacting i@izs.me
thenify-all@1.6.0: thenify-all@1.6.0:
resolution: {integrity: sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA==} resolution: {integrity: sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA==}
@@ -7699,6 +7740,37 @@ snapshots:
dependencies: dependencies:
'@types/node': 25.0.9 '@types/node': 25.0.9
'@typescript/native-preview-darwin-arm64@7.0.0-dev.20260120.1':
optional: true
'@typescript/native-preview-darwin-x64@7.0.0-dev.20260120.1':
optional: true
'@typescript/native-preview-linux-arm64@7.0.0-dev.20260120.1':
optional: true
'@typescript/native-preview-linux-arm@7.0.0-dev.20260120.1':
optional: true
'@typescript/native-preview-linux-x64@7.0.0-dev.20260120.1':
optional: true
'@typescript/native-preview-win32-arm64@7.0.0-dev.20260120.1':
optional: true
'@typescript/native-preview-win32-x64@7.0.0-dev.20260120.1':
optional: true
'@typescript/native-preview@7.0.0-dev.20260120.1':
optionalDependencies:
'@typescript/native-preview-darwin-arm64': 7.0.0-dev.20260120.1
'@typescript/native-preview-darwin-x64': 7.0.0-dev.20260120.1
'@typescript/native-preview-linux-arm': 7.0.0-dev.20260120.1
'@typescript/native-preview-linux-arm64': 7.0.0-dev.20260120.1
'@typescript/native-preview-linux-x64': 7.0.0-dev.20260120.1
'@typescript/native-preview-win32-arm64': 7.0.0-dev.20260120.1
'@typescript/native-preview-win32-x64': 7.0.0-dev.20260120.1
'@typespec/ts-http-runtime@0.3.2': '@typespec/ts-http-runtime@0.3.2':
dependencies: dependencies:
http-proxy-agent: 7.0.2 http-proxy-agent: 7.0.2
@@ -8196,7 +8268,7 @@ snapshots:
npmlog: 6.0.2 npmlog: 6.0.2
rc: 1.2.8 rc: 1.2.8
semver: 7.7.3 semver: 7.7.3
tar: 7.5.3 tar: 7.5.4
url-join: 4.0.1 url-join: 4.0.1
which: 2.0.2 which: 2.0.2
yargs: 17.7.2 yargs: 17.7.2
@@ -10438,7 +10510,7 @@ snapshots:
tailwindcss@4.1.17: {} tailwindcss@4.1.17: {}
tar@7.5.3: tar@7.5.4:
dependencies: dependencies:
'@isaacs/fs-minipass': 4.0.1 '@isaacs/fs-minipass': 4.0.1
chownr: 3.0.0 chownr: 3.0.0
+3 -2
View File
@@ -7,6 +7,8 @@ import process from "node:process";
const args = process.argv.slice(2); const args = process.argv.slice(2);
const env = { ...process.env }; const env = { ...process.env };
const cwd = process.cwd(); const cwd = process.cwd();
const compiler = env.CLAWDBOT_TS_COMPILER === "tsc" ? "tsc" : "tsgo";
const projectArgs = ["--project", "tsconfig.json"];
const distRoot = path.join(cwd, "dist"); const distRoot = path.join(cwd, "dist");
const distEntry = path.join(distRoot, "entry.js"); const distEntry = path.join(distRoot, "entry.js");
@@ -110,11 +112,10 @@ const writeBuildStamp = () => {
}; };
if (!shouldBuild()) { if (!shouldBuild()) {
logRunner("Skipping build; dist is fresh.");
runNode(); runNode();
} else { } else {
logRunner("Building TypeScript (dist is stale)."); logRunner("Building TypeScript (dist is stale).");
const build = spawn("pnpm", ["exec", "tsc", "-p", "tsconfig.json"], { const build = spawn("pnpm", ["exec", compiler, ...projectArgs], {
cwd, cwd,
env, env,
stdio: "inherit", stdio: "inherit",
+11 -4
View File
@@ -5,8 +5,10 @@ import process from "node:process";
const args = process.argv.slice(2); const args = process.argv.slice(2);
const env = { ...process.env }; const env = { ...process.env };
const cwd = process.cwd(); const cwd = process.cwd();
const compiler = env.CLAWDBOT_TS_COMPILER === "tsc" ? "tsc" : "tsgo";
const projectArgs = ["--project", "tsconfig.json"];
const initialBuild = spawnSync("pnpm", ["exec", "tsc", "-p", "tsconfig.json"], { const initialBuild = spawnSync("pnpm", ["exec", compiler, ...projectArgs], {
cwd, cwd,
env, env,
stdio: "inherit", stdio: "inherit",
@@ -16,7 +18,12 @@ if (initialBuild.status !== 0) {
process.exit(initialBuild.status ?? 1); process.exit(initialBuild.status ?? 1);
} }
const tsc = spawn("pnpm", ["exec", "tsc", "--watch", "--preserveWatchOutput"], { const watchArgs =
compiler === "tsc"
? [...projectArgs, "--watch", "--preserveWatchOutput"]
: [...projectArgs, "--watch"];
const compilerProcess = spawn("pnpm", ["exec", compiler, ...watchArgs], {
cwd, cwd,
env, env,
stdio: "inherit", stdio: "inherit",
@@ -34,14 +41,14 @@ function cleanup(code = 0) {
if (exiting) return; if (exiting) return;
exiting = true; exiting = true;
nodeProcess.kill("SIGTERM"); nodeProcess.kill("SIGTERM");
tsc.kill("SIGTERM"); compilerProcess.kill("SIGTERM");
process.exit(code); process.exit(code);
} }
process.on("SIGINT", () => cleanup(130)); process.on("SIGINT", () => cleanup(130));
process.on("SIGTERM", () => cleanup(143)); process.on("SIGTERM", () => cleanup(143));
tsc.on("exit", (code) => { compilerProcess.on("exit", (code) => {
if (exiting) return; if (exiting) return;
cleanup(code ?? 1); cleanup(code ?? 1);
}); });
+9 -39
View File
@@ -400,7 +400,7 @@ export function createExecTool(
host = "gateway"; host = "gateway";
} }
const configuredSecurity = defaults?.security ?? "deny"; const configuredSecurity = defaults?.security ?? (host === "sandbox" ? "deny" : "allowlist");
const requestedSecurity = normalizeExecSecurity(params.security); const requestedSecurity = normalizeExecSecurity(params.security);
let security = minSecurity(configuredSecurity, requestedSecurity ?? configuredSecurity); let security = minSecurity(configuredSecurity, requestedSecurity ?? configuredSecurity);
if (elevatedRequested) { if (elevatedRequested) {
@@ -447,7 +447,10 @@ export function createExecTool(
applyPathPrepend(env, defaultPathPrepend); applyPathPrepend(env, defaultPathPrepend);
if (host === "node") { if (host === "node") {
const approvals = resolveExecApprovals(defaults?.agentId); const approvals = resolveExecApprovals(
defaults?.agentId,
host === "node" ? { security: "allowlist" } : undefined,
);
const hostSecurity = minSecurity(security, approvals.agent.security); const hostSecurity = minSecurity(security, approvals.agent.security);
const hostAsk = maxAsk(ask, approvals.agent.ask); const hostAsk = maxAsk(ask, approvals.agent.ask);
const askFallback = approvals.agent.askFallback; const askFallback = approvals.agent.askFallback;
@@ -491,12 +494,7 @@ export function createExecTool(
if (nodeEnv) { if (nodeEnv) {
applyPathPrepend(nodeEnv, defaultPathPrepend, { requireExisting: true }); applyPathPrepend(nodeEnv, defaultPathPrepend, { requireExisting: true });
} }
const resolution = resolveCommandResolution(params.command, workdir, env); const requiresAsk = hostAsk === "always" || hostAsk === "on-miss";
const allowlistMatch =
hostSecurity === "allowlist" ? matchAllowlist(approvals.allowlist, resolution) : null;
const requiresAsk =
hostAsk === "always" ||
(hostAsk === "on-miss" && hostSecurity === "allowlist" && !allowlistMatch);
let approvedByAsk = false; let approvedByAsk = false;
let approvalDecision: "allow-once" | "allow-always" | null = null; let approvalDecision: "allow-once" | "allow-always" | null = null;
@@ -511,7 +509,7 @@ export function createExecTool(
security: hostSecurity, security: hostSecurity,
ask: hostAsk, ask: hostAsk,
agentId: defaults?.agentId, agentId: defaults?.agentId,
resolvedPath: resolution?.resolvedPath ?? null, resolvedPath: null,
sessionKey: defaults?.sessionKey ?? null, sessionKey: defaults?.sessionKey ?? null,
timeoutMs: 120_000, timeoutMs: 120_000,
}, },
@@ -529,11 +527,7 @@ export function createExecTool(
approvedByAsk = true; approvedByAsk = true;
approvalDecision = "allow-once"; approvalDecision = "allow-once";
} else if (askFallback === "allowlist") { } else if (askFallback === "allowlist") {
if (!allowlistMatch) { // Defer allowlist enforcement to the node host.
throw new Error("exec denied: approval required (approval UI not available)");
}
approvedByAsk = true;
approvalDecision = "allow-once";
} else { } else {
throw new Error("exec denied: approval required (approval UI not available)"); throw new Error("exec denied: approval required (approval UI not available)");
} }
@@ -545,32 +539,8 @@ export function createExecTool(
if (decision === "allow-always") { if (decision === "allow-always") {
approvedByAsk = true; approvedByAsk = true;
approvalDecision = "allow-always"; approvalDecision = "allow-always";
if (hostSecurity === "allowlist") {
const pattern =
resolution?.resolvedPath ??
resolution?.rawExecutable ??
params.command.split(/\s+/).shift() ??
"";
if (pattern) {
addAllowlistEntry(approvals.file, defaults?.agentId, pattern);
}
}
} }
} }
if (hostSecurity === "allowlist" && !allowlistMatch && !approvedByAsk) {
throw new Error("exec denied: allowlist miss");
}
if (allowlistMatch) {
recordAllowlistUse(
approvals.file,
defaults?.agentId,
allowlistMatch,
params.command,
resolution?.resolvedPath,
);
}
const invokeParams: Record<string, unknown> = { const invokeParams: Record<string, unknown> = {
nodeId, nodeId,
command: "system.run", command: "system.run",
@@ -616,7 +586,7 @@ export function createExecTool(
} }
if (host === "gateway") { if (host === "gateway") {
const approvals = resolveExecApprovals(defaults?.agentId); const approvals = resolveExecApprovals(defaults?.agentId, { security: "allowlist" });
const hostSecurity = minSecurity(security, approvals.agent.security); const hostSecurity = minSecurity(security, approvals.agent.security);
const hostAsk = maxAsk(ask, approvals.agent.ask); const hostAsk = maxAsk(ask, approvals.agent.ask);
const askFallback = approvals.agent.askFallback; const askFallback = approvals.agent.askFallback;
@@ -0,0 +1,149 @@
import { describe, expect, it, vi } from "vitest";
import type { TemplateContext } from "../templating.js";
import type { FollowupRun, QueueSettings } from "./queue.js";
import { createMockTypingController } from "./test-helpers.js";
const runEmbeddedPiAgentMock = vi.fn();
vi.mock("../../agents/model-fallback.js", () => ({
runWithModelFallback: async ({
run,
}: {
run: (provider: string, model: string) => Promise<unknown>;
}) => ({
// Force a cross-provider fallback candidate
result: await run("openai-codex", "gpt-5.2"),
provider: "openai-codex",
model: "gpt-5.2",
}),
}));
vi.mock("../../agents/pi-embedded.js", () => ({
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
runEmbeddedPiAgent: (params: unknown) => runEmbeddedPiAgentMock(params),
}));
vi.mock("./queue.js", async () => {
const actual = await vi.importActual<typeof import("./queue.js")>("./queue.js");
return {
...actual,
enqueueFollowupRun: vi.fn(),
scheduleFollowupDrain: vi.fn(),
};
});
import { runReplyAgent } from "./agent-runner.js";
function createBaseRun(params: { runOverrides?: Partial<FollowupRun["run"]> }) {
const typing = createMockTypingController();
const sessionCtx = {
Provider: "telegram",
OriginatingTo: "chat",
AccountId: "primary",
MessageSid: "msg",
Surface: "telegram",
} as unknown as TemplateContext;
const resolvedQueue = { mode: "interrupt" } as unknown as QueueSettings;
const followupRun = {
prompt: "hello",
summaryLine: "hello",
enqueuedAt: Date.now(),
run: {
agentId: "main",
agentDir: "/tmp/agent",
sessionId: "session",
sessionKey: "main",
messageProvider: "telegram",
sessionFile: "/tmp/session.jsonl",
workspaceDir: "/tmp",
config: {},
skillsSnapshot: {},
provider: "anthropic",
model: "claude-opus",
authProfileId: "anthropic:clawd",
authProfileIdSource: "manual",
thinkLevel: "low",
verboseLevel: "off",
elevatedLevel: "off",
bashElevated: {
enabled: false,
allowed: false,
defaultLevel: "off",
},
timeoutMs: 5_000,
blockReplyBreak: "message_end",
},
} as unknown as FollowupRun;
return {
typing,
sessionCtx,
resolvedQueue,
followupRun: {
...followupRun,
run: { ...followupRun.run, ...params.runOverrides },
},
};
}
describe("authProfileId fallback scoping", () => {
it("drops authProfileId when provider changes during fallback", async () => {
runEmbeddedPiAgentMock.mockReset();
runEmbeddedPiAgentMock.mockResolvedValue({ payloads: [{ text: "ok" }], meta: {} });
const sessionKey = "main";
const sessionEntry = {
sessionId: "session",
updatedAt: Date.now(),
totalTokens: 1,
compactionCount: 0,
};
const { typing, sessionCtx, resolvedQueue, followupRun } = createBaseRun({
runOverrides: {
provider: "anthropic",
model: "claude-opus",
authProfileId: "anthropic:clawd",
authProfileIdSource: "manual",
},
});
await runReplyAgent({
commandBody: "hello",
followupRun,
queueKey: sessionKey,
resolvedQueue,
shouldSteer: false,
shouldFollowup: false,
isActive: false,
isStreaming: false,
typing,
sessionCtx,
sessionEntry,
sessionStore: { [sessionKey]: sessionEntry },
sessionKey,
storePath: undefined,
defaultModel: "anthropic/claude-opus-4-5",
agentCfgContextTokens: 100_000,
resolvedVerboseLevel: "off",
isNewSession: false,
blockStreamingEnabled: false,
resolvedBlockStreamingBreak: "message_end",
shouldInjectGroupIntro: false,
typingMode: "instant",
});
expect(runEmbeddedPiAgentMock).toHaveBeenCalledTimes(1);
const call = runEmbeddedPiAgentMock.mock.calls[0]?.[0] as {
authProfileId?: unknown;
authProfileIdSource?: unknown;
provider?: unknown;
};
expect(call.provider).toBe("openai-codex");
expect(call.authProfileId).toBeUndefined();
expect(call.authProfileIdSource).toBeUndefined();
});
});
+1 -3
View File
@@ -184,9 +184,7 @@ export async function buildStatusReply(params: {
const requesterKey = resolveInternalSessionKey({ key: sessionKey, alias, mainKey }); const requesterKey = resolveInternalSessionKey({ key: sessionKey, alias, mainKey });
const runs = listSubagentRunsForRequester(requesterKey); const runs = listSubagentRunsForRequester(requesterKey);
const verboseEnabled = resolvedVerboseLevel && resolvedVerboseLevel !== "off"; const verboseEnabled = resolvedVerboseLevel && resolvedVerboseLevel !== "off";
if (runs.length === 0) { if (runs.length > 0) {
if (verboseEnabled) subagentsLine = "🤖 Subagents: none";
} else {
const active = runs.filter((entry) => !entry.endedAt); const active = runs.filter((entry) => !entry.endedAt);
const done = runs.length - active.length; const done = runs.length - active.length;
if (verboseEnabled) { if (verboseEnabled) {
+14
View File
@@ -215,6 +215,20 @@ describe("handleCommands subagents", () => {
expect(result.reply?.text).toContain("Subagents: none"); expect(result.reply?.text).toContain("Subagents: none");
}); });
it("omits subagent status line when none exist", async () => {
resetSubagentRegistryForTests();
const cfg = {
commands: { text: true },
channels: { whatsapp: { allowFrom: ["*"] } },
session: { mainKey: "main", scope: "per-sender" },
} as ClawdbotConfig;
const params = buildParams("/status", cfg);
params.resolvedVerboseLevel = "on";
const result = await handleCommands(params);
expect(result.shouldContinue).toBe(false);
expect(result.reply?.text).not.toContain("Subagents:");
});
it("returns help for unknown subagents action", async () => { it("returns help for unknown subagents action", async () => {
resetSubagentRegistryForTests(); resetSubagentRegistryForTests();
const cfg = { const cfg = {
+16
View File
@@ -143,6 +143,22 @@ describe("buildStatusMessage", () => {
expect(normalized).toContain("Media: image ok (openai/gpt-5.2) · audio skipped (maxBytes)"); expect(normalized).toContain("Media: image ok (openai/gpt-5.2) · audio skipped (maxBytes)");
}); });
it("omits media line when all decisions are none", () => {
const text = buildStatusMessage({
agent: { model: "anthropic/claude-opus-4-5" },
sessionEntry: { sessionId: "media-none", updatedAt: 0 },
sessionKey: "agent:main:main",
queue: { mode: "none" },
mediaDecisions: [
{ capability: "image", outcome: "no-attachment", attachments: [] },
{ capability: "audio", outcome: "no-attachment", attachments: [] },
{ capability: "video", outcome: "no-attachment", attachments: [] },
],
});
expect(normalizeTestText(text)).not.toContain("Media:");
});
it("does not show elevated label when session explicitly disables it", () => { it("does not show elevated label when session explicitly disables it", () => {
const text = buildStatusMessage({ const text = buildStatusMessage({
agent: { model: "anthropic/claude-opus-4-5", elevatedDefault: "on" }, agent: { model: "anthropic/claude-opus-4-5", elevatedDefault: "on" },
+2 -1
View File
@@ -201,8 +201,9 @@ const formatMediaUnderstandingLine = (decisions?: MediaUnderstandingDecision[])
} }
return null; return null;
}) })
.filter(Boolean); .filter((part): part is string => part != null);
if (parts.length === 0) return null; if (parts.length === 0) return null;
if (parts.every((part) => part.endsWith(" none"))) return null;
return `📎 Media: ${parts.join(" · ")}`; return `📎 Media: ${parts.join(" · ")}`;
}; };
+5 -1
View File
@@ -5,6 +5,7 @@ import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { formatCliCommand } from "./command-format.js"; import { formatCliCommand } from "./command-format.js";
import { formatHelpExamples } from "./help-format.js";
import { registerBrowserActionInputCommands } from "./browser-cli-actions-input.js"; import { registerBrowserActionInputCommands } from "./browser-cli-actions-input.js";
import { registerBrowserActionObserveCommands } from "./browser-cli-actions-observe.js"; import { registerBrowserActionObserveCommands } from "./browser-cli-actions-observe.js";
import { registerBrowserDebugCommands } from "./browser-cli-debug.js"; import { registerBrowserDebugCommands } from "./browser-cli-debug.js";
@@ -26,7 +27,10 @@ export function registerBrowserCli(program: Command) {
.addHelpText( .addHelpText(
"after", "after",
() => () =>
`\nExamples:\n ${[...browserCoreExamples, ...browserActionExamples].join("\n ")}\n\n${theme.muted("Docs:")} ${formatDocsLink( `\n${theme.heading("Examples:")}\n${formatHelpExamples(
[...browserCoreExamples, ...browserActionExamples].map((cmd) => [cmd, ""]),
true,
)}\n\n${theme.muted("Docs:")} ${formatDocsLink(
"/cli/browser", "/cli/browser",
"docs.clawd.bot/cli/browser", "docs.clawd.bot/cli/browser",
)}\n`, )}\n`,
+55 -26
View File
@@ -3,6 +3,8 @@ import type { Command } from "commander";
import { callGateway } from "../gateway/call.js"; import { callGateway } from "../gateway/call.js";
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js"; import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { renderTable } from "../terminal/table.js";
import { theme } from "../terminal/theme.js";
import { withProgress } from "./progress.js"; import { withProgress } from "./progress.js";
type DevicesRpcOpts = { type DevicesRpcOpts = {
@@ -96,11 +98,11 @@ function parseDevicePairingList(value: unknown): DevicePairingList {
} }
function formatTokenSummary(tokens: DeviceTokenSummary[] | undefined) { function formatTokenSummary(tokens: DeviceTokenSummary[] | undefined) {
if (!tokens || tokens.length === 0) return "tokens: none"; if (!tokens || tokens.length === 0) return "none";
const parts = tokens const parts = tokens
.map((t) => `${t.role}${t.revokedAtMs ? " (revoked)" : ""}`) .map((t) => `${t.role}${t.revokedAtMs ? " (revoked)" : ""}`)
.sort((a, b) => a.localeCompare(b)); .sort((a, b) => a.localeCompare(b));
return `tokens: ${parts.join(", ")}`; return parts.join(", ");
} }
export function registerDevicesCli(program: Command) { export function registerDevicesCli(program: Command) {
@@ -118,32 +120,59 @@ export function registerDevicesCli(program: Command) {
return; return;
} }
if (list.pending?.length) { if (list.pending?.length) {
defaultRuntime.log("Pending:"); const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
for (const req of list.pending) { defaultRuntime.log(
const name = req.displayName || req.deviceId; `${theme.heading("Pending")} ${theme.muted(`(${list.pending.length})`)}`,
const repair = req.isRepair ? " (repair)" : ""; );
const ip = req.remoteIp ? ` · ${req.remoteIp}` : ""; defaultRuntime.log(
const age = renderTable({
typeof req.ts === "number" ? ` · ${formatAge(Date.now() - req.ts)} ago` : ""; width: tableWidth,
const role = req.role ? ` · role: ${req.role}` : ""; columns: [
defaultRuntime.log(`- ${req.requestId}: ${name}${repair}${role}${ip}${age}`); { key: "Request", header: "Request", minWidth: 10 },
} { key: "Device", header: "Device", minWidth: 16, flex: true },
{ key: "Role", header: "Role", minWidth: 8 },
{ key: "IP", header: "IP", minWidth: 12 },
{ key: "Age", header: "Age", minWidth: 8 },
{ key: "Flags", header: "Flags", minWidth: 8 },
],
rows: list.pending.map((req) => ({
Request: req.requestId,
Device: req.displayName || req.deviceId,
Role: req.role ?? "",
IP: req.remoteIp ?? "",
Age: typeof req.ts === "number" ? `${formatAge(Date.now() - req.ts)} ago` : "",
Flags: req.isRepair ? "repair" : "",
})),
}).trimEnd(),
);
} }
if (list.paired?.length) { if (list.paired?.length) {
defaultRuntime.log("Paired:"); const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
for (const device of list.paired) { defaultRuntime.log(
const name = device.displayName || device.deviceId; `${theme.heading("Paired")} ${theme.muted(`(${list.paired.length})`)}`,
const roles = device.roles?.length ? `roles: ${device.roles.join(", ")}` : "roles: -"; );
const scopes = device.scopes?.length defaultRuntime.log(
? `scopes: ${device.scopes.join(", ")}` renderTable({
: "scopes: -"; width: tableWidth,
const ip = device.remoteIp ? ` · ${device.remoteIp}` : ""; columns: [
const tokens = formatTokenSummary(device.tokens); { key: "Device", header: "Device", minWidth: 16, flex: true },
defaultRuntime.log(`- ${name} · ${roles} · ${scopes} · ${tokens}${ip}`); { key: "Roles", header: "Roles", minWidth: 12, flex: true },
} { key: "Scopes", header: "Scopes", minWidth: 12, flex: true },
{ key: "Tokens", header: "Tokens", minWidth: 12, flex: true },
{ key: "IP", header: "IP", minWidth: 12 },
],
rows: list.paired.map((device) => ({
Device: device.displayName || device.deviceId,
Roles: device.roles?.length ? device.roles.join(", ") : "",
Scopes: device.scopes?.length ? device.scopes.join(", ") : "",
Tokens: formatTokenSummary(device.tokens),
IP: device.remoteIp ?? "",
})),
}).trimEnd(),
);
} }
if (!list.pending?.length && !list.paired?.length) { if (!list.pending?.length && !list.paired?.length) {
defaultRuntime.log("No device pairing entries."); defaultRuntime.log(theme.muted("No device pairing entries."));
} }
}), }),
); );
@@ -160,7 +189,7 @@ export function registerDevicesCli(program: Command) {
return; return;
} }
const deviceId = (result as { device?: { deviceId?: string } })?.device?.deviceId; const deviceId = (result as { device?: { deviceId?: string } })?.device?.deviceId;
defaultRuntime.log(`device approved: ${deviceId ?? "ok"}`); defaultRuntime.log(`${theme.success("Approved")} ${theme.command(deviceId ?? "ok")}`);
}), }),
); );
@@ -176,7 +205,7 @@ export function registerDevicesCli(program: Command) {
return; return;
} }
const deviceId = (result as { deviceId?: string })?.deviceId; const deviceId = (result as { deviceId?: string })?.deviceId;
defaultRuntime.log(`device rejected: ${deviceId ?? "ok"}`); defaultRuntime.log(`${theme.warn("Rejected")} ${theme.command(deviceId ?? "ok")}`);
}), }),
); );
+49 -9
View File
@@ -8,6 +8,7 @@ import { resolveMessageChannelSelection } from "../infra/outbound/channel-select
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { renderTable } from "../terminal/table.js";
function parseLimit(value: unknown): number | null { function parseLimit(value: unknown): number | null {
if (typeof value === "number" && Number.isFinite(value)) { if (typeof value === "number" && Number.isFinite(value)) {
@@ -22,9 +23,11 @@ function parseLimit(value: unknown): number | null {
return parsed; return parsed;
} }
function formatEntry(entry: { kind: string; id: string; name?: string | undefined }): string { function buildRows(entries: Array<{ id: string; name?: string | undefined }>) {
const name = entry.name?.trim(); return entries.map((entry) => ({
return name ? `${entry.id}\t${name}` : entry.id; ID: entry.id,
Name: entry.name?.trim() ?? "",
}));
} }
export function registerDirectoryCli(program: Command) { export function registerDirectoryCli(program: Command) {
@@ -77,10 +80,21 @@ export function registerDirectoryCli(program: Command) {
return; return;
} }
if (!result) { if (!result) {
defaultRuntime.log("not available"); defaultRuntime.log(theme.muted("Not available."));
return; return;
} }
defaultRuntime.log(formatEntry(result)); const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
defaultRuntime.log(`${theme.heading("Self")}`);
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "ID", header: "ID", minWidth: 16, flex: true },
{ key: "Name", header: "Name", minWidth: 18, flex: true },
],
rows: buildRows([result]),
}).trimEnd(),
);
} catch (err) { } catch (err) {
defaultRuntime.error(danger(String(err))); defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1); defaultRuntime.exit(1);
@@ -111,9 +125,22 @@ export function registerDirectoryCli(program: Command) {
defaultRuntime.log(JSON.stringify(result, null, 2)); defaultRuntime.log(JSON.stringify(result, null, 2));
return; return;
} }
for (const entry of result) { if (result.length === 0) {
defaultRuntime.log(formatEntry(entry)); defaultRuntime.log(theme.muted("No peers found."));
return;
} }
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
defaultRuntime.log(`${theme.heading("Peers")} ${theme.muted(`(${result.length})`)}`);
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "ID", header: "ID", minWidth: 16, flex: true },
{ key: "Name", header: "Name", minWidth: 18, flex: true },
],
rows: buildRows(result),
}).trimEnd(),
);
} catch (err) { } catch (err) {
defaultRuntime.error(danger(String(err))); defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1); defaultRuntime.exit(1);
@@ -143,9 +170,22 @@ export function registerDirectoryCli(program: Command) {
defaultRuntime.log(JSON.stringify(result, null, 2)); defaultRuntime.log(JSON.stringify(result, null, 2));
return; return;
} }
for (const entry of result) { if (result.length === 0) {
defaultRuntime.log(formatEntry(entry)); defaultRuntime.log(theme.muted("No groups found."));
return;
} }
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
defaultRuntime.log(`${theme.heading("Groups")} ${theme.muted(`(${result.length})`)}`);
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "ID", header: "ID", minWidth: 16, flex: true },
{ key: "Name", header: "Name", minWidth: 18, flex: true },
],
rows: buildRows(result),
}).trimEnd(),
);
} catch (err) { } catch (err) {
defaultRuntime.error(danger(String(err))); defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1); defaultRuntime.exit(1);
+38 -18
View File
@@ -7,7 +7,9 @@ import type { Command } from "commander";
import { loadConfig } from "../config/config.js"; import { loadConfig } from "../config/config.js";
import { pickPrimaryTailnetIPv4, pickPrimaryTailnetIPv6 } from "../infra/tailnet.js"; import { pickPrimaryTailnetIPv4, pickPrimaryTailnetIPv6 } from "../infra/tailnet.js";
import { getWideAreaZonePath, WIDE_AREA_DISCOVERY_DOMAIN } from "../infra/widearea-dns.js"; import { getWideAreaZonePath, WIDE_AREA_DISCOVERY_DOMAIN } from "../infra/widearea-dns.js";
import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { renderTable } from "../terminal/table.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
type RunOpts = { allowFailure?: boolean; inherit?: boolean }; type RunOpts = { allowFailure?: boolean; inherit?: boolean };
@@ -112,14 +114,28 @@ export function registerDnsCli(program: Command) {
const tailnetIPv6 = pickPrimaryTailnetIPv6(); const tailnetIPv6 = pickPrimaryTailnetIPv6();
const zonePath = getWideAreaZonePath(); const zonePath = getWideAreaZonePath();
console.log(`Domain: ${WIDE_AREA_DISCOVERY_DOMAIN}`); const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
console.log(`Zone file (gateway-owned): ${zonePath}`); defaultRuntime.log(theme.heading("DNS setup"));
console.log( defaultRuntime.log(
`Detected tailnet IP: ${tailnetIPv4 ?? "—"}${tailnetIPv6 ? ` (v6 ${tailnetIPv6})` : ""}`, renderTable({
width: tableWidth,
columns: [
{ key: "Key", header: "Key", minWidth: 18 },
{ key: "Value", header: "Value", minWidth: 24, flex: true },
],
rows: [
{ Key: "Domain", Value: WIDE_AREA_DISCOVERY_DOMAIN },
{ Key: "Zone file", Value: zonePath },
{
Key: "Tailnet IP",
Value: `${tailnetIPv4 ?? "—"}${tailnetIPv6 ? ` (v6 ${tailnetIPv6})` : ""}`,
},
],
}).trimEnd(),
); );
console.log(""); defaultRuntime.log("");
console.log("Recommended ~/.clawdbot/clawdbot.json:"); defaultRuntime.log(theme.heading("Recommended ~/.clawdbot/clawdbot.json:"));
console.log( defaultRuntime.log(
JSON.stringify( JSON.stringify(
{ {
gateway: { bind: "auto" }, gateway: { bind: "auto" },
@@ -129,14 +145,16 @@ export function registerDnsCli(program: Command) {
2, 2,
), ),
); );
console.log(""); defaultRuntime.log("");
console.log("Tailscale admin (DNS → Nameservers):"); defaultRuntime.log(theme.heading("Tailscale admin (DNS → Nameservers):"));
console.log(`- Add nameserver: ${tailnetIPv4 ?? "<this machine's tailnet IPv4>"}`); defaultRuntime.log(
console.log(`- Restrict to domain (Split DNS): clawdbot.internal`); theme.muted(`- Add nameserver: ${tailnetIPv4 ?? "<this machine's tailnet IPv4>"}`),
);
defaultRuntime.log(theme.muted("- Restrict to domain (Split DNS): clawdbot.internal"));
if (!opts.apply) { if (!opts.apply) {
console.log(""); defaultRuntime.log("");
console.log("Run with --apply to install CoreDNS and configure it."); defaultRuntime.log(theme.muted("Run with --apply to install CoreDNS and configure it."));
return; return;
} }
@@ -205,16 +223,18 @@ export function registerDnsCli(program: Command) {
fs.writeFileSync(zonePath, zoneLines.join("\n"), "utf-8"); fs.writeFileSync(zonePath, zoneLines.join("\n"), "utf-8");
} }
console.log(""); defaultRuntime.log("");
console.log("Starting CoreDNS (sudo)…"); defaultRuntime.log(theme.heading("Starting CoreDNS (sudo)…"));
run("sudo", ["brew", "services", "restart", "coredns"], { run("sudo", ["brew", "services", "restart", "coredns"], {
inherit: true, inherit: true,
}); });
if (cfg.discovery?.wideArea?.enabled !== true) { if (cfg.discovery?.wideArea?.enabled !== true) {
console.log(""); defaultRuntime.log("");
console.log( defaultRuntime.log(
"Note: enable discovery.wideArea.enabled in ~/.clawdbot/clawdbot.json on the gateway and restart the gateway so it writes the DNS-SD zone.", theme.muted(
"Note: enable discovery.wideArea.enabled in ~/.clawdbot/clawdbot.json on the gateway and restart the gateway so it writes the DNS-SD zone.",
),
); );
} }
}); });
+114 -4
View File
@@ -5,7 +5,8 @@ import type { Command } from "commander";
import type { ExecApprovalsAgent, ExecApprovalsFile } from "../infra/exec-approvals.js"; import type { ExecApprovalsAgent, ExecApprovalsFile } from "../infra/exec-approvals.js";
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.js"; import { isRich, theme } from "../terminal/theme.js";
import { renderTable } from "../terminal/table.js";
import { callGatewayFromCli } from "./gateway-rpc.js"; import { callGatewayFromCli } from "./gateway-rpc.js";
import { nodesCallOpts, resolveNodeId } from "./nodes-cli/rpc.js"; import { nodesCallOpts, resolveNodeId } from "./nodes-cli/rpc.js";
import type { NodesRpcOpts } from "./nodes-cli/types.js"; import type { NodesRpcOpts } from "./nodes-cli/types.js";
@@ -24,6 +25,17 @@ type ExecApprovalsCliOpts = NodesRpcOpts & {
agent?: string; agent?: string;
}; };
function formatAge(msAgo: number) {
const s = Math.max(0, Math.floor(msAgo / 1000));
if (s < 60) return `${s}s`;
const m = Math.floor(s / 60);
if (m < 60) return `${m}m`;
const h = Math.floor(m / 60);
if (h < 24) return `${h}h`;
const d = Math.floor(h / 24);
return `${d}d`;
}
async function readStdin(): Promise<string> { async function readStdin(): Promise<string> {
const chunks: Buffer[] = []; const chunks: Buffer[] = [];
for await (const chunk of process.stdin) { for await (const chunk of process.stdin) {
@@ -89,6 +101,9 @@ function isEmptyAgent(agent: ExecApprovalsAgent): boolean {
} }
export function registerExecApprovalsCli(program: Command) { export function registerExecApprovalsCli(program: Command) {
const formatExample = (cmd: string, desc: string) =>
` ${theme.command(cmd)}\n ${theme.muted(desc)}`;
const approvals = program const approvals = program
.command("approvals") .command("approvals")
.alias("exec-approvals") .alias("exec-approvals")
@@ -106,8 +121,87 @@ export function registerExecApprovalsCli(program: Command) {
.action(async (opts: ExecApprovalsCliOpts) => { .action(async (opts: ExecApprovalsCliOpts) => {
const nodeId = await resolveTargetNodeId(opts); const nodeId = await resolveTargetNodeId(opts);
const snapshot = await loadSnapshot(opts, nodeId); const snapshot = await loadSnapshot(opts, nodeId);
const payload = opts.json ? JSON.stringify(snapshot) : JSON.stringify(snapshot, null, 2); if (opts.json) {
defaultRuntime.log(payload); defaultRuntime.log(JSON.stringify(snapshot));
return;
}
const rich = isRich();
const heading = (text: string) => (rich ? theme.heading(text) : text);
const muted = (text: string) => (rich ? theme.muted(text) : text);
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const file = snapshot.file ?? { version: 1 };
const defaults = file.defaults ?? {};
const defaultsParts = [
defaults.security ? `security=${defaults.security}` : null,
defaults.ask ? `ask=${defaults.ask}` : null,
defaults.askFallback ? `askFallback=${defaults.askFallback}` : null,
typeof defaults.autoAllowSkills === "boolean"
? `autoAllowSkills=${defaults.autoAllowSkills ? "on" : "off"}`
: null,
].filter(Boolean) as string[];
const agents = file.agents ?? {};
const allowlistRows: Array<{ Agent: string; Pattern: string; LastUsed: string }> = [];
const now = Date.now();
for (const [agentId, agent] of Object.entries(agents)) {
const allowlist = Array.isArray(agent.allowlist) ? agent.allowlist : [];
for (const entry of allowlist) {
const pattern = entry?.pattern?.trim() ?? "";
if (!pattern) continue;
const lastUsedAt = typeof entry.lastUsedAt === "number" ? entry.lastUsedAt : null;
allowlistRows.push({
Agent: agentId,
Pattern: pattern,
LastUsed: lastUsedAt
? `${formatAge(Math.max(0, now - lastUsedAt))} ago`
: muted("unknown"),
});
}
}
const summaryRows = [
{ Field: "Path", Value: snapshot.path },
{ Field: "Exists", Value: snapshot.exists ? "yes" : "no" },
{ Field: "Hash", Value: snapshot.hash },
{ Field: "Version", Value: String(file.version ?? 1) },
{ Field: "Socket", Value: file.socket?.path ?? "default" },
{ Field: "Defaults", Value: defaultsParts.length > 0 ? defaultsParts.join(", ") : "none" },
{ Field: "Agents", Value: String(Object.keys(agents).length) },
{ Field: "Allowlist", Value: String(allowlistRows.length) },
];
defaultRuntime.log(heading("Approvals"));
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "Field", header: "Field", minWidth: 8 },
{ key: "Value", header: "Value", minWidth: 24, flex: true },
],
rows: summaryRows,
}).trimEnd(),
);
if (allowlistRows.length === 0) {
defaultRuntime.log("");
defaultRuntime.log(muted("No allowlist entries."));
return;
}
defaultRuntime.log("");
defaultRuntime.log(heading("Allowlist"));
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "Agent", header: "Agent", minWidth: 8 },
{ key: "Pattern", header: "Pattern", minWidth: 20, flex: true },
{ key: "LastUsed", header: "Last Used", minWidth: 10 },
],
rows: allowlistRows,
}).trimEnd(),
);
}); });
nodesCallOpts(getCmd); nodesCallOpts(getCmd);
@@ -151,7 +245,23 @@ export function registerExecApprovalsCli(program: Command) {
}); });
nodesCallOpts(setCmd); nodesCallOpts(setCmd);
const allowlist = approvals.command("allowlist").description("Edit the per-agent allowlist"); const allowlist = approvals
.command("allowlist")
.description("Edit the per-agent allowlist")
.addHelpText(
"after",
() =>
`\n${theme.heading("Examples:")}\n${formatExample(
'clawdbot approvals allowlist add "~/Projects/**/bin/rg"',
"Allowlist a local binary pattern for the default agent.",
)}\n${formatExample(
'clawdbot approvals allowlist add --agent main --node <id|name|ip> "/usr/bin/uptime"',
"Allowlist on a specific node/agent.",
)}\n${formatExample(
'clawdbot approvals allowlist remove "~/Projects/**/bin/rg"',
"Remove an allowlist pattern.",
)}\n\n${theme.muted("Docs:")} ${formatDocsLink("/cli/approvals", "docs.clawd.bot/cli/approvals")}\n`,
);
const allowlistAdd = allowlist const allowlistAdd = allowlist
.command("add <pattern>") .command("add <pattern>")
+21
View File
@@ -0,0 +1,21 @@
import { theme } from "../terminal/theme.js";
export type HelpExample = [command: string, description: string];
export function formatHelpExample(command: string, description: string): string {
return ` ${theme.command(command)}\n ${theme.muted(description)}`;
}
export function formatHelpExampleLine(command: string, description: string): string {
if (!description) return ` ${theme.command(command)}`;
return ` ${theme.command(command)} ${theme.muted(`# ${description}`)}`;
}
export function formatHelpExamples(examples: HelpExample[], inline = false): string {
const formatter = inline ? formatHelpExampleLine : formatHelpExample;
return examples.map(([command, description]) => formatter(command, description)).join("\n");
}
export function formatHelpExampleGroup(label: string, examples: HelpExample[], inline = false) {
return `${theme.muted(label)}\n${formatHelpExamples(examples, inline)}`;
}
+130 -112
View File
@@ -1,7 +1,6 @@
import fs from "node:fs"; import fs from "node:fs";
import fsp from "node:fs/promises"; import fsp from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import chalk from "chalk";
import type { Command } from "commander"; import type { Command } from "commander";
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../agents/agent-scope.js"; import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../agents/agent-scope.js";
import type { ClawdbotConfig } from "../config/config.js"; import type { ClawdbotConfig } from "../config/config.js";
@@ -23,6 +22,7 @@ import { recordHookInstall } from "../hooks/installs.js";
import { buildPluginStatusReport } from "../plugins/status.js"; import { buildPluginStatusReport } from "../plugins/status.js";
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { renderTable } from "../terminal/table.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { formatCliCommand } from "./command-format.js"; import { formatCliCommand } from "./command-format.js";
import { resolveUserPath } from "../utils.js"; import { resolveUserPath } from "../utils.js";
@@ -66,50 +66,40 @@ function buildHooksReport(config: ClawdbotConfig): HookStatusReport {
return buildWorkspaceHookStatus(workspaceDir, { config, entries }); return buildWorkspaceHookStatus(workspaceDir, { config, entries });
} }
/** function formatHookStatus(hook: HookStatusEntry): string {
* Format a single hook for display in the list if (hook.eligible) return theme.success("✓ ready");
*/ if (hook.disabled) return theme.warn("⏸ disabled");
function formatHookLine(hook: HookStatusEntry, verbose = false): string { return theme.error("✗ missing");
}
function formatHookName(hook: HookStatusEntry): string {
const emoji = hook.emoji ?? "🔗"; const emoji = hook.emoji ?? "🔗";
const status = hook.eligible return `${emoji} ${theme.command(hook.name)}`;
? chalk.green("✓") }
: hook.disabled
? chalk.yellow("disabled")
: chalk.red("missing reqs");
const name = hook.eligible ? chalk.white(hook.name) : chalk.gray(hook.name); function formatHookSource(hook: HookStatusEntry): string {
if (!hook.managedByPlugin) return hook.source;
return `plugin:${hook.pluginId ?? "unknown"}`;
}
const desc = chalk.gray( function formatHookMissingSummary(hook: HookStatusEntry): string {
hook.description.length > 50 ? `${hook.description.slice(0, 47)}...` : hook.description, const missing: string[] = [];
); if (hook.missing.bins.length > 0) {
const sourceLabel = hook.managedByPlugin missing.push(`bins: ${hook.missing.bins.join(", ")}`);
? chalk.magenta(`plugin:${hook.pluginId ?? "unknown"}`)
: "";
if (verbose) {
const missing: string[] = [];
if (hook.missing.bins.length > 0) {
missing.push(`bins: ${hook.missing.bins.join(", ")}`);
}
if (hook.missing.anyBins.length > 0) {
missing.push(`anyBins: ${hook.missing.anyBins.join(", ")}`);
}
if (hook.missing.env.length > 0) {
missing.push(`env: ${hook.missing.env.join(", ")}`);
}
if (hook.missing.config.length > 0) {
missing.push(`config: ${hook.missing.config.join(", ")}`);
}
if (hook.missing.os.length > 0) {
missing.push(`os: ${hook.missing.os.join(", ")}`);
}
const missingStr = missing.length > 0 ? chalk.red(` [${missing.join("; ")}]`) : "";
const sourceSuffix = sourceLabel ? ` ${sourceLabel}` : "";
return `${emoji} ${name} ${status}${missingStr}\n ${desc}${sourceSuffix}`;
} }
if (hook.missing.anyBins.length > 0) {
const sourceSuffix = sourceLabel ? ` ${sourceLabel}` : ""; missing.push(`anyBins: ${hook.missing.anyBins.join(", ")}`);
return `${emoji} ${name} ${status} - ${desc}${sourceSuffix}`; }
if (hook.missing.env.length > 0) {
missing.push(`env: ${hook.missing.env.join(", ")}`);
}
if (hook.missing.config.length > 0) {
missing.push(`config: ${hook.missing.config.join(", ")}`);
}
if (hook.missing.os.length > 0) {
missing.push(`os: ${hook.missing.os.join(", ")}`);
}
return missing.join("; ");
} }
async function readInstalledPackageVersion(dir: string): Promise<string | undefined> { async function readInstalledPackageVersion(dir: string): Promise<string | undefined> {
@@ -157,27 +147,39 @@ export function formatHooksList(report: HookStatusReport, opts: HooksListOptions
} }
const eligible = hooks.filter((h) => h.eligible); const eligible = hooks.filter((h) => h.eligible);
const notEligible = hooks.filter((h) => !h.eligible); const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const rows = hooks.map((hook) => {
const missing = formatHookMissingSummary(hook);
return {
Status: formatHookStatus(hook),
Hook: formatHookName(hook),
Description: theme.muted(hook.description),
Source: formatHookSource(hook),
Missing: missing ? theme.warn(missing) : "",
};
});
const columns = [
{ key: "Status", header: "Status", minWidth: 10 },
{ key: "Hook", header: "Hook", minWidth: 18, flex: true },
{ key: "Description", header: "Description", minWidth: 24, flex: true },
{ key: "Source", header: "Source", minWidth: 12, flex: true },
];
if (opts.verbose) {
columns.push({ key: "Missing", header: "Missing", minWidth: 18, flex: true });
}
const lines: string[] = []; const lines: string[] = [];
lines.push(chalk.bold.cyan("Hooks") + chalk.gray(` (${eligible.length}/${hooks.length} ready)`)); lines.push(
lines.push(""); `${theme.heading("Hooks")} ${theme.muted(`(${eligible.length}/${hooks.length} ready)`)}`,
);
if (eligible.length > 0) { lines.push(
lines.push(chalk.bold.green("Ready:")); renderTable({
for (const hook of eligible) { width: tableWidth,
lines.push(` ${formatHookLine(hook, opts.verbose)}`); columns,
} rows,
} }).trimEnd(),
);
if (notEligible.length > 0 && !opts.eligible) {
if (eligible.length > 0) lines.push("");
lines.push(chalk.bold.yellow("Not ready:"));
for (const hook of notEligible) {
lines.push(` ${formatHookLine(hook, opts.verbose)}`);
}
}
return lines.join("\n"); return lines.join("\n");
} }
@@ -205,33 +207,33 @@ export function formatHookInfo(
const lines: string[] = []; const lines: string[] = [];
const emoji = hook.emoji ?? "🔗"; const emoji = hook.emoji ?? "🔗";
const status = hook.eligible const status = hook.eligible
? chalk.green("✓ Ready") ? theme.success("✓ Ready")
: hook.disabled : hook.disabled
? chalk.yellow("⏸ Disabled") ? theme.warn("⏸ Disabled")
: chalk.red("✗ Missing requirements"); : theme.error("✗ Missing requirements");
lines.push(`${emoji} ${chalk.bold.cyan(hook.name)} ${status}`); lines.push(`${emoji} ${theme.heading(hook.name)} ${status}`);
lines.push(""); lines.push("");
lines.push(chalk.white(hook.description)); lines.push(hook.description);
lines.push(""); lines.push("");
// Details // Details
lines.push(chalk.bold("Details:")); lines.push(theme.heading("Details:"));
if (hook.managedByPlugin) { if (hook.managedByPlugin) {
lines.push(` Source: ${hook.source} (${hook.pluginId ?? "unknown"})`); lines.push(`${theme.muted(" Source:")} ${hook.source} (${hook.pluginId ?? "unknown"})`);
} else { } else {
lines.push(` Source: ${hook.source}`); lines.push(`${theme.muted(" Source:")} ${hook.source}`);
} }
lines.push(` Path: ${chalk.gray(hook.filePath)}`); lines.push(`${theme.muted(" Path:")} ${hook.filePath}`);
lines.push(` Handler: ${chalk.gray(hook.handlerPath)}`); lines.push(`${theme.muted(" Handler:")} ${hook.handlerPath}`);
if (hook.homepage) { if (hook.homepage) {
lines.push(` Homepage: ${chalk.blue(hook.homepage)}`); lines.push(`${theme.muted(" Homepage:")} ${hook.homepage}`);
} }
if (hook.events.length > 0) { if (hook.events.length > 0) {
lines.push(` Events: ${hook.events.join(", ")}`); lines.push(`${theme.muted(" Events:")} ${hook.events.join(", ")}`);
} }
if (hook.managedByPlugin) { if (hook.managedByPlugin) {
lines.push(` Managed by plugin; enable/disable via hooks CLI not available.`); lines.push(theme.muted(" Managed by plugin; enable/disable via hooks CLI not available."));
} }
// Requirements // Requirements
@@ -244,40 +246,40 @@ export function formatHookInfo(
if (hasRequirements) { if (hasRequirements) {
lines.push(""); lines.push("");
lines.push(chalk.bold("Requirements:")); lines.push(theme.heading("Requirements:"));
if (hook.requirements.bins.length > 0) { if (hook.requirements.bins.length > 0) {
const binsStatus = hook.requirements.bins.map((bin) => { const binsStatus = hook.requirements.bins.map((bin) => {
const missing = hook.missing.bins.includes(bin); const missing = hook.missing.bins.includes(bin);
return missing ? chalk.red(`✗ ${bin}`) : chalk.green(`✓ ${bin}`); return missing ? theme.error(`✗ ${bin}`) : theme.success(`✓ ${bin}`);
}); });
lines.push(` Binaries: ${binsStatus.join(", ")}`); lines.push(`${theme.muted(" Binaries:")} ${binsStatus.join(", ")}`);
} }
if (hook.requirements.anyBins.length > 0) { if (hook.requirements.anyBins.length > 0) {
const anyBinsStatus = const anyBinsStatus =
hook.missing.anyBins.length > 0 hook.missing.anyBins.length > 0
? chalk.red(`✗ (any of: ${hook.requirements.anyBins.join(", ")})`) ? theme.error(`✗ (any of: ${hook.requirements.anyBins.join(", ")})`)
: chalk.green(`✓ (any of: ${hook.requirements.anyBins.join(", ")})`); : theme.success(`✓ (any of: ${hook.requirements.anyBins.join(", ")})`);
lines.push(` Any binary: ${anyBinsStatus}`); lines.push(`${theme.muted(" Any binary:")} ${anyBinsStatus}`);
} }
if (hook.requirements.env.length > 0) { if (hook.requirements.env.length > 0) {
const envStatus = hook.requirements.env.map((env) => { const envStatus = hook.requirements.env.map((env) => {
const missing = hook.missing.env.includes(env); const missing = hook.missing.env.includes(env);
return missing ? chalk.red(`✗ ${env}`) : chalk.green(`✓ ${env}`); return missing ? theme.error(`✗ ${env}`) : theme.success(`✓ ${env}`);
}); });
lines.push(` Environment: ${envStatus.join(", ")}`); lines.push(`${theme.muted(" Environment:")} ${envStatus.join(", ")}`);
} }
if (hook.requirements.config.length > 0) { if (hook.requirements.config.length > 0) {
const configStatus = hook.configChecks.map((check) => { const configStatus = hook.configChecks.map((check) => {
return check.satisfied ? chalk.green(`✓ ${check.path}`) : chalk.red(`✗ ${check.path}`); return check.satisfied ? theme.success(`✓ ${check.path}`) : theme.error(`✗ ${check.path}`);
}); });
lines.push(` Config: ${configStatus.join(", ")}`); lines.push(`${theme.muted(" Config:")} ${configStatus.join(", ")}`);
} }
if (hook.requirements.os.length > 0) { if (hook.requirements.os.length > 0) {
const osStatus = const osStatus =
hook.missing.os.length > 0 hook.missing.os.length > 0
? chalk.red(`✗ (${hook.requirements.os.join(", ")})`) ? theme.error(`✗ (${hook.requirements.os.join(", ")})`)
: chalk.green(`✓ (${hook.requirements.os.join(", ")})`); : theme.success(`✓ (${hook.requirements.os.join(", ")})`);
lines.push(` OS: ${osStatus}`); lines.push(`${theme.muted(" OS:")} ${osStatus}`);
} }
} }
@@ -313,15 +315,15 @@ export function formatHooksCheck(report: HookStatusReport, opts: HooksCheckOptio
const notEligible = report.hooks.filter((h) => !h.eligible); const notEligible = report.hooks.filter((h) => !h.eligible);
const lines: string[] = []; const lines: string[] = [];
lines.push(chalk.bold.cyan("Hooks Status")); lines.push(theme.heading("Hooks Status"));
lines.push(""); lines.push("");
lines.push(`Total hooks: ${report.hooks.length}`); lines.push(`${theme.muted("Total hooks:")} ${report.hooks.length}`);
lines.push(chalk.green(`Ready: ${eligible.length}`)); lines.push(`${theme.success("Ready:")} ${eligible.length}`);
lines.push(chalk.yellow(`Not ready: ${notEligible.length}`)); lines.push(`${theme.warn("Not ready:")} ${notEligible.length}`);
if (notEligible.length > 0) { if (notEligible.length > 0) {
lines.push(""); lines.push("");
lines.push(chalk.bold.yellow("Hooks not ready:")); lines.push(theme.heading("Hooks not ready:"));
for (const hook of notEligible) { for (const hook of notEligible) {
const reasons = []; const reasons = [];
if (hook.disabled) reasons.push("disabled"); if (hook.disabled) reasons.push("disabled");
@@ -374,7 +376,9 @@ export async function enableHook(hookName: string): Promise<void> {
}; };
await writeConfigFile(nextConfig); await writeConfigFile(nextConfig);
console.log(`${chalk.green("✓")} Enabled hook: ${hook.emoji ?? "🔗"} ${hookName}`); defaultRuntime.log(
`${theme.success("✓")} Enabled hook: ${hook.emoji ?? "🔗"} ${theme.command(hookName)}`,
);
} }
export async function disableHook(hookName: string): Promise<void> { export async function disableHook(hookName: string): Promise<void> {
@@ -408,7 +412,9 @@ export async function disableHook(hookName: string): Promise<void> {
}; };
await writeConfigFile(nextConfig); await writeConfigFile(nextConfig);
console.log(`${chalk.yellow("⏸")} Disabled hook: ${hook.emoji ?? "🔗"} ${hookName}`); defaultRuntime.log(
`${theme.warn("⏸")} Disabled hook: ${hook.emoji ?? "🔗"} ${theme.command(hookName)}`,
);
} }
export function registerHooksCli(program: Command): void { export function registerHooksCli(program: Command): void {
@@ -431,9 +437,11 @@ export function registerHooksCli(program: Command): void {
try { try {
const config = loadConfig(); const config = loadConfig();
const report = buildHooksReport(config); const report = buildHooksReport(config);
console.log(formatHooksList(report, opts)); defaultRuntime.log(formatHooksList(report, opts));
} catch (err) { } catch (err) {
console.error(chalk.red("Error:"), err instanceof Error ? err.message : String(err)); defaultRuntime.error(
`${theme.error("Error:")} ${err instanceof Error ? err.message : String(err)}`,
);
process.exit(1); process.exit(1);
} }
}); });
@@ -446,9 +454,11 @@ export function registerHooksCli(program: Command): void {
try { try {
const config = loadConfig(); const config = loadConfig();
const report = buildHooksReport(config); const report = buildHooksReport(config);
console.log(formatHookInfo(report, name, opts)); defaultRuntime.log(formatHookInfo(report, name, opts));
} catch (err) { } catch (err) {
console.error(chalk.red("Error:"), err instanceof Error ? err.message : String(err)); defaultRuntime.error(
`${theme.error("Error:")} ${err instanceof Error ? err.message : String(err)}`,
);
process.exit(1); process.exit(1);
} }
}); });
@@ -461,9 +471,11 @@ export function registerHooksCli(program: Command): void {
try { try {
const config = loadConfig(); const config = loadConfig();
const report = buildHooksReport(config); const report = buildHooksReport(config);
console.log(formatHooksCheck(report, opts)); defaultRuntime.log(formatHooksCheck(report, opts));
} catch (err) { } catch (err) {
console.error(chalk.red("Error:"), err instanceof Error ? err.message : String(err)); defaultRuntime.error(
`${theme.error("Error:")} ${err instanceof Error ? err.message : String(err)}`,
);
process.exit(1); process.exit(1);
} }
}); });
@@ -475,7 +487,9 @@ export function registerHooksCli(program: Command): void {
try { try {
await enableHook(name); await enableHook(name);
} catch (err) { } catch (err) {
console.error(chalk.red("Error:"), err instanceof Error ? err.message : String(err)); defaultRuntime.error(
`${theme.error("Error:")} ${err instanceof Error ? err.message : String(err)}`,
);
process.exit(1); process.exit(1);
} }
}); });
@@ -487,7 +501,9 @@ export function registerHooksCli(program: Command): void {
try { try {
await disableHook(name); await disableHook(name);
} catch (err) { } catch (err) {
console.error(chalk.red("Error:"), err instanceof Error ? err.message : String(err)); defaultRuntime.error(
`${theme.error("Error:")} ${err instanceof Error ? err.message : String(err)}`,
);
process.exit(1); process.exit(1);
} }
}); });
@@ -570,7 +586,7 @@ export function registerHooksCli(program: Command): void {
path: resolved, path: resolved,
logger: { logger: {
info: (msg) => defaultRuntime.log(msg), info: (msg) => defaultRuntime.log(msg),
warn: (msg) => defaultRuntime.log(chalk.yellow(msg)), warn: (msg) => defaultRuntime.log(theme.warn(msg)),
}, },
}); });
if (!result.ok) { if (!result.ok) {
@@ -650,7 +666,7 @@ export function registerHooksCli(program: Command): void {
spec: raw, spec: raw,
logger: { logger: {
info: (msg) => defaultRuntime.log(msg), info: (msg) => defaultRuntime.log(msg),
warn: (msg) => defaultRuntime.log(chalk.yellow(msg)), warn: (msg) => defaultRuntime.log(theme.warn(msg)),
}, },
}); });
if (!result.ok) { if (!result.ok) {
@@ -726,15 +742,15 @@ export function registerHooksCli(program: Command): void {
for (const hookId of targets) { for (const hookId of targets) {
const record = installs[hookId]; const record = installs[hookId];
if (!record) { if (!record) {
defaultRuntime.log(chalk.yellow(`No install record for "${hookId}".`)); defaultRuntime.log(theme.warn(`No install record for "${hookId}".`));
continue; continue;
} }
if (record.source !== "npm") { if (record.source !== "npm") {
defaultRuntime.log(chalk.yellow(`Skipping "${hookId}" (source: ${record.source}).`)); defaultRuntime.log(theme.warn(`Skipping "${hookId}" (source: ${record.source}).`));
continue; continue;
} }
if (!record.spec) { if (!record.spec) {
defaultRuntime.log(chalk.yellow(`Skipping "${hookId}" (missing npm spec).`)); defaultRuntime.log(theme.warn(`Skipping "${hookId}" (missing npm spec).`));
continue; continue;
} }
@@ -749,11 +765,11 @@ export function registerHooksCli(program: Command): void {
expectedHookPackId: hookId, expectedHookPackId: hookId,
logger: { logger: {
info: (msg) => defaultRuntime.log(msg), info: (msg) => defaultRuntime.log(msg),
warn: (msg) => defaultRuntime.log(chalk.yellow(msg)), warn: (msg) => defaultRuntime.log(theme.warn(msg)),
}, },
}); });
if (!probe.ok) { if (!probe.ok) {
defaultRuntime.log(chalk.red(`Failed to check ${hookId}: ${probe.error}`)); defaultRuntime.log(theme.error(`Failed to check ${hookId}: ${probe.error}`));
continue; continue;
} }
@@ -773,11 +789,11 @@ export function registerHooksCli(program: Command): void {
expectedHookPackId: hookId, expectedHookPackId: hookId,
logger: { logger: {
info: (msg) => defaultRuntime.log(msg), info: (msg) => defaultRuntime.log(msg),
warn: (msg) => defaultRuntime.log(chalk.yellow(msg)), warn: (msg) => defaultRuntime.log(theme.warn(msg)),
}, },
}); });
if (!result.ok) { if (!result.ok) {
defaultRuntime.log(chalk.red(`Failed to update ${hookId}: ${result.error}`)); defaultRuntime.log(theme.error(`Failed to update ${hookId}: ${result.error}`));
continue; continue;
} }
@@ -811,9 +827,11 @@ export function registerHooksCli(program: Command): void {
try { try {
const config = loadConfig(); const config = loadConfig();
const report = buildHooksReport(config); const report = buildHooksReport(config);
console.log(formatHooksList(report, {})); defaultRuntime.log(formatHooksList(report, {}));
} catch (err) { } catch (err) {
console.error(chalk.red("Error:"), err instanceof Error ? err.message : String(err)); defaultRuntime.error(
`${theme.error("Error:")} ${err instanceof Error ? err.message : String(err)}`,
);
process.exit(1); process.exit(1);
} }
}); });
+17 -2
View File
@@ -1,13 +1,28 @@
import { defaultRuntime } from "../../runtime.js"; import { defaultRuntime } from "../../runtime.js";
import { isRich, theme } from "../../terminal/theme.js";
import { runCommandWithRuntime } from "../cli-utils.js"; import { runCommandWithRuntime } from "../cli-utils.js";
import { unauthorizedHintForMessage } from "./rpc.js"; import { unauthorizedHintForMessage } from "./rpc.js";
export function getNodesTheme() {
const rich = isRich();
const color = (fn: (value: string) => string) => (value: string) => (rich ? fn(value) : value);
return {
rich,
heading: color(theme.heading),
ok: color(theme.success),
warn: color(theme.warn),
muted: color(theme.muted),
error: color(theme.error),
};
}
export function runNodesCommand(label: string, action: () => Promise<void>) { export function runNodesCommand(label: string, action: () => Promise<void>) {
return runCommandWithRuntime(defaultRuntime, action, (err) => { return runCommandWithRuntime(defaultRuntime, action, (err) => {
const message = String(err); const message = String(err);
defaultRuntime.error(`nodes ${label} failed: ${message}`); const { error, warn } = getNodesTheme();
defaultRuntime.error(error(`nodes ${label} failed: ${message}`));
const hint = unauthorizedHintForMessage(message); const hint = unauthorizedHintForMessage(message);
if (hint) defaultRuntime.error(hint); if (hint) defaultRuntime.error(warn(hint));
defaultRuntime.exit(1); defaultRuntime.exit(1);
}); });
} }
+23 -8
View File
@@ -9,9 +9,10 @@ import {
writeBase64ToFile, writeBase64ToFile,
} from "../nodes-camera.js"; } from "../nodes-camera.js";
import { parseDurationMs } from "../parse-duration.js"; import { parseDurationMs } from "../parse-duration.js";
import { runNodesCommand } from "./cli-utils.js"; import { getNodesTheme, runNodesCommand } from "./cli-utils.js";
import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js"; import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js";
import type { NodesRpcOpts } from "./types.js"; import type { NodesRpcOpts } from "./types.js";
import { renderTable } from "../../terminal/table.js";
const parseFacing = (value: string): CameraFacing => { const parseFacing = (value: string): CameraFacing => {
const v = String(value ?? "") const v = String(value ?? "")
@@ -52,16 +53,30 @@ export function registerNodesCameraCommands(nodes: Command) {
} }
if (devices.length === 0) { if (devices.length === 0) {
defaultRuntime.log("No cameras reported."); const { muted } = getNodesTheme();
defaultRuntime.log(muted("No cameras reported."));
return; return;
} }
for (const device of devices) { const { heading, muted } = getNodesTheme();
const id = typeof device.id === "string" ? device.id : ""; const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const name = typeof device.name === "string" ? device.name : "Unknown Camera"; const rows = devices.map((device) => ({
const position = typeof device.position === "string" ? device.position : "unspecified"; Name: typeof device.name === "string" ? device.name : "Unknown Camera",
defaultRuntime.log(`${name} (${position})${id ? ` — ${id}` : ""}`); Position: typeof device.position === "string" ? device.position : muted("unspecified"),
} ID: typeof device.id === "string" ? device.id : "",
}));
defaultRuntime.log(heading("Cameras"));
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "Name", header: "Name", minWidth: 14, flex: true },
{ key: "Position", header: "Position", minWidth: 10 },
{ key: "ID", header: "ID", minWidth: 10, flex: true },
],
rows,
}).trimEnd(),
);
}); });
}), }),
{ timeoutMs: 60_000 }, { timeoutMs: 60_000 },
+25 -7
View File
@@ -6,7 +6,7 @@ import { writeBase64ToFile } from "../nodes-camera.js";
import { canvasSnapshotTempPath, parseCanvasSnapshotPayload } from "../nodes-canvas.js"; import { canvasSnapshotTempPath, parseCanvasSnapshotPayload } from "../nodes-canvas.js";
import { parseTimeoutMs } from "../nodes-run.js"; import { parseTimeoutMs } from "../nodes-run.js";
import { buildA2UITextJsonl, validateA2UIJsonl } from "./a2ui-jsonl.js"; import { buildA2UITextJsonl, validateA2UIJsonl } from "./a2ui-jsonl.js";
import { runNodesCommand } from "./cli-utils.js"; import { getNodesTheme, runNodesCommand } from "./cli-utils.js";
import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js"; import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js";
import type { NodesRpcOpts } from "./types.js"; import type { NodesRpcOpts } from "./types.js";
@@ -121,7 +121,10 @@ export function registerNodesCanvasCommands(nodes: Command) {
params.placement = placement; params.placement = placement;
} }
await invokeCanvas(opts, "canvas.present", params); await invokeCanvas(opts, "canvas.present", params);
if (!opts.json) defaultRuntime.log("canvas present ok"); if (!opts.json) {
const { ok } = getNodesTheme();
defaultRuntime.log(ok("canvas present ok"));
}
}); });
}), }),
); );
@@ -135,7 +138,10 @@ export function registerNodesCanvasCommands(nodes: Command) {
.action(async (opts: NodesRpcOpts) => { .action(async (opts: NodesRpcOpts) => {
await runNodesCommand("canvas hide", async () => { await runNodesCommand("canvas hide", async () => {
await invokeCanvas(opts, "canvas.hide", undefined); await invokeCanvas(opts, "canvas.hide", undefined);
if (!opts.json) defaultRuntime.log("canvas hide ok"); if (!opts.json) {
const { ok } = getNodesTheme();
defaultRuntime.log(ok("canvas hide ok"));
}
}); });
}), }),
); );
@@ -150,7 +156,10 @@ export function registerNodesCanvasCommands(nodes: Command) {
.action(async (url: string, opts: NodesRpcOpts) => { .action(async (url: string, opts: NodesRpcOpts) => {
await runNodesCommand("canvas navigate", async () => { await runNodesCommand("canvas navigate", async () => {
await invokeCanvas(opts, "canvas.navigate", { url }); await invokeCanvas(opts, "canvas.navigate", { url });
if (!opts.json) defaultRuntime.log("canvas navigate ok"); if (!opts.json) {
const { ok } = getNodesTheme();
defaultRuntime.log(ok("canvas navigate ok"));
}
}); });
}), }),
); );
@@ -179,7 +188,10 @@ export function registerNodesCanvasCommands(nodes: Command) {
? (raw as { payload?: { result?: string } }).payload ? (raw as { payload?: { result?: string } }).payload
: undefined; : undefined;
if (payload?.result) defaultRuntime.log(payload.result); if (payload?.result) defaultRuntime.log(payload.result);
else defaultRuntime.log("canvas eval ok"); else {
const { ok } = getNodesTheme();
defaultRuntime.log(ok("canvas eval ok"));
}
}); });
}), }),
); );
@@ -213,8 +225,11 @@ export function registerNodesCanvasCommands(nodes: Command) {
} }
await invokeCanvas(opts, "canvas.a2ui.pushJSONL", { jsonl }); await invokeCanvas(opts, "canvas.a2ui.pushJSONL", { jsonl });
if (!opts.json) { if (!opts.json) {
const { ok } = getNodesTheme();
defaultRuntime.log( defaultRuntime.log(
`canvas a2ui push ok (v0.8, ${messageCount} message${messageCount === 1 ? "" : "s"})`, ok(
`canvas a2ui push ok (v0.8, ${messageCount} message${messageCount === 1 ? "" : "s"})`,
),
); );
} }
}); });
@@ -230,7 +245,10 @@ export function registerNodesCanvasCommands(nodes: Command) {
.action(async (opts: NodesRpcOpts) => { .action(async (opts: NodesRpcOpts) => {
await runNodesCommand("canvas a2ui reset", async () => { await runNodesCommand("canvas a2ui reset", async () => {
await invokeCanvas(opts, "canvas.a2ui.reset", undefined); await invokeCanvas(opts, "canvas.a2ui.reset", undefined);
if (!opts.json) defaultRuntime.log("canvas a2ui reset ok"); if (!opts.json) {
const { ok } = getNodesTheme();
defaultRuntime.log(ok("canvas a2ui reset ok"));
}
}); });
}), }),
); );
+11 -5
View File
@@ -2,7 +2,7 @@ import type { Command } from "commander";
import { randomIdempotencyKey } from "../../gateway/call.js"; import { randomIdempotencyKey } from "../../gateway/call.js";
import { defaultRuntime } from "../../runtime.js"; import { defaultRuntime } from "../../runtime.js";
import { parseEnvPairs, parseTimeoutMs } from "../nodes-run.js"; import { parseEnvPairs, parseTimeoutMs } from "../nodes-run.js";
import { runNodesCommand } from "./cli-utils.js"; import { getNodesTheme, runNodesCommand } from "./cli-utils.js";
import { callGatewayCli, nodesCallOpts, resolveNodeId, unauthorizedHintForMessage } from "./rpc.js"; import { callGatewayCli, nodesCallOpts, resolveNodeId, unauthorizedHintForMessage } from "./rpc.js";
import type { NodesRpcOpts } from "./types.js"; import type { NodesRpcOpts } from "./types.js";
@@ -21,7 +21,8 @@ export function registerNodesInvokeCommands(nodes: Command) {
const nodeId = await resolveNodeId(opts, String(opts.node ?? "")); const nodeId = await resolveNodeId(opts, String(opts.node ?? ""));
const command = String(opts.command ?? "").trim(); const command = String(opts.command ?? "").trim();
if (!nodeId || !command) { if (!nodeId || !command) {
defaultRuntime.error("--node and --command required"); const { error } = getNodesTheme();
defaultRuntime.error(error("--node and --command required"));
defaultRuntime.exit(1); defaultRuntime.exit(1);
return; return;
} }
@@ -108,16 +109,21 @@ export function registerNodesInvokeCommands(nodes: Command) {
if (stdout) process.stdout.write(stdout); if (stdout) process.stdout.write(stdout);
if (stderr) process.stderr.write(stderr); if (stderr) process.stderr.write(stderr);
if (timedOut) { if (timedOut) {
defaultRuntime.error("run timed out"); const { error } = getNodesTheme();
defaultRuntime.error(error("run timed out"));
defaultRuntime.exit(1); defaultRuntime.exit(1);
return; return;
} }
if (exitCode !== null && exitCode !== 0) { if (exitCode !== null && exitCode !== 0) {
const hint = unauthorizedHintForMessage(`${stderr}\n${stdout}`); const hint = unauthorizedHintForMessage(`${stderr}\n${stdout}`);
if (hint) defaultRuntime.error(hint); if (hint) {
const { warn } = getNodesTheme();
defaultRuntime.error(warn(hint));
}
} }
if (exitCode !== null && exitCode !== 0 && !success) { if (exitCode !== null && exitCode !== 0 && !success) {
defaultRuntime.error(`run exit ${exitCode}`); const { error } = getNodesTheme();
defaultRuntime.error(error(`run exit ${exitCode}`));
defaultRuntime.exit(1); defaultRuntime.exit(1);
return; return;
} }
+3 -2
View File
@@ -1,7 +1,7 @@
import type { Command } from "commander"; import type { Command } from "commander";
import { randomIdempotencyKey } from "../../gateway/call.js"; import { randomIdempotencyKey } from "../../gateway/call.js";
import { defaultRuntime } from "../../runtime.js"; import { defaultRuntime } from "../../runtime.js";
import { runNodesCommand } from "./cli-utils.js"; import { getNodesTheme, runNodesCommand } from "./cli-utils.js";
import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js"; import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js";
import type { NodesRpcOpts } from "./types.js"; import type { NodesRpcOpts } from "./types.js";
@@ -49,7 +49,8 @@ export function registerNodesNotifyCommand(nodes: Command) {
defaultRuntime.log(JSON.stringify(result, null, 2)); defaultRuntime.log(JSON.stringify(result, null, 2));
return; return;
} }
defaultRuntime.log("notify ok"); const { ok } = getNodesTheme();
defaultRuntime.log(ok("notify ok"));
}); });
}), }),
); );
+33 -10
View File
@@ -1,9 +1,10 @@
import type { Command } from "commander"; import type { Command } from "commander";
import { defaultRuntime } from "../../runtime.js"; import { defaultRuntime } from "../../runtime.js";
import { formatAge, parsePairingList } from "./format.js"; import { formatAge, parsePairingList } from "./format.js";
import { runNodesCommand } from "./cli-utils.js"; import { getNodesTheme, runNodesCommand } from "./cli-utils.js";
import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js"; import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js";
import type { NodesRpcOpts } from "./types.js"; import type { NodesRpcOpts } from "./types.js";
import { renderTable } from "../../terminal/table.js";
export function registerNodesPairingCommands(nodes: Command) { export function registerNodesPairingCommands(nodes: Command) {
nodesCallOpts( nodesCallOpts(
@@ -19,16 +20,37 @@ export function registerNodesPairingCommands(nodes: Command) {
return; return;
} }
if (pending.length === 0) { if (pending.length === 0) {
defaultRuntime.log("No pending pairing requests."); const { muted } = getNodesTheme();
defaultRuntime.log(muted("No pending pairing requests."));
return; return;
} }
for (const r of pending) { const { heading, warn, muted } = getNodesTheme();
const name = r.displayName || r.nodeId; const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const repair = r.isRepair ? " (repair)" : ""; const now = Date.now();
const ip = r.remoteIp ? ` · ${r.remoteIp}` : ""; const rows = pending.map((r) => ({
const age = typeof r.ts === "number" ? ` · ${formatAge(Date.now() - r.ts)} ago` : ""; Request: r.requestId,
defaultRuntime.log(`- ${r.requestId}: ${name}${repair}${ip}${age}`); Node: r.displayName?.trim() ? r.displayName.trim() : r.nodeId,
} IP: r.remoteIp ?? "",
Requested:
typeof r.ts === "number"
? `${formatAge(Math.max(0, now - r.ts))} ago`
: muted("unknown"),
Repair: r.isRepair ? warn("yes") : "",
}));
defaultRuntime.log(heading("Pending"));
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "Request", header: "Request", minWidth: 8 },
{ key: "Node", header: "Node", minWidth: 14, flex: true },
{ key: "IP", header: "IP", minWidth: 10 },
{ key: "Requested", header: "Requested", minWidth: 12 },
{ key: "Repair", header: "Repair", minWidth: 6 },
],
rows,
}).trimEnd(),
);
}); });
}), }),
); );
@@ -86,7 +108,8 @@ export function registerNodesPairingCommands(nodes: Command) {
defaultRuntime.log(JSON.stringify(result, null, 2)); defaultRuntime.log(JSON.stringify(result, null, 2));
return; return;
} }
defaultRuntime.log(`node rename ok: ${nodeId} -> ${name}`); const { ok } = getNodesTheme();
defaultRuntime.log(ok(`node rename ok: ${nodeId} -> ${name}`));
}); });
}), }),
); );
+207 -57
View File
@@ -1,11 +1,11 @@
import type { Command } from "commander"; import type { Command } from "commander";
import { defaultRuntime } from "../../runtime.js"; import { defaultRuntime } from "../../runtime.js";
import { formatAge, formatPermissions, parseNodeList, parsePairingList } from "./format.js"; import { formatAge, formatPermissions, parseNodeList, parsePairingList } from "./format.js";
import { runNodesCommand } from "./cli-utils.js"; import { getNodesTheme, runNodesCommand } from "./cli-utils.js";
import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js"; import { callGatewayCli, nodesCallOpts, resolveNodeId } from "./rpc.js";
import type { NodesRpcOpts } from "./types.js"; import type { NodesRpcOpts } from "./types.js";
import { renderTable } from "../../terminal/table.js"; import { renderTable } from "../../terminal/table.js";
import { isRich, theme } from "../../terminal/theme.js"; import { parseDurationMs } from "../parse-duration.js";
function formatVersionLabel(raw: string) { function formatVersionLabel(raw: string) {
const trimmed = raw.trim(); const trimmed = raw.trim();
@@ -44,44 +44,128 @@ function formatNodeVersions(node: {
return parts.length > 0 ? parts.join(" · ") : null; return parts.length > 0 ? parts.join(" · ") : null;
} }
function parseSinceMs(raw: unknown, label: string): number | undefined {
if (raw === undefined || raw === null) return undefined;
const value =
typeof raw === "string" ? raw.trim() : typeof raw === "number" ? String(raw).trim() : null;
if (value === null) {
defaultRuntime.error(`${label}: invalid duration value`);
defaultRuntime.exit(1);
return undefined;
}
if (!value) return undefined;
try {
return parseDurationMs(value);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
defaultRuntime.error(`${label}: ${message}`);
defaultRuntime.exit(1);
return undefined;
}
}
export function registerNodesStatusCommands(nodes: Command) { export function registerNodesStatusCommands(nodes: Command) {
nodesCallOpts( nodesCallOpts(
nodes nodes
.command("status") .command("status")
.description("List known nodes with connection status and capabilities") .description("List known nodes with connection status and capabilities")
.option("--connected", "Only show connected nodes")
.option("--last-connected <duration>", "Only show nodes connected within duration (e.g. 24h)")
.action(async (opts: NodesRpcOpts) => { .action(async (opts: NodesRpcOpts) => {
await runNodesCommand("status", async () => { await runNodesCommand("status", async () => {
const connectedOnly = Boolean(opts.connected);
const sinceMs = parseSinceMs(opts.lastConnected, "Invalid --last-connected");
const result = (await callGatewayCli("node.list", opts, {})) as unknown; const result = (await callGatewayCli("node.list", opts, {})) as unknown;
const obj =
typeof result === "object" && result !== null
? (result as Record<string, unknown>)
: {};
const { ok, warn, muted } = getNodesTheme();
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const now = Date.now();
const nodes = parseNodeList(result);
const lastConnectedById =
sinceMs !== undefined
? new Map(
parsePairingList(await callGatewayCli("node.pair.list", opts, {})).paired.map(
(entry) => [entry.nodeId, entry],
),
)
: null;
const filtered = nodes.filter((n) => {
if (connectedOnly && !n.connected) return false;
if (sinceMs !== undefined) {
const paired = lastConnectedById?.get(n.nodeId);
const lastConnectedAtMs =
typeof paired?.lastConnectedAtMs === "number"
? paired.lastConnectedAtMs
: typeof n.connectedAtMs === "number"
? n.connectedAtMs
: undefined;
if (typeof lastConnectedAtMs !== "number") return false;
if (now - lastConnectedAtMs > sinceMs) return false;
}
return true;
});
if (opts.json) { if (opts.json) {
defaultRuntime.log(JSON.stringify(result, null, 2)); const ts = typeof obj.ts === "number" ? obj.ts : Date.now();
defaultRuntime.log(JSON.stringify({ ...obj, ts, nodes: filtered }, null, 2));
return; return;
} }
const nodes = parseNodeList(result);
const pairedCount = nodes.filter((n) => Boolean(n.paired)).length; const pairedCount = filtered.filter((n) => Boolean(n.paired)).length;
const connectedCount = nodes.filter((n) => Boolean(n.connected)).length; const connectedCount = filtered.filter((n) => Boolean(n.connected)).length;
const filteredLabel = filtered.length !== nodes.length ? ` (of ${nodes.length})` : "";
defaultRuntime.log( defaultRuntime.log(
`Known: ${nodes.length} · Paired: ${pairedCount} · Connected: ${connectedCount}`, `Known: ${filtered.length}${filteredLabel} · Paired: ${pairedCount} · Connected: ${connectedCount}`,
); );
for (const n of nodes) { if (filtered.length === 0) return;
const name = n.displayName || n.nodeId;
const ip = n.remoteIp ? ` · ${n.remoteIp}` : ""; const rows = filtered.map((n) => {
const device = n.deviceFamily ? ` · device: ${n.deviceFamily}` : ""; const name = n.displayName?.trim() ? n.displayName.trim() : n.nodeId;
const hw = n.modelIdentifier ? ` · hw: ${n.modelIdentifier}` : "";
const perms = formatPermissions(n.permissions); const perms = formatPermissions(n.permissions);
const permsText = perms ? ` · perms: ${perms}` : "";
const versions = formatNodeVersions(n); const versions = formatNodeVersions(n);
const versionText = versions ? ` · ${versions}` : ""; const detailParts = [
const caps = n.deviceFamily ? `device: ${n.deviceFamily}` : null,
Array.isArray(n.caps) && n.caps.length > 0 n.modelIdentifier ? `hw: ${n.modelIdentifier}` : null,
? `[${n.caps.map(String).filter(Boolean).sort().join(",")}]` perms ? `perms: ${perms}` : null,
: Array.isArray(n.caps) versions,
? "[]" ].filter(Boolean) as string[];
: "?"; const caps = Array.isArray(n.caps)
const pairing = n.paired ? "paired" : "unpaired"; ? n.caps.map(String).filter(Boolean).sort().join(", ")
defaultRuntime.log( : "?";
`- ${name} · ${n.nodeId}${ip}${device}${hw}${permsText}${versionText} · ${pairing} · ${n.connected ? "connected" : "disconnected"} · caps: ${caps}`, const paired = n.paired ? ok("paired") : warn("unpaired");
); const connected = n.connected ? ok("connected") : muted("disconnected");
} const since =
typeof n.connectedAtMs === "number"
? ` (${formatAge(Math.max(0, now - n.connectedAtMs))} ago)`
: "";
return {
Node: name,
ID: n.nodeId,
IP: n.remoteIp ?? "",
Detail: detailParts.join(" · "),
Status: `${paired} · ${connected}${since}`,
Caps: caps,
};
});
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "Node", header: "Node", minWidth: 14, flex: true },
{ key: "ID", header: "ID", minWidth: 10 },
{ key: "IP", header: "IP", minWidth: 10 },
{ key: "Detail", header: "Detail", minWidth: 18, flex: true },
{ key: "Status", header: "Status", minWidth: 18 },
{ key: "Caps", header: "Caps", minWidth: 12, flex: true },
],
rows,
}).trimEnd(),
);
}); });
}), }),
); );
@@ -108,6 +192,7 @@ export function registerNodesStatusCommands(nodes: Command) {
: {}; : {};
const displayName = typeof obj.displayName === "string" ? obj.displayName : nodeId; const displayName = typeof obj.displayName === "string" ? obj.displayName : nodeId;
const connected = Boolean(obj.connected); const connected = Boolean(obj.connected);
const paired = Boolean(obj.paired);
const caps = Array.isArray(obj.caps) ? obj.caps.map(String).filter(Boolean).sort() : null; const caps = Array.isArray(obj.caps) ? obj.caps.map(String).filter(Boolean).sort() : null;
const commands = Array.isArray(obj.commands) const commands = Array.isArray(obj.commands)
? obj.commands.map(String).filter(Boolean).sort() ? obj.commands.map(String).filter(Boolean).sort()
@@ -125,18 +210,38 @@ export function registerNodesStatusCommands(nodes: Command) {
}, },
); );
const parts: string[] = ["Node:", displayName, nodeId]; const { heading, ok, warn, muted } = getNodesTheme();
if (ip) parts.push(ip); const status = `${paired ? ok("paired") : warn("unpaired")} · ${
if (family) parts.push(`device: ${family}`); connected ? ok("connected") : muted("disconnected")
if (model) parts.push(`hw: ${model}`); }`;
if (perms) parts.push(`perms: ${perms}`); const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
if (versions) parts.push(versions); const rows = [
parts.push(connected ? "connected" : "disconnected"); { Field: "ID", Value: nodeId },
parts.push(`caps: ${caps ? `[${caps.join(",")}]` : "?"}`); displayName ? { Field: "Name", Value: displayName } : null,
defaultRuntime.log(parts.join(" · ")); ip ? { Field: "IP", Value: ip } : null,
defaultRuntime.log("Commands:"); family ? { Field: "Device", Value: family } : null,
model ? { Field: "Model", Value: model } : null,
perms ? { Field: "Perms", Value: perms } : null,
versions ? { Field: "Version", Value: versions } : null,
{ Field: "Status", Value: status },
{ Field: "Caps", Value: caps ? caps.join(", ") : "?" },
].filter(Boolean) as Array<{ Field: string; Value: string }>;
defaultRuntime.log(heading("Node"));
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "Field", header: "Field", minWidth: 8 },
{ key: "Value", header: "Value", minWidth: 24, flex: true },
],
rows,
}).trimEnd(),
);
defaultRuntime.log("");
defaultRuntime.log(heading("Commands"));
if (commands.length === 0) { if (commands.length === 0) {
defaultRuntime.log("- (none reported)"); defaultRuntime.log(muted("- (none reported)"));
return; return;
} }
for (const c of commands) defaultRuntime.log(`- ${c}`); for (const c of commands) defaultRuntime.log(`- ${c}`);
@@ -148,24 +253,60 @@ export function registerNodesStatusCommands(nodes: Command) {
nodes nodes
.command("list") .command("list")
.description("List pending and paired nodes") .description("List pending and paired nodes")
.option("--connected", "Only show connected nodes")
.option("--last-connected <duration>", "Only show nodes connected within duration (e.g. 24h)")
.action(async (opts: NodesRpcOpts) => { .action(async (opts: NodesRpcOpts) => {
await runNodesCommand("list", async () => { await runNodesCommand("list", async () => {
const connectedOnly = Boolean(opts.connected);
const sinceMs = parseSinceMs(opts.lastConnected, "Invalid --last-connected");
const result = (await callGatewayCli("node.pair.list", opts, {})) as unknown; const result = (await callGatewayCli("node.pair.list", opts, {})) as unknown;
if (opts.json) {
defaultRuntime.log(JSON.stringify(result, null, 2));
return;
}
const { pending, paired } = parsePairingList(result); const { pending, paired } = parsePairingList(result);
defaultRuntime.log(`Pending: ${pending.length} · Paired: ${paired.length}`); const { heading, muted, warn } = getNodesTheme();
const rich = isRich();
const heading = (text: string) => (rich ? theme.heading(text) : text);
const muted = (text: string) => (rich ? theme.muted(text) : text);
const warn = (text: string) => (rich ? theme.warn(text) : text);
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1); const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const now = Date.now(); const now = Date.now();
const hasFilters = connectedOnly || sinceMs !== undefined;
const pendingRows = hasFilters ? [] : pending;
const connectedById = hasFilters
? new Map(
parseNodeList(await callGatewayCli("node.list", opts, {})).map((node) => [
node.nodeId,
node,
]),
)
: null;
const filteredPaired = paired.filter((node) => {
if (connectedOnly) {
const live = connectedById?.get(node.nodeId);
if (!live?.connected) return false;
}
if (sinceMs !== undefined) {
const live = connectedById?.get(node.nodeId);
const lastConnectedAtMs =
typeof node.lastConnectedAtMs === "number"
? node.lastConnectedAtMs
: typeof live?.connectedAtMs === "number"
? live.connectedAtMs
: undefined;
if (typeof lastConnectedAtMs !== "number") return false;
if (now - lastConnectedAtMs > sinceMs) return false;
}
return true;
});
const filteredLabel =
hasFilters && filteredPaired.length !== paired.length ? ` (of ${paired.length})` : "";
defaultRuntime.log(
`Pending: ${pendingRows.length} · Paired: ${filteredPaired.length}${filteredLabel}`,
);
if (pending.length > 0) { if (opts.json) {
const pendingRows = pending.map((r) => ({ defaultRuntime.log(
JSON.stringify({ pending: pendingRows, paired: filteredPaired }, null, 2),
);
return;
}
if (pendingRows.length > 0) {
const pendingRowsRendered = pendingRows.map((r) => ({
Request: r.requestId, Request: r.requestId,
Node: r.displayName?.trim() ? r.displayName.trim() : r.nodeId, Node: r.displayName?.trim() ? r.displayName.trim() : r.nodeId,
IP: r.remoteIp ?? "", IP: r.remoteIp ?? "",
@@ -187,21 +328,30 @@ export function registerNodesStatusCommands(nodes: Command) {
{ key: "Requested", header: "Requested", minWidth: 12 }, { key: "Requested", header: "Requested", minWidth: 12 },
{ key: "Repair", header: "Repair", minWidth: 6 }, { key: "Repair", header: "Repair", minWidth: 6 },
], ],
rows: pendingRows, rows: pendingRowsRendered,
}).trimEnd(), }).trimEnd(),
); );
} }
if (paired.length > 0) { if (filteredPaired.length > 0) {
const pairedRows = paired.map((n) => ({ const pairedRows = filteredPaired.map((n) => {
Node: n.displayName?.trim() ? n.displayName.trim() : n.nodeId, const live = connectedById?.get(n.nodeId);
Id: n.nodeId, const lastConnectedAtMs =
IP: n.remoteIp ?? "",
LastConnect:
typeof n.lastConnectedAtMs === "number" typeof n.lastConnectedAtMs === "number"
? `${formatAge(Math.max(0, now - n.lastConnectedAtMs))} ago` ? n.lastConnectedAtMs
: muted("unknown"), : typeof live?.connectedAtMs === "number"
})); ? live.connectedAtMs
: undefined;
return {
Node: n.displayName?.trim() ? n.displayName.trim() : n.nodeId,
Id: n.nodeId,
IP: n.remoteIp ?? "",
LastConnect:
typeof lastConnectedAtMs === "number"
? `${formatAge(Math.max(0, now - lastConnectedAtMs))} ago`
: muted("unknown"),
};
});
defaultRuntime.log(""); defaultRuntime.log("");
defaultRuntime.log(heading("Paired")); defaultRuntime.log(heading("Paired"));
defaultRuntime.log( defaultRuntime.log(
+3
View File
@@ -8,6 +8,8 @@ export type NodesRpcOpts = {
params?: string; params?: string;
invokeTimeout?: string; invokeTimeout?: string;
idempotencyKey?: string; idempotencyKey?: string;
connected?: boolean;
lastConnected?: string;
target?: string; target?: string;
x?: string; x?: string;
y?: string; y?: string;
@@ -56,6 +58,7 @@ export type NodeListNode = {
permissions?: Record<string, boolean>; permissions?: Record<string, boolean>;
paired?: boolean; paired?: boolean;
connected?: boolean; connected?: boolean;
connectedAtMs?: number;
}; };
export type PendingRequest = { export type PendingRequest = {
+30 -9
View File
@@ -8,7 +8,9 @@ import {
listChannelPairingRequests, listChannelPairingRequests,
type PairingChannel, type PairingChannel,
} from "../pairing/pairing-store.js"; } from "../pairing/pairing-store.js";
import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { renderTable } from "../terminal/table.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { formatCliCommand } from "./command-format.js"; import { formatCliCommand } from "./command-format.js";
@@ -70,18 +72,35 @@ export function registerPairingCli(program: Command) {
const channel = parseChannel(channelRaw, channels); const channel = parseChannel(channelRaw, channels);
const requests = await listChannelPairingRequests(channel); const requests = await listChannelPairingRequests(channel);
if (opts.json) { if (opts.json) {
console.log(JSON.stringify({ channel, requests }, null, 2)); defaultRuntime.log(JSON.stringify({ channel, requests }, null, 2));
return; return;
} }
if (requests.length === 0) { if (requests.length === 0) {
console.log(`No pending ${channel} pairing requests.`); defaultRuntime.log(theme.muted(`No pending ${channel} pairing requests.`));
return; return;
} }
for (const r of requests) { const idLabel = resolvePairingIdLabel(channel);
const meta = r.meta ? JSON.stringify(r.meta) : ""; const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const idLabel = resolvePairingIdLabel(channel); defaultRuntime.log(
console.log(`${r.code} ${idLabel}=${r.id}${meta ? ` meta=${meta}` : ""} ${r.createdAt}`); `${theme.heading("Pairing requests")} ${theme.muted(`(${requests.length})`)}`,
} );
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "Code", header: "Code", minWidth: 10 },
{ key: "ID", header: idLabel, minWidth: 12, flex: true },
{ key: "Meta", header: "Meta", minWidth: 8, flex: true },
{ key: "Requested", header: "Requested", minWidth: 12 },
],
rows: requests.map((r) => ({
Code: r.code,
ID: r.id,
Meta: r.meta ? JSON.stringify(r.meta) : "",
Requested: r.createdAt,
})),
}).trimEnd(),
);
}); });
pairing pairing
@@ -113,11 +132,13 @@ export function registerPairingCli(program: Command) {
throw new Error(`No pending pairing request found for code: ${String(resolvedCode)}`); throw new Error(`No pending pairing request found for code: ${String(resolvedCode)}`);
} }
console.log(`Approved ${channel} sender ${approved.id}.`); defaultRuntime.log(
`${theme.success("Approved")} ${theme.muted(channel)} sender ${theme.command(approved.id)}.`,
);
if (!opts.notify) return; if (!opts.notify) return;
await notifyApproved(channel, approved.id).catch((err) => { await notifyApproved(channel, approved.id).catch((err) => {
console.log(`Failed to notify requester: ${String(err)}`); defaultRuntime.log(theme.warn(`Failed to notify requester: ${String(err)}`));
}); });
}); });
} }
+79 -45
View File
@@ -1,6 +1,5 @@
import fs from "node:fs"; import fs from "node:fs";
import path from "node:path"; import path from "node:path";
import chalk from "chalk";
import type { Command } from "commander"; import type { Command } from "commander";
import { loadConfig, writeConfigFile } from "../config/config.js"; import { loadConfig, writeConfigFile } from "../config/config.js";
@@ -14,6 +13,7 @@ import { buildPluginStatusReport } from "../plugins/status.js";
import { updateNpmInstalledPlugins } from "../plugins/update.js"; import { updateNpmInstalledPlugins } from "../plugins/update.js";
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { renderTable } from "../terminal/table.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { resolveUserPath } from "../utils.js"; import { resolveUserPath } from "../utils.js";
@@ -35,19 +35,19 @@ export type PluginUpdateOptions = {
function formatPluginLine(plugin: PluginRecord, verbose = false): string { function formatPluginLine(plugin: PluginRecord, verbose = false): string {
const status = const status =
plugin.status === "loaded" plugin.status === "loaded"
? chalk.green("✓") ? theme.success("loaded")
: plugin.status === "disabled" : plugin.status === "disabled"
? chalk.yellow("disabled") ? theme.warn("disabled")
: chalk.red("error"); : theme.error("error");
const name = plugin.name ? chalk.white(plugin.name) : chalk.white(plugin.id); const name = theme.command(plugin.name || plugin.id);
const idSuffix = plugin.name !== plugin.id ? chalk.gray(` (${plugin.id})`) : ""; const idSuffix = plugin.name && plugin.name !== plugin.id ? theme.muted(` (${plugin.id})`) : "";
const desc = plugin.description const desc = plugin.description
? chalk.gray( ? theme.muted(
plugin.description.length > 60 plugin.description.length > 60
? `${plugin.description.slice(0, 57)}...` ? `${plugin.description.slice(0, 57)}...`
: plugin.description, : plugin.description,
) )
: chalk.gray("(no description)"); : theme.muted("(no description)");
if (!verbose) { if (!verbose) {
return `${name}${idSuffix} ${status} - ${desc}`; return `${name}${idSuffix} ${status} - ${desc}`;
@@ -55,14 +55,14 @@ function formatPluginLine(plugin: PluginRecord, verbose = false): string {
const parts = [ const parts = [
`${name}${idSuffix} ${status}`, `${name}${idSuffix} ${status}`,
` source: ${chalk.gray(plugin.source)}`, ` source: ${theme.muted(plugin.source)}`,
` origin: ${plugin.origin}`, ` origin: ${plugin.origin}`,
]; ];
if (plugin.version) parts.push(` version: ${plugin.version}`); if (plugin.version) parts.push(` version: ${plugin.version}`);
if (plugin.providerIds.length > 0) { if (plugin.providerIds.length > 0) {
parts.push(` providers: ${plugin.providerIds.join(", ")}`); parts.push(` providers: ${plugin.providerIds.join(", ")}`);
} }
if (plugin.error) parts.push(chalk.red(` error: ${plugin.error}`)); if (plugin.error) parts.push(theme.error(` error: ${plugin.error}`));
return parts.join("\n"); return parts.join("\n");
} }
@@ -87,7 +87,7 @@ function applySlotSelectionForPlugin(
function logSlotWarnings(warnings: string[]) { function logSlotWarnings(warnings: string[]) {
if (warnings.length === 0) return; if (warnings.length === 0) return;
for (const warning of warnings) { for (const warning of warnings) {
defaultRuntime.log(chalk.yellow(warning)); defaultRuntime.log(theme.warn(warning));
} }
} }
@@ -124,19 +124,51 @@ export function registerPluginsCli(program: Command) {
} }
if (list.length === 0) { if (list.length === 0) {
defaultRuntime.log("No plugins found."); defaultRuntime.log(theme.muted("No plugins found."));
return;
}
const loaded = list.filter((p) => p.status === "loaded").length;
defaultRuntime.log(
`${theme.heading("Plugins")} ${theme.muted(`(${loaded}/${list.length} loaded)`)}`,
);
if (!opts.verbose) {
const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const rows = list.map((plugin) => ({
Name: plugin.name || plugin.id,
ID: plugin.name && plugin.name !== plugin.id ? plugin.id : "",
Status:
plugin.status === "loaded"
? theme.success("loaded")
: plugin.status === "disabled"
? theme.warn("disabled")
: theme.error("error"),
Source: plugin.source,
Version: plugin.version ?? "",
Description: plugin.description ?? "",
}));
defaultRuntime.log(
renderTable({
width: tableWidth,
columns: [
{ key: "Name", header: "Name", minWidth: 14, flex: true },
{ key: "ID", header: "ID", minWidth: 10, flex: true },
{ key: "Status", header: "Status", minWidth: 10 },
{ key: "Source", header: "Source", minWidth: 10 },
{ key: "Version", header: "Version", minWidth: 8 },
{ key: "Description", header: "Description", minWidth: 18, flex: true },
],
rows,
}).trimEnd(),
);
return; return;
} }
const lines: string[] = []; const lines: string[] = [];
const loaded = list.filter((p) => p.status === "loaded").length;
lines.push(
`${chalk.bold.cyan("Plugins")} ${chalk.gray(`(${loaded}/${list.length} loaded)`)}`,
);
lines.push("");
for (const plugin of list) { for (const plugin of list) {
lines.push(formatPluginLine(plugin, opts.verbose)); lines.push(formatPluginLine(plugin, true));
if (opts.verbose) lines.push(""); lines.push("");
} }
defaultRuntime.log(lines.join("\n").trim()); defaultRuntime.log(lines.join("\n").trim());
}); });
@@ -162,43 +194,45 @@ export function registerPluginsCli(program: Command) {
} }
const lines: string[] = []; const lines: string[] = [];
lines.push(chalk.bold.cyan(plugin.name || plugin.id)); lines.push(theme.heading(plugin.name || plugin.id));
if (plugin.name && plugin.name !== plugin.id) { if (plugin.name && plugin.name !== plugin.id) {
lines.push(chalk.gray(`id: ${plugin.id}`)); lines.push(theme.muted(`id: ${plugin.id}`));
} }
if (plugin.description) lines.push(plugin.description); if (plugin.description) lines.push(plugin.description);
lines.push(""); lines.push("");
lines.push(`Status: ${plugin.status}`); lines.push(`${theme.muted("Status:")} ${plugin.status}`);
lines.push(`Source: ${plugin.source}`); lines.push(`${theme.muted("Source:")} ${plugin.source}`);
lines.push(`Origin: ${plugin.origin}`); lines.push(`${theme.muted("Origin:")} ${plugin.origin}`);
if (plugin.version) lines.push(`Version: ${plugin.version}`); if (plugin.version) lines.push(`${theme.muted("Version:")} ${plugin.version}`);
if (plugin.toolNames.length > 0) { if (plugin.toolNames.length > 0) {
lines.push(`Tools: ${plugin.toolNames.join(", ")}`); lines.push(`${theme.muted("Tools:")} ${plugin.toolNames.join(", ")}`);
} }
if (plugin.hookNames.length > 0) { if (plugin.hookNames.length > 0) {
lines.push(`Hooks: ${plugin.hookNames.join(", ")}`); lines.push(`${theme.muted("Hooks:")} ${plugin.hookNames.join(", ")}`);
} }
if (plugin.gatewayMethods.length > 0) { if (plugin.gatewayMethods.length > 0) {
lines.push(`Gateway methods: ${plugin.gatewayMethods.join(", ")}`); lines.push(`${theme.muted("Gateway methods:")} ${plugin.gatewayMethods.join(", ")}`);
} }
if (plugin.providerIds.length > 0) { if (plugin.providerIds.length > 0) {
lines.push(`Providers: ${plugin.providerIds.join(", ")}`); lines.push(`${theme.muted("Providers:")} ${plugin.providerIds.join(", ")}`);
} }
if (plugin.cliCommands.length > 0) { if (plugin.cliCommands.length > 0) {
lines.push(`CLI commands: ${plugin.cliCommands.join(", ")}`); lines.push(`${theme.muted("CLI commands:")} ${plugin.cliCommands.join(", ")}`);
} }
if (plugin.services.length > 0) { if (plugin.services.length > 0) {
lines.push(`Services: ${plugin.services.join(", ")}`); lines.push(`${theme.muted("Services:")} ${plugin.services.join(", ")}`);
} }
if (plugin.error) lines.push(chalk.red(`Error: ${plugin.error}`)); if (plugin.error) lines.push(`${theme.error("Error:")} ${plugin.error}`);
if (install) { if (install) {
lines.push(""); lines.push("");
lines.push(`Install: ${install.source}`); lines.push(`${theme.muted("Install:")} ${install.source}`);
if (install.spec) lines.push(`Spec: ${install.spec}`); if (install.spec) lines.push(`${theme.muted("Spec:")} ${install.spec}`);
if (install.sourcePath) lines.push(`Source path: ${install.sourcePath}`); if (install.sourcePath) lines.push(`${theme.muted("Source path:")} ${install.sourcePath}`);
if (install.installPath) lines.push(`Install path: ${install.installPath}`); if (install.installPath)
if (install.version) lines.push(`Recorded version: ${install.version}`); lines.push(`${theme.muted("Install path:")} ${install.installPath}`);
if (install.installedAt) lines.push(`Installed at: ${install.installedAt}`); if (install.version) lines.push(`${theme.muted("Recorded version:")} ${install.version}`);
if (install.installedAt)
lines.push(`${theme.muted("Installed at:")} ${install.installedAt}`);
} }
defaultRuntime.log(lines.join("\n")); defaultRuntime.log(lines.join("\n"));
}); });
@@ -308,7 +342,7 @@ export function registerPluginsCli(program: Command) {
path: resolved, path: resolved,
logger: { logger: {
info: (msg) => defaultRuntime.log(msg), info: (msg) => defaultRuntime.log(msg),
warn: (msg) => defaultRuntime.log(chalk.yellow(msg)), warn: (msg) => defaultRuntime.log(theme.warn(msg)),
}, },
}); });
if (!result.ok) { if (!result.ok) {
@@ -372,7 +406,7 @@ export function registerPluginsCli(program: Command) {
spec: raw, spec: raw,
logger: { logger: {
info: (msg) => defaultRuntime.log(msg), info: (msg) => defaultRuntime.log(msg),
warn: (msg) => defaultRuntime.log(chalk.yellow(msg)), warn: (msg) => defaultRuntime.log(theme.warn(msg)),
}, },
}); });
if (!result.ok) { if (!result.ok) {
@@ -430,17 +464,17 @@ export function registerPluginsCli(program: Command) {
dryRun: opts.dryRun, dryRun: opts.dryRun,
logger: { logger: {
info: (msg) => defaultRuntime.log(msg), info: (msg) => defaultRuntime.log(msg),
warn: (msg) => defaultRuntime.log(chalk.yellow(msg)), warn: (msg) => defaultRuntime.log(theme.warn(msg)),
}, },
}); });
for (const outcome of result.outcomes) { for (const outcome of result.outcomes) {
if (outcome.status === "error") { if (outcome.status === "error") {
defaultRuntime.log(chalk.red(outcome.message)); defaultRuntime.log(theme.error(outcome.message));
continue; continue;
} }
if (outcome.status === "skipped") { if (outcome.status === "skipped") {
defaultRuntime.log(chalk.yellow(outcome.message)); defaultRuntime.log(theme.warn(outcome.message));
continue; continue;
} }
defaultRuntime.log(outcome.message); defaultRuntime.log(outcome.message);
@@ -467,14 +501,14 @@ export function registerPluginsCli(program: Command) {
const lines: string[] = []; const lines: string[] = [];
if (errors.length > 0) { if (errors.length > 0) {
lines.push(chalk.bold.red("Plugin errors:")); lines.push(theme.error("Plugin errors:"));
for (const entry of errors) { for (const entry of errors) {
lines.push(`- ${entry.id}: ${entry.error ?? "failed to load"} (${entry.source})`); lines.push(`- ${entry.id}: ${entry.error ?? "failed to load"} (${entry.source})`);
} }
} }
if (diags.length > 0) { if (diags.length > 0) {
if (lines.length > 0) lines.push(""); if (lines.length > 0) lines.push("");
lines.push(chalk.bold.yellow("Diagnostics:")); lines.push(theme.warn("Diagnostics:"));
for (const diag of diags) { for (const diag of diags) {
const target = diag.pluginId ? `${diag.pluginId}: ` : ""; const target = diag.pluginId ? `${diag.pluginId}: ` : "";
lines.push(`- ${target}${diag.message}`); lines.push(`- ${target}${diag.message}`);
+92 -5
View File
@@ -68,6 +68,83 @@ describe("cli program (nodes basics)", () => {
expect(runtime.log).toHaveBeenCalledWith("Pending: 0 · Paired: 0"); expect(runtime.log).toHaveBeenCalledWith("Pending: 0 · Paired: 0");
}); });
it("runs nodes list --connected and filters to connected nodes", async () => {
const now = Date.now();
callGateway.mockImplementation(async (opts: { method?: string }) => {
if (opts.method === "node.pair.list") {
return {
pending: [],
paired: [
{
nodeId: "n1",
displayName: "One",
remoteIp: "10.0.0.1",
lastConnectedAtMs: now - 1_000,
},
{
nodeId: "n2",
displayName: "Two",
remoteIp: "10.0.0.2",
lastConnectedAtMs: now - 1_000,
},
],
};
}
if (opts.method === "node.list") {
return {
nodes: [
{ nodeId: "n1", connected: true },
{ nodeId: "n2", connected: false },
],
};
}
return { ok: true };
});
const program = buildProgram();
runtime.log.mockClear();
await program.parseAsync(["nodes", "list", "--connected"], { from: "user" });
expect(callGateway).toHaveBeenCalledWith(expect.objectContaining({ method: "node.list" }));
const output = runtime.log.mock.calls.map((c) => String(c[0] ?? "")).join("\n");
expect(output).toContain("One");
expect(output).not.toContain("Two");
});
it("runs nodes status --last-connected and filters by age", async () => {
const now = Date.now();
callGateway.mockImplementation(async (opts: { method?: string }) => {
if (opts.method === "node.list") {
return {
ts: now,
nodes: [
{ nodeId: "n1", displayName: "One", connected: false },
{ nodeId: "n2", displayName: "Two", connected: false },
],
};
}
if (opts.method === "node.pair.list") {
return {
pending: [],
paired: [
{ nodeId: "n1", lastConnectedAtMs: now - 1_000 },
{ nodeId: "n2", lastConnectedAtMs: now - 2 * 24 * 60 * 60 * 1000 },
],
};
}
return { ok: true };
});
const program = buildProgram();
runtime.log.mockClear();
await program.parseAsync(["nodes", "status", "--last-connected", "24h"], {
from: "user",
});
expect(callGateway).toHaveBeenCalledWith(expect.objectContaining({ method: "node.pair.list" }));
const output = runtime.log.mock.calls.map((c) => String(c[0] ?? "")).join("\n");
expect(output).toContain("One");
expect(output).not.toContain("Two");
});
it("runs nodes status and calls node.list", async () => { it("runs nodes status and calls node.list", async () => {
callGateway.mockResolvedValue({ callGateway.mockResolvedValue({
ts: Date.now(), ts: Date.now(),
@@ -95,10 +172,14 @@ describe("cli program (nodes basics)", () => {
const output = runtime.log.mock.calls.map((c) => String(c[0] ?? "")).join("\n"); const output = runtime.log.mock.calls.map((c) => String(c[0] ?? "")).join("\n");
expect(output).toContain("Known: 1 · Paired: 1 · Connected: 1"); expect(output).toContain("Known: 1 · Paired: 1 · Connected: 1");
expect(output).toContain("iOS Node"); expect(output).toContain("iOS Node");
expect(output).toContain("Detail");
expect(output).toContain("device: iPad"); expect(output).toContain("device: iPad");
expect(output).toContain("hw: iPad16,6"); expect(output).toContain("hw: iPad16,6");
expect(output).toContain("Status");
expect(output).toContain("paired"); expect(output).toContain("paired");
expect(output).toContain("caps: [camera,canvas]"); expect(output).toContain("Caps");
expect(output).toContain("camera");
expect(output).toContain("canvas");
}); });
it("runs nodes status and shows unpaired nodes", async () => { it("runs nodes status and shows unpaired nodes", async () => {
@@ -123,12 +204,18 @@ describe("cli program (nodes basics)", () => {
const output = runtime.log.mock.calls.map((c) => String(c[0] ?? "")).join("\n"); const output = runtime.log.mock.calls.map((c) => String(c[0] ?? "")).join("\n");
expect(output).toContain("Known: 1 · Paired: 0 · Connected: 1"); expect(output).toContain("Known: 1 · Paired: 0 · Connected: 1");
expect(output).toContain("Peter's Tab S10 Ultra"); expect(output).toContain("Peter's Tab");
expect(output).toContain("S10 Ultra");
expect(output).toContain("Detail");
expect(output).toContain("device: Android"); expect(output).toContain("device: Android");
expect(output).toContain("hw: samsung SM-X926B"); expect(output).toContain("hw: samsung");
expect(output).toContain("SM-X926B");
expect(output).toContain("Status");
expect(output).toContain("unpaired"); expect(output).toContain("unpaired");
expect(output).toContain("connected"); expect(output).toContain("connected");
expect(output).toContain("caps: [camera,canvas]"); expect(output).toContain("Caps");
expect(output).toContain("camera");
expect(output).toContain("canvas");
}); });
it("runs nodes describe and calls node.describe", async () => { it("runs nodes describe and calls node.describe", async () => {
@@ -176,7 +263,7 @@ describe("cli program (nodes basics)", () => {
); );
const out = runtime.log.mock.calls.map((c) => String(c[0] ?? "")).join("\n"); const out = runtime.log.mock.calls.map((c) => String(c[0] ?? "")).join("\n");
expect(out).toContain("Commands:"); expect(out).toContain("Commands");
expect(out).toContain("canvas.eval"); expect(out).toContain("canvas.eval");
}); });
-5
View File
@@ -4,7 +4,6 @@ import { agentsListCommand } from "../../commands/agents.js";
import { healthCommand } from "../../commands/health.js"; import { healthCommand } from "../../commands/health.js";
import { sessionsCommand } from "../../commands/sessions.js"; import { sessionsCommand } from "../../commands/sessions.js";
import { statusCommand } from "../../commands/status.js"; import { statusCommand } from "../../commands/status.js";
import { setVerbose } from "../../globals.js";
import { defaultRuntime } from "../../runtime.js"; import { defaultRuntime } from "../../runtime.js";
import { getFlagValue, getPositiveIntFlagValue, getVerboseFlag, hasFlag } from "../argv.js"; import { getFlagValue, getPositiveIntFlagValue, getVerboseFlag, hasFlag } from "../argv.js";
import { registerBrowserCli } from "../browser-cli.js"; import { registerBrowserCli } from "../browser-cli.js";
@@ -46,7 +45,6 @@ const routeHealth: RouteSpec = {
const verbose = getVerboseFlag(argv, { includeDebug: true }); const verbose = getVerboseFlag(argv, { includeDebug: true });
const timeoutMs = getPositiveIntFlagValue(argv, "--timeout"); const timeoutMs = getPositiveIntFlagValue(argv, "--timeout");
if (timeoutMs === null) return false; if (timeoutMs === null) return false;
setVerbose(verbose);
await healthCommand({ json, timeoutMs, verbose }, defaultRuntime); await healthCommand({ json, timeoutMs, verbose }, defaultRuntime);
return true; return true;
}, },
@@ -63,7 +61,6 @@ const routeStatus: RouteSpec = {
const verbose = getVerboseFlag(argv, { includeDebug: true }); const verbose = getVerboseFlag(argv, { includeDebug: true });
const timeoutMs = getPositiveIntFlagValue(argv, "--timeout"); const timeoutMs = getPositiveIntFlagValue(argv, "--timeout");
if (timeoutMs === null) return false; if (timeoutMs === null) return false;
setVerbose(verbose);
await statusCommand({ json, deep, all, usage, timeoutMs, verbose }, defaultRuntime); await statusCommand({ json, deep, all, usage, timeoutMs, verbose }, defaultRuntime);
return true; return true;
}, },
@@ -73,12 +70,10 @@ const routeSessions: RouteSpec = {
match: (path) => path[0] === "sessions", match: (path) => path[0] === "sessions",
run: async (argv) => { run: async (argv) => {
const json = hasFlag(argv, "--json"); const json = hasFlag(argv, "--json");
const verbose = getVerboseFlag(argv);
const store = getFlagValue(argv, "--store"); const store = getFlagValue(argv, "--store");
if (store === null) return false; if (store === null) return false;
const active = getFlagValue(argv, "--active"); const active = getFlagValue(argv, "--active");
if (active === null) return false; if (active === null) return false;
setVerbose(verbose);
await sessionsCommand({ json, store, active }, defaultRuntime); await sessionsCommand({ json, store, active }, defaultRuntime);
return true; return true;
}, },
+2 -1
View File
@@ -80,7 +80,8 @@ export function configureProgramHelp(program: Command, ctx: ProgramContext) {
([cmd, desc]) => ` ${theme.command(cmd)}\n ${theme.muted(desc)}`, ([cmd, desc]) => ` ${theme.command(cmd)}\n ${theme.muted(desc)}`,
).join("\n"); ).join("\n");
program.addHelpText("afterAll", () => { program.addHelpText("afterAll", ({ command }) => {
if (command !== program) return "";
const docs = formatDocsLink("/cli", "docs.clawd.bot/cli"); const docs = formatDocsLink("/cli", "docs.clawd.bot/cli");
return `\n${theme.heading("Examples:")}\n${fmtExamples}\n\n${theme.muted("Docs:")} ${docs}\n`; return `\n${theme.heading("Examples:")}\n${fmtExamples}\n\n${theme.muted("Docs:")} ${docs}\n`;
}); });
+7 -1
View File
@@ -1,9 +1,10 @@
import type { Command } from "commander"; import type { Command } from "commander";
import { defaultRuntime } from "../../runtime.js"; import { defaultRuntime } from "../../runtime.js";
import { emitCliBanner } from "../banner.js"; import { emitCliBanner } from "../banner.js";
import { getCommandPath, hasHelpOrVersion } from "../argv.js"; import { getCommandPath, getVerboseFlag, hasHelpOrVersion } from "../argv.js";
import { ensureConfigReady } from "./config-guard.js"; import { ensureConfigReady } from "./config-guard.js";
import { ensurePluginRegistryLoaded } from "../plugin-registry.js"; import { ensurePluginRegistryLoaded } from "../plugin-registry.js";
import { setVerbose } from "../../globals.js";
function setProcessTitleForCommand(actionCommand: Command) { function setProcessTitleForCommand(actionCommand: Command) {
let current: Command = actionCommand; let current: Command = actionCommand;
@@ -24,6 +25,11 @@ export function registerPreActionHooks(program: Command, programVersion: string)
emitCliBanner(programVersion); emitCliBanner(programVersion);
const argv = process.argv; const argv = process.argv;
if (hasHelpOrVersion(argv)) return; if (hasHelpOrVersion(argv)) return;
const verbose = getVerboseFlag(argv, { includeDebug: true });
setVerbose(verbose);
if (!verbose) {
process.env.NODE_NO_WARNINGS ??= "1";
}
const commandPath = getCommandPath(argv, 2); const commandPath = getCommandPath(argv, 2);
if (commandPath[0] === "doctor") return; if (commandPath[0] === "doctor") return;
await ensureConfigReady({ runtime: defaultRuntime, commandPath }); await ensureConfigReady({ runtime: defaultRuntime, commandPath });
+28 -11
View File
@@ -12,6 +12,7 @@ import { defaultRuntime } from "../../runtime.js";
import { formatDocsLink } from "../../terminal/links.js"; import { formatDocsLink } from "../../terminal/links.js";
import { theme } from "../../terminal/theme.js"; import { theme } from "../../terminal/theme.js";
import { hasExplicitOptions } from "../command-options.js"; import { hasExplicitOptions } from "../command-options.js";
import { formatHelpExamples } from "../help-format.js";
import { createDefaultDeps } from "../deps.js"; import { createDefaultDeps } from "../deps.js";
import { runCommandWithRuntime } from "../cli-utils.js"; import { runCommandWithRuntime } from "../cli-utils.js";
import { collectOption } from "./helpers.js"; import { collectOption } from "./helpers.js";
@@ -48,13 +49,24 @@ export function registerAgentCommands(program: Command, args: { agentChannelOpti
"after", "after",
() => () =>
` `
Examples: ${theme.heading("Examples:")}
clawdbot agent --to +15555550123 --message "status update" ${formatHelpExamples([
clawdbot agent --agent ops --message "Summarize logs" ['clawdbot agent --to +15555550123 --message "status update"', "Start a new session."],
clawdbot agent --session-id 1234 --message "Summarize inbox" --thinking medium ['clawdbot agent --agent ops --message "Summarize logs"', "Use a specific agent."],
clawdbot agent --to +15555550123 --message "Trace logs" --verbose on --json [
clawdbot agent --to +15555550123 --message "Summon reply" --deliver 'clawdbot agent --session-id 1234 --message "Summarize inbox" --thinking medium',
clawdbot agent --agent ops --message "Generate report" --deliver --reply-channel slack --reply-to "#reports" "Target a session with explicit thinking level.",
],
[
'clawdbot agent --to +15555550123 --message "Trace logs" --verbose on --json',
"Enable verbose logging and JSON output.",
],
['clawdbot agent --to +15555550123 --message "Summon reply" --deliver', "Deliver reply."],
[
'clawdbot agent --agent ops --message "Generate report" --deliver --reply-channel slack --reply-to "#reports"',
"Send reply to a different channel/target.",
],
])}
${theme.muted("Docs:")} ${formatDocsLink("/cli/agent", "docs.clawd.bot/cli/agent")}`, ${theme.muted("Docs:")} ${formatDocsLink("/cli/agent", "docs.clawd.bot/cli/agent")}`,
) )
@@ -140,10 +152,15 @@ ${theme.muted("Docs:")} ${formatDocsLink("/cli/agent", "docs.clawd.bot/cli/agent
"after", "after",
() => () =>
` `
Examples: ${theme.heading("Examples:")}
clawdbot agents set-identity --agent main --name "Clawd" --emoji "🦞" ${formatHelpExamples([
clawdbot agents set-identity --workspace ~/clawd --from-identity ['clawdbot agents set-identity --agent main --name "Clawd" --emoji "🦞"', "Set name + emoji."],
clawdbot agents set-identity --identity-file ~/clawd/IDENTITY.md --agent main ["clawdbot agents set-identity --workspace ~/clawd --from-identity", "Load from IDENTITY.md."],
[
"clawdbot agents set-identity --identity-file ~/clawd/IDENTITY.md --agent main",
"Use a specific IDENTITY.md.",
],
])}
`, `,
) )
.action(async (opts) => { .action(async (opts) => {
+17 -5
View File
@@ -1,6 +1,7 @@
import type { Command } from "commander"; import type { Command } from "commander";
import { formatDocsLink } from "../../terminal/links.js"; import { formatDocsLink } from "../../terminal/links.js";
import { theme } from "../../terminal/theme.js"; import { theme } from "../../terminal/theme.js";
import { formatHelpExamples } from "../help-format.js";
import type { ProgramContext } from "./context.js"; import type { ProgramContext } from "./context.js";
import { createMessageCliHelpers } from "./message/helpers.js"; import { createMessageCliHelpers } from "./message/helpers.js";
import { registerMessageDiscordAdminCommands } from "./message/register.discord-admin.js"; import { registerMessageDiscordAdminCommands } from "./message/register.discord-admin.js";
@@ -28,11 +29,22 @@ export function registerMessageCommands(program: Command, ctx: ProgramContext) {
"after", "after",
() => () =>
` `
Examples: ${theme.heading("Examples:")}
clawdbot message send --target +15555550123 --message "Hi" ${formatHelpExamples([
clawdbot message send --target +15555550123 --message "Hi" --media photo.jpg ['clawdbot message send --target +15555550123 --message "Hi"', "Send a text message."],
clawdbot message poll --channel discord --target channel:123 --poll-question "Snack?" --poll-option Pizza --poll-option Sushi [
clawdbot message react --channel discord --target 123 --message-id 456 --emoji "✅" 'clawdbot message send --target +15555550123 --message "Hi" --media photo.jpg',
"Send a message with media.",
],
[
'clawdbot message poll --channel discord --target channel:123 --poll-question "Snack?" --poll-option Pizza --poll-option Sushi',
"Create a Discord poll.",
],
[
'clawdbot message react --channel discord --target 123 --message-id 456 --emoji "✅"',
"React to a message.",
],
])}
${theme.muted("Docs:")} ${formatDocsLink("/cli/message", "docs.clawd.bot/cli/message")}`, ${theme.muted("Docs:")} ${formatDocsLink("/cli/message", "docs.clawd.bot/cli/message")}`,
) )
@@ -7,6 +7,7 @@ import { defaultRuntime } from "../../runtime.js";
import { formatDocsLink } from "../../terminal/links.js"; import { formatDocsLink } from "../../terminal/links.js";
import { theme } from "../../terminal/theme.js"; import { theme } from "../../terminal/theme.js";
import { runCommandWithRuntime } from "../cli-utils.js"; import { runCommandWithRuntime } from "../cli-utils.js";
import { formatHelpExamples } from "../help-format.js";
import { parsePositiveIntOrUndefined } from "./helpers.js"; import { parsePositiveIntOrUndefined } from "./helpers.js";
function resolveVerbose(opts: { verbose?: boolean; debug?: boolean }): boolean { function resolveVerbose(opts: { verbose?: boolean; debug?: boolean }): boolean {
@@ -36,15 +37,18 @@ export function registerStatusHealthSessionsCommands(program: Command) {
.option("--debug", "Alias for --verbose", false) .option("--debug", "Alias for --verbose", false)
.addHelpText( .addHelpText(
"after", "after",
` () =>
Examples: `\n${theme.heading("Examples:")}\n${formatHelpExamples([
clawdbot status # show linked account + session store summary ["clawdbot status", "Show channel health + session summary."],
clawdbot status --all # full diagnosis (read-only) ["clawdbot status --all", "Full diagnosis (read-only)."],
clawdbot status --json # machine-readable output ["clawdbot status --json", "Machine-readable output."],
clawdbot status --usage # show model provider usage/quota snapshots ["clawdbot status --usage", "Show model provider usage/quota snapshots."],
clawdbot status --deep # run channel probes (WA + Telegram + Discord + Slack + Signal) [
clawdbot status --deep --timeout 5000 # tighten probe timeout "clawdbot status --deep",
clawdbot channels status # gateway channel runtime + probes`, "Run channel probes (WA + Telegram + Discord + Slack + Signal).",
],
["clawdbot status --deep --timeout 5000", "Tighten probe timeout."],
])}`,
) )
.addHelpText( .addHelpText(
"after", "after",
@@ -113,14 +117,15 @@ Examples:
.option("--active <minutes>", "Only show sessions updated within the past N minutes") .option("--active <minutes>", "Only show sessions updated within the past N minutes")
.addHelpText( .addHelpText(
"after", "after",
` () =>
Examples: `\n${theme.heading("Examples:")}\n${formatHelpExamples([
clawdbot sessions # list all sessions ["clawdbot sessions", "List all sessions."],
clawdbot sessions --active 120 # only last 2 hours ["clawdbot sessions --active 120", "Only last 2 hours."],
clawdbot sessions --json # machine-readable output ["clawdbot sessions --json", "Machine-readable output."],
clawdbot sessions --store ./tmp/sessions.json ["clawdbot sessions --store ./tmp/sessions.json", "Use a specific session store."],
])}\n\n${theme.muted(
Shows token usage per session when the agent reports it; set agents.defaults.contextTokens to see % of your model window.`, "Shows token usage per session when the agent reports it; set agents.defaults.contextTokens to see % of your model window.",
)}`,
) )
.addHelpText( .addHelpText(
"after", "after",
+17 -1
View File
@@ -21,7 +21,7 @@ const { nodesAction, registerNodesCli } = vi.hoisted(() => {
vi.mock("../acp-cli.js", () => ({ registerAcpCli })); vi.mock("../acp-cli.js", () => ({ registerAcpCli }));
vi.mock("../nodes-cli.js", () => ({ registerNodesCli })); vi.mock("../nodes-cli.js", () => ({ registerNodesCli }));
const { registerSubCliCommands } = await import("./register.subclis.js"); const { registerSubCliByName, registerSubCliCommands } = await import("./register.subclis.js");
describe("registerSubCliCommands", () => { describe("registerSubCliCommands", () => {
const originalArgv = process.argv; const originalArgv = process.argv;
@@ -78,4 +78,20 @@ describe("registerSubCliCommands", () => {
expect(registerNodesCli).toHaveBeenCalledTimes(1); expect(registerNodesCli).toHaveBeenCalledTimes(1);
expect(nodesAction).toHaveBeenCalledTimes(1); expect(nodesAction).toHaveBeenCalledTimes(1);
}); });
it("replaces placeholder when registering a subcommand by name", async () => {
process.argv = ["node", "clawdbot", "acp", "--help"];
const program = new Command();
program.name("clawdbot");
registerSubCliCommands(program, process.argv);
await registerSubCliByName(program, "acp");
const names = program.commands.map((cmd) => cmd.name());
expect(names.filter((name) => name === "acp")).toHaveLength(1);
await program.parseAsync(["node", "clawdbot", "acp"], { from: "user" });
expect(registerAcpCli).toHaveBeenCalledTimes(1);
expect(acpAction).toHaveBeenCalledTimes(1);
});
}); });
+11 -4
View File
@@ -18,10 +18,8 @@ const shouldRegisterPrimaryOnly = (argv: string[]) => {
return true; return true;
}; };
const shouldEagerRegisterSubcommands = (argv: string[]) => { const shouldEagerRegisterSubcommands = (_argv: string[]) => {
if (isTruthyEnvValue(process.env.CLAWDBOT_DISABLE_LAZY_SUBCOMMANDS)) return true; return isTruthyEnvValue(process.env.CLAWDBOT_DISABLE_LAZY_SUBCOMMANDS);
if (hasHelpOrVersion(argv)) return true;
return false;
}; };
const loadConfig = async (): Promise<ClawdbotConfig> => { const loadConfig = async (): Promise<ClawdbotConfig> => {
@@ -234,6 +232,15 @@ function removeCommand(program: Command, command: Command) {
} }
} }
export async function registerSubCliByName(program: Command, name: string): Promise<boolean> {
const entry = entries.find((candidate) => candidate.name === name);
if (!entry) return false;
const existing = program.commands.find((cmd) => cmd.name() === entry.name);
if (existing) removeCommand(program, existing);
await entry.register(program);
return true;
}
function registerLazyCommand(program: Command, entry: SubCliEntry) { function registerLazyCommand(program: Command, entry: SubCliEntry) {
const placeholder = program.command(entry.name).description(entry.description); const placeholder = program.command(entry.name).description(entry.description);
placeholder.allowUnknownOption(true); placeholder.allowUnknownOption(true);
+10 -1
View File
@@ -10,6 +10,7 @@ import { assertSupportedRuntime } from "../infra/runtime-guard.js";
import { formatUncaughtError } from "../infra/errors.js"; import { formatUncaughtError } from "../infra/errors.js";
import { installUnhandledRejectionHandler } from "../infra/unhandled-rejections.js"; import { installUnhandledRejectionHandler } from "../infra/unhandled-rejections.js";
import { enableConsoleCapture } from "../logging.js"; import { enableConsoleCapture } from "../logging.js";
import { getPrimaryCommand, hasHelpOrVersion } from "./argv.js";
import { tryRouteCli } from "./route.js"; import { tryRouteCli } from "./route.js";
export function rewriteUpdateFlagArgv(argv: string[]): string[] { export function rewriteUpdateFlagArgv(argv: string[]): string[] {
@@ -47,7 +48,15 @@ export async function runCli(argv: string[] = process.argv) {
process.exit(1); process.exit(1);
}); });
await program.parseAsync(rewriteUpdateFlagArgv(normalizedArgv)); const parseArgv = rewriteUpdateFlagArgv(normalizedArgv);
if (hasHelpOrVersion(parseArgv)) {
const primary = getPrimaryCommand(parseArgv);
if (primary) {
const { registerSubCliByName } = await import("./program/register.subclis.js");
await registerSubCliByName(program, primary);
}
}
await program.parseAsync(parseArgv);
} }
function stripWindowsNodeExec(argv: string[]): string[] { function stripWindowsNodeExec(argv: string[]): string[] {
+67 -56
View File
@@ -5,6 +5,7 @@ import { sandboxExplainCommand } from "../commands/sandbox-explain.js";
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { formatHelpExamples } from "./help-format.js";
// --- Types --- // --- Types ---
@@ -12,58 +13,34 @@ type CommandOptions = Record<string, unknown>;
// --- Helpers --- // --- Helpers ---
const EXAMPLES = { const SANDBOX_EXAMPLES = {
main: ` main: [
Examples: ["clawdbot sandbox list", "List all sandbox containers."],
clawdbot sandbox list # List all sandbox containers ["clawdbot sandbox list --browser", "List only browser containers."],
clawdbot sandbox list --browser # List only browser containers ["clawdbot sandbox recreate --all", "Recreate all containers."],
clawdbot sandbox recreate --all # Recreate all containers ["clawdbot sandbox recreate --session main", "Recreate a specific session."],
clawdbot sandbox recreate --session main # Recreate specific session ["clawdbot sandbox recreate --agent mybot", "Recreate agent containers."],
clawdbot sandbox recreate --agent mybot # Recreate agent containers ["clawdbot sandbox explain", "Explain effective sandbox config."],
clawdbot sandbox explain # Explain effective sandbox config`, ],
list: [
list: ` ["clawdbot sandbox list", "List all sandbox containers."],
Examples: ["clawdbot sandbox list --browser", "List only browser containers."],
clawdbot sandbox list # List all sandbox containers ["clawdbot sandbox list --json", "JSON output."],
clawdbot sandbox list --browser # List only browser containers ],
clawdbot sandbox list --json # JSON output recreate: [
["clawdbot sandbox recreate --all", "Recreate all containers."],
Output includes: ["clawdbot sandbox recreate --session main", "Recreate a specific session."],
• Container name and status (running/stopped) ["clawdbot sandbox recreate --agent mybot", "Recreate a specific agent (includes sub-agents)."],
• Docker image and whether it matches current config ["clawdbot sandbox recreate --browser --all", "Recreate only browser containers."],
• Age (time since creation) ["clawdbot sandbox recreate --all --force", "Skip confirmation."],
• Idle time (time since last use) ],
• Associated session/agent ID`, explain: [
["clawdbot sandbox explain", "Show effective sandbox config."],
recreate: ` ["clawdbot sandbox explain --session agent:main:main", "Explain a specific session."],
Examples: ["clawdbot sandbox explain --agent work", "Explain an agent sandbox."],
clawdbot sandbox recreate --all # Recreate all containers ["clawdbot sandbox explain --json", "JSON output."],
clawdbot sandbox recreate --session main # Specific session ],
clawdbot sandbox recreate --agent mybot # Specific agent (includes sub-agents) } as const;
clawdbot sandbox recreate --browser --all # All browser containers only
clawdbot sandbox recreate --all --force # Skip confirmation
Why use this?
After updating Docker images or sandbox configuration, existing containers
continue running with old settings. This command removes them so they'll be
recreated automatically with current config when next needed.
Filter options:
--all Remove all sandbox containers
--session Remove container for specific session key
--agent Remove containers for agent (includes agent:id:* variants)
Modifiers:
--browser Only affect browser containers (not regular sandbox)
--force Skip confirmation prompt`,
explain: `
Examples:
clawdbot sandbox explain
clawdbot sandbox explain --session agent:main:main
clawdbot sandbox explain --agent work
clawdbot sandbox explain --json`,
};
function createRunner( function createRunner(
commandFn: (opts: CommandOptions, runtime: typeof defaultRuntime) => Promise<void>, commandFn: (opts: CommandOptions, runtime: typeof defaultRuntime) => Promise<void>,
@@ -84,7 +61,10 @@ export function registerSandboxCli(program: Command) {
const sandbox = program const sandbox = program
.command("sandbox") .command("sandbox")
.description("Manage sandbox containers (Docker-based agent isolation)") .description("Manage sandbox containers (Docker-based agent isolation)")
.addHelpText("after", EXAMPLES.main) .addHelpText(
"after",
() => `\n${theme.heading("Examples:")}\n${formatHelpExamples(SANDBOX_EXAMPLES.main)}\n`,
)
.addHelpText( .addHelpText(
"after", "after",
() => () =>
@@ -101,7 +81,17 @@ export function registerSandboxCli(program: Command) {
.description("List sandbox containers and their status") .description("List sandbox containers and their status")
.option("--json", "Output result as JSON", false) .option("--json", "Output result as JSON", false)
.option("--browser", "List browser containers only", false) .option("--browser", "List browser containers only", false)
.addHelpText("after", EXAMPLES.list) .addHelpText(
"after",
() =>
`\n${theme.heading("Examples:")}\n${formatHelpExamples(SANDBOX_EXAMPLES.list)}\n\n${theme.heading(
"Output includes:",
)}\n${theme.muted("- Container name and status (running/stopped)")}\n${theme.muted(
"- Docker image and whether it matches current config",
)}\n${theme.muted("- Age (time since creation)")}\n${theme.muted(
"- Idle time (time since last use)",
)}\n${theme.muted("- Associated session/agent ID")}`,
)
.action( .action(
createRunner((opts) => createRunner((opts) =>
sandboxListCommand( sandboxListCommand(
@@ -124,7 +114,25 @@ export function registerSandboxCli(program: Command) {
.option("--agent <id>", "Recreate containers for specific agent") .option("--agent <id>", "Recreate containers for specific agent")
.option("--browser", "Only recreate browser containers", false) .option("--browser", "Only recreate browser containers", false)
.option("--force", "Skip confirmation prompt", false) .option("--force", "Skip confirmation prompt", false)
.addHelpText("after", EXAMPLES.recreate) .addHelpText(
"after",
() =>
`\n${theme.heading("Examples:")}\n${formatHelpExamples(SANDBOX_EXAMPLES.recreate)}\n\n${theme.heading(
"Why use this?",
)}\n${theme.muted(
"After updating Docker images or sandbox configuration, existing containers continue running with old settings.",
)}\n${theme.muted(
"This command removes them so they'll be recreated automatically with current config when next needed.",
)}\n\n${theme.heading("Filter options:")}\n${theme.muted(
" --all Remove all sandbox containers",
)}\n${theme.muted(
" --session Remove container for specific session key",
)}\n${theme.muted(
" --agent Remove containers for agent (includes agent:id:* variants)",
)}\n\n${theme.heading("Modifiers:")}\n${theme.muted(
" --browser Only affect browser containers (not regular sandbox)",
)}\n${theme.muted(" --force Skip confirmation prompt")}`,
)
.action( .action(
createRunner((opts) => createRunner((opts) =>
sandboxRecreateCommand( sandboxRecreateCommand(
@@ -148,7 +156,10 @@ export function registerSandboxCli(program: Command) {
.option("--session <key>", "Session key to inspect (defaults to agent main)") .option("--session <key>", "Session key to inspect (defaults to agent main)")
.option("--agent <id>", "Agent id to inspect (defaults to derived agent)") .option("--agent <id>", "Agent id to inspect (defaults to derived agent)")
.option("--json", "Output result as JSON", false) .option("--json", "Output result as JSON", false)
.addHelpText("after", EXAMPLES.explain) .addHelpText(
"after",
() => `\n${theme.heading("Examples:")}\n${formatHelpExamples(SANDBOX_EXAMPLES.explain)}\n`,
)
.action( .action(
createRunner((opts) => createRunner((opts) =>
sandboxExplainCommand( sandboxExplainCommand(
+1 -2
View File
@@ -1,4 +1,3 @@
import chalk from "chalk";
import type { Command } from "commander"; import type { Command } from "commander";
import { loadConfig } from "../config/config.js"; import { loadConfig } from "../config/config.js";
@@ -121,7 +120,7 @@ export function registerSecurityCli(program: Command) {
lines.push(""); lines.push("");
lines.push(heading(label)); lines.push(heading(label));
for (const f of list) { for (const f of list) {
lines.push(`${chalk.gray(f.checkId)} ${f.title}`); lines.push(`${theme.muted(f.checkId)} ${f.title}`);
lines.push(` ${f.detail}`); lines.push(` ${f.detail}`);
if (f.remediation?.trim()) lines.push(` ${muted(`Fix: ${f.remediation.trim()}`)}`); if (f.remediation?.trim()) lines.push(` ${muted(`Fix: ${f.remediation.trim()}`)}`);
} }
+25 -1
View File
@@ -1,6 +1,7 @@
import type { Command } from "commander"; import type { Command } from "commander";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { formatHelpExampleGroup } from "./help-format.js";
import { createDefaultDeps } from "./deps.js"; import { createDefaultDeps } from "./deps.js";
import { import {
runDaemonInstall, runDaemonInstall,
@@ -20,13 +21,36 @@ import {
} from "./node-cli/daemon.js"; } from "./node-cli/daemon.js";
export function registerServiceCli(program: Command) { export function registerServiceCli(program: Command) {
const gatewayExamples: Array<[string, string]> = [
["clawdbot service gateway status", "Show gateway service status + probe."],
[
"clawdbot service gateway install --port 18789 --token <token>",
"Install the Gateway service on port 18789.",
],
["clawdbot service gateway restart", "Restart the Gateway service."],
];
const nodeExamples: Array<[string, string]> = [
["clawdbot service node status", "Show node host service status."],
[
"clawdbot service node install --host gateway.local --port 18789 --tls",
"Install the node host service with TLS.",
],
["clawdbot service node restart", "Restart the node host service."],
];
const service = program const service = program
.command("service") .command("service")
.description("Manage Gateway and node host services (launchd/systemd/schtasks)") .description("Manage Gateway and node host services (launchd/systemd/schtasks)")
.addHelpText( .addHelpText(
"after", "after",
() => () =>
`\n${theme.muted("Docs:")} ${formatDocsLink("/cli/service", "docs.clawd.bot/cli/service")}\n`, `\n${theme.heading("Examples:")}\n${formatHelpExampleGroup(
"Gateway:",
gatewayExamples,
)}\n\n${formatHelpExampleGroup("Node:", nodeExamples)}\n\n${theme.muted(
"Docs:",
)} ${formatDocsLink("/cli/service", "docs.clawd.bot/cli/service")}\n`,
); );
const gateway = service.command("gateway").description("Manage the Gateway service"); const gateway = service.command("gateway").description("Manage the Gateway service");
+94 -94
View File
@@ -1,4 +1,3 @@
import chalk from "chalk";
import type { Command } from "commander"; import type { Command } from "commander";
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../agents/agent-scope.js"; import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../agents/agent-scope.js";
import { import {
@@ -9,6 +8,7 @@ import {
import { loadConfig } from "../config/config.js"; import { loadConfig } from "../config/config.js";
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { renderTable } from "../terminal/table.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { formatCliCommand } from "./command-format.js"; import { formatCliCommand } from "./command-format.js";
@@ -31,47 +31,36 @@ function appendClawdHubHint(output: string, json?: boolean): string {
return `${output}\n\nTip: use \`npx clawdhub\` to search, install, and sync skills.`; return `${output}\n\nTip: use \`npx clawdhub\` to search, install, and sync skills.`;
} }
/** function formatSkillStatus(skill: SkillStatusEntry): string {
* Format a single skill for display in the list if (skill.eligible) return theme.success("✓ ready");
*/ if (skill.disabled) return theme.warn("⏸ disabled");
function formatSkillLine(skill: SkillStatusEntry, verbose = false): string { if (skill.blockedByAllowlist) return theme.warn("🚫 blocked");
return theme.error("✗ missing");
}
function formatSkillName(skill: SkillStatusEntry): string {
const emoji = skill.emoji ?? "📦"; const emoji = skill.emoji ?? "📦";
const status = skill.eligible return `${emoji} ${theme.command(skill.name)}`;
? chalk.green("✓") }
: skill.disabled
? chalk.yellow("disabled")
: skill.blockedByAllowlist
? chalk.yellow("blocked")
: chalk.red("missing reqs");
const name = skill.eligible ? chalk.white(skill.name) : chalk.gray(skill.name); function formatSkillMissingSummary(skill: SkillStatusEntry): string {
const missing: string[] = [];
const desc = chalk.gray( if (skill.missing.bins.length > 0) {
skill.description.length > 50 ? `${skill.description.slice(0, 47)}...` : skill.description, missing.push(`bins: ${skill.missing.bins.join(", ")}`);
);
if (verbose) {
const missing: string[] = [];
if (skill.missing.bins.length > 0) {
missing.push(`bins: ${skill.missing.bins.join(", ")}`);
}
if (skill.missing.anyBins.length > 0) {
missing.push(`anyBins: ${skill.missing.anyBins.join(", ")}`);
}
if (skill.missing.env.length > 0) {
missing.push(`env: ${skill.missing.env.join(", ")}`);
}
if (skill.missing.config.length > 0) {
missing.push(`config: ${skill.missing.config.join(", ")}`);
}
if (skill.missing.os.length > 0) {
missing.push(`os: ${skill.missing.os.join(", ")}`);
}
const missingStr = missing.length > 0 ? chalk.red(` [${missing.join("; ")}]`) : "";
return `${emoji} ${name} ${status}${missingStr}\n ${desc}`;
} }
if (skill.missing.anyBins.length > 0) {
return `${emoji} ${name} ${status} - ${desc}`; missing.push(`anyBins: ${skill.missing.anyBins.join(", ")}`);
}
if (skill.missing.env.length > 0) {
missing.push(`env: ${skill.missing.env.join(", ")}`);
}
if (skill.missing.config.length > 0) {
missing.push(`config: ${skill.missing.config.join(", ")}`);
}
if (skill.missing.os.length > 0) {
missing.push(`os: ${skill.missing.os.join(", ")}`);
}
return missing.join("; ");
} }
/** /**
@@ -108,28 +97,39 @@ export function formatSkillsList(report: SkillStatusReport, opts: SkillsListOpti
} }
const eligible = skills.filter((s) => s.eligible); const eligible = skills.filter((s) => s.eligible);
const notEligible = skills.filter((s) => !s.eligible); const tableWidth = Math.max(60, (process.stdout.columns ?? 120) - 1);
const rows = skills.map((skill) => {
const missing = formatSkillMissingSummary(skill);
return {
Status: formatSkillStatus(skill),
Skill: formatSkillName(skill),
Description: theme.muted(skill.description),
Source: skill.source ?? "",
Missing: missing ? theme.warn(missing) : "",
};
});
const columns = [
{ key: "Status", header: "Status", minWidth: 10 },
{ key: "Skill", header: "Skill", minWidth: 18, flex: true },
{ key: "Description", header: "Description", minWidth: 24, flex: true },
{ key: "Source", header: "Source", minWidth: 10 },
];
if (opts.verbose) {
columns.push({ key: "Missing", header: "Missing", minWidth: 18, flex: true });
}
const lines: string[] = []; const lines: string[] = [];
lines.push( lines.push(
chalk.bold.cyan("Skills") + chalk.gray(` (${eligible.length}/${skills.length} ready)`), `${theme.heading("Skills")} ${theme.muted(`(${eligible.length}/${skills.length} ready)`)}`,
);
lines.push(
renderTable({
width: tableWidth,
columns,
rows,
}).trimEnd(),
); );
lines.push("");
if (eligible.length > 0) {
lines.push(chalk.bold.green("Ready:"));
for (const skill of eligible) {
lines.push(` ${formatSkillLine(skill, opts.verbose)}`);
}
}
if (notEligible.length > 0 && !opts.eligible) {
if (eligible.length > 0) lines.push("");
lines.push(chalk.bold.yellow("Not ready:"));
for (const skill of notEligible) {
lines.push(` ${formatSkillLine(skill, opts.verbose)}`);
}
}
return appendClawdHubHint(lines.join("\n"), opts.json); return appendClawdHubHint(lines.join("\n"), opts.json);
} }
@@ -161,27 +161,27 @@ export function formatSkillInfo(
const lines: string[] = []; const lines: string[] = [];
const emoji = skill.emoji ?? "📦"; const emoji = skill.emoji ?? "📦";
const status = skill.eligible const status = skill.eligible
? chalk.green("✓ Ready") ? theme.success("✓ Ready")
: skill.disabled : skill.disabled
? chalk.yellow("⏸ Disabled") ? theme.warn("⏸ Disabled")
: skill.blockedByAllowlist : skill.blockedByAllowlist
? chalk.yellow("🚫 Blocked by allowlist") ? theme.warn("🚫 Blocked by allowlist")
: chalk.red("✗ Missing requirements"); : theme.error("✗ Missing requirements");
lines.push(`${emoji} ${chalk.bold.cyan(skill.name)} ${status}`); lines.push(`${emoji} ${theme.heading(skill.name)} ${status}`);
lines.push(""); lines.push("");
lines.push(chalk.white(skill.description)); lines.push(skill.description);
lines.push(""); lines.push("");
// Details // Details
lines.push(chalk.bold("Details:")); lines.push(theme.heading("Details:"));
lines.push(` Source: ${skill.source}`); lines.push(`${theme.muted(" Source:")} ${skill.source}`);
lines.push(` Path: ${chalk.gray(skill.filePath)}`); lines.push(`${theme.muted(" Path:")} ${skill.filePath}`);
if (skill.homepage) { if (skill.homepage) {
lines.push(` Homepage: ${chalk.blue(skill.homepage)}`); lines.push(`${theme.muted(" Homepage:")} ${skill.homepage}`);
} }
if (skill.primaryEnv) { if (skill.primaryEnv) {
lines.push(` Primary env: ${skill.primaryEnv}`); lines.push(`${theme.muted(" Primary env:")} ${skill.primaryEnv}`);
} }
// Requirements // Requirements
@@ -194,51 +194,51 @@ export function formatSkillInfo(
if (hasRequirements) { if (hasRequirements) {
lines.push(""); lines.push("");
lines.push(chalk.bold("Requirements:")); lines.push(theme.heading("Requirements:"));
if (skill.requirements.bins.length > 0) { if (skill.requirements.bins.length > 0) {
const binsStatus = skill.requirements.bins.map((bin) => { const binsStatus = skill.requirements.bins.map((bin) => {
const missing = skill.missing.bins.includes(bin); const missing = skill.missing.bins.includes(bin);
return missing ? chalk.red(`✗ ${bin}`) : chalk.green(`✓ ${bin}`); return missing ? theme.error(`✗ ${bin}`) : theme.success(`✓ ${bin}`);
}); });
lines.push(` Binaries: ${binsStatus.join(", ")}`); lines.push(`${theme.muted(" Binaries:")} ${binsStatus.join(", ")}`);
} }
if (skill.requirements.anyBins.length > 0) { if (skill.requirements.anyBins.length > 0) {
const anyBinsMissing = skill.missing.anyBins.length > 0; const anyBinsMissing = skill.missing.anyBins.length > 0;
const anyBinsStatus = skill.requirements.anyBins.map((bin) => { const anyBinsStatus = skill.requirements.anyBins.map((bin) => {
const missing = anyBinsMissing; const missing = anyBinsMissing;
return missing ? chalk.red(`✗ ${bin}`) : chalk.green(`✓ ${bin}`); return missing ? theme.error(`✗ ${bin}`) : theme.success(`✓ ${bin}`);
}); });
lines.push(` Any binaries: ${anyBinsStatus.join(", ")}`); lines.push(`${theme.muted(" Any binaries:")} ${anyBinsStatus.join(", ")}`);
} }
if (skill.requirements.env.length > 0) { if (skill.requirements.env.length > 0) {
const envStatus = skill.requirements.env.map((env) => { const envStatus = skill.requirements.env.map((env) => {
const missing = skill.missing.env.includes(env); const missing = skill.missing.env.includes(env);
return missing ? chalk.red(`✗ ${env}`) : chalk.green(`✓ ${env}`); return missing ? theme.error(`✗ ${env}`) : theme.success(`✓ ${env}`);
}); });
lines.push(` Environment: ${envStatus.join(", ")}`); lines.push(`${theme.muted(" Environment:")} ${envStatus.join(", ")}`);
} }
if (skill.requirements.config.length > 0) { if (skill.requirements.config.length > 0) {
const configStatus = skill.requirements.config.map((cfg) => { const configStatus = skill.requirements.config.map((cfg) => {
const missing = skill.missing.config.includes(cfg); const missing = skill.missing.config.includes(cfg);
return missing ? chalk.red(`✗ ${cfg}`) : chalk.green(`✓ ${cfg}`); return missing ? theme.error(`✗ ${cfg}`) : theme.success(`✓ ${cfg}`);
}); });
lines.push(` Config: ${configStatus.join(", ")}`); lines.push(`${theme.muted(" Config:")} ${configStatus.join(", ")}`);
} }
if (skill.requirements.os.length > 0) { if (skill.requirements.os.length > 0) {
const osStatus = skill.requirements.os.map((osName) => { const osStatus = skill.requirements.os.map((osName) => {
const missing = skill.missing.os.includes(osName); const missing = skill.missing.os.includes(osName);
return missing ? chalk.red(`✗ ${osName}`) : chalk.green(`✓ ${osName}`); return missing ? theme.error(`✗ ${osName}`) : theme.success(`✓ ${osName}`);
}); });
lines.push(` OS: ${osStatus.join(", ")}`); lines.push(`${theme.muted(" OS:")} ${osStatus.join(", ")}`);
} }
} }
// Install options // Install options
if (skill.install.length > 0 && !skill.eligible) { if (skill.install.length > 0 && !skill.eligible) {
lines.push(""); lines.push("");
lines.push(chalk.bold("Install options:")); lines.push(theme.heading("Install options:"));
for (const inst of skill.install) { for (const inst of skill.install) {
lines.push(` ${chalk.yellow("→")} ${inst.label}`); lines.push(` ${theme.warn("→")} ${inst.label}`);
} }
} }
@@ -281,17 +281,17 @@ export function formatSkillsCheck(report: SkillStatusReport, opts: SkillsCheckOp
} }
const lines: string[] = []; const lines: string[] = [];
lines.push(chalk.bold.cyan("Skills Status Check")); lines.push(theme.heading("Skills Status Check"));
lines.push(""); lines.push("");
lines.push(`Total: ${report.skills.length}`); lines.push(`${theme.muted("Total:")} ${report.skills.length}`);
lines.push(`${chalk.green("✓")} Eligible: ${eligible.length}`); lines.push(`${theme.success("✓")} ${theme.muted("Eligible:")} ${eligible.length}`);
lines.push(`${chalk.yellow("⏸")} Disabled: ${disabled.length}`); lines.push(`${theme.warn("⏸")} ${theme.muted("Disabled:")} ${disabled.length}`);
lines.push(`${chalk.yellow("🚫")} Blocked by allowlist: ${blocked.length}`); lines.push(`${theme.warn("🚫")} ${theme.muted("Blocked by allowlist:")} ${blocked.length}`);
lines.push(`${chalk.red("✗")} Missing requirements: ${missingReqs.length}`); lines.push(`${theme.error("✗")} ${theme.muted("Missing requirements:")} ${missingReqs.length}`);
if (eligible.length > 0) { if (eligible.length > 0) {
lines.push(""); lines.push("");
lines.push(chalk.bold.green("Ready to use:")); lines.push(theme.heading("Ready to use:"));
for (const skill of eligible) { for (const skill of eligible) {
const emoji = skill.emoji ?? "📦"; const emoji = skill.emoji ?? "📦";
lines.push(` ${emoji} ${skill.name}`); lines.push(` ${emoji} ${skill.name}`);
@@ -300,7 +300,7 @@ export function formatSkillsCheck(report: SkillStatusReport, opts: SkillsCheckOp
if (missingReqs.length > 0) { if (missingReqs.length > 0) {
lines.push(""); lines.push("");
lines.push(chalk.bold.red("Missing requirements:")); lines.push(theme.heading("Missing requirements:"));
for (const skill of missingReqs) { for (const skill of missingReqs) {
const emoji = skill.emoji ?? "📦"; const emoji = skill.emoji ?? "📦";
const missing: string[] = []; const missing: string[] = [];
@@ -319,7 +319,7 @@ export function formatSkillsCheck(report: SkillStatusReport, opts: SkillsCheckOp
if (skill.missing.os.length > 0) { if (skill.missing.os.length > 0) {
missing.push(`os: ${skill.missing.os.join(", ")}`); missing.push(`os: ${skill.missing.os.join(", ")}`);
} }
lines.push(` ${emoji} ${skill.name} ${chalk.gray(`(${missing.join("; ")})`)}`); lines.push(` ${emoji} ${skill.name} ${theme.muted(`(${missing.join("; ")})`)}`);
} }
} }
@@ -350,7 +350,7 @@ export function registerSkillsCli(program: Command) {
const config = loadConfig(); const config = loadConfig();
const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config)); const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config));
const report = buildWorkspaceSkillStatus(workspaceDir, { config }); const report = buildWorkspaceSkillStatus(workspaceDir, { config });
console.log(formatSkillsList(report, opts)); defaultRuntime.log(formatSkillsList(report, opts));
} catch (err) { } catch (err) {
defaultRuntime.error(String(err)); defaultRuntime.error(String(err));
defaultRuntime.exit(1); defaultRuntime.exit(1);
@@ -367,7 +367,7 @@ export function registerSkillsCli(program: Command) {
const config = loadConfig(); const config = loadConfig();
const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config)); const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config));
const report = buildWorkspaceSkillStatus(workspaceDir, { config }); const report = buildWorkspaceSkillStatus(workspaceDir, { config });
console.log(formatSkillInfo(report, name, opts)); defaultRuntime.log(formatSkillInfo(report, name, opts));
} catch (err) { } catch (err) {
defaultRuntime.error(String(err)); defaultRuntime.error(String(err));
defaultRuntime.exit(1); defaultRuntime.exit(1);
@@ -383,7 +383,7 @@ export function registerSkillsCli(program: Command) {
const config = loadConfig(); const config = loadConfig();
const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config)); const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config));
const report = buildWorkspaceSkillStatus(workspaceDir, { config }); const report = buildWorkspaceSkillStatus(workspaceDir, { config });
console.log(formatSkillsCheck(report, opts)); defaultRuntime.log(formatSkillsCheck(report, opts));
} catch (err) { } catch (err) {
defaultRuntime.error(String(err)); defaultRuntime.error(String(err));
defaultRuntime.exit(1); defaultRuntime.exit(1);
@@ -396,7 +396,7 @@ export function registerSkillsCli(program: Command) {
const config = loadConfig(); const config = loadConfig();
const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config)); const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config));
const report = buildWorkspaceSkillStatus(workspaceDir, { config }); const report = buildWorkspaceSkillStatus(workspaceDir, { config });
console.log(formatSkillsList(report, {})); defaultRuntime.log(formatSkillsList(report, {}));
} catch (err) { } catch (err) {
defaultRuntime.error(String(err)); defaultRuntime.error(String(err));
defaultRuntime.exit(1); defaultRuntime.exit(1);
+42
View File
@@ -447,6 +447,7 @@ describe("update-cli", () => {
it("requires confirmation on downgrade when non-interactive", async () => { it("requires confirmation on downgrade when non-interactive", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-update-")); const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-update-"));
try { try {
setTty(false);
await fs.writeFile( await fs.writeFile(
path.join(tempDir, "package.json"), path.join(tempDir, "package.json"),
JSON.stringify({ name: "clawdbot", version: "2.0.0" }), JSON.stringify({ name: "clawdbot", version: "2.0.0" }),
@@ -483,4 +484,45 @@ describe("update-cli", () => {
await fs.rm(tempDir, { recursive: true, force: true }); await fs.rm(tempDir, { recursive: true, force: true });
} }
}); });
it("allows downgrade with --yes in non-interactive mode", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-update-"));
try {
setTty(false);
await fs.writeFile(
path.join(tempDir, "package.json"),
JSON.stringify({ name: "clawdbot", version: "2.0.0" }),
"utf-8",
);
const { resolveClawdbotPackageRoot } = await import("../infra/clawdbot-root.js");
const { resolveNpmChannelTag } = await import("../infra/update-check.js");
const { runGatewayUpdate } = await import("../infra/update-runner.js");
const { defaultRuntime } = await import("../runtime.js");
const { updateCommand } = await import("./update-cli.js");
vi.mocked(resolveClawdbotPackageRoot).mockResolvedValue(tempDir);
vi.mocked(resolveNpmChannelTag).mockResolvedValue({
tag: "latest",
version: "0.0.1",
});
vi.mocked(runGatewayUpdate).mockResolvedValue({
status: "ok",
mode: "npm",
steps: [],
durationMs: 100,
});
vi.mocked(defaultRuntime.error).mockClear();
vi.mocked(defaultRuntime.exit).mockClear();
await updateCommand({ yes: true });
expect(defaultRuntime.error).not.toHaveBeenCalledWith(
expect.stringContaining("Downgrade confirmation required."),
);
expect(runGatewayUpdate).toHaveBeenCalled();
} finally {
await fs.rm(tempDir, { recursive: true, force: true });
}
});
}); });
+50 -28
View File
@@ -32,6 +32,7 @@ import { formatCliCommand } from "./command-format.js";
import { stylePromptMessage } from "../terminal/prompt-style.js"; import { stylePromptMessage } from "../terminal/prompt-style.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { renderTable } from "../terminal/table.js"; import { renderTable } from "../terminal/table.js";
import { formatHelpExamples } from "./help-format.js";
import { import {
formatUpdateAvailableHint, formatUpdateAvailableHint,
formatUpdateOneLiner, formatUpdateOneLiner,
@@ -45,6 +46,7 @@ export type UpdateCommandOptions = {
channel?: string; channel?: string;
tag?: string; tag?: string;
timeout?: string; timeout?: string;
yes?: boolean;
}; };
export type UpdateStatusOptions = { export type UpdateStatusOptions = {
json?: boolean; json?: boolean;
@@ -375,6 +377,8 @@ function printResult(result: UpdateRunResult, opts: PrintResultOptions) {
} }
export async function updateCommand(opts: UpdateCommandOptions): Promise<void> { export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
process.noDeprecation = true;
process.env.NODE_NO_WARNINGS = "1";
const timeoutMs = opts.timeout ? Number.parseInt(opts.timeout, 10) * 1000 : undefined; const timeoutMs = opts.timeout ? Number.parseInt(opts.timeout, 10) * 1000 : undefined;
if (timeoutMs !== undefined && (Number.isNaN(timeoutMs) || timeoutMs <= 0)) { if (timeoutMs !== undefined && (Number.isNaN(timeoutMs) || timeoutMs <= 0)) {
@@ -427,7 +431,7 @@ export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
const needsConfirm = const needsConfirm =
currentVersion != null && (targetVersion == null || (cmp != null && cmp > 0)); currentVersion != null && (targetVersion == null || (cmp != null && cmp > 0));
if (needsConfirm) { if (needsConfirm && !opts.yes) {
if (!process.stdin.isTTY || opts.json) { if (!process.stdin.isTTY || opts.json) {
defaultRuntime.error( defaultRuntime.error(
[ [
@@ -667,27 +671,46 @@ export function registerUpdateCli(program: Command) {
.option("--channel <stable|beta|dev>", "Persist update channel (git + npm)") .option("--channel <stable|beta|dev>", "Persist update channel (git + npm)")
.option("--tag <dist-tag|version>", "Override npm dist-tag or version for this update") .option("--tag <dist-tag|version>", "Override npm dist-tag or version for this update")
.option("--timeout <seconds>", "Timeout for each update step in seconds (default: 1200)") .option("--timeout <seconds>", "Timeout for each update step in seconds (default: 1200)")
.addHelpText( .option("--yes", "Skip confirmation prompts (non-interactive)", false)
"after", .addHelpText("after", () => {
() => const examples = [
` ["clawdbot update", "Update a source checkout (git)"],
Examples: ["clawdbot update --channel beta", "Switch to beta channel (git + npm)"],
clawdbot update # Update a source checkout (git) ["clawdbot update --channel dev", "Switch to dev channel (git + npm)"],
clawdbot update --channel beta # Switch to beta channel (git + npm) ["clawdbot update --tag beta", "One-off update to a dist-tag or version"],
clawdbot update --channel dev # Switch to dev channel (git + npm) ["clawdbot update --restart", "Update and restart the daemon"],
clawdbot update --tag beta # One-off update to a dist-tag or version ["clawdbot update --json", "Output result as JSON"],
clawdbot update --restart # Update and restart the daemon ["clawdbot update --yes", "Non-interactive (accept downgrade prompts)"],
clawdbot update --json # Output result as JSON ["clawdbot --update", "Shorthand for clawdbot update"],
clawdbot --update # Shorthand for clawdbot update ] as const;
const fmtExamples = examples
.map(([cmd, desc]) => ` ${theme.command(cmd)} ${theme.muted(`# ${desc}`)}`)
.join("\n");
return `
${theme.heading("What this does:")}
- Git checkouts: fetches, rebases, installs deps, builds, and runs doctor
- npm installs: updates via detected package manager
Notes: ${theme.heading("Switch channels:")}
- For git installs: fetches, rebases, installs deps, builds, and runs doctor - Use --channel stable|beta|dev to persist the update channel in config
- Run clawdbot update status to see the active channel and source
- Use --tag <dist-tag|version> for a one-off npm update without persisting
${theme.heading("Non-interactive:")}
- Use --yes to accept downgrade prompts
- Combine with --channel/--tag/--restart/--json/--timeout as needed
${theme.heading("Examples:")}
${fmtExamples}
${theme.heading("Notes:")}
- Switch channels with --channel stable|beta|dev
- For global installs: auto-updates via detected package manager when possible (see docs/install/updating.md) - For global installs: auto-updates via detected package manager when possible (see docs/install/updating.md)
- Downgrades require confirmation (can break configuration) - Downgrades require confirmation (can break configuration)
- Skips update if the working directory has uncommitted changes - Skips update if the working directory has uncommitted changes
${theme.muted("Docs:")} ${formatDocsLink("/cli/update", "docs.clawd.bot/cli/update")}`, ${theme.muted("Docs:")} ${formatDocsLink("/cli/update", "docs.clawd.bot/cli/update")}`;
) })
.action(async (opts) => { .action(async (opts) => {
try { try {
await updateCommand({ await updateCommand({
@@ -696,6 +719,7 @@ ${theme.muted("Docs:")} ${formatDocsLink("/cli/update", "docs.clawd.bot/cli/upda
channel: opts.channel as string | undefined, channel: opts.channel as string | undefined,
tag: opts.tag as string | undefined, tag: opts.tag as string | undefined,
timeout: opts.timeout as string | undefined, timeout: opts.timeout as string | undefined,
yes: Boolean(opts.yes),
}); });
} catch (err) { } catch (err) {
defaultRuntime.error(String(err)); defaultRuntime.error(String(err));
@@ -711,17 +735,15 @@ ${theme.muted("Docs:")} ${formatDocsLink("/cli/update", "docs.clawd.bot/cli/upda
.addHelpText( .addHelpText(
"after", "after",
() => () =>
` `\n${theme.heading("Examples:")}\n${formatHelpExamples([
Examples: ["clawdbot update status", "Show channel + version status."],
clawdbot update status ["clawdbot update status --json", "JSON output."],
clawdbot update status --json ["clawdbot update status --timeout 10", "Custom timeout."],
clawdbot update status --timeout 10 ])}\n\n${theme.heading("Notes:")}\n${theme.muted(
"- Shows current update channel (stable/beta/dev) and source",
Notes: )}\n${theme.muted("- Includes git tag/branch/SHA for source checkouts")}\n\n${theme.muted(
- Shows current update channel (stable/beta/dev) and source "Docs:",
- Includes git tag/branch/SHA for source checkouts )} ${formatDocsLink("/cli/update", "docs.clawd.bot/cli/update")}`,
${theme.muted("Docs:")} ${formatDocsLink("/cli/update", "docs.clawd.bot/cli/update")}`,
) )
.action(async (opts) => { .action(async (opts) => {
try { try {
+2
View File
@@ -5,6 +5,8 @@ export const GATEWAY_CLIENT_IDS = {
CLI: "cli", CLI: "cli",
GATEWAY_CLIENT: "gateway-client", GATEWAY_CLIENT: "gateway-client",
MACOS_APP: "clawdbot-macos", MACOS_APP: "clawdbot-macos",
IOS_APP: "clawdbot-ios",
ANDROID_APP: "clawdbot-android",
NODE_HOST: "node-host", NODE_HOST: "node-host",
TEST: "test", TEST: "test",
FINGERPRINT: "fingerprint", FINGERPRINT: "fingerprint",
+23 -2
View File
@@ -38,6 +38,24 @@ function isNodeEntry(entry: { role?: string; roles?: string[] }) {
return false; return false;
} }
function normalizeNodeInvokeResultParams(params: unknown): unknown {
if (!params || typeof params !== "object") return params;
const raw = params as Record<string, unknown>;
const normalized: Record<string, unknown> = { ...raw };
if (normalized.payloadJSON === null) {
delete normalized.payloadJSON;
} else if (normalized.payloadJSON !== undefined && typeof normalized.payloadJSON !== "string") {
if (normalized.payload === undefined) {
normalized.payload = normalized.payloadJSON;
}
delete normalized.payloadJSON;
}
if (normalized.error === null) {
delete normalized.error;
}
return normalized;
}
export const nodeHandlers: GatewayRequestHandlers = { export const nodeHandlers: GatewayRequestHandlers = {
"node.pair.request": async ({ params, respond, context }) => { "node.pair.request": async ({ params, respond, context }) => {
if (!validateNodePairRequestParams(params)) { if (!validateNodePairRequestParams(params)) {
@@ -258,6 +276,7 @@ export const nodeHandlers: GatewayRequestHandlers = {
caps, caps,
commands, commands,
permissions: live?.permissions ?? paired?.permissions, permissions: live?.permissions ?? paired?.permissions,
connectedAtMs: live?.connectedAtMs,
paired: Boolean(paired), paired: Boolean(paired),
connected: Boolean(live), connected: Boolean(live),
}; };
@@ -320,6 +339,7 @@ export const nodeHandlers: GatewayRequestHandlers = {
caps, caps,
commands, commands,
permissions: live?.permissions, permissions: live?.permissions,
connectedAtMs: live?.connectedAtMs,
paired: Boolean(paired), paired: Boolean(paired),
connected: Boolean(live), connected: Boolean(live),
}, },
@@ -415,7 +435,8 @@ export const nodeHandlers: GatewayRequestHandlers = {
}); });
}, },
"node.invoke.result": async ({ params, respond, context, client }) => { "node.invoke.result": async ({ params, respond, context, client }) => {
if (!validateNodeInvokeResultParams(params)) { const normalizedParams = normalizeNodeInvokeResultParams(params);
if (!validateNodeInvokeResultParams(normalizedParams)) {
respondInvalidParams({ respondInvalidParams({
respond, respond,
method: "node.invoke.result", method: "node.invoke.result",
@@ -423,7 +444,7 @@ export const nodeHandlers: GatewayRequestHandlers = {
}); });
return; return;
} }
const p = params as { const p = normalizedParams as {
id: string; id: string;
nodeId: string; nodeId: string;
ok: boolean; ok: boolean;
+87
View File
@@ -0,0 +1,87 @@
import { test } from "vitest";
import WebSocket from "ws";
import { PROTOCOL_VERSION } from "./protocol/index.js";
import { getFreePort, onceMessage, startGatewayServer } from "./test-helpers.server.js";
function connectReq(
ws: WebSocket,
params: { clientId: string; platform: string; token?: string; password?: string },
): Promise<{ ok: boolean; error?: { message?: string } }> {
const id = `c-${Math.random().toString(16).slice(2)}`;
ws.send(
JSON.stringify({
type: "req",
id,
method: "connect",
params: {
minProtocol: PROTOCOL_VERSION,
maxProtocol: PROTOCOL_VERSION,
client: {
id: params.clientId,
version: "dev",
platform: params.platform,
mode: "node",
},
auth: {
token: params.token,
password: params.password,
},
role: "node",
scopes: [],
caps: ["canvas"],
commands: ["system.notify"],
permissions: {},
},
}),
);
return onceMessage(
ws,
(o) => (o as { type?: string }).type === "res" && (o as { id?: string }).id === id,
);
}
test("accepts clawdbot-ios as a valid gateway client id", async () => {
const port = await getFreePort();
const server = await startGatewayServer(port);
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
await new Promise<void>((resolve) => ws.once("open", resolve));
const res = await connectReq(ws, { clientId: "clawdbot-ios", platform: "ios" });
// We don't care if auth fails here; we only care that schema validation accepts the client id.
// A schema rejection would close the socket before sending a response.
if (!res.ok) {
// allow unauthorized error when gateway requires auth
// but reject schema validation errors
const message = String(res.error?.message ?? "");
if (message.includes("invalid connect params")) {
throw new Error(message);
}
}
ws.close();
await server.close();
});
test("accepts clawdbot-android as a valid gateway client id", async () => {
const port = await getFreePort();
const server = await startGatewayServer(port);
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
await new Promise<void>((resolve) => ws.once("open", resolve));
const res = await connectReq(ws, { clientId: "clawdbot-android", platform: "android" });
// We don't care if auth fails here; we only care that schema validation accepts the client id.
// A schema rejection would close the socket before sending a response.
if (!res.ok) {
// allow unauthorized error when gateway requires auth
// but reject schema validation errors
const message = String(res.error?.message ?? "");
if (message.includes("invalid connect params")) {
throw new Error(message);
}
}
ws.close();
await server.close();
});
@@ -172,4 +172,55 @@ describe("gateway node command allowlist", () => {
ws.close(); ws.close();
await server.close(); await server.close();
}); });
test("accepts node invoke result with null payloadJSON", async () => {
const { server, ws, port } = await startServerWithClient();
await connectOk(ws);
let resolveInvoke: ((payload: { id?: string; nodeId?: string }) => void) | null = null;
const invokeReqP = new Promise<{ id?: string; nodeId?: string }>((resolve) => {
resolveInvoke = resolve;
});
const nodeClient = await connectNodeClient({
port,
commands: ["canvas.snapshot"],
instanceId: "node-null-payloadjson",
displayName: "node-null-payloadjson",
onEvent: (evt) => {
if (evt.event === "node.invoke.request") {
const payload = evt.payload as { id?: string; nodeId?: string };
resolveInvoke?.(payload);
}
},
});
const listRes = await rpcReq<{ nodes?: Array<{ nodeId: string }> }>(ws, "node.list", {});
const nodeId = listRes.payload?.nodes?.[0]?.nodeId ?? "";
expect(nodeId).toBeTruthy();
const invokeResP = rpcReq(ws, "node.invoke", {
nodeId,
command: "canvas.snapshot",
params: { format: "png" },
idempotencyKey: "allowlist-null-payloadjson",
});
const payload = await invokeReqP;
const requestId = payload?.id ?? "";
const nodeIdFromReq = payload?.nodeId ?? "node-null-payloadjson";
await nodeClient.request("node.invoke.result", {
id: requestId,
nodeId: nodeIdFromReq,
ok: true,
payloadJSON: null,
});
const invokeRes = await invokeResP;
expect(invokeRes.ok).toBe(true);
nodeClient.stop();
ws.close();
await server.close();
});
}); });
@@ -719,13 +719,17 @@ export function attachGatewayWsMessageHandler(params: {
if (role === "node") { if (role === "node") {
const context = buildRequestContext(); const context = buildRequestContext();
const nodeSession = context.nodeRegistry.register(nextClient, { remoteIp: remoteAddr }); const nodeSession = context.nodeRegistry.register(nextClient, { remoteIp: remoteAddr });
void updatePairedNodeMetadata(nodeSession.nodeId, { const instanceIdRaw = connectParams.client.instanceId;
lastConnectedAtMs: nodeSession.connectedAtMs, const instanceId = typeof instanceIdRaw === "string" ? instanceIdRaw.trim() : "";
}).catch((err) => const nodeIdsForPairing = new Set<string>([nodeSession.nodeId]);
logGateway.warn( if (instanceId) nodeIdsForPairing.add(instanceId);
`failed to record last connect for ${nodeSession.nodeId}: ${formatForLog(err)}`, for (const nodeId of nodeIdsForPairing) {
), void updatePairedNodeMetadata(nodeId, {
); lastConnectedAtMs: nodeSession.connectedAtMs,
}).catch((err) =>
logGateway.warn(`failed to record last connect for ${nodeId}: ${formatForLog(err)}`),
);
}
recordRemoteNodeInfo({ recordRemoteNodeInfo({
nodeId: nodeSession.nodeId, nodeId: nodeSession.nodeId,
displayName: nodeSession.displayName, displayName: nodeSession.displayName,
+35 -12
View File
@@ -188,31 +188,54 @@ export function ensureExecApprovals(): ExecApprovalsFile {
return updated; return updated;
} }
function normalizeSecurity(value?: ExecSecurity): ExecSecurity { function normalizeSecurity(value: ExecSecurity | undefined, fallback: ExecSecurity): ExecSecurity {
if (value === "allowlist" || value === "full" || value === "deny") return value; if (value === "allowlist" || value === "full" || value === "deny") return value;
return DEFAULT_SECURITY; return fallback;
} }
function normalizeAsk(value?: ExecAsk): ExecAsk { function normalizeAsk(value: ExecAsk | undefined, fallback: ExecAsk): ExecAsk {
if (value === "always" || value === "off" || value === "on-miss") return value; if (value === "always" || value === "off" || value === "on-miss") return value;
return DEFAULT_ASK; return fallback;
} }
export function resolveExecApprovals(agentId?: string): ExecApprovalsResolved { export type ExecApprovalsDefaultOverrides = {
security?: ExecSecurity;
ask?: ExecAsk;
askFallback?: ExecSecurity;
autoAllowSkills?: boolean;
};
export function resolveExecApprovals(
agentId?: string,
overrides?: ExecApprovalsDefaultOverrides,
): ExecApprovalsResolved {
const file = ensureExecApprovals(); const file = ensureExecApprovals();
const defaults = file.defaults ?? {}; const defaults = file.defaults ?? {};
const agentKey = agentId ?? "default"; const agentKey = agentId ?? "default";
const agent = file.agents?.[agentKey] ?? {}; const agent = file.agents?.[agentKey] ?? {};
const fallbackSecurity = overrides?.security ?? DEFAULT_SECURITY;
const fallbackAsk = overrides?.ask ?? DEFAULT_ASK;
const fallbackAskFallback = overrides?.askFallback ?? DEFAULT_ASK_FALLBACK;
const fallbackAutoAllowSkills = overrides?.autoAllowSkills ?? DEFAULT_AUTO_ALLOW_SKILLS;
const resolvedDefaults: Required<ExecApprovalsDefaults> = { const resolvedDefaults: Required<ExecApprovalsDefaults> = {
security: normalizeSecurity(defaults.security), security: normalizeSecurity(defaults.security, fallbackSecurity),
ask: normalizeAsk(defaults.ask), ask: normalizeAsk(defaults.ask, fallbackAsk),
askFallback: normalizeSecurity(defaults.askFallback ?? DEFAULT_ASK_FALLBACK), askFallback: normalizeSecurity(
autoAllowSkills: Boolean(defaults.autoAllowSkills ?? DEFAULT_AUTO_ALLOW_SKILLS), defaults.askFallback ?? fallbackAskFallback,
fallbackAskFallback,
),
autoAllowSkills: Boolean(defaults.autoAllowSkills ?? fallbackAutoAllowSkills),
}; };
const resolvedAgent: Required<ExecApprovalsDefaults> = { const resolvedAgent: Required<ExecApprovalsDefaults> = {
security: normalizeSecurity(agent.security ?? resolvedDefaults.security), security: normalizeSecurity(
ask: normalizeAsk(agent.ask ?? resolvedDefaults.ask), agent.security ?? resolvedDefaults.security,
askFallback: normalizeSecurity(agent.askFallback ?? resolvedDefaults.askFallback), resolvedDefaults.security,
),
ask: normalizeAsk(agent.ask ?? resolvedDefaults.ask, resolvedDefaults.ask),
askFallback: normalizeSecurity(
agent.askFallback ?? resolvedDefaults.askFallback,
resolvedDefaults.askFallback,
),
autoAllowSkills: Boolean(agent.autoAllowSkills ?? resolvedDefaults.autoAllowSkills), autoAllowSkills: Boolean(agent.autoAllowSkills ?? resolvedDefaults.autoAllowSkills),
}; };
const allowlist = Array.isArray(agent.allowlist) ? agent.allowlist : []; const allowlist = Array.isArray(agent.allowlist) ? agent.allowlist : [];
+34
View File
@@ -0,0 +1,34 @@
import { describe, expect, test } from "vitest";
import { buildNodeInvokeResultParams } from "./runner.js";
describe("buildNodeInvokeResultParams", () => {
test("omits optional fields when null/undefined", () => {
const params = buildNodeInvokeResultParams(
{ id: "invoke-1", nodeId: "node-1", command: "system.run" },
{ ok: true, payloadJSON: null, error: null },
);
expect(params).toEqual({ id: "invoke-1", nodeId: "node-1", ok: true });
expect("payloadJSON" in params).toBe(false);
expect("error" in params).toBe(false);
});
test("includes payloadJSON when provided", () => {
const params = buildNodeInvokeResultParams(
{ id: "invoke-2", nodeId: "node-2", command: "system.run" },
{ ok: true, payloadJSON: '{"ok":true}' },
);
expect(params.payloadJSON).toBe('{"ok":true}');
});
test("includes payload when provided", () => {
const params = buildNodeInvokeResultParams(
{ id: "invoke-3", nodeId: "node-3", command: "system.run" },
{ ok: false, payload: { reason: "bad" } },
);
expect(params.payload).toEqual({ reason: "bad" });
});
});
+89 -56
View File
@@ -104,7 +104,8 @@ const OUTPUT_CAP = 200_000;
const OUTPUT_EVENT_TAIL = 20_000; const OUTPUT_EVENT_TAIL = 20_000;
const execHostEnforced = process.env.CLAWDBOT_NODE_EXEC_HOST?.trim().toLowerCase() === "app"; const execHostEnforced = process.env.CLAWDBOT_NODE_EXEC_HOST?.trim().toLowerCase() === "app";
const execHostFallbackAllowed = process.env.CLAWDBOT_NODE_EXEC_FALLBACK?.trim() === "1"; const execHostFallbackAllowed =
process.env.CLAWDBOT_NODE_EXEC_FALLBACK?.trim().toLowerCase() !== "0";
const blockedEnvKeys = new Set([ const blockedEnvKeys = new Set([
"PATH", "PATH",
@@ -545,7 +546,7 @@ async function handleInvoke(
const rawCommand = typeof params.rawCommand === "string" ? params.rawCommand.trim() : ""; const rawCommand = typeof params.rawCommand === "string" ? params.rawCommand.trim() : "";
const cmdText = rawCommand || formatCommand(argv); const cmdText = rawCommand || formatCommand(argv);
const agentId = params.agentId?.trim() || undefined; const agentId = params.agentId?.trim() || undefined;
const approvals = resolveExecApprovals(agentId); const approvals = resolveExecApprovals(agentId, { security: "allowlist" });
const security = approvals.agent.security; const security = approvals.agent.security;
const ask = approvals.agent.ask; const ask = approvals.agent.ask;
const autoAllowSkills = approvals.agent.autoAllowSkills; const autoAllowSkills = approvals.agent.autoAllowSkills;
@@ -559,8 +560,7 @@ async function handleInvoke(
const skillAllow = const skillAllow =
autoAllowSkills && resolution?.executableName ? bins.has(resolution.executableName) : false; autoAllowSkills && resolution?.executableName ? bins.has(resolution.executableName) : false;
const useMacAppExec = const useMacAppExec = process.platform === "darwin";
process.platform === "darwin" && (execHostEnforced || !execHostFallbackAllowed);
if (useMacAppExec) { if (useMacAppExec) {
const approvalDecision = const approvalDecision =
params.approvalDecision === "allow-once" || params.approvalDecision === "allow-always" params.approvalDecision === "allow-once" || params.approvalDecision === "allow-always"
@@ -579,28 +579,28 @@ async function handleInvoke(
}; };
const response = await runViaMacAppExecHost({ approvals, request: execRequest }); const response = await runViaMacAppExecHost({ approvals, request: execRequest });
if (!response) { if (!response) {
await sendNodeEvent( if (execHostEnforced || !execHostFallbackAllowed) {
client, await sendNodeEvent(
"exec.denied", client,
buildExecEventPayload({ "exec.denied",
sessionKey, buildExecEventPayload({
runId, sessionKey,
host: "node", runId,
command: cmdText, host: "node",
reason: "companion-unavailable", command: cmdText,
}), reason: "companion-unavailable",
); }),
await sendInvokeResult(client, frame, { );
ok: false, await sendInvokeResult(client, frame, {
error: { ok: false,
code: "UNAVAILABLE", error: {
message: "COMPANION_APP_UNAVAILABLE: macOS app exec host unreachable", code: "UNAVAILABLE",
}, message: "COMPANION_APP_UNAVAILABLE: macOS app exec host unreachable",
}); },
return; });
} return;
}
if (!response.ok) { } else if (!response.ok) {
const reason = response.error.reason ?? "approval-required"; const reason = response.error.reason ?? "approval-required";
await sendNodeEvent( await sendNodeEvent(
client, client,
@@ -618,29 +618,29 @@ async function handleInvoke(
error: { code: "UNAVAILABLE", message: response.error.message }, error: { code: "UNAVAILABLE", message: response.error.message },
}); });
return; return;
} else {
const result: ExecHostRunResult = response.payload;
const combined = [result.stdout, result.stderr, result.error].filter(Boolean).join("\n");
await sendNodeEvent(
client,
"exec.finished",
buildExecEventPayload({
sessionKey,
runId,
host: "node",
command: cmdText,
exitCode: result.exitCode,
timedOut: result.timedOut,
success: result.success,
output: combined,
}),
);
await sendInvokeResult(client, frame, {
ok: true,
payloadJSON: JSON.stringify(result),
});
return;
} }
const result: ExecHostRunResult = response.payload;
const combined = [result.stdout, result.stderr, result.error].filter(Boolean).join("\n");
await sendNodeEvent(
client,
"exec.finished",
buildExecEventPayload({
sessionKey,
runId,
host: "node",
command: cmdText,
exitCode: result.exitCode,
timedOut: result.timedOut,
success: result.success,
output: combined,
}),
);
await sendInvokeResult(client, frame, {
ok: true,
payloadJSON: JSON.stringify(result),
});
return;
} }
if (security === "deny") { if (security === "deny") {
@@ -833,19 +833,52 @@ async function sendInvokeResult(
}, },
) { ) {
try { try {
await client.request("node.invoke.result", { await client.request("node.invoke.result", buildNodeInvokeResultParams(frame, result));
id: frame.id,
nodeId: frame.nodeId,
ok: result.ok,
payload: result.payload,
payloadJSON: result.payloadJSON ?? null,
error: result.error ?? null,
});
} catch { } catch {
// ignore: node invoke responses are best-effort // ignore: node invoke responses are best-effort
} }
} }
export function buildNodeInvokeResultParams(
frame: NodeInvokeRequestPayload,
result: {
ok: boolean;
payload?: unknown;
payloadJSON?: string | null;
error?: { code?: string; message?: string } | null;
},
): {
id: string;
nodeId: string;
ok: boolean;
payload?: unknown;
payloadJSON?: string;
error?: { code?: string; message?: string };
} {
const params: {
id: string;
nodeId: string;
ok: boolean;
payload?: unknown;
payloadJSON?: string;
error?: { code?: string; message?: string };
} = {
id: frame.id,
nodeId: frame.nodeId,
ok: result.ok,
};
if (result.payload !== undefined) {
params.payload = result.payload;
}
if (typeof result.payloadJSON === "string") {
params.payloadJSON = result.payloadJSON;
}
if (result.error) {
params.error = result.error;
}
return params;
}
async function sendNodeEvent(client: GatewayClient, event: string, payload: unknown) { async function sendNodeEvent(client: GatewayClient, event: string, payload: unknown) {
try { try {
await client.request("node.event", { await client.request("node.event", {
+18 -1
View File
@@ -1,4 +1,5 @@
import { execFile, spawn } from "node:child_process"; import { execFile, spawn } from "node:child_process";
import path from "node:path";
import { promisify } from "node:util"; import { promisify } from "node:util";
import { danger, shouldLogVerbose } from "../globals.js"; import { danger, shouldLogVerbose } from "../globals.js";
@@ -61,12 +62,28 @@ export async function runCommandWithTimeout(
const { windowsVerbatimArguments } = options; const { windowsVerbatimArguments } = options;
const hasInput = input !== undefined; const hasInput = input !== undefined;
const shouldSuppressNpmFund = (() => {
const cmd = path.basename(argv[0] ?? "");
if (cmd === "npm" || cmd === "npm.cmd" || cmd === "npm.exe") return true;
if (cmd === "node" || cmd === "node.exe") {
const script = argv[1] ?? "";
return script.includes("npm-cli.js");
}
return false;
})();
const resolvedEnv = env ? { ...process.env, ...env } : { ...process.env };
if (shouldSuppressNpmFund) {
if (resolvedEnv.NPM_CONFIG_FUND == null) resolvedEnv.NPM_CONFIG_FUND = "false";
if (resolvedEnv.npm_config_fund == null) resolvedEnv.npm_config_fund = "false";
}
// Spawn with inherited stdin (TTY) so tools like `pi` stay interactive when needed. // Spawn with inherited stdin (TTY) so tools like `pi` stay interactive when needed.
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
const child = spawn(argv[0], argv.slice(1), { const child = spawn(argv[0], argv.slice(1), {
stdio: [hasInput ? "pipe" : "inherit", "pipe", "pipe"], stdio: [hasInput ? "pipe" : "inherit", "pipe", "pipe"],
cwd, cwd,
env: env ? { ...process.env, ...env } : process.env, env: resolvedEnv,
windowsVerbatimArguments, windowsVerbatimArguments,
}); });
let stdout = ""; let stdout = "";
+2 -2
View File
@@ -179,6 +179,8 @@ export const registerTelegramHandlers = ({
const callback = ctx.callbackQuery; const callback = ctx.callbackQuery;
if (!callback) return; if (!callback) return;
if (shouldSkipUpdate(ctx)) return; if (shouldSkipUpdate(ctx)) return;
// Answer immediately to prevent Telegram from retrying while we process
await bot.api.answerCallbackQuery(callback.id).catch(() => {});
try { try {
const data = (callback.data ?? "").trim(); const data = (callback.data ?? "").trim();
const callbackMessage = callback.message; const callbackMessage = callback.message;
@@ -323,8 +325,6 @@ export const registerTelegramHandlers = ({
}); });
} catch (err) { } catch (err) {
runtime.error?.(danger(`callback handler failed: ${String(err)}`)); runtime.error?.(danger(`callback handler failed: ${String(err)}`));
} finally {
await bot.api.answerCallbackQuery(callback.id).catch(() => {});
} }
}); });
+99
View File
@@ -0,0 +1,99 @@
import { describe, expect, it, vi } from "vitest";
import { createEditorSubmitHandler } from "./tui.js";
describe("createEditorSubmitHandler", () => {
it("adds submitted messages to editor history", () => {
const editor = {
setText: vi.fn(),
addToHistory: vi.fn(),
};
const handler = createEditorSubmitHandler({
editor,
handleCommand: vi.fn(),
sendMessage: vi.fn(),
});
handler("hello world");
expect(editor.setText).toHaveBeenCalledWith("");
expect(editor.addToHistory).toHaveBeenCalledWith("hello world");
});
it("trims input before adding to history", () => {
const editor = {
setText: vi.fn(),
addToHistory: vi.fn(),
};
const handler = createEditorSubmitHandler({
editor,
handleCommand: vi.fn(),
sendMessage: vi.fn(),
});
handler(" hi ");
expect(editor.addToHistory).toHaveBeenCalledWith("hi");
});
it("does not add empty submissions to history", () => {
const editor = {
setText: vi.fn(),
addToHistory: vi.fn(),
};
const handler = createEditorSubmitHandler({
editor,
handleCommand: vi.fn(),
sendMessage: vi.fn(),
});
handler(" ");
expect(editor.addToHistory).not.toHaveBeenCalled();
});
it("routes slash commands to handleCommand", () => {
const editor = {
setText: vi.fn(),
addToHistory: vi.fn(),
};
const handleCommand = vi.fn();
const sendMessage = vi.fn();
const handler = createEditorSubmitHandler({
editor,
handleCommand,
sendMessage,
});
handler("/models");
expect(editor.addToHistory).toHaveBeenCalledWith("/models");
expect(handleCommand).toHaveBeenCalledWith("/models");
expect(sendMessage).not.toHaveBeenCalled();
});
it("routes normal messages to sendMessage", () => {
const editor = {
setText: vi.fn(),
addToHistory: vi.fn(),
};
const handleCommand = vi.fn();
const sendMessage = vi.fn();
const handler = createEditorSubmitHandler({
editor,
handleCommand,
sendMessage,
});
handler("hello");
expect(editor.addToHistory).toHaveBeenCalledWith("hello");
expect(sendMessage).toHaveBeenCalledWith("hello");
expect(handleCommand).not.toHaveBeenCalled();
});
});
+29 -10
View File
@@ -36,6 +36,30 @@ import type {
export { resolveFinalAssistantText } from "./tui-formatters.js"; export { resolveFinalAssistantText } from "./tui-formatters.js";
export type { TuiOptions } from "./tui-types.js"; export type { TuiOptions } from "./tui-types.js";
export function createEditorSubmitHandler(params: {
editor: {
setText: (value: string) => void;
addToHistory: (value: string) => void;
};
handleCommand: (value: string) => Promise<void> | void;
sendMessage: (value: string) => Promise<void> | void;
}) {
return (text: string) => {
const value = text.trim();
params.editor.setText("");
if (!value) return;
// Enable built-in editor prompt history navigation (up/down).
params.editor.addToHistory(value);
if (value.startsWith("/")) {
void params.handleCommand(value);
return;
}
void params.sendMessage(value);
};
}
export async function runTui(opts: TuiOptions) { export async function runTui(opts: TuiOptions) {
const config = loadConfig(); const config = loadConfig();
const initialSessionInput = (opts.session ?? "").trim(); const initialSessionInput = (opts.session ?? "").trim();
@@ -473,16 +497,11 @@ export async function runTui(opts: TuiOptions) {
}); });
updateAutocompleteProvider(); updateAutocompleteProvider();
editor.onSubmit = (text) => { editor.onSubmit = createEditorSubmitHandler({
const value = text.trim(); editor,
editor.setText(""); handleCommand,
if (!value) return; sendMessage,
if (value.startsWith("/")) { });
void handleCommand(value);
return;
}
void sendMessage(value);
};
editor.onEscape = () => { editor.onEscape = () => {
void abortActive(); void abortActive();