agentrun-sdk 0.0.4__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.
Files changed (128) hide show
  1. agentrun/__init__.py +209 -0
  2. agentrun/agent_runtime/__client_async_template.py +466 -0
  3. agentrun/agent_runtime/__endpoint_async_template.py +345 -0
  4. agentrun/agent_runtime/__init__.py +53 -0
  5. agentrun/agent_runtime/__runtime_async_template.py +477 -0
  6. agentrun/agent_runtime/api/__data_async_template.py +58 -0
  7. agentrun/agent_runtime/api/__init__.py +6 -0
  8. agentrun/agent_runtime/api/control.py +1362 -0
  9. agentrun/agent_runtime/api/data.py +98 -0
  10. agentrun/agent_runtime/client.py +868 -0
  11. agentrun/agent_runtime/endpoint.py +649 -0
  12. agentrun/agent_runtime/model.py +362 -0
  13. agentrun/agent_runtime/runtime.py +904 -0
  14. agentrun/credential/__client_async_template.py +177 -0
  15. agentrun/credential/__credential_async_template.py +216 -0
  16. agentrun/credential/__init__.py +28 -0
  17. agentrun/credential/api/__init__.py +5 -0
  18. agentrun/credential/api/control.py +606 -0
  19. agentrun/credential/client.py +319 -0
  20. agentrun/credential/credential.py +381 -0
  21. agentrun/credential/model.py +248 -0
  22. agentrun/integration/__init__.py +21 -0
  23. agentrun/integration/agentscope/__init__.py +12 -0
  24. agentrun/integration/agentscope/adapter.py +17 -0
  25. agentrun/integration/agentscope/builtin.py +65 -0
  26. agentrun/integration/agentscope/message_adapter.py +185 -0
  27. agentrun/integration/agentscope/model_adapter.py +60 -0
  28. agentrun/integration/agentscope/tool_adapter.py +59 -0
  29. agentrun/integration/builtin/__init__.py +16 -0
  30. agentrun/integration/builtin/model.py +97 -0
  31. agentrun/integration/builtin/sandbox.py +276 -0
  32. agentrun/integration/builtin/toolset.py +47 -0
  33. agentrun/integration/crewai/__init__.py +12 -0
  34. agentrun/integration/crewai/adapter.py +9 -0
  35. agentrun/integration/crewai/builtin.py +65 -0
  36. agentrun/integration/crewai/model_adapter.py +27 -0
  37. agentrun/integration/crewai/tool_adapter.py +26 -0
  38. agentrun/integration/google_adk/__init__.py +12 -0
  39. agentrun/integration/google_adk/adapter.py +15 -0
  40. agentrun/integration/google_adk/builtin.py +65 -0
  41. agentrun/integration/google_adk/message_adapter.py +144 -0
  42. agentrun/integration/google_adk/model_adapter.py +43 -0
  43. agentrun/integration/google_adk/tool_adapter.py +25 -0
  44. agentrun/integration/langchain/__init__.py +9 -0
  45. agentrun/integration/langchain/adapter.py +15 -0
  46. agentrun/integration/langchain/builtin.py +71 -0
  47. agentrun/integration/langchain/message_adapter.py +141 -0
  48. agentrun/integration/langchain/model_adapter.py +37 -0
  49. agentrun/integration/langchain/tool_adapter.py +50 -0
  50. agentrun/integration/langgraph/__init__.py +13 -0
  51. agentrun/integration/langgraph/adapter.py +20 -0
  52. agentrun/integration/langgraph/builtin.py +65 -0
  53. agentrun/integration/pydantic_ai/__init__.py +12 -0
  54. agentrun/integration/pydantic_ai/adapter.py +13 -0
  55. agentrun/integration/pydantic_ai/builtin.py +65 -0
  56. agentrun/integration/pydantic_ai/model_adapter.py +44 -0
  57. agentrun/integration/pydantic_ai/tool_adapter.py +19 -0
  58. agentrun/integration/utils/__init__.py +112 -0
  59. agentrun/integration/utils/adapter.py +167 -0
  60. agentrun/integration/utils/canonical.py +157 -0
  61. agentrun/integration/utils/converter.py +134 -0
  62. agentrun/integration/utils/model.py +107 -0
  63. agentrun/integration/utils/tool.py +1714 -0
  64. agentrun/model/__client_async_template.py +357 -0
  65. agentrun/model/__init__.py +57 -0
  66. agentrun/model/__model_proxy_async_template.py +270 -0
  67. agentrun/model/__model_service_async_template.py +267 -0
  68. agentrun/model/api/__init__.py +6 -0
  69. agentrun/model/api/control.py +1173 -0
  70. agentrun/model/api/data.py +196 -0
  71. agentrun/model/client.py +674 -0
  72. agentrun/model/model.py +218 -0
  73. agentrun/model/model_proxy.py +439 -0
  74. agentrun/model/model_service.py +438 -0
  75. agentrun/sandbox/__browser_sandbox_async_template.py +113 -0
  76. agentrun/sandbox/__client_async_template.py +466 -0
  77. agentrun/sandbox/__code_interpreter_sandbox_async_template.py +466 -0
  78. agentrun/sandbox/__init__.py +54 -0
  79. agentrun/sandbox/__sandbox_async_template.py +398 -0
  80. agentrun/sandbox/__template_async_template.py +150 -0
  81. agentrun/sandbox/api/__browser_data_async_template.py +140 -0
  82. agentrun/sandbox/api/__code_interpreter_data_async_template.py +206 -0
  83. agentrun/sandbox/api/__init__.py +17 -0
  84. agentrun/sandbox/api/__sandbox_data_async_template.py +100 -0
  85. agentrun/sandbox/api/browser_data.py +172 -0
  86. agentrun/sandbox/api/code_interpreter_data.py +396 -0
  87. agentrun/sandbox/api/control.py +1051 -0
  88. agentrun/sandbox/api/playwright_async.py +492 -0
  89. agentrun/sandbox/api/playwright_sync.py +492 -0
  90. agentrun/sandbox/api/sandbox_data.py +140 -0
  91. agentrun/sandbox/browser_sandbox.py +191 -0
  92. agentrun/sandbox/client.py +878 -0
  93. agentrun/sandbox/code_interpreter_sandbox.py +829 -0
  94. agentrun/sandbox/model.py +269 -0
  95. agentrun/sandbox/sandbox.py +737 -0
  96. agentrun/sandbox/template.py +215 -0
  97. agentrun/server/__init__.py +82 -0
  98. agentrun/server/invoker.py +131 -0
  99. agentrun/server/model.py +225 -0
  100. agentrun/server/openai_protocol.py +798 -0
  101. agentrun/server/protocol.py +96 -0
  102. agentrun/server/server.py +192 -0
  103. agentrun/toolset/__client_async_template.py +62 -0
  104. agentrun/toolset/__init__.py +51 -0
  105. agentrun/toolset/__toolset_async_template.py +204 -0
  106. agentrun/toolset/api/__init__.py +17 -0
  107. agentrun/toolset/api/control.py +262 -0
  108. agentrun/toolset/api/mcp.py +100 -0
  109. agentrun/toolset/api/openapi.py +1184 -0
  110. agentrun/toolset/client.py +102 -0
  111. agentrun/toolset/model.py +160 -0
  112. agentrun/toolset/toolset.py +271 -0
  113. agentrun/utils/__data_api_async_template.py +715 -0
  114. agentrun/utils/__init__.py +5 -0
  115. agentrun/utils/__resource_async_template.py +158 -0
  116. agentrun/utils/config.py +258 -0
  117. agentrun/utils/control_api.py +78 -0
  118. agentrun/utils/data_api.py +1110 -0
  119. agentrun/utils/exception.py +149 -0
  120. agentrun/utils/helper.py +34 -0
  121. agentrun/utils/log.py +77 -0
  122. agentrun/utils/model.py +168 -0
  123. agentrun/utils/resource.py +291 -0
  124. agentrun_sdk-0.0.4.dist-info/METADATA +262 -0
  125. agentrun_sdk-0.0.4.dist-info/RECORD +128 -0
  126. agentrun_sdk-0.0.4.dist-info/WHEEL +5 -0
  127. agentrun_sdk-0.0.4.dist-info/licenses/LICENSE +201 -0
  128. agentrun_sdk-0.0.4.dist-info/top_level.txt +1 -0
@@ -0,0 +1,215 @@
1
+ """
2
+ This file is auto generated by the code generation script.
3
+ Do not modify this file manually.
4
+ Use the `make codegen` command to regenerate.
5
+
6
+ 当前文件为自动生成的控制 API 客户端代码。请勿手动修改此文件。
7
+ 使用 `make codegen` 命令重新生成。
8
+
9
+ source: agentrun/sandbox/__template_async_template.py
10
+
11
+ Template 高层 API / Template High-Level API
12
+
13
+ 此模块定义沙箱模板资源的高级API。
14
+ This module defines the high-level API for sandbox template resources.
15
+ """
16
+
17
+ from typing import Dict, List, Optional
18
+
19
+ from agentrun.sandbox.model import (
20
+ PageableInput,
21
+ TemplateContainerConfiguration,
22
+ TemplateCredentialConfiguration,
23
+ TemplateInput,
24
+ TemplateLogConfiguration,
25
+ TemplateMcpOptions,
26
+ TemplateMcpState,
27
+ TemplateNetworkConfiguration,
28
+ TemplateOssConfiguration,
29
+ TemplateType,
30
+ )
31
+ from agentrun.utils.config import Config
32
+ from agentrun.utils.model import BaseModel
33
+
34
+
35
+ class Template(BaseModel):
36
+ """Template 实例
37
+
38
+ 封装了 Template 的基本信息和操作方法
39
+ """
40
+
41
+ template_id: Optional[str] = None
42
+ """模板 ID"""
43
+ template_name: Optional[str] = None
44
+ """模板名称"""
45
+ template_version: Optional[str] = None
46
+ """模板版本"""
47
+ template_arn: Optional[str] = None
48
+ """模板 ARN"""
49
+ resource_name: Optional[str] = None
50
+ """资源名称"""
51
+ template_type: Optional[TemplateType] = None
52
+ """模板类型"""
53
+ cpu: Optional[float] = None
54
+ """CPU 核数"""
55
+ memory: Optional[int] = None
56
+ """内存大小(MB)"""
57
+ disk_size: Optional[int] = None
58
+ """磁盘大小(GB)"""
59
+ description: Optional[str] = None
60
+ """描述"""
61
+ execution_role_arn: Optional[str] = None
62
+ """执行角色 ARN"""
63
+ sandbox_idle_timeout_in_seconds: Optional[int] = None
64
+ """沙箱空闲超时时间(秒)"""
65
+ sandbox_ttlin_seconds: Optional[int] = None
66
+ """沙箱存活时间(秒)"""
67
+ share_concurrency_limit_per_sandbox: Optional[int] = None
68
+ """每个沙箱的最大并发会话数"""
69
+ template_configuration: Optional[Dict] = None
70
+ """模板配置"""
71
+ environment_variables: Optional[Dict] = None
72
+ """环境变量"""
73
+ network_configuration: Optional[TemplateNetworkConfiguration] = None
74
+ """网络配置"""
75
+ oss_configuration: Optional[List[TemplateOssConfiguration]] = None
76
+ """OSS 配置列表"""
77
+ log_configuration: Optional[TemplateLogConfiguration] = None
78
+ """日志配置"""
79
+ credential_configuration: Optional[TemplateCredentialConfiguration] = None
80
+ """凭证配置"""
81
+ container_configuration: Optional[TemplateContainerConfiguration] = None
82
+ """容器配置"""
83
+ mcp_options: Optional[TemplateMcpOptions] = None
84
+ """MCP 选项"""
85
+ mcp_state: Optional[TemplateMcpState] = None
86
+ """MCP 状态"""
87
+ created_at: Optional[str] = None
88
+ """创建时间"""
89
+ last_updated_at: Optional[str] = None
90
+ """最后更新时间"""
91
+ status: Optional[str] = None
92
+ """状态"""
93
+ status_reason: Optional[str] = None
94
+ """状态原因"""
95
+
96
+ @classmethod
97
+ def __get_client(cls, config: Optional[Config] = None):
98
+ """获取 Sandbox 客户端"""
99
+ from .client import SandboxClient
100
+
101
+ return SandboxClient(config)
102
+
103
+ @classmethod
104
+ async def create_async(
105
+ cls, input: TemplateInput, config: Optional[Config] = None
106
+ ):
107
+ return await cls.__get_client(config=config).create_template_async(
108
+ input, config=config
109
+ )
110
+
111
+ @classmethod
112
+ def create(cls, input: TemplateInput, config: Optional[Config] = None):
113
+ return cls.__get_client(config=config).create_template(
114
+ input, config=config
115
+ )
116
+
117
+ @classmethod
118
+ async def delete_by_name_async(
119
+ cls, template_name: str, config: Optional[Config] = None
120
+ ):
121
+ return await cls.__get_client(config=config).delete_template_async(
122
+ template_name=template_name, config=config
123
+ )
124
+
125
+ @classmethod
126
+ def delete_by_name(
127
+ cls, template_name: str, config: Optional[Config] = None
128
+ ):
129
+ return cls.__get_client(config=config).delete_template(
130
+ template_name=template_name, config=config
131
+ )
132
+
133
+ @classmethod
134
+ async def update_by_name_async(
135
+ cls,
136
+ template_name: str,
137
+ input: TemplateInput,
138
+ config: Optional[Config] = None,
139
+ ):
140
+ return await cls.__get_client(config=config).update_template_async(
141
+ template_name=template_name, input=input, config=config
142
+ )
143
+
144
+ @classmethod
145
+ def update_by_name(
146
+ cls,
147
+ template_name: str,
148
+ input: TemplateInput,
149
+ config: Optional[Config] = None,
150
+ ):
151
+ return cls.__get_client(config=config).update_template(
152
+ template_name=template_name, input=input, config=config
153
+ )
154
+
155
+ @classmethod
156
+ async def get_by_name_async(
157
+ cls, template_name: str, config: Optional[Config] = None
158
+ ):
159
+ return await cls.__get_client(config=config).get_template_async(
160
+ template_name=template_name, config=config
161
+ )
162
+
163
+ @classmethod
164
+ def get_by_name(cls, template_name: str, config: Optional[Config] = None):
165
+ return cls.__get_client(config=config).get_template(
166
+ template_name=template_name, config=config
167
+ )
168
+
169
+ @classmethod
170
+ async def list_templates_async(
171
+ cls,
172
+ input: Optional[PageableInput] = None,
173
+ config: Optional[Config] = None,
174
+ ):
175
+ return await cls.__get_client(config=config).list_templates_async(
176
+ input, config=config
177
+ )
178
+
179
+ @classmethod
180
+ def list_templates(
181
+ cls,
182
+ input: Optional[PageableInput] = None,
183
+ config: Optional[Config] = None,
184
+ ):
185
+ return cls.__get_client(config=config).list_templates(
186
+ input, config=config
187
+ )
188
+
189
+ async def create_sandbox_async(
190
+ self,
191
+ sandbox_idle_timeout_seconds: Optional[int] = None,
192
+ config: Optional[Config] = None,
193
+ ):
194
+ if self.template_name is None:
195
+ raise ValueError("Template name is required")
196
+
197
+ return await self.__get_client(config=config).create_sandbox_async(
198
+ self.template_name,
199
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
200
+ config=config,
201
+ )
202
+
203
+ def create_sandbox(
204
+ self,
205
+ sandbox_idle_timeout_seconds: Optional[int] = None,
206
+ config: Optional[Config] = None,
207
+ ):
208
+ if self.template_name is None:
209
+ raise ValueError("Template name is required")
210
+
211
+ return self.__get_client(config=config).create_sandbox(
212
+ self.template_name,
213
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
214
+ config=config,
215
+ )
@@ -0,0 +1,82 @@
1
+ """AgentRun Server 模块 / AgentRun Server Module
2
+
3
+ 提供 HTTP Server 集成能力,支持符合 AgentRun 规范的 Agent 调用接口。
4
+
5
+ Example (基本使用):
6
+ >>> from agentrun.server import AgentRunServer, AgentRequest, AgentResponse
7
+ >>>
8
+ >>> def invoke_agent(request: AgentRequest) -> AgentResponse:
9
+ ... # 实现你的 Agent 逻辑
10
+ ... return AgentResponse(...)
11
+ >>>
12
+ >>> server = AgentRunServer(invoke_agent=invoke_agent)
13
+ >>> server.start(host="0.0.0.0", port=8080)
14
+
15
+ Example (异步处理):
16
+ >>> async def invoke_agent(request: AgentRequest) -> AgentResponse:
17
+ ... # 异步实现你的 Agent 逻辑
18
+ ... return AgentResponse(...)
19
+ >>>
20
+ >>> server = AgentRunServer(invoke_agent=invoke_agent)
21
+ >>> server.start()
22
+
23
+ Example (流式响应):
24
+ >>> async def invoke_agent(request: AgentRequest):
25
+ ... if request.stream:
26
+ ... async def stream():
27
+ ... for chunk in generate_chunks():
28
+ ... yield AgentStreamResponse(...)
29
+ ... return stream()
30
+ ... return AgentResponse(...)"""
31
+
32
+ from .model import (
33
+ AgentRequest,
34
+ AgentResponse,
35
+ AgentResponseChoice,
36
+ AgentResponseUsage,
37
+ AgentResult,
38
+ AgentRunResult,
39
+ AgentStreamIterator,
40
+ AgentStreamResponse,
41
+ AgentStreamResponseChoice,
42
+ AgentStreamResponseDelta,
43
+ Message,
44
+ MessageRole,
45
+ Tool,
46
+ ToolCall,
47
+ )
48
+ from .openai_protocol import OpenAIProtocolHandler
49
+ from .protocol import (
50
+ AsyncInvokeAgentHandler,
51
+ InvokeAgentHandler,
52
+ ProtocolHandler,
53
+ SyncInvokeAgentHandler,
54
+ )
55
+ from .server import AgentRunServer
56
+
57
+ __all__ = [
58
+ # Server
59
+ "AgentRunServer",
60
+ # Request/Response Models
61
+ "AgentRequest",
62
+ "AgentResponse",
63
+ "AgentResponseChoice",
64
+ "AgentResponseUsage",
65
+ "AgentRunResult",
66
+ "AgentStreamResponse",
67
+ "AgentStreamResponseChoice",
68
+ "AgentStreamResponseDelta",
69
+ "Message",
70
+ "MessageRole",
71
+ "Tool",
72
+ "ToolCall",
73
+ # Type Aliases
74
+ "AgentResult",
75
+ "AgentStreamIterator",
76
+ "InvokeAgentHandler",
77
+ "AsyncInvokeAgentHandler",
78
+ "SyncInvokeAgentHandler",
79
+ # Protocol
80
+ "ProtocolHandler",
81
+ "OpenAIProtocolHandler",
82
+ ]
@@ -0,0 +1,131 @@
1
+ """Agent 调用器 / Agent Invoker
2
+
3
+ 负责处理 Agent 调用的通用逻辑。
4
+ Handles common logic for agent invocations.
5
+ """
6
+
7
+ import asyncio
8
+ import inspect
9
+ from typing import cast
10
+
11
+ from .model import AgentRequest, AgentResult, AgentRunResult
12
+ from .protocol import (
13
+ AsyncInvokeAgentHandler,
14
+ InvokeAgentHandler,
15
+ SyncInvokeAgentHandler,
16
+ )
17
+
18
+
19
+ class AgentInvoker:
20
+ """Agent 调用器
21
+
22
+ 职责:
23
+ 1. 调用用户的 invoke_agent
24
+ 2. 处理同步/异步调用
25
+ 3. 自动转换 string/string迭代器为 AgentRunResult
26
+ 4. 错误处理
27
+
28
+ Example:
29
+ >>> def my_agent(request: AgentRequest) -> str:
30
+ ... return "Hello" # 自动转换为 AgentRunResult
31
+ >>>
32
+ >>> invoker = AgentInvoker(my_agent)
33
+ >>> result = await invoker.invoke(AgentRequest(...))
34
+ >>> # result 是 AgentRunResult 对象
35
+ """
36
+
37
+ def __init__(self, invoke_agent: InvokeAgentHandler):
38
+ """初始化 Agent 调用器
39
+
40
+ Args:
41
+ invoke_agent: Agent 处理函数,可以是同步或异步
42
+ """
43
+ self.invoke_agent = invoke_agent
44
+ self.is_async = inspect.iscoroutinefunction(invoke_agent)
45
+
46
+ async def invoke(self, request: AgentRequest) -> AgentResult:
47
+ """调用 Agent 并返回结果
48
+
49
+ 自动处理各种返回类型:
50
+ - string 或 string 迭代器 -> 转换为 AgentRunResult
51
+ - AgentRunResult -> 直接返回
52
+ - AgentResponse/ModelResponse -> 直接返回
53
+
54
+ Args:
55
+ request: AgentRequest 请求对象
56
+
57
+ Returns:
58
+ AgentResult: Agent 返回的结果
59
+
60
+ Raises:
61
+ Exception: Agent 执行中的任何异常
62
+ """
63
+ if self.is_async:
64
+ # 异步 handler
65
+ async_handler = cast(AsyncInvokeAgentHandler, self.invoke_agent)
66
+ result = await async_handler(request)
67
+ else:
68
+ # 同步 handler: 在线程池中运行,避免阻塞事件循环
69
+ sync_handler = cast(SyncInvokeAgentHandler, self.invoke_agent)
70
+ result = await asyncio.get_event_loop().run_in_executor(
71
+ None, sync_handler, request
72
+ )
73
+
74
+ # 自动转换 string 或 string 迭代器为 AgentRunResult
75
+ result = self._normalize_result(result)
76
+
77
+ return result
78
+
79
+ def _normalize_result(self, result: AgentResult) -> AgentResult:
80
+ """标准化返回结果
81
+
82
+ 将 string 或 string 迭代器自动转换为 AgentRunResult。
83
+
84
+ Args:
85
+ result: 原始返回结果
86
+
87
+ Returns:
88
+ AgentResult: 标准化后的结果
89
+ """
90
+ # 如果是字符串,转换为 AgentRunResult
91
+ if isinstance(result, str):
92
+ return AgentRunResult(content=result)
93
+
94
+ # 如果是迭代器,检查是否是字符串迭代器
95
+ if self._is_string_iterator(result):
96
+ return AgentRunResult(content=result) # type: ignore
97
+
98
+ # 其他类型直接返回
99
+ return result
100
+
101
+ def _is_string_iterator(self, obj) -> bool:
102
+ """检查是否是字符串迭代器
103
+
104
+ 通过类型注解或启发式方法判断。
105
+
106
+ Args:
107
+ obj: 要检查的对象
108
+
109
+ Returns:
110
+ bool: 是否是字符串迭代器
111
+ """
112
+ # 排除已知的复杂类型
113
+ from .model import AgentResponse, AgentRunResult
114
+
115
+ if isinstance(obj, (AgentResponse, AgentRunResult, str, dict)):
116
+ return False
117
+
118
+ # 检查是否是迭代器
119
+ is_iterator = (
120
+ hasattr(obj, "__iter__") and not isinstance(obj, (str, bytes, dict))
121
+ ) or hasattr(obj, "__aiter__")
122
+
123
+ if not is_iterator:
124
+ return False
125
+
126
+ # 启发式判断: 如果没有 choices 属性,很可能是字符串迭代器
127
+ # (AgentResponse/ModelResponse 都有 choices 属性)
128
+ if hasattr(obj, "choices") or hasattr(obj, "model"):
129
+ return False
130
+
131
+ return True
@@ -0,0 +1,225 @@
1
+ """AgentRun Server 模型定义 / AgentRun Server 模型Defines
2
+
3
+ 定义 invokeAgent callback 的参数结构和响应类型"""
4
+
5
+ from enum import Enum
6
+ from typing import (
7
+ Any,
8
+ AsyncIterator,
9
+ Dict,
10
+ Iterator,
11
+ List,
12
+ Optional,
13
+ TYPE_CHECKING,
14
+ Union,
15
+ )
16
+
17
+ from pydantic import BaseModel, Field
18
+
19
+ if TYPE_CHECKING:
20
+ # 运行时不导入,避免依赖问题
21
+ from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
22
+ from litellm.types.utils import ModelResponse
23
+
24
+
25
+ class MessageRole(str, Enum):
26
+ """消息角色"""
27
+
28
+ SYSTEM = "system"
29
+ USER = "user"
30
+ ASSISTANT = "assistant"
31
+ TOOL = "tool"
32
+
33
+
34
+ class Message(BaseModel):
35
+ """消息体"""
36
+
37
+ role: MessageRole
38
+ content: Optional[str] = None
39
+ name: Optional[str] = None
40
+ tool_calls: Optional[List[Dict[str, Any]]] = None
41
+ tool_call_id: Optional[str] = None
42
+
43
+
44
+ class ToolCall(BaseModel):
45
+ """工具调用"""
46
+
47
+ id: str
48
+ type: str = "function"
49
+ function: Dict[str, Any]
50
+
51
+
52
+ class Tool(BaseModel):
53
+ """工具定义 / 工具Defines"""
54
+
55
+ type: str = "function"
56
+ function: Dict[str, Any]
57
+
58
+
59
+ class AgentRequest(BaseModel):
60
+ """Agent 请求参数
61
+
62
+ invokeAgent callback 接收的参数结构
63
+ 符合 OpenAI Completions API 格式
64
+ """
65
+
66
+ # 必需参数
67
+ messages: List[Message] = Field(..., description="对话历史消息列表")
68
+
69
+ # 可选参数
70
+ model: Optional[str] = Field(None, description="模型名称")
71
+ stream: bool = Field(False, description="是否使用流式输出")
72
+ temperature: Optional[float] = Field(
73
+ None, description="采样温度", ge=0.0, le=2.0
74
+ )
75
+ top_p: Optional[float] = Field(
76
+ None, description="核采样参数", ge=0.0, le=1.0
77
+ )
78
+ max_tokens: Optional[int] = Field(
79
+ None, description="最大生成 token 数", gt=0
80
+ )
81
+ tools: Optional[List[Tool]] = Field(None, description="可用的工具列表")
82
+ tool_choice: Optional[Union[str, Dict[str, Any]]] = Field(
83
+ None, description="工具选择策略"
84
+ )
85
+ user: Optional[str] = Field(None, description="用户标识")
86
+
87
+ # 扩展参数
88
+ extra: Dict[str, Any] = Field(
89
+ default_factory=dict, description="其他自定义参数"
90
+ )
91
+
92
+
93
+ class AgentResponseChoice(BaseModel):
94
+ """响应选项"""
95
+
96
+ index: int
97
+ message: Message
98
+ finish_reason: Optional[str] = None
99
+
100
+
101
+ class AgentResponseUsage(BaseModel):
102
+ """Token 使用统计"""
103
+
104
+ prompt_tokens: int = 0
105
+ completion_tokens: int = 0
106
+ total_tokens: int = 0
107
+
108
+
109
+ class AgentRunResult(BaseModel):
110
+ """Agent 运行结果
111
+
112
+ 核心数据结构,用于表示 Agent 执行结果。
113
+ content 字段支持字符串或字符串迭代器。
114
+
115
+ Example:
116
+ >>> # 返回字符串
117
+ >>> AgentRunResult(content="Hello, world!")
118
+ >>>
119
+ >>> # 返回字符串迭代器(流式)
120
+ >>> def stream():
121
+ ... yield "Hello, "
122
+ ... yield "world!"
123
+ >>> AgentRunResult(content=stream())
124
+ """
125
+
126
+ model_config = {"arbitrary_types_allowed": True}
127
+
128
+ content: Union[str, Iterator[str], AsyncIterator[str], Any]
129
+ """响应内容,支持字符串或字符串迭代器 / 响应内容,Supports字符串或字符串迭代器"""
130
+
131
+
132
+ class AgentResponse(BaseModel):
133
+ """Agent 响应(非流式)
134
+
135
+ 灵活的响应数据结构,所有字段都是可选的。
136
+ 用户可以只填充需要的字段,协议层会根据实际协议格式补充或跳过字段。
137
+
138
+ Example:
139
+ >>> # 最简单 - 只返回内容
140
+ >>> AgentResponse(content="Hello")
141
+ >>>
142
+ >>> # OpenAI 格式 - 完整字段
143
+ >>> AgentResponse(
144
+ ... id="chatcmpl-123",
145
+ ... model="gpt-4",
146
+ ... choices=[...]
147
+ ... )
148
+ """
149
+
150
+ # 核心字段 - 协议无关
151
+ content: Optional[str] = None
152
+ """响应内容"""
153
+
154
+ # OpenAI 协议字段 - 可选
155
+ id: Optional[str] = Field(None, description="响应 ID")
156
+ object: Optional[str] = Field(None, description="对象类型")
157
+ created: Optional[int] = Field(None, description="创建时间戳")
158
+ model: Optional[str] = Field(None, description="使用的模型")
159
+ choices: Optional[List[AgentResponseChoice]] = Field(
160
+ None, description="响应选项列表"
161
+ )
162
+ usage: Optional[AgentResponseUsage] = Field(
163
+ None, description="Token 使用情况"
164
+ )
165
+
166
+ # 扩展字段 - 其他协议可能需要
167
+ extra: Dict[str, Any] = Field(
168
+ default_factory=dict, description="协议特定的额外字段"
169
+ )
170
+
171
+
172
+ class AgentStreamResponseDelta(BaseModel):
173
+ """流式响应增量"""
174
+
175
+ role: Optional[MessageRole] = None
176
+ content: Optional[str] = None
177
+ tool_calls: Optional[List[Dict[str, Any]]] = None
178
+
179
+
180
+ class AgentStreamResponse(BaseModel):
181
+ """流式响应块"""
182
+
183
+ id: Optional[str] = None
184
+ object: Optional[str] = None
185
+ created: Optional[int] = None
186
+ model: Optional[str] = None
187
+ choices: Optional[List["AgentStreamResponseChoice"]] = None
188
+ extra: Dict[str, Any] = Field(default_factory=dict)
189
+
190
+
191
+ class AgentStreamResponseChoice(BaseModel):
192
+ """流式响应选项"""
193
+
194
+ index: int
195
+ delta: AgentStreamResponseDelta
196
+ finish_reason: Optional[str] = None
197
+
198
+
199
+ # 类型别名 - 流式响应迭代器
200
+ AgentStreamIterator = Union[
201
+ Iterator[AgentResponse],
202
+ AsyncIterator[AgentResponse],
203
+ ]
204
+
205
+ # Model Service 类型 - 直接返回 litellm 的 ModelResponse
206
+ if TYPE_CHECKING:
207
+ ModelServiceResult = Union["ModelResponse", "CustomStreamWrapper"]
208
+ else:
209
+ ModelServiceResult = Any # 运行时使用 Any
210
+
211
+ # AgentResult - 支持多种返回形式
212
+ # 用户可以返回:
213
+ # 1. string 或 string 迭代器 - 自动转换为 AgentRunResult
214
+ # 2. AgentRunResult - 核心数据结构
215
+ # 3. AgentResponse - 完整响应对象
216
+ # 4. ModelResponse - Model Service 响应
217
+ AgentResult = Union[
218
+ str, # 简化: 直接返回字符串
219
+ Iterator[str], # 简化: 字符串流
220
+ AsyncIterator[str], # 简化: 异步字符串流
221
+ AgentRunResult, # 核心: AgentRunResult 对象
222
+ AgentResponse, # 完整: AgentResponse 对象
223
+ AgentStreamIterator, # 流式: AgentResponse 流
224
+ ModelServiceResult, # Model Service: ModelResponse 或 CustomStreamWrapper
225
+ ]