minicode-ai 0.9.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +935 -0
- package/LICENSE +21 -0
- package/README.md +200 -0
- package/cli/args.ts +195 -0
- package/cli/auto-update.ts +108 -0
- package/cli/commands/auth.ts +166 -0
- package/cli/commands/config.ts +382 -0
- package/cli/commands/doctor.ts +128 -0
- package/cli/commands/exec.ts +179 -0
- package/cli/commands/mcp.ts +35 -0
- package/cli/commands/memory.ts +107 -0
- package/cli/commands/pricing.ts +100 -0
- package/cli/commands/providers.ts +162 -0
- package/cli/commands/sessions.ts +128 -0
- package/cli/commands/skills.ts +61 -0
- package/cli/commands/stats.ts +82 -0
- package/cli/commands.ts +317 -0
- package/cli/index.ts +539 -0
- package/cli/model-manager.ts +112 -0
- package/cli/provider-manager.ts +111 -0
- package/cli/repl.ts +734 -0
- package/cli/router.ts +124 -0
- package/cli/setup.ts +780 -0
- package/cli/wizard.ts +21 -0
- package/docs/ARCHITECTURE.html +1222 -0
- package/docs/README.md +109 -0
- package/docs/architecture.md +44 -0
- package/docs/changelog.md +3 -0
- package/package.json +91 -0
- package/src/agents/pool.ts +43 -0
- package/src/app/mentions.ts +50 -0
- package/src/app/provider-layer.ts +118 -0
- package/src/app/rag-layer.ts +105 -0
- package/src/app/session.ts +154 -0
- package/src/app/tool-layer.ts +56 -0
- package/src/config.ts +372 -0
- package/src/constants.ts +119 -0
- package/src/hooks/run.ts +89 -0
- package/src/lib/atomic-write.ts +88 -0
- package/src/lib/db-path.ts +36 -0
- package/src/lib/git-hardening.ts +91 -0
- package/src/lib/ignore.ts +58 -0
- package/src/lib/keystore.ts +202 -0
- package/src/lib/net.ts +70 -0
- package/src/lib/safe-open.ts +138 -0
- package/src/lib/session-id.ts +23 -0
- package/src/lib/trash.ts +38 -0
- package/src/lib/trusted-exec.ts +81 -0
- package/src/lsp/client.ts +395 -0
- package/src/mcp/client.ts +400 -0
- package/src/mcp/http-transport.ts +344 -0
- package/src/mcp/server.ts +538 -0
- package/src/mcp/transport.ts +150 -0
- package/src/memory/files.ts +159 -0
- package/src/memory/vector.ts +775 -0
- package/src/policy/allowlist.ts +67 -0
- package/src/policy/bash-guard.ts +465 -0
- package/src/policy/compaction.ts +271 -0
- package/src/policy/context.ts +161 -0
- package/src/policy/executor.ts +199 -0
- package/src/policy/jail.ts +127 -0
- package/src/policy/permission.ts +517 -0
- package/src/policy/pricing.ts +336 -0
- package/src/policy/ratelimit.ts +54 -0
- package/src/policy/sandbox-policy.ts +157 -0
- package/src/policy/scrub.ts +138 -0
- package/src/policy/update-check.ts +200 -0
- package/src/policy/usage.ts +292 -0
- package/src/policy/verifier.ts +181 -0
- package/src/providers/anthropic.ts +401 -0
- package/src/providers/auth-store.ts +139 -0
- package/src/providers/build.ts +136 -0
- package/src/providers/detect.ts +126 -0
- package/src/providers/effort.ts +165 -0
- package/src/providers/guards.ts +107 -0
- package/src/providers/oauth.ts +326 -0
- package/src/providers/presets.ts +122 -0
- package/src/providers/provision.ts +298 -0
- package/src/providers/responses.ts +371 -0
- package/src/providers/router.ts +247 -0
- package/src/repo/repomap.ts +470 -0
- package/src/sandbox/docker.ts +129 -0
- package/src/sandbox/os.ts +193 -0
- package/src/session/checkpoint.ts +543 -0
- package/src/session/journal.ts +1347 -0
- package/src/session/persistence.ts +446 -0
- package/src/session/shadow-git.ts +351 -0
- package/src/session/turn-marker.ts +118 -0
- package/src/skills/loader.ts +117 -0
- package/src/telemetry/trace.ts +283 -0
- package/src/tools/ask_user.ts +54 -0
- package/src/tools/bash.ts +400 -0
- package/src/tools/code_run.ts +96 -0
- package/src/tools/delete_file.ts +31 -0
- package/src/tools/edit.ts +204 -0
- package/src/tools/git.ts +301 -0
- package/src/tools/glob.ts +90 -0
- package/src/tools/grep.ts +281 -0
- package/src/tools/hashline.ts +66 -0
- package/src/tools/index.ts +103 -0
- package/src/tools/lsp.ts +346 -0
- package/src/tools/mcp_call.ts +163 -0
- package/src/tools/memory.ts +144 -0
- package/src/tools/move_file.ts +47 -0
- package/src/tools/patch.ts +86 -0
- package/src/tools/read_file.ts +130 -0
- package/src/tools/read_image.ts +80 -0
- package/src/tools/submit_result.ts +49 -0
- package/src/tools/task.ts +346 -0
- package/src/tools/todo.ts +201 -0
- package/src/tools/web_fetch.ts +161 -0
- package/src/tools/web_search.ts +156 -0
- package/src/tools/write_file.ts +42 -0
- package/src/ui/approval/prompt.ts +76 -0
- package/src/ui/assistant/simple.ts +568 -0
- package/src/ui/assistant/turn-status.ts +303 -0
- package/src/ui/contract.ts +55 -0
- package/src/ui/footer.ts +105 -0
- package/src/ui/input/input.ts +805 -0
- package/src/ui/input/prompt-engine.ts +606 -0
- package/src/ui/render/collapse.ts +84 -0
- package/src/ui/render/detail.ts +24 -0
- package/src/ui/render/diff.ts +181 -0
- package/src/ui/render/errors.ts +239 -0
- package/src/ui/render/format.ts +54 -0
- package/src/ui/render/highlight.ts +294 -0
- package/src/ui/render/markdown.ts +120 -0
- package/src/ui/render/money.ts +12 -0
- package/src/ui/render/reasoning.ts +20 -0
- package/src/ui/render/sanitize.ts +80 -0
- package/src/ui/render/table.ts +88 -0
- package/src/ui/render/theme.ts +339 -0
- package/src/ui/render/width.ts +256 -0
- package/src/ui/render/wrap.ts +120 -0
- package/src/ui/runtime/bus-debug.ts +83 -0
- package/src/ui/runtime/chrome.ts +301 -0
- package/src/ui/runtime/spinner.ts +77 -0
- package/src/ui/runtime/statusline.ts +227 -0
- package/src/ui/screens/model-manager.ts +434 -0
- package/src/ui/screens/overlay.ts +61 -0
- package/src/ui/screens/picker.ts +307 -0
- package/src/ui/screens/provider-manager.ts +450 -0
- package/src/ui/screens/wizard.ts +132 -0
- package/vendor/minicore/VENDOR.md +29 -0
- package/vendor/minicore/package.json +21 -0
- package/vendor/minicore/src/core/budget.ts +26 -0
- package/vendor/minicore/src/core/compact.ts +76 -0
- package/vendor/minicore/src/core/errors.ts +60 -0
- package/vendor/minicore/src/core/events.ts +74 -0
- package/vendor/minicore/src/core/executor.ts +139 -0
- package/vendor/minicore/src/core/history.ts +32 -0
- package/vendor/minicore/src/core/index.ts +42 -0
- package/vendor/minicore/src/core/loop.ts +344 -0
- package/vendor/minicore/src/core/permission.ts +24 -0
- package/vendor/minicore/src/core/provider.ts +39 -0
- package/vendor/minicore/src/core/recovery.ts +34 -0
- package/vendor/minicore/src/core/session.ts +327 -0
- package/vendor/minicore/src/core/snapshot.ts +142 -0
- package/vendor/minicore/src/core/tokens.ts +111 -0
- package/vendor/minicore/src/core/tool.ts +118 -0
- package/vendor/minicore/src/core/types.ts +50 -0
- package/vendor/minicore/src/providers/openai-compat.ts +366 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process"
|
|
2
|
+
import { readdir, realpath, stat } from "node:fs/promises"
|
|
3
|
+
import { join, relative, resolve } from "node:path"
|
|
4
|
+
import type { Tool } from "#minicore"
|
|
5
|
+
import { LIMITS } from "../constants.ts"
|
|
6
|
+
import { isIgnored, loadIgnoreMatchers } from "../lib/ignore.ts"
|
|
7
|
+
import { safeReadFile } from "../lib/safe-open.ts"
|
|
8
|
+
import { isPathOutsideRoot, isSensitive } from "../policy/jail.ts"
|
|
9
|
+
import { scrubSecrets } from "../policy/scrub.ts"
|
|
10
|
+
|
|
11
|
+
async function walkGrep(
|
|
12
|
+
dir: string,
|
|
13
|
+
re: RegExp,
|
|
14
|
+
out: string[],
|
|
15
|
+
root: string,
|
|
16
|
+
limit: number,
|
|
17
|
+
signal: AbortSignal,
|
|
18
|
+
includeRe?: RegExp | null,
|
|
19
|
+
ignoreMatchers: ((rel: string) => boolean)[] = [],
|
|
20
|
+
) {
|
|
21
|
+
if (signal.aborted) throw new Error("aborted")
|
|
22
|
+
if (out.length >= limit) return
|
|
23
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
|
|
24
|
+
for (const e of entries) {
|
|
25
|
+
if (out.length >= limit) break
|
|
26
|
+
if (e.name.startsWith(".") || e.name === "node_modules" || e.name === ".git") continue
|
|
27
|
+
const full = join(dir, e.name)
|
|
28
|
+
const rel = relative(root, full).replace(/\\/g, "/")
|
|
29
|
+
if (isIgnored(rel, ignoreMatchers)) continue
|
|
30
|
+
if (e.isDirectory()) {
|
|
31
|
+
await walkGrep(full, re, out, root, limit, signal, includeRe, ignoreMatchers)
|
|
32
|
+
} else {
|
|
33
|
+
if (includeRe && !includeRe.test(rel) && !includeRe.test(e.name)) continue
|
|
34
|
+
if (/\.(png|jpg|jpeg|gif|webp|pdf|zip|exe|dll|bin)$/i.test(e.name)) continue
|
|
35
|
+
// symlink file escape check — resolve realpath and jail
|
|
36
|
+
const real = await realpath(full).catch(() => full)
|
|
37
|
+
if (isPathOutsideRoot(real, resolve(root)) || isSensitive(real) || isSensitive(rel)) continue
|
|
38
|
+
// also skip if symlink points outside (already covered) or sensitive
|
|
39
|
+
const st = await stat(real).catch(() => null)
|
|
40
|
+
if (!st || st.size > LIMITS.GREP_FILE_MAX_BYTES) continue
|
|
41
|
+
// Baca via safeReadFile (O_NOFOLLOW + verifikasi ulang target saat
|
|
42
|
+
// open): swap symlink antara realpath di atas dan read di bawah tak
|
|
43
|
+
// lagi bisa menyelundupkan konten luar/sensitif. Gagal = lewati berkas.
|
|
44
|
+
const text = (await safeReadFile(real, root).catch(() => null)) ?? ""
|
|
45
|
+
if (text.includes("\0")) continue
|
|
46
|
+
const lines = text.split("\n")
|
|
47
|
+
for (let i = 0; i < lines.length; i++) {
|
|
48
|
+
re.lastIndex = 0
|
|
49
|
+
if (re.test(lines[i]!)) {
|
|
50
|
+
const body = lines[i]!
|
|
51
|
+
// Baris terpotong ditandai "…" agar tak tampak utuh (audit #02).
|
|
52
|
+
const cut = body.length > LIMITS.GREP_MATCH_MAX_CHARS
|
|
53
|
+
out.push(
|
|
54
|
+
`${rel}:${i + 1}: ${scrubSecrets(cut ? `${body.slice(0, LIMITS.GREP_MATCH_MAX_CHARS)}…` : body)}`,
|
|
55
|
+
)
|
|
56
|
+
if (out.length >= limit) break
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function includeToRegExp(include: string): RegExp | null {
|
|
64
|
+
if (!include) return null
|
|
65
|
+
let esc = include.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
66
|
+
esc = esc.replace(/\*\*/g, "§§")
|
|
67
|
+
esc = esc.replace(/\*/g, "[^/]*")
|
|
68
|
+
esc = esc.replace(/§§/g, ".*")
|
|
69
|
+
esc = esc.replace(/\?/g, ".")
|
|
70
|
+
return new RegExp(`^${esc}$`)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ── ripgrep ──
|
|
74
|
+
// Walker JS di atas butuh ~3,5 s untuk repo 16k LOC karena membaca tiap file
|
|
75
|
+
// lewat event loop. `rg` melakukan hal sama dalam puluhan milidetik. Kita pakai
|
|
76
|
+
// bila tersedia, tapi fallback tetap dipertahankan supaya tool ini tak pernah
|
|
77
|
+
// bergantung pada binary eksternal.
|
|
78
|
+
let rgPath: string | null | undefined
|
|
79
|
+
|
|
80
|
+
export function ripgrepAvailable(): boolean {
|
|
81
|
+
if (rgPath !== undefined) return rgPath !== null
|
|
82
|
+
try {
|
|
83
|
+
const r = spawnSync("rg", ["--version"], { stdio: "ignore", timeout: 3000 })
|
|
84
|
+
rgPath = r.status === 0 ? "rg" : null
|
|
85
|
+
} catch {
|
|
86
|
+
rgPath = null
|
|
87
|
+
}
|
|
88
|
+
return rgPath !== null
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Normalisasi satu baris keluaran `rg --vimgrep`-style (`path:line:col:text`)
|
|
93
|
+
* ke format yang sama dengan walker (`path:line: text`), sekaligus scrub dan
|
|
94
|
+
* cap panjang. Return null bila baris harus dibuang (sensitif / tak terparse).
|
|
95
|
+
* Diekspor untuk test.
|
|
96
|
+
*/
|
|
97
|
+
export function normalizeRgLine(line: string, root: string): string | null {
|
|
98
|
+
// path bisa memuat ':' di Windows (C:\...), jadi cari dua ':' terakhir yang
|
|
99
|
+
// diikuti angka — bukan split(':') naif.
|
|
100
|
+
const m = /^(.*?):(\d+):(\d+):([\s\S]*)$/.exec(line)
|
|
101
|
+
if (!m) return null
|
|
102
|
+
const [, rawPath, lineNo, , text] = m
|
|
103
|
+
const rel = (rawPath ?? "").replace(/\\/g, "/").replace(/^\.\//, "")
|
|
104
|
+
if (!rel) return null
|
|
105
|
+
// Defense-in-depth: rg sudah dibatasi ke cwd, tapi jail tetap diterapkan
|
|
106
|
+
// supaya hasil identik dengan fallback (symlink/sensitive tetap dibuang).
|
|
107
|
+
if (isPathOutsideRoot(rel, resolve(root)) || isSensitive(rel)) return null
|
|
108
|
+
const body = text ?? ""
|
|
109
|
+
const cut = body.length > LIMITS.GREP_MATCH_MAX_CHARS
|
|
110
|
+
return `${rel}:${lineNo}: ${scrubSecrets(cut ? `${body.slice(0, LIMITS.GREP_MATCH_MAX_CHARS)}…` : body)}`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function runRipgrep(
|
|
114
|
+
pattern: string,
|
|
115
|
+
root: string,
|
|
116
|
+
limit: number,
|
|
117
|
+
signal: AbortSignal,
|
|
118
|
+
include?: string,
|
|
119
|
+
): Promise<string[]> {
|
|
120
|
+
const args = [
|
|
121
|
+
"--vimgrep", // path:line:col:text
|
|
122
|
+
"--no-heading",
|
|
123
|
+
"--color=never",
|
|
124
|
+
"--hidden", // tetap perlu --glob !.git di bawah
|
|
125
|
+
"--no-follow", // jangan ikuti symlink (setara jail walker)
|
|
126
|
+
"--max-filesize",
|
|
127
|
+
String(LIMITS.GREP_FILE_MAX_BYTES),
|
|
128
|
+
"--max-count",
|
|
129
|
+
String(limit),
|
|
130
|
+
"--glob",
|
|
131
|
+
"!.git/**",
|
|
132
|
+
"--glob",
|
|
133
|
+
"!node_modules/**",
|
|
134
|
+
"--glob",
|
|
135
|
+
"!.*/**",
|
|
136
|
+
]
|
|
137
|
+
if (include) args.push("--glob", include)
|
|
138
|
+
args.push("--regexp", pattern, "--", ".")
|
|
139
|
+
|
|
140
|
+
return new Promise((resolveOut, reject) => {
|
|
141
|
+
const p = spawn("rg", args, { cwd: root, stdio: ["ignore", "pipe", "pipe"] })
|
|
142
|
+
const out: string[] = []
|
|
143
|
+
let buf = ""
|
|
144
|
+
let stderr = ""
|
|
145
|
+
let done = false
|
|
146
|
+
const finish = (fn: () => void) => {
|
|
147
|
+
if (done) return
|
|
148
|
+
done = true
|
|
149
|
+
fn()
|
|
150
|
+
}
|
|
151
|
+
const onAbort = () => {
|
|
152
|
+
p.kill("SIGTERM")
|
|
153
|
+
finish(() => reject(new Error("aborted")))
|
|
154
|
+
}
|
|
155
|
+
if (signal.aborted) {
|
|
156
|
+
p.kill("SIGTERM")
|
|
157
|
+
return reject(new Error("aborted"))
|
|
158
|
+
}
|
|
159
|
+
signal.addEventListener("abort", onAbort, { once: true })
|
|
160
|
+
const timer = setTimeout(() => {
|
|
161
|
+
p.kill("SIGTERM")
|
|
162
|
+
finish(() => {
|
|
163
|
+
if (out.length > 0 && out.length < limit)
|
|
164
|
+
out.push(`… [truncated: ripgrep timeout ${LIMITS.GREP_RIPGREP_TIMEOUT_MS}ms]`)
|
|
165
|
+
resolveOut(out)
|
|
166
|
+
})
|
|
167
|
+
}, LIMITS.GREP_RIPGREP_TIMEOUT_MS)
|
|
168
|
+
|
|
169
|
+
p.stdout.on("data", (d: Buffer) => {
|
|
170
|
+
if (out.length >= limit) return
|
|
171
|
+
buf += d.toString()
|
|
172
|
+
let idx = buf.indexOf("\n")
|
|
173
|
+
while (idx >= 0) {
|
|
174
|
+
const line = buf.slice(0, idx).replace(/\r$/, "")
|
|
175
|
+
buf = buf.slice(idx + 1)
|
|
176
|
+
const norm = normalizeRgLine(line, root)
|
|
177
|
+
if (norm) out.push(norm)
|
|
178
|
+
if (out.length >= limit) {
|
|
179
|
+
p.kill("SIGTERM")
|
|
180
|
+
break
|
|
181
|
+
}
|
|
182
|
+
idx = buf.indexOf("\n")
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
p.stderr.on("data", (d: Buffer) => {
|
|
186
|
+
if (stderr.length < 2000) stderr += d.toString()
|
|
187
|
+
})
|
|
188
|
+
p.on("error", (e) => {
|
|
189
|
+
clearTimeout(timer)
|
|
190
|
+
signal.removeEventListener("abort", onAbort)
|
|
191
|
+
finish(() => reject(e))
|
|
192
|
+
})
|
|
193
|
+
p.on("close", (code) => {
|
|
194
|
+
clearTimeout(timer)
|
|
195
|
+
signal.removeEventListener("abort", onAbort)
|
|
196
|
+
// rg: 0 = ada match, 1 = tak ada match, 2 = error (mis. regex invalid)
|
|
197
|
+
if (code === 2 && out.length === 0) {
|
|
198
|
+
finish(() => reject(new Error(stderr.trim() || "ripgrep failed")))
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
finish(() => resolveOut(out))
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export const grepTool: Tool = {
|
|
207
|
+
name: "grep",
|
|
208
|
+
description:
|
|
209
|
+
"Search a regex across files. Returns file:line: content, capped (100/500 matches) with a refine hint. Skips .git, node_modules, and dotfiles; does not follow symlinks. Use for locating symbols or patterns before reading files.",
|
|
210
|
+
parameters: {
|
|
211
|
+
type: "object",
|
|
212
|
+
properties: {
|
|
213
|
+
pattern: { type: "string", description: "regex pattern (e.g. log.*Error)" },
|
|
214
|
+
cwd: { type: "string", description: "root directory (default: workspace root)" },
|
|
215
|
+
include: { type: "string", description: "file glob filter (e.g. *.ts)" },
|
|
216
|
+
limit: { type: "number" },
|
|
217
|
+
},
|
|
218
|
+
required: ["pattern"],
|
|
219
|
+
additionalProperties: false,
|
|
220
|
+
},
|
|
221
|
+
async execute({ pattern, cwd, include, limit }, ctx) {
|
|
222
|
+
const sessionRoot = (ctx as { cwd?: string }).cwd ?? process.cwd()
|
|
223
|
+
const rawRoot = (cwd as string) ?? "."
|
|
224
|
+
const root = resolve(sessionRoot, rawRoot)
|
|
225
|
+
if (isPathOutsideRoot(root, sessionRoot)) throw new Error(`cwd outside workspace: ${rawRoot}`)
|
|
226
|
+
const rawLim = limit as number | undefined
|
|
227
|
+
const lim = Number.isFinite(rawLim)
|
|
228
|
+
? Math.min(Math.max(Math.floor(rawLim!), 1), LIMITS.SEARCH_MAX_LIMIT)
|
|
229
|
+
: LIMITS.SEARCH_DEFAULT_LIMIT
|
|
230
|
+
const pat = pattern as string
|
|
231
|
+
// Validasi regex di JS lebih dulu: pesan errornya jelas dan berlaku untuk
|
|
232
|
+
// kedua jalur. Sintaks yang JS terima praktis selalu diterima rg juga.
|
|
233
|
+
let re: RegExp
|
|
234
|
+
try {
|
|
235
|
+
re = new RegExp(pat)
|
|
236
|
+
} catch (e) {
|
|
237
|
+
throw new Error(`invalid regex: ${(e as Error).message}`)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const noMatch = () =>
|
|
241
|
+
`no matches for /${pat}/ in ${rawRoot}${include ? ` (include ${include})` : ""}`
|
|
242
|
+
|
|
243
|
+
// Minta satu lebih agar tahu pasti apakah hasil terpotong (temuan #02:
|
|
244
|
+
// limit-hit diam-diam tampak lengkap). Tampilkan tetap `lim`.
|
|
245
|
+
const want = lim + 1
|
|
246
|
+
if (process.env.MINICODE_GREP_ENGINE !== "js" && ripgrepAvailable()) {
|
|
247
|
+
try {
|
|
248
|
+
const hits = await runRipgrep(pat, root, want, ctx.signal, include as string | undefined)
|
|
249
|
+
if (hits.length === 0) return noMatch()
|
|
250
|
+
// runRipgrep tandai timeout via suffix khusus di elemen terakhir (bila timedOut)
|
|
251
|
+
return withCapMark(hits, lim)
|
|
252
|
+
} catch (e) {
|
|
253
|
+
if ((e as Error).message === "aborted") throw e
|
|
254
|
+
// rg gagal (regex flavour berbeda, binary rusak, permission) → fallback
|
|
255
|
+
process.stderr.write(
|
|
256
|
+
`[warn] grep: ripgrep failed (${(e as Error).message}) — fallback JS\n`,
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const incRe = include ? includeToRegExp(include as string) : null
|
|
262
|
+
const ignoreMatchers = await loadIgnoreMatchers(root)
|
|
263
|
+
const out: string[] = []
|
|
264
|
+
await walkGrep(root, re, out, root, want, ctx.signal, incRe, ignoreMatchers)
|
|
265
|
+
return out.length === 0 ? noMatch() : withCapMark(out, lim)
|
|
266
|
+
},
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Tandai bila hasil mencapai cap (suffix timeout rg dipertahankan duluan).
|
|
270
|
+
export function withCapMark(hits: string[], lim: number): string {
|
|
271
|
+
const body = [...hits]
|
|
272
|
+
let suffix = ""
|
|
273
|
+
const last = body[body.length - 1]
|
|
274
|
+
if (typeof last === "string" && last.startsWith("… [truncated: ripgrep timeout")) {
|
|
275
|
+
suffix = `\n${body.pop()}`
|
|
276
|
+
}
|
|
277
|
+
if (body.length > lim) {
|
|
278
|
+
return `${body.slice(0, lim).join("\n")}${suffix}\n… [truncated: showing first ${lim} matches — refine pattern/include]`
|
|
279
|
+
}
|
|
280
|
+
return body.join("\n") + suffix
|
|
281
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Hashline deterministic edit — port dari OpenCode (deterministic file modification).
|
|
2
|
+
// Setiap line di-hash (FNV-1a 32-bit) → hunk dicocokkan via hash, bukan string diff naive.
|
|
3
|
+
// Ini menghindari partial-write failures yang sering terjadi di diff-based editor.
|
|
4
|
+
|
|
5
|
+
function fnv1a(str: string): number {
|
|
6
|
+
let h = 2166136261
|
|
7
|
+
for (let i = 0; i < str.length; i++) {
|
|
8
|
+
h ^= str.charCodeAt(i)
|
|
9
|
+
h = Math.imul(h, 16777619)
|
|
10
|
+
}
|
|
11
|
+
return h >>> 0
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function hashLine(line: string): string {
|
|
15
|
+
return fnv1a(line).toString(16).padStart(8, "0")
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function hashLines(lines: string[]): string[] {
|
|
19
|
+
return lines.map(hashLine)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Cari hunk `search` di `content` via hash matching. Return {start, end} index lines atau null.
|
|
23
|
+
// Menggunakan sliding window hash comparison — lebih robust vs whitespace/CRLF.
|
|
24
|
+
export function findHashlineHunk(
|
|
25
|
+
content: string,
|
|
26
|
+
search: string,
|
|
27
|
+
): { start: number; end: number } | null {
|
|
28
|
+
const contentLines = content.split("\n")
|
|
29
|
+
const searchLines = search.split("\n")
|
|
30
|
+
if (searchLines.length === 0 || searchLines.length > contentLines.length) return null
|
|
31
|
+
const contentHashes = hashLines(contentLines)
|
|
32
|
+
const searchHashes = hashLines(searchLines)
|
|
33
|
+
// exact hash first
|
|
34
|
+
outer: for (let i = 0; i <= contentLines.length - searchLines.length; i++) {
|
|
35
|
+
for (let j = 0; j < searchLines.length; j++) {
|
|
36
|
+
if (contentHashes[i + j] !== searchHashes[j]) continue outer
|
|
37
|
+
}
|
|
38
|
+
// verify raw equality to guard hash collision (extremely rare)
|
|
39
|
+
for (let j = 0; j < searchLines.length; j++) {
|
|
40
|
+
if (contentLines[i + j] !== searchLines[j]) continue outer
|
|
41
|
+
}
|
|
42
|
+
return { start: i, end: i + searchLines.length }
|
|
43
|
+
}
|
|
44
|
+
// fallback: trimmed hash (ignore leading/trailing whitespace) — fuzzy match
|
|
45
|
+
const trimmedContentHashes = hashLines(contentLines.map((l) => l.trim()))
|
|
46
|
+
const trimmedSearchHashes = hashLines(searchLines.map((l) => l.trim()))
|
|
47
|
+
outer2: for (let i = 0; i <= contentLines.length - searchLines.length; i++) {
|
|
48
|
+
for (let j = 0; j < searchLines.length; j++) {
|
|
49
|
+
if (trimmedContentHashes[i + j] !== trimmedSearchHashes[j]) continue outer2
|
|
50
|
+
}
|
|
51
|
+
return { start: i, end: i + searchLines.length }
|
|
52
|
+
}
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Apply hunk: replace `search` dengan `replace` di `content` via hashline.
|
|
57
|
+
export function applyHashline(content: string, search: string, replace: string): string | null {
|
|
58
|
+
const hunk = findHashlineHunk(content, search)
|
|
59
|
+
if (!hunk) return null
|
|
60
|
+
const lines = content.split("\n")
|
|
61
|
+
const replaceLines = replace.split("\n")
|
|
62
|
+
// handle trailing newline preservation
|
|
63
|
+
const before = lines.slice(0, hunk.start)
|
|
64
|
+
const after = lines.slice(hunk.end)
|
|
65
|
+
return [...before, ...replaceLines, ...after].join("\n")
|
|
66
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export { askUserTool } from "./ask_user.ts"
|
|
2
|
+
export { bashKillTool, bashOutputTool, bashTool } from "./bash.ts"
|
|
3
|
+
export { codeRunTool } from "./code_run.ts"
|
|
4
|
+
export { deleteFileTool } from "./delete_file.ts"
|
|
5
|
+
export { editTool } from "./edit.ts"
|
|
6
|
+
export { gitCommitTool, gitDiffTool, gitLogTool, gitStatusTool } from "./git.ts"
|
|
7
|
+
export { globTool } from "./glob.ts"
|
|
8
|
+
export { grepTool } from "./grep.ts"
|
|
9
|
+
export {
|
|
10
|
+
lspDefinitionTool,
|
|
11
|
+
lspDiagnosticsTool,
|
|
12
|
+
lspHoverTool,
|
|
13
|
+
lspReferencesTool,
|
|
14
|
+
lspSymbolsTool,
|
|
15
|
+
lspWorkspaceSymbolsTool,
|
|
16
|
+
} from "./lsp.ts"
|
|
17
|
+
export { mcpCallTool, mcpListTool, mcpPromptTool, mcpReadTool } from "./mcp_call.ts"
|
|
18
|
+
export { forgetMemoryTool, readMemoryTool, writeMemoryTool } from "./memory.ts"
|
|
19
|
+
export { moveFileTool } from "./move_file.ts"
|
|
20
|
+
export { applyPatchTool } from "./patch.ts"
|
|
21
|
+
export { readFileTool } from "./read_file.ts"
|
|
22
|
+
export { readImageTool } from "./read_image.ts"
|
|
23
|
+
export { submitResultTool } from "./submit_result.ts"
|
|
24
|
+
export { delegateTaskTool } from "./task.ts"
|
|
25
|
+
export { todoReadTool, todoSession, todoWriteTool } from "./todo.ts"
|
|
26
|
+
export { webFetchTool } from "./web_fetch.ts"
|
|
27
|
+
export { writeFileTool } from "./write_file.ts"
|
|
28
|
+
|
|
29
|
+
import type { Tool } from "#minicore"
|
|
30
|
+
import { askUserTool } from "./ask_user.ts"
|
|
31
|
+
import { bashKillTool, bashOutputTool, bashTool } from "./bash.ts"
|
|
32
|
+
import { codeRunTool } from "./code_run.ts"
|
|
33
|
+
import { deleteFileTool } from "./delete_file.ts"
|
|
34
|
+
import { editTool } from "./edit.ts"
|
|
35
|
+
import { gitCommitTool, gitDiffTool, gitLogTool, gitStatusTool } from "./git.ts"
|
|
36
|
+
import { globTool } from "./glob.ts"
|
|
37
|
+
import { grepTool } from "./grep.ts"
|
|
38
|
+
import {
|
|
39
|
+
lspDefinitionTool,
|
|
40
|
+
lspDiagnosticsTool,
|
|
41
|
+
lspHoverTool,
|
|
42
|
+
lspReferencesTool,
|
|
43
|
+
lspSymbolsTool,
|
|
44
|
+
lspWorkspaceSymbolsTool,
|
|
45
|
+
} from "./lsp.ts"
|
|
46
|
+
import { mcpCallTool, mcpListTool, mcpPromptTool, mcpReadTool } from "./mcp_call.ts"
|
|
47
|
+
import { forgetMemoryTool, readMemoryTool, writeMemoryTool } from "./memory.ts"
|
|
48
|
+
import { moveFileTool } from "./move_file.ts"
|
|
49
|
+
import { applyPatchTool } from "./patch.ts"
|
|
50
|
+
import { readFileTool } from "./read_file.ts"
|
|
51
|
+
import { readImageTool } from "./read_image.ts"
|
|
52
|
+
import { submitResultTool } from "./submit_result.ts"
|
|
53
|
+
import { delegateTaskTool } from "./task.ts"
|
|
54
|
+
import { todoReadTool, todoWriteTool } from "./todo.ts"
|
|
55
|
+
import { webFetchTool } from "./web_fetch.ts"
|
|
56
|
+
import { webSearchTool } from "./web_search.ts"
|
|
57
|
+
import { writeFileTool } from "./write_file.ts"
|
|
58
|
+
|
|
59
|
+
export const allTools: Tool[] = [
|
|
60
|
+
readFileTool,
|
|
61
|
+
writeFileTool,
|
|
62
|
+
editTool,
|
|
63
|
+
applyPatchTool,
|
|
64
|
+
globTool,
|
|
65
|
+
grepTool,
|
|
66
|
+
bashTool,
|
|
67
|
+
bashOutputTool,
|
|
68
|
+
bashKillTool,
|
|
69
|
+
gitStatusTool,
|
|
70
|
+
gitDiffTool,
|
|
71
|
+
gitLogTool,
|
|
72
|
+
gitCommitTool,
|
|
73
|
+
webFetchTool,
|
|
74
|
+
webSearchTool,
|
|
75
|
+
readMemoryTool,
|
|
76
|
+
writeMemoryTool,
|
|
77
|
+
forgetMemoryTool,
|
|
78
|
+
todoWriteTool,
|
|
79
|
+
todoReadTool,
|
|
80
|
+
delegateTaskTool,
|
|
81
|
+
mcpListTool,
|
|
82
|
+
mcpCallTool,
|
|
83
|
+
mcpReadTool,
|
|
84
|
+
mcpPromptTool,
|
|
85
|
+
lspDiagnosticsTool,
|
|
86
|
+
lspDefinitionTool,
|
|
87
|
+
lspReferencesTool,
|
|
88
|
+
lspHoverTool,
|
|
89
|
+
lspSymbolsTool,
|
|
90
|
+
lspWorkspaceSymbolsTool,
|
|
91
|
+
moveFileTool,
|
|
92
|
+
deleteFileTool,
|
|
93
|
+
readImageTool,
|
|
94
|
+
codeRunTool,
|
|
95
|
+
submitResultTool,
|
|
96
|
+
askUserTool,
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
// MCP server tools (prefixed "serverid.toolname") di-append runtime via connectAll()
|
|
100
|
+
export function withMcpTools(base: Tool[], mcpTools: Tool[]): Tool[] {
|
|
101
|
+
const seen = new Set(base.map((t) => t.name))
|
|
102
|
+
return [...base, ...mcpTools.filter((t) => !seen.has(t.name))]
|
|
103
|
+
}
|