-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat.ts
More file actions
342 lines (302 loc) · 10.3 KB
/
Copy pathformat.ts
File metadata and controls
342 lines (302 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* Formatting utilities for MCP tool responses
*/
import { isRepoError, type SyncResult } from "../tools/sync.js";
import type { SearchResult, FileInfo } from "./search.js";
import type { SyncMetadata } from "./sync-metadata.js";
import type { ErrorLookupResult } from "./error-lookup.js";
import type { SemanticSearchToolResult } from "../tools/search.js";
import type { ErrorLookupToolResult } from "../tools/error-lookup.js";
import { MCP_VERSION } from "../version.js";
import {
formatUpgradeStatusLine,
getUpgradeInfo,
} from "./version-self-check.js";
export function formatSyncResult(result: SyncResult): string {
const lines = [
result.success ? "✓ Sync completed" : "⚠ Sync completed with errors",
"",
`Version: ${result.version}`,
result.message,
"",
"Repositories:",
];
for (const repo of result.repos) {
const icon = isRepoError(repo) ? "✗" : "✓";
lines.push(` ${icon} ${repo.name}: ${repo.status}`);
}
return lines.join("\n");
}
export function formatStatus(status: {
reposDir: string;
repos: {
name: string;
description: string;
cloned: boolean;
commit?: string;
}[];
syncMetadata?: SyncMetadata | null;
}): string {
const lines = [
"Aztec MCP Server Status",
"",
// Live version read from package.json at module load (see
// ``src/version.ts``). The previous implementation pulled this
// from sync metadata, which was the version that ran the LAST
// sync — stale across upgrades that didn't touch the clones.
`MCP server version: ${MCP_VERSION}`,
];
// npm-latest comparison done at boot (``checkForUpgrade`` in
// ``src/index.ts``). Prints either "you are up to date" or an
// upgrade-available warning. Empty string when the registry check
// failed at boot, so we stay silent rather than misleading.
const upgradeLine = formatUpgradeStatusLine(getUpgradeInfo());
if (upgradeLine) {
lines.push(upgradeLine);
}
lines.push(`Repos directory: ${status.reposDir}`);
if (status.syncMetadata) {
lines.push(`Last synced: ${status.syncMetadata.syncedAt}`);
if (status.syncMetadata.mcpVersion !== MCP_VERSION) {
// Only mention this when it differs from the live version —
// otherwise it's just noise that duplicates the line above.
lines.push(
` (last sync ran under MCP server v${status.syncMetadata.mcpVersion} — re-run aztec_sync_repos to refresh metadata)`
);
}
lines.push(`Aztec version: ${status.syncMetadata.aztecVersion}`);
}
lines.push("");
lines.push("Repositories:");
for (const repo of status.repos) {
const icon = repo.cloned ? "✓" : "○";
const commit = repo.commit ? ` (${repo.commit})` : "";
lines.push(` ${icon} ${repo.name}${commit}`);
lines.push(` ${repo.description}`);
}
const clonedCount = status.repos.filter((r) => r.cloned).length;
if (clonedCount === 0) {
lines.push("");
lines.push("No repositories cloned. Run aztec_sync_repos to get started.");
}
return lines.join("\n");
}
export function formatSearchResults(result: {
success: boolean;
results: SearchResult[];
message: string;
}): string {
const lines = [result.message, ""];
if (!result.success || result.results.length === 0) {
return lines.join("\n");
}
for (const match of result.results) {
lines.push(`**${match.file}:${match.line}**`);
lines.push("```");
lines.push(match.content);
lines.push("```");
lines.push("");
}
return lines.join("\n");
}
export function formatExamplesList(result: {
success: boolean;
examples: FileInfo[];
message: string;
}): string {
const lines = [result.message, ""];
if (!result.success || result.examples.length === 0) {
return lines.join("\n");
}
// Group by repo
const byRepo = new Map<string, FileInfo[]>();
for (const example of result.examples) {
if (!byRepo.has(example.repo)) {
byRepo.set(example.repo, []);
}
byRepo.get(example.repo)!.push(example);
}
for (const [repo, examples] of byRepo) {
lines.push(`**${repo}:**`);
for (const example of examples) {
lines.push(` - ${example.name}`);
}
lines.push("");
}
return lines.join("\n");
}
export function formatExampleContent(result: {
success: boolean;
example?: FileInfo;
content?: string;
message: string;
}): string {
if (!result.success || !result.content) {
return result.message;
}
const lines = [
`**${result.example!.name}** (${result.example!.repo})`,
`Path: ${result.example!.path}`,
"",
"```noir",
result.content,
"```",
];
return lines.join("\n");
}
export function formatFileContent(result: {
success: boolean;
content?: string;
message: string;
}): string {
if (!result.success || !result.content) {
return result.message;
}
return result.content;
}
export function formatErrorLookupResult(result: ErrorLookupToolResult): string {
const lines = [result.message, ""];
const { catalogMatches, codeMatches } = result.result;
// When semantic results exist AND every catalog match is below the
// strong-match threshold, the catalog hits are low-confidence cues.
// Two cases:
//
// semanticHasResults = true → semantic returned content-bearing
// chunks (the lookupAztecError filter only sets semanticResults
// when at least one chunk passed isUsefulSemanticChunk). The
// weak catalog hint is now actively misleading — the user keeps
// anchoring on it as the "primary answer" even though semantic
// gave us better context. SUPPRESS the catalog section entirely.
//
// semanticHasResults = false → semantic ran but produced nothing
// useful (or didn't run: no client, version mismatch, backend
// failed). The user has no other signal. KEEP the weak catalog
// with a clear "Lower-Confidence Catalog Hints" header so they
// have *something* to look at, framed honestly.
const semanticHasResults =
!!result.semanticResults && result.semanticResults.length > 0;
const catalogIsWeakOnly =
catalogMatches.length > 0 &&
catalogMatches.every((m) => m.score < 70);
const suppressWeakCatalog = catalogIsWeakOnly && semanticHasResults;
const renderSemanticFirst = semanticHasResults && catalogIsWeakOnly;
function renderSemantic() {
if (!result.semanticResults || result.semanticResults.length === 0) return;
lines.push("## Related Documentation");
lines.push("");
for (const match of result.semanticResults) {
if (match.title) {
lines.push(`**${match.title}**`);
}
if (match.source) {
lines.push(`Source: ${match.source}`);
}
lines.push("");
lines.push(match.text);
lines.push("");
lines.push("---");
lines.push("");
}
}
function renderCatalog() {
if (catalogMatches.length === 0) return;
// Phase 2 suppression: when semantic returned content-bearing
// chunks AND the catalog is weak-only, the catalog hits are
// pure noise that the user keeps anchoring on. Hide them
// entirely. They remain in `result.catalogMatches` for
// programmatic consumers that need every signal.
if (suppressWeakCatalog) return;
lines.push(
catalogIsWeakOnly
? "## Lower-Confidence Catalog Hints"
: "## Known Errors"
);
if (catalogIsWeakOnly) {
// Only point at "documentation results above" when there
// actually is a semantic section above (semantic ran AND
// returned hits, AND we reordered to render it first). In
// every other weak-only state — no client, version mismatch,
// backend failed, semantic returned empty — there's no docs
// section to point at, so use neutral copy that names the
// weakness without implying a better answer is below.
lines.push(
renderSemanticFirst
? "_These are word-overlap fuzzy matches, not direct hits — the documentation results above are likely more authoritative._"
: "_These are word-overlap fuzzy matches, not direct hits. Treat as low-confidence cues only._"
);
}
lines.push("");
for (const m of catalogMatches) {
const { entry } = m;
lines.push(`**${entry.name}**`);
if (entry.errorCode !== undefined) lines.push(`- Code: ${entry.errorCode}`);
if (entry.hexSignature) lines.push(`- Hex: ${entry.hexSignature}`);
lines.push(`- Category: ${entry.category}`);
lines.push(`- Source: ${entry.source}`);
lines.push(`- Match: ${m.matchType} (score ${m.score})`);
lines.push(`- **Cause**: ${entry.cause}`);
lines.push(`- **Fix**: ${entry.fix}`);
lines.push("");
}
}
function renderCode() {
if (codeMatches.length === 0) return;
lines.push("## Related Code References");
lines.push("");
for (const match of codeMatches) {
lines.push(`**${match.file}:${match.line}**`);
lines.push("```");
lines.push(match.content);
lines.push("```");
lines.push("");
}
}
if (renderSemanticFirst) {
renderSemantic();
renderCatalog();
renderCode();
} else {
renderCatalog();
renderCode();
renderSemantic();
}
if (
catalogMatches.length === 0 &&
codeMatches.length === 0 &&
(!result.semanticResults || result.semanticResults.length === 0) &&
// Don't repeat the "try" hints when the message already explains
// *why* there are no semantic results (version mismatch / backend
// failure) — the message field is already descriptive.
result.semanticHealth !== "version_mismatch" &&
result.semanticHealth !== "failed"
) {
lines.push("No matching errors found. Try:");
lines.push("- A numeric error code (e.g., `2002`)");
lines.push("- A hex signature (e.g., `0xa5b2ba17`)");
lines.push("- An error message substring (e.g., `insufficient fee`)");
}
return lines.join("\n");
}
/**
* Format semantic search results from DocsGPT.
*/
export function formatSemanticSearchResults(result: SemanticSearchToolResult): string {
const lines = [result.message, ""];
if (!result.success || result.results.length === 0) {
return lines.join("\n");
}
for (const match of result.results) {
if (match.title) {
lines.push(`**${match.title}**`);
}
if (match.source) {
lines.push(`Source: ${match.source}`);
}
lines.push("");
lines.push(match.text);
lines.push("");
lines.push("---");
lines.push("");
}
return lines.join("\n");
}