-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(hooks): extract text from ContentPart for UserPromptSubmit hook #2176
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
tears-mysthrala
wants to merge
6
commits into
MoonshotAI:main
Choose a base branch
from
tears-mysthrala:fix-2148-userpromptsubmit-hook-empty-prompt
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
6 commits
Select commit
Hold shift + click to select a range
ef48323
fix(hooks): extract text from ContentPart for UserPromptSubmit hook
bdf20dd
Merge branch 'main' into fix-2148-userpromptsubmit-hook-empty-prompt
tears-mysthrala b1902ff
Merge branch 'main' into fix-2148-userpromptsubmit-hook-empty-prompt
tears-mysthrala 9043528
Merge remote-tracking branch 'upstream/main' into fix-2148-userprompt…
mkdl-clawdia 367a8df
test(hooks): initialize _plan_mode in UserPromptSubmit hook test soul…
mkdl-clawdia 42c3c88
refactor(soul): compute user message and text input once in run()
mkdl-clawdia 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Tests for UserPromptSubmit hook text extraction.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
| from kosong.message import ContentPart, TextPart | ||
|
|
||
| from kimi_cli.hooks.engine import HookEngine | ||
| from kimi_cli.soul.kimisoul import KimiSoul | ||
|
|
||
|
|
||
| def _make_runnable_soul() -> KimiSoul: | ||
| """Minimal KimiSoul bypassing __init__, just enough for run().""" | ||
| soul = object.__new__(KimiSoul) | ||
|
|
||
| runtime = MagicMock() | ||
| runtime.session.id = "test-session" | ||
| runtime.approval_runtime = None | ||
| runtime.oauth.ensure_fresh = AsyncMock() | ||
| soul._runtime = runtime | ||
|
|
||
| ctx = MagicMock() | ||
| ctx.history = [] | ||
| ctx.append_message = AsyncMock() | ||
| soul._context = ctx | ||
|
|
||
| soul._hook_engine = MagicMock(spec=HookEngine) | ||
| soul._hook_engine.trigger = AsyncMock(return_value=[]) | ||
|
|
||
| soul._loop_control = MagicMock() | ||
| soul._loop_control.max_ralph_iterations = 0 | ||
|
|
||
| soul._plan_mode = False | ||
|
|
||
| soul._agent = MagicMock() | ||
| soul._agent.system_prompt = "sys" | ||
|
|
||
| soul._turn = AsyncMock(return_value=MagicMock()) | ||
| soul._slash_commands = [] | ||
| soul._steer_queue = MagicMock() | ||
| soul._steer_queue.empty.return_value = True | ||
|
|
||
| soul._stop_hook_active = False | ||
| soul._injection_providers = [] | ||
| soul._compaction = MagicMock() | ||
| soul._checkpoint = AsyncMock() | ||
|
|
||
| return soul | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_user_prompt_submit_hook_receives_text_from_string() -> None: | ||
| """When user_input is a plain string, the hook receives it as prompt.""" | ||
| soul = _make_runnable_soul() | ||
|
|
||
| with patch("kimi_cli.soul.kimisoul.wire_send"): | ||
| await soul.run("hello world") | ||
|
|
||
| call_args = soul._hook_engine.trigger.call_args_list[0] | ||
| assert call_args[0][0] == "UserPromptSubmit" | ||
| assert call_args[1]["matcher_value"] == "hello world" | ||
| assert call_args[1]["input_data"]["prompt"] == "hello world" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_user_prompt_submit_hook_receives_text_from_content_parts() -> None: | ||
| """When user_input is a list of ContentPart, the hook receives extracted text.""" | ||
| soul = _make_runnable_soul() | ||
| parts: list[ContentPart] = [TextPart(text="hello"), TextPart(text="world")] | ||
|
|
||
| with patch("kimi_cli.soul.kimisoul.wire_send"): | ||
| await soul.run(parts) | ||
|
|
||
| call_args = soul._hook_engine.trigger.call_args_list[0] | ||
| assert call_args[0][0] == "UserPromptSubmit" | ||
| assert call_args[1]["matcher_value"] == "hello world" | ||
| assert call_args[1]["input_data"]["prompt"] == "hello world" | ||
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.
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.
_plan_modebefore callingKimiSoul.runThe new tests build
KimiSoulviaobject.__new__but never initialize_plan_mode, andrun()unconditionally reads that field when emitting telemetry ("plan" if self._plan_mode else "agent"). As written, this call path raisesAttributeErrorbefore the hook assertions execute, so both tests fail when run. Set_plan_mode(and any otherrun()-required fields) in_make_runnable_soulto keep the tests runnable.Useful? React with 👍 / 👎.