(args map[string]any, defaultPath string)
| 92 | } |
| 93 | |
| 94 | func parseCommentsInner(args map[string]any, defaultPath string) ([]model.LlmComment, *CommentRepair, string) { |
| 95 | var rawComments []any |
| 96 | var repair *CommentRepair |
| 97 | if arr, ok := args["comments"].([]any); ok && len(arr) > 0 { |
| 98 | rawComments = arr |
| 99 | } else if s, ok := args["comments"].(string); ok && s != "" { |
| 100 | if err := json.Unmarshal([]byte(s), &rawComments); err != nil { |
| 101 | // A string here is a schema violation that in practice fails to |
| 102 | // parse and takes the whole batch with it. Try the repair first. |
| 103 | entries, rep := parseRepairedComments(s) |
| 104 | if entries == nil { |
| 105 | // Keep "invalid character" as the parser produced it: that |
| 106 | // phrasing is what leads the model to regenerate the batch, |
| 107 | // while naming the schema violation instead makes it resend the |
| 108 | // same broken string. |
| 109 | return nil, nil, fmt.Sprintf("Error: failed to parse 'comments' JSON string: %v", err) |
| 110 | } |
| 111 | rawComments = entries |
| 112 | repair = rep |
| 113 | } |
| 114 | } |
| 115 | if len(rawComments) == 0 { |
| 116 | raw, _ := json.Marshal(args) |
| 117 | return nil, nil, fmt.Sprintf("Error: 'comments' array is required. Got args: %s", string(raw)) |
| 118 | } |
| 119 | |
| 120 | var comments []model.LlmComment |
| 121 | for _, raw := range rawComments { |
| 122 | obj, ok := raw.(map[string]any) |
| 123 | if !ok { |
| 124 | continue |
| 125 | } |
| 126 | |
| 127 | cm := model.LlmComment{} |
| 128 | |
| 129 | if content, ok := obj["content"].(string); ok { |
| 130 | cm.Content = content |
| 131 | } |
| 132 | if suggestion, ok := obj["suggestion_code"].(string); ok { |
| 133 | cm.SuggestionCode = suggestion |
| 134 | } |
| 135 | if existing, ok := obj["existing_code"].(string); ok { |
| 136 | cm.ExistingCode = existing |
| 137 | } |
| 138 | if thinking, ok := obj["thinking"].(string); ok { |
| 139 | cm.Thinking = thinking |
| 140 | } |
| 141 | if category, ok := obj["category"].(string); ok { |
| 142 | cm.Category = normalizeCodeCommentCategory(category) |
| 143 | } |
| 144 | if severity, ok := obj["severity"].(string); ok { |
| 145 | cm.Severity = normalizeCodeCommentSeverity(severity) |
| 146 | } |
| 147 | if path, ok := obj["path"].(string); ok && path != "" { |
| 148 | cm.Path = path |
| 149 | } |
| 150 | if cm.Path == "" { |
| 151 | cm.Path = defaultPath |
no test coverage detected