Skip to content

Commit d161558

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

File tree

8 files changed

+298
-1
lines changed

8 files changed

+298
-1
lines changed

a

+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
2+
var tools = map[string]types.Tool{
3+
"sys.time.now": {
4+
ToolDef: types.ToolDef{
5+
Parameters: types.Parameters{
6+
Description: "Returns the current date and time in RFC3339 format",
7+
},
8+
BuiltinFunc: SysTimeNow,
9+
},
10+
},
11+
"sys.ls": {
12+
ToolDef: types.ToolDef{
13+
Parameters: types.Parameters{
14+
Description: "Lists the contents of a directory",
15+
Arguments: types.ObjectSchema(
16+
"dir", "The directory to list"),
17+
},
18+
BuiltinFunc: SysLs,
19+
},
20+
},
21+
"sys.read": {
22+
ToolDef: types.ToolDef{
23+
Parameters: types.Parameters{
24+
Description: "Reads the contents of a file",
25+
Arguments: types.ObjectSchema(
26+
"filename", "The name of the file to read"),
27+
},
28+
BuiltinFunc: SysRead,
29+
},
30+
},
31+
"sys.write": {
32+
ToolDef: types.ToolDef{
33+
Parameters: types.Parameters{
34+
Description: "Write the contents to a file",
35+
Arguments: types.ObjectSchema(
36+
"filename", "The name of the file to write to",
37+
"content", "The content to write"),
38+
},
39+
BuiltinFunc: SysWrite,
40+
},
41+
},
42+
"sys.append": {
43+
ToolDef: types.ToolDef{
44+
Parameters: types.Parameters{
45+
Description: "Appends the contents to a file",
46+
Arguments: types.ObjectSchema(
47+
"filename", "The name of the file to append to",
48+
"content", "The content to append"),
49+
},
50+
BuiltinFunc: SysAppend,
51+
},
52+
},
53+
"sys.http.get": {
54+
ToolDef: types.ToolDef{
55+
Parameters: types.Parameters{
56+
Description: "Download the contents of a http or https URL",
57+
Arguments: types.ObjectSchema(
58+
"url", "The URL to download"),
59+
},
60+
BuiltinFunc: SysHTTPGet,
61+
},
62+
},
63+
"sys.http.html2text": {
64+
ToolDef: types.ToolDef{
65+
Parameters: types.Parameters{
66+
Description: "Download the contents of a http or https URL returning the content as rendered text converted from HTML",
67+
Arguments: types.ObjectSchema(
68+
"url", "The URL to download"),
69+
},
70+
BuiltinFunc: SysHTTPHtml2Text,
71+
},
72+
},
73+
"sys.abort": {
74+
ToolDef: types.ToolDef{
75+
Parameters: types.Parameters{
76+
Description: "Aborts execution",
77+
Arguments: types.ObjectSchema(
78+
"message", "The description of the error or unexpected result that caused abort to be called",
79+
),
80+
},
81+
BuiltinFunc: SysAbort,
82+
},
83+
},
84+
"sys.chat.finish": {
85+
ToolDef: types.ToolDef{
86+
Parameters: types.Parameters{
87+
Description: "Concludes the conversation. This can not be used to ask a question.",
88+
Arguments: types.ObjectSchema(
89+
"return", "The instructed value to return or a summary of the dialog if no value is instructed",
90+
),
91+
},
92+
BuiltinFunc: SysChatFinish,
93+
},
94+
},
95+
"sys.http.post": {
96+
ToolDef: types.ToolDef{
97+
Parameters: types.Parameters{
98+
Description: "Write contents to a http or https URL using the POST method",
99+
Arguments: types.ObjectSchema(
100+
"url", "The URL to POST to",
101+
"content", "The content to POST",
102+
"contentType", "The \"content type\" of the content such as application/json or text/plain"),
103+
},
104+
BuiltinFunc: SysHTTPPost,
105+
},
106+
},
107+
"sys.find": {
108+
ToolDef: types.ToolDef{
109+
Parameters: types.Parameters{
110+
Description: "Traverse a directory looking for files that match a pattern in the style of the unix find command",
111+
Arguments: types.ObjectSchema(
112+
"pattern", "The file pattern to look for. The pattern is a traditional unix glob format with * matching any character and ? matching a single character",
113+
"directory", "The directory to search in. The current directory \".\" will be used as the default if no argument is passed",
114+
),
115+
},
116+
BuiltinFunc: SysFind,
117+
},
118+
},
119+
"sys.exec": {
120+
ToolDef: types.ToolDef{
121+
Parameters: types.Parameters{
122+
Description: "Execute a command and get the output of the command",
123+
Arguments: types.ObjectSchema(
124+
"command", "The command to run including all applicable arguments",
125+
"directory", "The directory to use as the current working directory of the command. The current directory \".\" will be used if no argument is passed",
126+
),
127+
},
128+
BuiltinFunc: SysExec,
129+
},
130+
},
131+
"sys.getenv": {
132+
ToolDef: types.ToolDef{
133+
Parameters: types.Parameters{
134+
Description: "Gets the value of an OS environment variable",
135+
Arguments: types.ObjectSchema(
136+
"name", "The environment variable name to lookup"),
137+
},
138+
BuiltinFunc: SysGetenv,
139+
},
140+
},
141+
"sys.download": {
142+
ToolDef: types.ToolDef{
143+
Parameters: types.Parameters{
144+
Description: "Downloads a URL, saving the contents to disk at a given location",
145+
Arguments: types.ObjectSchema(
146+
"url", "The URL to download, either http or https.",
147+
"location", "(optional) The on disk location to store the file. If no location is specified a temp location will be used. If the target file already exists it will fail unless override is set to true.",
148+
"override", "If true and a file at the location exists, the file will be overwritten, otherwise fail. Default is false"),
149+
},
150+
BuiltinFunc: SysDownload,
151+
},
152+
},
153+
"sys.remove": {
154+
ToolDef: types.ToolDef{
155+
Parameters: types.Parameters{
156+
Description: "Removes the specified files",
157+
Arguments: types.ObjectSchema(
158+
"location", "The file to remove"),
159+
},
160+
BuiltinFunc: SysRemove,
161+
},
162+
},
163+
"sys.stat": {
164+
ToolDef: types.ToolDef{
165+
Parameters: types.Parameters{
166+
Description: "Gets size, modfied time, and mode of the specified file",
167+
Arguments: types.ObjectSchema(
168+
"filepath", "The complete path and filename of the file",
169+
),
170+
},
171+
BuiltinFunc: SysStat,
172+
},
173+
},
174+
"sys.prompt": {
175+
ToolDef: types.ToolDef{
176+
Parameters: types.Parameters{
177+
Description: "Prompts the user for input",
178+
Arguments: types.ObjectSchema(
179+
"message", "The message to display to the user",
180+
"fields", "A comma-separated list of fields to prompt for",
181+
"sensitive", "(true or false) Whether the input should be hidden",
182+
),
183+
},
184+
BuiltinFunc: SysPrompt,
185+
},
186+
},
187+
"sys.chat.history": {
188+
ToolDef: types.ToolDef{
189+
Parameters: types.Parameters{
190+
Description: "Retrieves the previous chat dialog",
191+
Arguments: types.ObjectSchema(),
192+
},
193+
BuiltinFunc: SysChatHistory,
194+
},
195+
},
196+
}

b

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"sys.abort"
2+
"sys.chat.finish"
3+
"sys.chat.history"
4+
"sys.echo"
5+
"sys.prompt"
6+
"sys.time.now"
7+
8+
"sys.append"
9+
"sys.download"
10+
"sys.exec"
11+
"sys.find"
12+
"sys.getenv"
13+
"sys.http.get"
14+
"sys.http.html2text"
15+
"sys.http.post"
16+
"sys.ls"
17+
"sys.read"
18+
"sys.remove"
19+
"sys.stat"
20+
"sys.write"

ghaction.gpt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
chat: true
2+
tools: sys.exec, sys.read, sys.write, sys.ls
3+
4+
You are an expert at Github Actions.
5+
6+
Do the following sequentially.
7+
8+
1. List all files in .github/workflows/
9+
2. Read each file .github/workflow to understand the currently defined workflows.
10+
3. Provide a summarize list of all workflows, don't show the full contents
11+
12+
Ask the user what they'd like to do.

output-file

Whitespace-only changes.

pkg/cli/main.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cli
2+
3+
import (
4+
"os"
5+
6+
"github.com/acorn-io/cmd"
7+
"github.com/gptscript-ai/gptscript/pkg/daemon"
8+
"github.com/gptscript-ai/gptscript/pkg/mvl"
9+
10+
// Load all VCS
11+
_ "github.com/gptscript-ai/gptscript/pkg/loader/vcs"
12+
)
13+
14+
func Main() {
15+
if len(os.Args) > 2 && os.Args[1] == "sys.daemon" {
16+
if os.Getenv("GPTSCRIPT_DEBUG") == "true" {
17+
mvl.SetDebug()
18+
}
19+
if err := daemon.SysDaemon(); err != nil {
20+
log.Debugf("failed running daemon: %v", err)
21+
}
22+
os.Exit(0)
23+
}
24+
cmd.Main(New())
25+
}

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)
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Chat: true
2+
Name: chatbot
3+
Tools: http://127.0.0.1:11111/swagger.json
4+
Context: context.gpt
5+
6+
THis is a tool, say hi

0 commit comments

Comments
 (0)