@aexol/spectral 0.9.230 → 0.9.231
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/dist/designer/data/skills/blog-post.d.ts +3 -0
- package/dist/designer/data/skills/blog-post.d.ts.map +1 -0
- package/dist/designer/data/skills/blog-post.js +84 -0
- package/dist/designer/data/skills/blog-post.ts +87 -0
- package/dist/designer/data/skills/critique.d.ts +3 -0
- package/dist/designer/data/skills/critique.d.ts.map +1 -0
- package/dist/designer/data/skills/critique.js +109 -0
- package/dist/designer/data/skills/critique.ts +112 -0
- package/dist/designer/data/skills/dashboard.d.ts +3 -0
- package/dist/designer/data/skills/dashboard.d.ts.map +1 -0
- package/dist/designer/data/skills/dashboard.js +165 -0
- package/dist/designer/data/skills/dashboard.ts +168 -0
- package/dist/designer/data/skills/design-brief.d.ts +3 -0
- package/dist/designer/data/skills/design-brief.d.ts.map +1 -0
- package/dist/designer/data/skills/design-brief.js +141 -0
- package/dist/designer/data/skills/design-brief.ts +144 -0
- package/dist/designer/data/skills/design-skill.d.ts +10 -0
- package/dist/designer/data/skills/design-skill.d.ts.map +1 -0
- package/dist/designer/data/skills/design-skill.js +1 -0
- package/dist/designer/data/skills/design-skill.ts +9 -0
- package/dist/designer/data/skills/docs-page.d.ts +3 -0
- package/dist/designer/data/skills/docs-page.d.ts.map +1 -0
- package/dist/designer/data/skills/docs-page.js +68 -0
- package/dist/designer/data/skills/docs-page.ts +71 -0
- package/dist/designer/data/skills/index.d.ts +3 -0
- package/dist/designer/data/skills/index.d.ts.map +1 -0
- package/dist/designer/data/skills/index.js +28 -0
- package/dist/designer/data/skills/index.ts +30 -0
- package/dist/designer/data/skills/magazine-poster.d.ts +3 -0
- package/dist/designer/data/skills/magazine-poster.d.ts.map +1 -0
- package/dist/designer/data/skills/magazine-poster.js +70 -0
- package/dist/designer/data/skills/magazine-poster.ts +73 -0
- package/dist/designer/data/skills/mobile-app.d.ts +3 -0
- package/dist/designer/data/skills/mobile-app.d.ts.map +1 -0
- package/dist/designer/data/skills/mobile-app.js +229 -0
- package/dist/designer/data/skills/mobile-app.ts +232 -0
- package/dist/designer/data/skills/motion-frames.d.ts +3 -0
- package/dist/designer/data/skills/motion-frames.d.ts.map +1 -0
- package/dist/designer/data/skills/motion-frames.js +121 -0
- package/dist/designer/data/skills/motion-frames.ts +124 -0
- package/dist/designer/data/skills/pricing-page.d.ts +3 -0
- package/dist/designer/data/skills/pricing-page.d.ts.map +1 -0
- package/dist/designer/data/skills/pricing-page.js +76 -0
- package/dist/designer/data/skills/pricing-page.ts +79 -0
- package/dist/designer/data/skills/saas-landing.d.ts +3 -0
- package/dist/designer/data/skills/saas-landing.d.ts.map +1 -0
- package/dist/designer/data/skills/saas-landing.js +71 -0
- package/dist/designer/data/skills/saas-landing.ts +74 -0
- package/dist/designer/data/skills/slide-deck.d.ts +3 -0
- package/dist/designer/data/skills/slide-deck.d.ts.map +1 -0
- package/dist/designer/data/skills/slide-deck.js +213 -0
- package/dist/designer/data/skills/slide-deck.ts +216 -0
- package/dist/designer/data/skills/social-carousel.d.ts +3 -0
- package/dist/designer/data/skills/social-carousel.d.ts.map +1 -0
- package/dist/designer/data/skills/social-carousel.js +135 -0
- package/dist/designer/data/skills/social-carousel.ts +138 -0
- package/dist/designer/data/skills/web-prototype.d.ts +3 -0
- package/dist/designer/data/skills/web-prototype.d.ts.map +1 -0
- package/dist/designer/data/skills/web-prototype.js +159 -0
- package/dist/designer/data/skills/web-prototype.ts +162 -0
- package/dist/designer/skills.d.ts +4 -10
- package/dist/designer/skills.d.ts.map +1 -1
- package/dist/designer/skills.js +2 -1643
- package/dist/extensions/wizard/index.js +9 -1
- package/dist/generated/parser/ast/nodes.d.ts +115 -0
- package/dist/generated/parser/ast/nodes.d.ts.map +1 -0
- package/dist/generated/parser/ast/nodes.js +8 -0
- package/dist/generated/parser/lexer/lexer.d.ts +28 -0
- package/dist/generated/parser/lexer/lexer.d.ts.map +1 -0
- package/dist/generated/parser/{lexer.js → lexer/lexer.js} +1 -180
- package/dist/generated/parser/{lexer.d.ts → lexer/tokens.d.ts} +5 -24
- package/dist/generated/parser/lexer/tokens.d.ts.map +1 -0
- package/dist/generated/parser/lexer/tokens.js +187 -0
- package/dist/generated/parser/mod.d.ts +5 -7
- package/dist/generated/parser/mod.d.ts.map +1 -1
- package/dist/generated/parser/mod.js +2 -1
- package/dist/generated/parser/parser/cursor.d.ts +47 -0
- package/dist/generated/parser/parser/cursor.d.ts.map +1 -0
- package/dist/generated/parser/parser/cursor.js +12 -0
- package/dist/generated/parser/parser/definitions.d.ts +16 -0
- package/dist/generated/parser/parser/definitions.d.ts.map +1 -0
- package/dist/generated/parser/parser/definitions.js +353 -0
- package/dist/generated/parser/parser/expressions.d.ts +17 -0
- package/dist/generated/parser/parser/expressions.d.ts.map +1 -0
- package/dist/generated/parser/parser/expressions.js +165 -0
- package/dist/generated/parser/parser.d.ts +18 -133
- package/dist/generated/parser/parser.d.ts.map +1 -1
- package/dist/generated/parser/parser.js +9 -505
- package/dist/generated/parser/tokenizer.d.ts +1 -2
- package/dist/generated/parser/tokenizer.d.ts.map +1 -1
- package/dist/generated/parser/tokenizer.js +2 -1
- package/dist/generated/parser/validator.d.ts +1 -1
- package/dist/generated/parser/validator.d.ts.map +1 -1
- package/dist/mcp/glimpse-ui.js +9 -1
- package/dist/relay/dispatcher.d.ts +11 -104
- package/dist/relay/dispatcher.d.ts.map +1 -1
- package/dist/relay/dispatcher.js +10 -1130
- package/dist/relay/rest.d.ts +31 -0
- package/dist/relay/rest.d.ts.map +1 -0
- package/dist/relay/rest.js +85 -0
- package/dist/relay/routing.d.ts +109 -0
- package/dist/relay/routing.d.ts.map +1 -0
- package/dist/relay/routing.js +669 -0
- package/dist/relay/wizard-login.d.ts +17 -0
- package/dist/relay/wizard-login.d.ts.map +1 -0
- package/dist/relay/wizard-login.js +419 -0
- package/dist/sdk/agent-core/agent-loop.d.ts +2 -9
- package/dist/sdk/agent-core/agent-loop.d.ts.map +1 -1
- package/dist/sdk/agent-core/agent-loop.js +4 -288
- package/dist/sdk/agent-core/tool-execution.d.ts +33 -0
- package/dist/sdk/agent-core/tool-execution.d.ts.map +1 -0
- package/dist/sdk/agent-core/tool-execution.js +300 -0
- package/dist/sdk/ai/env-api-keys.js +9 -1
- package/dist/sdk/ai/providers/openai/reasoning-replay.d.ts +48 -0
- package/dist/sdk/ai/providers/openai/reasoning-replay.d.ts.map +1 -0
- package/dist/sdk/ai/providers/openai/reasoning-replay.js +209 -0
- package/dist/sdk/ai/providers/openai-completions.d.ts.map +1 -1
- package/dist/sdk/ai/providers/openai-completions.js +1 -203
- package/dist/sdk/coding-agent/core/compaction/compaction.d.ts +6 -118
- package/dist/sdk/coding-agent/core/compaction/compaction.d.ts.map +1 -1
- package/dist/sdk/coding-agent/core/compaction/compaction.js +11 -733
- package/dist/sdk/coding-agent/core/compaction/cut-points.d.ts +58 -0
- package/dist/sdk/coding-agent/core/compaction/cut-points.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/compaction/cut-points.js +191 -0
- package/dist/sdk/coding-agent/core/compaction/dedupe.d.ts +28 -0
- package/dist/sdk/coding-agent/core/compaction/dedupe.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/compaction/dedupe.js +156 -0
- package/dist/sdk/coding-agent/core/compaction/summarize.d.ts +21 -0
- package/dist/sdk/coding-agent/core/compaction/summarize.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/compaction/summarize.js +181 -0
- package/dist/sdk/coding-agent/core/compaction/tokens.d.ts +47 -0
- package/dist/sdk/coding-agent/core/compaction/tokens.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/compaction/tokens.js +224 -0
- package/dist/sdk/coding-agent/core/extensions/session-events.d.ts +157 -0
- package/dist/sdk/coding-agent/core/extensions/session-events.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/extensions/session-events.js +13 -0
- package/dist/sdk/coding-agent/core/extensions/types.d.ts +6 -365
- package/dist/sdk/coding-agent/core/extensions/types.d.ts.map +1 -1
- package/dist/sdk/coding-agent/core/extensions/types.js +2 -10
- package/dist/sdk/coding-agent/core/extensions/ui-types.d.ts +219 -0
- package/dist/sdk/coding-agent/core/extensions/ui-types.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/extensions/ui-types.js +4 -0
- package/dist/sdk/coding-agent/core/package/default-package-manager.d.ts +115 -0
- package/dist/sdk/coding-agent/core/package/default-package-manager.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/package/default-package-manager.js +1184 -0
- package/dist/sdk/coding-agent/core/package/discovery.d.ts +20 -0
- package/dist/sdk/coding-agent/core/package/discovery.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/package/discovery.js +283 -0
- package/dist/sdk/coding-agent/core/package/ignore.d.ts +29 -0
- package/dist/sdk/coding-agent/core/package/ignore.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/package/ignore.js +235 -0
- package/dist/sdk/coding-agent/core/package/types.d.ts +128 -0
- package/dist/sdk/coding-agent/core/package/types.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/package/types.js +18 -0
- package/dist/sdk/coding-agent/core/package/update-check.d.ts +43 -0
- package/dist/sdk/coding-agent/core/package/update-check.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/package/update-check.js +253 -0
- package/dist/sdk/coding-agent/core/package-manager.d.ts +2 -188
- package/dist/sdk/coding-agent/core/package-manager.d.ts.map +1 -1
- package/dist/sdk/coding-agent/core/package-manager.js +1 -1940
- package/dist/sdk/coding-agent/core/session/migrate.d.ts +18 -0
- package/dist/sdk/coding-agent/core/session/migrate.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/session/migrate.js +78 -0
- package/dist/sdk/coding-agent/core/session/types.d.ts +165 -0
- package/dist/sdk/coding-agent/core/session/types.d.ts.map +1 -0
- package/dist/sdk/coding-agent/core/session/types.js +4 -0
- package/dist/sdk/coding-agent/core/session-manager.d.ts +3 -161
- package/dist/sdk/coding-agent/core/session-manager.d.ts.map +1 -1
- package/dist/sdk/coding-agent/core/session-manager.js +4 -72
- package/package.json +2 -2
- package/dist/generated/parser/lexer.d.ts.map +0 -1
|
@@ -4,11 +4,18 @@
|
|
|
4
4
|
* Pure functions for compaction logic. The session manager handles I/O,
|
|
5
5
|
* and after compaction the session is reloaded.
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
8
|
-
import { convertToLlm, createBranchSummaryMessage, createCompactionSummaryMessage, createCustomMessage, } from "../messages.js";
|
|
7
|
+
import { createBranchSummaryMessage, createCompactionSummaryMessage, createCustomMessage, } from "../messages.js";
|
|
9
8
|
import { buildSessionContext } from "../session-manager.js";
|
|
10
|
-
import { computeFileLists, createFileOps, extractFileOpsFromMessage, formatFileOperations,
|
|
11
|
-
import {
|
|
9
|
+
import { computeFileLists, createFileOps, extractFileOpsFromMessage, formatFileOperations, } from "./utils.js";
|
|
10
|
+
import { DEFAULT_COMPACTION_POLICY, normalizeManualKeepRecentTokens, } from "./policy.js";
|
|
11
|
+
import { estimateCompactionTokenMetrics, estimateContextTokens, } from "./tokens.js";
|
|
12
|
+
import { findCutPoint } from "./cut-points.js";
|
|
13
|
+
import { generateSummary, generateTurnPrefixSummary } from "./summarize.js";
|
|
14
|
+
import { deduplicateToolCalls } from "./dedupe.js";
|
|
15
|
+
export { calculateContextTokens, estimateCompactionTokenMetrics, estimateContextTokens, estimateTextTokens, estimateTokens, getLastAssistantUsage, shouldCompact, } from "./tokens.js";
|
|
16
|
+
export { findCutPoint, findTurnStartIndex, } from "./cut-points.js";
|
|
17
|
+
export { generateSummary } from "./summarize.js";
|
|
18
|
+
export { deduplicateToolCalls } from "./dedupe.js";
|
|
12
19
|
/**
|
|
13
20
|
* Extract file operations from messages and previous compaction entries.
|
|
14
21
|
*/
|
|
@@ -79,481 +86,6 @@ export function getManualCompactionSettings(settings, manualPolicy = DEFAULT_COM
|
|
|
79
86
|
return settings;
|
|
80
87
|
return { ...settings, keepRecentTokens: manualKeepRecentTokens };
|
|
81
88
|
}
|
|
82
|
-
// ============================================================================
|
|
83
|
-
// Token calculation
|
|
84
|
-
// ============================================================================
|
|
85
|
-
/**
|
|
86
|
-
* Calculate total context tokens from usage.
|
|
87
|
-
* Uses the native totalTokens field when available, falls back to computing from components.
|
|
88
|
-
*/
|
|
89
|
-
export function calculateContextTokens(usage) {
|
|
90
|
-
return usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Get usage from an assistant message if available.
|
|
94
|
-
* Skips aborted and error messages as they don't have valid usage data.
|
|
95
|
-
*/
|
|
96
|
-
function getAssistantUsage(msg) {
|
|
97
|
-
if (msg.role === "assistant" && "usage" in msg) {
|
|
98
|
-
const assistantMsg = msg;
|
|
99
|
-
if (assistantMsg.stopReason !== "aborted" && assistantMsg.stopReason !== "error" && assistantMsg.usage) {
|
|
100
|
-
return assistantMsg.usage;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Find the last non-aborted assistant message usage from session entries.
|
|
107
|
-
*/
|
|
108
|
-
export function getLastAssistantUsage(entries) {
|
|
109
|
-
for (let i = entries.length - 1; i >= 0; i--) {
|
|
110
|
-
const entry = entries[i];
|
|
111
|
-
if (entry.type === "message") {
|
|
112
|
-
const usage = getAssistantUsage(entry.message);
|
|
113
|
-
if (usage)
|
|
114
|
-
return usage;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return undefined;
|
|
118
|
-
}
|
|
119
|
-
function getLastAssistantUsageInfo(messages) {
|
|
120
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
121
|
-
const usage = getAssistantUsage(messages[i]);
|
|
122
|
-
if (usage)
|
|
123
|
-
return { usage, index: i };
|
|
124
|
-
}
|
|
125
|
-
return undefined;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Estimate context tokens from messages, using the last assistant usage when available.
|
|
129
|
-
* If there are messages after the last usage, estimate their tokens with estimateTokens.
|
|
130
|
-
*/
|
|
131
|
-
export function estimateContextTokens(messages) {
|
|
132
|
-
const usageInfo = getLastAssistantUsageInfo(messages);
|
|
133
|
-
if (!usageInfo) {
|
|
134
|
-
let estimated = 0;
|
|
135
|
-
for (const message of messages) {
|
|
136
|
-
estimated += estimateTokens(message);
|
|
137
|
-
}
|
|
138
|
-
return {
|
|
139
|
-
tokens: estimated,
|
|
140
|
-
usageTokens: 0,
|
|
141
|
-
trailingTokens: estimated,
|
|
142
|
-
lastUsageIndex: null,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
const usageTokens = calculateContextTokens(usageInfo.usage);
|
|
146
|
-
let trailingTokens = 0;
|
|
147
|
-
for (let i = usageInfo.index + 1; i < messages.length; i++) {
|
|
148
|
-
trailingTokens += estimateTokens(messages[i]);
|
|
149
|
-
}
|
|
150
|
-
return {
|
|
151
|
-
tokens: usageTokens + trailingTokens,
|
|
152
|
-
usageTokens,
|
|
153
|
-
trailingTokens,
|
|
154
|
-
lastUsageIndex: usageInfo.index,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Check if compaction should trigger based on context usage.
|
|
159
|
-
*/
|
|
160
|
-
export function shouldCompact(contextTokens, contextWindow, settings) {
|
|
161
|
-
if (!settings.enabled)
|
|
162
|
-
return false;
|
|
163
|
-
if (!Number.isFinite(contextWindow) || contextWindow <= 0)
|
|
164
|
-
return false;
|
|
165
|
-
if (!Number.isFinite(contextTokens) || contextTokens < 0)
|
|
166
|
-
return false;
|
|
167
|
-
return contextTokens >= contextWindow * CONTEXT_LIMIT_RATIO;
|
|
168
|
-
}
|
|
169
|
-
// ============================================================================
|
|
170
|
-
// Cut point detection
|
|
171
|
-
// ============================================================================
|
|
172
|
-
/**
|
|
173
|
-
* Estimate token count for a message using chars/4 heuristic.
|
|
174
|
-
* This is conservative (overestimates tokens).
|
|
175
|
-
*/
|
|
176
|
-
export function estimateTokens(message) {
|
|
177
|
-
let chars = 0;
|
|
178
|
-
switch (message.role) {
|
|
179
|
-
case "user": {
|
|
180
|
-
const content = message.content;
|
|
181
|
-
if (typeof content === "string") {
|
|
182
|
-
chars = content.length;
|
|
183
|
-
}
|
|
184
|
-
else if (Array.isArray(content)) {
|
|
185
|
-
for (const block of content) {
|
|
186
|
-
if (block.type === "text" && block.text) {
|
|
187
|
-
chars += block.text.length;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return Math.ceil(chars / 4);
|
|
192
|
-
}
|
|
193
|
-
case "assistant": {
|
|
194
|
-
const assistant = message;
|
|
195
|
-
for (const block of assistant.content) {
|
|
196
|
-
if (block.type === "text") {
|
|
197
|
-
chars += block.text.length;
|
|
198
|
-
}
|
|
199
|
-
else if (block.type === "thinking") {
|
|
200
|
-
chars += block.thinking.length;
|
|
201
|
-
}
|
|
202
|
-
else if (block.type === "toolCall") {
|
|
203
|
-
chars += block.name.length + JSON.stringify(block.arguments).length;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return Math.ceil(chars / 4);
|
|
207
|
-
}
|
|
208
|
-
case "custom":
|
|
209
|
-
case "toolResult": {
|
|
210
|
-
if (typeof message.content === "string") {
|
|
211
|
-
chars = message.content.length;
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
for (const block of message.content) {
|
|
215
|
-
if (block.type === "text" && block.text) {
|
|
216
|
-
chars += block.text.length;
|
|
217
|
-
}
|
|
218
|
-
if (block.type === "image") {
|
|
219
|
-
chars += 4800; // Estimate images as 4000 chars, or 1200 tokens
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
return Math.ceil(chars / 4);
|
|
224
|
-
}
|
|
225
|
-
case "bashExecution": {
|
|
226
|
-
chars = message.command.length + message.output.length;
|
|
227
|
-
return Math.ceil(chars / 4);
|
|
228
|
-
}
|
|
229
|
-
case "branchSummary":
|
|
230
|
-
case "compactionSummary": {
|
|
231
|
-
chars = message.summary.length;
|
|
232
|
-
return Math.ceil(chars / 4);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return 0;
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Find valid cut points: indices of user, assistant, custom, or bashExecution messages.
|
|
239
|
-
* Never cut at tool results (they must follow their tool call).
|
|
240
|
-
* When we cut at an assistant message with tool calls, its tool results follow it
|
|
241
|
-
* and will be kept.
|
|
242
|
-
* BashExecutionMessage is treated like a user message (user-initiated context).
|
|
243
|
-
*/
|
|
244
|
-
function findValidCutPoints(entries, startIndex, endIndex) {
|
|
245
|
-
const cutPoints = [];
|
|
246
|
-
for (let i = startIndex; i < endIndex; i++) {
|
|
247
|
-
const entry = entries[i];
|
|
248
|
-
switch (entry.type) {
|
|
249
|
-
case "message": {
|
|
250
|
-
const role = entry.message.role;
|
|
251
|
-
switch (role) {
|
|
252
|
-
case "bashExecution":
|
|
253
|
-
case "custom":
|
|
254
|
-
case "branchSummary":
|
|
255
|
-
case "compactionSummary":
|
|
256
|
-
case "user":
|
|
257
|
-
case "assistant":
|
|
258
|
-
cutPoints.push(i);
|
|
259
|
-
break;
|
|
260
|
-
case "toolResult":
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
break;
|
|
264
|
-
}
|
|
265
|
-
case "thinking_level_change":
|
|
266
|
-
case "model_change":
|
|
267
|
-
case "compaction":
|
|
268
|
-
case "branch_summary":
|
|
269
|
-
case "custom":
|
|
270
|
-
case "custom_message":
|
|
271
|
-
case "label":
|
|
272
|
-
case "session_info":
|
|
273
|
-
break;
|
|
274
|
-
}
|
|
275
|
-
// branch_summary and custom_message are user-role messages, valid cut points
|
|
276
|
-
if (entry.type === "branch_summary" || entry.type === "custom_message") {
|
|
277
|
-
cutPoints.push(i);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return cutPoints;
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Find the user message (or bashExecution) that starts the turn containing the given entry index.
|
|
284
|
-
* Returns -1 if no turn start found before the index.
|
|
285
|
-
* BashExecutionMessage is treated like a user message for turn boundaries.
|
|
286
|
-
*/
|
|
287
|
-
export function findTurnStartIndex(entries, entryIndex, startIndex) {
|
|
288
|
-
for (let i = entryIndex; i >= startIndex; i--) {
|
|
289
|
-
const entry = entries[i];
|
|
290
|
-
// branch_summary and custom_message are user-role messages, can start a turn
|
|
291
|
-
if (entry.type === "branch_summary" || entry.type === "custom_message") {
|
|
292
|
-
return i;
|
|
293
|
-
}
|
|
294
|
-
if (entry.type === "message") {
|
|
295
|
-
const role = entry.message.role;
|
|
296
|
-
if (role === "user" || role === "bashExecution") {
|
|
297
|
-
return i;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
return -1;
|
|
302
|
-
}
|
|
303
|
-
function isTurnBoundaryEntry(entry) {
|
|
304
|
-
if (!entry)
|
|
305
|
-
return false;
|
|
306
|
-
if (entry.type === "branch_summary" || entry.type === "custom_message")
|
|
307
|
-
return true;
|
|
308
|
-
if (entry.type !== "message")
|
|
309
|
-
return false;
|
|
310
|
-
const role = entry.message.role;
|
|
311
|
-
return role === "user" ||
|
|
312
|
-
role === "bashExecution" ||
|
|
313
|
-
role === "custom" ||
|
|
314
|
-
role === "branchSummary" ||
|
|
315
|
-
role === "compactionSummary";
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Find the cut point in session entries that keeps approximately `keepRecentTokens`.
|
|
319
|
-
*
|
|
320
|
-
* Algorithm: Walk backwards from newest, accumulating estimated message sizes.
|
|
321
|
-
* Stop when we've accumulated >= keepRecentTokens. Cut at that point.
|
|
322
|
-
*
|
|
323
|
-
* Can cut at user OR assistant messages (never tool results). When cutting at an
|
|
324
|
-
* assistant message with tool calls, its tool results come after and will be kept.
|
|
325
|
-
*
|
|
326
|
-
* Returns CutPointResult with:
|
|
327
|
-
* - firstKeptEntryIndex: the entry index to start keeping from
|
|
328
|
-
* - turnStartIndex: if cutting mid-turn, the user message that started that turn
|
|
329
|
-
* - isSplitTurn: whether we're cutting in the middle of a turn
|
|
330
|
-
*
|
|
331
|
-
* Only considers entries between `startIndex` and `endIndex` (exclusive).
|
|
332
|
-
*/
|
|
333
|
-
export function findCutPoint(entries, startIndex, endIndex, keepRecentTokens, options = {}) {
|
|
334
|
-
if (options.anchorEntryId) {
|
|
335
|
-
const anchorIndex = entries.findIndex((entry) => entry.id === options.anchorEntryId);
|
|
336
|
-
if (anchorIndex >= startIndex && anchorIndex < endIndex) {
|
|
337
|
-
return { firstKeptEntryIndex: anchorIndex, turnStartIndex: -1, isSplitTurn: false };
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
const cutPoints = findValidCutPoints(entries, startIndex, endIndex);
|
|
341
|
-
if (cutPoints.length === 0) {
|
|
342
|
-
return { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };
|
|
343
|
-
}
|
|
344
|
-
// Walk backwards from newest, accumulating estimated message sizes.
|
|
345
|
-
// Phase-boundary callers provide an explicit marker, so the newest valid
|
|
346
|
-
// boundary is the desired cut point and the usual recent-token suffix is not
|
|
347
|
-
// retained raw.
|
|
348
|
-
let accumulatedTokens = 0;
|
|
349
|
-
let cutIndex = options.phaseBoundary
|
|
350
|
-
? cutPoints[cutPoints.length - 1]
|
|
351
|
-
: cutPoints[0]; // Default: keep from first message (not header)
|
|
352
|
-
if (!options.phaseBoundary) {
|
|
353
|
-
for (let i = endIndex - 1; i >= startIndex; i--) {
|
|
354
|
-
const entry = entries[i];
|
|
355
|
-
if (entry.type !== "message")
|
|
356
|
-
continue;
|
|
357
|
-
// Estimate this message's size
|
|
358
|
-
const messageTokens = estimateTokens(entry.message);
|
|
359
|
-
accumulatedTokens += messageTokens;
|
|
360
|
-
// Check if we've exceeded the budget
|
|
361
|
-
if (accumulatedTokens >= keepRecentTokens) {
|
|
362
|
-
// Find the closest valid cut point at or after this entry
|
|
363
|
-
for (let c = 0; c < cutPoints.length; c++) {
|
|
364
|
-
if (cutPoints[c] >= i) {
|
|
365
|
-
cutIndex = cutPoints[c];
|
|
366
|
-
break;
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
// Scan backwards from cutIndex to include any non-message entries (bash, settings, etc.)
|
|
374
|
-
while (cutIndex > startIndex) {
|
|
375
|
-
const prevEntry = entries[cutIndex - 1];
|
|
376
|
-
// Stop at session header or compaction boundaries
|
|
377
|
-
if (prevEntry.type === "compaction") {
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
if (prevEntry.type === "message") {
|
|
381
|
-
// Stop if we hit any message
|
|
382
|
-
break;
|
|
383
|
-
}
|
|
384
|
-
// Include this non-message entry (bash, settings change, etc.)
|
|
385
|
-
cutIndex--;
|
|
386
|
-
}
|
|
387
|
-
// Pin latest user message: never fold the most recent user message into the
|
|
388
|
-
// compaction summary. If the computed cut point lands after the latest user
|
|
389
|
-
// message, pull it back so that message (and everything after) survives
|
|
390
|
-
// verbatim. This protects the current task/brief from truncation mid-run.
|
|
391
|
-
if (options.pinLatestUserMessage) {
|
|
392
|
-
let latestUserIndex = -1;
|
|
393
|
-
for (let i = endIndex - 1; i >= startIndex; i--) {
|
|
394
|
-
const entry = entries[i];
|
|
395
|
-
if (entry.type === "message" && entry.message.role === "user") {
|
|
396
|
-
latestUserIndex = i;
|
|
397
|
-
break;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
if (latestUserIndex >= 0 && cutIndex > latestUserIndex) {
|
|
401
|
-
cutIndex = latestUserIndex;
|
|
402
|
-
// Re-include any non-message entries (bash, settings, etc.) immediately
|
|
403
|
-
// before the pinned user message, so we don't lose adjacent context.
|
|
404
|
-
while (cutIndex > startIndex) {
|
|
405
|
-
const prevEntry = entries[cutIndex - 1];
|
|
406
|
-
if (prevEntry.type === "compaction" || prevEntry.type === "message")
|
|
407
|
-
break;
|
|
408
|
-
cutIndex--;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
// Determine if this is a split turn
|
|
413
|
-
const cutEntry = entries[cutIndex];
|
|
414
|
-
const isTurnBoundary = isTurnBoundaryEntry(cutEntry);
|
|
415
|
-
const turnStartIndex = isTurnBoundary ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);
|
|
416
|
-
return {
|
|
417
|
-
firstKeptEntryIndex: cutIndex,
|
|
418
|
-
turnStartIndex,
|
|
419
|
-
isSplitTurn: !isTurnBoundary && turnStartIndex !== -1,
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
// ============================================================================
|
|
423
|
-
// Summarization
|
|
424
|
-
// ============================================================================
|
|
425
|
-
// Legacy LLM summarization path. AgentSession now builds persisted compaction
|
|
426
|
-
// entries with deterministic DCP-lite (`compactDcpLite`) and lets
|
|
427
|
-
// `session_before_compact` hooks provide memory details. Keep these exports for
|
|
428
|
-
// compatibility with external SDK callers and older harness code, but do not
|
|
429
|
-
// route new session compaction work through this model-generated fallback.
|
|
430
|
-
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.
|
|
431
|
-
|
|
432
|
-
Use this EXACT format:
|
|
433
|
-
|
|
434
|
-
## Goal
|
|
435
|
-
[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
|
|
436
|
-
|
|
437
|
-
## Constraints & Preferences
|
|
438
|
-
- [Any constraints, preferences, or requirements mentioned by user]
|
|
439
|
-
- [Or "(none)" if none were mentioned]
|
|
440
|
-
|
|
441
|
-
## Progress
|
|
442
|
-
### Done
|
|
443
|
-
- [x] [Completed tasks/changes]
|
|
444
|
-
|
|
445
|
-
### In Progress
|
|
446
|
-
- [ ] [Current work]
|
|
447
|
-
|
|
448
|
-
### Blocked
|
|
449
|
-
- [Issues preventing progress, if any]
|
|
450
|
-
|
|
451
|
-
## Key Decisions
|
|
452
|
-
- **[Decision]**: [Brief rationale]
|
|
453
|
-
|
|
454
|
-
## Next Steps
|
|
455
|
-
1. [Ordered list of what should happen next]
|
|
456
|
-
|
|
457
|
-
## Critical Context
|
|
458
|
-
- [Any data, examples, or references needed to continue]
|
|
459
|
-
- [Or "(none)" if not applicable]
|
|
460
|
-
|
|
461
|
-
Keep each section concise. Preserve exact file paths, function names, and error messages.`;
|
|
462
|
-
const UPDATE_SUMMARIZATION_PROMPT = `The messages above are NEW conversation messages to incorporate into the existing summary provided in <previous-summary> tags.
|
|
463
|
-
|
|
464
|
-
Update the existing structured summary with new information. RULES:
|
|
465
|
-
- PRESERVE all existing information from the previous summary
|
|
466
|
-
- ADD new progress, decisions, and context from the new messages
|
|
467
|
-
- UPDATE the Progress section: move items from "In Progress" to "Done" when completed
|
|
468
|
-
- UPDATE "Next Steps" based on what was accomplished
|
|
469
|
-
- PRESERVE exact file paths, function names, and error messages
|
|
470
|
-
- If something is no longer relevant, you may remove it
|
|
471
|
-
|
|
472
|
-
Use this EXACT format:
|
|
473
|
-
|
|
474
|
-
## Goal
|
|
475
|
-
[Preserve existing goals, add new ones if the task expanded]
|
|
476
|
-
|
|
477
|
-
## Constraints & Preferences
|
|
478
|
-
- [Preserve existing, add new ones discovered]
|
|
479
|
-
|
|
480
|
-
## Progress
|
|
481
|
-
### Done
|
|
482
|
-
- [x] [Include previously done items AND newly completed items]
|
|
483
|
-
|
|
484
|
-
### In Progress
|
|
485
|
-
- [ ] [Current work - update based on progress]
|
|
486
|
-
|
|
487
|
-
### Blocked
|
|
488
|
-
- [Current blockers - remove if resolved]
|
|
489
|
-
|
|
490
|
-
## Key Decisions
|
|
491
|
-
- **[Decision]**: [Brief rationale] (preserve all previous, add new)
|
|
492
|
-
|
|
493
|
-
## Next Steps
|
|
494
|
-
1. [Update based on current state]
|
|
495
|
-
|
|
496
|
-
## Critical Context
|
|
497
|
-
- [Preserve important context, add new if needed]
|
|
498
|
-
|
|
499
|
-
Keep each section concise. Preserve exact file paths, function names, and error messages.`;
|
|
500
|
-
function createSummarizationOptions(model, maxTokens, apiKey, headers, signal, thinkingLevel) {
|
|
501
|
-
const options = { maxTokens, signal, apiKey, headers };
|
|
502
|
-
if (model.reasoning && thinkingLevel && thinkingLevel !== "off") {
|
|
503
|
-
options.reasoning = thinkingLevel;
|
|
504
|
-
}
|
|
505
|
-
return options;
|
|
506
|
-
}
|
|
507
|
-
async function completeSummarization(model, context, options, streamFn) {
|
|
508
|
-
if (!streamFn) {
|
|
509
|
-
return completeSimple(model, context, options);
|
|
510
|
-
}
|
|
511
|
-
const stream = await streamFn(model, context, options);
|
|
512
|
-
return stream.result();
|
|
513
|
-
}
|
|
514
|
-
/**
|
|
515
|
-
* Generate a summary of the conversation using the LLM.
|
|
516
|
-
* If previousSummary is provided, uses the update prompt to merge.
|
|
517
|
-
*
|
|
518
|
-
* @deprecated The active coding-agent session path uses deterministic DCP-lite
|
|
519
|
-
* (`compactDcpLite`) plus `session_before_compact` hook details. This legacy
|
|
520
|
-
* LLM summarizer remains exported for compatibility with older callers only.
|
|
521
|
-
*/
|
|
522
|
-
export async function generateSummary(currentMessages, model, reserveTokens, apiKey, headers, signal, customInstructions, previousSummary, thinkingLevel, streamFn) {
|
|
523
|
-
const maxTokens = Math.min(Math.floor(0.8 * reserveTokens), model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY);
|
|
524
|
-
// Use update prompt if we have a previous summary, otherwise initial prompt
|
|
525
|
-
let basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;
|
|
526
|
-
if (customInstructions) {
|
|
527
|
-
basePrompt = `${basePrompt}\n\nAdditional focus: ${customInstructions}`;
|
|
528
|
-
}
|
|
529
|
-
// Serialize conversation to text so model doesn't try to continue it
|
|
530
|
-
// Convert to LLM messages first (handles custom types like bashExecution, custom, etc.)
|
|
531
|
-
const llmMessages = convertToLlm(currentMessages);
|
|
532
|
-
const conversationText = serializeConversation(llmMessages);
|
|
533
|
-
// Build the prompt with conversation wrapped in tags
|
|
534
|
-
let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
|
|
535
|
-
if (previousSummary) {
|
|
536
|
-
promptText += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
|
|
537
|
-
}
|
|
538
|
-
promptText += basePrompt;
|
|
539
|
-
const summarizationMessages = [
|
|
540
|
-
{
|
|
541
|
-
role: "user",
|
|
542
|
-
content: [{ type: "text", text: promptText }],
|
|
543
|
-
timestamp: Date.now(),
|
|
544
|
-
},
|
|
545
|
-
];
|
|
546
|
-
const completionOptions = createSummarizationOptions(model, maxTokens, apiKey, headers, signal, thinkingLevel);
|
|
547
|
-
const response = await completeSummarization(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, completionOptions, streamFn);
|
|
548
|
-
if (response.stopReason === "error") {
|
|
549
|
-
throw new Error(`Summarization failed: ${response.errorMessage || "Unknown error"}`);
|
|
550
|
-
}
|
|
551
|
-
const textContent = response.content
|
|
552
|
-
.filter((c) => c.type === "text")
|
|
553
|
-
.map((c) => c.text)
|
|
554
|
-
.join("\n");
|
|
555
|
-
return textContent;
|
|
556
|
-
}
|
|
557
89
|
export function prepareCompaction(pathEntries, settings, options = {}) {
|
|
558
90
|
if (pathEntries.length > 0 && pathEntries[pathEntries.length - 1].type === "compaction") {
|
|
559
91
|
return undefined;
|
|
@@ -630,239 +162,9 @@ export function prepareCompaction(pathEntries, settings, options = {}) {
|
|
|
630
162
|
compactionNumber,
|
|
631
163
|
};
|
|
632
164
|
}
|
|
633
|
-
export function estimateTextTokens(text) {
|
|
634
|
-
if (!text.trim())
|
|
635
|
-
return 0;
|
|
636
|
-
return Math.ceil(text.length / 4);
|
|
637
|
-
}
|
|
638
|
-
function estimateMessagesTokens(messages) {
|
|
639
|
-
let tokens = 0;
|
|
640
|
-
for (const message of messages) {
|
|
641
|
-
tokens += estimateTokens(message);
|
|
642
|
-
}
|
|
643
|
-
return tokens;
|
|
644
|
-
}
|
|
645
|
-
export function estimateCompactionTokenMetrics(preparation, summary) {
|
|
646
|
-
const tokensCompacted = estimateMessagesTokens(preparation.messagesToSummarize) +
|
|
647
|
-
estimateMessagesTokens(preparation.turnPrefixMessages);
|
|
648
|
-
const summaryTokens = estimateTextTokens(summary);
|
|
649
|
-
const tokensRemoved = Math.max(0, tokensCompacted - summaryTokens);
|
|
650
|
-
const reductionPercent = tokensCompacted > 0
|
|
651
|
-
? Math.round((tokensRemoved / tokensCompacted) * 10_000) / 100
|
|
652
|
-
: 0;
|
|
653
|
-
return {
|
|
654
|
-
tokensCompacted,
|
|
655
|
-
tokensRemoved,
|
|
656
|
-
summaryTokens,
|
|
657
|
-
reductionPercent,
|
|
658
|
-
compactionNumber: preparation.compactionNumber,
|
|
659
|
-
largestTokenSources: collectLargestTokenSources(preparation.messagesToSummarize, preparation.turnPrefixMessages),
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
|
-
function addBucket(buckets, bucket) {
|
|
663
|
-
const key = `${bucket.type}:${bucket.name}`;
|
|
664
|
-
const previous = buckets.get(key);
|
|
665
|
-
buckets.set(key, {
|
|
666
|
-
...bucket,
|
|
667
|
-
tokens: (previous?.tokens ?? 0) + bucket.tokens,
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
function collectLargestTokenSources(messagesToSummarize, turnPrefixMessages) {
|
|
671
|
-
const buckets = new Map();
|
|
672
|
-
const collect = (messages, source) => {
|
|
673
|
-
for (const message of messages) {
|
|
674
|
-
const messageTokens = estimateTokens(message);
|
|
675
|
-
addBucket(buckets, { type: "source", name: source, tokens: messageTokens });
|
|
676
|
-
addBucket(buckets, { type: "role", name: message.role, tokens: messageTokens });
|
|
677
|
-
if (message.role === "toolResult") {
|
|
678
|
-
addBucket(buckets, { type: "tool", name: message.toolName, tokens: messageTokens });
|
|
679
|
-
continue;
|
|
680
|
-
}
|
|
681
|
-
if (message.role === "assistant") {
|
|
682
|
-
for (const block of message.content) {
|
|
683
|
-
if (block.type !== "toolCall")
|
|
684
|
-
continue;
|
|
685
|
-
const toolTokens = estimateTextTokens(`${block.name} ${JSON.stringify(block.arguments)}`);
|
|
686
|
-
addBucket(buckets, { type: "tool", name: block.name, tokens: toolTokens });
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
};
|
|
691
|
-
collect(messagesToSummarize, "history");
|
|
692
|
-
collect(turnPrefixMessages, "turn_prefix");
|
|
693
|
-
return [...buckets.values()]
|
|
694
|
-
.filter((bucket) => bucket.tokens > 0)
|
|
695
|
-
.sort((a, b) => b.tokens - a.tokens || a.type.localeCompare(b.type) || a.name.localeCompare(b.name))
|
|
696
|
-
.slice(0, 5);
|
|
697
|
-
}
|
|
698
|
-
// ============================================================================
|
|
699
|
-
// Tool Call Deduplication
|
|
700
|
-
// ============================================================================
|
|
701
|
-
/**
|
|
702
|
-
* Tools that always return the same result for the same arguments.
|
|
703
|
-
* Same (name, args) from any point in the session = duplicate.
|
|
704
|
-
* Only the most recent call is kept.
|
|
705
|
-
*/
|
|
706
|
-
const IDEMPOTENT_READ_TOOLS = new Set(["read"]);
|
|
707
|
-
/**
|
|
708
|
-
* Tools whose output may differ between calls with the same arguments.
|
|
709
|
-
* Deduplication requires comparing actual outputs to determine equivalence.
|
|
710
|
-
*/
|
|
711
|
-
const OUTPUT_DEPENDENT_TOOLS = new Set(["bash"]);
|
|
712
|
-
/**
|
|
713
|
-
* Tools that mutate state. Never deduplicated — chronological ordering matters.
|
|
714
|
-
*/
|
|
715
|
-
const MUTABLE_TOOLS = new Set(["edit", "write"]);
|
|
716
|
-
/**
|
|
717
|
-
* Build a stable string key from a Record's sorted keys.
|
|
718
|
-
* Ensures {a:1, b:2} and {b:2, a:1} produce the same key.
|
|
719
|
-
*/
|
|
720
|
-
function stableArgs(args) {
|
|
721
|
-
const sortedKeys = Object.keys(args).sort();
|
|
722
|
-
const sorted = {};
|
|
723
|
-
for (const key of sortedKeys) {
|
|
724
|
-
sorted[key] = args[key];
|
|
725
|
-
}
|
|
726
|
-
return JSON.stringify(sorted);
|
|
727
|
-
}
|
|
728
|
-
/**
|
|
729
|
-
* Compute the deduplication key for a tool call.
|
|
730
|
-
* - Idempotent-read tools: keyed by (name, args) — same args always same result
|
|
731
|
-
* - Output-dependent tools: keyed by (name, args, output) — output comparison needed
|
|
732
|
-
*/
|
|
733
|
-
function toolCallDedupKey(toolName, args, toolCallId, resultMap) {
|
|
734
|
-
const argsKey = stableArgs(args);
|
|
735
|
-
if (OUTPUT_DEPENDENT_TOOLS.has(toolName)) {
|
|
736
|
-
const output = resultMap.get(toolCallId) ?? "";
|
|
737
|
-
return `${toolName}:${argsKey}:${output}`;
|
|
738
|
-
}
|
|
739
|
-
return `${toolName}:${argsKey}`;
|
|
740
|
-
}
|
|
741
|
-
/**
|
|
742
|
-
* Check whether a tool should be excluded from deduplication.
|
|
743
|
-
* Mutating tools and unknown tools are never deduplicated (conservative).
|
|
744
|
-
*/
|
|
745
|
-
function isMutableOrUnknownTool(toolName) {
|
|
746
|
-
if (MUTABLE_TOOLS.has(toolName))
|
|
747
|
-
return true;
|
|
748
|
-
if (IDEMPOTENT_READ_TOOLS.has(toolName))
|
|
749
|
-
return false;
|
|
750
|
-
if (OUTPUT_DEPENDENT_TOOLS.has(toolName))
|
|
751
|
-
return false;
|
|
752
|
-
// Unknown tools: conservative — never deduplicate
|
|
753
|
-
return true;
|
|
754
|
-
}
|
|
755
|
-
/**
|
|
756
|
-
* Deduplicate repeated tool calls in a message array by keeping only the
|
|
757
|
-
* most recent occurrence of each (tool, args) pair.
|
|
758
|
-
*
|
|
759
|
-
* Strategy:
|
|
760
|
-
* - **Idempotent-read tools** (read, grep, glob): same (name, args) → keep last only.
|
|
761
|
-
* These tools return the same content for the same arguments.
|
|
762
|
-
* - **Output-dependent tools** (bash): same (name, args, output) → keep last only.
|
|
763
|
-
* Two bash calls with same command but different output are NOT duplicates.
|
|
764
|
-
* - **Mutating tools** (edit, write): never deduplicated. Chronological ordering
|
|
765
|
-
* of mutations matters for correctness.
|
|
766
|
-
* - **Unknown tools** (extensions, MCP, custom): never deduplicated. Conservative
|
|
767
|
-
* by default — only tools in the known sets above participate.
|
|
768
|
-
*
|
|
769
|
-
* Deduplication removes both the ToolCall block from the assistant message
|
|
770
|
-
* and the corresponding ToolResultMessage from the array.
|
|
771
|
-
*
|
|
772
|
-
* Recalculated each time compaction runs — prompt cache is only impacted
|
|
773
|
-
* alongside compression, not on every turn.
|
|
774
|
-
*/
|
|
775
|
-
export function deduplicateToolCalls(messages) {
|
|
776
|
-
if (messages.length === 0)
|
|
777
|
-
return messages;
|
|
778
|
-
// Phase 1: Build result lookup for output-dependent tools
|
|
779
|
-
const resultMap = new Map();
|
|
780
|
-
for (const msg of messages) {
|
|
781
|
-
if (msg.role === "toolResult" && Array.isArray(msg.content)) {
|
|
782
|
-
const text = msg.content
|
|
783
|
-
.filter((c) => c.type === "text")
|
|
784
|
-
.map((c) => c.text)
|
|
785
|
-
.join("\n");
|
|
786
|
-
resultMap.set(msg.toolCallId, text);
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
// Phase 2: Walk newest→oldest, collect keys. The first encounter
|
|
790
|
-
// (newest) wins; all earlier tool calls with the same key are duplicates.
|
|
791
|
-
const seen = new Map(); // key → toolCallId (keep newest)
|
|
792
|
-
const duplicateIds = new Set();
|
|
793
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
794
|
-
const msg = messages[i];
|
|
795
|
-
if (msg.role !== "assistant")
|
|
796
|
-
continue;
|
|
797
|
-
if (!("content" in msg) || !Array.isArray(msg.content))
|
|
798
|
-
continue;
|
|
799
|
-
// Iterate blocks newest-first: within a single assistant message,
|
|
800
|
-
// the rightmost tool call is the "most recent" one.
|
|
801
|
-
for (let j = msg.content.length - 1; j >= 0; j--) {
|
|
802
|
-
const block = msg.content[j];
|
|
803
|
-
if (typeof block !== "object" || block === null)
|
|
804
|
-
continue;
|
|
805
|
-
if (!("type" in block) || block.type !== "toolCall")
|
|
806
|
-
continue;
|
|
807
|
-
const toolBlock = block;
|
|
808
|
-
if (isMutableOrUnknownTool(toolBlock.name))
|
|
809
|
-
continue;
|
|
810
|
-
const key = toolCallDedupKey(toolBlock.name, toolBlock.arguments, toolBlock.id, resultMap);
|
|
811
|
-
if (seen.has(key)) {
|
|
812
|
-
duplicateIds.add(toolBlock.id);
|
|
813
|
-
}
|
|
814
|
-
else {
|
|
815
|
-
seen.set(key, toolBlock.id);
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
if (duplicateIds.size === 0)
|
|
820
|
-
return messages;
|
|
821
|
-
// Phase 3: Filter out duplicate tool results and strip duplicate
|
|
822
|
-
// ToolCall blocks from assistant messages.
|
|
823
|
-
const deduped = [];
|
|
824
|
-
let modified = false;
|
|
825
|
-
for (const msg of messages) {
|
|
826
|
-
if (msg.role === "toolResult" && duplicateIds.has(msg.toolCallId)) {
|
|
827
|
-
modified = true;
|
|
828
|
-
continue;
|
|
829
|
-
}
|
|
830
|
-
if (msg.role === "assistant" && "content" in msg && Array.isArray(msg.content)) {
|
|
831
|
-
const originalLength = msg.content.length;
|
|
832
|
-
const filteredContent = msg.content.filter((block) => {
|
|
833
|
-
if (typeof block !== "object" || block === null)
|
|
834
|
-
return true;
|
|
835
|
-
if (!("type" in block) || block.type !== "toolCall")
|
|
836
|
-
return true;
|
|
837
|
-
return !duplicateIds.has(block.id);
|
|
838
|
-
});
|
|
839
|
-
if (filteredContent.length < originalLength) {
|
|
840
|
-
modified = true;
|
|
841
|
-
deduped.push({ ...msg, content: filteredContent });
|
|
842
|
-
continue;
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
deduped.push(msg);
|
|
846
|
-
}
|
|
847
|
-
return modified ? deduped : messages;
|
|
848
|
-
}
|
|
849
165
|
// ============================================================================
|
|
850
166
|
// Main compaction function
|
|
851
167
|
// ============================================================================
|
|
852
|
-
const TURN_PREFIX_SUMMARIZATION_PROMPT = `This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained.
|
|
853
|
-
|
|
854
|
-
Summarize the prefix to provide context for the retained suffix:
|
|
855
|
-
|
|
856
|
-
## Original Request
|
|
857
|
-
[What did the user ask for in this turn?]
|
|
858
|
-
|
|
859
|
-
## Early Progress
|
|
860
|
-
- [Key decisions and work done in the prefix]
|
|
861
|
-
|
|
862
|
-
## Context for Suffix
|
|
863
|
-
- [Information needed to understand the retained recent work]
|
|
864
|
-
|
|
865
|
-
Be concise. Focus on what's needed to understand the kept suffix.`;
|
|
866
168
|
/**
|
|
867
169
|
* Generate summaries for compaction using prepared data.
|
|
868
170
|
* Returns CompactionResult - SessionManager adds uuid/parentUuid when saving.
|
|
@@ -912,27 +214,3 @@ export async function compact(preparation, model, apiKey, headers, customInstruc
|
|
|
912
214
|
details: { readFiles, modifiedFiles },
|
|
913
215
|
};
|
|
914
216
|
}
|
|
915
|
-
/**
|
|
916
|
-
* Generate a summary for a turn prefix (when splitting a turn).
|
|
917
|
-
*/
|
|
918
|
-
async function generateTurnPrefixSummary(messages, model, reserveTokens, apiKey, headers, signal, thinkingLevel, streamFn) {
|
|
919
|
-
const maxTokens = Math.min(Math.floor(0.5 * reserveTokens), model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY); // Smaller budget for turn prefix
|
|
920
|
-
const llmMessages = convertToLlm(messages);
|
|
921
|
-
const conversationText = serializeConversation(llmMessages);
|
|
922
|
-
const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`;
|
|
923
|
-
const summarizationMessages = [
|
|
924
|
-
{
|
|
925
|
-
role: "user",
|
|
926
|
-
content: [{ type: "text", text: promptText }],
|
|
927
|
-
timestamp: Date.now(),
|
|
928
|
-
},
|
|
929
|
-
];
|
|
930
|
-
const response = await completeSummarization(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, createSummarizationOptions(model, maxTokens, apiKey, headers, signal, thinkingLevel), streamFn);
|
|
931
|
-
if (response.stopReason === "error") {
|
|
932
|
-
throw new Error(`Turn prefix summarization failed: ${response.errorMessage || "Unknown error"}`);
|
|
933
|
-
}
|
|
934
|
-
return response.content
|
|
935
|
-
.filter((c) => c.type === "text")
|
|
936
|
-
.map((c) => c.text)
|
|
937
|
-
.join("\n");
|
|
938
|
-
}
|