memory

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountMessageTokens

func CountMessageTokens(msg session.Message) int

CountMessageTokens returns the estimated token count for a single message.

func CountMessagesTokens

func CountMessagesTokens(msgs []session.Message) int

CountMessagesTokens returns the total estimated token count for a batch of messages.

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens delegates to types.EstimateTokens for token count approximation.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer manages background observation and reflection processing.

func NewBuffer

func NewBuffer(
	observer *Observer,
	reflector *Reflector,
	store *Store,
	msgThreshold, obsThreshold int,
	getMessages MessageProvider,
	logger *zap.SugaredLogger,
) *Buffer

NewBuffer creates a new asynchronous observation buffer.

func (*Buffer) SetCompactor

func (b *Buffer) SetCompactor(c MessageCompactor)

SetCompactor enables message compaction after observation. When set, observed messages are replaced with a summary message in the session, effectively reducing the session's memory footprint.

func (*Buffer) SetReflectionConsolidationThreshold

func (b *Buffer) SetReflectionConsolidationThreshold(n int)

SetReflectionConsolidationThreshold overrides the default number of reflections that must accumulate before meta-reflection (consolidation) is triggered.

func (*Buffer) Start

func (b *Buffer) Start(wg *sync.WaitGroup)

Start launches the background goroutine. The WaitGroup is incremented so callers can wait for graceful shutdown.

func (*Buffer) Stop

func (b *Buffer) Stop()

Stop signals the background goroutine to stop and waits for completion.

func (*Buffer) Trigger

func (b *Buffer) Trigger(sessionKey string)

Trigger sends a non-blocking signal to process the given session.

type GraphHooks

type GraphHooks struct {
	// contains filtered or unexported fields
}

GraphHooks adds graph relationship tracking to the memory system. It generates triples for temporal ordering, session membership, and reflection-observation links.

func NewGraphHooks

func NewGraphHooks(callback TripleCallback, logger *zap.SugaredLogger) *GraphHooks

NewGraphHooks creates a new graph hooks instance.

func (*GraphHooks) OnObservation

func (h *GraphHooks) OnObservation(obs Observation, previousObsID string)

OnObservation generates graph triples when an observation is saved. Triples created:

  • observation:ID --[in_session]--> session:SessionKey
  • observation:ID --[follows]--> observation:PreviousID (if provided)

func (*GraphHooks) OnReflection

func (h *GraphHooks) OnReflection(ref Reflection, observationIDs []string)

OnReflection generates graph triples when a reflection is saved. Triples created:

  • reflection:ID --[in_session]--> session:SessionKey
  • reflection:ID --[reflects_on]--> observation:ObsID (for each related observation)

type MessageCompactor

type MessageCompactor func(sessionKey string, upToIndex int, summary string) error

MessageCompactor replaces observed messages with a summary to reduce session size.

type MessageProvider

type MessageProvider func(sessionKey string) ([]session.Message, error)

MessageProvider retrieves messages for a session key.

type Observation

type Observation struct {
	ID               uuid.UUID
	SessionKey       string
	Content          string
	TokenCount       int
	SourceStartIndex int
	SourceEndIndex   int
	CreatedAt        time.Time
}

Observation represents a compressed note from conversation history.

type Observer

type Observer struct {
	// contains filtered or unexported fields
}

Observer generates compressed observation notes from conversation history.

func NewObserver

func NewObserver(generator TextGenerator, store *Store, logger *zap.SugaredLogger) *Observer

NewObserver creates a new Observer.

func (*Observer) Observe

func (o *Observer) Observe(ctx context.Context, sessionKey string, messages []session.Message, lastObservedIndex int) (*Observation, error)

Observe generates a compressed observation from un-observed messages. It takes messages from lastObservedIndex+1 onward, calls the LLM, and persists the result. Returns nil if there are no new messages to observe.

type Reflection

type Reflection struct {
	ID         uuid.UUID
	SessionKey string
	Content    string
	TokenCount int
	Generation int
	CreatedAt  time.Time
}

Reflection represents condensed observations.

type Reflector

type Reflector struct {
	// contains filtered or unexported fields
}

Reflector condenses accumulated observations into reflections.

func NewReflector

func NewReflector(generator TextGenerator, store *Store, logger *zap.SugaredLogger) *Reflector

NewReflector creates a new Reflector.

func (*Reflector) Reflect

func (r *Reflector) Reflect(ctx context.Context, sessionKey string) (*Reflection, error)

Reflect condenses all observations for a session into a single reflection. Returns nil if there are no observations to condense.

func (*Reflector) ReflectOnReflections

func (r *Reflector) ReflectOnReflections(ctx context.Context, sessionKey string) (*Reflection, error)

ReflectOnReflections condenses accumulated reflections into a higher-generation reflection. Returns nil if there are no reflections to condense.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store provides CRUD operations for observations and reflections.

func NewStore

func NewStore(client *ent.Client, logger *zap.SugaredLogger) *Store

NewStore creates a new observational memory store.

func (*Store) DeleteObservations

func (s *Store) DeleteObservations(ctx context.Context, ids []uuid.UUID) error

DeleteObservations deletes observations by their IDs.

func (*Store) DeleteObservationsBySession

func (s *Store) DeleteObservationsBySession(ctx context.Context, sessionKey string) error

DeleteObservationsBySession deletes all observations for a session.

func (*Store) DeleteReflections

func (s *Store) DeleteReflections(ctx context.Context, ids []uuid.UUID) error

DeleteReflections deletes reflections by their IDs.

func (*Store) DeleteReflectionsBySession

func (s *Store) DeleteReflectionsBySession(ctx context.Context, sessionKey string) error

DeleteReflectionsBySession deletes all reflections for a session.

func (*Store) GetObservation

func (s *Store) GetObservation(ctx context.Context, id uuid.UUID) (*Observation, error)

GetObservation retrieves a single observation by its ID.

func (*Store) GetReflection

func (s *Store) GetReflection(ctx context.Context, id uuid.UUID) (*Reflection, error)

GetReflection retrieves a single reflection by its ID.

func (*Store) ListObservations

func (s *Store) ListObservations(ctx context.Context, sessionKey string) ([]Observation, error)

ListObservations returns observations for a session ordered by created_at ascending.

func (*Store) ListRecentObservations

func (s *Store) ListRecentObservations(ctx context.Context, sessionKey string, limit int) ([]Observation, error)

ListRecentObservations returns the N most recent observations for a session. Results are ordered by created_at ascending (oldest first) for chronological display.

func (*Store) ListRecentReflections

func (s *Store) ListRecentReflections(ctx context.Context, sessionKey string, limit int) ([]Reflection, error)

ListRecentReflections returns the N most recent reflections for a session. Results are ordered by created_at ascending (oldest first) for chronological display.

func (*Store) ListReflections

func (s *Store) ListReflections(ctx context.Context, sessionKey string) ([]Reflection, error)

ListReflections returns reflections for a session ordered by created_at ascending.

func (*Store) SaveObservation

func (s *Store) SaveObservation(ctx context.Context, obs Observation) error

SaveObservation persists an observation to the database.

func (*Store) SaveReflection

func (s *Store) SaveReflection(ctx context.Context, ref Reflection) error

SaveReflection persists a reflection to the database.

func (*Store) SetEmbedCallback

func (s *Store) SetEmbedCallback(cb types.EmbedCallback)

SetEmbedCallback sets the optional embedding hook.

func (*Store) SetGraphCallback

func (s *Store) SetGraphCallback(cb types.ContentCallback)

SetGraphCallback sets the optional graph relationship hook.

func (*Store) SetGraphHooks

func (s *Store) SetGraphHooks(hooks *GraphHooks)

SetGraphHooks sets the graph hooks for temporal/session triple generation.

type TextGenerator

type TextGenerator interface {
	GenerateText(ctx context.Context, systemPrompt, userPrompt string) (string, error)
}

TextGenerator generates text from a prompt. Used to abstract LLM calls for testability.

type TripleCallback

type TripleCallback func(triples []graph.Triple)

TripleCallback is a hook that receives graph triples for asynchronous storage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL