-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_multi_agent.py
More file actions
executable file
·105 lines (88 loc) · 3.84 KB
/
Copy pathpatch_multi_agent.py
File metadata and controls
executable file
·105 lines (88 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
"""
Entropy Runtime · multi-agent 端点升级脚本
将 MemoryStore 留言板读写切换到 MessageBoard
"""
import os, sys
API_FILE = "/root/EntropyGuard/routes/api.py"
BAK_FILE = API_FILE + f".bak.mb_multi_{int(__import__('time').time())}"
with open(API_FILE, "r", encoding="utf-8") as f:
content = f.read()
# 备份
with open(BAK_FILE, "w", encoding="utf-8") as f:
f.write(content)
print(f"Backup: {BAK_FILE}")
# ---- Patch 1: 添加 import ----
old_import = "from memory import MemoryStore, generate_summary_text, auto_summarize_session"
new_import = old_import + "\nfrom messageboard import get_messageboard"
assert old_import in content, "import line not found"
content = content.replace(old_import, new_import, 1)
print("Patch 1: import added")
# ---- Patch 2: 更新 prompt_a 指导 Agent A 使用 session_id ----
old_prompt_a = '''留言板消息格式(必须使用 write_board 工具写入):
{{
"from": "{agent_a}",
"to": "{agent_b}",
"type": "analysis_result",
"content": "你的分析结果内容...",
"timestamp": "<ISO时间戳>"
}}'''
new_prompt_a = f'''使用 write_board 工具写入留言板,设置参数:
- content: 你的分析结果(JSON 格式,包含分析摘要、发现的问题、建议等)
- to_agent: "board"
- session_id: "{{session_id}}" (本次多智能体会话 ID)'''
assert old_prompt_a in content, "prompt_a old text not found"
content = content.replace(old_prompt_a, new_prompt_a, 1)
print("Patch 2: prompt_a updated")
# ---- Patch 3: 替换 Step 2 读取留言板 ----
old_read_board = ''' # ---------- Step 2: 读取留言板内容 ----------
board_memories = MemoryStore.get_recent(limit=20)
board_content = "\\n".join(
f"[{m.get('timestamp','?')}] {m.get('content','')}"
for m in board_memories[-10:]
)
results["steps"].append({
"step": "read_board",
"board_messages_count": len(board_memories),
"content_preview": board_content[-1500:] if board_content else "(empty)",
})'''
new_read_board = ''' # ---------- Step 2: 读取留言板内容 (MessageBoard v2) ----------
board = get_messageboard()
all_board_msgs = board.get_inbox("board", limit=50)
# 按 session_id 过滤本次会话的消息
if session_id:
board_messages = [
m for m in all_board_msgs
if (m.get("metadata") or {}).get("session_id") == session_id
]
else:
board_messages = all_board_msgs[-20:]
def _fmt_content(c):
if isinstance(c, dict):
return c.get("text", json.dumps(c, ensure_ascii=False))
return str(c)
board_content = "\\n".join(
f"[{m.get('timestamp','?')}] [{m.get('from_agent','?')}] {_fmt_content(m.get('content',''))}"
for m in board_messages[-10:]
)
results["steps"].append({
"step": "read_board_v2",
"board_messages_count": len(board_messages),
"board_total": len(all_board_msgs),
"session_id": session_id,
"content_preview": board_content[-1500:] if board_content else "(empty)",
})'''
assert old_read_board in content, "read_board old text not found"
content = content.replace(old_read_board, new_read_board, 1)
print("Patch 3: read_board section updated")
# ---- Patch 4: 更新 prompt_b 提示 ----
old_prompt_b_hint = "Agent A ({agent_a}) 已经完成了分析并将结果写入了留言板。"
new_prompt_b_hint = "Agent A ({agent_a}) 已经完成了分析并将结果写入了留言板(会话 ID: {session_id})。"
assert old_prompt_b_hint in content, "prompt_b old hint not found"
content = content.replace(old_prompt_b_hint, new_prompt_b_hint, 1)
print("Patch 4: prompt_b hint updated")
# 写回
with open(API_FILE, "w", encoding="utf-8") as f:
f.write(content)
print(f"\nAll 4 patches applied successfully to {API_FILE}")
print("Restart entropyruntime service to take effect")