-
Notifications
You must be signed in to change notification settings - Fork 242
/
auth.go
71 lines (59 loc) · 1.7 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package auth
import (
"fmt"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/gptscript-ai/gptscript/pkg/builtin"
"github.com/gptscript-ai/gptscript/pkg/context"
"github.com/gptscript-ai/gptscript/pkg/engine"
"github.com/gptscript-ai/gptscript/pkg/runner"
)
func Authorize(ctx engine.Context, input string) (runner.AuthorizerResponse, error) {
defer context.GetPauseFuncFromCtx(ctx.Ctx)()()
if IsSafe(ctx) {
return runner.AuthorizerResponse{
Accept: true,
}, nil
}
var result bool
err := survey.AskOne(&survey.Confirm{
Help: fmt.Sprintf("The full source of the tools is as follows:\n\n%s", ctx.Tool.String()),
Default: true,
Message: ConfirmMessage(ctx, input),
}, &result)
if err != nil {
return runner.AuthorizerResponse{}, err
}
return runner.AuthorizerResponse{
Accept: result,
Message: "Request denied, blocking execution.",
}, nil
}
func IsSafe(ctx engine.Context) bool {
if !ctx.Tool.IsCommand() {
return true
}
_, ok := builtin.SafeTools[strings.Split(ctx.Tool.Instructions, "\n")[0][2:]]
return ok
}
func ConfirmMessage(ctx engine.Context, input string) string {
var (
loc = ctx.Tool.Source.Location
interpreter = strings.Split(ctx.Tool.Instructions, "\n")[0][2:]
)
if ctx.Tool.Source.Repo != nil {
loc = ctx.Tool.Source.Repo.Root
loc = strings.TrimPrefix(loc, "https://")
loc = strings.TrimSuffix(loc, ".git")
loc = filepath.Join(loc, ctx.Tool.Source.Repo.Path, ctx.Tool.Source.Repo.Name)
}
if ctx.Tool.BuiltinFunc != nil {
loc = "Builtin"
}
return fmt.Sprintf(`Description: %s
Interpreter: %s
Source: %s
Input: %s
Allow the above tool to execute?`, ctx.Tool.Description, interpreter, loc, strings.TrimSpace(input))
}