@blockrun/runcode 2.3.0 → 2.4.0
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/dist/agent/loop.js +10 -1
- package/dist/compression/adapter.d.ts +13 -0
- package/dist/compression/adapter.js +104 -0
- package/dist/compression/codebook.d.ts +23 -0
- package/dist/compression/codebook.js +118 -0
- package/dist/compression/index.d.ts +32 -0
- package/dist/compression/index.js +258 -0
- package/dist/compression/layers/deduplication.d.ts +27 -0
- package/dist/compression/layers/deduplication.js +97 -0
- package/dist/compression/layers/dictionary.d.ts +20 -0
- package/dist/compression/layers/dictionary.js +67 -0
- package/dist/compression/layers/dynamic-codebook.d.ts +25 -0
- package/dist/compression/layers/dynamic-codebook.js +145 -0
- package/dist/compression/layers/json-compact.d.ts +22 -0
- package/dist/compression/layers/json-compact.js +74 -0
- package/dist/compression/layers/observation.d.ts +20 -0
- package/dist/compression/layers/observation.js +126 -0
- package/dist/compression/layers/paths.d.ts +23 -0
- package/dist/compression/layers/paths.js +107 -0
- package/dist/compression/layers/whitespace.d.ts +26 -0
- package/dist/compression/layers/whitespace.js +57 -0
- package/dist/compression/types.d.ts +83 -0
- package/dist/compression/types.js +26 -0
- package/package.json +1 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layer 1: Message Deduplication
|
|
3
|
+
*
|
|
4
|
+
* Removes exact duplicate messages from conversation history.
|
|
5
|
+
* Common in heartbeat patterns and repeated tool calls.
|
|
6
|
+
*
|
|
7
|
+
* Safe for LLM: Identical messages add no new information.
|
|
8
|
+
* Expected savings: 2-5%
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from 'node:crypto';
|
|
11
|
+
/**
|
|
12
|
+
* Generate a hash for a message based on its semantic content.
|
|
13
|
+
* Uses role + content + tool_call_id to identify duplicates.
|
|
14
|
+
*/
|
|
15
|
+
function hashMessage(message) {
|
|
16
|
+
const parts = [
|
|
17
|
+
message.role,
|
|
18
|
+
message.content || "",
|
|
19
|
+
message.tool_call_id || "",
|
|
20
|
+
message.name || "",
|
|
21
|
+
];
|
|
22
|
+
// Include tool_calls if present
|
|
23
|
+
if (message.tool_calls) {
|
|
24
|
+
parts.push(JSON.stringify(message.tool_calls.map((tc) => ({
|
|
25
|
+
name: tc.function.name,
|
|
26
|
+
args: tc.function.arguments,
|
|
27
|
+
}))));
|
|
28
|
+
}
|
|
29
|
+
const content = parts.join("|");
|
|
30
|
+
return createHash("md5").update(content).digest("hex");
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Remove exact duplicate messages from the conversation.
|
|
34
|
+
*
|
|
35
|
+
* Strategy:
|
|
36
|
+
* - Keep first occurrence of each unique message
|
|
37
|
+
* - Preserve order for semantic coherence
|
|
38
|
+
* - Never dedupe system messages (they set context)
|
|
39
|
+
* - Allow duplicate user messages (user might repeat intentionally)
|
|
40
|
+
* - CRITICAL: Never dedupe assistant messages with tool_calls that are
|
|
41
|
+
* referenced by subsequent tool messages (breaks Anthropic tool_use/tool_result pairing)
|
|
42
|
+
*/
|
|
43
|
+
export function deduplicateMessages(messages) {
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
const result = [];
|
|
46
|
+
let duplicatesRemoved = 0;
|
|
47
|
+
// First pass: collect all tool_call_ids that are referenced by tool messages
|
|
48
|
+
// These tool_calls MUST be preserved to maintain tool_use/tool_result pairing
|
|
49
|
+
const referencedToolCallIds = new Set();
|
|
50
|
+
for (const message of messages) {
|
|
51
|
+
if (message.role === "tool" && message.tool_call_id) {
|
|
52
|
+
referencedToolCallIds.add(message.tool_call_id);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const message of messages) {
|
|
56
|
+
// Always keep system messages (they set important context)
|
|
57
|
+
if (message.role === "system") {
|
|
58
|
+
result.push(message);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
// Always keep user messages (user might repeat intentionally)
|
|
62
|
+
if (message.role === "user") {
|
|
63
|
+
result.push(message);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
// Always keep tool messages (they are results of tool calls)
|
|
67
|
+
// Removing them would break the tool_use/tool_result pairing
|
|
68
|
+
if (message.role === "tool") {
|
|
69
|
+
result.push(message);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
// For assistant messages with tool_calls, check if any are referenced
|
|
73
|
+
// by subsequent tool messages - if so, we MUST keep this message
|
|
74
|
+
if (message.role === "assistant" && message.tool_calls) {
|
|
75
|
+
const hasReferencedToolCall = message.tool_calls.some((tc) => referencedToolCallIds.has(tc.id));
|
|
76
|
+
if (hasReferencedToolCall) {
|
|
77
|
+
// This assistant message has tool_calls that are referenced - keep it
|
|
78
|
+
result.push(message);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// For other assistant messages, check for duplicates
|
|
83
|
+
const hash = hashMessage(message);
|
|
84
|
+
if (!seen.has(hash)) {
|
|
85
|
+
seen.add(hash);
|
|
86
|
+
result.push(message);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
duplicatesRemoved++;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
messages: result,
|
|
94
|
+
duplicatesRemoved,
|
|
95
|
+
originalCount: messages.length,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layer 3: Dictionary Encoding
|
|
3
|
+
*
|
|
4
|
+
* Replaces frequently repeated long phrases with short codes.
|
|
5
|
+
* Uses a static codebook of common patterns from production logs.
|
|
6
|
+
*
|
|
7
|
+
* Safe for LLM: Reversible substitution with codebook header.
|
|
8
|
+
* Expected savings: 4-8%
|
|
9
|
+
*/
|
|
10
|
+
import { NormalizedMessage } from "../types.js";
|
|
11
|
+
export interface DictionaryResult {
|
|
12
|
+
messages: NormalizedMessage[];
|
|
13
|
+
substitutionCount: number;
|
|
14
|
+
usedCodes: Set<string>;
|
|
15
|
+
charsSaved: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Apply dictionary encoding to all messages.
|
|
19
|
+
*/
|
|
20
|
+
export declare function encodeMessages(messages: NormalizedMessage[]): DictionaryResult;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layer 3: Dictionary Encoding
|
|
3
|
+
*
|
|
4
|
+
* Replaces frequently repeated long phrases with short codes.
|
|
5
|
+
* Uses a static codebook of common patterns from production logs.
|
|
6
|
+
*
|
|
7
|
+
* Safe for LLM: Reversible substitution with codebook header.
|
|
8
|
+
* Expected savings: 4-8%
|
|
9
|
+
*/
|
|
10
|
+
import { getInverseCodebook } from "../codebook.js";
|
|
11
|
+
/**
|
|
12
|
+
* Apply dictionary encoding to a string.
|
|
13
|
+
* Returns the encoded string and stats.
|
|
14
|
+
*/
|
|
15
|
+
function encodeContent(content, inverseCodebook) {
|
|
16
|
+
let encoded = content;
|
|
17
|
+
let substitutions = 0;
|
|
18
|
+
let charsSaved = 0;
|
|
19
|
+
const codes = new Set();
|
|
20
|
+
// Sort phrases by length (longest first) to avoid partial matches
|
|
21
|
+
const phrases = Object.keys(inverseCodebook).sort((a, b) => b.length - a.length);
|
|
22
|
+
for (const phrase of phrases) {
|
|
23
|
+
const code = inverseCodebook[phrase];
|
|
24
|
+
const regex = new RegExp(escapeRegex(phrase), "g");
|
|
25
|
+
const matches = encoded.match(regex);
|
|
26
|
+
if (matches && matches.length > 0) {
|
|
27
|
+
encoded = encoded.replace(regex, code);
|
|
28
|
+
substitutions += matches.length;
|
|
29
|
+
charsSaved += matches.length * (phrase.length - code.length);
|
|
30
|
+
codes.add(code);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { encoded, substitutions, codes, charsSaved };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Escape special regex characters in a string.
|
|
37
|
+
*/
|
|
38
|
+
function escapeRegex(str) {
|
|
39
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Apply dictionary encoding to all messages.
|
|
43
|
+
*/
|
|
44
|
+
export function encodeMessages(messages) {
|
|
45
|
+
const inverseCodebook = getInverseCodebook();
|
|
46
|
+
let totalSubstitutions = 0;
|
|
47
|
+
let totalCharsSaved = 0;
|
|
48
|
+
const allUsedCodes = new Set();
|
|
49
|
+
const result = messages.map((message) => {
|
|
50
|
+
if (!message.content || typeof message.content !== "string")
|
|
51
|
+
return message;
|
|
52
|
+
const { encoded, substitutions, codes, charsSaved } = encodeContent(message.content, inverseCodebook);
|
|
53
|
+
totalSubstitutions += substitutions;
|
|
54
|
+
totalCharsSaved += charsSaved;
|
|
55
|
+
codes.forEach((code) => allUsedCodes.add(code));
|
|
56
|
+
return {
|
|
57
|
+
...message,
|
|
58
|
+
content: encoded,
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
messages: result,
|
|
63
|
+
substitutionCount: totalSubstitutions,
|
|
64
|
+
usedCodes: allUsedCodes,
|
|
65
|
+
charsSaved: totalCharsSaved,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L7: Dynamic Codebook Builder
|
|
3
|
+
*
|
|
4
|
+
* Inspired by claw-compactor's frequency-based codebook.
|
|
5
|
+
* Builds codebook from actual content being compressed,
|
|
6
|
+
* rather than relying on static patterns.
|
|
7
|
+
*
|
|
8
|
+
* Finds phrases that appear 3+ times and replaces with short codes.
|
|
9
|
+
*/
|
|
10
|
+
import { NormalizedMessage } from "../types.js";
|
|
11
|
+
interface DynamicCodebookResult {
|
|
12
|
+
messages: NormalizedMessage[];
|
|
13
|
+
charsSaved: number;
|
|
14
|
+
dynamicCodes: Record<string, string>;
|
|
15
|
+
substitutions: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Apply dynamic codebook to messages.
|
|
19
|
+
*/
|
|
20
|
+
export declare function applyDynamicCodebook(messages: NormalizedMessage[]): DynamicCodebookResult;
|
|
21
|
+
/**
|
|
22
|
+
* Generate header for dynamic codes (to include in system message).
|
|
23
|
+
*/
|
|
24
|
+
export declare function generateDynamicCodebookHeader(codebook: Record<string, string>): string;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L7: Dynamic Codebook Builder
|
|
3
|
+
*
|
|
4
|
+
* Inspired by claw-compactor's frequency-based codebook.
|
|
5
|
+
* Builds codebook from actual content being compressed,
|
|
6
|
+
* rather than relying on static patterns.
|
|
7
|
+
*
|
|
8
|
+
* Finds phrases that appear 3+ times and replaces with short codes.
|
|
9
|
+
*/
|
|
10
|
+
// Config
|
|
11
|
+
const MIN_PHRASE_LENGTH = 20;
|
|
12
|
+
const MAX_PHRASE_LENGTH = 200;
|
|
13
|
+
const MIN_FREQUENCY = 3;
|
|
14
|
+
const MAX_ENTRIES = 100;
|
|
15
|
+
const CODE_PREFIX = "$D"; // Dynamic codes: $D01, $D02, etc.
|
|
16
|
+
/**
|
|
17
|
+
* Find repeated phrases in content.
|
|
18
|
+
*/
|
|
19
|
+
function findRepeatedPhrases(allContent) {
|
|
20
|
+
const phrases = new Map();
|
|
21
|
+
// Split by sentence-like boundaries
|
|
22
|
+
const segments = allContent.split(/(?<=[.!?\n])\s+/);
|
|
23
|
+
for (const segment of segments) {
|
|
24
|
+
const trimmed = segment.trim();
|
|
25
|
+
if (trimmed.length >= MIN_PHRASE_LENGTH &&
|
|
26
|
+
trimmed.length <= MAX_PHRASE_LENGTH) {
|
|
27
|
+
phrases.set(trimmed, (phrases.get(trimmed) || 0) + 1);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// Also find repeated lines
|
|
31
|
+
const lines = allContent.split("\n");
|
|
32
|
+
for (const line of lines) {
|
|
33
|
+
const trimmed = line.trim();
|
|
34
|
+
if (trimmed.length >= MIN_PHRASE_LENGTH &&
|
|
35
|
+
trimmed.length <= MAX_PHRASE_LENGTH) {
|
|
36
|
+
phrases.set(trimmed, (phrases.get(trimmed) || 0) + 1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return phrases;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Build dynamic codebook from message content.
|
|
43
|
+
*/
|
|
44
|
+
function buildDynamicCodebook(messages) {
|
|
45
|
+
// Combine all content
|
|
46
|
+
let allContent = "";
|
|
47
|
+
for (const msg of messages) {
|
|
48
|
+
if (msg.content) {
|
|
49
|
+
allContent += msg.content + "\n";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Find repeated phrases
|
|
53
|
+
const phrases = findRepeatedPhrases(allContent);
|
|
54
|
+
// Filter by frequency and sort by savings potential
|
|
55
|
+
const candidates = [];
|
|
56
|
+
for (const [phrase, count] of phrases.entries()) {
|
|
57
|
+
if (count >= MIN_FREQUENCY) {
|
|
58
|
+
// Savings = (phrase length - code length) * occurrences
|
|
59
|
+
const codeLength = 4; // e.g., "$D01"
|
|
60
|
+
const savings = (phrase.length - codeLength) * count;
|
|
61
|
+
if (savings > 50) {
|
|
62
|
+
candidates.push({ phrase, count, savings });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Sort by savings (descending) and take top entries
|
|
67
|
+
candidates.sort((a, b) => b.savings - a.savings);
|
|
68
|
+
const topCandidates = candidates.slice(0, MAX_ENTRIES);
|
|
69
|
+
// Build codebook
|
|
70
|
+
const codebook = {};
|
|
71
|
+
topCandidates.forEach((c, i) => {
|
|
72
|
+
const code = `${CODE_PREFIX}${String(i + 1).padStart(2, "0")}`;
|
|
73
|
+
codebook[code] = c.phrase;
|
|
74
|
+
});
|
|
75
|
+
return codebook;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Escape special regex characters.
|
|
79
|
+
*/
|
|
80
|
+
function escapeRegex(str) {
|
|
81
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Apply dynamic codebook to messages.
|
|
85
|
+
*/
|
|
86
|
+
export function applyDynamicCodebook(messages) {
|
|
87
|
+
// Build codebook from content
|
|
88
|
+
const codebook = buildDynamicCodebook(messages);
|
|
89
|
+
if (Object.keys(codebook).length === 0) {
|
|
90
|
+
return {
|
|
91
|
+
messages,
|
|
92
|
+
charsSaved: 0,
|
|
93
|
+
dynamicCodes: {},
|
|
94
|
+
substitutions: 0,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
// Create inverse map for replacement
|
|
98
|
+
const phraseToCode = {};
|
|
99
|
+
for (const [code, phrase] of Object.entries(codebook)) {
|
|
100
|
+
phraseToCode[phrase] = code;
|
|
101
|
+
}
|
|
102
|
+
// Sort phrases by length (longest first) to avoid partial replacements
|
|
103
|
+
const sortedPhrases = Object.keys(phraseToCode).sort((a, b) => b.length - a.length);
|
|
104
|
+
let charsSaved = 0;
|
|
105
|
+
let substitutions = 0;
|
|
106
|
+
// Apply replacements
|
|
107
|
+
const result = messages.map((msg) => {
|
|
108
|
+
if (!msg.content || typeof msg.content !== "string")
|
|
109
|
+
return msg;
|
|
110
|
+
let content = msg.content;
|
|
111
|
+
for (const phrase of sortedPhrases) {
|
|
112
|
+
const code = phraseToCode[phrase];
|
|
113
|
+
const regex = new RegExp(escapeRegex(phrase), "g");
|
|
114
|
+
const matches = content.match(regex);
|
|
115
|
+
if (matches) {
|
|
116
|
+
content = content.replace(regex, code);
|
|
117
|
+
charsSaved += (phrase.length - code.length) * matches.length;
|
|
118
|
+
substitutions += matches.length;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return { ...msg, content };
|
|
122
|
+
});
|
|
123
|
+
return {
|
|
124
|
+
messages: result,
|
|
125
|
+
charsSaved,
|
|
126
|
+
dynamicCodes: codebook,
|
|
127
|
+
substitutions,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Generate header for dynamic codes (to include in system message).
|
|
132
|
+
*/
|
|
133
|
+
export function generateDynamicCodebookHeader(codebook) {
|
|
134
|
+
if (Object.keys(codebook).length === 0)
|
|
135
|
+
return "";
|
|
136
|
+
const entries = Object.entries(codebook)
|
|
137
|
+
.slice(0, 20) // Limit header size
|
|
138
|
+
.map(([code, phrase]) => {
|
|
139
|
+
// Truncate long phrases in header
|
|
140
|
+
const displayPhrase = phrase.length > 40 ? phrase.slice(0, 37) + "..." : phrase;
|
|
141
|
+
return `${code}=${displayPhrase}`;
|
|
142
|
+
})
|
|
143
|
+
.join(", ");
|
|
144
|
+
return `[DynDict: ${entries}]`;
|
|
145
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layer 5: JSON Compaction
|
|
3
|
+
*
|
|
4
|
+
* Minifies JSON in tool_call arguments and tool results.
|
|
5
|
+
* Removes pretty-print whitespace from JSON strings.
|
|
6
|
+
*
|
|
7
|
+
* Safe for LLM: JSON semantics unchanged.
|
|
8
|
+
* Expected savings: 2-4%
|
|
9
|
+
*/
|
|
10
|
+
import { NormalizedMessage } from "../types.js";
|
|
11
|
+
export interface JsonCompactResult {
|
|
12
|
+
messages: NormalizedMessage[];
|
|
13
|
+
charsSaved: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Apply JSON compaction to all messages.
|
|
17
|
+
*
|
|
18
|
+
* Targets:
|
|
19
|
+
* - tool_call arguments (in assistant messages)
|
|
20
|
+
* - tool message content (often JSON)
|
|
21
|
+
*/
|
|
22
|
+
export declare function compactMessagesJson(messages: NormalizedMessage[]): JsonCompactResult;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layer 5: JSON Compaction
|
|
3
|
+
*
|
|
4
|
+
* Minifies JSON in tool_call arguments and tool results.
|
|
5
|
+
* Removes pretty-print whitespace from JSON strings.
|
|
6
|
+
*
|
|
7
|
+
* Safe for LLM: JSON semantics unchanged.
|
|
8
|
+
* Expected savings: 2-4%
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Compact a JSON string by parsing and re-stringifying without formatting.
|
|
12
|
+
*/
|
|
13
|
+
function compactJson(jsonString) {
|
|
14
|
+
try {
|
|
15
|
+
const parsed = JSON.parse(jsonString);
|
|
16
|
+
return JSON.stringify(parsed);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// Not valid JSON, return as-is
|
|
20
|
+
return jsonString;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Check if a string looks like JSON (starts with { or [).
|
|
25
|
+
*/
|
|
26
|
+
function looksLikeJson(str) {
|
|
27
|
+
const trimmed = str.trim();
|
|
28
|
+
return ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
|
|
29
|
+
(trimmed.startsWith("[") && trimmed.endsWith("]")));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Compact tool_call arguments in a message.
|
|
33
|
+
*/
|
|
34
|
+
function compactToolCalls(toolCalls) {
|
|
35
|
+
return toolCalls.map((tc) => ({
|
|
36
|
+
...tc,
|
|
37
|
+
function: {
|
|
38
|
+
...tc.function,
|
|
39
|
+
arguments: compactJson(tc.function.arguments),
|
|
40
|
+
},
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Apply JSON compaction to all messages.
|
|
45
|
+
*
|
|
46
|
+
* Targets:
|
|
47
|
+
* - tool_call arguments (in assistant messages)
|
|
48
|
+
* - tool message content (often JSON)
|
|
49
|
+
*/
|
|
50
|
+
export function compactMessagesJson(messages) {
|
|
51
|
+
let charsSaved = 0;
|
|
52
|
+
const result = messages.map((message) => {
|
|
53
|
+
const newMessage = { ...message };
|
|
54
|
+
// Compact tool_calls arguments
|
|
55
|
+
if (message.tool_calls && message.tool_calls.length > 0) {
|
|
56
|
+
const originalLength = JSON.stringify(message.tool_calls).length;
|
|
57
|
+
newMessage.tool_calls = compactToolCalls(message.tool_calls);
|
|
58
|
+
const newLength = JSON.stringify(newMessage.tool_calls).length;
|
|
59
|
+
charsSaved += originalLength - newLength;
|
|
60
|
+
}
|
|
61
|
+
// Compact tool message content if it looks like JSON
|
|
62
|
+
if (message.role === "tool" && message.content && typeof message.content === "string" && looksLikeJson(message.content)) {
|
|
63
|
+
const originalLength = message.content.length;
|
|
64
|
+
const compacted = compactJson(message.content);
|
|
65
|
+
charsSaved += originalLength - compacted.length;
|
|
66
|
+
newMessage.content = compacted;
|
|
67
|
+
}
|
|
68
|
+
return newMessage;
|
|
69
|
+
});
|
|
70
|
+
return {
|
|
71
|
+
messages: result,
|
|
72
|
+
charsSaved,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L6: Observation Compression (AGGRESSIVE)
|
|
3
|
+
*
|
|
4
|
+
* Inspired by claw-compactor's 97% compression on tool results.
|
|
5
|
+
* Tool call results (especially large ones) are summarized to key info only.
|
|
6
|
+
*
|
|
7
|
+
* This is the biggest compression win - tool outputs can be 10KB+ but
|
|
8
|
+
* only ~200 chars of actual useful information.
|
|
9
|
+
*/
|
|
10
|
+
import { NormalizedMessage } from "../types.js";
|
|
11
|
+
interface ObservationResult {
|
|
12
|
+
messages: NormalizedMessage[];
|
|
13
|
+
charsSaved: number;
|
|
14
|
+
observationsCompressed: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Compress tool results in messages.
|
|
18
|
+
*/
|
|
19
|
+
export declare function compressObservations(messages: NormalizedMessage[]): ObservationResult;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L6: Observation Compression (AGGRESSIVE)
|
|
3
|
+
*
|
|
4
|
+
* Inspired by claw-compactor's 97% compression on tool results.
|
|
5
|
+
* Tool call results (especially large ones) are summarized to key info only.
|
|
6
|
+
*
|
|
7
|
+
* This is the biggest compression win - tool outputs can be 10KB+ but
|
|
8
|
+
* only ~200 chars of actual useful information.
|
|
9
|
+
*/
|
|
10
|
+
// Max length for tool results before compression kicks in
|
|
11
|
+
const TOOL_RESULT_THRESHOLD = 500;
|
|
12
|
+
// Max length to compress tool results down to
|
|
13
|
+
const COMPRESSED_RESULT_MAX = 300;
|
|
14
|
+
/**
|
|
15
|
+
* Extract key information from tool result.
|
|
16
|
+
* Keeps: errors, key values, status, first/last important lines.
|
|
17
|
+
*/
|
|
18
|
+
function compressToolResult(content) {
|
|
19
|
+
if (!content || content.length <= TOOL_RESULT_THRESHOLD) {
|
|
20
|
+
return content;
|
|
21
|
+
}
|
|
22
|
+
const lines = content.split("\n").map((l) => l.trim()).filter(Boolean);
|
|
23
|
+
// Priority 1: Error messages (always keep)
|
|
24
|
+
const errorLines = lines.filter((l) => /error|exception|failed|denied|refused|timeout|invalid/i.test(l) &&
|
|
25
|
+
l.length < 200);
|
|
26
|
+
// Priority 2: Status/result lines
|
|
27
|
+
const statusLines = lines.filter((l) => /success|complete|created|updated|found|result|status|total|count/i.test(l) &&
|
|
28
|
+
l.length < 150);
|
|
29
|
+
// Priority 3: Key JSON fields (extract important values)
|
|
30
|
+
const jsonMatches = [];
|
|
31
|
+
const jsonPattern = /"(id|name|status|error|message|count|total|url|path)":\s*"?([^",}\n]+)"?/gi;
|
|
32
|
+
let match;
|
|
33
|
+
while ((match = jsonPattern.exec(content)) !== null) {
|
|
34
|
+
jsonMatches.push(`${match[1]}: ${match[2].slice(0, 50)}`);
|
|
35
|
+
}
|
|
36
|
+
// Priority 4: First and last meaningful lines
|
|
37
|
+
const firstLine = lines[0]?.slice(0, 100);
|
|
38
|
+
const lastLine = lines.length > 1 ? lines[lines.length - 1]?.slice(0, 100) : "";
|
|
39
|
+
// Build compressed observation
|
|
40
|
+
const parts = [];
|
|
41
|
+
if (errorLines.length > 0) {
|
|
42
|
+
parts.push("[ERR] " + errorLines.slice(0, 3).join(" | "));
|
|
43
|
+
}
|
|
44
|
+
if (statusLines.length > 0) {
|
|
45
|
+
parts.push(statusLines.slice(0, 3).join(" | "));
|
|
46
|
+
}
|
|
47
|
+
if (jsonMatches.length > 0) {
|
|
48
|
+
parts.push(jsonMatches.slice(0, 5).join(", "));
|
|
49
|
+
}
|
|
50
|
+
if (parts.length === 0) {
|
|
51
|
+
// Fallback: keep first/last lines with truncation marker
|
|
52
|
+
parts.push(firstLine || "");
|
|
53
|
+
if (lines.length > 2) {
|
|
54
|
+
parts.push(`[...${lines.length - 2} lines...]`);
|
|
55
|
+
}
|
|
56
|
+
if (lastLine && lastLine !== firstLine) {
|
|
57
|
+
parts.push(lastLine);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
let result = parts.join("\n");
|
|
61
|
+
// Final length cap
|
|
62
|
+
if (result.length > COMPRESSED_RESULT_MAX) {
|
|
63
|
+
result = result.slice(0, COMPRESSED_RESULT_MAX - 20) + "\n[...truncated]";
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Compress large repeated content blocks.
|
|
69
|
+
* Detects when same large block appears multiple times.
|
|
70
|
+
*/
|
|
71
|
+
function deduplicateLargeBlocks(messages) {
|
|
72
|
+
const blockHashes = new Map(); // hash -> first occurrence index
|
|
73
|
+
let charsSaved = 0;
|
|
74
|
+
const result = messages.map((msg, idx) => {
|
|
75
|
+
if (!msg.content || typeof msg.content !== "string" || msg.content.length < 500) {
|
|
76
|
+
return msg;
|
|
77
|
+
}
|
|
78
|
+
// Hash first 200 chars as block identifier
|
|
79
|
+
const blockKey = msg.content.slice(0, 200);
|
|
80
|
+
if (blockHashes.has(blockKey)) {
|
|
81
|
+
const firstIdx = blockHashes.get(blockKey);
|
|
82
|
+
const original = msg.content;
|
|
83
|
+
const compressed = `[See message #${firstIdx + 1} - same content]`;
|
|
84
|
+
charsSaved += original.length - compressed.length;
|
|
85
|
+
return { ...msg, content: compressed };
|
|
86
|
+
}
|
|
87
|
+
blockHashes.set(blockKey, idx);
|
|
88
|
+
return msg;
|
|
89
|
+
});
|
|
90
|
+
return { messages: result, charsSaved };
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Compress tool results in messages.
|
|
94
|
+
*/
|
|
95
|
+
export function compressObservations(messages) {
|
|
96
|
+
let charsSaved = 0;
|
|
97
|
+
let observationsCompressed = 0;
|
|
98
|
+
// First pass: compress individual tool results
|
|
99
|
+
let result = messages.map((msg) => {
|
|
100
|
+
// Only compress tool role messages (these are tool call results)
|
|
101
|
+
if (msg.role !== "tool" || !msg.content || typeof msg.content !== "string") {
|
|
102
|
+
return msg;
|
|
103
|
+
}
|
|
104
|
+
const original = msg.content;
|
|
105
|
+
if (original.length <= TOOL_RESULT_THRESHOLD) {
|
|
106
|
+
return msg;
|
|
107
|
+
}
|
|
108
|
+
const compressed = compressToolResult(original);
|
|
109
|
+
const saved = original.length - compressed.length;
|
|
110
|
+
if (saved > 50) {
|
|
111
|
+
charsSaved += saved;
|
|
112
|
+
observationsCompressed++;
|
|
113
|
+
return { ...msg, content: compressed };
|
|
114
|
+
}
|
|
115
|
+
return msg;
|
|
116
|
+
});
|
|
117
|
+
// Second pass: deduplicate large repeated blocks
|
|
118
|
+
const dedupResult = deduplicateLargeBlocks(result);
|
|
119
|
+
result = dedupResult.messages;
|
|
120
|
+
charsSaved += dedupResult.charsSaved;
|
|
121
|
+
return {
|
|
122
|
+
messages: result,
|
|
123
|
+
charsSaved,
|
|
124
|
+
observationsCompressed,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layer 4: Path Shortening
|
|
3
|
+
*
|
|
4
|
+
* Detects common filesystem path prefixes and replaces them with short codes.
|
|
5
|
+
* Common in coding assistant contexts with repeated file paths.
|
|
6
|
+
*
|
|
7
|
+
* Safe for LLM: Lossless abbreviation with path map header.
|
|
8
|
+
* Expected savings: 1-3%
|
|
9
|
+
*/
|
|
10
|
+
import { NormalizedMessage } from "../types.js";
|
|
11
|
+
export interface PathShorteningResult {
|
|
12
|
+
messages: NormalizedMessage[];
|
|
13
|
+
pathMap: Record<string, string>;
|
|
14
|
+
charsSaved: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Apply path shortening to all messages.
|
|
18
|
+
*/
|
|
19
|
+
export declare function shortenPaths(messages: NormalizedMessage[]): PathShorteningResult;
|
|
20
|
+
/**
|
|
21
|
+
* Generate the path map header for the codebook.
|
|
22
|
+
*/
|
|
23
|
+
export declare function generatePathMapHeader(pathMap: Record<string, string>): string;
|