-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(hook): include response and stop_reason in Stop hook payload #2308
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
Open
AkaCoder404
wants to merge
4
commits into
MoonshotAI:main
Choose a base branch
from
AkaCoder404:feat/hook-stop-response
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a6f66ce
feat(hook): include response and stop_reason in Stop hook payload
AkaCoder404 c1a10da
docs(hooks): document response and stop_reason fields in Stop hook
AkaCoder404 3a4e6c6
docs(changelog): add Stop hook response and stop_reason entries
AkaCoder404 18b3e0e
fix(hook): truncate Stop hook response to 500 chars for parity with S…
AkaCoder404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """Tests for KimiSoul lifecycle hook integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
| from kosong.message import Message | ||
| from kosong.tooling.empty import EmptyToolset | ||
|
|
||
| import kimi_cli.soul.kimisoul as kimisoul_module | ||
| from kimi_cli.hooks.config import HookDef | ||
| from kimi_cli.hooks.engine import HookEngine | ||
| from kimi_cli.soul.agent import Agent, Runtime | ||
| from kimi_cli.soul.context import Context | ||
| from kimi_cli.soul.kimisoul import KimiSoul, TurnOutcome | ||
| from kimi_cli.wire.types import TextPart | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_stop_hook_includes_response_and_stop_reason( | ||
| runtime: Runtime, tmp_path: Path, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| """Stop hook receives response text and stop_reason from TurnOutcome.""" | ||
| captured_payloads: list[dict] = [] | ||
|
|
||
| async def _capture_trigger( | ||
| event: str, *, matcher_value: str = "", input_data: dict | None = None | ||
| ) -> list: | ||
| captured_payloads.append(input_data or {}) | ||
| return [] | ||
|
|
||
| agent = Agent( | ||
| name="Test Agent", | ||
| system_prompt="Test system prompt.", | ||
| toolset=EmptyToolset(), | ||
| runtime=runtime, | ||
| ) | ||
| soul = KimiSoul(agent, context=Context(file_backend=tmp_path / "history.jsonl")) | ||
| soul._turn = AsyncMock( # type: ignore[method-assign] | ||
| return_value=TurnOutcome( | ||
| stop_reason="no_tool_calls", | ||
| final_message=Message( | ||
| role="assistant", content=[TextPart(text="The fix is complete.")] | ||
| ), | ||
| step_count=1, | ||
| ) | ||
| ) | ||
| monkeypatch.setattr(kimisoul_module, "wire_send", lambda _msg: None) | ||
|
|
||
| hook_engine = HookEngine([HookDef(event="Stop", command="echo ok", timeout=5)]) | ||
| monkeypatch.setattr(hook_engine, "trigger", _capture_trigger) | ||
| soul.set_hook_engine(hook_engine) | ||
|
|
||
| await soul.run([TextPart(text="fix the bug")]) | ||
|
|
||
| stop_payloads = [p for p in captured_payloads if p.get("hook_event_name") == "Stop"] | ||
| assert len(stop_payloads) == 1 | ||
| assert stop_payloads[0].get("response") == "The fix is complete." | ||
| assert stop_payloads[0].get("stop_reason") == "no_tool_calls" |
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.
Uh oh!
There was an error while loading. Please reload this page.