Skip to content

Commit 78273e0

Browse files
Merge pull request #165 from ibuildthecloud/main
Fix http tool lookups that are on the root of the domain
2 parents f7c9a43 + 55e57b6 commit 78273e0

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

pkg/loader/loader_test.go

+95-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,99 @@
11
package loader
22

3-
import "testing"
3+
import (
4+
"context"
5+
"encoding/json"
6+
"testing"
47

5-
func TestLoader(*testing.T) {
8+
"github.com/hexops/autogold/v2"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func toString(obj any) string {
13+
s, err := json.MarshalIndent(obj, "", " ")
14+
if err != nil {
15+
panic(err)
16+
}
17+
return string(s)
18+
}
19+
20+
func TestHelloWorld(t *testing.T) {
21+
prg, err := Program(context.Background(),
22+
"https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt",
23+
"")
24+
require.NoError(t, err)
25+
autogold.Expect(`{
26+
"name": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt",
27+
"entryToolId": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt:1",
28+
"toolSet": {
29+
"https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt:1": {
30+
"modelName": "gpt-4-turbo-preview",
31+
"internalPrompt": null,
32+
"instructions": "Say hello world",
33+
"id": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt:1",
34+
"localTools": {
35+
"": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt:1"
36+
},
37+
"source": {
38+
"location": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt",
39+
"lineNo": 1
40+
},
41+
"workingDir": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example"
42+
},
43+
"https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt:1": {
44+
"modelName": "gpt-4-turbo-preview",
45+
"internalPrompt": null,
46+
"tools": [
47+
"../bob.gpt"
48+
],
49+
"instructions": "call bob",
50+
"id": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt:1",
51+
"toolMapping": {
52+
"../bob.gpt": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt:1"
53+
},
54+
"localTools": {
55+
"": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt:1"
56+
},
57+
"source": {
58+
"location": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt",
59+
"lineNo": 1
60+
},
61+
"workingDir": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub"
62+
}
63+
}
64+
}`).Equal(t, toString(prg))
65+
66+
prg, err = Program(context.Background(), "https://get.gptscript.ai/echo.gpt", "")
67+
require.NoError(t, err)
68+
69+
autogold.Expect(`{
70+
"name": "https://get.gptscript.ai/echo.gpt",
71+
"entryToolId": "https://get.gptscript.ai/echo.gpt:1",
72+
"toolSet": {
73+
"https://get.gptscript.ai/echo.gpt:1": {
74+
"description": "Returns back the input of the script",
75+
"modelName": "gpt-4-turbo-preview",
76+
"internalPrompt": null,
77+
"arguments": {
78+
"type": "object",
79+
"properties": {
80+
"input": {
81+
"description": " Any string",
82+
"type": "string"
83+
}
84+
}
85+
},
86+
"instructions": "echo \"${input}\"",
87+
"id": "https://get.gptscript.ai/echo.gpt:1",
88+
"localTools": {
89+
"": "https://get.gptscript.ai/echo.gpt:1"
90+
},
91+
"source": {
92+
"location": "https://get.gptscript.ai/echo.gpt",
93+
"lineNo": 1
94+
},
95+
"workingDir": "https://get.gptscript.ai/"
96+
}
97+
}
98+
}`).Equal(t, toString(prg))
699
}

pkg/loader/url.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func loadURL(ctx context.Context, base *source, name string) (*source, bool, err
2727
)
2828

2929
if base.Path != "" && relative {
30+
// Don't use path.Join because this is a URL and will break the :// protocol by cleaning it
3031
url = base.Path + "/" + name
3132
}
3233

@@ -64,7 +65,14 @@ func loadURL(ctx context.Context, base *source, name string) (*source, bool, err
6465
pathURL.Path = path.Dir(parsed.Path)
6566
pathString := pathURL.String()
6667
name = path.Base(parsed.Path)
67-
url = pathString + "/" + name
68+
69+
// Append to pathString name. This is not the same as the original URL. This is an attempt to end up
70+
// with a clean URL with no ../ in it.
71+
if strings.HasSuffix(pathString, "/") {
72+
url = pathString + name
73+
} else {
74+
url = pathString + "/" + name
75+
}
6876

6977
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
7078
if err != nil {

0 commit comments

Comments
 (0)