Files
semif-agent/semif_agent/bridges/registry.py
T
Denton Social ddc5cb66c8 bridges: standalone third-party API bridges; isolate the command gateway
Split messaging UX (invite links, read, send) out of the command gateway into a
new `semif_agent.bridges` layer. Each bridge is its own process (`cli bridge`)
against its own service daemon/profile; the SimpleX bridge owns a second
simplex-chat daemon and buffers every inbound DM, so a message sent to the
connect-link address shows up in `simplex.next_message`.

- new: bridges/{base,inbox,registry,simplex}.py + neutral semif_agent/simplex_ws.py
- gateway: delete bridge.py; drop address/observer/pull-mode from the command path
- codegen: inject the bridge catalog (describe_bridges) into body + elicitation prompts
- seeds: repoint simplex.next_message/connect_link to top-level `simplex_bridge_url`
- config/bootstrap: new top-level `bridges` block + `simplex_bridge_url`; render
  semif-simplex-forward + semif-bridge units; gateway config loses bridge/inbox
- docs: AGENTS.md gateway isolation is non-negotiable
2026-09-28 15:24:44 -05:00

112 lines
3.8 KiB
Python

"""The bridge catalog: discovery, prompt description, and standalone runner.
Every known bridge class is listed in `CATALOG`. `known_infos()` exposes their
static metadata, `describe_bridges()` renders the catalog block injected into
the code-generation prompts, and `run_bridges()` starts the enabled ones as a
standalone process (`python -m semif_agent.cli bridge`). Adding a bridge means
adding its class here and a `config.example.json` block; the gateway is never
touched.
"""
from __future__ import annotations
import threading
from .base import BridgeInfo, BridgeService
from .simplex import SimplexBridge
#: Known bridge services. Order is presentation order.
CATALOG: tuple[type[BridgeService], ...] = (SimplexBridge,)
def _class_for(name: str) -> type[BridgeService] | None:
for bridge_class in CATALOG:
if bridge_class.INFO.name == name:
return bridge_class
return None
def known_infos() -> list[BridgeInfo]:
return [bridge_class.INFO for bridge_class in CATALOG]
def describe_bridges() -> str:
"""A prompt block describing every available bridge service.
Injected into the skill-body and elicitation prompts so codegen knows which
real services it may build a skill against, how to reach each one, and which
`ctx.config` variable carries its URL. Static — no bridge needs to be
running or enabled.
"""
lines = [
"Available bridge services (call these over HTTP with urllib.request; "
"never speak a service's native protocol directly):",
]
for info in known_infos():
lines.append(f"- {info.name}: {info.description}")
lines.append(f" config var: {info.url_config_var}")
if info.config_vars:
other = [v for v in info.config_vars if v != info.url_config_var]
if other:
lines.append(f" other config vars: {', '.join(other)}")
for endpoint in info.endpoints:
lines.append(f" {endpoint}")
return "\n".join(lines) + "\n"
def build_bridge(config: dict, name: str, trace=None) -> BridgeService:
"""Instantiate one bridge from the top-level `bridges.<name>` config block."""
bridge_class = _class_for(name)
if bridge_class is None:
raise KeyError(f"unknown bridge: {name!r}")
block = (config.get("bridges", {}) or {}).get(name, {}) or {}
return bridge_class(block, trace=trace)
def run_bridges(
config: dict,
names: list[str] | None = None,
trace=None,
) -> int:
"""Run the selected (+ enabled) bridges until interrupted.
With explicit `names`, a bridge runs even if disabled is not set; with no
names, only `enabled` bridges run. Returns a process exit code.
"""
blocks = config.get("bridges", {}) or {}
selected: list[BridgeService] = []
for bridge_class in CATALOG:
name = bridge_class.INFO.name
if names and name not in names:
continue
block = blocks.get(name, {}) or {}
if not names and not block.get("enabled", False):
continue
bridge = bridge_class(block, trace=trace)
ok, hint = bridge.check_requirements()
if not ok:
print(f"bridge {name} unavailable: {hint}")
for running in selected:
running.stop()
return 1
port = bridge.start()
selected.append(bridge)
print(
f"bridge {name} listening on http://{bridge.host}:{port} "
f"(skills use the {bridge_class.INFO.url_config_var} config var)"
)
if not selected:
print("no bridge services enabled (set bridges.<name>.enabled)")
return 1
stop = threading.Event()
try:
while not stop.wait(0.5):
pass
except KeyboardInterrupt:
pass
finally:
for bridge in selected:
bridge.stop()
return 0