@drsamonline/compression 0.12.7 → 0.12.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +19 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drsamonline/compression",
3
- "version": "0.12.7",
3
+ "version": "0.12.8",
4
4
  "description": "Prompt compression engines, presets and the quota-aware budget manager.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.js CHANGED
@@ -609,6 +609,25 @@ export function compress(text, opts = {}) {
609
609
  }
610
610
 
611
611
  const restored = restore(current, spans);
612
+
613
+ // Inflation guard, on by default. Restoring protected spans can make the
614
+ // "compressed" text longer than the input — a compressor that grows a prompt
615
+ // costs the user tokens and charges them for the privilege. If it did not
616
+ // shrink, the original goes through untouched and the result says why.
617
+ if (restored.length >= originalChars) {
618
+ return {
619
+ text: original,
620
+ preset: presetName,
621
+ originalChars,
622
+ compressedChars: originalChars,
623
+ ratio: 0,
624
+ savedChars: 0,
625
+ skipped: 'inflation: compression would not have shrunk this prompt',
626
+ steps,
627
+ tokens,
628
+ };
629
+ }
630
+
612
631
  const compressedChars = restored.length;
613
632
  const savedChars = Math.max(0, originalChars - compressedChars);
614
633