1212from acp .connection import Connection , MethodHandler
1313from acp .exceptions import RequestError
1414from acp .interfaces import Agent as V1Agent
15- from acp .interfaces import Client as V1Client
1615
1716from . import v2
1817from .v2 ._connection import open_connection
19- from .v2 .agent import AgentFactory as V2AgentFactory
2018from .v2 .agent import AgentSideConnection as V2AgentSideConnection
19+ from .v2 .meta import AGENT_METHODS as V2_AGENT_METHODS
2120
2221__all__ = [
2322 "AgentProtocolConnection" ,
2423 "AgentProtocolRouter" ,
2524]
2625
27- V1AgentFactory = Callable [[V1Client ], V1Agent ]
26+ V1AgentFactory = Callable [[V1AgentSideConnection ], V1Agent ]
27+ V2AgentFactory = Callable [[V2AgentSideConnection ], v2 .Agent ]
2828
2929
3030def _dump (model : BaseModel ) -> dict [str , Any ]:
@@ -71,8 +71,8 @@ def _normalize_initialize(params: Any, selected_version: int) -> dict[str, Any]:
7171class _AgentNegotiationHandler :
7272 def __init__ (
7373 self ,
74- v1_agent : V1AgentFactory | V1Agent | None ,
75- v2_agent : V2AgentFactory | v2 . Agent | None ,
74+ v1_agent : V1AgentFactory | None ,
75+ v2_agent : V2AgentFactory | None ,
7676 ) -> None :
7777 self ._v1_agent = v1_agent
7878 self ._v2_agent = v2_agent
@@ -88,13 +88,13 @@ async def __call__(self, method: str, params: Any | None, is_notification: bool)
8888 async with self ._lock :
8989 if self ._selected is None :
9090 return await self ._initialize (method , params , is_notification )
91- if not is_notification and method == v2 . AGENT_METHODS ["initialize" ]:
91+ if not is_notification and method == V2_AGENT_METHODS ["initialize" ]:
9292 raise RequestError .invalid_request ({"details" : "ACP connections may only be initialized once" })
9393 handler = self ._selected
9494 return await handler (method , params , is_notification )
9595
9696 async def _initialize (self , method : str , params : Any , is_notification : bool ) -> Any :
97- if is_notification or method != v2 . AGENT_METHODS ["initialize" ]:
97+ if is_notification or method != V2_AGENT_METHODS ["initialize" ]:
9898 raise RequestError .invalid_request ({"details" : "The first ACP request must be initialize" })
9999 requested = _read_protocol_version (params )
100100 selected = self ._select (requested )
@@ -139,7 +139,7 @@ class AgentProtocolConnection:
139139 def __init__ (self , connection : Connection ) -> None :
140140 self ._connection = connection
141141
142- async def listen (self ) -> None :
142+ async def _listen (self ) -> None :
143143 await self ._connection .main_loop ()
144144
145145 async def close (self ) -> None :
@@ -158,15 +158,23 @@ class AgentProtocolRouter:
158158 def __init__ (
159159 self ,
160160 * ,
161- v1 : V1AgentFactory | V1Agent | None = None ,
162- v2 : V2AgentFactory | v2 . Agent | None = None ,
161+ v1 : V1AgentFactory | None = None ,
162+ v2 : V2AgentFactory | None = None ,
163163 ) -> None :
164164 if v1 is None and v2 is None :
165165 raise ValueError ("Configure at least one ACP protocol implementation" )
166166 self ._v1 = v1
167167 self ._v2 = v2
168168
169169 def connect (
170+ self ,
171+ input_stream : Any ,
172+ output_stream : Any = None ,
173+ ** connection_kwargs : Any ,
174+ ) -> AgentProtocolConnection :
175+ return self ._connect (input_stream , output_stream , ** connection_kwargs )
176+
177+ def _connect (
170178 self ,
171179 input_stream : Any ,
172180 output_stream : Any = None ,
@@ -197,13 +205,13 @@ async def run(
197205 from acp .stdio import stdio_streams
198206
199207 output_stream , input_stream = await stdio_streams (limit = stdio_buffer_limit_bytes )
200- connection = self .connect (
208+ connection = self ._connect (
201209 input_stream ,
202210 output_stream ,
203211 listening = False ,
204212 ** connection_kwargs ,
205213 )
206214 try :
207- await connection .listen ()
215+ await connection ._listen ()
208216 finally :
209217 await asyncio .shield (connection .close ())
0 commit comments