@sixb/agent 0.2.4 → 0.2.6

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.
Files changed (117) hide show
  1. package/README.md +2 -2
  2. package/dist/agent-prompt.d.ts +13 -3
  3. package/dist/agent-prompt.d.ts.map +1 -1
  4. package/dist/artifacts/layout-check/layout-check.d.ts +21 -24
  5. package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -1
  6. package/dist/artifacts/publish-tool.d.ts.map +1 -1
  7. package/dist/background-commands.d.ts +2 -2
  8. package/dist/context-compaction.d.ts.map +1 -1
  9. package/dist/conversation-messages.d.ts +1 -1
  10. package/dist/conversation-messages.d.ts.map +1 -1
  11. package/dist/index.d.ts +0 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +11082 -9281
  14. package/dist/index.js.map +37 -33
  15. package/dist/login-shell.d.ts +18 -2
  16. package/dist/login-shell.d.ts.map +1 -1
  17. package/dist/model-adapters.d.ts +5 -2
  18. package/dist/model-adapters.d.ts.map +1 -1
  19. package/dist/model-lines.d.ts +1 -0
  20. package/dist/model-lines.d.ts.map +1 -1
  21. package/dist/run-agent-turn.d.ts +3 -14
  22. package/dist/run-agent-turn.d.ts.map +1 -1
  23. package/dist/run-conversation-turn.d.ts.map +1 -1
  24. package/dist/run-environment.d.ts.map +1 -1
  25. package/dist/run-sandbox.d.ts +11 -1
  26. package/dist/run-sandbox.d.ts.map +1 -1
  27. package/dist/run-subagent.d.ts.map +1 -1
  28. package/dist/sandbox-api-context.d.ts.map +1 -1
  29. package/dist/search-script.d.ts +7 -0
  30. package/dist/search-script.d.ts.map +1 -0
  31. package/dist/skills/built-in.d.ts.map +1 -1
  32. package/dist/subagent-tools.d.ts +11 -1
  33. package/dist/subagent-tools.d.ts.map +1 -1
  34. package/dist/tools/bash.d.ts +1 -1
  35. package/dist/tools/bash.d.ts.map +1 -1
  36. package/dist/tools/glob.d.ts +41 -0
  37. package/dist/tools/glob.d.ts.map +1 -0
  38. package/dist/tools/grep.d.ts +64 -0
  39. package/dist/tools/grep.d.ts.map +1 -0
  40. package/dist/tools/model-spec.d.ts.map +1 -1
  41. package/dist/tools/sandbox-search.d.ts +26 -0
  42. package/dist/tools/sandbox-search.d.ts.map +1 -0
  43. package/dist/tools/search/command.d.ts +15 -0
  44. package/dist/tools/search/command.d.ts.map +1 -0
  45. package/dist/tools/search/gitignore.d.ts +28 -0
  46. package/dist/tools/search/gitignore.d.ts.map +1 -0
  47. package/dist/tools/search/glob.d.ts +15 -0
  48. package/dist/tools/search/glob.d.ts.map +1 -0
  49. package/dist/tools/search/main.d.ts +2 -0
  50. package/dist/tools/search/main.d.ts.map +1 -0
  51. package/dist/tools/search/match.d.ts +30 -0
  52. package/dist/tools/search/match.d.ts.map +1 -0
  53. package/dist/tools/search/pattern.d.ts +19 -0
  54. package/dist/tools/search/pattern.d.ts.map +1 -0
  55. package/dist/tools/search/read.d.ts +62 -0
  56. package/dist/tools/search/read.d.ts.map +1 -0
  57. package/dist/tools/search/request.d.ts +80 -0
  58. package/dist/tools/search/request.d.ts.map +1 -0
  59. package/dist/tools/search/search.d.ts +4 -0
  60. package/dist/tools/search/search.d.ts.map +1 -0
  61. package/dist/tools/search/walk.d.ts +43 -0
  62. package/dist/tools/search/walk.d.ts.map +1 -0
  63. package/dist/tools/tool-outputs.d.ts.map +1 -1
  64. package/dist/turn-inbox.d.ts +75 -0
  65. package/dist/turn-inbox.d.ts.map +1 -0
  66. package/package.json +7 -5
  67. package/src/agent-prompt.ts +63 -28
  68. package/src/agent-runtime/preflight.ts +1 -0
  69. package/src/artifacts/layout-check/check.cjs +176 -95
  70. package/src/artifacts/layout-check/layout-check.ts +118 -76
  71. package/src/artifacts/publish-tool.ts +48 -99
  72. package/src/background-commands.ts +8 -6
  73. package/src/context-compaction.ts +1 -0
  74. package/src/conversation-messages.ts +11 -4
  75. package/src/generated/search.mjs +985 -0
  76. package/src/index.ts +0 -1
  77. package/src/login-shell.ts +30 -3
  78. package/src/model-adapters.ts +23 -4
  79. package/src/model-lines.ts +1 -1
  80. package/src/run-agent-turn.ts +85 -129
  81. package/src/run-conversation-turn.ts +15 -6
  82. package/src/run-environment.ts +42 -11
  83. package/src/run-sandbox.ts +60 -1
  84. package/src/run-subagent.ts +9 -2
  85. package/src/sandbox-api-context.ts +10 -1
  86. package/src/search-script-assets.d.ts +5 -0
  87. package/src/search-script.ts +9 -0
  88. package/src/skills/built-in.ts +16 -0
  89. package/src/skills/sixb-artifacts/SKILL.md +7 -7
  90. package/src/skills/sixb-artifacts/canvas.md +3 -2
  91. package/src/skills/sixb-artifacts/document.md +92 -5
  92. package/src/skills/sixb-artifacts/examples/document/service-proposal/design.css +114 -0
  93. package/src/skills/sixb-artifacts/examples/document/service-proposal/document.html +159 -0
  94. package/src/skills/sixb-artifacts/examples/document/service-proposal/images/fieldwise.svg +1 -0
  95. package/src/skills/sixb-artifacts/examples/document/service-proposal/images/harbor-pine.svg +1 -0
  96. package/src/skills/sixb-artifacts/queries.md +2 -1
  97. package/src/subagent-tools.ts +23 -5
  98. package/src/tools/bash.ts +9 -10
  99. package/src/tools/glob.ts +124 -0
  100. package/src/tools/grep.ts +190 -0
  101. package/src/tools/model-spec.ts +4 -0
  102. package/src/tools/sandbox-search.ts +219 -0
  103. package/src/tools/search/command.ts +116 -0
  104. package/src/tools/search/gitignore.ts +101 -0
  105. package/src/tools/search/glob.ts +90 -0
  106. package/src/tools/search/main.ts +9 -0
  107. package/src/tools/search/match.ts +278 -0
  108. package/src/tools/search/pattern.ts +178 -0
  109. package/src/tools/search/read.ts +237 -0
  110. package/src/tools/search/request.ts +101 -0
  111. package/src/tools/search/search.ts +297 -0
  112. package/src/tools/search/walk.ts +145 -0
  113. package/src/tools/tool-outputs.ts +7 -2
  114. package/src/turn-inbox.ts +283 -0
  115. package/dist/artifacts/board-previews.d.ts +0 -41
  116. package/dist/artifacts/board-previews.d.ts.map +0 -1
  117. package/src/artifacts/board-previews.ts +0 -218
@@ -0,0 +1,985 @@
1
+ // src/tools/search/request.ts
2
+ var SEARCH_LIMITS = {
3
+ files: 100,
4
+ lines: 100,
5
+ linesPerFile: 20,
6
+ lineCharacters: 500,
7
+ matchLead: 40,
8
+ matchCharacters: 4000,
9
+ textBytes: 24 * 1024,
10
+ fileBytes: 10 * 1024 * 1024,
11
+ binaryProbeBytes: 8 * 1024
12
+ };
13
+
14
+ class SearchError extends Error {
15
+ code;
16
+ constructor(code, message) {
17
+ super(message);
18
+ this.code = code;
19
+ this.name = "SearchError";
20
+ }
21
+ }
22
+
23
+ // src/tools/search/search.ts
24
+ import { Buffer as Buffer3 } from "node:buffer";
25
+ import { lstatSync as lstatSync2, realpathSync, statSync as statSync2 } from "node:fs";
26
+ import { basename, dirname, relative, resolve } from "node:path";
27
+
28
+ // src/tools/search/pattern.ts
29
+ var MAX_ALTERNATIVES = 256;
30
+
31
+ class PatternError extends Error {
32
+ }
33
+ function pathPatternSource(pattern) {
34
+ const segments = pattern.split("/");
35
+ let source = "";
36
+ segments.forEach((segment, index) => {
37
+ const last = index === segments.length - 1;
38
+ if (segment === "**") {
39
+ source += last ? ".*" : "(?:[^/]*/)*";
40
+ return;
41
+ }
42
+ source += segmentSource(segment);
43
+ if (!last)
44
+ source += "/";
45
+ });
46
+ return source;
47
+ }
48
+ function segmentSource(segment) {
49
+ let source = "";
50
+ for (let index = 0;index < segment.length; index += 1) {
51
+ const char = segment[index];
52
+ if (char === "\\") {
53
+ index += 1;
54
+ source += escapeRegExp(segment[index] ?? "\\");
55
+ } else if (char === "*") {
56
+ while (segment[index + 1] === "*")
57
+ index += 1;
58
+ source += "[^/]*";
59
+ } else if (char === "?") {
60
+ source += "[^/]";
61
+ } else if (char === "[") {
62
+ const parsed = characterClass(segment, index);
63
+ if (parsed) {
64
+ source += parsed.source;
65
+ index = parsed.end;
66
+ } else {
67
+ source += "\\[";
68
+ }
69
+ } else {
70
+ source += escapeRegExp(char);
71
+ }
72
+ }
73
+ return source;
74
+ }
75
+ function characterClass(segment, start) {
76
+ let index = start + 1;
77
+ const negated = segment[index] === "!" || segment[index] === "^";
78
+ if (negated)
79
+ index += 1;
80
+ let body = "";
81
+ if (segment[index] === "]") {
82
+ body += "\\]";
83
+ index += 1;
84
+ }
85
+ for (;index < segment.length; index += 1) {
86
+ const char = segment[index];
87
+ if (char === "]") {
88
+ return { source: negated ? `[^/${body}]` : `[${body}]`, end: index };
89
+ }
90
+ if (char === "\\" && index + 1 < segment.length) {
91
+ index += 1;
92
+ body += escapeClassChar(segment[index]);
93
+ } else {
94
+ body += char === "-" ? "-" : escapeClassChar(char);
95
+ }
96
+ }
97
+ return;
98
+ }
99
+ function expandBraces(pattern) {
100
+ const expanded = [];
101
+ expandInto(pattern, expanded);
102
+ return expanded;
103
+ }
104
+ function expandInto(pattern, into) {
105
+ for (let index = 0;index < pattern.length; index += 1) {
106
+ const char = pattern[index];
107
+ if (char === "\\") {
108
+ index += 1;
109
+ continue;
110
+ }
111
+ if (char !== "{")
112
+ continue;
113
+ const group = braceGroup(pattern, index);
114
+ if (!group)
115
+ continue;
116
+ const prefix = pattern.slice(0, index);
117
+ const suffix = pattern.slice(group.end + 1);
118
+ for (const option of group.options)
119
+ expandInto(`${prefix}${option}${suffix}`, into);
120
+ return;
121
+ }
122
+ if (into.length >= MAX_ALTERNATIVES) {
123
+ throw new PatternError(`it names more than ${MAX_ALTERNATIVES} alternatives with {…}`);
124
+ }
125
+ into.push(pattern);
126
+ }
127
+ function braceGroup(pattern, start) {
128
+ const options = [];
129
+ let depth = 0;
130
+ let optionStart = start + 1;
131
+ for (let index = start + 1;index < pattern.length; index += 1) {
132
+ const char = pattern[index];
133
+ if (char === "\\") {
134
+ index += 1;
135
+ } else if (char === "{") {
136
+ depth += 1;
137
+ } else if (char === "}" && depth > 0) {
138
+ depth -= 1;
139
+ } else if (char === "," && depth === 0) {
140
+ options.push(pattern.slice(optionStart, index));
141
+ optionStart = index + 1;
142
+ } else if (char === "}") {
143
+ if (options.length === 0)
144
+ return;
145
+ options.push(pattern.slice(optionStart, index));
146
+ return { options, end: index };
147
+ }
148
+ }
149
+ return;
150
+ }
151
+ function compilePattern(source, flags = "") {
152
+ try {
153
+ return new RegExp(`^${source}$`, flags);
154
+ } catch (error) {
155
+ throw new PatternError(error instanceof Error ? error.message : String(error));
156
+ }
157
+ }
158
+ function escapeRegExp(text) {
159
+ return text.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
160
+ }
161
+ function escapeClassChar(char) {
162
+ return /[\\\]^[-]/.test(char) ? `\\${char}` : char;
163
+ }
164
+
165
+ // src/tools/search/glob.ts
166
+ function compileGlob(pattern) {
167
+ const alternatives = expandBraces(pattern).map(compileAlternative);
168
+ const segments = alternatives.flatMap((alternative) => alternative.segments);
169
+ const folderSegments = new Map;
170
+ const segmentMatches = (patternSegment, name) => {
171
+ let regex = folderSegments.get(patternSegment);
172
+ if (!regex) {
173
+ regex = compilePattern(segmentSource(patternSegment));
174
+ folderSegments.set(patternSegment, regex);
175
+ }
176
+ return regex.test(name);
177
+ };
178
+ return {
179
+ matches(relativePath) {
180
+ const name = relativePath.slice(relativePath.lastIndexOf("/") + 1);
181
+ return alternatives.some((alternative) => alternative.regex.test(alternative.anyDepth ? name : relativePath));
182
+ },
183
+ mayContain(relativeFolder) {
184
+ const names = relativeFolder.split("/");
185
+ return alternatives.some((alternative) => {
186
+ if (alternative.anyDepth)
187
+ return true;
188
+ for (const [index, name] of names.entries()) {
189
+ const segment = alternative.segments[index];
190
+ if (segment === undefined)
191
+ return false;
192
+ if (segment === "**")
193
+ return true;
194
+ if (!segmentMatches(segment, name))
195
+ return false;
196
+ }
197
+ return alternative.segments.length > names.length;
198
+ });
199
+ },
200
+ namesHidden: segments.some((segment) => segment.startsWith(".")),
201
+ namesNodeModules: segments.some((segment) => segment.includes("node_modules"))
202
+ };
203
+ }
204
+ function compileAlternative(pattern) {
205
+ let relative = pattern;
206
+ while (relative.startsWith("./"))
207
+ relative = relative.slice(2);
208
+ const anchored = relative.startsWith("/");
209
+ if (anchored)
210
+ relative = relative.replace(/^\/+/, "");
211
+ if (!relative)
212
+ throw new PatternError("it names no file");
213
+ const anyDepth = !anchored && !relative.includes("/");
214
+ const segments = relative.split("/");
215
+ return {
216
+ anyDepth,
217
+ regex: compilePattern(anyDepth ? segmentSource(relative) : pathPatternSource(relative)),
218
+ segments
219
+ };
220
+ }
221
+
222
+ // src/tools/search/match.ts
223
+ function textMatcher(pattern, literal) {
224
+ return literal ? literalMatcher(pattern) : regexMatcher(pattern);
225
+ }
226
+ function literalMatcher(pattern) {
227
+ const ignoreCase = !/\p{Lu}/u.test(pattern);
228
+ const needle = ignoreCase ? pattern.toLowerCase() : pattern;
229
+ return {
230
+ line(line) {
231
+ const matched = linePrefix(line, SEARCH_LIMITS.matchCharacters);
232
+ const index = (ignoreCase ? matched.toLowerCase() : matched).indexOf(needle);
233
+ return index < 0 ? undefined : { index: Math.min(index, line.length), length: needle.length };
234
+ },
235
+ mayMatch(text) {
236
+ if (!ignoreCase)
237
+ return text.includes(needle);
238
+ if (hasLineOver(text, SEARCH_LIMITS.matchCharacters))
239
+ return true;
240
+ return text.toLowerCase().includes(needle);
241
+ }
242
+ };
243
+ }
244
+ function regexMatcher(pattern) {
245
+ const regex = lineRegex(pattern);
246
+ const whole = wholeTextRegex(pattern, regex);
247
+ return {
248
+ line(line) {
249
+ const found = regex.exec(linePrefix(line, SEARCH_LIMITS.matchCharacters));
250
+ return found ? { index: found.index, length: found[0].length } : undefined;
251
+ },
252
+ mayMatch(text) {
253
+ if (!whole || !linesAreWhole(text))
254
+ return true;
255
+ return whole.test(text);
256
+ }
257
+ };
258
+ }
259
+ function lineRegex(pattern) {
260
+ const written = pattern.replace(/\\(?:[pP]\{[^}]*\}|.)/g, "");
261
+ const flags = /\p{Lu}/u.test(written) || /\\[pP]\{/.test(pattern) ? "" : "i";
262
+ try {
263
+ return new RegExp(pattern, `${flags}u`);
264
+ } catch {}
265
+ try {
266
+ return new RegExp(pattern, flags);
267
+ } catch (error) {
268
+ const message = error instanceof Error ? error.message : String(error);
269
+ throw new SearchError("invalid_pattern", message.replace(/^Invalid regular expression: (?:\/.*\/[a-z]*: )?/, ""));
270
+ }
271
+ }
272
+ function wholeTextRegex(pattern, line) {
273
+ if (!readsOneLine(pattern))
274
+ return;
275
+ return new RegExp(line.source, `${line.flags}m`);
276
+ }
277
+ function linesAreWhole(text) {
278
+ return !hasLineOver(text, SEARCH_LIMITS.matchCharacters) && !hasLoneBreak(text);
279
+ }
280
+ function hasLoneBreak(text) {
281
+ if (text.includes("\u2028") || text.includes("\u2029"))
282
+ return true;
283
+ for (let index = text.indexOf("\r");index >= 0; index = text.indexOf("\r", index + 1)) {
284
+ if (text.charCodeAt(index + 1) !== LINE_FEED)
285
+ return true;
286
+ }
287
+ return false;
288
+ }
289
+ var LINE_FEED = 10;
290
+ var ESCAPES = new Set(["b", "B", "d", "w", "S", "k"]);
291
+ var CLASS_ESCAPES = new Set(["d", "w", "S"]);
292
+ function readsOneLine(pattern) {
293
+ let inClass = false;
294
+ let previous;
295
+ for (let index = 0;index < pattern.length; index += 1) {
296
+ let code = pattern.charCodeAt(index);
297
+ if (isLineBreakOrControl(code))
298
+ return false;
299
+ if (code === BACKSLASH) {
300
+ index += 1;
301
+ const escaped = pattern[index] ?? "";
302
+ if (/[A-Za-z0-9]/.test(escaped)) {
303
+ if (!(inClass ? CLASS_ESCAPES : ESCAPES).has(escaped))
304
+ return false;
305
+ previous = undefined;
306
+ continue;
307
+ }
308
+ code = pattern.charCodeAt(index);
309
+ if (isLineBreakOrControl(code))
310
+ return false;
311
+ } else if (!inClass && code === OPEN_PAREN) {
312
+ if (LOOKAROUND_OR_MODIFIER.test(pattern.slice(index, index + 4)))
313
+ return false;
314
+ } else if (!inClass && code === OPEN_BRACKET) {
315
+ if (pattern[index + 1] === "^")
316
+ return false;
317
+ inClass = true;
318
+ previous = undefined;
319
+ continue;
320
+ } else if (inClass && code === CLOSE_BRACKET) {
321
+ inClass = false;
322
+ continue;
323
+ } else if (inClass && code === DASH && previous !== undefined && index + 1 < pattern.length) {
324
+ const end = pattern[index + 1] === "\\" ? pattern.charCodeAt(index + 2) : pattern.charCodeAt(index + 1);
325
+ if (previous <= 8233 && end >= 8232)
326
+ return false;
327
+ }
328
+ previous = inClass ? code : undefined;
329
+ }
330
+ return true;
331
+ }
332
+ var LOOKAROUND_OR_MODIFIER = /^\(\?(?:[=!]|<[=!]|[ims-])/;
333
+ var OPEN_PAREN = 40;
334
+ var BACKSLASH = 92;
335
+ var OPEN_BRACKET = 91;
336
+ var CLOSE_BRACKET = 93;
337
+ var DASH = 45;
338
+ function isLineBreakOrControl(code) {
339
+ return code < 32 || code === 8232 || code === 8233;
340
+ }
341
+ function hasLineOver(text, length) {
342
+ if (text.length <= length)
343
+ return false;
344
+ for (let start = 0;start <= text.length; ) {
345
+ const newline = text.indexOf(`
346
+ `, start);
347
+ const end = newline < 0 ? text.length : newline;
348
+ if (end - start > length)
349
+ return true;
350
+ start = end + 1;
351
+ }
352
+ return false;
353
+ }
354
+ function lines(text) {
355
+ const split = text.split(`
356
+ `);
357
+ if (split.at(-1) === "")
358
+ split.pop();
359
+ return split.map((line) => line.endsWith("\r") ? line.slice(0, -1) : line);
360
+ }
361
+ function cutLine(line, match) {
362
+ const budget = SEARCH_LIMITS.lineCharacters;
363
+ if (line.length <= budget)
364
+ return line;
365
+ const room = budget - 1;
366
+ const start = match.index + match.length <= budget ? 0 : Math.max(0, Math.min(match.index - SEARCH_LIMITS.matchLead, line.length - room));
367
+ if (start === 0)
368
+ return `${linePrefix(line, budget)}…`;
369
+ const from = isLowSurrogate(line.charCodeAt(start)) ? start + 1 : start;
370
+ const window = linePrefix(line.slice(from, from + room + 1), room);
371
+ return `…${window}${from + window.length < line.length ? "…" : ""}`;
372
+ }
373
+ function isLowSurrogate(code) {
374
+ return code >= 56320 && code <= 57343;
375
+ }
376
+ function linePrefix(line, length) {
377
+ if (line.length <= length)
378
+ return line;
379
+ const code = line.charCodeAt(length - 1);
380
+ return line.slice(0, code >= 55296 && code <= 56319 ? length - 1 : length);
381
+ }
382
+
383
+ // src/tools/search/read.ts
384
+ import { Buffer } from "node:buffer";
385
+ import { open } from "node:fs/promises";
386
+ async function openText(path, strict) {
387
+ let file;
388
+ try {
389
+ file = await open(path, "r");
390
+ const { size } = await file.stat();
391
+ if (size > SEARCH_LIMITS.fileBytes) {
392
+ await closeQuietly(file);
393
+ return settled({ skipped: "too_large" });
394
+ }
395
+ const opened = file;
396
+ return {
397
+ bytes: size,
398
+ read: () => readText(opened, size, strict),
399
+ close: () => closeQuietly(opened)
400
+ };
401
+ } catch {
402
+ if (file)
403
+ await closeQuietly(file);
404
+ return settled({ skipped: "unreadable" });
405
+ }
406
+ }
407
+ function settled(text) {
408
+ return { bytes: 0, read: async () => text, close: async () => {} };
409
+ }
410
+ async function readText(file, size, strict) {
411
+ try {
412
+ const first = size <= READ_AT_ONCE_BYTES ? size : PROBE_BYTES;
413
+ let bytes = await readBytes(file, Buffer.allocUnsafe(first), 0);
414
+ if (bytes.subarray(0, PROBE_BYTES).includes(0))
415
+ return { skipped: "binary" };
416
+ if (bytes.length === first && first < size) {
417
+ const whole = Buffer.allocUnsafe(size);
418
+ bytes.copy(whole);
419
+ bytes = await readBytes(file, whole, first);
420
+ }
421
+ let text;
422
+ if (strict) {
423
+ try {
424
+ text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
425
+ } catch {
426
+ return { skipped: "binary" };
427
+ }
428
+ } else {
429
+ text = bytes.toString("utf8");
430
+ }
431
+ return { text: text.startsWith("\uFEFF") ? text.slice(1) : text };
432
+ } catch {
433
+ return { skipped: "unreadable" };
434
+ } finally {
435
+ await closeQuietly(file);
436
+ }
437
+ }
438
+ var PROBE_BYTES = SEARCH_LIMITS.binaryProbeBytes;
439
+ var READ_AT_ONCE_BYTES = 64 * 1024;
440
+ async function readBytes(file, bytes, from) {
441
+ let filled = from;
442
+ while (filled < bytes.length) {
443
+ const { bytesRead } = await file.read(bytes, filled, bytes.length - filled, filled);
444
+ if (bytesRead === 0)
445
+ break;
446
+ filled += bytesRead;
447
+ }
448
+ return bytes.subarray(0, filled);
449
+ }
450
+ async function closeQuietly(file) {
451
+ await file.close().catch(() => {});
452
+ }
453
+ var READ_AHEAD = 32;
454
+ var READ_AHEAD_BYTES = 16 * 1024 * 1024;
455
+
456
+ class ReadAhead {
457
+ items;
458
+ open;
459
+ holdsNothing;
460
+ #slots = [];
461
+ #held = 0;
462
+ #exhausted = false;
463
+ #stopped = false;
464
+ constructor(items, open, holdsNothing = () => false) {
465
+ this.items = items;
466
+ this.open = open;
467
+ this.holdsNothing = holdsNothing;
468
+ }
469
+ async next() {
470
+ this.#fill();
471
+ const slot = this.#slots[0];
472
+ if (!slot)
473
+ return;
474
+ await slot.opening;
475
+ this.#start();
476
+ const read = await slot.result;
477
+ this.#slots.shift();
478
+ this.#held -= slot.holds;
479
+ this.#fill();
480
+ this.#start();
481
+ return { item: slot.item, read };
482
+ }
483
+ async stop() {
484
+ this.#exhausted = true;
485
+ this.#stopped = true;
486
+ await Promise.all(this.#slots.splice(0).map(async (slot) => {
487
+ await slot.opening;
488
+ if (slot.result)
489
+ await slot.result;
490
+ else
491
+ await slot.pending?.close();
492
+ }));
493
+ }
494
+ #fill() {
495
+ while (!this.#exhausted && this.#slots.length < READ_AHEAD) {
496
+ const step = this.items.next();
497
+ if (step.done) {
498
+ this.#exhausted = true;
499
+ return;
500
+ }
501
+ const slot = { item: step.value, holds: 0 };
502
+ this.#slots.push(slot);
503
+ slot.opening = this.open(step.value).then((pending) => {
504
+ slot.pending = pending;
505
+ this.#start();
506
+ });
507
+ }
508
+ }
509
+ #start() {
510
+ if (this.#stopped)
511
+ return;
512
+ for (let index = 0;index < this.#slots.length; index += 1) {
513
+ const slot = this.#slots[index];
514
+ const pending = slot.pending;
515
+ if (slot.result || !pending)
516
+ continue;
517
+ if (index > 0 && this.#held + pending.bytes > READ_AHEAD_BYTES)
518
+ continue;
519
+ slot.holds = pending.bytes;
520
+ this.#held += slot.holds;
521
+ slot.result = pending.read().then((result) => {
522
+ if (this.holdsNothing(result) && this.#slots.includes(slot)) {
523
+ this.#held -= slot.holds;
524
+ slot.holds = 0;
525
+ this.#start();
526
+ }
527
+ return result;
528
+ });
529
+ }
530
+ }
531
+ }
532
+
533
+ // src/tools/search/walk.ts
534
+ import { Buffer as Buffer2 } from "node:buffer";
535
+ import { lstatSync, readdirSync, readFileSync, statSync } from "node:fs";
536
+
537
+ // src/tools/search/gitignore.ts
538
+ function parseGitignore(text) {
539
+ const rules = [];
540
+ const body = text.startsWith("\uFEFF") ? text.slice(1) : text;
541
+ for (const rawLine of body.split(`
542
+ `)) {
543
+ const rule = parseLine(rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine);
544
+ if (rule)
545
+ rules.push(rule);
546
+ }
547
+ return rules;
548
+ }
549
+ function parseLine(rawLine) {
550
+ if (rawLine === "" || rawLine.startsWith("#"))
551
+ return;
552
+ let line = withoutTrailingSpaces(rawLine);
553
+ const negated = line.startsWith("!");
554
+ if (negated)
555
+ line = line.slice(1);
556
+ const folderOnly = line.endsWith("/");
557
+ line = line.replace(/\/+$/, "");
558
+ const anyDepth = !line.includes("/");
559
+ line = line.replace(/^\//, "");
560
+ if (!line)
561
+ return;
562
+ try {
563
+ const regex = compilePattern(anyDepth ? segmentSource(line) : pathPatternSource(line));
564
+ return { negated, folderOnly, anyDepth, regex };
565
+ } catch (error) {
566
+ if (error instanceof PatternError)
567
+ return;
568
+ throw error;
569
+ }
570
+ }
571
+ function withoutTrailingSpaces(line) {
572
+ let end = line.length;
573
+ while (end > 0 && line[end - 1] === " ") {
574
+ let backslashes = 0;
575
+ while (line[end - 2 - backslashes] === "\\")
576
+ backslashes += 1;
577
+ if (backslashes % 2 === 1)
578
+ break;
579
+ end -= 1;
580
+ }
581
+ return line.slice(0, end);
582
+ }
583
+ function isIgnored(files, path, isFolder) {
584
+ const name = path.slice(path.lastIndexOf("/") + 1);
585
+ let ignored = false;
586
+ for (const file of files) {
587
+ const prefix = file.folder.endsWith("/") ? file.folder : `${file.folder}/`;
588
+ if (!path.startsWith(prefix))
589
+ continue;
590
+ const relative = path.slice(prefix.length);
591
+ for (const rule of file.rules) {
592
+ if (rule.folderOnly && !isFolder)
593
+ continue;
594
+ if (rule.negated !== ignored)
595
+ continue;
596
+ if (rule.regex.test(rule.anyDepth ? name : relative))
597
+ ignored = !rule.negated;
598
+ }
599
+ }
600
+ return ignored;
601
+ }
602
+
603
+ // src/tools/search/walk.ts
604
+ class FileWalk {
605
+ options;
606
+ #incomplete = false;
607
+ constructor(options) {
608
+ this.options = options;
609
+ }
610
+ get incomplete() {
611
+ return this.#incomplete;
612
+ }
613
+ *files() {
614
+ yield* this.#folder(this.options.root, "", this.options.ignoreFiles);
615
+ }
616
+ *#folder(folder, relativeFolder, ignoreFiles) {
617
+ if (this.#pastDeadline())
618
+ return;
619
+ let entries;
620
+ try {
621
+ entries = readdirSync(folder, { withFileTypes: true });
622
+ } catch {
623
+ return;
624
+ }
625
+ const own = entries.some((entry) => entry.name === ".gitignore") ? readIgnoreFile(folder) : undefined;
626
+ const applying = own ? [...ignoreFiles, own] : ignoreFiles;
627
+ for (const entry of sortedEntries(entries)) {
628
+ if (this.#incomplete)
629
+ return;
630
+ const { name } = entry;
631
+ if (name === ".git")
632
+ continue;
633
+ if (!this.options.hidden && name.startsWith("."))
634
+ continue;
635
+ const path = childPath(folder, name);
636
+ const relative = relativeFolder ? `${relativeFolder}/${name}` : name;
637
+ if (entry.isDirectory()) {
638
+ if (name === "node_modules" && !this.options.nodeModules)
639
+ continue;
640
+ if (this.options.skipFolders.has(path))
641
+ continue;
642
+ if (isIgnored(applying, path, true))
643
+ continue;
644
+ if (this.options.mayContain && !this.options.mayContain(relative))
645
+ continue;
646
+ yield* this.#folder(path, relative, applying);
647
+ continue;
648
+ }
649
+ const symlink = entry.isSymbolicLink();
650
+ if (!entry.isFile() && !(symlink && isLinkToFile(path)))
651
+ continue;
652
+ if (isIgnored(applying, path, false))
653
+ continue;
654
+ if (this.#pastDeadline())
655
+ return;
656
+ yield { path, relative, symlink };
657
+ }
658
+ }
659
+ #pastDeadline() {
660
+ if (Date.now() >= this.options.deadline)
661
+ this.#incomplete = true;
662
+ return this.#incomplete;
663
+ }
664
+ }
665
+ var IGNORE_FILE_BYTES = 1024 * 1024;
666
+ function readIgnoreFile(folder, name = ".gitignore") {
667
+ try {
668
+ const path = childPath(folder, name);
669
+ const stats = lstatSync(path);
670
+ if (!stats.isFile() || stats.size > IGNORE_FILE_BYTES)
671
+ return;
672
+ return { folder, rules: parseGitignore(readFileSync(path, "utf8")) };
673
+ } catch {
674
+ return;
675
+ }
676
+ }
677
+ function sortedEntries(entries) {
678
+ return entries.map((entry) => ({
679
+ entry,
680
+ key: Buffer2.from(entry.isDirectory() ? `${entry.name}/` : entry.name)
681
+ })).sort((left, right) => Buffer2.compare(left.key, right.key)).map(({ entry }) => entry);
682
+ }
683
+ function childPath(folder, name) {
684
+ return folder.endsWith("/") ? `${folder}${name}` : `${folder}/${name}`;
685
+ }
686
+ function isLinkToFile(path) {
687
+ try {
688
+ return statSync(path).isFile();
689
+ } catch {
690
+ return false;
691
+ }
692
+ }
693
+
694
+ // src/tools/search/search.ts
695
+ function globFiles(request, workingDirectory) {
696
+ const scope = resolveScope(request, workingDirectory);
697
+ const matcher = compileGlobOrFail(request.pattern, "invalid_pattern");
698
+ if (scope.file)
699
+ throw new SearchError("path_is_file", "The path is a file.");
700
+ const walk = scope.walk(matcher, matcher.namesHidden);
701
+ const files = [];
702
+ let truncated = false;
703
+ for (const file of walk.files()) {
704
+ if (!matcher.matches(file.relative))
705
+ continue;
706
+ if (files.length === SEARCH_LIMITS.files) {
707
+ truncated = true;
708
+ break;
709
+ }
710
+ files.push(scope.display(file.path));
711
+ }
712
+ return { files, truncated, ...walk.incomplete ? { incomplete: true } : {} };
713
+ }
714
+ async function grepFiles(request, workingDirectory) {
715
+ const scope = resolveScope(request, workingDirectory);
716
+ const matcher = textMatcher(request.pattern, request.literal === true);
717
+ const include = request.include === undefined ? undefined : compileGlobOrFail(request.include, "invalid_include");
718
+ const walk = scope.file ? undefined : scope.walk(include, true);
719
+ const searched = function* () {
720
+ if (!walk) {
721
+ yield { path: scope.target, relative: basename(scope.target), symlink: false };
722
+ return;
723
+ }
724
+ for (const file of walk.files()) {
725
+ if (include && !include.matches(file.relative))
726
+ continue;
727
+ if (file.symlink && !scope.holds(file.path))
728
+ continue;
729
+ yield file;
730
+ }
731
+ };
732
+ const reads = new ReadAhead(searched(), (file) => openText(file.path, walk === undefined), (read) => !("text" in read));
733
+ const matches = [];
734
+ const more = {};
735
+ let files = 0;
736
+ let textBytes = 0;
737
+ let textFull = false;
738
+ let truncated = false;
739
+ let stopped = false;
740
+ try {
741
+ for (let next = await reads.next();next; next = await reads.next()) {
742
+ const { item: file, read } = next;
743
+ if (!("text" in read)) {
744
+ if (walk === undefined && read.skipped !== "unreadable")
745
+ throw unsearchedError(read.skipped);
746
+ continue;
747
+ }
748
+ if (!matcher.mayMatch(read.text))
749
+ continue;
750
+ const path = scope.display(file.path);
751
+ let shown = 0;
752
+ let counted = 0;
753
+ for (const [index, line] of lines(read.text).entries()) {
754
+ if (Date.now() >= scope.deadline) {
755
+ stopped = true;
756
+ break;
757
+ }
758
+ const match = matcher.line(line);
759
+ if (match === undefined)
760
+ continue;
761
+ const text = cutLine(line, match);
762
+ const bytes = Buffer3.byteLength(` ${index + 1}: ${text}
763
+ `) + (shown === 0 ? Buffer3.byteLength(`${path}
764
+
765
+ `) : 0);
766
+ if (textBytes + bytes > SEARCH_LIMITS.textBytes)
767
+ textFull = true;
768
+ const full = textFull || matches.length === SEARCH_LIMITS.lines;
769
+ if (shown === 0 && full) {
770
+ truncated = true;
771
+ break;
772
+ }
773
+ if (shown > 0 && (shown === SEARCH_LIMITS.linesPerFile || full)) {
774
+ counted += 1;
775
+ continue;
776
+ }
777
+ matches.push({ path, line: index + 1, text });
778
+ textBytes += bytes;
779
+ shown += 1;
780
+ }
781
+ if (shown > 0)
782
+ files += 1;
783
+ if (counted > 0)
784
+ more[path] = counted;
785
+ if (truncated || stopped)
786
+ break;
787
+ }
788
+ } finally {
789
+ await reads.stop();
790
+ }
791
+ return {
792
+ matches,
793
+ files,
794
+ ...Object.keys(more).length > 0 ? { more } : {},
795
+ truncated,
796
+ ...walk?.incomplete || stopped ? { incomplete: true } : {}
797
+ };
798
+ }
799
+ function resolveScope(request, workingDirectory) {
800
+ const deadline = Date.now() + request.timeoutMs;
801
+ const working = realpathSync(workingDirectory);
802
+ const context = request.contextDirectory === undefined ? undefined : realPath(resolve(working, request.contextDirectory));
803
+ const target = realPath(resolve(working, request.path ?? "."));
804
+ if (target === undefined)
805
+ throw new SearchError("path_missing", "The path does not exist.");
806
+ const root = context !== undefined && isWithin(target, context) ? context : isWithin(target, working) ? working : undefined;
807
+ if (root === undefined) {
808
+ throw new SearchError("path_outside", "The path is outside the run's folders.");
809
+ }
810
+ const stats = statSync2(target);
811
+ if (!stats.isFile() && !stats.isDirectory()) {
812
+ throw new SearchError("path_not_searchable", "The path is not a file or a folder.");
813
+ }
814
+ const holds = (path) => {
815
+ const real = realPath(path);
816
+ return real !== undefined && (isWithin(real, working) || context !== undefined && isWithin(real, context));
817
+ };
818
+ const display = (path) => isWithin(path, working) ? relative(working, path) || "." : path;
819
+ const walk = (matcher, hidden) => {
820
+ const namesNodeModules = relative(root, target).split("/").includes("node_modules");
821
+ return new FileWalk({
822
+ root: target,
823
+ ignoreFiles: [...repositoryIgnoreFiles(root), ...ancestorIgnoreFiles(root, target)],
824
+ hidden,
825
+ nodeModules: namesNodeModules || matcher?.namesNodeModules === true,
826
+ skipFolders: new Set(context !== undefined && !isWithin(target, context) ? [context] : []),
827
+ ...matcher ? { mayContain: (folder) => matcher.mayContain(folder) } : {},
828
+ deadline
829
+ });
830
+ };
831
+ return { target, file: stats.isFile(), deadline, holds, display, walk };
832
+ }
833
+ var REPOSITORY_LEVELS = 20;
834
+ function repositoryIgnoreFiles(root) {
835
+ const above = [];
836
+ let folder = root;
837
+ for (let level = 0;level <= REPOSITORY_LEVELS; level += 1) {
838
+ if (holdsGit(folder)) {
839
+ const exclude = readIgnoreFile(folder, ".git/info/exclude");
840
+ return [
841
+ ...exclude ? [exclude] : [],
842
+ ...above.flatMap((parent) => readIgnoreFile(parent) ?? [])
843
+ ];
844
+ }
845
+ const parent = dirname(folder);
846
+ if (parent === folder)
847
+ return [];
848
+ folder = parent;
849
+ above.unshift(folder);
850
+ }
851
+ return [];
852
+ }
853
+ function holdsGit(folder) {
854
+ try {
855
+ lstatSync2(childPath(folder, ".git"));
856
+ return true;
857
+ } catch {
858
+ return false;
859
+ }
860
+ }
861
+ function ancestorIgnoreFiles(root, target) {
862
+ if (target === root)
863
+ return [];
864
+ const folders = [];
865
+ for (let folder = dirname(target);isWithin(folder, root); folder = dirname(folder)) {
866
+ folders.unshift(folder);
867
+ if (folder === root)
868
+ break;
869
+ }
870
+ return folders.flatMap((folder) => readIgnoreFile(folder) ?? []);
871
+ }
872
+ function compileGlobOrFail(pattern, code) {
873
+ try {
874
+ return compileGlob(pattern);
875
+ } catch (error) {
876
+ if (error instanceof PatternError)
877
+ throw new SearchError(code, error.message);
878
+ throw error;
879
+ }
880
+ }
881
+ function unsearchedError(reason) {
882
+ return reason === "binary" ? new SearchError("file_binary", "The file is binary, or not UTF-8 text.") : new SearchError("file_too_large", "The file is over 10 MiB.");
883
+ }
884
+ function realPath(path) {
885
+ try {
886
+ return realpathSync(path);
887
+ } catch {
888
+ return;
889
+ }
890
+ }
891
+ function isWithin(path, root) {
892
+ return path === root || path.startsWith(root.endsWith("/") ? root : `${root}/`);
893
+ }
894
+
895
+ // src/tools/search/command.ts
896
+ var USAGE = "Usage: search.mjs <request JSON>";
897
+ var KEYS = {
898
+ glob: ["tool", "pattern", "path", "contextDirectory", "timeoutMs"],
899
+ grep: ["tool", "pattern", "path", "include", "literal", "contextDirectory", "timeoutMs"]
900
+ };
901
+ async function runSearchCommand(args, workingDirectory) {
902
+ try {
903
+ const request = parseRequest(args);
904
+ const result = request.tool === "glob" ? globFiles(request, workingDirectory) : await grepFiles(request, workingDirectory);
905
+ return { exitCode: 0, stdout: `${JSON.stringify(result)}
906
+ `, stderr: "" };
907
+ } catch (error) {
908
+ if (error instanceof SearchError)
909
+ return failure(2, error.code, error.message);
910
+ if (error instanceof RequestError)
911
+ return failure(2, "invalid_arguments", error.message);
912
+ const message = error instanceof Error ? error.message : "The search failed unexpectedly.";
913
+ return failure(3, "internal_error", message);
914
+ }
915
+ }
916
+
917
+ class RequestError extends Error {
918
+ }
919
+ function failure(exitCode, code, message) {
920
+ return { exitCode, stdout: "", stderr: `${JSON.stringify({ error: { code, message } })}
921
+ ` };
922
+ }
923
+ function fail(message) {
924
+ throw new RequestError(message);
925
+ }
926
+ function parseRequest(args) {
927
+ if (args.length !== 1)
928
+ fail(USAGE);
929
+ let value;
930
+ try {
931
+ value = JSON.parse(args[0]);
932
+ } catch {
933
+ fail("The search request is not valid JSON.");
934
+ }
935
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
936
+ fail("The search request must be a JSON object.");
937
+ }
938
+ const input = value;
939
+ const tool = input.tool;
940
+ if (tool !== "glob" && tool !== "grep")
941
+ fail("tool must be 'glob' or 'grep'.");
942
+ for (const key of Object.keys(input)) {
943
+ if (!KEYS[tool].includes(key))
944
+ fail(`The ${tool} request has an unknown property '${key}'.`);
945
+ }
946
+ const pattern = nonEmptyString(input.pattern, "pattern");
947
+ const timeoutMs = input.timeoutMs;
948
+ if (typeof timeoutMs !== "number" || !Number.isSafeInteger(timeoutMs) || timeoutMs < 0) {
949
+ fail("timeoutMs must be a non-negative integer.");
950
+ }
951
+ const path = optionalString(input.path, "path");
952
+ const contextDirectory = optionalString(input.contextDirectory, "contextDirectory");
953
+ const scope = {
954
+ timeoutMs,
955
+ ...path === undefined ? {} : { path },
956
+ ...contextDirectory === undefined ? {} : { contextDirectory }
957
+ };
958
+ if (tool === "glob")
959
+ return { tool, pattern, ...scope };
960
+ if (input.literal !== undefined && typeof input.literal !== "boolean") {
961
+ fail("literal must be true or false.");
962
+ }
963
+ const include = optionalString(input.include, "include");
964
+ return {
965
+ tool,
966
+ pattern,
967
+ ...scope,
968
+ ...include === undefined ? {} : { include },
969
+ ...input.literal === undefined ? {} : { literal: input.literal }
970
+ };
971
+ }
972
+ function nonEmptyString(value, field) {
973
+ if (typeof value !== "string" || value === "")
974
+ fail(`${field} must be a non-empty string.`);
975
+ return value;
976
+ }
977
+ function optionalString(value, field) {
978
+ return value === undefined ? undefined : nonEmptyString(value, field);
979
+ }
980
+
981
+ // src/tools/search/main.ts
982
+ var result = await runSearchCommand(process.argv.slice(2), process.cwd());
983
+ process.stdout.write(result.stdout);
984
+ process.stderr.write(result.stderr);
985
+ process.exitCode = result.exitCode;