grix-connector 3.17.1 → 3.19.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 +28 -0
- package/dist/adapter/acp/acp-adapter.js +15 -14
- package/dist/adapter/claude/claude-adapter.js +21 -21
- package/dist/adapter/claude/skill-scanner.js +2 -2
- package/dist/adapter/codex/codex-bridge.js +8 -8
- package/dist/adapter/codex/codex-trust.js +4 -4
- package/dist/adapter/codex/config-overrides.js +1 -0
- package/dist/adapter/codex/raw-capture-hook.js +1 -0
- package/dist/adapter/opencode/opencode-adapter.js +3 -3
- package/dist/adapter/opencode/opencode-transport.js +4 -4
- package/dist/adapter/openhuman/openhuman-adapter.js +2 -2
- package/dist/adapter/pi/pi-adapter.js +1 -1
- package/dist/audit/adapters/codex/codex-audit-adapter.js +1 -1
- package/dist/audit/core/audit-options.js +1 -1
- package/dist/audit/core/audit-session-registry.js +3 -3
- package/dist/audit/core/replay-finalizer.js +1 -1
- package/dist/audit/integration/bridge-audit-controller.js +1 -1
- package/dist/audit/query/audit-local-actions.js +1 -1
- package/dist/audit/raw-capture/adapter-hook.js +0 -0
- package/dist/audit/raw-capture/attribution.js +2 -0
- package/dist/audit/raw-capture/body-source.js +1 -0
- package/dist/audit/raw-capture/capture-relay.js +13 -0
- package/dist/audit/raw-capture/redaction.js +1 -0
- package/dist/audit/raw-capture/types.js +1 -0
- package/dist/audit/storage/file-audit-replay-store.js +2 -2
- package/dist/audit/worker/audit-recorder-worker-entry.js +1 -1
- package/dist/bridge/bridge.js +9 -9
- package/dist/bridge/send-controller.js +3 -3
- package/dist/core/aibot/client.js +2 -2
- package/dist/core/aibot/connection-handle.js +1 -1
- package/dist/core/files/file-serve.js +2 -2
- package/dist/core/log/packet-log.js +2 -2
- package/dist/core/mcp/tools.js +1 -1
- package/dist/core/persistence/session-binding-store.js +1 -1
- package/dist/core/persistence/stop-result-outbox.js +2 -0
- package/dist/core/persistence/terminal-commit-token-store.js +2 -0
- package/dist/core/persistence/terminal-outbox.js +2 -0
- package/dist/core/provider-quota/index.js +1 -1
- package/dist/core/provider-quota/kiro.js +1 -1
- package/dist/core/provider-quota/openai-codex.js +2 -0
- package/dist/core/provider-quota/presentation.js +1 -1
- package/dist/core/provider-quota/providers.js +2 -2
- package/dist/core/proxy/relay-state-sync.js +1 -0
- package/dist/default-skills/grix-agent-dispatch/SKILL.md +17 -14
- package/dist/default-skills/grix-audit-data/SKILL.md +4 -2
- package/dist/default-skills/grix-chat-state/SKILL.md +11 -3
- package/dist/default-skills/grix-query/SKILL.md +4 -2
- package/dist/default-skills/index.js +1 -1
- package/dist/grix.js +7 -7
- package/dist/kimi-plugins/grix-layer/SKILL.md +68 -0
- package/dist/kimi-plugins/grix-layer/SYSTEM.md +16 -0
- package/dist/kimi-plugins/grix-layer/kimi.plugin.json +10 -0
- package/dist/kimi-plugins/grix-layer/references/gateway-architecture.md +153 -0
- package/dist/kimi-plugins/grix-layer/scripts/gateway.mjs +31 -0
- package/dist/kimi-plugins/grix-layer/scripts/lib/architecture-map.mjs +305 -0
- package/dist/kimi-plugins/grix-layer/scripts/lib/downstream-client.mjs +133 -0
- package/dist/kimi-plugins/grix-layer/scripts/lib/navigation-service.mjs +762 -0
- package/dist/kimi-plugins/grix-layer/scripts/lib/normalizers.mjs +202 -0
- package/dist/kimi-plugins/grix-layer/scripts/lib/policy.mjs +78 -0
- package/dist/kimi-plugins/grix-layer/scripts/lib/response-budget.mjs +83 -0
- package/dist/kimi-plugins/grix-layer/scripts/lib/tool-server.mjs +106 -0
- package/dist/kimi-plugins/index.js +3 -0
- package/dist/manager-share-config.js +1 -1
- package/dist/manager.js +2 -2
- package/dist/mcp/stream-http/security.js +1 -1
- package/dist/service/service-manager.js +3 -3
- package/openclaw-plugin/index.js +56 -36
- package/openclaw-plugin/skills/grix-query/SKILL.md +6 -2
- package/package.json +8 -3
|
@@ -0,0 +1,762 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
ArchitectureCache,
|
|
5
|
+
architectureSummary,
|
|
6
|
+
buildArchitectureMap,
|
|
7
|
+
classifyProblem,
|
|
8
|
+
fingerprintFor,
|
|
9
|
+
loadProjectOverride,
|
|
10
|
+
} from "./architecture-map.mjs";
|
|
11
|
+
import {
|
|
12
|
+
normalizeArchitecture,
|
|
13
|
+
normalizeCoverage,
|
|
14
|
+
normalizeFolders,
|
|
15
|
+
normalizeIndexStatus,
|
|
16
|
+
normalizeProjects,
|
|
17
|
+
normalizeCodeSearch,
|
|
18
|
+
normalizeSearch,
|
|
19
|
+
normalizeTrace,
|
|
20
|
+
} from "./normalizers.mjs";
|
|
21
|
+
import {
|
|
22
|
+
LIMITS,
|
|
23
|
+
NavigatorError,
|
|
24
|
+
compactText,
|
|
25
|
+
escapeRegex,
|
|
26
|
+
normalizeRepoPath,
|
|
27
|
+
uniqueBy,
|
|
28
|
+
} from "./policy.mjs";
|
|
29
|
+
import { boundResponse } from "./response-budget.mjs";
|
|
30
|
+
|
|
31
|
+
const INBOUND_WORDS = [
|
|
32
|
+
"调用者", "谁调用", "入口", "影响范围", "used by", "caller", "callers", "impact",
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const QUERY_SYNONYMS = [
|
|
36
|
+
["注册", ["register", "registration"]],
|
|
37
|
+
["暴露", ["expose", "allow"]],
|
|
38
|
+
["限制", ["limit", "allow"]],
|
|
39
|
+
["工具", ["tool"]],
|
|
40
|
+
["列表", ["list", "registry"]],
|
|
41
|
+
["调用", ["call"]],
|
|
42
|
+
["请求", ["request"]],
|
|
43
|
+
["路由", ["route"]],
|
|
44
|
+
["配置", ["config"]],
|
|
45
|
+
["索引", ["index"]],
|
|
46
|
+
["解析", ["parse"]],
|
|
47
|
+
["保存", ["save", "persist"]],
|
|
48
|
+
["查询", ["query", "search"]],
|
|
49
|
+
["身份", ["identity"]],
|
|
50
|
+
["注入", ["inject", "injection"]],
|
|
51
|
+
["会话", ["session", "thread"]],
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
const GENERIC_PATH_SEGMENTS = new Set([
|
|
55
|
+
"app", "cmd", "core", "internal", "lib", "packages", "pkg", "src",
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
const LITERAL_STOP_WORDS = new Set([
|
|
59
|
+
"about", "after", "agent", "also", "and", "are", "before", "being", "code",
|
|
60
|
+
"does", "exact", "field", "find", "from", "have", "here", "into", "just", "like", "more", "project",
|
|
61
|
+
"should", "that", "the", "their", "then", "this", "through", "using", "what",
|
|
62
|
+
"when", "where", "which", "with", "would",
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
function caseFlexibleWord(word) {
|
|
66
|
+
const escaped = escapeRegex(word);
|
|
67
|
+
if (!/^[A-Za-z]/.test(word)) return escaped;
|
|
68
|
+
return `[${word[0].toLowerCase()}${word[0].toUpperCase()}]${escaped.slice(1)}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function phraseRegex(words) {
|
|
72
|
+
return words.map(caseFlexibleWord).join("[_ .:/-]*");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function exactLiteralPattern(value) {
|
|
76
|
+
const text = compactText(value, 320);
|
|
77
|
+
if (!text) return "";
|
|
78
|
+
const patterns = [];
|
|
79
|
+
const quoted = [...text.matchAll(/["'`“”‘’]([^"'`“”‘’]{4,80})["'`“”‘’]/g)]
|
|
80
|
+
.map((match) => match[1]);
|
|
81
|
+
const structured = text.match(/[A-Za-z][A-Za-z0-9]*(?:[_./:-][A-Za-z0-9]+)+|[a-z]+[A-Z][A-Za-z0-9]*/g) ?? [];
|
|
82
|
+
for (const literal of [...structured, ...quoted]) {
|
|
83
|
+
const words = literal.match(/[A-Za-z][A-Za-z0-9]*/g) ?? [];
|
|
84
|
+
patterns.push(words.length ? phraseRegex(words) : escapeRegex(literal));
|
|
85
|
+
}
|
|
86
|
+
for (const literal of text.match(/[\p{Script=Han}]{4,40}/gu) ?? []) {
|
|
87
|
+
patterns.push(escapeRegex(literal));
|
|
88
|
+
}
|
|
89
|
+
const words = (text.match(/[A-Za-z][A-Za-z0-9]*/g) ?? [])
|
|
90
|
+
.filter((word) => word.length >= 3 && !LITERAL_STOP_WORDS.has(word.toLowerCase()));
|
|
91
|
+
for (let index = 0; index < words.length - 1; index += 1) {
|
|
92
|
+
patterns.push(phraseRegex(words.slice(index, index + 2)));
|
|
93
|
+
}
|
|
94
|
+
const selected = [];
|
|
95
|
+
for (const pattern of uniqueBy(patterns, (item) => item)) {
|
|
96
|
+
if (selected.length >= 6) break;
|
|
97
|
+
if ([...selected, pattern].join("|").length > 480) continue;
|
|
98
|
+
selected.push(pattern);
|
|
99
|
+
}
|
|
100
|
+
return selected.join("|");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function strongestExactLiteral(value) {
|
|
104
|
+
const text = compactText(value, 320);
|
|
105
|
+
if (!text) return "";
|
|
106
|
+
const ranked = [];
|
|
107
|
+
for (const match of text.matchAll(/["'`“”‘’]([^"'`“”‘’]{4,80})["'`“”‘’]/g)) {
|
|
108
|
+
ranked.push({ value: match[1], score: 1000 + match[1].length });
|
|
109
|
+
}
|
|
110
|
+
for (const literal of text.match(/[A-Za-z][A-Za-z0-9]*(?:[_./:-][A-Za-z0-9]+)+|[a-z]+[A-Z][A-Za-z0-9]*/g) ?? []) {
|
|
111
|
+
const isSymbolLike = /_|[a-z][A-Z]/.test(literal);
|
|
112
|
+
ranked.push({ value: literal, score: (isSymbolLike ? 900 : 500) + literal.length });
|
|
113
|
+
}
|
|
114
|
+
const words = (text.match(/[A-Za-z][A-Za-z0-9]*/g) ?? [])
|
|
115
|
+
.filter((word) => word.length >= 3 && !LITERAL_STOP_WORDS.has(word.toLowerCase()));
|
|
116
|
+
for (let index = 0; index < words.length - 1; index += 1) {
|
|
117
|
+
const valuePair = words.slice(index, index + 2).join(" ");
|
|
118
|
+
ranked.push({ value: valuePair, score: 700 + valuePair.length });
|
|
119
|
+
}
|
|
120
|
+
return ranked
|
|
121
|
+
.sort((a, b) => b.score - a.score || b.value.length - a.value.length)[0]
|
|
122
|
+
?.value ?? "";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function exactHintLayer(request) {
|
|
126
|
+
const fileHint = request.fileHint ? normalizeRepoPath(request.fileHint) : "";
|
|
127
|
+
return {
|
|
128
|
+
id: "exact_hint",
|
|
129
|
+
purpose: "架构不可用或路径已知时的受限精确定位",
|
|
130
|
+
paths: fileHint ? [fileHint] : [],
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function candidateMatchesScope(candidate, layer) {
|
|
135
|
+
const scopes = layer.paths ?? [];
|
|
136
|
+
if (!scopes.length) return true;
|
|
137
|
+
const file = normalizeRepoPath(candidate.file);
|
|
138
|
+
return scopes.some((item) => {
|
|
139
|
+
const scope = normalizeRepoPath(item);
|
|
140
|
+
if (!scope) return false;
|
|
141
|
+
if (layer.id === "exact_hint" && !scope.includes("/")) {
|
|
142
|
+
return file === scope || file.endsWith(`/${scope}`);
|
|
143
|
+
}
|
|
144
|
+
return file === scope || file.startsWith(`${scope}/`);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function candidateSignature(candidates = []) {
|
|
149
|
+
return candidates
|
|
150
|
+
.map((candidate) => `${candidate.file}:${candidate.qualifiedName}`)
|
|
151
|
+
.join("|");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function searchQuery(problem) {
|
|
155
|
+
const expanded = [problem];
|
|
156
|
+
for (const [word, synonyms] of QUERY_SYNONYMS) {
|
|
157
|
+
if (problem.includes(word)) expanded.push(...synonyms);
|
|
158
|
+
}
|
|
159
|
+
return compactText(expanded.join(" "), 180)
|
|
160
|
+
.replace(/[^\p{L}\p{N}_./:\-\s]/gu, " ")
|
|
161
|
+
.replace(/\s+/g, " ")
|
|
162
|
+
.trim();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function scoreCandidate(candidate, { problem, knownSymbol, fileHint, layer }) {
|
|
166
|
+
const text = `${problem} ${knownSymbol}`.toLowerCase();
|
|
167
|
+
const qualified = candidate.qualifiedName.toLowerCase();
|
|
168
|
+
const name = candidate.name.toLowerCase();
|
|
169
|
+
let score = (candidate.score || 0) + (candidate.searchRankBoost || 0);
|
|
170
|
+
const reasons = [];
|
|
171
|
+
if (knownSymbol && qualified.includes(knownSymbol.toLowerCase())) {
|
|
172
|
+
score += 12;
|
|
173
|
+
reasons.push("known-symbol");
|
|
174
|
+
}
|
|
175
|
+
if (fileHint && candidate.file.includes(normalizeRepoPath(fileHint))) {
|
|
176
|
+
score += 10;
|
|
177
|
+
reasons.push("file-hint");
|
|
178
|
+
}
|
|
179
|
+
if ((layer.paths ?? []).some((scope) => candidate.file.startsWith(scope))) {
|
|
180
|
+
score += 6;
|
|
181
|
+
reasons.push(`layer:${layer.id}`);
|
|
182
|
+
}
|
|
183
|
+
for (const segment of candidate.file.toLowerCase().split("/").filter(
|
|
184
|
+
(item) => item.length >= 3 && !GENERIC_PATH_SEGMENTS.has(item),
|
|
185
|
+
)) {
|
|
186
|
+
if (text.includes(segment)) {
|
|
187
|
+
score += 5;
|
|
188
|
+
reasons.push(`path-keyword:${segment}`);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
for (const token of text.split(/\s+/).filter((item) => item.length >= 3).slice(0, 12)) {
|
|
193
|
+
if (name.includes(token)) score += 2;
|
|
194
|
+
}
|
|
195
|
+
score += Math.min(candidate.inbound + candidate.outbound, 8) * 0.15;
|
|
196
|
+
if (/(^|\/)(test|tests|spec|specs)(\/|$)/i.test(candidate.file)) {
|
|
197
|
+
score -= 4;
|
|
198
|
+
reasons.push("test-penalty");
|
|
199
|
+
}
|
|
200
|
+
return { ...candidate, rank: Math.round(score * 100) / 100, reasons };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function publicCandidate(candidate) {
|
|
204
|
+
return {
|
|
205
|
+
id: candidate.id,
|
|
206
|
+
symbol: candidate.qualifiedName,
|
|
207
|
+
kind: candidate.label || undefined,
|
|
208
|
+
file: candidate.file,
|
|
209
|
+
lines: candidate.lines,
|
|
210
|
+
score: candidate.rank,
|
|
211
|
+
reasons: candidate.reasons,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function recommendedRead(candidate, reason) {
|
|
216
|
+
if (!candidate?.file || !candidate?.lines) return undefined;
|
|
217
|
+
return {
|
|
218
|
+
file: candidate.file,
|
|
219
|
+
start_line: candidate.lines.start,
|
|
220
|
+
end_line: candidate.lines.end,
|
|
221
|
+
reason,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export class NavigationService {
|
|
226
|
+
constructor({
|
|
227
|
+
downstream,
|
|
228
|
+
cwd = process.cwd(),
|
|
229
|
+
cache = new ArchitectureCache(),
|
|
230
|
+
now = () => Date.now(),
|
|
231
|
+
}) {
|
|
232
|
+
this.downstream = downstream;
|
|
233
|
+
this.cwd = path.resolve(cwd);
|
|
234
|
+
this.cache = cache;
|
|
235
|
+
this.now = now;
|
|
236
|
+
this.sessions = new Map();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
cleanupSessions() {
|
|
240
|
+
const cutoff = this.now() - LIMITS.sessionTtlMs;
|
|
241
|
+
for (const [id, session] of this.sessions) {
|
|
242
|
+
if (session.createdAt < cutoff) this.sessions.delete(id);
|
|
243
|
+
}
|
|
244
|
+
while (this.sessions.size > LIMITS.sessionCount) {
|
|
245
|
+
this.sessions.delete(this.sessions.keys().next().value);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
createSession(data) {
|
|
250
|
+
this.cleanupSessions();
|
|
251
|
+
const id = randomUUID();
|
|
252
|
+
this.sessions.set(id, {
|
|
253
|
+
...data,
|
|
254
|
+
createdAt: this.now(),
|
|
255
|
+
refinements: 0,
|
|
256
|
+
});
|
|
257
|
+
return id;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async resolveProject(projectHint = "") {
|
|
261
|
+
const projects = normalizeProjects(await this.downstream.call("list_projects", {}));
|
|
262
|
+
if (!projects.length) {
|
|
263
|
+
throw new NavigatorError("not_indexed", "当前 Codebase 中没有可用的已索引项目。", {
|
|
264
|
+
project_path: this.cwd,
|
|
265
|
+
requires_user_confirmation: true,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
if (projectHint) {
|
|
269
|
+
const exact = projects.find((project) => project.name === projectHint);
|
|
270
|
+
if (exact) return exact;
|
|
271
|
+
const byPath = projects.find((project) => path.resolve(project.rootPath) === path.resolve(projectHint));
|
|
272
|
+
if (byPath) return byPath;
|
|
273
|
+
const pathHint =
|
|
274
|
+
path.isAbsolute(projectHint) ||
|
|
275
|
+
projectHint === "." ||
|
|
276
|
+
projectHint.startsWith(`.${path.sep}`) ||
|
|
277
|
+
projectHint.includes(path.sep);
|
|
278
|
+
if (pathHint) {
|
|
279
|
+
throw new NavigatorError("not_indexed", `路径 ${compactText(projectHint, 80)} 尚未建立索引。`, {
|
|
280
|
+
project_path: path.resolve(projectHint),
|
|
281
|
+
available_projects: projects.slice(0, 8).map((project) => project.name),
|
|
282
|
+
requires_user_confirmation: true,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
throw new NavigatorError("needs_user_input", `找不到已索引项目 ${compactText(projectHint, 80)}。`, {
|
|
286
|
+
choices: projects.slice(0, 8).map((project) => project.name),
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
const containing = projects
|
|
290
|
+
.filter((project) => {
|
|
291
|
+
if (!project.rootPath) return false;
|
|
292
|
+
const relative = path.relative(path.resolve(project.rootPath), this.cwd);
|
|
293
|
+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
|
294
|
+
})
|
|
295
|
+
.sort((a, b) => b.rootPath.length - a.rootPath.length);
|
|
296
|
+
if (containing.length) return containing[0];
|
|
297
|
+
throw new NavigatorError("not_indexed", "当前目录未对应任何已索引项目。", {
|
|
298
|
+
project_path: this.cwd,
|
|
299
|
+
available_projects: projects.slice(0, 8).map((project) => project.name),
|
|
300
|
+
requires_user_confirmation: true,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async context(projectHint = "") {
|
|
305
|
+
const project = await this.resolveProject(projectHint);
|
|
306
|
+
const status = normalizeIndexStatus(
|
|
307
|
+
await this.downstream.call("index_status", { project: project.name }),
|
|
308
|
+
);
|
|
309
|
+
if (!status.nodes || status.status.toLowerCase().includes("not")) {
|
|
310
|
+
throw new NavigatorError("not_indexed", `项目 ${project.name} 尚未完成索引。`, {
|
|
311
|
+
project: project.name,
|
|
312
|
+
requires_user_confirmation: true,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
if (status.stale) {
|
|
316
|
+
throw new NavigatorError("stale_index", `项目 ${project.name} 的索引已过期。`, {
|
|
317
|
+
project: project.name,
|
|
318
|
+
requires_user_confirmation: true,
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
const fingerprint = fingerprintFor({ ...status, project: project.name });
|
|
322
|
+
let map = await this.cache.get(project.name, fingerprint);
|
|
323
|
+
if (!map) {
|
|
324
|
+
const [foldersPayload, architecturePayload] = await Promise.all([
|
|
325
|
+
this.downstream.call("search_graph", {
|
|
326
|
+
project: project.name,
|
|
327
|
+
label: "Folder",
|
|
328
|
+
limit: LIMITS.folderResults,
|
|
329
|
+
include_connected: false,
|
|
330
|
+
format: "json",
|
|
331
|
+
}),
|
|
332
|
+
this.downstream.call("get_architecture", {
|
|
333
|
+
project: project.name,
|
|
334
|
+
aspects: ["layers", "packages", "boundaries", "entry_points", "languages"],
|
|
335
|
+
max_results: 40,
|
|
336
|
+
format: "json",
|
|
337
|
+
}),
|
|
338
|
+
]);
|
|
339
|
+
const override = await loadProjectOverride(status.rootPath || project.rootPath);
|
|
340
|
+
map = buildArchitectureMap({
|
|
341
|
+
project: project.name,
|
|
342
|
+
fingerprint,
|
|
343
|
+
folders: normalizeFolders(foldersPayload),
|
|
344
|
+
architecture: normalizeArchitecture(architecturePayload),
|
|
345
|
+
override,
|
|
346
|
+
});
|
|
347
|
+
await this.cache.set(map).catch(() => {
|
|
348
|
+
// A read-only home directory must not prevent navigation.
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
return { project, status, fingerprint, map };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
async getProjectOutline({ project: projectHint = "" } = {}) {
|
|
355
|
+
const { project, status, map } = await this.context(projectHint);
|
|
356
|
+
return boundResponse({
|
|
357
|
+
status: map.layers.length ? "ok" : "architecture_uncertain",
|
|
358
|
+
project: project.name,
|
|
359
|
+
index: {
|
|
360
|
+
nodes: status.nodes,
|
|
361
|
+
edges: status.edges,
|
|
362
|
+
fingerprint: map.fingerprint,
|
|
363
|
+
},
|
|
364
|
+
...architectureSummary(map),
|
|
365
|
+
next_action: map.layers.length ? "locate_code" : "declare .codebase-navigator.json",
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async searchLayer(project, layer, request) {
|
|
370
|
+
const problemText = request.problem.toLowerCase();
|
|
371
|
+
const scopes = [...(layer.paths ?? [])]
|
|
372
|
+
.sort((a, b) => {
|
|
373
|
+
const aLeaf = a.toLowerCase().split("/").at(-1);
|
|
374
|
+
const bLeaf = b.toLowerCase().split("/").at(-1);
|
|
375
|
+
return Number(problemText.includes(bLeaf)) - Number(problemText.includes(aLeaf));
|
|
376
|
+
})
|
|
377
|
+
.slice(0, LIMITS.searchScopes);
|
|
378
|
+
if (!scopes.length) return [];
|
|
379
|
+
const query = searchQuery(request.problem);
|
|
380
|
+
const results = [];
|
|
381
|
+
for (const scope of scopes) {
|
|
382
|
+
const args = {
|
|
383
|
+
project: project.name,
|
|
384
|
+
file_pattern: `${scope}/**`,
|
|
385
|
+
limit: LIMITS.searchResultsPerScope,
|
|
386
|
+
include_connected: false,
|
|
387
|
+
format: "json",
|
|
388
|
+
};
|
|
389
|
+
if (request.knownSymbol) {
|
|
390
|
+
args.name_pattern = escapeRegex(request.knownSymbol);
|
|
391
|
+
} else if (query) {
|
|
392
|
+
args.query = query;
|
|
393
|
+
}
|
|
394
|
+
const payload = await this.downstream.call("search_graph", args);
|
|
395
|
+
results.push(
|
|
396
|
+
...normalizeSearch(payload).map((candidate, rank) => ({
|
|
397
|
+
...candidate,
|
|
398
|
+
searchRankBoost: Math.max(0, 5 - rank * 0.25),
|
|
399
|
+
})),
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
return uniqueBy(results, (candidate) => `${candidate.file}:${candidate.qualifiedName}`)
|
|
403
|
+
.map((candidate) => ({
|
|
404
|
+
...scoreCandidate(candidate, { ...request, layer }),
|
|
405
|
+
}))
|
|
406
|
+
.sort((a, b) => b.rank - a.rank || a.qualifiedName.localeCompare(b.qualifiedName))
|
|
407
|
+
.slice(0, LIMITS.candidates)
|
|
408
|
+
.map((candidate, index) => ({ ...candidate, id: `c${index + 1}` }));
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async searchExactLayer(project, layer, request, literalHint) {
|
|
412
|
+
const pattern = exactLiteralPattern(literalHint);
|
|
413
|
+
const scopes = (layer.paths ?? []).slice(0, LIMITS.searchScopes);
|
|
414
|
+
if (!pattern) return [];
|
|
415
|
+
const args = {
|
|
416
|
+
project: project.name,
|
|
417
|
+
pattern,
|
|
418
|
+
mode: "compact",
|
|
419
|
+
context: 0,
|
|
420
|
+
regex: true,
|
|
421
|
+
limit: LIMITS.exactResults,
|
|
422
|
+
};
|
|
423
|
+
if (scopes.length) {
|
|
424
|
+
args.path_filter = scopes
|
|
425
|
+
.map((scope) => layer.id === "exact_hint"
|
|
426
|
+
? scope.includes("/")
|
|
427
|
+
? `^${escapeRegex(scope)}(?:/|$)`
|
|
428
|
+
: `(?:^|/)${escapeRegex(scope)}(?:/|$)`
|
|
429
|
+
: `^${escapeRegex(scope)}(?:/|$)`)
|
|
430
|
+
.join("|");
|
|
431
|
+
}
|
|
432
|
+
const payload = await this.downstream.call("search_code", args);
|
|
433
|
+
return normalizeCodeSearch(payload)
|
|
434
|
+
.filter((candidate) => candidateMatchesScope(candidate, layer))
|
|
435
|
+
.map((candidate) => {
|
|
436
|
+
const span = candidate.lines.end - candidate.lines.start + 1;
|
|
437
|
+
const symbolBonus = /^(method|function|constructor)$/i.test(candidate.label) ? 5 : 0;
|
|
438
|
+
const precisionBonus = span <= 80 ? 5 : span <= 240 ? 2 : -3;
|
|
439
|
+
return {
|
|
440
|
+
...scoreCandidate(candidate, { ...request, layer }),
|
|
441
|
+
rank: 40 + symbolBonus + precisionBonus + Math.min(candidate.matchLines.length, 3),
|
|
442
|
+
reasons: ["exact-literal", `layer:${layer.id}`],
|
|
443
|
+
};
|
|
444
|
+
})
|
|
445
|
+
.sort((a, b) =>
|
|
446
|
+
b.rank - a.rank ||
|
|
447
|
+
(a.lines.end - a.lines.start) - (b.lines.end - b.lines.start),
|
|
448
|
+
)
|
|
449
|
+
.slice(0, LIMITS.candidates)
|
|
450
|
+
.map((candidate, index) => ({ ...candidate, id: `c${index + 1}` }));
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
exactLocationResponse({ project, layer, locatorId, candidates }) {
|
|
454
|
+
return boundResponse({
|
|
455
|
+
status: "located",
|
|
456
|
+
project: project.name,
|
|
457
|
+
locator_id: locatorId,
|
|
458
|
+
selected_layer: {
|
|
459
|
+
id: layer.id,
|
|
460
|
+
purpose: layer.purpose,
|
|
461
|
+
paths: layer.paths,
|
|
462
|
+
},
|
|
463
|
+
candidates: candidates.map(publicCandidate),
|
|
464
|
+
recommended_reads: [
|
|
465
|
+
recommendedRead(
|
|
466
|
+
candidates[0],
|
|
467
|
+
layer.id === "exact_hint"
|
|
468
|
+
? "提示范围内精确字面量命中"
|
|
469
|
+
: "架构层内精确字面量命中",
|
|
470
|
+
),
|
|
471
|
+
].filter(Boolean),
|
|
472
|
+
match: {
|
|
473
|
+
strategy: "exact_literal",
|
|
474
|
+
source_returned: false,
|
|
475
|
+
},
|
|
476
|
+
next_action: "read_recommended_ranges",
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
traceDirection(problem) {
|
|
481
|
+
const lower = problem.toLowerCase();
|
|
482
|
+
return INBOUND_WORDS.some((word) => lower.includes(word)) ? "inbound" : "outbound";
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
async verify(project, candidate, problem) {
|
|
486
|
+
try {
|
|
487
|
+
const payload = await this.downstream.call("trace_path", {
|
|
488
|
+
project: project.name,
|
|
489
|
+
function_name: candidate.qualifiedName,
|
|
490
|
+
direction: this.traceDirection(problem),
|
|
491
|
+
depth: 1,
|
|
492
|
+
mode: "calls",
|
|
493
|
+
include_tests: false,
|
|
494
|
+
format: "json",
|
|
495
|
+
});
|
|
496
|
+
return normalizeTrace(payload).slice(0, LIMITS.related);
|
|
497
|
+
} catch {
|
|
498
|
+
return [];
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
async coverage(project, files) {
|
|
503
|
+
const paths = files.filter(Boolean).slice(0, LIMITS.reads);
|
|
504
|
+
if (!paths.length) return [];
|
|
505
|
+
try {
|
|
506
|
+
return normalizeCoverage(
|
|
507
|
+
await this.downstream.call("check_index_coverage", {
|
|
508
|
+
project: project.name,
|
|
509
|
+
paths,
|
|
510
|
+
format: "json",
|
|
511
|
+
}),
|
|
512
|
+
);
|
|
513
|
+
} catch {
|
|
514
|
+
return [];
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
async locateCode(request) {
|
|
519
|
+
const { project, fingerprint, map } = await this.context(request.project ?? "");
|
|
520
|
+
if (request.fileHint || !map.layers.length) {
|
|
521
|
+
const layer = exactHintLayer(request);
|
|
522
|
+
const literalHint = request.knownSymbol ||
|
|
523
|
+
(request.fileHint ? request.problem : strongestExactLiteral(request.problem));
|
|
524
|
+
const exactCandidates = await this.searchExactLayer(
|
|
525
|
+
project,
|
|
526
|
+
layer,
|
|
527
|
+
request,
|
|
528
|
+
literalHint,
|
|
529
|
+
);
|
|
530
|
+
if (exactCandidates.length) {
|
|
531
|
+
const locatorId = this.createSession({
|
|
532
|
+
request,
|
|
533
|
+
map,
|
|
534
|
+
project,
|
|
535
|
+
fingerprint,
|
|
536
|
+
layer,
|
|
537
|
+
candidates: exactCandidates,
|
|
538
|
+
candidateSignature: candidateSignature(exactCandidates),
|
|
539
|
+
});
|
|
540
|
+
return this.exactLocationResponse({
|
|
541
|
+
project,
|
|
542
|
+
layer,
|
|
543
|
+
locatorId,
|
|
544
|
+
candidates: exactCandidates,
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
if (!map.layers.length) {
|
|
549
|
+
const layer = exactHintLayer(request);
|
|
550
|
+
const locatorId = this.createSession({
|
|
551
|
+
request,
|
|
552
|
+
map,
|
|
553
|
+
project,
|
|
554
|
+
fingerprint,
|
|
555
|
+
layer,
|
|
556
|
+
candidates: [],
|
|
557
|
+
});
|
|
558
|
+
return boundResponse({
|
|
559
|
+
status: "architecture_uncertain",
|
|
560
|
+
project: project.name,
|
|
561
|
+
locator_id: locatorId,
|
|
562
|
+
message: "无法从图谱推断稳定的分层结构,受限精确搜索也未命中。",
|
|
563
|
+
next_action: "refine_location",
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const classification = request.layer
|
|
568
|
+
? {
|
|
569
|
+
confident: true,
|
|
570
|
+
selected: {
|
|
571
|
+
layer: map.layers.find((layer) => layer.id === request.layer),
|
|
572
|
+
score: 100,
|
|
573
|
+
reasons: ["caller-selected"],
|
|
574
|
+
},
|
|
575
|
+
choices: [],
|
|
576
|
+
}
|
|
577
|
+
: classifyProblem(map, request);
|
|
578
|
+
|
|
579
|
+
if (!classification.selected?.layer) {
|
|
580
|
+
const locatorId = this.createSession({ request, map, project, fingerprint });
|
|
581
|
+
return boundResponse({
|
|
582
|
+
status: "ambiguous",
|
|
583
|
+
project: project.name,
|
|
584
|
+
locator_id: locatorId,
|
|
585
|
+
message: "问题不能稳定归入唯一架构层;请选择最接近的一层。",
|
|
586
|
+
choices: classification.choices,
|
|
587
|
+
next_action: "refine_location",
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
return this.locateInLayer({
|
|
592
|
+
project,
|
|
593
|
+
fingerprint,
|
|
594
|
+
map,
|
|
595
|
+
request,
|
|
596
|
+
layer: classification.selected.layer,
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
async locateInLayer({
|
|
601
|
+
project,
|
|
602
|
+
fingerprint,
|
|
603
|
+
map,
|
|
604
|
+
request,
|
|
605
|
+
layer,
|
|
606
|
+
locatorId,
|
|
607
|
+
previousSignature = "",
|
|
608
|
+
}) {
|
|
609
|
+
const candidates = await this.searchLayer(project, layer, request);
|
|
610
|
+
const sessionId = locatorId ?? this.createSession({ request, map, project, fingerprint, layer, candidates });
|
|
611
|
+
if (!candidates.length) {
|
|
612
|
+
return boundResponse({
|
|
613
|
+
status: "insufficient_coverage",
|
|
614
|
+
project: project.name,
|
|
615
|
+
locator_id: sessionId,
|
|
616
|
+
selected_layer: { id: layer.id, paths: layer.paths },
|
|
617
|
+
message: "已限定架构层,但没有命中可编辑符号。",
|
|
618
|
+
next_action: "refine_location",
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
const signature = candidateSignature(candidates);
|
|
622
|
+
if (previousSignature && signature === previousSignature) {
|
|
623
|
+
const session = this.sessions.get(sessionId);
|
|
624
|
+
if (session) Object.assign(session, { layer, candidates, candidateSignature: signature });
|
|
625
|
+
return boundResponse({
|
|
626
|
+
status: "insufficient_coverage",
|
|
627
|
+
project: project.name,
|
|
628
|
+
locator_id: sessionId,
|
|
629
|
+
selected_layer: { id: layer.id, paths: layer.paths },
|
|
630
|
+
progress: "unchanged",
|
|
631
|
+
message: "细化提示未改变候选集合,已停止重复返回相同结果。",
|
|
632
|
+
next_action: "provide_symbol_or_path",
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const top = candidates[0];
|
|
637
|
+
const related = await this.verify(project, top, request.problem);
|
|
638
|
+
const reads = [
|
|
639
|
+
recommendedRead(top, "最高分目标符号"),
|
|
640
|
+
recommendedRead(
|
|
641
|
+
related.find((item) => item.lines),
|
|
642
|
+
this.traceDirection(request.problem) === "inbound" ? "直接调用者" : "直接被调用者",
|
|
643
|
+
),
|
|
644
|
+
].filter(Boolean);
|
|
645
|
+
const coverage = await this.coverage(project, reads.map((item) => item.file));
|
|
646
|
+
const closeScores = candidates.length > 1 && candidates[0].rank - candidates[1].rank < 1.5;
|
|
647
|
+
const session = this.sessions.get(sessionId);
|
|
648
|
+
if (session) Object.assign(session, {
|
|
649
|
+
layer,
|
|
650
|
+
candidates,
|
|
651
|
+
related,
|
|
652
|
+
candidateSignature: signature,
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
return boundResponse({
|
|
656
|
+
status: closeScores ? "ambiguous" : "located",
|
|
657
|
+
project: project.name,
|
|
658
|
+
locator_id: sessionId,
|
|
659
|
+
selected_layer: {
|
|
660
|
+
id: layer.id,
|
|
661
|
+
purpose: layer.purpose,
|
|
662
|
+
paths: layer.paths,
|
|
663
|
+
},
|
|
664
|
+
candidates: candidates.map(publicCandidate),
|
|
665
|
+
recommended_reads: reads,
|
|
666
|
+
related: related.map((item) => ({
|
|
667
|
+
direction: item.direction,
|
|
668
|
+
symbol: item.qualifiedName,
|
|
669
|
+
file: item.file,
|
|
670
|
+
lines: item.lines,
|
|
671
|
+
})),
|
|
672
|
+
coverage,
|
|
673
|
+
next_action: closeScores ? "refine_location" : "read_recommended_ranges",
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
async refineLocation({
|
|
678
|
+
locatorId,
|
|
679
|
+
layer: layerId,
|
|
680
|
+
candidateId,
|
|
681
|
+
additionalHint = "",
|
|
682
|
+
}) {
|
|
683
|
+
this.cleanupSessions();
|
|
684
|
+
const session = this.sessions.get(locatorId);
|
|
685
|
+
if (!session) {
|
|
686
|
+
throw new NavigatorError("needs_user_input", "定位会话不存在或已经过期,请重新调用 locate_code。");
|
|
687
|
+
}
|
|
688
|
+
if (session.refinements >= LIMITS.refinements) {
|
|
689
|
+
throw new NavigatorError("needs_user_input", "定位已细化两次,请补充明确的符号名、文件提示或报错文本。");
|
|
690
|
+
}
|
|
691
|
+
session.refinements += 1;
|
|
692
|
+
const current = await this.context(session.project.name);
|
|
693
|
+
if (current.fingerprint !== session.fingerprint) {
|
|
694
|
+
throw new NavigatorError("stale_index", "索引在定位过程中发生变化,请重新调用 locate_code。");
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
if (candidateId && session.candidates?.length) {
|
|
698
|
+
const candidate = session.candidates.find((item) => item.id === candidateId);
|
|
699
|
+
if (!candidate) throw new NavigatorError("needs_user_input", `候选 ${candidateId} 不存在。`);
|
|
700
|
+
const related = await this.verify(session.project, candidate, session.request.problem);
|
|
701
|
+
const reads = [
|
|
702
|
+
recommendedRead(candidate, "调用者确认的目标符号"),
|
|
703
|
+
recommendedRead(related.find((item) => item.lines), "一跳关系符号"),
|
|
704
|
+
].filter(Boolean);
|
|
705
|
+
return boundResponse({
|
|
706
|
+
status: "located",
|
|
707
|
+
project: session.project.name,
|
|
708
|
+
locator_id: locatorId,
|
|
709
|
+
candidates: [publicCandidate(candidate)],
|
|
710
|
+
recommended_reads: reads,
|
|
711
|
+
next_action: "read_recommended_ranges",
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
const layer =
|
|
716
|
+
session.map.layers.find((item) => item.id === layerId) ??
|
|
717
|
+
session.layer;
|
|
718
|
+
if (!layer) {
|
|
719
|
+
return boundResponse({
|
|
720
|
+
status: "ambiguous",
|
|
721
|
+
project: session.project.name,
|
|
722
|
+
locator_id: locatorId,
|
|
723
|
+
message: "请选择一个架构层。",
|
|
724
|
+
choices: architectureSummary(session.map).layers,
|
|
725
|
+
next_action: "refine_location",
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
session.request = {
|
|
729
|
+
...session.request,
|
|
730
|
+
problem: `${session.request.problem} ${compactText(additionalHint, 180)}`.trim(),
|
|
731
|
+
layer: layer.id,
|
|
732
|
+
};
|
|
733
|
+
const exactCandidates = await this.searchExactLayer(
|
|
734
|
+
session.project,
|
|
735
|
+
layer,
|
|
736
|
+
session.request,
|
|
737
|
+
additionalHint,
|
|
738
|
+
);
|
|
739
|
+
if (exactCandidates.length) {
|
|
740
|
+
Object.assign(session, {
|
|
741
|
+
layer,
|
|
742
|
+
candidates: exactCandidates,
|
|
743
|
+
candidateSignature: candidateSignature(exactCandidates),
|
|
744
|
+
});
|
|
745
|
+
return this.exactLocationResponse({
|
|
746
|
+
project: session.project,
|
|
747
|
+
layer,
|
|
748
|
+
locatorId,
|
|
749
|
+
candidates: exactCandidates,
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
return this.locateInLayer({
|
|
753
|
+
project: session.project,
|
|
754
|
+
fingerprint: session.fingerprint,
|
|
755
|
+
map: session.map,
|
|
756
|
+
request: session.request,
|
|
757
|
+
layer,
|
|
758
|
+
locatorId,
|
|
759
|
+
previousSignature: session.candidateSignature,
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
}
|