Skip to content

Commit 362352d

Browse files
committed
refactor: align compress blocks with bN metadata and placeholders
1 parent 66f4e66 commit 362352d

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

lib/message-ids.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ export function parseBoundaryId(id: string): ParsedBoundaryId | null {
8383
return null
8484
}
8585

86-
export function formatMessageIdMarker(ref: string): string {
87-
return `Message ID: ${ref}`
88-
}
89-
9086
export function formatMessageIdTag(ref: string): string {
9187
return `<${MESSAGE_ID_TAG_NAME}>${ref}</${MESSAGE_ID_TAG_NAME}>`
9288
}

lib/messages/inject/inject.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { SessionState, WithParts } from "../../state"
22
import type { Logger } from "../../logger"
33
import type { PluginConfig } from "../../config"
4-
import { formatMessageIdMarker, formatMessageIdTag } from "../../message-ids"
4+
import { formatMessageIdTag } from "../../message-ids"
55
import { createSyntheticTextPart, createSyntheticToolPart, isIgnoredUserMessage } from "../utils"
66
import {
77
addAnchor,
@@ -102,16 +102,10 @@ export const insertMessageIdContext = (state: SessionState, messages: WithParts[
102102
continue
103103
}
104104

105-
const marker = formatMessageIdMarker(messageRef)
106105
const tag = formatMessageIdTag(messageRef)
107106

108-
if (message.info.role === "user") {
109-
const hasMarker = message.parts.some(
110-
(part) => part.type === "text" && part.text.trim() === marker,
111-
)
112-
if (!hasMarker) {
113-
message.parts.push(createSyntheticTextPart(message, marker))
114-
}
107+
if (message.info.role === "user" && !isIgnoredUserMessage(message)) {
108+
message.parts.push(createSyntheticTextPart(message, tag))
115109
continue
116110
}
117111

@@ -124,6 +118,6 @@ export const insertMessageIdContext = (state: SessionState, messages: WithParts[
124118
continue
125119
}
126120

127-
message.parts.push(createSyntheticToolPart(message, marker, toolModelId))
121+
message.parts.push(createSyntheticToolPart(message, tag, toolModelId))
128122
}
129123
}

lib/tools/compress-utils.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { SessionState, WithParts, CompressSummary } from "../state"
2-
import { formatBlockRef, parseBoundaryId } from "../message-ids"
2+
import { formatBlockRef, formatMessageIdTag, parseBoundaryId } from "../message-ids"
33
import { isIgnoredUserMessage } from "../messages/utils"
44
import { countAllMessageTokens, countTokens } from "../strategies/utils"
55

6-
const BLOCK_PLACEHOLDER_REGEX = /\{block_(\d+)\}/gi
6+
const BLOCK_PLACEHOLDER_REGEX = /\(b(\d+)\)|\{block_(\d+)\}/gi
77

88
export interface CompressToolArgs {
99
topic: string
@@ -56,11 +56,15 @@ export interface AppliedCompressionResult {
5656
}
5757

5858
export 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

6266
export function formatBlockPlaceholder(blockId: number): string {
63-
return `{block_${blockId}}`
67+
return `(b${blockId})`
6468
}
6569

6670
export 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

507512
export 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

516522
export 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*\[Compressed conversation b\d+\]/i)
565+
function restoreStoredCompressedSummary(summary: string): string {
566+
const headerMatch = summary.match(/^\s*\[Compressed conversation(?: section)?(?: 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)*<dcp-message-id>b\d+<\/dcp-message-id>\s*$/i, "")
575+
.replace(/(?:\r?\n)+$/, "")
567576
}
568577

569578
function 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

Comments
 (0)