Skip to content

Commit a6a492d

Browse files
committed
feat: add sdkserver support for remote file parsing
Signed-off-by: tylerslaton <[email protected]>
1 parent 21f0fea commit a6a492d

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

pkg/cli/parse.go

+2-20
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package cli
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"os"
76
"strings"
87

98
"github.com/gptscript-ai/gptscript/pkg/input"
10-
"github.com/gptscript-ai/gptscript/pkg/loader"
119
"github.com/gptscript-ai/gptscript/pkg/parser"
1210
"github.com/spf13/cobra"
1311
)
@@ -28,25 +26,9 @@ func locationName(l string) string {
2826
}
2927

3028
func (e *Parse) Run(_ *cobra.Command, args []string) error {
31-
var (
32-
content string
33-
err error
34-
)
35-
36-
// Attempt to read the file first, if that fails, try to load the URL. Finally,
37-
// return an error if both fail.
38-
content, err = input.FromFile(args[0])
29+
content, err := input.FromLocation(args[0])
3930
if err != nil {
40-
log.Debugf("failed to read file %s (due to %v) attempting to load the URL...", args[0], err)
41-
content, err = loader.ContentFromURL(args[0])
42-
if err != nil {
43-
return err
44-
}
45-
// If the content is empty and there was no error, this is not a remote file. Return a generic
46-
// error indicating that the file could not be loaded.
47-
if content == "" {
48-
return fmt.Errorf("failed to load %v", args[0])
49-
}
31+
return err
5032
}
5133

5234
docs, err := parser.Parse(strings.NewReader(content), parser.Options{

pkg/input/input.go

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"io"
66
"os"
77
"strings"
8+
9+
"github.com/gptscript-ai/gptscript/pkg/loader"
810
)
911

1012
func FromArgs(args []string) string {
@@ -39,3 +41,23 @@ func FromFile(file string) (string, error) {
3941

4042
return "", nil
4143
}
44+
45+
// FromLocation takes a string that can be a file path or a URL to a file and returns the content of that file.
46+
func FromLocation(s string) (string, error) {
47+
// Attempt to read the file first, if that fails, try to load the URL. Finally,
48+
// return an error if both fail.
49+
content, err := FromFile(s)
50+
if err != nil {
51+
log.Debugf("failed to read file %s (due to %v) attempting to load the URL...", s, err)
52+
content, err = loader.ContentFromURL(s)
53+
if err != nil {
54+
return "", err
55+
}
56+
// If the content is empty and there was no error, this is not a remote file. Return a generic
57+
// error indicating that the file could not be loaded.
58+
if content == "" {
59+
return "", fmt.Errorf("failed to load %v", s)
60+
}
61+
}
62+
return content, nil
63+
}

pkg/sdkserver/routes.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/acorn-io/broadcaster"
1616
gcontext "github.com/gptscript-ai/gptscript/pkg/context"
1717
"github.com/gptscript-ai/gptscript/pkg/gptscript"
18+
"github.com/gptscript-ai/gptscript/pkg/input"
1819
"github.com/gptscript-ai/gptscript/pkg/loader"
1920
"github.com/gptscript-ai/gptscript/pkg/parser"
2021
"github.com/gptscript-ai/gptscript/pkg/runner"
@@ -225,15 +226,14 @@ func (s *server) parse(w http.ResponseWriter, r *http.Request) {
225226
if reqObject.Content != "" {
226227
out, err = parser.Parse(strings.NewReader(reqObject.Content), reqObject.Options)
227228
} else {
228-
var file *os.File
229-
file, err = os.Open(reqObject.File)
230-
if err != nil {
231-
logger.Errorf("failed to open file: %v", err)
232-
writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to open file: %w", err))
229+
content, loadErr := input.FromLocation(reqObject.File)
230+
if loadErr != nil {
231+
logger.Errorf(loadErr.Error())
232+
writeError(logger, w, http.StatusInternalServerError, loadErr)
233233
return
234234
}
235-
out, err = parser.Parse(file, reqObject.Options)
236-
_ = file.Close()
235+
236+
out, err = parser.Parse(strings.NewReader(content), reqObject.Options)
237237
}
238238
if err != nil {
239239
logger.Errorf("failed to parse file: %v", err)

0 commit comments

Comments
 (0)