From 02b2b42561ca0338090c0bc911855e009ed4abba Mon Sep 17 00:00:00 2001 From: Hanson Mei Date: Sat, 30 May 2026 18:32:07 +0800 Subject: [PATCH] fix: Handle hook return values to enable context modification The plugin hook system calls _hook('agent_before', locals()) but was not handling the return value. This prevented plugins from modifying the agent context (e.g., system_prompt). This fix: - Captures the hook return value - Applies modifications when a dict is returned - Maintains backward compatibility with hooks that don't return values Fixes #537 --- agent_loop.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/agent_loop.py b/agent_loop.py index 7afb58507..fb3cb39d8 100644 --- a/agent_loop.py +++ b/agent_loop.py @@ -46,7 +46,11 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, {"role": "user", "content": initial_user_content if initial_user_content is not None else user_input} ] turn = 0; handler.max_turns = max_turns - _hook('agent_before', locals()) + result = _hook('agent_before', locals()) + if isinstance(result, dict): + # Hook 可能修改了 locals,需要更新变量 + if 'system_prompt' in result: + messages[0]['content'] = result['system_prompt'] while turn < handler.max_turns: turn += 1; turnstr = f'LLM Running (Turn {turn}) ...' if handler.parent.task_dir: turnstr = f'Turn {turn} ...'