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,395 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer"
|
|
2
|
+
import { type ChildProcess, spawn } from "node:child_process"
|
|
3
|
+
import { extname, resolve as resolvePath } from "node:path"
|
|
4
|
+
import { pathToFileURL } from "node:url"
|
|
5
|
+
import { LIMITS } from "../constants.ts"
|
|
6
|
+
import { sanitizeSpawnEnv } from "../policy/scrub.ts"
|
|
7
|
+
|
|
8
|
+
export interface LspServerEntry {
|
|
9
|
+
ext: string // ".ts"
|
|
10
|
+
command: string
|
|
11
|
+
args: string[]
|
|
12
|
+
env?: Record<string, string>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const LANG_ID: Record<string, string> = {
|
|
16
|
+
".ts": "typescript",
|
|
17
|
+
".tsx": "typescriptreact",
|
|
18
|
+
".js": "javascript",
|
|
19
|
+
".jsx": "javascriptreact",
|
|
20
|
+
".mjs": "javascript",
|
|
21
|
+
".cjs": "javascript",
|
|
22
|
+
".py": "python",
|
|
23
|
+
".go": "go",
|
|
24
|
+
".rs": "rust",
|
|
25
|
+
".java": "java",
|
|
26
|
+
".c": "c",
|
|
27
|
+
".h": "c",
|
|
28
|
+
".cpp": "cpp",
|
|
29
|
+
".hpp": "cpp",
|
|
30
|
+
".cs": "csharp",
|
|
31
|
+
".rb": "ruby",
|
|
32
|
+
".php": "php",
|
|
33
|
+
".json": "json",
|
|
34
|
+
".css": "css",
|
|
35
|
+
".html": "html",
|
|
36
|
+
".md": "markdown",
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function languageIdFor(file: string): string {
|
|
40
|
+
return LANG_ID[extname(file).toLowerCase()] ?? "plaintext"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sleep(ms: number): Promise<void> {
|
|
44
|
+
return new Promise((r) => setTimeout(r, ms))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class LspConnection {
|
|
48
|
+
private proc: ChildProcess | null = null
|
|
49
|
+
private buf: Buffer | null = null
|
|
50
|
+
private pending = new Map<
|
|
51
|
+
number,
|
|
52
|
+
{
|
|
53
|
+
resolve: (v: unknown) => void
|
|
54
|
+
reject: (e: Error) => void
|
|
55
|
+
timer: ReturnType<typeof setTimeout>
|
|
56
|
+
}
|
|
57
|
+
>()
|
|
58
|
+
private seq = 0
|
|
59
|
+
private started = false
|
|
60
|
+
// guard anti double-spawn saat beberapa tool call paralel
|
|
61
|
+
private starting?: Promise<void>
|
|
62
|
+
// abort HANYA saat stop() — bukan timeout, agar server tidak mati sendiri
|
|
63
|
+
private killSignal = new AbortController()
|
|
64
|
+
private opened = new Map<string, number>()
|
|
65
|
+
readonly diagnostics = new Map<string, Record<string, unknown>[]>()
|
|
66
|
+
|
|
67
|
+
constructor(public entry: LspServerEntry) {}
|
|
68
|
+
|
|
69
|
+
start(rootPath: string): Promise<void> {
|
|
70
|
+
if (this.started) return Promise.resolve()
|
|
71
|
+
this.starting ??= this.doStart(rootPath)
|
|
72
|
+
return this.starting
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private async doStart(rootPath: string): Promise<void> {
|
|
76
|
+
this.proc = spawn(this.entry.command, this.entry.args, {
|
|
77
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
78
|
+
// env kredensial di-strip; env eksplisit config server menang setelahnya
|
|
79
|
+
env: sanitizeSpawnEnv(process.env, this.entry.env),
|
|
80
|
+
signal: this.killSignal.signal,
|
|
81
|
+
})
|
|
82
|
+
this.proc.on("error", (err) => this.failAll(err))
|
|
83
|
+
this.proc.on("exit", (code) => {
|
|
84
|
+
const wasStarted = this.started
|
|
85
|
+
this.started = false
|
|
86
|
+
this.starting = undefined
|
|
87
|
+
if (wasStarted) this.failAll(new Error(`LSP server ${this.entry.ext} exited (${code})`))
|
|
88
|
+
})
|
|
89
|
+
this.proc.stderr?.on("data", (d: Buffer) =>
|
|
90
|
+
process.stderr.write(`[lsp${this.entry.ext}] ${d.toString().trim().slice(0, 500)}\n`),
|
|
91
|
+
)
|
|
92
|
+
this.proc.stdout!.on("data", (chunk: Buffer) => this.handleData(chunk))
|
|
93
|
+
|
|
94
|
+
// initialize handshake
|
|
95
|
+
await this.request(
|
|
96
|
+
"initialize",
|
|
97
|
+
{
|
|
98
|
+
processId: process.pid,
|
|
99
|
+
rootUri: pathToFileURL(resolvePath(rootPath)).href,
|
|
100
|
+
capabilities: {
|
|
101
|
+
workspace: { symbol: {} },
|
|
102
|
+
textDocument: {
|
|
103
|
+
publishDiagnostics: { relatedInformation: false },
|
|
104
|
+
hover: { contentFormat: ["markdown", "plaintext"] },
|
|
105
|
+
definition: {},
|
|
106
|
+
references: {},
|
|
107
|
+
documentSymbol: { hierarchicalDocumentSymbolSupport: false },
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
LIMITS.LSP_INIT_TIMEOUT_MS,
|
|
112
|
+
)
|
|
113
|
+
this.notify("initialized", {})
|
|
114
|
+
this.started = true
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async request(
|
|
118
|
+
method: string,
|
|
119
|
+
params: Record<string, unknown> = {},
|
|
120
|
+
timeoutMs = 10_000,
|
|
121
|
+
): Promise<unknown> {
|
|
122
|
+
const id = ++this.seq
|
|
123
|
+
const msg = Buffer.from(JSON.stringify({ jsonrpc: "2.0", id, method, params }), "utf8")
|
|
124
|
+
const stdin = this.proc?.stdin
|
|
125
|
+
if (!stdin) throw new Error(`LSP ${method}: server stdin unavailable`)
|
|
126
|
+
stdin.write(`Content-Length: ${msg.length}\r\n\r\n`)
|
|
127
|
+
stdin.write(msg)
|
|
128
|
+
return new Promise((resolve, reject) => {
|
|
129
|
+
const timer = setTimeout(() => {
|
|
130
|
+
this.pending.delete(id)
|
|
131
|
+
reject(new Error(`LSP ${method} timed out after ${timeoutMs}ms`))
|
|
132
|
+
}, timeoutMs)
|
|
133
|
+
this.pending.set(id, { resolve, reject, timer })
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
notify(method: string, params: Record<string, unknown> = {}) {
|
|
138
|
+
if (!this.proc?.stdin) throw new Error(`LSP server ${this.entry.ext} not running`)
|
|
139
|
+
const msg = Buffer.from(JSON.stringify({ jsonrpc: "2.0", method, params }), "utf8")
|
|
140
|
+
this.proc.stdin.write(`Content-Length: ${msg.length}\r\n\r\n`)
|
|
141
|
+
this.proc.stdin.write(msg)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
openDocument(absPath: string, text: string): string {
|
|
145
|
+
const uri = pathToFileURL(absPath).href
|
|
146
|
+
const langId = languageIdFor(absPath)
|
|
147
|
+
if (!this.opened.has(uri)) {
|
|
148
|
+
this.notify("textDocument/didOpen", {
|
|
149
|
+
textDocument: { uri, languageId: langId, version: 1, text },
|
|
150
|
+
})
|
|
151
|
+
this.opened.set(uri, 1)
|
|
152
|
+
} else {
|
|
153
|
+
const v = (this.opened.get(uri) ?? 1) + 1
|
|
154
|
+
this.notify("textDocument/didChange", {
|
|
155
|
+
textDocument: { uri, version: v },
|
|
156
|
+
contentChanges: [{ text }],
|
|
157
|
+
})
|
|
158
|
+
this.opened.set(uri, v)
|
|
159
|
+
}
|
|
160
|
+
return uri
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async waitForDiagnostics(uri: string, timeoutMs = 5_000): Promise<Record<string, unknown>[]> {
|
|
164
|
+
const start = Date.now()
|
|
165
|
+
while (Date.now() - start < timeoutMs) {
|
|
166
|
+
if (this.diagnostics.has(uri)) {
|
|
167
|
+
await sleep(400) // settle window for follow-up pushes
|
|
168
|
+
return this.diagnostics.get(uri) ?? []
|
|
169
|
+
}
|
|
170
|
+
await sleep(100)
|
|
171
|
+
}
|
|
172
|
+
return this.diagnostics.get(uri) ?? []
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async stop(): Promise<void> {
|
|
176
|
+
if (!this.proc) return
|
|
177
|
+
try {
|
|
178
|
+
await this.request("shutdown", {}, 2_000)
|
|
179
|
+
} catch {}
|
|
180
|
+
try {
|
|
181
|
+
this.notify("exit")
|
|
182
|
+
} catch {}
|
|
183
|
+
// send didClose for all opened docs
|
|
184
|
+
for (const uri of this.opened.keys()) {
|
|
185
|
+
try {
|
|
186
|
+
this.notify("textDocument/didClose", { textDocument: { uri } })
|
|
187
|
+
} catch {}
|
|
188
|
+
}
|
|
189
|
+
this.opened.clear()
|
|
190
|
+
this.diagnostics.clear()
|
|
191
|
+
try {
|
|
192
|
+
this.proc.stdin?.end()
|
|
193
|
+
} catch {}
|
|
194
|
+
this.killSignal.abort()
|
|
195
|
+
await new Promise<void>((r) => {
|
|
196
|
+
const p = this.proc!
|
|
197
|
+
const t = setTimeout(() => {
|
|
198
|
+
try {
|
|
199
|
+
p.kill("SIGKILL")
|
|
200
|
+
} catch {}
|
|
201
|
+
r()
|
|
202
|
+
}, 2_000)
|
|
203
|
+
p.once("exit", () => {
|
|
204
|
+
clearTimeout(t)
|
|
205
|
+
r()
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
this.proc = null
|
|
209
|
+
this.started = false
|
|
210
|
+
this.starting = undefined
|
|
211
|
+
this.buf = null
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private handleData(chunk: Buffer) {
|
|
215
|
+
this.buf = this.buf ? Buffer.concat([this.buf, chunk]) : chunk
|
|
216
|
+
for (;;) {
|
|
217
|
+
if (!this.buf) break
|
|
218
|
+
const sep = this.buf.indexOf("\r\n\r\n")
|
|
219
|
+
if (sep === -1) break
|
|
220
|
+
const header = this.buf.slice(0, sep).toString("utf8")
|
|
221
|
+
const m = /Content-Length:\s*(\d+)/i.exec(header)
|
|
222
|
+
if (!m) {
|
|
223
|
+
this.buf = this.buf.slice(sep + 4)
|
|
224
|
+
continue
|
|
225
|
+
}
|
|
226
|
+
const len = Number(m[1])
|
|
227
|
+
const total = sep + 4 + len
|
|
228
|
+
if (this.buf.length < total) break
|
|
229
|
+
const body = this.buf.slice(sep + 4, total).toString("utf8")
|
|
230
|
+
this.buf = this.buf.slice(total)
|
|
231
|
+
try {
|
|
232
|
+
this.dispatch(JSON.parse(body))
|
|
233
|
+
} catch {}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private dispatch(msg: Record<string, unknown>) {
|
|
238
|
+
// response to our request
|
|
239
|
+
if (msg.id != null && ("result" in msg || "error" in msg)) {
|
|
240
|
+
const p = this.pending.get(msg.id as number)
|
|
241
|
+
if (p) {
|
|
242
|
+
clearTimeout(p.timer)
|
|
243
|
+
this.pending.delete(msg.id as number)
|
|
244
|
+
if (msg.error)
|
|
245
|
+
p.reject(new Error(String((msg.error as Record<string, unknown>).message ?? "LSP error")))
|
|
246
|
+
else p.resolve(msg.result)
|
|
247
|
+
}
|
|
248
|
+
return
|
|
249
|
+
}
|
|
250
|
+
// server → client notifications
|
|
251
|
+
if (msg.method === "textDocument/publishDiagnostics") {
|
|
252
|
+
const params = msg.params as
|
|
253
|
+
| { uri?: string; diagnostics?: Record<string, unknown>[] }
|
|
254
|
+
| undefined
|
|
255
|
+
if (params?.uri) this.diagnostics.set(params.uri, params.diagnostics ?? [])
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private failAll(err: Error) {
|
|
260
|
+
for (const p of this.pending.values()) {
|
|
261
|
+
clearTimeout(p.timer)
|
|
262
|
+
p.reject(err)
|
|
263
|
+
}
|
|
264
|
+
this.pending.clear()
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// registry keyed by lowercase extension
|
|
269
|
+
const activeServers = new Map<string, LspConnection>()
|
|
270
|
+
|
|
271
|
+
export function configureServers(entries: LspServerEntry[]): void {
|
|
272
|
+
for (const e of entries) {
|
|
273
|
+
const key = e.ext.toLowerCase()
|
|
274
|
+
if (!activeServers.has(key)) activeServers.set(key, new LspConnection(e))
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Seam uji: kosongkan registry (isolasi antar test file — repomap mengambil
|
|
279
|
+
// jalur LSP begitu ADA server terdaftar, jadi stub uji tak boleh bocor).
|
|
280
|
+
export function clearLspServers(): void {
|
|
281
|
+
activeServers.clear()
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function getConfiguredExts(): string[] {
|
|
285
|
+
return [...activeServers.keys()]
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async function getConnection(file: string, cwd?: string): Promise<LspConnection> {
|
|
289
|
+
const ext = extname(file).toLowerCase()
|
|
290
|
+
const conn = activeServers.get(ext)
|
|
291
|
+
if (!conn) throw new Error(`no LSP configured for '${ext}' — add via minicode config lsp add`)
|
|
292
|
+
// Root server = workspace sesi, BUKAN process.cwd(): tanpa ini sesi --cwd
|
|
293
|
+
// beda direktori menjalankan server di direktori yang salah (simbol dan
|
|
294
|
+
// diagnostics menunjuk workspace yang keliru). Default process.cwd()
|
|
295
|
+
// mempertahankan pemanggil lama (verifier tanpa konteks cwd).
|
|
296
|
+
await conn.start(cwd ?? process.cwd())
|
|
297
|
+
return conn
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export interface LspPositionResult {
|
|
301
|
+
position: { line: number; character: number }
|
|
302
|
+
text: string
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export function findSymbolPosition(
|
|
306
|
+
text: string,
|
|
307
|
+
symbol: string,
|
|
308
|
+
): LspPositionResult["position"] | null {
|
|
309
|
+
const escaped = symbol.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
310
|
+
// prefer word-boundary match to avoid hitting comments/import substrings; fallback to plain indexOf
|
|
311
|
+
const re = new RegExp(`\\b${escaped}\\b`)
|
|
312
|
+
const m = re.exec(text)
|
|
313
|
+
const idx = m?.index ?? text.indexOf(symbol)
|
|
314
|
+
if (idx === -1) return null
|
|
315
|
+
const before = text.slice(0, idx).split("\n")
|
|
316
|
+
return { line: before.length - 1, character: before[before.length - 1]!.length }
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export async function lspDiagnostics(
|
|
320
|
+
absPath: string,
|
|
321
|
+
text: string,
|
|
322
|
+
timeoutMs = 5_000,
|
|
323
|
+
cwd?: string,
|
|
324
|
+
): Promise<{ uri: string; items: Record<string, unknown>[] }> {
|
|
325
|
+
const conn = await getConnection(absPath, cwd)
|
|
326
|
+
const uri = conn.openDocument(absPath, text)
|
|
327
|
+
const items = await conn.waitForDiagnostics(uri, timeoutMs)
|
|
328
|
+
return { uri, items }
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export async function lspCall(
|
|
332
|
+
absPath: string,
|
|
333
|
+
text: string,
|
|
334
|
+
method: string,
|
|
335
|
+
params: Record<string, unknown>,
|
|
336
|
+
cwd?: string,
|
|
337
|
+
waitDiagsMs = 3_000,
|
|
338
|
+
): Promise<unknown> {
|
|
339
|
+
const conn = await getConnection(absPath, cwd)
|
|
340
|
+
const uri = conn.openDocument(absPath, text)
|
|
341
|
+
await conn.waitForDiagnostics(uri, waitDiagsMs).catch(() => [])
|
|
342
|
+
return conn.request(method, params)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export async function closeAllLsp(): Promise<void> {
|
|
346
|
+
for (const conn of activeServers.values()) {
|
|
347
|
+
try {
|
|
348
|
+
await conn.stop()
|
|
349
|
+
} catch {}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export interface WorkspaceSymbol {
|
|
354
|
+
name: string
|
|
355
|
+
kind: number
|
|
356
|
+
location: { uri: string; range?: { start?: { line?: number } } }
|
|
357
|
+
containerName?: string
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Query workspace symbols across all configured LSP servers (best-effort).
|
|
361
|
+
// Returns merged, deduped symbols. Empty query "" should return top symbols
|
|
362
|
+
// on servers that support it; we cap and timeout per server.
|
|
363
|
+
export async function workspaceSymbols(
|
|
364
|
+
query: string,
|
|
365
|
+
timeoutMs = 4000,
|
|
366
|
+
cwd?: string,
|
|
367
|
+
): Promise<WorkspaceSymbol[]> {
|
|
368
|
+
const conns = [...activeServers.values()]
|
|
369
|
+
if (conns.length === 0) return []
|
|
370
|
+
const results = await Promise.all(
|
|
371
|
+
conns.map(async (conn) => {
|
|
372
|
+
try {
|
|
373
|
+
await conn.start(cwd ?? process.cwd())
|
|
374
|
+
const res = (await conn.request("workspace/symbol", { query }, timeoutMs)) as
|
|
375
|
+
| WorkspaceSymbol[]
|
|
376
|
+
| null
|
|
377
|
+
return Array.isArray(res) ? res : []
|
|
378
|
+
} catch {
|
|
379
|
+
return [] as WorkspaceSymbol[]
|
|
380
|
+
}
|
|
381
|
+
}),
|
|
382
|
+
)
|
|
383
|
+
const seen = new Set<string>()
|
|
384
|
+
const merged: WorkspaceSymbol[] = []
|
|
385
|
+
for (const list of results) {
|
|
386
|
+
for (const s of list) {
|
|
387
|
+
const key = `${s.location.uri}::${s.name}::${s.kind}`
|
|
388
|
+
if (!seen.has(key)) {
|
|
389
|
+
seen.add(key)
|
|
390
|
+
merged.push(s)
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return merged
|
|
395
|
+
}
|