prompt-compressor 1.0.0 → 1.0.2
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/README.md +11 -1
- package/package.json +30 -7
- package/src/algorithms.js +42 -25
- package/src/protect.js +4 -0
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/prompt-compressor)
|
|
6
6
|
|
|
7
|
+
⭐ **Find it useful? [Star it on GitHub](https://github.com/kettertom/prompt-compressor)** · 🐛 **Found a bug or have an idea? [Open an issue](https://github.com/kettertom/prompt-compressor/issues/new)**
|
|
8
|
+
|
|
7
9
|
## Install
|
|
8
10
|
|
|
9
11
|
```bash
|
|
@@ -56,7 +58,15 @@ const { output, stats } = compress(text, {
|
|
|
56
58
|
});
|
|
57
59
|
```
|
|
58
60
|
|
|
59
|
-
##
|
|
61
|
+
## Use it as an MCP server (no code)
|
|
62
|
+
|
|
63
|
+
Want your AI assistant to compress prompts for you? Use [`prompt-compressor-mcp`](https://www.npmjs.com/package/prompt-compressor-mcp):
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
claude mcp add prompt-compressor -- npx -y prompt-compressor-mcp
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Inside your own MCP server
|
|
60
70
|
|
|
61
71
|
```js
|
|
62
72
|
import { compress } from 'prompt-compressor';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prompt-compressor",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Rule-based prompt compression — reduce LLM token usage without a model, without an API, without sending data anywhere.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -8,14 +8,29 @@
|
|
|
8
8
|
"types": "index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
11
|
+
"types": "./index.d.ts",
|
|
12
|
+
"import": "./index.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
"files": [
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"index.d.ts",
|
|
18
|
+
"src/"
|
|
19
|
+
],
|
|
16
20
|
"keywords": [
|
|
17
|
-
"prompt",
|
|
18
|
-
"
|
|
21
|
+
"prompt",
|
|
22
|
+
"compression",
|
|
23
|
+
"llm",
|
|
24
|
+
"tokens",
|
|
25
|
+
"bedrock",
|
|
26
|
+
"openai",
|
|
27
|
+
"anthropic",
|
|
28
|
+
"claude",
|
|
29
|
+
"gpt",
|
|
30
|
+
"langchain",
|
|
31
|
+
"mcp",
|
|
32
|
+
"rag",
|
|
33
|
+
"nlp"
|
|
19
34
|
],
|
|
20
35
|
"author": "Tom Ketter",
|
|
21
36
|
"license": "MIT",
|
|
@@ -24,5 +39,13 @@
|
|
|
24
39
|
"url": "https://github.com/kettertom/prompt-compressor.git"
|
|
25
40
|
},
|
|
26
41
|
"homepage": "https://prompt-compressor.com",
|
|
27
|
-
"engines": {
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=16"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/kettertom/prompt-compressor/issues"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"test": "node --test test/*.test.js"
|
|
50
|
+
}
|
|
28
51
|
}
|
package/src/algorithms.js
CHANGED
|
@@ -68,26 +68,39 @@ export function stripAnsiCodes(text) {
|
|
|
68
68
|
* @risk very-low | @savings 10–20%
|
|
69
69
|
*/
|
|
70
70
|
export function removeFiller(text) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
[
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
[
|
|
81
|
-
|
|
82
|
-
[
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
[
|
|
86
|
-
|
|
71
|
+
// Lead-in phrases: drop the phrase, KEEP the sentence that follows it.
|
|
72
|
+
// (Earlier versions deleted the whole sentence, which removed real instructions,
|
|
73
|
+
// e.g. "Please note that you must never share PII." disappeared entirely.)
|
|
74
|
+
const leadIns = [
|
|
75
|
+
/\bplease note that[,]?\s+/gi,
|
|
76
|
+
/\bit is important to note that[,]?\s+/gi,
|
|
77
|
+
/\bit's important to note that[,]?\s+/gi,
|
|
78
|
+
/\bit should be noted that[,]?\s+/gi,
|
|
79
|
+
/\bplease be advised that\s+/gi,
|
|
80
|
+
/\bit goes without saying(?: that)?[,]?\s+/gi,
|
|
81
|
+
/\bwhat this means is that\s+/gi,
|
|
82
|
+
/\bin other words[,:]?\s+/gi,
|
|
83
|
+
/\bas mentioned (?:above|earlier|before)[,:]?\s+/gi,
|
|
84
|
+
/\bfor the record[,:]?\s+/gi,
|
|
85
|
+
/(?<![\w-])note:\s+/gi,
|
|
86
|
+
/\bkindly\s+/gi,
|
|
87
|
+
/\bfeel free to\s+/gi,
|
|
88
|
+
];
|
|
89
|
+
const words = [
|
|
90
|
+
[/\bthe reason for this is(?: that)?\s+/gi, "because "],
|
|
87
91
|
];
|
|
88
92
|
let out = text;
|
|
89
|
-
for (const
|
|
90
|
-
|
|
93
|
+
for (const p of leadIns) {
|
|
94
|
+
out = out.replace(p, (m, offset, str) => {
|
|
95
|
+
// remember to capitalize the next letter if the phrase started a sentence
|
|
96
|
+
const before = str.slice(0, offset);
|
|
97
|
+
return /(^|[.!?:]\s*|\n\s*)$/.test(before) ? "\u0000CAP" : "";
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
for (const [p, r] of words) out = out.replace(p, r);
|
|
101
|
+
out = out.replace(/\u0000CAP(\s*)(\S)/g, (_, ws, ch) => ws + ch.toUpperCase()).replace(/\u0000CAP/g, "");
|
|
102
|
+
// collapse runs of spaces/tabs only — keep newlines and paragraph structure
|
|
103
|
+
return out.replace(/[ \t]{2,}/g, " ").replace(/[ \t]+\n/g, "\n").replace(/\n{3,}/g, "\n\n").trim();
|
|
91
104
|
}
|
|
92
105
|
|
|
93
106
|
/**
|
|
@@ -158,7 +171,7 @@ export function removeHedging(text) {
|
|
|
158
171
|
];
|
|
159
172
|
let out = text;
|
|
160
173
|
for (const [p, r] of patterns) out = out.replace(p, r);
|
|
161
|
-
return out.replace(
|
|
174
|
+
return out.replace(/[ \t]{2,}/g, " ").replace(/[ \t]+\n/g, "\n").trim();
|
|
162
175
|
}
|
|
163
176
|
|
|
164
177
|
/**
|
|
@@ -196,7 +209,7 @@ export function removeParentheticals(text) {
|
|
|
196
209
|
];
|
|
197
210
|
let out = text;
|
|
198
211
|
for (const p of patterns) out = out.replace(p, "");
|
|
199
|
-
return out.replace(
|
|
212
|
+
return out.replace(/[ \t]{2,}/g, " ").replace(/[ \t]+\n/g, "\n").trim();
|
|
200
213
|
}
|
|
201
214
|
|
|
202
215
|
/**
|
|
@@ -361,13 +374,17 @@ export function deduplicateLines(text) {
|
|
|
361
374
|
* @risk low | @savings 5–15%
|
|
362
375
|
*/
|
|
363
376
|
export function deduplicateSentences(text) {
|
|
364
|
-
|
|
377
|
+
// Split but keep each separator, so line breaks and paragraphs survive.
|
|
378
|
+
const parts = text.split(/((?<=[.!?])\s+)/);
|
|
365
379
|
const seen = new Set();
|
|
366
|
-
|
|
380
|
+
let out = "";
|
|
381
|
+
for (let i = 0; i < parts.length; i += 2) {
|
|
382
|
+
const s = parts[i], sep = parts[i + 1] ?? "";
|
|
367
383
|
const key = s.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
368
|
-
if (key.length < 8 || !seen.has(key)) { seen.add(key);
|
|
369
|
-
|
|
370
|
-
}
|
|
384
|
+
if (key.length < 8 || !seen.has(key)) { seen.add(key); out += s + sep; }
|
|
385
|
+
else if (sep.includes("\n") && !/\n\s*$/.test(out)) out = out.replace(/[ \t]*$/, "") + "\n";
|
|
386
|
+
}
|
|
387
|
+
return out.replace(/\n{3,}/g, "\n\n").trim();
|
|
371
388
|
}
|
|
372
389
|
|
|
373
390
|
/**
|
package/src/protect.js
CHANGED
|
@@ -24,11 +24,15 @@ const VAR_PATTERNS = [
|
|
|
24
24
|
export function protectVariables(text) {
|
|
25
25
|
const map = {};
|
|
26
26
|
let counter = 0;
|
|
27
|
+
const byMatch = new Map();
|
|
27
28
|
let result = text;
|
|
28
29
|
for (const pattern of VAR_PATTERNS) {
|
|
29
30
|
result = result.replace(new RegExp(pattern.source, "g"), match => {
|
|
31
|
+
// Same variable → same placeholder, so duplicate lines still dedupe.
|
|
32
|
+
if (byMatch.has(match)) return byMatch.get(match);
|
|
30
33
|
const key = `\x00VAR${counter++}\x00`;
|
|
31
34
|
map[key] = match;
|
|
35
|
+
byMatch.set(match, key);
|
|
32
36
|
return key;
|
|
33
37
|
});
|
|
34
38
|
}
|