Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disabling of the cache when parsing tools #734

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ init-docs:
docker run --rm --workdir=/docs -v $${PWD}/docs:/docs node:18-buster yarn install

# Ensure docs build without errors. Makes sure generated docs are in-sync with CLI.
validate-docs:
validate-docs: gen-docs
docker run --rm --workdir=/docs -v $${PWD}/docs:/docs node:18-buster yarn build
go run tools/gendocs/main.go
if [ -n "$$(git status --porcelain --untracked-files=no)" ]; then \
git status --porcelain --untracked-files=no; \
echo "Encountered dirty repo!"; \
git diff; \
exit 1 \
;fi

gen-docs:
go run tools/gendocs/main.go
2 changes: 1 addition & 1 deletion pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func New() *cobra.Command {
root,
&Eval{gptscript: root},
&Credential{root: root},
&Parse{},
&Parse{gptscript: root},
&Fmt{},
&Getenv{},
&SDKServer{
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type Parse struct {
PrettyPrint bool `usage:"Indent the json output" short:"p"`
gptscript *GPTScript
}

func (e *Parse) Customize(cmd *cobra.Command) {
Expand All @@ -26,7 +27,7 @@ func locationName(l string) string {
}

func (e *Parse) Run(_ *cobra.Command, args []string) error {
content, err := input.FromLocation(args[0])
content, err := input.FromLocation(args[0], e.gptscript.DisableCache)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func FromFile(file string) (string, error) {
}

// FromLocation takes a string that can be a file path or a URL to a file and returns the content of that file.
func FromLocation(s string) (string, error) {
func FromLocation(s string, disableCache bool) (string, error) {
// Attempt to read the file first, if that fails, try to load the URL. Finally,
// return an error if both fail.
content, err := FromFile(s)
if err != nil {
log.Debugf("failed to read file %s (due to %v) attempting to load the URL...", s, err)
content, err = loader.ContentFromURL(s)
content, err = loader.ContentFromURL(s, disableCache)
if err != nil {
return "", err
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/loader/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ func getWithDefaults(req *http.Request) ([]byte, string, error) {
panic("unreachable")
}

func ContentFromURL(url string) (string, error) {
cache, err := cache.New()
func ContentFromURL(url string, disableCache bool) (string, error) {
cache, err := cache.New(cache.Options{
DisableCache: disableCache,
})
if err != nil {
return "", fmt.Errorf("failed to create cache: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdkserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (s *server) parse(w http.ResponseWriter, r *http.Request) {
if reqObject.Content != "" {
out, err = parser.Parse(strings.NewReader(reqObject.Content), reqObject.Options)
} else {
content, loadErr := input.FromLocation(reqObject.File)
content, loadErr := input.FromLocation(reqObject.File, reqObject.DisableCache)
if loadErr != nil {
logger.Errorf(loadErr.Error())
writeError(logger, w, http.StatusInternalServerError, loadErr)
Expand Down
3 changes: 2 additions & 1 deletion pkg/sdkserver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ type parseRequest struct {
parser.Options `json:",inline"`
content `json:",inline"`

File string `json:"file"`
DisableCache bool `json:"disableCache"`
File string `json:"file"`
}

type modelsRequest struct {
Expand Down