-
Notifications
You must be signed in to change notification settings - Fork 2
fix: back button thread cycling and Korean short-mode truncation #13
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ | |
| function getMaxTokens(length: ResponseLength): number { | ||
| switch (length) { | ||
| case "short": | ||
| return 200 | ||
| return 350 | ||
| case "long": | ||
| return 1024 | ||
| default: | ||
|
|
@@ -41,7 +41,7 @@ | |
| let lastAllowedIndex = text.length | ||
| let match: RegExpExecArray | null | ||
|
|
||
| while ((match = wordRegex.exec(text)) !== null) { | ||
| wordCount += 1 | ||
| if (wordCount === wordLimit) { | ||
| lastAllowedIndex = wordRegex.lastIndex | ||
|
|
@@ -56,7 +56,7 @@ | |
| return { text, truncated: false } | ||
| } | ||
|
|
||
| function countWords(text: string): number { | ||
| return text.trim().split(/\s+/).filter(Boolean).length | ||
| } | ||
|
|
||
|
|
@@ -72,12 +72,19 @@ | |
|
|
||
| function polishTruncatedShortResponse(text: string, wordLimit: number): string { | ||
| let result = text.trimEnd() | ||
| const sentenceMatches = [...result.matchAll(/[.!?。!?](?=\s|$)/g)] | ||
|
|
||
| // Already ends cleanly - just enforce word limit | ||
| if (/[.!?。!?]$/u.test(result)) { | ||
| return clampToWordLimit(result, wordLimit).text | ||
| } | ||
|
|
||
| // Try to truncate at the last complete sentence | ||
| const sentenceMatches = [...result.matchAll(/[.!?。!?](?=\s|$)/g)] | ||
| if (sentenceMatches.length > 0) { | ||
| const lastSentence = sentenceMatches[sentenceMatches.length - 1] | ||
| const sentenceSafe = result.slice(0, (lastSentence.index ?? 0) + lastSentence[0].length).trimEnd() | ||
|
Comment on lines
+82
to
86
|
||
| if (countWords(sentenceSafe) >= Math.min(40, wordLimit)) { | ||
| // Accept if we keep at least 30% of the content (works for both EN and KO) | ||
| if (sentenceSafe.length >= result.length * 0.3) { | ||
| result = sentenceSafe | ||
| } | ||
| } | ||
|
Comment on lines
86
to
91
|
||
|
|
@@ -227,7 +234,7 @@ | |
| } | ||
|
|
||
| if (!request.signal?.aborted) { | ||
| if (wordLimit && truncatedShortResponse) { | ||
| if (wordLimit) { | ||
| fullContent = polishTruncatedShortResponse(fullContent, wordLimit) | ||
| } | ||
|
|
||
|
|
||
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.
polishTruncatedShortResponsereturns early when the original text ends with sentence punctuation, butclampToWordLimitcan still truncate the content to the word limit and produce a string that no longer ends with punctuation (and won’t get the later cleanup/ellipsis). Consider clamping first, then re-checking the ending, or using the{ truncated }flag to only early-return when no truncation occurred.