@floydous/pi-agent-kernel 0.2.0 → 0.3.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.
@@ -30,9 +30,8 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
30
30
  name: "lsp",
31
31
  label: "Language Server Protocol (LSP)",
32
32
  description:
33
- "Query language server for definitions, references (supports exclude_tests and exclude_declaration), hover docstrings, document symbols, or diagnostics with instant Tree-sitter fallback.",
34
- promptSnippet:
35
- "Query LSP for definitions, references (with test filtering), hover type signatures, and workspace diagnostics",
33
+ "LSP definitions, references, hover, document_symbols, or diagnostics with Tree-sitter fallback.",
34
+ promptSnippet: "Query LSP for definitions, references, hover, and diagnostics",
36
35
  renderShell: "default",
37
36
  parameters: Type.Object({
38
37
  action: Type.String({
@@ -43,34 +42,32 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
43
42
  "document_symbols",
44
43
  "diagnostics",
45
44
  ],
46
- description:
47
- "LSP operation: 'definition' | 'references' | 'hover' | 'document_symbols' | 'diagnostics'",
45
+ description: "LSP operation",
48
46
  }),
49
- path: Type.String({ description: "File path (absolute or relative)" }),
47
+ path: Type.Optional(Type.String({ description: "File path (optional for definition if symbol given)" })),
50
48
  line: Type.Optional(
51
49
  Type.Number({
52
- description:
53
- "1-based line number (required for definition, references, hover)",
50
+ description: "1-based line number (for definition, references, hover)",
54
51
  }),
55
52
  ),
56
53
  character: Type.Optional(
57
54
  Type.Number({
58
- description: "1-based character/column number (default: 1)",
55
+ description: "1-based column number (default: 1)",
59
56
  }),
60
57
  ),
61
58
  symbol: Type.Optional(
62
59
  Type.String({
63
- description: "Optional symbol name for definition, references, or hover; avoids manual line/character coordinates",
60
+ description: "Symbol name (avoids line/character coordinates)",
64
61
  }),
65
62
  ),
66
63
  exclude_tests: Type.Optional(
67
64
  Type.Boolean({
68
- description: "Exclude references located in test files or test modules (default: false)",
65
+ description: "Exclude references in tests (default: false)",
69
66
  }),
70
67
  ),
71
68
  exclude_declaration: Type.Optional(
72
69
  Type.Boolean({
73
- description: "Exclude the symbol's own declaration site from references (default: false)",
70
+ description: "Exclude declaration site from references (default: false)",
74
71
  }),
75
72
  ),
76
73
  }),
@@ -84,38 +81,109 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
84
81
  if (
85
82
  !params ||
86
83
  typeof params !== "object" ||
87
- typeof params.path !== "string" ||
88
- !params.path.trim() ||
89
84
  !["definition", "references", "hover", "document_symbols", "diagnostics"].includes(params.action)
90
85
  ) {
91
86
  return {
92
- content: [{ type: "text", text: "[LSP ERROR] Invalid action or file path." }],
87
+ content: [{ type: "text", text: "[LSP ERROR] Invalid action or parameters." }],
93
88
  isError: true,
94
89
  };
95
90
  }
96
91
  const action = params.action;
97
- const targetPath = params.path;
98
- if (
99
- typeof targetPath !== "string" ||
100
- !targetPath.trim() ||
101
- !["definition", "references", "hover", "document_symbols", "diagnostics"].includes(action)
102
- ) {
103
- return {
104
- content: [{ type: "text", text: "[LSP ERROR] Invalid action or file path." }],
105
- isError: true,
106
- };
92
+ const targetPath = typeof params.path === "string" ? params.path.trim() : "";
93
+ const requestedSymbol = typeof params.symbol === "string" ? params.symbol.trim() : "";
94
+
95
+ const result = (
96
+ text: string,
97
+ source?: "lsp" | "tree-sitter" | "syntax-check",
98
+ extra: Record<string, unknown> = {},
99
+ isError = false,
100
+ ) => ({
101
+ content: [{ type: "text", text }],
102
+ details: { action, ...(source ? { source } : {}), ...extra },
103
+ ...(isError ? { isError: true } : {}),
104
+ });
105
+
106
+ // Bare-symbol definition lookup: path is omitted, resolve workspace-wide
107
+ if (!targetPath) {
108
+ if (action !== "definition") {
109
+ return result(
110
+ `[LSP ERROR] 'path' is required for action '${action}'.`,
111
+ undefined,
112
+ { error: "missing_path" },
113
+ true,
114
+ );
115
+ }
116
+ if (!requestedSymbol) {
117
+ return result(
118
+ "[LSP ERROR] 'symbol' is required for workspace definition lookup when 'path' is omitted.",
119
+ undefined,
120
+ { error: "missing_symbol" },
121
+ true,
122
+ );
123
+ }
124
+
125
+ // Resolve workspace-wide using searchAstSymbols with exact match first
126
+ let astHits = searchAstSymbols(ctx.cwd, {
127
+ name: requestedSymbol,
128
+ exactMatch: true,
129
+ });
130
+ if (astHits.length === 0) {
131
+ astHits = searchAstSymbols(ctx.cwd, {
132
+ name: requestedSymbol,
133
+ exactMatch: false,
134
+ });
135
+ }
136
+
137
+ if (astHits.length === 0) {
138
+ return result(
139
+ `Definition not found for symbol '${requestedSymbol}' in workspace.`,
140
+ "tree-sitter",
141
+ { count: 0, symbol: requestedSymbol },
142
+ );
143
+ }
144
+
145
+ if (astHits.length === 1) {
146
+ const hit = astHits[0];
147
+ const aliasInfo = hit.aliasedFrom
148
+ ? `\n (aliased from '${hit.aliasedFrom.originalName}' in ${hit.aliasedFrom.module || "workspace"})`
149
+ : "";
150
+ return result(
151
+ `[Tree-sitter AST Definition - unique match for '${requestedSymbol}']\n → [${hit.kind}] ${hit.name} (${hit.filePath}:${hit.line})\n Signature: ${hit.signature}${aliasInfo}`,
152
+ "tree-sitter",
153
+ { count: 1, symbol: requestedSymbol },
154
+ );
155
+ }
156
+
157
+ // Ambiguous matches: bounded candidate list (up to 5 candidates)
158
+ const formatted = astHits
159
+ .slice(0, 5)
160
+ .map((h) => {
161
+ const aliasInfo = h.aliasedFrom
162
+ ? `\n (aliased from '${h.aliasedFrom.originalName}' in ${h.aliasedFrom.module || "workspace"})`
163
+ : "";
164
+ return ` → [${h.kind}] ${h.name} (${h.filePath}:${h.line})\n Signature: ${h.signature}${aliasInfo}`;
165
+ })
166
+ .join("\n");
167
+ return result(
168
+ `[Tree-sitter AST Definition - ${astHits.length} matches for '${requestedSymbol}']\n${formatted}`,
169
+ "tree-sitter",
170
+ { count: astHits.length, symbol: requestedSymbol },
171
+ );
107
172
  }
108
173
  const absPath = path.isAbsolute(targetPath)
109
174
  ? targetPath
110
175
  : path.resolve(ctx.cwd, targetPath);
111
176
 
112
177
  if (!fs.existsSync(absPath)) {
113
- return {
114
- content: [
115
- { type: "text", text: `[LSP ERROR] File not found: ${targetPath}` },
116
- ],
117
- isError: true,
118
- };
178
+ return result(`[LSP ERROR] File not found: ${targetPath}`, undefined, { error: "file_not_found" }, true);
179
+ }
180
+ if (fs.statSync(absPath).isDirectory()) {
181
+ return result(
182
+ `[LSP ERROR] Diagnostics requires a file, not directory: ${targetPath}`,
183
+ undefined,
184
+ { error: "directory_path" },
185
+ true,
186
+ );
119
187
  }
120
188
 
121
189
  // Read the source only for local cursor/symbol resolution. An LSP query
@@ -125,17 +193,16 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
125
193
  try {
126
194
  observedContent = fs.readFileSync(absPath);
127
195
  } catch (error: any) {
128
- return {
129
- content: [
130
- { type: "text", text: `[LSP ERROR] Unable to read ${targetPath}: ${error.message}` },
131
- ],
132
- isError: true,
133
- };
196
+ return result(
197
+ `[LSP ERROR] Unable to read ${targetPath}: ${error.message}`,
198
+ undefined,
199
+ { error: "read_failed" },
200
+ true,
201
+ );
134
202
  }
135
203
  // Intentionally no epistemic read record: the source buffer is an
136
204
  // implementation input, not content returned by the LSP operation.
137
205
 
138
- const requestedSymbol = typeof params.symbol === "string" ? params.symbol.trim() : "";
139
206
  let line0 = Math.max(0, (params.line || 1) - 1);
140
207
  let col0 = Math.max(0, (params.character || 1) - 1);
141
208
 
@@ -235,23 +302,13 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
235
302
  const formatted = docSyms
236
303
  .map((s) => `${s.line}: [${s.kind}] ${s.name} - ${s.signature}`)
237
304
  .join("\n");
238
- return {
239
- content: [
240
- {
241
- type: "text",
242
- text: formatted,
243
- },
244
- ],
245
- };
305
+ return result(formatted, "tree-sitter", { count: docSyms.length });
246
306
  }
247
- return {
248
- content: [
249
- {
250
- type: "text",
251
- text: `No symbols found in ${path.basename(absPath)}.`,
252
- },
253
- ],
254
- };
307
+ return result(
308
+ `No symbols found in ${path.basename(absPath)}.`,
309
+ "tree-sitter",
310
+ { count: 0 },
311
+ );
255
312
  }
256
313
 
257
314
  if (action === "references") {
@@ -261,21 +318,17 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
261
318
  const refs = findSymbolReferences(ctx.cwd, targetSym);
262
319
  if (refs.length > 0) {
263
320
  const formatted = refs
264
- .map((r) => `${r.filePath}:${r.line}:${r.column}`)
321
+ .map((r) => {
322
+ const col0 = Math.max(0, r.column - 1);
323
+ const endCol0 = col0 + targetSym.length;
324
+ const snippet = windowAround(r.lineText, col0, endCol0, 50);
325
+ return `${r.filePath}:${r.line}:${r.column}: ${snippet}`;
326
+ })
265
327
  .join("\n");
266
- return {
267
- content: [
268
- {
269
- type: "text",
270
- text: formatted,
271
- },
272
- ],
273
- };
328
+ return result(formatted, "tree-sitter", { count: refs.length });
274
329
  }
275
330
  }
276
- return {
277
- content: [{ type: "text", text: "No references found in workspace." }],
278
- };
331
+ return result("No references found in workspace.", "tree-sitter", { count: 0 });
279
332
  }
280
333
 
281
334
  if (action === "hover") {
@@ -289,7 +342,7 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
289
342
  targetSym,
290
343
  );
291
344
  if (localHover) {
292
- return { content: [{ type: "text", text: localHover }] };
345
+ return result(localHover, "tree-sitter");
293
346
  }
294
347
  const astHits = searchAstSymbols(ctx.cwd, {
295
348
  name: targetSym,
@@ -298,24 +351,13 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
298
351
  });
299
352
  if (astHits.length > 0) {
300
353
  const hit = astHits[0];
301
- return {
302
- content: [
303
- {
304
- type: "text",
305
- text: `[Tree-sitter AST Hover - ${hit.kind} ${hit.name}]\n${hit.signature}\nDeclared in ${hit.filePath}:${hit.line}`,
306
- },
307
- ],
308
- };
354
+ return result(
355
+ `[Tree-sitter AST Hover - ${hit.kind} ${hit.name}]\n${hit.signature}\nDeclared in ${hit.filePath}:${hit.line}`,
356
+ "tree-sitter",
357
+ );
309
358
  }
310
359
  }
311
- return {
312
- content: [
313
- {
314
- type: "text",
315
- text: "No hover information available at this position.",
316
- },
317
- ],
318
- };
360
+ return result("No hover information available at this position.", "tree-sitter");
319
361
  }
320
362
 
321
363
  if (action === "definition") {
@@ -336,14 +378,10 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
336
378
  for (const cand of candidates) {
337
379
  if (fs.existsSync(cand) && cand !== absPath) {
338
380
  const rel = path.relative(ctx.cwd, cand).replace(/\\/g, "/");
339
- return {
340
- content: [
341
- {
342
- type: "text",
343
- text: `[Tree-sitter AST Definition - module '${targetSym}']\n → [module] ${targetSym} (${rel}:1)\n Module file in workspace`,
344
- },
345
- ],
346
- };
381
+ return result(
382
+ `[Tree-sitter AST Definition - module '${targetSym}']\n → [module] ${targetSym} (${rel}:1)\n Module file in workspace`,
383
+ "tree-sitter",
384
+ );
347
385
  }
348
386
  }
349
387
 
@@ -384,64 +422,48 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
384
422
  return ` → [${h.kind}] ${h.name} (${h.filePath}:${h.line})\n Signature: ${h.signature}${aliasInfo}`;
385
423
  })
386
424
  .join("\n");
387
- return {
388
- content: [
389
- {
390
- type: "text",
391
- text: `[Tree-sitter AST Definition - ${astHits.length} match(es) for '${targetSym}']\n${formatted}`,
392
- },
393
- ],
394
- };
425
+ return result(
426
+ `[Tree-sitter AST Definition - ${astHits.length} match(es) for '${targetSym}']\n${formatted}`,
427
+ "tree-sitter",
428
+ { count: astHits.length },
429
+ );
395
430
  }
396
431
  }
397
- return {
398
- content: [{ type: "text", text: "Definition not found." }],
399
- };
432
+ return result("Definition not found.", "tree-sitter", { count: 0 });
400
433
  }
401
434
 
402
435
  if (action === "diagnostics") {
403
436
  const syn = checkSyntax(absPath);
404
437
  if (!syn.valid) {
405
438
  const status = syn.status || "failed";
406
- return {
407
- content: [
408
- {
409
- type: "text",
410
- text: `- [1:1] [${status.toUpperCase()}] Syntax validation: ${syn.error}`,
411
- },
412
- ],
413
- };
439
+ return result(
440
+ `- [1:1] [${status.toUpperCase()}] Syntax validation: ${syn.error}`,
441
+ "syntax-check",
442
+ { status },
443
+ );
414
444
  }
415
- return {
416
- content: [],
417
- };
445
+ const relPath = path.relative(ctx.cwd, absPath).replace(/\\/g, "/") || absPath;
446
+ return result(`${relPath} clean`, "syntax-check", { status: "clean" });
418
447
  }
419
448
 
420
- return {
421
- content: [
422
- {
423
- type: "text",
424
- text: `[LSP Notice] No installed LSP server found for ${path.basename(absPath)}. Use 'ast_search' or run '/lsp install' to configure an LSP server.`,
425
- },
426
- ],
427
- };
449
+ return result(
450
+ `[LSP Notice] No installed LSP server found for ${path.basename(absPath)}. Use 'ast_search' or run '/lsp install' to configure an LSP server.`,
451
+ "tree-sitter",
452
+ { status: "unavailable" },
453
+ );
428
454
  }
429
455
 
430
456
  try {
431
457
  if (action === "definition") {
432
458
  const defs = await client.gotoDefinition(absPath, line0, col0);
433
459
  if (defs.length > 0) {
434
- return {
435
- content: [{ type: "text", text: formatDefinitions(defs, ctx.cwd) }],
436
- };
460
+ return result(formatDefinitions(defs, ctx.cwd), "lsp", { count: defs.length });
437
461
  }
438
462
  // Only fallback to Tree-sitter AST for programming languages when LSP is unavailable or errored
439
463
  const ext = path.extname(absPath).toLowerCase();
440
464
  const nonCodeExts = [".md", ".markdown", ".json", ".jsonc", ".yaml", ".yml", ".toml", ".proto", ".txt"];
441
465
  if (nonCodeExts.includes(ext)) {
442
- return {
443
- content: [{ type: "text", text: formatDefinitions(defs, ctx.cwd) }],
444
- };
466
+ return result(formatDefinitions(defs, ctx.cwd), "lsp", { count: defs.length });
445
467
  }
446
468
 
447
469
  // Tree-sitter fallback with exact symbol ranking
@@ -480,20 +502,15 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
480
502
  return ` → [${h.kind}] ${h.name} (${h.filePath}:${h.line})\n Signature: ${h.signature}${aliasInfo}`;
481
503
  })
482
504
  .join("\n");
483
- return {
484
- content: [
485
- {
486
- type: "text",
487
- text: `[Tree-sitter AST Fallback - ${astHits.length} declaration match(es) for '${targetSym}']\n${formatted}`,
488
- },
489
- ],
490
- };
505
+ return result(
506
+ `[Tree-sitter AST Local Fallback - ${astHits.length} declaration match(es) for '${targetSym}']\n${formatted}`,
507
+ "tree-sitter",
508
+ { count: astHits.length },
509
+ );
491
510
  }
492
511
  }
493
512
 
494
- return {
495
- content: [{ type: "text", text: formatDefinitions(defs, ctx.cwd) }],
496
- };
513
+ return result(formatDefinitions(defs, ctx.cwd), "lsp", { count: defs.length });
497
514
  }
498
515
 
499
516
  if (action === "references") {
@@ -534,17 +551,13 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
534
551
  if (refs.length > 0) {
535
552
  refs = filterRefs(refs);
536
553
  if (refs.length > 0) {
537
- return {
538
- content: [{ type: "text", text: formatReferences(refs, ctx.cwd) }],
539
- };
554
+ return result(formatReferences(refs, ctx.cwd), "lsp", { count: refs.length });
540
555
  }
541
556
  }
542
557
  const ext = path.extname(absPath).toLowerCase();
543
558
  const nonCodeExts = [".md", ".markdown", ".json", ".jsonc", ".yaml", ".yml", ".toml", ".proto", ".txt"];
544
559
  if (nonCodeExts.includes(ext)) {
545
- return {
546
- content: [{ type: "text", text: formatReferences(refs, ctx.cwd) }],
547
- };
560
+ return result(formatReferences(refs, ctx.cwd), "lsp", { count: refs.length });
548
561
  }
549
562
 
550
563
  // Workspace-wide symbol reference search fallback
@@ -587,19 +600,10 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
587
600
  return `${r.filePath}:${r.line}:${r.column}: ${snippet}`;
588
601
  })
589
602
  .join("\n");
590
- return {
591
- content: [
592
- {
593
- type: "text",
594
- text: formatted,
595
- },
596
- ],
597
- };
603
+ return result(formatted, "tree-sitter", { count: astRefs.length });
598
604
  }
599
605
  }
600
- return {
601
- content: [{ type: "text", text: formatReferences(refs, ctx.cwd) }],
602
- };
606
+ return result(formatReferences(refs, ctx.cwd), "lsp", { count: refs.length });
603
607
  }
604
608
 
605
609
  if (action === "hover") {
@@ -627,32 +631,31 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
627
631
  const candidateHover = await candidateClient.hover(candidatePath, candidateLine, candidateColumn);
628
632
  const candidateHoverText = formatHover(candidateHover);
629
633
  if (candidateHover && !candidateHoverText.startsWith("No hover information")) {
630
- return { content: [{ type: "text", text: candidateHoverText }] };
634
+ return result(candidateHoverText, "lsp");
631
635
  }
632
636
  }
633
- return {
634
- content: [{ type: "text", text: `[AST Hover] ${candidate.kind} ${candidate.name}\n${candidate.signature}\nDeclared in ${candidate.filePath}:${candidate.line}` }],
635
- };
637
+ return result(
638
+ `[AST Hover] ${candidate.kind} ${candidate.name}\n${candidate.signature}\nDeclared in ${candidate.filePath}:${candidate.line}`,
639
+ "tree-sitter",
640
+ );
636
641
  }
637
642
  if (candidates.length > 1) {
638
- return {
639
- content: [{ type: "text", text: `Multiple declarations found for '${requestedSymbol}':\n${candidates.slice(0, 10).map((candidate) => `- ${candidate.filePath}:${candidate.line} [${candidate.kind}] ${candidate.signature}`).join("\n")}` }],
640
- };
643
+ return result(
644
+ `Multiple declarations found for '${requestedSymbol}':\n${candidates.slice(0, 10).map((candidate) => `- ${candidate.filePath}:${candidate.line} [${candidate.kind}] ${candidate.signature}`).join("\n")}`,
645
+ "tree-sitter",
646
+ { count: candidates.length },
647
+ );
641
648
  }
642
649
  }
643
650
  const h = await client.hover(absPath, line0, col0);
644
651
  const hoverText = formatHover(h);
645
652
  if (h && !hoverText.startsWith("No hover information")) {
646
- return {
647
- content: [{ type: "text", text: hoverText }],
648
- };
653
+ return result(hoverText, "lsp");
649
654
  }
650
655
  const ext = path.extname(absPath).toLowerCase();
651
656
  const nonCodeExts = [".md", ".markdown", ".json", ".jsonc", ".yaml", ".yml", ".toml", ".proto", ".txt"];
652
657
  if (nonCodeExts.includes(ext)) {
653
- return {
654
- content: [{ type: "text", text: hoverText }],
655
- };
658
+ return result(hoverText, "lsp");
656
659
  }
657
660
 
658
661
  // Tree-sitter local variable / parameter / symbol hover fallback
@@ -666,7 +669,7 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
666
669
  targetSym,
667
670
  );
668
671
  if (localHover) {
669
- return { content: [{ type: "text", text: localHover }] };
672
+ return result(localHover, "tree-sitter");
670
673
  }
671
674
  const astHits = searchAstSymbols(ctx.cwd, {
672
675
  name: targetSym,
@@ -674,116 +677,79 @@ export function registerLspTool(pi: ExtensionAPI, deps?: SessionDeps): void {
674
677
  });
675
678
  if (astHits.length > 0) {
676
679
  const hit = astHits[0];
677
- return {
678
- content: [
679
- {
680
- type: "text",
681
- text: `[Tree-sitter AST Hover - ${hit.kind} ${hit.name}]\n${hit.signature}\nDeclared in ${hit.filePath}:${hit.line}`,
682
- },
683
- ],
684
- };
680
+ return result(
681
+ `[Tree-sitter AST Hover - ${hit.kind} ${hit.name}]\n${hit.signature}\nDeclared in ${hit.filePath}:${hit.line}`,
682
+ "tree-sitter",
683
+ );
685
684
  }
686
685
  }
687
686
 
688
- return {
689
- content: [{ type: "text", text: hoverText }],
690
- };
687
+ return result(hoverText, "lsp");
691
688
  }
692
689
 
693
690
  if (action === "document_symbols") {
694
691
  const syms = await client.documentSymbol(absPath);
695
692
  if (syms.length > 0) {
696
- return {
697
- content: [{ type: "text", text: formatDocumentSymbols(syms) }],
698
- };
693
+ return result(formatDocumentSymbols(syms), "lsp", { count: syms.length });
699
694
  }
700
695
  const ext = path.extname(absPath).toLowerCase();
701
696
  const nonCodeExts = [".md", ".markdown", ".json", ".jsonc", ".yaml", ".yml", ".toml", ".proto", ".txt"];
702
697
  if (nonCodeExts.includes(ext)) {
703
- return {
704
- content: [{ type: "text", text: formatDocumentSymbols(syms) }],
705
- };
698
+ return result(formatDocumentSymbols(syms), "lsp", { count: syms.length });
706
699
  }
707
700
  const docSyms = extractDocumentSymbols(absPath);
708
701
  if (docSyms.length > 0) {
709
702
  const formatted = docSyms
710
703
  .map((s) => `• [${s.kind}] ${s.name} (line ${s.line}) - ${s.signature}`)
711
704
  .join("\n");
712
- return {
713
- content: [
714
- {
715
- type: "text",
716
- text: `[Tree-sitter AST Document Symbols - ${docSyms.length} symbol(s)]\n${formatted}`,
717
- },
718
- ],
719
- };
705
+ return result(
706
+ `[Tree-sitter AST Document Symbols - ${docSyms.length} symbol(s)]\n${formatted}`,
707
+ "tree-sitter",
708
+ { count: docSyms.length },
709
+ );
720
710
  }
721
- return {
722
- content: [
723
- {
724
- type: "text",
725
- text: `[Tree-sitter AST] No symbols found in ${path.basename(absPath)}.`,
726
- },
727
- ],
728
- };
711
+ return result(
712
+ `[Tree-sitter AST] No symbols found in ${path.basename(absPath)}.`,
713
+ "tree-sitter",
714
+ { count: 0 },
715
+ );
729
716
  }
730
717
 
731
718
  if (action === "diagnostics") {
732
719
  const diagnosticResult = await client.getDiagnosticsResult(absPath);
733
720
  if (diagnosticResult.diagnostics.length > 0) {
734
- return {
735
- content: [
736
- { type: "text", text: formatDiagnostics(diagnosticResult.diagnostics, absPath, ctx.cwd) },
737
- ],
738
- };
721
+ return result(
722
+ formatDiagnostics(diagnosticResult.diagnostics, absPath, ctx.cwd),
723
+ "lsp",
724
+ { status: diagnosticResult.status, count: diagnosticResult.diagnostics.length },
725
+ );
739
726
  }
740
727
 
741
728
  const syn = checkSyntax(absPath);
742
729
  if (!syn.valid) {
743
730
  const status = syn.status || "failed";
744
- return {
745
- content: [
746
- {
747
- type: "text",
748
- text: `- [1:1] [${status.toUpperCase()}] Syntax validation: ${syn.error}`,
749
- },
750
- ],
751
- };
731
+ return result(
732
+ `- [1:1] [${status.toUpperCase()}] Syntax validation: ${syn.error}`,
733
+ "syntax-check",
734
+ { status },
735
+ );
752
736
  }
753
737
 
754
738
  if (diagnosticResult.status !== "clean") {
755
- return {
756
- content: [
757
- {
758
- type: "text",
759
- text: `[LSP ${diagnosticResult.status.toUpperCase()}] No definitive diagnostics result for ${path.relative(ctx.cwd, absPath)} (syntax validation passed).`,
760
- },
761
- ],
762
- };
739
+ return result(
740
+ `[LSP ${diagnosticResult.status.toUpperCase()}] No definitive diagnostics result for ${path.relative(ctx.cwd, absPath)} (syntax validation passed).`,
741
+ "lsp",
742
+ { status: diagnosticResult.status, count: 0 },
743
+ );
763
744
  }
764
745
 
765
746
  const relPath = path.relative(ctx.cwd, absPath).replace(/\\/g, "/") || absPath;
766
- return {
767
- content: [
768
- {
769
- type: "text",
770
- text: `${relPath} clean`,
771
- },
772
- ],
773
- };
747
+ return result(`${relPath} clean`, "lsp", { status: "clean", count: 0 });
774
748
  }
775
749
 
776
- return {
777
- content: [
778
- { type: "text", text: `[LSP ERROR] Unsupported action: ${action}` },
779
- ],
780
- isError: true,
781
- };
750
+ return result(`[LSP ERROR] Unsupported action: ${action}`, undefined, { error: "unsupported_action" }, true);
782
751
  } catch (err: any) {
783
- return {
784
- content: [{ type: "text", text: `[LSP ERROR] ${err.message}` }],
785
- isError: true,
786
- };
752
+ return result(`[LSP ERROR] ${err.message}`, undefined, { error: "query_failed" }, true);
787
753
  }
788
754
  },
789
755
  renderCall(args: any, theme: any, context: any) {