Skip to content

Commit 8375c7c

Browse files
committed
refactor(protocol): streamline connection attachment
1 parent 198c46d commit 8375c7c

4 files changed

Lines changed: 12 additions & 38 deletions

File tree

src/acp/agent/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,14 @@ def __init__(
102102
self._notify_connected(agent)
103103

104104
@classmethod
105-
def _attach(
105+
def attach(
106106
cls,
107107
to_agent: Callable[[Client], Agent] | Agent,
108108
connection: Connection,
109109
*,
110110
use_unstable_protocol: bool = False,
111111
) -> tuple[AgentSideConnection, MethodHandler]:
112+
"""Attach an agent-side wrapper to an existing connection."""
112113
self = cls.__new__(cls)
113114
agent, handler = self._prepare(to_agent, use_unstable_protocol=use_unstable_protocol)
114115
self._conn = connection

src/acp/client/connection.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Any, cast, final
77

88
from .._transport import Transport
9-
from ..connection import Connection, MethodHandler
9+
from ..connection import Connection
1010
from ..exceptions import RequestError
1111
from ..interfaces import Agent, Client
1212
from ..meta import AGENT_METHODS, CLIENT_METHODS
@@ -122,7 +122,9 @@ def __init__(
122122
use_unstable_protocol: bool = False,
123123
**connection_kwargs: Any,
124124
) -> None:
125-
client, handler = self._prepare(to_client, use_unstable_protocol=use_unstable_protocol)
125+
client = to_client(self) if callable(to_client) else to_client
126+
self._session_updates = _SessionUpdateTracker(cast(Client, client))
127+
handler = build_client_router(cast(Client, self._session_updates), use_unstable_protocol=use_unstable_protocol)
126128

127129
if isinstance(input_stream, Transport):
128130
if output_stream is not None:
@@ -134,34 +136,6 @@ def __init__(
134136
):
135137
raise TypeError(_CLIENT_CONNECTION_ERROR)
136138
self._conn = Connection(handler, input_stream, output_stream, **connection_kwargs)
137-
self._notify_connected(client)
138-
139-
@classmethod
140-
def _attach(
141-
cls,
142-
to_client: Callable[[Agent], Client] | Client,
143-
connection: Connection,
144-
*,
145-
use_unstable_protocol: bool = False,
146-
) -> tuple[ClientSideConnection, MethodHandler]:
147-
self = cls.__new__(cls)
148-
client, handler = self._prepare(to_client, use_unstable_protocol=use_unstable_protocol)
149-
self._conn = connection
150-
self._notify_connected(client)
151-
return self, handler
152-
153-
def _prepare(
154-
self,
155-
to_client: Callable[[Agent], Client] | Client,
156-
*,
157-
use_unstable_protocol: bool,
158-
) -> tuple[Client, MethodHandler]:
159-
client = cast(Client, to_client(self) if callable(to_client) else to_client)
160-
self._session_updates = _SessionUpdateTracker(client)
161-
handler = build_client_router(cast(Client, self._session_updates), use_unstable_protocol=use_unstable_protocol)
162-
return client, handler
163-
164-
def _notify_connected(self, client: Client) -> None:
165139
if on_connect := getattr(client, "on_connect", None):
166140
on_connect(self)
167141

src/acp/experimental/negotiation.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def __init__(
7878
self._v2_agent = v2_agent
7979
self._connection: Connection | None = None
8080
self._selected: MethodHandler | None = None
81-
self._endpoint: V1AgentSideConnection | V2AgentSideConnection | None = None
8281
self._lock = asyncio.Lock()
8382

8483
def bind_connection(self, connection: Connection) -> None:
@@ -103,10 +102,10 @@ async def _initialize(self, method: str, params: Any, is_notification: bool) ->
103102

104103
if self._v2_agent is not None and requested >= v2.PROTOCOL_VERSION:
105104
selected = v2.PROTOCOL_VERSION
106-
endpoint, handler = V2AgentSideConnection._attach(self._v2_agent, connection)
105+
_, handler = V2AgentSideConnection.attach(self._v2_agent, connection)
107106
elif self._v1_agent is not None and requested >= v1_meta.PROTOCOL_VERSION:
108107
selected = v1_meta.PROTOCOL_VERSION
109-
endpoint, handler = V1AgentSideConnection._attach(self._v1_agent, connection)
108+
_, handler = V1AgentSideConnection.attach(self._v1_agent, connection)
110109
else:
111110
supported = [
112111
version
@@ -119,7 +118,6 @@ async def _initialize(self, method: str, params: Any, is_notification: bool) ->
119118
raise RequestError.invalid_request({
120119
"details": f"Unsupported ACP protocol {requested}; configured versions are {supported}"
121120
})
122-
self._endpoint = endpoint
123121
self._selected = handler
124122
normalized = _normalize_initialize(params, selected)
125123
response = await handler(method, normalized, False)

src/acp/experimental/v2/agent.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pydantic import BaseModel
88

9-
from acp.connection import Connection
9+
from acp.connection import Connection, MethodHandler
1010

1111
from . import schema
1212
from ._connection import open_connection
@@ -75,11 +75,12 @@ def __init__(
7575
on_connect(self)
7676

7777
@classmethod
78-
def _attach(
78+
def attach(
7979
cls,
8080
agent_factory: Callable[[AgentSideConnection], object],
8181
connection: Connection,
82-
) -> tuple[AgentSideConnection, _AgentRouter]:
82+
) -> tuple[AgentSideConnection, MethodHandler]:
83+
"""Attach an agent-side wrapper to an existing connection."""
8384
self = cls.__new__(cls)
8485
self._state = InitializationState()
8586
self._conn = connection

0 commit comments

Comments
 (0)