@tarquinen/opencode-dcp 3.0.4 → 3.1.0

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.
Files changed (160) hide show
  1. package/README.md +22 -17
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +11 -9
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/commands/compression-targets.d.ts +13 -0
  6. package/dist/lib/commands/compression-targets.d.ts.map +1 -0
  7. package/dist/lib/commands/compression-targets.js +97 -0
  8. package/dist/lib/commands/compression-targets.js.map +1 -0
  9. package/dist/lib/commands/context.js +1 -1
  10. package/dist/lib/commands/context.js.map +1 -1
  11. package/dist/lib/commands/decompress.d.ts.map +1 -1
  12. package/dist/lib/commands/decompress.js +43 -28
  13. package/dist/lib/commands/decompress.js.map +1 -1
  14. package/dist/lib/commands/manual.d.ts.map +1 -1
  15. package/dist/lib/commands/manual.js +6 -6
  16. package/dist/lib/commands/manual.js.map +1 -1
  17. package/dist/lib/commands/recompress.d.ts.map +1 -1
  18. package/dist/lib/commands/recompress.js +34 -31
  19. package/dist/lib/commands/recompress.js.map +1 -1
  20. package/dist/lib/compress/index.d.ts +4 -0
  21. package/dist/lib/compress/index.d.ts.map +1 -0
  22. package/dist/lib/compress/index.js +3 -0
  23. package/dist/lib/compress/index.js.map +1 -0
  24. package/dist/lib/compress/message-utils.d.ts +8 -0
  25. package/dist/lib/compress/message-utils.d.ts.map +1 -0
  26. package/dist/lib/compress/message-utils.js +123 -0
  27. package/dist/lib/compress/message-utils.js.map +1 -0
  28. package/dist/lib/compress/message.d.ts +4 -0
  29. package/dist/lib/compress/message.d.ts.map +1 -0
  30. package/dist/lib/compress/message.js +78 -0
  31. package/dist/lib/compress/message.js.map +1 -0
  32. package/dist/lib/compress/pipeline.d.ts +29 -0
  33. package/dist/lib/compress/pipeline.d.ts.map +1 -0
  34. package/dist/lib/compress/pipeline.js +40 -0
  35. package/dist/lib/compress/pipeline.js.map +1 -0
  36. package/dist/lib/compress/protected-content.d.ts +5 -0
  37. package/dist/lib/compress/protected-content.d.ts.map +1 -0
  38. package/dist/lib/compress/protected-content.js +106 -0
  39. package/dist/lib/compress/protected-content.js.map +1 -0
  40. package/dist/lib/compress/range-utils.d.ts +10 -0
  41. package/dist/lib/compress/range-utils.d.ts.map +1 -0
  42. package/dist/lib/compress/range-utils.js +199 -0
  43. package/dist/lib/compress/range-utils.js.map +1 -0
  44. package/dist/lib/compress/range.d.ts +4 -0
  45. package/dist/lib/compress/range.d.ts.map +1 -0
  46. package/dist/lib/compress/range.js +86 -0
  47. package/dist/lib/compress/range.js.map +1 -0
  48. package/dist/lib/compress/search.d.ts +11 -0
  49. package/dist/lib/compress/search.d.ts.map +1 -0
  50. package/dist/lib/compress/search.js +204 -0
  51. package/dist/lib/compress/search.js.map +1 -0
  52. package/dist/lib/compress/state.d.ts +8 -0
  53. package/dist/lib/compress/state.d.ts.map +1 -0
  54. package/dist/lib/compress/state.js +197 -0
  55. package/dist/lib/compress/state.js.map +1 -0
  56. package/dist/lib/compress/types.d.ts +92 -0
  57. package/dist/lib/compress/types.d.ts.map +1 -0
  58. package/dist/lib/{tools → compress}/types.js.map +1 -1
  59. package/dist/lib/config.d.ts +5 -7
  60. package/dist/lib/config.d.ts.map +1 -1
  61. package/dist/lib/config.js +24 -30
  62. package/dist/lib/config.js.map +1 -1
  63. package/dist/lib/hooks.d.ts.map +1 -1
  64. package/dist/lib/hooks.js +6 -8
  65. package/dist/lib/hooks.js.map +1 -1
  66. package/dist/lib/message-ids.d.ts +1 -1
  67. package/dist/lib/message-ids.d.ts.map +1 -1
  68. package/dist/lib/message-ids.js +19 -3
  69. package/dist/lib/message-ids.js.map +1 -1
  70. package/dist/lib/messages/inject/inject.d.ts +3 -2
  71. package/dist/lib/messages/inject/inject.d.ts.map +1 -1
  72. package/dist/lib/messages/inject/inject.js +31 -10
  73. package/dist/lib/messages/inject/inject.js.map +1 -1
  74. package/dist/lib/messages/inject/utils.d.ts +2 -2
  75. package/dist/lib/messages/inject/utils.d.ts.map +1 -1
  76. package/dist/lib/messages/inject/utils.js +102 -42
  77. package/dist/lib/messages/inject/utils.js.map +1 -1
  78. package/dist/lib/messages/priority.d.ts +13 -0
  79. package/dist/lib/messages/priority.d.ts.map +1 -0
  80. package/dist/lib/messages/priority.js +65 -0
  81. package/dist/lib/messages/priority.js.map +1 -0
  82. package/dist/lib/messages/prune.js +6 -4
  83. package/dist/lib/messages/prune.js.map +1 -1
  84. package/dist/lib/messages/utils.d.ts +9 -2
  85. package/dist/lib/messages/utils.d.ts.map +1 -1
  86. package/dist/lib/messages/utils.js +41 -9
  87. package/dist/lib/messages/utils.js.map +1 -1
  88. package/dist/lib/prompts/compress-message.d.ts +2 -0
  89. package/dist/lib/prompts/compress-message.d.ts.map +1 -0
  90. package/dist/lib/prompts/compress-message.js +44 -0
  91. package/dist/lib/prompts/compress-message.js.map +1 -0
  92. package/dist/lib/prompts/compress-range.d.ts +2 -0
  93. package/dist/lib/prompts/compress-range.d.ts.map +1 -0
  94. package/dist/lib/prompts/{compress.js → compress-range.js} +5 -29
  95. package/dist/lib/prompts/compress-range.js.map +1 -0
  96. package/dist/lib/prompts/internal-overlays.d.ts +2 -2
  97. package/dist/lib/prompts/internal-overlays.d.ts.map +1 -1
  98. package/dist/lib/prompts/internal-overlays.js +17 -11
  99. package/dist/lib/prompts/internal-overlays.js.map +1 -1
  100. package/dist/lib/prompts/message-priority-guidance.d.ts +2 -0
  101. package/dist/lib/prompts/message-priority-guidance.d.ts.map +1 -0
  102. package/dist/lib/prompts/message-priority-guidance.js +9 -0
  103. package/dist/lib/prompts/message-priority-guidance.js.map +1 -0
  104. package/dist/lib/prompts/store.d.ts +3 -2
  105. package/dist/lib/prompts/store.d.ts.map +1 -1
  106. package/dist/lib/prompts/store.js +24 -12
  107. package/dist/lib/prompts/store.js.map +1 -1
  108. package/dist/lib/prompts/system.d.ts +1 -1
  109. package/dist/lib/prompts/system.d.ts.map +1 -1
  110. package/dist/lib/prompts/system.js +26 -9
  111. package/dist/lib/prompts/system.js.map +1 -1
  112. package/dist/lib/shared-utils.d.ts +1 -0
  113. package/dist/lib/shared-utils.d.ts.map +1 -1
  114. package/dist/lib/shared-utils.js +7 -0
  115. package/dist/lib/shared-utils.js.map +1 -1
  116. package/dist/lib/state/persistence.d.ts +1 -0
  117. package/dist/lib/state/persistence.d.ts.map +1 -1
  118. package/dist/lib/state/persistence.js +1 -0
  119. package/dist/lib/state/persistence.js.map +1 -1
  120. package/dist/lib/state/types.d.ts +6 -0
  121. package/dist/lib/state/types.d.ts.map +1 -1
  122. package/dist/lib/state/utils.d.ts +1 -0
  123. package/dist/lib/state/utils.d.ts.map +1 -1
  124. package/dist/lib/state/utils.js +31 -9
  125. package/dist/lib/state/utils.js.map +1 -1
  126. package/dist/lib/strategies/index.d.ts +0 -1
  127. package/dist/lib/strategies/index.d.ts.map +1 -1
  128. package/dist/lib/strategies/index.js +0 -1
  129. package/dist/lib/strategies/index.js.map +1 -1
  130. package/dist/lib/strategies/utils.d.ts +1 -5
  131. package/dist/lib/strategies/utils.d.ts.map +1 -1
  132. package/dist/lib/strategies/utils.js +24 -28
  133. package/dist/lib/strategies/utils.js.map +1 -1
  134. package/dist/lib/ui/notification.d.ts +8 -1
  135. package/dist/lib/ui/notification.d.ts.map +1 -1
  136. package/dist/lib/ui/notification.js +66 -13
  137. package/dist/lib/ui/notification.js.map +1 -1
  138. package/package.json +1 -1
  139. package/dist/lib/prompts/compress.d.ts +0 -2
  140. package/dist/lib/prompts/compress.d.ts.map +0 -1
  141. package/dist/lib/prompts/compress.js.map +0 -1
  142. package/dist/lib/strategies/supersede-writes.d.ts +0 -13
  143. package/dist/lib/strategies/supersede-writes.d.ts.map +0 -1
  144. package/dist/lib/strategies/supersede-writes.js +0 -94
  145. package/dist/lib/strategies/supersede-writes.js.map +0 -1
  146. package/dist/lib/tools/compress.d.ts +0 -4
  147. package/dist/lib/tools/compress.d.ts.map +0 -1
  148. package/dist/lib/tools/compress.js +0 -110
  149. package/dist/lib/tools/compress.js.map +0 -1
  150. package/dist/lib/tools/index.d.ts +0 -3
  151. package/dist/lib/tools/index.d.ts.map +0 -1
  152. package/dist/lib/tools/index.js +0 -2
  153. package/dist/lib/tools/index.js.map +0 -1
  154. package/dist/lib/tools/types.d.ts +0 -13
  155. package/dist/lib/tools/types.d.ts.map +0 -1
  156. package/dist/lib/tools/utils.d.ts +0 -80
  157. package/dist/lib/tools/utils.d.ts.map +0 -1
  158. package/dist/lib/tools/utils.js +0 -675
  159. package/dist/lib/tools/utils.js.map +0 -1
  160. /package/dist/lib/{tools → compress}/types.js +0 -0
@@ -1,675 +0,0 @@
1
- import { formatBlockRef, formatMessageIdTag, parseBoundaryId } from "../message-ids";
2
- import { isIgnoredUserMessage } from "../messages/utils";
3
- import { countAllMessageTokens } from "../strategies/utils";
4
- import { getFilePathsFromParameters, isFilePathProtected, isToolNameProtected, } from "../protected-patterns";
5
- import { buildSubagentResultText, getSubAgentId, mergeSubagentResult, } from "../subagents/subagent-results";
6
- const BLOCK_PLACEHOLDER_REGEX = /\(b(\d+)\)|\{block_(\d+)\}/gi;
7
- export function normalizeCompressArgs(args) {
8
- if ("content" in args && typeof args.content === "object" && args.content !== null) {
9
- return args;
10
- }
11
- return {
12
- topic: args.topic,
13
- content: {
14
- startId: args.startId,
15
- endId: args.endId,
16
- summary: args.summary,
17
- },
18
- };
19
- }
20
- export const COMPRESSED_BLOCK_HEADER = "[Compressed conversation section]";
21
- export function formatBlockPlaceholder(blockId) {
22
- return `(b${blockId})`;
23
- }
24
- export function validateCompressArgs(args) {
25
- if (typeof args.topic !== "string" || args.topic.trim().length === 0) {
26
- throw new Error("topic is required and must be a non-empty string");
27
- }
28
- if (typeof args.content?.startId !== "string" || args.content.startId.trim().length === 0) {
29
- throw new Error("content.startId is required and must be a non-empty string");
30
- }
31
- if (typeof args.content?.endId !== "string" || args.content.endId.trim().length === 0) {
32
- throw new Error("content.endId is required and must be a non-empty string");
33
- }
34
- if (typeof args.content?.summary !== "string" || args.content.summary.trim().length === 0) {
35
- throw new Error("content.summary is required and must be a non-empty string");
36
- }
37
- }
38
- export async function fetchSessionMessages(client, sessionId) {
39
- const response = await client.session.messages({
40
- path: { id: sessionId },
41
- });
42
- const payload = (response?.data || response);
43
- return Array.isArray(payload) ? payload : [];
44
- }
45
- export function buildSearchContext(state, rawMessages) {
46
- const rawMessagesById = new Map();
47
- const rawIndexById = new Map();
48
- for (const msg of rawMessages) {
49
- rawMessagesById.set(msg.info.id, msg);
50
- }
51
- for (let index = 0; index < rawMessages.length; index++) {
52
- const message = rawMessages[index];
53
- if (!message) {
54
- continue;
55
- }
56
- rawIndexById.set(message.info.id, index);
57
- }
58
- const summaryByBlockId = new Map();
59
- for (const [blockId, block] of state.prune.messages.blocksById) {
60
- if (!block.active) {
61
- continue;
62
- }
63
- summaryByBlockId.set(blockId, block);
64
- }
65
- return {
66
- rawMessages,
67
- rawMessagesById,
68
- rawIndexById,
69
- summaryByBlockId,
70
- };
71
- }
72
- export function resolveBoundaryIds(context, state, startId, endId) {
73
- const lookup = buildBoundaryReferenceLookup(context, state);
74
- const issues = [];
75
- const parsedStartId = parseBoundaryId(startId);
76
- const parsedEndId = parseBoundaryId(endId);
77
- if (parsedStartId === null) {
78
- issues.push("startId is invalid. Use an injected message ID (mNNNN) or block ID (bN).");
79
- }
80
- if (parsedEndId === null) {
81
- issues.push("endId is invalid. Use an injected message ID (mNNNN) or block ID (bN).");
82
- }
83
- if (issues.length > 0) {
84
- throwCombinedIssues(issues);
85
- }
86
- if (!parsedStartId || !parsedEndId) {
87
- throw new Error("Invalid boundary ID(s)");
88
- }
89
- const startReference = lookup.get(parsedStartId.ref);
90
- const endReference = lookup.get(parsedEndId.ref);
91
- if (!startReference) {
92
- issues.push(`startId ${parsedStartId.ref} is not available in the current conversation context. Choose an injected ID visible in context.`);
93
- }
94
- if (!endReference) {
95
- issues.push(`endId ${parsedEndId.ref} is not available in the current conversation context. Choose an injected ID visible in context.`);
96
- }
97
- if (issues.length > 0) {
98
- throwCombinedIssues(issues);
99
- }
100
- if (!startReference || !endReference) {
101
- throw new Error("Failed to resolve boundary IDs");
102
- }
103
- if (startReference.rawIndex > endReference.rawIndex) {
104
- throw new Error(`startId ${parsedStartId.ref} appears after endId ${parsedEndId.ref} in the conversation. Start must come before end.`);
105
- }
106
- return { startReference, endReference };
107
- }
108
- function buildBoundaryReferenceLookup(context, state) {
109
- const lookup = new Map();
110
- for (const [messageRef, messageId] of state.messageIds.byRef) {
111
- const rawMessage = context.rawMessagesById.get(messageId);
112
- if (!rawMessage) {
113
- continue;
114
- }
115
- if (rawMessage.info.role === "user" && isIgnoredUserMessage(rawMessage)) {
116
- continue;
117
- }
118
- const rawIndex = context.rawIndexById.get(messageId);
119
- if (rawIndex === undefined) {
120
- continue;
121
- }
122
- lookup.set(messageRef, {
123
- kind: "message",
124
- rawIndex,
125
- messageId,
126
- });
127
- }
128
- const summaries = Array.from(context.summaryByBlockId.values()).sort((a, b) => a.blockId - b.blockId);
129
- for (const summary of summaries) {
130
- const anchorMessage = context.rawMessagesById.get(summary.anchorMessageId);
131
- if (!anchorMessage) {
132
- continue;
133
- }
134
- if (anchorMessage.info.role === "user" && isIgnoredUserMessage(anchorMessage)) {
135
- continue;
136
- }
137
- const rawIndex = context.rawIndexById.get(summary.anchorMessageId);
138
- if (rawIndex === undefined) {
139
- continue;
140
- }
141
- const blockRef = formatBlockRef(summary.blockId);
142
- if (!lookup.has(blockRef)) {
143
- lookup.set(blockRef, {
144
- kind: "compressed-block",
145
- rawIndex,
146
- blockId: summary.blockId,
147
- anchorMessageId: summary.anchorMessageId,
148
- });
149
- }
150
- }
151
- return lookup;
152
- }
153
- export function resolveRange(context, startReference, endReference) {
154
- const startRawIndex = startReference.rawIndex;
155
- const endRawIndex = endReference.rawIndex;
156
- const messageIds = [];
157
- const messageSeen = new Set();
158
- const toolIds = [];
159
- const toolSeen = new Set();
160
- const requiredBlockIds = [];
161
- const requiredBlockSeen = new Set();
162
- const messageTokenById = new Map();
163
- for (let index = startRawIndex; index <= endRawIndex; index++) {
164
- const rawMessage = context.rawMessages[index];
165
- if (!rawMessage) {
166
- continue;
167
- }
168
- if (rawMessage.info.role === "user" && isIgnoredUserMessage(rawMessage)) {
169
- continue;
170
- }
171
- const messageId = rawMessage.info.id;
172
- if (!messageSeen.has(messageId)) {
173
- messageSeen.add(messageId);
174
- messageIds.push(messageId);
175
- }
176
- if (!messageTokenById.has(messageId)) {
177
- messageTokenById.set(messageId, countAllMessageTokens(rawMessage));
178
- }
179
- const parts = Array.isArray(rawMessage.parts) ? rawMessage.parts : [];
180
- for (const part of parts) {
181
- if (part.type !== "tool" || !part.callID) {
182
- continue;
183
- }
184
- if (toolSeen.has(part.callID)) {
185
- continue;
186
- }
187
- toolSeen.add(part.callID);
188
- toolIds.push(part.callID);
189
- }
190
- }
191
- const rangeMessageIdSet = new Set(messageIds);
192
- const summariesInRange = [];
193
- for (const summary of context.summaryByBlockId.values()) {
194
- if (!rangeMessageIdSet.has(summary.anchorMessageId)) {
195
- continue;
196
- }
197
- const anchorIndex = context.rawIndexById.get(summary.anchorMessageId);
198
- if (anchorIndex === undefined) {
199
- continue;
200
- }
201
- summariesInRange.push({
202
- blockId: summary.blockId,
203
- rawIndex: anchorIndex,
204
- });
205
- }
206
- summariesInRange.sort((a, b) => a.rawIndex - b.rawIndex || a.blockId - b.blockId);
207
- for (const summary of summariesInRange) {
208
- if (requiredBlockSeen.has(summary.blockId)) {
209
- continue;
210
- }
211
- requiredBlockSeen.add(summary.blockId);
212
- requiredBlockIds.push(summary.blockId);
213
- }
214
- if (messageIds.length === 0) {
215
- throw new Error("Failed to map boundary matches back to raw messages. Choose boundaries that include original conversation messages.");
216
- }
217
- return {
218
- startReference,
219
- endReference,
220
- messageIds,
221
- messageTokenById,
222
- toolIds,
223
- requiredBlockIds,
224
- };
225
- }
226
- export function resolveAnchorMessageId(startReference) {
227
- if (startReference.kind === "compressed-block") {
228
- if (!startReference.anchorMessageId) {
229
- throw new Error("Failed to map boundary matches back to raw messages");
230
- }
231
- return startReference.anchorMessageId;
232
- }
233
- if (!startReference.messageId) {
234
- throw new Error("Failed to map boundary matches back to raw messages");
235
- }
236
- return startReference.messageId;
237
- }
238
- export function parseBlockPlaceholders(summary) {
239
- const placeholders = [];
240
- const regex = new RegExp(BLOCK_PLACEHOLDER_REGEX);
241
- let match;
242
- while ((match = regex.exec(summary)) !== null) {
243
- const full = match[0];
244
- const blockIdPart = match[1] || match[2];
245
- const parsed = Number.parseInt(blockIdPart, 10);
246
- if (!Number.isInteger(parsed)) {
247
- continue;
248
- }
249
- placeholders.push({
250
- raw: full,
251
- blockId: parsed,
252
- startIndex: match.index,
253
- endIndex: match.index + full.length,
254
- });
255
- }
256
- return placeholders;
257
- }
258
- export function validateSummaryPlaceholders(placeholders, requiredBlockIds, startReference, endReference, summaryByBlockId) {
259
- const boundaryOptionalIds = new Set();
260
- if (startReference.kind === "compressed-block") {
261
- if (startReference.blockId === undefined) {
262
- throw new Error("Failed to map boundary matches back to raw messages");
263
- }
264
- boundaryOptionalIds.add(startReference.blockId);
265
- }
266
- if (endReference.kind === "compressed-block") {
267
- if (endReference.blockId === undefined) {
268
- throw new Error("Failed to map boundary matches back to raw messages");
269
- }
270
- boundaryOptionalIds.add(endReference.blockId);
271
- }
272
- const strictRequiredIds = requiredBlockIds.filter((id) => !boundaryOptionalIds.has(id));
273
- const requiredSet = new Set(requiredBlockIds);
274
- const keptPlaceholderIds = new Set();
275
- const validPlaceholders = [];
276
- for (const placeholder of placeholders) {
277
- const isKnown = summaryByBlockId.has(placeholder.blockId);
278
- const isRequired = requiredSet.has(placeholder.blockId);
279
- const isDuplicate = keptPlaceholderIds.has(placeholder.blockId);
280
- if (isKnown && isRequired && !isDuplicate) {
281
- validPlaceholders.push(placeholder);
282
- keptPlaceholderIds.add(placeholder.blockId);
283
- }
284
- }
285
- placeholders.length = 0;
286
- placeholders.push(...validPlaceholders);
287
- return strictRequiredIds.filter((id) => !keptPlaceholderIds.has(id));
288
- }
289
- export function injectBlockPlaceholders(summary, placeholders, summaryByBlockId, startReference, endReference) {
290
- let cursor = 0;
291
- let expanded = summary;
292
- const consumed = [];
293
- const consumedSeen = new Set();
294
- if (placeholders.length > 0) {
295
- expanded = "";
296
- for (const placeholder of placeholders) {
297
- const target = summaryByBlockId.get(placeholder.blockId);
298
- if (!target) {
299
- throw new Error(`Compressed block not found: ${formatBlockPlaceholder(placeholder.blockId)}`);
300
- }
301
- expanded += summary.slice(cursor, placeholder.startIndex);
302
- expanded += restoreSummary(target.summary);
303
- cursor = placeholder.endIndex;
304
- if (!consumedSeen.has(placeholder.blockId)) {
305
- consumedSeen.add(placeholder.blockId);
306
- consumed.push(placeholder.blockId);
307
- }
308
- }
309
- expanded += summary.slice(cursor);
310
- }
311
- expanded = injectBoundarySummaryIfMissing(expanded, startReference, "start", summaryByBlockId, consumed, consumedSeen);
312
- expanded = injectBoundarySummaryIfMissing(expanded, endReference, "end", summaryByBlockId, consumed, consumedSeen);
313
- return {
314
- expandedSummary: expanded,
315
- consumedBlockIds: consumed,
316
- };
317
- }
318
- export function allocateBlockId(state) {
319
- const next = state.prune.messages.nextBlockId;
320
- if (!Number.isInteger(next) || next < 1) {
321
- state.prune.messages.nextBlockId = 2;
322
- return 1;
323
- }
324
- state.prune.messages.nextBlockId = next + 1;
325
- return next;
326
- }
327
- export function wrapCompressedSummary(blockId, summary) {
328
- const header = COMPRESSED_BLOCK_HEADER;
329
- const footer = formatMessageIdTag(formatBlockRef(blockId));
330
- const body = summary.trim();
331
- if (body.length === 0) {
332
- return `${header}\n${footer}`;
333
- }
334
- return `${header}\n${body}\n\n${footer}`;
335
- }
336
- export function applyCompressionState(state, input, range, anchorMessageId, blockId, summary, consumedBlockIds) {
337
- const messagesState = state.prune.messages;
338
- const consumed = [...new Set(consumedBlockIds.filter((id) => Number.isInteger(id) && id > 0))];
339
- const included = [...consumed];
340
- const effectiveMessageIds = new Set(range.messageIds);
341
- const effectiveToolIds = new Set(range.toolIds);
342
- for (const consumedBlockId of consumed) {
343
- const consumedBlock = messagesState.blocksById.get(consumedBlockId);
344
- if (!consumedBlock) {
345
- continue;
346
- }
347
- for (const messageId of consumedBlock.effectiveMessageIds) {
348
- effectiveMessageIds.add(messageId);
349
- }
350
- for (const toolId of consumedBlock.effectiveToolIds) {
351
- effectiveToolIds.add(toolId);
352
- }
353
- }
354
- const initiallyActiveMessages = new Set();
355
- for (const messageId of effectiveMessageIds) {
356
- const entry = messagesState.byMessageId.get(messageId);
357
- if (entry && entry.activeBlockIds.length > 0) {
358
- initiallyActiveMessages.add(messageId);
359
- }
360
- }
361
- const initiallyActiveToolIds = new Set();
362
- for (const activeBlockId of messagesState.activeBlockIds) {
363
- const activeBlock = messagesState.blocksById.get(activeBlockId);
364
- if (!activeBlock || !activeBlock.active) {
365
- continue;
366
- }
367
- for (const toolId of activeBlock.effectiveToolIds) {
368
- initiallyActiveToolIds.add(toolId);
369
- }
370
- }
371
- const createdAt = Date.now();
372
- const block = {
373
- blockId,
374
- active: true,
375
- deactivatedByUser: false,
376
- compressedTokens: 0,
377
- topic: input.topic,
378
- startId: input.startId,
379
- endId: input.endId,
380
- anchorMessageId,
381
- compressMessageId: input.compressMessageId,
382
- includedBlockIds: included,
383
- consumedBlockIds: consumed,
384
- parentBlockIds: [],
385
- directMessageIds: [],
386
- directToolIds: [],
387
- effectiveMessageIds: [...effectiveMessageIds],
388
- effectiveToolIds: [...effectiveToolIds],
389
- createdAt,
390
- summary,
391
- };
392
- messagesState.blocksById.set(blockId, block);
393
- messagesState.activeBlockIds.add(blockId);
394
- messagesState.activeByAnchorMessageId.set(anchorMessageId, blockId);
395
- const deactivatedAt = Date.now();
396
- for (const consumedBlockId of consumed) {
397
- const consumedBlock = messagesState.blocksById.get(consumedBlockId);
398
- if (!consumedBlock || !consumedBlock.active) {
399
- continue;
400
- }
401
- consumedBlock.active = false;
402
- consumedBlock.deactivatedAt = deactivatedAt;
403
- consumedBlock.deactivatedByBlockId = blockId;
404
- if (!consumedBlock.parentBlockIds.includes(blockId)) {
405
- consumedBlock.parentBlockIds.push(blockId);
406
- }
407
- messagesState.activeBlockIds.delete(consumedBlockId);
408
- const mappedBlockId = messagesState.activeByAnchorMessageId.get(consumedBlock.anchorMessageId);
409
- if (mappedBlockId === consumedBlockId) {
410
- messagesState.activeByAnchorMessageId.delete(consumedBlock.anchorMessageId);
411
- }
412
- }
413
- const removeActiveBlockId = (entry, blockIdToRemove) => {
414
- if (entry.activeBlockIds.length === 0) {
415
- return;
416
- }
417
- entry.activeBlockIds = entry.activeBlockIds.filter((id) => id !== blockIdToRemove);
418
- };
419
- for (const consumedBlockId of consumed) {
420
- const consumedBlock = messagesState.blocksById.get(consumedBlockId);
421
- if (!consumedBlock) {
422
- continue;
423
- }
424
- for (const messageId of consumedBlock.effectiveMessageIds) {
425
- const entry = messagesState.byMessageId.get(messageId);
426
- if (!entry) {
427
- continue;
428
- }
429
- removeActiveBlockId(entry, consumedBlockId);
430
- }
431
- }
432
- for (const messageId of range.messageIds) {
433
- const tokenCount = range.messageTokenById.get(messageId) || 0;
434
- const existing = messagesState.byMessageId.get(messageId);
435
- if (!existing) {
436
- messagesState.byMessageId.set(messageId, {
437
- tokenCount,
438
- allBlockIds: [blockId],
439
- activeBlockIds: [blockId],
440
- });
441
- continue;
442
- }
443
- existing.tokenCount = Math.max(existing.tokenCount, tokenCount);
444
- if (!existing.allBlockIds.includes(blockId)) {
445
- existing.allBlockIds.push(blockId);
446
- }
447
- if (!existing.activeBlockIds.includes(blockId)) {
448
- existing.activeBlockIds.push(blockId);
449
- }
450
- }
451
- for (const messageId of block.effectiveMessageIds) {
452
- if (range.messageTokenById.has(messageId)) {
453
- continue;
454
- }
455
- const existing = messagesState.byMessageId.get(messageId);
456
- if (!existing) {
457
- continue;
458
- }
459
- if (!existing.allBlockIds.includes(blockId)) {
460
- existing.allBlockIds.push(blockId);
461
- }
462
- if (!existing.activeBlockIds.includes(blockId)) {
463
- existing.activeBlockIds.push(blockId);
464
- }
465
- }
466
- let compressedTokens = 0;
467
- const newlyCompressedMessageIds = [];
468
- for (const messageId of effectiveMessageIds) {
469
- const entry = messagesState.byMessageId.get(messageId);
470
- if (!entry) {
471
- continue;
472
- }
473
- const isNowActive = entry.activeBlockIds.length > 0;
474
- const wasActive = initiallyActiveMessages.has(messageId);
475
- if (isNowActive && !wasActive) {
476
- compressedTokens += entry.tokenCount;
477
- newlyCompressedMessageIds.push(messageId);
478
- }
479
- }
480
- const newlyCompressedToolIds = [];
481
- for (const toolId of effectiveToolIds) {
482
- if (!initiallyActiveToolIds.has(toolId)) {
483
- newlyCompressedToolIds.push(toolId);
484
- }
485
- }
486
- block.directMessageIds = [...newlyCompressedMessageIds];
487
- block.directToolIds = [...newlyCompressedToolIds];
488
- block.compressedTokens = compressedTokens;
489
- state.stats.pruneTokenCounter += compressedTokens;
490
- state.stats.totalPruneTokens += state.stats.pruneTokenCounter;
491
- state.stats.pruneTokenCounter = 0;
492
- return {
493
- compressedTokens,
494
- messageIds: range.messageIds,
495
- newlyCompressedMessageIds,
496
- newlyCompressedToolIds,
497
- };
498
- }
499
- function restoreSummary(summary) {
500
- const headerMatch = summary.match(/^\s*\[Compressed conversation(?: section)?(?: b\d+)?\]/i);
501
- if (!headerMatch) {
502
- return summary;
503
- }
504
- const afterHeader = summary.slice(headerMatch[0].length);
505
- const withoutLeadingBreaks = afterHeader.replace(/^(?:\r?\n)+/, "");
506
- return withoutLeadingBreaks
507
- .replace(/(?:\r?\n)*<dcp-message-id>b\d+<\/dcp-message-id>\s*$/i, "")
508
- .replace(/(?:\r?\n)+$/, "");
509
- }
510
- function injectBoundarySummaryIfMissing(summary, reference, position, summaryByBlockId, consumed, consumedSeen) {
511
- if (reference.kind !== "compressed-block" || reference.blockId === undefined) {
512
- return summary;
513
- }
514
- if (consumedSeen.has(reference.blockId)) {
515
- return summary;
516
- }
517
- const target = summaryByBlockId.get(reference.blockId);
518
- if (!target) {
519
- throw new Error(`Compressed block not found: ${formatBlockPlaceholder(reference.blockId)}`);
520
- }
521
- const injectedBody = restoreSummary(target.summary);
522
- const next = position === "start"
523
- ? mergeWithSpacing(injectedBody, summary)
524
- : mergeWithSpacing(summary, injectedBody);
525
- consumedSeen.add(reference.blockId);
526
- consumed.push(reference.blockId);
527
- return next;
528
- }
529
- function mergeWithSpacing(left, right) {
530
- const l = left.trim();
531
- const r = right.trim();
532
- if (!l) {
533
- return right;
534
- }
535
- if (!r) {
536
- return left;
537
- }
538
- return `${l}\n\n${r}`;
539
- }
540
- export function appendProtectedUserMessages(summary, range, searchContext, state, enabled) {
541
- if (!enabled)
542
- return summary;
543
- const userTexts = [];
544
- for (const messageId of range.messageIds) {
545
- const existingCompressionEntry = state.prune.messages.byMessageId.get(messageId);
546
- if (existingCompressionEntry && existingCompressionEntry.activeBlockIds.length > 0) {
547
- continue;
548
- }
549
- const message = searchContext.rawMessagesById.get(messageId);
550
- if (!message)
551
- continue;
552
- if (message.info.role !== "user")
553
- continue;
554
- if (isIgnoredUserMessage(message))
555
- continue;
556
- const parts = Array.isArray(message.parts) ? message.parts : [];
557
- for (const part of parts) {
558
- if (part.type === "text" && typeof part.text === "string" && part.text.trim()) {
559
- userTexts.push(part.text);
560
- break;
561
- }
562
- }
563
- }
564
- if (userTexts.length === 0) {
565
- return summary;
566
- }
567
- const heading = "\n\nThe following user messages were sent in this conversation verbatim:";
568
- const body = userTexts.map((text) => `\n${text}`).join("");
569
- return summary + heading + body;
570
- }
571
- export async function appendProtectedTools(client, state, allowSubAgents, summary, range, searchContext, protectedTools, protectedFilePatterns = []) {
572
- const protectedOutputs = [];
573
- for (const messageId of range.messageIds) {
574
- const existingCompressionEntry = state.prune.messages.byMessageId.get(messageId);
575
- if (existingCompressionEntry && existingCompressionEntry.activeBlockIds.length > 0) {
576
- continue;
577
- }
578
- const message = searchContext.rawMessagesById.get(messageId);
579
- if (!message)
580
- continue;
581
- const parts = Array.isArray(message.parts) ? message.parts : [];
582
- for (const part of parts) {
583
- if (part.type === "tool" && part.callID) {
584
- let isToolProtected = isToolNameProtected(part.tool, protectedTools);
585
- if (!isToolProtected && protectedFilePatterns.length > 0) {
586
- const filePaths = getFilePathsFromParameters(part.tool, part.state?.input);
587
- if (isFilePathProtected(filePaths, protectedFilePatterns)) {
588
- isToolProtected = true;
589
- }
590
- }
591
- if (isToolProtected) {
592
- const title = `Tool: ${part.tool}`;
593
- let output = "";
594
- if (part.state?.status === "completed" && part.state?.output) {
595
- output =
596
- typeof part.state.output === "string"
597
- ? part.state.output
598
- : JSON.stringify(part.state.output);
599
- }
600
- if (allowSubAgents &&
601
- part.tool === "task" &&
602
- part.state?.status === "completed" &&
603
- typeof part.state?.output === "string") {
604
- const cachedSubAgentResult = state.subAgentResultCache.get(part.callID);
605
- if (cachedSubAgentResult !== undefined) {
606
- if (cachedSubAgentResult) {
607
- output = mergeSubagentResult(part.state.output, cachedSubAgentResult);
608
- }
609
- }
610
- else {
611
- const subAgentSessionId = getSubAgentId(part);
612
- if (subAgentSessionId) {
613
- let subAgentResultText = "";
614
- try {
615
- const subAgentMessages = await fetchSessionMessages(client, subAgentSessionId);
616
- subAgentResultText = buildSubagentResultText(subAgentMessages);
617
- }
618
- catch {
619
- subAgentResultText = "";
620
- }
621
- if (subAgentResultText) {
622
- state.subAgentResultCache.set(part.callID, subAgentResultText);
623
- output = mergeSubagentResult(part.state.output, subAgentResultText);
624
- }
625
- }
626
- }
627
- }
628
- if (output) {
629
- protectedOutputs.push(`\n### ${title}\n${output}`);
630
- }
631
- }
632
- }
633
- }
634
- }
635
- if (protectedOutputs.length === 0) {
636
- return summary;
637
- }
638
- const heading = "\n\nThe following protected tools were used in this conversation as well:";
639
- return summary + heading + protectedOutputs.join("");
640
- }
641
- export function appendMissingBlockSummaries(summary, missingBlockIds, summaryByBlockId, consumedBlockIds) {
642
- const consumedSeen = new Set(consumedBlockIds);
643
- const consumed = [...consumedBlockIds];
644
- const missingSummaries = [];
645
- for (const blockId of missingBlockIds) {
646
- if (consumedSeen.has(blockId)) {
647
- continue;
648
- }
649
- const target = summaryByBlockId.get(blockId);
650
- if (!target) {
651
- throw new Error(`Compressed block not found: ${formatBlockPlaceholder(blockId)}`);
652
- }
653
- missingSummaries.push(`\n### ${formatBlockPlaceholder(blockId)}\n${restoreSummary(target.summary)}`);
654
- consumedSeen.add(blockId);
655
- consumed.push(blockId);
656
- }
657
- if (missingSummaries.length === 0) {
658
- return {
659
- expandedSummary: summary,
660
- consumedBlockIds: consumed,
661
- };
662
- }
663
- const heading = "\n\nThe following previously compressed summaries were also part of this conversation section:";
664
- return {
665
- expandedSummary: summary + heading + missingSummaries.join(""),
666
- consumedBlockIds: consumed,
667
- };
668
- }
669
- function throwCombinedIssues(issues) {
670
- if (issues.length === 1) {
671
- throw new Error(issues[0]);
672
- }
673
- throw new Error(issues.map((issue) => `- ${issue}`).join("\n"));
674
- }
675
- //# sourceMappingURL=utils.js.map