Skip to content

Commit

Permalink
Merge pull request #165 from ibuildthecloud/main
Browse files Browse the repository at this point in the history
Fix http tool lookups that are on the root of the domain
  • Loading branch information
ibuildthecloud authored Mar 15, 2024
2 parents f7c9a43 + 55e57b6 commit 78273e0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
97 changes: 95 additions & 2 deletions pkg/loader/loader_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,99 @@
package loader

import "testing"
import (
"context"
"encoding/json"
"testing"

func TestLoader(*testing.T) {
"github.com/hexops/autogold/v2"
"github.com/stretchr/testify/require"
)

func toString(obj any) string {
s, err := json.MarshalIndent(obj, "", " ")
if err != nil {
panic(err)
}
return string(s)
}

func TestHelloWorld(t *testing.T) {
prg, err := Program(context.Background(),
"https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt",
"")
require.NoError(t, err)
autogold.Expect(`{
"name": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt",
"entryToolId": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt:1",
"toolSet": {
"https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt:1": {
"modelName": "gpt-4-turbo-preview",
"internalPrompt": null,
"instructions": "Say hello world",
"id": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt:1",
"localTools": {
"": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt:1"
},
"source": {
"location": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt",
"lineNo": 1
},
"workingDir": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example"
},
"https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt:1": {
"modelName": "gpt-4-turbo-preview",
"internalPrompt": null,
"tools": [
"../bob.gpt"
],
"instructions": "call bob",
"id": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt:1",
"toolMapping": {
"../bob.gpt": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/bob.gpt:1"
},
"localTools": {
"": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt:1"
},
"source": {
"location": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub/tool.gpt",
"lineNo": 1
},
"workingDir": "https://raw.githubusercontent.com/ibuildthecloud/test/bafe5a62174e8a0ea162277dcfe3a2ddb7eea928/example/sub"
}
}
}`).Equal(t, toString(prg))

prg, err = Program(context.Background(), "https://get.gptscript.ai/echo.gpt", "")
require.NoError(t, err)

autogold.Expect(`{
"name": "https://get.gptscript.ai/echo.gpt",
"entryToolId": "https://get.gptscript.ai/echo.gpt:1",
"toolSet": {
"https://get.gptscript.ai/echo.gpt:1": {
"description": "Returns back the input of the script",
"modelName": "gpt-4-turbo-preview",
"internalPrompt": null,
"arguments": {
"type": "object",
"properties": {
"input": {
"description": " Any string",
"type": "string"
}
}
},
"instructions": "echo \"${input}\"",
"id": "https://get.gptscript.ai/echo.gpt:1",
"localTools": {
"": "https://get.gptscript.ai/echo.gpt:1"
},
"source": {
"location": "https://get.gptscript.ai/echo.gpt",
"lineNo": 1
},
"workingDir": "https://get.gptscript.ai/"
}
}
}`).Equal(t, toString(prg))
}
10 changes: 9 additions & 1 deletion pkg/loader/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func loadURL(ctx context.Context, base *source, name string) (*source, bool, err
)

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

Expand Down Expand Up @@ -64,7 +65,14 @@ func loadURL(ctx context.Context, base *source, name string) (*source, bool, err
pathURL.Path = path.Dir(parsed.Path)
pathString := pathURL.String()
name = path.Base(parsed.Path)
url = pathString + "/" + name

// Append to pathString name. This is not the same as the original URL. This is an attempt to end up
// with a clean URL with no ../ in it.
if strings.HasSuffix(pathString, "/") {
url = pathString + name
} else {
url = pathString + "/" + name
}

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

0 comments on commit 78273e0

Please sign in to comment.