Skip to content

Commit

Permalink
cmd/ask: allow users to attach historical context to their questions.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed Aug 5, 2024
1 parent bbd0594 commit 0a87e52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ type QuestionInfo struct {
FileData []byte `json:"file_data,omitempty"`
FileName string `json:"file_name,omitempty"`
PreviousQuestions []string `json:"previous_questions,omitempty"`
PreviousCommands []string `json:"previous_commands,omitempty"`
}

func (c *client) Ask(ctx context.Context, question QuestionInfo) (*Runbook, error) {
Expand Down
24 changes: 20 additions & 4 deletions cmd/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/getsavvyinc/savvy-cli/cmd/component"
"github.com/getsavvyinc/savvy-cli/cmd/component/list"
"github.com/getsavvyinc/savvy-cli/display"
"github.com/getsavvyinc/savvy-cli/server"
"github.com/getsavvyinc/savvy-cli/slice"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,17 +65,27 @@ var askCmd = &cobra.Command{
os.Exit(1)
}

var historyCmds []*server.RecordedCommand
if useHistory {
historyCmds, err = selectAndExpandHistory(ctx, logger)
if err != nil {
display.FatalErrWithSupportCTA(err)
return
}
}

var question string
if len(args) > 0 {
// be defensive: users can pass questions as one string or multiple strings
question = strings.Join(args[:], " ")
}

params := &AskParams{
goos: goos,
fileData: fileData,
filePath: filePath,
refine: false,
goos: goos,
fileData: fileData,
filePath: filePath,
refine: false,
previousCommands: historyCmds,
}

var state *runAskTerminalState
Expand Down Expand Up @@ -165,6 +177,7 @@ type AskParams struct {
filePath string
refine bool
previousQuestions []string
previousCommands []*server.RecordedCommand
}

type runAskTerminalState struct {
Expand Down Expand Up @@ -204,6 +217,7 @@ func runAsk(ctx context.Context, cl client.Client, question string, askParams *A
FileData: askParams.fileData,
FileName: path.Base(askParams.filePath),
PreviousQuestions: askParams.previousQuestions[:],
PreviousCommands: slice.Map(askParams.previousCommands, func(c *server.RecordedCommand) string { return c.Command }),
}
askParams.previousQuestions = append(askParams.previousQuestions, question)

Expand Down Expand Up @@ -337,8 +351,10 @@ func fileData(filePath string) ([]byte, error) {
}

var filePath string
var useHistory bool

func init() {
rootCmd.AddCommand(askCmd)
askCmd.Flags().StringVarP(&filePath, "file", "f", "", "File path for ask to read and use while generating an answer")
askCmd.Flags().BoolVarP(&useHistory, "history", "h", false, "Provide historical context to Savvy's AI model")
}

0 comments on commit 0a87e52

Please sign in to comment.