@nxuss/lemma 0.4.11 → 0.4.12
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 +27 -2
- package/dist/cjs/cli/lemma-proxy.d.ts.map +1 -1
- package/dist/cjs/cli/lemma-proxy.js +85 -10
- package/dist/cjs/cli/lemma-proxy.js.map +1 -1
- package/dist/cjs/utils/ContextSqueezer.d.ts +1 -0
- package/dist/cjs/utils/ContextSqueezer.d.ts.map +1 -1
- package/dist/cjs/utils/ContextSqueezer.js +80 -40
- package/dist/cjs/utils/ContextSqueezer.js.map +1 -1
- package/dist/esm/cli/lemma-proxy.d.ts.map +1 -1
- package/dist/esm/cli/lemma-proxy.js +85 -10
- package/dist/esm/cli/lemma-proxy.js.map +1 -1
- package/dist/esm/utils/ContextSqueezer.d.ts +1 -0
- package/dist/esm/utils/ContextSqueezer.d.ts.map +1 -1
- package/dist/esm/utils/ContextSqueezer.js +79 -40
- package/dist/esm/utils/ContextSqueezer.js.map +1 -1
- package/package.json +1 -1
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.stripComments = stripComments;
|
|
11
11
|
exports.compactWhitespace = compactWhitespace;
|
|
12
|
+
exports.detectFunctionDeclaration = detectFunctionDeclaration;
|
|
12
13
|
exports.treeShakeCode = treeShakeCode;
|
|
13
14
|
exports.squeezeCode = squeezeCode;
|
|
14
15
|
exports.squeezePrompt = squeezePrompt;
|
|
@@ -57,6 +58,29 @@ function compactWhitespace(code) {
|
|
|
57
58
|
.join('\n')
|
|
58
59
|
.replace(/[ \t]+/g, ' '); // Collapse multiple spaces/tabs into a single space
|
|
59
60
|
}
|
|
61
|
+
function detectFunctionDeclaration(context) {
|
|
62
|
+
// Normalize whitespaces
|
|
63
|
+
const normalized = context.replace(/\s+/g, ' ').trim();
|
|
64
|
+
// 1. Standard / Exported / Default / Async functions
|
|
65
|
+
const funcRegex = /(?:export\s+(?:default\s+)?)?(?:async\s+)?function\s+([a-zA-Z0-9_$]+)\s*\([\s\S]*?\)\s*(?::\s*[^{]+)?\s*\{\s*$/i;
|
|
66
|
+
const funcMatch = normalized.match(funcRegex);
|
|
67
|
+
if (funcMatch && funcMatch[1]) {
|
|
68
|
+
return funcMatch[1];
|
|
69
|
+
}
|
|
70
|
+
// 2. Arrow functions assigned to variables
|
|
71
|
+
const arrowRegex = /(?:const|let|var|export\s+(?:const|let|var))\s+([a-zA-Z0-9_$]+)\s*=\s*(?:async\s*)?(?:\([\s\S]*?\)|[a-zA-Z0-9_$]+)\s*=>\s*\{\s*$/i;
|
|
72
|
+
const arrowMatch = normalized.match(arrowRegex);
|
|
73
|
+
if (arrowMatch && arrowMatch[1]) {
|
|
74
|
+
return arrowMatch[1];
|
|
75
|
+
}
|
|
76
|
+
// 3. Class Methods (with or without modifiers)
|
|
77
|
+
const methodRegex = /(?:public|private|protected|static|async)?\s*(?:async\s+)?(?!if|for|while|switch|catch)([a-zA-Z0-9_$]+)\s*\([\s\S]*?\)\s*(?::\s*[^{]+)?\s*\{\s*$/i;
|
|
78
|
+
const methodMatch = normalized.match(methodRegex);
|
|
79
|
+
if (methodMatch && methodMatch[1]) {
|
|
80
|
+
return methodMatch[1];
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
60
84
|
/**
|
|
61
85
|
* Compresses very long TypeScript/JavaScript files by keeping only the function/class signatures
|
|
62
86
|
* and removing function body implementation details for functions that are not mentioned in the query!
|
|
@@ -73,6 +97,8 @@ function treeShakeCode(code, query) {
|
|
|
73
97
|
let inFunctionBody = false;
|
|
74
98
|
let bodyBracesCount = 0;
|
|
75
99
|
let skippedLinesCount = 0;
|
|
100
|
+
// Sliding window to reconstruct multiline signatures (up to 5 lines)
|
|
101
|
+
const slidingWindow = [];
|
|
76
102
|
for (let i = 0; i < lines.length; i++) {
|
|
77
103
|
const line = lines[i];
|
|
78
104
|
const trimmed = line.trim();
|
|
@@ -84,36 +110,34 @@ function treeShakeCode(code, query) {
|
|
|
84
110
|
if (bodyBracesCount <= 0) {
|
|
85
111
|
inFunctionBody = false;
|
|
86
112
|
resultLines.push(`/* ... [Lemma AST Tree-Shaker: body omitted to save ${skippedLinesCount + 1} lines] ... */ }`);
|
|
113
|
+
slidingWindow.length = 0; // Clear window
|
|
87
114
|
}
|
|
88
115
|
else {
|
|
89
116
|
skippedLinesCount++;
|
|
90
117
|
}
|
|
91
118
|
continue;
|
|
92
119
|
}
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
120
|
+
// Add current line to sliding window
|
|
121
|
+
slidingWindow.push(line);
|
|
122
|
+
if (slidingWindow.length > 5) {
|
|
123
|
+
slidingWindow.shift();
|
|
124
|
+
}
|
|
125
|
+
// Check if the current line ends with a '{' or has a '{' (could start a function)
|
|
126
|
+
if (trimmed.includes('{')) {
|
|
127
|
+
// Reconstruct signature context from sliding window
|
|
128
|
+
const signatureContext = slidingWindow.join('\n');
|
|
129
|
+
const funcName = detectFunctionDeclaration(signatureContext);
|
|
130
|
+
if (funcName && funcName.length > 2 && funcName !== 'constructor') {
|
|
131
|
+
slidingWindow.length = 0; // Clear window
|
|
132
|
+
// If a function name is found, and it is NOT mentioned in the user's query
|
|
133
|
+
if (query && !query.toLowerCase().includes(funcName.toLowerCase())) {
|
|
134
|
+
inFunctionBody = true;
|
|
135
|
+
bodyBracesCount = 1;
|
|
136
|
+
skippedLinesCount = 0;
|
|
137
|
+
resultLines.push(line);
|
|
138
|
+
continue;
|
|
107
139
|
}
|
|
108
140
|
}
|
|
109
|
-
// If a function name is found, and it is NOT mentioned in the user's query, and it is not 'constructor'
|
|
110
|
-
if (funcName && funcName.length > 2 && funcName !== 'constructor' && query && !query.toLowerCase().includes(funcName.toLowerCase())) {
|
|
111
|
-
inFunctionBody = true;
|
|
112
|
-
bodyBracesCount = 1;
|
|
113
|
-
skippedLinesCount = 0;
|
|
114
|
-
resultLines.push(line);
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
141
|
}
|
|
118
142
|
resultLines.push(line);
|
|
119
143
|
}
|
|
@@ -132,11 +156,14 @@ function squeezeCode(code) {
|
|
|
132
156
|
function squeezePrompt(prompt, query = '') {
|
|
133
157
|
const codeBlockRegex = /(```[a-zA-Z0-9+#-]*\n)([\s\S]*?)(```)/g;
|
|
134
158
|
const originalSize = prompt.length;
|
|
159
|
+
// Clean the query by stripping all code blocks. This prevents false positive matches
|
|
160
|
+
// on function names that are only present inside attached codebase files.
|
|
161
|
+
const cleanQuery = query ? query.replace(codeBlockRegex, '').trim() : '';
|
|
135
162
|
const squeezed = prompt.replace(codeBlockRegex, (match, prefix, code, suffix) => {
|
|
136
163
|
try {
|
|
137
164
|
const language = prefix.match(/```([a-zA-Z0-9+#-]+)/)?.[1] || '';
|
|
138
|
-
if (['typescript', 'javascript', 'ts', 'js', 'tsx', 'jsx'].includes(language.toLowerCase()) &&
|
|
139
|
-
const squeezedCode = treeShakeCode(code,
|
|
165
|
+
if (['typescript', 'javascript', 'ts', 'js', 'tsx', 'jsx'].includes(language.toLowerCase()) && cleanQuery) {
|
|
166
|
+
const squeezedCode = treeShakeCode(code, cleanQuery);
|
|
140
167
|
return `${prefix}${squeezedCode}\n${suffix}`;
|
|
141
168
|
}
|
|
142
169
|
else {
|
|
@@ -174,25 +201,38 @@ function pruneHistoryMessages(messages) {
|
|
|
174
201
|
if (index >= messages.length - 2 || msg.role === 'system') {
|
|
175
202
|
return msg;
|
|
176
203
|
}
|
|
177
|
-
if (typeof msg.content !== 'string') {
|
|
178
|
-
return msg;
|
|
179
|
-
}
|
|
180
|
-
// Check if the message contains massive codeblocks
|
|
181
204
|
const codeBlockRegex = /(```[a-zA-Z0-9+#-]*\n)([\s\S]*?)(```)/g;
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
205
|
+
const processText = (text) => {
|
|
206
|
+
const matches = text.match(codeBlockRegex);
|
|
207
|
+
if (matches && matches.length > 0) {
|
|
208
|
+
return text.replace(codeBlockRegex, (match, prefix, code, suffix) => {
|
|
209
|
+
if (code.length > 120) {
|
|
210
|
+
const lines = code.trim().split('\n');
|
|
211
|
+
const summary = `// [Lemma History Squeezer: Compacted ${lines.length} lines of code here to protect context limit]`;
|
|
212
|
+
tokensSavedEstimate += (code.length - summary.length);
|
|
213
|
+
return `${prefix}${summary}\n${suffix}`;
|
|
214
|
+
}
|
|
215
|
+
return match;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return text;
|
|
219
|
+
};
|
|
220
|
+
if (typeof msg.content === 'string') {
|
|
221
|
+
return { ...msg, content: processText(msg.content) };
|
|
222
|
+
}
|
|
223
|
+
else if (Array.isArray(msg.content)) {
|
|
224
|
+
const newContent = msg.content.map((part) => {
|
|
225
|
+
if (part && typeof part === 'object') {
|
|
226
|
+
if (part.type === 'text' && typeof part.text === 'string') {
|
|
227
|
+
return { ...part, text: processText(part.text) };
|
|
228
|
+
}
|
|
229
|
+
if (typeof part.text === 'string') {
|
|
230
|
+
return { ...part, text: processText(part.text) };
|
|
231
|
+
}
|
|
192
232
|
}
|
|
193
|
-
return
|
|
233
|
+
return part;
|
|
194
234
|
});
|
|
195
|
-
return { ...msg, content };
|
|
235
|
+
return { ...msg, content: newContent };
|
|
196
236
|
}
|
|
197
237
|
return msg;
|
|
198
238
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContextSqueezer.js","sourceRoot":"","sources":["../../../src/utils/ContextSqueezer.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAYH,sCAgCC;AAKD,8CAOC;AAMD,
|
|
1
|
+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,EAAE,CAAC;wBAClC,OAAO,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;oBACnD,CAAC;gBACH,CAAC;gBACD,OAAO,IAAI,CAAC;YACd,CAAC,CAAC,CAAC;YACH,OAAO,EAAE,GAAG,GAAG,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;QACzC,CAAC;QAED,OAAO,GAAG,CAAC;IACb,CAAC,CAAC,CAAC;IAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,mBAAmB,EAAE,CAAC;AACnD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lemma-proxy.d.ts","sourceRoot":"","sources":["../../../src/cli/lemma-proxy.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"lemma-proxy.d.ts","sourceRoot":"","sources":["../../../src/cli/lemma-proxy.ts"],"names":[],"mappings":";AAmvCA,wBAAsB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CA6KhL"}
|
|
@@ -332,10 +332,37 @@ async function semanticSet(provider, prompt, data) {
|
|
|
332
332
|
catch (e) { }
|
|
333
333
|
}
|
|
334
334
|
// ── Prompt extraction ──────────────────────────────────────────────────────────
|
|
335
|
+
function extractContentText(content) {
|
|
336
|
+
if (typeof content === 'string') {
|
|
337
|
+
return content;
|
|
338
|
+
}
|
|
339
|
+
if (Array.isArray(content)) {
|
|
340
|
+
return content
|
|
341
|
+
.map(part => {
|
|
342
|
+
if (part && typeof part === 'object') {
|
|
343
|
+
if (part.type === 'text' && typeof part.text === 'string') {
|
|
344
|
+
return part.text;
|
|
345
|
+
}
|
|
346
|
+
if (typeof part.text === 'string') {
|
|
347
|
+
return part.text;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return '';
|
|
351
|
+
})
|
|
352
|
+
.filter(Boolean)
|
|
353
|
+
.join('\n');
|
|
354
|
+
}
|
|
355
|
+
if (content && typeof content === 'object') {
|
|
356
|
+
if (typeof content.text === 'string') {
|
|
357
|
+
return content.text;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return '';
|
|
361
|
+
}
|
|
335
362
|
function extractPrompt(body, provider) {
|
|
336
363
|
const msgs = body.messages || [];
|
|
337
364
|
if (provider === 'openai' || provider === 'anthropic') {
|
|
338
|
-
return msgs.map((m) => (
|
|
365
|
+
return msgs.map((m) => extractContentText(m.content)).join('\n');
|
|
339
366
|
}
|
|
340
367
|
if (provider === 'gemini') {
|
|
341
368
|
const contents = body.contents || [];
|
|
@@ -343,6 +370,35 @@ function extractPrompt(body, provider) {
|
|
|
343
370
|
}
|
|
344
371
|
return null;
|
|
345
372
|
}
|
|
373
|
+
function injectSqueezedContent(message, squeezedPrompt) {
|
|
374
|
+
if (typeof message.content === 'string') {
|
|
375
|
+
message.content = squeezedPrompt;
|
|
376
|
+
}
|
|
377
|
+
else if (Array.isArray(message.content)) {
|
|
378
|
+
let updated = false;
|
|
379
|
+
for (const part of message.content) {
|
|
380
|
+
if (part && typeof part === 'object' && part.type === 'text' && typeof part.text === 'string') {
|
|
381
|
+
part.text = squeezedPrompt;
|
|
382
|
+
updated = true;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (!updated) {
|
|
387
|
+
message.content.push({ type: 'text', text: squeezedPrompt });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
else if (message.content && typeof message.content === 'object') {
|
|
391
|
+
if (typeof message.content.text === 'string') {
|
|
392
|
+
message.content.text = squeezedPrompt;
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
message.content = squeezedPrompt;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
message.content = squeezedPrompt;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
346
402
|
// ── SSE helpers ────────────────────────────────────────────────────────────────
|
|
347
403
|
function setSseHeaders(res, fromCache, similarity, tier) {
|
|
348
404
|
res.setHeader('Content-Type', 'text/event-stream');
|
|
@@ -612,7 +668,7 @@ class LemmaServer {
|
|
|
612
668
|
}
|
|
613
669
|
if (provider === 'openai' || provider === 'anthropic') {
|
|
614
670
|
if (req.body.messages && req.body.messages.length > 0) {
|
|
615
|
-
req.body.messages[req.body.messages.length - 1]
|
|
671
|
+
injectSqueezedContent(req.body.messages[req.body.messages.length - 1], squeezedPrompt);
|
|
616
672
|
// Apply conversational history pruning to save tokens on historical messages!
|
|
617
673
|
const pruneResult = pruneHistoryMessages(req.body.messages);
|
|
618
674
|
req.body.messages = pruneResult.messages;
|
|
@@ -839,15 +895,34 @@ program.command('mcp')
|
|
|
839
895
|
.description('Start the Lemma MCP Server (Model Context Protocol)')
|
|
840
896
|
.action(() => {
|
|
841
897
|
const { spawn } = require('child_process');
|
|
842
|
-
|
|
843
|
-
const
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
898
|
+
// Robust search candidates for both development (TS source) and compiled (JS production) environments.
|
|
899
|
+
const mcpCandidates = [
|
|
900
|
+
path.join(__dirname, '..', 'mcp', 'index.js'), // compiled relative (prod /dist/cjs/cli -> dist/cjs/mcp)
|
|
901
|
+
path.join(__dirname, '..', 'mcp', 'index.ts'), // dev relative (dev /src/cli -> src/mcp)
|
|
902
|
+
path.join(__dirname, '..', '..', '..', 'src', 'mcp', 'index.ts'), // fallback absolute relative
|
|
903
|
+
path.join(process.cwd(), 'src', 'mcp', 'index.ts'),
|
|
904
|
+
path.join(process.cwd(), 'dist', 'cjs', 'mcp', 'index.js'),
|
|
905
|
+
];
|
|
906
|
+
let targetPath = '';
|
|
907
|
+
for (const cand of mcpCandidates) {
|
|
908
|
+
if (fs.existsSync(cand)) {
|
|
909
|
+
targetPath = cand;
|
|
910
|
+
break;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
if (!targetPath) {
|
|
914
|
+
console.error('❌ Error: Could not find Lemma MCP Server script. Please rebuild or reinstall the package.');
|
|
915
|
+
process.exit(1);
|
|
916
|
+
}
|
|
917
|
+
let cmd = 'node';
|
|
918
|
+
let args = [];
|
|
919
|
+
if (targetPath.endsWith('.ts')) {
|
|
920
|
+
cmd = 'npx';
|
|
921
|
+
args = ['tsx', targetPath];
|
|
922
|
+
}
|
|
923
|
+
else {
|
|
849
924
|
cmd = 'node';
|
|
850
|
-
args = [
|
|
925
|
+
args = [targetPath];
|
|
851
926
|
}
|
|
852
927
|
const mcp = spawn(cmd, args, { stdio: 'inherit' });
|
|
853
928
|
mcp.on('exit', (code) => process.exit(code || 0));
|