Skip to content

Commit 678bbd8

Browse files
KaiLiKailigithub
authored andcommitted
fix(agent_loop): apply agent_before hook return value to context
The plugin hook system in plugins/hooks.py already supports returning a dict to mutate the agent context, but agent_runner_loop() discarded the return value of _hook('agent_before', ...). This prevented plugins from modifying system_prompt / user_input / initial_user_content. Capture the return value; when a dict is returned, apply its overrides to the messages list. Non-dict returns and missing keys are ignored, so existing hooks that only inspect ctx (e.g. logging/metrics) continue to work without modification. Closes #537
1 parent 2057d86 commit 678bbd8

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

agent_loop.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
4646
{"role": "user", "content": initial_user_content if initial_user_content is not None else user_input}
4747
]
4848
turn = 0; handler.max_turns = max_turns
49-
_hook('agent_before', locals())
49+
_ctx = _hook('agent_before', locals()) # plugins may return dict to override system_prompt/user_input
50+
if isinstance(_ctx, dict):
51+
if 'system_prompt' in _ctx:
52+
system_prompt = _ctx['system_prompt']
53+
messages[0]['content'] = system_prompt
54+
if _ctx.get('initial_user_content') is not None:
55+
messages[1]['content'] = _ctx['initial_user_content']
56+
elif 'user_input' in _ctx and initial_user_content is None:
57+
messages[1]['content'] = _ctx['user_input']
5058
while turn < handler.max_turns:
5159
turn += 1; turnstr = f'LLM Running (Turn {turn}) ...'
5260
if handler.parent.task_dir: turnstr = f'Turn {turn} ...'

0 commit comments

Comments
 (0)