diff --git a/AGENTS.md b/AGENTS.md index e7c5792..ab62234 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -734,9 +734,9 @@ CLI, unit tests (24) + integration tests (2). `codegen.timeout: 3600`, the SimpleX gateway enabled (`gateway.simplex.enabled = true`, `ws_url` from `simplex_chat.port`) and the standalone forwarding bridge enabled (`bridges.simplex.enabled = true`, - `ws_url` from `simplex_chat.forward_port`, top-level `simplex_bridge_url`) and - the LLM bridge enabled (`bridges.llm.enabled = true`, top-level - `llm_bridge_url`; it reuses the `llm` model); + `ws_url` from `simplex_chat.forward_port`) and + the LLM bridge enabled (`bridges.llm.enabled = true`; it reuses the `llm` + model); the operator sets `llm.model`/`llm.base_url` and `codegen.model`/ `codegen.base_url`. Bootstrap renders/enables four user services: `semif-simplex.service` (the command `simplex-chat` bot daemon, @@ -1273,9 +1273,13 @@ CLI, unit tests (24) + integration tests (2). (surviving bridge restarts), unlike the live `/inbox` buffer; both are read-only because v7 has no mark-read command (acking is a client-side read receipt). -- **Skill-facing config.** The top-level `simplex_bridge_url` is the address a - body calls (the data-contract config search auto-populates it); bootstrap keeps - it in sync with the bridge port. Skills reach a bridge with the ordinary `http` +- **Skill-facing config.** A body calls a bridge at the address derived from that + bridge's own `bridges.` block (`host`/`port`/`token`) — the + `simplex_bridge_url` / `llm_bridge_url` / `nextcloud_bridge_url` var (+ `_token`) + is injected into `ctx.config` by `build_scheduler` via + `bridges.registry.derived_config_vars`, so there is no duplicate top-level key to + keep in sync and the data-contract config search finds it and auto-populates the + skill config. Skills reach a bridge with the ordinary `http` transport, so their hermetic tests are loopback HTTP like any other HTTP body. `simplex.next_message` and `simplex.connect_link` are the seeds. - **LLM bridge** (`bridges/llm.py`). A generic `POST /chat` @@ -1287,7 +1291,7 @@ CLI, unit tests (24) + integration tests (2). scheduler's already-configured `llm` client through `run_bridges`/`build_bridge` into `LLMBridge`, so the endpoint/model/sampler stay configured once in the top-level `llm` block; the `bridges.llm` block carries only the local listener - (`enabled`/`host`/`port`/`token`). Bodies reach it via the top-level + (`enabled`/`host`/`port`/`token`). Bodies reach it via the derived `llm_bridge_url` (+ optional `llm_bridge_token`). It runs inside the existing `semif-bridge.service` process (all enabled bridges start together); with no client it reports `502`, never a fabricated reply. `calendar.create_event` is @@ -1306,7 +1310,7 @@ CLI, unit tests (24) + integration tests (2). `nextcloud_url`/`nextcloud_username`/`nextcloud_app_password`/ `nextcloud_default_calendar`/`nextcloud_default_addressbook` as a fallback, so the same account the calendar seeds collect is configured once and a block value - wins. Bodies reach it via the top-level `nextcloud_bridge_url` (+ optional + wins. Bodies reach it via the derived `nextcloud_bridge_url` (+ optional `nextcloud_bridge_token`); the `BridgeInfo` declares only those two config vars. Routes (reads `GET`, mutations `POST`): files (`/files`, `/files/stat`, `/files/read` text-or-base64, `/files/search`, `/files/write|mkdir|delete|move|copy`), @@ -1357,7 +1361,7 @@ The bridge read path (`simplex.next_message`) and contact-link lookup - [x] **4. LLM bridge** (`bridges/llm.py`). Shipped: `LLMBridge` exposes a generic `POST /chat` in front of the scheduler's configured `llm` client (threaded through `run_bridges`/`build_bridge`, never re-configured), registered - in `CATALOG` and seeded under `bridges.llm` + top-level `llm_bridge_url`. + in `CATALOG` and seeded under `bridges.llm` (its URL is derived from that block). `calendar.create_event` extracts its title + description through it. - [x] **5. More bridges.** Each new third-party API gets its own `bridges/.py` + config block + `CATALOG` entry; the codegen prompts pick it up automatically diff --git a/README.md b/README.md index dc9364f..7724345 100644 --- a/README.md +++ b/README.md @@ -513,14 +513,17 @@ Key blocks: | `gateway.simplex` | command gateway: ws_url, allowlist, batching | | `gateway.voice` | voice gateway: wake/STT/TTS engines + models, audio devices, VAD, follow-up window | | `bridges.simplex` | forwarding bridge: host/port/token/ws_url | -| `simplex_bridge_url` / `simplex_bridge_token` | what skill bodies call | | `bridges.llm` | LLM bridge: host/port/token (model comes from `llm`) | -| `llm_bridge_url` / `llm_bridge_token` | what skill bodies call for generation | | `bridges.nextcloud` | Nextcloud bridge: host/port/token + connection (url/username/app_password/defaults) | -| `nextcloud_bridge_url` / `nextcloud_bridge_token` | what skill bodies call for Nextcloud | | `simplex_chat` | gateway/forward ports and bot display names (binary refs come from `pins.json`) | | `dashboard` | bind host/port | +Bridge skills read `simplex_bridge_url` / `llm_bridge_url` / `nextcloud_bridge_url` +(+ their `_token`) from `ctx.config`; each is **derived** at startup from its +`bridges.` host/port/token, so there is no duplicate top-level key to keep +in sync. A bridge with no block (or no port) leaves the variable unresolved and +the operator is asked for it. + `llm.model` and `codegen.model` are **required** (no default): set each to a model its endpoint serves. A missing model fails fast at startup with a clear config error rather than sending a request to a model that isn't there. @@ -710,7 +713,8 @@ so they see messages that arrived while the bridge was down — unlike the live `/inbox` receive buffer. Both are read-only: v7 has no mark-read command, so acking is a client-side read receipt, not an API call. -`simplex_bridge_url` (top-level config) is what skills call; the bridge catalog +Skills call the forwarding bridge at the address derived from `bridges.simplex` +(`simplex_bridge_url`); the bridge catalog is injected into every codegen prompt via `describe_bridges()`, so it is the single source of bridge specifics. Adding a bridge = a class + a config block + a `CATALOG` entry. @@ -726,10 +730,10 @@ text (e.g. extract an event title + description) over HTTP instead of speaking the OpenAI-compatible protocol itself. It does not own the model connection: `cli bridge` threads the scheduler's already-configured `llm` client into it, so the endpoint/model/sampler live only in the top-level `llm` block; `bridges.llm` -carries just the local listener. Skills call it via `llm_bridge_url` -(+ optional `llm_bridge_token`); with no client it returns `502`, never a -fabricated reply. It starts in the same `semif-bridge.service` process as the -other enabled bridges. +carries just the local listener. Skills call it at the address derived from +`bridges.llm` (`llm_bridge_url`, + optional `llm_bridge_token`); with no client +it returns `502`, never a fabricated reply. It starts in the same +`semif-bridge.service` process as the other enabled bridges. The **Nextcloud bridge** (`semif_agent/bridges/nextcloud.py`) is the third: it wraps the account's native protocols — WebDAV files, CalDAV calendar events and @@ -740,8 +744,8 @@ in the same `semif-bridge` process. The connection (base URL, username, app password) is configured in `bridges.nextcloud` and falls back to the top-level `nextcloud_url` / `nextcloud_username` / `nextcloud_app_password` / `nextcloud_default_calendar` the calendar seeds use, so the account is -configured once. Skills call it via `nextcloud_bridge_url` (+ optional -`nextcloud_bridge_token`). Its HTTP surface: +configured once. Skills call it at the address derived from `bridges.nextcloud` +(`nextcloud_bridge_url`, + optional `nextcloud_bridge_token`). Its HTTP surface: ``` GET /health diff --git a/config.example.json b/config.example.json index 6b01190..f221871 100644 --- a/config.example.json +++ b/config.example.json @@ -117,12 +117,6 @@ }, "skill_bodies": "data/skills", "skill_seeds": "seeds", - "simplex_bridge_url": "http://127.0.0.1:5227", - "simplex_bridge_token": "", - "llm_bridge_url": "http://127.0.0.1:5229", - "llm_bridge_token": "", - "nextcloud_bridge_url": "http://127.0.0.1:5230", - "nextcloud_bridge_token": "", "log": "data/decisions.jsonl", "trace": "data/runs.jsonl", "category_registry": "data/categories.json", @@ -136,7 +130,7 @@ "bridges": { "_comment": "Standalone third-party API bridges: a simple, secure local HTTP layer between codegen-authored skill bodies and real system services. Each bridge runs as its own process (`python -m semif_agent.cli bridge [--name NAME]`), one process per service daemon, and never runs inside the command gateway — gateway isolation is non-negotiable (see AGENTS.md). The bridge catalog (names, endpoints, config vars) is injected into the code-generation prompts via `semif_agent.bridges.describe_bridges()`.", "simplex": { - "_comment": "SimpleX forwarding bridge: buffers every inbound DM (no allowlist — the point is to see who reached the bot through its invite link), and serves invite-link / read / send over HTTP. It owns its own simplex-chat daemon/profile (`ws_url`, `forward_port` above), separate from the command gateway's. Skills use the top-level `simplex_bridge_url`; keep it in sync with host/port. Binds localhost only; an optional `token` is required in the X-Semif-Token header when set.", + "_comment": "SimpleX forwarding bridge: buffers every inbound DM (no allowlist — the point is to see who reached the bot through its invite link), and serves invite-link / read / send over HTTP. It owns its own simplex-chat daemon/profile (`ws_url`, `forward_port` above), separate from the command gateway's. Skills call it at http://host:port (derived from this block; no top-level duplicate). Binds localhost only; an optional `token` is required in the X-Semif-Token header when set.", "enabled": true, "host": "127.0.0.1", "port": 5227, @@ -148,14 +142,14 @@ "reconnect": {"initial": 1.0, "max": 60.0, "jitter": 0.2} }, "llm": { - "_comment": "LLM bridge: a generic `POST /chat` in front of the agent's configured `llm` model, so skill bodies can ask for short generated text (e.g. an event title + description) over HTTP instead of speaking the OpenAI-compatible protocol. It reuses the top-level `llm` client (endpoint/model/sampler configured once there); this block only carries the local listener. Skills use the top-level `llm_bridge_url`; keep it in sync with host/port. Binds localhost only; an optional `token` is required in the X-Semif-Token header when set. It runs inside the same `semif-bridge.service` process as the other enabled bridges.", + "_comment": "LLM bridge: a generic `POST /chat` in front of the agent's configured `llm` model, so skill bodies can ask for short generated text (e.g. an event title + description) over HTTP instead of speaking the OpenAI-compatible protocol. It reuses the top-level `llm` client (endpoint/model/sampler configured once there); this block only carries the local listener. Skills call it at http://host:port (derived from this block; no top-level duplicate). Binds localhost only; an optional `token` is required in the X-Semif-Token header when set. It runs inside the same `semif-bridge.service` process as the other enabled bridges.", "enabled": true, "host": "127.0.0.1", "port": 5229, "token": "" }, "nextcloud": { - "_comment": "Nextcloud bridge: files (WebDAV), calendar events and tasks (CalDAV), contacts (CardDAV), and Notes (the Notes app) over one uniform local JSON API, so skill bodies never speak the native DAV/OCS protocols. It has no service daemon (Nextcloud is plain HTTPS) and runs inside the same `semif-bridge` process as the other enabled bridges. The connection is configured HERE (url/username/app_password); when a value is blank the bridge falls back to the top-level nextcloud_url/nextcloud_username/nextcloud_app_password/nextcloud_default_calendar the calendar seeds already use, so the account is configured once. `app_password` is a Nextcloud app password (Settings > Security > Devices & sessions), not the account password. `default_calendar`/`default_addressbook` are used when a request names none and the account has several. `verify_tls` false skips certificate verification (self-signed hosts). Skills use the top-level `nextcloud_bridge_url`; keep it in sync with host/port. Binds localhost only; an optional `token` is required in the X-Semif-Token header when set.", + "_comment": "Nextcloud bridge: files (WebDAV), calendar events and tasks (CalDAV), contacts (CardDAV), and Notes (the Notes app) over one uniform local JSON API, so skill bodies never speak the native DAV/OCS protocols. It has no service daemon (Nextcloud is plain HTTPS) and runs inside the same `semif-bridge` process as the other enabled bridges. The connection is configured HERE (url/username/app_password); when a value is blank the bridge falls back to the top-level nextcloud_url/nextcloud_username/nextcloud_app_password/nextcloud_default_calendar the calendar seeds already use, so the account is configured once. `app_password` is a Nextcloud app password (Settings > Security > Devices & sessions), not the account password. `default_calendar`/`default_addressbook` are used when a request names none and the account has several. `verify_tls` false skips certificate verification (self-signed hosts). Skills call it at http://host:port (derived from this block; no top-level duplicate). Binds localhost only; an optional `token` is required in the X-Semif-Token header when set.", "enabled": true, "host": "127.0.0.1", "port": 5230, diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index 94b7735..31f2eb5 100755 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -616,21 +616,21 @@ SimpleX forwarding bridge (messaging UX for skills): and 'semif-bridge' (the standalone bridge API). Its contact address is printed above; re-print with: "$PYTHON" scripts/simplex-address.py --ws-url ws://127.0.0.1:$SIMPLEX_FORWARD_PORT - - Skills reach it over HTTP via the top-level simplex_bridge_url - (default http://127.0.0.1:5227); simplex.connect_link shows its address and - simplex.next_message reads messages sent to it. + - Skills reach it over HTTP at the address derived from bridges.simplex + (host/port, default http://127.0.0.1:5227); simplex.connect_link shows its + address and simplex.next_message reads messages sent to it. LLM bridge (generation for skills): - Runs inside the same 'semif-bridge' process. It reuses the top-level `llm` - model (configured once) and serves a generic POST /chat; skills call it via - the top-level llm_bridge_url (default http://127.0.0.1:5229). + model (configured once) and serves a generic POST /chat; skills call it at + the address derived from bridges.llm (default http://127.0.0.1:5229). Nextcloud bridge (files/calendar/tasks/contacts/notes for skills): - Runs inside the same 'semif-bridge' process (Nextcloud is plain HTTPS, so there is no separate daemon). Configure the account once in config.json under bridges.nextcloud (url/username/app_password) — or reuse the top-level - nextcloud_* values the calendar seeds collect. Skills call it via the - top-level nextcloud_bridge_url (default http://127.0.0.1:5230). + nextcloud_* values the calendar seeds collect. Skills call it at the address + derived from bridges.nextcloud (default http://127.0.0.1:5230). EOF if [[ "$VOICE" = 1 ]]; then diff --git a/semif_agent/bridges/__init__.py b/semif_agent/bridges/__init__.py index 1599454..78285b9 100644 --- a/semif_agent/bridges/__init__.py +++ b/semif_agent/bridges/__init__.py @@ -26,6 +26,7 @@ from .base import BridgeInfo, BridgeService from .inbox import MessagingInbox from .registry import ( build_bridge, + derived_config_vars, describe_bridges, known_infos, run_bridges, @@ -36,6 +37,7 @@ __all__ = [ "BridgeService", "MessagingInbox", "build_bridge", + "derived_config_vars", "describe_bridges", "known_infos", "run_bridges", diff --git a/semif_agent/bridges/registry.py b/semif_agent/bridges/registry.py index 366d120..e25328e 100644 --- a/semif_agent/bridges/registry.py +++ b/semif_agent/bridges/registry.py @@ -32,6 +32,35 @@ def known_infos() -> list[BridgeInfo]: return [bridge_class.INFO for bridge_class in CATALOG] +def derived_config_vars(config: dict) -> dict[str, str]: + """Skill-facing config vars for every bridge, derived from its own block. + + The `bridges.` block is the single source of truth: its `host`/`port` + are the listener a skill body calls and its `token` is the shared secret. + There is deliberately no duplicate top-level `*_bridge_url`/`*_bridge_token` + to keep in sync — the values are derived here and injected into the config a + skill reads (`ctx.config`). A bridge with no block (or no port) contributes + nothing, so an unconfigured bridge still surfaces as an unresolved contract + variable the operator is asked for. + """ + blocks = config.get("bridges", {}) or {} + derived: dict[str, str] = {} + for info in known_infos(): + block = blocks.get(info.name) + if not isinstance(block, dict): + continue + host = str(block.get("host") or "127.0.0.1").strip() + # A bind-all address is not a callable one; skills always call loopback. + if host in ("", "0.0.0.0", "::", "[::]"): + host = "127.0.0.1" + port = block.get("port") + if info.url_config_var and port: + derived[info.url_config_var] = f"http://{host}:{int(port)}" + if info.auth_config_var: + derived[info.auth_config_var] = str(block.get("token") or "") + return derived + + def describe_bridges() -> str: """A prompt block describing every available bridge service. diff --git a/semif_agent/cli.py b/semif_agent/cli.py index f5b99f4..5faab4d 100644 --- a/semif_agent/cli.py +++ b/semif_agent/cli.py @@ -313,6 +313,13 @@ def build_llm_client(config: dict, engine): def build_scheduler(config: dict) -> tuple[Scheduler, dict]: + # Bridge listener addresses/tokens are derived from each bridge's own + # `bridges.` block (host/port/token) — the single source of truth. + # There is no duplicate top-level *_bridge_url to drift; skills read these + # from ctx.config and the contract config search finds them here. + from .bridges.registry import derived_config_vars + + config = {**config, **derived_config_vars(config)} engine = build_engine(config) llm = build_llm_client(config, engine) log = DecisionLog(config.get("log", "data/decisions.jsonl")) diff --git a/tests/test_bridges.py b/tests/test_bridges.py index 3a2e34c..f2e17f9 100644 --- a/tests/test_bridges.py +++ b/tests/test_bridges.py @@ -15,7 +15,11 @@ import urllib.request import pytest from semif_agent.bridges.llm import LLMBridge -from semif_agent.bridges.registry import describe_bridges, known_infos +from semif_agent.bridges.registry import ( + derived_config_vars, + describe_bridges, + known_infos, +) from semif_agent.bridges.simplex import SimplexBridge @@ -674,6 +678,39 @@ def test_every_bridge_documents_its_config_vars_and_auth(): assert info.auth_config_var in info.config_vars, info.name +def test_derived_config_vars_come_from_each_bridge_block(): + config = { + "bridges": { + "simplex": {"host": "127.0.0.1", "port": 5227, "token": "s"}, + "llm": {"host": "127.0.0.1", "port": 5229, "token": ""}, + "nextcloud": {"host": "127.0.0.1", "port": 5230, "token": "n"}, + } + } + derived = derived_config_vars(config) + assert derived["simplex_bridge_url"] == "http://127.0.0.1:5227" + assert derived["simplex_bridge_token"] == "s" + assert derived["llm_bridge_url"] == "http://127.0.0.1:5229" + assert derived["llm_bridge_token"] == "" + assert derived["nextcloud_bridge_url"] == "http://127.0.0.1:5230" + assert derived["nextcloud_bridge_token"] == "n" + + +def test_derived_config_vars_normalize_bind_all_host(): + config = {"bridges": {"llm": {"host": "0.0.0.0", "port": 5229}}} + assert derived_config_vars(config)["llm_bridge_url"] == "http://127.0.0.1:5229" + + +def test_derived_config_vars_skip_unconfigured_bridges(): + # No block at all: nothing derived, so the contract variable stays + # unresolved and the operator is asked for it. + assert derived_config_vars({}) == {} + # A block without a port is not a callable address: no URL, but the token + # var is still satisfiable (empty = no auth). + derived = derived_config_vars({"bridges": {"nextcloud": {"token": ""}}}) + assert "nextcloud_bridge_url" not in derived + assert derived["nextcloud_bridge_token"] == "" + + def test_describe_bridges_renders_service_auth_and_endpoints(): text = describe_bridges() assert "simplex" in text