"""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()