@thincoder/core 0.11.5 → 0.12.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.
- package/CHANGELOG.md +27 -0
- package/abort-provenance.mjs +23 -0
- package/advisor/citations.mjs +5 -1
- package/advisor/compaction.mjs +13 -10
- package/advisor/history.mjs +6 -1
- package/advisor/loop.mjs +35 -10
- package/advisor/messages.mjs +31 -43
- package/advisor/project-context.mjs +49 -45
- package/advisor/run.mjs +82 -26
- package/advisor.mjs +40 -11
- package/agent/assemble.mjs +5 -5
- package/agent/chat-call.mjs +21 -1
- package/agent/completion.mjs +1 -20
- package/agent/dispatch-gates.mjs +8 -3
- package/agent/dispatch-run.mjs +131 -20
- package/agent/dispatch.mjs +23 -22
- package/agent/family-tools.mjs +4 -7
- package/agent/goal-judge.mjs +99 -0
- package/agent/goal-state.mjs +96 -0
- package/agent/helpers.mjs +13 -4
- package/agent/offload.mjs +1 -1
- package/agent/planning-graph-guardrail.mjs +224 -0
- package/agent/planning-prompt.mjs +179 -0
- package/agent/planning-step.mjs +460 -173
- package/agent/post-turn.mjs +23 -33
- package/agent/record-results.mjs +8 -5
- package/agent/run-stages.mjs +18 -38
- package/agent/run-start.mjs +26 -19
- package/agent/setup-reminders.mjs +16 -14
- package/agent/setup.mjs +3 -2
- package/agent/spawn-child.mjs +17 -4
- package/agent/suspension.mjs +42 -21
- package/agent/task-lifecycle.mjs +165 -52
- package/agent/timers.mjs +9 -1
- package/agent/turn-loop.mjs +160 -69
- package/agent/turn-projection.mjs +26 -15
- package/agent/turn-task-state.mjs +4 -4
- package/agent/write-gate-ledger.mjs +1 -1
- package/agent/write-gate.mjs +14 -11
- package/agent-tools/advisor-async.mjs +2 -0
- package/agent-tools/advisor-launch.mjs +27 -13
- package/agent-tools/advisor-settle.mjs +19 -18
- package/agent-tools/advisor.mjs +90 -13
- package/agent-tools/bash-async.mjs +3 -3
- package/agent-tools/batch-ledger-bind.mjs +8 -5
- package/agent-tools/batch-read.mjs +42 -0
- package/agent-tools/batch-record-write.mjs +20 -12
- package/agent-tools/batch-segment-vocab.mjs +6 -4
- package/agent-tools/batch-skeleton.mjs +43 -12
- package/agent-tools/consult.mjs +5 -1
- package/agent-tools/context.mjs +5 -4
- package/agent-tools/design-slots.mjs +7 -4
- package/agent-tools/design-token.mjs +3 -2
- package/agent-tools/doc-graph.mjs +257 -95
- package/agent-tools/escalate-async.mjs +4 -1
- package/agent-tools/goal.mjs +28 -95
- package/agent-tools/plan.mjs +1 -1
- package/agent-tools/review-facts.mjs +56 -7
- package/agent-tools/settings.mjs +35 -1
- package/agent-tools/ssh-transport.mjs +23 -22
- package/agent-tools/step.mjs +24 -4
- package/agent-tools/subagent-actions.mjs +6 -2
- package/agent-tools/subagent-async.mjs +4 -2
- package/agent-tools/subagent.mjs +1 -1
- package/agent-tools/task.mjs +73 -65
- package/agent-tools.mjs +3 -3
- package/agent.mjs +12 -3
- package/auto-think.mjs +16 -3
- package/browser/actions.mjs +5 -1
- package/browser/cdp.mjs +1 -1
- package/browser/pool.mjs +110 -17
- package/compaction-work-state.mjs +103 -13
- package/compress-form.mjs +29 -3
- package/config-defaults.mjs +27 -3
- package/config-io.mjs +3 -14
- package/config-normalize.mjs +24 -4
- package/config-presets.mjs +40 -26
- package/config-warn.mjs +30 -0
- package/config.mjs +139 -63
- package/context-degrade.mjs +4 -4
- package/context-push.mjs +20 -9
- package/context.mjs +65 -20
- package/doc-graph/anchors.mjs +39 -40
- package/doc-graph/clusters.mjs +49 -0
- package/doc-graph/code-parsers.mjs +494 -0
- package/doc-graph/context-pack.mjs +392 -0
- package/doc-graph/dir-signals.mjs +228 -0
- package/doc-graph/edges.mjs +231 -0
- package/doc-graph/format.mjs +70 -0
- package/doc-graph/gc-cmd.mjs +104 -0
- package/doc-graph/gc.mjs +112 -0
- package/doc-graph/glob-work-graph.mjs +307 -0
- package/doc-graph/golden-set.mjs +131 -0
- package/doc-graph/ls-work-graph.mjs +534 -0
- package/doc-graph/outline.mjs +377 -0
- package/doc-graph/output-annotations.mjs +85 -0
- package/doc-graph/process-docs.mjs +68 -0
- package/doc-graph/read-work-graph.mjs +91 -0
- package/doc-graph/rebuild-cmd.mjs +75 -0
- package/doc-graph/resolvers.mjs +322 -0
- package/doc-graph/roots.mjs +86 -0
- package/doc-graph/schema.mjs +110 -0
- package/doc-graph/search-augment.mjs +418 -0
- package/doc-graph/store.mjs +231 -0
- package/doc-graph/target-resolve.mjs +352 -0
- package/doc-graph/walk.mjs +87 -0
- package/doc-graph.mjs +583 -707
- package/explore-distill.mjs +52 -17
- package/generate-title.mjs +7 -4
- package/ledger-close.mjs +6 -6
- package/ledger-cmd.mjs +3 -3
- package/ledger-consistency.mjs +8 -6
- package/ledger-db.mjs +45 -10
- package/ledger-migrate.mjs +25 -10
- package/ledger-read.mjs +1 -1
- package/ledger-write-ops.mjs +6 -6
- package/ledger.mjs +2 -2
- package/model-context.mjs +54 -18
- package/model-specs.mjs +86 -67
- package/package.json +1 -1
- package/prompt-overlays.mjs +3 -3
- package/prompts/advisor-round1.md +6 -5
- package/prompts/common.md +16 -6
- package/prompts/consult-base.md +2 -1
- package/prompts/discipline-engineering.md +1 -1
- package/prompts/discipline-normal.md +12 -8
- package/prompts/knowledge/doc-lookup.md +2 -2
- package/prompts/knowledge/normal-batch-discipline.md +2 -2
- package/prompts/knowledge/normal-delivery-collaboration.md +3 -3
- package/prompts/knowledge/tool-advisor.md +2 -2
- package/prompts/knowledge/tool-apply-patch.md +1 -1
- package/prompts/knowledge/tool-bash.md +2 -2
- package/prompts/knowledge/tool-batch.md +4 -4
- package/prompts/knowledge/tool-glob.md +9 -2
- package/prompts/knowledge/tool-goal.md +9 -6
- package/prompts/knowledge/tool-grep.md +7 -2
- package/prompts/knowledge/tool-ls.md +11 -5
- package/prompts/knowledge/tool-lsp.md +1 -1
- package/prompts/knowledge/tool-operations.md +3 -2
- package/prompts/knowledge/tool-plan.md +1 -1
- package/prompts/knowledge/tool-read.md +7 -6
- package/prompts/knowledge/tool-repo-graph-read.md +12 -0
- package/prompts/knowledge/tool-repo-graph.md +12 -0
- package/prompts/knowledge/tool-step.md +1 -1
- package/prompts/knowledge/tool-subagent.md +2 -2
- package/prompts/knowledge/tool-task.md +10 -9
- package/prompts/knowledge/tool-tree.md +7 -1
- package/prompts/persona-eng-coder.md +1 -1
- package/prompts/persona-eng-designer.md +1 -1
- package/prompts/persona-engineering.md +2 -1
- package/prompts/persona-explore.md +1 -1
- package/prompts/persona-normal.md +7 -6
- package/prompts/persona-plan.md +2 -2
- package/prompts/planning-next-moves.md +7 -8
- package/provider/anthropic.mjs +53 -51
- package/provider/core.mjs +85 -34
- package/provider/errors.mjs +33 -10
- package/provider/google.mjs +244 -37
- package/provider/list-models.mjs +28 -14
- package/provider/responses-request.mjs +15 -9
- package/provider/responses.mjs +10 -8
- package/provider/sse.mjs +3 -6
- package/proxy-transport.mjs +228 -119
- package/rules-file.mjs +79 -0
- package/rules.mjs +13 -51
- package/session-lifecycle.mjs +32 -20
- package/team-ledger.mjs +7 -7
- package/think-off.mjs +37 -18
- package/token-window.mjs +48 -31
- package/tool-args.mjs +8 -8
- package/tool-docs/bash.md +1 -1
- package/tool-docs/glob.md +3 -1
- package/tool-docs/goal.md +2 -2
- package/tool-docs/grep.md +3 -1
- package/tool-docs/ls.md +7 -1
- package/tool-docs/read.md +1 -1
- package/tool-docs/repo_graph-read.md +3 -0
- package/tool-docs/repo_graph.md +3 -0
- package/tool-docs/tree.md +1 -1
- package/tools/bash.mjs +53 -8
- package/tools/browser.mjs +4 -4
- package/tools/file.mjs +34 -7
- package/tools/glob-dialect.mjs +57 -7
- package/tools/index.mjs +3 -7
- package/tools/patch.mjs +8 -7
- package/tools/process-doc-mark.mjs +13 -0
- package/tools/search.mjs +152 -37
- package/tools/shared.mjs +6 -0
- package/tools/tree.mjs +211 -8
- package/window-policy.mjs +205 -126
- package/prompts/knowledge/tool-doc-graph-read.md +0 -20
- package/prompts/knowledge/tool-doc-graph.md +0 -49
- package/prompts/knowledge/tool-peek.md +0 -10
- package/prompts/knowledge/tool-repo-outline.md +0 -4
- package/tool-docs/doc_graph-read.md +0 -3
- package/tool-docs/doc_graph.md +0 -3
- package/tool-docs/peek.md +0 -3
- package/tool-docs/repo_outline.md +0 -3
- package/tools/peek.mjs +0 -447
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* doc-graph/code-parsers.mjs — 多语言轻量静态解析器(零第三方依赖)。
|
|
3
|
+
*
|
|
4
|
+
* 首期覆盖:JS/TS (ESM + CJS + 动态 import)、Python、C# (.NET)、Go、Rust、SQL(MySQL / T-SQL / PostgreSQL 方言共性),
|
|
5
|
+
* 以及 Markdown 文档。只做确定性静态语法提取(import / export / namespace / 文档路径与符号候选),
|
|
6
|
+
* 不做 AST 重量依赖、不做语义猜测。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const CODE_EXT_LANG = Object.freeze({
|
|
10
|
+
".mjs": "js", ".cjs": "js", ".js": "js", ".jsx": "js",
|
|
11
|
+
".ts": "ts", ".tsx": "ts",
|
|
12
|
+
".py": "py",
|
|
13
|
+
".cs": "cs",
|
|
14
|
+
".go": "go",
|
|
15
|
+
".rs": "rs",
|
|
16
|
+
".sql": "sql",
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export const CODE_EXTENSIONS = new Set(Object.keys(CODE_EXT_LANG))
|
|
20
|
+
export const DOC_EXTENSIONS = new Set([".md"])
|
|
21
|
+
|
|
22
|
+
/** 识别源码语言;非支持扩展返回 null。 */
|
|
23
|
+
export function detectLang(relPath) {
|
|
24
|
+
const s = String(relPath ?? "").toLowerCase()
|
|
25
|
+
const dot = s.lastIndexOf(".")
|
|
26
|
+
if (dot < 0) return null
|
|
27
|
+
return CODE_EXT_LANG[s.slice(dot)] ?? null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 判定路径是否属于测试文件(跨语言常见命名与目录约定)。 */
|
|
31
|
+
export function isTestFilePath(relPath) {
|
|
32
|
+
const p = String(relPath ?? "").replace(/\\/g, "/").toLowerCase()
|
|
33
|
+
if (/(?:^|\/)(?:test|tests|__tests__|spec|specs)\//.test(p)) return true
|
|
34
|
+
if (/(?:\.test|\.spec|_test)\.[a-z0-9]+$/.test(p)) return true
|
|
35
|
+
if (/(?:^|\/)test_[^/]+\.py$/.test(p)) return true
|
|
36
|
+
if (/tests?\.cs$/.test(p)) return true
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 轻量去注释(保留引号内字符串与换行,保持行号不变)。 */
|
|
41
|
+
function stripCStyleComments(text) {
|
|
42
|
+
return String(text ?? "").replace(
|
|
43
|
+
/("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`)|\/\*[\s\S]*?\*\/|\/\/[^\n]*/g,
|
|
44
|
+
(m, str) => (str ? str : m.replace(/[^\n]/g, " ")),
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function lineNumberAt(text, idx) {
|
|
49
|
+
let line = 1
|
|
50
|
+
for (let i = 0; i < idx && i < text.length; i++) {
|
|
51
|
+
if (text.charCodeAt(i) === 10) line++
|
|
52
|
+
}
|
|
53
|
+
return line
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function extractJsImportSymbols(clause) {
|
|
57
|
+
const out = []
|
|
58
|
+
let rest = String(clause ?? "").replace(/^\s*(?:import|export)\s+(?:type\s+)?/, "")
|
|
59
|
+
const brace = /\{([\s\S]*?)\}/.exec(rest)
|
|
60
|
+
if (brace) {
|
|
61
|
+
for (const part of brace[1].split(",")) {
|
|
62
|
+
const clean = part.replace(/^\s*type\s+/, "").trim()
|
|
63
|
+
if (!clean) continue
|
|
64
|
+
const name = clean.split(/\s+as\s+/)[0].trim()
|
|
65
|
+
if (/^[A-Za-z_$][\w$]*$/.test(name)) out.push(name)
|
|
66
|
+
}
|
|
67
|
+
rest = rest.replace(brace[0], "")
|
|
68
|
+
}
|
|
69
|
+
const ns = /\*\s+as\s+([A-Za-z_$][\w$]*)/.exec(rest)
|
|
70
|
+
if (ns) out.push(ns[1])
|
|
71
|
+
else {
|
|
72
|
+
const def = /^\s*([A-Za-z_$][\w$]*)\s*,?\s*$/.exec(rest)
|
|
73
|
+
if (def && def[1] !== "from") out.push(def[1])
|
|
74
|
+
}
|
|
75
|
+
return [...new Set(out)]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 设计档指路:源码头注 / 行注释里写出的 `docs/.../*.md` 路径,抽成本档 docCites 面(P0-2 落点)。
|
|
79
|
+
const DOC_CITE_PATH_RE = /docs\/[A-Za-z0-9_.\-/]+\.md/g
|
|
80
|
+
|
|
81
|
+
/** 从原始 `text`(未去注释)抽块注释与行注释里的 `docs/*.md` token。 */
|
|
82
|
+
function extractJsDocCites(text) {
|
|
83
|
+
const src = String(text ?? "")
|
|
84
|
+
const out = []
|
|
85
|
+
const seen = new Set()
|
|
86
|
+
const push = (raw, line) => {
|
|
87
|
+
if (!raw) return
|
|
88
|
+
if (raw.includes("...")) return // 示意性路径(`docs/.../design/foo.md`):盘上不存在,落了也是悬空
|
|
89
|
+
if (/^https?:\/\//i.test(raw)) return // 绝对 URL
|
|
90
|
+
// 大小写保留:`store` 的断链探针要拿它做 `existsSync`,小写化会在大小写敏感文件系统上
|
|
91
|
+
// 把「盘上存在、只是被挡在图外」的档误报成断链(`doc-path` 面同样保留原文)。
|
|
92
|
+
const norm = raw.replace(/^\.\//, "")
|
|
93
|
+
const key = `${norm.toLowerCase()}\0${line}` // 去重仍按忽略大小写的形
|
|
94
|
+
if (seen.has(key)) return
|
|
95
|
+
seen.add(key)
|
|
96
|
+
out.push({ raw: norm, line })
|
|
97
|
+
}
|
|
98
|
+
let m
|
|
99
|
+
// 块注释(含头注 `/** ... */`):设计档指路的常见位置。
|
|
100
|
+
const blockRe = /\/\*\*?[\s\S]*?\*\//g
|
|
101
|
+
while ((m = blockRe.exec(src)) !== null) {
|
|
102
|
+
const blockStart = lineNumberAt(src, m.index)
|
|
103
|
+
const inner = m[0]
|
|
104
|
+
DOC_CITE_PATH_RE.lastIndex = 0
|
|
105
|
+
let lm
|
|
106
|
+
while ((lm = DOC_CITE_PATH_RE.exec(inner)) !== null) {
|
|
107
|
+
const before = inner.slice(0, lm.index)
|
|
108
|
+
const offsetLines = (before.match(/\n/g) ?? []).length
|
|
109
|
+
push(lm[0], blockStart + offsetLines)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// 行注释:`// ... docs/...md`(与块注释同形取全部 token —— 一行写两条指路时不得只取第一条,
|
|
113
|
+
// 否则第二条永远不会成为 `code-cites` 边,断链面也跟着看不到它)
|
|
114
|
+
const lineRe = /\/\/[^\n]*/g
|
|
115
|
+
while ((m = lineRe.exec(src)) !== null) {
|
|
116
|
+
const line = lineNumberAt(src, m.index)
|
|
117
|
+
DOC_CITE_PATH_RE.lastIndex = 0
|
|
118
|
+
let lm
|
|
119
|
+
while ((lm = DOC_CITE_PATH_RE.exec(m[0])) !== null) push(lm[0], line)
|
|
120
|
+
}
|
|
121
|
+
return out
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 解析 JS / TS (ESM / CJS / 动态 import)。 */
|
|
125
|
+
export function parseJsTs(text) {
|
|
126
|
+
const clean = stripCStyleComments(text)
|
|
127
|
+
const imports = []
|
|
128
|
+
const seenImp = new Map()
|
|
129
|
+
const addImp = (raw, symbols, line) => {
|
|
130
|
+
if (!raw || raw.startsWith("node:")) return
|
|
131
|
+
const key = raw
|
|
132
|
+
const existing = seenImp.get(key)
|
|
133
|
+
if (existing) {
|
|
134
|
+
for (const s of symbols) if (!existing.symbols.includes(s)) existing.symbols.push(s)
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
const entry = { raw, symbols: [...symbols], line }
|
|
138
|
+
seenImp.set(key, entry)
|
|
139
|
+
imports.push(entry)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// import ... from '...' / export ... from '...'
|
|
143
|
+
const esmRe = /\b(?:import|export)\s+([^;]*?)\s+from\s*["']([^"']+)["']/g
|
|
144
|
+
let m
|
|
145
|
+
while ((m = esmRe.exec(clean)) !== null) {
|
|
146
|
+
addImp(m[2], extractJsImportSymbols(m[1]), lineNumberAt(clean, m.index))
|
|
147
|
+
}
|
|
148
|
+
// side-effect import '...'
|
|
149
|
+
const sideRe = /\bimport\s*["']([^"']+)["']/g
|
|
150
|
+
while ((m = sideRe.exec(clean)) !== null) {
|
|
151
|
+
addImp(m[1], [], lineNumberAt(clean, m.index))
|
|
152
|
+
}
|
|
153
|
+
// dynamic import('...')
|
|
154
|
+
const dynRe = /\bimport\s*\(\s*["']([^"']+)["']\s*\)/g
|
|
155
|
+
while ((m = dynRe.exec(clean)) !== null) {
|
|
156
|
+
addImp(m[1], [], lineNumberAt(clean, m.index))
|
|
157
|
+
}
|
|
158
|
+
// CommonJS require('...')
|
|
159
|
+
const reqRe = /\brequire\s*\(\s*["']([^"']+)["']\s*\)/g
|
|
160
|
+
while ((m = reqRe.exec(clean)) !== null) {
|
|
161
|
+
addImp(m[1], [], lineNumberAt(clean, m.index))
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const exports = new Set()
|
|
165
|
+
const namedRe = /\bexport\s+(?:default\s+)?(?:async\s+)?(?:function\*?\s+|class\s+|(?:const|let|var)\s+|interface\s+|type\s+|enum\s+)([A-Za-z_$][\w$]*)/g
|
|
166
|
+
while ((m = namedRe.exec(clean)) !== null) exports.add(m[1])
|
|
167
|
+
|
|
168
|
+
const braceExpRe = /\bexport\s+(?:type\s+)?\{([^}]+)\}/g
|
|
169
|
+
while ((m = braceExpRe.exec(clean)) !== null) {
|
|
170
|
+
for (const part of m[1].split(",")) {
|
|
171
|
+
const tokens = part.replace(/^\s*type\s+/, "").trim().split(/\s+as\s+/)
|
|
172
|
+
const exported = (tokens[1] ?? tokens[0] ?? "").trim()
|
|
173
|
+
if (/^[A-Za-z_$][\w$]*$/.test(exported) && exported !== "default") exports.add(exported)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const destructRe = /\bexport\s+(?:const|let|var)\s*\{([^}]+)\}\s*=/g
|
|
177
|
+
while ((m = destructRe.exec(clean)) !== null) {
|
|
178
|
+
for (const part of m[1].split(",")) {
|
|
179
|
+
const name = part.split(":")[0].trim()
|
|
180
|
+
if (/^[A-Za-z_$][\w$]*$/.test(name)) exports.add(name)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const cjsNamedRe = /\b(?:module\.)?exports\.([A-Za-z_$][\w$]*)\s*=/g
|
|
184
|
+
while ((m = cjsNamedRe.exec(clean)) !== null) exports.add(m[1])
|
|
185
|
+
|
|
186
|
+
return { imports, exports: [...exports], namespace: null, refs: [], docCites: extractJsDocCites(text) }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** 解析 Python。 */
|
|
190
|
+
export function parsePython(text) {
|
|
191
|
+
const lines = String(text ?? "").split("\n")
|
|
192
|
+
const imports = []
|
|
193
|
+
const exports = new Set()
|
|
194
|
+
for (let i = 0; i < lines.length; i++) {
|
|
195
|
+
const line = lines[i]
|
|
196
|
+
const trimmed = line.trim()
|
|
197
|
+
if (!trimmed || trimmed.startsWith("#")) continue
|
|
198
|
+
const fromM = /^from\s+(\.+[\w.]*|[\w.]+)\s+import\s+(.+)/.exec(trimmed)
|
|
199
|
+
if (fromM) {
|
|
200
|
+
const mod = fromM[1]
|
|
201
|
+
const syms = fromM[2].replace(/[()#].*$/, "").split(",")
|
|
202
|
+
.map((s) => s.trim().split(/\s+as\s+/)[0].trim())
|
|
203
|
+
.filter((s) => /^[A-Za-z_]\w*$/.test(s))
|
|
204
|
+
imports.push({ raw: mod, symbols: syms, line: i + 1 })
|
|
205
|
+
continue
|
|
206
|
+
}
|
|
207
|
+
const impM = /^import\s+(.+)/.exec(trimmed)
|
|
208
|
+
if (impM) {
|
|
209
|
+
for (const part of impM[1].replace(/#.*$/, "").split(",")) {
|
|
210
|
+
const mod = part.trim().split(/\s+as\s+/)[0].trim()
|
|
211
|
+
if (mod) imports.push({ raw: mod, symbols: [], line: i + 1 })
|
|
212
|
+
}
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
// 仅顶层定义作为导出(无缩进)
|
|
216
|
+
const defM = /^(?:async\s+)?(?:def|class)\s+([A-Za-z_]\w*)/.exec(line)
|
|
217
|
+
if (defM && !defM[1].startsWith("_")) exports.add(defM[1])
|
|
218
|
+
}
|
|
219
|
+
return { imports, exports: [...exports], namespace: null, refs: [] }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** 解析 C# (.NET)。 */
|
|
223
|
+
export function parseCSharp(text) {
|
|
224
|
+
const clean = stripCStyleComments(text)
|
|
225
|
+
const imports = []
|
|
226
|
+
const exports = new Set()
|
|
227
|
+
let namespace = null
|
|
228
|
+
|
|
229
|
+
const nsM = /\bnamespace\s+([A-Za-z_][\w.]*)/.exec(clean)
|
|
230
|
+
if (nsM) namespace = nsM[1]
|
|
231
|
+
|
|
232
|
+
const usingRe = /(?:^|\n)\s*(global\s+)?using\s+(?:static\s+)?(?:[A-Za-z_]\w*\s*=\s*)?([A-Za-z_][\w.]*)\s*;/g
|
|
233
|
+
let m
|
|
234
|
+
while ((m = usingRe.exec(clean)) !== null) {
|
|
235
|
+
const isGlobal = Boolean(m[1])
|
|
236
|
+
const ns = m[2]
|
|
237
|
+
if (/^(?:System|Microsoft|Windows)(?:\.|$)/.test(ns)) continue
|
|
238
|
+
imports.push({ raw: ns, symbols: [], line: lineNumberAt(clean, m.index), global: isGlobal })
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const typeRe = /\b(?:public|internal|protected)\s+(?:(?:static|abstract|sealed|partial|readonly|ref)\s+)*(?:class|struct|record(?:\s+(?:class|struct))?|interface|enum)\s+([A-Za-z_]\w*)/g
|
|
242
|
+
while ((m = typeRe.exec(clean)) !== null) exports.add(m[1])
|
|
243
|
+
|
|
244
|
+
const delegateRe = /\b(?:public|internal|protected)\s+(?:(?:new|unsafe)\s+)*delegate\s+[\w.<>,\[\]\s?]+\s+([A-Za-z_]\w*)\s*(?:<[^>]+>)?\s*\(/g
|
|
245
|
+
while ((m = delegateRe.exec(clean)) !== null) exports.add(m[1])
|
|
246
|
+
|
|
247
|
+
// 提取代码中引用的 PascalCase 类型标识符,用于从 using namespace 精准匹配真正用到的 .cs 文件
|
|
248
|
+
const refs = new Set()
|
|
249
|
+
const identRe = /\b([A-Z][A-Za-z0-9_]{2,})\b/g
|
|
250
|
+
while ((m = identRe.exec(clean)) !== null) {
|
|
251
|
+
if (!exports.has(m[1])) refs.add(m[1])
|
|
252
|
+
}
|
|
253
|
+
return { imports, exports: [...exports], namespace, refs: [...refs] }
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** 解析 Go。 */
|
|
257
|
+
export function parseGo(text) {
|
|
258
|
+
const clean = stripCStyleComments(text)
|
|
259
|
+
const imports = []
|
|
260
|
+
const exports = new Set()
|
|
261
|
+
let namespace = null
|
|
262
|
+
|
|
263
|
+
const pkgM = /(?:^|\n)\s*package\s+([A-Za-z_]\w*)/.exec(clean)
|
|
264
|
+
if (pkgM) namespace = pkgM[1]
|
|
265
|
+
|
|
266
|
+
// 单行 import "pkg"
|
|
267
|
+
const singleRe = /(?:^|\n)\s*import\s+(?:[A-Za-z_.]\w*\s+)?"([^"]+)"/g
|
|
268
|
+
let m
|
|
269
|
+
while ((m = singleRe.exec(clean)) !== null) {
|
|
270
|
+
imports.push({ raw: m[1], symbols: [], line: lineNumberAt(clean, m.index) })
|
|
271
|
+
}
|
|
272
|
+
// 块 import ( ... )
|
|
273
|
+
const blockRe = /\bimport\s*\(([\s\S]*?)\)/g
|
|
274
|
+
while ((m = blockRe.exec(clean)) !== null) {
|
|
275
|
+
const baseIdx = m.index
|
|
276
|
+
const inner = m[1]
|
|
277
|
+
const itemRe = /"([^"]+)"/g
|
|
278
|
+
let im
|
|
279
|
+
while ((im = itemRe.exec(inner)) !== null) {
|
|
280
|
+
imports.push({ raw: im[1], symbols: [], line: lineNumberAt(clean, baseIdx + im.index) })
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// 首字母大写即 Go 导出符号
|
|
285
|
+
const expRe = /(?:^|\n)\s*(?:func\s+(?:\([^)]*\)\s*)?|type\s+|const\s+|var\s+)([A-Z][A-Za-z0-9_]*)\b/g
|
|
286
|
+
while ((m = expRe.exec(clean)) !== null) exports.add(m[1])
|
|
287
|
+
|
|
288
|
+
return { imports, exports: [...exports], namespace, refs: [] }
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** 解析 Rust。 */
|
|
292
|
+
export function parseRust(text) {
|
|
293
|
+
const clean = stripCStyleComments(text)
|
|
294
|
+
const imports = []
|
|
295
|
+
const exports = new Set()
|
|
296
|
+
|
|
297
|
+
// mod foo;(声明外部子模块文件)
|
|
298
|
+
const modRe = /(?:^|\n)\s*(?:pub(?:\([^)]*\))?\s+)?mod\s+([A-Za-z_]\w*)\s*;/g
|
|
299
|
+
let m
|
|
300
|
+
while ((m = modRe.exec(clean)) !== null) {
|
|
301
|
+
imports.push({ raw: `mod:${m[1]}`, symbols: [m[1]], line: lineNumberAt(clean, m.index) })
|
|
302
|
+
}
|
|
303
|
+
// use crate::... / use super::... / use self::...
|
|
304
|
+
const useRe = /(?:^|\n)\s*(?:pub(?:\([^)]*\))?\s+)?use\s+((?:crate|super|self)::[^;]+);/g
|
|
305
|
+
while ((m = useRe.exec(clean)) !== null) {
|
|
306
|
+
imports.push({ raw: m[1].trim(), symbols: [], line: lineNumberAt(clean, m.index) })
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const expRe = /\bpub(?:\([^)]*\))?\s+(?:async\s+)?(?:unsafe\s+)?(?:fn|struct|enum|trait|type|const|static|mod)\s+([A-Za-z_]\w*)/g
|
|
310
|
+
while ((m = expRe.exec(clean)) !== null) exports.add(m[1])
|
|
311
|
+
|
|
312
|
+
return { imports, exports: [...exports], namespace: null, refs: [] }
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** 解析 SQL(MySQL / T-SQL / PostgreSQL 共性子集)。
|
|
316
|
+
*
|
|
317
|
+
* SQL 没有 import:它的依赖关系是 **对象名引用** —— 过程体里 `CALL p_x`、`DELETE FROM t_y`、
|
|
318
|
+
* 建表语句里 `REFERENCES t_z`。这与 Go 的 `package` / C# 的 `namespace` 同构,所以复用同一套落边机制:
|
|
319
|
+
* `namespace` = 本文件声明对象的 schema,`exports` = 可执行对象名
|
|
320
|
+
* (PROCEDURE / FUNCTION / TRIGGER / VIEW / EVENT),`imports` = 语句中引用的对象。
|
|
321
|
+
* 表名不进 exports —— 表是被引用的容器,它的定义文件靠**文件名**在 edges 里回查(`tables/.../cfg_case_field_acl.sql`)。
|
|
322
|
+
*
|
|
323
|
+
* 引用有两种形态,都抓:
|
|
324
|
+
* - **两段式** `schema.object`:自带 schema,候选面按 schema 限定;
|
|
325
|
+
* - **裸名** `CALL p_x` / `FROM t_y`:MySQL 里裸名就指**同一个库**,没有歧义可猜——只在同一库目录内落边。
|
|
326
|
+
* 裸名带 `detail: "call" | "table"`,因为 SQL 里可执行对象与表是两个命名空间
|
|
327
|
+
* (`bms` 库里 `sns_post` 既是函数又是表,丢了这一位就只能两边都漏或两边都连)。
|
|
328
|
+
*/
|
|
329
|
+
export function parseSql(text) {
|
|
330
|
+
// `-- line` 与 `/* block */`;保留换行以稳住行号。字符串字面量不动(COMMENT 里可能带 `--`)。
|
|
331
|
+
const clean = stripSqlComments(text)
|
|
332
|
+
const exports = new Set()
|
|
333
|
+
const tables = new Set()
|
|
334
|
+
// 视图进 exports(可被 `FROM` 引用),但它不是可执行对象 —— 裸 `CALL` 不该指到它。
|
|
335
|
+
const views = new Set()
|
|
336
|
+
let namespace = null
|
|
337
|
+
let m
|
|
338
|
+
// CREATE [OR REPLACE|DEFINER=...|ALGORITHM=...] {PROCEDURE|FUNCTION|TRIGGER|VIEW|EVENT} [IF NOT EXISTS] `schema`.`name`
|
|
339
|
+
// Identifier forms: bare word, MySQL backticks, T-SQL brackets, ANSI double quotes.
|
|
340
|
+
const IDENT = "(?:[\\w$]+|`[^`]+`|\\[[^\\]]+\\]|\"[^\"]+\")"
|
|
341
|
+
const createRe = new RegExp(
|
|
342
|
+
`\\bCREATE\\s+(?:OR\\s+REPLACE\\s+|DEFINER\\s*=\\s*\\S+\\s+|ALGORITHM\\s*=\\s*\\w+\\s+|SQL\\s+SECURITY\\s+\\w+\\s+|UNIQUE\\s+|TEMPORARY\\s+)*(PROCEDURE|FUNCTION|TRIGGER|VIEW|EVENT)\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?(${IDENT}\\s*\\.\\s*)?(${IDENT})`, "gi")
|
|
343
|
+
while ((m = createRe.exec(clean)) !== null) {
|
|
344
|
+
const schema = m[2] ? unquoteSqlIdent(m[2].replace(/\s*\.\s*$/, "")) : null
|
|
345
|
+
const name = unquoteSqlIdent(m[3])
|
|
346
|
+
if (schema && !namespace) namespace = schema
|
|
347
|
+
exports.add(name)
|
|
348
|
+
if (/^view$/i.test(m[1])) views.add(name)
|
|
349
|
+
}
|
|
350
|
+
// CREATE TABLE [IF NOT EXISTS] `schema`.`name` —— 表:只记名字,不进 exports
|
|
351
|
+
const tableRe = new RegExp(
|
|
352
|
+
`\\bCREATE\\s+(?:TEMPORARY\\s+)?TABLE\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?(${IDENT}\\s*\\.\\s*)?(${IDENT})`, "gi")
|
|
353
|
+
while ((m = tableRe.exec(clean)) !== null) {
|
|
354
|
+
const schema = m[1] ? unquoteSqlIdent(m[1].replace(/\s*\.\s*$/, "")) : null
|
|
355
|
+
if (schema && !namespace) namespace = schema
|
|
356
|
+
tables.add(unquoteSqlIdent(m[2]))
|
|
357
|
+
}
|
|
358
|
+
// 引用扫描屏蔽字符串字面量;双引号与反引号保留,因为它们在受支持方言里可包裹标识符。
|
|
359
|
+
const refClean = clean.replace(/'(?:''|[^'])*'/g, (literal) => literal.replace(/[^\n]/g, " "))
|
|
360
|
+
// 引用面一:带 schema 的两段式 `a.b`。
|
|
361
|
+
const imports = []
|
|
362
|
+
const seen = new Set()
|
|
363
|
+
const refRe = new RegExp(
|
|
364
|
+
`\\b(?:CALL|EXEC(?:UTE)?|FROM|JOIN|INTO|UPDATE|REFERENCES|TRUNCATE(?:\\s+TABLE)?)\\s+(${IDENT}\\s*\\.\\s*${IDENT})`, "gi")
|
|
365
|
+
while ((m = refRe.exec(refClean)) !== null) {
|
|
366
|
+
const raw = m[1].replace(/\s+/g, "").replace(/[`"]/g, "").replace(/\[|\]/g, "")
|
|
367
|
+
if (seen.has(raw)) continue
|
|
368
|
+
seen.add(raw)
|
|
369
|
+
imports.push({ raw, symbols: [], line: lineNumberAt(refClean, m.index) })
|
|
370
|
+
}
|
|
371
|
+
// 引用面二:裸名(无 schema 前缀)。`INTO` 在这一面只接 `INSERT INTO` / `REPLACE INTO`:
|
|
372
|
+
// `SELECT … INTO var` 的 var 是变量,拿它当表名只会给每个过程造一条不存在的引用。
|
|
373
|
+
const bareRe = new RegExp(
|
|
374
|
+
`\\b(?:CALL|EXEC(?:UTE)?|FROM|JOIN|INSERT\\s+INTO|REPLACE\\s+INTO|UPDATE|REFERENCES|TRUNCATE(?:\\s+TABLE)?)\\s+(?!${IDENT}\\s*\\.)(${IDENT})`, "gi")
|
|
375
|
+
const seenBare = new Set()
|
|
376
|
+
while ((m = bareRe.exec(refClean)) !== null) {
|
|
377
|
+
const name = unquoteSqlIdent(m[1])
|
|
378
|
+
if (!name || /^(?:dual)$/i.test(name)) continue
|
|
379
|
+
const detail = /^(?:CALL|EXEC)/i.test(m[0].trim()) ? "call" : "table"
|
|
380
|
+
// 名字后面紧跟 `(` 的是**表函数调用**(`FROM JSON_TABLE(...)`),不是表名:表引用永远不带括号。
|
|
381
|
+
// 两个例外不能挡:`CALL p_x(...)` 正是带括号的那一种(已在 `detail === "table"` 里排除),
|
|
382
|
+
// `INSERT INTO t (col, …)` 的列清单也紧跟在表名后 —— 拿括号一刀切会削掉确实存在的插入引用。
|
|
383
|
+
if (detail === "table" && !/^(?:INSERT|REPLACE)\b/i.test(m[0].trim())
|
|
384
|
+
&& /^\s*\(/.test(refClean.slice(m.index + m[0].length))) continue
|
|
385
|
+
const key = `${detail}:${name.toLowerCase()}`
|
|
386
|
+
if (seenBare.has(key)) continue
|
|
387
|
+
seenBare.add(key)
|
|
388
|
+
imports.push({ raw: name, symbols: [], detail, line: lineNumberAt(refClean, m.index) })
|
|
389
|
+
}
|
|
390
|
+
return { imports, exports: [...exports], namespace, refs: [], tables: [...tables], views: [...views] }
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** 去 SQL 注释;字符串字面量原样保留。行号与原文一致(块注释内的换行保留)。 */
|
|
394
|
+
function stripSqlComments(text) {
|
|
395
|
+
return String(text ?? "").replace(
|
|
396
|
+
/('(?:''|[^'])*'|"(?:""|[^"])*"|`(?:``|[^`])*`)|\/\*[\s\S]*?\*\/|--[^\n]*|#[^\n]*/g,
|
|
397
|
+
(match, str) => (str ? str : match.replace(/[^\n]/g, " ")),
|
|
398
|
+
)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function unquoteSqlIdent(raw) {
|
|
402
|
+
const s = String(raw ?? "").trim()
|
|
403
|
+
if (s.length >= 2 && s.startsWith("`") && s.endsWith("`")) return s.slice(1, -1)
|
|
404
|
+
if (s.length >= 2 && ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("[") && s.endsWith("]")))) return s.slice(1, -1)
|
|
405
|
+
return s.replace(/^\[|\]$/g, "")
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** 按语言分派解析源码文件。 */
|
|
409
|
+
export function parseCodeFile(lang, text) {
|
|
410
|
+
switch (lang) {
|
|
411
|
+
case "js":
|
|
412
|
+
case "ts": return parseJsTs(text)
|
|
413
|
+
case "py": return parsePython(text)
|
|
414
|
+
case "cs": return parseCSharp(text)
|
|
415
|
+
case "go": return parseGo(text)
|
|
416
|
+
case "rs": return parseRust(text)
|
|
417
|
+
case "sql": return parseSql(text)
|
|
418
|
+
default: return { imports: [], exports: [], namespace: null, refs: [] }
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const DOC_PATH_RE = /(?:^|[\s`("'\[\u300c\u300a(【::])([A-Za-z0-9_.\-][A-Za-z0-9_.\-/]*\.(?:mjs|cjs|js|jsx|ts|tsx|py|cs|go|rs|sql|md))(?=$|[\s`)"'\]#§\u300d\u300b)】。,,;;:?])/gu
|
|
423
|
+
const BACKTICK_RE = /`([^`\n]+)`/g
|
|
424
|
+
const IDENT_CANDIDATE_RE = /^[A-Za-z_][A-Za-z0-9_]{2,60}$/
|
|
425
|
+
/** 点分属性路径:`agent.planningNextMoves`(配置键 / 字段路径的常见写法)。 */
|
|
426
|
+
const PROP_PATH_RE = /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)+$/
|
|
427
|
+
const KNOWN_EXT_LOWER = new Set([...CODE_EXTENSIONS, ...DOC_EXTENSIONS].map((e) => e.toLowerCase()))
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* 反引号内的点分属性路径(`agent.planningNextMoves`)——`IDENT_CANDIDATE_RE` 只认单词字符,
|
|
431
|
+
* 带点整段被挡在候选面之外,`repo_graph(context)` 因此查不到 `planningNextMoves` 这类字面量。
|
|
432
|
+
* 末段是已知文件扩展名时**排除**:`planning-step.mjs` 是档 token,归 `doc-path` 面;
|
|
433
|
+
* 重复收进候选会让查询面用候选命中覆盖既有 suffix 语义。
|
|
434
|
+
*/
|
|
435
|
+
function isPropPathToken(raw) {
|
|
436
|
+
if (raw.length > 80 || !PROP_PATH_RE.test(raw)) return false
|
|
437
|
+
const last = raw.slice(raw.lastIndexOf(".") + 1)
|
|
438
|
+
return !KNOWN_EXT_LOWER.has(`.${last.toLowerCase()}`)
|
|
439
|
+
}
|
|
440
|
+
const MD_HEADING_RE = /^\s*(#{1,4})\s+(.+?)\s*#*\s*$/
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* 解析 Markdown 文档:
|
|
444
|
+
* - pathRefs:文档中明确提到的源码/文档路径 token(含行号)
|
|
445
|
+
* - symbolCandidates:反引号内合法标识符候选与点分属性路径候选(最终仅与仓内真实代码导出表求交集后才成边,零噪音;点分路径必然查不到导出表,故只作候选面、不入边)
|
|
446
|
+
* - headings:H1–H4 章节标题(含层级与行号),供 `repo_graph(context)` 支持中文概念与多词主题检索
|
|
447
|
+
*/
|
|
448
|
+
export function parseMarkdownDoc(text) {
|
|
449
|
+
const lines = String(text ?? "").split("\n")
|
|
450
|
+
const pathRefs = []
|
|
451
|
+
const symbolCandidates = []
|
|
452
|
+
const headings = []
|
|
453
|
+
const seenPath = new Set()
|
|
454
|
+
const seenSym = new Set()
|
|
455
|
+
let inFence = false
|
|
456
|
+
|
|
457
|
+
for (let i = 0; i < lines.length; i++) {
|
|
458
|
+
const line = lines[i]
|
|
459
|
+
if (/^\s*(?:```|~~~)/.test(line)) {
|
|
460
|
+
inFence = !inFence
|
|
461
|
+
continue
|
|
462
|
+
}
|
|
463
|
+
if (inFence) continue
|
|
464
|
+
|
|
465
|
+
const hm = MD_HEADING_RE.exec(line)
|
|
466
|
+
if (hm) {
|
|
467
|
+
const title = hm[2].replace(/`+/g, "").trim()
|
|
468
|
+
if (title) headings.push({ title, level: hm[1].length, line: i + 1 })
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
DOC_PATH_RE.lastIndex = 0
|
|
472
|
+
let m
|
|
473
|
+
while ((m = DOC_PATH_RE.exec(line)) !== null) {
|
|
474
|
+
const raw = m[1].replace(/^\.\//, "")
|
|
475
|
+
if (raw.startsWith("http:") || raw.startsWith("https:")) continue
|
|
476
|
+
const key = `${raw}\0${i + 1}`
|
|
477
|
+
if (!seenPath.has(key)) {
|
|
478
|
+
seenPath.add(key)
|
|
479
|
+
pathRefs.push({ raw, line: i + 1 })
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
BACKTICK_RE.lastIndex = 0
|
|
484
|
+
while ((m = BACKTICK_RE.exec(line)) !== null) {
|
|
485
|
+
const raw = m[1].trim()
|
|
486
|
+
// 两支候选:单词形标识符(既有)∥ 点分属性路径(配置键 / 字段路径)。
|
|
487
|
+
if (!IDENT_CANDIDATE_RE.test(raw) && !isPropPathToken(raw)) continue
|
|
488
|
+
if (seenSym.has(raw)) continue
|
|
489
|
+
seenSym.add(raw)
|
|
490
|
+
symbolCandidates.push({ symbol: raw, line: i + 1 })
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return { pathRefs, symbolCandidates, headings }
|
|
494
|
+
}
|