teff.memory.context¶
teff.memory.context
¶
Context injection helpers for long-term memory.
These turn recalled memories into a block of text that can be inserted
into an agent's system prompt (or a LLM call's messages) so a model sees
relevant cross-session facts without needing the memory tool.
Classes:
| Name | Description |
|---|---|
MemoryConfig |
Declarative memory injection for |
Functions:
| Name | Description |
|---|---|
last_user_text |
Return the most recent non-empty user message text. |
memory_context |
Return a formatted block of recalled memories, or |
memory_context_from_config |
Recall block for a node's |
MemoryConfig
dataclass
¶
Declarative memory injection for agent / llm nodes.
Passed to :class:~teff.node.agent.ReActAgent,
:class:~teff.node.llm.LLM (and the flow.react() /
flow.harness() / flow.llm() helpers) via the memory
parameter. A plain config dict is accepted too — that is what YAML
workflows deserialize to.
Attributes:
| Name | Type | Description |
|---|---|---|
store |
MemoryStore | dict | None
|
A ready :class: |
namespace |
tuple[str, ...] | list[str] | str | None
|
Namespace subtree to recall from (a string becomes a
single-segment namespace). Each segment may reference
|
k |
int
|
Maximum number of memories recalled per turn. |
header |
str
|
First line of the injected block. |
Methods:
| Name | Description |
|---|---|
to_dict |
Config-dict form (used internally; YAML round-trips as this). |
Source code in teff/memory/context.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
to_dict
¶
to_dict()
Config-dict form (used internally; YAML round-trips as this).
Source code in teff/memory/context.py
54 55 56 57 58 59 60 61 | |
last_user_text
¶
last_user_text(messages, fallback='')
Return the most recent non-empty user message text.
Source code in teff/memory/context.py
145 146 147 148 149 150 151 152 153 | |
memory_context
async
¶
memory_context(store, query, *, namespace=(), k=5, header=DEFAULT_HEADER, bullet='-')
Return a formatted block of recalled memories, or "" if none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
Any
|
A :class: |
required |
query
|
str
|
Natural-language query used for the semantic recall. |
required |
namespace
|
tuple[str, ...]
|
Namespace subtree to recall from. |
()
|
k
|
int
|
Maximum number of memories to include. |
5
|
header
|
str
|
First line of the block. |
DEFAULT_HEADER
|
bullet
|
str
|
Per-item bullet prefix. |
'-'
|
The returned string is meant to be appended to a system prompt; it is empty when nothing matched, so callers can skip it entirely.
Source code in teff/memory/context.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
memory_context_from_config
async
¶
memory_context_from_config(cfg, *, state, ctx)
Recall block for a node's memory config, or "" when off.
Shared by :class:~teff.node.agent.ReActAgent and
:class:~teff.node.llm.LLM. Reads the node's memory config
({store, namespace, k, header}), resolves the store — a
:class:~teff.memory.base.MemoryStore instance, or a config dict
built via memory_from_config using ctx's provider registry —
and recalls memories for the most recent user message. Namespace
segments may reference ${owner} / ${session_id} /
${checkpoint_id}, resolved from ctx — the building block for
per-user memory behind a shared multi-tenant graph.
The returned block is meant to be prepended to the LLM messages as a
system message; it is empty when memory is unconfigured or
nothing matched.
Source code in teff/memory/context.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |