@qvac/harness 1.0.0-rc.21 → 1.0.0-rc.22
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 +44 -0
- package/README.md +1 -0
- package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +1 -0
- 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/machine/completion/actions/tools.js +7 -1
- package/dist-lib/lib/tools/files/glob.js +48 -9
- package/dist-lib/lib/tools/files/grep.js +27 -13
- package/dist-lib/lib/tools/files/read-file.js +120 -76
- 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/toolsets.d.ts +1 -1
- package/dist-lib/lib/tools/toolsets.js +1 -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/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 +5 -1
- package/worker/harness-sidecar.mjs +2 -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,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,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", "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,10 @@
|
|
|
1
|
+
import type { ToolDefinition } from '../../lib/contract/index.ts';
|
|
2
|
+
type DrawResult = {
|
|
3
|
+
content: string;
|
|
4
|
+
answer: true;
|
|
5
|
+
} | {
|
|
6
|
+
error: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function drawDiagram(args: Record<string, unknown>, userTurns?: readonly string[]): DrawResult;
|
|
9
|
+
export declare const drawDiagramTool: ToolDefinition;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { DIAGRAM_TYPES, renderMermaid, sliceValue } from './render.js';
|
|
2
|
+
function isType(value) {
|
|
3
|
+
return DIAGRAM_TYPES.some((type) => type === value);
|
|
4
|
+
}
|
|
5
|
+
function stringOr(value) {
|
|
6
|
+
return typeof value === 'string' ? value : undefined;
|
|
7
|
+
}
|
|
8
|
+
function linksOf(raw) {
|
|
9
|
+
if (!Array.isArray(raw) || raw.length === 0) {
|
|
10
|
+
return 'links must be a non-empty list of {"from", "to", "label"}';
|
|
11
|
+
}
|
|
12
|
+
const links = [];
|
|
13
|
+
for (const item of raw) {
|
|
14
|
+
const from = stringOr(item?.from)?.trim();
|
|
15
|
+
if (!from) {
|
|
16
|
+
return 'every link needs a "from" name';
|
|
17
|
+
}
|
|
18
|
+
links.push({ from, to: stringOr(item.to), label: stringOr(item.label) });
|
|
19
|
+
}
|
|
20
|
+
return links;
|
|
21
|
+
}
|
|
22
|
+
const HORIZONTAL_ASK = /horizontal|orizzontal|left[\s-]*to[\s-]*right|sideways|landscape|\bLR\b/i;
|
|
23
|
+
function askedHorizontal(userTurns) {
|
|
24
|
+
return userTurns === undefined || userTurns.some((turn) => HORIZONTAL_ASK.test(turn));
|
|
25
|
+
}
|
|
26
|
+
export function drawDiagram(args, userTurns) {
|
|
27
|
+
if (!isType(args.type)) {
|
|
28
|
+
return { error: `type must be one of ${DIAGRAM_TYPES.join(', ')}` };
|
|
29
|
+
}
|
|
30
|
+
const links = linksOf(args.links);
|
|
31
|
+
if (typeof links === 'string') {
|
|
32
|
+
return { error: links };
|
|
33
|
+
}
|
|
34
|
+
if (args.type === 'pie' && !links.some((link) => sliceValue(link) !== undefined)) {
|
|
35
|
+
return { error: 'every pie slice needs its number: {"from": "Rent", "to": "40"}' };
|
|
36
|
+
}
|
|
37
|
+
const direction = args.direction === 'horizontal' && askedHorizontal(userTurns) ? 'horizontal' : 'vertical';
|
|
38
|
+
const code = renderMermaid({ type: args.type, direction, links });
|
|
39
|
+
const caption = stringOr(args.caption)?.trim();
|
|
40
|
+
return {
|
|
41
|
+
content: `\`\`\`mermaid\n${code}\n\`\`\`${caption ? `\n\n${caption}` : ''}\n`,
|
|
42
|
+
answer: true
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export const drawDiagramTool = {
|
|
46
|
+
name: 'draw_diagram',
|
|
47
|
+
toolset: 'diagrams',
|
|
48
|
+
group: 'diagrams',
|
|
49
|
+
skillGated: true,
|
|
50
|
+
autoApprove: true,
|
|
51
|
+
description: 'Draw any diagram in the chat. Give its type and its links; the app renders the picture. ' +
|
|
52
|
+
'Every box, step, date or slice is a name; every arrow or entry is one link.',
|
|
53
|
+
parameters: {
|
|
54
|
+
type: 'object',
|
|
55
|
+
properties: {
|
|
56
|
+
type: {
|
|
57
|
+
type: 'string',
|
|
58
|
+
enum: [...DIAGRAM_TYPES],
|
|
59
|
+
description: 'flowchart: steps or parts and arrows. sequence: who calls whom, in order. ' +
|
|
60
|
+
'state: modes and the events between them. mindmap: an idea tree from one root. ' +
|
|
61
|
+
'er: tables, from a table to a table it owns. class: from a parent class to a child. ' +
|
|
62
|
+
'timeline: from a date to what happened. gantt: from a task to its length, like "5d". ' +
|
|
63
|
+
'pie: from a slice to its number.'
|
|
64
|
+
},
|
|
65
|
+
direction: {
|
|
66
|
+
type: 'string',
|
|
67
|
+
enum: ['vertical', 'horizontal'],
|
|
68
|
+
description: 'Only when the user asks: "horizontal" for horizontal, left to right, or sideways; ' +
|
|
69
|
+
'"vertical" for top to bottom. Leave it out otherwise.'
|
|
70
|
+
},
|
|
71
|
+
links: {
|
|
72
|
+
type: 'array',
|
|
73
|
+
description: 'One entry per arrow, in order. A name ending in ? is a decision.',
|
|
74
|
+
items: {
|
|
75
|
+
type: 'object',
|
|
76
|
+
properties: {
|
|
77
|
+
from: { type: 'string' },
|
|
78
|
+
to: { type: 'string' },
|
|
79
|
+
label: { type: 'string', description: 'the text on the arrow, any characters' }
|
|
80
|
+
},
|
|
81
|
+
required: ['from', 'to']
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
caption: { type: 'string', description: 'One sentence saying what the diagram shows' }
|
|
85
|
+
},
|
|
86
|
+
required: ['type', 'links']
|
|
87
|
+
},
|
|
88
|
+
effect: () => 'read',
|
|
89
|
+
execute: async (args, context) => drawDiagram(args, context?.userTurns)
|
|
90
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HarnessPlugin, PluginSetup } from '../../lib/plugins/index.ts';
|
|
2
|
+
export { drawDiagram, drawDiagramTool } from './draw.ts';
|
|
3
|
+
export { renderMermaid } from './render.ts';
|
|
4
|
+
export interface DiagramsOptions {
|
|
5
|
+
readonly read?: readonly string[];
|
|
6
|
+
}
|
|
7
|
+
export declare class Diagrams implements HarnessPlugin {
|
|
8
|
+
readonly name = "diagrams";
|
|
9
|
+
readonly api = 1;
|
|
10
|
+
readonly read: readonly string[] | undefined;
|
|
11
|
+
constructor({ read }?: DiagramsOptions);
|
|
12
|
+
setup(): PluginSetup;
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PluginRegistry } from '../../lib/plugins/index.js';
|
|
2
|
+
import { drawDiagramTool } from './draw.js';
|
|
3
|
+
export { drawDiagram, drawDiagramTool } from './draw.js';
|
|
4
|
+
export { renderMermaid } from './render.js';
|
|
5
|
+
export class Diagrams {
|
|
6
|
+
name = 'diagrams';
|
|
7
|
+
api = PluginRegistry.API_VERSION;
|
|
8
|
+
read;
|
|
9
|
+
constructor({ read } = {}) {
|
|
10
|
+
this.read = read;
|
|
11
|
+
}
|
|
12
|
+
setup() {
|
|
13
|
+
return { tools: [drawDiagramTool] };
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const DIAGRAM_TYPES: readonly ['flowchart', 'sequence', 'state', 'mindmap', 'er', 'class', 'timeline', 'gantt', 'pie'];
|
|
2
|
+
export type DiagramType = (typeof DIAGRAM_TYPES)[number];
|
|
3
|
+
export type Direction = 'vertical' | 'horizontal';
|
|
4
|
+
export interface Link {
|
|
5
|
+
readonly from: string;
|
|
6
|
+
readonly to?: string | undefined;
|
|
7
|
+
readonly label?: string | undefined;
|
|
8
|
+
}
|
|
9
|
+
export interface DiagramSpec {
|
|
10
|
+
readonly type: DiagramType;
|
|
11
|
+
readonly direction: Direction;
|
|
12
|
+
readonly links: readonly Link[];
|
|
13
|
+
readonly today?: Date | undefined;
|
|
14
|
+
}
|
|
15
|
+
export declare function sliceValue(link: Link): number | undefined;
|
|
16
|
+
export declare function renderMermaid({ type, direction, links, today }: DiagramSpec): string;
|