62159
Sy
symb / files
symb / internal / treesitter / context.go
3.1 KB
1package treesitter 2 3import ( 4 "fmt" 5 "sort" 6 "strings" 7) 8 9// MaxOutlineBytes caps the outline to avoid consuming too much of the LLM 10// context window. ~16KB ≈ 4-5K tokens, enough for ~100 Go files. 11const MaxOutlineBytes = 16 * 1024 12 13// FormatOutline produces a compact YAML-like project outline for LLM system 14// prompt injection. Groups methods by receiver type under each file. 15// Output is capped at MaxOutlineBytes to protect the context window. 16// 17// Example output: 18// 19// # Project Symbols 20// internal/mcp/proxy.go: 21// Proxy: RegisterTool, CallTool, ListTools 22// fn: NewProxy, parseRetryAfter 23// type: ToolHandler 24func FormatOutline(snap map[string][]Symbol) string { 25 if len(snap) == 0 { 26 return "" 27 } 28 29 paths := make([]string, 0, len(snap)) 30 for p := range snap { 31 paths = append(paths, p) 32 } 33 sort.Strings(paths) 34 35 var b strings.Builder 36 b.WriteString("# Project Symbols\n") 37 38 for _, path := range paths { 39 syms := snap[path] 40 text := formatFileCompact(syms) 41 if text == "" { 42 continue 43 } 44 entry := fmt.Sprintf("%s:\n%s", path, text) 45 if b.Len()+len(entry) > MaxOutlineBytes { 46 fmt.Fprintf(&b, "# ... truncated (%d files total)\n", len(paths)) 47 break 48 } 49 b.WriteString(entry) 50 } 51 return b.String() 52} 53 54// fileGroups collects symbols into categories for compact rendering. 55type fileGroups struct { 56 methods map[string][]string // receiver -> method names 57 funcs []string 58 types []string 59 consts []string 60 vars []string 61} 62 63func newFileGroups() *fileGroups { 64 return &fileGroups{methods: make(map[string][]string)} 65} 66 67func (g *fileGroups) add(s Symbol) { 68 switch s.Kind { 69 case KindPackage, KindImport: 70 // skip 71 case KindFunction: 72 g.funcs = append(g.funcs, s.Name) 73 case KindMethod: 74 recv := s.Receiver 75 if recv == "" { 76 recv = "?" 77 } 78 g.methods[recv] = append(g.methods[recv], s.Name) 79 case KindStruct: 80 g.types = append(g.types, s.Name+" (struct)") 81 case KindInterface: 82 g.types = append(g.types, s.Name+" (interface)") 83 case KindType: 84 g.types = append(g.types, s.Name) 85 case KindConst: 86 g.consts = append(g.consts, s.Name) 87 case KindVar: 88 g.vars = append(g.vars, s.Name) 89 } 90} 91 92func (g *fileGroups) empty() bool { 93 return len(g.funcs) == 0 && len(g.methods) == 0 && 94 len(g.types) == 0 && len(g.consts) == 0 && len(g.vars) == 0 95} 96 97func (g *fileGroups) render() string { 98 var b strings.Builder 99 100 if len(g.types) > 0 { 101 fmt.Fprintf(&b, " type: %s\n", strings.Join(g.types, ", ")) 102 } 103 104 recvs := make([]string, 0, len(g.methods)) 105 for r := range g.methods { 106 recvs = append(recvs, r) 107 } 108 sort.Strings(recvs) 109 for _, recv := range recvs { 110 fmt.Fprintf(&b, " %s: %s\n", recv, strings.Join(g.methods[recv], ", ")) 111 } 112 113 if len(g.funcs) > 0 { 114 fmt.Fprintf(&b, " fn: %s\n", strings.Join(g.funcs, ", ")) 115 } 116 if len(g.consts) > 0 { 117 fmt.Fprintf(&b, " const: %s\n", strings.Join(g.consts, ", ")) 118 } 119 if len(g.vars) > 0 { 120 fmt.Fprintf(&b, " var: %s\n", strings.Join(g.vars, ", ")) 121 } 122 123 return b.String() 124} 125 126// formatFileCompact produces a compact per-file representation. 127func formatFileCompact(syms []Symbol) string { 128 g := newFileGroups() 129 for _, s := range syms { 130 g.add(s) 131 } 132 if g.empty() { 133 return "" 134 } 135 return g.render() 136}