Skip to content

Commit d69070f

Browse files
author
Hanson Mei
committed
feat: Add AGENTS.md auto-injection via plugin system
- Add plugins/agents_loader.py: Auto-inject AGENTS.md into system prompt via agent_before hook - Fix agent_loop.py: Handle hook return value to update system_prompt - Bug fixes found during implementation: * hooks.py uses 'register' decorator, not 'on' * Hook must return modified context dict to take effect This enables project-specific constraints in AGENTS.md to be automatically loaded without modifying core source code.
1 parent 2e6968a commit d69070f

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

agent_loop.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ 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+
result = _hook('agent_before', locals())
50+
if isinstance(result, dict):
51+
# Hook 可能修改了 locals,需要更新变量
52+
if 'system_prompt' in result:
53+
messages[0]['content'] = result['system_prompt']
5054
while turn < handler.max_turns:
5155
turn += 1; turnstr = f'LLM Running (Turn {turn}) ...'
5256
if handler.parent.task_dir: turnstr = f'Turn {turn} ...'

plugins/agents_loader.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
AGENTS.md loader plugin - 在 agent_before 时注入 AGENTS.md 到 system prompt
3+
"""
4+
import os
5+
from plugins import hooks
6+
7+
_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
8+
_AGENTS_PATH = os.path.join(_PROJECT_ROOT, 'AGENTS.md')
9+
10+
# 加载 AGENTS.md 内容
11+
_AGENTS_CONTENT = None
12+
if os.path.exists(_AGENTS_PATH):
13+
with open(_AGENTS_PATH, 'r', encoding='utf-8') as f:
14+
_AGENTS_CONTENT = f.read()
15+
16+
@hooks.register('agent_before')
17+
def inject_agents_md(ctx):
18+
"""在 agent 启动前注入 AGENTS.md 到 system prompt"""
19+
if _AGENTS_CONTENT and 'system_prompt' in ctx:
20+
# 在 system prompt 后面追加 AGENTS.md 内容
21+
ctx['system_prompt'] += f'\n\n[Project Constraints] (../AGENTS.md)\n{_AGENTS_CONTENT}\n'
22+
return ctx
23+
return ctx

0 commit comments

Comments
 (0)