@thincoder/core 0.9.1

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 (203) hide show
  1. package/abort-provenance.mjs +116 -0
  2. package/advisor/citations.mjs +139 -0
  3. package/advisor/compaction.mjs +174 -0
  4. package/advisor/convergence.mjs +80 -0
  5. package/advisor/history.mjs +77 -0
  6. package/advisor/loop.mjs +288 -0
  7. package/advisor/messages.mjs +299 -0
  8. package/advisor/notice.mjs +141 -0
  9. package/advisor/project-context.mjs +197 -0
  10. package/advisor/repos.mjs +150 -0
  11. package/advisor/run.mjs +190 -0
  12. package/advisor/truncate.mjs +57 -0
  13. package/advisor.mjs +281 -0
  14. package/agent/child-marks.mjs +24 -0
  15. package/agent/completion.mjs +145 -0
  16. package/agent/dispatch.mjs +493 -0
  17. package/agent/family-tools.mjs +174 -0
  18. package/agent/helpers.mjs +412 -0
  19. package/agent/post-turn.mjs +70 -0
  20. package/agent/record-results.mjs +174 -0
  21. package/agent/relay-prefix.mjs +39 -0
  22. package/agent/run-stages.mjs +244 -0
  23. package/agent/setup-reminders.mjs +199 -0
  24. package/agent/setup.mjs +234 -0
  25. package/agent/spawn-child.mjs +258 -0
  26. package/agent/suspension.mjs +240 -0
  27. package/agent/write-gate.mjs +87 -0
  28. package/agent-tools/advisor-async.mjs +481 -0
  29. package/agent-tools/advisor-settle.mjs +240 -0
  30. package/agent-tools/advisor.mjs +280 -0
  31. package/agent-tools/async-discard.mjs +143 -0
  32. package/agent-tools/async-settle.mjs +299 -0
  33. package/agent-tools/batch-segment.mjs +265 -0
  34. package/agent-tools/child-permission.mjs +45 -0
  35. package/agent-tools/consult.mjs +471 -0
  36. package/agent-tools/design-token.mjs +117 -0
  37. package/agent-tools/digest-budget.mjs +76 -0
  38. package/agent-tools/eng.mjs +102 -0
  39. package/agent-tools/escalate-async.mjs +302 -0
  40. package/agent-tools/goal.mjs +119 -0
  41. package/agent-tools/panel-blocks.mjs +24 -0
  42. package/agent-tools/parent-channel.mjs +231 -0
  43. package/agent-tools/plan.mjs +86 -0
  44. package/agent-tools/read-history.mjs +309 -0
  45. package/agent-tools/recent-changes.mjs +24 -0
  46. package/agent-tools/review-facts.mjs +31 -0
  47. package/agent-tools/settings.mjs +268 -0
  48. package/agent-tools/skill.mjs +63 -0
  49. package/agent-tools/spawn-gates.mjs +109 -0
  50. package/agent-tools/subagent-actions.mjs +496 -0
  51. package/agent-tools/subagent-async.mjs +456 -0
  52. package/agent-tools/subagent-panel.mjs +160 -0
  53. package/agent-tools/subagent-run.mjs +208 -0
  54. package/agent-tools/subagent-scheduler.mjs +446 -0
  55. package/agent-tools/subagent-spawn.mjs +478 -0
  56. package/agent-tools/subagent.mjs +419 -0
  57. package/agent-tools/task.mjs +87 -0
  58. package/agent-tools/timer.mjs +46 -0
  59. package/agent-tools/verify.mjs +295 -0
  60. package/agent-tools.mjs +23 -0
  61. package/agent.mjs +430 -0
  62. package/auto-think.mjs +115 -0
  63. package/compress-form.mjs +24 -0
  64. package/config-io.mjs +277 -0
  65. package/config-migrate.mjs +178 -0
  66. package/config-presets.mjs +49 -0
  67. package/config.mjs +419 -0
  68. package/context.mjs +495 -0
  69. package/conventions.mjs +223 -0
  70. package/embedding.mjs +120 -0
  71. package/escape.mjs +152 -0
  72. package/expand-home.mjs +16 -0
  73. package/explore-distill.mjs +152 -0
  74. package/generate-title.mjs +123 -0
  75. package/git/checkpoint.mjs +448 -0
  76. package/git/gitmem.mjs +100 -0
  77. package/history-window.mjs +179 -0
  78. package/hooks.mjs +108 -0
  79. package/i18n.mjs +106 -0
  80. package/index-bin.mjs +48 -0
  81. package/index-discover.mjs +176 -0
  82. package/ledger-cmd.mjs +209 -0
  83. package/ledger-db.mjs +86 -0
  84. package/ledger-surface.mjs +76 -0
  85. package/ledger.mjs +202 -0
  86. package/log.mjs +195 -0
  87. package/manifest.mjs +338 -0
  88. package/markdown.mjs +106 -0
  89. package/mcp/helpers.mjs +51 -0
  90. package/mcp/transport-http.mjs +248 -0
  91. package/mcp/transport-stdio.mjs +140 -0
  92. package/mcp/transport-ws.mjs +122 -0
  93. package/mcp.mjs +295 -0
  94. package/memory/code-index.mjs +219 -0
  95. package/memory/code-sync.mjs +427 -0
  96. package/memory/core.mjs +318 -0
  97. package/memory/delete.mjs +242 -0
  98. package/memory/docs.mjs +431 -0
  99. package/memory/file-walk.mjs +109 -0
  100. package/memory/origin.mjs +24 -0
  101. package/memory/scan.mjs +177 -0
  102. package/memory/schema.mjs +460 -0
  103. package/memory.mjs +21 -0
  104. package/model-ref.mjs +66 -0
  105. package/model-specs.mjs +277 -0
  106. package/package.json +32 -0
  107. package/peer-domains.mjs +265 -0
  108. package/peer-instances.mjs +178 -0
  109. package/permission.mjs +79 -0
  110. package/process-probe.mjs +315 -0
  111. package/prompt-files.mjs +113 -0
  112. package/prompt-overlays.mjs +78 -0
  113. package/prompts/advisor-design.md +43 -0
  114. package/prompts/advisor-round1.md +41 -0
  115. package/prompts/advisor-round2.md +46 -0
  116. package/prompts/advisor-round3.md +42 -0
  117. package/prompts/common.md +158 -0
  118. package/prompts/consult-base.md +19 -0
  119. package/prompts/discipline-engineering.md +123 -0
  120. package/prompts/discipline-normal.md +206 -0
  121. package/prompts/persona-coder.md +21 -0
  122. package/prompts/persona-eng-coder.md +41 -0
  123. package/prompts/persona-eng-designer.md +80 -0
  124. package/prompts/persona-engineering.md +160 -0
  125. package/prompts/persona-explore.md +15 -0
  126. package/prompts/persona-normal.md +35 -0
  127. package/prompts/persona-plan.md +27 -0
  128. package/provider/anthropic.mjs +225 -0
  129. package/provider/core.mjs +491 -0
  130. package/provider/errors.mjs +101 -0
  131. package/provider/google.mjs +257 -0
  132. package/provider/index.mjs +7 -0
  133. package/provider/list-models.mjs +163 -0
  134. package/provider/normalize.mjs +81 -0
  135. package/provider/rate.mjs +168 -0
  136. package/provider/responses.mjs +495 -0
  137. package/provider/retry.mjs +88 -0
  138. package/provider/sse.mjs +264 -0
  139. package/provider/wait-status.mjs +59 -0
  140. package/proxy.mjs +274 -0
  141. package/rules.mjs +53 -0
  142. package/session-gc.mjs +248 -0
  143. package/session-guard.mjs +59 -0
  144. package/session-lifecycle.mjs +305 -0
  145. package/session-migrate.mjs +48 -0
  146. package/session-rename.mjs +38 -0
  147. package/session-segments.mjs +99 -0
  148. package/session-slot-write.mjs +168 -0
  149. package/session-slots-manifest.mjs +264 -0
  150. package/session-slots.mjs +298 -0
  151. package/session-store.mjs +441 -0
  152. package/session.mjs +244 -0
  153. package/skills.mjs +234 -0
  154. package/text-budget.mjs +79 -0
  155. package/token-ttl.mjs +285 -0
  156. package/tool-docs/apply_patch.md +15 -0
  157. package/tool-docs/bash.md +38 -0
  158. package/tool-docs/delete.md +13 -0
  159. package/tool-docs/edit.md +30 -0
  160. package/tool-docs/execute.md +21 -0
  161. package/tool-docs/fetch.md +12 -0
  162. package/tool-docs/file_ops.md +17 -0
  163. package/tool-docs/get_current_time.md +8 -0
  164. package/tool-docs/git.md +54 -0
  165. package/tool-docs/glob.md +11 -0
  166. package/tool-docs/grep.md +19 -0
  167. package/tool-docs/hashline_edit.md +14 -0
  168. package/tool-docs/insert_after.md +15 -0
  169. package/tool-docs/lint.md +10 -0
  170. package/tool-docs/ls.md +12 -0
  171. package/tool-docs/lsp.md +10 -0
  172. package/tool-docs/process.md +10 -0
  173. package/tool-docs/question.md +16 -0
  174. package/tool-docs/read.md +20 -0
  175. package/tool-docs/read_image.md +8 -0
  176. package/tool-docs/tree.md +14 -0
  177. package/tool-docs/wait_for.md +22 -0
  178. package/tool-docs/websearch.md +16 -0
  179. package/tool-docs/write.md +11 -0
  180. package/tools/bash.mjs +276 -0
  181. package/tools/edit-batch.mjs +204 -0
  182. package/tools/edit-diff.mjs +388 -0
  183. package/tools/exec-run.mjs +43 -0
  184. package/tools/execute.mjs +243 -0
  185. package/tools/file.mjs +464 -0
  186. package/tools/git-checkpoint.mjs +143 -0
  187. package/tools/git-ext.mjs +173 -0
  188. package/tools/git.mjs +415 -0
  189. package/tools/glob-dialect.mjs +130 -0
  190. package/tools/index.mjs +76 -0
  191. package/tools/linter.mjs +120 -0
  192. package/tools/lsp.mjs +335 -0
  193. package/tools/ops.mjs +293 -0
  194. package/tools/patch.mjs +290 -0
  195. package/tools/question.mjs +26 -0
  196. package/tools/repomap.mjs +314 -0
  197. package/tools/search.mjs +248 -0
  198. package/tools/shared.mjs +467 -0
  199. package/tools/tree.mjs +81 -0
  200. package/tools/web.mjs +224 -0
  201. package/tools/write-path.mjs +191 -0
  202. package/traces/trace-store.mjs +303 -0
  203. package/undo-stack.mjs +47 -0
package/mcp.mjs ADDED
@@ -0,0 +1,295 @@
1
+ /**
2
+ * mcp.mjs — MCP (Model Context Protocol) client
3
+ * config: { command, args?, name } or { url, name, headers? } or { wsUrl, name, headers? }
4
+ */
5
+ import { INIT_TIMEOUT_MS, withTimeout, sanitizeToolName } from "./mcp/helpers.mjs"
6
+ import { stdioTransport } from "./mcp/transport-stdio.mjs"
7
+ import { httpTransport } from "./mcp/transport-http.mjs"
8
+ import { wsTransport } from "./mcp/transport-ws.mjs"
9
+
10
+ /** F6/D-5:config.token → `Authorization: Bearer <token>` 合成(仅当 headers 未显式给
11
+ * Authorization——显式优先,向后兼容)。合成发生在传给 transport 前,不写回 config。 */
12
+ export function withBearerToken(config) {
13
+ if (!config?.token || config.headers?.Authorization) return config
14
+ return { ...config, headers: { ...config.headers, Authorization: `Bearer ${config.token}` } }
15
+ }
16
+
17
+ /** Race a pending MCP request against an abort signal — a hung MCP server must not
18
+ * hold the turn hostage. signal absent → passthrough. */
19
+ async function sendWithSignal(promise, signal) {
20
+ if (!signal) return promise
21
+ return new Promise((resolve, reject) => {
22
+ const onAbort = () => {
23
+ const e = new DOMException("The operation was aborted", "AbortError")
24
+ e.reason = signal.reason
25
+ reject(e)
26
+ }
27
+ if (signal.aborted) return onAbort()
28
+ signal.addEventListener("abort", onAbort, { once: true })
29
+ promise.then(
30
+ (v) => { signal.removeEventListener("abort", onAbort); resolve(v) },
31
+ (e) => { signal.removeEventListener("abort", onAbort); reject(e) },
32
+ )
33
+ })
34
+ }
35
+
36
+ // ---- MCP lifecycle ----
37
+
38
+ /** 2026-08-31 MCP 会诊 P5:CLI session 注册表(serverName → session)。
39
+ * session.state.transport 可变(重连替换);buildTools 的 execute 动态取
40
+ * session.state.transport——server 崩溃后无需重建 agent.tools 即自愈。
41
+ * 2026-09-01 MCP.md §4:导出给 /mcp test 的零副作用断言用(probe 后 _sessions 不增)。 */
42
+ export const _sessions = new Map()
43
+ /** 退避重连进行中(serverName → promise)——与 vscode 语义对齐;延迟表见 _mcpHooks。 */
44
+ const _reconnecting = new Map()
45
+
46
+ /** 2026-08-31 MCP 会诊 P5:测试钩子(退避延迟可替换,惯例同 rate.mjs _rateHooks)。
47
+ * scheduleReconnect 与 ensureAlive 读这里的 delay/reconnectDelays——测试可注入微秒级延迟。 */
48
+ export const _mcpHooks = {
49
+ delay: (ms) => new Promise((r) => setTimeout(r, ms)),
50
+ reconnectDelays: [1000, 2000, 4000, 8000],
51
+ }
52
+
53
+ /** 按 config 创建并完成握手的 transport(findTransportConfig 与 vscode 对齐)。 */
54
+ async function createConnectedTransport(rawConfig, serverName) {
55
+ const config = withBearerToken(rawConfig) // F6/D-5:token 合成(不写回原 config)
56
+ let transport
57
+ if (config.wsUrl) {
58
+ transport = wsTransport(config.wsUrl, config.headers ?? {})
59
+ await transport.connect()
60
+ } else if (config.url) {
61
+ transport = httpTransport(config.url, config.headers ?? {})
62
+ try {
63
+ await transport.openSSE()
64
+ } catch {
65
+ // Server doesn't support GET SSE — degrade to pure Streamable HTTP POST mode.
66
+ // MCP.md §4 D-1:显式标记 postOnly——POST-only server(glm-websearch 类)isAlive
67
+ // 不得因 eventSource == null 误判死(否则 ensureAlive 触发无意义重连循环)。
68
+ transport.markPostOnly()
69
+ }
70
+ } else {
71
+ transport = stdioTransport(config.command, config.args ?? [], config.env)
72
+ }
73
+ const mcpTools = await doInitialize(transport, serverName)
74
+ return { transport, mcpTools }
75
+ }
76
+
77
+ /** F4/D-2:一次性探活——createConnectedTransport(initialize + tools/list)+ 计时。
78
+ * 零副作用:不进 _sessions、不动 agent.tools、无 onDead 挂钩;finally close
79
+ * (closed=true 使 onDead 重连不会触发)。initialize 与 tools/list 同受
80
+ * INIT_TIMEOUT_MS 约束(见 doInitialize)。不复用 connectMcpServer(避免污染
81
+ * session 幂等表)。 */
82
+ export async function probeMcpServer(config) {
83
+ const start = Date.now()
84
+ let transport
85
+ let mcpTools
86
+ try {
87
+ ;({ transport, mcpTools } = await createConnectedTransport(config, config.name ?? config.command ?? config.url ?? config.wsUrl))
88
+ return { ok: true, toolCount: mcpTools.length, latencyMs: Date.now() - start }
89
+ } catch (error) {
90
+ return { ok: false, error: error?.message ?? String(error) }
91
+ } finally {
92
+ try { transport?.close() } catch { /* ignore */ }
93
+ }
94
+ }
95
+
96
+ /** onDead → 后台退避重连;成功替换 session.state.transport(tools 闭包动态引用,
97
+ * agent.tools 无需重建);失败静默(下次 execute 前置检查再试)。 */
98
+ function scheduleReconnect(name, config) {
99
+ if (_reconnecting.has(name)) return _reconnecting.get(name)
100
+ const p = (async () => {
101
+ let lastErr
102
+ for (const delayMs of _mcpHooks.reconnectDelays) {
103
+ await _mcpHooks.delay(delayMs)
104
+ try {
105
+ const { transport } = await createConnectedTransport(config, name)
106
+ const session = _sessions.get(name)
107
+ if (!session || session.closed) { transport.close(); return false }
108
+ attachSession(session, transport)
109
+ return true
110
+ } catch (error) {
111
+ lastErr = error
112
+ }
113
+ }
114
+ console.error(`[mcp] ${name} reconnect failed after ${_mcpHooks.reconnectDelays.length} attempts: ${lastErr?.message ?? lastErr}`)
115
+ return false
116
+ })().finally(() => _reconnecting.delete(name))
117
+ _reconnecting.set(name, p)
118
+ return p
119
+ }
120
+
121
+ /** 给 session 绑定一个新 transport(建连 onDead 钩子 → 自愈链)。 */
122
+ function attachSession(session, transport) {
123
+ session.state.transport = transport
124
+ transport.onDead?.(() => {
125
+ if (session.state.transport !== transport) return // 已再替换,旧钩子作废
126
+ scheduleReconnect(session.config.name ?? session.config.command ?? session.config.url ?? session.config.wsUrl, session.config)
127
+ })
128
+ }
129
+
130
+ function buildTools(mcpTools, session, config) {
131
+ const prefix = config.name ? `${config.name}_` : "mcp_"
132
+ // 2026-08-31 MCP 会诊 P6:sanitize 碰撞/空名防御 + schema/description 类型守卫 + 输出防御
133
+ const seen = new Set()
134
+ const out = []
135
+ for (const t of mcpTools) {
136
+ const rawName = sanitizeToolName(prefix + t.name)
137
+ if (!rawName || rawName === "mcp_") continue
138
+ let name = rawName
139
+ for (let n = 2; seen.has(name); n++) name = `${rawName}_${n}`
140
+ seen.add(name)
141
+ out.push({
142
+ name,
143
+ description: typeof t.description === "string" ? t.description : `MCP tool: ${t.name}`,
144
+ parameters: (t.inputSchema && typeof t.inputSchema === "object") ? t.inputSchema : { type: "object", properties: {} },
145
+ readonly: false,
146
+ async execute(args, ctx) {
147
+ // 2026-08-31 会诊 #11:上层 signal 中断 + send 第 3 参底层取消(pending 即刻作废)
148
+ // P5:执行前动态取 session.transport——死亡时等重连 promise/触发一次重连再试
149
+ const transport = await ensureAlive(session)
150
+ const send = transport.send("tools/call", { name: t.name, arguments: args }, ctx?.signal)
151
+ const resp = await sendWithSignal(send, ctx?.signal)
152
+ if (resp.error) throw new Error(`MCP tool "${t.name}": ${resp.error.message}`)
153
+ if (resp.result?.isError) throw new Error(`MCP tool "${t.name}": ${extractMcpText(resp.result.content) || "(server reported an error)"}`)
154
+ return truncateMcpOutput(extractMcpText(resp.result?.content ?? [])) || "(no output)"
155
+ },
156
+ _mcpTransport: session.state.transport,
157
+ _mcpName: config.name,
158
+ })
159
+ }
160
+ return out
161
+ }
162
+
163
+ /** P5:拿到活的 transport——死连接等待/触发重连;耗尽后抛错(工具错误透给模型)。 */
164
+ async function ensureAlive(session) {
165
+ const t = session.state.transport
166
+ if (t?.isAlive?.()) return t
167
+ const name = session.config.name ?? session.config.command ?? session.config.url ?? session.config.wsUrl
168
+ let reconnect = _reconnecting.get(name)
169
+ if (!reconnect) reconnect = scheduleReconnect(name, session.config)
170
+ const ok = await reconnect
171
+ if (!ok || !session.state.transport?.isAlive?.()) {
172
+ throw new Error(`MCP server "${name}" is unavailable (reconnect failed)`)
173
+ }
174
+ return session.state.transport
175
+ }
176
+
177
+ /** MCP content 数组 → 文本(非数组/元素非对象防御)。 */
178
+ function extractMcpText(content) {
179
+ if (!Array.isArray(content)) return typeof content === "string" ? content : JSON.stringify(content)
180
+ return content
181
+ .filter((c) => c && typeof c === "object")
182
+ .map((c) => (c.type === "text" ? c.text : c.type === "resource" ? `[resource: ${c.resource?.uri}]` : JSON.stringify(c)))
183
+ .join("\n")
184
+ }
185
+
186
+ /** 输出截断(32KB 上限,防 server 回 10MB 撑爆上下文)。 */
187
+ function truncateMcpOutput(text) {
188
+ if (text.length <= 32_000) return text
189
+ return text.slice(0, 32_000) + "\n[… truncated: " + (text.length - 32_000) + " chars omitted]"
190
+ }
191
+
192
+ async function doInitialize(transport, _name) {
193
+ const initResp = await withTimeout(
194
+ transport.send("initialize", {
195
+ protocolVersion: "2024-11-05",
196
+ capabilities: {},
197
+ clientInfo: { name: "thincoder", version: "1.0.0" },
198
+ }),
199
+ INIT_TIMEOUT_MS,
200
+ )
201
+ if (initResp.error) throw new Error(`initialize error: ${initResp.error.message}`)
202
+ transport.notify?.("notifications/initialized", {})
203
+
204
+ // 2026-08-31 MCP 会诊 P6:tools/list 分页被忽略(nextCursor 多页工具静默丢失)——
205
+ // 循环跟随 cursor 直到 server 不再返回(上限 20 页防死循环)。
206
+ // MCP.md §4 评审 #8:每页同受 INIT_TIMEOUT_MS 约束(否则 probe 的延迟统计无界)。
207
+ const tools = []
208
+ let cursor
209
+ for (let page = 0; page < 20; page++) {
210
+ const toolsResp = await withTimeout(
211
+ transport.send("tools/list", cursor ? { cursor } : {}),
212
+ INIT_TIMEOUT_MS,
213
+ )
214
+ if (toolsResp.error) throw new Error(`tools/list failed: ${toolsResp.error.message}`)
215
+ tools.push(...(toolsResp.result?.tools ?? []))
216
+ cursor = toolsResp.result?.nextCursor
217
+ if (!cursor) break
218
+ }
219
+ return tools
220
+ }
221
+
222
+ /** Connect to an MCP server (stdio/http/ws), initialize, and return built tool wrappers.
223
+ * 2026-08-31 MCP 会诊 P5:session 幂等——同 name 活连接复用(make-agent 每进程一次,
224
+ * cmd-mcp 重复 add 同一 server 不会双实例),崩溃后下次调用/execute 自愈。 */
225
+ export async function connectMcpServer(config) {
226
+ if (!config || (!config.command && !config.url && !config.wsUrl))
227
+ throw new Error(`MCP server "${config?.name ?? ""}": needs either 'wsUrl' (websocket), 'command' (stdio), or 'url' (http)`)
228
+ const name = config.name ?? config.command ?? config.url ?? config.wsUrl
229
+ // MCP.md §4 D-5:fingerprint 计入 token 字段——/mcp edit 改 token → 指纹变更 →
230
+ // 旧连接主动关闭重建(T13)。
231
+ const configFingerprint = JSON.stringify([config.command ?? null, config.args ?? null, config.url ?? null, config.wsUrl ?? null, config.env ?? null, config.headers ?? null, config.token ?? null])
232
+
233
+ const existing = _sessions.get(name)
234
+ if (existing && !existing.closed) {
235
+ const sameConfig = existing.configFingerprint === configFingerprint
236
+ if (sameConfig && existing.state.transport?.isAlive?.()) {
237
+ return existing.state.tools
238
+ }
239
+ if (!sameConfig) {
240
+ // config 变更:主动关闭旧连接(不触发 onDead 重连)并丢弃 session
241
+ existing.closed = true
242
+ try { existing.state.transport?.close() } catch { /* ignore */ }
243
+ _sessions.delete(name)
244
+ }
245
+ }
246
+
247
+ let transport
248
+ let mcpTools
249
+ try {
250
+ ;({ transport, mcpTools } = await createConnectedTransport(config, name))
251
+ } catch (error) {
252
+ // MCP.md §4 评审 #7:握手失败不泄漏 transport(GET SSE 降级成功但 POST initialize
253
+ // 失败时,openSSE 若开了流会留一个悬挂的 reader/请求)。
254
+ try { transport?.close() } catch { /* ignore */ }
255
+ throw error
256
+ }
257
+ const session = {
258
+ config, configFingerprint,
259
+ state: { transport, tools: null },
260
+ closed: false,
261
+ }
262
+ session.state.tools = buildTools(mcpTools, session, config)
263
+ attachSession(session, transport)
264
+ _sessions.set(name, session)
265
+ return session.state.tools
266
+ }
267
+
268
+ /** Close all MCP transport connections on an agent's tools */
269
+ export function closeAllMcp(agent) {
270
+ for (const t of agent.tools) {
271
+ if (t._mcpTransport) closeSession(t._mcpName)
272
+ }
273
+ }
274
+
275
+ /** 主动关闭 session(不触发 onDead 重连):transport close + 登记关闭标记 + 清注册表。 */
276
+ function closeSession(name) {
277
+ const session = _sessions.get(name)
278
+ if (!session) return
279
+ session.closed = true
280
+ try { session.state.transport?.close() } catch { /* ignore */ }
281
+ _sessions.delete(name)
282
+ }
283
+
284
+ /** Remove MCP tools belonging to a specific server from the agent's tool list */
285
+ export function removeMcpTools(agent, serverName) {
286
+ const keep = []
287
+ for (const t of agent.tools) {
288
+ if (t._mcpName === serverName) {
289
+ closeSession(serverName)
290
+ } else {
291
+ keep.push(t)
292
+ }
293
+ }
294
+ agent.tools = keep
295
+ }
@@ -0,0 +1,219 @@
1
+ /**
2
+ * memory/code-index.mjs — code and document chunking, language detection, symbol extraction
3
+ */
4
+
5
+ import { segmentCJK, CODE_EXTS, DOC_EXTS, SKIP_DIRS, BIG_FILE_LINES } from "./schema.mjs"
6
+
7
+ /** Infer file language by extension */
8
+ export function detectLanguage(filename) {
9
+ const ext = filename.slice(filename.lastIndexOf(".")).toLowerCase()
10
+ const map = {
11
+ ".mjs": "javascript", ".js": "javascript", ".cjs": "javascript", ".jsx": "jsx",
12
+ ".ts": "typescript", ".tsx": "tsx", ".mts": "typescript", ".cts": "typescript",
13
+ ".py": "python", ".rs": "rust", ".go": "go", ".java": "java",
14
+ ".c": "c", ".h": "c", ".cpp": "cpp", ".hpp": "cpp",
15
+ ".rb": "ruby", ".swift": "swift", ".kt": "kotlin", ".dart": "dart", ".lua": "lua",
16
+ ".cs": "csharp", ".fs": "fsharp", ".fsx": "fsharp",
17
+ ".clj": "clojure", ".cljs": "clojure", ".ex": "elixir", ".exs": "elixir",
18
+ ".erl": "erlang", ".hrl": "erlang", ".scala": "scala", ".groovy": "groovy",
19
+ ".pl": "perl", ".pm": "perl", ".r": "r", ".jl": "julia", ".zig": "zig",
20
+ ".ps1": "powershell", ".proto": "protobuf", ".graphql": "graphql", ".tf": "terraform", ".hcl": "hcl",
21
+ ".sh": "bash", ".bash": "bash", ".sql": "sql",
22
+ ".yaml": "yaml", ".yml": "yaml", ".toml": "toml", ".json": "json",
23
+ ".css": "css", ".html": "html", ".vue": "vue", ".svelte": "svelte",
24
+ ".md": "markdown", ".mdc": "markdown", ".mdx": "markdown", ".org": "org", ".wiki": "wiki", ".tex": "tex",
25
+ }
26
+ return map[ext] ?? ext.slice(1)
27
+ }
28
+
29
+ /**
30
+ * Extract top-level symbol declarations (functions, classes, const exports, etc.)
31
+ * from JS/TS files using regex. Returns [{ name, line, kind }].
32
+ */
33
+ export function extractSymbols(lines, ext) {
34
+ const jsish = new Set([".mjs", ".js", ".ts", ".jsx", ".tsx"])
35
+ if (!jsish.has(ext)) return []
36
+
37
+ const symbols = []
38
+ const text = lines.join("\n")
39
+ const re = /(?:export\s+)?(?:(?:async\s+)?function\s+(\w+)|class\s+(\w+)|(?:export\s+)?(?:const|let|var)\s+(\w+))/gm
40
+ let m
41
+ while ((m = re.exec(text))) {
42
+ const name = m[1] || m[2] || m[3]
43
+ if (!name || name[0] !== name[0].toLowerCase() && name.length < 2) continue
44
+ const line = text.slice(0, m.index).split("\n").length
45
+ symbols.push({ name, line, kind: m[1] ? "function" : m[2] ? "class" : "variable" })
46
+ }
47
+ return symbols
48
+ }
49
+
50
+ /** Extract top-level def/class from Python files. */
51
+ export function extractPySymbols(lines) {
52
+ const symbols = []
53
+ const re = /^(?:async\s+)?(?:def|class)\s+(\w+)/gm
54
+ const text = lines.join("\n")
55
+ let m
56
+ while ((m = re.exec(text))) {
57
+ symbols.push({ name: m[1], line: text.slice(0, m.index).split("\n").length, kind: text[m.index] === "c" ? "class" : "function" })
58
+ }
59
+ return symbols
60
+ }
61
+
62
+ /**
63
+ * Split a file into code chunks. Small files become a single chunk;
64
+ * large files are split by symbol, with inter-symbol content merged into the preceding symbol chunk.
65
+ * Each chunk includes the JSDoc/docstring before its symbol to improve search quality.
66
+ */
67
+ export function chunkCode(lines, filepath) {
68
+ const ext = filepath.slice(filepath.lastIndexOf(".")).toLowerCase()
69
+ const chunks = []
70
+
71
+ if (lines.length <= BIG_FILE_LINES) {
72
+ const doc = extractLeadingDoc(lines, 1, ext)
73
+ const content = (doc ? doc + "\n" : "") + lines.join("\n").trimEnd()
74
+ chunks.push({ name: filepath, line_start: 1, line_end: lines.length, content })
75
+ return chunks
76
+ }
77
+
78
+ const symbols = ext === ".py" ? extractPySymbols(lines) : extractSymbols(lines, ext)
79
+ if (symbols.length <= 1) {
80
+ const doc = extractLeadingDoc(lines, 1, ext)
81
+ const content = (doc ? doc + "\n" : "") + lines.join("\n").trimEnd()
82
+ chunks.push({ name: filepath, line_start: 1, line_end: lines.length, content })
83
+ return chunks
84
+ }
85
+
86
+ for (let i = 0; i < symbols.length; i++) {
87
+ const sym = symbols[i]
88
+ const start = sym.line
89
+ const end = i + 1 < symbols.length ? symbols[i + 1].line - 1 : lines.length
90
+ if (start > end) continue
91
+ const doc = extractLeadingDoc(lines, start, ext)
92
+ const body = lines.slice(start - 1, end).join("\n").trimEnd()
93
+ const content = (doc ? doc + "\n" : "") + body
94
+ if (!content) continue
95
+ chunks.push({ name: `${filepath}:${sym.name}`, line_start: start, line_end: end, content })
96
+ }
97
+ return chunks
98
+ }
99
+
100
+ /**
101
+ * Extract the JSDoc/docstring comment preceding a given line.
102
+ * JS/TS: scan backwards for JSDoc block comments or consecutive // comment lines
103
+ * Python: look for a """...""" docstring on the line after the symbol definition
104
+ */
105
+ export function extractLeadingDoc(lines, lineNum, ext) {
106
+ if (ext === ".py") {
107
+ if (lineNum >= lines.length) return ""
108
+ const next = lines[lineNum]
109
+ const m = next?.match(/^\s*"""(.+?)"""\s*$/)
110
+ if (m) return m[1].trim()
111
+ if (/^\s*"""\s*$/.test(next)) {
112
+ const parts = []
113
+ for (let i = lineNum + 1; i < lines.length && i < lineNum + 8; i++) {
114
+ if (/^\s*"""\s*$/.test(lines[i])) break
115
+ parts.push(lines[i].trim())
116
+ }
117
+ const text = parts.join(" ").trim()
118
+ return text.length > 0 && text.length < 300 ? text : ""
119
+ }
120
+ return ""
121
+ }
122
+
123
+ const jsish = new Set([".mjs", ".js", ".ts", ".jsx", ".tsx"])
124
+ if (!jsish.has(ext)) return ""
125
+
126
+ const parts = []
127
+ let i = lineNum - 2
128
+ if (i >= 0 && /^\s*\*\/\s*$/.test(lines[i])) {
129
+ while (i >= 0) {
130
+ const line = lines[i].trim()
131
+ if (/^\s*\/\*\*/.test(line)) {
132
+ parts.unshift(line.replace(/^\s*\/\*\*\s*/, "").replace(/\s*\*\/\s*$/, "").trim())
133
+ break
134
+ }
135
+ parts.unshift(line.replace(/^\s*\*\s?/, "").trim())
136
+ i--
137
+ }
138
+ } else {
139
+ while (i >= 0 && /^\s*\/\//.test(lines[i])) {
140
+ parts.unshift(lines[i].replace(/^\s*\/\/\s*/, "").trim())
141
+ i--
142
+ }
143
+ }
144
+
145
+ const text = parts.join(" ").trim()
146
+ return text.length > 0 && text.length < 300 ? text : ""
147
+ }
148
+
149
+ /** Yield control to the event loop for one tick (allows keyboard input to be processed). Uses setImmediate for lower latency than setTimeout(0). */
150
+ export function yieldTick() {
151
+ return new Promise((r) => setImmediate(r))
152
+ }
153
+
154
+ /** Index a single file: delete old chunks → chunk → insert new chunks */
155
+ export function _upsertCodeFile(memory, origin, rel, lines, lang, mtimeMs) {
156
+ const chunks = chunkCode(lines, rel)
157
+ memory.db.exec("BEGIN")
158
+ try {
159
+ memory.db.prepare(`DELETE FROM code_chunks WHERE origin = ? AND path = ?`).run(origin, rel)
160
+ const insert = memory.db.prepare(`
161
+ INSERT INTO code_chunks (origin, path, language, chunk_type, symbol_name, content, line_start, line_end, mtime_ms, seg_content)
162
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
163
+ `)
164
+ for (const c of chunks) {
165
+ const isFile = c.name === rel
166
+ insert.run(origin, rel, lang, isFile ? "file" : "symbol", isFile ? "" : c.name.slice(rel.length + 1), c.content, c.line_start, c.line_end, mtimeMs, segmentCJK(c.content))
167
+ }
168
+ memory.db.exec("COMMIT")
169
+ } catch (e) {
170
+ memory.db.exec("ROLLBACK")
171
+ throw e
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Split a markdown file by ## headings. Each ## section is indexed independently,
177
+ * with the heading path as the heading label (e.g. "README.md > Deployment > Docker") for easy retrieval.
178
+ */
179
+ export function chunkMarkdown(lines, filepath) {
180
+ const chunks = []
181
+ let start = 1
182
+ let heading = filepath
183
+
184
+ for (let i = 0; i < lines.length; i++) {
185
+ const m = lines[i].match(/^(#{1,4})\s+(.+)/)
186
+ if (m) {
187
+ if (i > start) {
188
+ chunks.push({ heading, line_start: start, line_end: i, content: lines.slice(start - 1, i).join("\n").trimEnd() })
189
+ }
190
+ heading = `${filepath} > ${m[2].trim()}`
191
+ start = i + 1
192
+ }
193
+ }
194
+ if (start <= lines.length) {
195
+ chunks.push({ heading, line_start: start, line_end: lines.length, content: lines.slice(start - 1).join("\n").trimEnd() })
196
+ }
197
+ return chunks.filter((c) => c.content)
198
+ }
199
+
200
+ /** Upsert a documentation file's chunks into the doc_chunks table within a transaction */
201
+ export function _upsertDocFile(memory, origin, rel, lines, mtimeMs) {
202
+ const chunks = chunkMarkdown(lines, rel)
203
+ const lang = rel.endsWith(".rst") ? "rst" : rel.endsWith(".adoc") ? "asciidoc" : rel.endsWith(".txt") ? "text" : "markdown"
204
+ memory.db.exec("BEGIN")
205
+ try {
206
+ memory.db.prepare(`DELETE FROM doc_chunks WHERE origin = ? AND path = ?`).run(origin, rel)
207
+ const insert = memory.db.prepare(`
208
+ INSERT INTO doc_chunks (origin, path, language, heading, content, line_start, line_end, mtime_ms, seg_content)
209
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
210
+ `)
211
+ for (const c of chunks) {
212
+ insert.run(origin, rel, lang, c.heading, c.content, c.line_start, c.line_end, mtimeMs, segmentCJK(c.content))
213
+ }
214
+ memory.db.exec("COMMIT")
215
+ } catch (e) {
216
+ memory.db.exec("ROLLBACK")
217
+ throw e
218
+ }
219
+ }