22
33import asyncio
44from collections .abc import Callable
5- from typing import Any , cast
5+ from typing import Any
66
77from pydantic import BaseModel
88
@@ -97,15 +97,28 @@ async def _initialize(self, method: str, params: Any, is_notification: bool) ->
9797 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 )
100- selected = self ._select (requested )
101100 connection = self ._connection
102101 if connection is None :
103102 raise RuntimeError ("Protocol router is not connected" )
104103
105- if selected == v2 .PROTOCOL_VERSION :
106- endpoint , handler = V2AgentSideConnection ._attach (cast (Any , self ._v2_agent ), connection )
104+ if self ._v2_agent is not None and requested >= v2 .PROTOCOL_VERSION :
105+ selected = v2 .PROTOCOL_VERSION
106+ endpoint , handler = V2AgentSideConnection ._attach (self ._v2_agent , connection )
107+ elif self ._v1_agent is not None and requested >= v1_meta .PROTOCOL_VERSION :
108+ selected = v1_meta .PROTOCOL_VERSION
109+ endpoint , handler = V1AgentSideConnection ._attach (self ._v1_agent , connection )
107110 else :
108- endpoint , handler = V1AgentSideConnection ._attach (cast (Any , self ._v1_agent ), connection )
111+ supported = [
112+ version
113+ for version , implementation in (
114+ (v1_meta .PROTOCOL_VERSION , self ._v1_agent ),
115+ (v2 .PROTOCOL_VERSION , self ._v2_agent ),
116+ )
117+ if implementation is not None
118+ ]
119+ raise RequestError .invalid_request ({
120+ "details" : f"Unsupported ACP protocol { requested } ; configured versions are { supported } "
121+ })
109122 self ._endpoint = endpoint
110123 self ._selected = handler
111124 normalized = _normalize_initialize (params , selected )
@@ -117,23 +130,6 @@ async def _initialize(self, method: str, params: Any, is_notification: bool) ->
117130 })
118131 return response
119132
120- def _select (self , requested : int ) -> int :
121- if self ._v2_agent is not None and requested >= v2 .PROTOCOL_VERSION :
122- return v2 .PROTOCOL_VERSION
123- if self ._v1_agent is not None and requested >= v1_meta .PROTOCOL_VERSION :
124- return v1_meta .PROTOCOL_VERSION
125- supported = [
126- version
127- for version , implementation in (
128- (v1_meta .PROTOCOL_VERSION , self ._v1_agent ),
129- (v2 .PROTOCOL_VERSION , self ._v2_agent ),
130- )
131- if implementation is not None
132- ]
133- raise RequestError .invalid_request ({
134- "details" : f"Unsupported ACP protocol { requested } ; configured versions are { supported } "
135- })
136-
137133
138134class AgentProtocolConnection :
139135 def __init__ (self , connection : Connection ) -> None :
0 commit comments