refactor: no comments left - one-line module docstrings, contracts on public fields only; jobs/job.py; example config and README
This commit is contained in:
@@ -1,13 +1,7 @@
|
||||
"""Frontend ABC + the runtime context handed to ``configure``.
|
||||
"""Frontend ABC and the runtime context handed to ``configure``.
|
||||
|
||||
A frontend is anything that routes inbound traffic into the gateway: an
|
||||
HTTP surface mounted under its ``path`` on the single gateway port
|
||||
(``frontends/root.py``), a poller (Telegram), or both. ``GatewayRuntime``
|
||||
carries everything a frontend may need that isn't user-config: built
|
||||
registries, per-agent backends, and the in-memory token store. The
|
||||
user's ``/config/config.py`` defines a ``Gateway`` (lists); ``cli.main``
|
||||
turns that into a ``GatewayRuntime`` and hands it to each frontend's
|
||||
``configure``.
|
||||
A frontend routes inbound traffic into the gateway (an HTTP mount, a
|
||||
poller, or both); ``GatewayRuntime`` carries the built state each needs.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -36,20 +30,8 @@ if TYPE_CHECKING:
|
||||
class GatewayRuntime:
|
||||
"""Post-build state of the gateway, shared with every frontend.
|
||||
|
||||
Backends are keyed by **agent name**, not type — one ``RaycastBackend``
|
||||
instance can serve many ``RaycastAgent`` instances, but the lookup
|
||||
site (an inbound request with ``model=<agent.name>``) already has
|
||||
the name in hand, so the indirection lives one step earlier.
|
||||
|
||||
``mcp_internal_urls`` is filled in Phase 2.1: one loopback URL per
|
||||
declared ``McpServer`` so ``ClaudeSdkBackend``
|
||||
can pass them to ``BackendOptions.mcp_servers`` without re-running
|
||||
discovery.
|
||||
|
||||
``db`` (Phase 4.1) is the shared :class:`Database` handle. Phase 4.2
|
||||
will switch ``TokenStore`` to read from it; Phase 4.3 admin/audit
|
||||
write through it. Phase 4.1 only attaches it — existing frontends
|
||||
ignore it.
|
||||
Backends are keyed by agent name, not type: one backend instance may
|
||||
serve several agents, so the indirection lives at lookup time.
|
||||
"""
|
||||
|
||||
agents: AgentRegistry
|
||||
@@ -58,62 +40,36 @@ class GatewayRuntime:
|
||||
token_store: TokenStore
|
||||
db: Database
|
||||
mcp_internal_urls: Mapping[str, str] = field(default_factory=dict)
|
||||
# Phase 4.3 — AdminFrontend reads creds + cookie-signing key from
|
||||
# the runtime so the user's ``config.py`` doesn't have to know
|
||||
# anything about env wiring. Defaulted to empty so existing tests /
|
||||
# call sites that don't touch the admin path keep building; the
|
||||
# admin frontend ``configure()`` itself rejects empty values.
|
||||
admin_user: str = ""
|
||||
"""Operator login for the admin console, checked by ``AdminFrontend``."""
|
||||
admin_pass: str = ""
|
||||
session_secret: str = ""
|
||||
# The full sibling-frontends list, in declaration order. AdminFrontend
|
||||
# uses it to advertise concrete bearer-endpoint URLs (host/port) on
|
||||
# the dashboard so the operator can copy ready-to-use links / curl
|
||||
# snippets. Other frontends ignore it.
|
||||
frontends: Sequence[Frontend] = field(default_factory=tuple)
|
||||
# Frontends that finish a turn (Anthropic Messages, Markdown) iterate
|
||||
# this list and ``await`` each handler with a ``TurnRecord``. Handlers
|
||||
# are appended during ``configure()`` by frontends that want a
|
||||
# cross-frontend chat archive — currently the markdown frontend's
|
||||
# ``log_all_chats`` mode. Handler exceptions are caught at the call
|
||||
# site; they never block the user-visible response.
|
||||
#
|
||||
# The field is typed as ``list[Any]`` rather than the precise
|
||||
# ``list[TurnLogHandler]`` because the alias lives under TYPE_CHECKING
|
||||
# to keep ``anthropic.types`` out of the runtime import graph for
|
||||
# this base module.
|
||||
"""Every frontend in declaration order, for advertising their URLs."""
|
||||
turn_log_handlers: list[TurnLogHandler] = field(default_factory=list)
|
||||
# M1b: conversations service, event bus and the shared session pool.
|
||||
# ``Any`` for the same import-graph reason as above; ``None`` only in
|
||||
# tests that build a runtime without them.
|
||||
"""Called with a ``TurnRecord`` after each turn; failures never reach the user."""
|
||||
conversations: Any = None
|
||||
bus: Any = None
|
||||
pool: Any = None
|
||||
scheduler: Any = None
|
||||
# External origin the reverse proxy puts in front of the gateway
|
||||
# (``Gateway.public_url``); ``None`` means "derive from the request".
|
||||
public_url: str | None = None
|
||||
"""``Gateway.public_url``; ``None`` derives the origin from the request."""
|
||||
|
||||
|
||||
class Frontend(ABC):
|
||||
"""Routes inbound traffic into the gateway.
|
||||
|
||||
HTTP frontends set ``path`` and return their ASGI app from ``app()``;
|
||||
``cli`` mounts every such app under that path on the one gateway
|
||||
port, so ``/anthropic/v1/messages`` reaches the Anthropic frontend's
|
||||
``/v1/messages``. ``serve()`` is for work outside HTTP - polling,
|
||||
vault mirrors - and defaults to nothing. ``landing`` marks the app
|
||||
that ``/`` redirects to (the admin console).
|
||||
these are mounted under that path on the one gateway port. ``serve()``
|
||||
is for non-HTTP work (polling, vault mirrors) and defaults to nothing.
|
||||
``landing`` marks the app that ``/`` redirects to.
|
||||
|
||||
A frontend that shows conversations declares ``name`` (the binding
|
||||
key) and ``kinds`` (which conversation kinds it shows);
|
||||
``core/conversations`` refuses to bind a conversation to a frontend
|
||||
outside its declaration. The first frontend in declaration order
|
||||
whose ``materialize`` returns a binding is the *home* of that kind:
|
||||
``spawn`` calls it so a new conversation gets a window (a vault file,
|
||||
a Telegram topic). ``agent_for`` names the default agent for a kind
|
||||
so callers may omit ``agent``. Stateless frontends (MCP, admin) keep
|
||||
the defaults and stay outside the routing.
|
||||
key) and ``kinds`` (which conversation kinds it shows). The first
|
||||
frontend whose ``materialize`` returns a binding is the *home* of
|
||||
that kind, used by ``spawn`` for new conversations; ``agent_for``
|
||||
names the default agent for a kind. Stateless frontends (MCP, admin)
|
||||
leave these at their defaults.
|
||||
"""
|
||||
|
||||
name: str = ""
|
||||
|
||||
Reference in New Issue
Block a user