Skip to content

Commit 01583db

Browse files
Merge pull request #101 from ibuildthecloud/main
Don't set the working dir of tools
2 parents 6126d27 + 51826f5 commit 01583db

File tree

7 files changed

+20
-45
lines changed

7 files changed

+20
-45
lines changed

pkg/engine/cmd.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string)
4040
return tool.BuiltinFunc(ctx, e.Env, input)
4141
}
4242

43-
cmd, stop, err := e.newCommand(ctx, nil, tool.Instructions, input)
43+
var extraEnv []string
44+
if tool.WorkingDir != "" {
45+
extraEnv = append(extraEnv, "GPTSCRIPT_TOOL_DIR="+tool.WorkingDir)
46+
}
47+
48+
cmd, stop, err := e.newCommand(ctx, extraEnv, tool.Instructions, input)
4449
if err != nil {
4550
return "", err
4651
}
@@ -59,9 +64,6 @@ func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string)
5964
cmd.Stdin = strings.NewReader(input)
6065
cmd.Stderr = io.MultiWriter(all, os.Stderr)
6166
cmd.Stdout = io.MultiWriter(all, output)
62-
if tool.WorkingDir != "" {
63-
cmd.Dir = tool.WorkingDir
64-
}
6567

6668
if err := cmd.Run(); err != nil {
6769
_, _ = os.Stderr.Write(output.Bytes())
@@ -82,10 +84,6 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, instructions
8284
envMap := map[string]string{}
8385
for _, env := range env {
8486
key, value, _ := strings.Cut(env, "=")
85-
key, ok := strings.CutPrefix(key, "GPTSCRIPT_VAR_")
86-
if !ok {
87-
continue
88-
}
8987
envMap[key] = value
9088
}
9189

pkg/engine/http.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ const DaemonURLSuffix = ".daemon.gpt.local"
1717

1818
func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Tool, input string) (cmdRet *Return, cmdErr error) {
1919
envMap := map[string]string{}
20+
2021
for _, env := range e.Env {
21-
v, ok := strings.CutPrefix(env, "GPTSCRIPT_VAR_")
22-
if ok {
23-
k, v, _ := strings.Cut(v, "=")
24-
envMap[k] = v
25-
}
22+
k, v, _ := strings.Cut(env, "=")
23+
envMap[k] = v
2624
}
2725

2826
toolURL := strings.Split(tool.Instructions, "\n")[0][2:]
@@ -64,10 +62,6 @@ func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Too
6462

6563
req.Header.Set("X-GPTScript-Tool-Name", tool.Parameters.Name)
6664

67-
for k, v := range envMap {
68-
req.Header.Set("X-GPTScript-Var-"+k, v)
69-
}
70-
7165
if err := json.Unmarshal([]byte(input), &map[string]any{}); err == nil {
7266
req.Header.Set("Content-Type", "application/json")
7367
} else {

pkg/runner/log.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

pkg/runner/runner.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package runner
22

33
import (
44
"context"
5-
"os"
65
"sync"
76
"time"
87

@@ -21,41 +20,30 @@ type Monitor interface {
2120
}
2221

2322
type Options struct {
24-
WorkingDir string
2523
MonitorFactory MonitorFactory `usage:"-"`
2624
}
2725

2826
func complete(opts ...Options) (result Options) {
2927
for _, opt := range opts {
30-
result.WorkingDir = types.FirstSet(opt.WorkingDir, result.WorkingDir)
3128
result.MonitorFactory = types.FirstSet(opt.MonitorFactory, result.MonitorFactory)
3229
}
3330
if result.MonitorFactory == nil {
3431
result.MonitorFactory = noopFactory{}
3532
}
36-
if result.WorkingDir == "" {
37-
var err error
38-
result.WorkingDir, err = os.Getwd()
39-
if err != nil {
40-
log.Fatalf("failed to determine current working directory: %v", err)
41-
}
42-
}
4333
return
4434
}
4535

4636
type Runner struct {
47-
c engine.Model
48-
factory MonitorFactory
49-
workingDir string
37+
c engine.Model
38+
factory MonitorFactory
5039
}
5140

5241
func New(client engine.Model, opts ...Options) (*Runner, error) {
5342
opt := complete(opts...)
5443

5544
return &Runner{
56-
c: client,
57-
factory: opt.MonitorFactory,
58-
workingDir: opt.WorkingDir,
45+
c: client,
46+
factory: opt.MonitorFactory,
5947
}, nil
6048
}
6149

@@ -106,10 +94,6 @@ func (r *Runner) call(callCtx engine.Context, monitor Monitor, env []string, inp
10694
Env: env,
10795
}
10896

109-
if r.workingDir != "" {
110-
e.Env = append(e.Env, "GPTSCRIPT_VAR_WORKDIR="+r.workingDir)
111-
}
112-
11397
monitor.Event(Event{
11498
Time: time.Now(),
11599
CallContext: &callCtx,

pkg/tests/testdata/TestCwd/call3.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"role": "tool",
7272
"content": [
7373
{
74-
"text": "/testdata/TestCwd\nthe data"
74+
"text": "testdata/TestCwd\nthe data"
7575
}
7676
],
7777
"toolCall": {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#
2-
#!/bin/bash
2+
#!/usr/bin/env X=${GPTSCRIPT_TOOL_DIR} /bin/bash
33
set -e -x
44

5+
[ ${X} = ${GPTSCRIPT_TOOL_DIR} ]
6+
cd $X
57
echo sub
68
cat sub.txt

pkg/tests/testdata/TestCwd/test.gpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ name: local
88
#!/bin/bash
99
set -e -x
1010

11+
[ "" = "${TOOL_DIR}" ]
1112
P=$(pwd)
12-
echo ${P##${GPTSCRIPT_VAR_WORKDIR}}
13+
echo ${GPTSCRIPT_TOOL_DIR##${P}}
14+
cd $GPTSCRIPT_TOOL_DIR
1315
cat data.txt

0 commit comments

Comments
 (0)