@@ -17,6 +17,7 @@ import {
1717import { Dynamic } from "solid-js/web"
1818import path from "node:path"
1919import { mkdir , writeFile } from "node:fs/promises"
20+ import { appendFileSync } from "node:fs"
2021import { useRoute , useRouteData } from "../../context/route"
2122import { useProject } from "../../context/project"
2223import { useSync } from "../../context/sync"
@@ -1487,15 +1488,213 @@ function AssistantMessage(props: { message: AssistantMessage; parts: Part[]; las
14871488 const childShortcut = useCommandShortcut ( "session.child.first" )
14881489 const backgroundShortcut = useCommandShortcut ( "session.background" )
14891490
1491+ // Collapse all reasoning parts into a single block (regardless of whether
1492+ // they were adjacent in the part stream) so models that fragment extended
1493+ // thinking across reasoning/text/reasoning boundaries don't render
1494+ // scattered "Thought" boxes. Some models (e.g. MiniMax-M3) also echo the
1495+ // thinking content into the text part inside `<think>`/`</think>` tags
1496+ // (sometimes split across the reasoning/text boundary, sometimes already
1497+ // opened inside reasoning and closed inside text) — strip those so the
1498+ // response text stays clean. While merging, drop duplicate reasoning
1499+ // chunks that streaming models occasionally emit. We compare on a
1500+ // normalized fingerprint (lowercased, no whitespace, no Unicode
1501+ // punctuation) so "friendlyhello back" matches "friendly hello back" and
1502+ // use SUBSTRING `includes` (not prefix/suffix only) so we catch duplicates
1503+ // even when the model interleaves new content between re-sends of the
1504+ // same chunk (e.g. 4-part stream: A B A B where the second A and B are
1505+ // near-duplicates of the first). After merging, we also strip the
1506+ // reasoning echo from any text part whose content STARTS with the
1507+ // reasoning (the model often streams "thinking aloud" into the text
1508+ // part, then appends the actual response — we keep only the response).
1509+ // The sync store keeps every part; this only changes what the TUI shows.
1510+ //
1511+ // DEBUG: when reasoning dedup misses a case, set DEBUG_DEDUP_LOG = true
1512+ // and check C:/Users/user/Downloads/opencode_work/displayparts.log
1513+ // for the part sequence, fingerprints, and merge decisions.
1514+ const DEBUG_DEDUP_LOG = false
1515+ const DEDUP_LOG_PATH = "C:/Users/user/Downloads/opencode_work/displayparts.log"
1516+ const dedupLog = ( msg : string ) => {
1517+ if ( ! DEBUG_DEDUP_LOG ) return
1518+ try {
1519+ appendFileSync ( DEDUP_LOG_PATH , `${ new Date ( ) . toISOString ( ) . slice ( 11 , 23 ) } ${ msg } \n` )
1520+ } catch { }
1521+ }
1522+ // Find the position in `text` after the content equivalent to `reasoning`
1523+ // (modulo whitespace / Unicode punctuation / case). Returns -1 if `text`
1524+ // does not start with the reasoning in fingerprint space. Used to strip
1525+ // the reasoning echo from a text part that begins with the same content
1526+ // the model streamed into the ReasoningPart, so the tail (the actual
1527+ // response) is preserved verbatim.
1528+ const findReasoningEndInText = ( reasoning : string , text : string ) : number => {
1529+ let rIdx = 0
1530+ let tIdx = 0
1531+ const rLower = reasoning . toLowerCase ( )
1532+ while ( rIdx < rLower . length && tIdx < text . length ) {
1533+ const rChar = rLower [ rIdx ]
1534+ const tChar = text [ tIdx ] . toLowerCase ( )
1535+ if ( rChar === tChar ) {
1536+ rIdx ++
1537+ tIdx ++
1538+ } else if ( / [ \s \p{ P} ] / u. test ( rChar ) ) {
1539+ rIdx ++
1540+ } else if ( / [ \s \p{ P} ] / u. test ( tChar ) ) {
1541+ tIdx ++
1542+ } else {
1543+ return - 1
1544+ }
1545+ }
1546+ if ( rIdx < rLower . length ) return - 1
1547+ while ( tIdx < text . length && / [ \s \p{ P} ] / u. test ( text [ tIdx ] ) ) tIdx ++
1548+ return tIdx
1549+ }
1550+ const displayParts = createMemo ( ( ) => {
1551+ const stripThink = ( s : string ) => s . replace ( / < \/ ? t h i n k > / g, "" ) . trim ( )
1552+ // For dedup comparison only — strips whitespace, punctuation, and case
1553+ // so "friendlyhello back." matches "friendly hello back." Display keeps
1554+ // the original whitespace.
1555+ const fingerprint = ( s : string ) => s . toLowerCase ( ) . replace ( / [ \s \p{ P} ] / gu, "" )
1556+ const truncate = ( s : string , n : number ) => ( s . length > n ? s . slice ( 0 , n ) + "…" : s )
1557+ dedupLog ( `--- memo run: ${ props . parts . length } parts, msg=${ props . message . id } ---` )
1558+ const reasoningParts : ReasoningPart [ ] = [ ]
1559+ const otherParts : Part [ ] = [ ]
1560+ for ( let i = 0 ; i < props . parts . length ; i ++ ) {
1561+ const part = props . parts [ i ]
1562+ if ( part . type === "reasoning" ) {
1563+ const cleaned = stripThink ( part . text )
1564+ dedupLog (
1565+ ` in[${ i } ] reasoning rawLen=${ part . text . length } cleanLen=${ cleaned . length } fp=${ truncate ( fingerprint ( cleaned ) , 60 ) } text=${ truncate ( JSON . stringify ( cleaned ) , 120 ) } ` ,
1566+ )
1567+ if ( cleaned ) reasoningParts . push ( { ...part , text : cleaned } )
1568+ } else if ( part . type === "text" ) {
1569+ const cleaned = stripThink ( part . text )
1570+ dedupLog (
1571+ ` in[${ i } ] text rawLen=${ part . text . length } cleanLen=${ cleaned . length } fp=${ truncate ( fingerprint ( cleaned ) , 60 ) } text=${ truncate ( JSON . stringify ( cleaned ) , 120 ) } ` ,
1572+ )
1573+ if ( cleaned ) otherParts . push ( { ...part , text : cleaned } )
1574+ } else {
1575+ dedupLog ( ` in[${ i } ] ${ part . type } (passthrough)` )
1576+ otherParts . push ( part )
1577+ }
1578+ }
1579+ if ( reasoningParts . length === 0 ) {
1580+ dedupLog ( `--- result: 0 reasoning parts, ${ otherParts . length } other parts ---` )
1581+ return otherParts
1582+ }
1583+ dedupLog ( ` >> reducing ${ reasoningParts . length } reasoning parts` )
1584+ const merged : ReasoningPart = reasoningParts . reduce (
1585+ ( acc , curr ) => {
1586+ const accFp = fingerprint ( acc . text )
1587+ const currFp = fingerprint ( curr . text )
1588+ const accEnd = curr . time . end ?? acc . time . end
1589+ let action : string
1590+ let next : ReasoningPart
1591+ if ( accFp . length > 0 ) {
1592+ if ( accFp . includes ( currFp ) && currFp . length > 0 ) {
1593+ // curr is fully covered by acc (anywhere in acc's text, modulo
1594+ // whitespace/punct/case). Drop curr — it's a re-sent chunk.
1595+ action = "DROP curr (acc.includes(curr))"
1596+ next = { ...acc , time : { start : acc . time . start , end : accEnd } }
1597+ dedupLog (
1598+ ` merge: ${ action } accFpLen=${ accFp . length } currFpLen=${ currFp . length } accFpTail=${ truncate ( accFp , 30 ) } currFpHead=${ truncate ( currFp , 30 ) } ` ,
1599+ )
1600+ return next
1601+ }
1602+ if ( currFp . includes ( accFp ) && accFp . length > 0 ) {
1603+ // acc is fully covered by curr (curr is a strict rewrite of acc
1604+ // with extra trailing content). Replace acc with curr.
1605+ action = "REPLACE acc with curr (curr.includes(acc))"
1606+ next = { ...acc , text : curr . text , time : { start : acc . time . start , end : accEnd } }
1607+ dedupLog (
1608+ ` merge: ${ action } accFpLen=${ accFp . length } currFpLen=${ currFp . length } accFpTail=${ truncate ( accFp , 30 ) } currFpHead=${ truncate ( currFp , 30 ) } ` ,
1609+ )
1610+ return next
1611+ }
1612+ // No full containment — fall back to plain concat. (Earlier we had
1613+ // strict prefix/suffix checks here; they missed the 4-part
1614+ // alternating case A B A B where the second A is contained in
1615+ // acc (A+B) but neither at the start nor at the end. The
1616+ // substring-includes checks above cover those.)
1617+ action = "CONCAT (no overlap detected)"
1618+ } else {
1619+ action = "CONCAT (acc was empty)"
1620+ }
1621+ next = {
1622+ ...acc ,
1623+ text : acc . text + curr . text ,
1624+ time : { start : acc . time . start , end : accEnd } ,
1625+ }
1626+ dedupLog (
1627+ ` merge: ${ action } accFpLen=${ accFp . length } currFpLen=${ currFp . length } accFpTail=${ truncate ( accFp , 30 ) } currFpHead=${ truncate ( currFp , 30 ) } ` ,
1628+ )
1629+ return next
1630+ } ,
1631+ )
1632+ // Second pass: handle text parts that echo the merged reasoning.
1633+ // (a) exact equal to reasoning → drop (no new content)
1634+ // (b) subset of reasoning → drop (text is just a fragment of reasoning)
1635+ // (c) starts with reasoning → STRIP the echo, keep the tail (the
1636+ // actual response, e.g. "Привет! Чем могу помочь?")
1637+ // (d) superset of reasoning (reasoning inside, text wraps it) → keep
1638+ // (e) no overlap → keep
1639+ // Stripping (c) uses a character-level diff that skips whitespace and
1640+ // Unicode punctuation on both sides, so reasoning "askwhat they need."
1641+ // and text "ask what they need." still align correctly and the response
1642+ // after the echo is preserved verbatim (with its original whitespace).
1643+ const reasoningFp = fingerprint ( merged . text )
1644+ const transformedOtherParts = otherParts
1645+ . map ( ( part ) => {
1646+ if ( part . type !== "text" || reasoningFp . length === 0 ) return part
1647+ const partFp = fingerprint ( part . text )
1648+ if ( partFp . length === 0 ) return part
1649+ if ( reasoningFp === partFp ) {
1650+ dedupLog (
1651+ ` text-dedup: DROP (exact equal to reasoning) reasoningFpLen=${ reasoningFp . length } partFp=${ truncate ( partFp , 50 ) } ` ,
1652+ )
1653+ return null
1654+ }
1655+ if ( reasoningFp . includes ( partFp ) ) {
1656+ dedupLog (
1657+ ` text-dedup: DROP (subset of reasoning) reasoningFpLen=${ reasoningFp . length } partFp=${ truncate ( partFp , 50 ) } ` ,
1658+ )
1659+ return null
1660+ }
1661+ if ( partFp . startsWith ( reasoningFp ) ) {
1662+ // Text begins with the reasoning content. Walk the original
1663+ // (non-normalized) text and find the position right after the
1664+ // reasoning ends, then keep only the tail.
1665+ const endPos = findReasoningEndInText ( merged . text , part . text )
1666+ if ( endPos > 0 && endPos < part . text . length ) {
1667+ const remaining = part . text . slice ( endPos ) . trim ( )
1668+ if ( remaining . length > 0 ) {
1669+ dedupLog (
1670+ ` text-strip: stripped reasoning echo at pos=${ endPos } , kept ${ remaining . length } chars: ${ truncate ( JSON . stringify ( remaining ) , 80 ) } ` ,
1671+ )
1672+ return { ...part , text : remaining }
1673+ }
1674+ }
1675+ dedupLog (
1676+ ` text-dedup: DROP (text started with reasoning echo, nothing left after strip) partFp=${ truncate ( partFp , 50 ) } ` ,
1677+ )
1678+ return null
1679+ }
1680+ return part
1681+ } )
1682+ . filter ( ( p ) : p is Part => p !== null )
1683+ dedupLog (
1684+ `--- result: merged reasoning len=${ merged . text . length } text=${ truncate ( JSON . stringify ( merged . text ) , 200 ) } ; otherParts ${ otherParts . length } →${ transformedOtherParts . length } after text-dedup ---` ,
1685+ )
1686+ return [ merged , ...transformedOtherParts ]
1687+ } )
1688+
14901689 return (
14911690 < >
1492- < For each = { props . parts } >
1691+ < For each = { displayParts ( ) } >
14931692 { ( part , index ) => {
14941693 const component = createMemo ( ( ) => PART_MAPPING [ part . type as keyof typeof PART_MAPPING ] )
14951694 return (
14961695 < Show when = { component ( ) } >
14971696 < Dynamic
1498- last = { index ( ) === props . parts . length - 1 }
1697+ last = { index ( ) === displayParts ( ) . length - 1 }
14991698 component = { component ( ) }
15001699 part = { part as any }
15011700 message = { props . message }
0 commit comments