-
Notifications
You must be signed in to change notification settings - Fork 200
fix: keep conversation titles valid UTF-8 in promptToTitle truncation
#9837
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
eminemead
wants to merge
1
commit into
rilldata:main
Choose a base branch
from
eminemead:xiaofei.yin/fix-conversation-title-utf8
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.
+111
−5
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package ai | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| "unicode/utf8" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPromptToTitle_multibyteTruncationStaysValidUTF8(t *testing.T) { | ||
| // Live Rill_UO prompt whose previous byte-wise cut (title[:47]+"...") split 有. | ||
| prompt := "今年乘用车增长快于大盘的区域都有哪些" | ||
| require.Greater(t, len(prompt), 50) | ||
|
|
||
| oldCut := prompt[:47] + "..." | ||
| require.False(t, utf8.ValidString(oldCut), "the previous cut point must be invalid UTF-8 (the bug)") | ||
|
|
||
| title := promptToTitle(prompt) | ||
| require.True(t, utf8.ValidString(title)) | ||
| require.LessOrEqual(t, len(title), 50) | ||
| require.True(t, strings.HasSuffix(title, "...")) | ||
| require.Equal(t, "今年乘用车增长快于大盘的区域都...", title) | ||
| } | ||
|
|
||
| func TestPromptToTitle_asciiStillTruncatesAt50Bytes(t *testing.T) { | ||
| title := promptToTitle(strings.Repeat("a", 80)) | ||
| require.Equal(t, strings.Repeat("a", 47)+"...", title) | ||
| require.True(t, utf8.ValidString(title)) | ||
| } | ||
|
|
||
| func TestPromptToTitle_emptyFallsBack(t *testing.T) { | ||
| require.Equal(t, "New Conversation", promptToTitle(" \n\t ")) | ||
| } |
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
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.
truncateUTF8is called exactly once, with a constant, which also makes themaxBytes <= len(ellipsis)branch unreachable. Per the repo's convention against single-use utility functions, the back-off could be inlined inpromptToTitleas a walk from index 47 while!utf8.RuneStart(title[i]), which avoids re-validating the whole prefix on every iteration. Either form relies on the prompt being valid UTF-8 on entry, which holds for all current entry points.