|
20 | 20 | if TYPE_CHECKING: |
21 | 21 | from agentrun.super_agent.agent import SuperAgent |
22 | 22 |
|
23 | | -from alibabacloud_agentrun20250910.client import Client as _DaraClient |
24 | | -from alibabacloud_agentrun20250910.models import ( |
25 | | - CreateAgentRuntimeInput as _DaraCreateAgentRuntimeInput, |
26 | | -) |
27 | | -from alibabacloud_agentrun20250910.models import ( |
28 | | - ListAgentRuntimesRequest as _DaraListAgentRuntimesRequest, |
29 | | -) |
30 | 23 | from alibabacloud_agentrun20250910.models import ( |
31 | 24 | ProtocolConfiguration as _DaraProtocolConfiguration, |
32 | 25 | ) |
33 | | -from alibabacloud_agentrun20250910.models import ( |
34 | | - UpdateAgentRuntimeInput as _DaraUpdateAgentRuntimeInput, |
35 | | -) |
36 | 26 | from pydantic import Field |
37 | 27 |
|
38 | 28 | from agentrun.agent_runtime.model import ( |
|
50 | 40 | API_VERSION = "2025-09-10" |
51 | 41 | SUPER_AGENT_PROTOCOL_TYPE = "SUPER_AGENT" |
52 | 42 | # ``SUPER_AGENT_TAG`` 标识下游 AgentRuntime 是超级 Agent, 用于 list 过滤。 |
| 43 | +# 写入 ``systemTags`` 字段 (由服务端原生支持), create/update/list 的 system_tags 参数统一使用。 |
53 | 44 | SUPER_AGENT_TAG = "x-agentrun-super" |
54 | 45 | # ``EXTERNAL_TAG`` 标识下游 AgentRuntime 由外部 (SuperAgent) 托管调用, 不由 AgentRun 直接托管。 |
55 | 46 | # 保留常量以便外部消费者引用; 创建超级 Agent 时不再写入此 tag。 |
56 | 47 | EXTERNAL_TAG = "x-agentrun-external" |
57 | | -# 创建下游 AgentRuntime 时固定写入的 tag 列表: ``[SUPER_AGENT_TAG]`` (仅一个)。 |
| 48 | +# 创建下游 AgentRuntime 时固定写入的 systemTags 列表: ``[SUPER_AGENT_TAG]`` (仅一个)。 |
58 | 49 | SUPER_AGENT_CREATE_TAGS = [SUPER_AGENT_TAG] |
59 | 50 | SUPER_AGENT_RESOURCE_PATH = "__SUPER_AGENT__" |
60 | 51 | SUPER_AGENT_INVOKE_PATH = "/invoke" |
@@ -153,16 +144,15 @@ def model_dump(self, **kwargs: Any) -> Dict[str, Any]: |
153 | 144 | return super().model_dump(**kwargs) |
154 | 145 |
|
155 | 146 |
|
156 | | -# ─── Dara 模型/客户端猴补丁 ────────────────────────────────────── |
157 | | -# 当前版 Dara SDK 缺 ``ProtocolConfiguration.externalEndpoint`` 和 |
158 | | -# ``CreateAgentRuntimeInput/UpdateAgentRuntimeInput/ListAgentRuntimesRequest.tags`` |
159 | | -# 字段, 会在 Pydantic ↔ Dara ``from_map / to_map`` roundtrip 中静默丢失; 且 |
160 | | -# ``Client.list_agent_runtimes_with_options{,_async}`` 不会把 ``tags`` 写到 query。 |
| 147 | +# ─── Dara 模型猴补丁 ────────────────────────────────────── |
| 148 | +# 当前版 Dara SDK 缺 ``ProtocolConfiguration.externalEndpoint`` 字段, 会在 |
| 149 | +# Pydantic ↔ Dara ``from_map / to_map`` roundtrip 中静默丢失。补丁延迟到 |
| 150 | +# ``SuperAgentClient`` 实例化时 (见 ``ensure_super_agent_patches_applied``) 才 |
| 151 | +# 触发, 避免仅 import 本模块的调用方被动承担全局副作用。补丁用哨兵属性保证 |
| 152 | +# 幂等, 重复调用安全。TODO: 等 Dara SDK 原生支持后删除。 |
161 | 153 | # |
162 | | -# 所有补丁都延迟到 ``SuperAgentClient`` 实例化时 (见 |
163 | | -# ``ensure_super_agent_patches_applied``) 才触发, 避免仅 import 本模块的调用方 |
164 | | -# 被动承担全局副作用。补丁本身用哨兵属性保证幂等, 重复调用安全。 |
165 | | -# TODO: 等 Dara SDK 原生支持后删除。 |
| 154 | +# ``tags`` (原 hack 写入) 已由 SDK 原生 ``systemTags`` 字段替代, 不再需要任何 |
| 155 | +# 补丁; create/update/list 统一走 ``system_tags`` 参数。 |
166 | 156 |
|
167 | 157 |
|
168 | 158 | def _patch_dara_protocol_configuration() -> None: |
@@ -193,120 +183,14 @@ def _patched_from_map( |
193 | 183 | _DaraProtocolConfiguration._super_agent_patched = True # type: ignore[attr-defined] |
194 | 184 |
|
195 | 185 |
|
196 | | -def _patch_dara_tags(cls: Any) -> None: |
197 | | - """给 Dara model 补齐 ``tags`` 字段的 from_map/to_map 读写.""" |
198 | | - if getattr(cls, "_super_agent_tags_patched", False): |
199 | | - return |
200 | | - _orig_to_map = cls.to_map |
201 | | - _orig_from_map = cls.from_map |
202 | | - |
203 | | - def _patched_to_map(self: Any) -> Dict[str, Any]: |
204 | | - result = _orig_to_map(self) |
205 | | - tags = getattr(self, "tags", None) |
206 | | - if tags is not None: |
207 | | - result["tags"] = tags |
208 | | - return result |
209 | | - |
210 | | - def _patched_from_map(self: Any, m: Optional[Dict[str, Any]] = None) -> Any: |
211 | | - _orig_from_map(self, m) |
212 | | - if m and m.get("tags") is not None: |
213 | | - self.tags = m.get("tags") |
214 | | - return self |
215 | | - |
216 | | - cls.to_map = _patched_to_map # type: ignore[assignment] |
217 | | - cls.from_map = _patched_from_map # type: ignore[assignment] |
218 | | - cls._super_agent_tags_patched = True # type: ignore[attr-defined] |
219 | | - |
220 | | - |
221 | | -def _tags_query_value(tags: Any) -> Optional[str]: |
222 | | - if tags is None: |
223 | | - return None |
224 | | - if isinstance(tags, str): |
225 | | - return tags |
226 | | - if isinstance(tags, (list, tuple)): |
227 | | - return ",".join(str(t) for t in tags) |
228 | | - return str(tags) |
229 | | - |
230 | | - |
231 | | -def _patch_dara_client_list_tags() -> None: |
232 | | - """包裹 ``Client.list_agent_runtimes_with_options{,_async}``: 若 request 带 |
233 | | - ``tags`` 就在底层 ``call_api`` 调用前把 ``tags`` (列表 → 逗号分隔) 追加到 |
234 | | - ``req.query``。 |
235 | | -
|
236 | | - 每次 API 调用由 ``_get_client()`` 新建 ``Client`` 实例, 实例属性级别的 |
237 | | - ``self.call_api = _injecting`` 替换在并发下是安全的。 |
238 | | - """ |
239 | | - if getattr(_DaraClient, "_super_agent_list_tags_patched", False): |
240 | | - return |
241 | | - |
242 | | - _orig_sync = _DaraClient.list_agent_runtimes_with_options |
243 | | - _orig_async = _DaraClient.list_agent_runtimes_with_options_async |
244 | | - |
245 | | - def _patched_sync( |
246 | | - self: Any, request: Any, headers: Any, runtime: Any |
247 | | - ) -> Any: |
248 | | - tags_value = _tags_query_value(getattr(request, "tags", None)) |
249 | | - if tags_value is None: |
250 | | - return _orig_sync(self, request, headers, runtime) |
251 | | - orig_call_api = self.call_api |
252 | | - |
253 | | - def _injecting(params: Any, req: Any, rt: Any) -> Any: |
254 | | - if req.query is None: |
255 | | - req.query = {} |
256 | | - req.query["tags"] = tags_value |
257 | | - return orig_call_api(params, req, rt) |
258 | | - |
259 | | - self.call_api = _injecting |
260 | | - try: |
261 | | - return _orig_sync(self, request, headers, runtime) |
262 | | - finally: |
263 | | - try: |
264 | | - del self.call_api |
265 | | - except AttributeError: |
266 | | - pass |
267 | | - |
268 | | - async def _patched_async( |
269 | | - self: Any, request: Any, headers: Any, runtime: Any |
270 | | - ) -> Any: |
271 | | - tags_value = _tags_query_value(getattr(request, "tags", None)) |
272 | | - if tags_value is None: |
273 | | - return await _orig_async(self, request, headers, runtime) |
274 | | - orig_call_api_async = self.call_api_async |
275 | | - |
276 | | - async def _injecting(params: Any, req: Any, rt: Any) -> Any: |
277 | | - if req.query is None: |
278 | | - req.query = {} |
279 | | - req.query["tags"] = tags_value |
280 | | - return await orig_call_api_async(params, req, rt) |
281 | | - |
282 | | - self.call_api_async = _injecting |
283 | | - try: |
284 | | - return await _orig_async(self, request, headers, runtime) |
285 | | - finally: |
286 | | - try: |
287 | | - del self.call_api_async |
288 | | - except AttributeError: |
289 | | - pass |
290 | | - |
291 | | - _DaraClient.list_agent_runtimes_with_options = _patched_sync # type: ignore[assignment] |
292 | | - _DaraClient.list_agent_runtimes_with_options_async = _patched_async # type: ignore[assignment] |
293 | | - _DaraClient._super_agent_list_tags_patched = True # type: ignore[attr-defined] |
294 | | - |
295 | | - |
296 | 186 | def ensure_super_agent_patches_applied() -> None: |
297 | | - """按需应用全部 Dara SDK 兼容补丁 (幂等)。 |
| 187 | + """按需应用 Dara SDK 兼容补丁 (幂等)。 |
298 | 188 |
|
299 | 189 | 由 ``SuperAgentClient.__init__`` 调用。如果调用方直接使用 |
300 | | - ``to_create_input`` / ``to_update_input`` 并自己构造 ``CreateAgentRuntimeInput`` |
301 | | - / ``ListAgentRuntimesRequest``, 也应在 Pydantic → Dara 转换前调用一次本函数。 |
| 190 | + ``to_create_input`` / ``to_update_input`` 并自己构造 Dara 输入, 也应在 |
| 191 | + Pydantic → Dara 转换前调用一次本函数。 |
302 | 192 | """ |
303 | 193 | _patch_dara_protocol_configuration() |
304 | | - _patch_dara_tags(_DaraCreateAgentRuntimeInput) |
305 | | - _patch_dara_tags(_DaraUpdateAgentRuntimeInput) |
306 | | - # ``ListAgentRuntimesRequest`` 补齐 from_map/to_map 保留属性; 真正让服务端 |
307 | | - # 生效的 query 注入由 ``_patch_dara_client_list_tags`` 完成。 |
308 | | - _patch_dara_tags(_DaraListAgentRuntimesRequest) |
309 | | - _patch_dara_client_list_tags() |
310 | 194 |
|
311 | 195 |
|
312 | 196 | # ─── AgentRuntime ↔ SuperAgent 转换 ──────────────────────── |
@@ -415,7 +299,7 @@ def to_create_input( |
415 | 299 | agent_runtime_name=name, |
416 | 300 | description=description, |
417 | 301 | protocol_configuration=pc, |
418 | | - tags=list(SUPER_AGENT_CREATE_TAGS), |
| 302 | + system_tags=list(SUPER_AGENT_CREATE_TAGS), |
419 | 303 | # 超级 Agent 的数据面入口 (与 protocolConfiguration.externalEndpoint 同值)。 |
420 | 304 | external_agent_endpoint_url=build_super_agent_endpoint(cfg), |
421 | 305 | # 占位 artifact: SUPER_AGENT 不跑用户 container/code, 但服务端要求非空。 |
@@ -446,7 +330,7 @@ def to_update_input( |
446 | 330 | agent_runtime_name=name, |
447 | 331 | description=merged.get("description"), |
448 | 332 | protocol_configuration=pc, |
449 | | - tags=list(SUPER_AGENT_CREATE_TAGS), |
| 333 | + system_tags=list(SUPER_AGENT_CREATE_TAGS), |
450 | 334 | # 超级 Agent 的数据面入口 (与 protocolConfiguration.externalEndpoint 同值)。 |
451 | 335 | external_agent_endpoint_url=build_super_agent_endpoint(cfg), |
452 | 336 | # 占位 artifact: SUPER_AGENT 不跑用户 container/code, 但服务端要求非空。 |
|
0 commit comments