@qvac/harness 1.0.0-rc.21 → 1.0.0-rc.23
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/CHANGELOG.md +103 -0
- package/README.md +2 -0
- package/dist-lib/index.d.ts +4 -1
- package/dist-lib/index.js +12 -4
- package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +3 -1
- package/dist-lib/lib/capabilities/completion-engine/tool-outcome.js +2 -4
- package/dist-lib/lib/capabilities/tool-turns/answered.d.ts +15 -0
- package/dist-lib/lib/capabilities/tool-turns/answered.js +16 -0
- package/dist-lib/lib/capabilities/tool-turns.d.ts +2 -0
- package/dist-lib/lib/capabilities/tool-turns.js +1 -0
- package/dist-lib/lib/contract/completion.d.ts +2 -1
- package/dist-lib/lib/contract/drive.d.ts +24 -0
- package/dist-lib/lib/contract/root-search.d.ts +34 -0
- package/dist-lib/lib/contract/root-search.js +1 -0
- package/dist-lib/lib/contract/run.d.ts +2 -0
- package/dist-lib/lib/helpers/root-search.d.ts +3 -0
- package/dist-lib/lib/helpers/root-search.js +74 -0
- package/dist-lib/lib/machine/completion/actions/tools.js +7 -1
- package/dist-lib/lib/machine/completion/metrics.js +3 -1
- package/dist-lib/lib/machine/completion/types.d.ts +2 -0
- package/dist-lib/lib/models/model-catalog-constants.d.ts +1 -0
- package/dist-lib/lib/models/model-catalog-constants.js +5 -1
- package/dist-lib/lib/models/model-catalog.d.ts +10 -3
- package/dist-lib/lib/models/model-catalog.js +14 -8
- package/dist-lib/lib/models/model-delete.js +3 -0
- package/dist-lib/lib/models/model-list.d.ts +2 -1
- package/dist-lib/lib/models/model-list.js +5 -1
- package/dist-lib/lib/models/model-pool.d.ts +5 -0
- package/dist-lib/lib/models/model-pool.js +16 -4
- package/dist-lib/lib/plugins/registry.d.ts +2 -0
- package/dist-lib/lib/residency/index.js +9 -6
- package/dist-lib/lib/spawn.d.ts +9 -0
- package/dist-lib/lib/tools/files/glob.js +71 -10
- package/dist-lib/lib/tools/files/grep.js +65 -22
- package/dist-lib/lib/tools/files/read-file.js +120 -76
- package/dist-lib/lib/tools/fs/engine.d.ts +9 -0
- package/dist-lib/lib/tools/fs/engine.js +28 -0
- package/dist-lib/lib/tools/fs/suggest.d.ts +8 -0
- package/dist-lib/lib/tools/fs/suggest.js +110 -0
- package/dist-lib/lib/tools/fs/tool-path.d.ts +3 -0
- package/dist-lib/lib/tools/fs/tool-path.js +13 -0
- package/dist-lib/lib/tools/fs/walk.d.ts +3 -0
- package/dist-lib/lib/tools/fs/walk.js +20 -20
- package/dist-lib/lib/tools/toolsets.d.ts +1 -1
- package/dist-lib/lib/tools/toolsets.js +2 -0
- package/dist-lib/plugins/charts/chart-tool.d.ts +8 -0
- package/dist-lib/plugins/charts/chart-tool.js +175 -0
- package/dist-lib/plugins/charts/index.d.ts +16 -0
- package/dist-lib/plugins/charts/index.js +21 -0
- package/dist-lib/plugins/diagrams/draw.d.ts +10 -0
- package/dist-lib/plugins/diagrams/draw.js +90 -0
- package/dist-lib/plugins/diagrams/index.d.ts +13 -0
- package/dist-lib/plugins/diagrams/index.js +15 -0
- package/dist-lib/plugins/diagrams/render.d.ts +16 -0
- package/dist-lib/plugins/diagrams/render.js +285 -0
- package/dist-lib/plugins/ripgrep/index.d.ts +15 -0
- package/dist-lib/plugins/ripgrep/index.js +28 -0
- package/dist-lib/plugins/sandbox-windows/prebuilds/win32-arm64/qvac-sandbox.exe +0 -0
- package/dist-lib/plugins/sandbox-windows/prebuilds/win32-x64/qvac-sandbox.exe +0 -0
- package/package.json +28 -2
- package/worker/harness-sidecar.mjs +5 -0
|
@@ -2,25 +2,35 @@ import b4a from 'b4a';
|
|
|
2
2
|
import { errorMessage } from '../../guards.js';
|
|
3
3
|
import { documentText, isDocument } from '../fs/document-text.js';
|
|
4
4
|
import { createLocalRoots } from '../fs/local.js';
|
|
5
|
-
import { defaultRealpath, resolveKey } from '../fs/scope.js';
|
|
6
|
-
import { anchorOf, positiveInt, resolveRooted, rootedArgs, rootedPath, stringArg } from '../fs/tool-path.js';
|
|
5
|
+
import { callDenyRoots, defaultRealpath, resolveKey } from '../fs/scope.js';
|
|
6
|
+
import { anchorOf, positiveInt, resolveRooted, rootedArgs, rootedPath, signedLine, stringArg } from '../fs/tool-path.js';
|
|
7
|
+
import { missingFile } from '../fs/suggest.js';
|
|
7
8
|
const MAX_DOCUMENT_BYTES = 8 * 1024 * 1024;
|
|
8
9
|
const READ_LINES = 400;
|
|
9
10
|
const READ_CHARS = 20_000;
|
|
11
|
+
const LINE_CHARS = 2000;
|
|
12
|
+
const TAIL_LINES = 50;
|
|
13
|
+
const COUNT_BYTES = 8 * 1024 * 1024;
|
|
14
|
+
const COUNT_MIB = COUNT_BYTES / 1024 / 1024;
|
|
10
15
|
const BINARY_PROBE = 1024;
|
|
11
|
-
const LINE_BYTES = 3 *
|
|
16
|
+
const LINE_BYTES = 3 * LINE_CHARS + 4;
|
|
12
17
|
export function createReadFileTool({ realpath = defaultRealpath, deniedRoots } = {}) {
|
|
13
18
|
return {
|
|
14
19
|
name: 'read_file',
|
|
15
20
|
toolset: 'files',
|
|
16
21
|
description: 'Read one file from the shared folder. Lines come back numbered ("12| text"). For a long file, ' +
|
|
17
|
-
'read a range with startLine/endLine
|
|
22
|
+
'read a range with startLine/endLine. A line over 2000 chars is cut; the cut names its full ' +
|
|
23
|
+
'length (bytes for a text file, chars for a document). PDF, Word and PowerPoint files come ' +
|
|
24
|
+
'back as their text. ' +
|
|
18
25
|
'Use this, not exec, for files in the shared folder.',
|
|
19
26
|
parameters: {
|
|
20
27
|
type: 'object',
|
|
21
28
|
properties: {
|
|
22
29
|
path: { type: 'string', description: 'File path, relative to the shared folder' },
|
|
23
|
-
startLine: {
|
|
30
|
+
startLine: {
|
|
31
|
+
type: 'integer',
|
|
32
|
+
description: 'First line to read (1-based, optional); negative counts from the end, -50 is the last 50 lines'
|
|
33
|
+
},
|
|
24
34
|
endLine: { type: 'integer', description: 'Last line to read (optional)' }
|
|
25
35
|
},
|
|
26
36
|
required: ['path']
|
|
@@ -46,16 +56,23 @@ export function createReadFileTool({ realpath = defaultRealpath, deniedRoots } =
|
|
|
46
56
|
if ('error' in resolved) {
|
|
47
57
|
return resolved;
|
|
48
58
|
}
|
|
59
|
+
const missing = async () => missingFile({
|
|
60
|
+
requested,
|
|
61
|
+
resolved,
|
|
62
|
+
anchor: anchorOf(ctx),
|
|
63
|
+
deny: await callDenyRoots(deniedRoots, ctx?.policy, realpath)
|
|
64
|
+
});
|
|
49
65
|
try {
|
|
50
66
|
const range = {
|
|
51
|
-
start:
|
|
67
|
+
start: signedLine(args.startLine),
|
|
52
68
|
end: positiveInt(args.endLine)
|
|
53
69
|
};
|
|
70
|
+
const target = { resolved, requested, missing };
|
|
54
71
|
if (isDocument(resolved.key)) {
|
|
55
|
-
const text = await documentOf(
|
|
72
|
+
const text = await documentOf(target, ctx);
|
|
56
73
|
return typeof text === 'string' ? numbered(text, range) : text;
|
|
57
74
|
}
|
|
58
|
-
return await textRange(
|
|
75
|
+
return await textRange(target, range);
|
|
59
76
|
}
|
|
60
77
|
catch (err) {
|
|
61
78
|
return { error: errorMessage(err) };
|
|
@@ -63,7 +80,7 @@ export function createReadFileTool({ realpath = defaultRealpath, deniedRoots } =
|
|
|
63
80
|
}
|
|
64
81
|
};
|
|
65
82
|
}
|
|
66
|
-
async function documentOf(resolved, requested, ctx) {
|
|
83
|
+
async function documentOf({ resolved, requested, missing }, ctx) {
|
|
67
84
|
const { drive } = resolved.root;
|
|
68
85
|
const entry = await drive.entry(resolved.key);
|
|
69
86
|
if (entry?.value.blob && entry.value.blob.byteLength > MAX_DOCUMENT_BYTES) {
|
|
@@ -73,7 +90,7 @@ async function documentOf(resolved, requested, ctx) {
|
|
|
73
90
|
}
|
|
74
91
|
const bytes = entry ? await drive.get(resolved.key) : null;
|
|
75
92
|
if (bytes === null) {
|
|
76
|
-
return
|
|
93
|
+
return missing();
|
|
77
94
|
}
|
|
78
95
|
if (bytes.byteLength > MAX_DOCUMENT_BYTES) {
|
|
79
96
|
return {
|
|
@@ -82,39 +99,51 @@ async function documentOf(resolved, requested, ctx) {
|
|
|
82
99
|
}
|
|
83
100
|
return documentText(resolved.key, bytes, { ocr: ctx?.ocr, signal: ctx?.signal });
|
|
84
101
|
}
|
|
85
|
-
function numbered(text, { start, end }) {
|
|
102
|
+
function numbered(text, { start = 1, end }) {
|
|
86
103
|
const lines = text.split('\n');
|
|
87
104
|
if (lines.length > 1 && lines[lines.length - 1] === '') {
|
|
88
105
|
lines.pop();
|
|
89
106
|
}
|
|
90
107
|
const total = lines.length;
|
|
91
|
-
const first =
|
|
92
|
-
const
|
|
108
|
+
const first = tailStart(start, total);
|
|
109
|
+
const rows = [];
|
|
93
110
|
let chars = 0;
|
|
94
|
-
let cut = false;
|
|
95
111
|
for (let n = first; n <= Math.min(total, end ?? first + READ_LINES - 1); n++) {
|
|
96
|
-
const row = `${n}| ${lines[n - 1]}`;
|
|
112
|
+
const row = `${n}| ${clip(lines[n - 1] ?? '')}`;
|
|
97
113
|
if (chars + row.length > READ_CHARS) {
|
|
98
|
-
cut = out.length === 0;
|
|
99
|
-
if (cut) {
|
|
100
|
-
out.push(row.slice(0, READ_CHARS));
|
|
101
|
-
}
|
|
102
114
|
break;
|
|
103
115
|
}
|
|
104
|
-
|
|
116
|
+
rows.push(row);
|
|
105
117
|
chars += row.length + 1;
|
|
106
118
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
119
|
+
return { content: present({ first, rows, total, pastEnd: start > total ? start : undefined }) };
|
|
120
|
+
}
|
|
121
|
+
function tailStart(start, total) {
|
|
122
|
+
if (start < 0) {
|
|
123
|
+
return Math.max(1, total + start + 1);
|
|
124
|
+
}
|
|
125
|
+
return start > total ? Math.max(1, total - TAIL_LINES + 1) : start;
|
|
126
|
+
}
|
|
127
|
+
function present({ first, rows, total, pastEnd }) {
|
|
128
|
+
const last = first + rows.length - 1;
|
|
129
|
+
const shown = pastEnd === undefined
|
|
130
|
+
? `lines ${first}-${last} of ${total ?? 'unknown'}`
|
|
131
|
+
: `startLine ${pastEnd} is past the end; the file has ${total} lines`;
|
|
132
|
+
const header = total === undefined || last < total ? `${shown}; continue with startLine ${last + 1}` : shown;
|
|
133
|
+
return `${header}\n${rows.join('\n')}`;
|
|
134
|
+
}
|
|
135
|
+
function clip(text, bytes) {
|
|
136
|
+
const line = text.endsWith('\r') ? text.slice(0, -1) : text;
|
|
137
|
+
if (line.length <= LINE_CHARS) {
|
|
138
|
+
return line;
|
|
139
|
+
}
|
|
140
|
+
const all = bytes === undefined ? `${line.length} chars` : `${bytes} bytes`;
|
|
141
|
+
return `${line.slice(0, LINE_CHARS)}… (line cut at ${LINE_CHARS} chars; ${all} in all)`;
|
|
113
142
|
}
|
|
114
|
-
async function textRange(resolved, requested, range) {
|
|
143
|
+
async function textRange({ resolved, requested, missing }, range) {
|
|
115
144
|
const { drive } = resolved.root;
|
|
116
145
|
if (!(await drive.entry(resolved.key))) {
|
|
117
|
-
return
|
|
146
|
+
return missing();
|
|
118
147
|
}
|
|
119
148
|
const reader = new TextRange(range);
|
|
120
149
|
let probed = 0;
|
|
@@ -124,65 +153,83 @@ async function textRange(resolved, requested, range) {
|
|
|
124
153
|
}
|
|
125
154
|
probed += chunk.length;
|
|
126
155
|
reader.feed(chunk);
|
|
127
|
-
if (reader.
|
|
128
|
-
|
|
156
|
+
if (probed > COUNT_BYTES && reader.counting) {
|
|
157
|
+
return reader.done
|
|
158
|
+
? { content: present({ ...reader.finish(), total: undefined }) }
|
|
159
|
+
: {
|
|
160
|
+
error: `${requested} has more than ${reader.lines} lines; its end is past the ${COUNT_MIB} MiB read_file counts, read a startLine range instead`
|
|
161
|
+
};
|
|
129
162
|
}
|
|
130
163
|
}
|
|
131
|
-
return { content: reader.finish() };
|
|
164
|
+
return { content: present(reader.finish()) };
|
|
132
165
|
}
|
|
133
166
|
class TextRange {
|
|
134
|
-
|
|
167
|
+
#start;
|
|
168
|
+
#end;
|
|
169
|
+
#keep;
|
|
170
|
+
#ring = [];
|
|
171
|
+
#rows = [];
|
|
135
172
|
#first;
|
|
136
173
|
#last;
|
|
137
|
-
#out = [];
|
|
138
|
-
#shownFirst;
|
|
139
174
|
#chars = 0;
|
|
140
|
-
|
|
141
|
-
#
|
|
175
|
+
done = false;
|
|
176
|
+
#open = false;
|
|
142
177
|
#lines = 0;
|
|
143
178
|
#line = emptyLine();
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
this.#
|
|
179
|
+
constructor({ start = 1, end }) {
|
|
180
|
+
this.#start = start;
|
|
181
|
+
this.#end = end;
|
|
182
|
+
this.#keep = start < 0 ? -start : TAIL_LINES;
|
|
183
|
+
this.#first = start < 0 ? Infinity : start;
|
|
147
184
|
this.#last = end ?? this.#first + READ_LINES - 1;
|
|
148
|
-
|
|
185
|
+
}
|
|
186
|
+
get lines() {
|
|
187
|
+
return this.#lines;
|
|
188
|
+
}
|
|
189
|
+
get counting() {
|
|
190
|
+
return this.done || this.#lines + 1 < this.#first;
|
|
149
191
|
}
|
|
150
192
|
feed(chunk) {
|
|
151
193
|
let at = 0;
|
|
152
|
-
while (
|
|
153
|
-
if (this
|
|
154
|
-
this
|
|
194
|
+
while (at < chunk.length) {
|
|
195
|
+
if (this.done) {
|
|
196
|
+
this.#count(chunk, at);
|
|
155
197
|
return;
|
|
156
198
|
}
|
|
157
199
|
const newline = chunk.indexOf(0x0a, at);
|
|
158
200
|
this.#append(chunk.subarray(at, newline === -1 ? chunk.length : newline));
|
|
159
|
-
if (newline !== -1 || (this.#line.cut && this.#lines + 1 >= this.#first)) {
|
|
160
|
-
this.#close();
|
|
161
|
-
}
|
|
162
201
|
if (newline === -1) {
|
|
163
202
|
return;
|
|
164
203
|
}
|
|
204
|
+
this.#close();
|
|
165
205
|
at = newline + 1;
|
|
166
206
|
}
|
|
167
207
|
}
|
|
168
208
|
finish() {
|
|
169
|
-
|
|
170
|
-
if (eof && this.#line.bytes > 0) {
|
|
209
|
+
if (this.#line.size > 0 || this.#lines === 0) {
|
|
171
210
|
this.#close();
|
|
172
211
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
212
|
+
const total = this.#lines + (this.#open ? 1 : 0);
|
|
213
|
+
if (this.#start > 0 && this.#start <= total) {
|
|
214
|
+
return { first: this.#first, rows: this.#rows, total };
|
|
215
|
+
}
|
|
216
|
+
this.#first = tailStart(this.#start, total);
|
|
217
|
+
this.#last = (this.#start < 0 ? this.#end : undefined) ?? this.#first + READ_LINES - 1;
|
|
218
|
+
for (let n = this.#first; n <= total && !this.done; n++) {
|
|
219
|
+
this.#take(n, this.#ring[(n - 1) % this.#keep]);
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
first: this.#first,
|
|
223
|
+
rows: this.#rows,
|
|
224
|
+
total,
|
|
225
|
+
pastEnd: this.#start > total ? this.#start : undefined
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
#count(chunk, from) {
|
|
229
|
+
for (let n = b4a.indexOf(chunk, 0x0a, from); n !== -1; n = b4a.indexOf(chunk, 0x0a, n + 1)) {
|
|
230
|
+
this.#lines++;
|
|
178
231
|
}
|
|
179
|
-
|
|
180
|
-
const lastShown = this.#shownFirst + this.#out.length - 1;
|
|
181
|
-
const shown = this.#cut
|
|
182
|
-
? `line ${this.#shownFirst} of ${total}, cut at ${READ_CHARS} characters`
|
|
183
|
-
: `lines ${this.#shownFirst}-${lastShown} of ${total}`;
|
|
184
|
-
const header = !eof || lastShown < this.#lines ? `${shown}; continue with startLine ${lastShown + 1}` : shown;
|
|
185
|
-
return `${header}\n${this.#out.join('\n')}`;
|
|
232
|
+
this.#open = chunk[chunk.length - 1] !== 0x0a;
|
|
186
233
|
}
|
|
187
234
|
#append(bytes) {
|
|
188
235
|
const kept = bytes.subarray(0, LINE_BYTES - this.#line.bytes);
|
|
@@ -190,35 +237,32 @@ class TextRange {
|
|
|
190
237
|
this.#line.parts.push(kept);
|
|
191
238
|
}
|
|
192
239
|
this.#line.bytes += kept.length;
|
|
193
|
-
this.#line.
|
|
240
|
+
this.#line.size += bytes.length;
|
|
194
241
|
}
|
|
195
242
|
#close() {
|
|
196
243
|
this.#lines++;
|
|
197
|
-
|
|
244
|
+
const line = this.#line;
|
|
198
245
|
this.#line = emptyLine();
|
|
199
246
|
if (this.#lines < this.#first) {
|
|
247
|
+
this.#ring[(this.#lines - 1) % this.#keep] = line;
|
|
200
248
|
return;
|
|
201
249
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
if (this.#chars + row.length > READ_CHARS) {
|
|
207
|
-
this
|
|
208
|
-
if (this.#cut) {
|
|
209
|
-
this.#out.push(row.slice(0, READ_CHARS));
|
|
210
|
-
}
|
|
211
|
-
this.stopped = true;
|
|
250
|
+
this.#take(this.#lines, line);
|
|
251
|
+
}
|
|
252
|
+
#take(n, line) {
|
|
253
|
+
const row = n > this.#last ? null : `${n}| ${lineText(line)}`;
|
|
254
|
+
if (row === null || this.#chars + row.length > READ_CHARS) {
|
|
255
|
+
this.done = true;
|
|
212
256
|
return;
|
|
213
257
|
}
|
|
214
|
-
this.#
|
|
258
|
+
this.#rows.push(row);
|
|
215
259
|
this.#chars += row.length + 1;
|
|
216
|
-
this
|
|
260
|
+
this.done = n >= this.#last;
|
|
217
261
|
}
|
|
218
262
|
}
|
|
219
263
|
function emptyLine() {
|
|
220
|
-
return { parts: [], bytes: 0,
|
|
264
|
+
return { parts: [], bytes: 0, size: 0 };
|
|
221
265
|
}
|
|
222
266
|
function lineText(line) {
|
|
223
|
-
return b4a.toString(b4a.concat(line.parts), 'utf8');
|
|
267
|
+
return clip(b4a.toString(b4a.concat(line.parts), 'utf8'), line.size);
|
|
224
268
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AbortSignal as BareAbortSignal } from 'bare-abort-controller';
|
|
2
|
+
import type { DriveRoot } from '../../contract/drive.ts';
|
|
3
|
+
export interface EngineCall<T> {
|
|
4
|
+
run: () => Promise<T>;
|
|
5
|
+
found: (answer: T) => boolean;
|
|
6
|
+
includeIgnored: boolean;
|
|
7
|
+
signal: BareAbortSignal | undefined;
|
|
8
|
+
}
|
|
9
|
+
export declare function engineAnswer<T>(root: DriveRoot, folder: string, { run, found, includeIgnored, signal }: EngineCall<T>): Promise<T | null>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { HIDDEN_DIRS, rootRules, WALK_SKIP_DIRS } from './walk.js';
|
|
2
|
+
async function ignoredFolder(root, folder, { includeIgnored }) {
|
|
3
|
+
const parts = folder.split('/').filter(Boolean);
|
|
4
|
+
if (parts.some((name) => WALK_SKIP_DIRS.has(name))) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
if (includeIgnored) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
const rules = await rootRules(root, folder);
|
|
11
|
+
const isDir = true;
|
|
12
|
+
return parts.some((name, i) => HIDDEN_DIRS.has(name) || rules.ignored(parts.slice(0, i + 1).join('/'), isDir));
|
|
13
|
+
}
|
|
14
|
+
export async function engineAnswer(root, folder, { run, found, includeIgnored, signal }) {
|
|
15
|
+
try {
|
|
16
|
+
const answer = await run();
|
|
17
|
+
if (found(answer)) {
|
|
18
|
+
return answer;
|
|
19
|
+
}
|
|
20
|
+
return (await ignoredFolder(root, folder, { includeIgnored })) ? null : answer;
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
if (signal?.aborted) {
|
|
24
|
+
throw err;
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type DriveRoot, type FsToolError, type ResolvedKey } from './scope.ts';
|
|
2
|
+
export interface MissingPath {
|
|
3
|
+
readonly requested: string;
|
|
4
|
+
readonly resolved: ResolvedKey;
|
|
5
|
+
readonly anchor: DriveRoot | undefined;
|
|
6
|
+
readonly deny: readonly string[];
|
|
7
|
+
}
|
|
8
|
+
export declare function missingFile({ requested, resolved: { root, key }, anchor, deny }: MissingPath): Promise<FsToolError>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { displayPath, isDeniedPath } from './scope.js';
|
|
3
|
+
import { shownPath } from './tool-path.js';
|
|
4
|
+
import { WALK_SKIP_DIRS } from './walk.js';
|
|
5
|
+
const SUGGESTIONS = 3;
|
|
6
|
+
const HINT = 'Use list_dir or glob to find the right path.';
|
|
7
|
+
export async function missingFile({ requested, resolved: { root, key }, anchor, deny }) {
|
|
8
|
+
const show = (at) => shownPath(displayPath(root, at), anchor);
|
|
9
|
+
let child = key;
|
|
10
|
+
let parent = parentOf(key);
|
|
11
|
+
let found = await children(root, parent, deny);
|
|
12
|
+
while (found.length === 0 && parent !== '/') {
|
|
13
|
+
child = parent;
|
|
14
|
+
parent = parentOf(parent);
|
|
15
|
+
found = await children(root, parent, deny);
|
|
16
|
+
}
|
|
17
|
+
const folderMissing = child !== key;
|
|
18
|
+
const near = similar(found.filter((entry) => entry.folder === folderMissing).map((entry) => entry.name), child.slice(child.lastIndexOf('/') + 1)).map((name) => show(under(parent, name)));
|
|
19
|
+
const picks = [...(await rerooted(requested, deny, anchor)), ...near].slice(0, SUGGESTIONS);
|
|
20
|
+
const nearest = show(parent) === '.' ? 'the shared folder' : show(parent);
|
|
21
|
+
const head = folderMissing
|
|
22
|
+
? `no such file: ${requested}. The folder ${show(child)} does not exist; the nearest that does is ${nearest}.`
|
|
23
|
+
: `no such file: ${requested}.`;
|
|
24
|
+
return { error: picks.length ? `${head} Did you mean:\n${picks.join('\n')}` : `${head} ${HINT}` };
|
|
25
|
+
}
|
|
26
|
+
async function rerooted(requested, deny, anchor) {
|
|
27
|
+
if (anchor?.kind !== 'local' || !path.isAbsolute(requested)) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
const given = path.resolve(requested);
|
|
31
|
+
if (inside(anchor.root, given) || !inside(path.dirname(anchor.root), given)) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const key = `/${path.relative(path.dirname(anchor.root), given).split(path.sep).join('/')}`;
|
|
35
|
+
const shown = displayPath(anchor, key);
|
|
36
|
+
return (await anchor.drive.entry(key)) && !isDeniedPath(shown, deny) ? [shown] : [];
|
|
37
|
+
}
|
|
38
|
+
function inside(folder, target) {
|
|
39
|
+
const relative = path.relative(folder, target);
|
|
40
|
+
return relative !== '' && relative.split(path.sep)[0] !== '..' && !path.isAbsolute(relative);
|
|
41
|
+
}
|
|
42
|
+
async function children(root, folder, deny) {
|
|
43
|
+
const found = [];
|
|
44
|
+
for await (const entry of root.drive.list(folder, { recursive: false, skip: WALK_SKIP_DIRS })) {
|
|
45
|
+
if (!isDeniedPath(displayPath(root, entry.key), deny)) {
|
|
46
|
+
found.push({ name: entry.key.slice(entry.key.lastIndexOf('/') + 1), folder: 'dir' in entry });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return found;
|
|
50
|
+
}
|
|
51
|
+
function parentOf(key) {
|
|
52
|
+
return key.slice(0, key.lastIndexOf('/')) || '/';
|
|
53
|
+
}
|
|
54
|
+
function under(folder, name) {
|
|
55
|
+
return folder === '/' ? `/${name}` : `${folder}/${name}`;
|
|
56
|
+
}
|
|
57
|
+
function similar(names, requested) {
|
|
58
|
+
const name = requested.toLowerCase();
|
|
59
|
+
const wanted = stem(name);
|
|
60
|
+
const typos = allowedTypos(wanted.length);
|
|
61
|
+
const tier = (candidate) => {
|
|
62
|
+
if (candidate === name) {
|
|
63
|
+
return 1;
|
|
64
|
+
}
|
|
65
|
+
const found = stem(candidate);
|
|
66
|
+
if (found === wanted) {
|
|
67
|
+
return 2;
|
|
68
|
+
}
|
|
69
|
+
if (Math.min(found.length, wanted.length) >= 3 &&
|
|
70
|
+
(candidate.includes(name) ||
|
|
71
|
+
name.includes(candidate) ||
|
|
72
|
+
found.includes(wanted) ||
|
|
73
|
+
wanted.includes(found))) {
|
|
74
|
+
return 3;
|
|
75
|
+
}
|
|
76
|
+
const near = typos > 0 && Math.abs(found.length - wanted.length) <= typos;
|
|
77
|
+
return near && editDistance(found, wanted) <= typos ? 4 : undefined;
|
|
78
|
+
};
|
|
79
|
+
return names
|
|
80
|
+
.flatMap((entry) => {
|
|
81
|
+
const rank = tier(entry.toLowerCase());
|
|
82
|
+
return rank === undefined
|
|
83
|
+
? []
|
|
84
|
+
: [{ entry, rank, distance: editDistance(entry.toLowerCase(), name) }];
|
|
85
|
+
})
|
|
86
|
+
.sort((a, b) => a.rank - b.rank || a.distance - b.distance || a.entry.localeCompare(b.entry))
|
|
87
|
+
.slice(0, SUGGESTIONS)
|
|
88
|
+
.map((match) => match.entry);
|
|
89
|
+
}
|
|
90
|
+
function allowedTypos(length) {
|
|
91
|
+
if (length < 3) {
|
|
92
|
+
return 0;
|
|
93
|
+
}
|
|
94
|
+
return length < 5 ? 1 : 2;
|
|
95
|
+
}
|
|
96
|
+
function stem(name) {
|
|
97
|
+
return name.slice(0, name.length - path.extname(name).length) || name;
|
|
98
|
+
}
|
|
99
|
+
function editDistance(a, b) {
|
|
100
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
101
|
+
for (let i = 1; i <= a.length; i++) {
|
|
102
|
+
const current = [i];
|
|
103
|
+
for (let j = 1; j <= b.length; j++) {
|
|
104
|
+
const substitute = (previous[j - 1] ?? 0) + (a[i - 1] === b[j - 1] ? 0 : 1);
|
|
105
|
+
current.push(Math.min((previous[j] ?? 0) + 1, (current[j - 1] ?? 0) + 1, substitute));
|
|
106
|
+
}
|
|
107
|
+
previous = current;
|
|
108
|
+
}
|
|
109
|
+
return previous[b.length] ?? 0;
|
|
110
|
+
}
|
|
@@ -5,6 +5,8 @@ export interface Grants {
|
|
|
5
5
|
readonly fsWriteRoots?: readonly DriveRoot[] | undefined;
|
|
6
6
|
readonly policy?: CompiledPolicy | undefined;
|
|
7
7
|
}
|
|
8
|
+
export declare const MAX_LIMIT = 500;
|
|
9
|
+
export declare function pagingError(args: Record<string, unknown>): string | null;
|
|
8
10
|
export declare function anchorOf(grants: Grants | undefined): DriveRoot | undefined;
|
|
9
11
|
export declare function rootedPath(p: string, anchor: DriveRoot | undefined): string;
|
|
10
12
|
export declare function shownPath(full: string, anchor: DriveRoot | undefined): string;
|
|
@@ -18,6 +20,7 @@ export declare function resolveRooted(p: string, { grants, write, realpath, deni
|
|
|
18
20
|
export declare function rootedArgs(args: Record<string, unknown>, grants: Grants | undefined, keys: readonly string[]): string[] | null;
|
|
19
21
|
export declare function stringArg(args: Record<string, unknown>, key: string): string | null;
|
|
20
22
|
export declare function positiveInt(value: unknown): number | undefined;
|
|
23
|
+
export declare function signedLine(value: unknown): number | undefined;
|
|
21
24
|
export declare function existsNow(p: string | null, grants: Grants | undefined): boolean;
|
|
22
25
|
export interface LabelPaths {
|
|
23
26
|
readonly grants: Grants | undefined;
|
|
@@ -2,6 +2,15 @@ import fs from 'fs';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { callDenyRoots, hostPath, resolveKey } from './scope.js';
|
|
4
4
|
const HYPER_PREFIX = 'hyper://';
|
|
5
|
+
export const MAX_LIMIT = 500;
|
|
6
|
+
export function pagingError(args) {
|
|
7
|
+
for (const key of ['limit', 'offset']) {
|
|
8
|
+
if (args[key] !== undefined && !(Number.isInteger(args[key]) && Number(args[key]) >= 0)) {
|
|
9
|
+
return `${key} is a whole number`;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
5
14
|
export function anchorOf(grants) {
|
|
6
15
|
return grants?.fsRoots?.[0] ?? grants?.fsWriteRoots?.[0];
|
|
7
16
|
}
|
|
@@ -53,6 +62,10 @@ export function positiveInt(value) {
|
|
|
53
62
|
const n = Number(value);
|
|
54
63
|
return value === undefined || value === null || !Number.isInteger(n) || n < 1 ? undefined : n;
|
|
55
64
|
}
|
|
65
|
+
export function signedLine(value) {
|
|
66
|
+
const n = Number(value);
|
|
67
|
+
return Number.isInteger(n) && n < 0 ? n : positiveInt(value);
|
|
68
|
+
}
|
|
56
69
|
export function existsNow(p, grants) {
|
|
57
70
|
const anchor = anchorOf(grants);
|
|
58
71
|
if (!p || !anchor || anchor.kind !== 'local') {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { DriveRoot, WalkSummary } from '../../contract/drive.ts';
|
|
2
|
+
import { IgnoreRules } from './ignore.ts';
|
|
2
3
|
export declare const WALK_SKIP_DIRS: Set<string>;
|
|
4
|
+
export declare const HIDDEN_DIRS: Set<string>;
|
|
3
5
|
export declare const MAX_WALK_ENTRIES = 10000;
|
|
4
6
|
export declare const MAX_WALK_MS = 30000;
|
|
5
7
|
export interface WalkedFile {
|
|
@@ -20,5 +22,6 @@ export interface WalkOptions {
|
|
|
20
22
|
maxEntries?: number;
|
|
21
23
|
maxMs?: number;
|
|
22
24
|
}
|
|
25
|
+
export declare function rootRules(root: DriveRoot, folder: string): Promise<IgnoreRules>;
|
|
23
26
|
export declare function walkFiles(root: DriveRoot, folder?: string, opts?: WalkOptions): Promise<WalkResult>;
|
|
24
27
|
export declare function streamFiles(root: DriveRoot, folder?: string, opts?: WalkOptions): WalkStream;
|
|
@@ -5,13 +5,29 @@ import { pushQueue } from '../../queue.js';
|
|
|
5
5
|
import { IgnoreRules } from './ignore.js';
|
|
6
6
|
import { displayPath } from './scope.js';
|
|
7
7
|
export const WALK_SKIP_DIRS = new Set(['.git', 'node_modules', '.trash']);
|
|
8
|
-
const HIDDEN_DIRS = new Set(['.hg', '.svn', '.venv', '.cache', '.next']);
|
|
8
|
+
export const HIDDEN_DIRS = new Set(['.hg', '.svn', '.venv', '.cache', '.next']);
|
|
9
9
|
const IGNORE_FILES = ['.gitignore', '.ignore'];
|
|
10
10
|
export const MAX_WALK_ENTRIES = 10_000;
|
|
11
11
|
export const MAX_WALK_MS = 30_000;
|
|
12
12
|
function childKey(dir, name) {
|
|
13
13
|
return dir === '/' ? `/${name}` : `${dir}/${name}`;
|
|
14
14
|
}
|
|
15
|
+
async function withIgnoreFile(root, rules, key) {
|
|
16
|
+
const bytes = await root.drive.get(key);
|
|
17
|
+
const dir = key.slice(0, key.lastIndexOf('/'));
|
|
18
|
+
return bytes === null ? rules : rules.add(dir.slice(1), b4a.toString(bytes, 'utf8'));
|
|
19
|
+
}
|
|
20
|
+
export async function rootRules(root, folder) {
|
|
21
|
+
let rules = new IgnoreRules();
|
|
22
|
+
const parts = folder.split('/').filter(Boolean);
|
|
23
|
+
for (let depth = 0; depth < parts.length; depth++) {
|
|
24
|
+
const dir = '/' + parts.slice(0, depth).join('/');
|
|
25
|
+
for (const name of IGNORE_FILES) {
|
|
26
|
+
rules = await withIgnoreFile(root, rules, childKey(dir, name));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return rules;
|
|
30
|
+
}
|
|
15
31
|
function isIgnoreFile(child, key) {
|
|
16
32
|
return !('dir' in child) && child.value.linkname === null && child.key === key;
|
|
17
33
|
}
|
|
@@ -30,24 +46,8 @@ class Walk {
|
|
|
30
46
|
this.#maxEntries = maxEntries;
|
|
31
47
|
this.#deadline = Date.now() + maxMs;
|
|
32
48
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const dir = key.slice(0, key.lastIndexOf('/'));
|
|
36
|
-
return bytes === null ? rules : rules.add(dir.slice(1), b4a.toString(bytes, 'utf8'));
|
|
37
|
-
}
|
|
38
|
-
async rootRules(folder) {
|
|
39
|
-
let rules = new IgnoreRules();
|
|
40
|
-
if (this.#includeIgnored) {
|
|
41
|
-
return rules;
|
|
42
|
-
}
|
|
43
|
-
const parts = folder.split('/').filter(Boolean);
|
|
44
|
-
for (let depth = 0; depth < parts.length; depth++) {
|
|
45
|
-
const dir = '/' + parts.slice(0, depth).join('/');
|
|
46
|
-
for (const name of IGNORE_FILES) {
|
|
47
|
-
rules = await this.#withIgnoreFile(rules, childKey(dir, name));
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return rules;
|
|
49
|
+
rootRules(folder) {
|
|
50
|
+
return this.#includeIgnored ? Promise.resolve(new IgnoreRules()) : rootRules(this.#root, folder);
|
|
51
51
|
}
|
|
52
52
|
async #list(dir) {
|
|
53
53
|
const children = [];
|
|
@@ -71,7 +71,7 @@ class Walk {
|
|
|
71
71
|
for (const name of IGNORE_FILES) {
|
|
72
72
|
const key = childKey(dir, name);
|
|
73
73
|
if (!this.#includeIgnored && children.some((child) => isIgnoreFile(child, key))) {
|
|
74
|
-
rules = await this.#
|
|
74
|
+
rules = await withIgnoreFile(this.#root, rules, key);
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
return rules;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Toolset } from '../contract/index.ts';
|
|
2
2
|
export type { Toolset };
|
|
3
|
-
export declare const TOOLSETS: readonly ["web-search", "knowledge", "files", "exec", "http-request", "mcp-call", "image", "music", "coding", "team"];
|
|
3
|
+
export declare const TOOLSETS: readonly ["web-search", "knowledge", "files", "exec", "http-request", "mcp-call", "image", "diagrams", "charts", "music", "coding", "team"];
|
|
4
4
|
export declare function asToolset(value?: string | null): Toolset | null;
|
|
5
5
|
export declare function toolsetEnabled(toolset: Toolset, disabled?: ReadonlyArray<string> | null): boolean;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Chart, Mark, ThemeName } from 'qvac-charts';
|
|
2
|
+
import type { ToolDefinition } from '../../lib/contract/index.ts';
|
|
3
|
+
export interface ChartToolOptions {
|
|
4
|
+
chart: typeof Chart;
|
|
5
|
+
theme: ThemeName;
|
|
6
|
+
mark: Mark | undefined;
|
|
7
|
+
}
|
|
8
|
+
export declare function createChartTool({ chart, theme, mark }: ChartToolOptions): ToolDefinition;
|