Skip to content

fix: back button thread cycling and Korean short-mode truncation - #13

Merged
aiedwardyi merged 2 commits into
mainfrom
fix/navigation-history-and-truncation
Apr 8, 2026
Merged

aiedwardyi merged 2 commits into
mainfrom
fix/navigation-history-and-truncation

Conversation

@aiedwardyi

@aiedwardyi aiedwardyi commented Apr 8, 2026

Copy link
Copy Markdown
Owner

Summary

  • Use router.replace for 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 thread
  • Increase short-mode maxTokens from 200 to 350 to give Korean text enough room to complete thoughts (Korean characters use ~2-3x more tokens than English)
  • Apply sentence-boundary cleanup to all short responses, not just word-limit-truncated ones - catches cases where the model hits the token ceiling mid-sentence
  • Replace word-count threshold with length-based threshold for sentence fallback so it works correctly for both English and Korean

Test results

  • Open chat, switch between 3+ threads via dropdown, press back - goes to homepage, not previous thread
  • Homepage -> thread A -> switch to thread B -> back = homepage
  • Korean short-mode debate - Perplexity, Claude, GPT all end at sentence boundaries
  • No self-introduction from any model
  • Build passes cleanly

…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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.replace to 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.

Comment thread src/app/api/chat/route.ts Outdated
Comment on lines +76 to +79
// Already ends cleanly - just enforce word limit
if (/[.!?。!?]$/u.test(result)) {
return clampToWordLimit(result, wordLimit).text
}

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/app/api/chat/route.ts
Comment on lines +81 to 85
// 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()

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread src/app/api/chat/route.ts
Comment on lines 85 to 90
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
}
}

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
…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.
@aiedwardyi
aiedwardyi merged commit b359ed1 into main Apr 8, 2026
1 check passed
@aiedwardyi
aiedwardyi deleted the fix/navigation-history-and-truncation branch April 8, 2026 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants