Skip to content

Commit 4c8085a

Browse files
Add ?tool param
1 parent ed39111 commit 4c8085a

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

pkg/loader/loader.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/gptscript-ai/gptscript/pkg/assemble"
2222
"github.com/gptscript-ai/gptscript/pkg/builtin"
23+
"github.com/gptscript-ai/gptscript/pkg/engine"
2324
"github.com/gptscript-ai/gptscript/pkg/parser"
2425
"github.com/gptscript-ai/gptscript/pkg/types"
2526
)
@@ -137,7 +138,7 @@ func loadURL(ctx context.Context, base *Source, name string) (*Source, bool, err
137138
}, true, nil
138139
}
139140

140-
func loadProgram(data []byte, into *types.Program) (types.Tool, error) {
141+
func loadProgram(data []byte, into *types.Program, targetToolName string) (types.Tool, error) {
141142
var (
142143
ext types.Program
143144
id string
@@ -158,7 +159,19 @@ func loadProgram(data []byte, into *types.Program) (types.Tool, error) {
158159
into.ToolSet[v.ID] = v
159160
}
160161

161-
return into.ToolSet[ext.EntryToolID+id], nil
162+
tool := into.ToolSet[ext.EntryToolID+id]
163+
if targetToolName == "" {
164+
return tool, nil
165+
}
166+
167+
tool, ok := into.ToolSet[tool.LocalTools[targetToolName]]
168+
if !ok {
169+
return tool, &engine.ErrToolNotFound{
170+
ToolName: targetToolName,
171+
}
172+
}
173+
174+
return tool, nil
162175
}
163176

164177
func ReadTool(ctx context.Context, prg *types.Program, base *Source, targetToolName string) (types.Tool, error) {
@@ -169,7 +182,7 @@ func ReadTool(ctx context.Context, prg *types.Program, base *Source, targetToolN
169182
_ = base.Content.Close()
170183

171184
if bytes.HasPrefix(data, assemble.Header) {
172-
return loadProgram(data, prg)
185+
return loadProgram(data, prg, targetToolName)
173186
}
174187

175188
tools, err := parser.Parse(bytes.NewReader(data))
@@ -263,6 +276,7 @@ func link(ctx context.Context, prg *types.Program, base *Source, tool types.Tool
263276
}
264277

265278
tool.ToolMapping = map[string]string{}
279+
tool.LocalTools = map[string]string{}
266280
toolNames := map[string]struct{}{}
267281

268282
// Add now to break circular loops, but later we will update this tool and copy the new
@@ -315,6 +329,10 @@ func link(ctx context.Context, prg *types.Program, base *Source, tool types.Tool
315329
tool.Tools[i] = newToolName
316330
}
317331

332+
for _, localTool := range localTools {
333+
tool.LocalTools[localTool.Name] = localTool.ID
334+
}
335+
318336
tool = builtin.SetDefaults(tool)
319337
prg.ToolSet[tool.ID] = tool
320338

pkg/server/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (s *Server) list(rw http.ResponseWriter, req *http.Request) {
108108
_ = enc.Encode(builtin.SysProgram())
109109
return
110110
} else if strings.HasSuffix(path, ".gpt") {
111-
prg, err := loader.Program(req.Context(), path, "")
111+
prg, err := loader.Program(req.Context(), path, req.URL.Query().Get("tool"))
112112
if err != nil {
113113
http.Error(rw, err.Error(), http.StatusInternalServerError)
114114
return
@@ -149,7 +149,7 @@ func (s *Server) run(rw http.ResponseWriter, req *http.Request) {
149149
path += ".gpt"
150150
}
151151

152-
prg, err := loader.Program(req.Context(), path, "")
152+
prg, err := loader.Program(req.Context(), path, req.URL.Query().Get("tool"))
153153
if errors.Is(err, fs.ErrNotExist) {
154154
http.NotFound(rw, req)
155155
return

pkg/types/tool.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ const (
1515
type ToolSet map[string]Tool
1616

1717
type Program struct {
18-
EntryToolID string `json:"entryToolId,omitempty"`
19-
ToolSet ToolSet `json:"toolSet,omitempty"`
18+
EntryToolID string `json:"entryToolId,omitempty"`
19+
ToolSet ToolSet `json:"toolSet,omitempty"`
20+
Exports map[string]string `json:"exports,omitempty"`
2021
}
2122

2223
type BuiltinFunc func(ctx context.Context, env []string, input string) (string, error)
@@ -29,6 +30,7 @@ type Tool struct {
2930
Instructions string `json:"instructions,omitempty"`
3031
Tools []string `json:"tools,omitempty"`
3132
ToolMapping map[string]string `json:"toolMapping,omitempty"`
33+
LocalTools map[string]string `json:"localTools,omitempty"`
3234
BuiltinFunc BuiltinFunc `json:"-"`
3335

3436
Vision bool `json:"vision,omitempty"`

0 commit comments

Comments
 (0)