Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up more AI output #203

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/argo_client/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

var tracer = otel.Tracer("pkg/argo_client")

var ErrNoVersionFound = errors.New("no kubernetes version found")

// GetApplicationByName takes a context and a name, then queries the Argo Application client to retrieve the Application with the specified name.
// It returns the found Application and any error encountered during the process.
// If successful, the Application client connection is closed before returning.
Expand Down Expand Up @@ -72,6 +74,11 @@ func (argo *ArgoClient) GetKubernetesVersionByApplication(ctx context.Context, a
// cleanup trailing "+"
version = strings.TrimSuffix(version, "+")

version = strings.TrimSpace(version)
if version == "" {
return "", ErrNoVersionFound
}

return version, nil
}

Expand Down
20 changes: 18 additions & 2 deletions pkg/checks/diff/ai_summary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package diff

import (
"regexp"
"strings"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -44,13 +45,28 @@ func aiDiffSummary(ctx context.Context, mrNote *msg.Message, cfg config.ServerCo
mrNote.AddToAppMessage(ctx, name, cr)
}

var codeBlockBegin = regexp.MustCompile("^```(\\w+)")

func cleanUpAiSummary(aiSummary string) string {
aiSummary = strings.TrimSpace(aiSummary)

// occasionally the model thinks it should wrap it in a code block.
// comments do not need this, as they are already rendered as markdown.
aiSummary = strings.TrimPrefix(aiSummary, "```markdown")
aiSummary = strings.TrimSuffix(aiSummary, "```")
for {
newSummary := aiSummary

newSummary = codeBlockBegin.ReplaceAllString(newSummary, "")
newSummary = strings.TrimPrefix(newSummary, "#***")
newSummary = strings.TrimSuffix(newSummary, "```")
newSummary = strings.TrimSuffix(newSummary, "#***")
newSummary = strings.TrimSpace(newSummary)

if newSummary == aiSummary {
break
}

aiSummary = newSummary
}

return strings.TrimSpace(aiSummary)
}
8 changes: 8 additions & 0 deletions pkg/checks/diff/ai_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ func TestCleanUpAiSummary(t *testing.T) {
actual := cleanUpAiSummary(input)
assert.Equal(t, expected, actual)
})

t.Run("weird prefix and suffix", func(t *testing.T) {
input := "```plaintext\n#***\n- Added environment variables FF_TIMESTAMPS and FF_SCRIPT_SECTIONS\n#***"
expected := "- Added environment variables FF_TIMESTAMPS and FF_SCRIPT_SECTIONS"

actual := cleanUpAiSummary(input)
assert.Equal(t, expected, actual)
})
}
Loading