aboutsummaryrefslogtreecommitdiff
path: root/xmpp-cli-bridge/xmpp_cli_bridge/agent.py
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/xmpp_cli_bridge/agent.py
initial commitHEADmaster
Diffstat (limited to 'xmpp-cli-bridge/xmpp_cli_bridge/agent.py')
-rw-r--r--xmpp-cli-bridge/xmpp_cli_bridge/agent.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/xmpp-cli-bridge/xmpp_cli_bridge/agent.py b/xmpp-cli-bridge/xmpp_cli_bridge/agent.py
new file mode 100644
index 0000000..a988320
--- /dev/null
+++ b/xmpp-cli-bridge/xmpp_cli_bridge/agent.py
@@ -0,0 +1,69 @@
+"""xmpp-agent-bridge --jid JID [--allow JID]... [--agent-dir DIR]
+
+Expose the coding agent at ~/Dev/agent (agent.py) as an XMPP bot: each
+chat message becomes one agent input line, and the agent's finished turn
+is sent back as the reply. Run it from the directory the agent should
+work in (agent.py defaults its --workdir to the child's cwd).
+"""
+from __future__ import annotations
+
+import argparse
+import os
+import re
+import sys
+from pathlib import Path
+
+from slixmpp import JID
+
+from . import serve
+from .__main__ import make_handler
+
+# agent.py's prompt: "> ", or "(N) > " with --show-token-usage (also
+# enabled by the inherited LLM_SHOW_TOKEN_USAGE env var). re.search is
+# leftmost, so a reply containing a bare "> " line only delays it to
+# the max-wait timeout rather than cutting it short.
+PROMPT = re.compile(r"(?:\(\d+\) )?> $")
+
+MAX_WAIT = 600.0 # an agent turn spans several LLM calls
+
+
+def main(argv: list[str] | None = None) -> None:
+ p = argparse.ArgumentParser(
+ description="Expose the ~/Dev/agent coding agent over XMPP."
+ )
+ p.add_argument("--jid", required=True)
+ p.add_argument("--password", default=os.environ.get("XMPP_PASSWORD"))
+ p.add_argument("--allow", action="append", default=[], metavar="JID",
+ help="sender bare JID allowed to interact (repeatable)")
+ p.add_argument("--agent-dir", type=Path,
+ default=Path("~/Dev/agent").expanduser(),
+ help="directory containing agent.py and its venv "
+ "(default: ~/Dev/agent)")
+ p.add_argument("--max-wait", type=float, default=MAX_WAIT, metavar="SECONDS",
+ help="overall reply timeout (default %g)" % MAX_WAIT)
+ args = p.parse_args(argv)
+ if not args.password:
+ p.error("--password or XMPP_PASSWORD required")
+ if not (args.agent_dir / "agent.py").is_file():
+ p.error(f"no agent.py in {args.agent_dir}")
+
+ python = args.agent_dir / "bin" / "python3"
+ cmd = [
+ str(python if python.exists() else sys.executable),
+ str(args.agent_dir / "agent.py"),
+ # permission prompts read stdin, which the bridge feeds only one
+ # line per reply -- they would deadlock, so approve everything
+ "--trust-level", "4",
+ ]
+
+ # default deny: only the bridge account's own bare JID
+ allowed = {JID(j).bare for j in args.allow} or {JID(args.jid).bare}
+ serve(
+ args.jid,
+ args.password,
+ make_handler(cmd, allowed, PROMPT, args.max_wait),
+ )
+
+
+if __name__ == "__main__":
+ main()