|
| 1 | +package docs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "go/ast" |
| 7 | + "go/parser" |
| 8 | + "go/token" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "golang.org/x/text/cases" |
| 14 | + "golang.org/x/text/language" |
| 15 | + |
| 16 | + "chuchu/internal/langdetect" |
| 17 | + "chuchu/internal/llm" |
| 18 | +) |
| 19 | + |
| 20 | +type APIEndpoint struct { |
| 21 | + Method string |
| 22 | + Path string |
| 23 | + Handler string |
| 24 | + Description string |
| 25 | + Params []string |
| 26 | + Returns string |
| 27 | + File string |
| 28 | + Line int |
| 29 | +} |
| 30 | + |
| 31 | +type APIDocGenerator struct { |
| 32 | + provider llm.Provider |
| 33 | + model string |
| 34 | + workDir string |
| 35 | +} |
| 36 | + |
| 37 | +func NewAPIDocGenerator(provider llm.Provider, model, workDir string) *APIDocGenerator { |
| 38 | + return &APIDocGenerator{ |
| 39 | + provider: provider, |
| 40 | + model: model, |
| 41 | + workDir: workDir, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (g *APIDocGenerator) Generate(ctx context.Context, format string) (string, error) { |
| 46 | + lang := langdetect.DetectLanguage(g.workDir) |
| 47 | + |
| 48 | + endpoints, err := g.discoverEndpoints(lang) |
| 49 | + if err != nil { |
| 50 | + return "", fmt.Errorf("failed to discover endpoints: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + if len(endpoints) == 0 { |
| 54 | + return "", fmt.Errorf("no API endpoints found") |
| 55 | + } |
| 56 | + |
| 57 | + doc, err := g.generateDocumentation(ctx, endpoints, format) |
| 58 | + if err != nil { |
| 59 | + return "", err |
| 60 | + } |
| 61 | + |
| 62 | + filename := g.getOutputFilename(format) |
| 63 | + if err := os.WriteFile(filename, []byte(doc), 0644); err != nil { |
| 64 | + return "", fmt.Errorf("failed to write documentation: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + return filename, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (g *APIDocGenerator) discoverEndpoints(lang langdetect.Language) ([]APIEndpoint, error) { |
| 71 | + var endpoints []APIEndpoint |
| 72 | + |
| 73 | + err := filepath.Walk(g.workDir, func(path string, info os.FileInfo, err error) error { |
| 74 | + if err != nil || info.IsDir() { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + if strings.Contains(path, "vendor/") || strings.Contains(path, "node_modules/") { |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + switch lang { |
| 83 | + case langdetect.Go: |
| 84 | + if strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go") { |
| 85 | + eps, _ := g.parseGoFile(path) |
| 86 | + endpoints = append(endpoints, eps...) |
| 87 | + } |
| 88 | + case langdetect.TypeScript: |
| 89 | + if strings.HasSuffix(path, ".ts") || strings.HasSuffix(path, ".js") { |
| 90 | + eps, _ := g.parseTypeScriptFile(path) |
| 91 | + endpoints = append(endpoints, eps...) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | + }) |
| 97 | + |
| 98 | + return endpoints, err |
| 99 | +} |
| 100 | + |
| 101 | +func (g *APIDocGenerator) parseGoFile(path string) ([]APIEndpoint, error) { |
| 102 | + content, err := os.ReadFile(path) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + fset := token.NewFileSet() |
| 108 | + node, err := parser.ParseFile(fset, path, content, parser.ParseComments) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + var endpoints []APIEndpoint |
| 114 | + httpMethods := []string{"GET", "POST", "PUT", "DELETE", "PATCH"} |
| 115 | + |
| 116 | + ast.Inspect(node, func(n ast.Node) bool { |
| 117 | + call, ok := n.(*ast.CallExpr) |
| 118 | + if !ok { |
| 119 | + return true |
| 120 | + } |
| 121 | + |
| 122 | + sel, ok := call.Fun.(*ast.SelectorExpr) |
| 123 | + if !ok { |
| 124 | + return true |
| 125 | + } |
| 126 | + |
| 127 | + method := strings.ToUpper(sel.Sel.Name) |
| 128 | + if !contains(httpMethods, method) { |
| 129 | + return true |
| 130 | + } |
| 131 | + |
| 132 | + if len(call.Args) < 2 { |
| 133 | + return true |
| 134 | + } |
| 135 | + |
| 136 | + pathLit, ok := call.Args[0].(*ast.BasicLit) |
| 137 | + if !ok { |
| 138 | + return true |
| 139 | + } |
| 140 | + |
| 141 | + handlerIdent, ok := call.Args[1].(*ast.Ident) |
| 142 | + if !ok { |
| 143 | + return true |
| 144 | + } |
| 145 | + |
| 146 | + endpoint := APIEndpoint{ |
| 147 | + Method: method, |
| 148 | + Path: strings.Trim(pathLit.Value, "\""), |
| 149 | + Handler: handlerIdent.Name, |
| 150 | + File: path, |
| 151 | + Line: fset.Position(call.Pos()).Line, |
| 152 | + } |
| 153 | + |
| 154 | + endpoints = append(endpoints, endpoint) |
| 155 | + return true |
| 156 | + }) |
| 157 | + |
| 158 | + return endpoints, nil |
| 159 | +} |
| 160 | + |
| 161 | +func (g *APIDocGenerator) parseTypeScriptFile(path string) ([]APIEndpoint, error) { |
| 162 | + content, err := os.ReadFile(path) |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + |
| 167 | + var endpoints []APIEndpoint |
| 168 | + lines := strings.Split(string(content), "\n") |
| 169 | + |
| 170 | + httpMethods := []string{"get", "post", "put", "delete", "patch"} |
| 171 | + c := cases.Title(language.English) |
| 172 | + |
| 173 | + for i, line := range lines { |
| 174 | + for _, method := range httpMethods { |
| 175 | + if strings.Contains(line, fmt.Sprintf(".%s(", method)) || |
| 176 | + strings.Contains(line, fmt.Sprintf("@%s(", c.String(method))) { |
| 177 | + |
| 178 | + pathStart := strings.Index(line, "\"") |
| 179 | + if pathStart == -1 { |
| 180 | + pathStart = strings.Index(line, "'") |
| 181 | + } |
| 182 | + if pathStart == -1 { |
| 183 | + continue |
| 184 | + } |
| 185 | + |
| 186 | + remaining := line[pathStart+1:] |
| 187 | + pathEnd := strings.IndexAny(remaining, "\"'") |
| 188 | + if pathEnd == -1 { |
| 189 | + continue |
| 190 | + } |
| 191 | + |
| 192 | + endpoint := APIEndpoint{ |
| 193 | + Method: strings.ToUpper(method), |
| 194 | + Path: remaining[:pathEnd], |
| 195 | + File: path, |
| 196 | + Line: i + 1, |
| 197 | + } |
| 198 | + endpoints = append(endpoints, endpoint) |
| 199 | + break |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return endpoints, nil |
| 205 | +} |
| 206 | + |
| 207 | +func (g *APIDocGenerator) generateDocumentation(ctx context.Context, endpoints []APIEndpoint, format string) (string, error) { |
| 208 | + endpointList := g.formatEndpointList(endpoints) |
| 209 | + |
| 210 | + var formatInstruction string |
| 211 | + switch format { |
| 212 | + case "openapi": |
| 213 | + formatInstruction = "Generate OpenAPI 3.0 specification in YAML format" |
| 214 | + case "markdown": |
| 215 | + formatInstruction = "Generate Markdown documentation with clear sections" |
| 216 | + case "postman": |
| 217 | + formatInstruction = "Generate Postman Collection v2.1 in JSON format" |
| 218 | + default: |
| 219 | + formatInstruction = "Generate Markdown documentation" |
| 220 | + } |
| 221 | + |
| 222 | + prompt := fmt.Sprintf(`Generate API documentation for these endpoints: |
| 223 | +
|
| 224 | +%s |
| 225 | +
|
| 226 | +%s. |
| 227 | +
|
| 228 | +Include: |
| 229 | +- Full endpoint details |
| 230 | +- Request/response examples |
| 231 | +- Authentication requirements (if detected) |
| 232 | +- Error responses |
| 233 | +
|
| 234 | +Return ONLY the documentation, no explanations.`, endpointList, formatInstruction) |
| 235 | + |
| 236 | + resp, err := g.provider.Chat(ctx, llm.ChatRequest{ |
| 237 | + UserPrompt: prompt, |
| 238 | + Model: g.model, |
| 239 | + }) |
| 240 | + |
| 241 | + if err != nil { |
| 242 | + return "", err |
| 243 | + } |
| 244 | + |
| 245 | + return g.extractDoc(resp.Text, format), nil |
| 246 | +} |
| 247 | + |
| 248 | +func (g *APIDocGenerator) formatEndpointList(endpoints []APIEndpoint) string { |
| 249 | + var builder strings.Builder |
| 250 | + |
| 251 | + for i, ep := range endpoints { |
| 252 | + builder.WriteString(fmt.Sprintf("%d. %s %s\n", i+1, ep.Method, ep.Path)) |
| 253 | + if ep.Handler != "" { |
| 254 | + builder.WriteString(fmt.Sprintf(" Handler: %s\n", ep.Handler)) |
| 255 | + } |
| 256 | + builder.WriteString(fmt.Sprintf(" Location: %s:%d\n", ep.File, ep.Line)) |
| 257 | + builder.WriteString("\n") |
| 258 | + } |
| 259 | + |
| 260 | + return builder.String() |
| 261 | +} |
| 262 | + |
| 263 | +func (g *APIDocGenerator) getOutputFilename(format string) string { |
| 264 | + switch format { |
| 265 | + case "openapi": |
| 266 | + return filepath.Join(g.workDir, "api-spec.yaml") |
| 267 | + case "postman": |
| 268 | + return filepath.Join(g.workDir, "api-collection.json") |
| 269 | + default: |
| 270 | + return filepath.Join(g.workDir, "API.md") |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +func (g *APIDocGenerator) extractDoc(text, format string) string { |
| 275 | + text = strings.TrimSpace(text) |
| 276 | + |
| 277 | + markers := map[string][]string{ |
| 278 | + "openapi": {"```yaml", "```yml", "```"}, |
| 279 | + "postman": {"```json", "```"}, |
| 280 | + "markdown": {"```markdown", "```md", "```"}, |
| 281 | + } |
| 282 | + |
| 283 | + if prefixes, ok := markers[format]; ok { |
| 284 | + for _, marker := range prefixes { |
| 285 | + if strings.HasPrefix(text, marker) { |
| 286 | + text = strings.TrimPrefix(text, marker) |
| 287 | + text = strings.TrimPrefix(text, "\n") |
| 288 | + text = strings.TrimSuffix(text, "```") |
| 289 | + break |
| 290 | + } |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + return strings.TrimSpace(text) |
| 295 | +} |
| 296 | + |
| 297 | +func contains(slice []string, item string) bool { |
| 298 | + for _, s := range slice { |
| 299 | + if strings.EqualFold(s, item) { |
| 300 | + return true |
| 301 | + } |
| 302 | + } |
| 303 | + return false |
| 304 | +} |
0 commit comments