agentrun-inner-test 0.0.62__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of agentrun-inner-test might be problematic. Click here for more details.
- agentrun/__init__.py +358 -0
- agentrun/agent_runtime/__client_async_template.py +466 -0
- agentrun/agent_runtime/__endpoint_async_template.py +345 -0
- agentrun/agent_runtime/__init__.py +53 -0
- agentrun/agent_runtime/__runtime_async_template.py +477 -0
- agentrun/agent_runtime/api/__data_async_template.py +58 -0
- agentrun/agent_runtime/api/__init__.py +6 -0
- agentrun/agent_runtime/api/control.py +1362 -0
- agentrun/agent_runtime/api/data.py +98 -0
- agentrun/agent_runtime/client.py +868 -0
- agentrun/agent_runtime/endpoint.py +649 -0
- agentrun/agent_runtime/model.py +362 -0
- agentrun/agent_runtime/runtime.py +904 -0
- agentrun/credential/__client_async_template.py +177 -0
- agentrun/credential/__credential_async_template.py +216 -0
- agentrun/credential/__init__.py +28 -0
- agentrun/credential/api/__init__.py +5 -0
- agentrun/credential/api/control.py +606 -0
- agentrun/credential/client.py +319 -0
- agentrun/credential/credential.py +381 -0
- agentrun/credential/model.py +248 -0
- agentrun/integration/__init__.py +21 -0
- agentrun/integration/agentscope/__init__.py +13 -0
- agentrun/integration/agentscope/adapter.py +17 -0
- agentrun/integration/agentscope/builtin.py +88 -0
- agentrun/integration/agentscope/message_adapter.py +185 -0
- agentrun/integration/agentscope/model_adapter.py +60 -0
- agentrun/integration/agentscope/tool_adapter.py +59 -0
- agentrun/integration/builtin/__init__.py +18 -0
- agentrun/integration/builtin/knowledgebase.py +137 -0
- agentrun/integration/builtin/model.py +93 -0
- agentrun/integration/builtin/sandbox.py +1234 -0
- agentrun/integration/builtin/toolset.py +47 -0
- agentrun/integration/crewai/__init__.py +13 -0
- agentrun/integration/crewai/adapter.py +9 -0
- agentrun/integration/crewai/builtin.py +88 -0
- agentrun/integration/crewai/model_adapter.py +31 -0
- agentrun/integration/crewai/tool_adapter.py +26 -0
- agentrun/integration/google_adk/__init__.py +13 -0
- agentrun/integration/google_adk/adapter.py +15 -0
- agentrun/integration/google_adk/builtin.py +88 -0
- agentrun/integration/google_adk/message_adapter.py +144 -0
- agentrun/integration/google_adk/model_adapter.py +46 -0
- agentrun/integration/google_adk/tool_adapter.py +235 -0
- agentrun/integration/langchain/__init__.py +31 -0
- agentrun/integration/langchain/adapter.py +15 -0
- agentrun/integration/langchain/builtin.py +94 -0
- agentrun/integration/langchain/message_adapter.py +141 -0
- agentrun/integration/langchain/model_adapter.py +37 -0
- agentrun/integration/langchain/tool_adapter.py +50 -0
- agentrun/integration/langgraph/__init__.py +36 -0
- agentrun/integration/langgraph/adapter.py +20 -0
- agentrun/integration/langgraph/agent_converter.py +1073 -0
- agentrun/integration/langgraph/builtin.py +88 -0
- agentrun/integration/pydantic_ai/__init__.py +13 -0
- agentrun/integration/pydantic_ai/adapter.py +13 -0
- agentrun/integration/pydantic_ai/builtin.py +88 -0
- agentrun/integration/pydantic_ai/model_adapter.py +44 -0
- agentrun/integration/pydantic_ai/tool_adapter.py +19 -0
- agentrun/integration/utils/__init__.py +112 -0
- agentrun/integration/utils/adapter.py +560 -0
- agentrun/integration/utils/canonical.py +164 -0
- agentrun/integration/utils/converter.py +134 -0
- agentrun/integration/utils/model.py +110 -0
- agentrun/integration/utils/tool.py +1759 -0
- agentrun/knowledgebase/__client_async_template.py +173 -0
- agentrun/knowledgebase/__init__.py +53 -0
- agentrun/knowledgebase/__knowledgebase_async_template.py +438 -0
- agentrun/knowledgebase/api/__data_async_template.py +414 -0
- agentrun/knowledgebase/api/__init__.py +19 -0
- agentrun/knowledgebase/api/control.py +606 -0
- agentrun/knowledgebase/api/data.py +624 -0
- agentrun/knowledgebase/client.py +311 -0
- agentrun/knowledgebase/knowledgebase.py +748 -0
- agentrun/knowledgebase/model.py +270 -0
- agentrun/memory_collection/__client_async_template.py +178 -0
- agentrun/memory_collection/__init__.py +37 -0
- agentrun/memory_collection/__memory_collection_async_template.py +457 -0
- agentrun/memory_collection/api/__init__.py +5 -0
- agentrun/memory_collection/api/control.py +610 -0
- agentrun/memory_collection/client.py +323 -0
- agentrun/memory_collection/memory_collection.py +844 -0
- agentrun/memory_collection/model.py +162 -0
- agentrun/model/__client_async_template.py +357 -0
- agentrun/model/__init__.py +57 -0
- agentrun/model/__model_proxy_async_template.py +270 -0
- agentrun/model/__model_service_async_template.py +267 -0
- agentrun/model/api/__init__.py +6 -0
- agentrun/model/api/control.py +1173 -0
- agentrun/model/api/data.py +196 -0
- agentrun/model/client.py +674 -0
- agentrun/model/model.py +235 -0
- agentrun/model/model_proxy.py +439 -0
- agentrun/model/model_service.py +438 -0
- agentrun/sandbox/__aio_sandbox_async_template.py +523 -0
- agentrun/sandbox/__browser_sandbox_async_template.py +110 -0
- agentrun/sandbox/__client_async_template.py +491 -0
- agentrun/sandbox/__code_interpreter_sandbox_async_template.py +463 -0
- agentrun/sandbox/__init__.py +69 -0
- agentrun/sandbox/__sandbox_async_template.py +463 -0
- agentrun/sandbox/__template_async_template.py +152 -0
- agentrun/sandbox/aio_sandbox.py +912 -0
- agentrun/sandbox/api/__aio_data_async_template.py +335 -0
- agentrun/sandbox/api/__browser_data_async_template.py +140 -0
- agentrun/sandbox/api/__code_interpreter_data_async_template.py +206 -0
- agentrun/sandbox/api/__init__.py +19 -0
- agentrun/sandbox/api/__sandbox_data_async_template.py +107 -0
- agentrun/sandbox/api/aio_data.py +551 -0
- agentrun/sandbox/api/browser_data.py +172 -0
- agentrun/sandbox/api/code_interpreter_data.py +396 -0
- agentrun/sandbox/api/control.py +1051 -0
- agentrun/sandbox/api/playwright_async.py +492 -0
- agentrun/sandbox/api/playwright_sync.py +492 -0
- agentrun/sandbox/api/sandbox_data.py +154 -0
- agentrun/sandbox/browser_sandbox.py +185 -0
- agentrun/sandbox/client.py +925 -0
- agentrun/sandbox/code_interpreter_sandbox.py +823 -0
- agentrun/sandbox/model.py +384 -0
- agentrun/sandbox/sandbox.py +848 -0
- agentrun/sandbox/template.py +217 -0
- agentrun/server/__init__.py +191 -0
- agentrun/server/agui_normalizer.py +180 -0
- agentrun/server/agui_protocol.py +797 -0
- agentrun/server/invoker.py +309 -0
- agentrun/server/model.py +427 -0
- agentrun/server/openai_protocol.py +535 -0
- agentrun/server/protocol.py +140 -0
- agentrun/server/server.py +208 -0
- agentrun/toolset/__client_async_template.py +62 -0
- agentrun/toolset/__init__.py +51 -0
- agentrun/toolset/__toolset_async_template.py +204 -0
- agentrun/toolset/api/__init__.py +17 -0
- agentrun/toolset/api/control.py +262 -0
- agentrun/toolset/api/mcp.py +100 -0
- agentrun/toolset/api/openapi.py +1251 -0
- agentrun/toolset/client.py +102 -0
- agentrun/toolset/model.py +321 -0
- agentrun/toolset/toolset.py +271 -0
- agentrun/utils/__data_api_async_template.py +721 -0
- agentrun/utils/__init__.py +5 -0
- agentrun/utils/__resource_async_template.py +158 -0
- agentrun/utils/config.py +270 -0
- agentrun/utils/control_api.py +105 -0
- agentrun/utils/data_api.py +1121 -0
- agentrun/utils/exception.py +151 -0
- agentrun/utils/helper.py +108 -0
- agentrun/utils/log.py +77 -0
- agentrun/utils/model.py +168 -0
- agentrun/utils/resource.py +291 -0
- agentrun_inner_test-0.0.62.dist-info/METADATA +265 -0
- agentrun_inner_test-0.0.62.dist-info/RECORD +154 -0
- agentrun_inner_test-0.0.62.dist-info/WHEEL +5 -0
- agentrun_inner_test-0.0.62.dist-info/licenses/LICENSE +201 -0
- agentrun_inner_test-0.0.62.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""异常定义"""
|
|
2
|
+
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class AgentRunError(Exception):
|
|
7
|
+
"""AgentRun SDK 基础异常类"""
|
|
8
|
+
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
message: str,
|
|
12
|
+
**kwargs,
|
|
13
|
+
):
|
|
14
|
+
"""初始化异常
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
message: 错误消息
|
|
18
|
+
details: 详细信息
|
|
19
|
+
"""
|
|
20
|
+
msg = message or ""
|
|
21
|
+
if kwargs is not None:
|
|
22
|
+
msg += self.kwargs_str(**kwargs)
|
|
23
|
+
|
|
24
|
+
super().__init__(msg)
|
|
25
|
+
self.message = message
|
|
26
|
+
self.details = kwargs
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def kwargs_str(cls, **kwargs) -> str:
|
|
30
|
+
"""获取详细信息字符串
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
str: 详细信息字符串
|
|
34
|
+
"""
|
|
35
|
+
if not kwargs:
|
|
36
|
+
return ""
|
|
37
|
+
# return ", ".join([f"{key}={value!s}" for key, value in kwargs.items()])
|
|
38
|
+
import json
|
|
39
|
+
|
|
40
|
+
return json.dumps(
|
|
41
|
+
kwargs,
|
|
42
|
+
ensure_ascii=False,
|
|
43
|
+
default=lambda x: x.__dict__,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def details_str(self) -> str:
|
|
47
|
+
return self.kwargs_str(**self.details)
|
|
48
|
+
|
|
49
|
+
def __str__(self) -> str:
|
|
50
|
+
return self.message
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class HTTPError(AgentRunError):
|
|
54
|
+
"""HTTP 异常类"""
|
|
55
|
+
|
|
56
|
+
def __init__(
|
|
57
|
+
self,
|
|
58
|
+
status_code: int,
|
|
59
|
+
message: str,
|
|
60
|
+
request_id: Optional[str] = None,
|
|
61
|
+
**kwargs,
|
|
62
|
+
):
|
|
63
|
+
self.status_code = status_code
|
|
64
|
+
self.request_id = request_id
|
|
65
|
+
self.details = kwargs
|
|
66
|
+
super().__init__(message, **kwargs)
|
|
67
|
+
|
|
68
|
+
def __str__(self) -> str:
|
|
69
|
+
return (
|
|
70
|
+
f"HTTP {self.status_code}: {self.message}. Request ID:"
|
|
71
|
+
f" {self.request_id}. Details: {self.details_str()}"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
def to_resource_error(
|
|
75
|
+
self, resource_type: str, resource_id: Optional[str] = ""
|
|
76
|
+
):
|
|
77
|
+
if self.status_code == 404 and (
|
|
78
|
+
"does not exist" in self.message or "not found" in self.message
|
|
79
|
+
):
|
|
80
|
+
return ResourceNotExistError(resource_type, resource_id)
|
|
81
|
+
elif (
|
|
82
|
+
self.status_code == 400
|
|
83
|
+
or self.status_code == 409
|
|
84
|
+
or self.status_code == 500
|
|
85
|
+
) and ("already exists" in self.message):
|
|
86
|
+
# TODO: ModelProxy already exists returns 500
|
|
87
|
+
return ResourceAlreadyExistError(resource_type, resource_id)
|
|
88
|
+
else:
|
|
89
|
+
return self
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class ClientError(HTTPError):
|
|
93
|
+
"""客户端异常类"""
|
|
94
|
+
|
|
95
|
+
def __init__(
|
|
96
|
+
self,
|
|
97
|
+
status_code: int,
|
|
98
|
+
message: str,
|
|
99
|
+
request_id: Optional[str] = None,
|
|
100
|
+
**kwargs,
|
|
101
|
+
):
|
|
102
|
+
super().__init__(status_code, message, request_id=request_id, **kwargs)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class ServerError(HTTPError):
|
|
106
|
+
"""服务端异常类"""
|
|
107
|
+
|
|
108
|
+
def __init__(
|
|
109
|
+
self, status_code: int, message: str, request_id: Optional[str] = None
|
|
110
|
+
):
|
|
111
|
+
super().__init__(status_code, message, request_id=request_id)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class ResourceNotExistError(AgentRunError):
|
|
115
|
+
"""资源不存在异常"""
|
|
116
|
+
|
|
117
|
+
def __init__(
|
|
118
|
+
self,
|
|
119
|
+
resource_type: str,
|
|
120
|
+
resource_id: Optional[str] = "",
|
|
121
|
+
):
|
|
122
|
+
super().__init__(
|
|
123
|
+
f"Resource {resource_type}({resource_id}) does not exist."
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class ResourceAlreadyExistError(AgentRunError):
|
|
128
|
+
"""资源已存在异常"""
|
|
129
|
+
|
|
130
|
+
def __init__(
|
|
131
|
+
self,
|
|
132
|
+
resource_type: str,
|
|
133
|
+
resource_id: Optional[str] = "",
|
|
134
|
+
):
|
|
135
|
+
super().__init__(
|
|
136
|
+
f"Resource {resource_type}({resource_id}) already exists."
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class DeleteResourceError(AgentRunError):
|
|
141
|
+
"""删除资源异常"""
|
|
142
|
+
|
|
143
|
+
def __init__(
|
|
144
|
+
self,
|
|
145
|
+
message: Optional[str] = None,
|
|
146
|
+
):
|
|
147
|
+
msg = "Failed to delete resource."
|
|
148
|
+
if message:
|
|
149
|
+
msg += f" Reason: {message}"
|
|
150
|
+
|
|
151
|
+
super().__init__(msg)
|
agentrun/utils/helper.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""辅助工具模块 / Helper Utilities Module
|
|
2
|
+
|
|
3
|
+
此模块提供一些通用的辅助函数。
|
|
4
|
+
This module provides general utility functions.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any, Optional
|
|
8
|
+
|
|
9
|
+
from typing_extensions import NotRequired, TypedDict, Unpack
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def mask_password(password: Optional[str]) -> str:
|
|
13
|
+
"""遮蔽密码用于日志记录 / Mask password for logging purposes
|
|
14
|
+
|
|
15
|
+
将密码部分字符替换为星号,用于安全地记录日志。
|
|
16
|
+
Replaces part of the password characters with asterisks for safe logging.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
password: 原始密码,可选 / Original password, optional
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
str: 遮蔽后的密码 / Masked password
|
|
23
|
+
|
|
24
|
+
Examples:
|
|
25
|
+
>>> mask_password("password123")
|
|
26
|
+
'pa******23'
|
|
27
|
+
>>> mask_password("abc")
|
|
28
|
+
'a*c'
|
|
29
|
+
"""
|
|
30
|
+
if not password:
|
|
31
|
+
return ""
|
|
32
|
+
if len(password) <= 2:
|
|
33
|
+
return "*" * len(password)
|
|
34
|
+
if len(password) <= 4:
|
|
35
|
+
return password[0] + "*" * (len(password) - 2) + password[-1]
|
|
36
|
+
return password[0:2] + "*" * (len(password) - 4) + password[-2:]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MergeOptions(TypedDict):
|
|
40
|
+
concat_list: NotRequired[bool]
|
|
41
|
+
no_new_field: NotRequired[bool]
|
|
42
|
+
ignore_empty_list: NotRequired[bool]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def merge(a: Any, b: Any, **args: Unpack[MergeOptions]) -> Any:
|
|
46
|
+
"""通用合并函数 / Generic deep merge helper.
|
|
47
|
+
|
|
48
|
+
合并规则概览:
|
|
49
|
+
- 若 ``b`` 为 ``None``: 返回 ``a``
|
|
50
|
+
- 若 ``a`` 为 ``None``: 返回 ``b``
|
|
51
|
+
- ``dict``: 递归按 key 深度合并
|
|
52
|
+
- ``list``: 连接列表 ``a + b``
|
|
53
|
+
- ``tuple``: 连接元组 ``a + b``
|
|
54
|
+
- ``set``/``frozenset``: 取并集
|
|
55
|
+
- 具有 ``__dict__`` 的同类型对象: 按属性字典递归合并后构造新实例
|
|
56
|
+
- 其他类型: 直接返回 ``b`` (视为覆盖)
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
# None 合并: 保留非 None 一方
|
|
60
|
+
if b is None:
|
|
61
|
+
return a
|
|
62
|
+
if a is None:
|
|
63
|
+
return b
|
|
64
|
+
|
|
65
|
+
# dict 深度合并
|
|
66
|
+
if isinstance(a, dict) and isinstance(b, dict):
|
|
67
|
+
result: dict[Any, Any] = dict(a)
|
|
68
|
+
for key, value in b.items():
|
|
69
|
+
if key in result:
|
|
70
|
+
result[key] = merge(result[key], value, **args)
|
|
71
|
+
else:
|
|
72
|
+
if args.get("no_new_field", False):
|
|
73
|
+
continue
|
|
74
|
+
result[key] = value
|
|
75
|
+
return result
|
|
76
|
+
|
|
77
|
+
# list 合并: 连接
|
|
78
|
+
if isinstance(a, list) and isinstance(b, list):
|
|
79
|
+
if args.get("concat_list", False):
|
|
80
|
+
return [*a, *b]
|
|
81
|
+
if args.get("ignore_empty_list", False):
|
|
82
|
+
if len(b) == 0:
|
|
83
|
+
return a
|
|
84
|
+
return b
|
|
85
|
+
|
|
86
|
+
# tuple 合并: 连接
|
|
87
|
+
if isinstance(a, tuple) and isinstance(b, tuple):
|
|
88
|
+
return (*a, *b)
|
|
89
|
+
|
|
90
|
+
# set / frozenset: 并集
|
|
91
|
+
if isinstance(a, set) and isinstance(b, set):
|
|
92
|
+
return a | b
|
|
93
|
+
if isinstance(a, frozenset) and isinstance(b, frozenset):
|
|
94
|
+
return a | b
|
|
95
|
+
|
|
96
|
+
# 同类型且具备 __dict__ 的对象: 按属性递归合并, 就地更新 a
|
|
97
|
+
if type(a) is type(b) and hasattr(a, "__dict__") and hasattr(b, "__dict__"):
|
|
98
|
+
for key, value in b.__dict__.items():
|
|
99
|
+
if key in a.__dict__:
|
|
100
|
+
setattr(a, key, merge(getattr(a, key), value, **args))
|
|
101
|
+
else:
|
|
102
|
+
if args.get("no_new_field", False):
|
|
103
|
+
continue
|
|
104
|
+
setattr(a, key, value)
|
|
105
|
+
return a
|
|
106
|
+
|
|
107
|
+
# 其他情况: 视为覆盖, 返回 b
|
|
108
|
+
return b
|
agentrun/utils/log.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""日志模块 / Logging Module
|
|
2
|
+
|
|
3
|
+
此模块配置 AgentRun SDK 的日志系统。
|
|
4
|
+
This module configures the logging system for AgentRun SDK.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
from dotenv import load_dotenv
|
|
11
|
+
|
|
12
|
+
load_dotenv()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class CustomFormatter(logging.Formatter):
|
|
16
|
+
"""自定义日志格式化器 / Custom Log Formatter
|
|
17
|
+
|
|
18
|
+
提供带颜色的日志输出格式。
|
|
19
|
+
Provides colorful log output format.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
FORMATS = {
|
|
23
|
+
"DEBUG": (
|
|
24
|
+
"\n\x1b[1;36m%(levelname)s\x1b[0m \x1b[36m[%(name)s] %(asctime)s"
|
|
25
|
+
" \x1b[2;3m%(pathname)s:%(lineno)s\x1b[0m\n\x1b[2m%(message)s\x1b[0m\n"
|
|
26
|
+
),
|
|
27
|
+
"INFO": (
|
|
28
|
+
"\n\x1b[1;34m%(levelname)s\x1b[0m \x1b[34m[%(name)s] %(asctime)s"
|
|
29
|
+
" \x1b[2;3m%(pathname)s:%(lineno)s\x1b[0m\n%(message)s\n"
|
|
30
|
+
),
|
|
31
|
+
"WARNING": (
|
|
32
|
+
"\n\x1b[1;33m%(levelname)s\x1b[0m \x1b[33m[%(name)s] %(asctime)s"
|
|
33
|
+
" \x1b[2;3m%(pathname)s:%(lineno)s\x1b[0m\n%(message)s\n"
|
|
34
|
+
),
|
|
35
|
+
"ERROR": (
|
|
36
|
+
"\n\x1b[1;31m%(levelname)s\x1b[0m \x1b[31m[%(name)s] %(asctime)s"
|
|
37
|
+
" \x1b[2;3m%(pathname)s:%(lineno)s\x1b[0m\n%(message)s\n"
|
|
38
|
+
),
|
|
39
|
+
"CRITICAL": (
|
|
40
|
+
"\n\x1b[1;31m%(levelname)s\x1b[0m \x1b[31m[%(name)s] %(asctime)s"
|
|
41
|
+
" \x1b[2;3m%(pathname)s:%(lineno)s\x1b[0m\n%(message)s\n"
|
|
42
|
+
),
|
|
43
|
+
"DEFAULT": (
|
|
44
|
+
"\n%(levelname)s [%(name)s] %(asctime)s"
|
|
45
|
+
" \x1b[2;3m%(pathname)s:%(lineno)s\x1b[0m\n%(message)s\n"
|
|
46
|
+
),
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
def format(self, record):
|
|
50
|
+
formatter = logging.Formatter(
|
|
51
|
+
self.FORMATS.get(record.levelname, self.FORMATS["DEFAULT"])
|
|
52
|
+
)
|
|
53
|
+
return formatter.format(record)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
logger = logging.getLogger("agentrun-logger")
|
|
57
|
+
|
|
58
|
+
logger.setLevel(level=logging.INFO)
|
|
59
|
+
handler = logging.StreamHandler()
|
|
60
|
+
handler.setFormatter(CustomFormatter())
|
|
61
|
+
logger.addHandler(handler)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
if os.getenv("AGENTRUN_SDK_DEBUG") not in [
|
|
65
|
+
None,
|
|
66
|
+
"",
|
|
67
|
+
"False",
|
|
68
|
+
"FALSE",
|
|
69
|
+
"false",
|
|
70
|
+
"0",
|
|
71
|
+
]:
|
|
72
|
+
logger.setLevel(logging.DEBUG)
|
|
73
|
+
logger.warning(
|
|
74
|
+
"启用 AgentRun SDK 调试日志, 移除 AGENTRUN_SDK_DEBUG 环境变量以关闭"
|
|
75
|
+
)
|
|
76
|
+
else:
|
|
77
|
+
logger.setLevel(logging.INFO)
|
agentrun/utils/model.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""数据模型基类模块 / Data Model Base Module
|
|
2
|
+
|
|
3
|
+
此模块定义所有数据模型的基类和通用配置。
|
|
4
|
+
This module defines base classes and common configurations for all data models.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from typing import List, Optional, Union
|
|
9
|
+
|
|
10
|
+
from darabonba.model import DaraModel
|
|
11
|
+
from pydantic import AliasGenerator
|
|
12
|
+
from pydantic import BaseModel as PydanticModel
|
|
13
|
+
from pydantic import ConfigDict, Field, ValidationError
|
|
14
|
+
from Tea.model import TeaModel
|
|
15
|
+
from typing_extensions import Self
|
|
16
|
+
|
|
17
|
+
from agentrun.utils.log import logger
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def to_camel_case(field_name: str) -> str:
|
|
21
|
+
"""将下划线命名转换为驼峰命名 / Convert snake_case to camelCase
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
field_name: 下划线命名的字段名 / Field name in snake_case
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
str: 驼峰命名的字段名 / Field name in camelCase
|
|
28
|
+
|
|
29
|
+
Examples:
|
|
30
|
+
>>> to_camel_case("hello_world")
|
|
31
|
+
'helloWorld'
|
|
32
|
+
>>> to_camel_case("access_key_id")
|
|
33
|
+
'accessKeyId'
|
|
34
|
+
"""
|
|
35
|
+
if "_" not in field_name:
|
|
36
|
+
return field_name
|
|
37
|
+
parts = field_name.split("_")
|
|
38
|
+
return parts[0] + "".join(word.capitalize() for word in parts[1:])
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class BaseModel(PydanticModel):
|
|
42
|
+
model_config = ConfigDict(
|
|
43
|
+
use_attribute_docstrings=True,
|
|
44
|
+
validate_by_name=True,
|
|
45
|
+
validate_by_alias=False,
|
|
46
|
+
serialize_by_alias=True,
|
|
47
|
+
use_enum_values=True,
|
|
48
|
+
extra="allow",
|
|
49
|
+
arbitrary_types_allowed=True,
|
|
50
|
+
alias_generator=AliasGenerator(
|
|
51
|
+
validation_alias=to_camel_case,
|
|
52
|
+
serialization_alias=to_camel_case,
|
|
53
|
+
),
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def from_inner_object(
|
|
58
|
+
cls, obj: Union[DaraModel, TeaModel], extra: Optional[dict] = None
|
|
59
|
+
) -> Self:
|
|
60
|
+
"""从 Darabonba 模型对象创建 Pydantic 模型对象,可选地合并额外的字段"""
|
|
61
|
+
logger.debug(
|
|
62
|
+
"before parse object obj=%s, extra=%s", obj.to_map(), extra
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
d = {**extra, **obj.to_map()} if extra else obj.to_map() # type: ignore
|
|
66
|
+
try:
|
|
67
|
+
result = cls.model_validate(d, by_alias=True)
|
|
68
|
+
except ValidationError as e:
|
|
69
|
+
logger.warning("validate type failed, %s", e)
|
|
70
|
+
result = cls.model_construct(None, **d) # type: ignore
|
|
71
|
+
return result
|
|
72
|
+
|
|
73
|
+
def update_self(self, other: Optional["BaseModel"]) -> Self:
|
|
74
|
+
"""更新自身属性 / Update self attributes
|
|
75
|
+
|
|
76
|
+
用另一个模型对象的属性更新当前对象。
|
|
77
|
+
Update current object with attributes from another model object.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
other: 另一个模型对象,可选 / Another model object, optional
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
Self: 更新后的自身 / Updated self
|
|
84
|
+
"""
|
|
85
|
+
if other is not None:
|
|
86
|
+
self.__dict__.update(other.__dict__)
|
|
87
|
+
|
|
88
|
+
return self
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class NetworkMode(str, Enum):
|
|
92
|
+
"""网络访问模式 / Network Access Mode
|
|
93
|
+
|
|
94
|
+
定义 Agent Runtime 的网络访问模式。
|
|
95
|
+
Defines network access modes for Agent Runtime.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
PUBLIC = "PUBLIC"
|
|
99
|
+
"""公网模式 / Public network mode"""
|
|
100
|
+
PRIVATE = "PRIVATE"
|
|
101
|
+
"""私网模式 / Private network mode"""
|
|
102
|
+
PUBLIC_AND_PRIVATE = "PUBLIC_AND_PRIVATE"
|
|
103
|
+
"""公私网混合模式 / Public and private network mode"""
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class NetworkConfig(BaseModel):
|
|
107
|
+
"""网络配置 / Network Configuration
|
|
108
|
+
|
|
109
|
+
定义 Agent Runtime 的网络配置。
|
|
110
|
+
Defines network configuration for Agent Runtime.
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
network_mode: NetworkMode = Field(
|
|
114
|
+
alias="networkMode", default=NetworkMode.PUBLIC
|
|
115
|
+
)
|
|
116
|
+
"""网络访问模式 / Network access mode"""
|
|
117
|
+
security_group_id: Optional[str] = Field(
|
|
118
|
+
alias="securityGroupId", default=None
|
|
119
|
+
)
|
|
120
|
+
"""安全组 ID"""
|
|
121
|
+
vpc_id: Optional[str] = Field(alias="vpcId", default=None)
|
|
122
|
+
"""私有网络 ID"""
|
|
123
|
+
vswitch_ids: Optional[List[str]] = Field(alias="vswitchIds", default=None)
|
|
124
|
+
"""私有网络交换机 ID 列表"""
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class PageableInput(BaseModel):
|
|
128
|
+
page_number: Optional[int] = None
|
|
129
|
+
"""页码"""
|
|
130
|
+
page_size: Optional[int] = None
|
|
131
|
+
"""页大小"""
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class Status(str, Enum):
|
|
135
|
+
"""Agent Runtime 状态"""
|
|
136
|
+
|
|
137
|
+
CREATING = "CREATING"
|
|
138
|
+
CREATE_FAILED = "CREATE_FAILED"
|
|
139
|
+
UPDATING = "UPDATING"
|
|
140
|
+
UPDATE_FAILED = "UPDATE_FAILED"
|
|
141
|
+
READY = "READY"
|
|
142
|
+
DELETING = "DELETING"
|
|
143
|
+
DELETE_FAILED = "DELETE_FAILED"
|
|
144
|
+
|
|
145
|
+
@staticmethod
|
|
146
|
+
def is_final_status(status: Optional["Status"] = None) -> bool:
|
|
147
|
+
"""判断状态是否为最终状态"""
|
|
148
|
+
return status in {
|
|
149
|
+
None,
|
|
150
|
+
Status.READY,
|
|
151
|
+
Status.CREATE_FAILED,
|
|
152
|
+
Status.UPDATE_FAILED,
|
|
153
|
+
Status.DELETE_FAILED,
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
def is_final(self) -> bool:
|
|
157
|
+
"""判断状态是否为最终状态"""
|
|
158
|
+
return Status.is_final_status(self)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
__all__ = [
|
|
162
|
+
"BaseModel",
|
|
163
|
+
"Field",
|
|
164
|
+
"NetworkMode",
|
|
165
|
+
"NetworkConfig",
|
|
166
|
+
"PageableInput",
|
|
167
|
+
"Status",
|
|
168
|
+
]
|