@blockrun/clawrouter 0.12.101 → 0.12.103
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/cli.d.ts +0 -15
- package/dist/cli.js +80069 -273
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +1406 -34
- package/dist/index.js +81085 -1075
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/auth.d.ts +0 -82
- package/dist/auth.d.ts.map +0 -1
- package/dist/auth.js +0 -388
- package/dist/auth.js.map +0 -1
- package/dist/balance.d.ts +0 -97
- package/dist/balance.d.ts.map +0 -1
- package/dist/balance.js +0 -157
- package/dist/balance.js.map +0 -1
- package/dist/cli.d.ts.map +0 -1
- package/dist/compression/codebook.d.ts +0 -24
- package/dist/compression/codebook.d.ts.map +0 -1
- package/dist/compression/codebook.js +0 -119
- package/dist/compression/codebook.js.map +0 -1
- package/dist/compression/index.d.ts +0 -33
- package/dist/compression/index.d.ts.map +0 -1
- package/dist/compression/index.js +0 -246
- package/dist/compression/index.js.map +0 -1
- package/dist/compression/layers/deduplication.d.ts +0 -28
- package/dist/compression/layers/deduplication.d.ts.map +0 -1
- package/dist/compression/layers/deduplication.js +0 -101
- package/dist/compression/layers/deduplication.js.map +0 -1
- package/dist/compression/layers/dictionary.d.ts +0 -21
- package/dist/compression/layers/dictionary.d.ts.map +0 -1
- package/dist/compression/layers/dictionary.js +0 -73
- package/dist/compression/layers/dictionary.js.map +0 -1
- package/dist/compression/layers/dynamic-codebook.d.ts +0 -26
- package/dist/compression/layers/dynamic-codebook.d.ts.map +0 -1
- package/dist/compression/layers/dynamic-codebook.js +0 -149
- package/dist/compression/layers/dynamic-codebook.js.map +0 -1
- package/dist/compression/layers/json-compact.d.ts +0 -23
- package/dist/compression/layers/json-compact.d.ts.map +0 -1
- package/dist/compression/layers/json-compact.js +0 -79
- package/dist/compression/layers/json-compact.js.map +0 -1
- package/dist/compression/layers/observation.d.ts +0 -21
- package/dist/compression/layers/observation.d.ts.map +0 -1
- package/dist/compression/layers/observation.js +0 -130
- package/dist/compression/layers/observation.js.map +0 -1
- package/dist/compression/layers/paths.d.ts +0 -24
- package/dist/compression/layers/paths.d.ts.map +0 -1
- package/dist/compression/layers/paths.js +0 -110
- package/dist/compression/layers/paths.js.map +0 -1
- package/dist/compression/layers/whitespace.d.ts +0 -27
- package/dist/compression/layers/whitespace.d.ts.map +0 -1
- package/dist/compression/layers/whitespace.js +0 -60
- package/dist/compression/layers/whitespace.js.map +0 -1
- package/dist/compression/types.d.ts +0 -84
- package/dist/compression/types.d.ts.map +0 -1
- package/dist/compression/types.js +0 -27
- package/dist/compression/types.js.map +0 -1
- package/dist/config.d.ts +0 -12
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -22
- package/dist/config.js.map +0 -1
- package/dist/dedup.d.ts +0 -34
- package/dist/dedup.d.ts.map +0 -1
- package/dist/dedup.js +0 -150
- package/dist/dedup.js.map +0 -1
- package/dist/doctor.d.ts +0 -8
- package/dist/doctor.d.ts.map +0 -1
- package/dist/doctor.js +0 -375
- package/dist/doctor.js.map +0 -1
- package/dist/error-classification.test.d.ts +0 -2
- package/dist/error-classification.test.d.ts.map +0 -1
- package/dist/error-classification.test.js +0 -59
- package/dist/error-classification.test.js.map +0 -1
- package/dist/errors.d.ts +0 -54
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -85
- package/dist/errors.js.map +0 -1
- package/dist/exclude-models.d.ts +0 -28
- package/dist/exclude-models.d.ts.map +0 -1
- package/dist/exclude-models.integration.test.d.ts +0 -2
- package/dist/exclude-models.integration.test.d.ts.map +0 -1
- package/dist/exclude-models.integration.test.js +0 -46
- package/dist/exclude-models.integration.test.js.map +0 -1
- package/dist/exclude-models.js +0 -70
- package/dist/exclude-models.js.map +0 -1
- package/dist/exclude-models.test.d.ts +0 -5
- package/dist/exclude-models.test.d.ts.map +0 -1
- package/dist/exclude-models.test.js +0 -95
- package/dist/exclude-models.test.js.map +0 -1
- package/dist/fs-read.d.ts +0 -11
- package/dist/fs-read.d.ts.map +0 -1
- package/dist/fs-read.js +0 -47
- package/dist/fs-read.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/journal.d.ts +0 -67
- package/dist/journal.d.ts.map +0 -1
- package/dist/journal.js +0 -184
- package/dist/journal.js.map +0 -1
- package/dist/journal.test.d.ts +0 -2
- package/dist/journal.test.d.ts.map +0 -1
- package/dist/journal.test.js +0 -201
- package/dist/journal.test.js.map +0 -1
- package/dist/logger.d.ts +0 -33
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -35
- package/dist/logger.js.map +0 -1
- package/dist/models.d.ts +0 -107
- package/dist/models.d.ts.map +0 -1
- package/dist/models.js +0 -941
- package/dist/models.js.map +0 -1
- package/dist/models.test.d.ts +0 -2
- package/dist/models.test.d.ts.map +0 -1
- package/dist/models.test.js +0 -38
- package/dist/models.test.js.map +0 -1
- package/dist/partners/index.d.ts +0 -8
- package/dist/partners/index.d.ts.map +0 -1
- package/dist/partners/index.js +0 -6
- package/dist/partners/index.js.map +0 -1
- package/dist/partners/registry.d.ts +0 -51
- package/dist/partners/registry.d.ts.map +0 -1
- package/dist/partners/registry.js +0 -256
- package/dist/partners/registry.js.map +0 -1
- package/dist/partners/tools.d.ts +0 -24
- package/dist/partners/tools.d.ts.map +0 -1
- package/dist/partners/tools.js +0 -97
- package/dist/partners/tools.js.map +0 -1
- package/dist/payment-preauth.d.ts +0 -17
- package/dist/payment-preauth.d.ts.map +0 -1
- package/dist/payment-preauth.js +0 -102
- package/dist/payment-preauth.js.map +0 -1
- package/dist/provider.d.ts +0 -19
- package/dist/provider.d.ts.map +0 -1
- package/dist/provider.js +0 -45
- package/dist/provider.js.map +0 -1
- package/dist/proxy.d.ts +0 -210
- package/dist/proxy.d.ts.map +0 -1
- package/dist/proxy.debrand.test.d.ts +0 -2
- package/dist/proxy.debrand.test.d.ts.map +0 -1
- package/dist/proxy.debrand.test.js +0 -101
- package/dist/proxy.debrand.test.js.map +0 -1
- package/dist/proxy.degraded-response.test.d.ts +0 -2
- package/dist/proxy.degraded-response.test.d.ts.map +0 -1
- package/dist/proxy.degraded-response.test.js +0 -105
- package/dist/proxy.degraded-response.test.js.map +0 -1
- package/dist/proxy.image-download.test.d.ts +0 -2
- package/dist/proxy.image-download.test.d.ts.map +0 -1
- package/dist/proxy.image-download.test.js +0 -92
- package/dist/proxy.image-download.test.js.map +0 -1
- package/dist/proxy.js +0 -3810
- package/dist/proxy.js.map +0 -1
- package/dist/proxy.models-endpoint.test.d.ts +0 -2
- package/dist/proxy.models-endpoint.test.d.ts.map +0 -1
- package/dist/proxy.models-endpoint.test.js +0 -19
- package/dist/proxy.models-endpoint.test.js.map +0 -1
- package/dist/proxy.multiturn-thinking.test.d.ts +0 -2
- package/dist/proxy.multiturn-thinking.test.d.ts.map +0 -1
- package/dist/proxy.multiturn-thinking.test.js +0 -82
- package/dist/proxy.multiturn-thinking.test.js.map +0 -1
- package/dist/proxy.payment-chain-reuse.test.d.ts +0 -2
- package/dist/proxy.payment-chain-reuse.test.d.ts.map +0 -1
- package/dist/proxy.payment-chain-reuse.test.js +0 -30
- package/dist/proxy.payment-chain-reuse.test.js.map +0 -1
- package/dist/proxy.solana-resilience.test.d.ts +0 -12
- package/dist/proxy.solana-resilience.test.d.ts.map +0 -1
- package/dist/proxy.solana-resilience.test.js +0 -39
- package/dist/proxy.solana-resilience.test.js.map +0 -1
- package/dist/report.d.ts +0 -6
- package/dist/report.d.ts.map +0 -1
- package/dist/report.js +0 -44
- package/dist/report.js.map +0 -1
- package/dist/response-cache.advanced.test.d.ts +0 -2
- package/dist/response-cache.advanced.test.d.ts.map +0 -1
- package/dist/response-cache.advanced.test.js +0 -566
- package/dist/response-cache.advanced.test.js.map +0 -1
- package/dist/response-cache.d.ts +0 -85
- package/dist/response-cache.d.ts.map +0 -1
- package/dist/response-cache.edge-cases.test.d.ts +0 -2
- package/dist/response-cache.edge-cases.test.d.ts.map +0 -1
- package/dist/response-cache.edge-cases.test.js +0 -488
- package/dist/response-cache.edge-cases.test.js.map +0 -1
- package/dist/response-cache.extreme.test.d.ts +0 -2
- package/dist/response-cache.extreme.test.d.ts.map +0 -1
- package/dist/response-cache.extreme.test.js +0 -561
- package/dist/response-cache.extreme.test.js.map +0 -1
- package/dist/response-cache.js +0 -240
- package/dist/response-cache.js.map +0 -1
- package/dist/response-cache.test.d.ts +0 -2
- package/dist/response-cache.test.d.ts.map +0 -1
- package/dist/response-cache.test.js +0 -242
- package/dist/response-cache.test.js.map +0 -1
- package/dist/retry.d.ts +0 -42
- package/dist/retry.d.ts.map +0 -1
- package/dist/retry.js +0 -99
- package/dist/retry.js.map +0 -1
- package/dist/router/config.d.ts +0 -11
- package/dist/router/config.d.ts.map +0 -1
- package/dist/router/config.js +0 -1215
- package/dist/router/config.js.map +0 -1
- package/dist/router/index.d.ts +0 -18
- package/dist/router/index.d.ts.map +0 -1
- package/dist/router/index.js +0 -19
- package/dist/router/index.js.map +0 -1
- package/dist/router/llm-classifier.d.ts +0 -29
- package/dist/router/llm-classifier.d.ts.map +0 -1
- package/dist/router/llm-classifier.js +0 -99
- package/dist/router/llm-classifier.js.map +0 -1
- package/dist/router/rules.d.ts +0 -12
- package/dist/router/rules.d.ts.map +0 -1
- package/dist/router/rules.js +0 -202
- package/dist/router/rules.js.map +0 -1
- package/dist/router/selector.d.ts +0 -58
- package/dist/router/selector.d.ts.map +0 -1
- package/dist/router/selector.js +0 -165
- package/dist/router/selector.js.map +0 -1
- package/dist/router/selector.test.d.ts +0 -2
- package/dist/router/selector.test.d.ts.map +0 -1
- package/dist/router/selector.test.js +0 -67
- package/dist/router/selector.test.js.map +0 -1
- package/dist/router/strategy.d.ts +0 -19
- package/dist/router/strategy.d.ts.map +0 -1
- package/dist/router/strategy.js +0 -138
- package/dist/router/strategy.js.map +0 -1
- package/dist/router/strategy.test.d.ts +0 -2
- package/dist/router/strategy.test.d.ts.map +0 -1
- package/dist/router/strategy.test.js +0 -131
- package/dist/router/strategy.test.js.map +0 -1
- package/dist/router/types.d.ts +0 -127
- package/dist/router/types.d.ts.map +0 -1
- package/dist/router/types.js +0 -11
- package/dist/router/types.js.map +0 -1
- package/dist/session.d.ts +0 -124
- package/dist/session.d.ts.map +0 -1
- package/dist/session.js +0 -256
- package/dist/session.js.map +0 -1
- package/dist/session.test.d.ts +0 -2
- package/dist/session.test.d.ts.map +0 -1
- package/dist/session.test.js +0 -163
- package/dist/session.test.js.map +0 -1
- package/dist/solana-balance.d.ts +0 -48
- package/dist/solana-balance.d.ts.map +0 -1
- package/dist/solana-balance.js +0 -142
- package/dist/solana-balance.js.map +0 -1
- package/dist/spend-control.d.ts +0 -112
- package/dist/spend-control.d.ts.map +0 -1
- package/dist/spend-control.js +0 -278
- package/dist/spend-control.js.map +0 -1
- package/dist/spend-control.test.d.ts +0 -5
- package/dist/spend-control.test.d.ts.map +0 -1
- package/dist/spend-control.test.js +0 -210
- package/dist/spend-control.test.js.map +0 -1
- package/dist/stats.d.ts +0 -64
- package/dist/stats.d.ts.map +0 -1
- package/dist/stats.js +0 -295
- package/dist/stats.js.map +0 -1
- package/dist/types.d.ts +0 -150
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -9
- package/dist/types.js.map +0 -1
- package/dist/update-hint.test.d.ts +0 -9
- package/dist/update-hint.test.d.ts.map +0 -1
- package/dist/update-hint.test.js +0 -156
- package/dist/update-hint.test.js.map +0 -1
- package/dist/updater.d.ts +0 -10
- package/dist/updater.d.ts.map +0 -1
- package/dist/updater.js +0 -55
- package/dist/updater.js.map +0 -1
- package/dist/version.d.ts +0 -3
- package/dist/version.d.ts.map +0 -1
- package/dist/version.js +0 -16
- package/dist/version.js.map +0 -1
- package/dist/wallet.d.ts +0 -52
- package/dist/wallet.d.ts.map +0 -1
- package/dist/wallet.js +0 -94
- package/dist/wallet.js.map +0 -1
- package/dist/wallet.test.d.ts +0 -6
- package/dist/wallet.test.d.ts.map +0 -1
- package/dist/wallet.test.js +0 -117
- package/dist/wallet.test.js.map +0 -1
- package/dist/x402-sdk.test.d.ts +0 -6
- package/dist/x402-sdk.test.d.ts.map +0 -1
- package/dist/x402-sdk.test.js +0 -161
- package/dist/x402-sdk.test.js.map +0 -1
|
@@ -1,101 +0,0 @@
|
|
|
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 crypto from "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
|
-
// Handle content - stringify arrays (multimodal), use string directly, or empty string
|
|
17
|
-
let contentStr = "";
|
|
18
|
-
if (typeof message.content === "string") {
|
|
19
|
-
contentStr = message.content;
|
|
20
|
-
}
|
|
21
|
-
else if (Array.isArray(message.content)) {
|
|
22
|
-
contentStr = JSON.stringify(message.content);
|
|
23
|
-
}
|
|
24
|
-
const parts = [message.role, contentStr, message.tool_call_id || "", message.name || ""];
|
|
25
|
-
// Include tool_calls if present
|
|
26
|
-
if (message.tool_calls) {
|
|
27
|
-
parts.push(JSON.stringify(message.tool_calls.map((tc) => ({
|
|
28
|
-
name: tc.function.name,
|
|
29
|
-
args: tc.function.arguments,
|
|
30
|
-
}))));
|
|
31
|
-
}
|
|
32
|
-
const content = parts.join("|");
|
|
33
|
-
return crypto.createHash("md5").update(content).digest("hex");
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Remove exact duplicate messages from the conversation.
|
|
37
|
-
*
|
|
38
|
-
* Strategy:
|
|
39
|
-
* - Keep first occurrence of each unique message
|
|
40
|
-
* - Preserve order for semantic coherence
|
|
41
|
-
* - Never dedupe system messages (they set context)
|
|
42
|
-
* - Allow duplicate user messages (user might repeat intentionally)
|
|
43
|
-
* - CRITICAL: Never dedupe assistant messages with tool_calls that are
|
|
44
|
-
* referenced by subsequent tool messages (breaks Anthropic tool_use/tool_result pairing)
|
|
45
|
-
*/
|
|
46
|
-
export function deduplicateMessages(messages) {
|
|
47
|
-
const seen = new Set();
|
|
48
|
-
const result = [];
|
|
49
|
-
let duplicatesRemoved = 0;
|
|
50
|
-
// First pass: collect all tool_call_ids that are referenced by tool messages
|
|
51
|
-
// These tool_calls MUST be preserved to maintain tool_use/tool_result pairing
|
|
52
|
-
const referencedToolCallIds = new Set();
|
|
53
|
-
for (const message of messages) {
|
|
54
|
-
if (message.role === "tool" && message.tool_call_id) {
|
|
55
|
-
referencedToolCallIds.add(message.tool_call_id);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
for (const message of messages) {
|
|
59
|
-
// Always keep system messages (they set important context)
|
|
60
|
-
if (message.role === "system") {
|
|
61
|
-
result.push(message);
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
// Always keep user messages (user might repeat intentionally)
|
|
65
|
-
if (message.role === "user") {
|
|
66
|
-
result.push(message);
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
// Always keep tool messages (they are results of tool calls)
|
|
70
|
-
// Removing them would break the tool_use/tool_result pairing
|
|
71
|
-
if (message.role === "tool") {
|
|
72
|
-
result.push(message);
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
// For assistant messages with tool_calls, check if any are referenced
|
|
76
|
-
// by subsequent tool messages - if so, we MUST keep this message
|
|
77
|
-
if (message.role === "assistant" && message.tool_calls) {
|
|
78
|
-
const hasReferencedToolCall = message.tool_calls.some((tc) => referencedToolCallIds.has(tc.id));
|
|
79
|
-
if (hasReferencedToolCall) {
|
|
80
|
-
// This assistant message has tool_calls that are referenced - keep it
|
|
81
|
-
result.push(message);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
// For other assistant messages, check for duplicates
|
|
86
|
-
const hash = hashMessage(message);
|
|
87
|
-
if (!seen.has(hash)) {
|
|
88
|
-
seen.add(hash);
|
|
89
|
-
result.push(message);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
duplicatesRemoved++;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return {
|
|
96
|
-
messages: result,
|
|
97
|
-
duplicatesRemoved,
|
|
98
|
-
originalCount: messages.length,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
//# sourceMappingURL=deduplication.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deduplication.js","sourceRoot":"","sources":["../../../src/compression/layers/deduplication.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAQ5B;;;GAGG;AACH,SAAS,WAAW,CAAC,OAA0B;IAC7C,uFAAuF;IACvF,IAAI,UAAU,GAAG,EAAE,CAAC;IACpB,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QACxC,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC;IAC/B,CAAC;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC1C,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED,MAAM,KAAK,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,YAAY,IAAI,EAAE,EAAE,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC;IAEzF,gCAAgC;IAChC,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;QACvB,KAAK,CAAC,IAAI,CACR,IAAI,CAAC,SAAS,CACZ,OAAO,CAAC,UAAU,CAAC,GAAG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7D,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC5B,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACrB,SAAS;QACX,CAAC;QAED,sEAAsE;QACtE,iEAAiE;QACjE,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACvD,MAAM,qBAAqB,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,CAC3D,qBAAqB,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,CACjC,CAAC;YACF,IAAI,qBAAqB,EAAE,CAAC;gBAC1B,sEAAsE;gBACtE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBACrB,SAAS;YACX,CAAC;QACH,CAAC;QAED,qDAAqD;QACrD,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;QAElC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YACpB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACf,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,CAAC;aAAM,CAAC;YACN,iBAAiB,EAAE,CAAC;QACtB,CAAC;IACH,CAAC;IAED,OAAO;QACL,QAAQ,EAAE,MAAM;QAChB,iBAAiB;QACjB,aAAa,EAAE,QAAQ,CAAC,MAAM;KAC/B,CAAC;AACJ,CAAC"}
|
|
@@ -1,21 +0,0 @@
|
|
|
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;
|
|
21
|
-
//# sourceMappingURL=dictionary.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dictionary.d.ts","sourceRoot":"","sources":["../../../src/compression/layers/dictionary.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,iBAAiB,EAAE,CAAC;IAC9B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,SAAS,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;CACpB;AA6CD;;GAEG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,iBAAiB,EAAE,GAAG,gBAAgB,CA+B9E"}
|
|
@@ -1,73 +0,0 @@
|
|
|
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
|
-
// Defensive type check - content might be array/object for multimodal messages
|
|
17
|
-
if (!content || typeof content !== "string") {
|
|
18
|
-
return { encoded: content, substitutions: 0, codes: new Set(), charsSaved: 0 };
|
|
19
|
-
}
|
|
20
|
-
let encoded = content;
|
|
21
|
-
let substitutions = 0;
|
|
22
|
-
let charsSaved = 0;
|
|
23
|
-
const codes = new Set();
|
|
24
|
-
// Sort phrases by length (longest first) to avoid partial matches
|
|
25
|
-
const phrases = Object.keys(inverseCodebook).sort((a, b) => b.length - a.length);
|
|
26
|
-
for (const phrase of phrases) {
|
|
27
|
-
const code = inverseCodebook[phrase];
|
|
28
|
-
const regex = new RegExp(escapeRegex(phrase), "g");
|
|
29
|
-
const matches = encoded.match(regex);
|
|
30
|
-
if (matches && matches.length > 0) {
|
|
31
|
-
encoded = encoded.replace(regex, code);
|
|
32
|
-
substitutions += matches.length;
|
|
33
|
-
charsSaved += matches.length * (phrase.length - code.length);
|
|
34
|
-
codes.add(code);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return { encoded, substitutions, codes, charsSaved };
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Escape special regex characters in a string.
|
|
41
|
-
*/
|
|
42
|
-
function escapeRegex(str) {
|
|
43
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Apply dictionary encoding to all messages.
|
|
47
|
-
*/
|
|
48
|
-
export function encodeMessages(messages) {
|
|
49
|
-
const inverseCodebook = getInverseCodebook();
|
|
50
|
-
let totalSubstitutions = 0;
|
|
51
|
-
let totalCharsSaved = 0;
|
|
52
|
-
const allUsedCodes = new Set();
|
|
53
|
-
const result = messages.map((message) => {
|
|
54
|
-
// Only process string content (skip arrays for multimodal messages)
|
|
55
|
-
if (!message.content || typeof message.content !== "string")
|
|
56
|
-
return message;
|
|
57
|
-
const { encoded, substitutions, codes, charsSaved } = encodeContent(message.content, inverseCodebook);
|
|
58
|
-
totalSubstitutions += substitutions;
|
|
59
|
-
totalCharsSaved += charsSaved;
|
|
60
|
-
codes.forEach((code) => allUsedCodes.add(code));
|
|
61
|
-
return {
|
|
62
|
-
...message,
|
|
63
|
-
content: encoded,
|
|
64
|
-
};
|
|
65
|
-
});
|
|
66
|
-
return {
|
|
67
|
-
messages: result,
|
|
68
|
-
substitutionCount: totalSubstitutions,
|
|
69
|
-
usedCodes: allUsedCodes,
|
|
70
|
-
charsSaved: totalCharsSaved,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
//# sourceMappingURL=dictionary.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dictionary.js","sourceRoot":"","sources":["../../../src/compression/layers/dictionary.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AASpD;;;GAGG;AACH,SAAS,aAAa,CACpB,OAAe,EACf,eAAuC;IAEvC,+EAA+E;IAC/E,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAC5C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;IACjF,CAAC;IACD,IAAI,OAAO,GAAG,OAAO,CAAC;IACtB,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAEhC,kEAAkE;IAClE,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;IAEjF,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,IAAI,GAAG,eAAe,CAAC,MAA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O,CAAC,OAAO,EACf,eAAe,CAChB,CAAC;QAEF,kBAAkB,IAAI,aAAa,CAAC;QACpC,eAAe,IAAI,UAAU,CAAC;QAC9B,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;QAEhD,OAAO;YACL,GAAG,OAAO;YACV,OAAO,EAAE,OAAO;SACjB,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,QAAQ,EAAE,MAAM;QAChB,iBAAiB,EAAE,kBAAkB;QACrC,SAAS,EAAE,YAAY;QACvB,UAAU,EAAE,eAAe;KAC5B,CAAC;AACJ,CAAC"}
|
|
@@ -1,26 +0,0 @@
|
|
|
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 {};
|
|
26
|
-
//# sourceMappingURL=dynamic-codebook.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-codebook.d.ts","sourceRoot":"","sources":["../../../src/compression/layers/dynamic-codebook.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,UAAU,qBAAqB;IAC7B,QAAQ,EAAE,iBAAiB,EAAE,CAAC;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,aAAa,EAAE,MAAM,CAAC;CACvB;AAyFD;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,iBAAiB,EAAE,GAAG,qBAAqB,CAmDzF;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAatF"}
|
|
@@ -1,149 +0,0 @@
|
|
|
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 && trimmed.length <= MAX_PHRASE_LENGTH) {
|
|
26
|
-
phrases.set(trimmed, (phrases.get(trimmed) || 0) + 1);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
// Also find repeated lines
|
|
30
|
-
const lines = allContent.split("\n");
|
|
31
|
-
for (const line of lines) {
|
|
32
|
-
const trimmed = line.trim();
|
|
33
|
-
if (trimmed.length >= MIN_PHRASE_LENGTH && trimmed.length <= MAX_PHRASE_LENGTH) {
|
|
34
|
-
phrases.set(trimmed, (phrases.get(trimmed) || 0) + 1);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return phrases;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Build dynamic codebook from message content.
|
|
41
|
-
*/
|
|
42
|
-
function buildDynamicCodebook(messages) {
|
|
43
|
-
// Combine all content
|
|
44
|
-
let allContent = "";
|
|
45
|
-
for (const msg of messages) {
|
|
46
|
-
// Only process string content (skip arrays for multimodal messages)
|
|
47
|
-
if (msg.content && typeof msg.content === "string") {
|
|
48
|
-
allContent += msg.content + "\n";
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
// Find repeated phrases
|
|
52
|
-
const phrases = findRepeatedPhrases(allContent);
|
|
53
|
-
// Filter by frequency and sort by savings potential
|
|
54
|
-
const candidates = [];
|
|
55
|
-
for (const [phrase, count] of phrases.entries()) {
|
|
56
|
-
if (count >= MIN_FREQUENCY) {
|
|
57
|
-
// Savings = (phrase length - code length) * occurrences
|
|
58
|
-
const codeLength = 4; // e.g., "$D01"
|
|
59
|
-
const savings = (phrase.length - codeLength) * count;
|
|
60
|
-
if (savings > 50) {
|
|
61
|
-
candidates.push({ phrase, count, savings });
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
// Sort by savings (descending) and take top entries
|
|
66
|
-
candidates.sort((a, b) => b.savings - a.savings);
|
|
67
|
-
const topCandidates = candidates.slice(0, MAX_ENTRIES);
|
|
68
|
-
// Build codebook
|
|
69
|
-
const codebook = {};
|
|
70
|
-
topCandidates.forEach((c, i) => {
|
|
71
|
-
const code = `${CODE_PREFIX}${String(i + 1).padStart(2, "0")}`;
|
|
72
|
-
codebook[code] = c.phrase;
|
|
73
|
-
});
|
|
74
|
-
return codebook;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Escape special regex characters.
|
|
78
|
-
*/
|
|
79
|
-
function escapeRegex(str) {
|
|
80
|
-
// Defensive type check
|
|
81
|
-
if (!str || typeof str !== "string")
|
|
82
|
-
return "";
|
|
83
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Apply dynamic codebook to messages.
|
|
87
|
-
*/
|
|
88
|
-
export function applyDynamicCodebook(messages) {
|
|
89
|
-
// Build codebook from content
|
|
90
|
-
const codebook = buildDynamicCodebook(messages);
|
|
91
|
-
if (Object.keys(codebook).length === 0) {
|
|
92
|
-
return {
|
|
93
|
-
messages,
|
|
94
|
-
charsSaved: 0,
|
|
95
|
-
dynamicCodes: {},
|
|
96
|
-
substitutions: 0,
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
// Create inverse map for replacement
|
|
100
|
-
const phraseToCode = {};
|
|
101
|
-
for (const [code, phrase] of Object.entries(codebook)) {
|
|
102
|
-
phraseToCode[phrase] = code;
|
|
103
|
-
}
|
|
104
|
-
// Sort phrases by length (longest first) to avoid partial replacements
|
|
105
|
-
const sortedPhrases = Object.keys(phraseToCode).sort((a, b) => b.length - a.length);
|
|
106
|
-
let charsSaved = 0;
|
|
107
|
-
let substitutions = 0;
|
|
108
|
-
// Apply replacements
|
|
109
|
-
const result = messages.map((msg) => {
|
|
110
|
-
// Only process string content (skip arrays for multimodal messages)
|
|
111
|
-
if (!msg.content || typeof msg.content !== "string")
|
|
112
|
-
return msg;
|
|
113
|
-
let content = msg.content;
|
|
114
|
-
for (const phrase of sortedPhrases) {
|
|
115
|
-
const code = phraseToCode[phrase];
|
|
116
|
-
const regex = new RegExp(escapeRegex(phrase), "g");
|
|
117
|
-
const matches = content.match(regex);
|
|
118
|
-
if (matches) {
|
|
119
|
-
content = content.replace(regex, code);
|
|
120
|
-
charsSaved += (phrase.length - code.length) * matches.length;
|
|
121
|
-
substitutions += matches.length;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return { ...msg, content };
|
|
125
|
-
});
|
|
126
|
-
return {
|
|
127
|
-
messages: result,
|
|
128
|
-
charsSaved,
|
|
129
|
-
dynamicCodes: codebook,
|
|
130
|
-
substitutions,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Generate header for dynamic codes (to include in system message).
|
|
135
|
-
*/
|
|
136
|
-
export function generateDynamicCodebookHeader(codebook) {
|
|
137
|
-
if (Object.keys(codebook).length === 0)
|
|
138
|
-
return "";
|
|
139
|
-
const entries = Object.entries(codebook)
|
|
140
|
-
.slice(0, 20) // Limit header size
|
|
141
|
-
.map(([code, phrase]) => {
|
|
142
|
-
// Truncate long phrases in header
|
|
143
|
-
const displayPhrase = phrase.length > 40 ? phrase.slice(0, 37) + "..." : phrase;
|
|
144
|
-
return `${code}=${displayPhrase}`;
|
|
145
|
-
})
|
|
146
|
-
.join(", ");
|
|
147
|
-
return `[DynDict: ${entries}]`;
|
|
148
|
-
}
|
|
149
|
-
//# sourceMappingURL=dynamic-codebook.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-codebook.js","sourceRoot":"","sources":["../../../src/compression/layers/dynamic-codebook.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAWH,SAAS;AACT,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAC7B,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,aAAa,GAAG,CAAC,CAAC;AACxB,MAAM,WAAW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AAM,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC;QAChF,OAAO,GAAG,IAAI,IAAI,aAAa,EAAE,CAAC;IACpC,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;IAEd,OAAO,aAAa,OAAO,GAAG,CAAC;AACjC,CAAC"}
|
|
@@ -1,23 +0,0 @@
|
|
|
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;
|
|
23
|
-
//# sourceMappingURL=json-compact.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"json-compact.d.ts","sourceRoot":"","sources":["../../../src/compression/layers/json-compact.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,iBAAiB,EAAY,MAAM,aAAa,CAAC;AAE1D,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,iBAAiB,EAAE,CAAC;IAC9B,UAAU,EAAE,MAAM,CAAC;CACpB;AAuCD;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,iBAAiB,EAAE,GAAG,iBAAiB,CAmCpF"}
|
|
@@ -1,79 +0,0 @@
|
|
|
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
|
-
// Only process string content (skip arrays for multimodal messages)
|
|
63
|
-
if (message.role === "tool" &&
|
|
64
|
-
message.content &&
|
|
65
|
-
typeof message.content === "string" &&
|
|
66
|
-
looksLikeJson(message.content)) {
|
|
67
|
-
const originalLength = message.content.length;
|
|
68
|
-
const compacted = compactJson(message.content);
|
|
69
|
-
charsSaved += originalLength - compacted.length;
|
|
70
|
-
newMessage.content = compacted;
|
|
71
|
-
}
|
|
72
|
-
return newMessage;
|
|
73
|
-
});
|
|
74
|
-
return {
|
|
75
|
-
messages: result,
|
|
76
|
-
charsSaved,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
//# sourceMappingURL=json-compact.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"json-compact.js","sourceRoot":"","sources":["../../../src/compression/layers/json-compact.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AASH;;GAEG;AACH,SAAS,WAAW,CAAC,UAAkB;IACrC,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACtC,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IAAC,MAAM,CAAC;QACP,+BAA+B;QAC/B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MAAM,CAAC;YAC/D,UAAU,IAAI,cAAc,GAAG,SAAS,CAAC;QAC3C,CAAC;QAED,qDAAqD;QACrD,oEAAoE;QACpE,IACE,OAAO,CAAC,IAAI,KAAK,MAAM;YACvB,OAAO,CAAC,OAAO;YACf,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ;YACnC,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,EAC9B,CAAC;YACD,MAAM,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;YAC9C,MAAM,SAAS,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YAC/C,UAAU,IAAI,cAAc,GAAG,SAAS,CAAC,MAAM,CAAC;YAChD,UAAU,CAAC,OAAO,GAAG,SAAS,CAAC;QACjC,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,QAAQ,EAAE,MAAM;QAChB,UAAU;KACX,CAAC;AACJ,CAAC"}
|
|
@@ -1,21 +0,0 @@
|
|
|
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 {};
|
|
21
|
-
//# sourceMappingURL=observation.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"observation.d.ts","sourceRoot":"","sources":["../../../src/compression/layers/observation.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,UAAU,iBAAiB;IACzB,QAAQ,EAAE,iBAAiB,EAAE,CAAC;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,sBAAsB,EAAE,MAAM,CAAC;CAChC;AAoHD;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,iBAAiB,EAAE,GAAG,iBAAiB,CAuCrF"}
|