@aexol/spectral 0.9.229 → 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/auth-helper.d.ts.map +1 -1
- package/dist/auth-helper.js +14 -8
- package/dist/commands/login.d.ts +12 -4
- package/dist/commands/login.d.ts.map +1 -1
- package/dist/commands/login.js +36 -15
- package/dist/commands/serve.d.ts.map +1 -1
- package/dist/commands/serve.js +3 -0
- package/dist/config.d.ts +15 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +10 -1
- 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/generated/zeus/const.d.ts.map +1 -1
- package/dist/generated/zeus/const.js +21 -0
- package/dist/generated/zeus/index.d.ts +98 -0
- package/dist/generated/zeus/index.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/registration.d.ts +10 -2
- package/dist/relay/registration.d.ts.map +1 -1
- package/dist/relay/registration.js +32 -3
- 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
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cut point detection for compaction.
|
|
3
|
+
*
|
|
4
|
+
* Pure functions for finding valid compaction cut points and turn boundaries.
|
|
5
|
+
*/
|
|
6
|
+
import type { SessionEntry } from "../session-manager.js";
|
|
7
|
+
/**
|
|
8
|
+
* Find the user message (or bashExecution) that starts the turn containing the given entry index.
|
|
9
|
+
* Returns -1 if no turn start found before the index.
|
|
10
|
+
* BashExecutionMessage is treated like a user message for turn boundaries.
|
|
11
|
+
*/
|
|
12
|
+
export declare function findTurnStartIndex(entries: SessionEntry[], entryIndex: number, startIndex: number): number;
|
|
13
|
+
export interface CutPointResult {
|
|
14
|
+
/** Index of first entry to keep */
|
|
15
|
+
firstKeptEntryIndex: number;
|
|
16
|
+
/** Index of user message that starts the turn being split, or -1 if not splitting */
|
|
17
|
+
turnStartIndex: number;
|
|
18
|
+
/** Whether this cut splits a turn (cut point is not a user message) */
|
|
19
|
+
isSplitTurn: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface FindCutPointOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Explicit phase-boundary compaction should prefer the newest safe boundary
|
|
24
|
+
* over retaining a large just-finished turn as the recent raw suffix.
|
|
25
|
+
*/
|
|
26
|
+
phaseBoundary?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* When provided, use this entry id as a fixed cut point instead of walking
|
|
29
|
+
* backwards from the newest entry. This lets request-time adapters freeze a
|
|
30
|
+
* previously-computed cut without regenerating the summary.
|
|
31
|
+
*/
|
|
32
|
+
anchorEntryId?: string;
|
|
33
|
+
/**
|
|
34
|
+
* When true, ensure the most recent user message is never folded into the
|
|
35
|
+
* compaction summary — the cut point is pulled back to (or before) the
|
|
36
|
+
* latest user message so it survives verbatim. This protects the current
|
|
37
|
+
* task/brief from being reduced to a truncated excerpt mid-run.
|
|
38
|
+
*/
|
|
39
|
+
pinLatestUserMessage?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Find the cut point in session entries that keeps approximately `keepRecentTokens`.
|
|
43
|
+
*
|
|
44
|
+
* Algorithm: Walk backwards from newest, accumulating estimated message sizes.
|
|
45
|
+
* Stop when we've accumulated >= keepRecentTokens. Cut at that point.
|
|
46
|
+
*
|
|
47
|
+
* Can cut at user OR assistant messages (never tool results). When cutting at an
|
|
48
|
+
* assistant message with tool calls, its tool results come after and will be kept.
|
|
49
|
+
*
|
|
50
|
+
* Returns CutPointResult with:
|
|
51
|
+
* - firstKeptEntryIndex: the entry index to start keeping from
|
|
52
|
+
* - turnStartIndex: if cutting mid-turn, the user message that started that turn
|
|
53
|
+
* - isSplitTurn: whether we're cutting in the middle of a turn
|
|
54
|
+
*
|
|
55
|
+
* Only considers entries between `startIndex` and `endIndex` (exclusive).
|
|
56
|
+
*/
|
|
57
|
+
export declare function findCutPoint(entries: SessionEntry[], startIndex: number, endIndex: number, keepRecentTokens: number, options?: FindCutPointOptions): CutPointResult;
|
|
58
|
+
//# sourceMappingURL=cut-points.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cut-points.d.ts","sourceRoot":"","sources":["../../../../../src/sdk/coding-agent/core/compaction/cut-points.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAkD1D;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAe1G;AAED,MAAM,WAAW,cAAc;IAC9B,mCAAmC;IACnC,mBAAmB,EAAE,MAAM,CAAC;IAC5B,qFAAqF;IACrF,cAAc,EAAE,MAAM,CAAC;IACvB,uEAAuE;IACvE,WAAW,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAC/B;AAcD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAC3B,OAAO,EAAE,YAAY,EAAE,EACvB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,gBAAgB,EAAE,MAAM,EACxB,OAAO,GAAE,mBAAwB,GAC/B,cAAc,CAgGhB"}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cut point detection for compaction.
|
|
3
|
+
*
|
|
4
|
+
* Pure functions for finding valid compaction cut points and turn boundaries.
|
|
5
|
+
*/
|
|
6
|
+
import { estimateTokens } from "./tokens.js";
|
|
7
|
+
/**
|
|
8
|
+
* Find valid cut points: indices of user, assistant, custom, or bashExecution messages.
|
|
9
|
+
* Never cut at tool results (they must follow their tool call).
|
|
10
|
+
* When we cut at an assistant message with tool calls, its tool results follow it
|
|
11
|
+
* and will be kept.
|
|
12
|
+
* BashExecutionMessage is treated like a user message (user-initiated context).
|
|
13
|
+
*/
|
|
14
|
+
function findValidCutPoints(entries, startIndex, endIndex) {
|
|
15
|
+
const cutPoints = [];
|
|
16
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
17
|
+
const entry = entries[i];
|
|
18
|
+
switch (entry.type) {
|
|
19
|
+
case "message": {
|
|
20
|
+
const role = entry.message.role;
|
|
21
|
+
switch (role) {
|
|
22
|
+
case "bashExecution":
|
|
23
|
+
case "custom":
|
|
24
|
+
case "branchSummary":
|
|
25
|
+
case "compactionSummary":
|
|
26
|
+
case "user":
|
|
27
|
+
case "assistant":
|
|
28
|
+
cutPoints.push(i);
|
|
29
|
+
break;
|
|
30
|
+
case "toolResult":
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
case "thinking_level_change":
|
|
36
|
+
case "model_change":
|
|
37
|
+
case "compaction":
|
|
38
|
+
case "branch_summary":
|
|
39
|
+
case "custom":
|
|
40
|
+
case "custom_message":
|
|
41
|
+
case "label":
|
|
42
|
+
case "session_info":
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
// branch_summary and custom_message are user-role messages, valid cut points
|
|
46
|
+
if (entry.type === "branch_summary" || entry.type === "custom_message") {
|
|
47
|
+
cutPoints.push(i);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return cutPoints;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Find the user message (or bashExecution) that starts the turn containing the given entry index.
|
|
54
|
+
* Returns -1 if no turn start found before the index.
|
|
55
|
+
* BashExecutionMessage is treated like a user message for turn boundaries.
|
|
56
|
+
*/
|
|
57
|
+
export function findTurnStartIndex(entries, entryIndex, startIndex) {
|
|
58
|
+
for (let i = entryIndex; i >= startIndex; i--) {
|
|
59
|
+
const entry = entries[i];
|
|
60
|
+
// branch_summary and custom_message are user-role messages, can start a turn
|
|
61
|
+
if (entry.type === "branch_summary" || entry.type === "custom_message") {
|
|
62
|
+
return i;
|
|
63
|
+
}
|
|
64
|
+
if (entry.type === "message") {
|
|
65
|
+
const role = entry.message.role;
|
|
66
|
+
if (role === "user" || role === "bashExecution") {
|
|
67
|
+
return i;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return -1;
|
|
72
|
+
}
|
|
73
|
+
function isTurnBoundaryEntry(entry) {
|
|
74
|
+
if (!entry)
|
|
75
|
+
return false;
|
|
76
|
+
if (entry.type === "branch_summary" || entry.type === "custom_message")
|
|
77
|
+
return true;
|
|
78
|
+
if (entry.type !== "message")
|
|
79
|
+
return false;
|
|
80
|
+
const role = entry.message.role;
|
|
81
|
+
return role === "user" ||
|
|
82
|
+
role === "bashExecution" ||
|
|
83
|
+
role === "custom" ||
|
|
84
|
+
role === "branchSummary" ||
|
|
85
|
+
role === "compactionSummary";
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Find the cut point in session entries that keeps approximately `keepRecentTokens`.
|
|
89
|
+
*
|
|
90
|
+
* Algorithm: Walk backwards from newest, accumulating estimated message sizes.
|
|
91
|
+
* Stop when we've accumulated >= keepRecentTokens. Cut at that point.
|
|
92
|
+
*
|
|
93
|
+
* Can cut at user OR assistant messages (never tool results). When cutting at an
|
|
94
|
+
* assistant message with tool calls, its tool results come after and will be kept.
|
|
95
|
+
*
|
|
96
|
+
* Returns CutPointResult with:
|
|
97
|
+
* - firstKeptEntryIndex: the entry index to start keeping from
|
|
98
|
+
* - turnStartIndex: if cutting mid-turn, the user message that started that turn
|
|
99
|
+
* - isSplitTurn: whether we're cutting in the middle of a turn
|
|
100
|
+
*
|
|
101
|
+
* Only considers entries between `startIndex` and `endIndex` (exclusive).
|
|
102
|
+
*/
|
|
103
|
+
export function findCutPoint(entries, startIndex, endIndex, keepRecentTokens, options = {}) {
|
|
104
|
+
if (options.anchorEntryId) {
|
|
105
|
+
const anchorIndex = entries.findIndex((entry) => entry.id === options.anchorEntryId);
|
|
106
|
+
if (anchorIndex >= startIndex && anchorIndex < endIndex) {
|
|
107
|
+
return { firstKeptEntryIndex: anchorIndex, turnStartIndex: -1, isSplitTurn: false };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const cutPoints = findValidCutPoints(entries, startIndex, endIndex);
|
|
111
|
+
if (cutPoints.length === 0) {
|
|
112
|
+
return { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };
|
|
113
|
+
}
|
|
114
|
+
// Walk backwards from newest, accumulating estimated message sizes.
|
|
115
|
+
// Phase-boundary callers provide an explicit marker, so the newest valid
|
|
116
|
+
// boundary is the desired cut point and the usual recent-token suffix is not
|
|
117
|
+
// retained raw.
|
|
118
|
+
let accumulatedTokens = 0;
|
|
119
|
+
let cutIndex = options.phaseBoundary
|
|
120
|
+
? cutPoints[cutPoints.length - 1]
|
|
121
|
+
: cutPoints[0]; // Default: keep from first message (not header)
|
|
122
|
+
if (!options.phaseBoundary) {
|
|
123
|
+
for (let i = endIndex - 1; i >= startIndex; i--) {
|
|
124
|
+
const entry = entries[i];
|
|
125
|
+
if (entry.type !== "message")
|
|
126
|
+
continue;
|
|
127
|
+
// Estimate this message's size
|
|
128
|
+
const messageTokens = estimateTokens(entry.message);
|
|
129
|
+
accumulatedTokens += messageTokens;
|
|
130
|
+
// Check if we've exceeded the budget
|
|
131
|
+
if (accumulatedTokens >= keepRecentTokens) {
|
|
132
|
+
// Find the closest valid cut point at or after this entry
|
|
133
|
+
for (let c = 0; c < cutPoints.length; c++) {
|
|
134
|
+
if (cutPoints[c] >= i) {
|
|
135
|
+
cutIndex = cutPoints[c];
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// Scan backwards from cutIndex to include any non-message entries (bash, settings, etc.)
|
|
144
|
+
while (cutIndex > startIndex) {
|
|
145
|
+
const prevEntry = entries[cutIndex - 1];
|
|
146
|
+
// Stop at session header or compaction boundaries
|
|
147
|
+
if (prevEntry.type === "compaction") {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
if (prevEntry.type === "message") {
|
|
151
|
+
// Stop if we hit any message
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
// Include this non-message entry (bash, settings change, etc.)
|
|
155
|
+
cutIndex--;
|
|
156
|
+
}
|
|
157
|
+
// Pin latest user message: never fold the most recent user message into the
|
|
158
|
+
// compaction summary. If the computed cut point lands after the latest user
|
|
159
|
+
// message, pull it back so that message (and everything after) survives
|
|
160
|
+
// verbatim. This protects the current task/brief from truncation mid-run.
|
|
161
|
+
if (options.pinLatestUserMessage) {
|
|
162
|
+
let latestUserIndex = -1;
|
|
163
|
+
for (let i = endIndex - 1; i >= startIndex; i--) {
|
|
164
|
+
const entry = entries[i];
|
|
165
|
+
if (entry.type === "message" && entry.message.role === "user") {
|
|
166
|
+
latestUserIndex = i;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (latestUserIndex >= 0 && cutIndex > latestUserIndex) {
|
|
171
|
+
cutIndex = latestUserIndex;
|
|
172
|
+
// Re-include any non-message entries (bash, settings, etc.) immediately
|
|
173
|
+
// before the pinned user message, so we don't lose adjacent context.
|
|
174
|
+
while (cutIndex > startIndex) {
|
|
175
|
+
const prevEntry = entries[cutIndex - 1];
|
|
176
|
+
if (prevEntry.type === "compaction" || prevEntry.type === "message")
|
|
177
|
+
break;
|
|
178
|
+
cutIndex--;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Determine if this is a split turn
|
|
183
|
+
const cutEntry = entries[cutIndex];
|
|
184
|
+
const isTurnBoundary = isTurnBoundaryEntry(cutEntry);
|
|
185
|
+
const turnStartIndex = isTurnBoundary ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);
|
|
186
|
+
return {
|
|
187
|
+
firstKeptEntryIndex: cutIndex,
|
|
188
|
+
turnStartIndex,
|
|
189
|
+
isSplitTurn: !isTurnBoundary && turnStartIndex !== -1,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool call deduplication for compaction.
|
|
3
|
+
*
|
|
4
|
+
* Removes repeated tool calls from message arrays before summarization.
|
|
5
|
+
*/
|
|
6
|
+
import type { AgentMessage } from "../../../agent-core/index.js";
|
|
7
|
+
/**
|
|
8
|
+
* Deduplicate repeated tool calls in a message array by keeping only the
|
|
9
|
+
* most recent occurrence of each (tool, args) pair.
|
|
10
|
+
*
|
|
11
|
+
* Strategy:
|
|
12
|
+
* - **Idempotent-read tools** (read, grep, glob): same (name, args) → keep last only.
|
|
13
|
+
* These tools return the same content for the same arguments.
|
|
14
|
+
* - **Output-dependent tools** (bash): same (name, args, output) → keep last only.
|
|
15
|
+
* Two bash calls with same command but different output are NOT duplicates.
|
|
16
|
+
* - **Mutating tools** (edit, write): never deduplicated. Chronological ordering
|
|
17
|
+
* of mutations matters for correctness.
|
|
18
|
+
* - **Unknown tools** (extensions, MCP, custom): never deduplicated. Conservative
|
|
19
|
+
* by default — only tools in the known sets above participate.
|
|
20
|
+
*
|
|
21
|
+
* Deduplication removes both the ToolCall block from the assistant message
|
|
22
|
+
* and the corresponding ToolResultMessage from the array.
|
|
23
|
+
*
|
|
24
|
+
* Recalculated each time compaction runs — prompt cache is only impacted
|
|
25
|
+
* alongside compression, not on every turn.
|
|
26
|
+
*/
|
|
27
|
+
export declare function deduplicateToolCalls(messages: AgentMessage[]): AgentMessage[];
|
|
28
|
+
//# sourceMappingURL=dedupe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dedupe.d.ts","sourceRoot":"","sources":["../../../../../src/sdk/coding-agent/core/compaction/dedupe.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAoEjE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,YAAY,EAAE,GAAG,YAAY,EAAE,CA8E7E"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool call deduplication for compaction.
|
|
3
|
+
*
|
|
4
|
+
* Removes repeated tool calls from message arrays before summarization.
|
|
5
|
+
*/
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// Tool Call Deduplication
|
|
8
|
+
// ============================================================================
|
|
9
|
+
/**
|
|
10
|
+
* Tools that always return the same result for the same arguments.
|
|
11
|
+
* Same (name, args) from any point in the session = duplicate.
|
|
12
|
+
* Only the most recent call is kept.
|
|
13
|
+
*/
|
|
14
|
+
const IDEMPOTENT_READ_TOOLS = new Set(["read"]);
|
|
15
|
+
/**
|
|
16
|
+
* Tools whose output may differ between calls with the same arguments.
|
|
17
|
+
* Deduplication requires comparing actual outputs to determine equivalence.
|
|
18
|
+
*/
|
|
19
|
+
const OUTPUT_DEPENDENT_TOOLS = new Set(["bash"]);
|
|
20
|
+
/**
|
|
21
|
+
* Tools that mutate state. Never deduplicated — chronological ordering matters.
|
|
22
|
+
*/
|
|
23
|
+
const MUTABLE_TOOLS = new Set(["edit", "write"]);
|
|
24
|
+
/**
|
|
25
|
+
* Build a stable string key from a Record's sorted keys.
|
|
26
|
+
* Ensures {a:1, b:2} and {b:2, a:1} produce the same key.
|
|
27
|
+
*/
|
|
28
|
+
function stableArgs(args) {
|
|
29
|
+
const sortedKeys = Object.keys(args).sort();
|
|
30
|
+
const sorted = {};
|
|
31
|
+
for (const key of sortedKeys) {
|
|
32
|
+
sorted[key] = args[key];
|
|
33
|
+
}
|
|
34
|
+
return JSON.stringify(sorted);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Compute the deduplication key for a tool call.
|
|
38
|
+
* - Idempotent-read tools: keyed by (name, args) — same args always same result
|
|
39
|
+
* - Output-dependent tools: keyed by (name, args, output) — output comparison needed
|
|
40
|
+
*/
|
|
41
|
+
function toolCallDedupKey(toolName, args, toolCallId, resultMap) {
|
|
42
|
+
const argsKey = stableArgs(args);
|
|
43
|
+
if (OUTPUT_DEPENDENT_TOOLS.has(toolName)) {
|
|
44
|
+
const output = resultMap.get(toolCallId) ?? "";
|
|
45
|
+
return `${toolName}:${argsKey}:${output}`;
|
|
46
|
+
}
|
|
47
|
+
return `${toolName}:${argsKey}`;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check whether a tool should be excluded from deduplication.
|
|
51
|
+
* Mutating tools and unknown tools are never deduplicated (conservative).
|
|
52
|
+
*/
|
|
53
|
+
function isMutableOrUnknownTool(toolName) {
|
|
54
|
+
if (MUTABLE_TOOLS.has(toolName))
|
|
55
|
+
return true;
|
|
56
|
+
if (IDEMPOTENT_READ_TOOLS.has(toolName))
|
|
57
|
+
return false;
|
|
58
|
+
if (OUTPUT_DEPENDENT_TOOLS.has(toolName))
|
|
59
|
+
return false;
|
|
60
|
+
// Unknown tools: conservative — never deduplicate
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Deduplicate repeated tool calls in a message array by keeping only the
|
|
65
|
+
* most recent occurrence of each (tool, args) pair.
|
|
66
|
+
*
|
|
67
|
+
* Strategy:
|
|
68
|
+
* - **Idempotent-read tools** (read, grep, glob): same (name, args) → keep last only.
|
|
69
|
+
* These tools return the same content for the same arguments.
|
|
70
|
+
* - **Output-dependent tools** (bash): same (name, args, output) → keep last only.
|
|
71
|
+
* Two bash calls with same command but different output are NOT duplicates.
|
|
72
|
+
* - **Mutating tools** (edit, write): never deduplicated. Chronological ordering
|
|
73
|
+
* of mutations matters for correctness.
|
|
74
|
+
* - **Unknown tools** (extensions, MCP, custom): never deduplicated. Conservative
|
|
75
|
+
* by default — only tools in the known sets above participate.
|
|
76
|
+
*
|
|
77
|
+
* Deduplication removes both the ToolCall block from the assistant message
|
|
78
|
+
* and the corresponding ToolResultMessage from the array.
|
|
79
|
+
*
|
|
80
|
+
* Recalculated each time compaction runs — prompt cache is only impacted
|
|
81
|
+
* alongside compression, not on every turn.
|
|
82
|
+
*/
|
|
83
|
+
export function deduplicateToolCalls(messages) {
|
|
84
|
+
if (messages.length === 0)
|
|
85
|
+
return messages;
|
|
86
|
+
// Phase 1: Build result lookup for output-dependent tools
|
|
87
|
+
const resultMap = new Map();
|
|
88
|
+
for (const msg of messages) {
|
|
89
|
+
if (msg.role === "toolResult" && Array.isArray(msg.content)) {
|
|
90
|
+
const text = msg.content
|
|
91
|
+
.filter((c) => c.type === "text")
|
|
92
|
+
.map((c) => c.text)
|
|
93
|
+
.join("\n");
|
|
94
|
+
resultMap.set(msg.toolCallId, text);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Phase 2: Walk newest→oldest, collect keys. The first encounter
|
|
98
|
+
// (newest) wins; all earlier tool calls with the same key are duplicates.
|
|
99
|
+
const seen = new Map(); // key → toolCallId (keep newest)
|
|
100
|
+
const duplicateIds = new Set();
|
|
101
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
102
|
+
const msg = messages[i];
|
|
103
|
+
if (msg.role !== "assistant")
|
|
104
|
+
continue;
|
|
105
|
+
if (!("content" in msg) || !Array.isArray(msg.content))
|
|
106
|
+
continue;
|
|
107
|
+
// Iterate blocks newest-first: within a single assistant message,
|
|
108
|
+
// the rightmost tool call is the "most recent" one.
|
|
109
|
+
for (let j = msg.content.length - 1; j >= 0; j--) {
|
|
110
|
+
const block = msg.content[j];
|
|
111
|
+
if (typeof block !== "object" || block === null)
|
|
112
|
+
continue;
|
|
113
|
+
if (!("type" in block) || block.type !== "toolCall")
|
|
114
|
+
continue;
|
|
115
|
+
const toolBlock = block;
|
|
116
|
+
if (isMutableOrUnknownTool(toolBlock.name))
|
|
117
|
+
continue;
|
|
118
|
+
const key = toolCallDedupKey(toolBlock.name, toolBlock.arguments, toolBlock.id, resultMap);
|
|
119
|
+
if (seen.has(key)) {
|
|
120
|
+
duplicateIds.add(toolBlock.id);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
seen.set(key, toolBlock.id);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (duplicateIds.size === 0)
|
|
128
|
+
return messages;
|
|
129
|
+
// Phase 3: Filter out duplicate tool results and strip duplicate
|
|
130
|
+
// ToolCall blocks from assistant messages.
|
|
131
|
+
const deduped = [];
|
|
132
|
+
let modified = false;
|
|
133
|
+
for (const msg of messages) {
|
|
134
|
+
if (msg.role === "toolResult" && duplicateIds.has(msg.toolCallId)) {
|
|
135
|
+
modified = true;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (msg.role === "assistant" && "content" in msg && Array.isArray(msg.content)) {
|
|
139
|
+
const originalLength = msg.content.length;
|
|
140
|
+
const filteredContent = msg.content.filter((block) => {
|
|
141
|
+
if (typeof block !== "object" || block === null)
|
|
142
|
+
return true;
|
|
143
|
+
if (!("type" in block) || block.type !== "toolCall")
|
|
144
|
+
return true;
|
|
145
|
+
return !duplicateIds.has(block.id);
|
|
146
|
+
});
|
|
147
|
+
if (filteredContent.length < originalLength) {
|
|
148
|
+
modified = true;
|
|
149
|
+
deduped.push({ ...msg, content: filteredContent });
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
deduped.push(msg);
|
|
154
|
+
}
|
|
155
|
+
return modified ? deduped : messages;
|
|
156
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM summarization for compaction.
|
|
3
|
+
*
|
|
4
|
+
* Prompts and helpers for generating conversation summaries via the LLM.
|
|
5
|
+
*/
|
|
6
|
+
import type { AgentMessage, StreamFn, ThinkingLevel } from "../../../agent-core/index.js";
|
|
7
|
+
import type { Model } from "../../../ai/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Generate a summary of the conversation using the LLM.
|
|
10
|
+
* If previousSummary is provided, uses the update prompt to merge.
|
|
11
|
+
*
|
|
12
|
+
* @deprecated The active coding-agent session path uses deterministic DCP-lite
|
|
13
|
+
* (`compactDcpLite`) plus `session_before_compact` hook details. This legacy
|
|
14
|
+
* LLM summarizer remains exported for compatibility with older callers only.
|
|
15
|
+
*/
|
|
16
|
+
export declare function generateSummary(currentMessages: AgentMessage[], model: Model<any>, reserveTokens: number, apiKey: string | undefined, headers?: Record<string, string>, signal?: AbortSignal, customInstructions?: string, previousSummary?: string, thinkingLevel?: ThinkingLevel, streamFn?: StreamFn): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Generate a summary for a turn prefix (when splitting a turn).
|
|
19
|
+
*/
|
|
20
|
+
export declare function generateTurnPrefixSummary(messages: AgentMessage[], model: Model<any>, reserveTokens: number, apiKey: string | undefined, headers?: Record<string, string>, signal?: AbortSignal, thinkingLevel?: ThinkingLevel, streamFn?: StreamFn): Promise<string>;
|
|
21
|
+
//# sourceMappingURL=summarize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"summarize.d.ts","sourceRoot":"","sources":["../../../../../src/sdk/coding-agent/core/compaction/summarize.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC1F,OAAO,KAAK,EAA6B,KAAK,EAAuB,MAAM,sBAAsB,CAAC;AAmHlG;;;;;;;GAOG;AACH,wBAAsB,eAAe,CACpC,eAAe,EAAE,YAAY,EAAE,EAC/B,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,MAAM,CAAC,EAAE,WAAW,EACpB,kBAAkB,CAAC,EAAE,MAAM,EAC3B,eAAe,CAAC,EAAE,MAAM,EACxB,aAAa,CAAC,EAAE,aAAa,EAC7B,QAAQ,CAAC,EAAE,QAAQ,GACjB,OAAO,CAAC,MAAM,CAAC,CAmDjB;AAiBD;;GAEG;AACH,wBAAsB,yBAAyB,CAC9C,QAAQ,EAAE,YAAY,EAAE,EACxB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,MAAM,CAAC,EAAE,WAAW,EACpB,aAAa,CAAC,EAAE,aAAa,EAC7B,QAAQ,CAAC,EAAE,QAAQ,GACjB,OAAO,CAAC,MAAM,CAAC,CA+BjB"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM summarization for compaction.
|
|
3
|
+
*
|
|
4
|
+
* Prompts and helpers for generating conversation summaries via the LLM.
|
|
5
|
+
*/
|
|
6
|
+
import { completeSimple } from "../../../ai/index.js";
|
|
7
|
+
import { convertToLlm } from "../messages.js";
|
|
8
|
+
import { SUMMARIZATION_SYSTEM_PROMPT, serializeConversation } from "./utils.js";
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// Summarization
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// Legacy LLM summarization path. AgentSession now builds persisted compaction
|
|
13
|
+
// entries with deterministic DCP-lite (`compactDcpLite`) and lets
|
|
14
|
+
// `session_before_compact` hooks provide memory details. Keep these exports for
|
|
15
|
+
// compatibility with external SDK callers and older harness code, but do not
|
|
16
|
+
// route new session compaction work through this model-generated fallback.
|
|
17
|
+
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.
|
|
18
|
+
|
|
19
|
+
Use this EXACT format:
|
|
20
|
+
|
|
21
|
+
## Goal
|
|
22
|
+
[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]
|
|
23
|
+
|
|
24
|
+
## Constraints & Preferences
|
|
25
|
+
- [Any constraints, preferences, or requirements mentioned by user]
|
|
26
|
+
- [Or "(none)" if none were mentioned]
|
|
27
|
+
|
|
28
|
+
## Progress
|
|
29
|
+
### Done
|
|
30
|
+
- [x] [Completed tasks/changes]
|
|
31
|
+
|
|
32
|
+
### In Progress
|
|
33
|
+
- [ ] [Current work]
|
|
34
|
+
|
|
35
|
+
### Blocked
|
|
36
|
+
- [Issues preventing progress, if any]
|
|
37
|
+
|
|
38
|
+
## Key Decisions
|
|
39
|
+
- **[Decision]**: [Brief rationale]
|
|
40
|
+
|
|
41
|
+
## Next Steps
|
|
42
|
+
1. [Ordered list of what should happen next]
|
|
43
|
+
|
|
44
|
+
## Critical Context
|
|
45
|
+
- [Any data, examples, or references needed to continue]
|
|
46
|
+
- [Or "(none)" if not applicable]
|
|
47
|
+
|
|
48
|
+
Keep each section concise. Preserve exact file paths, function names, and error messages.`;
|
|
49
|
+
const UPDATE_SUMMARIZATION_PROMPT = `The messages above are NEW conversation messages to incorporate into the existing summary provided in <previous-summary> tags.
|
|
50
|
+
|
|
51
|
+
Update the existing structured summary with new information. RULES:
|
|
52
|
+
- PRESERVE all existing information from the previous summary
|
|
53
|
+
- ADD new progress, decisions, and context from the new messages
|
|
54
|
+
- UPDATE the Progress section: move items from "In Progress" to "Done" when completed
|
|
55
|
+
- UPDATE "Next Steps" based on what was accomplished
|
|
56
|
+
- PRESERVE exact file paths, function names, and error messages
|
|
57
|
+
- If something is no longer relevant, you may remove it
|
|
58
|
+
|
|
59
|
+
Use this EXACT format:
|
|
60
|
+
|
|
61
|
+
## Goal
|
|
62
|
+
[Preserve existing goals, add new ones if the task expanded]
|
|
63
|
+
|
|
64
|
+
## Constraints & Preferences
|
|
65
|
+
- [Preserve existing, add new ones discovered]
|
|
66
|
+
|
|
67
|
+
## Progress
|
|
68
|
+
### Done
|
|
69
|
+
- [x] [Include previously done items AND newly completed items]
|
|
70
|
+
|
|
71
|
+
### In Progress
|
|
72
|
+
- [ ] [Current work - update based on progress]
|
|
73
|
+
|
|
74
|
+
### Blocked
|
|
75
|
+
- [Current blockers - remove if resolved]
|
|
76
|
+
|
|
77
|
+
## Key Decisions
|
|
78
|
+
- **[Decision]**: [Brief rationale] (preserve all previous, add new)
|
|
79
|
+
|
|
80
|
+
## Next Steps
|
|
81
|
+
1. [Update based on current state]
|
|
82
|
+
|
|
83
|
+
## Critical Context
|
|
84
|
+
- [Preserve important context, add new if needed]
|
|
85
|
+
|
|
86
|
+
Keep each section concise. Preserve exact file paths, function names, and error messages.`;
|
|
87
|
+
function createSummarizationOptions(model, maxTokens, apiKey, headers, signal, thinkingLevel) {
|
|
88
|
+
const options = { maxTokens, signal, apiKey, headers };
|
|
89
|
+
if (model.reasoning && thinkingLevel && thinkingLevel !== "off") {
|
|
90
|
+
options.reasoning = thinkingLevel;
|
|
91
|
+
}
|
|
92
|
+
return options;
|
|
93
|
+
}
|
|
94
|
+
async function completeSummarization(model, context, options, streamFn) {
|
|
95
|
+
if (!streamFn) {
|
|
96
|
+
return completeSimple(model, context, options);
|
|
97
|
+
}
|
|
98
|
+
const stream = await streamFn(model, context, options);
|
|
99
|
+
return stream.result();
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Generate a summary of the conversation using the LLM.
|
|
103
|
+
* If previousSummary is provided, uses the update prompt to merge.
|
|
104
|
+
*
|
|
105
|
+
* @deprecated The active coding-agent session path uses deterministic DCP-lite
|
|
106
|
+
* (`compactDcpLite`) plus `session_before_compact` hook details. This legacy
|
|
107
|
+
* LLM summarizer remains exported for compatibility with older callers only.
|
|
108
|
+
*/
|
|
109
|
+
export async function generateSummary(currentMessages, model, reserveTokens, apiKey, headers, signal, customInstructions, previousSummary, thinkingLevel, streamFn) {
|
|
110
|
+
const maxTokens = Math.min(Math.floor(0.8 * reserveTokens), model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY);
|
|
111
|
+
// Use update prompt if we have a previous summary, otherwise initial prompt
|
|
112
|
+
let basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;
|
|
113
|
+
if (customInstructions) {
|
|
114
|
+
basePrompt = `${basePrompt}\n\nAdditional focus: ${customInstructions}`;
|
|
115
|
+
}
|
|
116
|
+
// Serialize conversation to text so model doesn't try to continue it
|
|
117
|
+
// Convert to LLM messages first (handles custom types like bashExecution, custom, etc.)
|
|
118
|
+
const llmMessages = convertToLlm(currentMessages);
|
|
119
|
+
const conversationText = serializeConversation(llmMessages);
|
|
120
|
+
// Build the prompt with conversation wrapped in tags
|
|
121
|
+
let promptText = `<conversation>\n${conversationText}\n</conversation>\n\n`;
|
|
122
|
+
if (previousSummary) {
|
|
123
|
+
promptText += `<previous-summary>\n${previousSummary}\n</previous-summary>\n\n`;
|
|
124
|
+
}
|
|
125
|
+
promptText += basePrompt;
|
|
126
|
+
const summarizationMessages = [
|
|
127
|
+
{
|
|
128
|
+
role: "user",
|
|
129
|
+
content: [{ type: "text", text: promptText }],
|
|
130
|
+
timestamp: Date.now(),
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
const completionOptions = createSummarizationOptions(model, maxTokens, apiKey, headers, signal, thinkingLevel);
|
|
134
|
+
const response = await completeSummarization(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, completionOptions, streamFn);
|
|
135
|
+
if (response.stopReason === "error") {
|
|
136
|
+
throw new Error(`Summarization failed: ${response.errorMessage || "Unknown error"}`);
|
|
137
|
+
}
|
|
138
|
+
const textContent = response.content
|
|
139
|
+
.filter((c) => c.type === "text")
|
|
140
|
+
.map((c) => c.text)
|
|
141
|
+
.join("\n");
|
|
142
|
+
return textContent;
|
|
143
|
+
}
|
|
144
|
+
const TURN_PREFIX_SUMMARIZATION_PROMPT = `This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained.
|
|
145
|
+
|
|
146
|
+
Summarize the prefix to provide context for the retained suffix:
|
|
147
|
+
|
|
148
|
+
## Original Request
|
|
149
|
+
[What did the user ask for in this turn?]
|
|
150
|
+
|
|
151
|
+
## Early Progress
|
|
152
|
+
- [Key decisions and work done in the prefix]
|
|
153
|
+
|
|
154
|
+
## Context for Suffix
|
|
155
|
+
- [Information needed to understand the retained recent work]
|
|
156
|
+
|
|
157
|
+
Be concise. Focus on what's needed to understand the kept suffix.`;
|
|
158
|
+
/**
|
|
159
|
+
* Generate a summary for a turn prefix (when splitting a turn).
|
|
160
|
+
*/
|
|
161
|
+
export async function generateTurnPrefixSummary(messages, model, reserveTokens, apiKey, headers, signal, thinkingLevel, streamFn) {
|
|
162
|
+
const maxTokens = Math.min(Math.floor(0.5 * reserveTokens), model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY); // Smaller budget for turn prefix
|
|
163
|
+
const llmMessages = convertToLlm(messages);
|
|
164
|
+
const conversationText = serializeConversation(llmMessages);
|
|
165
|
+
const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`;
|
|
166
|
+
const summarizationMessages = [
|
|
167
|
+
{
|
|
168
|
+
role: "user",
|
|
169
|
+
content: [{ type: "text", text: promptText }],
|
|
170
|
+
timestamp: Date.now(),
|
|
171
|
+
},
|
|
172
|
+
];
|
|
173
|
+
const response = await completeSummarization(model, { systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages }, createSummarizationOptions(model, maxTokens, apiKey, headers, signal, thinkingLevel), streamFn);
|
|
174
|
+
if (response.stopReason === "error") {
|
|
175
|
+
throw new Error(`Turn prefix summarization failed: ${response.errorMessage || "Unknown error"}`);
|
|
176
|
+
}
|
|
177
|
+
return response.content
|
|
178
|
+
.filter((c) => c.type === "text")
|
|
179
|
+
.map((c) => c.text)
|
|
180
|
+
.join("\n");
|
|
181
|
+
}
|