Skip to content

Commit 75bd379

Browse files
chore: add location option to loading scripts
1 parent 0c73f4b commit 75bd379

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

pkg/loader/loader.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,20 @@ func ProgramFromSource(ctx context.Context, content, subToolName string, opts ..
373373
}
374374
opt := complete(opts...)
375375

376+
var locationPath, locationName string
377+
if opt.Location != "" {
378+
locationPath = path.Dir(opt.Location)
379+
locationName = path.Base(opt.Location)
380+
}
381+
376382
prg := types.Program{
377383
ToolSet: types.ToolSet{},
378384
}
379385
tools, err := readTool(ctx, opt.Cache, &prg, &source{
380386
Content: []byte(content),
381-
Location: "inline",
387+
Path: locationPath,
388+
Name: locationName,
389+
Location: opt.Location,
382390
}, subToolName)
383391
if err != nil {
384392
return types.Program{}, err
@@ -388,12 +396,18 @@ func ProgramFromSource(ctx context.Context, content, subToolName string, opts ..
388396
}
389397

390398
type Options struct {
391-
Cache *cache.Client
399+
Cache *cache.Client
400+
Location string
392401
}
393402

394403
func complete(opts ...Options) (result Options) {
395404
for _, opt := range opts {
396405
result.Cache = types.FirstSet(opt.Cache, result.Cache)
406+
result.Location = types.FirstSet(opt.Location, result.Location)
407+
}
408+
409+
if result.Location == "" {
410+
result.Location = "inline"
397411
}
398412

399413
return

pkg/sdkserver/routes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (s *server) execHandler(w http.ResponseWriter, r *http.Request) {
183183
logger.Debugf("executing tool: %+v", reqObject)
184184
var (
185185
def fmt.Stringer = &reqObject.ToolDefs
186-
programLoader loaderFunc = loader.ProgramFromSource
186+
programLoader = loaderWithLocation(loader.ProgramFromSource, reqObject.Location)
187187
)
188188
if reqObject.Content != "" {
189189
def = &reqObject.content

pkg/sdkserver/run.go

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ import (
1616

1717
type loaderFunc func(context.Context, string, string, ...loader.Options) (types.Program, error)
1818

19+
func loaderWithLocation(f loaderFunc, loc string) loaderFunc {
20+
return func(ctx context.Context, s string, s2 string, options ...loader.Options) (types.Program, error) {
21+
return f(ctx, s, s2, append(options, loader.Options{
22+
Location: loc,
23+
})...)
24+
}
25+
}
26+
1927
func (s *server) execAndStream(ctx context.Context, programLoader loaderFunc, logger mvl.Logger, w http.ResponseWriter, opts gptscript.Options, chatState, input, subTool string, toolDef fmt.Stringer) {
2028
g, err := gptscript.New(ctx, s.gptscriptOpts, opts)
2129
if err != nil {

pkg/sdkserver/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type toolOrFileRequest struct {
6161
CredentialContext string `json:"credentialContext"`
6262
CredentialOverrides []string `json:"credentialOverrides"`
6363
Confirm bool `json:"confirm"`
64+
Location string `json:"location,omitempty"`
6465
}
6566

6667
type content struct {

0 commit comments

Comments
 (0)