@sixb/agent 0.2.3 → 0.2.5
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 +3 -3
- package/dist/agent-prompt.d.ts.map +1 -1
- package/dist/artifacts/layout-check/layout-check.d.ts +21 -24
- package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -1
- package/dist/artifacts/publish-tool.d.ts +26 -44
- package/dist/artifacts/publish-tool.d.ts.map +1 -1
- package/dist/ask-tool.d.ts +62 -0
- package/dist/ask-tool.d.ts.map +1 -0
- package/dist/attachments.d.ts +1 -1
- package/dist/attachments.d.ts.map +1 -1
- package/dist/background-commands.d.ts +3 -3
- package/dist/background-commands.d.ts.map +1 -1
- package/dist/background-notices.d.ts.map +1 -1
- package/dist/child-files.d.ts +1 -1
- package/dist/child-questions.d.ts +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10457 -7971
- package/dist/index.js.map +46 -38
- package/dist/run-agent-turn.d.ts +3 -14
- package/dist/run-agent-turn.d.ts.map +1 -1
- package/dist/run-conversation-turn.d.ts.map +1 -1
- package/dist/run-environment.d.ts +4 -1
- package/dist/run-environment.d.ts.map +1 -1
- package/dist/run-subagent.d.ts +1 -1
- package/dist/run-subagent.d.ts.map +1 -1
- package/dist/sandbox-api-context.d.ts.map +1 -1
- package/dist/search-script.d.ts +7 -0
- package/dist/search-script.d.ts.map +1 -0
- package/dist/skills/built-in.d.ts.map +1 -1
- package/dist/subagent-tools.d.ts +20 -1
- package/dist/subagent-tools.d.ts.map +1 -1
- package/dist/tools/background.d.ts +50 -18
- package/dist/tools/background.d.ts.map +1 -1
- package/dist/tools/bash.d.ts +1 -0
- package/dist/tools/bash.d.ts.map +1 -1
- package/dist/tools/edit-text.d.ts +22 -0
- package/dist/tools/edit-text.d.ts.map +1 -0
- package/dist/tools/edit.d.ts +54 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/file-writes.d.ts +52 -0
- package/dist/tools/file-writes.d.ts.map +1 -0
- package/dist/tools/glob.d.ts +41 -0
- package/dist/tools/glob.d.ts.map +1 -0
- package/dist/tools/grep.d.ts +64 -0
- package/dist/tools/grep.d.ts.map +1 -0
- package/dist/tools/model-spec.d.ts.map +1 -1
- package/dist/tools/read.d.ts +16 -3
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/sandbox-search.d.ts +26 -0
- package/dist/tools/sandbox-search.d.ts.map +1 -0
- package/dist/tools/search/command.d.ts +15 -0
- package/dist/tools/search/command.d.ts.map +1 -0
- package/dist/tools/search/gitignore.d.ts +28 -0
- package/dist/tools/search/gitignore.d.ts.map +1 -0
- package/dist/tools/search/glob.d.ts +15 -0
- package/dist/tools/search/glob.d.ts.map +1 -0
- package/dist/tools/search/main.d.ts +2 -0
- package/dist/tools/search/main.d.ts.map +1 -0
- package/dist/tools/search/match.d.ts +30 -0
- package/dist/tools/search/match.d.ts.map +1 -0
- package/dist/tools/search/pattern.d.ts +19 -0
- package/dist/tools/search/pattern.d.ts.map +1 -0
- package/dist/tools/search/read.d.ts +62 -0
- package/dist/tools/search/read.d.ts.map +1 -0
- package/dist/tools/search/request.d.ts +80 -0
- package/dist/tools/search/request.d.ts.map +1 -0
- package/dist/tools/search/search.d.ts +4 -0
- package/dist/tools/search/search.d.ts.map +1 -0
- package/dist/tools/search/walk.d.ts +43 -0
- package/dist/tools/search/walk.d.ts.map +1 -0
- package/dist/tools/tool-outputs.d.ts.map +1 -1
- package/dist/tools/view-file.d.ts +20 -23
- package/dist/tools/view-file.d.ts.map +1 -1
- package/dist/tools/web-fetch/tool.d.ts +1 -0
- package/dist/tools/web-fetch/tool.d.ts.map +1 -1
- package/dist/tools/web-search.d.ts +1 -0
- package/dist/tools/web-search.d.ts.map +1 -1
- package/dist/tools/write.d.ts +36 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/turn-inbox.d.ts +75 -0
- package/dist/turn-inbox.d.ts.map +1 -0
- package/package.json +6 -4
- package/src/agent-prompt.ts +10 -9
- package/src/agent-runtime/preflight.ts +1 -0
- package/src/artifacts/layout-check/check.cjs +176 -95
- package/src/artifacts/layout-check/layout-check.ts +118 -76
- package/src/artifacts/publish-tool.ts +123 -192
- package/src/ask-tool.ts +223 -0
- package/src/attachments.ts +7 -6
- package/src/background-commands.ts +5 -5
- package/src/background-notices.ts +5 -12
- package/src/child-files.ts +1 -1
- package/src/child-questions.ts +1 -1
- package/src/context-compaction.ts +4 -1
- package/src/generated/search.mjs +985 -0
- package/src/index.ts +1 -2
- package/src/run-agent-turn.ts +99 -133
- package/src/run-child.ts +1 -1
- package/src/run-conversation-turn.ts +8 -5
- package/src/run-environment.ts +40 -21
- package/src/run-subagent.ts +4 -4
- package/src/sandbox-api-context.ts +3 -0
- package/src/search-script-assets.d.ts +5 -0
- package/src/search-script.ts +9 -0
- package/src/skills/built-in.ts +16 -0
- package/src/skills/sixb-artifacts/SKILL.md +13 -13
- package/src/skills/sixb-artifacts/canvas.md +5 -4
- package/src/skills/sixb-artifacts/document.md +92 -5
- package/src/skills/sixb-artifacts/examples/document/service-proposal/design.css +114 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/document.html +159 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/images/fieldwise.svg +1 -0
- package/src/skills/sixb-artifacts/examples/document/service-proposal/images/harbor-pine.svg +1 -0
- package/src/skills/sixb-artifacts/page.md +2 -2
- package/src/skills/sixb-artifacts/queries.md +2 -1
- package/src/skills/sixb-artifacts/slides.md +2 -2
- package/src/subagent-tools.ts +115 -81
- package/src/tools/background.ts +227 -67
- package/src/tools/bash.ts +2 -1
- package/src/tools/edit-text.ts +241 -0
- package/src/tools/edit.ts +166 -0
- package/src/tools/file-writes.ts +270 -0
- package/src/tools/glob.ts +124 -0
- package/src/tools/grep.ts +190 -0
- package/src/tools/model-spec.ts +13 -13
- package/src/tools/read.ts +75 -9
- package/src/tools/sandbox-search.ts +219 -0
- package/src/tools/search/command.ts +116 -0
- package/src/tools/search/gitignore.ts +101 -0
- package/src/tools/search/glob.ts +90 -0
- package/src/tools/search/main.ts +9 -0
- package/src/tools/search/match.ts +278 -0
- package/src/tools/search/pattern.ts +178 -0
- package/src/tools/search/read.ts +237 -0
- package/src/tools/search/request.ts +101 -0
- package/src/tools/search/search.ts +297 -0
- package/src/tools/search/walk.ts +145 -0
- package/src/tools/tool-outputs.ts +9 -4
- package/src/tools/view-file.ts +78 -80
- package/src/tools/web-fetch/tool.ts +2 -1
- package/src/tools/web-search.ts +1 -0
- package/src/tools/write.ts +95 -0
- package/src/turn-inbox.ts +283 -0
- package/dist/artifacts/board-previews.d.ts +0 -41
- package/dist/artifacts/board-previews.d.ts.map +0 -1
- package/dist/ask-parent-tool.d.ts +0 -36
- package/dist/ask-parent-tool.d.ts.map +0 -1
- package/src/artifacts/board-previews.ts +0 -218
- package/src/ask-parent-tool.ts +0 -100
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type GlobSearchRequest,
|
|
3
|
+
type GrepSearchRequest,
|
|
4
|
+
SearchError,
|
|
5
|
+
type SearchRequest,
|
|
6
|
+
} from "./request"
|
|
7
|
+
import { globFiles, grepFiles } from "./search"
|
|
8
|
+
|
|
9
|
+
const USAGE = "Usage: search.mjs <request JSON>"
|
|
10
|
+
|
|
11
|
+
const KEYS: Readonly<Record<SearchRequest["tool"], readonly string[]>> = {
|
|
12
|
+
glob: ["tool", "pattern", "path", "contextDirectory", "timeoutMs"],
|
|
13
|
+
grep: ["tool", "pattern", "path", "include", "literal", "contextDirectory", "timeoutMs"],
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** What the process prints, and the code it exits with. */
|
|
17
|
+
export interface SearchCommandResult {
|
|
18
|
+
readonly exitCode: number
|
|
19
|
+
readonly stdout: string
|
|
20
|
+
readonly stderr: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `search.mjs '<request JSON>'`: the Agent's `glob` and `grep`, the same in every sandbox. The
|
|
25
|
+
* request is one JSON argument, so no shell quoting reaches a pattern; the result is one JSON
|
|
26
|
+
* line. A search that cannot run exits 2 with `{"error":{"code","message"}}` on stderr: a request
|
|
27
|
+
* it does not understand is `invalid_arguments`, and a search the request names wrongly has its
|
|
28
|
+
* own code ({@link SearchError}). Anything else exits 3, as `internal_error`.
|
|
29
|
+
*/
|
|
30
|
+
export async function runSearchCommand(
|
|
31
|
+
args: readonly string[],
|
|
32
|
+
workingDirectory: string
|
|
33
|
+
): Promise<SearchCommandResult> {
|
|
34
|
+
try {
|
|
35
|
+
const request = parseRequest(args)
|
|
36
|
+
const result =
|
|
37
|
+
request.tool === "glob"
|
|
38
|
+
? globFiles(request, workingDirectory)
|
|
39
|
+
: await grepFiles(request, workingDirectory)
|
|
40
|
+
|
|
41
|
+
return { exitCode: 0, stdout: `${JSON.stringify(result)}\n`, stderr: "" }
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error instanceof SearchError) return failure(2, error.code, error.message)
|
|
44
|
+
if (error instanceof RequestError) return failure(2, "invalid_arguments", error.message)
|
|
45
|
+
|
|
46
|
+
const message = error instanceof Error ? error.message : "The search failed unexpectedly."
|
|
47
|
+
return failure(3, "internal_error", message)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class RequestError extends Error {}
|
|
52
|
+
|
|
53
|
+
function failure(exitCode: number, code: string, message: string): SearchCommandResult {
|
|
54
|
+
return { exitCode, stdout: "", stderr: `${JSON.stringify({ error: { code, message } })}\n` }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function fail(message: string): never {
|
|
58
|
+
throw new RequestError(message)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function parseRequest(args: readonly string[]): SearchRequest {
|
|
62
|
+
if (args.length !== 1) fail(USAGE)
|
|
63
|
+
|
|
64
|
+
let value: unknown
|
|
65
|
+
try {
|
|
66
|
+
value = JSON.parse(args[0] as string)
|
|
67
|
+
} catch {
|
|
68
|
+
fail("The search request is not valid JSON.")
|
|
69
|
+
}
|
|
70
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
71
|
+
fail("The search request must be a JSON object.")
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const input = value as Record<string, unknown>
|
|
75
|
+
const tool = input.tool
|
|
76
|
+
if (tool !== "glob" && tool !== "grep") fail("tool must be 'glob' or 'grep'.")
|
|
77
|
+
for (const key of Object.keys(input)) {
|
|
78
|
+
if (!KEYS[tool].includes(key)) fail(`The ${tool} request has an unknown property '${key}'.`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const pattern = nonEmptyString(input.pattern, "pattern")
|
|
82
|
+
const timeoutMs = input.timeoutMs
|
|
83
|
+
if (typeof timeoutMs !== "number" || !Number.isSafeInteger(timeoutMs) || timeoutMs < 0) {
|
|
84
|
+
fail("timeoutMs must be a non-negative integer.")
|
|
85
|
+
}
|
|
86
|
+
const path = optionalString(input.path, "path")
|
|
87
|
+
const contextDirectory = optionalString(input.contextDirectory, "contextDirectory")
|
|
88
|
+
const scope = {
|
|
89
|
+
timeoutMs,
|
|
90
|
+
...(path === undefined ? {} : { path }),
|
|
91
|
+
...(contextDirectory === undefined ? {} : { contextDirectory }),
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (tool === "glob") return { tool, pattern, ...scope } satisfies GlobSearchRequest
|
|
95
|
+
|
|
96
|
+
if (input.literal !== undefined && typeof input.literal !== "boolean") {
|
|
97
|
+
fail("literal must be true or false.")
|
|
98
|
+
}
|
|
99
|
+
const include = optionalString(input.include, "include")
|
|
100
|
+
return {
|
|
101
|
+
tool,
|
|
102
|
+
pattern,
|
|
103
|
+
...scope,
|
|
104
|
+
...(include === undefined ? {} : { include }),
|
|
105
|
+
...(input.literal === undefined ? {} : { literal: input.literal }),
|
|
106
|
+
} satisfies GrepSearchRequest
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function nonEmptyString(value: unknown, field: string): string {
|
|
110
|
+
if (typeof value !== "string" || value === "") fail(`${field} must be a non-empty string.`)
|
|
111
|
+
return value
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function optionalString(value: unknown, field: string): string | undefined {
|
|
115
|
+
return value === undefined ? undefined : nonEmptyString(value, field)
|
|
116
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { compilePattern, PatternError, pathPatternSource, segmentSource } from "./pattern"
|
|
2
|
+
|
|
3
|
+
/** One line of a `.gitignore`. */
|
|
4
|
+
export interface IgnoreRule {
|
|
5
|
+
/** `!pattern`: brings back what an earlier rule excluded. */
|
|
6
|
+
readonly negated: boolean
|
|
7
|
+
/** `pattern/`: matches folders only. */
|
|
8
|
+
readonly folderOnly: boolean
|
|
9
|
+
/** No `/` but a trailing one: matches a name at any depth below the file's folder. */
|
|
10
|
+
readonly anyDepth: boolean
|
|
11
|
+
readonly regex: RegExp
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A `.gitignore` and the folder it applies to, an absolute path. */
|
|
15
|
+
export interface IgnoreFile {
|
|
16
|
+
readonly folder: string
|
|
17
|
+
readonly rules: readonly IgnoreRule[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Parse a `.gitignore` as git reads it. A line that is not a valid pattern is skipped, as git
|
|
22
|
+
* skips it.
|
|
23
|
+
*/
|
|
24
|
+
export function parseGitignore(text: string): IgnoreRule[] {
|
|
25
|
+
const rules: IgnoreRule[] = []
|
|
26
|
+
|
|
27
|
+
// Git drops a UTF-8 byte order mark, which would otherwise join the first pattern.
|
|
28
|
+
const body = text.startsWith("\uFEFF") ? text.slice(1) : text
|
|
29
|
+
|
|
30
|
+
for (const rawLine of body.split("\n")) {
|
|
31
|
+
const rule = parseLine(rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine)
|
|
32
|
+
if (rule) rules.push(rule)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return rules
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function parseLine(rawLine: string): IgnoreRule | undefined {
|
|
39
|
+
// `\#` and `\!` stay escaped: the pattern compiler reads them as the characters themselves.
|
|
40
|
+
if (rawLine === "" || rawLine.startsWith("#")) return undefined
|
|
41
|
+
|
|
42
|
+
let line = withoutTrailingSpaces(rawLine)
|
|
43
|
+
const negated = line.startsWith("!")
|
|
44
|
+
if (negated) line = line.slice(1)
|
|
45
|
+
|
|
46
|
+
const folderOnly = line.endsWith("/")
|
|
47
|
+
line = line.replace(/\/+$/, "")
|
|
48
|
+
|
|
49
|
+
// A `/` at the start or in the middle anchors the pattern to the file's folder.
|
|
50
|
+
const anyDepth = !line.includes("/")
|
|
51
|
+
line = line.replace(/^\//, "")
|
|
52
|
+
if (!line) return undefined
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const regex = compilePattern(anyDepth ? segmentSource(line) : pathPatternSource(line))
|
|
56
|
+
return { negated, folderOnly, anyDepth, regex }
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (error instanceof PatternError) return undefined
|
|
59
|
+
throw error
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Trailing spaces are dropped, unless a backslash escapes them. */
|
|
64
|
+
function withoutTrailingSpaces(line: string): string {
|
|
65
|
+
let end = line.length
|
|
66
|
+
|
|
67
|
+
while (end > 0 && line[end - 1] === " ") {
|
|
68
|
+
let backslashes = 0
|
|
69
|
+
while (line[end - 2 - backslashes] === "\\") backslashes += 1
|
|
70
|
+
if (backslashes % 2 === 1) break
|
|
71
|
+
end -= 1
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return line.slice(0, end)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Whether the `.gitignore` files that apply exclude an entry, given as an absolute path. Files
|
|
79
|
+
* come from the outermost folder in, and a later rule wins over an earlier one, so a deeper file
|
|
80
|
+
* overrides its parents. What lies inside an excluded folder is never asked about: the walk does
|
|
81
|
+
* not enter it, so a rule cannot bring it back, as in git.
|
|
82
|
+
*/
|
|
83
|
+
export function isIgnored(files: readonly IgnoreFile[], path: string, isFolder: boolean): boolean {
|
|
84
|
+
const name = path.slice(path.lastIndexOf("/") + 1)
|
|
85
|
+
let ignored = false
|
|
86
|
+
|
|
87
|
+
for (const file of files) {
|
|
88
|
+
const prefix = file.folder.endsWith("/") ? file.folder : `${file.folder}/`
|
|
89
|
+
if (!path.startsWith(prefix)) continue
|
|
90
|
+
const relative = path.slice(prefix.length)
|
|
91
|
+
|
|
92
|
+
for (const rule of file.rules) {
|
|
93
|
+
if (rule.folderOnly && !isFolder) continue
|
|
94
|
+
// Only a rule that would change the verdict so far needs testing.
|
|
95
|
+
if (rule.negated !== ignored) continue
|
|
96
|
+
if (rule.regex.test(rule.anyDepth ? name : relative)) ignored = !rule.negated
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return ignored
|
|
101
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compilePattern,
|
|
3
|
+
expandBraces,
|
|
4
|
+
PatternError,
|
|
5
|
+
pathPatternSource,
|
|
6
|
+
segmentSource,
|
|
7
|
+
} from "./pattern"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A `glob` pattern, matched against paths relative to where the search starts. A pattern without
|
|
11
|
+
* `/` matches a name at any depth (`*.md` finds every Markdown file), as in `.gitignore`; one with
|
|
12
|
+
* `/` matches from the start, with `**` for any number of folders.
|
|
13
|
+
*/
|
|
14
|
+
export interface GlobMatcher {
|
|
15
|
+
matches(relativePath: string): boolean
|
|
16
|
+
/** Whether a folder, relative to where the search starts, may hold a match. */
|
|
17
|
+
mayContain(relativeFolder: string): boolean
|
|
18
|
+
/** A segment starts with `.`: the pattern looks for hidden files. */
|
|
19
|
+
readonly namesHidden: boolean
|
|
20
|
+
readonly namesNodeModules: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface Alternative {
|
|
24
|
+
/** Matched against the name alone, at any depth. */
|
|
25
|
+
readonly anyDepth: boolean
|
|
26
|
+
readonly regex: RegExp
|
|
27
|
+
readonly segments: readonly string[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function compileGlob(pattern: string): GlobMatcher {
|
|
31
|
+
const alternatives = expandBraces(pattern).map(compileAlternative)
|
|
32
|
+
const segments = alternatives.flatMap((alternative) => alternative.segments)
|
|
33
|
+
const folderSegments = new Map<string, RegExp>()
|
|
34
|
+
|
|
35
|
+
// One folder segment against one pattern segment, compiled once for the whole walk.
|
|
36
|
+
const segmentMatches = (patternSegment: string, name: string): boolean => {
|
|
37
|
+
let regex = folderSegments.get(patternSegment)
|
|
38
|
+
if (!regex) {
|
|
39
|
+
regex = compilePattern(segmentSource(patternSegment))
|
|
40
|
+
folderSegments.set(patternSegment, regex)
|
|
41
|
+
}
|
|
42
|
+
return regex.test(name)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
matches(relativePath) {
|
|
47
|
+
const name = relativePath.slice(relativePath.lastIndexOf("/") + 1)
|
|
48
|
+
return alternatives.some((alternative) =>
|
|
49
|
+
alternative.regex.test(alternative.anyDepth ? name : relativePath)
|
|
50
|
+
)
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
mayContain(relativeFolder) {
|
|
54
|
+
const names = relativeFolder.split("/")
|
|
55
|
+
return alternatives.some((alternative) => {
|
|
56
|
+
if (alternative.anyDepth) return true
|
|
57
|
+
|
|
58
|
+
for (const [index, name] of names.entries()) {
|
|
59
|
+
const segment = alternative.segments[index]
|
|
60
|
+
if (segment === undefined) return false
|
|
61
|
+
if (segment === "**") return true
|
|
62
|
+
if (!segmentMatches(segment, name)) return false
|
|
63
|
+
}
|
|
64
|
+
// The folder matched as far as it goes: the pattern must still name what is inside.
|
|
65
|
+
return alternative.segments.length > names.length
|
|
66
|
+
})
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
namesHidden: segments.some((segment) => segment.startsWith(".")),
|
|
70
|
+
namesNodeModules: segments.some((segment) => segment.includes("node_modules")),
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function compileAlternative(pattern: string): Alternative {
|
|
75
|
+
let relative = pattern
|
|
76
|
+
while (relative.startsWith("./")) relative = relative.slice(2)
|
|
77
|
+
|
|
78
|
+
// A leading `/` anchors the pattern where the search starts, as in `.gitignore`.
|
|
79
|
+
const anchored = relative.startsWith("/")
|
|
80
|
+
if (anchored) relative = relative.replace(/^\/+/, "")
|
|
81
|
+
if (!relative) throw new PatternError("it names no file")
|
|
82
|
+
|
|
83
|
+
const anyDepth = !anchored && !relative.includes("/")
|
|
84
|
+
const segments = relative.split("/")
|
|
85
|
+
return {
|
|
86
|
+
anyDepth,
|
|
87
|
+
regex: compilePattern(anyDepth ? segmentSource(relative) : pathPatternSource(relative)),
|
|
88
|
+
segments,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The engine of the Agent's `glob` and `grep`, as a run installs it at `lib/search.mjs` and runs
|
|
2
|
+
// it on Bun or Node: `bun run generate:search-script` bundles it into src/generated/search.mjs.
|
|
3
|
+
import { runSearchCommand } from "./command"
|
|
4
|
+
|
|
5
|
+
const result = await runSearchCommand(process.argv.slice(2), process.cwd())
|
|
6
|
+
|
|
7
|
+
process.stdout.write(result.stdout)
|
|
8
|
+
process.stderr.write(result.stderr)
|
|
9
|
+
process.exitCode = result.exitCode
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { SEARCH_LIMITS, SearchError } from "./request"
|
|
2
|
+
|
|
3
|
+
/** Where a line first matched `grep`'s pattern, in UTF-16 units. */
|
|
4
|
+
export interface LineMatch {
|
|
5
|
+
readonly index: number
|
|
6
|
+
readonly length: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface TextMatcher {
|
|
10
|
+
/**
|
|
11
|
+
* Where a line first matches, over its first {@link SEARCH_LIMITS.matchCharacters} characters.
|
|
12
|
+
*/
|
|
13
|
+
line(line: string): LineMatch | undefined
|
|
14
|
+
/**
|
|
15
|
+
* Whether any line of a file may match: false only when none does, so a file that cannot match
|
|
16
|
+
* is never split into lines. It may say true of a file with no matching line.
|
|
17
|
+
*/
|
|
18
|
+
mayMatch(text: string): boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* `grep`'s pattern, matched line by line. Case is smart: ignored when the pattern has no uppercase
|
|
23
|
+
* letter. Literal text is a plain substring search, which no line can slow down.
|
|
24
|
+
*/
|
|
25
|
+
export function textMatcher(pattern: string, literal: boolean): TextMatcher {
|
|
26
|
+
return literal ? literalMatcher(pattern) : regexMatcher(pattern)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function literalMatcher(pattern: string): TextMatcher {
|
|
30
|
+
const ignoreCase = !/\p{Lu}/u.test(pattern)
|
|
31
|
+
const needle = ignoreCase ? pattern.toLowerCase() : pattern
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
line(line) {
|
|
35
|
+
const matched = linePrefix(line, SEARCH_LIMITS.matchCharacters)
|
|
36
|
+
const index = (ignoreCase ? matched.toLowerCase() : matched).indexOf(needle)
|
|
37
|
+
|
|
38
|
+
// Lowercasing lengthens a few characters (İ): the index is then near the match, not on it.
|
|
39
|
+
return index < 0 ? undefined : { index: Math.min(index, line.length), length: needle.length }
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
// A line's text is part of the file's. Lowercasing is the same character by character, but
|
|
43
|
+
// for Σ, which reads what follows it: only where a line is cut at 4,000 characters does that
|
|
44
|
+
// differ from the file's text, and a file with such a line is searched line by line.
|
|
45
|
+
mayMatch(text) {
|
|
46
|
+
if (!ignoreCase) return text.includes(needle)
|
|
47
|
+
if (hasLineOver(text, SEARCH_LIMITS.matchCharacters)) return true
|
|
48
|
+
|
|
49
|
+
return text.toLowerCase().includes(needle)
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function regexMatcher(pattern: string): TextMatcher {
|
|
55
|
+
const regex = lineRegex(pattern)
|
|
56
|
+
const whole = wholeTextRegex(pattern, regex)
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
line(line) {
|
|
60
|
+
const found = regex.exec(linePrefix(line, SEARCH_LIMITS.matchCharacters))
|
|
61
|
+
return found ? { index: found.index, length: found[0].length } : undefined
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
mayMatch(text) {
|
|
65
|
+
if (!whole || !linesAreWhole(text)) return true
|
|
66
|
+
return whole.test(text)
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Unicode mode first, so `\p{…}` works; without it when that refuses what plain mode accepts.
|
|
73
|
+
* Case is ignored when the pattern writes no uppercase letter and no property escape: `\p{Lu}`
|
|
74
|
+
* ignoring case would match every cased letter.
|
|
75
|
+
*/
|
|
76
|
+
function lineRegex(pattern: string): RegExp {
|
|
77
|
+
const written = pattern.replace(/\\(?:[pP]\{[^}]*\}|.)/g, "")
|
|
78
|
+
const flags = /\p{Lu}/u.test(written) || /\\[pP]\{/.test(pattern) ? "" : "i"
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
return new RegExp(pattern, `${flags}u`)
|
|
82
|
+
} catch {}
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
return new RegExp(pattern, flags)
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
88
|
+
throw new SearchError(
|
|
89
|
+
"invalid_pattern",
|
|
90
|
+
message.replace(/^Invalid regular expression: (?:\/.*\/[a-z]*: )?/, "")
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The line regex with `m`, to test a whole file at once; nothing for a pattern written with a
|
|
97
|
+
* lookaround or a modifier group, or that can match a line break. The test stands in for the lines
|
|
98
|
+
* only on a file whose lines are whole ({@link linesAreWhole}).
|
|
99
|
+
*
|
|
100
|
+
* Why a line's match is then a match in the whole text: the line is a stretch of it, and the
|
|
101
|
+
* same characters match the same way. The pattern consumes no line break, so a match never
|
|
102
|
+
* leaves the line: what can still differ is what its assertions read.
|
|
103
|
+
*
|
|
104
|
+
* - `^` and `$`: with `m`, they hold where a line starts or ends, `\n` and `\r\n` alike. With no
|
|
105
|
+
* lone `\r` and no U+2028 or U+2029 in the file, those are exactly a line's edges: in the line
|
|
106
|
+
* and in the whole text, they hold at the same places.
|
|
107
|
+
* - `\b` and `\B`: past a line's edge, the whole text holds `\n` or `\r`, which is no word
|
|
108
|
+
* character, as nothing is: they read the same at a line's edges.
|
|
109
|
+
* - Backreferences repeat what their group matched in the line.
|
|
110
|
+
* - A lookaround could say no where an assertion says yes, and is rare in a search: a pattern
|
|
111
|
+
* with one is searched line by line, so nothing above rests on how one reads. A modifier group
|
|
112
|
+
* changes the flags themselves: `(?-m:^)` turns `m` back off.
|
|
113
|
+
*
|
|
114
|
+
* A match in the whole text that no line holds, across lines, is never reported: it only costs a
|
|
115
|
+
* file split into lines.
|
|
116
|
+
*
|
|
117
|
+
* Why the test stays fast: a line bounds what each try can read, and with it how far a regex
|
|
118
|
+
* backtracks. A pattern that can match a line break backtracks over the whole file instead —
|
|
119
|
+
* `[^x]*y` takes seconds on 100 KB — which is the other reason it is searched line by line.
|
|
120
|
+
*/
|
|
121
|
+
function wholeTextRegex(pattern: string, line: RegExp): RegExp | undefined {
|
|
122
|
+
if (!readsOneLine(pattern)) return undefined
|
|
123
|
+
return new RegExp(line.source, `${line.flags}m`)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Whether the line breaks `m` sees are the lines `grep` splits, each matched whole: no line is
|
|
128
|
+
* cut at {@link SEARCH_LIMITS.matchCharacters}, and no lone `\r`, U+2028 or U+2029, which end a
|
|
129
|
+
* line for `^` and `$` but stay inside one for {@link lines}.
|
|
130
|
+
*/
|
|
131
|
+
function linesAreWhole(text: string): boolean {
|
|
132
|
+
return !hasLineOver(text, SEARCH_LIMITS.matchCharacters) && !hasLoneBreak(text)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A `\r` not followed by `\n`, U+2028 or U+2029. Searched for as text, faster than a regex. */
|
|
136
|
+
function hasLoneBreak(text: string): boolean {
|
|
137
|
+
if (text.includes("\u2028") || text.includes("\u2029")) return true
|
|
138
|
+
|
|
139
|
+
for (let index = text.indexOf("\r"); index >= 0; index = text.indexOf("\r", index + 1)) {
|
|
140
|
+
if (text.charCodeAt(index + 1) !== LINE_FEED) return true
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return false
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const LINE_FEED = 0x0a
|
|
147
|
+
|
|
148
|
+
/** Escapes that never match a line break, outside a class and inside one. */
|
|
149
|
+
const ESCAPES = new Set(["b", "B", "d", "w", "S", "k"])
|
|
150
|
+
const CLASS_ESCAPES = new Set(["d", "w", "S"])
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Whether a pattern, as written, has no lookaround, no modifier group, and cannot match `\n`,
|
|
154
|
+
* `\r`, U+2028 or U+2029. What it cannot rule out says no:
|
|
155
|
+
*
|
|
156
|
+
* - `(?=`, `(?!`, `(?<=`, `(?<!`, and `(?i:`, `(?m:`, `(?s:`, `(?-…:` outside a class.
|
|
157
|
+
* - `.` never matches a line break, without the `s` flag, nor do `\d`, `\w` and `\S`.
|
|
158
|
+
* - A negated class does, unless it names them: `[^…]` says no.
|
|
159
|
+
* - `\s`, `\D`, `\W`, `\n`, `\r`, `\t`, `\v`, `\f`, `\0`, `\x…`, `\u…`, `\c…`, `\p{…}` and digit
|
|
160
|
+
* escapes (an octal `\12` is `\n` without the `u` flag) all may, alone or as a range's end.
|
|
161
|
+
* - So may a control character or a line separator written as itself, escaped or not, and a
|
|
162
|
+
* range that spans one.
|
|
163
|
+
* - `\b` and `\B` are assertions, and `\k<name>` repeats a group's text; in a class, `\b` is a
|
|
164
|
+
* backspace, a control character.
|
|
165
|
+
*/
|
|
166
|
+
function readsOneLine(pattern: string): boolean {
|
|
167
|
+
let inClass = false
|
|
168
|
+
/** The class's last character, a range's start. */
|
|
169
|
+
let previous: number | undefined
|
|
170
|
+
|
|
171
|
+
for (let index = 0; index < pattern.length; index += 1) {
|
|
172
|
+
let code = pattern.charCodeAt(index)
|
|
173
|
+
if (isLineBreakOrControl(code)) return false
|
|
174
|
+
|
|
175
|
+
if (code === BACKSLASH) {
|
|
176
|
+
index += 1
|
|
177
|
+
const escaped = pattern[index] ?? ""
|
|
178
|
+
if (/[A-Za-z0-9]/.test(escaped)) {
|
|
179
|
+
if (!(inClass ? CLASS_ESCAPES : ESCAPES).has(escaped)) return false
|
|
180
|
+
previous = undefined
|
|
181
|
+
continue
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// An escaped symbol is the symbol itself.
|
|
185
|
+
code = pattern.charCodeAt(index)
|
|
186
|
+
if (isLineBreakOrControl(code)) return false
|
|
187
|
+
} else if (!inClass && code === OPEN_PAREN) {
|
|
188
|
+
if (LOOKAROUND_OR_MODIFIER.test(pattern.slice(index, index + 4))) return false
|
|
189
|
+
} else if (!inClass && code === OPEN_BRACKET) {
|
|
190
|
+
if (pattern[index + 1] === "^") return false
|
|
191
|
+
inClass = true
|
|
192
|
+
previous = undefined
|
|
193
|
+
continue
|
|
194
|
+
} else if (inClass && code === CLOSE_BRACKET) {
|
|
195
|
+
inClass = false
|
|
196
|
+
continue
|
|
197
|
+
} else if (inClass && code === DASH && previous !== undefined && index + 1 < pattern.length) {
|
|
198
|
+
const end =
|
|
199
|
+
pattern[index + 1] === "\\" ? pattern.charCodeAt(index + 2) : pattern.charCodeAt(index + 1)
|
|
200
|
+
if (previous <= 0x2029 && end >= 0x2028) return false
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
previous = inClass ? code : undefined
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return true
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** At a `(`: a lookaround, or a group that changes the `i`, `m` or `s` flag. */
|
|
210
|
+
const LOOKAROUND_OR_MODIFIER = /^\(\?(?:[=!]|<[=!]|[ims-])/
|
|
211
|
+
|
|
212
|
+
const OPEN_PAREN = 0x28
|
|
213
|
+
const BACKSLASH = 0x5c
|
|
214
|
+
const OPEN_BRACKET = 0x5b
|
|
215
|
+
const CLOSE_BRACKET = 0x5d
|
|
216
|
+
const DASH = 0x2d
|
|
217
|
+
|
|
218
|
+
function isLineBreakOrControl(code: number): boolean {
|
|
219
|
+
return code < 0x20 || code === 0x2028 || code === 0x2029
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Whether a line of the text, its `\r` included, is longer than `length` UTF-16 units. */
|
|
223
|
+
function hasLineOver(text: string, length: number): boolean {
|
|
224
|
+
if (text.length <= length) return false
|
|
225
|
+
|
|
226
|
+
for (let start = 0; start <= text.length; ) {
|
|
227
|
+
const newline = text.indexOf("\n", start)
|
|
228
|
+
const end = newline < 0 ? text.length : newline
|
|
229
|
+
if (end - start > length) return true
|
|
230
|
+
start = end + 1
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return false
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** A file's lines, without their `\n` or `\r\n`. */
|
|
237
|
+
export function lines(text: string): string[] {
|
|
238
|
+
const split = text.split("\n")
|
|
239
|
+
if (split.at(-1) === "") split.pop()
|
|
240
|
+
return split.map((line) => (line.endsWith("\r") ? line.slice(0, -1) : line))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* A line as `grep` shows it: whole when it fits, else cut to {@link SEARCH_LIMITS.lineCharacters}
|
|
245
|
+
* characters around its first match, so a match far into a minified line is still seen. A cut
|
|
246
|
+
* never splits a character written as two units.
|
|
247
|
+
*/
|
|
248
|
+
export function cutLine(line: string, match: LineMatch): string {
|
|
249
|
+
const budget = SEARCH_LIMITS.lineCharacters
|
|
250
|
+
if (line.length <= budget) return line
|
|
251
|
+
|
|
252
|
+
// The window opens a little before the match, or ends with the line, the leading `…` in the
|
|
253
|
+
// budget; a match within the line's first characters keeps its start.
|
|
254
|
+
const room = budget - 1
|
|
255
|
+
const start =
|
|
256
|
+
match.index + match.length <= budget
|
|
257
|
+
? 0
|
|
258
|
+
: Math.max(0, Math.min(match.index - SEARCH_LIMITS.matchLead, line.length - room))
|
|
259
|
+
if (start === 0) return `${linePrefix(line, budget)}…`
|
|
260
|
+
|
|
261
|
+
const from = isLowSurrogate(line.charCodeAt(start)) ? start + 1 : start
|
|
262
|
+
// One unit past the room, so a character the room would split is seen and left out.
|
|
263
|
+
const window = linePrefix(line.slice(from, from + room + 1), room)
|
|
264
|
+
|
|
265
|
+
return `…${window}${from + window.length < line.length ? "…" : ""}`
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function isLowSurrogate(code: number): boolean {
|
|
269
|
+
return code >= 0xdc00 && code <= 0xdfff
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** A line's first `length` UTF-16 units, never half of a character written as two. */
|
|
273
|
+
function linePrefix(line: string, length: number): string {
|
|
274
|
+
if (line.length <= length) return line
|
|
275
|
+
|
|
276
|
+
const code = line.charCodeAt(length - 1)
|
|
277
|
+
return line.slice(0, code >= 0xd800 && code <= 0xdbff ? length - 1 : length)
|
|
278
|
+
}
|