gencode-ai 0.3.0 → 0.4.1
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/RELEASE_NOTES_v0.4.0.md +140 -0
- package/dist/agent/agent.d.ts +17 -2
- package/dist/agent/agent.d.ts.map +1 -1
- package/dist/agent/agent.js +279 -49
- package/dist/agent/agent.js.map +1 -1
- package/dist/agent/types.d.ts +15 -1
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/checkpointing/checkpoint-manager.d.ts +24 -0
- package/dist/checkpointing/checkpoint-manager.d.ts.map +1 -1
- package/dist/checkpointing/checkpoint-manager.js +28 -0
- package/dist/checkpointing/checkpoint-manager.js.map +1 -1
- package/dist/cli/components/App.d.ts +8 -0
- package/dist/cli/components/App.d.ts.map +1 -1
- package/dist/cli/components/App.js +478 -36
- package/dist/cli/components/App.js.map +1 -1
- package/dist/cli/components/CommandSuggestions.d.ts.map +1 -1
- package/dist/cli/components/CommandSuggestions.js +2 -0
- package/dist/cli/components/CommandSuggestions.js.map +1 -1
- package/dist/cli/components/Header.d.ts +6 -1
- package/dist/cli/components/Header.d.ts.map +1 -1
- package/dist/cli/components/Header.js +3 -3
- package/dist/cli/components/Header.js.map +1 -1
- package/dist/cli/components/Messages.d.ts.map +1 -1
- package/dist/cli/components/Messages.js +7 -9
- package/dist/cli/components/Messages.js.map +1 -1
- package/dist/cli/index.js +3 -2
- package/dist/cli/index.js.map +1 -1
- package/dist/config/types.d.ts +20 -1
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/input/history-manager.d.ts +78 -0
- package/dist/input/history-manager.d.ts.map +1 -0
- package/dist/input/history-manager.js +224 -0
- package/dist/input/history-manager.js.map +1 -0
- package/dist/input/index.d.ts +6 -0
- package/dist/input/index.d.ts.map +1 -0
- package/dist/input/index.js +5 -0
- package/dist/input/index.js.map +1 -0
- package/dist/prompts/index.js +3 -3
- package/dist/prompts/index.js.map +1 -1
- package/dist/providers/gemini.d.ts.map +1 -1
- package/dist/providers/gemini.js +33 -2
- package/dist/providers/gemini.js.map +1 -1
- package/dist/providers/google.d.ts +22 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +297 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/index.d.ts +4 -4
- package/dist/providers/index.js +11 -11
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +6 -0
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/registry.js +3 -3
- package/dist/providers/registry.js.map +1 -1
- package/dist/providers/types.d.ts +30 -4
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/session/compression/engine.d.ts +109 -0
- package/dist/session/compression/engine.d.ts.map +1 -0
- package/dist/session/compression/engine.js +311 -0
- package/dist/session/compression/engine.js.map +1 -0
- package/dist/session/compression/index.d.ts +12 -0
- package/dist/session/compression/index.d.ts.map +1 -0
- package/dist/session/compression/index.js +11 -0
- package/dist/session/compression/index.js.map +1 -0
- package/dist/session/compression/types.d.ts +90 -0
- package/dist/session/compression/types.d.ts.map +1 -0
- package/dist/session/compression/types.js +17 -0
- package/dist/session/compression/types.js.map +1 -0
- package/dist/session/manager.d.ts +64 -3
- package/dist/session/manager.d.ts.map +1 -1
- package/dist/session/manager.js +254 -2
- package/dist/session/manager.js.map +1 -1
- package/dist/session/types.d.ts +16 -0
- package/dist/session/types.d.ts.map +1 -1
- package/dist/session/types.js.map +1 -1
- package/docs/README.md +1 -0
- package/docs/diagrams/compression-decision.mmd +30 -0
- package/docs/diagrams/compression-workflow.mmd +54 -0
- package/docs/diagrams/layer1-pruning.mmd +45 -0
- package/docs/diagrams/layer2-compaction.mmd +42 -0
- package/docs/proposals/0007-context-management.md +252 -2
- package/docs/proposals/README.md +4 -3
- package/docs/providers.md +3 -3
- package/docs/session-compression.md +695 -0
- package/examples/agent-demo.ts +23 -1
- package/examples/basic.ts +3 -3
- package/package.json +4 -5
- package/src/agent/agent.ts +314 -52
- package/src/agent/types.ts +19 -1
- package/src/checkpointing/checkpoint-manager.ts +48 -0
- package/src/cli/components/App.tsx +553 -34
- package/src/cli/components/CommandSuggestions.tsx +2 -0
- package/src/cli/components/Header.tsx +16 -1
- package/src/cli/components/Messages.tsx +20 -14
- package/src/cli/index.tsx +3 -2
- package/src/config/types.ts +26 -1
- package/src/index.ts +3 -3
- package/src/input/history-manager.ts +289 -0
- package/src/input/index.ts +6 -0
- package/src/prompts/index.test.ts +2 -1
- package/src/prompts/index.ts +3 -3
- package/src/providers/{gemini.ts → google.ts} +69 -18
- package/src/providers/index.ts +14 -14
- package/src/providers/openai.ts +7 -0
- package/src/providers/registry.ts +3 -3
- package/src/providers/types.ts +33 -3
- package/src/session/compression/engine.ts +406 -0
- package/src/session/compression/index.ts +18 -0
- package/src/session/compression/types.ts +102 -0
- package/src/session/manager.ts +326 -3
- package/src/session/types.ts +21 -0
- package/tests/input-history-manager.test.ts +335 -0
- package/tests/session-checkpoint-persistence.test.ts +198 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three-layer compression engine (inspired by OpenCode)
|
|
3
|
+
* Layer 1: Tool output pruning (fast, no cost)
|
|
4
|
+
* Layer 2: Compaction summarization (LLM-based, medium cost)
|
|
5
|
+
* Layer 3: Message filtering (recovery optimization)
|
|
6
|
+
*/
|
|
7
|
+
import type { Message, MessageContent } from '../../providers/types.js';
|
|
8
|
+
import type { CompressionConfig, ConversationSummary, TokenUsage, ModelInfo } from './types.js';
|
|
9
|
+
interface LLMProvider {
|
|
10
|
+
complete(options: {
|
|
11
|
+
model: string;
|
|
12
|
+
messages: Message[];
|
|
13
|
+
maxTokens?: number;
|
|
14
|
+
}): Promise<{
|
|
15
|
+
content: string | MessageContent[];
|
|
16
|
+
}>;
|
|
17
|
+
getModel?(): string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Compression engine implementing OpenCode's three-layer strategy
|
|
21
|
+
*/
|
|
22
|
+
export declare class CompressionEngine {
|
|
23
|
+
private provider;
|
|
24
|
+
private config;
|
|
25
|
+
private readonly CHARS_PER_TOKEN;
|
|
26
|
+
constructor(provider: LLMProvider, config?: Partial<CompressionConfig>);
|
|
27
|
+
/**
|
|
28
|
+
* Estimate tokens using 4:1 character-to-token ratio (OpenCode approach)
|
|
29
|
+
*/
|
|
30
|
+
estimateTokens(text: string): number;
|
|
31
|
+
/**
|
|
32
|
+
* Calculate total tokens from messages
|
|
33
|
+
*/
|
|
34
|
+
calculateTotalTokens(messages: Message[], tokenUsage?: TokenUsage): number;
|
|
35
|
+
/**
|
|
36
|
+
* Calculate usable context space (OpenCode logic)
|
|
37
|
+
*/
|
|
38
|
+
getUsableContext(model: ModelInfo): number;
|
|
39
|
+
/**
|
|
40
|
+
* Check if compression is needed (OpenCode isOverflow logic)
|
|
41
|
+
*/
|
|
42
|
+
needsCompression(messages: Message[], model: ModelInfo, tokenUsage?: TokenUsage): {
|
|
43
|
+
needed: boolean;
|
|
44
|
+
strategy: 'prune' | 'compact' | 'none';
|
|
45
|
+
usagePercent?: number;
|
|
46
|
+
shouldWarn?: boolean;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Layer 1: Tool output pruning (OpenCode pruning logic)
|
|
50
|
+
* Fast and cost-free - removes old tool results
|
|
51
|
+
*/
|
|
52
|
+
pruneToolOutputs(messages: Message[]): Promise<{
|
|
53
|
+
pruned: boolean;
|
|
54
|
+
prunedCount: number;
|
|
55
|
+
savedTokens: number;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* Layer 2: Compaction summarization (OpenCode compaction logic)
|
|
59
|
+
* Generates continuation prompt focusing on future context needs
|
|
60
|
+
*/
|
|
61
|
+
compact(messages: Message[], range: [number, number]): Promise<ConversationSummary>;
|
|
62
|
+
/**
|
|
63
|
+
* Generate continuation prompt (OpenCode style)
|
|
64
|
+
* Focus on what's needed to continue, not just what was done
|
|
65
|
+
*/
|
|
66
|
+
private generateContinuationPrompt;
|
|
67
|
+
/**
|
|
68
|
+
* Extract files modified from tool uses
|
|
69
|
+
*/
|
|
70
|
+
private extractFilesModified;
|
|
71
|
+
/**
|
|
72
|
+
* Extract tool usage statistics
|
|
73
|
+
*/
|
|
74
|
+
private extractToolUsage;
|
|
75
|
+
/**
|
|
76
|
+
* Extract key decisions from conversation
|
|
77
|
+
*/
|
|
78
|
+
private extractKeyDecisions;
|
|
79
|
+
/**
|
|
80
|
+
* Check if message contains tool results
|
|
81
|
+
*/
|
|
82
|
+
private hasToolResults;
|
|
83
|
+
/**
|
|
84
|
+
* Clear tool result content (mark as pruned)
|
|
85
|
+
*/
|
|
86
|
+
private clearToolResults;
|
|
87
|
+
/**
|
|
88
|
+
* Format messages for summary prompt
|
|
89
|
+
*/
|
|
90
|
+
private formatMessagesForSummary;
|
|
91
|
+
/**
|
|
92
|
+
* Extract text content from message content
|
|
93
|
+
*/
|
|
94
|
+
private extractTextContent;
|
|
95
|
+
/**
|
|
96
|
+
* Summarize a tool use
|
|
97
|
+
*/
|
|
98
|
+
private summarizeToolUse;
|
|
99
|
+
/**
|
|
100
|
+
* Extract decision context from content
|
|
101
|
+
*/
|
|
102
|
+
private extractDecisionContext;
|
|
103
|
+
/**
|
|
104
|
+
* Generate unique summary ID
|
|
105
|
+
*/
|
|
106
|
+
private generateId;
|
|
107
|
+
}
|
|
108
|
+
export {};
|
|
109
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../../src/session/compression/engine.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AACxE,OAAO,KAAK,EACV,iBAAiB,EACjB,mBAAmB,EAEnB,UAAU,EACV,SAAS,EACV,MAAM,YAAY,C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QAAQ,EAAE,OAAO,EAAE,EACnB,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GACtB,OAAO,CAAC,mBAAmB,CAAC;IAwB/B;;;OAGG;YACW,0BAA0B;IA6BxC;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAmB5B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IA2BxB;;OAEG;YACW,mBAAmB;IAuBjC;;OAEG;IACH,OAAO,CAAC,cAAc;IAKtB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAaxB;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAahC;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAW1B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAcxB;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAW9B;;OAEG;IACH,OAAO,CAAC,UAAU;CAGnB"}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three-layer compression engine (inspired by OpenCode)
|
|
3
|
+
* Layer 1: Tool output pruning (fast, no cost)
|
|
4
|
+
* Layer 2: Compaction summarization (LLM-based, medium cost)
|
|
5
|
+
* Layer 3: Message filtering (recovery optimization)
|
|
6
|
+
*/
|
|
7
|
+
import { DEFAULT_COMPRESSION_CONFIG } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Compression engine implementing OpenCode's three-layer strategy
|
|
10
|
+
*/
|
|
11
|
+
export class CompressionEngine {
|
|
12
|
+
provider;
|
|
13
|
+
config;
|
|
14
|
+
// OpenCode constants
|
|
15
|
+
CHARS_PER_TOKEN = 4;
|
|
16
|
+
constructor(provider, config) {
|
|
17
|
+
this.provider = provider;
|
|
18
|
+
this.config = { ...DEFAULT_COMPRESSION_CONFIG, ...config };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Estimate tokens using 4:1 character-to-token ratio (OpenCode approach)
|
|
22
|
+
*/
|
|
23
|
+
estimateTokens(text) {
|
|
24
|
+
return Math.max(0, Math.round((text || '').length / this.CHARS_PER_TOKEN));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Calculate total tokens from messages
|
|
28
|
+
*/
|
|
29
|
+
calculateTotalTokens(messages, tokenUsage) {
|
|
30
|
+
// If we have actual token usage data, use it
|
|
31
|
+
if (tokenUsage) {
|
|
32
|
+
return (tokenUsage.input +
|
|
33
|
+
(tokenUsage.cache?.read || 0) +
|
|
34
|
+
tokenUsage.output +
|
|
35
|
+
(tokenUsage.reasoning || 0));
|
|
36
|
+
}
|
|
37
|
+
// Otherwise estimate
|
|
38
|
+
let total = 0;
|
|
39
|
+
for (const msg of messages) {
|
|
40
|
+
const content = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content);
|
|
41
|
+
total += this.estimateTokens(content);
|
|
42
|
+
}
|
|
43
|
+
return total;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Calculate usable context space (OpenCode logic)
|
|
47
|
+
*/
|
|
48
|
+
getUsableContext(model) {
|
|
49
|
+
const maxOutput = Math.min(model.outputLimit || 4096, this.config.reservedOutputTokens);
|
|
50
|
+
return model.contextWindow - maxOutput;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Check if compression is needed (OpenCode isOverflow logic)
|
|
54
|
+
*/
|
|
55
|
+
needsCompression(messages, model, tokenUsage) {
|
|
56
|
+
if (!this.config.enabled) {
|
|
57
|
+
return { needed: false, strategy: 'none' };
|
|
58
|
+
}
|
|
59
|
+
const totalTokens = this.calculateTotalTokens(messages, tokenUsage);
|
|
60
|
+
const usable = this.getUsableContext(model);
|
|
61
|
+
const usagePercent = (totalTokens / usable) * 100;
|
|
62
|
+
// Warn at 80% usage
|
|
63
|
+
const shouldWarn = usagePercent >= 80;
|
|
64
|
+
// Auto-compress at 90% usage or if exceeding usable space
|
|
65
|
+
const needed = totalTokens > usable || usagePercent >= 90;
|
|
66
|
+
if (!needed) {
|
|
67
|
+
return { needed: false, strategy: 'none', usagePercent, shouldWarn };
|
|
68
|
+
}
|
|
69
|
+
// Determine compression strategy
|
|
70
|
+
let strategy = 'compact';
|
|
71
|
+
if (totalTokens > this.config.pruneMinimum && this.config.enablePruning) {
|
|
72
|
+
strategy = 'prune';
|
|
73
|
+
}
|
|
74
|
+
return { needed: true, strategy, usagePercent, shouldWarn };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Layer 1: Tool output pruning (OpenCode pruning logic)
|
|
78
|
+
* Fast and cost-free - removes old tool results
|
|
79
|
+
*/
|
|
80
|
+
async pruneToolOutputs(messages) {
|
|
81
|
+
const totalTokens = this.calculateTotalTokens(messages);
|
|
82
|
+
if (totalTokens < this.config.pruneMinimum) {
|
|
83
|
+
return { pruned: false, prunedCount: 0, savedTokens: 0 };
|
|
84
|
+
}
|
|
85
|
+
// Collect recent tool outputs (protect last 40k tokens)
|
|
86
|
+
let protectedTokens = 0;
|
|
87
|
+
const protectedIndices = new Set();
|
|
88
|
+
for (let i = messages.length - 1; i >= 0 && protectedTokens < this.config.pruneProtect; i--) {
|
|
89
|
+
const msg = messages[i];
|
|
90
|
+
if (this.hasToolResults(msg)) {
|
|
91
|
+
const msgTokens = this.calculateTotalTokens([msg]);
|
|
92
|
+
protectedTokens += msgTokens;
|
|
93
|
+
protectedIndices.add(i);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Prune older tool outputs
|
|
97
|
+
let prunedCount = 0;
|
|
98
|
+
let savedTokens = 0;
|
|
99
|
+
for (let i = 0; i < messages.length; i++) {
|
|
100
|
+
if (!protectedIndices.has(i) && this.hasToolResults(messages[i])) {
|
|
101
|
+
const before = this.calculateTotalTokens([messages[i]]);
|
|
102
|
+
this.clearToolResults(messages[i]);
|
|
103
|
+
const after = this.calculateTotalTokens([messages[i]]);
|
|
104
|
+
savedTokens += before - after;
|
|
105
|
+
prunedCount++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return { pruned: prunedCount > 0, prunedCount, savedTokens };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Layer 2: Compaction summarization (OpenCode compaction logic)
|
|
112
|
+
* Generates continuation prompt focusing on future context needs
|
|
113
|
+
*/
|
|
114
|
+
async compact(messages, range) {
|
|
115
|
+
const toSummarize = messages.slice(range[0], range[1] + 1);
|
|
116
|
+
// Extract structured information
|
|
117
|
+
const filesModified = this.extractFilesModified(toSummarize);
|
|
118
|
+
const toolsUsed = this.extractToolUsage(toSummarize);
|
|
119
|
+
const keyDecisions = await this.extractKeyDecisions(toSummarize);
|
|
120
|
+
// Generate continuation prompt (OpenCode style)
|
|
121
|
+
const continuationPrompt = await this.generateContinuationPrompt(toSummarize);
|
|
122
|
+
return {
|
|
123
|
+
id: this.generateId(),
|
|
124
|
+
type: 'compaction',
|
|
125
|
+
coveringMessages: range,
|
|
126
|
+
content: continuationPrompt,
|
|
127
|
+
keyDecisions,
|
|
128
|
+
filesModified,
|
|
129
|
+
toolsUsed,
|
|
130
|
+
generatedAt: new Date().toISOString(),
|
|
131
|
+
estimatedTokens: this.estimateTokens(continuationPrompt),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Generate continuation prompt (OpenCode style)
|
|
136
|
+
* Focus on what's needed to continue, not just what was done
|
|
137
|
+
*/
|
|
138
|
+
async generateContinuationPrompt(messages) {
|
|
139
|
+
const prompt = `Provide a detailed prompt for continuing our conversation above.
|
|
140
|
+
|
|
141
|
+
Focus on information that would be helpful for continuing the conversation:
|
|
142
|
+
1. What we accomplished so far
|
|
143
|
+
2. What we're currently working on
|
|
144
|
+
3. Which files we modified and key changes made
|
|
145
|
+
4. What we plan to do next
|
|
146
|
+
5. Any important context or decisions that would be needed
|
|
147
|
+
|
|
148
|
+
Remember: The new session will NOT have access to our full conversation history,
|
|
149
|
+
so include all essential context needed to continue working effectively.
|
|
150
|
+
|
|
151
|
+
Be technical and specific. Use structured bullet points.
|
|
152
|
+
|
|
153
|
+
Conversation:
|
|
154
|
+
${this.formatMessagesForSummary(messages)}
|
|
155
|
+
|
|
156
|
+
Continuation Prompt:`;
|
|
157
|
+
const response = await this.provider.complete({
|
|
158
|
+
model: this.config.model ?? (this.provider.getModel?.() || 'unknown'),
|
|
159
|
+
messages: [{ role: 'user', content: prompt }],
|
|
160
|
+
maxTokens: 1500, // Larger for continuation prompt
|
|
161
|
+
});
|
|
162
|
+
return this.extractTextContent(response.content);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Extract files modified from tool uses
|
|
166
|
+
*/
|
|
167
|
+
extractFilesModified(messages) {
|
|
168
|
+
const files = new Set();
|
|
169
|
+
for (const msg of messages) {
|
|
170
|
+
if (typeof msg.content !== 'string') {
|
|
171
|
+
for (const block of msg.content) {
|
|
172
|
+
if (block.type === 'tool_use') {
|
|
173
|
+
if (['Write', 'Edit'].includes(block.name)) {
|
|
174
|
+
const filePath = block.input.file_path;
|
|
175
|
+
if (filePath)
|
|
176
|
+
files.add(filePath);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return Array.from(files);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Extract tool usage statistics
|
|
186
|
+
*/
|
|
187
|
+
extractToolUsage(messages) {
|
|
188
|
+
const toolStats = new Map();
|
|
189
|
+
for (const msg of messages) {
|
|
190
|
+
if (typeof msg.content !== 'string') {
|
|
191
|
+
for (const block of msg.content) {
|
|
192
|
+
if (block.type === 'tool_use') {
|
|
193
|
+
const stats = toolStats.get(block.name) || { count: 0, uses: [] };
|
|
194
|
+
stats.count++;
|
|
195
|
+
if (stats.uses.length < 3) {
|
|
196
|
+
stats.uses.push(this.summarizeToolUse(block));
|
|
197
|
+
}
|
|
198
|
+
toolStats.set(block.name, stats);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return Array.from(toolStats.entries()).map(([tool, stats]) => ({
|
|
204
|
+
tool,
|
|
205
|
+
count: stats.count,
|
|
206
|
+
notableUses: stats.uses,
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Extract key decisions from conversation
|
|
211
|
+
*/
|
|
212
|
+
async extractKeyDecisions(messages) {
|
|
213
|
+
const decisions = [];
|
|
214
|
+
const decisionIndicators = ['decided to', 'chose to', 'will use', 'going with'];
|
|
215
|
+
for (const msg of messages) {
|
|
216
|
+
const content = typeof msg.content === 'string' ? msg.content : '';
|
|
217
|
+
// Look for decision indicators
|
|
218
|
+
const hasDecision = decisionIndicators.some((indicator) => content.includes(indicator));
|
|
219
|
+
if (hasDecision) {
|
|
220
|
+
const decision = this.extractDecisionContext(content);
|
|
221
|
+
if (decision)
|
|
222
|
+
decisions.push(decision);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return decisions.slice(0, 5); // Keep top 5
|
|
226
|
+
}
|
|
227
|
+
// ===== Helper Methods =====
|
|
228
|
+
/**
|
|
229
|
+
* Check if message contains tool results
|
|
230
|
+
*/
|
|
231
|
+
hasToolResults(message) {
|
|
232
|
+
if (typeof message.content === 'string')
|
|
233
|
+
return false;
|
|
234
|
+
return message.content.some((block) => block.type === 'tool_result');
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Clear tool result content (mark as pruned)
|
|
238
|
+
*/
|
|
239
|
+
clearToolResults(message) {
|
|
240
|
+
if (typeof message.content !== 'string') {
|
|
241
|
+
for (const block of message.content) {
|
|
242
|
+
if (block.type === 'tool_result') {
|
|
243
|
+
// Mark as pruned (OpenCode style)
|
|
244
|
+
block.content = '[Old tool result content cleared]';
|
|
245
|
+
block.pruned = true;
|
|
246
|
+
block.prunedAt = new Date().toISOString();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Format messages for summary prompt
|
|
253
|
+
*/
|
|
254
|
+
formatMessagesForSummary(messages) {
|
|
255
|
+
return messages
|
|
256
|
+
.map((msg, idx) => {
|
|
257
|
+
const role = msg.role.toUpperCase();
|
|
258
|
+
const content = typeof msg.content === 'string'
|
|
259
|
+
? msg.content
|
|
260
|
+
: this.extractTextContent(msg.content);
|
|
261
|
+
return `[${idx + 1}] ${role}: ${content.slice(0, 500)}`;
|
|
262
|
+
})
|
|
263
|
+
.join('\n\n');
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Extract text content from message content
|
|
267
|
+
*/
|
|
268
|
+
extractTextContent(content) {
|
|
269
|
+
if (typeof content === 'string')
|
|
270
|
+
return content;
|
|
271
|
+
if (Array.isArray(content)) {
|
|
272
|
+
return content
|
|
273
|
+
.filter((c) => c.type === 'text')
|
|
274
|
+
.map((c) => c.text)
|
|
275
|
+
.join(' ');
|
|
276
|
+
}
|
|
277
|
+
return '';
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Summarize a tool use
|
|
281
|
+
*/
|
|
282
|
+
summarizeToolUse(block) {
|
|
283
|
+
switch (block.name) {
|
|
284
|
+
case 'Write':
|
|
285
|
+
case 'Edit':
|
|
286
|
+
return `Modified ${block.input.file_path}`;
|
|
287
|
+
case 'Bash':
|
|
288
|
+
return `Ran: ${block.input.command?.slice(0, 50)}`;
|
|
289
|
+
case 'Read':
|
|
290
|
+
return `Read ${block.input.file_path}`;
|
|
291
|
+
default:
|
|
292
|
+
return `Used ${block.name}`;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Extract decision context from content
|
|
297
|
+
*/
|
|
298
|
+
extractDecisionContext(content) {
|
|
299
|
+
const sentences = content.split(/[.!?]/);
|
|
300
|
+
const decisionKeywords = ['decided', 'chose', 'will use', 'going with'];
|
|
301
|
+
const decisionSentence = sentences.find((s) => decisionKeywords.some((keyword) => s.includes(keyword)));
|
|
302
|
+
return decisionSentence?.trim() || null;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Generate unique summary ID
|
|
306
|
+
*/
|
|
307
|
+
generateId() {
|
|
308
|
+
return `sum-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../../../src/session/compression/engine.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAUH,OAAO,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAYxD;;GAEG;AACH,MAAM,OAAO,iBAAiB;IACpB,QAAQ,CAAc;IACtB,MAAM,CAAoB;IAElC,qBAAqB;IACJ,eAAe,GAAG,CAAC,CAAC;IAErC,YAAY,QAAqB,EAAE,MAAmC;QACpE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,0BAA0B,EAAE,GAAG,MAAM,EAAE,CAAC;IAC7D,CAAC;IAED;;OAEG;IACH,cAAc,CAAC,IAAY;QACzB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED;;OAEG;IACH,oBAAoB,CAAC,QAAmB,EAAE,UAAuB;QAC/D,6CAA6C;QAC7C,IAAI,UAAU,EAAE,CAAC;YACf,OAAO,CACL,UAAU,CAAC,KAAK;gBAChB,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,IAAI,CAAC,CAAC;gBAC7B,UAAU,CAAC,MAAM;gBACjB,CAAC,UAAU,CAAC,SAAS,IAAI,CAAC,CAAC,CAC5B,CAAC;QACJ,CAAC;QAED,qBAAqB;QACrB,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;YAC3B,MAAM,OAAO,G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+1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Compression Module
|
|
3
|
+
*
|
|
4
|
+
* Implements OpenCode-inspired three-layer compression strategy:
|
|
5
|
+
* - Layer 1: Tool output pruning (fast, no cost)
|
|
6
|
+
* - Layer 2: Compaction summarization (LLM-based, medium cost)
|
|
7
|
+
* - Layer 3: Message filtering (recovery optimization)
|
|
8
|
+
*/
|
|
9
|
+
export { CompressionEngine } from './engine.js';
|
|
10
|
+
export type { CompressionConfig, ConversationSummary, ToolUsageSummary, TokenUsage, ModelInfo, } from './types.js';
|
|
11
|
+
export { DEFAULT_COMPRESSION_CONFIG } from './types.js';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/session/compression/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,YAAY,EACV,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,EACV,SAAS,GACV,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Compression Module
|
|
3
|
+
*
|
|
4
|
+
* Implements OpenCode-inspired three-layer compression strategy:
|
|
5
|
+
* - Layer 1: Tool output pruning (fast, no cost)
|
|
6
|
+
* - Layer 2: Compaction summarization (LLM-based, medium cost)
|
|
7
|
+
* - Layer 3: Message filtering (recovery optimization)
|
|
8
|
+
*/
|
|
9
|
+
export { CompressionEngine } from './engine.js';
|
|
10
|
+
export { DEFAULT_COMPRESSION_CONFIG } from './types.js';
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/session/compression/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAQhD,OAAO,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compression and summarization types for session management
|
|
3
|
+
* Inspired by OpenCode's multi-layer compression strategy
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Token usage tracking with fine-grained breakdown
|
|
7
|
+
*/
|
|
8
|
+
export interface TokenUsage {
|
|
9
|
+
/** Regular input tokens */
|
|
10
|
+
input: number;
|
|
11
|
+
/** Model output tokens */
|
|
12
|
+
output: number;
|
|
13
|
+
/** Reasoning tokens (for thinking models like o1/o3) */
|
|
14
|
+
reasoning?: number;
|
|
15
|
+
/** Cache statistics */
|
|
16
|
+
cache?: {
|
|
17
|
+
/** Cache hit tokens (~10% cost) */
|
|
18
|
+
read: number;
|
|
19
|
+
/** Cache creation tokens */
|
|
20
|
+
write: number;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Compression configuration
|
|
25
|
+
*/
|
|
26
|
+
export interface CompressionConfig {
|
|
27
|
+
/** Enable/disable compression */
|
|
28
|
+
enabled: boolean;
|
|
29
|
+
/** Enable Layer 1: Tool output pruning */
|
|
30
|
+
enablePruning: boolean;
|
|
31
|
+
/** Enable Layer 2: Compaction summarization */
|
|
32
|
+
enableCompaction: boolean;
|
|
33
|
+
/** Minimum tokens to trigger pruning (default: 20k) */
|
|
34
|
+
pruneMinimum: number;
|
|
35
|
+
/** Protect recent tokens from pruning (default: 40k) */
|
|
36
|
+
pruneProtect: number;
|
|
37
|
+
/** Reserved output tokens (default: 32k) */
|
|
38
|
+
reservedOutputTokens: number;
|
|
39
|
+
/** Model to use for summarization (optional, defaults to current model) */
|
|
40
|
+
model?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Tool usage summary
|
|
44
|
+
*/
|
|
45
|
+
export interface ToolUsageSummary {
|
|
46
|
+
/** Tool name */
|
|
47
|
+
tool: string;
|
|
48
|
+
/** Number of times used */
|
|
49
|
+
count: number;
|
|
50
|
+
/** Notable uses (up to 3) */
|
|
51
|
+
notableUses: string[];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Conversation summary metadata
|
|
55
|
+
*/
|
|
56
|
+
export interface ConversationSummary {
|
|
57
|
+
/** Unique summary ID */
|
|
58
|
+
id: string;
|
|
59
|
+
/** Summary type (compaction) */
|
|
60
|
+
type: 'compaction';
|
|
61
|
+
/** Range of messages covered [start, end] */
|
|
62
|
+
coveringMessages: [number, number];
|
|
63
|
+
/** Narrative summary content (continuation prompt) */
|
|
64
|
+
content: string;
|
|
65
|
+
/** Key decisions made */
|
|
66
|
+
keyDecisions: string[];
|
|
67
|
+
/** Files modified */
|
|
68
|
+
filesModified: string[];
|
|
69
|
+
/** Tools used with statistics */
|
|
70
|
+
toolsUsed: ToolUsageSummary[];
|
|
71
|
+
/** Generation timestamp */
|
|
72
|
+
generatedAt: string;
|
|
73
|
+
/** Estimated tokens in summary */
|
|
74
|
+
estimatedTokens: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Model information for compression decisions
|
|
78
|
+
*/
|
|
79
|
+
export interface ModelInfo {
|
|
80
|
+
/** Model context window size */
|
|
81
|
+
contextWindow: number;
|
|
82
|
+
/** Model output limit (optional) */
|
|
83
|
+
outputLimit?: number;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Default compression configuration
|
|
87
|
+
* Based on OpenCode constants
|
|
88
|
+
*/
|
|
89
|
+
export declare const DEFAULT_COMPRESSION_CONFIG: CompressionConfig;
|
|
90
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/session/compression/types.ts"],"names":[],"mappings":"AAAA;;;GAGG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;IACjC,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAC9B,2BAA2B;IAC3B,WAAW,EAAE,MAAM,CAAC;IACpB,kCAAkC;IAClC,eAAe,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,gCAAgC;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,oCAAoC;IACpC,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;GAGG;AACH,eAAO,MAAM,0BAA0B,EAAE,iBAOxC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compression and summarization types for session management
|
|
3
|
+
* Inspired by OpenCode's multi-layer compression strategy
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Default compression configuration
|
|
7
|
+
* Based on OpenCode constants
|
|
8
|
+
*/
|
|
9
|
+
export const DEFAULT_COMPRESSION_CONFIG = {
|
|
10
|
+
enabled: true,
|
|
11
|
+
enablePruning: true,
|
|
12
|
+
enableCompaction: true,
|
|
13
|
+
pruneMinimum: 20_000, // Minimum tokens to trigger pruning
|
|
14
|
+
pruneProtect: 40_000, // Protect recent 40k tokens
|
|
15
|
+
reservedOutputTokens: 32_000, // Reserve 32k for output
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/session/compression/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAuFH;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAsB;IAC3D,OAAO,EAAE,IAAI;IACb,aAAa,EAAE,IAAI;IACnB,gBAAgB,EAAE,IAAI;IACtB,YAAY,EAAE,MAAM,EAAO,oCAAoC;IAC/D,YAAY,EAAE,MAAM,EAAO,4BAA4B;IACvD,oBAAoB,EAAE,MAAM,EAAE,yBAAyB;CACxD,CAAC"}
|