Skip to content

Commit 6a2f438

Browse files
committed
docs: add server mode and shell function examples for tmux integration
- Add --port flag requirement for tmux subagent pane spawning - Add Fish shell function example with automatic port allocation - Add Bash/Zsh equivalent function example - Document how subagent panes work (opencode attach flow) - Add OPENCODE_PORT environment variable documentation - Add server mode reference section with opencode serve command
1 parent 601ea32 commit 6a2f438

File tree

1 file changed

+120
-3
lines changed

1 file changed

+120
-3
lines changed

docs/configurations.md

Lines changed: 120 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ Run background subagents in separate tmux panes for **visual multi-agent executi
259259

260260
1. **Must run inside tmux**: The feature only activates when OpenCode is already running inside a tmux session
261261
2. **Tmux installed**: Requires tmux to be available in PATH
262+
3. **Server mode**: OpenCode must run with `--port` flag to enable subagent pane spawning
262263

263264
### How It Works
264265

@@ -268,17 +269,133 @@ When `tmux.enabled` is `true` and you're inside a tmux session:
268269
- Panes are automatically closed when the subagent completes
269270
- Layout is automatically adjusted based on your configuration
270271

271-
**Example workflow**:
272+
### Running OpenCode with Tmux Subagent Support
273+
274+
To enable tmux subagent panes, OpenCode must run in **server mode** with the `--port` flag. This starts an HTTP server that subagent panes connect to via `opencode attach`.
275+
276+
**Basic setup**:
272277
```bash
273278
# Start tmux session
274279
tmux new -s dev
275280

276-
# Run OpenCode inside tmux
277-
opencode
281+
# Run OpenCode with server mode (port 4096)
282+
opencode --port 4096
278283

279284
# Now background agents will appear in separate panes
280285
```
281286

287+
**Recommended: Shell Function**
288+
289+
For convenience, create a shell function that automatically handles tmux sessions and port allocation. Here's an example for Fish shell:
290+
291+
```fish
292+
# ~/.config/fish/config.fish
293+
function oc
294+
set base_name (basename (pwd))
295+
set path_hash (echo (pwd) | md5 | cut -c1-4)
296+
set session_name "$base_name-$path_hash"
297+
298+
# Find available port starting from 4096
299+
function __oc_find_port
300+
set port 4096
301+
while test $port -lt 5096
302+
if not lsof -i :$port >/dev/null 2>&1
303+
echo $port
304+
return 0
305+
end
306+
set port (math $port + 1)
307+
end
308+
echo 4096
309+
end
310+
311+
set oc_port (__oc_find_port)
312+
set -x OPENCODE_PORT $oc_port
313+
314+
if set -q TMUX
315+
# Already inside tmux - just run with port
316+
opencode --port $oc_port $argv
317+
else
318+
# Create tmux session and run opencode
319+
set oc_cmd "OPENCODE_PORT=$oc_port opencode --port $oc_port $argv; exec fish"
320+
if tmux has-session -t "$session_name" 2>/dev/null
321+
tmux new-window -t "$session_name" -c (pwd) "$oc_cmd"
322+
tmux attach-session -t "$session_name"
323+
else
324+
tmux new-session -s "$session_name" -c (pwd) "$oc_cmd"
325+
end
326+
end
327+
328+
functions -e __oc_find_port
329+
end
330+
```
331+
332+
**Bash/Zsh equivalent**:
333+
334+
```bash
335+
# ~/.bashrc or ~/.zshrc
336+
oc() {
337+
local base_name=$(basename "$PWD")
338+
local path_hash=$(echo "$PWD" | md5sum | cut -c1-4)
339+
local session_name="${base_name}-${path_hash}"
340+
341+
# Find available port
342+
local port=4096
343+
while [ $port -lt 5096 ]; do
344+
if ! lsof -i :$port >/dev/null 2>&1; then
345+
break
346+
fi
347+
port=$((port + 1))
348+
done
349+
350+
export OPENCODE_PORT=$port
351+
352+
if [ -n "$TMUX" ]; then
353+
opencode --port $port "$@"
354+
else
355+
local oc_cmd="OPENCODE_PORT=$port opencode --port $port $*; exec $SHELL"
356+
if tmux has-session -t "$session_name" 2>/dev/null; then
357+
tmux new-window -t "$session_name" -c "$PWD" "$oc_cmd"
358+
tmux attach-session -t "$session_name"
359+
else
360+
tmux new-session -s "$session_name" -c "$PWD" "$oc_cmd"
361+
fi
362+
fi
363+
}
364+
```
365+
366+
**How subagent panes work**:
367+
368+
1. Main OpenCode starts HTTP server on specified port (e.g., `http://localhost:4096`)
369+
2. When a background agent spawns, Oh My OpenCode creates a new tmux pane
370+
3. The pane runs: `opencode attach http://localhost:4096 --session <session-id>`
371+
4. Each subagent pane shows real-time streaming output
372+
5. Panes are automatically closed when the subagent completes
373+
374+
**Environment variables**:
375+
376+
| Variable | Description |
377+
|----------|-------------|
378+
| `OPENCODE_PORT` | Default port for the HTTP server (used if `--port` not specified) |
379+
380+
### Server Mode Reference
381+
382+
OpenCode's server mode exposes an HTTP API for programmatic interaction:
383+
384+
```bash
385+
# Standalone server (no TUI)
386+
opencode serve --port 4096
387+
388+
# TUI with server (recommended for tmux integration)
389+
opencode --port 4096
390+
```
391+
392+
| Flag | Default | Description |
393+
|------|---------|-------------|
394+
| `--port` | `4096` | Port for HTTP server |
395+
| `--hostname` | `127.0.0.1` | Hostname to listen on |
396+
397+
For more details, see the [OpenCode Server documentation](https://opencode.ai/docs/server/).
398+
282399
## Git Master
283400

284401
Configure git-master skill behavior:

0 commit comments

Comments
 (0)