agentsmith 0.1.2

AI Agent forging utilities.
Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<h1 align="center">
  <br>
    <img 
      src="https://github.com/cryptopatrick/factory/blob/master/img/100days/agentsmith.png" 
      alt="AgentSmith" 
      width="200"
    />
  <br>
  AGENTSMITH
  <br>
</h1>

<h4 align="center">
  A Rust library for giving AI agents persistent, searchable memory that survives restarts.
</h4>

<p align="center">
  <a href="https://crates.io/crates/agentsmith" target="_blank">
    <img src="https://img.shields.io/crates/v/agentsmith" alt="Crates.io"/>
  </a>
  <a href="https://crates.io/crates/agentsmith" target="_blank">
    <img src="https://img.shields.io/crates/d/agentsmith" alt="Downloads"/>
  </a>
  <a href="https://docs.rs/agentsmith" target="_blank">
    <img src="https://docs.rs/agentsmith/badge.svg" alt="Documentation"/>
  </a>
  <a href="LICENSE" target="_blank">
    <img src="https://img.shields.io/github/license/sulu/sulu.svg" alt="GitHub license"/>
  </a>
</p>

<b>Author's bio:</b> πŸ‘‹πŸ˜€ Hi, I'm CryptoPatrick! I'm currently enrolled as an
Undergraduate student in Mathematics, at Chalmers & the University of Gothenburg, Sweden. <br>
If you have any questions or need more info, then please <a href="https://discord.gg/T8EWmJZpCB">join my Discord Channel: AiMath</a>

---

<p align="center">
  <a href="#-what-is-agentsmith">What is AgentSmith</a> β€’
  <a href="#-features">Features</a> β€’
  <a href="#-how-to-use">How To Use</a> β€’
  <a href="#-documentation">Documentation</a> β€’
  <a href="#-license">License</a>
</p>

## πŸ›Ž Important Notices
* **Framework Support**: Currently designed for Rig agents, but extensible to other frameworks
* **Storage**: Uses SQLite for persistent storage with full-text search capabilities
* **Inspired by Atuin**: Brings the power of Atuin's searchable shell history to AI agents

<!-- TABLE OF CONTENTS -->
<h2 id="table-of-contents"> :pushpin: Table of Contents</h2>

<details open="open">
  <summary>Table of Contents</summary>
  <ol>
    <li><a href="#-what-is-agentsmith">What is AgentSmith</a></li>
    <li><a href="#-features">Features</a></li>
      <ul>
        <li><a href="#-core-functionality">Core Functionality</a></li>
        <li><a href="#-memory-capabilities">Memory Capabilities</a></li>
        <li><a href="#-smart-agent-features">Smart Agent Features</a></li>
        <li><a href="#-persistence">Persistence</a></li>
      </ul>
    <li><a href="#-architecture">Architecture</a></li>
    <li><a href="#-how-to-use">How to Use</a></li>
    <li><a href="#-examples">Examples</a></li>
    <li><a href="#-testing">Testing</a></li>
    <li><a href="#-documentation">Documentation</a></li>
    <li><a href="#-license">License</a>
  </ol>
</details>

## πŸ€” What is AgentSmith

`agentsmith` is a Rust library that provides AI agents with persistent, fuzzy-searchable, metadata-rich memory that survives process restarts. Inspired by [Atuin](https://github.com/atuinsh/atuin) for shell history, AgentSmith ensures your agents never forget past conversations.

Built specifically for [Rig](https://github.com/0xPlaygrounds/rig) agents, AgentSmith wraps your existing agents with a memory layer that automatically:
- Records all conversations to a SQLite database
- Enables fuzzy search across historical interactions
- Provides session management for organizing conversations
- Allows agents to recall relevant context from past discussions

### Use Cases

- **Persistent AI Assistants**: Build chatbots that remember users across sessions
- **Context-Aware Agents**: Enable agents to reference past conversations naturally
- **Conversation Analytics**: Search and analyze agent interactions over time
- **Session Management**: Organize conversations by session for different contexts
- **Agent Memory Research**: Experiment with long-term memory for AI agents

## πŸ“· Features

`agentsmith` provides a complete memory layer for AI agents with persistent storage and intelligent retrieval:

### πŸ”§ Core Functionality
- **Persistent History**: Automatic storage of all agent interactions in SQLite
- **Fuzzy Search**: Full-text search across conversation history with relevance ranking
- **Session Management**: Group conversations into sessions for different contexts
- **Metadata Tracking**: Store timestamps, roles, and custom metadata with each interaction

### 🧠 Memory Capabilities
- **Automatic Recording**: All agent interactions are automatically saved
- **Smart Retrieval**: Retrieve recent messages or search by content
- **Context Injection**: Automatically include relevant past context in conversations
- **Cross-Session Search**: Search across all conversations or within specific sessions

### πŸ€– Smart Agent Features
- **Transparent Wrapping**: Drop-in wrapper for existing Rig agents
- **Automatic Context**: Agents automatically recall relevant past interactions
- **Session Summaries**: Generate summaries of conversation sessions
- **Conversation History**: Access chronological history of interactions

### πŸ’Ύ Persistence
- **SQLite Storage**: Reliable, file-based persistence with no external dependencies
- **Migration Support**: Built-in database schema migrations
- **Session Continuity**: Resume conversations across process restarts
- **Export Capabilities**: Import/export conversation history

## πŸ“ Architecture

1. πŸ› Overall Architecture
```diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           User Application (CLI/Backend/App)             β”‚
β”‚              Single call: smart_agent.chat()             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   SmartAgent Component                   β”‚
β”‚  β€’ Search history for relevant traces                    β”‚
β”‚  β€’ Inject past context as system message                 β”‚
β”‚  β€’ Call underlying Rig agent                             β”‚
β”‚  β€’ Log user & assistant turns with metadata              β”‚
β”‚  β€’ Auto-summarize every N turns                          β”‚
β”‚  β€’ Track success, duration, token usage                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚                          β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚    Rig Agent   β”‚        β”‚   AgentHistory   β”‚
       β”‚  β€’ GPT-4/etc   β”‚        β”‚  β€’ SQLite Pool   β”‚
       β”‚  β€’ Custom LLM  β”‚        β”‚  β€’ FTS5 Search   β”‚
       β”‚  β€’ Tools       β”‚        β”‚  β€’ JSONL Import  β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚  β€’ Summarization β”‚
                                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                           β”‚
                                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Ό ────────┐
                                   β”‚  SQLite Database β”‚
                                   β”‚  β€’ sessions      β”‚
                                   β”‚  β€’ traces        β”‚
                                   β”‚  β€’ traces_fts    β”‚
                                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

User β†’ SmartAgent β†’ History Search β†’ Rig Agent β†’ Log Response β†’ SQLite



2. πŸšƒ Data Flow Diagram

```diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 chat("How did we fix X?")                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Ό ────────┐
              β”‚    1. FTS5       β”‚
              β”‚     Search       │──────┐
              β”‚     top_k=4      β”‚      β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
                       β”‚                β”‚
                       β”‚  relevant      β”‚
                       β”‚  traces        β”‚
                       β–Ό                β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
              β”‚   2. Build         β”‚    β”‚
              β”‚   Context          β”‚β—„β”€β”€β”€β”˜
              β”‚   Messages         β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
                        β”‚ system: "Relevant past...\n"
                        β”‚ + user: "How did we fix X?"
                        β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚   3. Rig Agent     β”‚
              β”‚       .chat()      β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
                        β”‚ response
                        β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚   4. Log Turn      β”‚
              β”‚   β€’ User msg       β”‚
              β”‚   β€’ Assistant      β”‚
              β”‚   β€’ Metadata       β”‚
              β”‚     - duration     β”‚
              β”‚     - success      β”‚
              β”‚     - tokens       β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
                        β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚   5. Store in      β”‚
              β”‚   traces table     β”‚
              β”‚   + FTS5 index     β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

3. πŸ’Ύ Storage Layer Architecture

```diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ ┐
β”‚                        AgentHistory                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚                     Public API                      β”‚  β”‚
β”‚  β”‚  β€’ new(path, session_id)                            β”‚  β”‚
β”‚  β”‚  β€’ log_turn(message, metadata) β†’ Trace              β”‚  β”‚
β”‚  β”‚  β€’ search(query, limit, success_only) β†’ Vec<Trace>  β”‚  β”‚
β”‚  β”‚  β€’ recent(n) β†’ Vec<Trace>                           β”‚  β”‚
β”‚  β”‚  β€’ summarize_session(agent) β†’ String                β”‚  β”‚
β”‚  β”‚  β€’ import_jsonl(path) β†’ usize                       β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                               β”‚                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚                 SQLite Pool (sqlx)                   β”‚ β”‚
β”‚  β”‚  β€’ Connection management                             β”‚ β”‚
β”‚  β”‚  β€’ Async queries                                     β”‚ β”‚
β”‚  β”‚  β€’ Migration runner                                  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚        SQLite Database (file.db)     β”‚
              β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
              β”‚  β”‚           sessions              β”‚ β”‚
              β”‚  β”‚  - id (PK)                      β”‚ β”‚
              β”‚  β”‚  - summary                      β”‚ β”‚
              β”‚  β”‚  - updated_at                   β”‚ β”‚
              β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
              β”‚                                      β”‚
              β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
              β”‚  β”‚             traces              β”‚ β”‚
              β”‚  β”‚  - id (PK)                      β”‚ β”‚
              β”‚  β”‚  - session_id (FK)              β”‚ β”‚
              β”‚  β”‚  - role (user/assistant/sys)    β”‚ β”‚
              β”‚  β”‚  - content (TEXT)               β”‚ β”‚
              β”‚  β”‚  - metadata (JSON)              β”‚ β”‚
              β”‚  β”‚      β€’ duration_ms              β”‚ β”‚
              β”‚  β”‚      β€’ success                  β”‚ β”‚
              β”‚  β”‚      β€’ tokens_used              β”‚ β”‚
              β”‚  β”‚      β€’ tools_called             β”‚ β”‚
              β”‚  β”‚  - created_at                   β”‚ β”‚
              β”‚  β”‚  - embedding (future)           β”‚ β”‚
              β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
              β”‚                                      β”‚
              β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
              β”‚  β”‚    traces_fts (FTS5 virtual)    β”‚ β”‚
              β”‚  β”‚  - role                         β”‚ β”‚
              β”‚  β”‚  - content                      β”‚ β”‚
              β”‚  β”‚  - metadata                     β”‚ β”‚
              β”‚  β”‚  + INSERT/UPDATE/DELETE triggersβ”‚ β”‚
              β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

  4. ⏳ Trace Lifecycle

```diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 Message (from Rig)                   β”‚
β”‚  β€’ role: String                                      β”‚
β”‚  β€’ content: String                                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β”‚ + metadata HashMap
                    β”‚   (duration, success, tokens)
                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Trace::new()                       β”‚
β”‚  β€’ Generate UUID                                       β”‚
β”‚  β€’ Set timestamp                                       β”‚
β”‚  β€’ Attach session_id                                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                AgentHistory::log_trace()               β”‚
β”‚  β€’ Serialize metadata to JSON                          β”‚
β”‚  β€’ INSERT INTO traces                                  β”‚
β”‚  β€’ FTS5 trigger auto-indexes                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Persisted in SQLite                  β”‚
β”‚  β€’ Queryable via FTS5 MATCH                            β”‚
β”‚  β€’ Ranked by relevance                                 β”‚
β”‚  β€’ Filterable by metadata                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```


## πŸš™ How to Use

### Installation

Add `agentsmith` to your `Cargo.toml`:

```toml
[dependencies]
agentsmith = "0.1"
```

Or install with cargo:

```bash
cargo add agentsmith
```

### Basic Example

```rust
use agentsmith::{AgentHistory, SmartAgent};
use rig::providers::openai;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create persistent history
    let history = AgentHistory::new("my_agent.db", Some("session-123")).await?;

    // Create your Rig agent
    let client = openai::Client::new(&std::env::var("OPENAI_API_KEY")?);
    let agent = client
        .agent("gpt-4")
        .preamble("You are a helpful AI assistant with a perfect memory.")
        .build();

    // Wrap with SmartAgent for automatic memory
    let mut smart_agent = SmartAgent::new(agent, history);

    // Chat with automatic recall of relevant past interactions
    let reply = smart_agent.chat("How did we fix the JSON parsing bug?").await?;
    println!("Agent: {}", reply);

    Ok(())
}
```

### Advanced Usage

```rust
use agentsmith::{AgentHistory, Trace};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create history with custom session ID
    let history = AgentHistory::new("./chat.db", Some("debug-session")).await?;

    // Search past conversations
    let results = history.search("bug fix", 10, false).await?;
    for trace in results {
        println!("[{}] {}: {}",
            trace.created_at.format("%Y-%m-%d %H:%M:%S"),
            trace.role,
            trace.content
        );
    }

    // Get recent messages
    let recent = history.recent(5).await?;
    println!("Found {} recent messages", recent.len());

    // Manually add traces
    history.add_trace("user", "What's the status?").await?;
    history.add_trace("assistant", "All systems operational").await?;

    Ok(())
}
```

## πŸ§ͺ Examples

The repository includes several examples demonstrating different features:

```bash
# Persistent chat REPL that remembers across restarts
cargo run --example persistent_chat

# Demonstrate Atuin-style search capabilities
cargo run --example atuin_search_demo

# Import conversation logs from other sources
cargo run --example import_old_logs

# Basic usage example
cargo run --example basic
```

## πŸ§ͺ Testing

Run the test suite:

```bash
# Run all tests
cargo test

# Run with output
cargo test -- --nocapture
```

## πŸ“š Documentation

Comprehensive documentation is available at [docs.rs/agentsmith](https://docs.rs/agentsmith), including:
- API reference for all public types and functions
- Tutorial on wrapping Rig agents with persistent memory
- Examples of searching and managing conversation history
- Best practices for session management

## πŸ–Š Author

<a href="https://x.com/cryptopatrick">CryptoPatrick</a>

Keybase Verification:
https://keybase.io/cryptopatrick/sigs/8epNh5h2FtIX1UNNmf8YQ-k33M8J-Md4LnAN

## 🐣 Support
Leave a ⭐ if you think this project is cool.

## πŸ—„ License

This project is licensed under MIT. See [LICENSE](LICENSE) for details.