ValidateCodeResponse checks if a code response has basic quality signals.
(text string)
| 407 | |
| 408 | // ValidateCodeResponse checks if a code response has basic quality signals. |
| 409 | func ValidateCodeResponse(text string) CodeQuality { |
| 410 | q := CodeQuality{ |
| 411 | HasCode: false, |
| 412 | LinesOfCode: 0, |
| 413 | LinesOfComment: 0, |
| 414 | HasImports: false, |
| 415 | IsComplete: false, |
| 416 | } |
| 417 | |
| 418 | codeBlockRe := regexp.MustCompile("(?s)```\\w*\n(.*?)```") |
| 419 | matches := codeBlockRe.FindAllStringSubmatch(text, -1) |
| 420 | |
| 421 | if len(matches) == 0 { |
| 422 | return q |
| 423 | } |
| 424 | |
| 425 | q.HasCode = true |
| 426 | |
| 427 | for _, match := range matches { |
| 428 | code := match[1] |
| 429 | lines := strings.Split(code, "\n") |
| 430 | for _, line := range lines { |
| 431 | trimmed := strings.TrimSpace(line) |
| 432 | if trimmed == "" { |
| 433 | continue |
| 434 | } |
| 435 | if strings.HasPrefix(trimmed, "//") || strings.HasPrefix(trimmed, "#") || |
| 436 | strings.HasPrefix(trimmed, "/*") || strings.HasPrefix(trimmed, "*") { |
| 437 | q.LinesOfComment++ |
| 438 | } else { |
| 439 | q.LinesOfCode++ |
| 440 | } |
| 441 | if strings.HasPrefix(trimmed, "import") || strings.HasPrefix(trimmed, "from") || |
| 442 | strings.HasPrefix(trimmed, "require") || strings.HasPrefix(trimmed, "use") || |
| 443 | strings.HasPrefix(trimmed, "#include") { |
| 444 | q.HasImports = true |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // Simple completeness check: code has a function/class definition and closing brace |
| 450 | fullCode := text |
| 451 | if strings.Contains(fullCode, "func ") || strings.Contains(fullCode, "def ") || |
| 452 | strings.Contains(fullCode, "function ") || strings.Contains(fullCode, "class ") { |
| 453 | if strings.Contains(fullCode, "}") || strings.Contains(fullCode, "return") { |
| 454 | q.IsComplete = true |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Calculate comment ratio |
| 459 | total := q.LinesOfCode + q.LinesOfComment |
| 460 | if total > 0 { |
| 461 | q.CommentRatio = float64(q.LinesOfComment) / float64(total) |
| 462 | } |
| 463 | |
| 464 | return q |
| 465 | } |
| 466 |
no outgoing calls