config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package config
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "time"
  8. "github.com/BurntSushi/toml"
  9. )
  10. type ModelUsage struct {
  11. ProviderID string `toml:"provider_id"`
  12. ModelID string `toml:"model_id"`
  13. LastUsed time.Time `toml:"last_used"`
  14. }
  15. type ModeModel struct {
  16. ProviderID string `toml:"provider_id"`
  17. ModelID string `toml:"model_id"`
  18. }
  19. type State struct {
  20. Theme string `toml:"theme"`
  21. ModeModel map[string]ModeModel `toml:"mode_model"`
  22. Provider string `toml:"provider"`
  23. Model string `toml:"model"`
  24. Mode string `toml:"mode"`
  25. RecentlyUsedModels []ModelUsage `toml:"recently_used_models"`
  26. MessagesRight bool `toml:"messages_right"`
  27. SplitDiff bool `toml:"split_diff"`
  28. }
  29. func NewState() *State {
  30. return &State{
  31. Theme: "opencode",
  32. Mode: "build",
  33. ModeModel: make(map[string]ModeModel),
  34. RecentlyUsedModels: make([]ModelUsage, 0),
  35. }
  36. }
  37. // UpdateModelUsage updates the recently used models list with the specified model
  38. func (s *State) UpdateModelUsage(providerID, modelID string) {
  39. now := time.Now()
  40. // Check if this model is already in the list
  41. for i, usage := range s.RecentlyUsedModels {
  42. if usage.ProviderID == providerID && usage.ModelID == modelID {
  43. s.RecentlyUsedModels[i].LastUsed = now
  44. usage := s.RecentlyUsedModels[i]
  45. copy(s.RecentlyUsedModels[1:i+1], s.RecentlyUsedModels[0:i])
  46. s.RecentlyUsedModels[0] = usage
  47. return
  48. }
  49. }
  50. newUsage := ModelUsage{
  51. ProviderID: providerID,
  52. ModelID: modelID,
  53. LastUsed: now,
  54. }
  55. // Prepend to slice and limit to last 50 entries
  56. s.RecentlyUsedModels = append([]ModelUsage{newUsage}, s.RecentlyUsedModels...)
  57. if len(s.RecentlyUsedModels) > 50 {
  58. s.RecentlyUsedModels = s.RecentlyUsedModels[:50]
  59. }
  60. }
  61. func (s *State) RemoveModelFromRecentlyUsed(providerID, modelID string) {
  62. for i, usage := range s.RecentlyUsedModels {
  63. if usage.ProviderID == providerID && usage.ModelID == modelID {
  64. s.RecentlyUsedModels = append(s.RecentlyUsedModels[:i], s.RecentlyUsedModels[i+1:]...)
  65. return
  66. }
  67. }
  68. }
  69. // SaveState writes the provided Config struct to the specified TOML file.
  70. // It will create the file if it doesn't exist, or overwrite it if it does.
  71. func SaveState(filePath string, state *State) error {
  72. file, err := os.Create(filePath)
  73. if err != nil {
  74. return fmt.Errorf("failed to create/open config file %s: %w", filePath, err)
  75. }
  76. defer file.Close()
  77. writer := bufio.NewWriter(file)
  78. encoder := toml.NewEncoder(writer)
  79. if err := encoder.Encode(state); err != nil {
  80. return fmt.Errorf("failed to encode state to TOML file %s: %w", filePath, err)
  81. }
  82. if err := writer.Flush(); err != nil {
  83. return fmt.Errorf("failed to flush writer for state file %s: %w", filePath, err)
  84. }
  85. slog.Debug("State saved to file", "file", filePath)
  86. return nil
  87. }
  88. // LoadState loads the state from the specified TOML file.
  89. // It returns a pointer to the State struct and an error if any issues occur.
  90. func LoadState(filePath string) (*State, error) {
  91. var state State
  92. if _, err := toml.DecodeFile(filePath, &state); err != nil {
  93. if _, statErr := os.Stat(filePath); os.IsNotExist(statErr) {
  94. return nil, fmt.Errorf("state file not found at %s: %w", filePath, statErr)
  95. }
  96. return nil, fmt.Errorf("failed to decode TOML from file %s: %w", filePath, err)
  97. }
  98. return &state, nil
  99. }