Skip to content

Commit

Permalink
feat: implement summary for suggest (#186)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Crumbaugh <chasecrumbaugh4@gmail.com>
  • Loading branch information
anuraagnalluri and chase-crumbaugh authored Aug 14, 2023
1 parent 19970c3 commit d279299
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 143 deletions.
50 changes: 44 additions & 6 deletions cmd/suggest.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
goerr "errors"
"fmt"
"github.com/manifoldco/promptui"
"github.com/speakeasy-api/openapi-generation/v2/pkg/errors"
"github.com/speakeasy-api/speakeasy/internal/suggestions"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"os"
"strings"
)

Expand All @@ -22,13 +24,16 @@ you must first create an API key via https://app.speakeasyapi.dev and then set t
var severities = fmt.Sprintf("%s, %s, or %s", errors.SeverityError, errors.SeverityWarn, errors.SeverityHint)

func suggestInit() {
suggestCmd.Flags().StringP("schema", "s", "", "path to the OpenAPI document")
suggestCmd.Flags().StringP("schema", "s", "", "path to a directory containing OpenAPI document(s) or a single OpenAPI document itself")
suggestCmd.Flags().BoolP("auto-approve", "a", false, "auto continue through all prompts")
suggestCmd.Flags().StringP("output-file", "o", "", "output the modified file with suggested fixes applied to the specified path")
suggestCmd.Flags().IntP("max-suggestions", "n", -1, "maximum number of llm suggestions to fetch, the default is no limit")
suggestCmd.Flags().StringP("level", "l", "warn", fmt.Sprintf("%s. The minimum level of severity to request suggestions for", severities))
suggestCmd.Flags().BoolP("serial", "", false, "do not parallelize requesting suggestions")
suggestCmd.Flags().StringP("model", "m", "gpt-4-0613", "model to use when making llm suggestions (gpt-4-0613 recommended)")
suggestCmd.Flags().BoolP("summary", "y", false, "show a summary of the remaining validation errors and their counts")
suggestCmd.Flags().IntP("validation-loops", "v", -1, "number of times to run the validation loop, the default is no limit (only used in parallelized implementation)")
suggestCmd.Flags().IntP("num-specs", "c", -1, "number of specs to run suggest on, the default is no limit")
_ = suggestCmd.MarkFlagRequired("schema")
rootCmd.AddCommand(suggestCmd)
}
Expand All @@ -41,6 +46,11 @@ func suggestFixesOpenAPI(cmd *cobra.Command, args []string) error {
return err
}

schemaPathFileInfo, err := os.Stat(schemaPath)
if err != nil {
return err
}

autoApprove, err := cmd.Flags().GetBool("auto-approve")
if err != nil {
return err
Expand All @@ -61,6 +71,15 @@ func suggestFixesOpenAPI(cmd *cobra.Command, args []string) error {
return err
}

if schemaPathFileInfo.IsDir() && outputFile != "" {
return goerr.New("cannot specify an output file when running suggest on a directory of specs")
}

summary, err := cmd.Flags().GetBool("summary")
if err != nil {
return err
}

if outputFile == "" {
fmt.Println(promptui.Styler(promptui.FGWhite, promptui.FGItalic)("Specifying an output file with -o will allow you to automatically apply suggested fixes to the spec"))
fmt.Println()
Expand All @@ -86,20 +105,39 @@ func suggestFixesOpenAPI(cmd *cobra.Command, args []string) error {
OutputFile: outputFile,
Parallelize: !dontParallelize,
Level: severity,
Summary: summary,
}

maxSuggestion, err := cmd.Flags().GetInt("max-suggestions")
maxSuggestions, err := cmd.Flags().GetInt("max-suggestions")
if err != nil {
return err
}

if maxSuggestion != -1 {
suggestionConfig.MaxSuggestions = &maxSuggestion
if maxSuggestions != -1 {
suggestionConfig.MaxSuggestions = &maxSuggestions
}

if err := suggestions.StartSuggest(cmd.Context(), schemaPath, &suggestionConfig, true); err != nil {
rootCmd.SilenceUsage = true
numSpecs, err := cmd.Flags().GetInt("num-specs")
if err != nil {
return err
}

if numSpecs != -1 {
suggestionConfig.NumSpecs = &numSpecs
}

validationLoops, err := cmd.Flags().GetInt("validation-loops")
if err != nil {
return err
}

if validationLoops != -1 {
suggestionConfig.ValidationLoops = &validationLoops
}

err = suggestions.StartSuggest(cmd.Context(), schemaPath, schemaPathFileInfo.IsDir(), &suggestionConfig)
if err != nil {
rootCmd.SilenceUsage = true
return err
}

Expand Down
6 changes: 6 additions & 0 deletions internal/suggestions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func GetSuggestion(
fileType string,
previousSuggestionContext *string,
) (*Suggestion, error) {
openAIKey, err := GetOpenAIKey()
if err != nil {
return nil, err
}

apiKey, err := getSpeakeasyAPIKey()
if err != nil {
return nil, err
Expand All @@ -132,6 +137,7 @@ func GetSuggestion(

req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-session-token", token)
req.Header.Set("x-openai-key", openAIKey)
req.Header.Set("x-api-key", apiKey)
req.Header.Set("x-file-type", fileType)

Expand Down
Loading

0 comments on commit d279299

Please sign in to comment.