fix: back button thread cycling and Korean short-mode truncation - #13
Conversation
…t off Navigation: use router.replace for thread switching and new-debate within the chat page so back button returns to homepage instead of cycling through every previously visited thread. Truncation: increase short-mode maxTokens from 200 to 350 to give Korean enough room to complete thoughts. Apply sentence-boundary cleanup to all short responses (not just word-limit-truncated ones). Use length-based threshold instead of word-count for sentence fallback so it works for both English and Korean text.
There was a problem hiding this comment.
Pull request overview
This PR adjusts chat navigation history behavior and improves “short” response truncation/cleanup, with special handling to reduce mid-sentence cutoffs (notably for Korean).
Changes:
- Switch thread selection and “new debate” navigation to
router.replaceto prevent the back button from cycling through prior threads. - Increase short-mode
maxTokens(200 → 350) and apply sentence-boundary cleanup more broadly for short responses. - Change the sentence-fallback threshold from word-count-based to length-based.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/components/ThreadDropdown.tsx | Uses router.replace when switching threads to avoid adding history entries. |
| src/app/chat/page.tsx | Uses router.replace("/chat") on reset/new debate to keep back button behavior consistent. |
| src/app/api/chat/route.ts | Adjusts short-mode token budget and refines truncation polishing/sentence-boundary logic. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Already ends cleanly - just enforce word limit | ||
| if (/[.!?。!?]$/u.test(result)) { | ||
| return clampToWordLimit(result, wordLimit).text | ||
| } |
There was a problem hiding this comment.
polishTruncatedShortResponse returns early when the original text ends with sentence punctuation, but clampToWordLimit can 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.
| // 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() |
There was a problem hiding this comment.
The sentence-boundary detection only matches punctuation when followed by whitespace/end (/(?=\s|$)/). Short responses often end with quotes/parentheses (e.g., ..." or ...)), which will bypass sentence detection and cause the function to append ... even when the sentence is complete. Consider allowing common closing delimiters after the punctuation in both the ends cleanly check and sentenceMatches regex.
| const sentenceSafe = result.slice(0, (lastSentence.index ?? 0) + lastSentence[0].length).trimEnd() | ||
| 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 | ||
| } | ||
| } |
There was a problem hiding this comment.
countWords is no longer used after switching the sentence-fallback threshold to a length-based check. Removing the unused helper will avoid dead code and potential lint noise.
…lean endings Remove unused countWords function. Fix early-return path where clampToWordLimit could truncate a clean-ending response and produce text that no longer ends with punctuation - now falls through to sentence-boundary cleanup when that happens.
Summary
router.replacefor thread switching and new-debate navigation within the chat page so the browser back button returns to the homepage instead of cycling through every previously visited threadmaxTokensfrom 200 to 350 to give Korean text enough room to complete thoughts (Korean characters use ~2-3x more tokens than English)Test results