@tarquinen/opencode-dcp 3.2.5-beta0 → 3.2.7-beta0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/token-utils.js +2 -2
- package/dist/lib/token-utils.js.map +1 -1
- package/index.ts +141 -0
- package/lib/analysis/tokens.ts +225 -0
- package/lib/auth.ts +37 -0
- package/lib/commands/compression-targets.ts +137 -0
- package/lib/commands/context.ts +132 -0
- package/lib/commands/decompress.ts +275 -0
- package/lib/commands/help.ts +76 -0
- package/lib/commands/index.ts +11 -0
- package/lib/commands/manual.ts +125 -0
- package/lib/commands/recompress.ts +224 -0
- package/lib/commands/stats.ts +148 -0
- package/lib/commands/sweep.ts +268 -0
- package/lib/compress/index.ts +3 -0
- package/lib/compress/message-utils.ts +250 -0
- package/lib/compress/message.ts +137 -0
- package/lib/compress/pipeline.ts +106 -0
- package/lib/compress/protected-content.ts +154 -0
- package/lib/compress/range-utils.ts +308 -0
- package/lib/compress/range.ts +180 -0
- package/lib/compress/search.ts +267 -0
- package/lib/compress/state.ts +268 -0
- package/lib/compress/timing.ts +77 -0
- package/lib/compress/types.ts +108 -0
- package/lib/compress-permission.ts +25 -0
- package/lib/config.ts +1071 -0
- package/lib/hooks.ts +378 -0
- package/lib/host-permissions.ts +101 -0
- package/lib/logger.ts +235 -0
- package/lib/message-ids.ts +172 -0
- package/lib/messages/index.ts +8 -0
- package/lib/messages/inject/inject.ts +215 -0
- package/lib/messages/inject/subagent-results.ts +82 -0
- package/lib/messages/inject/utils.ts +374 -0
- package/lib/messages/priority.ts +102 -0
- package/lib/messages/prune.ts +238 -0
- package/lib/messages/query.ts +56 -0
- package/lib/messages/reasoning-strip.ts +40 -0
- package/lib/messages/sync.ts +124 -0
- package/lib/messages/utils.ts +187 -0
- package/lib/prompts/compress-message.ts +42 -0
- package/lib/prompts/compress-range.ts +60 -0
- package/lib/prompts/context-limit-nudge.ts +18 -0
- package/lib/prompts/extensions/nudge.ts +43 -0
- package/lib/prompts/extensions/system.ts +32 -0
- package/lib/prompts/extensions/tool.ts +35 -0
- package/lib/prompts/index.ts +29 -0
- package/lib/prompts/iteration-nudge.ts +6 -0
- package/lib/prompts/store.ts +467 -0
- package/lib/prompts/system.ts +33 -0
- package/lib/prompts/turn-nudge.ts +10 -0
- package/lib/protected-patterns.ts +128 -0
- package/lib/state/index.ts +4 -0
- package/lib/state/persistence.ts +256 -0
- package/lib/state/state.ts +190 -0
- package/lib/state/tool-cache.ts +98 -0
- package/lib/state/types.ts +112 -0
- package/lib/state/utils.ts +334 -0
- package/lib/strategies/deduplication.ts +127 -0
- package/lib/strategies/index.ts +2 -0
- package/lib/strategies/purge-errors.ts +88 -0
- package/lib/subagents/subagent-results.ts +74 -0
- package/lib/token-utils.ts +162 -0
- package/lib/ui/notification.ts +346 -0
- package/lib/ui/utils.ts +287 -0
- package/package.json +12 -3
- package/tui/data/context.ts +177 -0
- package/tui/index.tsx +34 -0
- package/tui/routes/summary.tsx +175 -0
- package/tui/shared/names.ts +9 -0
- package/tui/shared/theme.ts +58 -0
- package/tui/shared/types.ts +38 -0
- package/tui/slots/sidebar-content.tsx +502 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import type { CompressionBlock, SessionState } from "../state"
|
|
2
|
+
import { resolveAnchorMessageId, resolveBoundaryIds, resolveSelection } from "./search"
|
|
3
|
+
import type {
|
|
4
|
+
BoundaryReference,
|
|
5
|
+
CompressRangeToolArgs,
|
|
6
|
+
InjectedSummaryResult,
|
|
7
|
+
ParsedBlockPlaceholder,
|
|
8
|
+
ResolvedRangeCompression,
|
|
9
|
+
SearchContext,
|
|
10
|
+
} from "./types"
|
|
11
|
+
|
|
12
|
+
const BLOCK_PLACEHOLDER_REGEX = /\(b(\d+)\)|\{block_(\d+)\}/gi
|
|
13
|
+
|
|
14
|
+
export function validateArgs(args: CompressRangeToolArgs): void {
|
|
15
|
+
if (typeof args.topic !== "string" || args.topic.trim().length === 0) {
|
|
16
|
+
throw new Error("topic is required and must be a non-empty string")
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!Array.isArray(args.content) || args.content.length === 0) {
|
|
20
|
+
throw new Error("content is required and must be a non-empty array")
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
for (let index = 0; index < args.content.length; index++) {
|
|
24
|
+
const entry = args.content[index]
|
|
25
|
+
const prefix = `content[${index}]`
|
|
26
|
+
|
|
27
|
+
if (typeof entry?.startId !== "string" || entry.startId.trim().length === 0) {
|
|
28
|
+
throw new Error(`${prefix}.startId is required and must be a non-empty string`)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (typeof entry?.endId !== "string" || entry.endId.trim().length === 0) {
|
|
32
|
+
throw new Error(`${prefix}.endId is required and must be a non-empty string`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (typeof entry?.summary !== "string" || entry.summary.trim().length === 0) {
|
|
36
|
+
throw new Error(`${prefix}.summary is required and must be a non-empty string`)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function resolveRanges(
|
|
42
|
+
args: CompressRangeToolArgs,
|
|
43
|
+
searchContext: SearchContext,
|
|
44
|
+
state: SessionState,
|
|
45
|
+
): ResolvedRangeCompression[] {
|
|
46
|
+
return args.content.map((entry, index) => {
|
|
47
|
+
const normalizedEntry = {
|
|
48
|
+
startId: entry.startId.trim(),
|
|
49
|
+
endId: entry.endId.trim(),
|
|
50
|
+
summary: entry.summary,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const { startReference, endReference } = resolveBoundaryIds(
|
|
54
|
+
searchContext,
|
|
55
|
+
state,
|
|
56
|
+
normalizedEntry.startId,
|
|
57
|
+
normalizedEntry.endId,
|
|
58
|
+
)
|
|
59
|
+
const selection = resolveSelection(searchContext, startReference, endReference)
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
index,
|
|
63
|
+
entry: normalizedEntry,
|
|
64
|
+
selection,
|
|
65
|
+
anchorMessageId: resolveAnchorMessageId(startReference),
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function validateNonOverlapping(plans: ResolvedRangeCompression[]): void {
|
|
71
|
+
const sortedPlans = [...plans].sort(
|
|
72
|
+
(left, right) =>
|
|
73
|
+
left.selection.startReference.rawIndex - right.selection.startReference.rawIndex ||
|
|
74
|
+
left.selection.endReference.rawIndex - right.selection.endReference.rawIndex ||
|
|
75
|
+
left.index - right.index,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const issues: string[] = []
|
|
79
|
+
|
|
80
|
+
for (let index = 1; index < sortedPlans.length; index++) {
|
|
81
|
+
const previous = sortedPlans[index - 1]
|
|
82
|
+
const current = sortedPlans[index]
|
|
83
|
+
if (!previous || !current) {
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (current.selection.startReference.rawIndex > previous.selection.endReference.rawIndex) {
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
issues.push(
|
|
92
|
+
`content[${previous.index}] (${previous.entry.startId}..${previous.entry.endId}) overlaps content[${current.index}] (${current.entry.startId}..${current.entry.endId}). Overlapping ranges cannot be compressed in the same batch.`,
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (issues.length > 0) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
issues.length === 1 ? issues[0] : issues.map((issue) => `- ${issue}`).join("\n"),
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function parseBlockPlaceholders(summary: string): ParsedBlockPlaceholder[] {
|
|
104
|
+
const placeholders: ParsedBlockPlaceholder[] = []
|
|
105
|
+
const regex = new RegExp(BLOCK_PLACEHOLDER_REGEX)
|
|
106
|
+
|
|
107
|
+
let match: RegExpExecArray | null
|
|
108
|
+
while ((match = regex.exec(summary)) !== null) {
|
|
109
|
+
const full = match[0]
|
|
110
|
+
const blockIdPart = match[1] || match[2]
|
|
111
|
+
const parsed = Number.parseInt(blockIdPart, 10)
|
|
112
|
+
if (!Number.isInteger(parsed)) {
|
|
113
|
+
continue
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
placeholders.push({
|
|
117
|
+
raw: full,
|
|
118
|
+
blockId: parsed,
|
|
119
|
+
startIndex: match.index,
|
|
120
|
+
endIndex: match.index + full.length,
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return placeholders
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function validateSummaryPlaceholders(
|
|
128
|
+
placeholders: ParsedBlockPlaceholder[],
|
|
129
|
+
requiredBlockIds: number[],
|
|
130
|
+
startReference: BoundaryReference,
|
|
131
|
+
endReference: BoundaryReference,
|
|
132
|
+
summaryByBlockId: Map<number, CompressionBlock>,
|
|
133
|
+
): number[] {
|
|
134
|
+
const boundaryOptionalIds = new Set<number>()
|
|
135
|
+
if (startReference.kind === "compressed-block") {
|
|
136
|
+
if (startReference.blockId === undefined) {
|
|
137
|
+
throw new Error("Failed to map boundary matches back to raw messages")
|
|
138
|
+
}
|
|
139
|
+
boundaryOptionalIds.add(startReference.blockId)
|
|
140
|
+
}
|
|
141
|
+
if (endReference.kind === "compressed-block") {
|
|
142
|
+
if (endReference.blockId === undefined) {
|
|
143
|
+
throw new Error("Failed to map boundary matches back to raw messages")
|
|
144
|
+
}
|
|
145
|
+
boundaryOptionalIds.add(endReference.blockId)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const strictRequiredIds = requiredBlockIds.filter((id) => !boundaryOptionalIds.has(id))
|
|
149
|
+
const requiredSet = new Set(requiredBlockIds)
|
|
150
|
+
const keptPlaceholderIds = new Set<number>()
|
|
151
|
+
const validPlaceholders: ParsedBlockPlaceholder[] = []
|
|
152
|
+
|
|
153
|
+
for (const placeholder of placeholders) {
|
|
154
|
+
const isKnown = summaryByBlockId.has(placeholder.blockId)
|
|
155
|
+
const isRequired = requiredSet.has(placeholder.blockId)
|
|
156
|
+
const isDuplicate = keptPlaceholderIds.has(placeholder.blockId)
|
|
157
|
+
|
|
158
|
+
if (isKnown && isRequired && !isDuplicate) {
|
|
159
|
+
validPlaceholders.push(placeholder)
|
|
160
|
+
keptPlaceholderIds.add(placeholder.blockId)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
placeholders.length = 0
|
|
165
|
+
placeholders.push(...validPlaceholders)
|
|
166
|
+
|
|
167
|
+
return strictRequiredIds.filter((id) => !keptPlaceholderIds.has(id))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function injectBlockPlaceholders(
|
|
171
|
+
summary: string,
|
|
172
|
+
placeholders: ParsedBlockPlaceholder[],
|
|
173
|
+
summaryByBlockId: Map<number, CompressionBlock>,
|
|
174
|
+
startReference: BoundaryReference,
|
|
175
|
+
endReference: BoundaryReference,
|
|
176
|
+
): InjectedSummaryResult {
|
|
177
|
+
let cursor = 0
|
|
178
|
+
let expanded = summary
|
|
179
|
+
const consumed: number[] = []
|
|
180
|
+
const consumedSeen = new Set<number>()
|
|
181
|
+
|
|
182
|
+
if (placeholders.length > 0) {
|
|
183
|
+
expanded = ""
|
|
184
|
+
for (const placeholder of placeholders) {
|
|
185
|
+
const target = summaryByBlockId.get(placeholder.blockId)
|
|
186
|
+
if (!target) {
|
|
187
|
+
throw new Error(`Compressed block not found: (b${placeholder.blockId})`)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
expanded += summary.slice(cursor, placeholder.startIndex)
|
|
191
|
+
expanded += restoreSummary(target.summary)
|
|
192
|
+
cursor = placeholder.endIndex
|
|
193
|
+
|
|
194
|
+
if (!consumedSeen.has(placeholder.blockId)) {
|
|
195
|
+
consumedSeen.add(placeholder.blockId)
|
|
196
|
+
consumed.push(placeholder.blockId)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
expanded += summary.slice(cursor)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
expanded = injectBoundarySummary(
|
|
204
|
+
expanded,
|
|
205
|
+
startReference,
|
|
206
|
+
"start",
|
|
207
|
+
summaryByBlockId,
|
|
208
|
+
consumed,
|
|
209
|
+
consumedSeen,
|
|
210
|
+
)
|
|
211
|
+
expanded = injectBoundarySummary(
|
|
212
|
+
expanded,
|
|
213
|
+
endReference,
|
|
214
|
+
"end",
|
|
215
|
+
summaryByBlockId,
|
|
216
|
+
consumed,
|
|
217
|
+
consumedSeen,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
expandedSummary: expanded,
|
|
222
|
+
consumedBlockIds: consumed,
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function appendMissingBlockSummaries(
|
|
227
|
+
summary: string,
|
|
228
|
+
missingBlockIds: number[],
|
|
229
|
+
summaryByBlockId: Map<number, CompressionBlock>,
|
|
230
|
+
consumedBlockIds: number[],
|
|
231
|
+
): InjectedSummaryResult {
|
|
232
|
+
const consumedSeen = new Set<number>(consumedBlockIds)
|
|
233
|
+
const consumed = [...consumedBlockIds]
|
|
234
|
+
|
|
235
|
+
const missingSummaries: string[] = []
|
|
236
|
+
for (const blockId of missingBlockIds) {
|
|
237
|
+
if (consumedSeen.has(blockId)) {
|
|
238
|
+
continue
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const target = summaryByBlockId.get(blockId)
|
|
242
|
+
if (!target) {
|
|
243
|
+
throw new Error(`Compressed block not found: (b${blockId})`)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
missingSummaries.push(`\n### (b${blockId})\n${restoreSummary(target.summary)}`)
|
|
247
|
+
consumedSeen.add(blockId)
|
|
248
|
+
consumed.push(blockId)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (missingSummaries.length === 0) {
|
|
252
|
+
return {
|
|
253
|
+
expandedSummary: summary,
|
|
254
|
+
consumedBlockIds: consumed,
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const heading =
|
|
259
|
+
"\n\nThe following previously compressed summaries were also part of this conversation section:"
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
expandedSummary: summary + heading + missingSummaries.join(""),
|
|
263
|
+
consumedBlockIds: consumed,
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function restoreSummary(summary: string): string {
|
|
268
|
+
const headerMatch = summary.match(/^\s*\[Compressed conversation(?: section)?(?: b\d+)?\]/i)
|
|
269
|
+
if (!headerMatch) {
|
|
270
|
+
return summary
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const afterHeader = summary.slice(headerMatch[0].length)
|
|
274
|
+
const withoutLeadingBreaks = afterHeader.replace(/^(?:\r?\n)+/, "")
|
|
275
|
+
return withoutLeadingBreaks
|
|
276
|
+
.replace(/(?:\r?\n)*<dcp-message-id>b\d+<\/dcp-message-id>\s*$/i, "")
|
|
277
|
+
.replace(/(?:\r?\n)+$/, "")
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function injectBoundarySummary(
|
|
281
|
+
summary: string,
|
|
282
|
+
reference: BoundaryReference,
|
|
283
|
+
position: "start" | "end",
|
|
284
|
+
summaryByBlockId: Map<number, CompressionBlock>,
|
|
285
|
+
consumed: number[],
|
|
286
|
+
consumedSeen: Set<number>,
|
|
287
|
+
): string {
|
|
288
|
+
if (reference.kind !== "compressed-block" || reference.blockId === undefined) {
|
|
289
|
+
return summary
|
|
290
|
+
}
|
|
291
|
+
if (consumedSeen.has(reference.blockId)) {
|
|
292
|
+
return summary
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const target = summaryByBlockId.get(reference.blockId)
|
|
296
|
+
if (!target) {
|
|
297
|
+
throw new Error(`Compressed block not found: (b${reference.blockId})`)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const injectedBody = restoreSummary(target.summary)
|
|
301
|
+
const left = position === "start" ? injectedBody.trim() : summary.trim()
|
|
302
|
+
const right = position === "start" ? summary.trim() : injectedBody.trim()
|
|
303
|
+
const next = !left ? right : !right ? left : `${left}\n\n${right}`
|
|
304
|
+
|
|
305
|
+
consumedSeen.add(reference.blockId)
|
|
306
|
+
consumed.push(reference.blockId)
|
|
307
|
+
return next
|
|
308
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { tool } from "@opencode-ai/plugin"
|
|
2
|
+
import type { ToolContext } from "./types"
|
|
3
|
+
import { countTokens } from "../token-utils"
|
|
4
|
+
import { RANGE_FORMAT_EXTENSION } from "../prompts/extensions/tool"
|
|
5
|
+
import { finalizeSession, prepareSession, type NotificationEntry } from "./pipeline"
|
|
6
|
+
import { appendProtectedTools, appendProtectedUserMessages } from "./protected-content"
|
|
7
|
+
import {
|
|
8
|
+
appendMissingBlockSummaries,
|
|
9
|
+
injectBlockPlaceholders,
|
|
10
|
+
parseBlockPlaceholders,
|
|
11
|
+
resolveRanges,
|
|
12
|
+
validateArgs,
|
|
13
|
+
validateNonOverlapping,
|
|
14
|
+
validateSummaryPlaceholders,
|
|
15
|
+
} from "./range-utils"
|
|
16
|
+
import {
|
|
17
|
+
COMPRESSED_BLOCK_HEADER,
|
|
18
|
+
allocateBlockId,
|
|
19
|
+
allocateRunId,
|
|
20
|
+
applyCompressionState,
|
|
21
|
+
wrapCompressedSummary,
|
|
22
|
+
} from "./state"
|
|
23
|
+
import type { CompressRangeToolArgs } from "./types"
|
|
24
|
+
|
|
25
|
+
function buildSchema() {
|
|
26
|
+
return {
|
|
27
|
+
topic: tool.schema
|
|
28
|
+
.string()
|
|
29
|
+
.describe("Short label (3-5 words) for display - e.g., 'Auth System Exploration'"),
|
|
30
|
+
content: tool.schema
|
|
31
|
+
.array(
|
|
32
|
+
tool.schema.object({
|
|
33
|
+
startId: tool.schema
|
|
34
|
+
.string()
|
|
35
|
+
.describe(
|
|
36
|
+
"Message or block ID marking the beginning of range (e.g. m0001, b2)",
|
|
37
|
+
),
|
|
38
|
+
endId: tool.schema
|
|
39
|
+
.string()
|
|
40
|
+
.describe("Message or block ID marking the end of range (e.g. m0012, b5)"),
|
|
41
|
+
summary: tool.schema
|
|
42
|
+
.string()
|
|
43
|
+
.describe("Complete technical summary replacing all content in range"),
|
|
44
|
+
}),
|
|
45
|
+
)
|
|
46
|
+
.describe(
|
|
47
|
+
"One or more ranges to compress, each with start/end boundaries and a summary",
|
|
48
|
+
),
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createCompressRangeTool(ctx: ToolContext): ReturnType<typeof tool> {
|
|
53
|
+
ctx.prompts.reload()
|
|
54
|
+
const runtimePrompts = ctx.prompts.getRuntimePrompts()
|
|
55
|
+
|
|
56
|
+
return tool({
|
|
57
|
+
description: runtimePrompts.compressRange + RANGE_FORMAT_EXTENSION,
|
|
58
|
+
args: buildSchema(),
|
|
59
|
+
async execute(args, toolCtx) {
|
|
60
|
+
const input = args as CompressRangeToolArgs
|
|
61
|
+
validateArgs(input)
|
|
62
|
+
const callId =
|
|
63
|
+
typeof (toolCtx as unknown as { callID?: unknown }).callID === "string"
|
|
64
|
+
? (toolCtx as unknown as { callID: string }).callID
|
|
65
|
+
: undefined
|
|
66
|
+
|
|
67
|
+
const { rawMessages, searchContext } = await prepareSession(
|
|
68
|
+
ctx,
|
|
69
|
+
toolCtx,
|
|
70
|
+
`Compress Range: ${input.topic}`,
|
|
71
|
+
)
|
|
72
|
+
const resolvedPlans = resolveRanges(input, searchContext, ctx.state)
|
|
73
|
+
validateNonOverlapping(resolvedPlans)
|
|
74
|
+
|
|
75
|
+
const notifications: NotificationEntry[] = []
|
|
76
|
+
const preparedPlans: Array<{
|
|
77
|
+
entry: (typeof resolvedPlans)[number]["entry"]
|
|
78
|
+
selection: (typeof resolvedPlans)[number]["selection"]
|
|
79
|
+
anchorMessageId: string
|
|
80
|
+
finalSummary: string
|
|
81
|
+
consumedBlockIds: number[]
|
|
82
|
+
}> = []
|
|
83
|
+
let totalCompressedMessages = 0
|
|
84
|
+
|
|
85
|
+
for (const plan of resolvedPlans) {
|
|
86
|
+
const parsedPlaceholders = parseBlockPlaceholders(plan.entry.summary)
|
|
87
|
+
const missingBlockIds = validateSummaryPlaceholders(
|
|
88
|
+
parsedPlaceholders,
|
|
89
|
+
plan.selection.requiredBlockIds,
|
|
90
|
+
plan.selection.startReference,
|
|
91
|
+
plan.selection.endReference,
|
|
92
|
+
searchContext.summaryByBlockId,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
const injected = injectBlockPlaceholders(
|
|
96
|
+
plan.entry.summary,
|
|
97
|
+
parsedPlaceholders,
|
|
98
|
+
searchContext.summaryByBlockId,
|
|
99
|
+
plan.selection.startReference,
|
|
100
|
+
plan.selection.endReference,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
const summaryWithUsers = appendProtectedUserMessages(
|
|
104
|
+
injected.expandedSummary,
|
|
105
|
+
plan.selection,
|
|
106
|
+
searchContext,
|
|
107
|
+
ctx.state,
|
|
108
|
+
ctx.config.compress.protectUserMessages,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
const summaryWithTools = await appendProtectedTools(
|
|
112
|
+
ctx.client,
|
|
113
|
+
ctx.state,
|
|
114
|
+
ctx.config.experimental.allowSubAgents,
|
|
115
|
+
summaryWithUsers,
|
|
116
|
+
plan.selection,
|
|
117
|
+
searchContext,
|
|
118
|
+
ctx.config.compress.protectedTools,
|
|
119
|
+
ctx.config.protectedFilePatterns,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
const completedSummary = appendMissingBlockSummaries(
|
|
123
|
+
summaryWithTools,
|
|
124
|
+
missingBlockIds,
|
|
125
|
+
searchContext.summaryByBlockId,
|
|
126
|
+
injected.consumedBlockIds,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
preparedPlans.push({
|
|
130
|
+
entry: plan.entry,
|
|
131
|
+
selection: plan.selection,
|
|
132
|
+
anchorMessageId: plan.anchorMessageId,
|
|
133
|
+
finalSummary: completedSummary.expandedSummary,
|
|
134
|
+
consumedBlockIds: completedSummary.consumedBlockIds,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const runId = allocateRunId(ctx.state)
|
|
139
|
+
|
|
140
|
+
for (const preparedPlan of preparedPlans) {
|
|
141
|
+
const blockId = allocateBlockId(ctx.state)
|
|
142
|
+
const storedSummary = wrapCompressedSummary(blockId, preparedPlan.finalSummary)
|
|
143
|
+
const summaryTokens = countTokens(storedSummary)
|
|
144
|
+
|
|
145
|
+
const applied = applyCompressionState(
|
|
146
|
+
ctx.state,
|
|
147
|
+
{
|
|
148
|
+
topic: input.topic,
|
|
149
|
+
batchTopic: input.topic,
|
|
150
|
+
startId: preparedPlan.entry.startId,
|
|
151
|
+
endId: preparedPlan.entry.endId,
|
|
152
|
+
mode: "range",
|
|
153
|
+
runId,
|
|
154
|
+
compressMessageId: toolCtx.messageID,
|
|
155
|
+
compressCallId: callId,
|
|
156
|
+
summaryTokens,
|
|
157
|
+
},
|
|
158
|
+
preparedPlan.selection,
|
|
159
|
+
preparedPlan.anchorMessageId,
|
|
160
|
+
blockId,
|
|
161
|
+
storedSummary,
|
|
162
|
+
preparedPlan.consumedBlockIds,
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
totalCompressedMessages += applied.messageIds.length
|
|
166
|
+
|
|
167
|
+
notifications.push({
|
|
168
|
+
blockId,
|
|
169
|
+
runId,
|
|
170
|
+
summary: preparedPlan.finalSummary,
|
|
171
|
+
summaryTokens,
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
await finalizeSession(ctx, toolCtx, rawMessages, notifications, input.topic)
|
|
176
|
+
|
|
177
|
+
return `Compressed ${totalCompressedMessages} messages into ${COMPRESSED_BLOCK_HEADER}.`
|
|
178
|
+
},
|
|
179
|
+
})
|
|
180
|
+
}
|