memory

package
v0.7.0-alpha Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memory ingests records, builds a prompt from them, and runs that prompt on many models at once.

A record is text plus a pointer. Media bytes and sockets stay outside. Compose a Pipe to decide what is stored, a View to decide what each model reads, and a Plan to run those models together or take the first result.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lines

func Lines(records []Record) string

Lines renders records as one line each: "source: body ref".

Types

type Album

type Album interface {
	Open(ctx context.Context, ref string) (niro.Part, bool, error)
}

Album turns a record Ref into the image or video a vision model should see. The store keeps the pointer. The album keeps the bytes.

type Mem

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

Mem is an in-process store. cap is the most records kept per scope; zero keeps them all. The oldest record in that scope is dropped past the cap.

func New

func New(cap int) *Mem

New returns an in-process store.

func (*Mem) Forget

func (m *Mem) Forget(_ context.Context, q Query) (int, error)

Forget removes matching records and returns how many were removed.

func (*Mem) Put

func (m *Mem) Put(_ context.Context, rec Record) (Record, error)

Put stores a copy of rec. At is set to now when it is zero. An empty Body and Ref is rejected. A new record with an empty ID gets one.

func (*Mem) Recall

func (m *Mem) Recall(_ context.Context, q Query) ([]Record, error)

Recall returns copies of the matching records, oldest first.

type Pipe

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

Pipe is an ordered list of stages. Pipes are values: Then returns a new pipe.

func NewPipe

func NewPipe(stages ...Stage) *Pipe

NewPipe returns a pipe that runs stages in order.

func (*Pipe) Commit

func (p *Pipe) Commit(ctx context.Context, store Store, recs ...Record) ([]Record, error)

Commit runs the pipe, then stores whatever remains. A nil pipe stores the records as they are. A stage that returns nothing stops the pipe and stores nothing. A stage error stores nothing.

func (*Pipe) Then

func (p *Pipe) Then(stages ...Stage) *Pipe

Then returns a pipe with more stages appended.

type Plan

type Plan struct {
	Store    Store
	Scope    string
	View     View
	Messages []niro.Message
	Targets  []Target
	Album    Album
	Race     bool
}

Plan renders a view and runs every target. Race returns the first success and cancels the rest. Targets must stop when the context is canceled. When every target fails, Race returns each failure.

func (*Plan) Run

func (p *Plan) Run(ctx context.Context) ([]Result, error)

Run executes the plan.

func (*Plan) Save

func (p *Plan) Save(ctx context.Context, results []Result) ([]Record, error)

Save writes each successful result back into the plan store. The record source is the target name. Empty text is skipped.

type Query

type Query struct {
	ID       string
	Scope    string
	AnyScope bool
	Sources  []string
	Tags     []string
	Since    time.Time
	Until    time.Time
	Limit    int
	Match    func(Record) bool
}

Query selects records. Zero fields match nothing extra: an empty Query matches only records whose Scope is empty.

Sources match if the record source is one of them. Tags match only when the record carries every listed tag. Until is exclusive. Limit is applied by the store after matching, keeping the newest records. Limit is not part of Allows.

func (Query) Allows

func (q Query) Allows(r Record) bool

Allows reports whether r matches the query predicate.

type Record

type Record struct {
	ID     string
	Scope  string
	Source string
	Body   string
	Ref    string
	Tags   []string
	Meta   map[string]string
	At     time.Time
}

Record is one thing worth remembering. Ref points at something stored elsewhere (a URL, an id). A target with See resolves Ref through an Album into an image or video part. Tags and Meta are for the caller; this package does not interpret them.

type Reel

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

Reel is an in-process album. cap is the most frames kept; zero keeps them all. Putting the same ref again replaces that frame and makes it the newest.

func NewReel

func NewReel(cap int) *Reel

NewReel returns an in-process album.

func (*Reel) Open

func (r *Reel) Open(_ context.Context, ref string) (niro.Part, bool, error)

Open returns a copy of the frame stored at ref.

func (*Reel) Put

func (r *Reel) Put(ref string, part niro.Part) error

Put stores a copy of an image or video frame under ref.

type Result

type Result struct {
	Name   string
	Model  string
	Prompt string
	Text   string
	Usage  niro.Usage
	Err    error
}

Result is one target's output. Prompt is the system prompt that was sent. Err is set when that model failed. Other targets still finish unless Race is set.

type Section

type Section struct {
	Title string
	Query *Query
	Write func([]Record) string
}

Section is one block of a prompt. A nil Query is static: Write is called with no records. A nil Write renders records with Lines. An empty body is left out of the prompt.

type Stage

type Stage func(ctx context.Context, in []Record) ([]Record, error)

Stage rewrites a batch of records before they are stored. Return no records to drop the batch. Return more than one record from a single input to fan an event out into several memories.

func Filter

func Filter(fn func(Record) bool) Stage

Filter keeps records for which fn returns true.

func Map

func Map(fn func(context.Context, Record) ([]Record, error)) Stage

Map replaces each record with the records fn returns. An empty slice drops that record. An error stops the pipe before anything is stored.

type Store

type Store interface {
	Put(ctx context.Context, rec Record) (Record, error)
	Recall(ctx context.Context, q Query) ([]Record, error)
	Forget(ctx context.Context, q Query) (int, error)
}

Store is the memory backend. Put with an existing ID in the same scope replaces that record. Recall returns matches oldest first. Forget removes matches.

type Target

type Target struct {
	Name       string
	Provider   niro.Provider
	Model      string
	Scope      string
	View       *View
	Messages   []niro.Message
	Tools      []niro.Tool
	ToolChoice niro.ToolChoice
	Options    niro.Options
	Parts      []niro.Part
	See        *Query
	Album      Album
	Request    func(prompt string, parts []niro.Part) *niro.Request
}

Target is one model in a plan. Scope and View, when set, replace the plan's scope and view for this model only. Messages, when non-nil, replace the plan messages. Parts are images or video attached to the user turn. See recalls records and resolves each Ref through the target Album, or the plan Album. Request, when set, builds the provider request from the rendered prompt and those parts. The other request fields are not used.

type View

type View struct {
	Base     string
	Sections []Section
}

View is the system prompt: base instructions plus the sections, in order. Each section recalls its own query, so two models can read the same store through different views.

func (View) Build

func (v View) Build(ctx context.Context, store Store, scope string) (string, error)

Build recalls each section and joins the non-empty blocks. scope is written onto a query that does not set Scope or AnyScope.

Jump to

Keyboard shortcuts

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