/
log.go
175 lines (125 loc) · 2.92 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package thoughtstream
import (
"encoding/binary"
"encoding/json"
"fmt"
"os"
"path"
"sync"
"time"
"git.mills.io/prologic/bitcask"
"github.com/pkg/errors"
"github.com/greenboxal/agibootstrap/pkg/platform/stdlib/iterators"
"github.com/greenboxal/agibootstrap/pkg/psi"
)
type ThoughLogListener func(msg *Thought)
type ThoughtLog struct {
psi.NodeBase
mu sync.RWMutex
name string
messages []*Thought
lastMessageTs time.Time
f *os.File
log *bitcask.Bitcask
manager *Manager
}
func NewThoughtLog(manager *Manager, name string, basePath string) (*ThoughtLog, error) {
if err := os.MkdirAll(basePath, 0755); err != nil {
return nil, err
}
f, err := os.OpenFile(path.Join(basePath, name+".md"), os.O_CREATE|os.O_APPEND|os.O_WRONLY|os.O_SYNC, 0644)
if err != nil {
return nil, err
}
logPath := path.Join(basePath, name+".cask")
log, err := bitcask.Open(logPath)
if err != nil {
return nil, err
}
tl := &ThoughtLog{
name: name,
manager: manager,
f: f,
log: log,
}
tl.Init(tl, "")
return tl, nil
}
func (cl *ThoughtLog) PsiNodeName() string { return cl.name }
func (cl *ThoughtLog) Name() string { return cl.name }
func (cl *ThoughtLog) Messages() []*Thought { return cl.messages }
func (cl *ThoughtLog) Push(m *Thought) error {
var key [8]byte
binary.BigEndian.PutUint64(key[:], uint64(m.Pointer.Timestamp.UnixNano()))
data, err := json.Marshal(m)
if err != nil {
return err
}
doPush := func() error {
cl.mu.Lock()
defer cl.mu.Unlock()
if m.Pointer.Timestamp.Before(cl.lastMessageTs) {
return errors.New("message is older than last message")
}
if cl.log != nil {
if err := cl.log.Put(key[:], data); err != nil {
return err
}
}
if cl.f != nil {
str := fmt.Sprintf("## %s (%s):\n%s\n", m.From.Role, m.From.Name, m.Text)
_, err := cl.f.Write([]byte(str))
if err != nil {
return err
}
}
m.SetParent(cl)
cl.messages = append(cl.messages, m)
cl.lastMessageTs = m.Pointer.Timestamp
return nil
}
if err := doPush(); err != nil {
return err
}
return nil
}
func (cl *ThoughtLog) Close() error {
if cl.log != nil {
return cl.log.Close()
}
return nil
}
func (cl *ThoughtLog) ForkTemporary() *ThoughtLog {
name := fmt.Sprintf("%s-%d", cl.name, time.Now().UnixNano())
fork, err := NewThoughtLog(cl.manager, name, cl.f.Name()+".forktree")
if err != nil {
panic(err)
}
for _, t := range cl.messages {
if err := fork.Push(t); err != nil {
panic(err)
}
}
return fork
}
func (cl *ThoughtLog) GC() error {
if cl.log != nil {
if err := cl.GC(); err != nil {
return err
}
}
return nil
}
func (cl *ThoughtLog) EpochBarrier() {
cl.mu.Lock()
defer cl.mu.Unlock()
cl.messages = cl.messages[0:0]
}
func (cl *ThoughtLog) MessagesIterator() iterators.Iterator[*Thought] {
return iterators.FromSlice(cl.messages)
}
func (cl *ThoughtLog) BeginNext() *Thought {
t := NewThought()
t.SetParent(cl)
return t
}