history.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package readline
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/emirpasic/gods/v2/lists/arraylist"
  11. )
  12. type History struct {
  13. Buf *arraylist.List[string]
  14. Autosave bool
  15. Pos int
  16. Limit int
  17. Filename string
  18. Enabled bool
  19. }
  20. func NewHistory() (*History, error) {
  21. h := &History{
  22. Buf: arraylist.New[string](),
  23. Limit: 100, // resizeme
  24. Autosave: true,
  25. Enabled: true,
  26. }
  27. err := h.Init()
  28. if err != nil {
  29. return nil, err
  30. }
  31. return h, nil
  32. }
  33. func (h *History) Init() error {
  34. home, err := os.UserHomeDir()
  35. if err != nil {
  36. return err
  37. }
  38. path := filepath.Join(home, ".ollama", "history")
  39. if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
  40. return err
  41. }
  42. h.Filename = path
  43. f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0o600)
  44. if err != nil {
  45. if errors.Is(err, os.ErrNotExist) {
  46. return nil
  47. }
  48. return err
  49. }
  50. defer f.Close()
  51. r := bufio.NewReader(f)
  52. for {
  53. line, err := r.ReadString('\n')
  54. if err != nil {
  55. if errors.Is(err, io.EOF) {
  56. break
  57. }
  58. return err
  59. }
  60. line = strings.TrimSpace(line)
  61. if len(line) == 0 {
  62. continue
  63. }
  64. h.Add(line)
  65. }
  66. return nil
  67. }
  68. func (h *History) Add(s string) {
  69. if latest, _ := h.Buf.Get(h.Size() - 1); latest != s {
  70. h.Buf.Add(s)
  71. h.Compact()
  72. h.Pos = h.Size()
  73. if h.Autosave {
  74. _ = h.Save()
  75. }
  76. }
  77. }
  78. func (h *History) Compact() {
  79. s := h.Buf.Size()
  80. if s > h.Limit {
  81. for range s - h.Limit {
  82. h.Buf.Remove(0)
  83. }
  84. }
  85. }
  86. func (h *History) Clear() {
  87. h.Buf.Clear()
  88. }
  89. func (h *History) Prev() (line string) {
  90. if h.Pos > 0 {
  91. h.Pos -= 1
  92. }
  93. line, _ = h.Buf.Get(h.Pos)
  94. return line
  95. }
  96. func (h *History) Next() (line string) {
  97. if h.Pos < h.Buf.Size() {
  98. h.Pos += 1
  99. line, _ = h.Buf.Get(h.Pos)
  100. }
  101. return line
  102. }
  103. func (h *History) Size() int {
  104. return h.Buf.Size()
  105. }
  106. func (h *History) Save() error {
  107. if !h.Enabled {
  108. return nil
  109. }
  110. tmpFile := h.Filename + ".tmp"
  111. f, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC|os.O_APPEND, 0o600)
  112. if err != nil {
  113. return err
  114. }
  115. defer f.Close()
  116. buf := bufio.NewWriter(f)
  117. for cnt := range h.Size() {
  118. line, _ := h.Buf.Get(cnt)
  119. fmt.Fprintln(buf, line)
  120. }
  121. buf.Flush()
  122. f.Close()
  123. if err = os.Rename(tmpFile, h.Filename); err != nil {
  124. return err
  125. }
  126. return nil
  127. }