@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.
- package/package.json +1 -1
- package/src/index.js +19 -0
package/package.json
CHANGED
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
|
|