contextengine

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package contextengine implements the Proactive Context Engine. It automatically injects relevant memory facts into every MCP tool response via ToolHandlerMiddleware, so the LLM always has context without asking.

Package contextengine — processor.go Processes unprocessed interaction log entries into session summary facts. This closes the memory loop: tool calls → interaction log → summary facts → boot instructions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLastSessionSummary

func GetLastSessionSummary(ctx context.Context, store memory.FactStore) string

GetLastSessionSummary searches the fact store for the most recent session summary.

func LoadConfig

func LoadConfig(path string) (ctxdomain.EngineConfig, error)

LoadConfig loads engine configuration from a JSON file. If the file does not exist, returns DefaultEngineConfig. If the file exists but is invalid, returns an error.

func SaveDefaultConfig

func SaveDefaultConfig(path string) error

SaveDefaultConfig writes the default configuration to a JSON file. Useful for bootstrapping .rlm/context.json.

Types

type Engine

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

Engine is the Proactive Context Engine. It scores facts by relevance, selects the top ones within a token budget, and injects them into every tool response as a [MEMORY CONTEXT] block.

func New

New creates a new Proactive Context Engine.

func (*Engine) BuildContext

func (e *Engine) BuildContext(toolName string, args map[string]interface{}) *ctxdomain.ContextFrame

BuildContext scores and selects relevant facts for the given tool call, returning a ContextFrame ready for formatting and injection.

func (*Engine) GetAccessCount

func (e *Engine) GetAccessCount(factID string) int

GetAccessCount returns the internal access count for a fact.

func (*Engine) IsEnabled

func (e *Engine) IsEnabled() bool

IsEnabled returns whether the engine is active.

func (*Engine) Middleware

func (e *Engine) Middleware() server.ToolHandlerMiddleware

Middleware returns a ToolHandlerMiddleware that wraps every tool handler to inject relevant memory context into the response and optionally record tool calls to the interaction log for crash-safe memory.

func (*Engine) SetInteractionLogger

func (e *Engine) SetInteractionLogger(l InteractionLogger)

SetInteractionLogger attaches an optional interaction logger for crash-safe tool call recording. If set, every tool call passing through the middleware will be recorded fire-and-forget (errors logged, never propagated).

type InteractionLogger

type InteractionLogger interface {
	Record(ctx context.Context, toolName string, args map[string]interface{}) error
}

InteractionLogger records tool calls for crash-safe memory. Implementations must be safe for concurrent use.

type InteractionProcessor

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

InteractionProcessor processes unprocessed interaction log entries and creates session summary facts from them.

func NewInteractionProcessor

func NewInteractionProcessor(repo *sqlite.InteractionLogRepo, store memory.FactStore) *InteractionProcessor

NewInteractionProcessor creates a new processor.

func (*InteractionProcessor) ProcessShutdown

func (p *InteractionProcessor) ProcessShutdown(ctx context.Context) (string, error)

ProcessShutdown processes entries from the current session at graceful shutdown. Similar to ProcessStartup but labels differently.

func (*InteractionProcessor) ProcessStartup

func (p *InteractionProcessor) ProcessStartup(ctx context.Context) (string, error)

ProcessStartup processes unprocessed entries from a previous (possibly crashed) session. It creates an L1 "session summary" fact and marks all entries as processed. Returns the summary text (empty if nothing to process).

type StoreFactProvider

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

StoreFactProvider adapts FactStore + HotCache to the FactProvider interface, bridging infrastructure storage with the context engine domain.

func NewStoreFactProvider

func NewStoreFactProvider(store memory.FactStore, cache memory.HotCache) *StoreFactProvider

NewStoreFactProvider creates a FactProvider backed by FactStore and optional HotCache.

func (*StoreFactProvider) GetL0Facts

func (p *StoreFactProvider) GetL0Facts() ([]*memory.Fact, error)

GetL0Facts returns all L0 (project-level) facts. Uses HotCache if available, falls back to store.

func (*StoreFactProvider) GetRelevantFacts

func (p *StoreFactProvider) GetRelevantFacts(args map[string]interface{}) ([]*memory.Fact, error)

GetRelevantFacts returns candidate facts for context injection. Uses keyword search from tool arguments + L0 facts as candidates.

func (*StoreFactProvider) RecordAccess

func (p *StoreFactProvider) RecordAccess(factID string)

RecordAccess increments the access counter for a fact.

Jump to

Keyboard shortcuts

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