Functions
AgentMemory
Alias of ::ai::memory/AgentMemory
ChatOptions
Alias of ::ai::chat/ChatOptions
MemoryCapsule
Alias of ::ai::memory/MemoryCapsule
Role
Alias of ::ai::message/Role
Thread
Alias of ::ai::memory/Thread
ask
fn (mem: ::ai::memory/AgentMemory, question: Str, chat-opts: ::ai::chat/ChatOptions, opts: Map?): Map
Search memory for relevant context, then generate an AI answer. Returns {answer, sources}.
ask-with-thread
fn (mem: ::ai::memory/AgentMemory, t: ::ai::memory/Thread, question: Str, chat-opts: ::ai::chat/ChatOptions, opts: Map?): Map
Like ask, but includes thread history for multi-turn context. Appends question and answer to the thread automatically.
build-context
fn (mem: ::ai::memory/AgentMemory, query: Str, opts: Map?): Str
Search session memory and KB, format as a context string for AI prompts. Options: limit (default 10), min-score (default 0.5).
build-context-safe
fn (mem: ::ai::memory/AgentMemory, query: Str, opts: Map?): Str
Best-effort build-context. Any failure (no embedding provider
configured, transient store error, etc.) degrades to "" so callers
can still answer ungrounded rather than aborting the whole reply.
Use this in interactive request paths and demos / CI / offline
tutorials where an embedding backend may not be wired. Prefer
build-context directly in code paths that should propagate errors.
build-hybrid-context
fn (mem: ::ai::memory/AgentMemory, query: Str, opts: Map?): Str
Build a citation-preserving context string from hybrid vector + graph retrieval.
build-rag-prompt
fn (history-text: Str, context: Str, question: Str): Str
Compose a RAG prompt from optional history, optional context, and a question.
compact-window
fn (mem: ::ai::memory/AgentMemory, chat-opts: ::ai::chat/ChatOptions, opts: Map?): ::ai::memory/MemoryCapsule
Summarize a recent memory window into a durable MemoryCapsule and store it in the KB. Options: hours, limit, id, instruction, metadata, emit-stream.
compact-window-safe
fn (mem: ::ai::memory/AgentMemory, chat-opts: ::ai::chat/ChatOptions, opts: Map?): Map
Like compact-window, returning {ok, capsule, error} and emitting compaction errors when requested.
format-kb-results
fn (results: Vec): Vec
Format a Vec of KB search results as [KB] content lines.
format-session-results
fn (results: Vec): Vec
Format a Vec of store search results (with v.sender-name, v.content) as [name]: content lines.
format-thread-lines
fn (history: Vec): Str
Format a Vec of ChatMessages as role: content lines.
ns
Alias of ::ai::rag/
Retrieve-Augment-Generate: compose memory search with AI generation. Accepts chat-fn parameter for provider-free AI calls.
passes-role-filter
fn (entry: Map, include-roles: Vec?, exclude-roles: Vec?): Bool
Apply include-roles / exclude-roles semantics:
- When
include-rolesis non-empty, the entry must have a role in that list. Untagged entries are dropped. exclude-rolesis a blacklist applied next. Tagged matches are dropped; untagged entries pass.- With neither set, every entry passes.
recent-text
fn (mem: ::ai::memory/AgentMemory, opts: Map?): Str
Format recent session memory as bulleted - <content> lines, oldest
first.
Convenience over ::ai::memory/recent for synthesis prompts that want
the raw window as text rather than a finished summary. Errors degrade
to "" so missing stores / providers don't crash callers.
Options:
limit— number of records to include (default 100, matches::ai::memory/recent).hours— recency window in hours (default 24).include-roles—Vec<Str>?. When set, only records whosemetadata.roleis in this list are kept. Untagged records are dropped.exclude-roles—Vec<Str>?. When set, records whosemetadata.roleis in this list are dropped. Untagged records pass.
The role filters compose with limit: filtering happens after
recent, so limit bounds the window, not the post-filter count.
For synthesis prompts that want only user turns + task records,
pass exclude-roles: ["assistant"].
role-in
fn (roles: Vec, role: Str): Bool
True when role equals any element of roles.
role-of
fn (entry: Map): Str
Read the role tag from a ::ai::memory/recent entry's metadata.
Returns "" when unset so role filters never accidentally drop
untagged records (callers asking to exclude assistant shouldn't
also exclude legacy un-roled writes).
summarize
fn (mem: ::ai::memory/AgentMemory, chat-opts: ::ai::chat/ChatOptions, opts: Map?): Str
Summarize recent messages from session memory using AI. Options: hours, limit, instruction.
with-memory-block
fn (base: Str, context: Str, heading: Str?): Str
Compose a system prompt by appending a memory-context section to base.
Returns base unchanged when context is empty (or whitespace only), so
callers can pass the result of build-context / build-context-safe
directly without branching. When context is non-empty, the result is
${base}\n\n${heading}\n${context}. heading defaults to ## Memory.