Skip to content

Commit d9bc9cc

Browse files
bug: fix loading local files after a remote address
1 parent e904bca commit d9bc9cc

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

pkg/loader/loader.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ type source struct {
4949
Repo *types.Repo
5050
}
5151

52+
func (s source) WithRemote(remote bool) *source {
53+
s.Remote = remote
54+
return &s
55+
}
56+
5257
func (s *source) String() string {
5358
if s.Path == "" && s.Name == "" {
5459
return ""
@@ -436,7 +441,8 @@ func resolve(ctx context.Context, cache *cache.Client, prg *types.Program, base
436441

437442
func input(ctx context.Context, cache *cache.Client, base *source, name string) (*source, error) {
438443
if strings.HasPrefix(name, "http://") || strings.HasPrefix(name, "https://") {
439-
base.Remote = true
444+
// copy and modify
445+
base = base.WithRemote(true)
440446
}
441447

442448
if !base.Remote {

pkg/loader/loader_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package loader
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
"net/http/httptest"
69
"os"
10+
"path/filepath"
711
"testing"
812

913
"github.com/gptscript-ai/gptscript/pkg/types"
@@ -19,6 +23,34 @@ func toString(obj any) string {
1923
return string(s)
2024
}
2125

26+
func TestLocalRemote(t *testing.T) {
27+
s := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
28+
}))
29+
defer s.Close()
30+
dir, err := os.MkdirTemp("", "gptscript-test")
31+
require.NoError(t, err)
32+
33+
err = os.WriteFile(filepath.Join(dir, "chatbot.gpt"), []byte(fmt.Sprintf(`
34+
Chat: true
35+
Name: chatbot
36+
Context: context.gpt
37+
Tools: http://%s/swagger.json
38+
39+
THis is a tool, say hi
40+
`, s.Listener.Addr().String())), 0644)
41+
require.NoError(t, err)
42+
43+
err = os.WriteFile(filepath.Join(dir, "context.gpt"), []byte(`
44+
#!sys.echo
45+
46+
Stuff
47+
`), 0644)
48+
require.NoError(t, err)
49+
50+
_, err = Program(context.Background(), filepath.Join(dir, "chatbot.gpt"), "")
51+
require.NoError(t, err)
52+
}
53+
2254
func TestIsOpenAPI(t *testing.T) {
2355
datav2, err := os.ReadFile("testdata/openapi_v2.yaml")
2456
require.NoError(t, err)

0 commit comments

Comments
 (0)