Skip to content

http_relay

Class info

🛈 DocStrings

Relay a stdio ACP client to a remote agent (Streamable HTTP or WebSocket).

ACP clients that can only launch local agents (Zed, JetBrains, ...) spawn this as their agent; it forwards the newline-delimited JSON-RPC from stdin to the remote /acp endpoint and the server's messages back to stdout, unchanged. http(s):// URLs use Streamable HTTP, ws(s):// URLs the WebSocket transport.

aclientp-connect https://example.com/api/acp -H 'X-API-Key: ${MY_API_KEY}'
aclientp-connect wss://example.com/acp -H 'Authorization: Bearer ${MY_TOKEN}'

Header values may reference environment variables (${NAME}), so secrets can stay in the client's agent environment instead of its arguments. stdout carries protocol messages only; diagnostics go to stderr.

parse_header

parse_header(raw: str, env: dict[str, str] | None = None) -> tuple[str, str]

Parse Name: value, expanding ${VAR} references in the value.

Raises:

Type Description
ValueError

The header is malformed or references an unset variable.

Source code in src/aclientp/http_relay.py
def parse_header(raw: str, env: dict[str, str] | None = None) -> tuple[str, str]:
    """Parse ``Name: value``, expanding ``${VAR}`` references in the value.

    Raises:
        ValueError: The header is malformed or references an unset variable.
    """
    name, sep, value = raw.partition(":")
    if not sep or not name.strip():
        msg = f"Invalid header {raw!r}, expected 'Name: value'"
        raise ValueError(msg)
    try:
        expanded = string.Template(value.strip()).substitute(env if env is not None else os.environ)
    except KeyError as exc:
        msg = f"Header {name.strip()!r} references unset environment variable {exc.args[0]}"
        raise ValueError(msg) from exc
    return name.strip(), expanded

relay async

relay(url: str, headers: dict[str, str]) -> None

Forward stdin to the remote agent and its messages to stdout until either side ends.

Source code in src/aclientp/http_relay.py
async def relay(url: str, headers: dict[str, str]) -> None:
    """Forward stdin to the remote agent and its messages to stdout until either side ends."""
    stdin, stdout = await stdio_streams()
    async with (
        connect_remote_transport(url, headers=headers) as (remote_reader, remote_writer),
        anyio.create_task_group() as tg,
    ):

        async def pump(source: anyio.abc.ByteReceiveStream, sink: anyio.abc.ByteSendStream) -> None:
            try:
                async for chunk in source:
                    await sink.send(chunk)
            except (anyio.BrokenResourceError, anyio.ClosedResourceError):
                pass
            # Either side ending ends the relay (don't linger as an orphan).
            tg.cancel_scope.cancel()

        tg.start_soon(pump, stdin, remote_writer)
        tg.start_soon(pump, remote_reader, stdout)