Skip to content

Commit 6e24f16

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

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

pkg/cli/parse.go

Lines changed: 2 additions & 20 deletions
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.FromString(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

Lines changed: 27 additions & 0 deletions
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,28 @@ func FromFile(file string) (string, error) {
3941

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

pkg/sdkserver/routes.go

Lines changed: 7 additions & 7 deletions
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.FromString(reqObject.File)
230+
if loadErr != nil {
231+
logger.Errorf(loadErr.Error())
232+
writeError(logger, w, http.StatusInternalServerError, err)
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)