feat(plugins): fire pre_llm_call, post_llm_call, on_session_start, on_session_end hooks#2930
Closed
crxssrazr93 wants to merge 1 commit into
Closed
Conversation
…_session_end hooks The plugin system declares 6 lifecycle hooks in VALID_HOOKS but only pre_tool_call and post_tool_call are actually invoked. The remaining 4 hooks (pre_llm_call, post_llm_call, on_session_start, on_session_end) are documented in the plugin guide and accepted by register_hook() but never fired — making them effectively dead code. This commit wires up all 4 missing hooks: - pre_llm_call / post_llm_call: fired in run_agent.py around the main LLM API call (both streaming and non-streaming paths) - on_session_start: fired in cli.py when the interactive session begins - on_session_end: fired in cli.py during session cleanup Each invocation is wrapped in try/except to match the existing pattern used by pre_tool_call/post_tool_call, ensuring a misbehaving plugin cannot break the core agent loop. Includes an example plugin (docs/llm-switch-plugin-example/) that demonstrates a practical use case: auto-managing a local llama-server when switching models via /model. The plugin uses pre_llm_call to detect local model names and start/swap the server, and on_session_end to clean up on exit. It also registers a switch_local_llm tool for agent-driven model switching. Model configurations are defined in a declarative YAML file rather than shell scripts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
|
Superseded — rebased onto latest main (hooks already merged via #3542). New PR incoming with plugin-only changes and updated hook signatures. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The plugin system declares 6 lifecycle hooks in
VALID_HOOKS(plugins.py:52-59), but only 2 are actually invoked (pre_tool_call,post_tool_call). The remaining 4 are documented in the plugin guide and accepted byregister_hook(), but never fired anywhere in the codebase — making them dead code.This PR wires up all 4 missing hooks:
pre_llm_call/post_llm_call— fired inrun_agent.pyaround the main LLM API call (covers both streaming and non-streaming paths)on_session_start— fired incli.pywhen the interactive session begins (after banner, before first prompt)on_session_end— fired incli.pyduring session cleanup (before_run_cleanup())Each invocation uses the same try/import/invoke/except pattern as the existing
pre_tool_call/post_tool_callhooks inmodel_tools.py.Motivation
These hooks unlock important plugin use cases that aren't possible today:
pre_llm_call: Auto-manage local LLM servers — when a user switches to a local model via/model custom:write, a plugin can detect the model name and start/swap a llama-server or vLLM instance before the API call goes out. This is the primary motivating use case.post_llm_call: Token usage tracking, response logging, cost monitoring across providers.on_session_start: Initialize plugin state, connect to external services, display plugin status.on_session_end: Clean up resources (kill servers, close connections, flush logs).Example Plugin: llm-switch
Included in
docs/llm-switch-plugin-example/is a complete working plugin that demonstrates the hooks. It auto-manages a local llama-server when switching models:models.yamlfile (GGUF paths, context sizes, sampling params)/model custom:write, thepre_llm_callhook detects "write" in the configon_session_endkills the server on exitswitch_local_llmtool for agent-driven switchingChanges
run_agent.py: +10 lines —invoke_hook("pre_llm_call", ...)before API call,invoke_hook("post_llm_call", ...)after responsecli.py: +10 lines —invoke_hook("on_session_start", ...)inrun(),invoke_hook("on_session_end", ...)in cleanupdocs/llm-switch-plugin-example/: Complete example plugin (6 files)Testing
~/.hermes/plugins/llm-switch/Platforms tested