thincoder 0.9.0 → 0.11.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.
- package/README.md +6 -1
- package/package.json +1 -1
- package/src/cli/make-agent.mjs +7 -0
- package/src/config.mjs +46 -19
- package/src/context.mjs +18 -0
- package/src/prompts/coder.md +5 -2
- package/src/prompts/discipline.md +8 -5
- package/src/prompts/system.md +8 -5
- package/src/provider/anthropic.mjs +190 -0
- package/src/provider/core.mjs +36 -4
- package/src/provider/google.mjs +197 -0
- package/src/proxy.mjs +236 -0
- package/src/tools/codemode.mjs +178 -0
- package/src/tools/fetch.md +2 -1
- package/src/tools/git.mjs +120 -154
- package/src/tools/index.mjs +11 -9
- package/src/tools/linter.mjs +46 -32
- package/src/tools/lsp.mjs +317 -0
- package/src/tools/web.mjs +103 -82
- package/src/tools/websearch.md +5 -3
- package/src/tui/agent-turn.mjs +12 -13
- package/src/tui/cmd-advisor.mjs +29 -41
- package/src/tui/cmd-clear.mjs +11 -17
- package/src/tui/cmd-config.mjs +226 -142
- package/src/tui/cmd-extract.mjs +1 -1
- package/src/tui/cmd-fold.mjs +2 -3
- package/src/tui/cmd-goal.mjs +58 -27
- package/src/tui/cmd-help.mjs +3 -1
- package/src/tui/cmd-mcp.mjs +178 -142
- package/src/tui/cmd-model.mjs +15 -4
- package/src/tui/cmd-new.mjs +7 -13
- package/src/tui/cmd-restore.mjs +12 -16
- package/src/tui/cmd-session.mjs +28 -32
- package/src/tui/cmd-think.mjs +75 -50
- package/src/tui/cmd-undo.mjs +19 -23
- package/src/tui/cmd-upgrade.mjs +22 -26
- package/src/tui/index.mjs +64 -38
- package/src/tui/key-handler.mjs +48 -18
- package/src/tui/layout.mjs +12 -2
- package/src/tui/pickers.mjs +151 -182
- package/src/tui/render-frame.mjs +29 -11
- package/src/tui/slash-commands.mjs +26 -16
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tools/lsp.mjs — LSP (Language Server Protocol) code intelligence tool
|
|
3
|
+
* Zero-dependency JSON-RPC 2.0 over stdio client.
|
|
4
|
+
*
|
|
5
|
+
* Provides: go-to-definition, find-references, hover info, document symbols, diagnostics.
|
|
6
|
+
* Lazy-starts language servers on first call. Configurable via config.json lsp.servers.
|
|
7
|
+
*
|
|
8
|
+
* Config format:
|
|
9
|
+
* "lsp": {
|
|
10
|
+
* "servers": {
|
|
11
|
+
* "typescript": { "command": "typescript-language-server", "args": ["--stdio"] },
|
|
12
|
+
* "python": { "command": "pyright-langserver", "args": ["--stdio"] }
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { spawn } from "node:child_process"
|
|
18
|
+
import { existsSync, readFileSync } from "node:fs"
|
|
19
|
+
import { join, extname } from "node:path"
|
|
20
|
+
|
|
21
|
+
// ---- JSON-RPC transport over stdio ----
|
|
22
|
+
|
|
23
|
+
/** Send a JSON-RPC request to the server via stdin */
|
|
24
|
+
function send(proc, message) {
|
|
25
|
+
const body = JSON.stringify(message)
|
|
26
|
+
const header = `Content-Length: ${Buffer.byteLength(body)}\r\n\r\n`
|
|
27
|
+
proc.stdin.write(header + body)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Read one JSON-RPC message from stdout. Returns parsed JSON, or null on EOF. */
|
|
31
|
+
function readMessage(proc) {
|
|
32
|
+
return new Promise((resolve) => {
|
|
33
|
+
let header = ""
|
|
34
|
+
let contentLength = -1
|
|
35
|
+
|
|
36
|
+
const onData = (chunk) => {
|
|
37
|
+
if (contentLength < 0) {
|
|
38
|
+
header += chunk.toString()
|
|
39
|
+
const match = header.match(/Content-Length: (\d+)\r\n\r\n/)
|
|
40
|
+
if (match) {
|
|
41
|
+
contentLength = parseInt(match[1])
|
|
42
|
+
const bodyStart = header.indexOf("\r\n\r\n") + 4
|
|
43
|
+
const remaining = header.slice(bodyStart)
|
|
44
|
+
header = ""
|
|
45
|
+
if (remaining.length >= contentLength) {
|
|
46
|
+
proc.stdout.removeListener("data", onData)
|
|
47
|
+
try { resolve(JSON.parse(remaining.slice(0, contentLength))) } catch { resolve(null) }
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
// Need more data — leave remaining in a buffer-like state
|
|
51
|
+
proc.stdout.removeListener("data", onData)
|
|
52
|
+
readBody(proc, remaining, contentLength).then(resolve)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
proc.stdout.on("data", onData)
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function readBody(proc, buf, targetLen) {
|
|
62
|
+
return new Promise((resolve) => {
|
|
63
|
+
const onData = (chunk) => {
|
|
64
|
+
buf += chunk.toString()
|
|
65
|
+
if (buf.length >= targetLen) {
|
|
66
|
+
proc.stdout.removeListener("data", onData)
|
|
67
|
+
try { resolve(JSON.parse(buf.slice(0, targetLen))) } catch { resolve(null) }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
proc.stdout.on("data", onData)
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Send a request and wait for the matching response */
|
|
75
|
+
async function request(proc, method, params, id) {
|
|
76
|
+
send(proc, { jsonrpc: "2.0", id, method, params })
|
|
77
|
+
while (true) {
|
|
78
|
+
const msg = await readMessage(proc)
|
|
79
|
+
if (!msg) return null
|
|
80
|
+
if (msg.id === id) return msg
|
|
81
|
+
// Store notifications for later retrieval (diagnostics)
|
|
82
|
+
if (msg.method === "textDocument/publishDiagnostics") {
|
|
83
|
+
proc._diagnostics = proc._diagnostics || {}
|
|
84
|
+
proc._diagnostics[msg.params.uri] = msg.params.diagnostics
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Send a notification (no response expected) */
|
|
90
|
+
function notify(proc, method, params) {
|
|
91
|
+
send(proc, { jsonrpc: "2.0", method, params })
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ---- Language server process management ----
|
|
95
|
+
|
|
96
|
+
const servers = new Map() // ext → { proc, rootUri, ready }
|
|
97
|
+
|
|
98
|
+
/** Convert file path to file:// URI */
|
|
99
|
+
function toUri(absPath) {
|
|
100
|
+
return "file:///" + absPath.replace(/\\/g, "/").replace(/^([A-Z]):/, (_, d) => d.toLowerCase() + "%3A")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Resolve which language server to use for a file extension */
|
|
104
|
+
function resolveServerConfig(config, ext) {
|
|
105
|
+
const map = {
|
|
106
|
+
".js": "typescript", ".mjs": "typescript", ".cjs": "typescript",
|
|
107
|
+
".ts": "typescript", ".tsx": "typescript", ".mts": "typescript", ".cts": "typescript",
|
|
108
|
+
".py": "python", ".pyi": "python",
|
|
109
|
+
".rs": "rust",
|
|
110
|
+
".go": "go",
|
|
111
|
+
}
|
|
112
|
+
const key = map[ext] || ext.slice(1)
|
|
113
|
+
const servers = config?.lsp?.servers ?? {}
|
|
114
|
+
return servers[key] || null
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Start or reuse a language server for the given file */
|
|
118
|
+
async function getServer(cwd, filePath, config) {
|
|
119
|
+
const ext = extname(filePath).toLowerCase()
|
|
120
|
+
const srvConfig = resolveServerConfig(config, ext)
|
|
121
|
+
if (!srvConfig) return null
|
|
122
|
+
|
|
123
|
+
const abs = join(cwd, ...filePath.split("/"))
|
|
124
|
+
if (!existsSync(abs)) return null
|
|
125
|
+
|
|
126
|
+
const rootUri = toUri(cwd)
|
|
127
|
+
const key = `${ext}:${cwd}`
|
|
128
|
+
let entry = servers.get(key)
|
|
129
|
+
|
|
130
|
+
if (entry && entry.ready) return entry
|
|
131
|
+
|
|
132
|
+
// Start new server
|
|
133
|
+
const proc = spawn(srvConfig.command, srvConfig.args || [], {
|
|
134
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
135
|
+
windowsHide: true,
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
proc.stderr.on("data", () => {}) // suppress stderr noise
|
|
139
|
+
|
|
140
|
+
entry = { proc, rootUri, ready: false }
|
|
141
|
+
servers.set(key, entry)
|
|
142
|
+
|
|
143
|
+
// Initialize handshake
|
|
144
|
+
const initResult = await request(proc, "initialize", {
|
|
145
|
+
processId: null,
|
|
146
|
+
rootUri,
|
|
147
|
+
capabilities: {
|
|
148
|
+
textDocument: {
|
|
149
|
+
definition: { linkSupport: false },
|
|
150
|
+
references: {},
|
|
151
|
+
hover: { contentFormat: ["plaintext"] },
|
|
152
|
+
documentSymbol: { hierarchicalDocumentSymbolSupport: true },
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
workspace: {},
|
|
156
|
+
}, 1)
|
|
157
|
+
|
|
158
|
+
if (!initResult) {
|
|
159
|
+
proc.kill()
|
|
160
|
+
servers.delete(key)
|
|
161
|
+
return null
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
notify(proc, "initialized", {})
|
|
165
|
+
entry.ready = true
|
|
166
|
+
return entry
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Notify the server that a file is open (required before queries) */
|
|
170
|
+
async function ensureOpen(proc, uri, ext) {
|
|
171
|
+
const langMap = { ".js": "javascript", ".mjs": "javascript", ".cjs": "javascript", ".ts": "typescript", ".tsx": "typescript", ".mts": "typescript", ".cts": "typescript", ".py": "python", ".rs": "rust", ".go": "go" }
|
|
172
|
+
const languageId = langMap[ext] || ext.slice(1)
|
|
173
|
+
notify(proc, "textDocument/didOpen", {
|
|
174
|
+
textDocument: {
|
|
175
|
+
uri,
|
|
176
|
+
languageId,
|
|
177
|
+
version: 1,
|
|
178
|
+
text: readFileSync(uri.replace(/^file:\/\/\//, "").replace(/%3A/, ":").replace(/\//g, "\\"), "utf8"),
|
|
179
|
+
},
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ---- Tool definition ----
|
|
184
|
+
|
|
185
|
+
export const lspTool = {
|
|
186
|
+
name: "lsp",
|
|
187
|
+
description:
|
|
188
|
+
"LSP code intelligence: go to definition, find references, hover info, document symbols, diagnostics. " +
|
|
189
|
+
"Use this to understand code structure without grep-guessing function locations or type shapes.",
|
|
190
|
+
parameters: {
|
|
191
|
+
type: "object",
|
|
192
|
+
properties: {
|
|
193
|
+
subcommand: {
|
|
194
|
+
type: "string",
|
|
195
|
+
enum: ["definition", "references", "hover", "symbols", "diagnostics"],
|
|
196
|
+
description: "LSP operation to perform",
|
|
197
|
+
},
|
|
198
|
+
uri: {
|
|
199
|
+
type: "string",
|
|
200
|
+
description: "Target file path (relative to project root)",
|
|
201
|
+
},
|
|
202
|
+
line: {
|
|
203
|
+
type: "integer",
|
|
204
|
+
description: "1-based line number (for definition/references/hover)",
|
|
205
|
+
},
|
|
206
|
+
character: {
|
|
207
|
+
type: "integer",
|
|
208
|
+
description: "1-based character offset (for definition/references/hover)",
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
required: ["subcommand", "uri"],
|
|
212
|
+
},
|
|
213
|
+
readonly: true,
|
|
214
|
+
|
|
215
|
+
async execute(args, ctx) {
|
|
216
|
+
const config = ctx.agent?.config ?? {}
|
|
217
|
+
const cwd = ctx.cwd
|
|
218
|
+
const filePath = args.uri
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
const entry = await getServer(cwd, filePath, config)
|
|
222
|
+
if (!entry) {
|
|
223
|
+
const ext = extname(filePath).toLowerCase()
|
|
224
|
+
return `No LSP server configured for "${ext}" files. Add one to config.json:\n"lsp": { "servers": { "${ext.slice(1)}": { "command": "...", "args": ["--stdio"] } } }`
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const { proc } = entry
|
|
228
|
+
const abs = join(cwd, ...filePath.split("/"))
|
|
229
|
+
const uri = toUri(abs)
|
|
230
|
+
const ext = extname(filePath).toLowerCase()
|
|
231
|
+
await ensureOpen(proc, uri, ext)
|
|
232
|
+
|
|
233
|
+
// Wait briefly for diagnostics to arrive
|
|
234
|
+
await new Promise((r) => setTimeout(r, 300))
|
|
235
|
+
|
|
236
|
+
switch (args.subcommand) {
|
|
237
|
+
case "definition": {
|
|
238
|
+
if (!args.line || !args.character) return "Error: line and character required for definition"
|
|
239
|
+
const res = await request(proc, "textDocument/definition", {
|
|
240
|
+
textDocument: { uri },
|
|
241
|
+
position: { line: args.line - 1, character: args.character - 1 },
|
|
242
|
+
}, 10)
|
|
243
|
+
if (!res?.result) return "No definition found."
|
|
244
|
+
const locs = Array.isArray(res.result) ? res.result : [res.result]
|
|
245
|
+
return locs.map((l) => {
|
|
246
|
+
const path = l.uri.replace(/^file:\/\/\//, "").replace(/%3A/, ":")
|
|
247
|
+
return `${path}:${l.range.start.line + 1}:${l.range.start.character + 1}`
|
|
248
|
+
}).join("\n")
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
case "references": {
|
|
252
|
+
if (!args.line || !args.character) return "Error: line and character required for references"
|
|
253
|
+
const res = await request(proc, "textDocument/references", {
|
|
254
|
+
textDocument: { uri },
|
|
255
|
+
position: { line: args.line - 1, character: args.character - 1 },
|
|
256
|
+
context: { includeDeclaration: false },
|
|
257
|
+
}, 10)
|
|
258
|
+
if (!res?.result?.length) return "No references found."
|
|
259
|
+
return res.result.slice(0, 50).map((l) => {
|
|
260
|
+
const path = l.uri.replace(/^file:\/\/\//, "").replace(/%3A/, ":")
|
|
261
|
+
return `${path}:${l.range.start.line + 1}:${l.range.start.character + 1}`
|
|
262
|
+
}).join("\n") + (res.result.length > 50 ? `\n... and ${res.result.length - 50} more` : "")
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
case "hover": {
|
|
266
|
+
if (!args.line || !args.character) return "Error: line and character required for hover"
|
|
267
|
+
const res = await request(proc, "textDocument/hover", {
|
|
268
|
+
textDocument: { uri },
|
|
269
|
+
position: { line: args.line - 1, character: args.character - 1 },
|
|
270
|
+
}, 10)
|
|
271
|
+
if (!res?.result?.contents) return "No hover info available."
|
|
272
|
+
const contents = res.result.contents
|
|
273
|
+
if (typeof contents === "string") return contents
|
|
274
|
+
if (Array.isArray(contents)) return contents.map((c) => typeof c === "string" ? c : c.value).join("\n")
|
|
275
|
+
if (contents.value) return contents.value
|
|
276
|
+
return JSON.stringify(contents)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
case "symbols": {
|
|
280
|
+
const res = await request(proc, "textDocument/documentSymbol", {
|
|
281
|
+
textDocument: { uri },
|
|
282
|
+
}, 10)
|
|
283
|
+
if (!res?.result?.length) return "No symbols found."
|
|
284
|
+
function render(nodes, depth) {
|
|
285
|
+
const lines = []
|
|
286
|
+
for (const n of nodes) {
|
|
287
|
+
const kind = n.kind != null ? ` [${symbolKind(n.kind)}]` : ""
|
|
288
|
+
lines.push(`${" ".repeat(depth)}${n.name}${kind} — L${n.range.start.line + 1}`)
|
|
289
|
+
if (n.children?.length) lines.push(...render(n.children, depth + 1))
|
|
290
|
+
}
|
|
291
|
+
return lines
|
|
292
|
+
}
|
|
293
|
+
return render(res.result, 0).join("\n")
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
case "diagnostics": {
|
|
297
|
+
const diags = proc._diagnostics?.[uri]
|
|
298
|
+
if (!diags?.length) return "No diagnostics."
|
|
299
|
+
return diags.slice(0, 30).map((d) => {
|
|
300
|
+
const sev = { 1: "ERROR", 2: "WARN", 3: "INFO", 4: "HINT" }[d.severity] || "?"
|
|
301
|
+
return `L${d.range.start.line + 1}: ${sev}: ${d.message}${d.code ? ` [${d.code}]` : ""}`
|
|
302
|
+
}).join("\n") + (diags.length > 30 ? `\n... and ${diags.length - 30} more` : "")
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
default:
|
|
306
|
+
return `Unknown subcommand: ${args.subcommand}`
|
|
307
|
+
}
|
|
308
|
+
} catch (err) {
|
|
309
|
+
return `LSP error: ${err.message}`
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function symbolKind(k) {
|
|
315
|
+
const kinds = { 1: "file", 2: "module", 3: "namespace", 4: "package", 5: "class", 6: "method", 7: "property", 8: "field", 9: "constructor", 10: "enum", 11: "interface", 12: "function", 13: "variable", 14: "constant", 15: "string", 16: "number", 17: "boolean", 18: "array", 19: "object", 20: "key", 21: "null", 22: "enumMember", 23: "struct", 24: "event", 25: "operator", 26: "typeParameter" }
|
|
316
|
+
return kinds[k] || `kind-${k}`
|
|
317
|
+
}
|
package/src/tools/web.mjs
CHANGED
|
@@ -1,11 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { DESC, truncate, stripTags, htmlToText } from "./shared.mjs";
|
|
2
|
+
import { URL } from "node:url";
|
|
3
|
+
import { resolveWebProxy, proxyFetch } from "../proxy.mjs";
|
|
4
|
+
|
|
5
|
+
export const UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
|
6
|
+
const FETCH_TIMEOUT = 15_000
|
|
7
|
+
|
|
8
|
+
// ── Web search (Bing; direct by default, through proxy when configured and web toggle on) ──
|
|
9
|
+
|
|
10
|
+
function extractBing(html) {
|
|
11
|
+
const results = []
|
|
12
|
+
const blocks = html.split('<li class="b_algo"').slice(1)
|
|
13
|
+
for (const block of blocks) {
|
|
14
|
+
const link = block.match(/<h2[^>]*><a[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/)
|
|
15
|
+
if (!link) continue
|
|
16
|
+
const snippet = block.match(/<p[^>]*>([\s\S]*?)<\/p>/)
|
|
17
|
+
results.push({ href: link[1], title: stripTags(link[2]), snippet: snippet ? stripTags(snippet[1]) : "" })
|
|
18
|
+
}
|
|
19
|
+
return results
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function bingUrl(query, page) {
|
|
23
|
+
let u = `https://www.bing.com/search?q=${encodeURIComponent(query)}&setlang=en&setmkt=en-US`
|
|
24
|
+
if (page > 1) u += `&first=${(page - 1) * 10 + 1}`
|
|
25
|
+
return u
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const ENGINES = [{ name: "bing", label: "Bing", url: bingUrl, extract: extractBing, ua: UA }]
|
|
29
|
+
const ENGINE_NAMES = ENGINES.map(e => e.name)
|
|
30
|
+
|
|
31
|
+
async function fetchEngine(engine, query, page, ctx) {
|
|
32
|
+
const ctrl = new AbortController()
|
|
33
|
+
const timer = setTimeout(() => ctrl.abort(), FETCH_TIMEOUT)
|
|
34
|
+
try {
|
|
35
|
+
const response = await proxyFetch(engine.url(query, page), {
|
|
36
|
+
headers: { "User-Agent": engine.ua, "Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8" },
|
|
37
|
+
signal: ctrl.signal,
|
|
38
|
+
}, resolveWebProxy(ctx))
|
|
39
|
+
if (!response.ok) return null
|
|
40
|
+
const html = await response.text()
|
|
41
|
+
const results = engine.extract(html)
|
|
42
|
+
return { engine: engine.name, results }
|
|
43
|
+
} catch { return null }
|
|
44
|
+
finally { clearTimeout(timer) }
|
|
45
|
+
}
|
|
9
46
|
|
|
10
47
|
export const websearchTool = {
|
|
11
48
|
name: "websearch",
|
|
@@ -14,108 +51,92 @@ export const websearchTool = {
|
|
|
14
51
|
type: "object",
|
|
15
52
|
properties: {
|
|
16
53
|
query: { type: "string", description: "Search query" },
|
|
17
|
-
limit: { type: "number", description: "Max results (default 8)" },
|
|
54
|
+
limit: { type: "number", description: "Max results (default 8, max 20)" },
|
|
55
|
+
engine: { type: "string", enum: ENGINE_NAMES, description: "Specific engine — \"bing\" (Bing). Omit to search all engines concurrently." },
|
|
56
|
+
page: { type: "number", description: "Page number for pagination (1-based, default 1). Only used when engine is specified." },
|
|
18
57
|
},
|
|
19
58
|
required: ["query"],
|
|
20
59
|
},
|
|
21
60
|
readonly: true,
|
|
22
61
|
async execute(args, ctx) {
|
|
23
|
-
const limit = args.limit ?? 8
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
: AbortSignal.timeout(15_000),
|
|
32
|
-
})
|
|
33
|
-
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
34
|
-
html = await readBodyText(response)
|
|
35
|
-
} catch (error) {
|
|
36
|
-
throw new Error(`websearch request failed: ${error.cause?.code ?? error.message}`)
|
|
62
|
+
const limit = Math.min(args.limit ?? 8, 20)
|
|
63
|
+
const page = Math.max(1, args.page ?? 1)
|
|
64
|
+
if (args.engine) {
|
|
65
|
+
const engine = ENGINES.find(e => e.name === args.engine)
|
|
66
|
+
if (!engine) return `Unknown engine '${args.engine}'. Available: ${ENGINE_NAMES.join(", ")}`
|
|
67
|
+
const fetched = await fetchEngine(engine, args.query, page, ctx)
|
|
68
|
+
if (!fetched || fetched.results.length === 0) return `(no results from ${engine.label})`
|
|
69
|
+
return truncate(fetched.results.slice(0, limit).map((r, i) => `${i + 1}. ${r.title}\n ${r.href}\n ${r.snippet}`).join("\n\n"))
|
|
37
70
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
71
|
+
const promises = ENGINES.map(e => fetchEngine(e, args.query, 1, ctx))
|
|
72
|
+
const fetched = (await Promise.all(promises)).filter(Boolean)
|
|
73
|
+
if (fetched.length === 0) return "(no results — all search engines failed)"
|
|
74
|
+
const merged = [], indexes = fetched.map(() => 0)
|
|
75
|
+
let done = false
|
|
76
|
+
while (!done && merged.length < limit) {
|
|
77
|
+
done = true
|
|
78
|
+
for (let i = 0; i < fetched.length; i++) {
|
|
79
|
+
if (indexes[i] < fetched[i].results.length) {
|
|
80
|
+
merged.push({ ...fetched[i].results[indexes[i]], _engine: fetched[i].engine })
|
|
81
|
+
indexes[i]++; done = false
|
|
82
|
+
if (merged.length >= limit) break
|
|
83
|
+
}
|
|
84
|
+
}
|
|
52
85
|
}
|
|
53
|
-
|
|
54
|
-
return truncate(
|
|
55
|
-
results.map((r, i) => `${i + 1}. ${r.title}\n ${r.href}\n ${r.snippet}`).join("\n\n"),
|
|
56
|
-
)
|
|
86
|
+
return truncate(merged.slice(0, limit).map((r, i) => `${i + 1}. [${r._engine}] ${r.title}\n ${r.href}\n ${r.snippet}`).join("\n\n"))
|
|
57
87
|
},
|
|
58
88
|
}
|
|
59
89
|
|
|
90
|
+
// ── Fetch tool (with proxy support) ──────
|
|
60
91
|
|
|
61
|
-
// ---------------------------------------------------------------- ls
|
|
62
|
-
|
|
63
|
-
/** SSRF protection: block internal private-network/metadata endpoints (localhost allowed — user's dev server, tests depend on it) */
|
|
64
92
|
function isPrivateUrl(urlStr) {
|
|
65
|
-
let u
|
|
66
|
-
try { u = new URL(urlStr) } catch { return true }
|
|
93
|
+
let u; try { u = new URL(urlStr) } catch { return true }
|
|
67
94
|
const host = u.hostname.toLowerCase()
|
|
68
|
-
// localhost / 127.x allowed (user's dev server on local machine, test mock server)
|
|
69
95
|
if (host === "localhost" || host === "0.0.0.0" || host.endsWith(".localhost")) return false
|
|
70
96
|
if (host === "127.0.0.1" || host.startsWith("127.")) return false
|
|
71
|
-
// cloud metadata endpoint
|
|
72
97
|
if (host === "169.254.169.254" || host === "metadata.google.internal") return true
|
|
73
|
-
// IPv4 private ranges
|
|
74
98
|
const m = host.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
|
|
75
|
-
if (m) {
|
|
76
|
-
const [a, b] = [Number(m[1]), Number(m[2])]
|
|
77
|
-
if (a === 10) return true
|
|
78
|
-
if (a === 172 && b >= 16 && b <= 31) return true
|
|
79
|
-
if (a === 192 && b === 168) return true
|
|
80
|
-
if (a === 169 && b === 254) return true
|
|
81
|
-
if (a === 0) return true
|
|
82
|
-
}
|
|
83
|
-
// IPv6 loopback / link-local / unique local addresses
|
|
99
|
+
if (m) { const [a, b] = [Number(m[1]), Number(m[2])]; if (a === 10||a === 172&&b>=16&&b<=31||a === 192&&b===168||a === 169&&b===254||a===0) return true }
|
|
84
100
|
if (host === "::1" || host === "fe80::1" || host.startsWith("fc") || host.startsWith("fd")) return true
|
|
85
101
|
return false
|
|
86
102
|
}
|
|
87
103
|
|
|
104
|
+
// proxyFetch returns a native Response (Headers object, needs .get()) without proxy,
|
|
105
|
+
// but a Response-like with a plain lowercase-keyed Record through the CONNECT tunnel — handle both.
|
|
106
|
+
function headerOf(res, name) {
|
|
107
|
+
const h = res.headers
|
|
108
|
+
if (!h) return null
|
|
109
|
+
if (typeof h.get === "function") return h.get(name)
|
|
110
|
+
return h[name.toLowerCase()] ?? null
|
|
111
|
+
}
|
|
112
|
+
|
|
88
113
|
export const fetchTool = {
|
|
89
114
|
name: "fetch",
|
|
90
115
|
description: DESC("fetch"),
|
|
91
|
-
parameters: {
|
|
92
|
-
type: "object",
|
|
93
|
-
properties: {
|
|
94
|
-
url: { type: "string", description: "http/https URL" },
|
|
95
|
-
},
|
|
96
|
-
required: ["url"],
|
|
97
|
-
},
|
|
116
|
+
parameters: { type: "object", properties: { url: { type: "string", description: "http/https URL" } }, required: ["url"] },
|
|
98
117
|
readonly: true,
|
|
99
118
|
async execute(args, ctx) {
|
|
100
119
|
if (!/^https?:\/\//.test(args.url)) throw new Error("url must start with http:// or https://")
|
|
101
120
|
if (isPrivateUrl(args.url)) throw new Error("fetch blocked: internal/private/metadata addresses are not allowed")
|
|
102
|
-
let response
|
|
103
121
|
try {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
122
|
+
const proxyUri = resolveWebProxy(ctx)
|
|
123
|
+
const response = await proxyFetch(args.url, { headers: { "User-Agent": UA } }, proxyUri)
|
|
124
|
+
if (!response.ok) {
|
|
125
|
+
if ([301, 302, 307, 308].includes(response.status)) {
|
|
126
|
+
const loc = headerOf(response, "location")
|
|
127
|
+
if (loc) {
|
|
128
|
+
const r2 = await proxyFetch(loc, { headers: { "User-Agent": UA } }, proxyUri)
|
|
129
|
+
if (!r2.ok) throw new Error(`fetch failed: HTTP ${r2.status}`)
|
|
130
|
+
const ct2 = headerOf(r2, "content-type") ?? ""
|
|
131
|
+
const b2 = await r2.text()
|
|
132
|
+
return ct2.includes("text/html") ? truncate(htmlToText(b2)) : truncate(b2)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
throw new Error(`fetch failed: HTTP ${response.status}`)
|
|
136
|
+
}
|
|
137
|
+
const ct = headerOf(response, "content-type") ?? ""
|
|
138
|
+
const body = await response.text()
|
|
139
|
+
return ct.includes("text/html") ? truncate(htmlToText(body)) : truncate(body)
|
|
140
|
+
} catch (e) { throw new Error(`fetch failed: ${e.cause?.code ?? e.message}`) }
|
|
120
141
|
},
|
|
121
142
|
}
|
package/src/tools/websearch.md
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
Search the web
|
|
1
|
+
Search the web via Bing. Returns result titles, URLs, and snippets. Use for looking up current information, docs, error messages.
|
|
2
2
|
|
|
3
3
|
Parameters:
|
|
4
4
|
- query (required): Search query
|
|
5
|
-
- limit: Max results (default 8)
|
|
5
|
+
- limit: Max results (default 8, max 20)
|
|
6
|
+
- engine: Specific engine to use — "bing" (Bing). Omit to search all engines concurrently.
|
|
7
|
+
- page: Page number for pagination (1-based, default 1). Only used when engine is specified.
|
|
6
8
|
|
|
7
9
|
Notes:
|
|
8
10
|
- Before searching the web, call `memory_search` first — you may already know the answer from a previous session. Only reach for websearch if memory comes up empty.
|
|
9
11
|
- Use this for information that is NOT in the local codebase — current docs, error messages, API references
|
|
10
12
|
- Follow up with `fetch` to read full pages from the results
|
|
11
|
-
-
|
|
13
|
+
- Proxy support: set `"proxy": {"uri": "http://host:port", "web": true}` in config.json
|
package/src/tui/agent-turn.mjs
CHANGED
|
@@ -10,6 +10,9 @@ import { ansi, C } from "./ansi.mjs"
|
|
|
10
10
|
* handleSlash, summarize } */
|
|
11
11
|
export async function runAgentTurn(ctx, text) {
|
|
12
12
|
const { agent, state, pushLine, pushLabel, render, scheduleRender, ensureAssistantLabel, askPermission, askQuestion, handleSlash, summarize } = ctx
|
|
13
|
+
// 可注入覆盖(测试用);默认走真实实现
|
|
14
|
+
const runAgentImpl = ctx.runAgent ?? runAgent
|
|
15
|
+
const saveSessionImpl = ctx.saveSession ?? saveSession
|
|
13
16
|
pushLabel(`❯ You:`, ansi.bold + C.user)
|
|
14
17
|
pushLine(text, C.text)
|
|
15
18
|
|
|
@@ -205,14 +208,14 @@ export async function runAgentTurn(ctx, text) {
|
|
|
205
208
|
let n = 0
|
|
206
209
|
return () => {
|
|
207
210
|
if (++n % 5 !== 0) return
|
|
208
|
-
try {
|
|
211
|
+
try { saveSessionImpl(agent, state.lines) } catch (e) { console.error(`[session] incremental save failed: ${e.message}`) }
|
|
209
212
|
}
|
|
210
213
|
})(),
|
|
211
214
|
}
|
|
212
215
|
|
|
213
216
|
for (let resume = false; ; resume = true) {
|
|
214
217
|
try {
|
|
215
|
-
await
|
|
218
|
+
await runAgentImpl(agent, text, callbacks, { signal: state.controller.signal, resume })
|
|
216
219
|
flushStream()
|
|
217
220
|
break // Normal completion, exit loop
|
|
218
221
|
} catch (error) {
|
|
@@ -268,28 +271,24 @@ export async function runAgentTurn(ctx, text) {
|
|
|
268
271
|
}
|
|
269
272
|
// Save session after every turn (survives crashes)
|
|
270
273
|
try {
|
|
271
|
-
|
|
274
|
+
saveSessionImpl(agent, state.lines)
|
|
272
275
|
} catch {
|
|
273
276
|
// Save failure doesn't interrupt usage
|
|
274
277
|
}
|
|
275
278
|
render()
|
|
276
279
|
|
|
277
280
|
// Queued messages: auto-process next one
|
|
278
|
-
|
|
281
|
+
while (state.queue.length > 0 && !state.processing) {
|
|
279
282
|
const next = state.queue.shift()
|
|
280
|
-
// Queued slash commands execute directly
|
|
283
|
+
// Queued slash commands execute directly — check every item, not just the first
|
|
281
284
|
if (next.text.startsWith("/")) {
|
|
282
285
|
await handleSlash(next.text)
|
|
283
286
|
render()
|
|
284
|
-
|
|
285
|
-
if (state.queue.length > 0 && !state.processing) {
|
|
286
|
-
const next2 = state.queue.shift()
|
|
287
|
-
await runAgentTurn(ctx, next2.text)
|
|
288
|
-
}
|
|
289
|
-
} else {
|
|
290
|
-
pushLabel(`❯ You: (from queue)`, ansi.bold + C.user)
|
|
291
|
-
await runAgentTurn(ctx, next.text)
|
|
287
|
+
continue
|
|
292
288
|
}
|
|
289
|
+
pushLabel(`❯ You: (from queue)`, ansi.bold + C.user)
|
|
290
|
+
await runAgentTurn(ctx, next.text)
|
|
291
|
+
return
|
|
293
292
|
}
|
|
294
293
|
}
|
|
295
294
|
|