11# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2-
32from __future__ import annotations
43
4+ import time
5+
56import httpx
67
78from .routes import (
@@ -612,6 +613,92 @@ def update_status(
612613 cast_to = AgentUpdateStatusResponse ,
613614 )
614615
616+ def wait_until_ready (
617+ self ,
618+ uuid : str ,
619+ * ,
620+ timeout : float = 300.0 ,
621+ poll_interval : float = 5.0 ,
622+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
623+ # The extra values given here take precedence over values defined on the client or passed to this method.
624+ extra_headers : Headers | None = None ,
625+ extra_query : Query | None = None ,
626+ extra_body : Body | None = None ,
627+ ) -> AgentRetrieveResponse :
628+ """Wait for an agent to be ready (deployment status is STATUS_RUNNING).
629+
630+ This method polls the agent status until it reaches STATUS_RUNNING or a terminal
631+ error state. It handles timeout and deployment failures automatically.
632+
633+ Args:
634+ uuid: The unique identifier of the agent to wait for
635+
636+ timeout: Maximum time to wait in seconds (default: 300 seconds / 5 minutes)
637+
638+ poll_interval: Time to wait between status checks in seconds (default: 5 seconds)
639+
640+ extra_headers: Send extra headers
641+
642+ extra_query: Add additional query parameters to the request
643+
644+ extra_body: Add additional JSON properties to the request
645+
646+ Returns:
647+ AgentRetrieveResponse: The agent response when it reaches STATUS_RUNNING
648+
649+ Raises:
650+ AgentDeploymentError: If the agent deployment fails (STATUS_FAILED,
651+ STATUS_UNDEPLOYMENT_FAILED, or STATUS_DELETED)
652+ AgentDeploymentTimeoutError: If the agent doesn't reach STATUS_RUNNING
653+ within the timeout period
654+ ValueError: If uuid is empty
655+ """
656+ from ..._exceptions import AgentDeploymentError , AgentDeploymentTimeoutError
657+
658+ if not uuid :
659+ raise ValueError (f"Expected a non-empty value for `uuid` but received { uuid !r} " )
660+
661+ start_time = time .time ()
662+
663+ while True :
664+ agent_response = self .retrieve (
665+ uuid ,
666+ extra_headers = extra_headers ,
667+ extra_query = extra_query ,
668+ extra_body = extra_body ,
669+ )
670+
671+ # Check if agent and deployment exist
672+ if agent_response .agent and agent_response .agent .deployment :
673+ status = agent_response .agent .deployment .status
674+
675+ # Success case
676+ if status == "STATUS_RUNNING" :
677+ return agent_response
678+
679+ # Failure cases
680+ if status in ("STATUS_FAILED" , "STATUS_UNDEPLOYMENT_FAILED" , "STATUS_DELETED" ):
681+ raise AgentDeploymentError (
682+ f"Agent deployment failed with status: { status } " ,
683+ status = status ,
684+ )
685+
686+ # Check timeout
687+ elapsed_time = time .time () - start_time
688+ if elapsed_time >= timeout :
689+ current_status = (
690+ agent_response .agent .deployment .status
691+ if agent_response .agent and agent_response .agent .deployment
692+ else "UNKNOWN"
693+ )
694+ raise AgentDeploymentTimeoutError (
695+ f"Agent did not reach STATUS_RUNNING within { timeout } seconds. Current status: { current_status } " ,
696+ agent_id = uuid ,
697+ )
698+
699+ # Wait before polling again
700+ time .sleep (poll_interval )
701+
615702
616703class AsyncAgentsResource (AsyncAPIResource ):
617704 @cached_property
@@ -1108,6 +1195,94 @@ async def update_status(
11081195 cast_to = AgentUpdateStatusResponse ,
11091196 )
11101197
1198+ async def wait_until_ready (
1199+ self ,
1200+ uuid : str ,
1201+ * ,
1202+ timeout : float = 300.0 ,
1203+ poll_interval : float = 5.0 ,
1204+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1205+ # The extra values given here take precedence over values defined on the client or passed to this method.
1206+ extra_headers : Headers | None = None ,
1207+ extra_query : Query | None = None ,
1208+ extra_body : Body | None = None ,
1209+ ) -> AgentRetrieveResponse :
1210+ """Wait for an agent to be ready (deployment status is STATUS_RUNNING).
1211+
1212+ This method polls the agent status until it reaches STATUS_RUNNING or a terminal
1213+ error state. It handles timeout and deployment failures automatically.
1214+
1215+ Args:
1216+ uuid: The unique identifier of the agent to wait for
1217+
1218+ timeout: Maximum time to wait in seconds (default: 300 seconds / 5 minutes)
1219+
1220+ poll_interval: Time to wait between status checks in seconds (default: 5 seconds)
1221+
1222+ extra_headers: Send extra headers
1223+
1224+ extra_query: Add additional query parameters to the request
1225+
1226+ extra_body: Add additional JSON properties to the request
1227+
1228+ Returns:
1229+ AgentRetrieveResponse: The agent response when it reaches STATUS_RUNNING
1230+
1231+ Raises:
1232+ AgentDeploymentError: If the agent deployment fails (STATUS_FAILED,
1233+ STATUS_UNDEPLOYMENT_FAILED, or STATUS_DELETED)
1234+ AgentDeploymentTimeoutError: If the agent doesn't reach STATUS_RUNNING
1235+ within the timeout period
1236+ ValueError: If uuid is empty
1237+ """
1238+ import asyncio
1239+
1240+ from ..._exceptions import AgentDeploymentError , AgentDeploymentTimeoutError
1241+
1242+ if not uuid :
1243+ raise ValueError (f"Expected a non-empty value for `uuid` but received { uuid !r} " )
1244+
1245+ start_time = time .time ()
1246+
1247+ while True :
1248+ agent_response = await self .retrieve (
1249+ uuid ,
1250+ extra_headers = extra_headers ,
1251+ extra_query = extra_query ,
1252+ extra_body = extra_body ,
1253+ )
1254+
1255+ # Check if agent and deployment exist
1256+ if agent_response .agent and agent_response .agent .deployment :
1257+ status = agent_response .agent .deployment .status
1258+
1259+ # Success case
1260+ if status == "STATUS_RUNNING" :
1261+ return agent_response
1262+
1263+ # Failure cases
1264+ if status in ("STATUS_FAILED" , "STATUS_UNDEPLOYMENT_FAILED" , "STATUS_DELETED" ):
1265+ raise AgentDeploymentError (
1266+ f"Agent deployment failed with status: { status } " ,
1267+ status = status ,
1268+ )
1269+
1270+ # Check timeout
1271+ elapsed_time = time .time () - start_time
1272+ if elapsed_time >= timeout :
1273+ current_status = (
1274+ agent_response .agent .deployment .status
1275+ if agent_response .agent and agent_response .agent .deployment
1276+ else "UNKNOWN"
1277+ )
1278+ raise AgentDeploymentTimeoutError (
1279+ f"Agent did not reach STATUS_RUNNING within { timeout } seconds. Current status: { current_status } " ,
1280+ agent_id = uuid ,
1281+ )
1282+
1283+ # Wait before polling again
1284+ await asyncio .sleep (poll_interval )
1285+
11111286
11121287class AgentsResourceWithRawResponse :
11131288 def __init__ (self , agents : AgentsResource ) -> None :
@@ -1134,6 +1309,9 @@ def __init__(self, agents: AgentsResource) -> None:
11341309 self .update_status = to_raw_response_wrapper (
11351310 agents .update_status ,
11361311 )
1312+ self .wait_until_ready = to_raw_response_wrapper (
1313+ agents .wait_until_ready ,
1314+ )
11371315
11381316 @cached_property
11391317 def api_keys (self ) -> APIKeysResourceWithRawResponse :
@@ -1201,6 +1379,9 @@ def __init__(self, agents: AsyncAgentsResource) -> None:
12011379 self .update_status = async_to_raw_response_wrapper (
12021380 agents .update_status ,
12031381 )
1382+ self .wait_until_ready = async_to_raw_response_wrapper (
1383+ agents .wait_until_ready ,
1384+ )
12041385
12051386 @cached_property
12061387 def api_keys (self ) -> AsyncAPIKeysResourceWithRawResponse :
@@ -1268,6 +1449,9 @@ def __init__(self, agents: AgentsResource) -> None:
12681449 self .update_status = to_streamed_response_wrapper (
12691450 agents .update_status ,
12701451 )
1452+ self .wait_until_ready = to_streamed_response_wrapper (
1453+ agents .wait_until_ready ,
1454+ )
12711455
12721456 @cached_property
12731457 def api_keys (self ) -> APIKeysResourceWithStreamingResponse :
@@ -1335,6 +1519,9 @@ def __init__(self, agents: AsyncAgentsResource) -> None:
13351519 self .update_status = async_to_streamed_response_wrapper (
13361520 agents .update_status ,
13371521 )
1522+ self .wait_until_ready = async_to_streamed_response_wrapper (
1523+ agents .wait_until_ready ,
1524+ )
13381525
13391526 @cached_property
13401527 def api_keys (self ) -> AsyncAPIKeysResourceWithStreamingResponse :
0 commit comments