myagent-ai 1.20.11 → 1.20.13
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/web/api_server.py +18 -8
- package/web/ui/chat/flow_engine.js +5 -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
|
})
|
package/package.json
CHANGED
package/web/api_server.py
CHANGED
|
@@ -279,11 +279,12 @@ class ApiServer:
|
|
|
279
279
|
mgr.config.auto_accept = cfg.auto_accept
|
|
280
280
|
logger.info("通信管理器已热更新")
|
|
281
281
|
|
|
282
|
-
def _hot_reload_chat_platforms(self):
|
|
282
|
+
async def _hot_reload_chat_platforms(self):
|
|
283
283
|
"""热更新聊天平台:重新加载所有平台配置到 ChatBotManager
|
|
284
284
|
|
|
285
|
-
[v1.20.7] 修复: 调用 setup_platforms
|
|
285
|
+
[v1.20.7] 修复: 调用 setup_platforms 时会自动停止被禁用的平台并启动新启用的平台。
|
|
286
286
|
[v1.20.9] 修复: chat_manager 为 None 时懒创建,修复首次启用平台不生效的问题。
|
|
287
|
+
[v1.20.12] 改为 async 并补启未运行的 bot,修复首次启用平台后 bot 不启动的问题。
|
|
287
288
|
"""
|
|
288
289
|
if not self.core.chat_manager:
|
|
289
290
|
from chatbot.manager import ChatBotManager
|
|
@@ -291,11 +292,20 @@ class ApiServer:
|
|
|
291
292
|
logger.info("聊天平台管理器已懒创建")
|
|
292
293
|
mgr = self.core.chat_manager
|
|
293
294
|
platform_configs = self.core.config_mgr.config.chat_platforms
|
|
294
|
-
# setup_platforms 会自动对比新旧配置,停止被移除/禁用的 bot,启动新启用的 bot
|
|
295
295
|
handler = mgr._message_handler if hasattr(mgr, '_message_handler') else None
|
|
296
296
|
if not handler and hasattr(self.core, '_handle_chat_message'):
|
|
297
297
|
handler = self.core._handle_chat_message
|
|
298
|
+
if not handler:
|
|
299
|
+
logger.warning("聊天平台消息处理器为 None,平台可能无法处理消息")
|
|
298
300
|
mgr.setup_platforms(platform_configs, handler)
|
|
301
|
+
# [v1.20.12] 补启未运行的 bot
|
|
302
|
+
enabled_keys = [cfg.id or cfg.platform for cfg in platform_configs if cfg.enabled]
|
|
303
|
+
for key in enabled_keys:
|
|
304
|
+
if key in mgr._bots and key not in mgr._bot_tasks:
|
|
305
|
+
bot = mgr._bots.get(key)
|
|
306
|
+
if bot:
|
|
307
|
+
logger.info(f"补启动聊天平台: {key}")
|
|
308
|
+
asyncio.create_task(mgr._run_bot(key, bot), name=f"bot_{key}")
|
|
299
309
|
logger.info("聊天平台配置已热更新")
|
|
300
310
|
|
|
301
311
|
def _setup_routes(self):
|
|
@@ -3711,7 +3721,7 @@ window.addEventListener('beforeunload', function() {{
|
|
|
3711
3721
|
self.core.config_mgr.config.chat_platforms.append(cp)
|
|
3712
3722
|
self.core.config_mgr.save()
|
|
3713
3723
|
# 热更新聊天平台
|
|
3714
|
-
self._hot_reload_chat_platforms()
|
|
3724
|
+
await self._hot_reload_chat_platforms()
|
|
3715
3725
|
logger.info(f"新增聊天平台: {cp.display_name} (id={cp.id})")
|
|
3716
3726
|
return web.json_response({"ok": True, "id": cp.id, "platform": platform_name, "display_name": cp.display_name, "hot_reload": True})
|
|
3717
3727
|
|
|
@@ -3753,7 +3763,7 @@ window.addEventListener('beforeunload', function() {{
|
|
|
3753
3763
|
logger.info(f"平台 ID 变更: {old_id} -> {cp.id}")
|
|
3754
3764
|
self.core.config_mgr.save()
|
|
3755
3765
|
# 热更新聊天平台
|
|
3756
|
-
self._hot_reload_chat_platforms()
|
|
3766
|
+
await self._hot_reload_chat_platforms()
|
|
3757
3767
|
logger.info(f"聊天平台配置已更新: {cp.display_name} (id={cp.id})")
|
|
3758
3768
|
return web.json_response({"ok": True, "id": cp.id, "hot_reload": True})
|
|
3759
3769
|
|
|
@@ -3766,7 +3776,7 @@ window.addEventListener('beforeunload', function() {{
|
|
|
3766
3776
|
platforms.pop(i)
|
|
3767
3777
|
self.core.config_mgr.save()
|
|
3768
3778
|
# 热更新聊天平台
|
|
3769
|
-
self._hot_reload_chat_platforms()
|
|
3779
|
+
await self._hot_reload_chat_platforms()
|
|
3770
3780
|
logger.info(f"已删除聊天平台: {cp.display_name} (id={cp.id})")
|
|
3771
3781
|
return web.json_response({"ok": True, "hot_reload": True})
|
|
3772
3782
|
return web.json_response({"error": f"平台 {name} 不存在"}, status=404)
|
|
@@ -3783,7 +3793,7 @@ window.addEventListener('beforeunload', function() {{
|
|
|
3783
3793
|
cp.enabled = data.get("enabled", not cp.enabled)
|
|
3784
3794
|
self.core.config_mgr.save()
|
|
3785
3795
|
# 热更新聊天平台
|
|
3786
|
-
self._hot_reload_chat_platforms()
|
|
3796
|
+
await self._hot_reload_chat_platforms()
|
|
3787
3797
|
logger.info(f"聊天平台 {cp.display_name} 已{'启用' if cp.enabled else '禁用'}")
|
|
3788
3798
|
return web.json_response({"ok": True, "enabled": cp.enabled, "id": cp.id, "hot_reload": True})
|
|
3789
3799
|
|
|
@@ -6566,7 +6576,7 @@ window.addEventListener('beforeunload', function() {{
|
|
|
6566
6576
|
self._hot_reload_llm()
|
|
6567
6577
|
self._hot_reload_executor()
|
|
6568
6578
|
self._hot_reload_communication()
|
|
6569
|
-
self._hot_reload_chat_platforms()
|
|
6579
|
+
await self._hot_reload_chat_platforms()
|
|
6570
6580
|
|
|
6571
6581
|
logger.info(f"安全保存配置: ok={result.get('ok')}")
|
|
6572
6582
|
return web.json_response(result)
|
|
@@ -1853,9 +1853,13 @@ async function sendMessage(opts) {
|
|
|
1853
1853
|
(evt.content ? evt.content.substring(0, 100) : '');
|
|
1854
1854
|
} else if (evt.type === 'v2_file') {
|
|
1855
1855
|
// [v1.16.17] Agent is sending a file to the user
|
|
1856
|
+
// [v1.20.12] 修复: 后端发送 file_id,前端渲染代码期望 id
|
|
1856
1857
|
if (evt.data) {
|
|
1857
1858
|
if (!state.messages[msgIdx]._files) state.messages[msgIdx]._files = [];
|
|
1858
|
-
|
|
1859
|
+
var fd = evt.data;
|
|
1860
|
+
// 归一化: 后端用 file_id,渲染代码用 id
|
|
1861
|
+
if (fd.file_id && !fd.id) fd.id = fd.file_id;
|
|
1862
|
+
state.messages[msgIdx]._files.push(fd);
|
|
1859
1863
|
throttledStreamUpdate(msgIdx);
|
|
1860
1864
|
}
|
|
1861
1865
|
} else if (evt.type === 'v2_media') {
|