Skip to content

Commit 86c9ac2

Browse files
committed
feat:新增用户请求过滤器以改善交互逻辑
1 parent a00acfe commit 86c9ac2

5 files changed

Lines changed: 40 additions & 7 deletions

File tree

agents/matmaster_agent/constant.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
MATMASTER_AGENT_NAME = 'matmaster_agent'
1717

1818
ModelRole = 'model'
19+
UserRole = 'user'
1920

2021
Transfer2Agent = 'transfer_to_agent'
2122

agents/matmaster_agent/core_agents/public_agents/job_agents/submit_core_agent/agent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ async def _run_events(self, ctx: InvocationContext) -> AsyncGenerator[Event, Non
270270
ctx, self.name, '工具参数无变化,本次跳过执行', ModelRole
271271
):
272272
yield _info_event
273-
update_plan = copy.deepcopy(ctx.session.state['plan'])
274-
update_plan['steps'][ctx.session.state['plan_index']][
275-
'status'
276-
] = PlanStepStatusEnum.FAILED
277-
yield update_state_event(ctx, state_delta={'plan': update_plan})
273+
post_execution_step = copy.deepcopy(ctx.session.state[CURRENT_STEP])
274+
post_execution_step['status'] = PlanStepStatusEnum.FAILED
275+
yield update_state_event(
276+
ctx, state_delta={CURRENT_STEP: post_execution_step}
277+
)
278278
else:
279279
yield event

agents/matmaster_agent/flow_agents/agent.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
from agents.matmaster_agent.core_agents.comp_agents.dntransfer_climit_agent import (
2727
DisallowTransferAndContentLimitLlmAgent,
2828
)
29+
from agents.matmaster_agent.flow_agents.all_finished_agent.callback import (
30+
only_select_user_request,
31+
)
2932
from agents.matmaster_agent.flow_agents.all_finished_agent.constant import (
3033
ALL_FINISHED_AGENT,
3134
)
@@ -229,6 +232,7 @@ def after_init(self):
229232
description='检查用户的目标是否完成',
230233
output_schema=AllFinishedSchema,
231234
state_key=FINISHED_STATE,
235+
before_model_callback=only_select_user_request,
232236
)
233237

234238
self._analysis_agent = DisallowTransferAndContentLimitLlmAgent(
@@ -809,10 +813,11 @@ async def _run_research_flow(
809813
yield _plan_execute_event
810814

811815
# 回顾历史执行
816+
user_request = ctx.user_content.parts[0].text
812817
history_steps = ctx.session.state[HISTORY_STEPS]
813818
session_files = await get_session_files(ctx.session.id)
814819
self.all_finished_agent.instruction = create_all_finished_instruction(
815-
history_steps, session_files
820+
user_request, history_steps, session_files
816821
)
817822
async for _all_finished_event in self.all_finished_agent.run_async(ctx):
818823
yield _all_finished_event
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import logging
2+
from typing import Optional
3+
4+
from google.adk.agents.callback_context import CallbackContext
5+
from google.adk.models import LlmRequest, LlmResponse
6+
from google.genai.types import Content, Part
7+
8+
from agents.matmaster_agent.constant import MATMASTER_AGENT_NAME, UserRole
9+
from agents.matmaster_agent.logger import PrefixFilter
10+
11+
logger = logging.getLogger(__name__)
12+
logger.addFilter(PrefixFilter(MATMASTER_AGENT_NAME))
13+
logger.setLevel(logging.INFO)
14+
15+
16+
async def only_select_user_request(
17+
callback_context: CallbackContext, llm_request: LlmRequest
18+
) -> Optional[LlmResponse]:
19+
llm_request.contents = [
20+
Content(
21+
role=UserRole,
22+
parts=[Part(text=callback_context.user_content.parts[0].text)],
23+
)
24+
]

agents/matmaster_agent/flow_agents/all_finished_agent/prompt.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22

33

4-
def create_all_finished_instruction(history_steps, session_files):
4+
def create_all_finished_instruction(user_request, history_steps, session_files):
55
"""
66
Build an instruction prompt for an agent that decides whether the user's overall goal
77
has been completed up to the current point in the tool-call history.
@@ -27,6 +27,9 @@ def create_all_finished_instruction(history_steps, session_files):
2727
and persisted for this session will appear here. Use session_files as verifiable evidence
2828
that a file deliverable truly exists.
2929
30+
Below in the raw user_request:
31+
{user_request}
32+
3033
Below is the raw history_steps data (JSON):
3134
{history_text}
3235

0 commit comments

Comments
 (0)