Skip to content

Commit d5fb9f9

Browse files
committed
共享上下文放到system_reminder防止缓存不命中
1 parent 4f02492 commit d5fb9f9

3 files changed

Lines changed: 38 additions & 22 deletions

File tree

astrbot/core/astr_agent_tool_exec.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ async def _execute_handoff(
382382
umo, tool, prov_settings
383383
)
384384

385+
# 构建子代理的追加内容
386+
extra_content_parts = SubAgentManager.build_subagent_extra_content_parts(
387+
umo, agent_name
388+
)
389+
385390
# 获取子代理的超时时间
386391
execution_timeout = cls._get_subagent_execution_timeout()
387392

@@ -402,6 +407,7 @@ async def _run_subagent():
402407
tool_call_timeout=run_context.tool_call_timeout,
403408
stream=stream,
404409
runner_messages=runner_messages,
410+
extra_user_content_parts=extra_content_parts,
405411
)
406412

407413
# 添加执行超时控制
@@ -871,14 +877,9 @@ def _build_subagent_system_prompt(
871877
)
872878
if agent_name:
873879
runtime = prov_settings.get("computer_use_runtime", "local")
874-
static_subagent_prompt = SubAgentManager.build_static_subagent_prompts(
875-
umo, agent_name
876-
)
877-
dynamic_subagent_prompt = SubAgentManager.build_dynamic_subagent_prompts(
880+
subagent_system_prompt += SubAgentManager.build_subagent_system_prompt(
878881
umo, agent_name, runtime
879882
)
880-
subagent_system_prompt += static_subagent_prompt
881-
subagent_system_prompt += dynamic_subagent_prompt
882883
return subagent_system_prompt
883884

884885
@staticmethod

astrbot/core/star/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ async def tool_loop_agent(
220220
func_tool=tools,
221221
contexts=context_,
222222
system_prompt=system_prompt or "",
223+
extra_user_content_parts=kwargs.get("extra_user_content_parts", []),
223224
)
224225
if agent_context is None:
225226
agent_context = AstrAgentContext(

astrbot/core/subagent_manager.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -330,34 +330,48 @@ def get_subagent_history(cls, session_id: str, agent_name: str) -> list:
330330
return session.subagent_histories.get(agent_name, [])
331331

332332
@classmethod
333-
def build_static_subagent_prompts(cls, session_id: str, agent_name: str) -> str:
334-
"""构建不会在会话内变化的subagent提示词"""
333+
def build_subagent_system_prompt(
334+
cls, session_id: str, agent_name: str, runtime: str
335+
) -> str:
335336
parts = []
336337
rule = cls._build_rule_prompt()
337338
workdir = cls._build_workdir_prompt(session_id, agent_name)
338339
if rule:
339340
parts.append(rule)
340341
if workdir:
341342
parts.append(workdir)
342-
return "\n".join(parts)
343-
344-
@classmethod
345-
def build_dynamic_subagent_prompts(
346-
cls, session_id: str, agent_name: str, runtime: str
347-
) -> str:
348-
"""构建会话内可能变化的提示词(每次调用重建)"""
349-
parts = []
350343
skills = cls._build_subagent_skills_prompt(session_id, agent_name, runtime)
351344
if skills:
352345
parts.append(skills)
353-
shared = cls._build_shared_context_prompt(session_id, agent_name)
354-
if shared:
355-
parts.append(shared)
356-
time_p = cls._build_time_prompt()
357-
if time_p:
358-
parts.append(time_p)
359346
return "\n".join(parts)
360347

348+
@classmethod
349+
def build_subagent_extra_content_parts(
350+
cls, session_id: str, agent_name: str
351+
) -> list:
352+
"""构建子代理的追加内容部分(extra_user_content_parts)。
353+
354+
将共享上下文和时间信息作为追加内容返回,它们将被注入到用户消息中,
355+
356+
Returns:
357+
list[TextPart]: 追加内容部分列表
358+
"""
359+
from astrbot.core.agent.message import TextPart
360+
361+
parts = []
362+
363+
# 1. 共享上下文
364+
shared_context = cls._build_shared_context_prompt(session_id, agent_name)
365+
if shared_context:
366+
parts.append(TextPart(text=shared_context).mark_as_temp())
367+
368+
# 2. 时间信息
369+
time_prompt = cls._build_time_prompt()
370+
if time_prompt:
371+
parts.append(TextPart(text=time_prompt).mark_as_temp())
372+
373+
return parts
374+
361375
@classmethod
362376
def _build_subagent_skills_prompt(
363377
cls, session_id: str, agent_name: str, runtime: str = "local"

0 commit comments

Comments
 (0)