11import type { SessionState , WithParts , CompressSummary } from "../state"
2- import { formatBlockRef , parseBoundaryId } from "../message-ids"
2+ import { formatBlockRef , formatMessageIdTag , parseBoundaryId } from "../message-ids"
33import { isIgnoredUserMessage } from "../messages/utils"
44import { countAllMessageTokens , countTokens } from "../strategies/utils"
55
6- const BLOCK_PLACEHOLDER_REGEX = / \{ b l o c k _ ( \d + ) \} / gi
6+ const BLOCK_PLACEHOLDER_REGEX = / \( b ( \d + ) \) | \ {b l o c k _ ( \d + ) \} / gi
77
88export interface CompressToolArgs {
99 topic : string
@@ -56,11 +56,15 @@ export interface AppliedCompressionResult {
5656}
5757
5858export function formatCompressedBlockHeader ( blockId : number ) : string {
59- return `[Compressed conversation b${ blockId } ]`
59+ return "[Compressed conversation section]"
60+ }
61+
62+ export function formatCompressedBlockFooter ( blockId : number ) : string {
63+ return formatMessageIdTag ( formatBlockRef ( blockId ) )
6064}
6165
6266export function formatBlockPlaceholder ( blockId : number ) : string {
63- return `{block_ ${ blockId } } `
67+ return `(b ${ blockId } ) `
6468}
6569
6670export function validateCompressArgs ( args : CompressToolArgs ) : void {
@@ -343,7 +347,8 @@ export function parseBlockPlaceholders(summary: string): ParsedBlockPlaceholder[
343347 let match : RegExpExecArray | null
344348 while ( ( match = regex . exec ( summary ) ) !== null ) {
345349 const full = match [ 0 ]
346- const parsed = Number . parseInt ( match [ 1 ] , 10 )
350+ const blockIdPart = match [ 1 ] || match [ 2 ]
351+ const parsed = Number . parseInt ( blockIdPart , 10 )
347352 if ( ! Number . isInteger ( parsed ) ) {
348353 continue
349354 }
@@ -455,7 +460,7 @@ export function injectBlockPlaceholders(
455460 }
456461
457462 expanded += summary . slice ( cursor , placeholder . startIndex )
458- expanded += stripCompressedBlockHeader ( target . summary )
463+ expanded += restoreStoredCompressedSummary ( target . summary )
459464 cursor = placeholder . endIndex
460465
461466 if ( ! consumedSeen . has ( placeholder . blockId ) ) {
@@ -506,11 +511,12 @@ export function allocateBlockId(summaries: CompressSummary[]): number {
506511
507512export function addCompressedBlockHeader ( blockId : number , summary : string ) : string {
508513 const header = formatCompressedBlockHeader ( blockId )
514+ const footer = formatCompressedBlockFooter ( blockId )
509515 const body = summary . trim ( )
510516 if ( body . length === 0 ) {
511- return header
517+ return ` ${ header } \n ${ footer } `
512518 }
513- return `${ header } \n${ body } `
519+ return `${ header } \n${ body } \n\n ${ footer } `
514520}
515521
516522export function applyCompressionState (
@@ -556,14 +562,17 @@ export function countSummaryTokens(summary: string): number {
556562 return countTokens ( summary )
557563}
558564
559- function stripCompressedBlockHeader ( summary : string ) : string {
560- const headerMatch = summary . match ( / ^ \s * \[ C o m p r e s s e d c o n v e r s a t i o n b \d + \] / i)
565+ function restoreStoredCompressedSummary ( summary : string ) : string {
566+ const headerMatch = summary . match ( / ^ \s * \[ C o m p r e s s e d c o n v e r s a t i o n (?: s e c t i o n ) ? (?: b \d + ) ? \] / i)
561567 if ( ! headerMatch ) {
562568 return summary
563569 }
564570
565571 const afterHeader = summary . slice ( headerMatch [ 0 ] . length )
566- return afterHeader . replace ( / ^ (?: \r ? \n ) + / , "" )
572+ const withoutLeadingBreaks = afterHeader . replace ( / ^ (?: \r ? \n ) + / , "" )
573+ return withoutLeadingBreaks
574+ . replace ( / (?: \r ? \n ) * < d c p - m e s s a g e - i d > b \d + < \/ d c p - m e s s a g e - i d > \s * $ / i, "" )
575+ . replace ( / (?: \r ? \n ) + $ / , "" )
567576}
568577
569578function injectBoundarySummaryIfMissing (
@@ -586,7 +595,7 @@ function injectBoundarySummaryIfMissing(
586595 throw new Error ( `Compressed block not found: ${ formatBlockPlaceholder ( reference . blockId ) } ` )
587596 }
588597
589- const injectedBody = stripCompressedBlockHeader ( target . summary )
598+ const injectedBody = restoreStoredCompressedSummary ( target . summary )
590599 const next =
591600 position === "start"
592601 ? mergeWithSpacing ( injectedBody , summary )
0 commit comments