A small synchronous client for the memory gateway endpoint.
Connect to a socket path, call a tool, get the result or the typed
errors/1 exception back. Intended for a musician-side shim. It sends only
{id, tool, args, budget_seconds?}; it has no way to name a session,
principal or scope, because the socket is the session. See
:mod:marianne.memory.gateway.endpoint for the wire format and for the exact
(same-UID, not sibling-proof) security property.
Classes
MemoryGatewayClient
MemoryGatewayClient(path, *, timeout=30.0, max_response_bytes=DEFAULT_MAX_RESPONSE_BYTES)
Source code in src/marianne/memory/gateway/client.py
| def __init__(
self, path: Path | str, *, timeout: float = 30.0,
max_response_bytes: int = DEFAULT_MAX_RESPONSE_BYTES,
) -> None:
self._max = max_response_bytes
self._next_id = 0
self._buf = b""
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._sock.settimeout(timeout)
try:
self._sock.connect(str(path))
except OSError as exc:
self._sock.close()
raise BackendUnreachable(
f"cannot connect to the memory gateway socket: {exc.strerror or exc}",
subtype="connect_failed", remedy="the session may have closed",
) from None
|
Methods:
call
call(tool, budget_seconds=None, **args)
Call tool; return its result or raise the typed error from errors/1.
Source code in src/marianne/memory/gateway/client.py
| def call(self, tool: str, budget_seconds: float | None = None, **args: Any) -> Any:
"""Call ``tool``; return its result or raise the typed error from ``errors/1``."""
self._next_id += 1
req: dict[str, Any] = {"id": self._next_id, "tool": tool, "args": args}
if budget_seconds is not None:
req["budget_seconds"] = budget_seconds
try:
self._sock.sendall((json.dumps(req) + "\n").encode("utf-8"))
except OSError as exc:
raise BackendUnreachable(
f"cannot send to the gateway: {exc}", subtype="transport_failed",
) from None
try:
frame = json.loads(self._readline())
except ValueError:
raise BackendUnreachable(
"the gateway sent an unreadable frame", subtype="bad_frame",
) from None
if not isinstance(frame, dict):
raise BackendUnreachable("the gateway sent a bad frame", subtype="bad_frame")
if frame.get("ok") is True:
return frame.get("result")
err = frame.get("error")
if not isinstance(err, dict):
raise BackendUnreachable("the gateway sent a bad error frame", subtype="bad_frame")
raise _typed(err)
|