@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
package/CHANGELOG.md
CHANGED
|
@@ -21,6 +21,29 @@ breaking change is listed under Changed and Removed.
|
|
|
21
21
|
|
|
22
22
|
### Added
|
|
23
23
|
|
|
24
|
+
- `grep` takes `caseSensitive` to match exact case, and `grep` and `glob` take `includeIgnored` to also
|
|
25
|
+
search what `.gitignore`, `.ignore` and cache folders leave out; `.git`, `node_modules` and `.trash`
|
|
26
|
+
stay skipped. `glob` takes `limit` (default 100, at most 500) and `offset`. A `glob` that finds more
|
|
27
|
+
than 100 paths, with neither set, now samples across the top-level folders instead of taking the
|
|
28
|
+
newest 100, and says so in a `note`; with `limit` or `offset` it pages newest first as before.
|
|
29
|
+
- A `Diagrams` plugin gives the model `draw_diagram`: a diagram type and its links in, and the
|
|
30
|
+
Mermaid block the app renders out, as the run's reply. It draws flowcharts, sequence, state,
|
|
31
|
+
ER and class diagrams, mind maps, timelines, Gantt charts and pies, escapes every label itself,
|
|
32
|
+
and draws a horizontal sequence as a numbered `flowchart LR`, but only when a user turn asks for
|
|
33
|
+
horizontal, left to right or sideways in words. It sits in a new `diagrams`
|
|
34
|
+
toolset, and the shipped sidecar registers it with its read grant. On the diagrams pack, with the
|
|
35
|
+
`diagrams` skill that declares it, the 2B succeeds in 180 of 198 runs against 161 and draws no
|
|
36
|
+
broken diagram against 12; the 4B 180 against 170, with none broken against 18.
|
|
37
|
+
```js
|
|
38
|
+
new Harness({ plugins: [new Diagrams({ read: ['draw_diagram'] })] })
|
|
39
|
+
```
|
|
40
|
+
(#892)
|
|
41
|
+
- `answeredToolTurns` and `isAnswer` in `@qvac/harness/lib/capabilities/tool-turns`: a call whose
|
|
42
|
+
result answered the run (`{ content, answer: true }`) has no round after it, so a host that
|
|
43
|
+
rebuilds a chat from its own record replays it with these, before the run's reply. (#892)
|
|
44
|
+
- `ToolExecuteContext.userTurns`: the user's turns as the host sent them, so a tool can check an
|
|
45
|
+
argument the model sent against what was asked. (#892)
|
|
46
|
+
|
|
24
47
|
- Workspace skills run in every chat of the workspace. Send them as `workspaceSkills`, shaped like
|
|
25
48
|
`projectSkills`. On a name clash the project's skill wins over the workspace's, and the workspace's
|
|
26
49
|
over your own. A workspace skill with a script asks each member by its hash, as a project skill
|
|
@@ -586,6 +609,9 @@ breaking change is listed under Changed and Removed.
|
|
|
586
609
|
|
|
587
610
|
### Changed
|
|
588
611
|
|
|
612
|
+
- Two calls in one round whose results each answer the run (`{ content, answer: true }`) both
|
|
613
|
+
reach the reply, joined in order; only the first did before. (#892)
|
|
614
|
+
|
|
589
615
|
- **Breaking:** speech defaults to Audio8. A `speak` or `speakStream` that
|
|
590
616
|
names no model loads `Audio8-Q8_0` on every device profile, where it loaded
|
|
591
617
|
Supertonic 3. Audio8 is 1.05 GB with its codec decoder (Supertonic was 85 MB
|
|
@@ -1366,6 +1392,24 @@ breaking change is listed under Changed and Removed.
|
|
|
1366
1392
|
|
|
1367
1393
|
### Fixed
|
|
1368
1394
|
|
|
1395
|
+
- `read_file` returns what a model can use. A CRLF line loses its `\r`, and a
|
|
1396
|
+
line over 2000 characters is cut there, so one long line no longer uses up
|
|
1397
|
+
the read; the cut names the full length,
|
|
1398
|
+
`… (line cut at 2000 chars; 5800 bytes in all)` for a text file and
|
|
1399
|
+
`5800 chars` for a document. A `startLine` past the end returns the last 50
|
|
1400
|
+
lines under `startLine N is past the end; the file has T lines`, and a
|
|
1401
|
+
negative `startLine` reads from the end (`-50` is the last 50 lines). A range
|
|
1402
|
+
names the exact total (`lines 2-2 of 50001`) where it said `of unknown`.
|
|
1403
|
+
Every read is one pass over one stream: the file is counted to its end
|
|
1404
|
+
without decoding it, and the tail is kept in a ring as it goes. The count
|
|
1405
|
+
stops at 8 MiB: past it a range's total stays unknown, and a tail returns
|
|
1406
|
+
`has more than N lines; its end is past the 8 MiB read_file counts` instead
|
|
1407
|
+
of reading on. A missing
|
|
1408
|
+
path suggests up to three near matches: the same name in another case, the
|
|
1409
|
+
same name with another extension, a name that contains it, or a one- or
|
|
1410
|
+
two-letter typo. For a missing folder it names the nearest folder that
|
|
1411
|
+
exists, and for a path written from the wrong root it suggests the same path
|
|
1412
|
+
under the shared folder. Denied paths are never suggested. (#887)
|
|
1369
1413
|
- OCR loads in the harness sidecar. Since rc.16 the first OCR model load
|
|
1370
1414
|
failed with `CANNOT_LOAD`, so a scanned PDF got no text on desktop. (#844)
|
|
1371
1415
|
- `edit_file`, `insert_text` and `edit_many` write only what they change. A
|
package/README.md
CHANGED
|
@@ -327,6 +327,7 @@ itself.
|
|
|
327
327
|
| [`TranslationPlugin`](plugins/translation/README.md) | Translation on device |
|
|
328
328
|
| [`LanguageHint`](plugins/language-hint/README.md) | A prompt in another language, in English too, for the model |
|
|
329
329
|
| [`Weather`](plugins/weather/README.md) | Weather lookup |
|
|
330
|
+
| [`Diagrams`](plugins/diagrams/README.md) | Diagrams drawn from a type and its links, as the Mermaid the app renders |
|
|
330
331
|
| [`ObsidianRoute`](plugins/obsidian/README.md) | `obsidian …` commands against a vault |
|
|
331
332
|
|
|
332
333
|
Each plugin is imported from `@qvac/harness/plugins/<folder>`. Its own dependencies are optional
|
|
@@ -22,6 +22,7 @@ export function toolContext({ deps, context, name }) {
|
|
|
22
22
|
const { args, session, commitFacts } = deps;
|
|
23
23
|
return {
|
|
24
24
|
currentTime: args.messages.findLast((turn) => turn.role === 'user')?.sentAt || session.startedAt,
|
|
25
|
+
userTurns: args.messages.filter((turn) => turn.role === 'user').map((turn) => turn.content),
|
|
25
26
|
...session.toolContext(session.turnImages, context.metrics.ctxSize ?? args.ctxSize, args.signal),
|
|
26
27
|
skills: context.activated.filter((skill) => Agent.skillIsActive(skill, session.activation) &&
|
|
27
28
|
!unreadySkillState(skill, args.skillCatalog ?? [])),
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isRecord } from '../../guards.js';
|
|
2
|
+
import { isAnswer } from '../tool-turns.js';
|
|
2
3
|
const RETRYABLE_STATUS = new Set([408, 429]);
|
|
3
4
|
const RETRYABLE_CODE = new Set(['timeout', 'transport', 'busy']);
|
|
4
5
|
export function callSucceeded(result, outcome) {
|
|
@@ -9,10 +10,7 @@ export function callSucceeded(result, outcome) {
|
|
|
9
10
|
return typeof exitCode !== 'number' || exitCode === 0;
|
|
10
11
|
}
|
|
11
12
|
export function answerOf(result) {
|
|
12
|
-
|
|
13
|
-
return undefined;
|
|
14
|
-
}
|
|
15
|
-
return result.content.trim() ? result.content : undefined;
|
|
13
|
+
return isAnswer(result) ? result.content : undefined;
|
|
16
14
|
}
|
|
17
15
|
function retryHints(result) {
|
|
18
16
|
if (!isRecord(result) || Array.isArray(result)) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Message } from '../../contract/index.ts';
|
|
2
|
+
export interface AnsweredCall {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly arguments: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
export interface Answer {
|
|
8
|
+
readonly answer: true;
|
|
9
|
+
readonly content: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function isAnswer(result: unknown): result is Answer;
|
|
12
|
+
export interface AnsweredTurn extends Message {
|
|
13
|
+
readonly role: 'assistant' | 'tool';
|
|
14
|
+
}
|
|
15
|
+
export declare function answeredToolTurns(call: AnsweredCall): AnsweredTurn[];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isRecord } from '../../guards.js';
|
|
2
|
+
import { ToolCallMarkup } from '../../helpers/tool-call-markup.js';
|
|
3
|
+
import { toolResultTurn } from './result-turn.js';
|
|
4
|
+
const ANSWERED_PAYLOAD = { answered: true };
|
|
5
|
+
export function isAnswer(result) {
|
|
6
|
+
return (isRecord(result) &&
|
|
7
|
+
result.answer === true &&
|
|
8
|
+
typeof result.content === 'string' &&
|
|
9
|
+
result.content.trim().length > 0);
|
|
10
|
+
}
|
|
11
|
+
export function answeredToolTurns(call) {
|
|
12
|
+
return [
|
|
13
|
+
{ role: 'assistant', content: ToolCallMarkup.toolCallMarkup([call]) },
|
|
14
|
+
{ role: 'tool', content: toolResultTurn(call.name, call.id, ANSWERED_PAYLOAD) }
|
|
15
|
+
];
|
|
16
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { answeredToolTurns, isAnswer } from './tool-turns/answered.ts';
|
|
2
|
+
export type { AnsweredCall, AnsweredTurn } from './tool-turns/answered.ts';
|
|
1
3
|
export { attachedFiles, attachmentTurn, deliversFile } from './tool-turns/attachments.ts';
|
|
2
4
|
export type { AttachedFile } from './tool-turns/attachments.ts';
|
|
3
5
|
export { FILE_DELIVERED_NUDGE, GOAL_CONTINUATION_NUDGE, GOAL_STALLED_NUDGE, TOOL_CONTEXT_BOUNDARY_MESSAGE, TOOL_CONTINUATION_NUDGE, TOOL_CUT_OFF_MESSAGE, TOOL_DENIED_MESSAGE, TOOL_FAILING_MESSAGE, TOOL_REPEATED_FAILURE_MESSAGE, TOOL_REPEATED_MESSAGE, TOOL_ROUND_CUT_MESSAGE, TOOL_UNCHANGED_MESSAGE, WRAP_UP_NUDGE } from './tool-turns/copy.ts';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { answeredToolTurns, isAnswer } from './tool-turns/answered.js';
|
|
1
2
|
export { attachedFiles, attachmentTurn, deliversFile } from './tool-turns/attachments.js';
|
|
2
3
|
export { FILE_DELIVERED_NUDGE, GOAL_CONTINUATION_NUDGE, GOAL_STALLED_NUDGE, TOOL_CONTEXT_BOUNDARY_MESSAGE, TOOL_CONTINUATION_NUDGE, TOOL_CUT_OFF_MESSAGE, TOOL_DENIED_MESSAGE, TOOL_FAILING_MESSAGE, TOOL_REPEATED_FAILURE_MESSAGE, TOOL_REPEATED_MESSAGE, TOOL_ROUND_CUT_MESSAGE, TOOL_UNCHANGED_MESSAGE, WRAP_UP_NUDGE } from './tool-turns/copy.js';
|
|
3
4
|
export { DIRECT_CALL_ID, TOOL_RESULT_MAX_CHARS, historyPayload, materializeCompletionHistory, redactFailureTurns, toolResultHistoryEntry, toolResultTurn, withHookContext } from './tool-turns/result-turn.js';
|
|
@@ -153,6 +153,7 @@ export interface SkillTree {
|
|
|
153
153
|
}
|
|
154
154
|
export interface ToolExecuteContext {
|
|
155
155
|
currentTime?: number | undefined;
|
|
156
|
+
userTurns?: readonly string[] | undefined;
|
|
156
157
|
agentId?: string | null | undefined;
|
|
157
158
|
skillTrees?: ReadonlyMap<string, SkillTree> | undefined;
|
|
158
159
|
skills?: readonly string[] | undefined;
|
|
@@ -194,7 +195,7 @@ export interface CallTool {
|
|
|
194
195
|
(name: string, args: unknown): Promise<unknown>;
|
|
195
196
|
readonly names: readonly string[];
|
|
196
197
|
}
|
|
197
|
-
export type Toolset = 'web-search' | 'knowledge' | 'files' | 'exec' | 'http-request' | 'mcp-call' | 'image' | 'music' | 'coding' | 'team';
|
|
198
|
+
export type Toolset = 'web-search' | 'knowledge' | 'files' | 'exec' | 'http-request' | 'mcp-call' | 'image' | 'diagrams' | 'music' | 'coding' | 'team';
|
|
198
199
|
export interface ToolsetScoped {
|
|
199
200
|
toolset?: string | null;
|
|
200
201
|
}
|
|
@@ -55,9 +55,15 @@ export function endRunOnTool(args) {
|
|
|
55
55
|
export function finishDirectTool({ context, params }) {
|
|
56
56
|
return { ...applyToolResult(context, params, context.currentCall), directSearchDone: true };
|
|
57
57
|
}
|
|
58
|
+
function joinAnswers(earlier, next) {
|
|
59
|
+
if (earlier === null || !next) {
|
|
60
|
+
return earlier ?? next ?? null;
|
|
61
|
+
}
|
|
62
|
+
return `${earlier.trimEnd()}\n\n${next}`;
|
|
63
|
+
}
|
|
58
64
|
function roundResult(context, payload) {
|
|
59
65
|
return {
|
|
60
|
-
answer: context.answer
|
|
66
|
+
answer: joinAnswers(context.answer, payload.answer),
|
|
61
67
|
deliveredThisRound: context.deliveredThisRound || payload.delivered === true,
|
|
62
68
|
deliveryEndsRun: context.deliveryEndsRun || payload.deliveryEndsRun === true,
|
|
63
69
|
shownThisRound: payload.shown
|
|
@@ -1,16 +1,35 @@
|
|
|
1
1
|
import { relativeKey, searchTargets } from '../fs/targets.js';
|
|
2
2
|
import { compileGlob } from '../fs/glob.js';
|
|
3
3
|
import { callDenyRoots, defaultRealpath, isDeniedPath } from '../fs/scope.js';
|
|
4
|
-
import { anchorOf, shownPath, stringArg } from '../fs/tool-path.js';
|
|
4
|
+
import { anchorOf, MAX_LIMIT, pagingError, shownPath, stringArg } from '../fs/tool-path.js';
|
|
5
5
|
import { walkFiles } from '../fs/walk.js';
|
|
6
6
|
import { folderArg, folderPaths } from './list-dir.js';
|
|
7
7
|
const GLOB_MATCHES = 100;
|
|
8
|
+
function interleaveTopLevel(hits) {
|
|
9
|
+
const groups = new Map();
|
|
10
|
+
for (const hit of hits) {
|
|
11
|
+
const list = groups.get(hit.group) ?? [];
|
|
12
|
+
list.push(hit);
|
|
13
|
+
groups.set(hit.group, list);
|
|
14
|
+
}
|
|
15
|
+
const order = [];
|
|
16
|
+
for (let round = 0; order.length < hits.length; round++) {
|
|
17
|
+
for (const list of groups.values()) {
|
|
18
|
+
if (round < list.length) {
|
|
19
|
+
order.push(list[round]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return order;
|
|
24
|
+
}
|
|
8
25
|
export function createGlobTool({ realpath = defaultRealpath, deniedRoots } = {}) {
|
|
9
26
|
return {
|
|
10
27
|
name: 'glob',
|
|
11
28
|
toolset: 'files',
|
|
12
29
|
description: 'Find files in the shared folder by name pattern, newest first. "**" crosses folders: ' +
|
|
13
|
-
'"**/*.md" finds every Markdown file, "*report*" matches names anywhere.'
|
|
30
|
+
'"**/*.md" finds every Markdown file, "*report*" matches names anywhere. ' +
|
|
31
|
+
'Ignored files and cache folders are skipped (set includeIgnored to find them; .git and node_modules stay out). A long answer samples every ' +
|
|
32
|
+
'top-level folder and offset pages that sample.',
|
|
14
33
|
parameters: {
|
|
15
34
|
type: 'object',
|
|
16
35
|
properties: {
|
|
@@ -18,7 +37,13 @@ export function createGlobTool({ realpath = defaultRealpath, deniedRoots } = {})
|
|
|
18
37
|
dir: {
|
|
19
38
|
type: 'string',
|
|
20
39
|
description: 'Folder to search, relative to the shared folder (optional)'
|
|
21
|
-
}
|
|
40
|
+
},
|
|
41
|
+
includeIgnored: {
|
|
42
|
+
type: 'boolean',
|
|
43
|
+
description: 'Also find files .gitignore and cache folders leave out'
|
|
44
|
+
},
|
|
45
|
+
limit: { type: 'number', description: `Most paths to return (default ${GLOB_MATCHES})` },
|
|
46
|
+
offset: { type: 'number', description: 'Paths to skip, to page past a cut-off answer' }
|
|
22
47
|
},
|
|
23
48
|
required: ['pattern']
|
|
24
49
|
},
|
|
@@ -27,7 +52,7 @@ export function createGlobTool({ realpath = defaultRealpath, deniedRoots } = {})
|
|
|
27
52
|
stableRead: true,
|
|
28
53
|
autoApprove: true,
|
|
29
54
|
rootsRequired: true,
|
|
30
|
-
validate: (args) => (stringArg(args, 'pattern') ?
|
|
55
|
+
validate: (args) => (stringArg(args, 'pattern') ? pagingError(args) : 'pattern is required'),
|
|
31
56
|
paths: (args, ctx) => folderPaths(args, ctx),
|
|
32
57
|
resource: (args) => ({ kind: 'file', label: `glob ${String(args.pattern ?? '')}` }),
|
|
33
58
|
async execute(args, ctx) {
|
|
@@ -40,10 +65,13 @@ export function createGlobTool({ realpath = defaultRealpath, deniedRoots } = {})
|
|
|
40
65
|
const pattern = String(args.pattern);
|
|
41
66
|
const test = compileGlob(pattern);
|
|
42
67
|
const byName = !pattern.includes('/');
|
|
68
|
+
const limit = Math.min(Number(args.limit ?? GLOB_MATCHES), MAX_LIMIT);
|
|
69
|
+
const offset = Number(args.offset ?? 0);
|
|
70
|
+
const includeIgnored = args.includeIgnored === true;
|
|
43
71
|
const hits = [];
|
|
44
72
|
let walkTruncated = false;
|
|
45
|
-
for (const target of scoped.targets) {
|
|
46
|
-
const walk = await walkFiles(target.root, target.folder);
|
|
73
|
+
for (const [index, target] of scoped.targets.entries()) {
|
|
74
|
+
const walk = await walkFiles(target.root, target.folder, { includeIgnored });
|
|
47
75
|
walkTruncated ||= walk.truncated;
|
|
48
76
|
for (const file of walk.files) {
|
|
49
77
|
const rel = relativeKey(file.key, target.folder);
|
|
@@ -52,13 +80,24 @@ export function createGlobTool({ realpath = defaultRealpath, deniedRoots } = {})
|
|
|
52
80
|
continue;
|
|
53
81
|
}
|
|
54
82
|
const mtime = file.mtime ?? (await target.root.drive.entry(file.key))?.mtime ?? 0;
|
|
55
|
-
|
|
83
|
+
const group = `${index}/${rel.includes('/') ? rel.slice(0, rel.indexOf('/')) : ''}`;
|
|
84
|
+
hits.push({ path: shownPath(file.path, anchorOf(ctx)), mtime, group });
|
|
56
85
|
}
|
|
57
86
|
}
|
|
58
87
|
hits.sort((a, b) => b.mtime - a.mtime);
|
|
88
|
+
const sampled = hits.length > GLOB_MATCHES;
|
|
89
|
+
const order = sampled ? interleaveTopLevel(hits) : hits;
|
|
90
|
+
const page = order.slice(offset, offset + limit);
|
|
91
|
+
const truncated = walkTruncated || hits.length > offset + limit || undefined;
|
|
92
|
+
if (!sampled) {
|
|
93
|
+
return { matches: page.map((hit) => hit.path), truncated };
|
|
94
|
+
}
|
|
95
|
+
const groups = new Set(hits.map((hit) => hit.group)).size;
|
|
96
|
+
const shown = new Set(page.map((hit) => hit.group)).size;
|
|
59
97
|
return {
|
|
60
|
-
matches:
|
|
61
|
-
truncated
|
|
98
|
+
matches: page.map((hit) => hit.path),
|
|
99
|
+
truncated,
|
|
100
|
+
note: `showing ${page.length} of ${hits.length} paths, sampled across ${shown} of ${groups} top-level folders; page with offset`
|
|
62
101
|
};
|
|
63
102
|
}
|
|
64
103
|
};
|
|
@@ -3,11 +3,10 @@ import { searchTargets } from '../fs/targets.js';
|
|
|
3
3
|
import { compileGlob } from '../fs/glob.js';
|
|
4
4
|
import { callDenyRoots, defaultRealpath } from '../fs/scope.js';
|
|
5
5
|
import { DriveSearch } from '../fs/search.js';
|
|
6
|
-
import { anchorOf, shownPath, stringArg } from '../fs/tool-path.js';
|
|
6
|
+
import { anchorOf, MAX_LIMIT, pagingError, shownPath, stringArg } from '../fs/tool-path.js';
|
|
7
7
|
import { MAX_WALK_ENTRIES, MAX_WALK_MS } from '../fs/walk.js';
|
|
8
8
|
import { folderArg, folderPaths } from './list-dir.js';
|
|
9
9
|
const GREP_MATCHES = 100;
|
|
10
|
-
const MAX_LIMIT = 500;
|
|
11
10
|
const MAX_LINE_CHARS = 300;
|
|
12
11
|
const TALLY_MATCHES = 5000;
|
|
13
12
|
const SPREAD_MATCHES = 1000;
|
|
@@ -21,8 +20,10 @@ export function createGrepTool({ realpath = defaultRealpath, deniedRoots, maxWal
|
|
|
21
20
|
return {
|
|
22
21
|
name: 'grep',
|
|
23
22
|
toolset: 'files',
|
|
24
|
-
description: 'Search inside the files of the shared folder. The pattern is plain text, case-insensitive
|
|
25
|
-
'"a|b" finds either; set regex for a regular expression.
|
|
23
|
+
description: 'Search inside the files of the shared folder. The pattern is plain text, case-insensitive ' +
|
|
24
|
+
'(set caseSensitive to match case); "a|b" finds either; set regex for a regular expression. ' +
|
|
25
|
+
'Ignored files and cache folders are skipped (set includeIgnored to search them; .git and node_modules stay out). ' +
|
|
26
|
+
'Returns "path:line: text"; output ' +
|
|
26
27
|
'"files" lists matching files with the first line and count, "count" counts per file. Narrow with include (e.g. "*.ts").',
|
|
27
28
|
parameters: {
|
|
28
29
|
type: 'object',
|
|
@@ -37,6 +38,11 @@ export function createGrepTool({ realpath = defaultRealpath, deniedRoots, maxWal
|
|
|
37
38
|
description: 'Only files whose name matches this pattern (optional)'
|
|
38
39
|
},
|
|
39
40
|
regex: { type: 'boolean', description: 'Treat pattern as a regular expression' },
|
|
41
|
+
caseSensitive: { type: 'boolean', description: "Match the pattern's exact case" },
|
|
42
|
+
includeIgnored: {
|
|
43
|
+
type: 'boolean',
|
|
44
|
+
description: 'Also search files .gitignore and cache folders leave out'
|
|
45
|
+
},
|
|
40
46
|
output: {
|
|
41
47
|
type: 'string',
|
|
42
48
|
enum: [...OUTPUTS],
|
|
@@ -58,10 +64,9 @@ export function createGrepTool({ realpath = defaultRealpath, deniedRoots, maxWal
|
|
|
58
64
|
if (args.output !== undefined && !isOutput(args.output)) {
|
|
59
65
|
return `output is one of ${OUTPUTS.join(', ')}`;
|
|
60
66
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
67
|
+
const paging = pagingError(args);
|
|
68
|
+
if (paging) {
|
|
69
|
+
return paging;
|
|
65
70
|
}
|
|
66
71
|
if (args.regex !== true) {
|
|
67
72
|
return null;
|
|
@@ -86,15 +91,18 @@ export function createGrepTool({ realpath = defaultRealpath, deniedRoots, maxWal
|
|
|
86
91
|
const pattern = String(args.pattern);
|
|
87
92
|
const regex = args.regex === true;
|
|
88
93
|
const queries = regex ? [pattern] : alternatives(pattern);
|
|
94
|
+
const caseSensitive = args.caseSensitive === true;
|
|
89
95
|
const output = isOutput(args.output) ? args.output : 'content';
|
|
90
96
|
const limit = Math.min(Number(args.limit ?? GREP_MATCHES), MAX_LIMIT);
|
|
91
97
|
const offset = Number(args.offset ?? 0);
|
|
92
98
|
const spread = args.output === undefined && args.limit === undefined && offset === 0;
|
|
93
99
|
const wanted = spread ? SPREAD_MATCHES : reach(output, offset + limit);
|
|
94
100
|
const query = {
|
|
95
|
-
search: new DriveSearch(queries, { regex }),
|
|
101
|
+
search: new DriveSearch(queries, { regex, caseSensitive }),
|
|
96
102
|
queries,
|
|
97
103
|
regex,
|
|
104
|
+
caseSensitive,
|
|
105
|
+
includeIgnored: args.includeIgnored === true,
|
|
98
106
|
include: stringArg(args, 'include') ? includes(String(args.include), anchorOf(ctx)) : null,
|
|
99
107
|
deny,
|
|
100
108
|
maxEntries: maxWalkEntries
|
|
@@ -195,18 +203,24 @@ function noMatches(pattern, { regex, offset, found }) {
|
|
|
195
203
|
}
|
|
196
204
|
return 'no matches';
|
|
197
205
|
}
|
|
198
|
-
function searchTarget({ root, folder }, { search, queries, regex, include, deny, maxEntries, maxMatches }) {
|
|
206
|
+
function searchTarget({ root, folder }, { search, queries, regex, caseSensitive, includeIgnored, include, deny, maxEntries, maxMatches }) {
|
|
199
207
|
if (root.kind === 'hyper' && root.search && !include) {
|
|
200
208
|
return root.search({
|
|
201
209
|
folder,
|
|
202
210
|
queries: [...queries],
|
|
203
211
|
regex,
|
|
204
|
-
caseSensitive
|
|
205
|
-
includeIgnored
|
|
212
|
+
caseSensitive,
|
|
213
|
+
includeIgnored,
|
|
206
214
|
maxMatches,
|
|
207
215
|
maxEntries,
|
|
208
216
|
maxMs: MAX_WALK_MS
|
|
209
217
|
});
|
|
210
218
|
}
|
|
211
|
-
return search.searchFolder(root, folder, {
|
|
219
|
+
return search.searchFolder(root, folder, {
|
|
220
|
+
maxMatches,
|
|
221
|
+
maxEntries,
|
|
222
|
+
include,
|
|
223
|
+
deny,
|
|
224
|
+
includeIgnored
|
|
225
|
+
});
|
|
212
226
|
}
|