mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
feat:Modify the location where version is obtained, and insert version information into the context (#1300)
* feat:migrate version info from internal package to pkg/config * * fix lint issue
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/config"
|
||||
"github.com/sipeed/picoclaw/pkg/logger"
|
||||
"github.com/sipeed/picoclaw/pkg/providers"
|
||||
"github.com/sipeed/picoclaw/pkg/skills"
|
||||
@@ -80,8 +81,10 @@ func NewContextBuilder(workspace string) *ContextBuilder {
|
||||
func (cb *ContextBuilder) getIdentity() string {
|
||||
workspacePath, _ := filepath.Abs(filepath.Join(cb.workspace))
|
||||
toolDiscovery := cb.getDiscoveryRule()
|
||||
version := config.FormatVersion()
|
||||
|
||||
return fmt.Sprintf(`# picoclaw 🦞
|
||||
return fmt.Sprintf(
|
||||
`# picoclaw 🦞 (%s)
|
||||
|
||||
You are picoclaw, a helpful AI assistant.
|
||||
|
||||
@@ -102,7 +105,7 @@ Your workspace is at: %s
|
||||
4. **Context summaries** - Conversation summaries provided as context are approximate references only. They may be incomplete or outdated. Always defer to explicit user instructions over summary content.
|
||||
|
||||
%s`,
|
||||
workspacePath, workspacePath, workspacePath, workspacePath, workspacePath, toolDiscovery)
|
||||
version, workspacePath, workspacePath, workspacePath, workspacePath, workspacePath, toolDiscovery)
|
||||
}
|
||||
|
||||
func (cb *ContextBuilder) getDiscoveryRule() string {
|
||||
|
||||
@@ -59,6 +59,16 @@ type Config struct {
|
||||
Tools ToolsConfig `json:"tools"`
|
||||
Heartbeat HeartbeatConfig `json:"heartbeat"`
|
||||
Devices DevicesConfig `json:"devices"`
|
||||
// BuildInfo contains build-time version information
|
||||
BuildInfo BuildInfo `json:"build_info,omitempty"`
|
||||
}
|
||||
|
||||
// BuildInfo contains build-time version information
|
||||
type BuildInfo struct {
|
||||
Version string `json:"version"`
|
||||
GitCommit string `json:"git_commit"`
|
||||
BuildTime string `json:"build_time"`
|
||||
GoVersion string `json:"go_version"`
|
||||
}
|
||||
|
||||
// MarshalJSON implements custom JSON marshaling for Config
|
||||
|
||||
@@ -510,5 +510,11 @@ func DefaultConfig() *Config {
|
||||
Enabled: false,
|
||||
MonitorUSB: true,
|
||||
},
|
||||
BuildInfo: BuildInfo{
|
||||
Version: Version,
|
||||
GitCommit: GitCommit,
|
||||
BuildTime: BuildTime,
|
||||
GoVersion: GoVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// Build-time variables injected via ldflags during build process.
|
||||
// These are set by the Makefile or .goreleaser.yaml using the -X flag:
|
||||
//
|
||||
// -X github.com/sipeed/picoclaw/pkg/config.Version=<version>
|
||||
// -X github.com/sipeed/picoclaw/pkg/config.GitCommit=<commit>
|
||||
// -X github.com/sipeed/picoclaw/pkg/config.BuildTime=<timestamp>
|
||||
// -X github.com/sipeed/picoclaw/pkg/config.GoVersion=<go-version>
|
||||
var (
|
||||
Version = "dev" // Default value when not built with ldflags
|
||||
GitCommit string // Git commit SHA (short)
|
||||
BuildTime string // Build timestamp in RFC3339 format
|
||||
GoVersion string // Go version used for building
|
||||
)
|
||||
|
||||
// FormatVersion returns the version string with optional git commit
|
||||
func FormatVersion() string {
|
||||
v := Version
|
||||
if GitCommit != "" {
|
||||
v += fmt.Sprintf(" (git: %s)", GitCommit)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// FormatBuildInfo returns build time and go version info
|
||||
func FormatBuildInfo() (string, string) {
|
||||
build := BuildTime
|
||||
goVer := GoVersion
|
||||
if goVer == "" {
|
||||
goVer = runtime.Version()
|
||||
}
|
||||
return build, goVer
|
||||
}
|
||||
|
||||
// GetVersion returns the version string
|
||||
func GetVersion() string {
|
||||
return Version
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFormatVersion_NoGitCommit(t *testing.T) {
|
||||
oldVersion, oldGit := Version, GitCommit
|
||||
t.Cleanup(func() { Version, GitCommit = oldVersion, oldGit })
|
||||
|
||||
Version = "1.2.3"
|
||||
GitCommit = ""
|
||||
|
||||
assert.Equal(t, "1.2.3", FormatVersion())
|
||||
}
|
||||
|
||||
func TestFormatVersion_WithGitCommit(t *testing.T) {
|
||||
oldVersion, oldGit := Version, GitCommit
|
||||
t.Cleanup(func() { Version, GitCommit = oldVersion, oldGit })
|
||||
|
||||
Version = "1.2.3"
|
||||
GitCommit = "abc123"
|
||||
|
||||
assert.Equal(t, "1.2.3 (git: abc123)", FormatVersion())
|
||||
}
|
||||
|
||||
func TestFormatBuildInfo_UsesBuildTimeAndGoVersion_WhenSet(t *testing.T) {
|
||||
oldBuildTime, oldGoVersion := BuildTime, GoVersion
|
||||
t.Cleanup(func() { BuildTime, GoVersion = oldBuildTime, oldGoVersion })
|
||||
|
||||
BuildTime = "2026-02-20T00:00:00Z"
|
||||
GoVersion = "go1.23.0"
|
||||
|
||||
build, goVer := FormatBuildInfo()
|
||||
|
||||
assert.Equal(t, BuildTime, build)
|
||||
assert.Equal(t, GoVersion, goVer)
|
||||
}
|
||||
|
||||
func TestFormatBuildInfo_EmptyBuildTime_ReturnsEmptyBuild(t *testing.T) {
|
||||
oldBuildTime, oldGoVersion := BuildTime, GoVersion
|
||||
t.Cleanup(func() { BuildTime, GoVersion = oldBuildTime, oldGoVersion })
|
||||
|
||||
BuildTime = ""
|
||||
GoVersion = "go1.23.0"
|
||||
|
||||
build, goVer := FormatBuildInfo()
|
||||
|
||||
assert.Empty(t, build)
|
||||
assert.Equal(t, GoVersion, goVer)
|
||||
}
|
||||
|
||||
func TestFormatBuildInfo_EmptyGoVersion_FallsBackToRuntimeVersion(t *testing.T) {
|
||||
oldBuildTime, oldGoVersion := BuildTime, GoVersion
|
||||
t.Cleanup(func() { BuildTime, GoVersion = oldBuildTime, oldGoVersion })
|
||||
|
||||
BuildTime = "x"
|
||||
GoVersion = ""
|
||||
|
||||
build, goVer := FormatBuildInfo()
|
||||
|
||||
assert.Equal(t, "x", build)
|
||||
assert.Equal(t, runtime.Version(), goVer)
|
||||
}
|
||||
|
||||
func TestGetVersion(t *testing.T) {
|
||||
oldVersion := Version
|
||||
t.Cleanup(func() { Version = oldVersion })
|
||||
|
||||
Version = "dev"
|
||||
assert.Equal(t, "dev", GetVersion())
|
||||
}
|
||||
|
||||
func TestGetVersion_Custom(t *testing.T) {
|
||||
oldVersion := Version
|
||||
t.Cleanup(func() { Version = oldVersion })
|
||||
|
||||
Version = "v1.0.0"
|
||||
assert.Equal(t, "v1.0.0", GetVersion())
|
||||
}
|
||||
|
||||
func TestVersion_DefaultIsDev(t *testing.T) {
|
||||
// Reset to default values
|
||||
oldVersion := Version
|
||||
Version = "dev"
|
||||
t.Cleanup(func() { Version = oldVersion })
|
||||
|
||||
assert.Equal(t, "dev", Version)
|
||||
}
|
||||
Reference in New Issue
Block a user