@causyne/agent 1.20.26 → 1.20.28
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/agentLoop.js +52 -4
- package/src/agentLoop.ts +48 -4
- package/src/browser/causyneBrowser.js +1 -1
- package/src/browser/causyneBrowser.ts +1 -1
- package/src/browser/searchProviders.js +1 -1
- package/src/browser/searchProviders.ts +1 -1
- package/src/decideTools.js +9 -1
- package/src/decideTools.ts +7 -1
- package/src/dynamicCatalogue.js +3 -1
- package/src/dynamicCatalogue.ts +3 -1
- package/src/launchIntent.js +1 -1
- package/src/launchIntent.ts +1 -1
- package/src/mcp/mcpRegistry.js +6 -2
- package/src/mcp/mcpRegistry.ts +5 -2
- package/src/memory/actrRanker.js +78 -0
- package/src/memory/actrRanker.ts +111 -0
- package/src/memory/contextBudget.js +131 -0
- package/src/memory/contextBudget.ts +155 -0
- package/src/memory/episodicStore.js +651 -0
- package/src/memory/episodicStore.ts +787 -0
- package/src/memory/episodicTypes.js +8 -0
- package/src/memory/episodicTypes.ts +124 -0
- package/src/memory/nearDuplicate.js +179 -0
- package/src/memory/nearDuplicate.ts +232 -0
- package/src/memory/symbolFolder.js +301 -0
- package/src/memory/symbolFolder.ts +327 -0
- package/src/toolPacks.js +1 -0
- package/src/toolPacks.ts +1 -0
- package/src/tools/desktopOpen.js +9 -2
- package/src/tools/desktopOpen.ts +8 -2
- package/src/tools/memoryRecall.js +211 -0
- package/src/tools/memoryRecall.ts +251 -0
- package/src/tools/nativeEngine.js +16 -2
- package/src/tools/nativeEngine.ts +16 -2
- package/src/tools/registry.js +2 -1
- package/src/tools/subagentOrchestrator.js +3 -2
- package/src/tools/toolSelfRepair.js +4 -1
- package/src/tools/toolSelfRepair.ts +4 -1
- package/src/toolsList.js +1 -0
- package/src/toolsList.ts +1 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/memory/symbolFolder.ts
|
|
3
|
+
* Structural Code Symbol Folding (`foldSourceFileOutline`) for Causyne Agent.
|
|
4
|
+
*
|
|
5
|
+
* Extracts top-level and class-member symbol signatures (including multi-line
|
|
6
|
+
* function and method signatures), JSDoc summaries, and exact `[lineStart, lineEnd]`
|
|
7
|
+
* line spans while folding implementation bodies. Allows the agent to inspect a
|
|
8
|
+
* 1,500-line file in ~120 tokens and then read only the exact `[lineStart, lineEnd]`
|
|
9
|
+
* slice with `fs_read`.
|
|
10
|
+
*
|
|
11
|
+
* Authorship: Strict TypeScript (Causyne Agent).
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import os from 'node:os';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
const LANGUAGE_BY_EXT = {
|
|
17
|
+
'.ts': 'typescript',
|
|
18
|
+
'.tsx': 'tsx',
|
|
19
|
+
'.js': 'javascript',
|
|
20
|
+
'.jsx': 'jsx',
|
|
21
|
+
'.mjs': 'javascript',
|
|
22
|
+
'.cjs': 'javascript',
|
|
23
|
+
'.py': 'python',
|
|
24
|
+
'.go': 'go',
|
|
25
|
+
'.rs': 'rust',
|
|
26
|
+
'.kt': 'kotlin',
|
|
27
|
+
'.java': 'java'
|
|
28
|
+
};
|
|
29
|
+
const TS_JS_PATTERNS = [
|
|
30
|
+
{
|
|
31
|
+
kind: 'function',
|
|
32
|
+
regex: /^(\s*)(export\s+)?(default\s+)?(async\s+)?function\s*\*?\s*([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:<[^>]*>)?\s*\(/,
|
|
33
|
+
nameGroup: 5
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
kind: 'class',
|
|
37
|
+
regex: /^(\s*)(export\s+)?(default\s+)?(abstract\s+)?class\s+([A-Za-z_$][A-Za-z0-9_$]*)(?:\s+[^{]+)?/,
|
|
38
|
+
nameGroup: 5
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
kind: 'interface',
|
|
42
|
+
regex: /^(\s*)(export\s+)?interface\s+([A-Za-z_$][A-Za-z0-9_$]*)(?:\s+[^{]+)?/,
|
|
43
|
+
nameGroup: 3
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
kind: 'type',
|
|
47
|
+
regex: /^(\s*)(export\s+)?type\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:<[^>]*>)?\s*=/,
|
|
48
|
+
nameGroup: 3
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
kind: 'enum',
|
|
52
|
+
regex: /^(\s*)(export\s+)?(const\s+)?enum\s+([A-Za-z_$][A-Za-z0-9_$]*)/,
|
|
53
|
+
nameGroup: 4
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
kind: 'const',
|
|
57
|
+
regex: /^(\s*)(export\s+)(?:const|let)\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*(?::\s*[^=]+)?\s*=/,
|
|
58
|
+
nameGroup: 3
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
kind: 'method',
|
|
62
|
+
regex: /^(\s{2,4})(?:(?:public|private|protected|static|readonly|override|async)\s+)*([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:<[^>]*>)?\s*\(/,
|
|
63
|
+
nameGroup: 2
|
|
64
|
+
}
|
|
65
|
+
];
|
|
66
|
+
const PYTHON_PATTERNS = [
|
|
67
|
+
{
|
|
68
|
+
kind: 'class',
|
|
69
|
+
regex: /^(\s*)class\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?:\([^)]*\))?\s*:/,
|
|
70
|
+
nameGroup: 2
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
kind: 'function',
|
|
74
|
+
regex: /^(\s*)(?:async\s+)?def\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(/,
|
|
75
|
+
nameGroup: 2
|
|
76
|
+
}
|
|
77
|
+
];
|
|
78
|
+
const GO_RUST_PATTERNS = [
|
|
79
|
+
{
|
|
80
|
+
kind: 'function',
|
|
81
|
+
regex: /^(\s*)(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?(?:func|fn)\s+(?:\([^)]+\)\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*(?:<[^>]*>)?\s*\(/,
|
|
82
|
+
nameGroup: 2
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
kind: 'class',
|
|
86
|
+
regex: /^(\s*)(?:pub(?:\([^)]*\))?\s+)?(?:type\s+([A-Za-z_][A-Za-z0-9_]*)\s+struct|struct\s+([A-Za-z_][A-Za-z0-9_]*))/,
|
|
87
|
+
nameGroup: 2
|
|
88
|
+
}
|
|
89
|
+
];
|
|
90
|
+
const RESERVED_METHOD_NAMES = new Set([
|
|
91
|
+
'if', 'for', 'while', 'switch', 'catch', 'return', 'try', 'else', 'do', 'super', 'import', 'require'
|
|
92
|
+
]);
|
|
93
|
+
function extractPrecedingDocComment(lines, lineIndex) {
|
|
94
|
+
let idx = lineIndex - 1;
|
|
95
|
+
while (idx >= 0 && (lines[idx] ?? '').trim() === '') {
|
|
96
|
+
idx--;
|
|
97
|
+
}
|
|
98
|
+
if (idx < 0)
|
|
99
|
+
return undefined;
|
|
100
|
+
const line = (lines[idx] ?? '').trim();
|
|
101
|
+
if (line.endsWith('*/')) {
|
|
102
|
+
const docLines = [];
|
|
103
|
+
while (idx >= 0) {
|
|
104
|
+
const current = (lines[idx] ?? '').trim();
|
|
105
|
+
const cleaned = current
|
|
106
|
+
.replace(/^\/\*\*?/, '')
|
|
107
|
+
.replace(/\*\/$/, '')
|
|
108
|
+
.replace(/^\*\s?/, '')
|
|
109
|
+
.trim();
|
|
110
|
+
if (cleaned && !cleaned.startsWith('@')) {
|
|
111
|
+
docLines.unshift(cleaned);
|
|
112
|
+
}
|
|
113
|
+
if (current.startsWith('/*'))
|
|
114
|
+
break;
|
|
115
|
+
idx--;
|
|
116
|
+
}
|
|
117
|
+
const summary = docLines.join(' ').trim();
|
|
118
|
+
return summary ? summary.slice(0, 110) : undefined;
|
|
119
|
+
}
|
|
120
|
+
if (line.startsWith('//') || line.startsWith('#')) {
|
|
121
|
+
const cleaned = line.replace(/^(?:\/\/+|#+)\s*/, '').trim();
|
|
122
|
+
return cleaned ? cleaned.slice(0, 110) : undefined;
|
|
123
|
+
}
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Collapse a single-line or multi-line declaration header into a concise one-line signature.
|
|
128
|
+
*/
|
|
129
|
+
function extractCollapsedSignature(lines, startIndex, kind) {
|
|
130
|
+
const firstLine = (lines[startIndex] ?? '').trim();
|
|
131
|
+
if (kind !== 'function' && kind !== 'method') {
|
|
132
|
+
return firstLine.replace(/\s*\{$/, '').slice(0, 100);
|
|
133
|
+
}
|
|
134
|
+
let combined = '';
|
|
135
|
+
let parenDepth = 0;
|
|
136
|
+
let seenOpenParen = false;
|
|
137
|
+
const maxLookahead = Math.min(lines.length, startIndex + 12);
|
|
138
|
+
for (let i = startIndex; i < maxLookahead; i++) {
|
|
139
|
+
const part = (lines[i] ?? '').trim();
|
|
140
|
+
combined += (combined ? ' ' : '') + part;
|
|
141
|
+
for (let c = 0; c < part.length; c++) {
|
|
142
|
+
const ch = part[c];
|
|
143
|
+
if (ch === '(') {
|
|
144
|
+
parenDepth += 1;
|
|
145
|
+
seenOpenParen = true;
|
|
146
|
+
}
|
|
147
|
+
else if (ch === ')') {
|
|
148
|
+
parenDepth -= 1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (seenOpenParen && parenDepth <= 0) {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return combined
|
|
156
|
+
.replace(/\s+/g, ' ')
|
|
157
|
+
.replace(/\s*\{[^)]*$/, '')
|
|
158
|
+
.trim()
|
|
159
|
+
.slice(0, 110);
|
|
160
|
+
}
|
|
161
|
+
function findBraceBlockEnd(lines, startIndex) {
|
|
162
|
+
let depth = 0;
|
|
163
|
+
let opened = false;
|
|
164
|
+
for (let i = startIndex; i < lines.length; i++) {
|
|
165
|
+
const line = lines[i] ?? '';
|
|
166
|
+
for (let c = 0; c < line.length; c++) {
|
|
167
|
+
const ch = line[c];
|
|
168
|
+
if (ch === '{') {
|
|
169
|
+
depth += 1;
|
|
170
|
+
opened = true;
|
|
171
|
+
}
|
|
172
|
+
else if (ch === '}') {
|
|
173
|
+
depth -= 1;
|
|
174
|
+
if (opened && depth <= 0) {
|
|
175
|
+
return i + 1;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (!opened && line.trim().endsWith(';')) {
|
|
180
|
+
return i + 1;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return Math.min(lines.length, startIndex + 1);
|
|
184
|
+
}
|
|
185
|
+
function findIndentBlockEnd(lines, startIndex) {
|
|
186
|
+
const startLine = lines[startIndex] ?? '';
|
|
187
|
+
const baseIndent = (startLine.match(/^\s*/)?.[0] ?? '').length;
|
|
188
|
+
let lastLine = startIndex + 1;
|
|
189
|
+
for (let i = startIndex + 1; i < lines.length; i++) {
|
|
190
|
+
const line = lines[i] ?? '';
|
|
191
|
+
const trimmed = line.trim();
|
|
192
|
+
if (!trimmed || trimmed.startsWith('#'))
|
|
193
|
+
continue;
|
|
194
|
+
const indent = (line.match(/^\s*/)?.[0] ?? '').length;
|
|
195
|
+
if (indent <= baseIndent) {
|
|
196
|
+
return lastLine;
|
|
197
|
+
}
|
|
198
|
+
lastLine = i + 1;
|
|
199
|
+
}
|
|
200
|
+
return lastLine;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Fold a source code string into a compact structural symbol outline.
|
|
204
|
+
*/
|
|
205
|
+
export function foldSourceContentOutline(content, filePath, queryFilter) {
|
|
206
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
207
|
+
const language = LANGUAGE_BY_EXT[ext] ?? 'text';
|
|
208
|
+
const lines = content.split(/\r?\n/);
|
|
209
|
+
const totalLines = lines.length;
|
|
210
|
+
let importCount = 0;
|
|
211
|
+
for (const line of lines) {
|
|
212
|
+
const t = line.trim();
|
|
213
|
+
if (t.startsWith('import ') || t.startsWith('from ') || t.startsWith('use ') || t.startsWith('package ')) {
|
|
214
|
+
importCount += 1;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const patterns = language === 'python'
|
|
218
|
+
? PYTHON_PATTERNS
|
|
219
|
+
: (language === 'go' || language === 'rust')
|
|
220
|
+
? GO_RUST_PATTERNS
|
|
221
|
+
: TS_JS_PATTERNS;
|
|
222
|
+
const symbols = [];
|
|
223
|
+
const lowerQuery = queryFilter ? queryFilter.toLowerCase().trim() : '';
|
|
224
|
+
for (let i = 0; i < lines.length; i++) {
|
|
225
|
+
const line = lines[i] ?? '';
|
|
226
|
+
const trimmed = line.trim();
|
|
227
|
+
if (!trimmed || trimmed.startsWith('//') || trimmed.startsWith('*') || trimmed.startsWith('#')) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
for (const pat of patterns) {
|
|
231
|
+
const m = pat.regex.exec(line);
|
|
232
|
+
if (!m)
|
|
233
|
+
continue;
|
|
234
|
+
const rawName = m[pat.nameGroup] || m[3] || '';
|
|
235
|
+
if (!rawName || RESERVED_METHOD_NAMES.has(rawName))
|
|
236
|
+
continue;
|
|
237
|
+
// For class method pattern, ensure it is either followed by `{` on this/next lines (not a function call ending in `);`)
|
|
238
|
+
if (pat.kind === 'method') {
|
|
239
|
+
const sigCheck = extractCollapsedSignature(lines, i, 'method');
|
|
240
|
+
if (sigCheck.endsWith(';') || /=/.test(line))
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const exported = /\bexport\b|\bpub\b/.test(line);
|
|
244
|
+
const lineStart = i + 1;
|
|
245
|
+
const lineEnd = language === 'python'
|
|
246
|
+
? findIndentBlockEnd(lines, i)
|
|
247
|
+
: findBraceBlockEnd(lines, i);
|
|
248
|
+
const signature = extractCollapsedSignature(lines, i, pat.kind);
|
|
249
|
+
const docComment = extractPrecedingDocComment(lines, i);
|
|
250
|
+
if (lowerQuery) {
|
|
251
|
+
const hay = `${rawName} ${signature} ${docComment ?? ''}`.toLowerCase();
|
|
252
|
+
if (!hay.includes(lowerQuery))
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
symbols.push({
|
|
256
|
+
name: rawName,
|
|
257
|
+
kind: pat.kind,
|
|
258
|
+
signature,
|
|
259
|
+
lineStart,
|
|
260
|
+
lineEnd,
|
|
261
|
+
exported,
|
|
262
|
+
...(docComment ? { docComment } : {})
|
|
263
|
+
});
|
|
264
|
+
// Advance past multi-line parameter list header if applicable so inner lines aren't misread as methods
|
|
265
|
+
if (pat.kind === 'function') {
|
|
266
|
+
i = Math.max(i, lineEnd - 1);
|
|
267
|
+
}
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const header = `${path.basename(filePath)} (${language}, ${totalLines} lines, ${importCount} imports, ${symbols.length} symbols)`;
|
|
272
|
+
const bodyLines = symbols.map((sym) => {
|
|
273
|
+
const exp = sym.exported ? 'export ' : '';
|
|
274
|
+
const doc = sym.docComment ? ` — ${sym.docComment}` : '';
|
|
275
|
+
return ` [${sym.lineStart}-${sym.lineEnd}] ${exp}${sym.kind} ${sym.name}${doc}`;
|
|
276
|
+
});
|
|
277
|
+
const renderedOutline = [
|
|
278
|
+
header,
|
|
279
|
+
...(bodyLines.length > 0 ? bodyLines : [' (no structural symbols matched)']),
|
|
280
|
+
`Use fs_read with startLine/endLine to unfold a specific [lineStart-lineEnd] symbol.`
|
|
281
|
+
].join('\n');
|
|
282
|
+
return {
|
|
283
|
+
filePath,
|
|
284
|
+
language,
|
|
285
|
+
totalLines,
|
|
286
|
+
importCount,
|
|
287
|
+
symbols,
|
|
288
|
+
renderedOutline
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Read a file from disk and fold it into a compact structural symbol outline.
|
|
293
|
+
*/
|
|
294
|
+
export function foldSourceFileOutline(filePath, queryFilter) {
|
|
295
|
+
const expanded = filePath.startsWith('~/')
|
|
296
|
+
? path.join(os.homedir(), filePath.slice(2))
|
|
297
|
+
: filePath;
|
|
298
|
+
const resolved = path.resolve(expanded);
|
|
299
|
+
const content = fs.readFileSync(resolved, 'utf8');
|
|
300
|
+
return foldSourceContentOutline(content, resolved, queryFilter);
|
|
301
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/memory/symbolFolder.ts
|
|
3
|
+
* Structural Code Symbol Folding (`foldSourceFileOutline`) for Causyne Agent.
|
|
4
|
+
*
|
|
5
|
+
* Extracts top-level and class-member symbol signatures (including multi-line
|
|
6
|
+
* function and method signatures), JSDoc summaries, and exact `[lineStart, lineEnd]`
|
|
7
|
+
* line spans while folding implementation bodies. Allows the agent to inspect a
|
|
8
|
+
* 1,500-line file in ~120 tokens and then read only the exact `[lineStart, lineEnd]`
|
|
9
|
+
* slice with `fs_read`.
|
|
10
|
+
*
|
|
11
|
+
* Authorship: Strict TypeScript (Causyne Agent).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import os from 'node:os';
|
|
16
|
+
import path from 'node:path';
|
|
17
|
+
import type { FoldedFileOutline, FoldedSymbolEntry, FoldedSymbolKind } from './episodicTypes.js';
|
|
18
|
+
|
|
19
|
+
const LANGUAGE_BY_EXT: Readonly<Record<string, string>> = {
|
|
20
|
+
'.ts': 'typescript',
|
|
21
|
+
'.tsx': 'tsx',
|
|
22
|
+
'.js': 'javascript',
|
|
23
|
+
'.jsx': 'jsx',
|
|
24
|
+
'.mjs': 'javascript',
|
|
25
|
+
'.cjs': 'javascript',
|
|
26
|
+
'.py': 'python',
|
|
27
|
+
'.go': 'go',
|
|
28
|
+
'.rs': 'rust',
|
|
29
|
+
'.kt': 'kotlin',
|
|
30
|
+
'.java': 'java'
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
interface RawPattern {
|
|
34
|
+
readonly kind: FoldedSymbolKind;
|
|
35
|
+
readonly regex: RegExp;
|
|
36
|
+
readonly nameGroup: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const TS_JS_PATTERNS: readonly RawPattern[] = [
|
|
40
|
+
{
|
|
41
|
+
kind: 'function',
|
|
42
|
+
regex: /^(\s*)(export\s+)?(default\s+)?(async\s+)?function\s*\*?\s*([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:<[^>]*>)?\s*\(/,
|
|
43
|
+
nameGroup: 5
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
kind: 'class',
|
|
47
|
+
regex: /^(\s*)(export\s+)?(default\s+)?(abstract\s+)?class\s+([A-Za-z_$][A-Za-z0-9_$]*)(?:\s+[^{]+)?/,
|
|
48
|
+
nameGroup: 5
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
kind: 'interface',
|
|
52
|
+
regex: /^(\s*)(export\s+)?interface\s+([A-Za-z_$][A-Za-z0-9_$]*)(?:\s+[^{]+)?/,
|
|
53
|
+
nameGroup: 3
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
kind: 'type',
|
|
57
|
+
regex: /^(\s*)(export\s+)?type\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:<[^>]*>)?\s*=/,
|
|
58
|
+
nameGroup: 3
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
kind: 'enum',
|
|
62
|
+
regex: /^(\s*)(export\s+)?(const\s+)?enum\s+([A-Za-z_$][A-Za-z0-9_$]*)/,
|
|
63
|
+
nameGroup: 4
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
kind: 'const',
|
|
67
|
+
regex: /^(\s*)(export\s+)(?:const|let)\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*(?::\s*[^=]+)?\s*=/,
|
|
68
|
+
nameGroup: 3
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
kind: 'method',
|
|
72
|
+
regex: /^(\s{2,4})(?:(?:public|private|protected|static|readonly|override|async)\s+)*([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:<[^>]*>)?\s*\(/,
|
|
73
|
+
nameGroup: 2
|
|
74
|
+
}
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
const PYTHON_PATTERNS: readonly RawPattern[] = [
|
|
78
|
+
{
|
|
79
|
+
kind: 'class',
|
|
80
|
+
regex: /^(\s*)class\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?:\([^)]*\))?\s*:/,
|
|
81
|
+
nameGroup: 2
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
kind: 'function',
|
|
85
|
+
regex: /^(\s*)(?:async\s+)?def\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(/,
|
|
86
|
+
nameGroup: 2
|
|
87
|
+
}
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
const GO_RUST_PATTERNS: readonly RawPattern[] = [
|
|
91
|
+
{
|
|
92
|
+
kind: 'function',
|
|
93
|
+
regex: /^(\s*)(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?(?:func|fn)\s+(?:\([^)]+\)\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*(?:<[^>]*>)?\s*\(/,
|
|
94
|
+
nameGroup: 2
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
kind: 'class',
|
|
98
|
+
regex: /^(\s*)(?:pub(?:\([^)]*\))?\s+)?(?:type\s+([A-Za-z_][A-Za-z0-9_]*)\s+struct|struct\s+([A-Za-z_][A-Za-z0-9_]*))/,
|
|
99
|
+
nameGroup: 2
|
|
100
|
+
}
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
const RESERVED_METHOD_NAMES: ReadonlySet<string> = new Set([
|
|
104
|
+
'if', 'for', 'while', 'switch', 'catch', 'return', 'try', 'else', 'do', 'super', 'import', 'require'
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
function extractPrecedingDocComment(lines: readonly string[], lineIndex: number): string | undefined {
|
|
108
|
+
let idx = lineIndex - 1;
|
|
109
|
+
while (idx >= 0 && (lines[idx] ?? '').trim() === '') {
|
|
110
|
+
idx--;
|
|
111
|
+
}
|
|
112
|
+
if (idx < 0) return undefined;
|
|
113
|
+
const line = (lines[idx] ?? '').trim();
|
|
114
|
+
if (line.endsWith('*/')) {
|
|
115
|
+
const docLines: string[] = [];
|
|
116
|
+
while (idx >= 0) {
|
|
117
|
+
const current = (lines[idx] ?? '').trim();
|
|
118
|
+
const cleaned = current
|
|
119
|
+
.replace(/^\/\*\*?/, '')
|
|
120
|
+
.replace(/\*\/$/, '')
|
|
121
|
+
.replace(/^\*\s?/, '')
|
|
122
|
+
.trim();
|
|
123
|
+
if (cleaned && !cleaned.startsWith('@')) {
|
|
124
|
+
docLines.unshift(cleaned);
|
|
125
|
+
}
|
|
126
|
+
if (current.startsWith('/*')) break;
|
|
127
|
+
idx--;
|
|
128
|
+
}
|
|
129
|
+
const summary = docLines.join(' ').trim();
|
|
130
|
+
return summary ? summary.slice(0, 110) : undefined;
|
|
131
|
+
}
|
|
132
|
+
if (line.startsWith('//') || line.startsWith('#')) {
|
|
133
|
+
const cleaned = line.replace(/^(?:\/\/+|#+)\s*/, '').trim();
|
|
134
|
+
return cleaned ? cleaned.slice(0, 110) : undefined;
|
|
135
|
+
}
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Collapse a single-line or multi-line declaration header into a concise one-line signature.
|
|
141
|
+
*/
|
|
142
|
+
function extractCollapsedSignature(lines: readonly string[], startIndex: number, kind: FoldedSymbolKind): string {
|
|
143
|
+
const firstLine = (lines[startIndex] ?? '').trim();
|
|
144
|
+
if (kind !== 'function' && kind !== 'method') {
|
|
145
|
+
return firstLine.replace(/\s*\{$/, '').slice(0, 100);
|
|
146
|
+
}
|
|
147
|
+
let combined = '';
|
|
148
|
+
let parenDepth = 0;
|
|
149
|
+
let seenOpenParen = false;
|
|
150
|
+
const maxLookahead = Math.min(lines.length, startIndex + 12);
|
|
151
|
+
for (let i = startIndex; i < maxLookahead; i++) {
|
|
152
|
+
const part = (lines[i] ?? '').trim();
|
|
153
|
+
combined += (combined ? ' ' : '') + part;
|
|
154
|
+
for (let c = 0; c < part.length; c++) {
|
|
155
|
+
const ch = part[c];
|
|
156
|
+
if (ch === '(') {
|
|
157
|
+
parenDepth += 1;
|
|
158
|
+
seenOpenParen = true;
|
|
159
|
+
} else if (ch === ')') {
|
|
160
|
+
parenDepth -= 1;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (seenOpenParen && parenDepth <= 0) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return combined
|
|
168
|
+
.replace(/\s+/g, ' ')
|
|
169
|
+
.replace(/\s*\{[^)]*$/, '')
|
|
170
|
+
.trim()
|
|
171
|
+
.slice(0, 110);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function findBraceBlockEnd(lines: readonly string[], startIndex: number): number {
|
|
175
|
+
let depth = 0;
|
|
176
|
+
let opened = false;
|
|
177
|
+
for (let i = startIndex; i < lines.length; i++) {
|
|
178
|
+
const line = lines[i] ?? '';
|
|
179
|
+
for (let c = 0; c < line.length; c++) {
|
|
180
|
+
const ch = line[c];
|
|
181
|
+
if (ch === '{') {
|
|
182
|
+
depth += 1;
|
|
183
|
+
opened = true;
|
|
184
|
+
} else if (ch === '}') {
|
|
185
|
+
depth -= 1;
|
|
186
|
+
if (opened && depth <= 0) {
|
|
187
|
+
return i + 1;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (!opened && line.trim().endsWith(';')) {
|
|
192
|
+
return i + 1;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return Math.min(lines.length, startIndex + 1);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function findIndentBlockEnd(lines: readonly string[], startIndex: number): number {
|
|
199
|
+
const startLine = lines[startIndex] ?? '';
|
|
200
|
+
const baseIndent = (startLine.match(/^\s*/)?.[0] ?? '').length;
|
|
201
|
+
let lastLine = startIndex + 1;
|
|
202
|
+
for (let i = startIndex + 1; i < lines.length; i++) {
|
|
203
|
+
const line = lines[i] ?? '';
|
|
204
|
+
const trimmed = line.trim();
|
|
205
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
206
|
+
const indent = (line.match(/^\s*/)?.[0] ?? '').length;
|
|
207
|
+
if (indent <= baseIndent) {
|
|
208
|
+
return lastLine;
|
|
209
|
+
}
|
|
210
|
+
lastLine = i + 1;
|
|
211
|
+
}
|
|
212
|
+
return lastLine;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Fold a source code string into a compact structural symbol outline.
|
|
217
|
+
*/
|
|
218
|
+
export function foldSourceContentOutline(
|
|
219
|
+
content: string,
|
|
220
|
+
filePath: string,
|
|
221
|
+
queryFilter?: string
|
|
222
|
+
): FoldedFileOutline {
|
|
223
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
224
|
+
const language = LANGUAGE_BY_EXT[ext] ?? 'text';
|
|
225
|
+
const lines = content.split(/\r?\n/);
|
|
226
|
+
const totalLines = lines.length;
|
|
227
|
+
|
|
228
|
+
let importCount = 0;
|
|
229
|
+
for (const line of lines) {
|
|
230
|
+
const t = line.trim();
|
|
231
|
+
if (t.startsWith('import ') || t.startsWith('from ') || t.startsWith('use ') || t.startsWith('package ')) {
|
|
232
|
+
importCount += 1;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const patterns = language === 'python'
|
|
237
|
+
? PYTHON_PATTERNS
|
|
238
|
+
: (language === 'go' || language === 'rust')
|
|
239
|
+
? GO_RUST_PATTERNS
|
|
240
|
+
: TS_JS_PATTERNS;
|
|
241
|
+
|
|
242
|
+
const symbols: FoldedSymbolEntry[] = [];
|
|
243
|
+
const lowerQuery = queryFilter ? queryFilter.toLowerCase().trim() : '';
|
|
244
|
+
|
|
245
|
+
for (let i = 0; i < lines.length; i++) {
|
|
246
|
+
const line = lines[i] ?? '';
|
|
247
|
+
const trimmed = line.trim();
|
|
248
|
+
if (!trimmed || trimmed.startsWith('//') || trimmed.startsWith('*') || trimmed.startsWith('#')) {
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
for (const pat of patterns) {
|
|
253
|
+
const m = pat.regex.exec(line);
|
|
254
|
+
if (!m) continue;
|
|
255
|
+
const rawName = m[pat.nameGroup] || m[3] || '';
|
|
256
|
+
if (!rawName || RESERVED_METHOD_NAMES.has(rawName)) continue;
|
|
257
|
+
|
|
258
|
+
// For class method pattern, ensure it is either followed by `{` on this/next lines (not a function call ending in `);`)
|
|
259
|
+
if (pat.kind === 'method') {
|
|
260
|
+
const sigCheck = extractCollapsedSignature(lines, i, 'method');
|
|
261
|
+
if (sigCheck.endsWith(';') || /=/.test(line)) continue;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const exported = /\bexport\b|\bpub\b/.test(line);
|
|
265
|
+
const lineStart = i + 1;
|
|
266
|
+
const lineEnd = language === 'python'
|
|
267
|
+
? findIndentBlockEnd(lines, i)
|
|
268
|
+
: findBraceBlockEnd(lines, i);
|
|
269
|
+
const signature = extractCollapsedSignature(lines, i, pat.kind);
|
|
270
|
+
const docComment = extractPrecedingDocComment(lines, i);
|
|
271
|
+
|
|
272
|
+
if (lowerQuery) {
|
|
273
|
+
const hay = `${rawName} ${signature} ${docComment ?? ''}`.toLowerCase();
|
|
274
|
+
if (!hay.includes(lowerQuery)) break;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
symbols.push({
|
|
278
|
+
name: rawName,
|
|
279
|
+
kind: pat.kind,
|
|
280
|
+
signature,
|
|
281
|
+
lineStart,
|
|
282
|
+
lineEnd,
|
|
283
|
+
exported,
|
|
284
|
+
...(docComment ? { docComment } : {})
|
|
285
|
+
});
|
|
286
|
+
// Advance past multi-line parameter list header if applicable so inner lines aren't misread as methods
|
|
287
|
+
if (pat.kind === 'function') {
|
|
288
|
+
i = Math.max(i, lineEnd - 1);
|
|
289
|
+
}
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const header = `${path.basename(filePath)} (${language}, ${totalLines} lines, ${importCount} imports, ${symbols.length} symbols)`;
|
|
295
|
+
const bodyLines = symbols.map((sym) => {
|
|
296
|
+
const exp = sym.exported ? 'export ' : '';
|
|
297
|
+
const doc = sym.docComment ? ` — ${sym.docComment}` : '';
|
|
298
|
+
return ` [${sym.lineStart}-${sym.lineEnd}] ${exp}${sym.kind} ${sym.name}${doc}`;
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const renderedOutline = [
|
|
302
|
+
header,
|
|
303
|
+
...(bodyLines.length > 0 ? bodyLines : [' (no structural symbols matched)']),
|
|
304
|
+
`Use fs_read with startLine/endLine to unfold a specific [lineStart-lineEnd] symbol.`
|
|
305
|
+
].join('\n');
|
|
306
|
+
|
|
307
|
+
return {
|
|
308
|
+
filePath,
|
|
309
|
+
language,
|
|
310
|
+
totalLines,
|
|
311
|
+
importCount,
|
|
312
|
+
symbols,
|
|
313
|
+
renderedOutline
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Read a file from disk and fold it into a compact structural symbol outline.
|
|
319
|
+
*/
|
|
320
|
+
export function foldSourceFileOutline(filePath: string, queryFilter?: string): FoldedFileOutline {
|
|
321
|
+
const expanded = filePath.startsWith('~/')
|
|
322
|
+
? path.join(os.homedir(), filePath.slice(2))
|
|
323
|
+
: filePath;
|
|
324
|
+
const resolved = path.resolve(expanded);
|
|
325
|
+
const content = fs.readFileSync(resolved, 'utf8');
|
|
326
|
+
return foldSourceContentOutline(content, resolved, queryFilter);
|
|
327
|
+
}
|
package/src/toolPacks.js
CHANGED
package/src/toolPacks.ts
CHANGED
package/src/tools/desktopOpen.js
CHANGED
|
@@ -44,8 +44,15 @@ export async function openWithNativeOS(target, app) {
|
|
|
44
44
|
cliArgs = app ? ['-a', app, target] : [target];
|
|
45
45
|
}
|
|
46
46
|
else if (platform === 'win32') {
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
if (app) {
|
|
48
|
+
command = app;
|
|
49
|
+
cliArgs = [target];
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
const sysRoot = process.env.SYSTEMROOT || process.env.windir || 'C:\\Windows';
|
|
53
|
+
command = path.join(sysRoot, 'explorer.exe');
|
|
54
|
+
cliArgs = [target];
|
|
55
|
+
}
|
|
49
56
|
}
|
|
50
57
|
else {
|
|
51
58
|
command = 'xdg-open';
|
package/src/tools/desktopOpen.ts
CHANGED
|
@@ -68,8 +68,14 @@ export async function openWithNativeOS(target: string, app?: string): Promise<{
|
|
|
68
68
|
command = '/usr/bin/open';
|
|
69
69
|
cliArgs = app ? ['-a', app, target] : [target];
|
|
70
70
|
} else if (platform === 'win32') {
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
if (app) {
|
|
72
|
+
command = app;
|
|
73
|
+
cliArgs = [target];
|
|
74
|
+
} else {
|
|
75
|
+
const sysRoot = process.env.SYSTEMROOT || process.env.windir || 'C:\\Windows';
|
|
76
|
+
command = path.join(sysRoot, 'explorer.exe');
|
|
77
|
+
cliArgs = [target];
|
|
78
|
+
}
|
|
73
79
|
} else {
|
|
74
80
|
command = 'xdg-open';
|
|
75
81
|
cliArgs = [target];
|