Metadata-Version: 2.5
Name: snippbot-device
Version: 0.6.0
Summary: Snippbot device agent - connect any machine to Snippbot
Project-URL: Homepage, https://snippbot.com
Project-URL: Documentation, https://docs.snippbot.com
Project-URL: Repository, https://github.com/snippbot/snippbot
Author: Snippbot Team
License: Proprietary
Keywords: agents,ai,device,remote-execution,snippbot
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: System :: Systems Administration
Requires-Python: >=3.11
Requires-Dist: click>=8.1
Requires-Dist: cryptography>=41.0
Requires-Dist: psutil>=5.9
Requires-Dist: pyotp>=2.9
Requires-Dist: rich>=13.0
Requires-Dist: websockets>=12.0
Provides-Extra: all
Requires-Dist: dxcam>=0.3; (sys_platform == 'win32') and extra == 'all'
Requires-Dist: livekit>=1.0; extra == 'all'
Requires-Dist: mss>=9.0; extra == 'all'
Requires-Dist: numpy>=1.24; extra == 'all'
Requires-Dist: opencv-python-headless>=4.8; extra == 'all'
Requires-Dist: pillow>=10.0; extra == 'all'
Requires-Dist: playwright>=1.40; extra == 'all'
Requires-Dist: pyobjc-framework-cocoa>=10.0; (sys_platform == 'darwin') and extra == 'all'
Requires-Dist: pyobjc-framework-coremedia>=10.0; (sys_platform == 'darwin') and extra == 'all'
Requires-Dist: pyobjc-framework-quartz>=10.0; (sys_platform == 'darwin') and extra == 'all'
Requires-Dist: pyobjc-framework-screencapturekit>=10.0; (sys_platform == 'darwin') and extra == 'all'
Requires-Dist: python-xlib>=0.33; (sys_platform == 'linux') and extra == 'all'
Requires-Dist: soundcard>=0.4.3; (sys_platform == 'win32' or sys_platform == 'linux') and extra == 'all'
Requires-Dist: sounddevice>=0.4.6; extra == 'all'
Provides-Extra: audio
Requires-Dist: numpy>=1.24; extra == 'audio'
Requires-Dist: sounddevice>=0.4.6; extra == 'audio'
Provides-Extra: browser
Requires-Dist: playwright>=1.40; extra == 'browser'
Provides-Extra: camera
Requires-Dist: opencv-python-headless>=4.8; extra == 'camera'
Requires-Dist: pillow>=10.0; extra == 'camera'
Provides-Extra: remote
Requires-Dist: dxcam>=0.3; (sys_platform == 'win32') and extra == 'remote'
Requires-Dist: livekit>=1.0; extra == 'remote'
Requires-Dist: mss>=9.0; extra == 'remote'
Requires-Dist: numpy>=1.24; extra == 'remote'
Requires-Dist: pillow>=10.0; extra == 'remote'
Requires-Dist: pyobjc-framework-cocoa>=10.0; (sys_platform == 'darwin') and extra == 'remote'
Requires-Dist: pyobjc-framework-coremedia>=10.0; (sys_platform == 'darwin') and extra == 'remote'
Requires-Dist: pyobjc-framework-quartz>=10.0; (sys_platform == 'darwin') and extra == 'remote'
Requires-Dist: pyobjc-framework-screencapturekit>=10.0; (sys_platform == 'darwin') and extra == 'remote'
Requires-Dist: python-xlib>=0.33; (sys_platform == 'linux') and extra == 'remote'
Requires-Dist: soundcard>=0.4.3; (sys_platform == 'win32' or sys_platform == 'linux') and extra == 'remote'
Provides-Extra: screen
Requires-Dist: mss>=9.0; extra == 'screen'
Requires-Dist: pillow>=10.0; extra == 'screen'
Description-Content-Type: text/markdown

# Snippbot Device Agent

Connect any machine to [Snippbot](https://snippbot.com) as a remote execution node.

## What It Does

The device agent runs on a remote machine and connects back to your Snippbot daemon via WebSocket. Once paired, the AI agent can route tool calls — shell commands, file operations, screenshots, browser automation — to the remote device.

## Installation

```bash
pipx install snippbot-device
```

Or in a virtual environment:

```bash
pip install snippbot-device
```

With optional capabilities (use pip in a venv):

```bash
pip install snippbot-device[screen]    # Screenshots (Pillow + mss)
pip install snippbot-device[browser]   # Browser automation (Playwright)
pip install snippbot-device[camera]    # Camera capture (OpenCV)
pip install snippbot-device[all]       # All optional capabilities
```

Requires Python 3.11 or higher.

## Quick Start

### 1. Pair with your Snippbot daemon

From the Snippbot UI, click **Pair Device** to generate a 6-digit pairing code. Then on the remote machine:

```bash
snippbot-device pair --host DAEMON_IP --port 18781 --code XXXXXX
```

### 2. Start the agent

```bash
snippbot-device start        # Background (daemonized)
snippbot-device start -f     # Foreground (see logs)
```

### 3. Verify

```bash
snippbot-device status       # Check connection and resource usage
snippbot-device capabilities # List detected capabilities
```

## Commands

```
snippbot-device pair          Pair this device with a Snippbot daemon
snippbot-device configure     Change the daemon address or Cloudflare Access token
snippbot-device start         Start the device agent
snippbot-device stop          Stop the running agent
snippbot-device status        Show agent status and connection info
snippbot-device capabilities  List detected capabilities
```

## Capabilities

The agent auto-detects what the machine can do:

| Capability | Requirement | Description |
|---|---|---|
| `execution.bash` | Built-in | Shell commands (bash, zsh, cmd) |
| `execution.python` | Built-in | Python script execution |
| `filesystem.read` | Built-in | Read files |
| `filesystem.write` | Built-in | Write files |
| `filesystem.transfer` | Built-in | Send/receive files to daemon |
| `system.info` | Built-in | OS, CPU, memory, disk info |
| `capture.screenshot` | `[screen]` extra | Take screenshots |
| `capture.camera_photo` | `[camera]` extra | Camera capture |
| `browser.navigate` | `[browser]` extra | Browser automation |
| `display.remote_view` | `[remote]` extra | Screen streaming |
| `display.agent_control` | `[remote]` extra, `enable remote_agent` | AI control: an agent uses the screen, mouse and keyboard, with consent at the machine every session |
| `execution.docker` | Docker CLI in PATH | Docker container management |
| `mesh_command` | Named in `[capabilities]` | Run the commands this machine's owner allow-listed for its Snippbot mesh (see [Mesh Commands](#mesh-commands)) |

## Configuration

Config is stored at `~/.snippbot-device/config.toml` after pairing.

```toml
daemon_host = "192.168.1.100"
daemon_ws_port = 18781
device_name = "my-laptop"
device_token = "device_..."
device_id = "..."
max_concurrent_tasks = 4
heartbeat_interval = 15
auto_update = true
```

## Mesh Commands

In a Snippbot mesh, another machine may run commands that the owner of this machine allow-listed. The daemon never runs them itself. It sends each one to the device agent the owner chose as the machine's command runner. The agent runs only exact allow-listed argv: no shell, stdin closed, only `PATH`, `HOME` and `LANG` in the environment, a time limit and 1 MB of output.

**1. Turn it on in the agent.** It is off unless named in the `[capabilities]` list, even when that list is empty ("everything detected"). Add it to the list the agent already has in `~/.snippbot-device/config.toml`:

```toml
[capabilities]
capabilities = ["bash", "filesystem", "system_info", "mesh_command"]   # keep your existing entries

[mesh]
# Optional. Default: $SNIPPBOT_DATA_DIR/mesh-commands.json, else ~/.snippbot/mesh-commands.json
mesh_commands_file = "/Users/me/.snippbot/mesh-commands.json"
```

Restart the agent after changing the config. Point `mesh_commands_file` at the same `mesh-commands.json` the daemon reads (its data folder).

**2. List the commands in `mesh-commands.json`.** Write it by hand. It must be a plain file (not a link) that belongs to the user both programs run as and that no other user can write. Otherwise the whole file is refused.

```json
{"version": 1, "commands": [
  {"id": "disk_free", "name": "Free disk space", "argv": ["df", "-h", "{drive}"],
   "args": {"drive": {"enum": ["/", "/Volumes/Workspace"]}}},
  {"id": "tail_log", "name": "Last log lines", "argv": ["tail", "-n", "{lines}", "app.log"],
   "args": {"lines": {"pattern": "[0-9]{1,3}", "max_length": 3}},
   "cwd": "/var/log/myapp", "timeout_s": 30},
  {"id": "grep_tag", "name": "Count a tag", "argv": ["grep", "-c", "{tag}", "/var/log/app.log"],
   "args": {"tag": {"pattern": "[a-z-]{1,20}", "allow_dash": true}}}
]}
```

| Key | Meaning |
|---|---|
| `id`, `name`, `about` | `id` is 1-64 of `a-z 0-9 _`. `name` (up to 48 characters) and `about` (optional, up to 200) are what other machines see |
| `argv` | The program and its arguments, 1-64 strings. `argv[0]` is fixed text and may not be a shell or interpreter (`sh`, `bash`, `cmd`, `powershell`, `python*`, `perl`, `ruby`, `node`, `osascript`, `sudo`, `env`, `xargs`, `ssh`, ...). An element that is exactly `{name}` is filled from that argument; text around a placeholder (`--out={name}`) is refused |
| `args` | Every placeholder, each with `enum` (a list of allowed values) or `pattern` (a regular expression the whole value must match). Optional `max_length` (default 256, at most 1024). A pattern that matches any text (`.*`, `.+`, ...) is refused |
| `allow_dash` | Per pattern argument, `true` or `false` (default). Without it a value may not start with `-`, so it can't be read as an option. Turning it on or off changes what other machines see, and the owner reviews the sharing rule again |
| `cwd` | Optional. The full path of the folder the command runs in (default: home) |
| `timeout_s` | Optional, 1-86400. The longest the command may run, whatever the caller allows |

The agent re-reads the file for every command and runs nothing unless the argv is exactly an entry's template filled with allowed values. A Stop or a timeout ends the command's whole process group.

**3. Choose the command runner on the daemon's machine.** On the machine whose commands these are, a signed-in person chooses which paired device runs them: `GET /api/mesh/command-runner` lists the paired device agents, and `POST /api/mesh/command-runner {"device_id": "..."}` chooses one (`{"device_id": null}` clears the choice). The route works only on that machine and only for a signed-in person or an API key; device and agent tokens are refused. Only that device is sent commands. The daemon never trusts what a device says about itself, such as its host name or file path. Until a runner is chosen, command items show "Not usable yet". If that device unpairs, pairs again or its key changes, the choice is cleared and must be made again. Phones can't be chosen.

## Connecting Through a Tunnel

To reach the daemon from outside its LAN, give the agent a full WebSocket URL instead of a host and port. If the hostname is protected by Cloudflare Access, also give it a service token. The token needs an Access policy with the **Service Auth** action on that application.

```bash
snippbot-device configure --url wss://daemon.example.com/device/ws \
    --cf-access-client-id <CLIENT_ID>          # prompts for the secret
snippbot-device stop && snippbot-device start
```

This doesn't need re-pairing, because the device authenticates with its token, not its address. `snippbot-device configure --local` switches back to the paired host and port, and `--clear-cf-access` removes the token. You can also pair through the tunnel with `snippbot-device pair --url <wss-url>`; run `configure` first if the tunnel needs an Access token.

The settings are stored in the `[connection]` section:

```toml
daemon_url = "wss://daemon.example.com/device/ws"
cf_access_client_id = "....access"
cf_access_client_secret = "..."
```

On Windows, Ctrl+V doesn't paste into the hidden secret prompt; right-click to paste instead.

## How It Works

1. The agent connects to the daemon via WebSocket (`ws://host:port/ws/device`)
2. Authenticates using the token received during pairing
3. Advertises its capabilities (what tools it can execute)
4. Enters a message loop waiting for tool requests
5. Sends heartbeats every 15 seconds with CPU/memory/disk metrics
6. Auto-reconnects with exponential backoff if the connection drops

## Security

- Device tokens are stored locally and validated on each connection
- Environment variables matching `*_KEY`, `*_SECRET`, `*_TOKEN`, `*_PASSWORD` are stripped from command execution
- File operations are restricted to allowed paths (configurable)
- Execution timeout: 5 minutes max per tool call
- Output limit: 1 MB per result

## Logs

```bash
# Foreground mode: logs to stdout
# Background mode: logs to ~/.snippbot-device/agent.log
tail -f ~/.snippbot-device/agent.log
```

## See Also

- [Snippbot](https://snippbot.com) - Main project
- [Snippbot Docs](https://docs.snippbot.com) - Full documentation
