myagent-ai 1.20.11 → 1.20.12
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.
- package/agents/main_agent.py +17 -5
- package/package.json +1 -1
package/agents/main_agent.py
CHANGED
|
@@ -544,6 +544,18 @@ class MainAgent(BaseAgent):
|
|
|
544
544
|
text_delta_callback=None,
|
|
545
545
|
) -> AgentContext:
|
|
546
546
|
"""V2 内部循环逻辑 — 结构化输出 + 工具调度 + SSE 事件推送"""
|
|
547
|
+
|
|
548
|
+
# [v1.20.11] 安全发送 SSE 事件 — 兼容 sync/async 回调
|
|
549
|
+
async def _safe_sse(cb, event: dict):
|
|
550
|
+
"""安全调用 stream_callback,兼容同步和异步回调。"""
|
|
551
|
+
try:
|
|
552
|
+
if asyncio.iscoroutinefunction(cb):
|
|
553
|
+
await cb(event)
|
|
554
|
+
else:
|
|
555
|
+
cb(event)
|
|
556
|
+
except Exception as e:
|
|
557
|
+
logger.debug(f"SSE 事件发送失败: {e}")
|
|
558
|
+
|
|
547
559
|
max_iter = self.config.agent.max_iterations
|
|
548
560
|
current_task_plan = ""
|
|
549
561
|
all_tool_outputs = ""
|
|
@@ -1734,7 +1746,7 @@ class MainAgent(BaseAgent):
|
|
|
1734
1746
|
|
|
1735
1747
|
if _embed_url and stream_callback:
|
|
1736
1748
|
# 在线播放 — 发送 v2_media 事件让前端渲染嵌入播放器
|
|
1737
|
-
stream_callback
|
|
1749
|
+
await _safe_sse(stream_callback, {
|
|
1738
1750
|
"type": "v2_media",
|
|
1739
1751
|
"data": {
|
|
1740
1752
|
"media_type": _media_type,
|
|
@@ -1747,7 +1759,7 @@ class MainAgent(BaseAgent):
|
|
|
1747
1759
|
|
|
1748
1760
|
elif _fallback_link and stream_callback:
|
|
1749
1761
|
# [v1.20.10] 无法嵌入但提供外部链接 — 发送链接卡片
|
|
1750
|
-
stream_callback
|
|
1762
|
+
await _safe_sse(stream_callback, {
|
|
1751
1763
|
"type": "v2_media",
|
|
1752
1764
|
"data": {
|
|
1753
1765
|
"media_type": _media_type,
|
|
@@ -1812,7 +1824,7 @@ class MainAgent(BaseAgent):
|
|
|
1812
1824
|
if _wc_url:
|
|
1813
1825
|
_wc_session.current_url = _wc_url
|
|
1814
1826
|
if stream_callback:
|
|
1815
|
-
stream_callback
|
|
1827
|
+
await _safe_sse(stream_callback, {
|
|
1816
1828
|
"type": "v2_web_control",
|
|
1817
1829
|
"data": {
|
|
1818
1830
|
"action": "open",
|
|
@@ -1830,7 +1842,7 @@ class MainAgent(BaseAgent):
|
|
|
1830
1842
|
elif _wc_action == "close":
|
|
1831
1843
|
_wc_mgr.close_session(_wc_session_id)
|
|
1832
1844
|
if stream_callback:
|
|
1833
|
-
stream_callback
|
|
1845
|
+
await _safe_sse(stream_callback, {
|
|
1834
1846
|
"type": "v2_web_control",
|
|
1835
1847
|
"data": {"action": "close", "session_id": _wc_session_id}
|
|
1836
1848
|
})
|
|
@@ -1843,7 +1855,7 @@ class MainAgent(BaseAgent):
|
|
|
1843
1855
|
else:
|
|
1844
1856
|
_wc_session.current_url = _wc_url
|
|
1845
1857
|
if stream_callback:
|
|
1846
|
-
stream_callback
|
|
1858
|
+
await _safe_sse(stream_callback, {
|
|
1847
1859
|
"type": "v2_web_control",
|
|
1848
1860
|
"data": {"action": "navigate", "session_id": _wc_session_id, "url": _wc_url}
|
|
1849
1861
|
})
|