aboutsummaryrefslogtreecommitdiff
path: root/xmpp-cli-bridge/README.md
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2026-11-23 21:33:28 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2026-12-01 23:02:47 +0200
commitee5c222bf5d94fa993c44a6d0e1f53d82c962870 (patch)
treef5cd62d3abedba3865ecffcad39a6a6f18876fc3 /xmpp-cli-bridge/README.md
initial commitHEADmaster
Diffstat (limited to 'xmpp-cli-bridge/README.md')
-rw-r--r--xmpp-cli-bridge/README.md132
1 files changed, 132 insertions, 0 deletions
diff --git a/xmpp-cli-bridge/README.md b/xmpp-cli-bridge/README.md
new file mode 100644
index 0000000..dc6a274
--- /dev/null
+++ b/xmpp-cli-bridge/README.md
@@ -0,0 +1,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.