@aragon-agent/cli 0.6.78 → 0.6.79
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 +89 -21
- package/dist/agent/controller.d.ts +36 -0
- package/dist/agent/controller.js +108 -0
- package/dist/agent/system-prompt.d.ts +10 -0
- package/dist/agent/system-prompt.js +6 -0
- package/dist/app/App.js +108 -27
- package/dist/app/index-console.d.ts +59 -0
- package/dist/app/index-console.js +99 -0
- package/dist/cli.js +80 -4
- package/dist/commands/builtins.js +4 -2
- package/dist/commands/catalog.js +7 -1
- package/dist/commands/index-build.js +10 -1
- package/dist/commands/index-command.d.ts +43 -0
- package/dist/commands/index-command.js +244 -0
- package/dist/commands/index-copy.d.ts +50 -0
- package/dist/commands/index-copy.js +82 -0
- package/dist/commands/kit-command.d.ts +10 -0
- package/dist/commands/kit-command.js +8 -2
- package/dist/commands/kits-command.d.ts +35 -0
- package/dist/commands/kits-command.js +137 -0
- package/dist/commands/registry.d.ts +10 -0
- package/dist/compaction/memory-agent.js +61 -0
- package/dist/config/env.d.ts +30 -0
- package/dist/config/env.js +47 -0
- package/dist/config/launch-notice.d.ts +61 -0
- package/dist/config/launch-notice.js +138 -0
- package/dist/config/load.d.ts +20 -0
- package/dist/config/load.js +72 -2
- package/dist/diagnostics/info.d.ts +28 -0
- package/dist/diagnostics/info.js +41 -0
- package/dist/exec/cli-commands.d.ts +24 -0
- package/dist/exec/cli-commands.js +48 -1
- package/dist/exec/index.js +9 -0
- package/dist/exec/options.d.ts +12 -0
- package/dist/host/index-build.d.ts +38 -7
- package/dist/host/index-build.js +74 -20
- package/dist/indexer/commit.d.ts +77 -0
- package/dist/indexer/commit.js +145 -0
- package/dist/indexer/discover.d.ts +50 -0
- package/dist/indexer/discover.js +147 -0
- package/dist/indexer/engine.d.ts +51 -0
- package/dist/indexer/engine.js +477 -0
- package/dist/indexer/errors.d.ts +62 -0
- package/dist/indexer/errors.js +86 -0
- package/dist/indexer/finalize.d.ts +30 -0
- package/dist/indexer/finalize.js +19 -0
- package/dist/indexer/fingerprint.d.ts +41 -0
- package/dist/indexer/fingerprint.js +49 -0
- package/dist/indexer/ignore.d.ts +59 -0
- package/dist/indexer/ignore.js +140 -0
- package/dist/indexer/index.d.ts +21 -0
- package/dist/indexer/index.js +21 -0
- package/dist/indexer/l0.d.ts +55 -0
- package/dist/indexer/l0.js +168 -0
- package/dist/indexer/limits.d.ts +75 -0
- package/dist/indexer/limits.js +106 -0
- package/dist/indexer/link.d.ts +68 -0
- package/dist/indexer/link.js +220 -0
- package/dist/indexer/lock.d.ts +115 -0
- package/dist/indexer/lock.js +270 -0
- package/dist/indexer/parse.d.ts +68 -0
- package/dist/indexer/parse.js +163 -0
- package/dist/indexer/paths.d.ts +99 -0
- package/dist/indexer/paths.js +198 -0
- package/dist/indexer/plan.d.ts +48 -0
- package/dist/indexer/plan.js +80 -0
- package/dist/indexer/rank.d.ts +42 -0
- package/dist/indexer/rank.js +125 -0
- package/dist/indexer/read/cache.d.ts +28 -0
- package/dist/indexer/read/cache.js +54 -0
- package/dist/indexer/read/query.d.ts +48 -0
- package/dist/indexer/read/query.js +361 -0
- package/dist/indexer/scanner/extract.d.ts +36 -0
- package/dist/indexer/scanner/extract.js +134 -0
- package/dist/indexer/scanner/mask.d.ts +44 -0
- package/dist/indexer/scanner/mask.js +198 -0
- package/dist/indexer/scanner/py-extract.d.ts +29 -0
- package/dist/indexer/scanner/py-extract.js +164 -0
- package/dist/indexer/scanner/refs.d.ts +31 -0
- package/dist/indexer/scanner/refs.js +116 -0
- package/dist/indexer/scanner/ts-extract.d.ts +39 -0
- package/dist/indexer/scanner/ts-extract.js +277 -0
- package/dist/indexer/store.d.ts +88 -0
- package/dist/indexer/store.js +309 -0
- package/dist/indexer/tools.d.ts +120 -0
- package/dist/indexer/tools.js +132 -0
- package/dist/indexer/types.d.ts +225 -0
- package/dist/indexer/types.js +44 -0
- package/dist/indexer/wiring.d.ts +45 -0
- package/dist/indexer/wiring.js +42 -0
- package/dist/tools/index.d.ts +16 -2
- package/dist/tools/index.js +23 -0
- package/package.json +3 -3
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Character-level masking for the tier-1 scanner (spec 5.8 layer 1).
|
|
3
|
+
*
|
|
4
|
+
* WHY MASK INSTEAD OF PARSING: the alternative is `tree-sitter`, which needs a
|
|
5
|
+
* native build or wasm assets and would break "install and run" (spec NG1). The
|
|
6
|
+
* masking pass is what makes a character scanner safe enough to promise: it
|
|
7
|
+
* blanks strings, templates, regex literals and comments with SPACES of the same
|
|
8
|
+
* length, so a `class` inside a string can never be mistaken for a declaration
|
|
9
|
+
* and every byte offset (and therefore every line number) is preserved.
|
|
10
|
+
*
|
|
11
|
+
* Known and accepted limit (spec P2-7 / R8): a template literal is masked
|
|
12
|
+
* wholesale, including its `${...}` interpolations, so a call written only
|
|
13
|
+
* inside an interpolation is not reported. That is a syntax-level scanner, and
|
|
14
|
+
* `status: 'partial'` is how the artifact stays honest about it.
|
|
15
|
+
*
|
|
16
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
17
|
+
*/
|
|
18
|
+
/** Characters after which a `/` starts a regex literal rather than a division. */
|
|
19
|
+
const REGEX_PRECEDERS = '(,=:[!&|?{};+-*%<>~^';
|
|
20
|
+
/** True when `ch` can appear in an identifier (used by the regex heuristic too). */
|
|
21
|
+
function isWordChar(ch) {
|
|
22
|
+
return /[A-Za-z0-9_$]/.test(ch);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Decide whether a `/` at `index` opens a regex literal. Heuristic, not a parse:
|
|
26
|
+
* a slash after an operator/keyword position is a regex, after a value it is a
|
|
27
|
+
* division. Getting this wrong costs at most the masking of one expression.
|
|
28
|
+
*/
|
|
29
|
+
function startsRegex(source, index) {
|
|
30
|
+
for (let i = index - 1; i >= 0; i -= 1) {
|
|
31
|
+
const ch = source[i];
|
|
32
|
+
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r')
|
|
33
|
+
continue;
|
|
34
|
+
if (REGEX_PRECEDERS.includes(ch))
|
|
35
|
+
return true;
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Mask one source file. Linear in the input size, one pass, no recursion.
|
|
42
|
+
*
|
|
43
|
+
* Returns the same text with the same length: every masked character becomes a
|
|
44
|
+
* space except line breaks, which survive so line numbers stay valid.
|
|
45
|
+
*/
|
|
46
|
+
export function maskSource(source) {
|
|
47
|
+
const out = source.split('');
|
|
48
|
+
const lineStarts = [0];
|
|
49
|
+
let state = 'code';
|
|
50
|
+
let unterminated = false;
|
|
51
|
+
for (let i = 0; i < source.length; i += 1) {
|
|
52
|
+
const ch = source[i];
|
|
53
|
+
const next = source[i + 1];
|
|
54
|
+
if (ch === '\n')
|
|
55
|
+
lineStarts.push(i + 1);
|
|
56
|
+
if (state === 'code') {
|
|
57
|
+
if (ch === '/' && next === '/') {
|
|
58
|
+
state = 'line-comment';
|
|
59
|
+
out[i] = ' ';
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (ch === '/' && next === '*') {
|
|
63
|
+
state = 'block-comment';
|
|
64
|
+
out[i] = ' ';
|
|
65
|
+
out[i + 1] = ' ';
|
|
66
|
+
i += 1;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (ch === "'") {
|
|
70
|
+
state = 'single';
|
|
71
|
+
out[i] = ' ';
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (ch === '"') {
|
|
75
|
+
state = 'double';
|
|
76
|
+
out[i] = ' ';
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (ch === '`') {
|
|
80
|
+
state = 'template';
|
|
81
|
+
out[i] = ' ';
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (ch === '/' && startsRegex(source, i)) {
|
|
85
|
+
state = 'regex';
|
|
86
|
+
out[i] = ' ';
|
|
87
|
+
}
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (state === 'line-comment') {
|
|
91
|
+
if (ch === '\n') {
|
|
92
|
+
state = 'code';
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
out[i] = ' ';
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (state === 'block-comment') {
|
|
99
|
+
if (ch === '*' && next === '/') {
|
|
100
|
+
out[i] = ' ';
|
|
101
|
+
out[i + 1] = ' ';
|
|
102
|
+
i += 1;
|
|
103
|
+
state = 'code';
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (ch !== '\n')
|
|
107
|
+
out[i] = ' ';
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (state === 'regex') {
|
|
111
|
+
if (ch === '\\') {
|
|
112
|
+
out[i] = ' ';
|
|
113
|
+
if (next && next !== '\n') {
|
|
114
|
+
out[i + 1] = ' ';
|
|
115
|
+
i += 1;
|
|
116
|
+
}
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (ch === '/') {
|
|
120
|
+
out[i] = ' ';
|
|
121
|
+
state = 'code';
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (ch === '\n') {
|
|
125
|
+
// A regex cannot span lines: treat the line break as the terminator so
|
|
126
|
+
// one stray slash does not blank the rest of the file.
|
|
127
|
+
state = 'code';
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
out[i] = ' ';
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
// String and template states share the backslash-escape rule.
|
|
134
|
+
if (ch === '\\') {
|
|
135
|
+
out[i] = ' ';
|
|
136
|
+
if (next && next !== '\n') {
|
|
137
|
+
out[i + 1] = ' ';
|
|
138
|
+
i += 1;
|
|
139
|
+
}
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const terminator = state === 'single' ? "'" : state === 'double' ? '"' : '`';
|
|
143
|
+
if (ch === terminator) {
|
|
144
|
+
out[i] = ' ';
|
|
145
|
+
state = 'code';
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (ch === '\n' && state !== 'template') {
|
|
149
|
+
// An unterminated '...' or "..." ends at the newline: report and recover.
|
|
150
|
+
state = 'code';
|
|
151
|
+
unterminated = true;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (ch !== '\n')
|
|
155
|
+
out[i] = ' ';
|
|
156
|
+
}
|
|
157
|
+
if (state === 'block-comment' || state === 'single' || state === 'double' || state === 'template') {
|
|
158
|
+
unterminated = true;
|
|
159
|
+
}
|
|
160
|
+
return { masked: out.join(''), lineStarts, unterminated };
|
|
161
|
+
}
|
|
162
|
+
/** 1-based line number for a byte offset, via binary search over `lineStarts`. */
|
|
163
|
+
export function lineAt(lineStarts, offset) {
|
|
164
|
+
let low = 0;
|
|
165
|
+
let high = lineStarts.length - 1;
|
|
166
|
+
while (low < high) {
|
|
167
|
+
const mid = Math.ceil((low + high) / 2);
|
|
168
|
+
if (lineStarts[mid] <= offset)
|
|
169
|
+
low = mid;
|
|
170
|
+
else
|
|
171
|
+
high = mid - 1;
|
|
172
|
+
}
|
|
173
|
+
return low + 1;
|
|
174
|
+
}
|
|
175
|
+
/** Same text as `source` but with the given 1-based line replaced by spaces (keeps offsets). */
|
|
176
|
+
export function blankLine(source, lineStarts, line) {
|
|
177
|
+
const start = lineStarts[line - 1];
|
|
178
|
+
if (start === undefined)
|
|
179
|
+
return source;
|
|
180
|
+
const end = lineStarts[line] ?? source.length;
|
|
181
|
+
const span = source.slice(start, end);
|
|
182
|
+
// NEWLINES SURVIVE. The span ends at the NEXT line's start, so it includes the trailing
|
|
183
|
+
// `\n`; replacing it verbatim collapses two lines into one and every later line number
|
|
184
|
+
// shifts by one - the exact "off by one" the masking pass exists to avoid.
|
|
185
|
+
const blanked = span.replace(/[^\n]/g, ' ');
|
|
186
|
+
return `${source.slice(0, start)}${blanked}${source.slice(end)}`;
|
|
187
|
+
}
|
|
188
|
+
/** Split masked text into lines, paired with their 1-based number. */
|
|
189
|
+
export function maskedLines(masked) {
|
|
190
|
+
const lines = masked.split('\n');
|
|
191
|
+
return lines.map((text, index) => ({ line: index + 1, text }));
|
|
192
|
+
}
|
|
193
|
+
/** True when `ch` at `index` sits inside a word (used to reject partial identifier matches). */
|
|
194
|
+
export function isIdentifierBoundary(source, index) {
|
|
195
|
+
if (index <= 0)
|
|
196
|
+
return true;
|
|
197
|
+
return !isWordChar(source[index - 1]);
|
|
198
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Python declaration extraction (spec 5.8 layer 4).
|
|
3
|
+
*
|
|
4
|
+
* Python is the only tier-1 language besides TS/JS (spec NG4), because the
|
|
5
|
+
* reference toolchain indexes it too and a mixed repository would otherwise get
|
|
6
|
+
* an index that silently ignores half its code. The scanner is indentation
|
|
7
|
+
* based rather than brace based: an `indent` stack replaces the scope stack,
|
|
8
|
+
* which is what makes `class A:` -> `def m(self)` nest correctly.
|
|
9
|
+
*
|
|
10
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
11
|
+
*/
|
|
12
|
+
import type { ImportRow, IndexLang, SymbolRow } from '../types.js';
|
|
13
|
+
/** Everything the Python extractor produces. */
|
|
14
|
+
export interface PyExtractResult {
|
|
15
|
+
/** Symbols in source order. */
|
|
16
|
+
readonly symbols: readonly SymbolRow[];
|
|
17
|
+
/** Imports in source order. */
|
|
18
|
+
readonly imports: readonly ImportRow[];
|
|
19
|
+
/** Line count as seen by the scanner. */
|
|
20
|
+
readonly lines: number;
|
|
21
|
+
/** True when the file is only partially understood. */
|
|
22
|
+
readonly partial: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Extract classes, functions, methods, decorated definitions and module-level
|
|
26
|
+
* constants from one Python source. Decorators are attached to the definition
|
|
27
|
+
* that follows them, which is why the loop remembers the pending decorator names.
|
|
28
|
+
*/
|
|
29
|
+
export declare function extractPy(source: string, lang: IndexLang, path: string): PyExtractResult;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Python declaration extraction (spec 5.8 layer 4).
|
|
3
|
+
*
|
|
4
|
+
* Python is the only tier-1 language besides TS/JS (spec NG4), because the
|
|
5
|
+
* reference toolchain indexes it too and a mixed repository would otherwise get
|
|
6
|
+
* an index that silently ignores half its code. The scanner is indentation
|
|
7
|
+
* based rather than brace based: an `indent` stack replaces the scope stack,
|
|
8
|
+
* which is what makes `class A:` -> `def m(self)` nest correctly.
|
|
9
|
+
*
|
|
10
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
11
|
+
*/
|
|
12
|
+
import { maskSource } from './mask.js';
|
|
13
|
+
/** Longest signature kept per symbol. */
|
|
14
|
+
const SIGNATURE_MAX = 160;
|
|
15
|
+
/** Indentation width, counting a tab as 4 columns so mixed files still nest sanely. */
|
|
16
|
+
function indentOf(line) {
|
|
17
|
+
let width = 0;
|
|
18
|
+
for (const ch of line) {
|
|
19
|
+
if (ch === ' ')
|
|
20
|
+
width += 1;
|
|
21
|
+
else if (ch === '\t')
|
|
22
|
+
width += 4;
|
|
23
|
+
else
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
return width;
|
|
27
|
+
}
|
|
28
|
+
/** Append one symbol, resolving a duplicate qualified name with a `#n` suffix. */
|
|
29
|
+
function pushSymbol(symbols, used, row) {
|
|
30
|
+
const seen = used.get(row.q) ?? 0;
|
|
31
|
+
used.set(row.q, seen + 1);
|
|
32
|
+
symbols.push({ ...row, q: seen === 0 ? row.q : `${row.q}#${seen + 1}`, el: row.ln, fl: 0, r: 0 });
|
|
33
|
+
return symbols.length - 1;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Extract classes, functions, methods, decorated definitions and module-level
|
|
37
|
+
* constants from one Python source. Decorators are attached to the definition
|
|
38
|
+
* that follows them, which is why the loop remembers the pending decorator names.
|
|
39
|
+
*/
|
|
40
|
+
export function extractPy(source, lang, path) {
|
|
41
|
+
const { masked, unterminated } = maskSource(source);
|
|
42
|
+
const maskedRows = masked.split('\n');
|
|
43
|
+
const rawRows = source.split('\n');
|
|
44
|
+
const symbols = [];
|
|
45
|
+
const imports = [];
|
|
46
|
+
const used = new Map();
|
|
47
|
+
const scopes = [];
|
|
48
|
+
let partial = unterminated;
|
|
49
|
+
for (let index = 0; index < maskedRows.length; index += 1) {
|
|
50
|
+
const maskedLine = maskedRows[index];
|
|
51
|
+
const rawLine = rawRows[index] ?? '';
|
|
52
|
+
const lineNo = index + 1;
|
|
53
|
+
if (maskedLine.trim() === '')
|
|
54
|
+
continue;
|
|
55
|
+
const indent = indentOf(maskedLine);
|
|
56
|
+
// Close scopes whose body has ended (the line dedents to or past the owner).
|
|
57
|
+
while (scopes.length > 0 && indent <= scopes[scopes.length - 1].bodyIndent - 1) {
|
|
58
|
+
const closed = scopes.pop();
|
|
59
|
+
if (closed)
|
|
60
|
+
closeSymbol(symbols, closed, lineNo - 1);
|
|
61
|
+
}
|
|
62
|
+
collectImport(maskedLine, rawLine, lineNo, path, imports);
|
|
63
|
+
const declared = tryDefinition(maskedLine, rawLine, lineNo, indent, scopes, symbols, used, path);
|
|
64
|
+
if (!declared && scopes.length === 0)
|
|
65
|
+
tryModuleConstant(maskedLine, rawLine, lineNo, symbols, used, path);
|
|
66
|
+
}
|
|
67
|
+
for (const open of scopes)
|
|
68
|
+
closeSymbol(symbols, open, maskedRows.length);
|
|
69
|
+
return { symbols, imports, lines: maskedRows.length, partial };
|
|
70
|
+
}
|
|
71
|
+
/** Fill `el` when a scope closes. */
|
|
72
|
+
function closeSymbol(symbols, scope, endLine) {
|
|
73
|
+
const target = symbols[scope.symbolIndex];
|
|
74
|
+
if (!target || endLine <= target.el)
|
|
75
|
+
return;
|
|
76
|
+
symbols[scope.symbolIndex] = { ...target, el: endLine };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Try `class X:`, `def f(...)`, `async def f(...)`. A decorated definition keeps
|
|
80
|
+
* the decorator line in its signature, because that is the line a reader jumps to.
|
|
81
|
+
*/
|
|
82
|
+
function tryDefinition(maskedLine, rawLine, lineNo, indent, scopes, symbols, used, path) {
|
|
83
|
+
const text = maskedLine.trim();
|
|
84
|
+
const owner = scopes[scopes.length - 1];
|
|
85
|
+
const parent = owner?.qualified;
|
|
86
|
+
const classMatch = /^class\s+([A-Za-z_]\w*)\s*[(:]/.exec(text);
|
|
87
|
+
if (classMatch) {
|
|
88
|
+
const qualified = parent ? `${parent}.${classMatch[1]}` : classMatch[1];
|
|
89
|
+
const symbolIndex = pushSymbol(symbols, used, {
|
|
90
|
+
f: path,
|
|
91
|
+
n: classMatch[1],
|
|
92
|
+
q: qualified,
|
|
93
|
+
k: 'class',
|
|
94
|
+
ln: lineNo,
|
|
95
|
+
x: !classMatch[1].startsWith('_'),
|
|
96
|
+
sg: rawLine.trim().slice(0, SIGNATURE_MAX),
|
|
97
|
+
pa: parent,
|
|
98
|
+
});
|
|
99
|
+
scopes.push({ qualified, kind: 'class', bodyIndent: indent + 1, symbolIndex });
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
const defMatch = /^(?:async\s+)?def\s+([A-Za-z_]\w*)\s*\(/.exec(text);
|
|
103
|
+
if (!defMatch)
|
|
104
|
+
return false;
|
|
105
|
+
const inClass = owner?.kind === 'class';
|
|
106
|
+
const qualified = parent ? `${parent}.${defMatch[1]}` : defMatch[1];
|
|
107
|
+
const symbolIndex = pushSymbol(symbols, used, {
|
|
108
|
+
f: path,
|
|
109
|
+
n: defMatch[1],
|
|
110
|
+
q: qualified,
|
|
111
|
+
k: inClass ? 'method' : 'function',
|
|
112
|
+
ln: lineNo,
|
|
113
|
+
x: !defMatch[1].startsWith('_'),
|
|
114
|
+
sg: rawLine.trim().slice(0, SIGNATURE_MAX),
|
|
115
|
+
pa: parent,
|
|
116
|
+
});
|
|
117
|
+
scopes.push({
|
|
118
|
+
qualified,
|
|
119
|
+
kind: inClass ? 'method' : 'function',
|
|
120
|
+
bodyIndent: indent + 1,
|
|
121
|
+
symbolIndex,
|
|
122
|
+
});
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
/** A module-level `NAME = ...` constant: uppercase names only, so locals never leak in. */
|
|
126
|
+
function tryModuleConstant(maskedLine, rawLine, lineNo, symbols, used, path) {
|
|
127
|
+
const match = /^([A-Z][A-Z0-9_]{1,})\s*(?::[^=]+)?=/.exec(maskedLine.trim());
|
|
128
|
+
if (!match)
|
|
129
|
+
return;
|
|
130
|
+
pushSymbol(symbols, used, {
|
|
131
|
+
f: path,
|
|
132
|
+
n: match[1],
|
|
133
|
+
q: match[1],
|
|
134
|
+
k: 'constant',
|
|
135
|
+
ln: lineNo,
|
|
136
|
+
x: !match[1].startsWith('_'),
|
|
137
|
+
sg: rawLine.trim().slice(0, SIGNATURE_MAX),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** `import x` / `import x as y` / `from a.b import c`, with the module recorded as the specifier. */
|
|
141
|
+
function collectImport(maskedLine, rawLine, lineNo, path, out) {
|
|
142
|
+
const fromMatch = /^\s*from\s+([\w.]+)\s+import\s+(.+)$/.exec(rawLine);
|
|
143
|
+
if (fromMatch) {
|
|
144
|
+
const names = fromMatch[2]
|
|
145
|
+
.replace(/[()]/g, '')
|
|
146
|
+
.split(',')
|
|
147
|
+
.map((part) => part.trim())
|
|
148
|
+
.filter((part) => part !== '')
|
|
149
|
+
.map((part) => {
|
|
150
|
+
const alias = /^(\S+)\s+as\s+(\S+)$/.exec(part);
|
|
151
|
+
return alias ? { i: alias[1], l: alias[2] } : { i: part, l: part };
|
|
152
|
+
});
|
|
153
|
+
out.push({ f: path, ln: lineNo, sp: fromMatch[1].replace(/\./g, '/'), k: 'py', nm: names });
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const plain = /^\s*import\s+([\w.]+)(?:\s+as\s+(\w+))?/.exec(rawLine);
|
|
157
|
+
if (plain) {
|
|
158
|
+
const specifier = plain[1].replace(/\./g, '/');
|
|
159
|
+
const bound = plain[2] ?? plain[1].split('.')[0];
|
|
160
|
+
out.push({ f: path, ln: lineNo, sp: specifier, k: 'py', nm: [{ i: plain[1].split('.')[0], l: bound }] });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
void maskedLine;
|
|
164
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reference extraction (spec 5.8 layer 3).
|
|
3
|
+
*
|
|
4
|
+
* A reference is a NAME plus a kind plus a line. Resolution is deliberately not
|
|
5
|
+
* attempted here: a character scanner cannot know which `build` a file calls,
|
|
6
|
+
* and guessing "the nearest declaration" is how an index starts lying. Stage 5
|
|
7
|
+
* (LINK) resolves against imports and locals, and answers `none` when it cannot
|
|
8
|
+
* be sure.
|
|
9
|
+
*
|
|
10
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
11
|
+
*/
|
|
12
|
+
import type { RefKind } from '../types.js';
|
|
13
|
+
/** One reference as found in the text, before resolution. */
|
|
14
|
+
export interface RawRef {
|
|
15
|
+
/** Referenced name. */
|
|
16
|
+
readonly ln: number;
|
|
17
|
+
/** Kind word from `REF_KINDS`. */
|
|
18
|
+
readonly fk: RefKind;
|
|
19
|
+
/** Name being referenced. */
|
|
20
|
+
readonly n: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Extract every reference from one masked source.
|
|
24
|
+
*
|
|
25
|
+
* `lineAt` is resolved once per match through the line-start table, so a match
|
|
26
|
+
* that spans a line break reports the line it STARTS on - which is what a human
|
|
27
|
+
* clicking the row expects.
|
|
28
|
+
*/
|
|
29
|
+
export declare function extractRefs(source: string): readonly RawRef[];
|
|
30
|
+
/** True when the line is an import/export statement (its names are import bindings, not refs). */
|
|
31
|
+
export declare function isImportLine(lineText: string): boolean;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reference extraction (spec 5.8 layer 3).
|
|
3
|
+
*
|
|
4
|
+
* A reference is a NAME plus a kind plus a line. Resolution is deliberately not
|
|
5
|
+
* attempted here: a character scanner cannot know which `build` a file calls,
|
|
6
|
+
* and guessing "the nearest declaration" is how an index starts lying. Stage 5
|
|
7
|
+
* (LINK) resolves against imports and locals, and answers `none` when it cannot
|
|
8
|
+
* be sure.
|
|
9
|
+
*
|
|
10
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
11
|
+
*/
|
|
12
|
+
import { maskSource } from './mask.js';
|
|
13
|
+
/**
|
|
14
|
+
* The keyword blocklist. These are call-looking tokens that are not references
|
|
15
|
+
* to anything in the repository: reporting them would flood `refs.jsonl` with
|
|
16
|
+
* `if`, `for`, `catch` and make every "most referenced symbol" list wrong.
|
|
17
|
+
*/
|
|
18
|
+
const KEYWORDS = new Set([
|
|
19
|
+
'if',
|
|
20
|
+
'for',
|
|
21
|
+
'while',
|
|
22
|
+
'switch',
|
|
23
|
+
'catch',
|
|
24
|
+
'return',
|
|
25
|
+
'typeof',
|
|
26
|
+
'function',
|
|
27
|
+
'await',
|
|
28
|
+
'async',
|
|
29
|
+
'new',
|
|
30
|
+
'do',
|
|
31
|
+
'else',
|
|
32
|
+
'throw',
|
|
33
|
+
'delete',
|
|
34
|
+
'void',
|
|
35
|
+
'in',
|
|
36
|
+
'of',
|
|
37
|
+
'case',
|
|
38
|
+
'yield',
|
|
39
|
+
'super',
|
|
40
|
+
'import',
|
|
41
|
+
'export',
|
|
42
|
+
'require',
|
|
43
|
+
'constructor',
|
|
44
|
+
'get',
|
|
45
|
+
'set',
|
|
46
|
+
'static',
|
|
47
|
+
'readonly',
|
|
48
|
+
]);
|
|
49
|
+
/** The ordered rule table. `extends`/`implements` are matched before plain calls. */
|
|
50
|
+
const RULES = [
|
|
51
|
+
{ pattern: /\b(?:extends|implements)\s+([A-Za-z_$][\w$]*)/g, kind: 'extends' },
|
|
52
|
+
{ pattern: /\bnew\s+([A-Za-z_$][\w$]*)\s*[(<]/g, kind: 'new' },
|
|
53
|
+
{ pattern: /@([A-Za-z_$][\w$]*)/g, kind: 'decorator' },
|
|
54
|
+
{ pattern: /\b([a-z_$][\w$]*)\s*\(/g, kind: 'call' },
|
|
55
|
+
{ pattern: /\b([A-Z][\w$]*)\s*\(/g, kind: 'call' },
|
|
56
|
+
{ pattern: /:\s*([A-Za-z_$][\w$]*)\s*[;,)\]|=]/g, kind: 'type' },
|
|
57
|
+
{ pattern: /<([A-Z][\w$.]*)/g, kind: 'jsx' },
|
|
58
|
+
];
|
|
59
|
+
/** Lines whose text is skipped wholesale: import/export statements carry names, not references. */
|
|
60
|
+
const SKIP_LINE = /^\s*(import|export)\b[^=]*\bfrom\b|^\s*import\s*[\s{*]/;
|
|
61
|
+
/**
|
|
62
|
+
* Extract every reference from one masked source.
|
|
63
|
+
*
|
|
64
|
+
* `lineAt` is resolved once per match through the line-start table, so a match
|
|
65
|
+
* that spans a line break reports the line it STARTS on - which is what a human
|
|
66
|
+
* clicking the row expects.
|
|
67
|
+
*/
|
|
68
|
+
export function extractRefs(source) {
|
|
69
|
+
const { masked, lineStarts } = maskSource(source);
|
|
70
|
+
const out = [];
|
|
71
|
+
const seen = new Set();
|
|
72
|
+
for (const rule of RULES) {
|
|
73
|
+
rule.pattern.lastIndex = 0;
|
|
74
|
+
let match = rule.pattern.exec(masked);
|
|
75
|
+
while (match !== null) {
|
|
76
|
+
const name = match[1];
|
|
77
|
+
if (name && !KEYWORDS.has(name)) {
|
|
78
|
+
const line = lineOf(lineStarts, match.index);
|
|
79
|
+
const key = `${line}:${rule.kind}:${name}`;
|
|
80
|
+
if (!seen.has(key) && !isDeclarationLine(masked, match.index)) {
|
|
81
|
+
seen.add(key);
|
|
82
|
+
out.push({ ln: line, fk: rule.kind, n: name });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
match = rule.pattern.exec(masked);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
/** 1-based line of an offset (small binary search; duplicated here to keep the module standalone). */
|
|
91
|
+
function lineOf(lineStarts, offset) {
|
|
92
|
+
let low = 0;
|
|
93
|
+
let high = lineStarts.length - 1;
|
|
94
|
+
while (low < high) {
|
|
95
|
+
const mid = Math.ceil((low + high) / 2);
|
|
96
|
+
if (lineStarts[mid] <= offset)
|
|
97
|
+
low = mid;
|
|
98
|
+
else
|
|
99
|
+
high = mid - 1;
|
|
100
|
+
}
|
|
101
|
+
return low + 1;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* True when the match sits on a declaration rather than a use. `function foo(`
|
|
105
|
+
* and `const foo = (` would otherwise be recorded as a call to `foo` itself,
|
|
106
|
+
* which doubles every function's reference count.
|
|
107
|
+
*/
|
|
108
|
+
function isDeclarationLine(masked, offset) {
|
|
109
|
+
const start = masked.lastIndexOf('\n', offset) + 1;
|
|
110
|
+
const head = masked.slice(start, offset);
|
|
111
|
+
return /\b(function|class|interface|const|let|var|type|enum|def)\b/.test(head) && !/=/.test(head);
|
|
112
|
+
}
|
|
113
|
+
/** True when the line is an import/export statement (its names are import bindings, not refs). */
|
|
114
|
+
export function isImportLine(lineText) {
|
|
115
|
+
return SKIP_LINE.test(lineText);
|
|
116
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TS / TSX / JS declaration extraction on masked text (spec 5.8 layer 2).
|
|
3
|
+
*
|
|
4
|
+
* WHAT THIS IS NOT: a TypeScript parser. It never builds a tree, never resolves
|
|
5
|
+
* types, and never guesses at a construct it cannot see. It walks the masked
|
|
6
|
+
* source line by line with a brace-depth counter and a small scope stack, which
|
|
7
|
+
* is enough to answer the three questions the index exists for: what is declared,
|
|
8
|
+
* where does it start and end, and what is it declared inside.
|
|
9
|
+
*
|
|
10
|
+
* THE HONESTY RULE (spec R1): anything uncertain downgrades instead of guessing.
|
|
11
|
+
* A duplicate qualified name becomes `name#2` and marks the file `partial`; an
|
|
12
|
+
* unterminated comment or template marks it `partial`; a line the pattern table
|
|
13
|
+
* cannot classify is simply not a symbol. `partial` is a signal the model sees,
|
|
14
|
+
* so a wrong guess would be worse than a gap.
|
|
15
|
+
*
|
|
16
|
+
* ALL STATE IS LOCAL TO ONE CALL. The parser runs in a concurrency pool, so a
|
|
17
|
+
* module-level scope stack would let two files corrupt each other's `el` ranges.
|
|
18
|
+
*
|
|
19
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
20
|
+
*/
|
|
21
|
+
import type { ImportRow, IndexLang, SymbolRow } from '../types.js';
|
|
22
|
+
/** Everything the extractor produces for one file. */
|
|
23
|
+
export interface TsExtractResult {
|
|
24
|
+
/** Symbols in source order. */
|
|
25
|
+
readonly symbols: readonly SymbolRow[];
|
|
26
|
+
/** Imports in source order. */
|
|
27
|
+
readonly imports: readonly ImportRow[];
|
|
28
|
+
/** Line count as seen by the scanner. */
|
|
29
|
+
readonly lines: number;
|
|
30
|
+
/** True when the file is only partially understood. */
|
|
31
|
+
readonly partial: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Extract declarations and imports from one source file.
|
|
35
|
+
*
|
|
36
|
+
* `path` is written into every row so the artifact needs no second pass to
|
|
37
|
+
* attribute a row to a file.
|
|
38
|
+
*/
|
|
39
|
+
export declare function extractTs(source: string, lang: IndexLang, path: string): TsExtractResult;
|