Group committed starter skills into packs (semif_agent/packs.py) that bind a
concern to its categories, bridge(s), requirements, and config schema:
- seeds/ -> packs/<name>/ with a pack.json manifest + skills/<category>/<leaf>/.
Shipped: nextcloud, simplex, outlook, browser (each owning its bridge) and
essentials (time + calculator, no bridge).
- PackRegistry discovers packs (a root with no manifest is one implicit pack);
merge_packs loads every enabled pack. Pack enable is one switch
(packs.<name>.enabled -> bridges.<bridge>.enabled -> enabled_by_default);
disabling a pack drops its categories (incl. generated bodies) and its bridge.
- CATEGORY_DESCRIPTIONS now comes from the pack manifests (built-ins stay).
Adopt a Home-Assistant-style layered config (semif_agent/config.py):
- config.json is a thin index ({"include": [...]}) over config/core.json,
config/gateway.json, config/bridges.json, config/packs/*.json, deep-merged.
- Secrets live in the gitignored secrets.json, referenced as "!secret <key>";
api_key_env is removed.
- CORE_SCHEMA + each pack's config schema own defaults and GENERATE the
committed config.example.json; new `check-config` (validate + --write-example)
and `config-init` (seed the split layout) commands.
- scripts/config_merge.py (stdlib) lets bootstrap read the merged config.
Tests: tests/test_packs.py (renamed from test_seed_skills, +enable/merge),
new tests/test_config.py. All unit tests pass (772).
94 lines
3.5 KiB
Python
94 lines
3.5 KiB
Python
"""Read the readable text of a web page through the browser bridge.
|
|
|
|
Real integration: the local browser bridge (`browser_bridge_url`,
|
|
`browser_bridge_token`); the body never drives a browser directly. If the
|
|
request names a URL it opens that page first, otherwise it reads the page the
|
|
browser is already on. A bridge failure is reported honestly — never invented
|
|
page text.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
from semif_agent.skills import ActionResult
|
|
|
|
INTEGRATION = {
|
|
"service": "browser",
|
|
"transport": "http",
|
|
"config_vars": ["browser_bridge_url", "browser_bridge_token"],
|
|
}
|
|
|
|
CONTRACT = {
|
|
"browser_bridge_url": "Base URL of the local browser bridge, e.g. http://127.0.0.1:5232 (no trailing slash needed).",
|
|
"browser_bridge_token": "Shared secret for the browser bridge, if one is configured; sent as the X-Semif-Token header. Leave blank when the bridge requires no auth.",
|
|
}
|
|
|
|
TIMEOUT_SECONDS = 60
|
|
TEXT_CHARS = 4000
|
|
_URL_RE = re.compile(r"https?://[^\s<>\"')]+", re.IGNORECASE)
|
|
|
|
|
|
class BrowserBridgeError(Exception):
|
|
"""A real failure talking to the local browser bridge."""
|
|
|
|
|
|
def _request(ctx, path, payload=None, params=None):
|
|
base = str(ctx.config.get("browser_bridge_url", "") or "").strip().rstrip("/")
|
|
if not base:
|
|
raise BrowserBridgeError("browser_bridge_url is not configured")
|
|
token = str(ctx.config.get("browser_bridge_token", "") or "").strip()
|
|
url = base + path
|
|
if params:
|
|
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(params)
|
|
headers = {"Accept": "application/json"}
|
|
if token:
|
|
headers["X-Semif-Token"] = token
|
|
data = None
|
|
method = "GET"
|
|
if payload is not None:
|
|
data = json.dumps(payload).encode("utf-8")
|
|
headers["Content-Type"] = "application/json"
|
|
method = "POST"
|
|
http_request = urllib.request.Request(url, data=data, method=method, headers=headers)
|
|
try:
|
|
with urllib.request.urlopen(http_request, timeout=TIMEOUT_SECONDS) as response:
|
|
return json.loads(response.read().decode("utf-8") or "{}")
|
|
except urllib.error.HTTPError as exc:
|
|
detail = exc.read().decode("utf-8", "replace").strip()[:200]
|
|
raise BrowserBridgeError(f"{method} {path}: HTTP {exc.code} {detail}") from exc
|
|
except (urllib.error.URLError, TimeoutError, ValueError) as exc:
|
|
raise BrowserBridgeError(f"{method} {path}: {exc}") from exc
|
|
|
|
|
|
def _resolve_url(request):
|
|
source = request.user_input if request.user_input is not None else request.text
|
|
match = _URL_RE.search(source or "")
|
|
return match.group(0) if match else None
|
|
|
|
|
|
def act(ctx, request):
|
|
url = _resolve_url(request)
|
|
try:
|
|
if url:
|
|
_request(ctx, "/goto", {"url": url})
|
|
page = _request(ctx, "/content", params={"format": "text", "max_chars": TEXT_CHARS})
|
|
except BrowserBridgeError as exc:
|
|
return ActionResult(action_log=str(exc), new_state=request.text)
|
|
|
|
title = page.get("title") or ""
|
|
final_url = page.get("url") or url or "the current page"
|
|
text = (page.get("content") or "").strip()
|
|
if not text:
|
|
report = f"{title or final_url} ({final_url}) has no readable text."
|
|
return ActionResult(action_log=report, new_state=report)
|
|
if len(text) > TEXT_CHARS or page.get("truncated"):
|
|
text = text[:TEXT_CHARS].rstrip() + " …"
|
|
heading = f"{title} ({final_url})" if title else str(final_url)
|
|
report = f"{heading}:\n{text}"
|
|
return ActionResult(action_log=report, new_state=report)
|