]> git.giacomolli.uk - onechat/blobdiff - memory.go
Describe child agent directories and limits
[onechat] / memory.go
index 20f46d62ba6d50397cf6a0b7f892f8a59c2cf688..c9fe1c567504c11f0a94be48fdb581298cde510c 100644 (file)
--- a/memory.go
+++ b/memory.go
@@ -247,6 +247,26 @@ func (m *Memory) Flush(ctx context.Context) error {
        return m.syncer.Flush(ctx)
 }
 
        return m.syncer.Flush(ctx)
 }
 
+// Snapshot flushes all memory writes and runs snapshot while no append or
+// summary write can change the log, indexes, tree, or views. snapshot must not
+// call Memory.
+func (m *Memory) Snapshot(ctx context.Context, snapshot func() error) error {
+       m.mutex.Lock()
+       defer m.mutex.Unlock()
+
+       err := m.Flush(ctx)
+       if err != nil {
+               return fmt.Errorf("flush memory snapshot: %w", err)
+       }
+
+       err = snapshot()
+       if err != nil {
+               return fmt.Errorf("copy memory snapshot: %w", err)
+       }
+
+       return nil
+}
+
 // ViewLines returns the lines of the chat view, each with its newline, without
 // the leaf lines of the messages in skip.
 func (m *Memory) ViewLines(skip map[int]bool) []string {
 // ViewLines returns the lines of the chat view, each with its newline, without
 // the leaf lines of the messages in skip.
 func (m *Memory) ViewLines(skip map[int]bool) []string {