gateway: run simplex-chat in bot mode, not relay/headless
In v7 --headless means "chat relay" (requires --relay) and yields a relay address, not a user contact address, so the bot could not be added as a contact. Run bot mode instead (-p PORT --create-bot-display-name), which needs no TTY and is restart-safe. simplex-address.py now creates the user contact address on first run (/_address + /_show_address), and bootstrap invokes it after the services start and prints the link.
This commit is contained in:
1 parent
5ee3905aec
commit
6059bec1e4
4 files changed
+66
-28
No files matched your search
@@ -378,13 +378,14 @@ unit tests (24) + box integration tests (2).
|
|||||||
`data/` for continuity; `--public-dashboard` binds the dashboard to `0.0.0.0`.
|
`data/` for continuity; `--public-dashboard` binds the dashboard to `0.0.0.0`.
|
||||||
It also enables the SimpleX gateway (`gateway.simplex.enabled = true`,
|
It also enables the SimpleX gateway (`gateway.simplex.enabled = true`,
|
||||||
`ws_url` from `simplex_chat.port`) and renders/enables the two user services
|
`ws_url` from `simplex_chat.port`) and renders/enables the two user services
|
||||||
`semif-simplex.service` (the pinned `simplex-chat` daemon, `--relay
|
`semif-simplex.service` (the pinned `simplex-chat` bot daemon,
|
||||||
--headless` on `simplex_chat.port` — v7 requires `--relay` for `--headless`)
|
`--create-bot-display-name` on `simplex_chat.port`) and `semif-gateway.service`
|
||||||
and `semif-gateway.service`
|
|
||||||
(`.runtime/venv/bin/python -m semif_agent.cli gateway`). `--simplex-allowed-users
|
(`.runtime/venv/bin/python -m semif_agent.cli gateway`). `--simplex-allowed-users
|
||||||
CSV` / `--simplex-home-channel ID` / `--simplex-display-name NAME` populate
|
CSV` / `--simplex-home-channel ID` / `--simplex-display-name NAME` populate
|
||||||
the allowlist/fallback/identity; with an empty allowlist the gateway denies
|
the allowlist/fallback/identity; with an empty allowlist the gateway denies
|
||||||
everyone (the safe default until the human adds their contact id).
|
everyone (the safe default until the human adds their contact id). Bootstrap
|
||||||
|
then runs `scripts/simplex-address.py` to create/print the bot's contact
|
||||||
|
address.
|
||||||
- Run / verify on jarvis:
|
- Run / verify on jarvis:
|
||||||
```sh
|
```sh
|
||||||
REPO=~/repos/semif-agent && cd "$REPO" && export HF_HOME="$REPO/.runtime/hf"
|
REPO=~/repos/semif-agent && cd "$REPO" && export HF_HOME="$REPO/.runtime/hf"
|
||||||
@@ -576,16 +577,20 @@ unit tests (24) + box integration tests (2).
|
|||||||
co-serves the browser UI from a daemon thread. Config lives under
|
co-serves the browser UI from a daemon thread. Config lives under
|
||||||
`gateway.simplex` in `config.json`; `enabled` defaults false. On a
|
`gateway.simplex` in `config.json`; `enabled` defaults false. On a
|
||||||
bootstrap-provisioned box `scripts/bootstrap.sh` renders and enables the
|
bootstrap-provisioned box `scripts/bootstrap.sh` renders and enables the
|
||||||
`semif-simplex.service` daemon (pinned `simplex-chat`, profile under
|
`semif-simplex.service` bot daemon (pinned `simplex-chat` in **bot mode**,
|
||||||
`.runtime/simplex/`, port from `simplex_chat.port`) and the
|
profile under `.runtime/simplex/`, port from `simplex_chat.port`) and the
|
||||||
`semif-gateway.service` agent process, so the gateway runs headless across
|
`semif-gateway.service` agent process, so the gateway runs across
|
||||||
logout/reboot. The gateway is its own process — the REPL and the gateway are
|
logout/reboot. The gateway is its own process — the REPL and the gateway are
|
||||||
independent front ends onto the same on-disk logs/registry (do not run two
|
independent front ends onto the same on-disk logs/registry (do not run two
|
||||||
scheduler processes over one skill store concurrently).
|
scheduler processes over one skill store concurrently).
|
||||||
- **Bot address.** `python scripts/simplex-address.py` queries the running
|
- **Bot address / v7 relay gotcha.** The daemon must NOT run with `--headless`
|
||||||
daemon for the bot's contact link (`/_show_address <userId>`), which a human
|
or `--relay`: in SimpleX Chat v7 `--headless` means "chat relay" (requires
|
||||||
adds in their SimpleX app to start a DM. `bootstrap.sh` prints this hint; the
|
`--relay`) and yields a *relay* address, not a user contact address. Run bot
|
||||||
address is created automatically on the daemon's first `--headless` run.
|
mode instead (`simplex-chat -p PORT --create-bot-display-name NAME`), which
|
||||||
|
needs no TTY and is restart-safe. `python scripts/simplex-address.py` then
|
||||||
|
shows (creating on first run, via `/_address` / `/_show_address <userId>`)
|
||||||
|
the bot's user contact link, which a human adds in their SimpleX app to start
|
||||||
|
a DM. `bootstrap.sh` prints it at the end of provisioning.
|
||||||
- **Transport contract** (`gateway/base.py`): `GatewayAdapter.run(on_inbound,
|
- **Transport contract** (`gateway/base.py`): `GatewayAdapter.run(on_inbound,
|
||||||
outbound_queue)` blocks, delivering `InboundMessage`s and draining a stdlib
|
outbound_queue)` blocks, delivering `InboundMessage`s and draining a stdlib
|
||||||
`queue.Queue[OutboundMessage | None]`. Scheduler work is synchronous and can
|
`queue.Queue[OutboundMessage | None]`. Scheduler work is synchronous and can
|
||||||
|
|||||||
+22
-2
@@ -319,6 +319,25 @@ if command -v systemctl >/dev/null 2>&1; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# --- stage 8b: SimpleX bot contact address -------------------------------------------
|
||||||
|
SIMPLEX_ADDRESS=""
|
||||||
|
if command -v systemctl >/dev/null 2>&1; then
|
||||||
|
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
|
||||||
|
if systemctl --user is-active --quiet semif-simplex.service 2>/dev/null; then
|
||||||
|
echo "== ensuring the SimpleX bot contact address exists"
|
||||||
|
for _ in 1 2 3 4 5 6 7 8 9 10; do
|
||||||
|
if SIMPLEX_ADDRESS="$("$PYTHON" "$REPO_ROOT/scripts/simplex-address.py" 2>/dev/null)"; then
|
||||||
|
printf '%s\n' "$SIMPLEX_ADDRESS" | sed 's/^/ /'
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
if [[ -z "$SIMPLEX_ADDRESS" ]]; then
|
||||||
|
echo "WARN: bot address not ready; run: $PYTHON scripts/simplex-address.py" >&2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# --- stage 9: optional data copy -----------------------------------------------------
|
# --- stage 9: optional data copy -----------------------------------------------------
|
||||||
if [[ -n "$COPY_DATA" ]]; then
|
if [[ -n "$COPY_DATA" ]]; then
|
||||||
echo "== rsync data from $COPY_DATA"
|
echo "== rsync data from $COPY_DATA"
|
||||||
@@ -365,12 +384,13 @@ Done. Next steps:
|
|||||||
HF_HOME="$HF_CACHE" "$PYTHON" -m semif_agent.cli dashboard --port 8765
|
HF_HOME="$HF_CACHE" "$PYTHON" -m semif_agent.cli dashboard --port 8765
|
||||||
|
|
||||||
SimpleX gateway:
|
SimpleX gateway:
|
||||||
- The bot runs as systemd user services 'semif-simplex' (daemon, port $SIMPLEX_PORT) and
|
- The bot runs as systemd user services 'semif-simplex' (bot daemon, port $SIMPLEX_PORT) and
|
||||||
'semif-gateway' (agent). Check them with:
|
'semif-gateway' (agent). Check them with:
|
||||||
systemctl --user status semif-simplex semif-gateway
|
systemctl --user status semif-simplex semif-gateway
|
||||||
journalctl --user -u semif-gateway -f
|
journalctl --user -u semif-gateway -f
|
||||||
- Show the bot's contact address (add it as a contact in your SimpleX app):
|
- Its user contact address is printed above; re-print (creating if needed) with:
|
||||||
"$PYTHON" scripts/simplex-address.py
|
"$PYTHON" scripts/simplex-address.py
|
||||||
|
Add that address as a contact in your SimpleX app.
|
||||||
- Then put your contactId/display name in config.json
|
- Then put your contactId/display name in config.json
|
||||||
gateway.simplex.allowed_users (discover the id from a 'gateway_denied' trace
|
gateway.simplex.allowed_users (discover the id from a 'gateway_denied' trace
|
||||||
event or the daemon's /contacts) and reply. With an empty allowlist the
|
event or the daemon's /contacts) and reply. With an empty allowlist the
|
||||||
|
|||||||
+23
-13
@@ -1,10 +1,12 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Print the SimpleX bot's contact address from the running gateway daemon.
|
"""Print (creating if needed) the SimpleX bot's contact address.
|
||||||
|
|
||||||
The gateway talks to a local `simplex-chat` daemon (see simplex_chat in
|
The gateway talks to a local `simplex-chat` daemon running as a bot
|
||||||
config.json). To connect a phone/client to the bot you need the bot's contact
|
(`--create-bot-display-name`, see simplex_chat in config.json). To connect a
|
||||||
address; this queries it over the daemon's WebSocket API and prints the short
|
phone/client to the bot you need the bot's *user* contact address, not a chat
|
||||||
link (and full link). Requires the daemon to be running.
|
relay address; this queries the daemon's WebSocket API, creating the address on
|
||||||
|
first run, and prints the short link (and full link). Requires the daemon to be
|
||||||
|
running.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
.runtime/venv/bin/python scripts/simplex-address.py [--config config.json]
|
.runtime/venv/bin/python scripts/simplex-address.py [--config config.json]
|
||||||
@@ -29,18 +31,26 @@ def _ws_url(config_path: str) -> str:
|
|||||||
return str(cfg.get("gateway", {}).get("simplex", {}).get("ws_url") or "ws://127.0.0.1:5226")
|
return str(cfg.get("gateway", {}).get("simplex", {}).get("ws_url") or "ws://127.0.0.1:5226")
|
||||||
|
|
||||||
|
|
||||||
async def _show(ws_url: str, user_id: int) -> dict:
|
async def _resolve(ws_url: str, user_id: int) -> dict:
|
||||||
try:
|
try:
|
||||||
import websockets
|
import websockets
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise SystemExit("the `websockets` package is required (pip install websockets)")
|
raise SystemExit("the `websockets` package is required (pip install websockets)")
|
||||||
async with websockets.connect(ws_url, max_size=None) as ws:
|
async with websockets.connect(ws_url, max_size=None) as ws:
|
||||||
await ws.send(json.dumps({"corrId": "addr", "cmd": f"/_show_address {user_id}"}))
|
|
||||||
raw = await asyncio.wait_for(ws.recv(), timeout=10)
|
async def cmd(command: str) -> dict:
|
||||||
resp = json.loads(raw).get("resp", {}) or {}
|
await ws.send(json.dumps({"corrId": "addr", "cmd": command}))
|
||||||
if resp.get("type") != "userContactLink":
|
raw = await asyncio.wait_for(ws.recv(), timeout=20)
|
||||||
raise SystemExit(f"could not read address: {json.dumps(resp)[:300]}")
|
return json.loads(raw).get("resp", {}) or {}
|
||||||
|
|
||||||
|
resp = await cmd(f"/_show_address {user_id}")
|
||||||
|
if resp.get("type") == "userContactLink":
|
||||||
return resp.get("contactLink", {}) or {}
|
return resp.get("contactLink", {}) or {}
|
||||||
|
# No address yet -> create one (APICreateMyAddress).
|
||||||
|
resp = await cmd(f"/_address {user_id}")
|
||||||
|
if resp.get("type") == "userContactLinkCreated":
|
||||||
|
return {"connLinkContact": resp.get("connLinkContact", {}) or {}}
|
||||||
|
raise SystemExit(f"could not read or create address: {json.dumps(resp)[:300]}")
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
@@ -52,7 +62,7 @@ def main() -> int:
|
|||||||
|
|
||||||
ws_url = args.ws_url or _ws_url(args.config)
|
ws_url = args.ws_url or _ws_url(args.config)
|
||||||
try:
|
try:
|
||||||
link = asyncio.run(_show(ws_url, args.user_id))
|
link = asyncio.run(_resolve(ws_url, args.user_id))
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
print(f"cannot reach simplex-chat at {ws_url}: {exc}", file=sys.stderr)
|
print(f"cannot reach simplex-chat at {ws_url}: {exc}", file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
@@ -60,7 +70,7 @@ def main() -> int:
|
|||||||
print(f"simplex-chat at {ws_url} did not answer", file=sys.stderr)
|
print(f"simplex-chat at {ws_url} did not answer", file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
conn = link.get("connLinkContact", {}) or {}
|
conn = link.get("connLinkContact", {}) or link
|
||||||
short = conn.get("connShortLink")
|
short = conn.get("connShortLink")
|
||||||
full = conn.get("connFullLink")
|
full = conn.get("connFullLink")
|
||||||
if short:
|
if short:
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=SemIf agent SimpleX Chat daemon
|
Description=SemIf agent SimpleX Chat bot daemon
|
||||||
After=network-online.target
|
After=network-online.target
|
||||||
Wants=network-online.target
|
Wants=network-online.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
ExecStart=@REPO@/.runtime/bin/simplex-chat -p @PORT@ -d @REPO@/.runtime/simplex/simplex_v1 -k "" --relay --headless --user-display-name "@DISPLAY_NAME@"
|
# Bot mode: a local WebSocket chat server with a bot user. Do NOT add
|
||||||
|
# --headless here — in SimpleX Chat v7 --headless means "chat relay" and
|
||||||
|
# requires --relay, which yields a relay address, not a user contact address.
|
||||||
|
ExecStart=@REPO@/.runtime/bin/simplex-chat -p @PORT@ -d @REPO@/.runtime/simplex/simplex_v1 -k "" --create-bot-display-name "@DISPLAY_NAME@"
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=10
|
RestartSec=10
|
||||||
StandardOutput=journal
|
StandardOutput=journal
|
||||||
|
|||||||
Reference in new issue
Block a user