MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / formatContextAsMarkdown

Function formatContextAsMarkdown

src/context/formatter.ts:18–98  ·  view source on GitHub ↗
(
  context: TaskContext,
  /**
   * Generated-file test. Defaults to the filename convention alone; the
   * ContextBuilder passes a DB-backed predicate so files flagged by their
   * HEADER at index time (#1500) demote here too.
   */
  isGenerated: (filePath: string) => boolean = isGeneratedFile
)

Source from the content-addressed store, hash-verified

16 * - Code blocks only for key symbols
17 */
18export function formatContextAsMarkdown(
19 context: TaskContext,
20 /**
21 * Generated-file test. Defaults to the filename convention alone; the
22 * ContextBuilder passes a DB-backed predicate so files flagged by their
23 * HEADER at index time (#1500) demote here too.
24 */
25 isGenerated: (filePath: string) => boolean = isGeneratedFile
26): string {
27 const lines: string[] = [];
28
29 // Header with query
30 lines.push('## Code Context\n');
31 lines.push(`**Query:** ${context.query}\n`);
32
33 // Entry points - compact format. Re-sort so generated files (.pb.go,
34 // .pulsar.go, mocks, …) rank LAST — a flow query should lead with the
35 // hand-written implementation, not protobuf scaffolding.
36 const orderedEntries = [...context.entryPoints].sort((a, b) => {
37 const aGen = isGenerated(a.filePath) ? 1 : 0;
38 const bGen = isGenerated(b.filePath) ? 1 : 0;
39 return aGen - bGen;
40 });
41 if (orderedEntries.length > 0) {
42 lines.push('### Entry Points\n');
43 for (const node of orderedEntries) {
44 const location = node.startLine ? `:${node.startLine}` : '';
45 lines.push(`- **${node.name}** (${node.kind}) - ${node.filePath}${location}`);
46 if (node.signature) {
47 lines.push(` \`${node.signature}\``);
48 }
49 }
50 lines.push('');
51 }
52
53 // Related symbols - compact list (skip verbose structure tree). Drop nodes
54 // in generated source files (`.pb.go` / `.pulsar.go` / mocks / …) — agents
55 // chasing a flow never want to land on protobuf scaffolding (cosmos-Q3 used
56 // to list `gov.pulsar.go::GetExpeditedThreshold` and `1.pulsar.go::Get` in
57 // Related Symbols, pure noise that displaced real-flow entries).
58 const otherSymbols = Array.from(context.subgraph.nodes.values())
59 .filter(n => !context.entryPoints.some(e => e.id === n.id))
60 .filter(n => !isGenerated(n.filePath))
61 .slice(0, 10); // Limit to 10 related symbols
62
63 if (otherSymbols.length > 0) {
64 lines.push('### Related Symbols\n');
65 const byFile = new Map<string, Node[]>();
66 for (const node of otherSymbols) {
67 const existing = byFile.get(node.filePath) || [];
68 existing.push(node);
69 byFile.set(node.filePath, existing);
70 }
71
72 for (const [file, nodes] of byFile) {
73 const nodeList = nodes.map(n => `${n.name}:${n.startLine}`).join(', ');
74 lines.push(`- ${file}: ${nodeList}`);
75 }

Callers 1

buildContextMethod · 0.90

Calls 6

pushMethod · 0.80
fromMethod · 0.80
mapMethod · 0.80
getMethod · 0.65
setMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected