@aexol/spectral 0.7.1 → 0.7.5

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 (219) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/agent/agents.js +1 -1
  3. package/dist/agent/index.js +199 -184
  4. package/dist/commands/serve.js +0 -3
  5. package/dist/designer/data/systems/renault/DESIGN.md +1 -1
  6. package/dist/designer/philosophies.js +668 -0
  7. package/dist/mcp/sampling-handler.js +1 -1
  8. package/dist/memory/commands/status.js +1 -1
  9. package/dist/memory/compaction.js +2 -2
  10. package/dist/memory/config.js +1 -1
  11. package/dist/memory/debug-log.js +1 -1
  12. package/dist/memory/hooks/compaction-hook.js +29 -0
  13. package/dist/memory/index.js +2 -0
  14. package/dist/memory/observer.js +2 -2
  15. package/dist/memory/project-observations-store.js +14 -0
  16. package/dist/memory/tokens.js +1 -1
  17. package/dist/memory/tools/read-project-observations.js +82 -0
  18. package/dist/memory/tools/recall-observation.js +2 -2
  19. package/dist/pi/agent-core/agent-loop.js +501 -0
  20. package/dist/pi/agent-core/agent.js +401 -0
  21. package/dist/pi/agent-core/harness/agent-harness.js +899 -0
  22. package/dist/pi/agent-core/harness/compaction/branch-summarization.js +173 -0
  23. package/dist/pi/agent-core/harness/compaction/compaction.js +532 -0
  24. package/dist/pi/agent-core/harness/compaction/utils.js +130 -0
  25. package/dist/pi/agent-core/harness/env/nodejs.js +485 -0
  26. package/dist/pi/agent-core/harness/messages.js +101 -0
  27. package/dist/pi/agent-core/harness/prompt-templates.js +229 -0
  28. package/dist/pi/agent-core/harness/session/jsonl-repo.js +100 -0
  29. package/dist/pi/agent-core/harness/session/jsonl-storage.js +230 -0
  30. package/dist/pi/agent-core/harness/session/memory-repo.js +41 -0
  31. package/dist/pi/agent-core/harness/session/memory-storage.js +113 -0
  32. package/dist/pi/agent-core/harness/session/repo-utils.js +38 -0
  33. package/dist/pi/agent-core/harness/session/session.js +196 -0
  34. package/dist/pi/agent-core/harness/session/uuid.js +49 -0
  35. package/dist/pi/agent-core/harness/skills.js +310 -0
  36. package/dist/pi/agent-core/harness/system-prompt.js +29 -0
  37. package/dist/pi/agent-core/harness/types.js +93 -0
  38. package/dist/pi/agent-core/harness/utils/shell-output.js +125 -0
  39. package/dist/pi/agent-core/harness/utils/truncate.js +289 -0
  40. package/dist/pi/agent-core/index.js +24 -0
  41. package/dist/pi/agent-core/node.js +2 -0
  42. package/dist/pi/agent-core/proxy.js +277 -0
  43. package/dist/pi/agent-core/types.js +1 -0
  44. package/dist/pi/ai/api-registry.js +43 -0
  45. package/dist/pi/ai/cli.js +120 -0
  46. package/dist/pi/ai/env-api-keys.js +169 -0
  47. package/dist/pi/ai/image-models.generated.js +441 -0
  48. package/dist/pi/ai/image-models.js +22 -0
  49. package/dist/pi/ai/images-api-registry.js +21 -0
  50. package/dist/pi/ai/images.js +13 -0
  51. package/dist/pi/ai/index.js +18 -0
  52. package/dist/pi/ai/models.generated.js +16220 -0
  53. package/dist/pi/ai/models.js +70 -0
  54. package/dist/pi/ai/oauth.js +1 -0
  55. package/dist/pi/ai/providers/anthropic.js +945 -0
  56. package/dist/pi/ai/providers/faux.js +367 -0
  57. package/dist/pi/ai/providers/github-copilot-headers.js +28 -0
  58. package/dist/pi/ai/providers/openai-completions.js +945 -0
  59. package/dist/pi/ai/providers/openai-prompt-cache.js +9 -0
  60. package/dist/pi/ai/providers/register-builtins.js +97 -0
  61. package/dist/pi/ai/providers/simple-options.js +40 -0
  62. package/dist/pi/ai/providers/transform-messages.js +183 -0
  63. package/dist/pi/ai/session-resources.js +21 -0
  64. package/dist/pi/ai/stream.js +26 -0
  65. package/dist/pi/ai/types.js +1 -0
  66. package/dist/pi/ai/utils/diagnostics.js +24 -0
  67. package/dist/pi/ai/utils/event-stream.js +80 -0
  68. package/dist/pi/ai/utils/hash.js +13 -0
  69. package/dist/pi/ai/utils/headers.js +7 -0
  70. package/dist/pi/ai/utils/json-parse.js +112 -0
  71. package/dist/pi/ai/utils/node-http-proxy.js +96 -0
  72. package/dist/pi/ai/utils/oauth/anthropic.js +334 -0
  73. package/dist/pi/ai/utils/oauth/device-code.js +54 -0
  74. package/dist/pi/ai/utils/oauth/github-copilot.js +270 -0
  75. package/dist/pi/ai/utils/oauth/index.js +121 -0
  76. package/dist/pi/ai/utils/oauth/oauth-page.js +104 -0
  77. package/dist/pi/ai/utils/oauth/openai-codex.js +384 -0
  78. package/dist/pi/ai/utils/oauth/pkce.js +30 -0
  79. package/dist/pi/ai/utils/oauth/types.js +1 -0
  80. package/dist/pi/ai/utils/overflow.js +150 -0
  81. package/dist/pi/ai/utils/sanitize-unicode.js +25 -0
  82. package/dist/pi/ai/utils/typebox-helpers.js +20 -0
  83. package/dist/pi/ai/utils/validation.js +280 -0
  84. package/dist/pi/coding-agent/bun/cli.js +7 -0
  85. package/dist/pi/coding-agent/bun/restore-sandbox-env.js +31 -0
  86. package/dist/pi/coding-agent/cli/args.js +340 -0
  87. package/dist/pi/coding-agent/cli/file-processor.js +82 -0
  88. package/dist/pi/coding-agent/cli/initial-message.js +21 -0
  89. package/dist/pi/coding-agent/cli.js +17 -0
  90. package/dist/pi/coding-agent/config.js +414 -0
  91. package/dist/pi/coding-agent/core/agent-session-runtime.js +299 -0
  92. package/dist/pi/coding-agent/core/agent-session-services.js +117 -0
  93. package/dist/pi/coding-agent/core/agent-session.js +2498 -0
  94. package/dist/pi/coding-agent/core/auth-guidance.js +20 -0
  95. package/dist/pi/coding-agent/core/auth-storage.js +441 -0
  96. package/dist/pi/coding-agent/core/bash-executor.js +110 -0
  97. package/dist/pi/coding-agent/core/compaction/branch-summarization.js +242 -0
  98. package/dist/pi/coding-agent/core/compaction/compaction.js +624 -0
  99. package/dist/pi/coding-agent/core/compaction/index.js +6 -0
  100. package/dist/pi/coding-agent/core/compaction/utils.js +152 -0
  101. package/dist/pi/coding-agent/core/defaults.js +1 -0
  102. package/dist/pi/coding-agent/core/diagnostics.js +1 -0
  103. package/dist/pi/coding-agent/core/event-bus.js +24 -0
  104. package/dist/pi/coding-agent/core/exec.js +74 -0
  105. package/dist/pi/coding-agent/core/export-html/ansi-to-html.js +248 -0
  106. package/dist/pi/coding-agent/core/export-html/index.js +225 -0
  107. package/dist/pi/coding-agent/core/export-html/tool-renderer.js +107 -0
  108. package/dist/pi/coding-agent/core/extensions/index.js +8 -0
  109. package/dist/pi/coding-agent/core/extensions/loader.js +485 -0
  110. package/dist/pi/coding-agent/core/extensions/runner.js +824 -0
  111. package/dist/pi/coding-agent/core/extensions/types.js +44 -0
  112. package/dist/pi/coding-agent/core/extensions/wrapper.js +21 -0
  113. package/dist/pi/coding-agent/core/footer-data-provider.js +309 -0
  114. package/dist/pi/coding-agent/core/http-dispatcher.js +47 -0
  115. package/dist/pi/coding-agent/core/index.js +11 -0
  116. package/dist/pi/coding-agent/core/keybindings.js +294 -0
  117. package/dist/pi/coding-agent/core/messages.js +122 -0
  118. package/dist/pi/coding-agent/core/model-registry.js +728 -0
  119. package/dist/pi/coding-agent/core/model-resolver.js +494 -0
  120. package/dist/pi/coding-agent/core/output-guard.js +58 -0
  121. package/dist/pi/coding-agent/core/package-manager.js +2020 -0
  122. package/dist/pi/coding-agent/core/prompt-templates.js +237 -0
  123. package/dist/pi/coding-agent/core/provider-display-names.js +32 -0
  124. package/dist/pi/coding-agent/core/resolve-config-value.js +125 -0
  125. package/dist/pi/coding-agent/core/resource-loader.js +733 -0
  126. package/dist/pi/coding-agent/core/sdk.js +282 -0
  127. package/dist/pi/coding-agent/core/session-cwd.js +37 -0
  128. package/dist/pi/coding-agent/core/session-manager.js +1146 -0
  129. package/dist/pi/coding-agent/core/settings-manager.js +794 -0
  130. package/dist/pi/coding-agent/core/skills.js +386 -0
  131. package/dist/pi/coding-agent/core/slash-commands.js +24 -0
  132. package/dist/pi/coding-agent/core/source-info.js +18 -0
  133. package/dist/pi/coding-agent/core/system-prompt.js +122 -0
  134. package/dist/pi/coding-agent/core/telemetry.js +8 -0
  135. package/dist/pi/coding-agent/core/timings.js +30 -0
  136. package/dist/pi/coding-agent/core/tools/bash.js +341 -0
  137. package/dist/pi/coding-agent/core/tools/edit-diff.js +344 -0
  138. package/dist/pi/coding-agent/core/tools/edit.js +324 -0
  139. package/dist/pi/coding-agent/core/tools/file-mutation-queue.js +36 -0
  140. package/dist/pi/coding-agent/core/tools/find.js +297 -0
  141. package/dist/pi/coding-agent/core/tools/grep.js +303 -0
  142. package/dist/pi/coding-agent/core/tools/index.js +111 -0
  143. package/dist/pi/coding-agent/core/tools/ls.js +168 -0
  144. package/dist/pi/coding-agent/core/tools/output-accumulator.js +183 -0
  145. package/dist/pi/coding-agent/core/tools/path-utils.js +61 -0
  146. package/dist/pi/coding-agent/core/tools/read.js +288 -0
  147. package/dist/pi/coding-agent/core/tools/render-utils.js +48 -0
  148. package/dist/pi/coding-agent/core/tools/tool-definition-wrapper.js +33 -0
  149. package/dist/pi/coding-agent/core/tools/truncate.js +214 -0
  150. package/dist/pi/coding-agent/core/tools/write.js +212 -0
  151. package/dist/pi/coding-agent/index.js +41 -0
  152. package/dist/pi/coding-agent/main.js +5 -0
  153. package/dist/pi/coding-agent/migrations.js +280 -0
  154. package/dist/pi/coding-agent/modes/index.js +7 -0
  155. package/dist/pi/coding-agent/modes/interactive/components/diff.js +132 -0
  156. package/dist/pi/coding-agent/modes/interactive/components/keybinding-hints.js +35 -0
  157. package/dist/pi/coding-agent/modes/interactive/components/visual-truncate.js +32 -0
  158. package/dist/pi/coding-agent/modes/interactive/interactive-mode.js +3 -0
  159. package/dist/pi/coding-agent/modes/interactive/theme/theme.js +1023 -0
  160. package/dist/pi/coding-agent/modes/print-mode.js +130 -0
  161. package/dist/pi/coding-agent/modes/rpc/jsonl.js +48 -0
  162. package/dist/pi/coding-agent/modes/rpc/rpc-client.js +409 -0
  163. package/dist/pi/coding-agent/modes/rpc/rpc-mode.js +600 -0
  164. package/dist/pi/coding-agent/modes/rpc/rpc-types.js +7 -0
  165. package/dist/pi/coding-agent/utils/ansi.js +51 -0
  166. package/dist/pi/coding-agent/utils/changelog.js +86 -0
  167. package/dist/pi/coding-agent/utils/child-process.js +87 -0
  168. package/dist/pi/coding-agent/utils/clipboard-image.js +244 -0
  169. package/dist/pi/coding-agent/utils/clipboard-native.js +13 -0
  170. package/dist/pi/coding-agent/utils/clipboard.js +116 -0
  171. package/dist/pi/coding-agent/utils/exif-orientation.js +157 -0
  172. package/dist/pi/coding-agent/utils/frontmatter.js +25 -0
  173. package/dist/pi/coding-agent/utils/fs-watch.js +24 -0
  174. package/dist/pi/coding-agent/utils/git.js +162 -0
  175. package/dist/pi/coding-agent/utils/html.js +39 -0
  176. package/dist/pi/coding-agent/utils/image-convert.js +38 -0
  177. package/dist/pi/coding-agent/utils/image-resize.js +136 -0
  178. package/dist/pi/coding-agent/utils/mime.js +68 -0
  179. package/dist/pi/coding-agent/utils/paths.js +91 -0
  180. package/dist/pi/coding-agent/utils/photon.js +120 -0
  181. package/dist/pi/coding-agent/utils/pi-user-agent.js +4 -0
  182. package/dist/pi/coding-agent/utils/shell.js +194 -0
  183. package/dist/pi/coding-agent/utils/sleep.js +16 -0
  184. package/dist/pi/coding-agent/utils/syntax-highlight.js +117 -0
  185. package/dist/pi/coding-agent/utils/tools-manager.js +327 -0
  186. package/dist/pi/coding-agent/utils/version-check.js +81 -0
  187. package/dist/pi/coding-agent/utils/windows-self-update.js +76 -0
  188. package/dist/pi/tui/autocomplete.js +631 -0
  189. package/dist/pi/tui/components/box.js +103 -0
  190. package/dist/pi/tui/components/cancellable-loader.js +34 -0
  191. package/dist/pi/tui/components/editor.js +1915 -0
  192. package/dist/pi/tui/components/image.js +88 -0
  193. package/dist/pi/tui/components/input.js +425 -0
  194. package/dist/pi/tui/components/loader.js +68 -0
  195. package/dist/pi/tui/components/markdown.js +633 -0
  196. package/dist/pi/tui/components/select-list.js +158 -0
  197. package/dist/pi/tui/components/settings-list.js +184 -0
  198. package/dist/pi/tui/components/spacer.js +22 -0
  199. package/dist/pi/tui/components/text.js +88 -0
  200. package/dist/pi/tui/components/truncated-text.js +50 -0
  201. package/dist/pi/tui/editor-component.js +1 -0
  202. package/dist/pi/tui/fuzzy.js +109 -0
  203. package/dist/pi/tui/index.js +31 -0
  204. package/dist/pi/tui/keybindings.js +173 -0
  205. package/dist/pi/tui/keys.js +1172 -0
  206. package/dist/pi/tui/kill-ring.js +43 -0
  207. package/dist/pi/tui/stdin-buffer.js +360 -0
  208. package/dist/pi/tui/terminal-image.js +335 -0
  209. package/dist/pi/tui/terminal.js +324 -0
  210. package/dist/pi/tui/tui.js +1076 -0
  211. package/dist/pi/tui/undo-stack.js +24 -0
  212. package/dist/pi/tui/utils.js +1016 -0
  213. package/dist/relay/dispatcher.js +30 -0
  214. package/dist/server/handlers/queue.js +52 -0
  215. package/dist/server/pi-bridge.js +9 -1
  216. package/dist/server/session-stream.js +76 -111
  217. package/dist/server/storage.js +154 -2
  218. package/dist/server/title-generator.js +14 -153
  219. package/package.json +24 -6
@@ -0,0 +1,532 @@
1
+ import { completeSimple } from "../../../ai/index.js";
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" && assistantMsg.stopReason !== "error" && assistantMsg.usage) {
70
+ return assistantMsg.usage;
71
+ }
72
+ }
73
+ return undefined;
74
+ }
75
+ /** Return usage from the last successful assistant message in session entries. */
76
+ export function getLastAssistantUsage(entries) {
77
+ for (let i = entries.length - 1; i >= 0; i--) {
78
+ const entry = entries[i];
79
+ if (entry.type === "message") {
80
+ const usage = getAssistantUsage(entry.message);
81
+ if (usage)
82
+ return usage;
83
+ }
84
+ }
85
+ return undefined;
86
+ }
87
+ function getLastAssistantUsageInfo(messages) {
88
+ for (let i = messages.length - 1; i >= 0; i--) {
89
+ const usage = getAssistantUsage(messages[i]);
90
+ if (usage)
91
+ return { usage, index: i };
92
+ }
93
+ return undefined;
94
+ }
95
+ /** Estimate context tokens for messages using provider usage when available. */
96
+ export function estimateContextTokens(messages) {
97
+ const usageInfo = getLastAssistantUsageInfo(messages);
98
+ if (!usageInfo) {
99
+ let estimated = 0;
100
+ for (const message of messages) {
101
+ estimated += estimateTokens(message);
102
+ }
103
+ return {
104
+ tokens: estimated,
105
+ usageTokens: 0,
106
+ trailingTokens: estimated,
107
+ lastUsageIndex: null,
108
+ };
109
+ }
110
+ const usageTokens = calculateContextTokens(usageInfo.usage);
111
+ let trailingTokens = 0;
112
+ for (let i = usageInfo.index + 1; i < messages.length; i++) {
113
+ trailingTokens += estimateTokens(messages[i]);
114
+ }
115
+ return {
116
+ tokens: usageTokens + trailingTokens,
117
+ usageTokens,
118
+ trailingTokens,
119
+ lastUsageIndex: usageInfo.index,
120
+ };
121
+ }
122
+ /** Return whether context usage exceeds the configured compaction threshold. */
123
+ export function shouldCompact(contextTokens, contextWindow, settings) {
124
+ if (!settings.enabled)
125
+ return false;
126
+ return contextTokens > contextWindow - settings.reserveTokens;
127
+ }
128
+ /** Estimate token count for one message using a conservative character heuristic. */
129
+ export function estimateTokens(message) {
130
+ let chars = 0;
131
+ switch (message.role) {
132
+ case "user": {
133
+ const content = message.content;
134
+ if (typeof content === "string") {
135
+ chars = content.length;
136
+ }
137
+ else if (Array.isArray(content)) {
138
+ for (const block of content) {
139
+ if (block.type === "text" && block.text) {
140
+ chars += block.text.length;
141
+ }
142
+ }
143
+ }
144
+ return Math.ceil(chars / 4);
145
+ }
146
+ case "assistant": {
147
+ const assistant = message;
148
+ for (const block of assistant.content) {
149
+ if (block.type === "text") {
150
+ chars += block.text.length;
151
+ }
152
+ else if (block.type === "thinking") {
153
+ chars += block.thinking.length;
154
+ }
155
+ else if (block.type === "toolCall") {
156
+ chars += block.name.length + safeJsonStringify(block.arguments).length;
157
+ }
158
+ }
159
+ return Math.ceil(chars / 4);
160
+ }
161
+ case "custom":
162
+ case "toolResult": {
163
+ if (typeof message.content === "string") {
164
+ chars = message.content.length;
165
+ }
166
+ else {
167
+ for (const block of message.content) {
168
+ if (block.type === "text" && block.text) {
169
+ chars += block.text.length;
170
+ }
171
+ if (block.type === "image") {
172
+ chars += 4800;
173
+ }
174
+ }
175
+ }
176
+ return Math.ceil(chars / 4);
177
+ }
178
+ case "bashExecution": {
179
+ chars = message.command.length + message.output.length;
180
+ return Math.ceil(chars / 4);
181
+ }
182
+ case "branchSummary":
183
+ case "compactionSummary": {
184
+ chars = message.summary.length;
185
+ return Math.ceil(chars / 4);
186
+ }
187
+ }
188
+ return 0;
189
+ }
190
+ function findValidCutPoints(entries, startIndex, endIndex) {
191
+ const cutPoints = [];
192
+ for (let i = startIndex; i < endIndex; i++) {
193
+ const entry = entries[i];
194
+ switch (entry.type) {
195
+ case "message": {
196
+ const role = entry.message.role;
197
+ switch (role) {
198
+ case "bashExecution":
199
+ case "custom":
200
+ case "branchSummary":
201
+ case "compactionSummary":
202
+ case "user":
203
+ case "assistant":
204
+ cutPoints.push(i);
205
+ break;
206
+ case "toolResult":
207
+ break;
208
+ }
209
+ break;
210
+ }
211
+ case "thinking_level_change":
212
+ case "model_change":
213
+ case "compaction":
214
+ case "branch_summary":
215
+ case "custom":
216
+ case "custom_message":
217
+ case "label":
218
+ case "session_info":
219
+ case "leaf":
220
+ break;
221
+ }
222
+ if (entry.type === "branch_summary" || entry.type === "custom_message") {
223
+ cutPoints.push(i);
224
+ }
225
+ }
226
+ return cutPoints;
227
+ }
228
+ /** Find the user-visible message that starts the turn containing an entry. */
229
+ export function findTurnStartIndex(entries, entryIndex, startIndex) {
230
+ for (let i = entryIndex; i >= startIndex; i--) {
231
+ const entry = entries[i];
232
+ if (entry.type === "branch_summary" || entry.type === "custom_message") {
233
+ return i;
234
+ }
235
+ if (entry.type === "message") {
236
+ const role = entry.message.role;
237
+ if (role === "user" || role === "bashExecution") {
238
+ return i;
239
+ }
240
+ }
241
+ }
242
+ return -1;
243
+ }
244
+ /** Find the compaction cut point that keeps approximately the requested recent-token budget. */
245
+ export function findCutPoint(entries, startIndex, endIndex, keepRecentTokens) {
246
+ const cutPoints = findValidCutPoints(entries, startIndex, endIndex);
247
+ if (cutPoints.length === 0) {
248
+ return { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };
249
+ }
250
+ let accumulatedTokens = 0;
251
+ let cutIndex = cutPoints[0];
252
+ for (let i = endIndex - 1; i >= startIndex; i--) {
253
+ const entry = entries[i];
254
+ if (entry.type !== "message")
255
+ continue;
256
+ const messageTokens = estimateTokens(entry.message);
257
+ accumulatedTokens += messageTokens;
258
+ if (accumulatedTokens >= keepRecentTokens) {
259
+ for (let c = 0; c < cutPoints.length; c++) {
260
+ if (cutPoints[c] >= i) {
261
+ cutIndex = cutPoints[c];
262
+ break;
263
+ }
264
+ }
265
+ break;
266
+ }
267
+ }
268
+ while (cutIndex > startIndex) {
269
+ const prevEntry = entries[cutIndex - 1];
270
+ if (prevEntry.type === "compaction") {
271
+ break;
272
+ }
273
+ if (prevEntry.type === "message") {
274
+ break;
275
+ }
276
+ cutIndex--;
277
+ }
278
+ const cutEntry = entries[cutIndex];
279
+ const isUserMessage = cutEntry.type === "message" && cutEntry.message.role === "user";
280
+ const turnStartIndex = isUserMessage ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);
281
+ return {
282
+ firstKeptEntryIndex: cutIndex,
283
+ turnStartIndex,
284
+ isSplitTurn: !isUserMessage && turnStartIndex !== -1,
285
+ };
286
+ }
287
+ export const SUMMARIZATION_SYSTEM_PROMPT = `You are a context summarization assistant. Your task is to read a conversation between a user and an AI coding assistant, then produce a structured summary following the exact format specified.
288
+
289
+ Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.`;
290
+ 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.
291
+
292
+ Use this EXACT format:
293
+
294
+ ## Goal
295
+ [What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
296
+
297
+ ## Constraints & Preferences
298
+ - [Any constraints, preferences, or requirements mentioned by user]
299
+ - [Or "(none)" if none were mentioned]
300
+
301
+ ## Progress
302
+ ### Done
303
+ - [x] [Completed tasks/changes]
304
+
305
+ ### In Progress
306
+ - [ ] [Current work]
307
+
308
+ ### Blocked
309
+ - [Issues preventing progress, if any]
310
+
311
+ ## Key Decisions
312
+ - **[Decision]**: [Brief rationale]
313
+
314
+ ## Next Steps
315
+ 1. [Ordered list of what should happen next]
316
+
317
+ ## Critical Context
318
+ - [Any data, examples, or references needed to continue]
319
+ - [Or "(none)" if not applicable]
320
+
321
+ Keep each section concise. Preserve exact file paths, function names, and error messages.`;
322
+ const UPDATE_SUMMARIZATION_PROMPT = `The messages above are NEW conversation messages to incorporate into the existing summary provided in <previous-summary> tags.
323
+
324
+ Update the existing structured summary with new information. RULES:
325
+ - PRESERVE all existing information from the previous summary
326
+ - ADD new progress, decisions, and context from the new messages
327
+ - UPDATE the Progress section: move items from "In Progress" to "Done" when completed
328
+ - UPDATE "Next Steps" based on what was accomplished
329
+ - PRESERVE exact file paths, function names, and error messages
330
+ - If something is no longer relevant, you may remove it
331
+
332
+ Use this EXACT format:
333
+
334
+ ## Goal
335
+ [Preserve existing goals, add new ones if the task expanded]
336
+
337
+ ## Constraints & Preferences
338
+ - [Preserve existing, add new ones discovered]
339
+
340
+ ## Progress
341
+ ### Done
342
+ - [x] [Include previously done items AND newly completed items]
343
+
344
+ ### In Progress
345
+ - [ ] [Current work - update based on progress]
346
+
347
+ ### Blocked
348
+ - [Current blockers - remove if resolved]
349
+
350
+ ## Key Decisions
351
+ - **[Decision]**: [Brief rationale] (preserve all previous, add new)
352
+
353
+ ## Next Steps
354
+ 1. [Update based on current state]
355
+
356
+ ## Critical Context
357
+ - [Preserve important context, add new if needed]
358
+
359
+ Keep each section concise. Preserve exact file paths, function names, and error messages.`;
360
+ /** Generate or update a conversation summary for compaction. */
361
+ export async function generateSummary(currentMessages, model, reserveTokens, apiKey, headers, signal, customInstructions, previousSummary, thinkingLevel) {
362
+ const maxTokens = Math.min(Math.floor(0.8 * reserveTokens), model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY);
363
+ let basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;
364
+ if (customInstructions) {
365
+ basePrompt = `${basePrompt}\n\nAdditional focus: ${customInstructions}`;
366
+ }
367
+ const llmMessages = convertToLlm(currentMessages);
368
+ const conversationText = serializeConversation(llmMessages);
369
+ let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
370
+ if (previousSummary) {
371
+ promptText += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
372
+ }
373
+ promptText += basePrompt;
374
+ const summarizationMessages = [
375
+ {
376
+ role: "user",
377
+ content: [{ type: "text", text: promptText }],
378
+ timestamp: Date.now(),
379
+ },
380
+ ];
381
+ const completionOptions = model.reasoning && thinkingLevel && thinkingLevel !== "off"
382
+ ? { maxTokens, signal, apiKey, headers, reasoning: thinkingLevel }
383
+ : { maxTokens, signal, apiKey, headers };
384
+ const response = await completeSimple(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, completionOptions);
385
+ if (response.stopReason === "aborted") {
386
+ return err(new CompactionError("aborted", response.errorMessage || "Summarization aborted"));
387
+ }
388
+ if (response.stopReason === "error") {
389
+ return err(new CompactionError("summarization_failed", `Summarization failed: ${response.errorMessage || "Unknown error"}`));
390
+ }
391
+ const textContent = response.content
392
+ .filter((c) => c.type === "text")
393
+ .map((c) => c.text)
394
+ .join("\n");
395
+ return ok(textContent);
396
+ }
397
+ /** Prepare session entries for compaction, or return undefined when compaction is not applicable. */
398
+ export function prepareCompaction(pathEntries, settings) {
399
+ if (pathEntries.length === 0 || pathEntries[pathEntries.length - 1].type === "compaction") {
400
+ return ok(undefined);
401
+ }
402
+ let prevCompactionIndex = -1;
403
+ for (let i = pathEntries.length - 1; i >= 0; i--) {
404
+ if (pathEntries[i].type === "compaction") {
405
+ prevCompactionIndex = i;
406
+ break;
407
+ }
408
+ }
409
+ let previousSummary;
410
+ let boundaryStart = 0;
411
+ if (prevCompactionIndex >= 0) {
412
+ const prevCompaction = pathEntries[prevCompactionIndex];
413
+ previousSummary = prevCompaction.summary;
414
+ const firstKeptEntryIndex = pathEntries.findIndex((entry) => entry.id === prevCompaction.firstKeptEntryId);
415
+ boundaryStart = firstKeptEntryIndex >= 0 ? firstKeptEntryIndex : prevCompactionIndex + 1;
416
+ }
417
+ const boundaryEnd = pathEntries.length;
418
+ const tokensBefore = estimateContextTokens(buildSessionContext(pathEntries).messages).tokens;
419
+ const cutPoint = findCutPoint(pathEntries, boundaryStart, boundaryEnd, settings.keepRecentTokens);
420
+ const firstKeptEntry = pathEntries[cutPoint.firstKeptEntryIndex];
421
+ if (!firstKeptEntry?.id) {
422
+ return err(new CompactionError("invalid_session", "First kept entry has no UUID - session may need migration"));
423
+ }
424
+ const firstKeptEntryId = firstKeptEntry.id;
425
+ const historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex;
426
+ const messagesToSummarize = [];
427
+ for (let i = boundaryStart; i < historyEnd; i++) {
428
+ const msg = getMessageFromEntryForCompaction(pathEntries[i]);
429
+ if (msg)
430
+ messagesToSummarize.push(msg);
431
+ }
432
+ const turnPrefixMessages = [];
433
+ if (cutPoint.isSplitTurn) {
434
+ for (let i = cutPoint.turnStartIndex; i < cutPoint.firstKeptEntryIndex; i++) {
435
+ const msg = getMessageFromEntryForCompaction(pathEntries[i]);
436
+ if (msg)
437
+ turnPrefixMessages.push(msg);
438
+ }
439
+ }
440
+ const fileOps = extractFileOperations(messagesToSummarize, pathEntries, prevCompactionIndex);
441
+ if (cutPoint.isSplitTurn) {
442
+ for (const msg of turnPrefixMessages) {
443
+ extractFileOpsFromMessage(msg, fileOps);
444
+ }
445
+ }
446
+ return ok({
447
+ firstKeptEntryId,
448
+ messagesToSummarize,
449
+ turnPrefixMessages,
450
+ isSplitTurn: cutPoint.isSplitTurn,
451
+ tokensBefore,
452
+ previousSummary,
453
+ fileOps,
454
+ settings,
455
+ });
456
+ }
457
+ const TURN_PREFIX_SUMMARIZATION_PROMPT = `This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained.
458
+
459
+ Summarize the prefix to provide context for the retained suffix:
460
+
461
+ ## Original Request
462
+ [What did the user ask for in this turn?]
463
+
464
+ ## Early Progress
465
+ - [Key decisions and work done in the prefix]
466
+
467
+ ## Context for Suffix
468
+ - [Information needed to understand the retained recent work]
469
+
470
+ Be concise. Focus on what's needed to understand the kept suffix.`;
471
+ export { serializeConversation } from "./utils.js";
472
+ /** Generate compaction summary data from prepared session history. */
473
+ export async function compact(preparation, model, apiKey, headers, customInstructions, signal, thinkingLevel) {
474
+ const { firstKeptEntryId, messagesToSummarize, turnPrefixMessages, isSplitTurn, tokensBefore, previousSummary, fileOps, settings, } = preparation;
475
+ if (!firstKeptEntryId) {
476
+ return err(new CompactionError("invalid_session", "First kept entry has no UUID - session may need migration"));
477
+ }
478
+ let summary;
479
+ if (isSplitTurn && turnPrefixMessages.length > 0) {
480
+ const [historyResult, turnPrefixResult] = await Promise.all([
481
+ messagesToSummarize.length > 0
482
+ ? generateSummary(messagesToSummarize, model, settings.reserveTokens, apiKey, headers, signal, customInstructions, previousSummary, thinkingLevel)
483
+ : Promise.resolve(ok("No prior history.")),
484
+ generateTurnPrefixSummary(turnPrefixMessages, model, settings.reserveTokens, apiKey, headers, signal, thinkingLevel),
485
+ ]);
486
+ if (!historyResult.ok)
487
+ return err(historyResult.error);
488
+ if (!turnPrefixResult.ok)
489
+ return err(turnPrefixResult.error);
490
+ summary = `${historyResult.value}\n\n---\n\n**Turn Context (split turn):**\n\n${turnPrefixResult.value}`;
491
+ }
492
+ else {
493
+ const summaryResult = await generateSummary(messagesToSummarize, model, settings.reserveTokens, apiKey, headers, signal, customInstructions, previousSummary, thinkingLevel);
494
+ if (!summaryResult.ok)
495
+ return err(summaryResult.error);
496
+ summary = summaryResult.value;
497
+ }
498
+ const { readFiles, modifiedFiles } = computeFileLists(fileOps);
499
+ summary += formatFileOperations(readFiles, modifiedFiles);
500
+ return ok({
501
+ summary,
502
+ firstKeptEntryId,
503
+ tokensBefore,
504
+ details: { readFiles, modifiedFiles },
505
+ });
506
+ }
507
+ async function generateTurnPrefixSummary(messages, model, reserveTokens, apiKey, headers, signal, thinkingLevel) {
508
+ const maxTokens = Math.min(Math.floor(0.5 * reserveTokens), model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY);
509
+ const llmMessages = convertToLlm(messages);
510
+ const conversationText = serializeConversation(llmMessages);
511
+ const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`;
512
+ const summarizationMessages = [
513
+ {
514
+ role: "user",
515
+ content: [{ type: "text", text: promptText }],
516
+ timestamp: Date.now(),
517
+ },
518
+ ];
519
+ const response = await completeSimple(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, model.reasoning && thinkingLevel && thinkingLevel !== "off"
520
+ ? { maxTokens, signal, apiKey, headers, reasoning: thinkingLevel }
521
+ : { maxTokens, signal, apiKey, headers });
522
+ if (response.stopReason === "aborted") {
523
+ return err(new CompactionError("aborted", response.errorMessage || "Turn prefix summarization aborted"));
524
+ }
525
+ if (response.stopReason === "error") {
526
+ return err(new CompactionError("summarization_failed", `Turn prefix summarization failed: ${response.errorMessage || "Unknown error"}`));
527
+ }
528
+ return ok(response.content
529
+ .filter((c) => c.type === "text")
530
+ .map((c) => c.text)
531
+ .join("\n"));
532
+ }
@@ -0,0 +1,130 @@
1
+ /** Create an empty file-operation accumulator. */
2
+ export function createFileOps() {
3
+ return {
4
+ read: new Set(),
5
+ written: new Set(),
6
+ edited: new Set(),
7
+ };
8
+ }
9
+ /** Add file operations from assistant tool calls to an accumulator. */
10
+ export function extractFileOpsFromMessage(message, fileOps) {
11
+ if (message.role !== "assistant")
12
+ return;
13
+ if (!("content" in message) || !Array.isArray(message.content))
14
+ return;
15
+ for (const block of message.content) {
16
+ if (typeof block !== "object" || block === null)
17
+ continue;
18
+ if (!("type" in block) || block.type !== "toolCall")
19
+ continue;
20
+ if (!("arguments" in block) || !("name" in block))
21
+ continue;
22
+ const args = block.arguments;
23
+ if (!args)
24
+ continue;
25
+ const path = typeof args.path === "string" ? args.path : undefined;
26
+ if (!path)
27
+ continue;
28
+ switch (block.name) {
29
+ case "read":
30
+ fileOps.read.add(path);
31
+ break;
32
+ case "write":
33
+ fileOps.written.add(path);
34
+ break;
35
+ case "edit":
36
+ fileOps.edited.add(path);
37
+ break;
38
+ }
39
+ }
40
+ }
41
+ /** Compute sorted read-only and modified file lists from accumulated operations. */
42
+ export function computeFileLists(fileOps) {
43
+ const modified = new Set([...fileOps.edited, ...fileOps.written]);
44
+ const readOnly = [...fileOps.read].filter((f) => !modified.has(f)).sort();
45
+ const modifiedFiles = [...modified].sort();
46
+ return { readFiles: readOnly, modifiedFiles };
47
+ }
48
+ /** Format file lists as summary metadata tags. */
49
+ export function formatFileOperations(readFiles, modifiedFiles) {
50
+ const sections = [];
51
+ if (readFiles.length > 0) {
52
+ sections.push(`<read-files>\n${readFiles.join("\n")}\n</read-files>`);
53
+ }
54
+ if (modifiedFiles.length > 0) {
55
+ sections.push(`<modified-files>\n${modifiedFiles.join("\n")}\n</modified-files>`);
56
+ }
57
+ if (sections.length === 0)
58
+ return "";
59
+ return `\n\n${sections.join("\n\n")}`;
60
+ }
61
+ const TOOL_RESULT_MAX_CHARS = 2000;
62
+ function safeJsonStringify(value) {
63
+ try {
64
+ return JSON.stringify(value) ?? "undefined";
65
+ }
66
+ catch {
67
+ return "[unserializable]";
68
+ }
69
+ }
70
+ function truncateForSummary(text, maxChars) {
71
+ if (text.length <= maxChars)
72
+ return text;
73
+ const truncatedChars = text.length - maxChars;
74
+ return `${text.slice(0, maxChars)}\n\n[... ${truncatedChars} more characters truncated]`;
75
+ }
76
+ /** Serialize LLM messages to plain text for summarization prompts. */
77
+ export function serializeConversation(messages) {
78
+ const parts = [];
79
+ for (const msg of messages) {
80
+ if (msg.role === "user") {
81
+ const content = typeof msg.content === "string"
82
+ ? msg.content
83
+ : msg.content
84
+ .filter((c) => c.type === "text")
85
+ .map((c) => c.text)
86
+ .join("");
87
+ if (content)
88
+ parts.push(`[User]: ${content}`);
89
+ }
90
+ else if (msg.role === "assistant") {
91
+ const textParts = [];
92
+ const thinkingParts = [];
93
+ const toolCalls = [];
94
+ for (const block of msg.content) {
95
+ if (block.type === "text") {
96
+ textParts.push(block.text);
97
+ }
98
+ else if (block.type === "thinking") {
99
+ thinkingParts.push(block.thinking);
100
+ }
101
+ else if (block.type === "toolCall") {
102
+ const args = block.arguments;
103
+ const argsStr = Object.entries(args)
104
+ .map(([k, v]) => `${k}=${safeJsonStringify(v)}`)
105
+ .join(", ");
106
+ toolCalls.push(`${block.name}(${argsStr})`);
107
+ }
108
+ }
109
+ if (thinkingParts.length > 0) {
110
+ parts.push(`[Assistant thinking]: ${thinkingParts.join("\n")}`);
111
+ }
112
+ if (textParts.length > 0) {
113
+ parts.push(`[Assistant]: ${textParts.join("\n")}`);
114
+ }
115
+ if (toolCalls.length > 0) {
116
+ parts.push(`[Assistant tool calls]: ${toolCalls.join("; ")}`);
117
+ }
118
+ }
119
+ else if (msg.role === "toolResult") {
120
+ const content = msg.content
121
+ .filter((c) => c.type === "text")
122
+ .map((c) => c.text)
123
+ .join("");
124
+ if (content) {
125
+ parts.push(`[Tool result]: ${truncateForSummary(content, TOOL_RESULT_MAX_CHARS)}`);
126
+ }
127
+ }
128
+ }
129
+ return parts.join("\n\n");
130
+ }