src — nodes

Module: src-nodes Cohesion: 0.80 Members: 0

src — nodes

The src/nodes module is a foundational component for interacting with external computational entities, whether they are physical devices or companion applications running on those devices. It provides two distinct but complementary systems:

  1. Device Node System: For direct, low-level control and interaction with physical devices (e.g., servers, phones) using native system commands.
  2. Companion Node System: For managing companion applications that connect to a central Gateway (e.g., via WebSockets) and offer higher-level, app-specific capabilities.

This document details both systems, their core components, functionality, and how they integrate into the broader codebase.


1. Device Node System

The Device Node System, primarily managed by DeviceNodeManager, enables the application to directly control and query physical devices like macOS, Linux, and Android machines. It achieves this by abstracting the underlying communication mechanism through various DeviceTransport implementations. This system is ideal for scenarios requiring direct shell access, file transfer, or native OS-level actions.

1.1 Core Components

1.2 How it Works

  1. Initialization: When DeviceNodeManager.getInstance() is called, it attempts to load previously paired devices from ~/.codebuddy/devices.json.
  2. Pairing (pairDevice):

  1. Device Actions (e.g., cameraSnap, screenshot):

1.3 Architecture Diagram

graph TD
    A[DeviceNodeManager] --> B{DeviceTransport Interface}
    B --> C[SSHTransport]
    B --> D[ADBTransport]
    B --> E[LocalTransport]
    A --> F[DeviceNode]
    A --> G[PlatformCommands]

1.4 Integration


2. Companion Node System

The Companion Node System, managed by NodeManager, focuses on interacting with dedicated companion applications (nodes) that run on various platforms (macOS, iOS, Android, Linux, Windows). These companion apps are expected to connect to a central Gateway (e.g., via WebSockets) and expose higher-level, app-specific capabilities.

2.1 Core Components

2.2 How it Works

  1. Initialization: NodeManager.getInstance() initializes the manager, optionally with custom configuration.
  2. Pairing Request (requestPairing):

  1. Pairing Approval (approvePairing):

  1. Node Lifecycle (heartbeat, markOffline):

  1. Capability Invocation (invoke):

2.3 Architecture Diagram

graph TD
    A[NodeManager] --> B[NodeInfo]
    A --> C[NodePairingRequest]
    A --> D[NodeInvocation]

2.4 Integration


3. Relationship and Distinctions

While both systems deal with "nodes," they serve different purposes and employ distinct interaction models:

In essence, the Device Node System treats a device as a remote computer, while the Companion Node System treats it as a platform hosting a specialized application.


4. API Reference (Key Types/Interfaces)

Device Node System

Companion Node System


5. How to Contribute and Extend

Extending the Device Node System

  1. Create a new class that implements the DeviceTransport interface (e.g., BluetoothTransport).
  2. Add the new transport type to the TransportType union.
  3. Modify DeviceNodeManager.createTransport() to instantiate your new transport based on its TransportType.
  4. Implement getCapabilities() in your new transport to accurately reflect what it can do.

  1. Add the new capability string to the DeviceCapability union type.
  2. Update the getCapabilities() method in relevant DeviceTransport implementations to detect and report this new capability.
  3. If the capability involves executing a shell command, add a corresponding method to the PlatformCommands interface in platform-commands.ts and implement it for MacOSCommands, LinuxCommands, and AndroidCommands.
  4. Add a new action method to DeviceNodeManager (e.g., async newAction(deviceId: string, ...args: any[])) that checks for the capability, gets the transport, retrieves platform commands, and executes the appropriate command.

  1. If a new generic action is needed, add it to the PlatformCommands interface.
  2. Implement the new method in MacOSCommands, LinuxCommands, and AndroidCommands with the appropriate shell commands.

Extending the Companion Node System

  1. Add the new platform string to the NodePlatform union type.
  2. Update the PLATFORM_CAPABILITIES map in src/nodes/index.ts to define the default capabilities for this new platform.

  1. Add the new capability string to the NodeCapability union type.
  2. Update the PLATFORM_CAPABILITIES map for relevant platforms.
  3. If it's a common capability, consider adding a convenience method to NodeManager (e.g., async newCapability(nodeId: string, ...args: any[])) that calls this.invoke().
  4. Ensure that any external listener to the node:invoke event is updated to handle the new capability.