@@ -29,82 +29,69 @@ def __init__(self) -> None:
2929 self ._lock = asyncio .Lock ()
3030 self ._locked = False
3131
32- @property
33- def phase (self ) -> InitializationPhase :
34- return self ._phase
35-
36- def begin (self , request : schema .InitializeRequest ) -> None :
37- if self ._phase != "uninitialized" :
38- raise RequestError .invalid_request ({"details" : "ACP v2 connections may only be initialized once" })
32+ def begin (self , request : schema .InitializeRequest ) -> bool :
33+ if self ._locked or self ._phase in {"initializing" , "failed" }:
34+ raise RequestError .invalid_request ({"details" : "ACP v2 connection cannot initialize at this point" })
3935 if request .protocol_version != PROTOCOL_VERSION :
4036 raise RequestError .invalid_params ({
4137 "expectedProtocolVersion" : PROTOCOL_VERSION ,
4238 "receivedProtocolVersion" : request .protocol_version ,
4339 })
40+ was_initialized = self ._phase == "initialized"
4441 self ._phase = "initializing"
42+ self ._ready .clear ()
43+ return was_initialized
4544
4645 def complete (self , response : schema .InitializeResponse ) -> None :
47- if self ._phase != "initializing" :
48- raise RequestError .invalid_request ({"details" : "ACP v2 initialization is not in progress" })
4946 if response .protocol_version != PROTOCOL_VERSION :
5047 raise RequestError .invalid_request ({
5148 "expectedProtocolVersion" : PROTOCOL_VERSION ,
5249 "receivedProtocolVersion" : response .protocol_version ,
5350 })
51+ self .mark_initialized ()
52+
53+ def mark_initialized (self ) -> None :
5454 self ._phase = "initialized"
55- self ._failure = None
5655 self ._ready .set ()
5756
5857 def fail (self , error : BaseException ) -> None :
59- if self ._phase == "initialized" :
60- return
6158 self ._phase = "failed"
6259 self ._failure = error
6360 self ._ready .set ()
6461
65- async def initialized (self ) -> None :
66- if self ._phase in {"uninitialized" , "initializing" }:
67- await self ._ready .wait ()
68- if self ._phase == "initialized" :
69- return
70- if self ._failure is not None :
71- raise self ._failure
72- raise RequestError .invalid_request ({"details" : "ACP v2 connection has not been initialized" })
73-
7462 async def initialize_agent (
7563 self , params : Any , handle : Callable [[dict [str , Any ]], Awaitable [schema .InitializeResponse ]]
7664 ) -> schema .InitializeResponse :
7765 async with self ._lock :
7866 if self ._locked :
79- raise RequestError .invalid_request ({"details" : "ACP v2 connections may only be initialized once" })
67+ raise RequestError .invalid_request ({
68+ "details" : "ACP v2 connection cannot initialize after another request"
69+ })
8070 requested = read_protocol_version (params )
8171 if requested < PROTOCOL_VERSION :
8272 raise RequestError .unsupported_protocol_version (requested , [PROTOCOL_VERSION ])
8373 normalized = {** params , "protocolVersion" : PROTOCOL_VERSION }
8474
85- was_initialized = self ._phase == "initialized"
75+ previous_phase = self ._phase
8676 self ._phase = "initializing"
87- self ._ready .clear ()
8877 try :
8978 response = await handle (normalized )
9079 self .complete (response )
91- except BaseException as error :
92- if was_initialized :
93- self ._phase = "initialized"
94- self ._ready .set ()
95- else :
96- self .fail (error )
80+ except BaseException :
81+ self ._phase = previous_phase
9782 raise
9883 return response
9984
85+ def require_initialized (self , method : str ) -> None :
86+ if self ._phase != "initialized" :
87+ raise RequestError .invalid_request ({"details" : f"ACP v2 connection must be initialized before { method !r} " })
88+
10089 async def require (self , method : str , * , lock : bool = False ) -> None :
10190 async with self ._lock :
102- if self ._phase == "initialized" :
103- if lock :
104- self ._locked = True
105- return
106- if self ._phase != "initializing" :
107- raise RequestError .invalid_request ({
108- "details" : f"ACP v2 connection must be initialized before { method !r} "
109- })
110- await self .initialized ()
91+ if self ._phase == "initializing" :
92+ await self ._ready .wait ()
93+ if self ._failure is not None :
94+ raise self ._failure
95+ self .require_initialized (method )
96+ if lock :
97+ self ._locked = True
0 commit comments