history.go 2.3 KB

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