prompt-compressor 1.0.3 → 1.0.4

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 CHANGED
@@ -6,9 +6,9 @@
6
6
 
7
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
8
 
9
- **No model. No API key. Deterministic. Zero dependencies. Your data never leaves your machine.** At the default level it cut **23% of tokens across 8 real-world prompt types while keeping 94/94 critical instructions**. See the [benchmark](https://github.com/kettertom/prompt-compressor#benchmark).
9
+ **No model. No API key. Deterministic. Zero dependencies. Your data never leaves your machine.** At the default level it cut **22% of tokens across 8 real-world prompt types while keeping 94/94 critical instructions**. See the [benchmark](https://github.com/kettertom/prompt-compressor#benchmark).
10
10
 
11
- > 🔧 **Upgrade to 1.0.3 or later.** 1.0.2 and earlier could corrupt ```json blocks at `low` (repeated lines such as `}` or `"qty": 1` were removed as duplicates), and `medium` kept only about 22% of critical facts. Both are fixed and covered by the benchmark and tests.
11
+ > 🔧 **Upgrade to 1.0.4 or later.** 1.0.3 and earlier could delete a rule or ID written after a colon ("Here is the account to use: ACC-7781.") and stripped indentation inside code blocks, which breaks Python/YAML. 1.0.2 and earlier also corrupted ```json blocks at `low` and kept only ~22% of critical facts at `medium`. All fixed and covered by tests and the benchmark.
12
12
 
13
13
  ## Install
14
14
 
@@ -33,10 +33,10 @@ Pick a level — every algorithm at or below that risk runs automatically.
33
33
 
34
34
  | Level | What runs | Measured saving ([benchmark](https://github.com/kettertom/prompt-compressor#benchmark)) |
35
35
  |---|---|---|
36
- | `'none'` | Whitespace, unicode normalization, indentation | 4% avg (0–24%) |
37
- | `'very-low'` | + Filler removal, phrase shortening, JSON/SQL minify | 9% avg (0–32%) |
38
- | `'low'` | + Hedging removal, code comments, deduplication, stack trace truncation | **23% avg (2–59%)**, 100% of critical facts kept |
39
- | `'medium'` | + Near-duplicate removal (fuzzy / n-gram / Levenshtein), TextRank on plain prose only | 24% avg (5–59%), 100% of critical facts kept |
36
+ | `'none'` | Whitespace, unicode normalization, indentation | 2% avg (0–16%) |
37
+ | `'very-low'` | + Filler removal, phrase shortening, JSON/SQL minify | 8% avg (0–32%) |
38
+ | `'low'` | + Hedging removal, code comments, deduplication, stack trace truncation | **22% avg (2–56%)**, 100% of critical facts kept |
39
+ | `'medium'` | + Near-duplicate removal (fuzzy / n-gram / Levenshtein), TextRank on plain prose only | 24% avg (5–56%), 100% of critical facts kept |
40
40
 
41
41
  ```js
42
42
  compress(text, 'none') // safest
@@ -45,7 +45,7 @@ compress(text, 'low') // recommended default
45
45
  compress(text, 'medium') // most aggressive
46
46
  ```
47
47
 
48
- > **`medium` = `low` + near-duplicate removal + light summarization of plain prose.** Code blocks, lists, tables, `Key: value` lines, questions, and any sentence with a rule, number, ID or variable are never removed. On the dense prompts in the benchmark it saves about the same as `low` (24% vs 23%) and kept 94/94 critical facts. It helps most on long, chatty prose. *(Before 1.0.3, `medium` chained three summarizers and kept only 22% of critical facts. Upgrade if you used it.)*
48
+ > **`medium` = `low` + near-duplicate removal + light summarization of plain prose.** Code blocks, lists, tables, `Key: value` lines, questions, and any sentence with a rule, number, ID or variable are never removed. On the dense prompts in the benchmark it saves about the same as `low` (24% vs 22%) and kept 94/94 critical facts. It helps most on long, chatty prose. *(Before 1.0.3, `medium` chained three summarizers and kept only 22% of critical facts. Upgrade if you used it.)*
49
49
 
50
50
  ## Custom options
51
51
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prompt-compressor",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
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",
package/src/algorithms.js CHANGED
@@ -13,11 +13,16 @@
13
13
  * @risk none | @savings 5–15%
14
14
  */
15
15
  export function compressWhitespace(text) {
16
- return text
17
- .replace(/\r\n/g, "\n").replace(/\r/g, "\n")
18
- .replace(/\t/g, " ").replace(/[ \t]+/g, " ")
19
- .replace(/\n{3,}/g, "\n\n")
20
- .replace(/[ \t]+\n/g, "\n").replace(/\n[ \t]+/g, "\n")
16
+ // Code blocks keep their indentation (Python/YAML break without it);
17
+ // only trailing spaces are trimmed inside ``` fences.
18
+ return text.replace(/\r\n?/g, "\n")
19
+ .split(/(```[\s\S]*?```)/g)
20
+ .map(p => p.startsWith("```")
21
+ ? p.replace(/[ \t]+\n/g, "\n")
22
+ : p.replace(/\t/g, " ").replace(/[ \t]+/g, " ")
23
+ .replace(/\n{3,}/g, "\n\n")
24
+ .replace(/[ \t]+\n/g, "\n").replace(/\n[ \t]+/g, "\n"))
25
+ .join("")
21
26
  .trim();
22
27
  }
23
28
 
@@ -46,7 +51,7 @@ export function cleanPunctuation(text) {
46
51
  return text
47
52
  .replace(/!{2,}/g, "!").replace(/\?{2,}/g, "?")
48
53
  .replace(/\.{4,}/g, "...").replace(/,{2,}/g, ",")
49
- .replace(/ {2,}/g, " ");
54
+ .replace(/(\S) {2,}/g, "$1 ");
50
55
  }
51
56
 
52
57
  /**
@@ -100,7 +105,7 @@ export function removeFiller(text) {
100
105
  for (const [p, r] of words) out = out.replace(p, r);
101
106
  out = out.replace(/\u0000CAP(\s*)(\S)/g, (_, ws, ch) => ws + ch.toUpperCase()).replace(/\u0000CAP/g, "");
102
107
  // 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();
108
+ return out.replace(/(\S)[ \t]{2,}/g, "$1 ").replace(/[ \t]+\n/g, "\n").replace(/\n{3,}/g, "\n\n").trim();
104
109
  }
105
110
 
106
111
  /**
@@ -132,7 +137,7 @@ export function shortenPhrases(text) {
132
137
  [/\bfor the reason that\b/gi, "because"],
133
138
  ];
134
139
  let out = text;
135
- for (const [p, r] of map) out = out.replace(p, r);
140
+ for (const [p, r] of map) out = out.replace(p, (m) => (/^[A-Z]/.test(m) && r ? r[0].toUpperCase() + r.slice(1) : r));
136
141
  return out;
137
142
  }
138
143
 
@@ -171,7 +176,7 @@ export function removeHedging(text) {
171
176
  ];
172
177
  let out = text;
173
178
  for (const [p, r] of patterns) out = out.replace(p, r);
174
- return out.replace(/[ \t]{2,}/g, " ").replace(/[ \t]+\n/g, "\n").trim();
179
+ return out.replace(/(\S)[ \t]{2,}/g, "$1 ").replace(/[ \t]+\n/g, "\n").trim();
175
180
  }
176
181
 
177
182
  /**
@@ -179,16 +184,23 @@ export function removeHedging(text) {
179
184
  * Examples: "Here is a...", "I hope this helps", "I'd be happy to help"
180
185
  * @risk low | @savings 5–15%
181
186
  */
187
+ // A lead-in that carries a rule, number, ID or variable is content, not fluff.
188
+ const KEEP_LEADIN = /\b(?:must|never|always|only|not|don't|do not|cannot|can't|required?)\b|\d|\{\{?|\$\{|%\w+%|\x00/i;
189
+ const unlessCritical = (m) => (KEEP_LEADIN.test(m) ? m : "");
190
+
182
191
  export function removeMetaCommentary(text) {
183
192
  const patterns = [
184
- [/^(sure[,!]?\s*|of course[,!]?\s*|absolutely[,!]?\s*|certainly[,!]?\s*)/gim, ""],
185
- [/^(i('d| would) be happy to (help|assist)[^.!?\n]*[.!?]\s*)/gim, ""],
186
- [/^(here('s| is) (a |an |the )?[^.!?\n]*[.!?:]\s*)/gim, ""],
187
- [/^(the following (is|are|provides)[^.!?\n]*[.!?:]\s*)/gim, ""],
188
- [/^(below (is|are)[^.!?\n]*[.!?:]\s*)/gim, ""],
189
- [/(i hope (this|that) (helps?|is helpful)[^.!?\n]*[.!?]\s*)/gi, ""],
190
- [/(please (let me know|don't hesitate to ask)[^.!?\n]*[.!?]\s*)/gi, ""],
191
- [/(if you have (any )?(further )?(questions?)[^.!?\n]*[.!?]\s*)/gi, ""],
193
+ // "Sure," / "Of course!" — only with punctuation, so "Surely…"/"Great care…" stay
194
+ [/^(?:sure|of course|absolutely|certainly)[,!.][ \t]*/gim, ""],
195
+ [/^i(?:'d| would) be happy to (?:help|assist)[^.!?\n]*[.!?][ \t]*/gim, unlessCritical],
196
+ // "Here is …" / "The following …" / "Below is …": only a whole lead-in sentence
197
+ // ending in "." or a line that ends in ":" (never "…: <content>")
198
+ [/^here(?:'s| is| are) (?:a |an |the )?[^.!?:\n]*(?:\.[ \t]*|:[ \t]*$)/gim, unlessCritical],
199
+ [/^the following (?:is|are|provides)[^.!?:\n]*(?:\.[ \t]*|:[ \t]*$)/gim, unlessCritical],
200
+ [/^below (?:is|are)[^.!?:\n]*(?:\.[ \t]*|:[ \t]*$)/gim, unlessCritical],
201
+ [/i hope (?:this|that) (?:helps?|is helpful)[^.!?\n]*[.!?][ \t]*/gi, unlessCritical],
202
+ [/please (?:let me know|don't hesitate to ask)[^.!?\n]*[.!?][ \t]*/gi, unlessCritical],
203
+ [/if you have (?:any )?(?:further )?questions?[^.!?\n]*[.!?][ \t]*/gi, unlessCritical],
192
204
  ];
193
205
  let out = text;
194
206
  for (const [p, r] of patterns) out = out.replace(p, r);
@@ -209,7 +221,7 @@ export function removeParentheticals(text) {
209
221
  ];
210
222
  let out = text;
211
223
  for (const p of patterns) out = out.replace(p, "");
212
- return out.replace(/[ \t]{2,}/g, " ").replace(/[ \t]+\n/g, "\n").trim();
224
+ return out.replace(/(\S)[ \t]{2,}/g, "$1 ").replace(/[ \t]+\n/g, "\n").trim();
213
225
  }
214
226
 
215
227
  /**
@@ -316,9 +328,13 @@ export function minifySQL(text) {
316
328
  * @risk none | @savings 3–15%
317
329
  */
318
330
  export function normalizeIndentation(text) {
319
- return text.replace(/```[\s\S]*?```/g, block =>
320
- block.replace(/^( )+/gm, m => " ".repeat(m.length / 4))
321
- );
331
+ // 4-space → 2-space, only for blocks indented in steps of 4 (halving a
332
+ // 2-space block would merge nesting levels).
333
+ return text.replace(/```[\s\S]*?```/g, block => {
334
+ const indents = block.split("\n").map(l => (l.match(/^ +/) || [""])[0].length).filter(n => n > 0);
335
+ if (!indents.length || indents.some(n => n % 4 !== 0)) return block;
336
+ return block.replace(/^( )+/gm, m => " ".repeat(m.length / 4));
337
+ });
322
338
  }
323
339
 
324
340
  /**