diff --git a/pkg/cli/parse.go b/pkg/cli/parse.go index cb0f6fac..8599bd97 100644 --- a/pkg/cli/parse.go +++ b/pkg/cli/parse.go @@ -2,12 +2,10 @@ package cli import ( "encoding/json" - "fmt" "os" "strings" "github.com/gptscript-ai/gptscript/pkg/input" - "github.com/gptscript-ai/gptscript/pkg/loader" "github.com/gptscript-ai/gptscript/pkg/parser" "github.com/spf13/cobra" ) @@ -28,25 +26,9 @@ func locationName(l string) string { } func (e *Parse) Run(_ *cobra.Command, args []string) error { - var ( - content string - err error - ) - - // Attempt to read the file first, if that fails, try to load the URL. Finally, - // return an error if both fail. - content, err = input.FromFile(args[0]) + content, err := input.FromLocation(args[0]) if err != nil { - log.Debugf("failed to read file %s (due to %v) attempting to load the URL...", args[0], err) - content, err = loader.ContentFromURL(args[0]) - if err != nil { - return err - } - // If the content is empty and there was no error, this is not a remote file. Return a generic - // error indicating that the file could not be loaded. - if content == "" { - return fmt.Errorf("failed to load %v", args[0]) - } + return err } docs, err := parser.Parse(strings.NewReader(content), parser.Options{ diff --git a/pkg/input/input.go b/pkg/input/input.go index 415d1ad7..80bb49ee 100644 --- a/pkg/input/input.go +++ b/pkg/input/input.go @@ -5,6 +5,8 @@ import ( "io" "os" "strings" + + "github.com/gptscript-ai/gptscript/pkg/loader" ) func FromArgs(args []string) string { @@ -39,3 +41,23 @@ func FromFile(file string) (string, error) { return "", nil } + +// 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) { + // 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) + if err != nil { + return "", err + } + // If the content is empty and there was no error, this is not a remote file. Return a generic + // error indicating that the file could not be loaded. + if content == "" { + return "", fmt.Errorf("failed to load %v", s) + } + } + return content, nil +} diff --git a/pkg/sdkserver/routes.go b/pkg/sdkserver/routes.go index bcdfbf2b..e3a7dd68 100644 --- a/pkg/sdkserver/routes.go +++ b/pkg/sdkserver/routes.go @@ -15,6 +15,7 @@ import ( "github.com/acorn-io/broadcaster" gcontext "github.com/gptscript-ai/gptscript/pkg/context" "github.com/gptscript-ai/gptscript/pkg/gptscript" + "github.com/gptscript-ai/gptscript/pkg/input" "github.com/gptscript-ai/gptscript/pkg/loader" "github.com/gptscript-ai/gptscript/pkg/parser" "github.com/gptscript-ai/gptscript/pkg/runner" @@ -225,15 +226,14 @@ func (s *server) parse(w http.ResponseWriter, r *http.Request) { if reqObject.Content != "" { out, err = parser.Parse(strings.NewReader(reqObject.Content), reqObject.Options) } else { - var file *os.File - file, err = os.Open(reqObject.File) - if err != nil { - logger.Errorf("failed to open file: %v", err) - writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to open file: %w", err)) + content, loadErr := input.FromLocation(reqObject.File) + if loadErr != nil { + logger.Errorf(loadErr.Error()) + writeError(logger, w, http.StatusInternalServerError, loadErr) return } - out, err = parser.Parse(file, reqObject.Options) - _ = file.Close() + + out, err = parser.Parse(strings.NewReader(content), reqObject.Options) } if err != nil { logger.Errorf("failed to parse file: %v", err)