@pal-code/core 0.0.10

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.
@@ -0,0 +1,177 @@
1
+ /**
2
+ * repo-map — lightweight structural overview of source files.
3
+ * Extracts top-level declarations using @ast-grep/napi with regex fallback.
4
+ */
5
+
6
+ import {readFile, readdir} from 'fs/promises';
7
+ import {join, relative, extname} from 'path';
8
+
9
+ interface RepoMapEntry {
10
+ file: string;
11
+ declarations: string[];
12
+ }
13
+
14
+ // Patterns for top-level declarations (ast-grep pattern syntax)
15
+ const PATTERNS: Record<string, string[]> = {
16
+ typescript: [
17
+ 'function $NAME($$$)',
18
+ 'export function $NAME($$$)',
19
+ 'class $NAME { $$$ }',
20
+ 'export class $NAME { $$$ }',
21
+ 'interface $NAME { $$$ }',
22
+ 'export interface $NAME { $$$ }',
23
+ 'type $NAME = $$$',
24
+ 'export type $NAME = $$$',
25
+ 'const $NAME = ($$$) => $$$',
26
+ 'export const $NAME = ($$$) => $$$',
27
+ 'export default function $NAME($$$)',
28
+ 'enum $NAME { $$$ }',
29
+ 'export enum $NAME { $$$ }',
30
+ ],
31
+ javascript: [
32
+ 'function $NAME($$$)',
33
+ 'export function $NAME($$$)',
34
+ 'class $NAME { $$$ }',
35
+ 'export class $NAME { $$$ }',
36
+ 'const $NAME = ($$$) => $$$',
37
+ 'export const $NAME = ($$$) => $$$',
38
+ 'export default function $NAME($$$)',
39
+ 'module.exports = $EXPR',
40
+ ],
41
+ };
42
+
43
+ const SCAN_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs']);
44
+ const IGNORE_DIRS = new Set([
45
+ 'node_modules',
46
+ '.git',
47
+ 'dist',
48
+ 'build',
49
+ '.zig-cache',
50
+ '__pycache__',
51
+ '.next',
52
+ '.turbo',
53
+ ]);
54
+
55
+ // Regex fallback when @ast-grep/napi unavailable
56
+ const REGEX_DECLARATIONS: Array<{pattern: RegExp; nameGroup: number}> = [
57
+ {pattern: /(?:export\s+)?(?:async\s+)?function\s+(\w+)/g, nameGroup: 1},
58
+ {pattern: /(?:export\s+)?class\s+(\w+)/g, nameGroup: 1},
59
+ {pattern: /(?:export\s+)?interface\s+(\w+)/g, nameGroup: 1},
60
+ {pattern: /(?:export\s+)?type\s+(\w+)/g, nameGroup: 1},
61
+ {pattern: /(?:export\s+)?enum\s+(\w+)/g, nameGroup: 1},
62
+ {pattern: /(?:export\s+)?const\s+(\w+)\s*=\s*(?:\(|async\s*\()/g, nameGroup: 1},
63
+ ];
64
+
65
+ async function extractDeclarations(filePath: string): Promise<string[]> {
66
+ const content = await readFile(filePath, 'utf-8');
67
+ const ext = extname(filePath);
68
+ const declarations: string[] = [];
69
+
70
+ try {
71
+ const sg = await import('@ast-grep/napi');
72
+ const LangMap: Record<string, (typeof sg.Lang)[keyof typeof sg.Lang]> = {
73
+ '.ts': sg.Lang.TypeScript,
74
+ '.tsx': sg.Lang.Tsx,
75
+ '.js': sg.Lang.JavaScript,
76
+ '.jsx': sg.Lang.Tsx,
77
+ };
78
+ const lang = LangMap[ext] ?? sg.Lang.TypeScript;
79
+ const patterns =
80
+ PATTERNS[ext === '.tsx' || ext === '.jsx' ? 'typescript' : 'javascript'] ??
81
+ PATTERNS.typescript;
82
+
83
+ const root = sg.parse(lang, content);
84
+ const seen = new Set<string>();
85
+
86
+ for (const pattern of patterns) {
87
+ try {
88
+ const matches = root.root().findAll(pattern);
89
+ for (const m of matches) {
90
+ const name = m.getMatch('NAME')?.text();
91
+ if (name && !seen.has(name)) {
92
+ seen.add(name);
93
+ declarations.push(name);
94
+ }
95
+ }
96
+ } catch {
97
+ /* pattern may not match this language */
98
+ }
99
+ }
100
+ } catch {
101
+ // Regex fallback
102
+ for (const re of REGEX_DECLARATIONS) {
103
+ let match: RegExpExecArray | null;
104
+ const regex = new RegExp(re.pattern.source, re.pattern.flags);
105
+ while ((match = regex.exec(content)) !== null) {
106
+ const name = match[re.nameGroup];
107
+ if (name && !declarations.includes(name)) {
108
+ declarations.push(name);
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ return declarations;
115
+ }
116
+
117
+ export async function buildRepoMap(
118
+ dir: string,
119
+ maxTokens: number = 1000,
120
+ /** `.pal.json` ignore predicate — absolute path in, "skip" out. */
121
+ isIgnored?: (absPath: string) => boolean,
122
+ ): Promise<string> {
123
+ const entries: RepoMapEntry[] = [];
124
+
125
+ async function walk(current: string, depth: number) {
126
+ if (depth > 30) return; // safety net; symlinks are already not followed
127
+ let items;
128
+ try {
129
+ items = await readdir(current, {withFileTypes: true});
130
+ } catch {
131
+ return; // unreadable dir
132
+ }
133
+ for (const item of items) {
134
+ if (IGNORE_DIRS.has(item.name)) continue;
135
+ const fullPath = join(current, item.name);
136
+ if (isIgnored?.(fullPath)) continue;
137
+ if (item.isDirectory()) {
138
+ await walk(fullPath, depth + 1);
139
+ } else if (SCAN_EXTENSIONS.has(extname(item.name))) {
140
+ try {
141
+ const declarations = await extractDeclarations(fullPath);
142
+ if (declarations.length > 0) {
143
+ entries.push({
144
+ file: relative(dir, fullPath).replace(/\\/g, '/'),
145
+ declarations,
146
+ });
147
+ }
148
+ } catch {
149
+ /* skip unreadable files */
150
+ }
151
+ }
152
+ }
153
+ }
154
+
155
+ await walk(dir, 0);
156
+
157
+ // Format as tree
158
+ const lines: string[] = [];
159
+ let tokenEstimate = 0;
160
+
161
+ for (const entry of entries) {
162
+ const fileLine = `📄 ${entry.file}`;
163
+ if (tokenEstimate + Math.ceil(fileLine.length / 4) > maxTokens) break;
164
+ lines.push(fileLine);
165
+ tokenEstimate += Math.ceil(fileLine.length / 4);
166
+
167
+ for (const decl of entry.declarations) {
168
+ const declLine = ` └─ ${decl}`;
169
+ if (tokenEstimate + Math.ceil(declLine.length / 4) > maxTokens) break;
170
+ lines.push(declLine);
171
+ tokenEstimate += Math.ceil(declLine.length / 4);
172
+ }
173
+ }
174
+
175
+ if (lines.length === 0) return 'No source files found.';
176
+ return lines.join('\n');
177
+ }
@@ -0,0 +1,2 @@
1
+ export {SessionStore} from './store.js';
2
+ export type {Session} from './types.js';