Documentation
¶
Overview ¶
Package memory is X-Ray's memory store, run inside your own process.
It is the same store the X-Ray server uses: typed items with a scope and a subject, a keyword index, an HNSW index over embeddings when you give it an embedding endpoint, near-duplicate merging on write, recall shaping, and provenance, where an item a person wrote is never replaced by one a model wrote. There is no server, no Kubernetes and no network except the embedding endpoint you choose.
store, err := memory.New(ctx,
memory.WithEmbeddings("http://localhost:8090", "nomic", 256),
memory.WithFile("agent-memory.json"),
)
if err != nil { ... }
defer store.Close()
store.Upsert(ctx, xray.Write{
Kind: xray.KindStableFact, Scope: xray.ScopePublic, Confidence: "high",
Title: "Checkout retries on 503", Content: "Three times, with jitter.",
})
items, _ := store.Search(ctx, "payment retries", xray.WithLimit(5))
Store implements xray.Memory, the interface the remote xray.Client also implements. An agent written against it runs in process during development and against a shared X-Ray in production without a change.
With no embedding endpoint search is by keyword and trigram only. With WithFile the store loads the file on start and writes it after every change, so memory survives a restart; without it memory lives until the process ends.
Index ¶
- type Option
- type Store
- func (s *Store) Close() error
- func (s *Store) Consolidate(_ context.Context, apply bool, opts ...xray.QueryOption) (*xray.ConsolidationReport, error)
- func (s *Store) Context(_ context.Context, anchor string, maxDepth int) ([]xray.ContextItem, error)
- func (s *Store) Delete(_ context.Context, id string, opts ...xray.QueryOption) error
- func (s *Store) Expire(_ context.Context, id string, opts ...xray.QueryOption) error
- func (s *Store) Fetch(_ context.Context, ids []string, opts ...xray.QueryOption) ([]xray.Item, error)
- func (s *Store) Get(ctx context.Context, id string, opts ...xray.QueryOption) (*xray.Item, error)
- func (s *Store) List(_ context.Context, opts ...xray.QueryOption) ([]xray.Item, error)
- func (s *Store) Recent(_ context.Context, opts ...xray.QueryOption) ([]xray.Episode, error)
- func (s *Store) Refute(ctx context.Context, id, reason string, evidence []string, ...) (*xray.RefuteResult, error)
- func (s *Store) Search(ctx context.Context, query string, opts ...xray.QueryOption) ([]xray.Item, error)
- func (s *Store) Summary(context.Context) (*xray.Summary, error)
- func (s *Store) Supersede(ctx context.Context, oldID string, w xray.Write) (*xray.SupersedeResult, error)
- func (s *Store) Upsert(ctx context.Context, w xray.Write) (*xray.WriteResult, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*config)
Option configures New.
func WithEmbeddings ¶
WithEmbeddings turns on semantic search. endpoint is an OpenAI-compatible /v1/embeddings server (llama.cpp, Ollama, vLLM, LM Studio or a hosted one), model its model name and dims the vector size to truncate to (256 suits nomic). Items are embedded when written, so a write waits for the endpoint.
func WithFile ¶
WithFile keeps the store in a JSON file: loaded on New, rewritten after every change. The file holds every item, private ones included, so give it the permissions you would give the data (it is written 0600).
func WithMaxInputChars ¶
WithMaxInputChars caps the text sent to the embedder per item. The default is 8192, which fits a 2048-token model.
func WithOrigin ¶
WithOrigin sets who the program's writes are attributed to. The default is xray.OriginDerived: written by a model. Use xray.OriginHuman for a store that holds what a person told the agent, or xray.OriginObserved for facts the code measured. A derived write never replaces a human item.
func WithRecallShaping ¶
WithRecallShaping turns collapsing of near-duplicates in search results on or off (default on).
func WithWriteDedup ¶
WithWriteDedup turns merging of restated facts on write on or off (default on).
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is an in-process X-Ray memory store. It is safe for concurrent use.
func (*Store) Consolidate ¶
func (s *Store) Consolidate(_ context.Context, apply bool, opts ...xray.QueryOption) (*xray.ConsolidationReport, error)
Consolidate reports near-duplicate facts. With apply it retires the restatements. WithSubject restricts the sweep to one subject.
func (*Store) Context ¶
Context follows links from an anchor (an id, a subject or part of a title) with decaying activation, and returns the anchor items and the neighbourhood they reach. maxDepth 0 uses 3, and at most 8 is followed.
func (*Store) Fetch ¶
func (s *Store) Fetch(_ context.Context, ids []string, opts ...xray.QueryOption) ([]xray.Item, error)
Fetch returns the items with the given ids, in that order. A missing id, or a private item the subject does not own, is left out.
func (*Store) Refute ¶
func (s *Store) Refute(ctx context.Context, id, reason string, evidence []string, opts ...xray.QueryOption) (*xray.RefuteResult, error)
Refute retires an item that is wrong, records the reason and evidence on it, and retires every active derived item that cites it.
func (*Store) Search ¶
func (s *Store) Search(ctx context.Context, query string, opts ...xray.QueryOption) ([]xray.Item, error)
Search ranks items by relevance to query. Without a subject only shared items are searched; a subject also reaches that subject's private items.