1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# xmpp-cli-bridge
Bridge XMPP chat messages to a CLI application or a Python handler.
Each incoming chat message is written to the child process's stdin as one line;
whatever it prints back is sent as the XMPP reply. Messages from senders not on
an allowlist are ignored.
## Install
```sh
pip install .
# or, to just run it:
uv run xmpp-cli-bridge ...
```
Requires Python 3.9+ and a working `slixmpp` install.
## Usage
```sh
xmpp-cli-bridge --jid BOTJID [--password PW] [--allow JID]...
[--prompt REGEX] [--max-wait SECONDS]
CMD [ARGS...]
```
- `--jid` / `--password` — XMPP account for the bot. The password can be given
via the `XMPP_PASSWORD` environment variable instead.
- `--allow JID` — bare JID allowed to interact; repeatable. Default: only the
bot's own bare JID (i.e. deny everyone).
- `--prompt REGEX` — reply ends when stdout ends with a line matching REGEX
(for REPL-style programs). Without it, a reply ends after 0.5 s of silence.
- `--max-wait SECONDS` — overall reply timeout, default 30.
- `-- CMD ARGS...` — the program to expose, run directly (never via a shell).
The bridge blocks until Ctrl-C. Messages are handled one at a time, so a slow
or interactive command still behaves like a normal terminal conversation.
## Examples
Echo everything back (`cat` is line-based and prints no prompt, so quiet mode
is fine):
```sh
XMPP_PASSWORD=hunter2 xmpp-cli-bridge \
--jid bot@xmpp.example --allow me@xmpp.example -- cat
```
Expose a Python REPL: send `2 + 2` from `me@xmpp.example`, get `4` back. The
`^>>> $` prompt tells the bridge a reply is complete, and `-q` skips the
startup banner:
```sh
xmpp-cli-bridge --jid bot@xmpp.example --allow me@xmpp.example \
--prompt '^>>> $' -- python3 -i -q
```
Multiple allowed senders, longer timeout for a slow program:
```sh
xmpp-cli-bridge --jid bot@xmpp.example \
--allow alice@xmpp.example --allow bob@xmpp.example \
--max-wait 120 -- ./my-interactive-tool
```
## Library use
`serve(jid, password, handler)` logs in and dispatches messages to your
handler. The handler receives `(body, sender_jid)` and returns the reply text,
or `None` to stay silent. It runs in a worker thread, so it may block:
```python
from xmpp_cli_bridge import serve
def handler(body, sender):
if "hello" in body.lower():
return "hi!"
return None # no reply
serve("bot@xmpp.example", "hunter2", handler)
```
`xmpp_cli_bridge.__main__.make_handler(argv, allowed, prompt=None, max_wait=30)`
builds the same handler the CLI uses, if you want to compose the two.
## Coding-agent wrapper
`xmpp-agent-bridge` exposes the coding agent in `~/Dev/agent` (`agent.py`) as
an XMPP bot: each chat message becomes one agent input line, and the agent's
finished turn (intermediate responses included) is sent back as the reply.
Run it from the directory the agent should work in:
```sh
cd ~/Dev/my-project
XMPP_PASSWORD=hunter2 xmpp-agent-bridge \
--jid bot@xmpp.example --allow me@xmpp.example
```
- `--agent-dir DIR` — where `agent.py` and its virtualenv live (default
`~/Dev/agent`); the agent is run with `DIR/bin/python3` when present.
- `--max-wait SECONDS` — overall turn timeout, default 600 (a turn spans
several LLM calls).
The agent runs with `--trust-level 4`, i.e. every action, including shell
commands, is auto-approved: the bridge protocol feeds the child one line per
reply, so the agent's interactive permission prompts would deadlock it. The
allowlist and the bot account's password are the only guardrails.
## Behavior details
- One-line logs go to stdout (connections, messages sent/received, child
commands, reply timings, timeout dumps) so stalled replies can be diagnosed.
- ANSI escape sequences (terminal colors) are stripped from replies.
- Control characters that are illegal in XML (ESC, NUL, ...) are stripped
before sending; passing raw binary through would kill the XMPP stream.
- Groupchat and error messages are ignored; only 1:1 chats are handled.
- If the child exits, the reply is `process exited (N)`.
- The child's startup prompt is consumed at launch, and empty replies (a bare
prompt or an empty turn) are not sent at all.
- Only the text *before* the matched prompt is sent as the reply. To relay
`input()`-style questions (e.g. permission prompts), match only the prompt's
tail (`\? \[y/N\] $`), not the whole line (`.*\[y/N\] $`): the latter makes
the question itself the match, leaving an empty, suppressed reply.
## Tests
```sh
pytest
```
The tests cover the stdin/stdout plumbing and message handling without an
XMPP server.
|