mirror of
https://github.com/agent0ai/agent-zero.git
synced 2026-10-18 23:20:10 +00:00
improve: move agent instructions from per-message to system prompt
This commit is contained in:
parent
4f1be15fa7
commit
e4991b6e6e
3 changed files with 18 additions and 13 deletions
|
|
@ -1,8 +1,9 @@
|
|||
"""Inject WhatsApp conversation context into system prompt."""
|
||||
|
||||
from helpers.extension import Extension
|
||||
from helpers import plugins
|
||||
from agent import LoopData
|
||||
from plugins._whatsapp_integration.helpers.handler import CTX_WA_CHAT_ID
|
||||
from plugins._whatsapp_integration.helpers.handler import CTX_WA_CHAT_ID, PLUGIN_NAME
|
||||
|
||||
|
||||
class WhatsAppContextPrompt(Extension):
|
||||
|
|
@ -20,3 +21,12 @@ class WhatsAppContextPrompt(Extension):
|
|||
system_prompt.append(
|
||||
self.agent.read_prompt("fw.wa.system_context_reply.md")
|
||||
)
|
||||
config = plugins.get_plugin_config(PLUGIN_NAME) or {}
|
||||
instructions = config.get("agent_instructions", "")
|
||||
if instructions:
|
||||
system_prompt.append(
|
||||
self.agent.read_prompt(
|
||||
"fw.wa.user_message_instructions.md",
|
||||
instructions=instructions,
|
||||
)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ async def _dispatch_message(config: dict, msg: dict) -> None:
|
|||
|
||||
if existing:
|
||||
# Continue most recent chat for this JID
|
||||
await _route_to_chat(config, msg, existing[0])
|
||||
await _route_to_chat(msg, existing[0])
|
||||
else:
|
||||
await _start_new_chat(config, msg)
|
||||
|
||||
|
|
@ -147,7 +147,7 @@ async def _start_new_chat(config: dict, msg: dict) -> None:
|
|||
|
||||
save_tmp_chat(context)
|
||||
|
||||
user_msg = _build_user_message(context.agent0, msg, config)
|
||||
user_msg = _build_user_message(context.agent0, msg)
|
||||
system_ctx = context.agent0.read_prompt("fw.wa.system_context.md")
|
||||
|
||||
msg_id = str(uuid.uuid4())
|
||||
|
|
@ -169,7 +169,7 @@ async def _start_new_chat(config: dict, msg: dict) -> None:
|
|||
|
||||
|
||||
async def _route_to_chat(
|
||||
config: dict, msg: dict, context_id: str,
|
||||
msg: dict, context_id: str,
|
||||
) -> None:
|
||||
context = AgentContext.get(context_id)
|
||||
if not context:
|
||||
|
|
@ -179,7 +179,7 @@ async def _route_to_chat(
|
|||
context.data[CTX_WA_LAST_MSG_ID] = msg.get("messageId", "")
|
||||
context.data[CTX_WA_TYPING_ACTIVE] = True
|
||||
|
||||
user_msg = _build_user_message(context.agent0, msg, config)
|
||||
user_msg = _build_user_message(context.agent0, msg)
|
||||
msg_id = str(uuid.uuid4())
|
||||
media_urls = msg.get("mediaUrls", [])
|
||||
attachments = await _save_incoming_media(media_urls) if media_urls else []
|
||||
|
|
@ -258,7 +258,7 @@ def _md_to_whatsapp(text: str) -> str:
|
|||
# Message builders
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _build_user_message(agent: Agent, msg: dict, config: dict) -> str:
|
||||
def _build_user_message(agent: Agent, msg: dict) -> str:
|
||||
sender_name = msg.get("senderName", "Unknown")
|
||||
sender_number = msg.get("senderId", "").replace("@s.whatsapp.net", "").replace("@lid", "")
|
||||
is_group = msg.get("isGroup", False)
|
||||
|
|
@ -271,11 +271,6 @@ def _build_user_message(agent: Agent, msg: dict, config: dict) -> str:
|
|||
message_id=msg.get("messageId", ""),
|
||||
body=msg.get("body", ""),
|
||||
)
|
||||
instructions = config.get("agent_instructions", "")
|
||||
if instructions:
|
||||
text += agent.read_prompt(
|
||||
"fw.wa.user_message_instructions.md", instructions=instructions,
|
||||
)
|
||||
return text
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
|
||||
[Handler instructions: {{instructions}}]
|
||||
# WhatsApp custom rules
|
||||
{{instructions}}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue