Skip to content

Commit

Permalink
enhance: add gptscript module version to /api/version
Browse files Browse the repository at this point in the history
- Add the module version for `github.com/gptscript-ai/gptscript` to
  the `/api/version` API response
- Change `/api/version` API response field name from `otto` to `obot`

Signed-off-by: Nick Hale <[email protected]>
  • Loading branch information
njhale committed Dec 17, 2024
1 parent 32498e9 commit 264972e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
56 changes: 53 additions & 3 deletions pkg/api/handlers/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand All @@ -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, "-")
}

0 comments on commit 264972e

Please sign in to comment.