forked from agentscope-ai/agentscope-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentrun_client.py
More file actions
1035 lines (915 loc) · 37.8 KB
/
agentrun_client.py
File metadata and controls
1035 lines (915 loc) · 37.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# pylint: disable=line-too-long, arguments-renamed
import logging
import random
import string
import time
from http.client import HTTPS_PORT
from urllib.parse import urlparse
from alibabacloud_agentrun20250910.models import (
ContainerConfiguration,
CreateAgentRuntimeRequest,
CreateAgentRuntimeInput,
CreateAgentRuntimeEndpointRequest,
CreateAgentRuntimeEndpointInput,
NetworkConfiguration,
LogConfiguration,
GetAgentRuntimeRequest,
HealthCheckConfiguration,
)
from alibabacloud_agentrun20250910.client import Client
from alibabacloud_tea_openapi import models as open_api_models
from .base_client import BaseClient
from .utils import SessionManager
from ...sandbox.model import SandboxManagerEnvConfig
logger = logging.getLogger(__name__)
class AgentRunClient(BaseClient):
"""
Client for managing AgentRun containers in the sandbox environment.
This client provides methods to create, start, stop, remove,
and inspect AgentRun sessions. It also handles the underlying AgentRun
API calls and status polling.
"""
# Global attempts and interval for status polling
GET_AGENT_RUNTIME_STATUS_MAX_ATTEMPTS = 60
GET_AGENT_RUNTIME_STATUS_INTERVAL = 1
DEFAULT_ENDPOINT_NAME = "default-endpoint"
HTTPS_PROTOCOL = "https"
def __init__(self, config: SandboxManagerEnvConfig):
"""Initialize the AgentRunClient with the provided configuration.
Args:
config (SandboxManagerEnvConfig): Configuration object
containing AgentRun settings.
"""
self.config = config
self.client = self._create_agent_run_client()
self.session_manager = SessionManager(config)
self.agent_run_prefix = config.agent_run_prefix or "agentscope-sandbox"
self._get_agent_runtime_status_max_attempts = (
self.GET_AGENT_RUNTIME_STATUS_MAX_ATTEMPTS
)
self._get_agent_runtime_status_interval = (
self.GET_AGENT_RUNTIME_STATUS_INTERVAL
)
logger.info(f"AgentRunClient initialized with config: {config}")
def create(
self,
image,
name=None,
ports=None,
volumes=None,
environment=None,
runtime_config=None,
):
"""Create a new AgentRun session with the specified parameters.
Args:
image (str): The container image to use for the AgentRun session.
name (str, optional): The name for the session. If not provided,
a random name will be generated.
ports (list, optional): List of ports to expose.
volumes (list, optional): List of volumes to mount.
environment (dict, optional): Environment variables to set in
the container.
runtime_config (dict, optional): Additional runtime configuration.
Returns:
tuple: A tuple containing (session_id, None, endpoint_public_url).
Raises:
Exception: If the AgentRun session creation fails.
"""
logger.debug(f"Creating AgentRun session with image: {image}")
port = 80
if ports is not None and len(ports) > 0:
port = 80 if ports[0] == "80/tcp" else ports[0]
agent_run_image = self._replace_agent_runtime_images(image)
try:
session_id = name or self._generate_session_id()
logger.info(f"Created AgentRun session: {session_id}")
agent_runtime_name = f"{self.agent_run_prefix}-{session_id}"
# Prepare container configuration
container_config = ContainerConfiguration(
image=agent_run_image,
)
# Create agentRuntime
# Prepare network configuration if needed
if (
self.config.agent_run_vpc_id
and self.config.agent_run_security_group_id
and self.config.agent_run_vswitch_ids
):
logger.info(
"Create agent runtime with PUBLIC_AND_PRIVATE network",
)
network_config = NetworkConfiguration(
network_mode="PUBLIC_AND_PRIVATE",
vpc_id=self.config.agent_run_vpc_id,
security_group_id=self.config.agent_run_security_group_id,
vswitch_ids=self.config.agent_run_vswitch_ids,
)
else:
logger.info("Create agent runtime with PUBLIC network")
network_config = NetworkConfiguration(
network_mode="PUBLIC",
)
# Prepare log configuration if needed
log_config = None
if (
self.config.agentrun_log_project
and self.config.agentrun_log_store
):
log_config = LogConfiguration(
project=self.config.agentrun_log_project,
logstore=self.config.agentrun_log_store,
)
health_check_url = "/"
health_check_config = HealthCheckConfiguration(
http_get_url=health_check_url,
initial_delay_seconds=2,
period_seconds=1,
success_threshold=1,
failure_threshold=60,
timeout_seconds=1,
)
# Create the input object with all provided parameters
input_data = CreateAgentRuntimeInput(
agent_runtime_name=agent_runtime_name,
artifact_type="Container",
cpu=self.config.agent_run_cpu,
memory=self.config.agent_run_memory,
port=port,
container_configuration=container_config,
environment_variables=environment or {},
network_configuration=network_config,
log_configuration=log_config,
health_check_configuration=health_check_config,
description=f"agentScope sandbox deploy for"
f" {agent_runtime_name}",
)
# Create and check agent runtime
agent_runtime_id = self._create_and_check_agent_runtime(input_data)
# Create and check agent runtime endpoint
(
agent_runtime_endpoint_id,
endpoint_public_url,
) = self._create_and_check_agent_runtime_endpoint(
agent_runtime_id,
agent_runtime_name,
)
# Store session information
session_data = {
"session_id": session_id,
"created_time": time.time(),
"agent_runtime_name": agent_runtime_name,
"agent_runtime_id": agent_runtime_id,
"agent_runtime_endpoint_id": agent_runtime_endpoint_id,
"endpoint_public_url": endpoint_public_url,
"status": "running",
"image": image,
"ports": ports,
"runtime_token": environment["SECRET_TOKEN"],
"environment": environment,
}
self.session_manager.create_session(session_id, session_data)
logger.info(
f"Success to create agent runtime with ID:"
f" {agent_runtime_id}, create session id: {session_id}",
)
logger.info(
f"endpoint_public_url: {endpoint_public_url}",
)
if not endpoint_public_url.endswith("/"):
endpoint_public_url = f"{endpoint_public_url}/"
parsed_url = urlparse(endpoint_public_url)
endpoint_public_url_domain = parsed_url.netloc
endpoint_public_url_path = parsed_url.path
# Agentrun should adapt for ip and port format
ports = [f"{HTTPS_PORT}{endpoint_public_url_path}"]
return (
session_id,
ports,
endpoint_public_url_domain,
self.HTTPS_PROTOCOL,
)
except Exception as e:
logger.error(f"Failed to create AgentRun session: {e}")
raise
def start(self, session_id):
"""Start an AgentRun session by setting its status to running.
Args:
session_id (str): The ID of the session to start.
Returns:
bool: True if the session was successfully started,
False otherwise.
"""
session = self.session_manager.get_session(session_id)
if not session:
logger.warning(f"AgentRun session id not found: {session_id}")
return False
agent_runtime_id = session["agent_runtime_id"]
try:
resp = self._get_agent_runtime_status(agent_runtime_id)
if resp.get("success"):
if resp.get("status") in ["READY"]:
self.session_manager.update_session(
session_id,
{"status": "running"},
)
logger.info(
f"set agentRun session status to running: {session_id}",
)
return True
except Exception as e:
logger.error(
f"failed to set agentRun session status to running:"
f" {session_id}: {e}",
)
return False
def stop(self, session_id, timeout=None):
"""Stop an AgentRun session by setting its status to stopped.
Args:
session_id (str): The ID of the session to stop.
timeout (int, optional): Timeout for the stop operation (not
currently used).
Returns:
bool: True if the session was successfully stopped,
False otherwise.
"""
session = self.session_manager.get_session(session_id)
if not session:
logger.warning(f"AgentRun session id not found: {session_id}")
return False
try:
self.session_manager.update_session(
session_id,
{"status": "stopped"},
)
logger.info(
f"set agentRun session status to stopped: {session_id}",
)
return True
except Exception as e:
logger.error(
f"failed to set agentRun session status to stopped:"
f" {session_id}: {e}",
)
return False
def remove(self, session_id, force=False):
"""Remove an AgentRun session by deleting the underlying agent runtime.
Args:
session_id (str): The ID of the session to remove.
force (bool, optional): Whether to force removal (not currently
used).
Returns:
dict: A dictionary containing the result of the removal operation.
"""
session = self.session_manager.get_session(session_id)
if not session:
logger.warning(f"AgentRun session id not found: {session_id}")
return False
agent_runtime_id = session["agent_runtime_id"]
try:
logger.info(f"Deleting agent runtime with ID: {agent_runtime_id}")
# Call the SDK method
response = self.client.delete_agent_runtime(agent_runtime_id)
# Check if the response is successful
if response.body and response.body.code == "SUCCESS":
logger.info(
f"Agent runtime deletion initiated successfully for ID:"
f" {agent_runtime_id}",
)
# Poll for status
status_result = None
status_reason = None
if agent_runtime_id:
logger.info(
f"Polling status for agent runtime deletion ID:"
f" {agent_runtime_id}",
)
poll_status = self._poll_agent_runtime_status(
agent_runtime_id,
)
if isinstance(poll_status, dict):
status_result = poll_status.get("status")
status_reason = poll_status.get("status_reason")
logger.info(
f"Agent runtime deletion status: {status_result}",
)
# Return a dictionary with relevant information from the
# response
return {
"success": True,
"message": "Agent runtime deletion initiated successfully",
"agent_runtime_id": agent_runtime_id,
"status": status_result,
"status_reason": status_reason,
"request_id": response.body.request_id,
}
else:
logger.error("Failed to delete agent runtime")
# Return error information if the request was not successful
return {
"success": False,
"code": response.body.code if response.body else None,
"message": "Failed to delete agent runtime",
"request_id": response.body.request_id
if response.body
else None,
}
except Exception as e:
logger.error(
f"Exception occurred while deleting agent runtime: {str(e)}",
)
# Return error information if an exception occurred
return {
"success": False,
"error": str(e),
"message": f"Exception occurred while deleting agent "
f"runtime: {str(e)}",
}
def inspect(self, session_id):
"""Inspect an AgentRun session and return detailed information.
Args:
session_id (str): The ID of the session to inspect.
Returns:
dict: A dictionary containing session information,
agent runtime info, and status.
"""
session = self.session_manager.get_session(session_id)
if not session:
logger.warning(f"AgentRun session id not found: {session_id}")
return False
agent_runtime_id = session["agent_runtime_id"]
# Get agent_runtime information
agent_runtime_info = {}
try:
if agent_runtime_id:
# Create the request object
request = GetAgentRuntimeRequest()
# Call the SDK method
response = self.client.get_agent_runtime(
agent_runtime_id,
request,
)
# Check if the response is successful
if (
response.body
and response.body.code == "SUCCESS"
and response.body.data
):
# Extract relevant information from the agent runtime data
agent_runtime = response.body.data
agent_runtime_info = {
"agent_runtime_id": agent_runtime.agent_runtime_id,
"agent_runtime_name": agent_runtime.agent_runtime_name,
"agent_runtime_arn": agent_runtime.agent_runtime_arn,
"status": agent_runtime.status,
"status_reason": agent_runtime.status_reason,
"artifact_type": agent_runtime.artifact_type,
"cpu": agent_runtime.cpu,
"memory": agent_runtime.memory,
"port": agent_runtime.port,
"created_at": agent_runtime.created_at,
"last_updated_at": agent_runtime.last_updated_at,
"description": agent_runtime.description,
"agent_runtime_version": agent_runtime.agent_runtime_version, # noqa: E501
"environment_variables": agent_runtime.environment_variables, # noqa: E501
"request_id": response.body.request_id,
}
else:
logger.warning(
f"Failed to get agent runtime info for ID:"
f" {agent_runtime_id}",
)
agent_runtime_info = {
"error": "Failed to get agent runtime info",
"agent_runtime_id": agent_runtime_id,
"code": response.body.code if response.body else None,
"message": "Failed to get agent runtime info"
if response.body
else None,
}
except Exception as e:
logger.error(
f"Exception occurred while getting agent runtime info:"
f" {str(e)}",
)
agent_runtime_info = {
"error": str(e),
"message": f"Exception occurred while getting agent runtime "
f"info: {str(e)}",
"agent_runtime_id": agent_runtime_id,
}
return {
"session": session,
"agent_runtime_info": agent_runtime_info,
"runtime_token": session.get("runtime_token"),
"status": session.get("status", "unknown"),
"endpoint_url": session.get("endpoint_public_url"),
}
def get_status(self, session_id):
"""Get the status of an AgentRun session.
Args:
session_id (str): The ID of the session to get status for.
Returns:
str: The status of the session (running, exited, starting,
or unknown).
"""
session = self.session_manager.get_session(session_id)
if not session:
logger.warning(f"AgentRun session id not found: {session_id}")
return "unknown"
agent_runtime_id = session["agent_runtime_id"]
resp = self._get_agent_runtime_status(agent_runtime_id)
if resp.get("success"):
agent_run_status = resp.get("status")
if agent_run_status in ["READY", "ACTIVE"]:
return "running"
if agent_run_status in [
"CREATE_FAILED",
"UPDATE_FAILED",
"FAILED",
"DELETING",
]:
return "exited"
if agent_run_status in ["CREATING", "UPDATING"]:
return "starting"
return session.get("status", "unknown")
def _create_agent_run_client(self) -> Client:
"""Create and configure the AgentRun client.
Returns:
Client: Configured AgentRun client instance.
"""
config = open_api_models.Config(
access_key_id=self.config.agent_run_access_key_id,
access_key_secret=self.config.agent_run_access_key_secret,
region_id=self.config.agent_run_region_id,
read_timeout=60 * 1000,
)
config.endpoint = (
f"agentrun.{self.config.agent_run_region_id}.aliyuncs.com"
)
return Client(config)
def _generate_session_id(self) -> str:
"""Generate a random session ID.
Returns:
str: A random 6-character session ID.
"""
return "".join(
random.choices(string.ascii_letters + string.digits, k=6),
)
def _create_and_check_agent_runtime(
self,
input_data: CreateAgentRuntimeInput,
) -> str:
"""Create an agent runtime and check its status until it's ready.
Args:
input_data (CreateAgentRuntimeInput): The input data for
creating the agent runtime.
Returns:
str: The agent runtime ID.
Raises:
Exception: If the agent runtime creation fails or the agent
runtime is not ready.
"""
# Create the request object
agent_runtime_id = None
try:
create_agent_runtime_req = CreateAgentRuntimeRequest(
body=input_data,
)
create_agent_runtime_res = self.client.create_agent_runtime(
create_agent_runtime_req,
)
# Extract agent_runtime_id from response
if (
create_agent_runtime_res.body
and create_agent_runtime_res.body.data
):
agent_runtime_id = (
create_agent_runtime_res.body.data.agent_runtime_id
)
except Exception as e:
logger.error(f"Failed to create agent runtime: {e}")
raise
# Poll and check agent runtime status
if agent_runtime_id:
status_response = self._poll_agent_runtime_status(agent_runtime_id)
if not status_response.get("success"):
logger.error(
f"Failed to get agent runtime status:"
f" {status_response.get('message')}",
)
raise RuntimeError(
f"Failed to get agent runtime status:"
f" {status_response.get('message')}",
)
if status_response.get("status") not in ["READY", "ACTIVE"]:
logger.error(
f"Agent runtime is not ready. Status:"
f" {status_response.get('status')}",
)
raise RuntimeError(
f"Agent runtime is not ready. Status:"
f" {status_response.get('status')}",
)
return agent_runtime_id
def _create_and_check_agent_runtime_endpoint(
self,
agent_runtime_id: str,
agent_runtime_name: str,
) -> tuple:
"""Create an agent runtime endpoint and check its status until it's
ready.
Args:
agent_runtime_id (str): The ID of the agent runtime.
agent_runtime_name (str): The name of the agent runtime.
Returns:
tuple: A tuple containing (agent_runtime_endpoint_id,
endpoint_public_url).
Raises:
Exception: If the agent runtime endpoint creation fails or the
endpoint is not ready.
"""
# Create agent runtime endpoint
agent_runtime_endpoint_id = None
endpoint_public_url = None
if agent_runtime_id:
# Prepare endpoint configuration
endpoint_input = CreateAgentRuntimeEndpointInput(
agent_runtime_endpoint_name=self.DEFAULT_ENDPOINT_NAME,
target_version="LATEST",
description=f"agentScope deploy auto-generated endpoint for"
f" {agent_runtime_name}",
)
endpoint_request = CreateAgentRuntimeEndpointRequest(
body=endpoint_input,
)
try:
endpoint_response = self.client.create_agent_runtime_endpoint(
agent_runtime_id,
endpoint_request,
)
if endpoint_response.body and endpoint_response.body.data:
agent_runtime_endpoint_id = (
endpoint_response.body.data.agent_runtime_endpoint_id
)
endpoint_public_url = (
endpoint_response.body.data.endpoint_public_url
)
except Exception as e:
logger.error(f"Failed to create agent runtime endpoint: {e}")
raise
# Poll and check agent runtime endpoint status
if agent_runtime_id and agent_runtime_endpoint_id:
endpoint_status_response = (
self._poll_agent_runtime_endpoint_status(
agent_runtime_id,
agent_runtime_endpoint_id,
)
)
if not endpoint_status_response.get("success"):
logger.error(
f"Failed to get agent runtime endpoint status:"
f" {endpoint_status_response.get('message')}",
)
raise RuntimeError(
f"Failed to get agent runtime endpoint status:"
f" {endpoint_status_response.get('message')}",
)
if endpoint_status_response.get("status") not in [
"READY",
"ACTIVE",
]:
logger.error(
f"Agent runtime endpoint is not ready. Status:"
f" {endpoint_status_response.get('status')}",
)
raise RuntimeError(
f"Agent runtime endpoint is not ready. Status:"
f" {endpoint_status_response.get('status')}",
)
return agent_runtime_endpoint_id, endpoint_public_url
def _poll_agent_runtime_endpoint_status(
self,
agent_runtime_id: str,
agent_runtime_endpoint_id: str,
):
"""
Poll agent runtime endpoint status until a terminal state is
reached or max attempts exceeded.
Args:
agent_runtime_id (str): The ID of the agent runtime.
agent_runtime_endpoint_id (str): The ID of the agent runtime
endpoint.
Returns:
Dict[str, Any]: A dictionary containing the final agent runtime
endpoint status or error information.
"""
# Terminal states that indicate the end of polling for endpoints
terminal_states = {
"CREATE_FAILED",
"UPDATE_FAILED",
"READY",
"ACTIVE",
"FAILED",
"DELETING",
}
# Polling configuration
max_attempts = self._get_agent_runtime_status_max_attempts
interval_seconds = self._get_agent_runtime_status_interval
logger.info(
f"Starting to poll agent runtime endpoint status for ID:"
f" {agent_runtime_endpoint_id}",
)
for attempt in range(1, max_attempts + 1):
# Get current status
status_response = self._get_agent_runtime_endpoint_status(
agent_runtime_id,
agent_runtime_endpoint_id,
)
# Check if the request was successful
if not status_response.get("success"):
logger.warning(
f"Attempt {attempt}/{max_attempts}: Failed "
f"to get status - {status_response.get('message')}",
)
# Wait before next attempt unless this is the last attempt
if attempt < max_attempts:
time.sleep(interval_seconds)
continue
# Extract status information
current_status = status_response.get("status")
status_reason = status_response.get("status_reason")
# Log current status
logger.info(
f"Attempt {attempt}/{max_attempts}: Status = {current_status}",
)
if status_reason:
logger.info(f" Status reason: {status_reason}")
# Check if we've reached a terminal state
if current_status in terminal_states:
logger.info(
f"Reached terminal state '{current_status}' after"
f" {attempt} attempts",
)
return status_response
# Wait before next attempt unless this is the last attempt
if attempt < max_attempts:
time.sleep(interval_seconds)
# If we've exhausted all attempts without reaching a terminal state
logger.warning(
f"Exceeded maximum attempts ({max_attempts}) without reaching a "
f"terminal state",
)
return self._get_agent_runtime_endpoint_status(
agent_runtime_id,
agent_runtime_endpoint_id,
)
def _get_agent_runtime_status(
self,
agent_runtime_id: str,
agent_runtime_version: str = None,
):
"""Get agent runtime status.
Args:
agent_runtime_id (str): The ID of the agent runtime.
agent_runtime_version (str, optional): The version of the agent
runtime.
Returns:
Dict[str, Any]: A dictionary containing the agent runtime
status or error information.
"""
try:
logger.debug(
f"Getting agent runtime status for ID: {agent_runtime_id}",
)
# Create the request object
request = GetAgentRuntimeRequest(
agent_runtime_version=agent_runtime_version,
)
# Call the SDK method
response = self.client.get_agent_runtime(agent_runtime_id, request)
# Check if the response is successful
if (
response.body
and response.body.code == "SUCCESS"
and response.body.data
):
status = (
response.body.data.status
if hasattr(response.body.data, "status")
else None
)
logger.debug(
f"Agent runtime status for ID {agent_runtime_id}:"
f" {status}",
)
# Return the status from the agent runtime data
return {
"success": True,
"status": status,
"status_reason": response.body.data.status_reason
if hasattr(
response.body.data,
"status_reason",
)
else None,
"request_id": response.body.request_id,
}
else:
logger.error("Failed to get agent runtime status")
# Return error information if the request was not successful
return {
"success": False,
"code": response.body.code if response.body else None,
"message": "Failed to get agent runtime status",
"request_id": response.body.request_id
if response.body
else None,
}
except Exception as e:
logger.error(
f"Exception occurred while getting agent runtime status:"
f" {str(e)}",
)
# Return error information if an exception occurred
return {
"success": False,
"error": str(e),
"message": f"Exception occurred while getting agent runtime "
f"status: {str(e)}",
}
def _get_agent_runtime_endpoint_status(
self,
agent_runtime_id: str,
agent_runtime_endpoint_id: str,
):
"""Get agent runtime endpoint status.
Args:
agent_runtime_id (str): The ID of the agent runtime.
agent_runtime_endpoint_id (str): The ID of the agent runtime
endpoint.
Returns:
Dict[str, str]: A dictionary containing the agent runtime
endpoint status or error information.
"""
try:
logger.debug(
f"Getting agent runtime endpoint status for ID:"
f" {agent_runtime_endpoint_id}",
)
# Call the SDK method
response = self.client.get_agent_runtime_endpoint(
agent_runtime_id,
agent_runtime_endpoint_id,
)
# Check if the response is successful
if (
response.body
and response.body.code == "SUCCESS"
and response.body.data
):
status = (
response.body.data.status
if hasattr(response.body.data, "status")
else None
)
logger.debug(
f"Agent runtime endpoint status for ID"
f" {agent_runtime_endpoint_id}: {status}",
)
# Return the status from the agent runtime endpoint data
return {
"success": True,
"status": status,
"status_reason": response.body.data.status_reason
if hasattr(
response.body.data,
"status_reason",
)
else None,
"request_id": response.body.request_id,
}
else:
logger.debug("Failed to get agent runtime endpoint status")
# Return error information if the request was not successful
return {
"success": False,
"code": response.body.code if response.body else None,
"message": "Failed to get agent runtime endpoint status",
"request_id": response.body.request_id
if response.body
else None,
}
except Exception as e:
logger.debug(
f"Exception occurred while getting agent runtime endpoint "
f"status: {str(e)}",
)
# Return error information if an exception occurred
return {
"success": False,
"error": str(e),
"message": f"Exception occurred while getting agent runtime "
f"endpoint status: {str(e)}",
}
def _poll_agent_runtime_status(
self,
agent_runtime_id: str,
agent_runtime_version: str = None,
):
"""
Poll agent runtime status until a terminal state is reached or
max attempts exceeded.
Args:
agent_runtime_id (str): The ID of the agent runtime.
agent_runtime_version (str, optional): The version of the agent
runtime.
Returns:
Dict[str, Any]: A dictionary containing the final agent runtime
status or error information.
"""
# Terminal states that indicate the end of polling for agent runtimes
terminal_states = {
"CREATE_FAILED",
"UPDATE_FAILED",
"READY",
"ACTIVE",
"FAILED",
"DELETING",
}
# Polling configuration
max_attempts = self._get_agent_runtime_status_max_attempts
interval_seconds = self._get_agent_runtime_status_interval
logger.info(
f"Starting to poll agent runtime status for ID:"
f" {agent_runtime_id}",
)
for attempt in range(1, max_attempts + 1):
# Get current status
status_response = self._get_agent_runtime_status(
agent_runtime_id,
agent_runtime_version,
)
# Check if the request was successful
if not status_response.get("success"):
logger.warning(
f"Attempt {attempt}/{max_attempts}: Failed to "
f"get status - {status_response.get('message')}",
)
# Wait before next attempt unless this is the last attempt
if attempt < max_attempts:
time.sleep(interval_seconds)
continue
# Extract status information
current_status = status_response.get("status")
status_reason = status_response.get("status_reason")
# Log current status
logger.info(
f"Attempt {attempt}/{max_attempts}: Status = {current_status}",
)
if status_reason:
logger.info(f" Status reason: {status_reason}")
# Check if we've reached a terminal state
if current_status in terminal_states:
logger.info(
f"Reached terminal state '{current_status}' after"
f" {attempt} attempts",
)
return status_response
# Wait before next attempt unless this is the last attempt
if attempt < max_attempts:
time.sleep(interval_seconds)
# If we've exhausted all attempts without reaching a terminal state
logger.warning(
f"Exceeded maximum attempts ({max_attempts}) without reaching a "
f"terminal state",