-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: 修复创建新对话时 persona 继承丢失问题 #7046
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
idiotsj
wants to merge
1
commit into
AstrBotDevs:master
Choose a base branch
from
idiotsj:fix/persona-inheritance-minimal
base: master
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.
+148
−12
Open
Changes from all commits
Commits
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,15 @@ | ||
| """Helpers for persona marker handling.""" | ||
|
|
||
| PERSONA_NONE_MARKER = "[%None]" | ||
|
|
||
|
|
||
| def is_persona_none_marker(persona_id: str | None) -> bool: | ||
| """Return whether the persona id is the explicit no-persona marker.""" | ||
| return persona_id == PERSONA_NONE_MARKER | ||
|
|
||
|
|
||
| def normalize_persona_id(persona_id: str | None) -> str | None: | ||
| """Normalize the explicit no-persona marker to None.""" | ||
| if is_persona_none_marker(persona_id): | ||
| return None | ||
| return persona_id |
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,99 @@ | ||
| """Tests for conversation persona inheritance behavior.""" | ||
|
|
||
| from types import SimpleNamespace | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from astrbot.builtin_stars.builtin_commands.commands.conversation import ( | ||
| ConversationCommands, | ||
| ) | ||
| from astrbot.core.conversation_mgr import ConversationManager | ||
| from astrbot.core.persona_utils import PERSONA_NONE_MARKER | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_new_conversation_inherits_current_persona_when_not_provided(): | ||
| db = MagicMock() | ||
| db.get_conversation_by_id = AsyncMock( | ||
| return_value=SimpleNamespace(persona_id="psychologist") | ||
| ) | ||
| db.create_conversation = AsyncMock( | ||
| return_value=SimpleNamespace(conversation_id="new-cid") | ||
| ) | ||
|
|
||
| manager = ConversationManager(db) | ||
| manager.session_conversations["test:private:u1"] = "old-cid" | ||
|
|
||
| with patch( | ||
| "astrbot.core.conversation_mgr.sp.session_put", | ||
| new=AsyncMock(return_value=None), | ||
| ): | ||
| await manager.new_conversation("test:private:u1", platform_id="test") | ||
|
|
||
| assert db.create_conversation.await_args.kwargs["persona_id"] == "psychologist" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_new_conversation_does_not_inherit_persona_none_marker(): | ||
| db = MagicMock() | ||
| db.get_conversation_by_id = AsyncMock( | ||
| return_value=SimpleNamespace(persona_id=PERSONA_NONE_MARKER) | ||
| ) | ||
| db.create_conversation = AsyncMock( | ||
| return_value=SimpleNamespace(conversation_id="new-cid") | ||
| ) | ||
|
|
||
| manager = ConversationManager(db) | ||
| manager.session_conversations["test:private:u1"] = "old-cid" | ||
|
|
||
| with patch( | ||
| "astrbot.core.conversation_mgr.sp.session_put", | ||
| new=AsyncMock(return_value=None), | ||
| ): | ||
| await manager.new_conversation("test:private:u1", platform_id="test") | ||
|
|
||
| assert db.create_conversation.await_args.kwargs["persona_id"] is None | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_new_conversation_keeps_explicit_persona_id(): | ||
| db = MagicMock() | ||
| db.get_conversation_by_id = AsyncMock( | ||
| return_value=SimpleNamespace(persona_id="psychologist") | ||
| ) | ||
| db.create_conversation = AsyncMock( | ||
| return_value=SimpleNamespace(conversation_id="new-cid") | ||
| ) | ||
|
|
||
| manager = ConversationManager(db) | ||
| manager.session_conversations["test:private:u1"] = "old-cid" | ||
|
|
||
| with patch( | ||
| "astrbot.core.conversation_mgr.sp.session_put", | ||
| new=AsyncMock(return_value=None), | ||
| ): | ||
| await manager.new_conversation( | ||
| "test:private:u1", | ||
| platform_id="test", | ||
| persona_id="teacher", | ||
| ) | ||
|
|
||
| assert db.create_conversation.await_args.kwargs["persona_id"] == "teacher" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_current_persona_id_returns_none_for_none_marker(): | ||
| context = MagicMock() | ||
| context.conversation_manager.get_curr_conversation_id = AsyncMock( | ||
| return_value="old-cid" | ||
| ) | ||
| context.conversation_manager.get_conversation = AsyncMock( | ||
| return_value=MagicMock(persona_id=PERSONA_NONE_MARKER) | ||
| ) | ||
|
|
||
| command = ConversationCommands(context) | ||
|
|
||
| result = await command._get_current_persona_id("test:private:u1") | ||
|
|
||
| assert result is None |
Oops, something went wrong.
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.
这段代码正确地实现了在创建新对话时继承当前人格的逻辑。
然而,我注意到这可能会导致一些调用路径(例如
conversation.py中的new_conv)出现效率问题。new_conv会先调用_get_current_persona_id来解析当前的人格,然后将结果传给new_conversation。如果当前人格是[%None],_get_current_persona_id会返回None,然后new_conversation会因为persona_id is None而再次执行几乎相同的逻辑来解析当前人格,这导致了重复的数据库查询。虽然这不影响功能的正确性,但为了提高性能和代码的简洁性,建议在未来的重构中,让调用方(如
new_conv)直接调用new_conversation()而不传递persona_id,完全依赖此处的继承逻辑。由于相关调用方的代码不在此次变更范围内,这可以作为一个后续的改进点。