@eminent337/aery-core 0.67.120 → 0.67.121

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 (127) hide show
  1. package/README.md +14 -0
  2. package/dist/agent-loop.d.ts.map +1 -1
  3. package/dist/agent-loop.js +31 -1
  4. package/dist/agent-loop.js.map +1 -1
  5. package/dist/agent.d.ts +4 -3
  6. package/dist/agent.d.ts.map +1 -1
  7. package/dist/agent.js +7 -3
  8. package/dist/agent.js.map +1 -1
  9. package/dist/harness/agent-harness.d.ts +103 -0
  10. package/dist/harness/agent-harness.d.ts.map +1 -0
  11. package/dist/harness/agent-harness.js +788 -0
  12. package/dist/harness/agent-harness.js.map +1 -0
  13. package/dist/harness/compaction/branch-summarization.d.ts +88 -0
  14. package/dist/harness/compaction/branch-summarization.d.ts.map +1 -0
  15. package/dist/harness/compaction/branch-summarization.js +243 -0
  16. package/dist/harness/compaction/branch-summarization.js.map +1 -0
  17. package/dist/harness/compaction/compaction.d.ts +122 -0
  18. package/dist/harness/compaction/compaction.d.ts.map +1 -0
  19. package/dist/harness/compaction/compaction.js +631 -0
  20. package/dist/harness/compaction/compaction.js.map +1 -0
  21. package/dist/harness/compaction/utils.d.ts +38 -0
  22. package/dist/harness/compaction/utils.d.ts.map +1 -0
  23. package/dist/harness/compaction/utils.js +153 -0
  24. package/dist/harness/compaction/utils.js.map +1 -0
  25. package/dist/harness/env/nodejs.d.ts +50 -0
  26. package/dist/harness/env/nodejs.d.ts.map +1 -0
  27. package/dist/harness/env/nodejs.js +487 -0
  28. package/dist/harness/env/nodejs.js.map +1 -0
  29. package/dist/harness/execution-env.d.ts +4 -0
  30. package/dist/harness/execution-env.d.ts.map +1 -0
  31. package/dist/harness/execution-env.js +3 -0
  32. package/dist/harness/execution-env.js.map +1 -0
  33. package/dist/harness/factory.d.ts +6 -0
  34. package/dist/harness/factory.d.ts.map +1 -0
  35. package/dist/harness/factory.js +9 -0
  36. package/dist/harness/factory.js.map +1 -0
  37. package/dist/harness/messages.d.ts +51 -0
  38. package/dist/harness/messages.d.ts.map +1 -0
  39. package/dist/harness/messages.js +102 -0
  40. package/dist/harness/messages.js.map +1 -0
  41. package/dist/harness/prompt-templates.d.ts +47 -0
  42. package/dist/harness/prompt-templates.d.ts.map +1 -0
  43. package/dist/harness/prompt-templates.js +201 -0
  44. package/dist/harness/prompt-templates.js.map +1 -0
  45. package/dist/harness/session/jsonl-repo.d.ts +26 -0
  46. package/dist/harness/session/jsonl-repo.d.ts.map +1 -0
  47. package/dist/harness/session/jsonl-repo.js +97 -0
  48. package/dist/harness/session/jsonl-repo.js.map +1 -0
  49. package/dist/harness/session/jsonl-storage.d.ts +33 -0
  50. package/dist/harness/session/jsonl-storage.d.ts.map +1 -0
  51. package/dist/harness/session/jsonl-storage.js +159 -0
  52. package/dist/harness/session/jsonl-storage.js.map +1 -0
  53. package/dist/harness/session/memory-repo.d.ts +18 -0
  54. package/dist/harness/session/memory-repo.d.ts.map +1 -0
  55. package/dist/harness/session/memory-repo.js +42 -0
  56. package/dist/harness/session/memory-repo.js.map +1 -0
  57. package/dist/harness/session/memory-storage.d.ts +26 -0
  58. package/dist/harness/session/memory-storage.d.ts.map +1 -0
  59. package/dist/harness/session/memory-storage.js +89 -0
  60. package/dist/harness/session/memory-storage.js.map +1 -0
  61. package/dist/harness/session/repo/jsonl.d.ts +20 -0
  62. package/dist/harness/session/repo/jsonl.d.ts.map +1 -0
  63. package/dist/harness/session/repo/jsonl.js +92 -0
  64. package/dist/harness/session/repo/jsonl.js.map +1 -0
  65. package/dist/harness/session/repo/memory.d.ts +18 -0
  66. package/dist/harness/session/repo/memory.d.ts.map +1 -0
  67. package/dist/harness/session/repo/memory.js +42 -0
  68. package/dist/harness/session/repo/memory.js.map +1 -0
  69. package/dist/harness/session/repo/shared.d.ts +10 -0
  70. package/dist/harness/session/repo/shared.d.ts.map +1 -0
  71. package/dist/harness/session/repo/shared.js +31 -0
  72. package/dist/harness/session/repo/shared.js.map +1 -0
  73. package/dist/harness/session/repo-utils.d.ts +10 -0
  74. package/dist/harness/session/repo-utils.d.ts.map +1 -0
  75. package/dist/harness/session/repo-utils.js +31 -0
  76. package/dist/harness/session/repo-utils.js.map +1 -0
  77. package/dist/harness/session/session.d.ts +32 -0
  78. package/dist/harness/session/session.d.ts.map +1 -0
  79. package/dist/harness/session/session.js +196 -0
  80. package/dist/harness/session/session.js.map +1 -0
  81. package/dist/harness/session/storage/jsonl.d.ts +30 -0
  82. package/dist/harness/session/storage/jsonl.d.ts.map +1 -0
  83. package/dist/harness/session/storage/jsonl.js +170 -0
  84. package/dist/harness/session/storage/jsonl.js.map +1 -0
  85. package/dist/harness/session/storage/memory.d.ts +26 -0
  86. package/dist/harness/session/storage/memory.d.ts.map +1 -0
  87. package/dist/harness/session/storage/memory.js +90 -0
  88. package/dist/harness/session/storage/memory.js.map +1 -0
  89. package/dist/harness/session/uuid.d.ts +2 -0
  90. package/dist/harness/session/uuid.d.ts.map +1 -0
  91. package/dist/harness/session/uuid.js +50 -0
  92. package/dist/harness/session/uuid.js.map +1 -0
  93. package/dist/harness/skills.d.ts +43 -0
  94. package/dist/harness/skills.d.ts.map +1 -0
  95. package/dist/harness/skills.js +255 -0
  96. package/dist/harness/skills.js.map +1 -0
  97. package/dist/harness/system-prompt.d.ts +3 -0
  98. package/dist/harness/system-prompt.d.ts.map +1 -0
  99. package/dist/harness/system-prompt.js +30 -0
  100. package/dist/harness/system-prompt.js.map +1 -0
  101. package/dist/harness/types.d.ts +578 -0
  102. package/dist/harness/types.d.ts.map +1 -0
  103. package/dist/harness/types.js +56 -0
  104. package/dist/harness/types.js.map +1 -0
  105. package/dist/harness/utils/shell-output.d.ts +14 -0
  106. package/dist/harness/utils/shell-output.d.ts.map +1 -0
  107. package/dist/harness/utils/shell-output.js +125 -0
  108. package/dist/harness/utils/shell-output.js.map +1 -0
  109. package/dist/harness/utils/truncate.d.ts +70 -0
  110. package/dist/harness/utils/truncate.d.ts.map +1 -0
  111. package/dist/harness/utils/truncate.js +288 -0
  112. package/dist/harness/utils/truncate.js.map +1 -0
  113. package/dist/index.d.ts +15 -0
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.js +16 -0
  116. package/dist/index.js.map +1 -1
  117. package/dist/node.d.ts +3 -0
  118. package/dist/node.d.ts.map +1 -0
  119. package/dist/node.js +3 -0
  120. package/dist/node.js.map +1 -0
  121. package/dist/proxy.d.ts.map +1 -1
  122. package/dist/proxy.js +5 -2
  123. package/dist/proxy.js.map +1 -1
  124. package/dist/types.d.ts +50 -4
  125. package/dist/types.d.ts.map +1 -1
  126. package/dist/types.js.map +1 -1
  127. package/package.json +19 -2
@@ -0,0 +1,631 @@
1
+ /**
2
+ * Context compaction for long sessions.
3
+ *
4
+ * Pure functions for compaction logic. The session manager handles I/O,
5
+ * and after compaction the session is reloaded.
6
+ */
7
+ import { completeSimple } from "@eminent337/aery-ai";
8
+ import { convertToLlm, createBranchSummaryMessage, createCompactionSummaryMessage, createCustomMessage, } from "../messages.js";
9
+ import { buildSessionContext } from "../session/session.js";
10
+ import { CompactionError, err, ok } from "../types.js";
11
+ import { computeFileLists, createFileOps, extractFileOpsFromMessage, formatFileOperations, SUMMARIZATION_SYSTEM_PROMPT, serializeConversation, } from "./utils.js";
12
+ /**
13
+ * Extract file operations from messages and previous compaction entries.
14
+ */
15
+ function extractFileOperations(messages, entries, prevCompactionIndex) {
16
+ const fileOps = createFileOps();
17
+ // Collect from previous compaction's details (if pi-generated)
18
+ if (prevCompactionIndex >= 0) {
19
+ const prevCompaction = entries[prevCompactionIndex];
20
+ if (!prevCompaction.fromHook && prevCompaction.details) {
21
+ // fromHook field kept for session file compatibility
22
+ const details = prevCompaction.details;
23
+ if (Array.isArray(details.readFiles)) {
24
+ for (const f of details.readFiles)
25
+ fileOps.read.add(f);
26
+ }
27
+ if (Array.isArray(details.modifiedFiles)) {
28
+ for (const f of details.modifiedFiles)
29
+ fileOps.edited.add(f);
30
+ }
31
+ }
32
+ }
33
+ // Extract from tool calls in messages
34
+ for (const msg of messages) {
35
+ extractFileOpsFromMessage(msg, fileOps);
36
+ }
37
+ return fileOps;
38
+ }
39
+ // ============================================================================
40
+ // Message Extraction
41
+ // ============================================================================
42
+ /**
43
+ * Extract AgentMessage from an entry if it produces one.
44
+ * Returns undefined for entries that don't contribute to LLM context.
45
+ */
46
+ function getMessageFromEntry(entry) {
47
+ if (entry.type === "message") {
48
+ return entry.message;
49
+ }
50
+ if (entry.type === "custom_message") {
51
+ return createCustomMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp);
52
+ }
53
+ if (entry.type === "branch_summary") {
54
+ return createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);
55
+ }
56
+ if (entry.type === "compaction") {
57
+ return createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp);
58
+ }
59
+ return undefined;
60
+ }
61
+ function getMessageFromEntryForCompaction(entry) {
62
+ if (entry.type === "compaction") {
63
+ return undefined;
64
+ }
65
+ return getMessageFromEntry(entry);
66
+ }
67
+ export const DEFAULT_COMPACTION_SETTINGS = {
68
+ enabled: true,
69
+ reserveTokens: 16384,
70
+ keepRecentTokens: 20000,
71
+ };
72
+ // ============================================================================
73
+ // Token calculation
74
+ // ============================================================================
75
+ /**
76
+ * Calculate total context tokens from usage.
77
+ * Uses the native totalTokens field when available, falls back to computing from components.
78
+ */
79
+ export function calculateContextTokens(usage) {
80
+ return usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
81
+ }
82
+ /**
83
+ * Get usage from an assistant message if available.
84
+ * Skips aborted and error messages as they don't have valid usage data.
85
+ */
86
+ function getAssistantUsage(msg) {
87
+ if (msg.role === "assistant" && "usage" in msg) {
88
+ const assistantMsg = msg;
89
+ if (assistantMsg.stopReason !== "aborted" && assistantMsg.stopReason !== "error" && assistantMsg.usage) {
90
+ return assistantMsg.usage;
91
+ }
92
+ }
93
+ return undefined;
94
+ }
95
+ /**
96
+ * Find the last non-aborted assistant message usage from session entries.
97
+ */
98
+ export function getLastAssistantUsage(entries) {
99
+ for (let i = entries.length - 1; i >= 0; i--) {
100
+ const entry = entries[i];
101
+ if (entry.type === "message") {
102
+ const usage = getAssistantUsage(entry.message);
103
+ if (usage)
104
+ return usage;
105
+ }
106
+ }
107
+ return undefined;
108
+ }
109
+ function getLastAssistantUsageInfo(messages) {
110
+ for (let i = messages.length - 1; i >= 0; i--) {
111
+ const usage = getAssistantUsage(messages[i]);
112
+ if (usage)
113
+ return { usage, index: i };
114
+ }
115
+ return undefined;
116
+ }
117
+ /**
118
+ * Estimate context tokens from messages, using the last assistant usage when available.
119
+ * If there are messages after the last usage, estimate their tokens with estimateTokens.
120
+ */
121
+ export function estimateContextTokens(messages) {
122
+ const usageInfo = getLastAssistantUsageInfo(messages);
123
+ if (!usageInfo) {
124
+ let estimated = 0;
125
+ for (const message of messages) {
126
+ estimated += estimateTokens(message);
127
+ }
128
+ return {
129
+ tokens: estimated,
130
+ usageTokens: 0,
131
+ trailingTokens: estimated,
132
+ lastUsageIndex: null,
133
+ };
134
+ }
135
+ const usageTokens = calculateContextTokens(usageInfo.usage);
136
+ let trailingTokens = 0;
137
+ for (let i = usageInfo.index + 1; i < messages.length; i++) {
138
+ trailingTokens += estimateTokens(messages[i]);
139
+ }
140
+ return {
141
+ tokens: usageTokens + trailingTokens,
142
+ usageTokens,
143
+ trailingTokens,
144
+ lastUsageIndex: usageInfo.index,
145
+ };
146
+ }
147
+ /**
148
+ * Check if compaction should trigger based on context usage.
149
+ */
150
+ export function shouldCompact(contextTokens, contextWindow, settings) {
151
+ if (!settings.enabled)
152
+ return false;
153
+ return contextTokens > contextWindow - settings.reserveTokens;
154
+ }
155
+ // ============================================================================
156
+ // Cut point detection
157
+ // ============================================================================
158
+ /**
159
+ * Estimate token count for a message using chars/4 heuristic.
160
+ * This is conservative (overestimates tokens).
161
+ */
162
+ export function estimateTokens(message) {
163
+ let chars = 0;
164
+ switch (message.role) {
165
+ case "user": {
166
+ const content = message.content;
167
+ if (typeof content === "string") {
168
+ chars = content.length;
169
+ }
170
+ else if (Array.isArray(content)) {
171
+ for (const block of content) {
172
+ if (block.type === "text" && block.text) {
173
+ chars += block.text.length;
174
+ }
175
+ }
176
+ }
177
+ return Math.ceil(chars / 4);
178
+ }
179
+ case "assistant": {
180
+ const assistant = message;
181
+ for (const block of assistant.content) {
182
+ if (block.type === "text") {
183
+ chars += block.text.length;
184
+ }
185
+ else if (block.type === "thinking") {
186
+ chars += block.thinking.length;
187
+ }
188
+ else if (block.type === "toolCall") {
189
+ chars += block.name.length + JSON.stringify(block.arguments).length;
190
+ }
191
+ }
192
+ return Math.ceil(chars / 4);
193
+ }
194
+ case "custom":
195
+ case "toolResult": {
196
+ if (typeof message.content === "string") {
197
+ chars = message.content.length;
198
+ }
199
+ else {
200
+ for (const block of message.content) {
201
+ if (block.type === "text" && block.text) {
202
+ chars += block.text.length;
203
+ }
204
+ if (block.type === "image") {
205
+ chars += 4800; // Estimate images as 4000 chars, or 1200 tokens
206
+ }
207
+ }
208
+ }
209
+ return Math.ceil(chars / 4);
210
+ }
211
+ case "bashExecution": {
212
+ chars = message.command.length + message.output.length;
213
+ return Math.ceil(chars / 4);
214
+ }
215
+ case "branchSummary":
216
+ case "compactionSummary": {
217
+ chars = message.summary.length;
218
+ return Math.ceil(chars / 4);
219
+ }
220
+ }
221
+ return 0;
222
+ }
223
+ /**
224
+ * Find valid cut points: indices of user, assistant, custom, or bashExecution messages.
225
+ * Never cut at tool results (they must follow their tool call).
226
+ * When we cut at an assistant message with tool calls, its tool results follow it
227
+ * and will be kept.
228
+ * BashExecutionMessage is treated like a user message (user-initiated context).
229
+ */
230
+ function findValidCutPoints(entries, startIndex, endIndex) {
231
+ const cutPoints = [];
232
+ for (let i = startIndex; i < endIndex; i++) {
233
+ const entry = entries[i];
234
+ switch (entry.type) {
235
+ case "message": {
236
+ const role = entry.message.role;
237
+ switch (role) {
238
+ case "bashExecution":
239
+ case "custom":
240
+ case "branchSummary":
241
+ case "compactionSummary":
242
+ case "user":
243
+ case "assistant":
244
+ cutPoints.push(i);
245
+ break;
246
+ case "toolResult":
247
+ break;
248
+ }
249
+ break;
250
+ }
251
+ case "thinking_level_change":
252
+ case "model_change":
253
+ case "compaction":
254
+ case "branch_summary":
255
+ case "custom":
256
+ case "custom_message":
257
+ case "label":
258
+ case "session_info":
259
+ break;
260
+ }
261
+ // branch_summary and custom_message are user-role messages, valid cut points
262
+ if (entry.type === "branch_summary" || entry.type === "custom_message") {
263
+ cutPoints.push(i);
264
+ }
265
+ }
266
+ return cutPoints;
267
+ }
268
+ /**
269
+ * Find the user message (or bashExecution) that starts the turn containing the given entry index.
270
+ * Returns -1 if no turn start found before the index.
271
+ * BashExecutionMessage is treated like a user message for turn boundaries.
272
+ */
273
+ export function findTurnStartIndex(entries, entryIndex, startIndex) {
274
+ for (let i = entryIndex; i >= startIndex; i--) {
275
+ const entry = entries[i];
276
+ // branch_summary and custom_message are user-role messages, can start a turn
277
+ if (entry.type === "branch_summary" || entry.type === "custom_message") {
278
+ return i;
279
+ }
280
+ if (entry.type === "message") {
281
+ const role = entry.message.role;
282
+ if (role === "user" || role === "bashExecution") {
283
+ return i;
284
+ }
285
+ }
286
+ }
287
+ return -1;
288
+ }
289
+ /**
290
+ * Find the cut point in session entries that keeps approximately `keepRecentTokens`.
291
+ *
292
+ * Algorithm: Walk backwards from newest, accumulating estimated message sizes.
293
+ * Stop when we've accumulated >= keepRecentTokens. Cut at that point.
294
+ *
295
+ * Can cut at user OR assistant messages (never tool results). When cutting at an
296
+ * assistant message with tool calls, its tool results come after and will be kept.
297
+ *
298
+ * Returns CutPointResult with:
299
+ * - firstKeptEntryIndex: the entry index to start keeping from
300
+ * - turnStartIndex: if cutting mid-turn, the user message that started that turn
301
+ * - isSplitTurn: whether we're cutting in the middle of a turn
302
+ *
303
+ * Only considers entries between `startIndex` and `endIndex` (exclusive).
304
+ */
305
+ export function findCutPoint(entries, startIndex, endIndex, keepRecentTokens) {
306
+ const cutPoints = findValidCutPoints(entries, startIndex, endIndex);
307
+ if (cutPoints.length === 0) {
308
+ return { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };
309
+ }
310
+ // Walk backwards from newest, accumulating estimated message sizes
311
+ let accumulatedTokens = 0;
312
+ let cutIndex = cutPoints[0]; // Default: keep from first message (not header)
313
+ for (let i = endIndex - 1; i >= startIndex; i--) {
314
+ const entry = entries[i];
315
+ if (entry.type !== "message")
316
+ continue;
317
+ // Estimate this message's size
318
+ const messageTokens = estimateTokens(entry.message);
319
+ accumulatedTokens += messageTokens;
320
+ // Check if we've exceeded the budget
321
+ if (accumulatedTokens >= keepRecentTokens) {
322
+ // Find the closest valid cut point at or after this entry
323
+ for (let c = 0; c < cutPoints.length; c++) {
324
+ if (cutPoints[c] >= i) {
325
+ cutIndex = cutPoints[c];
326
+ break;
327
+ }
328
+ }
329
+ break;
330
+ }
331
+ }
332
+ // Scan backwards from cutIndex to include any non-message entries (bash, settings, etc.)
333
+ while (cutIndex > startIndex) {
334
+ const prevEntry = entries[cutIndex - 1];
335
+ // Stop at session header or compaction boundaries
336
+ if (prevEntry.type === "compaction") {
337
+ break;
338
+ }
339
+ if (prevEntry.type === "message") {
340
+ // Stop if we hit any message
341
+ break;
342
+ }
343
+ // Include this non-message entry (bash, settings change, etc.)
344
+ cutIndex--;
345
+ }
346
+ // Determine if this is a split turn
347
+ const cutEntry = entries[cutIndex];
348
+ const isUserMessage = cutEntry.type === "message" && cutEntry.message.role === "user";
349
+ const turnStartIndex = isUserMessage ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);
350
+ return {
351
+ firstKeptEntryIndex: cutIndex,
352
+ turnStartIndex,
353
+ isSplitTurn: !isUserMessage && turnStartIndex !== -1,
354
+ };
355
+ }
356
+ // ============================================================================
357
+ // Summarization
358
+ // ============================================================================
359
+ const SUMMARIZATION_PROMPT = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work.
360
+
361
+ Use this EXACT format:
362
+
363
+ ## Goal
364
+ [What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
365
+
366
+ ## Constraints & Preferences
367
+ - [Any constraints, preferences, or requirements mentioned by user]
368
+ - [Or "(none)" if none were mentioned]
369
+
370
+ ## Progress
371
+ ### Done
372
+ - [x] [Completed tasks/changes]
373
+
374
+ ### In Progress
375
+ - [ ] [Current work]
376
+
377
+ ### Blocked
378
+ - [Issues preventing progress, if any]
379
+
380
+ ## Key Decisions
381
+ - **[Decision]**: [Brief rationale]
382
+
383
+ ## Next Steps
384
+ 1. [Ordered list of what should happen next]
385
+
386
+ ## Critical Context
387
+ - [Any data, examples, or references needed to continue]
388
+ - [Or "(none)" if not applicable]
389
+
390
+ Keep each section concise. Preserve exact file paths, function names, and error messages.`;
391
+ const UPDATE_SUMMARIZATION_PROMPT = `The messages above are NEW conversation messages to incorporate into the existing summary provided in <previous-summary> tags.
392
+
393
+ Update the existing structured summary with new information. RULES:
394
+ - PRESERVE all existing information from the previous summary
395
+ - ADD new progress, decisions, and context from the new messages
396
+ - UPDATE the Progress section: move items from "In Progress" to "Done" when completed
397
+ - UPDATE "Next Steps" based on what was accomplished
398
+ - PRESERVE exact file paths, function names, and error messages
399
+ - If something is no longer relevant, you may remove it
400
+
401
+ Use this EXACT format:
402
+
403
+ ## Goal
404
+ [Preserve existing goals, add new ones if the task expanded]
405
+
406
+ ## Constraints & Preferences
407
+ - [Preserve existing, add new ones discovered]
408
+
409
+ ## Progress
410
+ ### Done
411
+ - [x] [Include previously done items AND newly completed items]
412
+
413
+ ### In Progress
414
+ - [ ] [Current work - update based on progress]
415
+
416
+ ### Blocked
417
+ - [Current blockers - remove if resolved]
418
+
419
+ ## Key Decisions
420
+ - **[Decision]**: [Brief rationale] (preserve all previous, add new)
421
+
422
+ ## Next Steps
423
+ 1. [Update based on current state]
424
+
425
+ ## Critical Context
426
+ - [Preserve important context, add new if needed]
427
+
428
+ Keep each section concise. Preserve exact file paths, function names, and error messages.`;
429
+ /**
430
+ * Generate a summary of the conversation using the LLM.
431
+ * If previousSummary is provided, uses the update prompt to merge.
432
+ */
433
+ export async function generateSummary(currentMessages, model, reserveTokens, apiKey, headers, signal, customInstructions, previousSummary, thinkingLevel) {
434
+ const maxTokens = Math.min(Math.floor(0.8 * reserveTokens), model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY);
435
+ // Use update prompt if we have a previous summary, otherwise initial prompt
436
+ let basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;
437
+ if (customInstructions) {
438
+ basePrompt = `${basePrompt}\n\nAdditional focus: ${customInstructions}`;
439
+ }
440
+ // Serialize conversation to text so model doesn't try to continue it
441
+ // Convert to LLM messages first (handles custom types like bashExecution, custom, etc.)
442
+ const llmMessages = convertToLlm(currentMessages);
443
+ const conversationText = serializeConversation(llmMessages);
444
+ // Build the prompt with conversation wrapped in tags
445
+ let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
446
+ if (previousSummary) {
447
+ promptText += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
448
+ }
449
+ promptText += basePrompt;
450
+ const summarizationMessages = [
451
+ {
452
+ role: "user",
453
+ content: [{ type: "text", text: promptText }],
454
+ timestamp: Date.now(),
455
+ },
456
+ ];
457
+ const completionOptions = model.reasoning && thinkingLevel && thinkingLevel !== "off"
458
+ ? { maxTokens, signal, apiKey, headers, reasoning: thinkingLevel }
459
+ : { maxTokens, signal, apiKey, headers };
460
+ const responseResult = await completeSimple(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, completionOptions).then((response) => ok(response), (error) => err(new CompactionError("unknown", "Summarization request failed", error)));
461
+ if (!responseResult.ok)
462
+ return err(responseResult.error);
463
+ const response = responseResult.value;
464
+ if (response.stopReason === "aborted") {
465
+ return err(new CompactionError("aborted", response.errorMessage || "Summarization aborted"));
466
+ }
467
+ if (response.stopReason === "error") {
468
+ return err(new CompactionError("summarization_failed", `Summarization failed: ${response.errorMessage || "Unknown error"}`));
469
+ }
470
+ const textContent = response.content
471
+ .filter((c) => c.type === "text")
472
+ .map((c) => c.text)
473
+ .join("\n");
474
+ return ok(textContent);
475
+ }
476
+ export function prepareCompaction(pathEntries, settings) {
477
+ if (pathEntries.length > 0 && pathEntries[pathEntries.length - 1].type === "compaction") {
478
+ return undefined;
479
+ }
480
+ let prevCompactionIndex = -1;
481
+ for (let i = pathEntries.length - 1; i >= 0; i--) {
482
+ if (pathEntries[i].type === "compaction") {
483
+ prevCompactionIndex = i;
484
+ break;
485
+ }
486
+ }
487
+ let previousSummary;
488
+ let boundaryStart = 0;
489
+ if (prevCompactionIndex >= 0) {
490
+ const prevCompaction = pathEntries[prevCompactionIndex];
491
+ previousSummary = prevCompaction.summary;
492
+ const firstKeptEntryIndex = pathEntries.findIndex((entry) => entry.id === prevCompaction.firstKeptEntryId);
493
+ boundaryStart = firstKeptEntryIndex >= 0 ? firstKeptEntryIndex : prevCompactionIndex + 1;
494
+ }
495
+ const boundaryEnd = pathEntries.length;
496
+ const tokensBefore = estimateContextTokens(buildSessionContext(pathEntries).messages).tokens;
497
+ const cutPoint = findCutPoint(pathEntries, boundaryStart, boundaryEnd, settings.keepRecentTokens);
498
+ // Get UUID of first kept entry
499
+ const firstKeptEntry = pathEntries[cutPoint.firstKeptEntryIndex];
500
+ if (!firstKeptEntry?.id) {
501
+ return undefined; // Session needs migration
502
+ }
503
+ const firstKeptEntryId = firstKeptEntry.id;
504
+ const historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex;
505
+ // Messages to summarize (will be discarded after summary)
506
+ const messagesToSummarize = [];
507
+ for (let i = boundaryStart; i < historyEnd; i++) {
508
+ const msg = getMessageFromEntryForCompaction(pathEntries[i]);
509
+ if (msg)
510
+ messagesToSummarize.push(msg);
511
+ }
512
+ // Messages for turn prefix summary (if splitting a turn)
513
+ const turnPrefixMessages = [];
514
+ if (cutPoint.isSplitTurn) {
515
+ for (let i = cutPoint.turnStartIndex; i < cutPoint.firstKeptEntryIndex; i++) {
516
+ const msg = getMessageFromEntryForCompaction(pathEntries[i]);
517
+ if (msg)
518
+ turnPrefixMessages.push(msg);
519
+ }
520
+ }
521
+ // Extract file operations from messages and previous compaction
522
+ const fileOps = extractFileOperations(messagesToSummarize, pathEntries, prevCompactionIndex);
523
+ // Also extract file ops from turn prefix if splitting
524
+ if (cutPoint.isSplitTurn) {
525
+ for (const msg of turnPrefixMessages) {
526
+ extractFileOpsFromMessage(msg, fileOps);
527
+ }
528
+ }
529
+ return {
530
+ firstKeptEntryId,
531
+ messagesToSummarize,
532
+ turnPrefixMessages,
533
+ isSplitTurn: cutPoint.isSplitTurn,
534
+ tokensBefore,
535
+ previousSummary,
536
+ fileOps,
537
+ settings,
538
+ };
539
+ }
540
+ // ============================================================================
541
+ // Main compaction function
542
+ // ============================================================================
543
+ const TURN_PREFIX_SUMMARIZATION_PROMPT = `This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained.
544
+
545
+ Summarize the prefix to provide context for the retained suffix:
546
+
547
+ ## Original Request
548
+ [What did the user ask for in this turn?]
549
+
550
+ ## Early Progress
551
+ - [Key decisions and work done in the prefix]
552
+
553
+ ## Context for Suffix
554
+ - [Information needed to understand the retained recent work]
555
+
556
+ Be concise. Focus on what's needed to understand the kept suffix.`;
557
+ /**
558
+ * Generate summaries for compaction using prepared data.
559
+ * Returns CompactionResult - SessionManager adds uuid/parentUuid when saving.
560
+ *
561
+ * @param preparation - Pre-calculated preparation from prepareCompaction()
562
+ * @param customInstructions - Optional custom focus for the summary
563
+ */
564
+ export { serializeConversation } from "./utils.js";
565
+ export async function compact(preparation, model, apiKey, headers, customInstructions, signal, thinkingLevel) {
566
+ const { firstKeptEntryId, messagesToSummarize, turnPrefixMessages, isSplitTurn, tokensBefore, previousSummary, fileOps, settings, } = preparation;
567
+ if (!firstKeptEntryId) {
568
+ return err(new CompactionError("invalid_session", "First kept entry has no UUID - session may need migration"));
569
+ }
570
+ let summary;
571
+ if (isSplitTurn && turnPrefixMessages.length > 0) {
572
+ const [historyResult, turnPrefixResult] = await Promise.all([
573
+ messagesToSummarize.length > 0
574
+ ? generateSummary(messagesToSummarize, model, settings.reserveTokens, apiKey, headers, signal, customInstructions, previousSummary, thinkingLevel)
575
+ : Promise.resolve(ok("No prior history.")),
576
+ generateTurnPrefixSummary(turnPrefixMessages, model, settings.reserveTokens, apiKey, headers, signal, thinkingLevel),
577
+ ]);
578
+ if (!historyResult.ok)
579
+ return err(historyResult.error);
580
+ if (!turnPrefixResult.ok)
581
+ return err(turnPrefixResult.error);
582
+ summary = `${historyResult.value}\n\n---\n\n**Turn Context (split turn):**\n\n${turnPrefixResult.value}`;
583
+ }
584
+ else {
585
+ const summaryResult = await generateSummary(messagesToSummarize, model, settings.reserveTokens, apiKey, headers, signal, customInstructions, previousSummary, thinkingLevel);
586
+ if (!summaryResult.ok)
587
+ return err(summaryResult.error);
588
+ summary = summaryResult.value;
589
+ }
590
+ const { readFiles, modifiedFiles } = computeFileLists(fileOps);
591
+ summary += formatFileOperations(readFiles, modifiedFiles);
592
+ return ok({
593
+ summary,
594
+ firstKeptEntryId,
595
+ tokensBefore,
596
+ details: { readFiles, modifiedFiles },
597
+ });
598
+ }
599
+ /**
600
+ * Generate a summary for a turn prefix (when splitting a turn).
601
+ */
602
+ async function generateTurnPrefixSummary(messages, model, reserveTokens, apiKey, headers, signal, thinkingLevel) {
603
+ const maxTokens = Math.min(Math.floor(0.5 * reserveTokens), model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY); // Smaller budget for turn prefix
604
+ const llmMessages = convertToLlm(messages);
605
+ const conversationText = serializeConversation(llmMessages);
606
+ const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`;
607
+ const summarizationMessages = [
608
+ {
609
+ role: "user",
610
+ content: [{ type: "text", text: promptText }],
611
+ timestamp: Date.now(),
612
+ },
613
+ ];
614
+ const responseResult = await completeSimple(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, model.reasoning && thinkingLevel && thinkingLevel !== "off"
615
+ ? { maxTokens, signal, apiKey, headers, reasoning: thinkingLevel }
616
+ : { maxTokens, signal, apiKey, headers }).then((response) => ok(response), (error) => err(new CompactionError("unknown", "Turn prefix summarization request failed", error)));
617
+ if (!responseResult.ok)
618
+ return err(responseResult.error);
619
+ const response = responseResult.value;
620
+ if (response.stopReason === "aborted") {
621
+ return err(new CompactionError("aborted", response.errorMessage || "Turn prefix summarization aborted"));
622
+ }
623
+ if (response.stopReason === "error") {
624
+ return err(new CompactionError("summarization_failed", `Turn prefix summarization failed: ${response.errorMessage || "Unknown error"}`));
625
+ }
626
+ return ok(response.content
627
+ .filter((c) => c.type === "text")
628
+ .map((c) => c.text)
629
+ .join("\n"));
630
+ }
631
+ //# sourceMappingURL=compaction.js.map