Skip to content

Commit

Permalink
cmd/ask: add past questions too
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed Jun 3, 2024
1 parent 63dbdc9 commit 7cd78f2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
22 changes: 9 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@ type Client interface {
GenerateRunbook(ctx context.Context, commands []string) (*GeneratedRunbook, error)
RunbookByID(ctx context.Context, id string) (*Runbook, error)
Runbooks(ctx context.Context) ([]RunbookInfo, error)
Ask(ctx context.Context, question QuestionInfo, history []History) (*Runbook, error)
Ask(ctx context.Context, question QuestionInfo) (*Runbook, error)
Explain(ctx context.Context, code CodeInfo) (<-chan string, error)
StepContentByStepID(ctx context.Context, stepID string) (*StepContent, error)
}

type History struct {
question QuestionInfo
answer *Runbook
}

type RecordedCommand struct {
Command string `json:"command"`
Prompt string `json:"prompt,omitempty"`
Expand Down Expand Up @@ -285,17 +280,18 @@ func (c *client) Runbooks(ctx context.Context) ([]RunbookInfo, error) {
}

type QuestionInfo struct {
Question string `json:"question"`
Tags map[string]string `json:"tags,omitempty"`
FileData []byte `json:"file_data,omitempty"`
FileName string `json:"file_name,omitempty"`
Question string `json:"question"`
Tags map[string]string `json:"tags,omitempty"`
FileData []byte `json:"file_data,omitempty"`
FileName string `json:"file_name,omitempty"`
PreviousQuestions []string `json:"previous_questions,omitempty"`
}

func (c *client) Ask(ctx context.Context, question QuestionInfo, history []History) (*Runbook, error) {
return ask(ctx, c.cl, c.apiURL("/api/v1/public/ask"), question, history)
func (c *client) Ask(ctx context.Context, question QuestionInfo) (*Runbook, error) {
return ask(ctx, c.cl, c.apiURL("/api/v1/public/ask"), question)
}

func ask(ctx context.Context, cl *http.Client, apiURL string, question QuestionInfo, history []History) (*Runbook, error) {
func ask(ctx context.Context, cl *http.Client, apiURL string, question QuestionInfo) (*Runbook, error) {
bs, err := json.Marshal(question)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions client/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func (g *guest) Runbooks(ctx context.Context) ([]RunbookInfo, error) {
return nil, fmt.Errorf("%w: %v", ErrCannotUseGuest, "list runbooks")
}

func (g *guest) Ask(ctx context.Context, question QuestionInfo, history []History) (*Runbook, error) {
return ask(ctx, g.cl, g.apiURL("/api/v1/public/ask"), question, history)
func (g *guest) Ask(ctx context.Context, question QuestionInfo) (*Runbook, error) {
return ask(ctx, g.cl, g.apiURL("/api/v1/public/ask"), question)
}

func (g *guest) Explain(ctx context.Context, code CodeInfo) (<-chan string, error) {
Expand Down
38 changes: 26 additions & 12 deletions cmd/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,23 @@ var askCmd = &cobra.Command{
// be defensive: users can pass questions as one string or multiple strings
question = strings.Join(args[:], " ")
}
var selectedCommand string
var refine bool

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

var selectedCommand string
refine := true
for refine {
for {
selectedCommand, refine = runAsk(ctx, cl, question, params)
if !refine {
break
}
params.refine = true
question = ""
}

if selectedCommand == "" {
Expand All @@ -92,16 +98,22 @@ var askCmd = &cobra.Command{
}

type AskParams struct {
goos string
fileData []byte
filePath string
goos string
fileData []byte
filePath string
refine bool
previousQuestions []string
}

func runAsk(ctx context.Context, cl client.Client, question string, askParams AskParams) (string, bool) {
func runAsk(ctx context.Context, cl client.Client, question string, askParams *AskParams) (string, bool) {
logger := loggerFromCtx(ctx).With("command", "ask", "method", "runAsk")
if len(question) == 0 {
// interactive mode
text := huh.NewText().Title("Ask Savvy a question").Value(&question)
title := "Ask Savvy a question"
if askParams.refine {
title = "Refine your question"
}
text := huh.NewText().Title(title).Value(&question)
form := huh.NewForm(huh.NewGroup(text))
if err := form.Run(); err != nil {
display.ErrorWithSupportCTA(err)
Expand All @@ -119,15 +131,17 @@ func runAsk(ctx context.Context, cl client.Client, question string, askParams As
Tags: map[string]string{
"os": askParams.goos,
},
FileData: askParams.fileData,
FileName: path.Base(askParams.filePath),
FileData: askParams.fileData,
FileName: path.Base(askParams.filePath),
PreviousQuestions: askParams.previousQuestions[:],
}
askParams.previousQuestions = append(askParams.previousQuestions, question)

var runbook *client.Runbook
if err := huhSpinner.New().Title("Savvy is generating an answer for you").Action(func() {
var err error

runbook, err = cl.Ask(ctx, qi, nil)
runbook, err = cl.Ask(ctx, qi)
if err != nil {
display.FatalErrWithSupportCTA(err)
return
Expand Down

0 comments on commit 7cd78f2

Please sign in to comment.