Skip to content

Don't set the working dir of tools #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions pkg/engine/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string)
return tool.BuiltinFunc(ctx, e.Env, input)
}

cmd, stop, err := e.newCommand(ctx, nil, tool.Instructions, input)
var extraEnv []string
if tool.WorkingDir != "" {
extraEnv = append(extraEnv, "GPTSCRIPT_TOOL_DIR="+tool.WorkingDir)
}

cmd, stop, err := e.newCommand(ctx, extraEnv, tool.Instructions, input)
if err != nil {
return "", err
}
Expand All @@ -59,9 +64,6 @@ func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string)
cmd.Stdin = strings.NewReader(input)
cmd.Stderr = io.MultiWriter(all, os.Stderr)
cmd.Stdout = io.MultiWriter(all, output)
if tool.WorkingDir != "" {
cmd.Dir = tool.WorkingDir
}

if err := cmd.Run(); err != nil {
_, _ = os.Stderr.Write(output.Bytes())
Expand All @@ -82,10 +84,6 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, instructions
envMap := map[string]string{}
for _, env := range env {
key, value, _ := strings.Cut(env, "=")
key, ok := strings.CutPrefix(key, "GPTSCRIPT_VAR_")
if !ok {
continue
}
envMap[key] = value
}

Expand Down
12 changes: 3 additions & 9 deletions pkg/engine/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ const DaemonURLSuffix = ".daemon.gpt.local"

func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Tool, input string) (cmdRet *Return, cmdErr error) {
envMap := map[string]string{}

for _, env := range e.Env {
v, ok := strings.CutPrefix(env, "GPTSCRIPT_VAR_")
if ok {
k, v, _ := strings.Cut(v, "=")
envMap[k] = v
}
k, v, _ := strings.Cut(env, "=")
envMap[k] = v
}

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

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

for k, v := range envMap {
req.Header.Set("X-GPTScript-Var-"+k, v)
}

if err := json.Unmarshal([]byte(input), &map[string]any{}); err == nil {
req.Header.Set("Content-Type", "application/json")
} else {
Expand Down
5 changes: 0 additions & 5 deletions pkg/runner/log.go

This file was deleted.

24 changes: 4 additions & 20 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package runner

import (
"context"
"os"
"sync"
"time"

Expand All @@ -21,41 +20,30 @@ type Monitor interface {
}

type Options struct {
WorkingDir string
MonitorFactory MonitorFactory `usage:"-"`
}

func complete(opts ...Options) (result Options) {
for _, opt := range opts {
result.WorkingDir = types.FirstSet(opt.WorkingDir, result.WorkingDir)
result.MonitorFactory = types.FirstSet(opt.MonitorFactory, result.MonitorFactory)
}
if result.MonitorFactory == nil {
result.MonitorFactory = noopFactory{}
}
if result.WorkingDir == "" {
var err error
result.WorkingDir, err = os.Getwd()
if err != nil {
log.Fatalf("failed to determine current working directory: %v", err)
}
}
return
}

type Runner struct {
c engine.Model
factory MonitorFactory
workingDir string
c engine.Model
factory MonitorFactory
}

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

return &Runner{
c: client,
factory: opt.MonitorFactory,
workingDir: opt.WorkingDir,
c: client,
factory: opt.MonitorFactory,
}, nil
}

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

if r.workingDir != "" {
e.Env = append(e.Env, "GPTSCRIPT_VAR_WORKDIR="+r.workingDir)
}

monitor.Event(Event{
Time: time.Now(),
CallContext: &callCtx,
Expand Down
2 changes: 1 addition & 1 deletion pkg/tests/testdata/TestCwd/call3.golden
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"role": "tool",
"content": [
{
"text": "/testdata/TestCwd\nthe data"
"text": "testdata/TestCwd\nthe data"
}
],
"toolCall": {
Expand Down
4 changes: 3 additions & 1 deletion pkg/tests/testdata/TestCwd/subtool/test.gpt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#
#!/bin/bash
#!/usr/bin/env X=${GPTSCRIPT_TOOL_DIR} /bin/bash
set -e -x

[ ${X} = ${GPTSCRIPT_TOOL_DIR} ]
cd $X
echo sub
cat sub.txt
4 changes: 3 additions & 1 deletion pkg/tests/testdata/TestCwd/test.gpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ name: local
#!/bin/bash
set -e -x

[ "" = "${TOOL_DIR}" ]
P=$(pwd)
echo ${P##${GPTSCRIPT_VAR_WORKDIR}}
echo ${GPTSCRIPT_TOOL_DIR##${P}}
cd $GPTSCRIPT_TOOL_DIR
cat data.txt