dsh-awesome-optimizer 3.1.0 → 3.5.0

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.
@@ -1,4 +1,5 @@
1
1
  import path from 'node:path';
2
+ import { maskCode, parameterEnd } from './code-lexical.js';
2
3
 
3
4
  const JS_TS_EXTS = new Set(['.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx', '.mts', '.cts']);
4
5
  const PY_EXTS = new Set(['.py', '.pyw']);
@@ -14,152 +15,146 @@ function truncateSig(sig, maxLen = 96) {
14
15
  return clean.length <= maxLen ? clean : `${clean.slice(0, maxLen - 3)}...`;
15
16
  }
16
17
 
17
- function scanJsTs(lines) {
18
- const entries = [];
19
- let inBlockComment = false;
20
-
21
- for (let i = 0; i < lines.length; i++) {
22
- const raw = lines[i];
23
- const trimmed = raw.trim();
24
- const lineNum = i + 1;
25
-
26
- if (inBlockComment) {
27
- if (trimmed.includes('*/')) inBlockComment = false;
28
- continue;
29
- }
30
- if (!trimmed || trimmed.startsWith('//')) continue;
31
- if (trimmed.startsWith('/*')) {
32
- if (!trimmed.includes('*/')) inBlockComment = true;
33
- continue;
34
- }
35
-
36
- const indent = raw.length - raw.trimStart().length;
37
-
38
- // Top-level declarations (indent <= 2)
39
- if (indent <= 2) {
40
- let m = trimmed.match(/^(export\s+(?:default\s+)?)?(async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)\s*(?:<[^>]*>)?\s*\(([^)]*)\)/);
41
- if (m) {
42
- const prefix = `${m[1] ? 'export ' : ''}${m[2] ? 'async ' : ''}function ${m[3]}(${m[4]})`;
43
- entries.push({ line: lineNum, text: truncateSig(prefix), nested: false });
44
- continue;
45
- }
46
-
47
- m = trimmed.match(/^(export\s+(?:default\s+)?)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)(?:\s+extends\s+([A-Za-z_$][\w$.]*))?/);
48
- if (m) {
49
- const prefix = `${m[1] ? 'export ' : ''}class ${m[2]}${m[3] ? ` extends ${m[3]}` : ''}`;
50
- entries.push({ line: lineNum, text: truncateSig(prefix), nested: false });
51
- continue;
52
- }
53
-
54
- m = trimmed.match(/^(export\s+)?(interface|type|enum)\s+([A-Za-z_$][\w$]*)/);
55
- if (m) {
56
- entries.push({ line: lineNum, text: truncateSig(`${m[1] ? 'export ' : ''}${m[2]} ${m[3]}`), nested: false });
57
- continue;
58
- }
18
+ const IDENTIFIER = '[\\p{ID_Start}_$][\\p{ID_Continue}$]*';
19
+ const jsFunction = new RegExp(`^(export\\s+(?:default\\s+)?)?(?:declare\\s+)?(async\\s+)?function\\s*\\*?\\s*(${IDENTIFIER})`, 'u');
20
+ const jsType = new RegExp(`^(export\\s+(?:default\\s+)?)?(?:declare\\s+)?(?:abstract\\s+)?(class|interface|type|enum)\\s+(${IDENTIFIER})`, 'u');
21
+ const jsVariable = new RegExp(`^(export\\s+)?(?:const|let|var)\\s+(${IDENTIFIER})(?:\\s*:[^=;]+)?\\s*=\\s*(async\\s+)?`, 'u');
22
+ const jsMethod = new RegExp(`^(?:(?:public|private|protected|static|readonly|override|abstract|get|set|async)\\s+)*\\*?\\s*(#?${IDENTIFIER})\\s*(?:<[^;{}]*>)?\\s*\\(`, 'u');
59
23
 
60
- m = trimmed.match(/^export\s+(?:const|let)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=\s*(async\s*)?(?:\(([^)]*)\)\s*=>|function\s*\(([^)]*)\))/);
61
- if (m) {
62
- const params = m[3] ?? m[4] ?? '';
63
- entries.push({ line: lineNum, text: truncateSig(`export const ${m[1]} = ${m[2] ? 'async ' : ''}(${params})`), nested: false });
64
- continue;
65
- }
66
-
67
- m = trimmed.match(/^export\s+(?:const|let)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=/);
68
- if (m) {
69
- entries.push({ line: lineNum, text: truncateSig(`export const ${m[1]}`), nested: false });
70
- continue;
24
+ function scanDeclarations(content, language) {
25
+ const masked = maskCode(content, language);
26
+ const entries = [];
27
+ let offset = 0;
28
+ let depth = 0;
29
+ let skipUntil = -1;
30
+ for (const [index, line] of masked.split('\n').entries()) {
31
+ const indent = line.length - line.trimStart().length;
32
+ const start = offset + indent;
33
+ const header = masked.slice(start, start + 8192);
34
+ const push = (text, nested = language === 'py' ? indent > 0 : depth > 0) => {
35
+ entries.push({ line: index + 1, text: truncateSig(text), nested });
36
+ };
37
+ const params = (prefix, from = 0) => {
38
+ let cursor = from;
39
+ while (/\s/.test(header[cursor] ?? '') && cursor < header.length) cursor++;
40
+ if (header[cursor] === '<') {
41
+ let depth = 1;
42
+ while (++cursor < header.length && depth > 0) {
43
+ if (header[cursor] === '<') depth++;
44
+ else if (header[cursor] === '>' && header[cursor - 1] !== '=') depth--;
45
+ }
71
46
  }
72
- }
73
-
74
- // Class / object method definitions at shallow indent (1 tab or 2-4 spaces)
75
- if (indent >= 1 && indent <= 4) {
76
- const m = trimmed.match(/^(?:(?:public|private|protected|static|readonly|override|get|set|async)\s+)*\*?\s*([A-Za-z_$][\w$]*)\s*(?:<[^>]*>)?\s*\(([^)]*)\)\s*(?::\s*[^{]+)?\{/);
77
- if (m && !CONTROL_WORDS.has(m[1])) {
78
- const isAsync = /\basync\s+/.test(trimmed.slice(0, trimmed.indexOf(m[1])));
79
- entries.push({
80
- line: lineNum,
81
- text: truncateSig(`${isAsync ? 'async ' : ''}${m[1]}(${m[2]})`),
82
- nested: true
83
- });
47
+ while (/\s/.test(header[cursor] ?? '') && cursor < header.length) cursor++;
48
+ const open = header[cursor] === '(' ? cursor : -1;
49
+ const end = parameterEnd(header, open);
50
+ if (end < 0) return null;
51
+ skipUntil = Math.max(skipUntil, start + end);
52
+ return { text: `${prefix}(${content.slice(start + open + 1, start + end).trim()})`, end };
53
+ };
54
+ if (start > skipUntil && line.trim()) {
55
+ let match;
56
+ const matches = regex => { match = header.match(regex); return match !== null; };
57
+ if (language === 'js') {
58
+ if (matches(jsFunction)) {
59
+ const signature = params(`${match[1] ? 'export ' : ''}${match[2] ? 'async ' : ''}function ${match[3]}`, match[0].length);
60
+ if (signature) push(signature.text);
61
+ } else if (matches(jsType)) {
62
+ const base = match[2] === 'class' ? header.slice(match[0].length).match(/^\s+extends\s+([\w$.]+)/)?.[1] : '';
63
+ push(`${match[1] ? 'export ' : ''}${match[2]} ${match[3]}${base ? ` extends ${base}` : ''}`);
64
+ } else if (matches(jsVariable)) {
65
+ const prefix = `${match[1] ? 'export ' : ''}const ${match[2]}`;
66
+ const rest = header.slice(match[0].length);
67
+ const arrow = rest.match(/^(?:<[^;{}]+>\s*)?\(/);
68
+ const expression = rest.match(/^function(?:\s+[\w$]+)?\s*\(/);
69
+ if (arrow || expression) {
70
+ const signature = params(`${prefix} = ${match[3] ? 'async ' : ''}`, match[0].length + (expression ? expression[0].length - 1 : 0));
71
+ if (signature && (expression || /^(?:\s*:\s*[^;=]+)?\s*=>/.test(header.slice(signature.end + 1, signature.end + 1024)))) push(signature.text);
72
+ else if (match[1]) push(prefix);
73
+ } else {
74
+ const single = rest.match(new RegExp(`^(${IDENTIFIER})\\s*=>`, 'u'));
75
+ if (single) push(`${prefix} = ${match[3] ? 'async ' : ''}(${single[1]})`);
76
+ else if (match[1]) push(prefix);
77
+ }
78
+ } else if (depth > 0 && matches(jsMethod) && !CONTROL_WORDS.has(match[1])) {
79
+ const signature = params(`${/\basync\s/.test(match[0]) ? 'async ' : ''}${match[1]}`, match[0].length - 1);
80
+ if (signature && /^\s*(?::[^;=]+)?\s*\{/.test(header.slice(signature.end + 1, signature.end + 1024))) push(signature.text, true);
81
+ }
82
+ } else if (language === 'py') {
83
+ if (matches(new RegExp(`^(async\\s+)?def\\s+(${IDENTIFIER})(?:\\[[^\\]]+\\])?\\s*\\(`, 'u'))) {
84
+ const signature = params(`${match[1] ? 'async ' : ''}def ${match[2]}`, match[0].length - 1);
85
+ if (signature) push(signature.text);
86
+ } else if (matches(new RegExp(`^class\\s+(${IDENTIFIER})(?:\\[[^\\]]+\\])?`, 'u'))) {
87
+ const rest = header.slice(match[0].length);
88
+ if (/^\s*\(/.test(rest)) {
89
+ const signature = params(`class ${match[1]}`, match[0].length);
90
+ if (signature) push(signature.text);
91
+ } else if (/^\s*:/.test(rest)) push(`class ${match[1]}`);
92
+ }
93
+ } else if (language === 'go') {
94
+ if (matches(/^func\s+(?:\([^)]*\)\s*)?([\p{ID_Start}_][\p{ID_Continue}_]*)(?:\[[^\]]+\])?\s*\(/u)) {
95
+ const open = match[0].lastIndexOf('(');
96
+ const end = parameterEnd(header, open);
97
+ if (end >= 0) { push(content.slice(start, start + end + 1)); skipUntil = start + end; }
98
+ } else if (matches(/^type\s+([\p{ID_Start}_][\p{ID_Continue}_]*)(?:\[[^\]]+\])?\s+(struct|interface)/u)) push(`type ${match[1]} ${match[2]}`);
99
+ } else if (language === 'rust') {
100
+ if (matches(/^(?:pub(?:\([^)]*\))?\s+)?(?:(?:async|unsafe|const|extern)\s+)*(fn\s+[\p{ID_Start}_][\p{ID_Continue}_]*(?:<[^;{}]*>)?\s*)\(/u)) {
101
+ const open = match[0].lastIndexOf('(');
102
+ const end = parameterEnd(header, open);
103
+ if (end >= 0) { push(content.slice(start, start + end + 1)); skipUntil = start + end; }
104
+ } else if (matches(/^(?:pub(?:\([^)]*\))?\s+)?(?:(?:unsafe|auto)\s+)?(?:struct|enum|trait)\s+[\p{ID_Start}_][\p{ID_Continue}_]*/u)) push(match[0]);
105
+ else if (matches(/^(?:unsafe\s+)?impl(?:<[^;{}]*>)?\s+[^{}]+/)) push(match[0]);
84
106
  }
85
107
  }
86
- }
87
- return entries;
88
- }
89
-
90
- function scanPython(lines) {
91
- const entries = [];
92
- for (let i = 0; i < lines.length; i++) {
93
- const raw = lines[i];
94
- const trimmed = raw.trim();
95
- if (!trimmed || trimmed.startsWith('#')) continue;
96
- const indent = raw.length - raw.trimStart().length;
97
- if (indent > 4) continue;
98
-
99
- let m = trimmed.match(/^class\s+([A-Za-z_]\w*)(?:\(([^)]*)\))?\s*:/);
100
- if (m) {
101
- entries.push({
102
- line: i + 1,
103
- text: truncateSig(`class ${m[1]}${m[2] ? `(${m[2]})` : ''}`),
104
- nested: indent > 0
105
- });
106
- continue;
107
- }
108
-
109
- m = trimmed.match(/^(async\s+)?def\s+([A-Za-z_]\w*)\s*\(([^)]*)\)/);
110
- if (m) {
111
- entries.push({
112
- line: i + 1,
113
- text: truncateSig(`${m[1] ? 'async ' : ''}def ${m[2]}(${m[3]})`),
114
- nested: indent > 0
115
- });
116
- }
117
- }
118
- return entries;
119
- }
120
-
121
- function scanGo(lines) {
122
- const entries = [];
123
- for (let i = 0; i < lines.length; i++) {
124
- const raw = lines[i];
125
- if (raw.startsWith('func ') || raw.startsWith('type ')) {
126
- const m = raw.match(/^(func\s+(?:\([^)]+\)\s*)?[A-Za-z_]\w*\([^)]*\)|type\s+[A-Za-z_]\w*\s+(?:struct|interface))/);
127
- if (m) entries.push({ line: i + 1, text: truncateSig(m[1]), nested: false });
108
+ if (language !== 'py') for (const char of line) {
109
+ if (char === '{') depth++;
110
+ else if (char === '}') depth = Math.max(0, depth - 1);
128
111
  }
112
+ offset += line.length + 1;
129
113
  }
130
114
  return entries;
131
115
  }
132
116
 
133
- function scanRust(lines) {
134
- const entries = [];
135
- for (let i = 0; i < lines.length; i++) {
136
- const trimmed = lines[i].trim();
137
- const indent = lines[i].length - lines[i].trimStart().length;
138
- if (indent > 4) continue;
139
- const m = trimmed.match(/^(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?(fn\s+[A-Za-z_]\w*\([^)]*\)|struct\s+[A-Za-z_]\w*|enum\s+[A-Za-z_]\w*|trait\s+[A-Za-z_]\w*|impl(?:<[^>]*>)?\s+[^{]+)/);
140
- if (m) {
141
- entries.push({ line: i + 1, text: truncateSig(m[0]), nested: indent > 0 });
142
- }
143
- }
144
- return entries;
117
+ /** Check language support without reading file contents. */
118
+ export function supportsCodeOutline(filePath) {
119
+ const ext = path.extname(filePath).toLowerCase();
120
+ return !ext || JS_TS_EXTS.has(ext) || PY_EXTS.has(ext) || GO_EXTS.has(ext) || RS_EXTS.has(ext);
145
121
  }
146
122
 
147
- /**
148
- * Extract lightweight symbol entries with 1-based line numbers for supported source languages.
149
- */
123
+ /** Extract lightweight symbols with 1-based line numbers. */
150
124
  export function extractCodeOutline(content, { filePath = '', startLine = 1, endLine = Infinity } = {}) {
151
125
  if (typeof content !== 'string' || !content) return [];
152
126
  const ext = path.extname(filePath).toLowerCase();
153
- const lines = content.replace(/\r\n/g, '\n').split('\n');
127
+ const language = PY_EXTS.has(ext) ? 'py' : GO_EXTS.has(ext) ? 'go' : RS_EXTS.has(ext) ? 'rust' : JS_TS_EXTS.has(ext) || !ext ? 'js' : null;
128
+ if (!language) return [];
129
+ return scanDeclarations(content, language).filter(item => item.line >= startLine && item.line <= endLine);
130
+ }
154
131
 
155
- let raw;
156
- if (PY_EXTS.has(ext)) raw = scanPython(lines);
157
- else if (GO_EXTS.has(ext)) raw = scanGo(lines);
158
- else if (RS_EXTS.has(ext)) raw = scanRust(lines);
159
- else if (JS_TS_EXTS.has(ext) || !ext) raw = scanJsTs(lines);
160
- else return [];
132
+ /** Cacheable symbols are independent of the caller's current read offset. */
133
+ export function buildCodeOutline(content, filePath) {
134
+ return { entries: extractCodeOutline(content, { filePath }), totalLines: content.split('\n').length };
135
+ }
161
136
 
162
- return raw.filter(item => item.line >= startLine && item.line <= endLine);
137
+ /** Prefer top-level APIs and sample across the remaining file instead of its first page. */
138
+ export function formatOutlineEntries(entries, { startLine = 1, endLine = Infinity, maxEntries = 20, maxBytes = 1536 } = {}) {
139
+ const all = entries.filter(item => item.line >= startLine && item.line <= endLine);
140
+ const groups = [[], [], []];
141
+ for (const entry of all) groups[entry.nested ? 2 : /^(?:export|pub\b)/.test(entry.text) ? 0 : 1].push(entry);
142
+ for (let count = Math.min(all.length, Math.max(0, Math.floor(maxEntries))); count > 0; count--) {
143
+ const shown = [];
144
+ for (const group of groups) {
145
+ const slots = Math.min(group.length, count - shown.length);
146
+ for (let i = 0; i < slots; i++) {
147
+ const index = slots === 1 ? 0 : Math.round(i * (group.length - 1) / (slots - 1));
148
+ shown.push(group[index]);
149
+ }
150
+ }
151
+ shown.sort((a, b) => a.line - b.line);
152
+ const lines = shown.map(item => `L${item.line}: ${item.nested ? ' ' : ''}${item.text}`);
153
+ if (all.length > count) lines.push(`... (+${all.length - count} more symbols)`);
154
+ const output = lines.join('\n');
155
+ if (Buffer.byteLength(output, 'utf8') <= maxBytes) return output;
156
+ }
157
+ return '';
163
158
  }
164
159
 
165
160
  /**
@@ -172,27 +167,5 @@ export function formatCodeOutline(content, {
172
167
  maxEntries = 20,
173
168
  maxBytes = 1536
174
169
  } = {}) {
175
- const all = extractCodeOutline(content, { filePath, startLine, endLine });
176
- if (all.length === 0) return '';
177
-
178
- const shown = all.slice(0, maxEntries);
179
- const lines = shown.map(item => `L${item.line}: ${item.nested ? ' ' : ''}${item.text}`);
180
- if (all.length > shown.length) {
181
- lines.push(`... (+${all.length - shown.length} more symbols)`);
182
- }
183
-
184
- let output = lines.join('\n');
185
- if (Buffer.byteLength(output, 'utf8') > maxBytes) {
186
- const trimmed = [];
187
- let bytes = 0;
188
- for (const line of lines) {
189
- const b = Buffer.byteLength(line, 'utf8') + 1;
190
- if (bytes + b > maxBytes - 48) break;
191
- trimmed.push(line);
192
- bytes += b;
193
- }
194
- trimmed.push('... (outline truncated)');
195
- output = trimmed.join('\n');
196
- }
197
- return output;
170
+ return formatOutlineEntries(extractCodeOutline(content, { filePath }), { startLine, endLine, maxEntries, maxBytes });
198
171
  }
@@ -9,6 +9,12 @@ export interface CompositeToolsOptions {
9
9
  defaultContextLines?: number;
10
10
  maxFiles?: number;
11
11
  maxWindows?: number;
12
+ /** Maximum lines requested in one merged read window. Default: 120. */
13
+ maxWindowLines?: number;
14
+ /** Maximum total lines requested per find_and_read call. Default: 600. */
15
+ maxReadLines?: number;
16
+ /** Maximum simultaneous reads in each budget-aware batch. Default: 3. */
17
+ readConcurrency?: number;
12
18
  maxBatchEdits?: number;
13
19
  maxLineChars?: number;
14
20
  maxTotalBytes?: number;
@@ -19,6 +25,8 @@ export interface CompositeToolsStats {
19
25
  findExecuted: number;
20
26
  findPatternsSearched: number;
21
27
  findWindowsRead: number;
28
+ findLinesRequested: number;
29
+ findReadBatches: number;
22
30
  findFilesMatched: number;
23
31
  editBatchesExecuted: number;
24
32
  editsRequested: number;