Skip to content

Commit a547d6b

Browse files
weimchraychen911
authored andcommitted
Bugfix: 修复GraphAgent中AgentNode的last_response被设为「思考/中间轮文本」的问题
- 原因:没有移除思考内容及做中间轮工具调用Event的检测 - 解决方案:使用Event.is_final_response保证是LLM最后一个结果,然后移除最后一个结果的思考内容
1 parent 099b571 commit a547d6b

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

trpc_agent_sdk/dsl/graph/_node_action/_agent.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,17 @@ async def execute(self, state: State) -> dict[str, Any]:
181181
if isinstance(candidate, str) and candidate:
182182
last_response = candidate
183183

184-
if (not self._is_graph_event(event)) and (
185-
not event.partial) and event.content and event.content.parts:
186-
text_parts = [part.text for part in event.content.parts if part.text]
187-
if text_parts:
188-
last_response = text_parts[-1]
184+
if (not self._is_graph_event(event)) and event.is_final_response():
185+
# Only clean final answers may become last_response. Skip
186+
# thought parts (part.thought) so a thinking model's
187+
# reasoning monologue is never surfaced as the answer.
188+
# is_final_response() already excludes partial, function
189+
# call/response, and code-execution events, so plan and
190+
# transfer_to_agent rounds are naturally ignored.
191+
if event.content and event.content.parts:
192+
visible_text = [part.text for part in event.content.parts if part.text and not part.thought]
193+
if visible_text:
194+
last_response = "".join(visible_text)
189195

190196
if not event.visible:
191197
if event.actions and event.actions.transfer_to_agent:
@@ -246,7 +252,7 @@ async def execute(self, state: State) -> dict[str, Any]:
246252

247253
node_response: Any = last_response
248254
structured_output: Any = None
249-
if isinstance(self.agent, LlmAgent) and self.agent.output_schema is not None:
255+
if isinstance(self.agent, LlmAgent) and self.agent.output_schema is not None and last_response:
250256
node_response = json.loads(last_response)
251257
structured_output = node_response
252258

trpc_agent_sdk/dsl/graph/_node_action/_llm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ async def _run_model_round(
251251
continue
252252

253253
response_parts = list(llm_response.content.parts)
254-
text_parts = [part.text for part in response_parts if part.text]
254+
# Exclude thought parts so a thinking model's reasoning does not
255+
# leak into response_text / STATE_KEY_LAST_RESPONSE.
256+
text_parts = [part.text for part in response_parts if part.text and not part.thought]
255257
response_text = "".join(text_parts)
256258

257259
logger.debug(f"[{self.name}] LLM response received ({len(response_text)} chars)")

0 commit comments

Comments
 (0)