context

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSessionNotFound = errors.New("session not found")
	ErrTaskNotFound    = errors.New("task not found")
)

Memory errors.

View Source
var (
	ErrUserNotFound = errors.New("user not found")
)

Memory errors.

Functions

This section is empty.

Types

type Cache

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

Cache provides in-memory caching capabilities.

func NewCache

func NewCache(maxSize int, ttl time.Duration) *Cache

NewCache creates a new Cache. The cleanup goroutine is started automatically to prevent memory leaks.

func (*Cache) Clear

func (c *Cache) Clear(ctx context.Context) error

Clear removes all entries.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, key string) error

Delete removes a cache entry.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string) (interface{}, bool)

Get retrieves a value from cache.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, value interface{}) error

Set stores a value in cache.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of items.

func (*Cache) Start

func (c *Cache) Start()

Start starts the cleanup goroutine. This method is idempotent - calling it multiple times has no additional effect.

func (*Cache) Stop

func (c *Cache) Stop()

Stop stops the cleanup goroutine. This method is idempotent and safe to call multiple times. Subsequent calls after the first will be no-ops.

type CacheItem

type CacheItem struct {
	Key        string
	Value      interface{}
	Expiration time.Time
}

CacheItem represents a cache entry.

type CacheWithTTL

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

CacheWithTTL creates a cache with custom TTL per item.

func NewCacheWithTTL

func NewCacheWithTTL(maxSize int) *CacheWithTTL

NewCacheWithTTL creates a new CacheWithTTL. The cleanup goroutine is started automatically to prevent memory leaks.

func (*CacheWithTTL) Clear

func (c *CacheWithTTL) Clear(ctx context.Context) error

Clear removes all entries.

func (*CacheWithTTL) Delete

func (c *CacheWithTTL) Delete(ctx context.Context, key string) error

Delete removes a cache entry.

func (*CacheWithTTL) Get

func (c *CacheWithTTL) Get(ctx context.Context, key string) (interface{}, bool)

Get retrieves a value from cache.

func (*CacheWithTTL) SetWithTTL

func (c *CacheWithTTL) SetWithTTL(ctx context.Context, key string, value interface{}, ttl time.Duration) error

SetWithTTL stores a value with custom TTL.

func (*CacheWithTTL) Size

func (c *CacheWithTTL) Size() int

Size returns the number of items.

func (*CacheWithTTL) Start

func (c *CacheWithTTL) Start()

Start starts the cleanup goroutine. This method is idempotent - calling it multiple times has no additional effect.

func (*CacheWithTTL) Stop

func (c *CacheWithTTL) Stop()

Stop stops the cleanup goroutine. This method is idempotent and safe to call multiple times.

type Interaction

type Interaction struct {
	Type      string                 `json:"type"`
	SessionID string                 `json:"session_id"`
	Items     []string               `json:"items"`
	Feedback  string                 `json:"feedback"`
	Time      time.Time              `json:"time"`
	Metadata  map[string]interface{} `json:"metadata"`
}

Interaction represents a user interaction.

type KnowledgeEntry

type KnowledgeEntry struct {
	ID        string
	Content   string
	Embedding []float64
	Metadata  map[string]interface{}
}

KnowledgeEntry represents a knowledge base entry.

type LRUCache

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

LRU Cache implementation.

func NewLRUCache

func NewLRUCache(maxSize int) *LRUCache

NewLRUCache creates a new LRU Cache.

func (*LRUCache) Delete

func (c *LRUCache) Delete(ctx context.Context, key string) error

Delete removes a cache entry.

func (*LRUCache) Get

func (c *LRUCache) Get(ctx context.Context, key string) (interface{}, bool)

Get retrieves a value and moves it to front.

func (*LRUCache) Set

func (c *LRUCache) Set(ctx context.Context, key string, value interface{}) error

Set stores a value.

func (*LRUCache) Size

func (c *LRUCache) Size() int

Size returns the number of items.

type LRUItem

type LRUItem struct {
	Key   string
	Value interface{}
	// contains filtered or unexported fields
}

LRUItem represents a cache item with doubly-linked list pointers.

type Message

type Message struct {
	Role    string    `json:"role"`
	Content string    `json:"content"`
	Time    time.Time `json:"time"`
}

Message represents a chat message.

type Option

type Option func(*RAG)

Option is a function that configures RAG.

func WithPersistentStorage

func WithPersistentStorage(searcher VectorSearcher, tableName string) Option

WithPersistentStorage enables pgvector storage.

type Preference

type Preference struct {
	Category  string    `json:"category"`
	Value     string    `json:"value"`
	Score     float64   `json:"score"`
	UpdatedAt time.Time `json:"updated_at"`
}

Preference represents a user preference.

type RAG

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

RAG provides retrieval-augmented generation capabilities.

func NewRAG

func NewRAG(maxSize int) *RAG

NewRAG creates a new RAG instance with in-memory storage.

func NewRAGWithOptions

func NewRAGWithOptions(maxSize int, opts ...Option) *RAG

NewRAGWithOptions creates a new RAG instance with options.

func (*RAG) Add

func (r *RAG) Add(ctx context.Context, entry *KnowledgeEntry) error

Add adds a knowledge entry.

func (*RAG) Delete

func (r *RAG) Delete(ctx context.Context, id string) error

Delete removes a knowledge entry.

func (*RAG) Get

func (r *RAG) Get(ctx context.Context, id string) (*KnowledgeEntry, bool)

Get retrieves a knowledge entry by ID.

func (*RAG) InitStorage

func (r *RAG) InitStorage(ctx context.Context) error

InitStorage initializes the persistent storage table.

func (*RAG) IsPersistent

func (r *RAG) IsPersistent() bool

IsPersistent returns true if using persistent storage.

func (*RAG) Search

func (r *RAG) Search(ctx context.Context, query []float64, topK int) ([]*KnowledgeEntry, error)

Search searches for similar entries using simple cosine similarity.

func (*RAG) SearchByText

func (r *RAG) SearchByText(ctx context.Context, query string, topK int) ([]*KnowledgeEntry, error)

SearchByText searches for entries matching text content.

func (*RAG) Size

func (r *RAG) Size() int

Size returns the number of entries.

type ResultRecord

type ResultRecord struct {
	Type      string                 `json:"type"`
	Content   string                 `json:"content"`
	Score     float64                `json:"score"`
	Metadata  map[string]interface{} `json:"metadata"`
	Timestamp time.Time              `json:"timestamp"`
}

ResultRecord represents a task result.

type SessionData

type SessionData struct {
	SessionID  string
	UserID     string
	Messages   []Message
	Context    map[string]interface{}
	AccessedAt time.Time
	CreatedAt  time.Time
}

SessionData holds session information.

type SessionMemory

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

SessionMemory stores conversation context for a session.

func NewSessionMemory

func NewSessionMemory(maxSize int, ttl time.Duration) *SessionMemory

NewSessionMemory creates a new SessionMemory.

func (*SessionMemory) AddMessage

func (m *SessionMemory) AddMessage(ctx context.Context, sessionID string, msg Message) error

AddMessage adds a message to the session.

func (*SessionMemory) Cleanup

func (m *SessionMemory) Cleanup(ctx context.Context) int

Cleanup removes all expired sessions and returns the count of removed sessions. Limits cleanup to avoid long lock holding that blocks other operations.

func (*SessionMemory) Clear

func (m *SessionMemory) Clear(ctx context.Context) error

Clear removes all sessions.

func (*SessionMemory) Close

func (m *SessionMemory) Close(ctx context.Context) error

Close stops the background cleanup task and clears all sessions.

func (*SessionMemory) Delete

func (m *SessionMemory) Delete(ctx context.Context, sessionID string) error

Delete removes a session.

func (*SessionMemory) Get

func (m *SessionMemory) Get(ctx context.Context, sessionID string) (*SessionData, bool)

Get retrieves session data and updates access time.

func (*SessionMemory) GetMessages

func (m *SessionMemory) GetMessages(ctx context.Context, sessionID string) ([]Message, error)

GetMessages returns session messages.

func (*SessionMemory) Set

func (m *SessionMemory) Set(ctx context.Context, sessionID, userID string, messages []Message) error

Set stores session data.

func (*SessionMemory) Size

func (m *SessionMemory) Size() int

Size returns the number of sessions.

func (*SessionMemory) StartCleanup

func (m *SessionMemory) StartCleanup()

StartCleanup starts the background cleanup task.

func (*SessionMemory) StopCleanup

func (m *SessionMemory) StopCleanup()

StopCleanup stops the background cleanup task.

type StepRecord

type StepRecord struct {
	Name      string                 `json:"name"`
	Input     string                 `json:"input"`
	Output    string                 `json:"output"`
	Duration  time.Duration          `json:"duration"`
	Metadata  map[string]interface{} `json:"metadata"`
	Timestamp time.Time              `json:"timestamp"`
}

StepRecord represents a task execution step.

type StyleEntry

type StyleEntry struct {
	Style      []string  `json:"style"`
	Occasion   string    `json:"occasion"`
	Confidence float64   `json:"confidence"`
	Time       time.Time `json:"time"`
}

StyleEntry represents a style preference change over time.

type TaskData

type TaskData struct {
	TaskID     string
	SessionID  string
	UserID     string
	Input      string
	Output     string
	Context    map[string]interface{}
	Steps      []StepRecord
	Results    []ResultRecord
	CreatedAt  time.Time
	AccessedAt time.Time
}

TaskData holds task information.

type TaskMemory

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

TaskMemory stores task-specific context and distillation.

func NewTaskMemory

func NewTaskMemory(maxSize int, ttl time.Duration) *TaskMemory

NewTaskMemory creates a new TaskMemory.

func (*TaskMemory) AddResult

func (m *TaskMemory) AddResult(ctx context.Context, taskID string, result ResultRecord) error

AddResult adds a result record.

func (*TaskMemory) AddStep

func (m *TaskMemory) AddStep(ctx context.Context, taskID string, step StepRecord) error

AddStep adds a step record.

func (*TaskMemory) Delete

func (m *TaskMemory) Delete(ctx context.Context, taskID string) error

Delete removes a task.

func (*TaskMemory) Distill

func (m *TaskMemory) Distill(ctx context.Context, taskID string) (*models.Task, error)

Distill extracts key information from task for future reference.

func (*TaskMemory) Get

func (m *TaskMemory) Get(ctx context.Context, taskID string) (*TaskData, bool)

Get retrieves task data.

func (*TaskMemory) GetContext

func (m *TaskMemory) GetContext(ctx context.Context, taskID string, key string) (interface{}, bool)

GetContext returns a context value.

func (*TaskMemory) GetResults

func (m *TaskMemory) GetResults(ctx context.Context, taskID string) ([]ResultRecord, error)

GetResults returns task results.

func (*TaskMemory) GetSteps

func (m *TaskMemory) GetSteps(ctx context.Context, taskID string) ([]StepRecord, error)

GetSteps returns task steps.

func (*TaskMemory) Set

func (m *TaskMemory) Set(ctx context.Context, taskID, sessionID, userID, input string) error

Set stores task data.

func (*TaskMemory) SetContext

func (m *TaskMemory) SetContext(ctx context.Context, taskID string, key string, value interface{}) error

SetContext sets a context value.

func (*TaskMemory) Size

func (m *TaskMemory) Size() int

Size returns the number of tasks.

func (*TaskMemory) Start

func (m *TaskMemory) Start(ctx context.Context)

Start starts the background cleanup goroutine. This should be called after creating TaskMemory to enable automatic TTL cleanup.

func (*TaskMemory) Stop

func (m *TaskMemory) Stop()

Stop stops the background cleanup goroutine. This should be called during application shutdown.

func (*TaskMemory) UpdateOutput

func (m *TaskMemory) UpdateOutput(ctx context.Context, taskID, output string) error

UpdateOutput updates task output.

type UserData

type UserData struct {
	UserID         string
	Profile        *models.UserProfile
	Preferences    []Preference
	History        []Interaction
	StyleEvolution []StyleEntry
	LastUpdated    time.Time
	CreatedAt      time.Time
}

UserData holds user information.

type UserMemory

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

UserMemory stores long-term user preferences and history.

func NewUserMemory

func NewUserMemory(maxSize int) *UserMemory

NewUserMemory creates a new UserMemory.

func (*UserMemory) AddInteraction

func (m *UserMemory) AddInteraction(ctx context.Context, userID string, interaction Interaction) error

AddInteraction adds a user interaction.

func (*UserMemory) AddPreference

func (m *UserMemory) AddPreference(ctx context.Context, userID string, pref Preference) error

AddPreference adds a user preference.

func (*UserMemory) Delete

func (m *UserMemory) Delete(ctx context.Context, userID string) error

Delete removes user data.

func (*UserMemory) Get

func (m *UserMemory) Get(ctx context.Context, userID string) (*UserData, bool)

Get retrieves user data.

func (*UserMemory) GetHistory

func (m *UserMemory) GetHistory(ctx context.Context, userID string) ([]Interaction, error)

GetHistory returns user interaction history.

func (*UserMemory) GetPreferences

func (m *UserMemory) GetPreferences(ctx context.Context, userID string) ([]Preference, error)

GetPreferences returns user preferences.

func (*UserMemory) GetStyleEvolution

func (m *UserMemory) GetStyleEvolution(ctx context.Context, userID string) ([]StyleEntry, error)

GetStyleEvolution returns style preference evolution.

func (*UserMemory) Set

func (m *UserMemory) Set(ctx context.Context, userID string, profile *models.UserProfile) error

Set stores user data.

func (*UserMemory) Size

func (m *UserMemory) Size() int

Size returns the number of users.

func (*UserMemory) UpdateProfile

func (m *UserMemory) UpdateProfile(ctx context.Context, userID string, profile *models.UserProfile) error

UpdateProfile updates user profile.

func (*UserMemory) UpdateStyleEvolution

func (m *UserMemory) UpdateStyleEvolution(ctx context.Context, userID string, entry StyleEntry) error

UpdateStyleEvolution updates style preference evolution.

type VectorIndex

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

VectorIndex is a simple in-memory vector index.

func NewVectorIndex

func NewVectorIndex() *VectorIndex

NewVectorIndex creates a new VectorIndex.

type VectorSearcher

type VectorSearcher interface {
	Search(ctx context.Context, table string, query []float64, limit int) ([]*postgres.SearchResult, error)
	AddEmbedding(ctx context.Context, table, id string, embedding []float64, metadata map[string]any) error
	DeleteEmbedding(ctx context.Context, table, id string) error
	CreateVectorTable(ctx context.Context, table string, metadataSchema string) error
}

VectorSearcher defines the interface for vector storage operations.

Jump to

Keyboard shortcuts

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