myagent-ai 1.47.36 → 1.47.38
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 +33 -10
- package/core/tool_dispatcher.py +22 -21
- package/groups/manager.py +23 -7
- package/package.json +1 -1
package/agents/main_agent.py
CHANGED
|
@@ -1274,34 +1274,56 @@ class MainAgent(BaseAgent):
|
|
|
1274
1274
|
|
|
1275
1275
|
# 将 chat_agent 条目写入 agent_chat 表(私聊记录)
|
|
1276
1276
|
if _chat_entries:
|
|
1277
|
+
logger.info(f"[{task_id}] [私聊保存] main_agent 发现 {len(_chat_entries)} 条私聊条目")
|
|
1277
1278
|
try:
|
|
1278
1279
|
from groups.manager import GroupManager
|
|
1280
|
+
logger.info(f"[{task_id}] [私聊保存] 正在创建 GroupManager")
|
|
1279
1281
|
_gm = GroupManager()
|
|
1280
1282
|
_gm.initialize()
|
|
1281
|
-
|
|
1283
|
+
logger.info(f"[{task_id}] [私聊保存] GroupManager 初始化完成")
|
|
1284
|
+
|
|
1285
|
+
for _idx, _ce in enumerate(_chat_entries):
|
|
1282
1286
|
_from_agent = agent_path or str(_effective_agent_id)
|
|
1283
1287
|
_from_name = ""
|
|
1288
|
+
logger.info(f"[{task_id}] [私聊保存] 处理第 {_idx+1} 条私聊: from_agent={_from_agent}, target_agent={_ce.get('target_agent', '')}")
|
|
1289
|
+
|
|
1284
1290
|
try:
|
|
1285
1291
|
import sqlite3
|
|
1286
|
-
|
|
1292
|
+
_db_path = str(Path.home() / ".myagent" / "data" / "agents.db")
|
|
1293
|
+
logger.info(f"[{task_id}] [私聊保存] 查询 agents.db: {_db_path}")
|
|
1294
|
+
_db = sqlite3.connect(_db_path)
|
|
1287
1295
|
_db.row_factory = sqlite3.Row
|
|
1288
1296
|
_row = _db.execute("SELECT name FROM agents WHERE path = ? OR id = ?", (_from_agent, _from_agent)).fetchone()
|
|
1289
1297
|
if _row and _row["name"]:
|
|
1290
1298
|
_from_name = _row["name"]
|
|
1299
|
+
logger.info(f"[{task_id}] [私聊保存] 查询到 from_name: {_from_name}")
|
|
1300
|
+
else:
|
|
1301
|
+
logger.info(f"[{task_id}] [私聊保存] 未查询到 from_name")
|
|
1291
1302
|
_db.close()
|
|
1292
|
-
except Exception:
|
|
1293
|
-
|
|
1294
|
-
|
|
1303
|
+
except Exception as _e:
|
|
1304
|
+
logger.warning(f"[{task_id}] [私聊保存] 查询 from_name 失败: {_e}")
|
|
1305
|
+
|
|
1306
|
+
_to_agent = _ce.get("target_agent", "")
|
|
1307
|
+
_to_name = _ce.get("target_name", "")
|
|
1308
|
+
_content = _ce.get("message", "")
|
|
1309
|
+
|
|
1310
|
+
logger.info(f"[{task_id}] [私聊保存] 调用 add_agent_chat: from_agent={_from_agent}, from_name={_from_name}, to_agent={_to_agent}, to_name={_to_name}")
|
|
1311
|
+
_msg_id = _gm.add_agent_chat(
|
|
1295
1312
|
group_id="",
|
|
1296
1313
|
from_agent=_from_agent,
|
|
1297
1314
|
from_name=_from_name,
|
|
1298
|
-
to_agent=
|
|
1299
|
-
to_name=
|
|
1300
|
-
content=
|
|
1315
|
+
to_agent=_to_agent,
|
|
1316
|
+
to_name=_to_name,
|
|
1317
|
+
content=_content,
|
|
1301
1318
|
)
|
|
1302
|
-
logger.info(f"[{task_id}]
|
|
1319
|
+
logger.info(f"[{task_id}] [私聊保存] add_agent_chat 返回 msg_id={_msg_id}")
|
|
1320
|
+
logger.info(f"[{task_id}] Agent私聊已保存: {_from_name} → {_to_name}: {_content[:80]}")
|
|
1321
|
+
|
|
1322
|
+
_gm.close()
|
|
1323
|
+
logger.info(f"[{task_id}] [私聊保存] GroupManager 已关闭,处理完成")
|
|
1303
1324
|
except Exception as _ce_err:
|
|
1304
|
-
logger.warning(f"[{task_id}] 保存Agent私聊记录失败: {_ce_err}")
|
|
1325
|
+
logger.warning(f"[{task_id}] [私聊保存] 保存Agent私聊记录失败: {_ce_err}")
|
|
1326
|
+
logger.exception(f"[{task_id}] [私聊保存] 异常详情")
|
|
1305
1327
|
|
|
1306
1328
|
# 普通文件列表照常持久化到 session memory
|
|
1307
1329
|
if _file_entries:
|
|
@@ -1362,6 +1384,7 @@ class MainAgent(BaseAgent):
|
|
|
1362
1384
|
stream_callback=stream_callback,
|
|
1363
1385
|
sent_files=sent_files,
|
|
1364
1386
|
agent_path=agent_path,
|
|
1387
|
+
agent_id=agent_path, # agent_path 就是 agent_id
|
|
1365
1388
|
)
|
|
1366
1389
|
|
|
1367
1390
|
# 兼容回退: dispatcher 未初始化时使用基础 fallback
|
package/core/tool_dispatcher.py
CHANGED
|
@@ -102,6 +102,7 @@ class ToolDispatcher:
|
|
|
102
102
|
stream_callback: Optional[Callable] = None,
|
|
103
103
|
sent_files: Optional[List[Dict[str, Any]]] = None,
|
|
104
104
|
agent_path: Optional[str] = None,
|
|
105
|
+
agent_id: Optional[str] = None,
|
|
105
106
|
) -> Dict[str, Any]:
|
|
106
107
|
"""
|
|
107
108
|
统一工具分发入口。
|
|
@@ -120,7 +121,7 @@ class ToolDispatcher:
|
|
|
120
121
|
"""
|
|
121
122
|
# ── 内置平台工具 (LLM 直接调用) ──
|
|
122
123
|
if tool_name == "command":
|
|
123
|
-
return await self._exec_command(params, timeout, task_id, stream_callback, sent_files,
|
|
124
|
+
return await self._exec_command(params, timeout, task_id, stream_callback, sent_files, agent_id)
|
|
124
125
|
elif tool_name == "web_control":
|
|
125
126
|
return await self._exec_web_control(params, task_id, stream_callback)
|
|
126
127
|
|
|
@@ -184,7 +185,7 @@ class ToolDispatcher:
|
|
|
184
185
|
async def _exec_command(self, params: Dict, timeout: int, task_id: str,
|
|
185
186
|
stream_callback: Optional[Callable] = None,
|
|
186
187
|
sent_files: Optional[List[Dict[str, Any]]] = None,
|
|
187
|
-
|
|
188
|
+
agent_id: Optional[str] = None) -> Dict:
|
|
188
189
|
"""执行 shell 命令"""
|
|
189
190
|
code_text = params.get("command", "")
|
|
190
191
|
if not code_text:
|
|
@@ -217,38 +218,42 @@ class ToolDispatcher:
|
|
|
217
218
|
# 如果有私聊标记,保存到数据库
|
|
218
219
|
if _chat_markers:
|
|
219
220
|
try:
|
|
221
|
+
logger.info(f"[{task_id}] [私聊保存] 开始处理,找到 {len(_chat_markers)} 条私聊标记")
|
|
220
222
|
# 延迟导入避免循环依赖
|
|
221
223
|
from groups.manager import GroupManager
|
|
222
224
|
# 获取数据目录
|
|
223
225
|
_data_dir = _P.home() / ".myagent" / "data"
|
|
226
|
+
logger.info(f"[{task_id}] [私聊保存] 数据目录: {_data_dir}")
|
|
224
227
|
_gm = GroupManager(_data_dir)
|
|
228
|
+
logger.info(f"[{task_id}] [私聊保存] GroupManager 初始化完成")
|
|
225
229
|
|
|
226
|
-
for _match in _chat_markers:
|
|
230
|
+
for _idx, _match in enumerate(_chat_markers):
|
|
227
231
|
_to_path = _match[0].strip()
|
|
228
232
|
_to_name = _match[1].strip()
|
|
229
233
|
_content = _match[2].strip()
|
|
230
234
|
|
|
231
|
-
# 获取当前 agent
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if _from_path and _from_path.startswith("agents/"):
|
|
235
|
-
_from_name = _from_path.replace("agents/", "").split("/")[-1]
|
|
236
|
-
else:
|
|
237
|
-
_from_name = _from_path if _from_path else "未知"
|
|
235
|
+
# 获取当前 agent 的 ID
|
|
236
|
+
_from_id = agent_id if agent_id else "unknown"
|
|
237
|
+
_from_name = _from_id if _from_id else "未知"
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
logger.info(f"[{task_id}] [私聊保存] 处理第 {_idx+1} 条: from_id={_from_id}, from_name={_from_name}, to_path={_to_path}, to_name={_to_name}")
|
|
240
|
+
|
|
241
|
+
_result = _gm.add_agent_chat(
|
|
240
242
|
group_id="",
|
|
241
|
-
from_agent=
|
|
243
|
+
from_agent=_from_id,
|
|
242
244
|
from_name=_from_name,
|
|
243
245
|
to_agent=_to_path,
|
|
244
246
|
to_name=_to_name,
|
|
245
247
|
content=_content,
|
|
246
248
|
)
|
|
249
|
+
logger.info(f"[{task_id}] [私聊保存] 第 {_idx+1} 条保存结果: msg_id={_result}")
|
|
247
250
|
logger.info(f"[{task_id}] 私聊记录已保存: {_from_name} → {_to_name}: {_content[:50]}...")
|
|
248
251
|
|
|
249
252
|
_gm.close()
|
|
253
|
+
logger.info(f"[{task_id}] [私聊保存] GroupManager 已关闭,处理完成")
|
|
250
254
|
except Exception as _ce:
|
|
251
|
-
logger.warning(f"[{task_id}] 保存私聊记录失败: {_ce}")
|
|
255
|
+
logger.warning(f"[{task_id}] [私聊保存] 保存私聊记录失败: {_ce}")
|
|
256
|
+
logger.exception(f"[{task_id}] [私聊保存] 异常详情")
|
|
252
257
|
|
|
253
258
|
# 注入权限检查器
|
|
254
259
|
if self._permission_checker:
|
|
@@ -290,17 +295,13 @@ class ToolDispatcher:
|
|
|
290
295
|
_to_name = _match[1].strip()
|
|
291
296
|
_content = _match[2].strip()
|
|
292
297
|
|
|
293
|
-
# 获取当前 agent
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if _from_path and _from_path.startswith("agents/"):
|
|
297
|
-
_from_name = _from_path.replace("agents/", "").split("/")[-1]
|
|
298
|
-
else:
|
|
299
|
-
_from_name = _from_path if _from_path else "未知"
|
|
298
|
+
# 获取当前 agent 的 ID
|
|
299
|
+
_from_id = agent_id if agent_id else "unknown"
|
|
300
|
+
_from_name = _from_id if _from_id else "未知"
|
|
300
301
|
|
|
301
302
|
_gm.add_agent_chat(
|
|
302
303
|
group_id="",
|
|
303
|
-
from_agent=
|
|
304
|
+
from_agent=_from_id,
|
|
304
305
|
from_name=_from_name,
|
|
305
306
|
to_agent=_to_path,
|
|
306
307
|
to_name=_to_name,
|
package/groups/manager.py
CHANGED
|
@@ -258,8 +258,10 @@ class GroupManager:
|
|
|
258
258
|
def _init_message_db(self):
|
|
259
259
|
"""初始化群消息 SQLite 数据库"""
|
|
260
260
|
db_path = self._data_dir / "groups" / "messages.db"
|
|
261
|
+
logger.info(f"[私聊保存] 初始化消息数据库: {db_path}")
|
|
261
262
|
self._db_conn = sqlite3.connect(str(db_path), check_same_thread=False)
|
|
262
263
|
self._db_conn.row_factory = sqlite3.Row
|
|
264
|
+
logger.info(f"[私聊保存] 数据库连接已创建")
|
|
263
265
|
# [v1.23.81] 群聊 session 映射表(每个群一个纯数字 session ID)
|
|
264
266
|
self._db_conn.execute("""
|
|
265
267
|
CREATE TABLE IF NOT EXISTS group_sessions (
|
|
@@ -805,16 +807,30 @@ class GroupManager:
|
|
|
805
807
|
def add_agent_chat(self, group_id: str, from_agent: str, from_name: str,
|
|
806
808
|
to_agent: str, to_name: str, content: str) -> str:
|
|
807
809
|
"""添加一条Agent间私聊记录,返回消息ID"""
|
|
810
|
+
logger.info(f"[私聊保存] add_agent_chat 被调用: group_id={group_id}, from_agent={from_agent}, from_name={from_name}, to_agent={to_agent}, to_name={to_name}, content={content[:50]}...")
|
|
811
|
+
|
|
808
812
|
if not self._db_conn:
|
|
813
|
+
logger.warning(f"[私聊保存] 数据库连接未初始化,无法保存")
|
|
809
814
|
return ""
|
|
815
|
+
|
|
810
816
|
msg_id = uuid.uuid4().hex[:16]
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
(
|
|
816
|
-
|
|
817
|
-
|
|
817
|
+
logger.info(f"[私聊保存] 生成的消息ID: {msg_id}")
|
|
818
|
+
|
|
819
|
+
try:
|
|
820
|
+
with self._db_lock:
|
|
821
|
+
logger.info(f"[私聊保存] 开始执行 INSERT 语句")
|
|
822
|
+
self._db_conn.execute(
|
|
823
|
+
"INSERT INTO agent_chat (id, group_id, from_agent, from_name, to_agent, to_name, content, timestamp) "
|
|
824
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
825
|
+
(msg_id, group_id, from_agent, from_name, to_agent, to_name, content, time.time()),
|
|
826
|
+
)
|
|
827
|
+
self._db_conn.commit()
|
|
828
|
+
logger.info(f"[私聊保存] INSERT 成功,已提交事务,msg_id={msg_id}")
|
|
829
|
+
except Exception as e:
|
|
830
|
+
logger.error(f"[私聊保存] INSERT 失败: {e}")
|
|
831
|
+
logger.exception(f"[私聊保存] 异常详情")
|
|
832
|
+
return ""
|
|
833
|
+
|
|
818
834
|
return msg_id
|
|
819
835
|
|
|
820
836
|
def get_agent_chats(self, group_id: str = "", from_agent: str = "",
|