Skip to content

Commit cbf8dcf

Browse files
Add /sys and /*.gpt
1 parent b57117a commit cbf8dcf

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

examples/search.gpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ vacation spot give the name and description.
77
name: search
88
description: Searches the internet for content
99
args: query: The query to search for
10-
tools: sys.http.text?
10+
tools: sys.http.html2text?
1111

1212
First download the content of "https://html.duckduckgo.com/html/?q=${query}".
1313
Look for the first 5 search results. Download each search result and look for content

pkg/builtin/builtin.go

+10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ var Tools = map[string]types.Tool{
8282
},
8383
}
8484

85+
func SysProgram() *types.Program {
86+
result := &types.Program{
87+
ToolSet: types.ToolSet{},
88+
}
89+
for _, tool := range ListTools() {
90+
result.ToolSet[tool.ID] = tool
91+
}
92+
return result
93+
}
94+
8595
func ListTools() (result []types.Tool) {
8696
var keys []string
8797
for k := range Tools {

pkg/server/server.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/acorn-io/broadcaster"
18+
"github.com/acorn-io/gptscript/pkg/builtin"
1819
"github.com/acorn-io/gptscript/pkg/loader"
1920
"github.com/acorn-io/gptscript/pkg/runner"
2021
"github.com/acorn-io/gptscript/pkg/types"
@@ -85,12 +86,30 @@ var (
8586
type execKey struct{}
8687

8788
func (s *Server) list(rw http.ResponseWriter, req *http.Request) {
89+
rw.Header().Set("Content-Type", "application/json")
90+
enc := json.NewEncoder(rw)
91+
enc.SetIndent("", " ")
92+
8893
path := filepath.Join(".", req.URL.Path)
94+
if req.URL.Path == "/sys" {
95+
_ = enc.Encode(builtin.SysProgram())
96+
return
97+
} else if strings.HasSuffix(path, ".gpt") {
98+
prg, err := loader.Program(req.Context(), path, "")
99+
if err != nil {
100+
http.Error(rw, err.Error(), http.StatusInternalServerError)
101+
return
102+
}
103+
_ = enc.Encode(prg)
104+
return
105+
}
106+
89107
files, err := os.ReadDir(path)
90108
if err != nil {
91109
http.Error(rw, err.Error(), http.StatusInternalServerError)
92110
return
93111
}
112+
94113
var result []string
95114
for _, file := range files {
96115
if file.IsDir() && !strings.HasPrefix(file.Name(), ".") {
@@ -100,8 +119,6 @@ func (s *Server) list(rw http.ResponseWriter, req *http.Request) {
100119
}
101120
}
102121

103-
enc := json.NewEncoder(rw)
104-
enc.SetIndent("", " ")
105122
_ = enc.Encode(result)
106123
}
107124

@@ -186,7 +203,7 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
186203
case http.MethodPost:
187204
s.run(rw, req)
188205
case http.MethodGet:
189-
if strings.Contains(strings.ToLower(req.Header.Get("Connection")), "upgrade") {
206+
if req.URL.Path == "/" && strings.Contains(strings.ToLower(req.Header.Get("Connection")), "upgrade") {
190207
err := s.melody.HandleRequest(rw, req)
191208
if err != nil {
192209
http.Error(rw, err.Error(), http.StatusInternalServerError)
@@ -244,7 +261,7 @@ func (s *Session) Stop(output string, err error) {
244261
e := Event{
245262
Event: runner.Event{
246263
Time: time.Now(),
247-
Type: "runEnd",
264+
Type: "runFinish",
248265
},
249266
RunID: s.id,
250267
Input: s.input,

0 commit comments

Comments
 (0)