diff --git a/pkg/credentials/util.go b/pkg/credentials/util.go index cf3291d7..f6165308 100644 --- a/pkg/credentials/util.go +++ b/pkg/credentials/util.go @@ -5,13 +5,14 @@ import ( ) type CredentialHelperDirs struct { - RevisionFile, BinDir, RepoDir string + RevisionFile, LastCheckedFile, BinDir, RepoDir string } func GetCredentialHelperDirs(cacheDir string) CredentialHelperDirs { return CredentialHelperDirs{ - RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"), - BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"), - RepoDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "repo"), + RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"), + LastCheckedFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "last-checked"), + BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"), + RepoDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "repo"), } } diff --git a/pkg/repos/get.go b/pkg/repos/get.go index 68b2e427..b3d5f609 100644 --- a/pkg/repos/get.go +++ b/pkg/repos/get.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" "sync" + "time" "github.com/BurntSushi/locker" "github.com/gptscript-ai/gptscript/pkg/config" @@ -112,11 +113,30 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co locker.Lock("gptscript-credential-helpers") defer locker.Unlock("gptscript-credential-helpers") + // 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(m.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(m.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 + } + } + } + + // Load the credential helpers repo information. _, repo, _, err := github.Load(ctx, nil, credentialHelpersRepo) if err != nil { return err } + // Update the last-checked file. + if err := os.WriteFile(m.credHelperDirs.LastCheckedFile, []byte(now.Format(time.RFC3339)), 0644); err != nil { + return err + } + var needsBuild bool // Check the last revision shasum and see if it is different from the current one.