-
Notifications
You must be signed in to change notification settings - Fork 105
feat(sdk): complete hooks implementation with additional context and stop hook #1547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sdk): complete hooks implementation with additional context and stop hook #1547
Conversation
Coverage Report •
|
||||||||||||||||||||||||||||||
322a14d to
4c36ba1
Compare
…stop hook - Implement additional_context injection in UserPromptSubmit hooks - Hook context is appended to MessageEvent.extended_content - Context flows through condensation and is included in LLM messages - Implement stop hook integration in conversation run loop - Stop hooks can deny premature agent completion - Feedback from hooks is injected as user message with [Stop hook feedback] prefix - Agent continues running after stop hook denial - Add comprehensive tests for both features - Tests for context appearing in extended_content and to_llm_message() - Tests for stop hook denial with feedback injection - Integration tests for full conversation loop with stop hooks - Add advanced hooks example (34_hooks_advanced.py)
4c36ba1 to
bda858e
Compare
|
This likely solves #1527! |
| config = HookConfig.from_dict( | ||
| { | ||
| "hooks": { | ||
| "UserPromptSubmit": [ | ||
| { | ||
| "hooks": [{"type": "command", "command": str(context_script)}], | ||
| } | ||
| ], | ||
| "Stop": [ | ||
| { | ||
| "hooks": [{"type": "command", "command": str(stop_script)}], | ||
| } | ||
| ], | ||
| } | ||
| } | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: could we make this a HookConfig a pydantic class that contains UserPromptSubmit Stop, etc, which are other pydantic class, so we can have stricter type check instead of putting together a dict like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a great idea. I’ll plan on implementing it in a follow-up PR so that we can keep this PR’s scope focused.
- Move 33_hooks.py to 33_hooks/ folder with separate scripts - Split into basic_hooks.py and advanced_hooks.py - Remove main() wrapper to match codebase convention (31/33 examples) - Extract shell scripts to scripts/ directory for reusability - Add README.md documenting hook types and usage Addresses review feedback: - Consolidate hook examples into folder structure - Simplify examples by removing main() wrapper
…with-additional-context-and-stop-hook-13
examples/01_standalone_sdk/33_hooks/hook_scripts/block_dangerous.sh
Outdated
Show resolved
Hide resolved
Update the hooks documentation to reflect the new 33_hooks.py example that demonstrates all four hook types: - PreToolUse: Block dangerous commands - PostToolUse: Log tool usage - UserPromptSubmit: Inject context into user messages - Stop: Enforce task completion criteria The new example uses external hook scripts in a hook_scripts/ directory and includes a Hook Types reference table. Related to: OpenHands/software-agent-sdk#1547 Co-authored-by: openhands <[email protected]>
Added tests verifying LocalConversation correctly wires hook callbacks to event persistence via original_callback parameter.
Use grep on raw JSON input instead of jq for detecting dangerous commands. This makes the example work out of the box without requiring jq installation.
…with-additional-context-and-stop-hook-13
|
Looks like there are a few issues preventing this PR from being merged!
If you'd like me to help, just leave a comment, like Feel free to include any additional details that might help me get this PR into a better state. You can manage your notification settings |
xingyaoww
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thank you!
|
@ak684 |
Update the hooks documentation to reflect the new 33_hooks.py example that demonstrates all four hook types: - PreToolUse: Block dangerous commands - PostToolUse: Log tool usage - UserPromptSubmit: Inject context into user messages - Stop: Enforce task completion criteria The new example uses external hook scripts in a hook_scripts/ directory and includes a Hook Types reference table. Related to: OpenHands/software-agent-sdk#1547 Co-authored-by: openhands <[email protected]>
follow up to: #1467
fixes #1527
Implement additional_context injection in UserPromptSubmit hooks
Implement stop hook integration in conversation run loop
Add comprehensive tests for both features
Add advanced hooks example (34_hooks_advanced.py)
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.12-nodejs22golang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:ab9d4ec-pythonRun
All tags pushed for this build
About Multi-Architecture Support
ab9d4ec-python) is a multi-arch manifest supporting both amd64 and arm64ab9d4ec-python-amd64) are also available if needed