@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.
- package/package.json +38 -0
- package/src/agent/events.ts +36 -0
- package/src/agent/fallback.ts +344 -0
- package/src/agent/index.ts +35 -0
- package/src/agent/roles.ts +193 -0
- package/src/agent/run.ts +713 -0
- package/src/agent/scope.ts +34 -0
- package/src/agent/todos.ts +66 -0
- package/src/compaction.ts +243 -0
- package/src/config.ts +387 -0
- package/src/extensions.ts +234 -0
- package/src/index.ts +157 -0
- package/src/logger.ts +54 -0
- package/src/provider/auth.ts +153 -0
- package/src/provider/complete.ts +63 -0
- package/src/provider/index.ts +6 -0
- package/src/provider/openrouter.ts +304 -0
- package/src/provider/pick-model.ts +43 -0
- package/src/provider/registry.ts +222 -0
- package/src/provider/types.ts +61 -0
- package/src/registry.ts +63 -0
- package/src/repo-map.ts +177 -0
- package/src/session/index.ts +2 -0
- package/src/session/store.ts +677 -0
- package/src/session/types.ts +72 -0
- package/src/snapshots.ts +107 -0
- package/src/tools/definitions.ts +365 -0
- package/src/tools/executor.ts +1364 -0
- package/src/tools/index.ts +5 -0
- package/src/tools/search.ts +201 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-platform file walking + content search.
|
|
3
|
+
*
|
|
4
|
+
* Used as a fallback when `rg` (ripgrep) is unavailable — a stock Windows box
|
|
5
|
+
* has neither `rg` nor GNU `grep`, so the agent must still be able to search.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {readdir, readFile} from 'fs/promises';
|
|
9
|
+
import {resolve, join, relative, sep} from 'path';
|
|
10
|
+
|
|
11
|
+
/** Directories that are almost never worth searching. */
|
|
12
|
+
export const DEFAULT_IGNORE = new Set([
|
|
13
|
+
'node_modules',
|
|
14
|
+
'.git',
|
|
15
|
+
'dist',
|
|
16
|
+
'build',
|
|
17
|
+
'.next',
|
|
18
|
+
'coverage',
|
|
19
|
+
'.zig-cache',
|
|
20
|
+
'zig-out',
|
|
21
|
+
'.cache',
|
|
22
|
+
'target',
|
|
23
|
+
'__pycache__',
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
/** Skip files larger than this (bytes) — avoids loading binaries/bundles. */
|
|
27
|
+
const MAX_FILE_BYTES = 2_000_000;
|
|
28
|
+
|
|
29
|
+
/** Hard cap on files visited, so a runaway tree can't hang the agent. */
|
|
30
|
+
const MAX_FILES_VISITED = 50_000;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Walk a directory tree depth-first, yielding file paths.
|
|
34
|
+
*
|
|
35
|
+
* `isIgnored` receives an absolute path and should return true to skip it.
|
|
36
|
+
* Symlinks are never followed (`Dirent` uses lstat semantics, so a linked
|
|
37
|
+
* directory is not reported as a directory) — cycles are impossible.
|
|
38
|
+
*/
|
|
39
|
+
export async function* walkFiles(
|
|
40
|
+
root: string,
|
|
41
|
+
ignore: Set<string> = DEFAULT_IGNORE,
|
|
42
|
+
isIgnored?: (absPath: string) => boolean,
|
|
43
|
+
): AsyncGenerator<string> {
|
|
44
|
+
const stack: string[] = [root];
|
|
45
|
+
let visited = 0;
|
|
46
|
+
|
|
47
|
+
while (stack.length > 0) {
|
|
48
|
+
const dir = stack.pop()!;
|
|
49
|
+
let entries;
|
|
50
|
+
try {
|
|
51
|
+
entries = await readdir(dir, {withFileTypes: true});
|
|
52
|
+
} catch {
|
|
53
|
+
continue; // unreadable dir (permissions) — skip
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for (const entry of entries) {
|
|
57
|
+
if (visited >= MAX_FILES_VISITED) return;
|
|
58
|
+
const full = join(dir, entry.name);
|
|
59
|
+
if (isIgnored?.(full)) continue;
|
|
60
|
+
if (entry.isDirectory()) {
|
|
61
|
+
if (ignore.has(entry.name)) continue;
|
|
62
|
+
stack.push(full);
|
|
63
|
+
} else if (entry.isFile()) {
|
|
64
|
+
visited++;
|
|
65
|
+
yield full;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Convert a glob (`**\/*.ts`, `src/*.js`) into an anchored RegExp. */
|
|
72
|
+
export function globToRegExp(glob: string): RegExp {
|
|
73
|
+
const normalized = glob.split(sep).join('/');
|
|
74
|
+
let out = '';
|
|
75
|
+
for (let i = 0; i < normalized.length; i++) {
|
|
76
|
+
const ch = normalized[i];
|
|
77
|
+
if (ch === '*') {
|
|
78
|
+
if (normalized[i + 1] === '*') {
|
|
79
|
+
// `**/` spans directories; a trailing `**` matches everything.
|
|
80
|
+
if (normalized[i + 2] === '/') {
|
|
81
|
+
out += '(?:.*/)?';
|
|
82
|
+
i += 2;
|
|
83
|
+
} else {
|
|
84
|
+
out += '.*';
|
|
85
|
+
i += 1;
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
out += '[^/]*';
|
|
89
|
+
}
|
|
90
|
+
} else if (ch === '?') {
|
|
91
|
+
out += '[^/]';
|
|
92
|
+
} else if ('+.^${}()|[]\\'.includes(ch)) {
|
|
93
|
+
out += '\\' + ch;
|
|
94
|
+
} else {
|
|
95
|
+
out += ch;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return new RegExp(out + '$');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Path relative to cwd, always using forward slashes for stable display. */
|
|
102
|
+
function relPath(p: string): string {
|
|
103
|
+
return relative(process.cwd(), p).split(sep).join('/');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface GrepOptions {
|
|
107
|
+
pattern: string;
|
|
108
|
+
path: string;
|
|
109
|
+
include?: string;
|
|
110
|
+
limit: number;
|
|
111
|
+
/** Extra predicate so callers can layer `.pal.json` ignore patterns on top. */
|
|
112
|
+
isIgnored?: (absPath: string) => boolean;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Pure-JS recursive content search (regex per line). */
|
|
116
|
+
export async function grepJS(opts: GrepOptions): Promise<string> {
|
|
117
|
+
let re: RegExp;
|
|
118
|
+
try {
|
|
119
|
+
re = new RegExp(opts.pattern);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
return `Invalid regex: ${err instanceof Error ? err.message : String(err)}`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const includeRe = opts.include ? globToRegExp(opts.include) : null;
|
|
125
|
+
const root = resolve(opts.path);
|
|
126
|
+
const out: string[] = [];
|
|
127
|
+
let matchCount = 0;
|
|
128
|
+
let currentFile = '';
|
|
129
|
+
const hardCap = opts.limit * 5;
|
|
130
|
+
|
|
131
|
+
for await (const file of walkFiles(root, DEFAULT_IGNORE, opts.isIgnored)) {
|
|
132
|
+
const rel = relPath(file);
|
|
133
|
+
if (includeRe && !includeRe.test(rel)) continue;
|
|
134
|
+
|
|
135
|
+
let content: string;
|
|
136
|
+
try {
|
|
137
|
+
content = await readFile(file, 'utf-8');
|
|
138
|
+
} catch {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (content.length > MAX_FILE_BYTES) continue;
|
|
142
|
+
|
|
143
|
+
const lines = content.split('\n');
|
|
144
|
+
for (let i = 0; i < lines.length; i++) {
|
|
145
|
+
if (!re.test(lines[i])) continue;
|
|
146
|
+
if (rel !== currentFile) {
|
|
147
|
+
if (out.length > 0) out.push('');
|
|
148
|
+
currentFile = rel;
|
|
149
|
+
out.push(rel + ':');
|
|
150
|
+
}
|
|
151
|
+
if (matchCount < opts.limit) {
|
|
152
|
+
out.push(` Line ${i + 1}: ${lines[i].trimEnd()}`);
|
|
153
|
+
}
|
|
154
|
+
matchCount++;
|
|
155
|
+
if (matchCount >= hardCap) break;
|
|
156
|
+
}
|
|
157
|
+
if (matchCount >= hardCap) break;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (matchCount === 0) return 'No matches found.';
|
|
161
|
+
const trunc =
|
|
162
|
+
matchCount > opts.limit
|
|
163
|
+
? `\n\n[${matchCount} matches found. Showing first ${opts.limit}. Refine the pattern or raise limit.]`
|
|
164
|
+
: '';
|
|
165
|
+
return `Found ${matchCount} match${matchCount !== 1 ? 'es' : ''}${trunc}\n\n${out.join('\n')}`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface GlobOptions {
|
|
169
|
+
pattern: string;
|
|
170
|
+
path: string;
|
|
171
|
+
limit: number;
|
|
172
|
+
/** Extra predicate so callers can layer `.pal.json` ignore patterns on top. */
|
|
173
|
+
isIgnored?: (absPath: string) => boolean;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Pure-JS file discovery by glob pattern. */
|
|
177
|
+
export async function globJS(opts: GlobOptions): Promise<string> {
|
|
178
|
+
const re = globToRegExp(opts.pattern);
|
|
179
|
+
const root = resolve(opts.path);
|
|
180
|
+
const files: string[] = [];
|
|
181
|
+
let hitLimit = false;
|
|
182
|
+
|
|
183
|
+
for await (const file of walkFiles(root, DEFAULT_IGNORE, opts.isIgnored)) {
|
|
184
|
+
const rel = relPath(file);
|
|
185
|
+
const abs = file.split(sep).join('/');
|
|
186
|
+
if (re.test(rel) || re.test(abs)) {
|
|
187
|
+
files.push(rel);
|
|
188
|
+
if (files.length > opts.limit) {
|
|
189
|
+
hitLimit = true;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (files.length === 0) return 'No files found matching pattern.';
|
|
196
|
+
const display = files.slice(0, opts.limit);
|
|
197
|
+
const trunc = hitLimit
|
|
198
|
+
? `\n\n[Showing first ${opts.limit} of ${files.length}+ results. Refine the pattern or raise limit.]`
|
|
199
|
+
: '';
|
|
200
|
+
return `Found ${hitLimit ? `${files.length}+` : files.length} file${files.length !== 1 ? 's' : ''}${trunc}\n\n${display.join('\n')}`;
|
|
201
|
+
}
|