Skip to content

Commit 0e1e2fc

Browse files
committed
fix(llm): only query llm with necessary lint result
1 parent f67c1f6 commit 0e1e2fc

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

deploy/reviewbot.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ spec:
3434
- -kube-config=/root/.kube/config
3535
- -llm.provider=ollama
3636
- -llm.server-url=$(LLM_SERVER_URL)
37-
- -llm.model=qwen2.5-coder:7
37+
- -llm.model=qwen2.5-coder:7b
3838
env:
3939
- name: GITHUB_WEBHOOK_SECRET
4040
valueFrom:

internal/lint/agent.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,43 +124,56 @@ func (a *Agent) processOutput(ctx context.Context, output LinterOutput, ref conf
124124
return newOutput, true
125125
}
126126

127-
// ApplyTypedMessageByIssueReferences applies the issue references to the lint results with the typed message.
128-
func (a *Agent) ApplyTypedMessageByIssueReferences(ctx context.Context, lintResults map[string][]LinterOutput) map[string][]LinterOutput {
129-
log := util.FromContext(ctx)
127+
// EnrichWithIssueReferences applies the issue references to the lint results with the typed message.
128+
func (a *Agent) EnrichWithIssueReferences(ctx context.Context, lintResults map[string][]LinterOutput) map[string][]LinterOutput {
130129
msgFormat := getMsgFormat(a.LinterConfig.ReportType)
131-
newLintResults := make(map[string][]LinterOutput, len(lintResults))
130+
enriched := make(map[string][]LinterOutput, len(lintResults))
132131

133132
// Process each file's outputs
134133
for file, outputs := range lintResults {
135-
newOutputs := make([]LinterOutput, 0, len(outputs))
134+
newOutputs := append([]LinterOutput(nil), outputs...)
136135

137136
// Apply each reference pattern
138-
for _, output := range outputs {
139-
processed := false
137+
for i, output := range outputs {
140138
for _, ref := range a.IssueReferences {
141139
if newOutput, ok := a.processOutput(ctx, output, ref, msgFormat); ok {
142-
newOutputs = append(newOutputs, newOutput)
143-
processed = true
140+
newOutputs[i] = newOutput
144141
break
145142
}
146143
}
147-
if !processed {
144+
}
145+
146+
if len(newOutputs) > 0 {
147+
enriched[file] = newOutputs
148+
}
149+
}
150+
return enriched
151+
}
152+
153+
// EnrichWithLLM enriches the lint results with LLM generated content.
154+
func (a *Agent) EnrichWithLLM(ctx context.Context, lintResults map[string][]LinterOutput) map[string][]LinterOutput {
155+
log := util.FromContext(ctx)
156+
enriched := make(map[string][]LinterOutput, len(lintResults))
157+
158+
for file, outputs := range lintResults {
159+
newOutputs := append([]LinterOutput(nil), outputs...)
160+
161+
for i, output := range outputs {
162+
// only enrich the output that is not typed
163+
if output.TypedMessage == "" {
148164
resp, err := llm.QueryForReference(ctx, a.ModelClient, output.Message)
149165
if err != nil {
150166
log.Errorf("failed to query LLM server: %v", err)
151167
} else {
152-
output.TypedMessage = output.Message + fmt.Sprintf(ReferenceFooter, resp)
168+
newOutputs[i].TypedMessage = newOutputs[i].Message + fmt.Sprintf(ReferenceFooter, resp)
153169
}
154-
newOutputs = append(newOutputs, output)
155170
}
156171
}
157-
158172
if len(newOutputs) > 0 {
159-
newLintResults[file] = newOutputs
173+
enriched[file] = newOutputs
160174
}
161175
}
162-
163-
return newLintResults
176+
return enriched
164177
}
165178

166179
const ReferenceFooter = `

internal/lint/agent_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func TestApplyTypedMessageByIssueReferences(t *testing.T) {
200200
ctx := context.Background()
201201

202202
// Call the method
203-
result := agent.ApplyTypedMessageByIssueReferences(ctx, tc.lintResults)
203+
result := agent.EnrichWithIssueReferences(ctx, tc.lintResults)
204204

205205
// Compare results
206206
if !reflect.DeepEqual(result, tc.expectedOutput) {

internal/lint/lint.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ func Report(ctx context.Context, a Agent, lintResults map[string][]LinterOutput)
194194

195195
log.Infof("[%s] found %d files with valid %d linter errors related to this PR %d (%s) \n", linterName, len(lintResults), countLinterErrors(lintResults), num, orgRepo)
196196

197-
lintResults = a.ApplyTypedMessageByIssueReferences(ctx, lintResults)
198-
197+
lintResults = a.EnrichWithIssueReferences(ctx, lintResults)
199198
if len(lintResults) > 0 {
200199
metric.IncIssueCounter(orgRepo, linterName, a.Provider.GetCodeReviewInfo().URL, a.Provider.GetCodeReviewInfo().HeadSHA, float64(countLinterErrors(lintResults)))
201200
}

internal/lint/providergithub.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ func (g *GithubProvider) Report(ctx context.Context, a Agent, lintResults map[st
343343

344344
metric.NotifyWebhookByText(ConstructGotchaMsg(linterName, a.Provider.GetCodeReviewInfo().URL, ch.GetHTMLURL(), lintResults))
345345
case config.GitHubPRReview:
346+
lintResults = a.EnrichWithLLM(ctx, lintResults)
346347
comments, err := g.ProcessComments(ctx, a, lintResults)
347348
if err != nil {
348349
log.Errorf("failed to process need to add comments: %v", err)
@@ -374,6 +375,7 @@ func (g *GithubProvider) Report(ctx context.Context, a Agent, lintResults map[st
374375

375376
// report top 10 lint results to pull request review comments at most
376377
top10LintResults := listTop10LintResults(lintResults)
378+
top10LintResults = a.EnrichWithLLM(ctx, top10LintResults)
377379
comments, err := g.ProcessComments(ctx, a, top10LintResults)
378380
if err != nil {
379381
log.Errorf("failed to process need to add comments: %v", err)

0 commit comments

Comments
 (0)