Skip to content

Commit

Permalink
clean up more AI output (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus authored Apr 25, 2024
1 parent a556dce commit 7bdb545
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
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)
})
}

0 comments on commit 7bdb545

Please sign in to comment.