diff --git a/go.mod b/go.mod index 43a17ae2..0f9c2fb9 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/spf13/cobra v1.8.1 golang.org/x/crypto v0.31.0 golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c + golang.org/x/mod v0.21.0 golang.org/x/term v0.27.0 golang.org/x/text v0.21.0 gopkg.in/yaml.v3 v3.0.1 @@ -254,7 +255,6 @@ require ( go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.26.0 // indirect go4.org v0.0.0-20230225012048-214862532bf5 // indirect - golang.org/x/mod v0.21.0 // indirect golang.org/x/net v0.30.0 // indirect golang.org/x/oauth2 v0.23.0 // indirect golang.org/x/sync v0.10.0 // indirect diff --git a/pkg/api/handlers/version.go b/pkg/api/handlers/version.go index c9e7cdb0..95e29c22 100644 --- a/pkg/api/handlers/version.go +++ b/pkg/api/handlers/version.go @@ -2,19 +2,24 @@ package handlers import ( "os" + "runtime/debug" + "strings" "github.com/obot-platform/obot/pkg/api" "github.com/obot-platform/obot/pkg/version" + "golang.org/x/mod/module" "sigs.k8s.io/yaml" ) type VersionHandler struct { - emailDomain string + gptscriptVersion string + emailDomain string } func NewVersionHandler(emailDomain string) *VersionHandler { return &VersionHandler{ - emailDomain: emailDomain, + emailDomain: emailDomain, + gptscriptVersion: getGPTScriptVersion(), } } @@ -30,7 +35,52 @@ func (v *VersionHandler) getVersionResponse() map[string]string { values["error"] = err.Error() } } - values["otto"] = version.Get().String() + values["obot"] = version.Get().String() + values["gptscript"] = v.gptscriptVersion values["emailDomain"] = v.emailDomain return values } + +const gptscriptModulePath = "github.com/gptscript-ai/gptscript" + +func getGPTScriptVersion() string { + bi, _ := debug.ReadBuildInfo() + + var gptscriptVersion string + for _, dep := range bi.Deps { + if dep.Path == gptscriptModulePath { + gptscriptVersion = simplifyModuleVersion(dep.Version) + break + } + } + + return gptscriptVersion +} + +// simplifyModuleVersion returns a simplified variant of a given module version string. +// If the given version is a Go pseudo-version, it strips the timestamp and truncates the revision to the first 7 characters. +// Empty strings and non-Go pseudo-versions are returned unaltered. +func simplifyModuleVersion(version string) string { + if version == "" || !module.IsPseudoVersion(version) { + return version + } + + // Extract the base version (tag) and revision (commit hash) + // Ignore errors, this should never happen if compilation succeeded + components := make([]string, 0, 2) + if base, err := module.PseudoVersionBase(version); err == nil && base != "" { + components = append(components, base) + } + + if rev, err := module.PseudoVersionRev(version); err == nil && len(rev) > 0 { + // Shorten the hash to the first 7 characters + if len(rev) > 7 { + rev = rev[:7] + } + + components = append(components, rev) + } + + // Combine the base version with the shortened hash + return strings.Join(components, "-") +}