Help us shape the future of Go! Take the 2026 Go Developer Survey

filememory

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package filememory provides a context provider with tools for persistent file-based agent memory.

Memory files have flat names within a working folder supplied by session state. The default folder is the root directory, so sessions share memories unless a StateInitializer selects separate folders. Descriptions are stored in hidden companion files named by appending "_description.md" to the full memory filename. A hidden memories.md index summarizes up to 50 files and is supplied to the agent before each invocation when present.

Searches are non-recursive and use Go regular expressions and case-insensitive filename patterns. Filename patterns support literal text and * wildcards; ? and brackets are literal characters. Backslashes are path separators, not escape characters. Leading separators, ./ and **/ select the working folder; *.* matches all filenames, including those without extensions. Search matches and line edits share 1-based line numbering. Search results retain each line's LF, CRLF or CR terminator. Edits insert replacement text verbatim, so callers include any terminator they want to keep. An empty replacement deletes the whole line, including its terminator.

Index

Constants

View Source
const (
	WriteToolName        = "file_memory_write"
	ReadFileToolName     = "file_memory_read"
	DeleteFileToolName   = "file_memory_delete"
	LsToolName           = "file_memory_ls"
	GrepToolName         = "file_memory_grep"
	ReplaceToolName      = "file_memory_replace"
	ReplaceLinesToolName = "file_memory_replace_lines"
)

Names of the tools supplied by the provider.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Instructions replaces the built-in instructions. Nil uses the defaults;
	// an empty or whitespace-only string omits the provider's instructions.
	Instructions *string

	// StateInitializer supplies state when the session has none. Nil uses an
	// empty working folder. The callback may receive a nil session when the
	// provider is invoked without one; in that case state is not persisted.
	StateInitializer func(*agent.Session) State
}

Config configures a file memory provider.

type ListEntry

type ListEntry struct {
	Name        string `json:"name"`
	Type        string `json:"type"`
	Description string `json:"description,omitempty"`
}

ListEntry describes a memory file returned by file_memory_ls.

type Provider

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

Provider supplies memory tools, instructions, and the current memory index. Use New to create a provider. Its filesystem handle is cleaned up when the provider becomes unreachable; Provider.Close releases it early. Provider is safe for concurrent use, including concurrent tool calls.

func New

func New(rootDirectory string, cfg Config) (*Provider, error)

New creates a file memory provider rooted at rootDirectory, creating the directory if needed. New directories use permission 0700 and new files use 0600; existing permissions are unchanged. The provider confines file access to this root. Runtime cleanup releases the handle when the provider becomes unreachable; Provider.Close can release it earlier. Cleanup is asynchronous and is not guaranteed to run before program exit.

A zero Config uses built-in instructions and the root directory as the working folder.

Register the provider in agent.Config.ContextProviders. Custom backends using automatic tool-invocation middleware must apply that middleware through agent.ProviderConfig.Middlewares, after context providers have supplied tools.

func (*Provider) Close

func (p *Provider) Close() error

Close is an optimization that releases the filesystem handle before runtime cleanup. It is safe to call more than once, but must not overlap active tool calls. Subsequent storage operations return an error wrapping fs.ErrClosed.

func (*Provider) Invoked

func (p *Provider) Invoked(ctx context.Context, invoked agent.InvokedContext) error

Invoked completes the context-provider lifecycle. Memory mutations are persisted by the tools, so no additional storage occurs after a run.

func (*Provider) Invoking

func (p *Provider) Invoking(ctx context.Context, invoking agent.InvokingContext) ([]*message.Message, []agent.Option, error)

Invoking adds memory instructions, tools and any existing memory index to the invocation without modifying the caller's messages or options.

func (*Provider) StateKeys

func (p *Provider) StateKeys() []string

StateKeys returns the session-state keys owned by the provider.

type State

type State struct {
	WorkingFolder string `json:"workingFolder"`
}

State holds the working folder for a session, relative to the store root. Its zero value selects the store root. Tools use the working folder selected at the start of each invocation.

Jump to

Keyboard shortcuts

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