updater.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. dshPackage = "@deepseek-ai/dsh"
  15. allowScripts = "@deepseek-ai/dsh-subprocess-local,koffi,node-pty,@google/genai,protobufjs"
  16. )
  17. // npmGlobalRoot 执行 npm root -g(隐藏窗口),返回全局 node_modules 路径。
  18. func npmGlobalRoot() string {
  19. exe, args := npmCommandFor("root", "-g")
  20. cmd := exec.Command(exe, args...)
  21. hideWindow(cmd)
  22. out, err := cmd.Output()
  23. if err != nil {
  24. return ""
  25. }
  26. return strings.TrimSpace(string(out))
  27. }
  28. // npmCommandFor 组装 npm 调用:优先 node + npm-cli.js,回退 cmd /c npm。
  29. func npmCommandFor(args ...string) (string, []string) {
  30. if node, err := exec.LookPath("node"); err == nil {
  31. if cli := detectNpmCLI(node); cli != "" {
  32. return node, append([]string{cli}, args...)
  33. }
  34. }
  35. return "cmd.exe", append([]string{"/c", "npm"}, args...)
  36. }
  37. func detectNpmCLI(node string) string {
  38. base := filepath.Dir(node)
  39. candidates := []string{
  40. filepath.Join(base, "node_modules", "npm", "bin", "npm-cli.js"),
  41. filepath.Join(base, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"),
  42. }
  43. for _, c := range candidates {
  44. if fileExists(c) {
  45. return c
  46. }
  47. }
  48. return ""
  49. }
  50. // readPackageVersion 读取 package.json 的 version。
  51. func readPackageVersion(path string) string {
  52. data, err := os.ReadFile(path)
  53. if err != nil {
  54. return ""
  55. }
  56. var v struct {
  57. Version string `json:"version"`
  58. }
  59. if err := json.Unmarshal(data, &v); err != nil {
  60. return ""
  61. }
  62. return v.Version
  63. }
  64. // Update 执行全局更新:npm install -g @deepseek-ai/dsh@latest --allow-scripts=...
  65. func (s *Supervisor) Update() (string, error) {
  66. s.mu.Lock()
  67. if s.updating {
  68. s.mu.Unlock()
  69. return "", errors.New("更新已在进行中")
  70. }
  71. s.updating = true
  72. s.mu.Unlock()
  73. defer func() {
  74. s.mu.Lock()
  75. s.updating = false
  76. s.mu.Unlock()
  77. }()
  78. exe, args := npmCommandFor("install", "-g", dshPackage+"@latest", "--allow-scripts="+allowScripts)
  79. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
  80. defer cancel()
  81. cmd := exec.CommandContext(ctx, exe, args...)
  82. hideWindow(cmd)
  83. s.log.Printf("执行更新: %s %s", exe, strings.Join(args, " "))
  84. out, err := cmd.CombinedOutput()
  85. text := strings.TrimSpace(string(out))
  86. if text != "" {
  87. s.log.Printf("更新输出: %s", text)
  88. }
  89. if err != nil {
  90. return text, fmt.Errorf("更新失败: %w", err)
  91. }
  92. s.mu.Lock()
  93. s.binCache = ""
  94. s.mu.Unlock()
  95. return text, nil
  96. }
  97. // LatestVersion 查询 npm 上最新版本。
  98. func (s *Supervisor) LatestVersion() (string, error) {
  99. exe, args := npmCommandFor("view", dshPackage, "version")
  100. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  101. defer cancel()
  102. cmd := exec.CommandContext(ctx, exe, args...)
  103. hideWindow(cmd)
  104. out, err := cmd.Output()
  105. if err != nil {
  106. return "", err
  107. }
  108. return strings.TrimSpace(string(out)), nil
  109. }
  110. // startAutoUpdate 定期后台更新:按间隔轮询,发现新版本才更新并重启。
  111. func (s *Supervisor) startAutoUpdate() {
  112. go func() {
  113. for {
  114. s.mu.Lock()
  115. enabled := s.cfg.AutoUpdate
  116. hours := s.cfg.UpdateIntervalH
  117. s.mu.Unlock()
  118. if !enabled {
  119. s.log.Printf("定期更新:已关闭")
  120. return
  121. }
  122. if hours <= 0 {
  123. hours = 24
  124. }
  125. time.Sleep(time.Duration(hours) * time.Hour)
  126. s.mu.Lock()
  127. enabled = s.cfg.AutoUpdate
  128. s.mu.Unlock()
  129. if !enabled {
  130. return
  131. }
  132. latest, err := s.LatestVersion()
  133. if err != nil {
  134. s.log.Printf("定期更新:查询最新版本失败: %v", err)
  135. continue
  136. }
  137. current := s.installedVersion()
  138. if latest == "" || current == latest {
  139. s.log.Printf("定期更新:已是最新(%s)", current)
  140. continue
  141. }
  142. s.log.Printf("定期更新:发现新版本 %s(当前 %s),开始更新", latest, current)
  143. if _, err := s.Update(); err != nil {
  144. s.log.Printf("定期更新失败: %v", err)
  145. continue
  146. }
  147. s.Restart()
  148. }
  149. }()
  150. }