Skip to content

Commit

Permalink
Add /sys and /*.gpt
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Feb 6, 2024
1 parent b57117a commit cbf8dcf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/search.gpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ vacation spot give the name and description.
name: search
description: Searches the internet for content
args: query: The query to search for
tools: sys.http.text?
tools: sys.http.html2text?

First download the content of "https://html.duckduckgo.com/html/?q=${query}".
Look for the first 5 search results. Download each search result and look for content
Expand Down
10 changes: 10 additions & 0 deletions pkg/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ var Tools = map[string]types.Tool{
},
}

func SysProgram() *types.Program {
result := &types.Program{
ToolSet: types.ToolSet{},
}
for _, tool := range ListTools() {
result.ToolSet[tool.ID] = tool
}
return result
}

func ListTools() (result []types.Tool) {
var keys []string
for k := range Tools {
Expand Down
25 changes: 21 additions & 4 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/acorn-io/broadcaster"
"github.com/acorn-io/gptscript/pkg/builtin"
"github.com/acorn-io/gptscript/pkg/loader"
"github.com/acorn-io/gptscript/pkg/runner"
"github.com/acorn-io/gptscript/pkg/types"
Expand Down Expand Up @@ -85,12 +86,30 @@ var (
type execKey struct{}

func (s *Server) list(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(rw)
enc.SetIndent("", " ")

path := filepath.Join(".", req.URL.Path)
if req.URL.Path == "/sys" {
_ = enc.Encode(builtin.SysProgram())
return
} else if strings.HasSuffix(path, ".gpt") {
prg, err := loader.Program(req.Context(), path, "")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
_ = enc.Encode(prg)
return
}

files, err := os.ReadDir(path)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}

var result []string
for _, file := range files {
if file.IsDir() && !strings.HasPrefix(file.Name(), ".") {
Expand All @@ -100,8 +119,6 @@ func (s *Server) list(rw http.ResponseWriter, req *http.Request) {
}
}

enc := json.NewEncoder(rw)
enc.SetIndent("", " ")
_ = enc.Encode(result)
}

Expand Down Expand Up @@ -186,7 +203,7 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
case http.MethodPost:
s.run(rw, req)
case http.MethodGet:
if strings.Contains(strings.ToLower(req.Header.Get("Connection")), "upgrade") {
if req.URL.Path == "/" && strings.Contains(strings.ToLower(req.Header.Get("Connection")), "upgrade") {
err := s.melody.HandleRequest(rw, req)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -244,7 +261,7 @@ func (s *Session) Stop(output string, err error) {
e := Event{
Event: runner.Event{
Time: time.Now(),
Type: "runEnd",
Type: "runFinish",
},
RunID: s.id,
Input: s.input,
Expand Down

0 comments on commit cbf8dcf

Please sign in to comment.