trimprompt 1.0.30 → 1.0.31

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/mcp.js CHANGED
@@ -1 +1,132 @@
1
- 'use strict';const a0_0x39f816=a0_0x1e90;(function(_0x325918,_0x4f3c0f){const _0x4beab2=a0_0x1e90,_0x1339da=_0x325918();while(!![]){try{const _0x47ac28=parseInt(_0x4beab2(0x1e8))/0x1*(-parseInt(_0x4beab2(0x21c))/0x2)+parseInt(_0x4beab2(0x20c))/0x3+-parseInt(_0x4beab2(0x203))/0x4*(parseInt(_0x4beab2(0x214))/0x5)+parseInt(_0x4beab2(0x227))/0x6*(parseInt(_0x4beab2(0x21b))/0x7)+-parseInt(_0x4beab2(0x1ee))/0x8+parseInt(_0x4beab2(0x1ed))/0x9*(-parseInt(_0x4beab2(0x1f1))/0xa)+parseInt(_0x4beab2(0x1f2))/0xb*(-parseInt(_0x4beab2(0x1ec))/0xc);if(_0x47ac28===_0x4f3c0f)break;else _0x1339da['push'](_0x1339da['shift']());}catch(_0x67d1bf){_0x1339da['push'](_0x1339da['shift']());}}}(a0_0x1aaa,0x8863d));var __createBinding=this&&this[a0_0x39f816(0x1ea)]||(Object[a0_0x39f816(0x221)]?function(_0x43d500,_0x124459,_0xc24552,_0x26f9d6){const _0x20fd68=a0_0x39f816;if(_0x26f9d6===undefined)_0x26f9d6=_0xc24552;var _0x3bba64=Object[_0x20fd68(0x20e)](_0x124459,_0xc24552);(!_0x3bba64||(_0x20fd68(0x225)in _0x3bba64?!_0x124459[_0x20fd68(0x20f)]:_0x3bba64[_0x20fd68(0x1e3)]||_0x3bba64[_0x20fd68(0x202)]))&&(_0x3bba64={'enumerable':!![],'get':function(){return _0x124459[_0xc24552];}}),Object[_0x20fd68(0x20b)](_0x43d500,_0x26f9d6,_0x3bba64);}:function(_0x1b5491,_0x465222,_0x4d49e3,_0x3c6592){if(_0x3c6592===undefined)_0x3c6592=_0x4d49e3;_0x1b5491[_0x3c6592]=_0x465222[_0x4d49e3];}),__setModuleDefault=this&&this[a0_0x39f816(0x211)]||(Object[a0_0x39f816(0x221)]?function(_0x2c703e,_0x44abba){const _0xc11ce9=a0_0x39f816;Object[_0xc11ce9(0x20b)](_0x2c703e,_0xc11ce9(0x213),{'enumerable':!![],'value':_0x44abba});}:function(_0x298f66,_0x5ea884){const _0x2c42d5=a0_0x39f816;_0x298f66[_0x2c42d5(0x213)]=_0x5ea884;}),__importStar=this&&this[a0_0x39f816(0x1e6)]||(function(){var _0x32330c=function(_0x5af01a){return _0x32330c=Object['getOwnPropertyNames']||function(_0x2c542d){const _0x4aab8f=a0_0x1e90;var _0x5a4d79=[];for(var _0x265f67 in _0x2c542d)if(Object[_0x4aab8f(0x1e7)][_0x4aab8f(0x1fd)][_0x4aab8f(0x218)](_0x2c542d,_0x265f67))_0x5a4d79[_0x5a4d79['length']]=_0x265f67;return _0x5a4d79;},_0x32330c(_0x5af01a);};return function(_0x161a03){const _0x135f70=a0_0x1e90;if(_0x161a03&&_0x161a03['__esModule'])return _0x161a03;var _0x47cd18={};if(_0x161a03!=null){for(var _0x104405=_0x32330c(_0x161a03),_0x42e25e=0x0;_0x42e25e<_0x104405['length'];_0x42e25e++)if(_0x104405[_0x42e25e]!==_0x135f70(0x213))__createBinding(_0x47cd18,_0x161a03,_0x104405[_0x42e25e]);}return __setModuleDefault(_0x47cd18,_0x161a03),_0x47cd18;};}());Object[a0_0x39f816(0x20b)](exports,a0_0x39f816(0x20f),{'value':!![]}),exports[a0_0x39f816(0x1f9)]=runMcpProxy;function a0_0x1e90(_0x270b79,_0x18a4b0){_0x270b79=_0x270b79-0x1e3;const _0x1aaa7c=a0_0x1aaa();let _0x1e9063=_0x1aaa7c[_0x270b79];return _0x1e9063;}const child_process_1=require(a0_0x39f816(0x1f0)),readline=__importStar(require('readline')),filters_1=require('./filters'),tracker_1=require('./tracker'),proxy_conv_1=require(a0_0x39f816(0x20d)),proxy_resp_1=require(a0_0x39f816(0x1f5));function a0_0x1aaa(){const _0x345081=['messages','logExecution','savedTokens','max_tokens','stdout','defineProperty','1262292WIqdaR','./proxy-conv','getOwnPropertyDescriptor','__esModule','inherit','__setModuleDefault','result','default','263700PncXhm','mcp-tool-call','isArray','features_enabled','call','trim','stringify','4193CIPmxU','166aTQNen','trimmedCount','stdin','compressConversation','complexity','create','2.0','exit','content','get','length','10074LtYXVa','writable','resp_optimize','params','__importStar','prototype','1619ORIMzb','line','__createBinding','jsonrpc','162156eFRjxH','32949lvjQfq','589200jmqBDV','mcp\x20tool\x20(id:\x20','child_process','820sMViOo','121oUpJGF','createInterface','proxy:conv-compress','./proxy-resp','text','spawn','method','runMcpProxy','optimizeMaxTokens','redaction_enabled','error','hasOwnProperty','tracker','\x20messages\x20(','close','type','configurable','16EBhXPm','write','getConfig'];a0_0x1aaa=function(){return _0x345081;};return a0_0x1aaa();}function runMcpProxy(_0x676797,_0x723cdd){const _0x20057d=a0_0x39f816,_0x42bc01=(0x0,child_process_1[_0x20057d(0x1f7)])(_0x676797,_0x723cdd,{'stdio':['pipe','pipe',_0x20057d(0x210)]}),_0x327a82=tracker_1['tracker'][_0x20057d(0x205)](),_0x56625b=_0x327a82[_0x20057d(0x217)]||{},_0x3c9f8c=_0x56625b['conv_compress']!==![],_0x3744c4=_0x56625b[_0x20057d(0x1e4)]!==![],_0x42ca0c=readline[_0x20057d(0x1f3)]({'input':process[_0x20057d(0x21e)],'terminal':![]});_0x42ca0c['on'](_0x20057d(0x1e9),_0x2bb7c4=>{const _0x51d234=_0x20057d;try{const _0x2a9e9d=JSON['parse'](_0x2bb7c4);if(_0x2a9e9d&&_0x2a9e9d[_0x51d234(0x1eb)]===_0x51d234(0x222)&&_0x2a9e9d[_0x51d234(0x1f8)]&&_0x2a9e9d[_0x51d234(0x1e5)]){const _0xeac1b8=_0x2a9e9d['params'];if(Array['isArray'](_0xeac1b8[_0x51d234(0x206)])&&_0xeac1b8[_0x51d234(0x206)][_0x51d234(0x226)]>0x0){if(_0x3c9f8c){const _0x12ace0=(0x0,proxy_conv_1[_0x51d234(0x21f)])(_0xeac1b8[_0x51d234(0x206)]);(_0x12ace0[_0x51d234(0x21d)]>0x0||_0x12ace0[_0x51d234(0x208)]>0x0)&&(_0xeac1b8[_0x51d234(0x206)]=_0x12ace0[_0x51d234(0x206)],tracker_1[_0x51d234(0x1fe)]['logExecution'](_0x51d234(0x1f4),_0x12ace0[_0x51d234(0x21d)]+_0x12ace0[_0x51d234(0x206)][_0x51d234(0x226)]+'\x20messages',_0x12ace0[_0x51d234(0x206)][_0x51d234(0x226)]+_0x51d234(0x1ff)+_0x12ace0[_0x51d234(0x21d)]+'\x20trimmed,\x20~'+_0x12ace0['savedTokens']+'\x20tokens\x20saved)',0x0,undefined,['conv_compress']));}if(_0x3744c4){const _0x512955=(0x0,proxy_resp_1[_0x51d234(0x1fa)])(_0xeac1b8['messages'],_0xeac1b8[_0x51d234(0x209)]);if(_0x512955){const _0x154385=_0xeac1b8[_0x51d234(0x209)]||_0x51d234(0x213);_0xeac1b8[_0x51d234(0x209)]=_0x512955[_0x51d234(0x209)],tracker_1[_0x51d234(0x1fe)][_0x51d234(0x207)]('proxy:resp-optimize','max_tokens:\x20'+_0x154385,'max_tokens:\x20'+_0x512955['max_tokens']+'\x20('+_0x512955[_0x51d234(0x220)]+')',0x0,undefined,[_0x51d234(0x1e4)]);}}}_0x42bc01[_0x51d234(0x21e)][_0x51d234(0x204)](JSON[_0x51d234(0x21a)](_0x2a9e9d)+'\x0a');}else _0x42bc01[_0x51d234(0x21e)][_0x51d234(0x204)](_0x2bb7c4+'\x0a');}catch{_0x42bc01[_0x51d234(0x21e)][_0x51d234(0x204)](_0x2bb7c4+'\x0a');}}),_0x42ca0c['on'](_0x20057d(0x200),()=>{try{_0x42bc01['stdin']['end']();}catch{}});const _0x12e818=readline[_0x20057d(0x1f3)]({'input':_0x42bc01['stdout'],'terminal':![]});_0x12e818['on']('line',_0x335672=>{const _0x37e2b5=_0x20057d;try{const _0x46f819=JSON['parse'](_0x335672);if(_0x46f819&&_0x46f819[_0x37e2b5(0x1eb)]===_0x37e2b5(0x222)&&_0x46f819['id']!==undefined&&_0x46f819[_0x37e2b5(0x212)]&&Array[_0x37e2b5(0x216)](_0x46f819[_0x37e2b5(0x212)][_0x37e2b5(0x224)])){let _0x3030ed=![];for(const _0x509a12 of _0x46f819[_0x37e2b5(0x212)][_0x37e2b5(0x224)]){if(_0x509a12[_0x37e2b5(0x201)]===_0x37e2b5(0x1f6)&&typeof _0x509a12[_0x37e2b5(0x1f6)]==='string'&&_0x509a12['text'][_0x37e2b5(0x219)]()[_0x37e2b5(0x226)]>0x0){const _0x302b6e=_0x509a12[_0x37e2b5(0x1f6)],_0x58d6d3=(0x0,filters_1['compressOutput'])(_0x37e2b5(0x215),_0x302b6e,'',tracker_1[_0x37e2b5(0x1fe)][_0x37e2b5(0x205)]()[_0x37e2b5(0x1fb)]);tracker_1[_0x37e2b5(0x1fe)][_0x37e2b5(0x207)](_0x37e2b5(0x1ef)+_0x46f819['id']+')',_0x302b6e,_0x58d6d3,0x0),_0x509a12[_0x37e2b5(0x1f6)]=_0x58d6d3,_0x3030ed=!![];}}_0x3030ed?process[_0x37e2b5(0x20a)][_0x37e2b5(0x204)](JSON[_0x37e2b5(0x21a)](_0x46f819)+'\x0a'):process[_0x37e2b5(0x20a)][_0x37e2b5(0x204)](_0x335672+'\x0a');}else process[_0x37e2b5(0x20a)][_0x37e2b5(0x204)](_0x335672+'\x0a');}catch{process[_0x37e2b5(0x20a)][_0x37e2b5(0x204)](_0x335672+'\x0a');}}),_0x42bc01['on'](_0x20057d(0x200),_0xba9385=>{const _0x531b39=_0x20057d;process[_0x531b39(0x223)](_0xba9385||0x0);}),_0x42bc01['on']('error',_0x145388=>{const _0x17dc2d=_0x20057d;console[_0x17dc2d(0x1fc)]('[TrimPrompt]\x20MCP\x20Server\x20Spawn\x20Error:\x20'+_0x145388['message']),process[_0x17dc2d(0x223)](0x1);});}
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.runMcpProxy = runMcpProxy;
37
+ const child_process_1 = require("child_process");
38
+ const readline = __importStar(require("readline"));
39
+ const filters_1 = require("./filters");
40
+ const tracker_1 = require("./tracker");
41
+ const proxy_conv_1 = require("./proxy-conv");
42
+ const proxy_resp_1 = require("./proxy-resp");
43
+ function runMcpProxy(command, args) {
44
+ const child = (0, child_process_1.spawn)(command, args, {
45
+ stdio: ['pipe', 'pipe', 'inherit']
46
+ });
47
+ const config = tracker_1.tracker.getConfig();
48
+ const featuresEnabled = config.features_enabled || {};
49
+ const convEnabled = featuresEnabled.conv_compress !== false;
50
+ const respEnabled = featuresEnabled.resp_optimize !== false;
51
+ const stdinRl = readline.createInterface({ input: process.stdin, terminal: false });
52
+ stdinRl.on('line', (line) => {
53
+ try {
54
+ const msg = JSON.parse(line);
55
+ if (msg && msg.jsonrpc === '2.0' && msg.method && msg.params) {
56
+ const params = msg.params;
57
+ if (Array.isArray(params.messages) && params.messages.length > 0) {
58
+ if (convEnabled) {
59
+ const result = (0, proxy_conv_1.compressConversation)(params.messages);
60
+ if (result.trimmedCount > 0 || result.savedTokens > 0) {
61
+ params.messages = result.messages;
62
+ tracker_1.tracker.logExecution('proxy:conv-compress', `${result.trimmedCount + result.messages.length} messages`, `${result.messages.length} messages (${result.trimmedCount} trimmed, ~${result.savedTokens} tokens saved)`, 0, undefined, ['conv_compress']);
63
+ }
64
+ }
65
+ if (respEnabled) {
66
+ const opt = (0, proxy_resp_1.optimizeMaxTokens)(params.messages, params.max_tokens);
67
+ if (opt) {
68
+ const prevMax = params.max_tokens || 'default';
69
+ params.max_tokens = opt.max_tokens;
70
+ tracker_1.tracker.logExecution('proxy:resp-optimize', `max_tokens: ${prevMax}`, `max_tokens: ${opt.max_tokens} (${opt.complexity})`, 0, undefined, ['resp_optimize']);
71
+ }
72
+ }
73
+ }
74
+ child.stdin.write(JSON.stringify(msg) + '\n');
75
+ }
76
+ else {
77
+ child.stdin.write(line + '\n');
78
+ }
79
+ }
80
+ catch {
81
+ child.stdin.write(line + '\n');
82
+ }
83
+ });
84
+ stdinRl.on('close', () => { try {
85
+ child.stdin.end();
86
+ }
87
+ catch { } });
88
+ const rl = readline.createInterface({
89
+ input: child.stdout,
90
+ terminal: false
91
+ });
92
+ rl.on('line', (line) => {
93
+ try {
94
+ const msg = JSON.parse(line);
95
+ if (msg &&
96
+ msg.jsonrpc === '2.0' &&
97
+ msg.id !== undefined &&
98
+ msg.result &&
99
+ Array.isArray(msg.result.content)) {
100
+ let textCompacted = false;
101
+ for (const item of msg.result.content) {
102
+ if (item.type === 'text' && typeof item.text === 'string' && item.text.trim().length > 0) {
103
+ const rawText = item.text;
104
+ const compressedText = (0, filters_1.compressOutput)('mcp-tool-call', rawText, '', tracker_1.tracker.getConfig().redaction_enabled);
105
+ tracker_1.tracker.logExecution(`mcp tool (id: ${msg.id})`, rawText, compressedText, 0);
106
+ item.text = compressedText;
107
+ textCompacted = true;
108
+ }
109
+ }
110
+ if (textCompacted) {
111
+ process.stdout.write(JSON.stringify(msg) + '\n');
112
+ }
113
+ else {
114
+ process.stdout.write(line + '\n');
115
+ }
116
+ }
117
+ else {
118
+ process.stdout.write(line + '\n');
119
+ }
120
+ }
121
+ catch {
122
+ process.stdout.write(line + '\n');
123
+ }
124
+ });
125
+ child.on('close', (code) => {
126
+ process.exit(code || 0);
127
+ });
128
+ child.on('error', (err) => {
129
+ console.error(`[TrimPrompt] MCP Server Spawn Error: ${err.message}`);
130
+ process.exit(1);
131
+ });
132
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trimprompt",
3
- "version": "1.0.30",
3
+ "version": "1.0.31",
4
4
  "description": "TrimPrompt — Save 80% on LLM tokens. Zero-config CLI proxy for AI coding agents",
5
5
  "keywords": [
6
6
  "llm",
@@ -24,9 +24,6 @@
24
24
  "trim": "cli.js"
25
25
  },
26
26
  "homepage": "https://trimprompt.ai",
27
- "scripts": {
28
- "postinstall": "node cli.js shims install || true"
29
- },
30
27
  "dependencies": {
31
28
  "commander": "^11.1.0",
32
29
  "express": "^4.18.2",
package/proxy-conv.js CHANGED
@@ -1 +1,181 @@
1
- 'use strict';function a0_0x5e51(_0x3355e4,_0x1b2e5e){_0x3355e4=_0x3355e4-0x1ef;const _0x2cd10e=a0_0x2cd1();let _0x5e51ab=_0x2cd10e[_0x3355e4];return _0x5e51ab;}const a0_0x362709=a0_0x5e51;(function(_0x349dbf,_0x3688b1){const _0x488f75=a0_0x5e51,_0x3f5a01=_0x349dbf();while(!![]){try{const _0xd5e438=-parseInt(_0x488f75(0x206))/0x1+-parseInt(_0x488f75(0x1fe))/0x2*(-parseInt(_0x488f75(0x1f6))/0x3)+parseInt(_0x488f75(0x1f5))/0x4+-parseInt(_0x488f75(0x214))/0x5+parseInt(_0x488f75(0x1f4))/0x6+parseInt(_0x488f75(0x1f7))/0x7+-parseInt(_0x488f75(0x1f0))/0x8;if(_0xd5e438===_0x3688b1)break;else _0x3f5a01['push'](_0x3f5a01['shift']());}catch(_0x2b7b2a){_0x3f5a01['push'](_0x3f5a01['shift']());}}}(a0_0x2cd1,0x981ae));Object[a0_0x362709(0x1f2)](exports,a0_0x362709(0x1f8),{'value':!![]}),exports[a0_0x362709(0x215)]=compressConversation;const ROLE_WEIGHT={'system':0x1,'user':0.8,'assistant':0.5,'tool':0.3};function a0_0x2cd1(){const _0x31a5ca=['string','role','slice','\x20lines\x20trimmed\x20by\x20TrimPrompt\x20...]\x0a','sort','map','forEach','tokens','push','isArray','tool_result','\x20chars\x20trimmed\x20...]','4717680HfRpnJ','compressConversation','type','3051632CqFTUm','system','defineProperty','originalIndex','3092352igTgWC','3118604rLUojB','476472diFMbj','8616993MilPEN','__esModule','reduce','\x20lines\x20trimmed\x20...]','content','score','\x20chars\x20trimmed\x20by\x20TrimPrompt\x20...]','2xWhWqc','filter','indexOf','msg','object','length','\x0a[...\x20','text','736854lbyMcY','join'];a0_0x2cd1=function(){return _0x31a5ca;};return a0_0x2cd1();}function estimateTokens(_0x14d85d){const _0x4e14c5=a0_0x362709;return Math['ceil'](_0x14d85d[_0x4e14c5(0x203)]/0x4);}function blockText(_0x371b06){const _0x399d58=a0_0x362709;if(!_0x371b06||typeof _0x371b06!==_0x399d58(0x202))return'';if(_0x371b06['type']===_0x399d58(0x205)&&typeof _0x371b06[_0x399d58(0x205)]===_0x399d58(0x208))return _0x371b06[_0x399d58(0x205)];if(_0x371b06[_0x399d58(0x1ef)]===_0x399d58(0x212)){if(typeof _0x371b06[_0x399d58(0x1fb)]===_0x399d58(0x208))return _0x371b06['content'];if(Array[_0x399d58(0x211)](_0x371b06[_0x399d58(0x1fb)]))return _0x371b06[_0x399d58(0x1fb)][_0x399d58(0x20d)](blockText)['filter'](Boolean)[_0x399d58(0x207)]('\x0a');}return'';}function messageText(_0x5dc84d){const _0xd89db4=a0_0x362709;if(typeof _0x5dc84d[_0xd89db4(0x1fb)]===_0xd89db4(0x208))return _0x5dc84d[_0xd89db4(0x1fb)];if(Array[_0xd89db4(0x211)](_0x5dc84d['content']))return _0x5dc84d[_0xd89db4(0x1fb)]['map'](blockText)[_0xd89db4(0x1ff)](Boolean)[_0xd89db4(0x207)]('\x0a');return'';}function summarizeText(_0x2781e9){const _0xdd7d9a=a0_0x362709,_0x4f2c19=estimateTokens(_0x2781e9),_0x496ecf=_0x2781e9['split']('\x0a');if(_0x4f2c19>0x7d0){if(_0x496ecf[_0xdd7d9a(0x203)]>0xa){const _0x690b8b=_0x496ecf[_0xdd7d9a(0x20a)](0x0,0x5)[_0xdd7d9a(0x207)]('\x0a'),_0x59a15d=_0x496ecf['slice'](-0x5)[_0xdd7d9a(0x207)]('\x0a');return _0x690b8b+_0xdd7d9a(0x204)+(_0x496ecf['length']-0xa)+_0xdd7d9a(0x20b)+_0x59a15d;}return _0x2781e9[_0xdd7d9a(0x20a)](0x0,0x7d0)+(_0xdd7d9a(0x204)+(_0x2781e9[_0xdd7d9a(0x203)]-0x7d0)+_0xdd7d9a(0x1fd));}if(_0x4f2c19>0x1f4){if(_0x496ecf[_0xdd7d9a(0x203)]>0xa)return _0x496ecf[_0xdd7d9a(0x20a)](0x0,0xa)[_0xdd7d9a(0x207)]('\x0a')+(_0xdd7d9a(0x204)+(_0x496ecf[_0xdd7d9a(0x203)]-0xa)+_0xdd7d9a(0x1fa));return _0x2781e9[_0xdd7d9a(0x20a)](0x0,0x320)+('\x0a[...\x20'+(_0x2781e9[_0xdd7d9a(0x203)]-0x320)+_0xdd7d9a(0x213));}return _0x2781e9;}function summarizeBlock(_0x321029){const _0x1f7dd5=a0_0x362709;if(!_0x321029||typeof _0x321029!==_0x1f7dd5(0x202))return _0x321029;if(_0x321029[_0x1f7dd5(0x1ef)]==='text'&&typeof _0x321029[_0x1f7dd5(0x205)]===_0x1f7dd5(0x208))return{..._0x321029,'text':summarizeText(_0x321029[_0x1f7dd5(0x205)])};if(_0x321029['type']===_0x1f7dd5(0x212)){if(typeof _0x321029['content']===_0x1f7dd5(0x208))return{..._0x321029,'content':summarizeText(_0x321029[_0x1f7dd5(0x1fb)])};if(Array[_0x1f7dd5(0x211)](_0x321029[_0x1f7dd5(0x1fb)])){const _0x28ecda=largestBlockIndex(_0x321029['content']);if(_0x28ecda>=0x0){const _0x4e524f=_0x321029['content'][_0x1f7dd5(0x20d)]((_0x5d8ea1,_0x52ba2b)=>_0x52ba2b===_0x28ecda?summarizeBlock(_0x5d8ea1):_0x5d8ea1);return{..._0x321029,'content':_0x4e524f};}}}return _0x321029;}function largestBlockIndex(_0x5c6dd7){const _0x7ad24e=a0_0x362709;let _0x51af54=-0x1,_0x4ef777=-0x1;return _0x5c6dd7[_0x7ad24e(0x20e)]((_0x2ac5fb,_0x4f6f7f)=>{const _0x53eab0=_0x7ad24e,_0x3d4686=blockText(_0x2ac5fb)[_0x53eab0(0x203)];_0x3d4686>_0x4ef777&&(_0x4ef777=_0x3d4686,_0x51af54=_0x4f6f7f);}),_0x4ef777>0x0?_0x51af54:-0x1;}function scoreMessage(_0x337a4d,_0x37bacb,_0x27449c){const _0x5392e1=ROLE_WEIGHT[_0x337a4d['role']]||0.4,_0x154b45=(_0x37bacb+0x1)/_0x27449c,_0x2c1648=messageText(_0x337a4d),_0x22e5a4=estimateTokens(_0x2c1648),_0x5ed6bb=_0x22e5a4>0x7d0?0.6:_0x22e5a4>0x1f4?0.8:0x1;return _0x5392e1*(0.3+0.7*_0x154b45)*_0x5ed6bb;}function summarizeMessage(_0x2ee05a){const _0x36d2fd=a0_0x362709,_0x3f36f5=estimateTokens(messageText(_0x2ee05a));if(_0x3f36f5<=0x1f4)return _0x2ee05a;if(typeof _0x2ee05a[_0x36d2fd(0x1fb)]==='string')return{..._0x2ee05a,'content':summarizeText(_0x2ee05a[_0x36d2fd(0x1fb)])};if(Array[_0x36d2fd(0x211)](_0x2ee05a[_0x36d2fd(0x1fb)])){const _0x7da060=largestBlockIndex(_0x2ee05a['content']);if(_0x7da060>=0x0){const _0x427a0b=_0x2ee05a[_0x36d2fd(0x1fb)][_0x36d2fd(0x20d)]((_0x24cb14,_0x15ea5c)=>_0x15ea5c===_0x7da060?summarizeBlock(_0x24cb14):_0x24cb14);return{..._0x2ee05a,'content':_0x427a0b};}}return _0x2ee05a;}function compressConversation(_0x37129e,_0xf3542e){const _0x2386ec=a0_0x362709,_0x5a03eb={'messages':_0x37129e,'trimmedCount':0x0,'savedTokens':0x0};if(!_0x37129e||!Array[_0x2386ec(0x211)](_0x37129e)||_0x37129e[_0x2386ec(0x203)]<=0x6)return _0x5a03eb;try{return _compressConversation(_0x37129e,_0xf3542e);}catch{return _0x5a03eb;}}function _compressConversation(_0x3a5ac7,_0x11c21a){const _0x5c2c95=a0_0x362709,_0x46d170=_0x11c21a||0x13880,_0xbb82fe=0x4,_0x185c77=_0x3a5ac7['filter'](_0xcb5362=>_0xcb5362['role']===_0x5c2c95(0x1f1)),_0x59478c=_0x3a5ac7[_0x5c2c95(0x1ff)](_0x322038=>_0x322038[_0x5c2c95(0x209)]!=='system'),_0x504cbf=_0x59478c[_0x5c2c95(0x20a)](-_0xbb82fe),_0x575b39=_0x59478c[_0x5c2c95(0x20a)](0x0,-_0xbb82fe),_0x2ab68d=_0x575b39[_0x5c2c95(0x20d)]((_0x18d02f,_0x461fb6)=>({'msg':_0x18d02f,'score':scoreMessage(_0x18d02f,_0x461fb6,_0x575b39[_0x5c2c95(0x203)]),'tokens':estimateTokens(messageText(_0x18d02f))})),_0x193538=_0x2ab68d[_0x5c2c95(0x1f9)]((_0xb9893f,_0x59a45e)=>_0xb9893f+_0x59a45e[_0x5c2c95(0x20f)],0x0),_0x7c06f9=_0x504cbf[_0x5c2c95(0x1f9)]((_0x295fed,_0x5ea4dd)=>_0x295fed+estimateTokens(messageText(_0x5ea4dd)),0x0),_0x7fa775=_0x185c77[_0x5c2c95(0x1f9)]((_0x2587bc,_0x261a32)=>_0x2587bc+estimateTokens(messageText(_0x261a32)),0x0),_0x27141e=_0x46d170-_0x7c06f9-_0x7fa775;if(_0x193538<=_0x27141e){let _0x3a8436=0x0;const _0x8ea163=_0x2ab68d[_0x5c2c95(0x20d)](_0x480287=>{const _0x40cf8a=_0x5c2c95;if(_0x480287[_0x40cf8a(0x20f)]>0x1f4){const _0x44a55f=summarizeMessage(_0x480287[_0x40cf8a(0x201)]),_0xb0a237=estimateTokens(messageText(_0x44a55f));if(_0xb0a237<_0x480287['tokens'])return _0x3a8436+=_0x480287[_0x40cf8a(0x20f)]-_0xb0a237,_0x44a55f;}return _0x480287[_0x40cf8a(0x201)];});return{'messages':[..._0x185c77,..._0x8ea163,..._0x504cbf],'trimmedCount':0x0,'savedTokens':_0x3a8436};}_0x2ab68d[_0x5c2c95(0x20c)]((_0x18c24d,_0x49955e)=>_0x49955e[_0x5c2c95(0x1fc)]-_0x18c24d[_0x5c2c95(0x1fc)]);let _0xb4d244=0x0,_0x47a26c=0x0,_0x2cba12=0x0;const _0x11e267=[];for(const _0x333236 of _0x2ab68d){if(_0xb4d244+_0x333236[_0x5c2c95(0x20f)]<=_0x27141e){if(_0x333236[_0x5c2c95(0x20f)]>0x3e8){const _0x5b9d28=summarizeMessage(_0x333236['msg']),_0x100d6a=estimateTokens(messageText(_0x5b9d28));_0x2cba12+=_0x333236['tokens']-_0x100d6a,_0xb4d244+=_0x100d6a,_0x11e267[_0x5c2c95(0x210)]({'msg':_0x5b9d28,'originalIndex':_0x575b39[_0x5c2c95(0x200)](_0x333236[_0x5c2c95(0x201)])});}else _0xb4d244+=_0x333236[_0x5c2c95(0x20f)],_0x11e267[_0x5c2c95(0x210)]({'msg':_0x333236[_0x5c2c95(0x201)],'originalIndex':_0x575b39[_0x5c2c95(0x200)](_0x333236[_0x5c2c95(0x201)])});}else _0x47a26c++,_0x2cba12+=_0x333236[_0x5c2c95(0x20f)];}_0x11e267[_0x5c2c95(0x20c)]((_0x40bba0,_0x232bde)=>_0x40bba0[_0x5c2c95(0x1f3)]-_0x232bde['originalIndex']);const _0x1ef78a=[..._0x185c77,..._0x11e267['map'](_0x274039=>_0x274039['msg']),..._0x504cbf];return{'messages':_0x1ef78a,'trimmedCount':_0x47a26c,'savedTokens':_0x2cba12};}
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compressConversation = compressConversation;
4
+ const ROLE_WEIGHT = {
5
+ system: 1.0,
6
+ user: 0.8,
7
+ assistant: 0.5,
8
+ tool: 0.3,
9
+ };
10
+ function estimateTokens(text) {
11
+ return Math.ceil(text.length / 4);
12
+ }
13
+ function blockText(b) {
14
+ if (!b || typeof b !== 'object')
15
+ return '';
16
+ if (b.type === 'text' && typeof b.text === 'string')
17
+ return b.text;
18
+ if (b.type === 'tool_result') {
19
+ if (typeof b.content === 'string')
20
+ return b.content;
21
+ if (Array.isArray(b.content))
22
+ return b.content.map(blockText).filter(Boolean).join('\n');
23
+ }
24
+ return '';
25
+ }
26
+ function messageText(msg) {
27
+ if (typeof msg.content === 'string')
28
+ return msg.content;
29
+ if (Array.isArray(msg.content)) {
30
+ return msg.content.map(blockText).filter(Boolean).join('\n');
31
+ }
32
+ return '';
33
+ }
34
+ function summarizeText(text) {
35
+ const tokens = estimateTokens(text);
36
+ const lines = text.split('\n');
37
+ if (tokens > 2000) {
38
+ if (lines.length > 10) {
39
+ const head = lines.slice(0, 5).join('\n');
40
+ const tail = lines.slice(-5).join('\n');
41
+ return `${head}\n[... ${lines.length - 10} lines trimmed by TrimPrompt ...]\n${tail}`;
42
+ }
43
+ return text.slice(0, 2000) + `\n[... ${text.length - 2000} chars trimmed by TrimPrompt ...]`;
44
+ }
45
+ if (tokens > 500) {
46
+ if (lines.length > 10) {
47
+ return lines.slice(0, 10).join('\n') + `\n[... ${lines.length - 10} lines trimmed ...]`;
48
+ }
49
+ return text.slice(0, 800) + `\n[... ${text.length - 800} chars trimmed ...]`;
50
+ }
51
+ return text;
52
+ }
53
+ function summarizeBlock(b) {
54
+ if (!b || typeof b !== 'object')
55
+ return b;
56
+ if (b.type === 'text' && typeof b.text === 'string') {
57
+ return { ...b, text: summarizeText(b.text) };
58
+ }
59
+ if (b.type === 'tool_result') {
60
+ if (typeof b.content === 'string')
61
+ return { ...b, content: summarizeText(b.content) };
62
+ if (Array.isArray(b.content)) {
63
+ const idx = largestBlockIndex(b.content);
64
+ if (idx >= 0) {
65
+ const content = b.content.map((c, i) => (i === idx ? summarizeBlock(c) : c));
66
+ return { ...b, content };
67
+ }
68
+ }
69
+ }
70
+ return b;
71
+ }
72
+ function largestBlockIndex(blocks) {
73
+ let idx = -1, max = -1;
74
+ blocks.forEach((b, i) => {
75
+ const len = blockText(b).length;
76
+ if (len > max) {
77
+ max = len;
78
+ idx = i;
79
+ }
80
+ });
81
+ return max > 0 ? idx : -1;
82
+ }
83
+ function scoreMessage(msg, index, total) {
84
+ const roleW = ROLE_WEIGHT[msg.role] || 0.4;
85
+ const recency = (index + 1) / total;
86
+ const text = messageText(msg);
87
+ const tokens = estimateTokens(text);
88
+ const lengthPenalty = tokens > 2000 ? 0.6 : tokens > 500 ? 0.8 : 1.0;
89
+ return roleW * (0.3 + 0.7 * recency) * lengthPenalty;
90
+ }
91
+ function summarizeMessage(msg) {
92
+ const tokens = estimateTokens(messageText(msg));
93
+ if (tokens <= 500)
94
+ return msg;
95
+ if (typeof msg.content === 'string') {
96
+ return { ...msg, content: summarizeText(msg.content) };
97
+ }
98
+ if (Array.isArray(msg.content)) {
99
+ const idx = largestBlockIndex(msg.content);
100
+ if (idx >= 0) {
101
+ const newContent = msg.content.map((b, i) => (i === idx ? summarizeBlock(b) : b));
102
+ return { ...msg, content: newContent };
103
+ }
104
+ }
105
+ return msg;
106
+ }
107
+ function compressConversation(messages, budgetTokens) {
108
+ const passthrough = { messages, trimmedCount: 0, savedTokens: 0 };
109
+ if (!messages || !Array.isArray(messages) || messages.length <= 6) {
110
+ return passthrough;
111
+ }
112
+ try {
113
+ return _compressConversation(messages, budgetTokens);
114
+ }
115
+ catch {
116
+ return passthrough;
117
+ }
118
+ }
119
+ function _compressConversation(messages, budgetTokens) {
120
+ const budget = budgetTokens || 80000;
121
+ const keepTailCount = 4;
122
+ const systemMsgs = messages.filter(m => m.role === 'system');
123
+ const nonSystem = messages.filter(m => m.role !== 'system');
124
+ const tail = nonSystem.slice(-keepTailCount);
125
+ const middle = nonSystem.slice(0, -keepTailCount);
126
+ const scored = middle.map((msg, i) => ({
127
+ msg,
128
+ score: scoreMessage(msg, i, middle.length),
129
+ tokens: estimateTokens(messageText(msg)),
130
+ }));
131
+ const totalTokens = scored.reduce((s, m) => s + m.tokens, 0);
132
+ const tailTokens = tail.reduce((s, m) => s + estimateTokens(messageText(m)), 0);
133
+ const sysTokens = systemMsgs.reduce((s, m) => s + estimateTokens(messageText(m)), 0);
134
+ const availBudget = budget - tailTokens - sysTokens;
135
+ if (totalTokens <= availBudget) {
136
+ let savedTokens = 0;
137
+ const compressed = scored.map(s => {
138
+ if (s.tokens > 500) {
139
+ const summarized = summarizeMessage(s.msg);
140
+ const newTokens = estimateTokens(messageText(summarized));
141
+ if (newTokens < s.tokens) {
142
+ savedTokens += s.tokens - newTokens;
143
+ return summarized;
144
+ }
145
+ }
146
+ return s.msg;
147
+ });
148
+ return {
149
+ messages: [...systemMsgs, ...compressed, ...tail],
150
+ trimmedCount: 0,
151
+ savedTokens,
152
+ };
153
+ }
154
+ scored.sort((a, b) => b.score - a.score);
155
+ let usedTokens = 0;
156
+ let trimmedCount = 0;
157
+ let savedTokens = 0;
158
+ const kept = [];
159
+ for (const s of scored) {
160
+ if (usedTokens + s.tokens <= availBudget) {
161
+ if (s.tokens > 1000) {
162
+ const summarized = summarizeMessage(s.msg);
163
+ const newTokens = estimateTokens(messageText(summarized));
164
+ savedTokens += s.tokens - newTokens;
165
+ usedTokens += newTokens;
166
+ kept.push({ msg: summarized, originalIndex: middle.indexOf(s.msg) });
167
+ }
168
+ else {
169
+ usedTokens += s.tokens;
170
+ kept.push({ msg: s.msg, originalIndex: middle.indexOf(s.msg) });
171
+ }
172
+ }
173
+ else {
174
+ trimmedCount++;
175
+ savedTokens += s.tokens;
176
+ }
177
+ }
178
+ kept.sort((a, b) => a.originalIndex - b.originalIndex);
179
+ const result = [...systemMsgs, ...kept.map(k => k.msg), ...tail];
180
+ return { messages: result, trimmedCount, savedTokens };
181
+ }