@averyyy/pi-agent-core 0.80.3-piclient.10

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