memory
Enable memory for a directory, then use remember to extract facts and recall to retrieve them. Memory configuration and scope changes stay local to the current execution branch. The files persist across runs.
import { enableMemory, setMemoryId, remember, recall } from "std::memory"
node main() {
enableMemory({ dir: "./mem" }) with approve
setMemoryId("alice")
remember("Alice's favorite color is blue") with approve
const facts = recall("Alice's favorite color") with approve
print(facts)
}remember and forget do nothing while memory is disabled. recall still raises its approval request and then returns an empty string.
Types
MemoryConfig
Configure the storage directory and optional memory model, extraction, compaction, and embedding settings. The same settings can be supplied in the memory block of agency.json. Relative dir paths use the working directory, and ~ expands to the home directory.
/** Configure the storage directory and optional memory model, extraction,
compaction, and embedding settings. The same settings can be supplied in
the `memory` block of `agency.json`. Relative `dir` paths use the working
directory, and `~` expands to the home directory. */
export type MemoryConfig = {
dir: string;
model?: string;
autoExtract?: { interval: number | null };
compaction?: { trigger: "token" | "messages" | null; threshold: number | null };
embeddings?: { model: string | null; provider: string | null }
}(source)
Effects
std::memory::enableMemory
effect std::memory::enableMemory {
dir: string
}(source)
std::memory::disableMemory
effect std::memory::disableMemory {}(source)
std::memory::remember
effect std::memory::remember {
contentLength: number
}(source)
std::memory::recall
effect std::memory::recall {
query: string
}(source)
std::memory::forget
effect std::memory::forget {
query: string
}(source)
Functions
isMemoryActive
isMemoryActive(): booleanReturn whether memory is enabled for the current execution branch.
Check this before offering memory tools when memory may be disabled.
Returns: boolean
(source)
setMemoryId
setMemoryId(id: string)Choose the user, conversation, or workspace scope for memory reads and writes. Enable memory before setting the scope.
A branch inherits its memory scope when it forks. Changing the scope inside a branch leaves the parent's scope unchanged. Switching storage directories keeps the scope, so set it again when you want a different one.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| id | string | Nonempty scope using letters, digits, dots, underscores, or hyphens, excluding . and ... |
(source)
getMemoryId
getMemoryId(): stringReturn the memory scope, or "default" when no scope is set or memory is disabled.
Returns: string
(source)
enableMemory
enableMemory(config: MemoryConfig) raises <std::memory::enableMemory>Enable persistent memory for the current execution branch. Enabling the active directory again leaves its configuration unchanged.
Runs on the same host that use the same absolute directory share a store. Enabling the active directory again leaves its configuration unchanged. A different directory becomes the active configuration until it is popped by disableMemory. Prefer the memory block form for temporary changes. The storage directory is created when the store first writes data.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| config | MemoryConfig | Supply a nonempty dir for the memory store. |
Throws: std::memory::enableMemory
Needs: fileRead, fileWrite
(source)
disableMemory
disableMemory() raises <std::memory::disableMemory>Remove the current branch's most recently enabled memory configuration. An earlier configuration becomes active again if one remains.
This also removes a configuration loaded from agency.json. Use the memory block form when the previous configuration must be restored.
Throws: std::memory::disableMemory
(source)
memory
memory(config: MemoryConfig, block: () -> any = null): ResultRun a block with a temporary memory configuration, then restore the previous one.
const result = memory({ dir: "./temporary-memory" }) as {
remember("Alice's favorite color is blue") with approve
}The block returns a Result and restores the prior configuration after normal completion or failure. This helper does not raise the enable-memory approval, so the caller decides when to expose it.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| config | MemoryConfig | Supply a nonempty dir for the memory store. | |
| block | () => any | null | Required block whose return value becomes the successful result. |
Returns: Result
Needs: fileRead, fileWrite
(source)
remember
remember(content: string) raises <std::memory::remember>Extract facts from text and persist them to memory. Does nothing while memory is disabled.
Parameters:
| Name | Type | Default |
|---|---|---|
| content | string |
Throws: std::memory::remember
Needs: fileRead, fileWrite, llm
(source)
recall
recall(query: string): string raises <std::memory::recall>Retrieve relevant stored facts as text for a question or topic. Return an empty string when memory is disabled or no facts match.
Parameters:
| Name | Type | Default |
|---|---|---|
| query | string |
Returns: string
Throws: std::memory::recall
Needs: fileRead, llm
(source)
forget
forget(query: string) raises <std::memory::forget>Stop recalling facts that match a natural-language description. This preserves stored history and does nothing while memory is disabled.
Forgetting expires matching observations and relations. The stored history remains available for auditing.
Parameters:
| Name | Type | Default |
|---|---|---|
| query | string |
Throws: std::memory::forget
Needs: fileRead, fileWrite, llm
(source)