ucode-agent 1.0.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.
- package/README.md +240 -0
- package/package.json +54 -0
- package/skills/build-app/SKILL.md +81 -0
- package/skills/code-review/SKILL.md +36 -0
- package/skills/debug/SKILL.md +47 -0
- package/skills/ui-ux/SKILL.md +237 -0
- package/skills/write-tests/SKILL.md +47 -0
- package/src/core/failure.js +70 -0
- package/src/core/history.js +278 -0
- package/src/core/loop.js +1146 -0
- package/src/core/provider.js +740 -0
- package/src/core/skills.js +165 -0
- package/src/core/window.js +127 -0
- package/src/tools/files.js +466 -0
- package/src/tools/index.js +394 -0
- package/src/tools/search.js +192 -0
- package/src/tools/shared.js +343 -0
- package/src/tools/shell.js +553 -0
- package/src/tools/web.js +96 -0
- package/src/ui/markdown.js +64 -0
- package/src/ui/plain.js +325 -0
- package/src/ui/screen.js +1067 -0
- package/src/ui/theme.js +256 -0
- package/ucode.js +118 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* shared.js — the plumbing every tool sits on.
|
|
3
|
+
*
|
|
4
|
+
* Path resolution, the session root, confirmation, output caps, filesystem
|
|
5
|
+
* errors worth reading, directory walking, glob matching, and the line diff
|
|
6
|
+
* that makes an edit visible. Nothing here is a tool; everything here is what
|
|
7
|
+
* the tools are built out of.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { promises as fs } from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { ToolFailure, Declined } from '../core/failure.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* How much of a result the model may see.
|
|
16
|
+
*
|
|
17
|
+
* File reads get a larger budget than everything else on purpose. Starving the
|
|
18
|
+
* model of the file it is about to edit costs far more — in wrong edits and in
|
|
19
|
+
* extra round trips — than the tokens it saves. A runaway build log still
|
|
20
|
+
* needs a firm lid, which is what the smaller cap is for.
|
|
21
|
+
*/
|
|
22
|
+
export const MAX_OUTPUT = Number(process.env.UCODE_MAX_TOOL_OUTPUT) || 12_000;
|
|
23
|
+
export const MAX_FILE_OUTPUT = Number(process.env.UCODE_MAX_FILE_OUTPUT) || 48_000;
|
|
24
|
+
|
|
25
|
+
export const READ_LINES = 600;
|
|
26
|
+
export const MAX_GLOB_HITS = 200;
|
|
27
|
+
export const MAX_GREP_HITS = 100;
|
|
28
|
+
|
|
29
|
+
/** Directories nobody means to search. */
|
|
30
|
+
export const SKIP = new Set([
|
|
31
|
+
'node_modules', '.git', '.hg', '.svn', 'dist', 'build', 'out',
|
|
32
|
+
'.next', '.nuxt', '.svelte-kit', '.cache', 'coverage', '__pycache__',
|
|
33
|
+
'.venv', 'venv', '.tox', '.pytest_cache', 'target', '.gradle', '.idea',
|
|
34
|
+
'vendor', 'Pods', '.terraform',
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// Session root
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
let root = process.cwd();
|
|
42
|
+
|
|
43
|
+
export function setRoot(dir) {
|
|
44
|
+
root = path.resolve(dir);
|
|
45
|
+
return root;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getRoot() {
|
|
49
|
+
return root;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// Confirmation
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The UI installs the real prompt here. Keeping it as an injection point means
|
|
58
|
+
* no tool owns a readline instance, and all of them stay testable.
|
|
59
|
+
*/
|
|
60
|
+
let asker = null;
|
|
61
|
+
|
|
62
|
+
export function setConfirm(fn) {
|
|
63
|
+
asker = fn;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function confirm(action, detail, risk = 'write') {
|
|
67
|
+
if (!asker) {
|
|
68
|
+
throw new ToolFailure({
|
|
69
|
+
kind: 'cannot_ask',
|
|
70
|
+
attempted: action,
|
|
71
|
+
failed: 'That needs the user to approve it, and there is no way to ask them from here.',
|
|
72
|
+
fix: 'Run ucode in a terminal so it can prompt before acting.',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (!(await asker({ action, detail, risk }))) throw new Declined(action);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// Paths
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
export function resolveIn(input, tool, argName = 'path') {
|
|
83
|
+
if (typeof input !== 'string' || !input.trim()) {
|
|
84
|
+
throw new ToolFailure({
|
|
85
|
+
kind: 'bad_args',
|
|
86
|
+
attempted: `running ${tool}`,
|
|
87
|
+
failed: `The "${argName}" argument was missing or was not a string.`,
|
|
88
|
+
fix: `Call ${tool} again with ${argName} set to a path relative to the project root.`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const abs = path.resolve(root, input.trim());
|
|
92
|
+
const rel = path.relative(root, abs);
|
|
93
|
+
const inside = rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
94
|
+
return {
|
|
95
|
+
abs,
|
|
96
|
+
inside,
|
|
97
|
+
// Short when it is in the project, fully spelled out when it is not — the
|
|
98
|
+
// display string is also the warning.
|
|
99
|
+
show: inside ? (rel === '' ? '.' : rel.split(path.sep).join('/')) : abs,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Reaching outside the folder ucode was started in always needs a yes. */
|
|
104
|
+
export async function guard(target, action) {
|
|
105
|
+
if (target.inside) return;
|
|
106
|
+
await confirm(action, `${target.abs}\nThat is outside this session's root (${root}).`, 'outside');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
// Results
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
|
|
113
|
+
export function cap(text, limit = MAX_OUTPUT) {
|
|
114
|
+
const s = String(text ?? '');
|
|
115
|
+
if (s.length <= limit) return s;
|
|
116
|
+
return `${s.slice(0, limit)}\n... [cut here — ${s.length - limit} more characters]`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Every tool resolves to this shape: what the model reads, plus a one-liner. */
|
|
120
|
+
export function result(content, summary, limit = MAX_OUTPUT) {
|
|
121
|
+
return { content: cap(content, limit), summary };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
// Filesystem errors, translated
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
export function fsFailure(err, attempted, target) {
|
|
129
|
+
const code = err?.code;
|
|
130
|
+
const common = { attempted, cause: err };
|
|
131
|
+
|
|
132
|
+
if (code === 'ENOENT') {
|
|
133
|
+
return new ToolFailure({
|
|
134
|
+
...common,
|
|
135
|
+
kind: 'not_found',
|
|
136
|
+
failed: `Nothing exists at ${target}.`,
|
|
137
|
+
fix: 'Check the path with list_dir or glob first. Paths are relative to the project root.',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (code === 'EACCES' || code === 'EPERM') {
|
|
141
|
+
return new ToolFailure({
|
|
142
|
+
...common,
|
|
143
|
+
kind: 'permission_denied',
|
|
144
|
+
failed: `The operating system refused access to ${target} (${code}).`,
|
|
145
|
+
fix: 'Check the permissions, or whether another program has the file open and locked.',
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
if (code === 'EISDIR') {
|
|
149
|
+
return new ToolFailure({
|
|
150
|
+
...common,
|
|
151
|
+
kind: 'is_directory',
|
|
152
|
+
failed: `${target} is a directory, not a file.`,
|
|
153
|
+
fix: 'Use list_dir to see inside it.',
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
if (code === 'ENOTDIR') {
|
|
157
|
+
return new ToolFailure({
|
|
158
|
+
...common,
|
|
159
|
+
kind: 'not_directory',
|
|
160
|
+
failed: `Something along the path ${target} is a file, not a directory.`,
|
|
161
|
+
fix: 'Re-check each segment of the path with list_dir.',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
return new ToolFailure({
|
|
165
|
+
...common,
|
|
166
|
+
kind: 'io_error',
|
|
167
|
+
failed: `${code ? `${code}: ` : ''}${err?.message ?? String(err)}`,
|
|
168
|
+
fix: 'Confirm the path exists and is readable, then try once more.',
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
// Text
|
|
174
|
+
// ---------------------------------------------------------------------------
|
|
175
|
+
|
|
176
|
+
export const looksBinary = (buf) => buf.includes(0);
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Split into lines the way a person counts them: a file ending in a newline
|
|
180
|
+
* has that many lines, not one more empty one at the bottom.
|
|
181
|
+
*/
|
|
182
|
+
export function toLines(text) {
|
|
183
|
+
const lines = String(text).split(/\r?\n/);
|
|
184
|
+
if (lines.length > 1 && lines[lines.length - 1] === '') lines.pop();
|
|
185
|
+
return lines;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function bytes(n) {
|
|
189
|
+
if (n < 1024) return `${n} B`;
|
|
190
|
+
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
|
|
191
|
+
return `${(n / 1024 / 1024).toFixed(1)} MB`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function clip(text, n = 60) {
|
|
195
|
+
const s = String(text ?? '');
|
|
196
|
+
return s.length > n ? `${s.slice(0, n)}…` : s;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// Diffs
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The changed region between two texts, with real line numbers on both sides.
|
|
205
|
+
*
|
|
206
|
+
* No diff algorithm is needed for what the tools actually do. Trimming the
|
|
207
|
+
* identical lines off the top and the bottom leaves exactly the block that
|
|
208
|
+
* changed, and the trimmed counts are the line numbers — removed lines
|
|
209
|
+
* numbered where they were in the old file, added lines numbered where they
|
|
210
|
+
* now are in the new one. Getting that right matters: a diff whose numbers
|
|
211
|
+
* are decorative is worse than a diff with no numbers, because it invites you
|
|
212
|
+
* to jump to a line that has nothing to do with the change.
|
|
213
|
+
*/
|
|
214
|
+
export function changedRegion(oldText, newText) {
|
|
215
|
+
const before = toLines(oldText);
|
|
216
|
+
const after = toLines(newText);
|
|
217
|
+
|
|
218
|
+
let head = 0;
|
|
219
|
+
while (head < before.length && head < after.length && before[head] === after[head]) head++;
|
|
220
|
+
|
|
221
|
+
let tail = 0;
|
|
222
|
+
while (
|
|
223
|
+
tail < before.length - head &&
|
|
224
|
+
tail < after.length - head &&
|
|
225
|
+
before[before.length - 1 - tail] === after[after.length - 1 - tail]
|
|
226
|
+
) tail++;
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
removed: before.slice(head, before.length - tail).map((text, i) => ({ n: head + i + 1, text })),
|
|
230
|
+
added: after.slice(head, after.length - tail).map((text, i) => ({ n: head + i + 1, text })),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Render a change for the screen as `-12| old` / `+12| new` rows.
|
|
236
|
+
*
|
|
237
|
+
* `offset` shifts both sides when the region being diffed is an excerpt rather
|
|
238
|
+
* than a whole file — an edit_file replacement knows the line it landed on, so
|
|
239
|
+
* the numbers shown are the file's numbers rather than the excerpt's.
|
|
240
|
+
* A row with no number is a note about what was left out, never part of the
|
|
241
|
+
* change itself.
|
|
242
|
+
*/
|
|
243
|
+
export function renderDiff({ removed, added }, { offset = 0, max = 16 } = {}) {
|
|
244
|
+
const out = [];
|
|
245
|
+
const room = Math.max(2, Math.floor(max / 2));
|
|
246
|
+
|
|
247
|
+
for (const line of removed.slice(0, room)) out.push(`-${line.n + offset}| ${line.text}`);
|
|
248
|
+
if (removed.length > room) out.push(`-… ${removed.length - room} more removed`);
|
|
249
|
+
|
|
250
|
+
for (const line of added.slice(0, room)) out.push(`+${line.n + offset}| ${line.text}`);
|
|
251
|
+
if (added.length > room) out.push(`+… ${added.length - room} more added`);
|
|
252
|
+
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The first lines of a brand-new file, so creating one shows something. */
|
|
257
|
+
export function renderNewFile(content, max = 16) {
|
|
258
|
+
const lines = toLines(content);
|
|
259
|
+
const out = lines.slice(0, max).map((text, i) => `+${i + 1}| ${text}`);
|
|
260
|
+
if (lines.length > max) out.push(`+… ${lines.length - max} more lines`);
|
|
261
|
+
return out;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ---------------------------------------------------------------------------
|
|
265
|
+
// Walking and globbing
|
|
266
|
+
// ---------------------------------------------------------------------------
|
|
267
|
+
|
|
268
|
+
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
269
|
+
|
|
270
|
+
/** A small glob: `**`, `*`, `?` and `{a,b}`. */
|
|
271
|
+
export function globToRegExp(pattern) {
|
|
272
|
+
let re = '';
|
|
273
|
+
let i = 0;
|
|
274
|
+
while (i < pattern.length) {
|
|
275
|
+
const c = pattern[i];
|
|
276
|
+
if (c === '*') {
|
|
277
|
+
if (pattern[i + 1] === '*') {
|
|
278
|
+
if (pattern[i + 2] === '/') { re += '(?:[^/]*/)*'; i += 3; } // spans directories
|
|
279
|
+
else { re += '.*'; i += 2; }
|
|
280
|
+
} else { re += '[^/]*'; i += 1; }
|
|
281
|
+
} else if (c === '?') {
|
|
282
|
+
re += '[^/]'; i += 1;
|
|
283
|
+
} else if (c === '{') {
|
|
284
|
+
const end = pattern.indexOf('}', i);
|
|
285
|
+
if (end === -1) { re += '\\{'; i += 1; }
|
|
286
|
+
else {
|
|
287
|
+
const alts = pattern.slice(i + 1, end).split(',').map((a) => escapeRe(a.trim()));
|
|
288
|
+
re += `(?:${alts.join('|')})`;
|
|
289
|
+
i = end + 1;
|
|
290
|
+
}
|
|
291
|
+
} else {
|
|
292
|
+
re += escapeRe(c); i += 1;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
// Windows paths are case-insensitive, so matching should be too.
|
|
296
|
+
return new RegExp(`^${re}$`, process.platform === 'win32' ? 'i' : '');
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Every file under a directory, as posix-style relative paths.
|
|
301
|
+
* Build and vendor folders are skipped unless the caller says otherwise, and
|
|
302
|
+
* an unreadable directory is stepped over rather than aborting the walk.
|
|
303
|
+
*/
|
|
304
|
+
export async function walk(base, { includeSkipped = false, limit = 20_000 } = {}) {
|
|
305
|
+
const files = [];
|
|
306
|
+
let level = [''];
|
|
307
|
+
|
|
308
|
+
// A level of the tree at a time, with every directory on that level read at
|
|
309
|
+
// once. Reading them one after another spends most of a large walk waiting
|
|
310
|
+
// on the disk for directories that did not depend on each other.
|
|
311
|
+
while (level.length && files.length < limit) {
|
|
312
|
+
const next = [];
|
|
313
|
+
for (let i = 0; i < level.length && files.length < limit; i += WALK_WIDTH) {
|
|
314
|
+
const slice = level.slice(i, i + WALK_WIDTH);
|
|
315
|
+
const listed = await Promise.all(slice.map((relDir) =>
|
|
316
|
+
fs.readdir(path.join(base, relDir), { withFileTypes: true }).then(
|
|
317
|
+
(entries) => ({ relDir, entries }),
|
|
318
|
+
() => ({ relDir, entries: [] }) // unreadable: step over it
|
|
319
|
+
)
|
|
320
|
+
));
|
|
321
|
+
|
|
322
|
+
// Results are consumed in the order the directories were queued, so the
|
|
323
|
+
// walk comes out the same every time however the reads finished.
|
|
324
|
+
for (const { relDir, entries } of listed) {
|
|
325
|
+
for (const entry of entries) {
|
|
326
|
+
const rel = relDir ? `${relDir}/${entry.name}` : entry.name;
|
|
327
|
+
if (entry.isDirectory()) {
|
|
328
|
+
if (!includeSkipped && SKIP.has(entry.name)) continue;
|
|
329
|
+
next.push(rel);
|
|
330
|
+
} else if (entry.isFile()) {
|
|
331
|
+
files.push(rel);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
level = next;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return files.slice(0, limit);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** How many directories, or files, are read at the same time. */
|
|
343
|
+
export const WALK_WIDTH = 32;
|