Skip to content

Commit

Permalink
Return kubectl error for interpretation, escape % char, enable debug (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok authored Mar 4, 2024
1 parent f592d74 commit 020883e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
10 changes: 7 additions & 3 deletions internal/source/ai-brain/assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/kubeshop/botkube-cloud-plugins/internal/remote"
Expand Down Expand Up @@ -127,11 +128,12 @@ func (i *assistant) handleThread(ctx context.Context, p *Payload) error {

i.cache.Set(p.MessageID, threadID)

i.log.WithFields(logrus.Fields{
log := i.log.WithFields(logrus.Fields{
"messageId": p.MessageID,
"threadId": threadID,
"prompt": p.Prompt,
}).Info("created a new assistant run")
})
log.Info("created a new assistant run")
run, err := i.openaiClient.CreateRun(ctx, threadID, openai.RunRequest{
AssistantID: i.assistID,
})
Expand All @@ -143,6 +145,7 @@ func (i *assistant) handleThread(ctx context.Context, p *Payload) error {
return wait.PollUntilContextCancel(ctx, openAIPollInterval, false, func(ctx context.Context) (bool, error) {
run, err = i.openaiClient.RetrieveRun(ctx, run.ThreadID, run.ID)
if err != nil {
log.WithError(err).Error("failed to retrieve assistant thread run")
return false, fmt.Errorf("while retrieving assistant thread run: %w", err)
}

Expand All @@ -153,7 +156,7 @@ func (i *assistant) handleThread(ctx context.Context, p *Payload) error {

switch run.Status {
case openai.RunStatusCancelling, openai.RunStatusFailed:
return false, fmt.Errorf("got unexpected status: %s", run.Status)
return true, fmt.Errorf("got unexpected status: %s", run.Status)

case openai.RunStatusExpired:
i.cache.Delete(p.MessageID)
Expand Down Expand Up @@ -226,6 +229,7 @@ func (i *assistant) handleStatusCompleted(ctx context.Context, run openai.Run, p
continue
}

c.Text.Value = strings.ReplaceAll(c.Text.Value, "%", "%%")
i.out <- source.Event{
Message: msgAIAnswer(p.MessageID, c.Text.Value),
}
Expand Down
2 changes: 1 addition & 1 deletion internal/source/ai-brain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func mergeConfigs(configs []*source.Config) (*Config, error) {
defaults := &Config{
OpenAIAssistantID: assistantID,
Log: config.Logger{
Level: "info",
Level: "debug",
Formatter: "json",
},
}
Expand Down
5 changes: 5 additions & 0 deletions internal/source/ai-brain/kubectl_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func (k *KubectlRunner) runKubectlCommand(ctx context.Context, cmd, ns string) (

cmd = fmt.Sprintf("%s %s", kubectlBinaryName, cmd)
out, err := plugin.ExecuteCommand(ctx, cmd, plugin.ExecuteCommandEnvs(envs))

if out.ExitCode != 0 {
return fmt.Sprintf("kubectl command failed: %s", color.ClearCode(out.CombinedOutput())), nil
}

if err != nil {
return "", err
}
Expand Down

0 comments on commit 020883e

Please sign in to comment.