@@ -535,7 +535,9 @@ async def connect_async(
535535
536536 Args:
537537 sandbox_id: Sandbox ID
538- type: 可选的类型参数,用于类型提示和运行时验证
538+ template_type: 可选的类型参数,用于类型提示和运行时类型决定。
539+ 提供时直接使用该类型决定返回的子类,不调用 get_template(无需 AKSK)。
540+ 未提供时通过 get_template 获取类型(需要 AKSK)。
539541 config: 配置对象
540542
541543 Returns:
@@ -552,47 +554,38 @@ async def connect_async(
552554 sandbox_id , config = config
553555 )
554556
555- # 根据 template_name 获取 template 类型
556- if sandbox .template_name is None :
557- raise ValueError (f"Sandbox { sandbox_id } has no template_name" )
557+ resolved_type = template_type
558+ if resolved_type is None :
559+ if sandbox .template_name is None :
560+ raise ValueError (f"Sandbox { sandbox_id } has no template_name" )
558561
559- template = await cls .get_template_async (
560- sandbox .template_name , config = config
561- )
562-
563- # 如果提供了 type 参数,验证类型是否匹配
564- if (
565- template_type is not None
566- and template .template_type != template_type
567- ):
568- raise ValueError (
569- f"Sandbox { sandbox_id } has template type"
570- f" { template .template_type } , but expected { template_type } "
562+ template = await cls .get_template_async (
563+ sandbox .template_name , config = config
571564 )
565+ resolved_type = template .template_type
572566
573- # 根据 template 类型创建相应的 Sandbox 子类
574567 from agentrun .sandbox .aio_sandbox import AioSandbox
575568 from agentrun .sandbox .browser_sandbox import BrowserSandbox
576569 from agentrun .sandbox .code_interpreter_sandbox import (
577570 CodeInterpreterSandbox ,
578571 )
579572
580573 result = None
581- if template . template_type == TemplateType .CODE_INTERPRETER :
574+ if resolved_type == TemplateType .CODE_INTERPRETER :
582575 result = CodeInterpreterSandbox .model_validate (
583576 sandbox .model_dump (by_alias = False )
584577 )
585- elif template . template_type == TemplateType .BROWSER :
578+ elif resolved_type == TemplateType .BROWSER :
586579 result = BrowserSandbox .model_validate (
587580 sandbox .model_dump (by_alias = False )
588581 )
589- elif template . template_type == TemplateType .AIO :
582+ elif resolved_type == TemplateType .AIO :
590583 result = AioSandbox .model_validate (
591584 sandbox .model_dump (by_alias = False )
592585 )
593586 else :
594587 raise ValueError (
595- f"Unsupported template type: { template . template_type } . "
588+ f"Unsupported template type: { resolved_type } . "
596589 "Expected 'code-interpreter', 'browser' or 'aio'"
597590 )
598591
@@ -612,7 +605,9 @@ def connect(
612605
613606 Args:
614607 sandbox_id: Sandbox ID
615- type: 可选的类型参数,用于类型提示和运行时验证
608+ template_type: 可选的类型参数,用于类型提示和运行时类型决定。
609+ 提供时直接使用该类型决定返回的子类,不调用 get_template(无需 AKSK)。
610+ 未提供时通过 get_template 获取类型(需要 AKSK)。
616611 config: 配置对象
617612
618613 Returns:
@@ -627,45 +622,36 @@ def connect(
627622 # 先获取 sandbox 信息
628623 sandbox = cls .__get_client ().get_sandbox (sandbox_id , config = config )
629624
630- # 根据 template_name 获取 template 类型
631- if sandbox .template_name is None :
632- raise ValueError (f"Sandbox { sandbox_id } has no template_name" )
625+ resolved_type = template_type
626+ if resolved_type is None :
627+ if sandbox .template_name is None :
628+ raise ValueError (f"Sandbox { sandbox_id } has no template_name" )
633629
634- template = cls .get_template (sandbox .template_name , config = config )
630+ template = cls .get_template (sandbox .template_name , config = config )
631+ resolved_type = template .template_type
635632
636- # 如果提供了 type 参数,验证类型是否匹配
637- if (
638- template_type is not None
639- and template .template_type != template_type
640- ):
641- raise ValueError (
642- f"Sandbox { sandbox_id } has template type"
643- f" { template .template_type } , but expected { template_type } "
644- )
645-
646- # 根据 template 类型创建相应的 Sandbox 子类
647633 from agentrun .sandbox .aio_sandbox import AioSandbox
648634 from agentrun .sandbox .browser_sandbox import BrowserSandbox
649635 from agentrun .sandbox .code_interpreter_sandbox import (
650636 CodeInterpreterSandbox ,
651637 )
652638
653639 result = None
654- if template . template_type == TemplateType .CODE_INTERPRETER :
640+ if resolved_type == TemplateType .CODE_INTERPRETER :
655641 result = CodeInterpreterSandbox .model_validate (
656642 sandbox .model_dump (by_alias = False )
657643 )
658- elif template . template_type == TemplateType .BROWSER :
644+ elif resolved_type == TemplateType .BROWSER :
659645 result = BrowserSandbox .model_validate (
660646 sandbox .model_dump (by_alias = False )
661647 )
662- elif template . template_type == TemplateType .AIO :
648+ elif resolved_type == TemplateType .AIO :
663649 result = AioSandbox .model_validate (
664650 sandbox .model_dump (by_alias = False )
665651 )
666652 else :
667653 raise ValueError (
668- f"Unsupported template type: { template . template_type } . "
654+ f"Unsupported template type: { resolved_type } . "
669655 "Expected 'code-interpreter', 'browser' or 'aio'"
670656 )
671657
0 commit comments