79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Matrix Bridge Daemon — connects Lucy's Matrix DM to the local agent API.
|
|
|
|
Polling loop that listens for new messages in a Matrix room, forwards them
|
|
to the AI agent framework, and sends responses back.
|
|
|
|
Usage:
|
|
python3 main.py # Normal mode
|
|
python3 main.py --config path/to.json # Custom config
|
|
python3 main.py --debug # Verbose logging
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
from config import Config
|
|
from bridge import Bridge
|
|
from api_server import start_api_server
|
|
|
|
|
|
def setup_logging(debug: bool = False) -> None:
|
|
"""Configure logging format and level."""
|
|
level = logging.DEBUG if debug else logging.INFO
|
|
logging.basicConfig(
|
|
level=level,
|
|
format="%(asctime)s [%(levelname)s] %(message)s",
|
|
handlers=[logging.StreamHandler(sys.stdout)],
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(
|
|
description="Matrix Bridge Daemon — connects Matrix DMs to the agent API",
|
|
)
|
|
parser.add_argument(
|
|
"--config",
|
|
default=os.path.join(os.path.dirname(__file__), "config.json"),
|
|
help="Path to configuration file (default: config.json)",
|
|
)
|
|
parser.add_argument(
|
|
"--debug",
|
|
action="store_true",
|
|
help="Enable verbose DEBUG-level logging",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
setup_logging(debug=args.debug)
|
|
log = logging.getLogger("matrix-bridge")
|
|
|
|
# Load configuration
|
|
try:
|
|
config = Config.load(args.config)
|
|
except Exception as e:
|
|
log.error(f"Failed to load config from {args.config}: {e}")
|
|
sys.exit(1)
|
|
|
|
# Create and run the bridge
|
|
bridge = Bridge(config)
|
|
|
|
# Start the API server (daemon thread, non-blocking)
|
|
start_api_server(bridge, config.bridge.api_host, config.bridge.api_port)
|
|
|
|
# Verify agent API is reachable
|
|
if bridge.agent._request("GET", "/api/sessions") is not None:
|
|
log.info("Agent API is reachable")
|
|
else:
|
|
log.warning("Agent API not reachable — will retry in main loop")
|
|
|
|
bridge.run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|