forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
304 lines (260 loc) · 7.98 KB
/
get.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package repos
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/BurntSushi/locker"
"github.com/gptscript-ai/gptscript/pkg/config"
"github.com/gptscript-ai/gptscript/pkg/credentials"
"github.com/gptscript-ai/gptscript/pkg/hash"
"github.com/gptscript-ai/gptscript/pkg/repos/git"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/golang"
"github.com/gptscript-ai/gptscript/pkg/types"
)
type Runtime interface {
ID() string
Supports(tool types.Tool, cmd []string) bool
Binary(ctx context.Context, tool types.Tool, dataRoot, toolSource string, env []string) (bool, []string, error)
Setup(ctx context.Context, tool types.Tool, dataRoot, toolSource string, env []string) ([]string, error)
GetHash(tool types.Tool) (string, error)
}
type noopRuntime struct {
}
func (n noopRuntime) ID() string {
return "none"
}
func (n noopRuntime) GetHash(_ types.Tool) (string, error) {
return "", nil
}
func (n noopRuntime) Supports(_ types.Tool, _ []string) bool {
return false
}
func (n noopRuntime) Binary(_ context.Context, _ types.Tool, _, _ string, _ []string) (bool, []string, error) {
return false, nil, nil
}
func (n noopRuntime) Setup(_ context.Context, _ types.Tool, _, _ string, _ []string) ([]string, error) {
return nil, nil
}
type Manager struct {
cacheDir string
storageDir string
gitDir string
runtimeDir string
runtimes []Runtime
credHelperConfig *credHelperConfig
}
type credHelperConfig struct {
lock sync.Mutex
initialized bool
cliCfg *config.CLIConfig
}
func New(cacheDir string, runtimes ...Runtime) *Manager {
root := filepath.Join(cacheDir, "repos")
return &Manager{
cacheDir: cacheDir,
storageDir: root,
gitDir: filepath.Join(root, "git"),
runtimeDir: filepath.Join(root, "runtimes"),
runtimes: runtimes,
}
}
func (m *Manager) EnsureCredentialHelpers(ctx context.Context) error {
if m.credHelperConfig == nil {
return nil
}
m.credHelperConfig.lock.Lock()
defer m.credHelperConfig.lock.Unlock()
if !m.credHelperConfig.initialized {
if err := m.deferredSetUpCredentialHelpers(ctx, m.credHelperConfig.cliCfg); err != nil {
return err
}
m.credHelperConfig.initialized = true
}
return nil
}
func (m *Manager) SetUpCredentialHelpers(_ context.Context, cliCfg *config.CLIConfig) error {
m.credHelperConfig = &credHelperConfig{
cliCfg: cliCfg,
}
return nil
}
func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *config.CLIConfig) error {
var (
helperName = cliCfg.CredentialsStore
distInfo, suffix string
)
// The file helper is built-in and does not need to be downloaded.
if helperName == config.File {
return nil
}
switch helperName {
case config.Wincred:
suffix = ".exe"
default:
distInfo = fmt.Sprintf("-%s-%s", runtime.GOOS, runtime.GOARCH)
}
repoName := credentials.RepoNameForCredentialStore(helperName)
locker.Lock(repoName)
defer locker.Unlock(repoName)
credHelperDirs := credentials.GetCredentialHelperDirs(m.cacheDir, helperName)
// Load the last-checked file to make sure we haven't checked the repo in the last 24 hours.
now := time.Now()
lastChecked, err := os.ReadFile(credHelperDirs.LastCheckedFile)
if err == nil {
if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(lastChecked))); err == nil && now.Sub(t) < 24*time.Hour {
// Make sure the binary still exists, and if it does, return.
if _, err := os.Stat(filepath.Join(credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil {
log.Debugf("Credential helper %s up-to-date as of %v, checking for updates after %v", helperName, t, t.Add(24*time.Hour))
return nil
}
}
}
if err := os.MkdirAll(filepath.Dir(credHelperDirs.LastCheckedFile), 0755); err != nil {
return err
}
// Update the last-checked file.
if err := os.WriteFile(credHelperDirs.LastCheckedFile, []byte(now.Format(time.RFC3339)), 0644); err != nil {
return err
}
gitURL, err := credentials.GitURLForRepoName(repoName)
if err != nil {
return err
}
tool := types.Tool{
ToolDef: types.ToolDef{
Parameters: types.Parameters{
Name: repoName,
},
},
Source: types.ToolSource{
Repo: &types.Repo{
Root: gitURL,
},
},
}
tag, err := golang.GetLatestTag(tool)
if err != nil {
return err
}
var needsDownloaded bool
// Check the last revision shasum and see if it is different from the current one.
lastRevision, err := os.ReadFile(credHelperDirs.RevisionFile)
if (err == nil && strings.TrimSpace(string(lastRevision)) != tool.Source.Repo.Root+tag) || errors.Is(err, fs.ErrNotExist) {
// Need to pull the latest version.
needsDownloaded = true
// Update the revision file to the new revision.
if err = os.WriteFile(credHelperDirs.RevisionFile, []byte(tool.Source.Repo.Root+tag), 0644); err != nil {
return err
}
} else if err != nil {
return err
}
if !needsDownloaded {
// Check for the existence of the credential helper binary.
// If it's there, we have no need to download it and can just return.
if _, err = os.Stat(filepath.Join(credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil {
return nil
}
}
// Find the Go runtime and use it to build the credential helper.
for _, rt := range m.runtimes {
if strings.HasPrefix(rt.ID(), "go") {
return rt.(*golang.Runtime).DownloadCredentialHelper(ctx, tool, helperName, distInfo, suffix, credHelperDirs.BinDir)
}
}
return fmt.Errorf("no Go runtime found to build the credential helper")
}
func (m *Manager) setup(ctx context.Context, runtime Runtime, tool types.Tool, env []string) (string, []string, error) {
locker.Lock(tool.ID)
defer locker.Unlock(tool.ID)
runtimeHash, err := runtime.GetHash(tool)
if err != nil {
return "", nil, err
}
target := filepath.Join(m.storageDir, tool.Source.Repo.Revision, tool.Source.Repo.Path, tool.Source.Repo.Name, runtime.ID())
targetFinal := filepath.Join(target, tool.Source.Repo.Path+runtimeHash)
doneFile := targetFinal + ".done"
envData, err := os.ReadFile(doneFile)
if err == nil {
var savedEnv []string
if err := json.Unmarshal(envData, &savedEnv); err == nil {
return targetFinal, append(env, savedEnv...), nil
}
} else if !errors.Is(err, fs.ErrNotExist) {
return "", nil, err
}
// Cleanup previous failed runs
_ = os.RemoveAll(doneFile + ".tmp")
_ = os.RemoveAll(doneFile)
_ = os.RemoveAll(target)
var (
newEnv []string
isBinary bool
)
if isBinary, newEnv, err = runtime.Binary(ctx, tool, m.runtimeDir, targetFinal, env); err != nil {
return "", nil, err
} else if !isBinary {
if tool.Source.Repo.VCS == "git" {
if err := git.Checkout(ctx, m.gitDir, tool.Source.Repo.Root, tool.Source.Repo.Revision, target); err != nil {
return "", nil, err
}
} else {
if err := os.MkdirAll(target, 0755); err != nil {
return "", nil, err
}
}
newEnv, err = runtime.Setup(ctx, tool, m.runtimeDir, targetFinal, env)
if err != nil {
return "", nil, err
}
}
out, err := os.Create(doneFile + ".tmp")
if err != nil {
return "", nil, err
}
defer out.Close()
if err := json.NewEncoder(out).Encode(newEnv); err != nil {
return "", nil, err
}
if err := out.Close(); err != nil {
return "", nil, err
}
return targetFinal, append(env, newEnv...), os.Rename(doneFile+".tmp", doneFile)
}
func (m *Manager) GetContext(ctx context.Context, tool types.Tool, cmd, env []string) (string, []string, error) {
var isLocal bool
if tool.Source.Repo == nil {
isLocal = true
d, _ := json.Marshal(tool)
id := hash.Digest(d)[:12]
tool.Source.Repo = &types.Repo{
VCS: "<local>",
Root: id,
Path: "/",
Name: id,
Revision: id,
}
}
for _, runtime := range m.runtimes {
if runtime.Supports(tool, cmd) {
log.Debugf("Runtime %s supports %v", runtime.ID(), cmd)
wd, env, err := m.setup(ctx, runtime, tool, env)
if isLocal {
wd = tool.WorkingDir
}
return wd, env, err
}
}
if isLocal {
return tool.WorkingDir, env, nil
}
return m.setup(ctx, &noopRuntime{}, tool, env)
}