@@ -18,7 +18,6 @@ import (
1818 "runtime"
1919 "strings"
2020
21- "github.com/gptscript-ai/gptscript/pkg/credentials"
2221 "github.com/gptscript-ai/gptscript/pkg/debugcmd"
2322 runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
2423 "github.com/gptscript-ai/gptscript/pkg/hash"
@@ -97,6 +96,14 @@ type tag struct {
9796 } `json:"commit"`
9897}
9998
99+ func GetLatestTag (tool types.Tool ) (string , error ) {
100+ r , ok := getLatestRelease (tool )
101+ if ! ok {
102+ return "" , fmt .Errorf ("failed to get latest release for %s" , tool .Name )
103+ }
104+ return r .label , nil
105+ }
106+
100107func getLatestRelease (tool types.Tool ) (* release , bool ) {
101108 if tool .Source .Repo == nil || ! strings .HasPrefix (tool .Source .Repo .Root , "https://github.com/" ) {
102109 return nil , false
@@ -116,11 +123,14 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
116123 account , repo := parts [1 ], parts [2 ]
117124
118125 resp , err := client .Get (fmt .Sprintf ("https://api.github.com/repos/%s/%s/tags" , account , repo ))
119- if err != nil || resp . StatusCode != http . StatusOK {
126+ if err != nil {
120127 // ignore error
121128 return nil , false
122129 }
123130 defer resp .Body .Close ()
131+ if resp .StatusCode != http .StatusOK {
132+ return nil , false
133+ }
124134
125135 var tags []tag
126136 if err := json .NewDecoder (resp .Body ).Decode (& tags ); err != nil {
@@ -137,11 +147,14 @@ func getLatestRelease(tool types.Tool) (*release, bool) {
137147 }
138148
139149 resp , err = client .Get (fmt .Sprintf ("https://github.com/%s/%s/releases/latest" , account , repo ))
140- if err != nil || resp . StatusCode != http . StatusFound {
150+ if err != nil {
141151 // ignore error
142152 return nil , false
143153 }
144154 defer resp .Body .Close ()
155+ if resp .StatusCode != http .StatusFound {
156+ return nil , false
157+ }
145158
146159 target := resp .Header .Get ("Location" )
147160 if target == "" {
@@ -212,7 +225,7 @@ func downloadBin(ctx context.Context, checksum, src, url, bin string) error {
212225 return nil
213226}
214227
215- func getChecksum (ctx context.Context , rel * release ) string {
228+ func getChecksum (ctx context.Context , rel * release , artifactName string ) string {
216229 resp , err := get (ctx , rel .checksumTxt ())
217230 if err != nil {
218231 // ignore error
@@ -223,7 +236,7 @@ func getChecksum(ctx context.Context, rel *release) string {
223236 scan := bufio .NewScanner (resp .Body )
224237 for scan .Scan () {
225238 fields := strings .Fields (scan .Text ())
226- if len (fields ) == 2 && (fields [1 ] == rel . srcBinName () || fields [1 ] == "*" + rel . srcBinName () ) {
239+ if len (fields ) == 2 && (fields [1 ] == artifactName || fields [1 ] == "*" + artifactName ) {
227240 return fields [0 ]
228241 }
229242 }
@@ -241,7 +254,7 @@ func (r *Runtime) Binary(ctx context.Context, tool types.Tool, _, toolSource str
241254 return false , nil , nil
242255 }
243256
244- checksum := getChecksum (ctx , rel )
257+ checksum := getChecksum (ctx , rel , rel . srcBinName () )
245258 if checksum == "" {
246259 return false , nil , nil
247260 }
@@ -268,30 +281,28 @@ func (r *Runtime) Setup(ctx context.Context, _ types.Tool, dataRoot, toolSource
268281 return newEnv , nil
269282}
270283
271- func (r * Runtime ) BuildCredentialHelper (ctx context.Context , helperName string , credHelperDirs credentials. CredentialHelperDirs , dataRoot , revision string , env [] string ) error {
284+ func (r * Runtime ) DownloadCredentialHelper (ctx context.Context , tool types. Tool , helperName , distInfo , suffix string , binDir string ) error {
272285 if helperName == "file" {
273286 return nil
274287 }
275288
276- var suffix string
277- if helperName == "wincred" {
278- suffix = ".exe"
289+ rel , ok := getLatestRelease (tool )
290+ if ! ok {
291+ return fmt .Errorf ("failed to find %s release" , r .ID ())
292+ }
293+ binaryName := "gptscript-credential-" + helperName
294+ checksum := getChecksum (ctx , rel , binaryName + distInfo + suffix )
295+ if checksum == "" {
296+ return fmt .Errorf ("failed to find %s release checksum for os=%s arch=%s" , r .ID (), runtime .GOOS , runtime .GOARCH )
279297 }
280298
281- binPath , err := r .getRuntime (ctx , dataRoot )
282- if err != nil {
283- return err
299+ url , _ := strings .CutSuffix (rel .binURL (), rel .srcBinName ())
300+ url += binaryName + distInfo + suffix
301+ if err := downloadBin (ctx , checksum , strings .TrimSuffix (binDir , "bin" ), url , binaryName + suffix ); err != nil {
302+ return fmt .Errorf ("failed to download %s release for os=%s arch=%s: %w" , r .ID (), runtime .GOOS , runtime .GOARCH , err )
284303 }
285- newEnv := runtimeEnv .AppendPath (env , binPath )
286304
287- log .InfofCtx (ctx , "Building credential helper %s" , helperName )
288- cmd := debugcmd .New (ctx , filepath .Join (binPath , "go" ),
289- "build" , "-buildvcs=false" , "-o" ,
290- filepath .Join (credHelperDirs .BinDir , "gptscript-credential-" + helperName + suffix ),
291- fmt .Sprintf ("./%s/cmd/" , helperName ))
292- cmd .Env = stripGo (append (env , newEnv ... ))
293- cmd .Dir = filepath .Join (credHelperDirs .RepoDir , revision )
294- return cmd .Run ()
305+ return nil
295306}
296307
297308func (r * Runtime ) getReleaseAndDigest () (string , string , error ) {
0 commit comments