@yuridulin/pointer 0.0.0-stage → 0.1.0

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 (71) hide show
  1. package/README.md +54 -2
  2. package/package.json +51 -4
  3. package/server/acp/catalog.ts +111 -0
  4. package/server/acp/connection.ts +233 -0
  5. package/server/acp/fake-agent.ts +408 -0
  6. package/server/acp/protocol.ts +167 -0
  7. package/server/acp/run.ts +139 -0
  8. package/server/acp/session.ts +1249 -0
  9. package/server/attachments.ts +115 -0
  10. package/server/changes.ts +239 -0
  11. package/server/cli.ts +362 -0
  12. package/server/compact.ts +361 -0
  13. package/server/config.ts +176 -0
  14. package/server/db.ts +462 -0
  15. package/server/dev.ts +8 -0
  16. package/server/diff.ts +190 -0
  17. package/server/engines.ts +63 -0
  18. package/server/files.ts +254 -0
  19. package/server/git.ts +319 -0
  20. package/server/history.ts +425 -0
  21. package/server/http/attachments.ts +63 -0
  22. package/server/http/browse.ts +108 -0
  23. package/server/http/chats.ts +627 -0
  24. package/server/http/closing.ts +14 -0
  25. package/server/http/files.ts +115 -0
  26. package/server/http/git.ts +84 -0
  27. package/server/http/preview.ts +21 -0
  28. package/server/http/providers.ts +200 -0
  29. package/server/http/reply.ts +38 -0
  30. package/server/hub.ts +521 -0
  31. package/server/install.ts +169 -0
  32. package/server/jobs.ts +664 -0
  33. package/server/linkPreview.ts +172 -0
  34. package/server/loop.ts +328 -0
  35. package/server/main.ts +353 -0
  36. package/server/probe.ts +34 -0
  37. package/server/process.ts +333 -0
  38. package/server/prompt.ts +196 -0
  39. package/server/providers/fake.ts +100 -0
  40. package/server/providers/opencode.ts +468 -0
  41. package/server/providers/registry.ts +149 -0
  42. package/server/providers/store.ts +210 -0
  43. package/server/segments.ts +248 -0
  44. package/server/sse.ts +93 -0
  45. package/server/tools/bash.ts +222 -0
  46. package/server/tools/edit.ts +254 -0
  47. package/server/tools/glob.ts +63 -0
  48. package/server/tools/grep.ts +122 -0
  49. package/server/tools/ignore.ts +54 -0
  50. package/server/tools/index.ts +129 -0
  51. package/server/tools/jail.ts +85 -0
  52. package/server/tools/jobs.ts +293 -0
  53. package/server/tools/read.ts +248 -0
  54. package/server/tools/write.ts +70 -0
  55. package/server/tray.ps1 +191 -0
  56. package/server/turn.ts +663 -0
  57. package/server/watch.ts +94 -0
  58. package/shared/acpConfig.ts +113 -0
  59. package/shared/links.ts +122 -0
  60. package/shared/types.ts +392 -0
  61. package/web/dist/app.css +1 -0
  62. package/web/dist/assets/codicon-mxdk0fnd.ttf +0 -0
  63. package/web/dist/index.html +28 -0
  64. package/web/dist/main.css +1 -0
  65. package/web/dist/main.js +1366 -0
  66. package/web/dist/tokens.css +1 -0
  67. package/web/dist/workers/css.worker.js +97 -0
  68. package/web/dist/workers/editor.worker.js +30 -0
  69. package/web/dist/workers/html.worker.js +300 -0
  70. package/web/dist/workers/json.worker.js +62 -0
  71. package/web/dist/workers/ts.worker.js +67733 -0
@@ -0,0 +1,222 @@
1
+ import type { ToolDef } from '../../shared/types'
2
+ import { ABORTED } from '../history'
3
+ import { drainPumps, MAX_OUTPUT, OutputRing, pumpStream } from '../jobs'
4
+ import { killTree, PROCESS_MARKER_ENV } from '../process'
5
+ import { resolveInRoot } from './jail'
6
+ import type { ToolContext, ToolResult } from './index'
7
+
8
+ export const DEFAULT_TIMEOUT = 120
9
+
10
+ export type BashArgs = { command: string; timeout?: number; workdir?: string; run_in_background?: boolean }
11
+
12
+ export const bashDef: ToolDef = {
13
+ name: 'bash',
14
+ description:
15
+ 'Run a shell command in Git Bash, the only shell on this machine. Each call starts a fresh shell: no state, variables or working directory persist between calls, so pass workdir instead of prefixing a command with `cd`. The working directory defaults to the workspace root; use Git Bash path forms such as /d/work/x, not Windows forms like D:\\work\\x. A non-zero exit code is reported as [exit code: N] in the output: that is a result to interpret, not a tool failure. The optional timeout is in seconds (default 120) and is capped by the configured limit. Commands that do not exit are killed at the timeout. Set run_in_background for a command that must outlive one call, such as a dev server or a watch: it returns a job id at once and has no timeout; read it with job_output, wait for it with wait, stop it with job_kill.',
16
+ parameters: {
17
+ type: 'object',
18
+ properties: {
19
+ command: { type: 'string', description: 'Shell command to run in Git Bash' },
20
+ timeout: {
21
+ type: 'integer',
22
+ description: 'Timeout in seconds. Default 120; capped by the configured shell timeout',
23
+ },
24
+ workdir: {
25
+ type: 'string',
26
+ description:
27
+ 'Directory inside the workspace folder to run in. Default: the workspace root. Prefer this over `cd`',
28
+ },
29
+ run_in_background: {
30
+ type: 'boolean',
31
+ description:
32
+ 'Run as a background job: return its id at once, no timeout, output read with job_output. Use for dev servers and watches',
33
+ },
34
+ },
35
+ required: ['command'],
36
+ additionalProperties: false,
37
+ },
38
+ }
39
+
40
+ export async function runBash(args: BashArgs, ctx: ToolContext): Promise<ToolResult> {
41
+ const shell = ctx.shell
42
+ const out = new OutputRing(ctx.chatId)
43
+ if (!shell) {
44
+ return {
45
+ text: 'bash: Git Bash was not found, the shell tool is unavailable',
46
+ isError: true,
47
+ details: { command: args.command, exitCode: null, timedOut: false, truncated: false, output: '' },
48
+ }
49
+ }
50
+ if (ctx.signal?.aborted) return aborted(args.command, out, null, false)
51
+
52
+ // The jail applies to the directory the command runs in; the command itself is not parsed.
53
+ const cwd = args.workdir === undefined ? ctx.root : resolveInRoot(ctx.root, args.workdir).abs
54
+
55
+ if (args.run_in_background === true) return startBackground(args, ctx, cwd, shell.path)
56
+
57
+ const seconds = timeoutSeconds(args.timeout, shell.maxTimeout)
58
+ // The run marker is inherited by every descendant, including double-forked background jobs
59
+ // that lose their Windows parent chain and therefore escape taskkill /T.
60
+ const marker = process.platform === 'win32' ? crypto.randomUUID() : ''
61
+ const command = `exec 2>&1\n${args.command}`
62
+ let proc: ReturnType<typeof Bun.spawn>
63
+ try {
64
+ proc = Bun.spawn([shell.path, '-c', command], {
65
+ cwd,
66
+ stdout: 'pipe',
67
+ stderr: 'pipe',
68
+ stdin: 'ignore',
69
+ ...(marker ? { env: { ...process.env, [PROCESS_MARKER_ENV]: marker } } : {}),
70
+ ...(process.platform === 'win32' ? {} : { detached: true }),
71
+ })
72
+ } catch (error) {
73
+ return {
74
+ text: error instanceof Error ? error.message : String(error),
75
+ isError: true,
76
+ details: { command: args.command, exitCode: null, timedOut: false, truncated: false, output: '' },
77
+ }
78
+ }
79
+
80
+ let timedOut = false
81
+ let abortedRun = false
82
+ let killing: Promise<void> = Promise.resolve()
83
+ const kill = (reason: 'abort' | 'timeout') => {
84
+ if (timedOut || abortedRun) return
85
+ if (reason === 'timeout') timedOut = true
86
+ else abortedRun = true
87
+ killing = killTree(proc.pid, shell.path, marker)
88
+ }
89
+ const onAbort = () => kill('abort')
90
+ ctx.signal?.addEventListener('abort', onAbort, { once: true })
91
+ if (ctx.signal?.aborted) kill('abort')
92
+ const timer = setTimeout(() => kill('timeout'), seconds * 1000)
93
+
94
+ const pumps = [
95
+ pumpStream(proc.stdout as ReadableStream<Uint8Array>, (chunk) => {
96
+ out.push(chunk)
97
+ ctx.onOutput(chunk)
98
+ }),
99
+ pumpStream(proc.stderr as ReadableStream<Uint8Array>, (chunk) => {
100
+ out.push(chunk)
101
+ ctx.onOutput(chunk)
102
+ }),
103
+ ]
104
+ let exitCode: number | null = null
105
+ try {
106
+ exitCode = await proc.exited
107
+ } finally {
108
+ clearTimeout(timer)
109
+ ctx.signal?.removeEventListener('abort', onAbort)
110
+ }
111
+ await killing
112
+ await drainPumps(pumps, abortedRun || timedOut)
113
+
114
+ if (abortedRun) return aborted(args.command, out, exitCode, timedOut)
115
+ // The command may have written anywhere in the workspace and the tool cannot name the
116
+ // files, so the client refreshes the tree, git status and open files as a whole.
117
+ ctx.onDiskChanged?.()
118
+ const tail = out.tail()
119
+ const details = {
120
+ command: args.command,
121
+ workdir: args.workdir ?? null,
122
+ exitCode,
123
+ timedOut,
124
+ truncated: tail.truncated,
125
+ output: tail.text,
126
+ }
127
+ if (timedOut) {
128
+ return {
129
+ text: assemble(tail, out.total, `Command timed out after ${seconds} s`, out.spill()),
130
+ isError: true,
131
+ details,
132
+ }
133
+ }
134
+ const code = exitCode ?? -1
135
+ // A non-zero code is a result the model reads, not a failed tool: `bun test` that finds a
136
+ // bug exits 1, and marking that an error makes the model treat a finding as a broken tool.
137
+ return {
138
+ text: assemble(tail, out.total, `[exit code: ${code}]`, out.spill()),
139
+ isError: false,
140
+ details,
141
+ }
142
+ }
143
+
144
+ function aborted(command: string, out: OutputRing, exitCode: number | null, timedOut: boolean): ToolResult {
145
+ // The abort text does not point at a log, so the file would only accumulate.
146
+ out.discardSpill()
147
+ const tail = out.tail()
148
+ return {
149
+ text: ABORTED,
150
+ isError: true,
151
+ details: { command, exitCode, timedOut, truncated: tail.truncated, output: tail.text },
152
+ }
153
+ }
154
+
155
+ function assemble(
156
+ tail: { text: string; truncated: boolean },
157
+ total: number,
158
+ ending: string,
159
+ spill: { path: string; chars: number } | null,
160
+ ): string {
161
+ const parts: string[] = []
162
+ if (tail.truncated) {
163
+ const note = spillNote(spill, total)
164
+ parts.push(`[output truncated: showing last ${MAX_OUTPUT} of ${total} chars${note}]`)
165
+ }
166
+ const body = tail.text.replace(/\n+$/, '')
167
+ if (body) parts.push(body)
168
+ parts.push(ending)
169
+ return parts.join('\n')
170
+ }
171
+
172
+ function spillNote(spill: { path: string; chars: number } | null, total: number): string {
173
+ if (!spill) return ''
174
+ if (spill.chars >= total) return `; full output: ${spill.path}`
175
+ return `; output log: ${spill.path} (first ${spill.chars} of ${total} chars)`
176
+ }
177
+
178
+ function timeoutSeconds(value: number | undefined, max: number): number {
179
+ const requested = value ?? DEFAULT_TIMEOUT
180
+ const seconds = Number.isFinite(requested) ? Math.floor(requested) : DEFAULT_TIMEOUT
181
+ return Math.min(Math.max(seconds, 1), Math.max(1, max))
182
+ }
183
+
184
+ function startBackground(args: BashArgs, ctx: ToolContext, cwd: string, shellPath: string): ToolResult {
185
+ if (!ctx.jobs) {
186
+ return {
187
+ text: 'bash: background jobs are unavailable in this chat',
188
+ isError: true,
189
+ details: { command: args.command, runInBackground: true },
190
+ }
191
+ }
192
+ const started = ctx.jobs.start({
193
+ chatId: ctx.chatId,
194
+ command: args.command,
195
+ cwd,
196
+ shell: { path: shellPath },
197
+ })
198
+ if (!started.ok) {
199
+ return {
200
+ text: started.error,
201
+ isError: true,
202
+ details: { command: args.command, runInBackground: true },
203
+ }
204
+ }
205
+ const job = started.job
206
+ return {
207
+ text: [
208
+ `Started background job ${job.id}: ${args.command}`,
209
+ `It runs without a timeout until it exits or is killed. Read its output with job_output (jobId ${job.id}), wait for it with wait, stop it with job_kill.`,
210
+ ].join('\n'),
211
+ isError: false,
212
+ details: {
213
+ command: args.command,
214
+ workdir: args.workdir ?? null,
215
+ jobId: job.id,
216
+ runInBackground: true,
217
+ status: job.status,
218
+ startedAt: job.startedAt,
219
+ },
220
+ }
221
+ }
222
+
@@ -0,0 +1,254 @@
1
+ import type { ToolDef } from '../../shared/types'
2
+ import { BLOB_LIMIT, writeAtomic, type Revision } from '../changes'
3
+ import { diffLines } from '../diff'
4
+ import { loadText } from './read'
5
+ import type { ToolContext, ToolResult } from './index'
6
+
7
+ export type EditReplacement = { old_string: string; new_string: string; replace_all?: boolean }
8
+ export type EditArgs = { path: string; edits: EditReplacement[] }
9
+
10
+ export const editDef: ToolDef = {
11
+ name: 'edit',
12
+ description:
13
+ 'Replace exact fragments of a text file. Paths are relative to the workspace root or absolute inside it; anything outside the folder is rejected. Batch all changes to one file in one call: every old_string is matched against the original file, so edits must not overlap. Each old_string must be unique inside the original unless replace_all is set. The file keeps its BOM and line endings.',
14
+ parameters: {
15
+ type: 'object',
16
+ properties: {
17
+ path: { type: 'string', description: 'File path inside the workspace folder' },
18
+ edits: {
19
+ type: 'array',
20
+ minItems: 1,
21
+ description: 'Replacements applied to the original file; batch every change to this file here',
22
+ items: {
23
+ type: 'object',
24
+ properties: {
25
+ old_string: { type: 'string', description: 'Exact text to replace' },
26
+ new_string: { type: 'string', description: 'Replacement text' },
27
+ replace_all: { type: 'boolean', description: 'Replace every occurrence instead of exactly one' },
28
+ },
29
+ required: ['old_string', 'new_string'],
30
+ additionalProperties: false,
31
+ },
32
+ },
33
+ },
34
+ required: ['path', 'edits'],
35
+ additionalProperties: false,
36
+ },
37
+ }
38
+
39
+ type EditRange = { at: number; length: number; next: string }
40
+
41
+ export async function runEdit(args: EditArgs, ctx: ToolContext): Promise<ToolResult> {
42
+ const edits = normalizeEdits(args.edits)
43
+ const loaded = loadText(ctx.root, args.path)
44
+ const rel = loaded.jailed.rel || '.'
45
+ const beforeLF = loaded.text
46
+ const multiple = edits.length > 1
47
+ const ranges: EditRange[] = []
48
+
49
+ for (let index = 0; index < edits.length; index++) {
50
+ const edit = edits[index]!
51
+ const prefix = multiple ? `edit ${index + 1}: ` : ''
52
+ const old = lf(edit.old_string)
53
+ const next = lf(edit.new_string)
54
+ if (old === '') throw new Error(`${prefix}old_string must not be empty`)
55
+ if (old === next) throw new Error(`${prefix}old_string and new_string are identical`)
56
+
57
+ const found = findAll(beforeLF, old)
58
+ if (found.length === 0) {
59
+ return { text: prefix + closest(beforeLF, old, rel), isError: true, details: { path: rel } }
60
+ }
61
+ if (found.length > 1 && !edit.replace_all) {
62
+ const lines = found.map((at) => lineNumberAt(beforeLF, at)).join(', ')
63
+ return {
64
+ text: `${prefix}old_string matches ${found.length} times in ${rel} (lines ${lines}); give a longer unique fragment or set replace_all`,
65
+ isError: true,
66
+ details: { path: rel },
67
+ }
68
+ }
69
+ for (const at of edit.replace_all ? found : [found[0]!]) ranges.push({ at, length: old.length, next })
70
+ }
71
+
72
+ ranges.sort((a, b) => a.at - b.at)
73
+ for (let i = 1; i < ranges.length; i++) {
74
+ const previous = ranges[i - 1]!
75
+ const current = ranges[i]!
76
+ if (previous.at + previous.length > current.at) {
77
+ const lines = [previous, current].map((range) => lineNumberAt(beforeLF, range.at)).join(', ')
78
+ return {
79
+ text: `edits overlap in ${rel} (lines ${lines}); make the fragments disjoint and match the original text`,
80
+ isError: true,
81
+ details: { path: rel },
82
+ }
83
+ }
84
+ }
85
+
86
+ let afterLF = beforeLF
87
+ for (let i = ranges.length - 1; i >= 0; i--) {
88
+ const range = ranges[i]!
89
+ afterLF = afterLF.slice(0, range.at) + range.next + afterLF.slice(range.at + range.length)
90
+ }
91
+ const restored = restoreEols(beforeLF, loaded.bytes, ranges)
92
+ const after = new TextEncoder().encode(restored)
93
+ if (after.byteLength > BLOB_LIMIT) {
94
+ throw new Error(`Edited ${rel} would be larger than the 2 MB limit; split the work into smaller files`)
95
+ }
96
+ writeAtomic(loaded.jailed.abs, after)
97
+ const count = ranges.length
98
+ const revisions: Revision[] = [{ path: rel, before: loaded.bytes, after }]
99
+ return {
100
+ text: `Edited ${rel}: ${count} ${count === 1 ? 'replacement' : 'replacements'}`,
101
+ isError: false,
102
+ details: { path: rel, diff: diffLines(beforeLF, afterLF) },
103
+ revisions,
104
+ }
105
+ }
106
+
107
+ /** Line endings of every line in `raw`; the last entry is '' when there is no final newline. */
108
+ function eolsOf(raw: string): string[] {
109
+ const out: string[] = []
110
+ let start = 0
111
+ for (let i = 0; i < raw.length; i++) {
112
+ if (raw[i] !== '\n') continue
113
+ out.push(i > start && raw[i - 1] === '\r' ? '\r\n' : '\n')
114
+ start = i + 1
115
+ }
116
+ if (start < raw.length || out.length === 0) out.push('')
117
+ return out
118
+ }
119
+
120
+ /** Inserted lines adopt the ending of the original line at the same position. */
121
+ function withEols(text: string, startLine: number, eols: string[]): string {
122
+ if (!text.includes('\n')) return text
123
+ const fallback = eols.findLast((item) => item !== '') ?? '\n'
124
+ const parts = text.split('\n')
125
+ let out = parts[0] ?? ''
126
+ for (let i = 1; i < parts.length; i++) {
127
+ const candidate = eols[startLine + i - 1]
128
+ const eol = candidate && candidate !== '' ? candidate : fallback
129
+ out += eol + (parts[i] ?? '')
130
+ }
131
+ return out
132
+ }
133
+
134
+ /**
135
+ * Rebuilds the raw file bytes from the LF-normalized text, so untouched lines keep the exact
136
+ * line ending they had on disk even in a file that mixes CRLF and LF.
137
+ */
138
+ function restoreEols(
139
+ beforeLF: string,
140
+ bytes: Uint8Array,
141
+ ranges: EditRange[],
142
+ ): string {
143
+ const decoded = new TextDecoder('utf-8', { ignoreBOM: true }).decode(bytes)
144
+ const bom = decoded.startsWith('\uFEFF')
145
+ const source = bom ? decoded.slice(1) : decoded
146
+ const eols = eolsOf(source)
147
+ let out = ''
148
+ let cursorLf = 0
149
+ let cursorRaw = 0
150
+ let breaks = 0
151
+ let extras = 0
152
+ // LF offsets map to raw offsets by counting the CRLF breaks removed by the normalization.
153
+ const rawOffset = (target: number): number => {
154
+ for (let i = cursorLf; i < target; i++) {
155
+ if (beforeLF[i] !== '\n') continue
156
+ if (eols[breaks] === '\r\n') extras++
157
+ breaks++
158
+ }
159
+ cursorLf = target
160
+ return target + extras
161
+ }
162
+ for (const range of ranges) {
163
+ const start = rawOffset(range.at)
164
+ const startLine = breaks
165
+ const end = rawOffset(range.at + range.length)
166
+ out += source.slice(cursorRaw, start) + withEols(range.next, startLine, eols)
167
+ cursorRaw = end
168
+ }
169
+ out += source.slice(cursorRaw)
170
+ return (bom ? '\uFEFF' : '') + out
171
+ }
172
+
173
+ function normalizeEdits(value: unknown): EditReplacement[] {
174
+ if (!Array.isArray(value) || value.length === 0) throw new Error('edits must be a non-empty array')
175
+ return value.map((item, index) => {
176
+ if (typeof item !== 'object' || item === null || Array.isArray(item)) {
177
+ throw new Error(`edits[${index}] must be an object`)
178
+ }
179
+ const edit = item as Record<string, unknown>
180
+ if (typeof edit.old_string !== 'string') throw new Error(`edits[${index}].old_string must be a string`)
181
+ if (typeof edit.new_string !== 'string') throw new Error(`edits[${index}].new_string must be a string`)
182
+ if (edit.replace_all !== undefined && typeof edit.replace_all !== 'boolean') {
183
+ throw new Error(`edits[${index}].replace_all must be a boolean`)
184
+ }
185
+ return { old_string: edit.old_string, new_string: edit.new_string, replace_all: edit.replace_all }
186
+ })
187
+ }
188
+
189
+ function lf(text: string): string {
190
+ return text.replaceAll('\r\n', '\n')
191
+ }
192
+
193
+ function findAll(text: string, needle: string): number[] {
194
+ const out: number[] = []
195
+ let from = 0
196
+ for (;;) {
197
+ const at = text.indexOf(needle, from)
198
+ if (at < 0) return out
199
+ out.push(at)
200
+ from = at + needle.length
201
+ }
202
+ }
203
+
204
+ function lineNumberAt(text: string, index: number): number {
205
+ let line = 1
206
+ for (let i = 0; i < index; i++) if (text[i] === '\n') line++
207
+ return line
208
+ }
209
+
210
+ function closest(text: string, old: string, rel: string): string {
211
+ const lines = text === '' ? [] : text.split('\n')
212
+ if (lines[lines.length - 1] === '') lines.pop()
213
+ if (lines.length === 0) return `old_string not found in ${rel}; the file is empty`
214
+ const needle = old.split('\n').find((line) => line.trim() !== '') ?? ''
215
+ let best = 0
216
+ let score = -1
217
+ for (let i = 0; i < lines.length; i++) {
218
+ const current = dice(needle, lines[i])
219
+ if (current > score) {
220
+ score = current
221
+ best = i
222
+ }
223
+ }
224
+ const lineNo = best + 1
225
+ const from = Math.max(1, lineNo - 2)
226
+ const to = Math.min(lines.length, lineNo + 2)
227
+ const context: string[] = []
228
+ for (let no = from; no <= to; no++) context.push(`${no}\t${lines[no - 1]}`)
229
+ return `old_string not found in ${rel}. Closest line ${lineNo}:\n${context.join('\n')}`
230
+ }
231
+
232
+ function bigrams(text: string): string[] {
233
+ const out: string[] = []
234
+ for (let i = 0; i < text.length - 1; i++) out.push(text.slice(i, i + 2))
235
+ return out
236
+ }
237
+
238
+ function dice(a: string, b: string): number {
239
+ if (a === b) return 1
240
+ const left = bigrams(a)
241
+ const right = bigrams(b)
242
+ if (left.length + right.length === 0) return 0
243
+ const counts = new Map<string, number>()
244
+ for (const gram of right) counts.set(gram, (counts.get(gram) ?? 0) + 1)
245
+ let common = 0
246
+ for (const gram of left) {
247
+ const left2 = counts.get(gram) ?? 0
248
+ if (left2 > 0) {
249
+ common++
250
+ counts.set(gram, left2 - 1)
251
+ }
252
+ }
253
+ return (2 * common) / (left.length + right.length)
254
+ }
@@ -0,0 +1,63 @@
1
+ import { statSync } from 'node:fs'
2
+ import type { ToolDef } from '../../shared/types'
3
+ import { resolveInRoot } from './jail'
4
+ import { checkAborted, walkFiles } from './ignore'
5
+ import type { ToolContext, ToolResult } from './index'
6
+
7
+ export const GLOB_LIMIT = 500
8
+
9
+ export type GlobArgs = { pattern: string; path?: string }
10
+
11
+ export const globDef: ToolDef = {
12
+ name: 'glob',
13
+ description:
14
+ 'List files whose path matches a glob pattern such as "**/*.ts". Paths are relative to the workspace root or absolute inside it; anything outside the folder is rejected. Directories such as node_modules, dist and .git are skipped.',
15
+ parameters: {
16
+ type: 'object',
17
+ properties: {
18
+ pattern: { type: 'string', description: 'Glob pattern, for example "**/*.ts"' },
19
+ path: {
20
+ type: 'string',
21
+ description: 'Directory inside the workspace folder to search from. Default: the workspace root',
22
+ },
23
+ },
24
+ required: ['pattern'],
25
+ additionalProperties: false,
26
+ },
27
+ }
28
+
29
+ export async function runGlob(args: GlobArgs, ctx: ToolContext): Promise<ToolResult> {
30
+ checkAborted(ctx.signal)
31
+ const jailed = resolveInRoot(ctx.root, args.path ?? '.')
32
+ const label = jailed.rel || '.'
33
+ let stat
34
+ try {
35
+ stat = statSync(jailed.abs)
36
+ } catch (error) {
37
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') return { text: `File not found: ${label}`, isError: true }
38
+ throw error
39
+ }
40
+ if (!stat.isDirectory()) return { text: `${label} is not a directory`, isError: true }
41
+
42
+ const glob = new Bun.Glob(args.pattern)
43
+ const found: string[] = []
44
+ let truncated = false
45
+ for (const file of walkFiles(jailed.abs, jailed.abs, ctx.signal)) {
46
+ checkAborted(ctx.signal)
47
+ if (!glob.match(file.rel)) continue
48
+ if (found.length >= GLOB_LIMIT) {
49
+ truncated = true
50
+ break
51
+ }
52
+ found.push(at(jailed.rel, file.rel))
53
+ }
54
+
55
+ const details = { pattern: args.pattern, path: label, count: found.length, truncated }
56
+ if (found.length === 0) return { text: `No files match ${args.pattern}`, isError: false, details }
57
+ const text = truncated ? `${found.join('\n')}\n[truncated at ${GLOB_LIMIT} results]` : found.join('\n')
58
+ return { text, isError: false, details }
59
+ }
60
+
61
+ function at(startRel: string, rel: string): string {
62
+ return startRel ? `${startRel}/${rel}` : rel
63
+ }
@@ -0,0 +1,122 @@
1
+ import { readFileSync, statSync } from 'node:fs'
2
+ import type { ToolDef } from '../../shared/types'
3
+ import { BLOB_LIMIT } from '../changes'
4
+ import { SPILL_LIMIT } from '../jobs'
5
+ import { isInsideRoot, resolveInRoots } from './jail'
6
+ import { checkAborted, walkFiles } from './ignore'
7
+ import type { ToolContext, ToolResult } from './index'
8
+
9
+ export const GREP_LIMIT = 200
10
+ export const LINE_LIMIT = 300
11
+ const TEXT_SNIFF = 8 * 1024
12
+
13
+ export type GrepArgs = { pattern: string; path?: string; glob?: string; ignore_case?: boolean }
14
+
15
+ export const grepDef: ToolDef = {
16
+ name: 'grep',
17
+ description:
18
+ 'Search file contents with a JavaScript regular expression. Paths are relative to the workspace root or absolute inside it; anything outside the folder is rejected. Returns rel:line:text rows. Binary and oversized files are skipped; directories such as node_modules, dist and .git are skipped.',
19
+ parameters: {
20
+ type: 'object',
21
+ properties: {
22
+ pattern: { type: 'string', description: 'JavaScript regular expression to search for' },
23
+ path: {
24
+ type: 'string',
25
+ description: 'File or directory inside the workspace folder to search. Default: the workspace root',
26
+ },
27
+ glob: { type: 'string', description: 'Glob filter of the file paths, for example "*.ts" or "**/*.ts"' },
28
+ ignore_case: { type: 'boolean', description: 'Match case-insensitively. Default false' },
29
+ },
30
+ required: ['pattern'],
31
+ additionalProperties: false,
32
+ },
33
+ }
34
+
35
+ export async function runGrep(args: GrepArgs, ctx: ToolContext): Promise<ToolResult> {
36
+ checkAborted(ctx.signal)
37
+ let regex: RegExp
38
+ try {
39
+ regex = new RegExp(args.pattern, args.ignore_case ? 'i' : '')
40
+ } catch (error) {
41
+ return { text: error instanceof Error ? error.message : String(error), isError: true }
42
+ }
43
+
44
+ const jailed = resolveInRoots([ctx.root, ...(ctx.extraRoots ?? [])], args.path ?? '.')
45
+ const label = jailed.rel || '.'
46
+ let stat
47
+ try {
48
+ stat = statSync(jailed.abs)
49
+ } catch (error) {
50
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') return { text: `File not found: ${label}`, isError: true }
51
+ throw error
52
+ }
53
+ const filter = args.glob ? new Bun.Glob(args.glob) : null
54
+
55
+ const matches: string[] = []
56
+ const files = new Set<string>()
57
+ let truncated = false
58
+ for (const file of candidates(jailed.abs, jailed.rel, stat.isDirectory(), ctx.signal)) {
59
+ checkAborted(ctx.signal)
60
+ if (filter && !filter.match(file.key)) continue
61
+ const limit = byteLimit(file.abs, ctx.extraRoots ?? [])
62
+ const text = readText(file.abs, limit)
63
+ if (text === null) continue
64
+ const rows = text.split('\n')
65
+ if (rows.length > 0 && rows[rows.length - 1] === '') rows.pop()
66
+ for (let index = 0; index < rows.length; index++) {
67
+ if (!regex.test(rows[index])) continue
68
+ if (matches.length >= GREP_LIMIT) {
69
+ truncated = true
70
+ break
71
+ }
72
+ matches.push(`${file.rel}:${index + 1}:${rows[index].slice(0, LINE_LIMIT)}`)
73
+ files.add(file.rel)
74
+ }
75
+ if (truncated) break
76
+ }
77
+
78
+ const details = { pattern: args.pattern, path: label, count: matches.length, files: files.size, truncated }
79
+ if (matches.length === 0) return { text: `No matches for ${args.pattern}`, isError: false, details }
80
+ const text = truncated ? `${matches.join('\n')}\n[truncated at ${GREP_LIMIT} matches]` : matches.join('\n')
81
+ return { text, isError: false, details }
82
+ }
83
+
84
+ function byteLimit(abs: string, extraRoots: string[]): number {
85
+ for (const root of extraRoots) {
86
+ if (root && isInsideRoot(root, abs)) return SPILL_LIMIT
87
+ }
88
+ return BLOB_LIMIT
89
+ }
90
+
91
+ function readText(abs: string, limit: number): string | null {
92
+ try {
93
+ const stat = statSync(abs)
94
+ if (!stat.isFile() || stat.size > limit) return null
95
+ const bytes = readFileSync(abs)
96
+ if (bytes.byteLength > limit) return null
97
+ if (bytes.subarray(0, TEXT_SNIFF).includes(0)) return null
98
+ return new TextDecoder('utf-8', { fatal: true }).decode(bytes).replaceAll('\r\n', '\n')
99
+ } catch {
100
+ return null
101
+ }
102
+ }
103
+
104
+ /** `key` is relative to the search directory: the glob filter matches against it. */
105
+ function* candidates(
106
+ startAbs: string,
107
+ startRel: string,
108
+ isDirectory: boolean,
109
+ signal?: AbortSignal,
110
+ ): Generator<{ abs: string; rel: string; key: string }> {
111
+ if (!isDirectory) {
112
+ yield { abs: startAbs, rel: startRel, key: startRel }
113
+ return
114
+ }
115
+ for (const file of walkFiles(startAbs, startAbs, signal)) {
116
+ yield { abs: file.abs, rel: at(startRel, file.rel), key: file.rel }
117
+ }
118
+ }
119
+
120
+ function at(startRel: string, rel: string): string {
121
+ return startRel ? `${startRel}/${rel}` : rel
122
+ }