1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
62
63
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
108
109
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""AgentDB-backed LlamaIndex ChatStore implementation."""
# ---------------------------------------------------------------------------
# Role mapping helpers
# ---------------------------------------------------------------------------
"""Convert a LlamaIndex ``MessageRole`` to an AgentDB role string."""
return
"""Convert an AgentDB role string back to a LlamaIndex ``MessageRole``."""
return
# Fall back to USER for unrecognised roles so content is not lost.
return
"""Reconstruct a ``ChatMessage`` from an AgentDB message row dict."""
=
=
# Restore any extra kwargs stored in message metadata.
= or
return
"""A LlamaIndex ``BaseChatStore`` backed by AgentDB conversations.
Each *key* passed to this store maps to one AgentDB conversation. Messages
are persisted with their role and content and retrieved in insertion order.
AgentDB supports the following roles out of the box: ``"user"``,
``"assistant"``, ``"system"``, and ``"tool"``. Any ``MessageRole`` value
is accepted and stored as its string representation.
Args:
db_path: Path to the AgentDB database file (created if absent).
Example::
from llamaindex_agentdb import AgentDBChatStore
from llama_index.core.llms import ChatMessage, MessageRole
chat_store = AgentDBChatStore(db_path="agent.agentdb")
chat_store.add_message(
"session-1",
ChatMessage(role=MessageRole.USER, content="Hello!"),
)
messages = chat_store.get_messages("session-1")
for msg in messages:
print(msg.role, msg.content)
"""
# -----------------------------------------------------------------------
# Pydantic / BaseChatStore plumbing
# -----------------------------------------------------------------------
# BaseChatStore (LlamaIndex >= 0.11) is itself a Pydantic BaseModel.
# We store the db_path as a proper field so it survives serialisation and
# the ``model_json_schema`` / ``class_name`` machinery works correctly.
: =
# Private attributes — managed via object.__setattr__ to avoid Pydantic
# field conflicts.
:
# pragma: no cover
return
# -----------------------------------------------------------------------
# Core interface — BaseChatStore abstract methods
# -----------------------------------------------------------------------
"""Replace all messages for ``key`` with the given list.
The existing conversation is deleted and re-created so the new messages
become the canonical history.
Args:
key: Conversation key / session identifier.
messages: Replacement message list.
"""
# Delete existing conversation (and its messages) if present.
pass
"""Retrieve all messages for ``key`` in chronological order.
Returns an empty list when the key does not exist.
Args:
key: Conversation key / session identifier.
Returns:
List of ``ChatMessage`` objects.
"""
=
return
return
"""Append a single message to the conversation identified by ``key``.
The conversation is created automatically if it does not yet exist.
The ``idx`` parameter is accepted for interface compatibility but
ignored — AgentDB always appends in insertion order.
Args:
key: Conversation key / session identifier.
message: The ``ChatMessage`` to store.
idx: Ignored. Present for ``BaseChatStore`` interface compatibility.
"""
# Ensure the conversation exists; silently ignore if already present.
pass
=
"""Delete all messages for ``key`` and return them.
The conversation record itself is also removed from the database.
Args:
key: Conversation key / session identifier.
Returns:
The list of messages that were stored, or ``None`` if the key did
not exist.
"""
=
return
return None
"""Delete the message at position ``idx`` and return it.
Because AgentDB does not support positional deletes, this method loads
the full message list, removes the message at ``idx``, and replaces the
conversation with the remainder.
Args:
key: Conversation key / session identifier.
idx: Zero-based index of the message to delete.
Returns:
The deleted ``ChatMessage``, or ``None`` when ``idx`` is out of range.
"""
=
return None
=
return
"""Remove and return the last message in the conversation.
Args:
key: Conversation key / session identifier.
Returns:
The removed ``ChatMessage``, or ``None`` when the conversation is
empty or does not exist.
"""
=
return None
return
"""Return all conversation keys stored in this database.
Returns:
List of conversation ID strings.
"""
=
return
return
# -----------------------------------------------------------------------
# Utility
# -----------------------------------------------------------------------
return f