# OpenCode Server Setup for Desktop Connectivity

## Problem

The OpenCode Desktop app (Electron GUI) stores its configuration in a Chromium-leveldb at `~/.config/ai.opencode.desktop/opencode.global.dat`. It does **NOT** read `~/.config/opencode/opencode.json` (the CLI config). Without connecting to a server or manually adding providers in the GUI, the Desktop shows "no providers".

## Solution: OpenCode Server

Run `opencode serve` as a systemd service on the host machine. The Desktop connects to this server and gets all providers/models from it.

### Systemd Service

```bash
# /etc/systemd/system/opencode-server.service
[Unit]
Description=OpenCode Server
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/opencode serve --port 4097 --hostname 0.0.0.0
User=rurouni
Group=rurouni
Environment=OPENCODE_SERVER_PASSWORD=***

[Install]
WantedBy=multi-user.target
```

Then:
```bash
sudo systemctl daemon-reload
sudo systemctl enable --now opencode-server
```

### Config Restriction

The `opencode.json` file read by `opencode serve` must NOT have top-level `default` or `models` keys — those are CLI-only. If the server fails with "Unrecognized keys: default, models", strip those keys and keep only the `provider` section.

**Workflow:**
1. Keep a full config at `~/.config/opencode/opencode.json` with `$schema`, `provider`, `default`, and `models` for CLI use
2. Create a server-only version at `~/.config/opencode/opencode.server.json` with just `provider`
3. Swap them: `cp ~/.config/opencode/opencode.server.json ~/.config/opencode/opencode.json` before starting server
4. Swap back after: `cp ~/.config/opencode/opencode.json.cli ~/.config/opencode/opencode.json`

### Connecting the Desktop

1. Launch OpenCode Desktop on the client machine
2. Open Settings (Ctrl+,)
3. Go to Server section → Add server: `http://<server-ip>:4097`
4. Enter the server password when prompted
5. All providers from the server config appear in the model picker

### UFW

Ensure port 4097 is open on the server for the client LAN/subnet:
```bash
sudo ufw allow from 192.168.1.0/24 to any port 4097 proto tcp
sudo ufw allow from 100.64.0.0/10 to any port 4097 proto tcp  # Tailscale
```

### CLI Attach

If `opencode` CLI is available on the client:
```bash
opencode attach http://<server-ip>:4097
```
