@falai/agent 0.3.20 → 0.3.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +18 -6
  2. package/dist/adapters/MemoryAdapter.d.ts +47 -0
  3. package/dist/adapters/MemoryAdapter.d.ts.map +1 -0
  4. package/dist/adapters/MemoryAdapter.js +178 -0
  5. package/dist/adapters/MemoryAdapter.js.map +1 -0
  6. package/dist/adapters/OpenSearchAdapter.d.ts +169 -0
  7. package/dist/adapters/OpenSearchAdapter.d.ts.map +1 -0
  8. package/dist/adapters/OpenSearchAdapter.js +457 -0
  9. package/dist/adapters/OpenSearchAdapter.js.map +1 -0
  10. package/dist/adapters/SQLiteAdapter.d.ts +69 -0
  11. package/dist/adapters/SQLiteAdapter.d.ts.map +1 -0
  12. package/dist/adapters/SQLiteAdapter.js +307 -0
  13. package/dist/adapters/SQLiteAdapter.js.map +1 -0
  14. package/dist/adapters/index.d.ts +5 -0
  15. package/dist/adapters/index.d.ts.map +1 -1
  16. package/dist/adapters/index.js +3 -0
  17. package/dist/adapters/index.js.map +1 -1
  18. package/dist/cjs/adapters/MemoryAdapter.d.ts +47 -0
  19. package/dist/cjs/adapters/MemoryAdapter.d.ts.map +1 -0
  20. package/dist/cjs/adapters/MemoryAdapter.js +182 -0
  21. package/dist/cjs/adapters/MemoryAdapter.js.map +1 -0
  22. package/dist/cjs/adapters/OpenSearchAdapter.d.ts +169 -0
  23. package/dist/cjs/adapters/OpenSearchAdapter.d.ts.map +1 -0
  24. package/dist/cjs/adapters/OpenSearchAdapter.js +461 -0
  25. package/dist/cjs/adapters/OpenSearchAdapter.js.map +1 -0
  26. package/dist/cjs/adapters/SQLiteAdapter.d.ts +69 -0
  27. package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +1 -0
  28. package/dist/cjs/adapters/SQLiteAdapter.js +311 -0
  29. package/dist/cjs/adapters/SQLiteAdapter.js.map +1 -0
  30. package/dist/cjs/adapters/index.d.ts +5 -0
  31. package/dist/cjs/adapters/index.d.ts.map +1 -1
  32. package/dist/cjs/adapters/index.js +7 -1
  33. package/dist/cjs/adapters/index.js.map +1 -1
  34. package/dist/cjs/index.d.ts +5 -0
  35. package/dist/cjs/index.d.ts.map +1 -1
  36. package/dist/cjs/index.js +7 -1
  37. package/dist/cjs/index.js.map +1 -1
  38. package/dist/index.d.ts +5 -0
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +3 -0
  41. package/dist/index.js.map +1 -1
  42. package/docs/ADAPTERS.md +39 -3
  43. package/docs/API_REFERENCE.md +179 -0
  44. package/docs/PERSISTENCE.md +154 -7
  45. package/docs/README.md +27 -2
  46. package/examples/opensearch-persistence.ts +175 -0
  47. package/package.json +10 -2
  48. package/src/adapters/MemoryAdapter.ts +245 -0
  49. package/src/adapters/OpenSearchAdapter.ts +666 -0
  50. package/src/adapters/SQLiteAdapter.ts +449 -0
  51. package/src/adapters/index.ts +15 -0
  52. package/src/index.ts +12 -0
@@ -0,0 +1,666 @@
1
+ /**
2
+ * OpenSearch Persistence Adapter
3
+ *
4
+ * Provides persistence for sessions and messages using OpenSearch.
5
+ * Also compatible with Elasticsearch 7.x (not tested with newer versions).
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { Client } from '@opensearch-project/opensearch';
10
+ * import { OpenSearchAdapter } from '@falai/agent';
11
+ *
12
+ * const client = new Client({
13
+ * node: 'https://localhost:9200',
14
+ * auth: {
15
+ * username: 'admin',
16
+ * password: 'admin'
17
+ * }
18
+ * });
19
+ *
20
+ * const adapter = new OpenSearchAdapter(client, {
21
+ * indices: {
22
+ * sessions: 'agent_sessions',
23
+ * messages: 'agent_messages'
24
+ * },
25
+ * autoCreateIndices: true
26
+ * });
27
+ *
28
+ * const agent = new Agent({
29
+ * model: provider,
30
+ * persistence: { adapter }
31
+ * });
32
+ * ```
33
+ */
34
+
35
+ import type {
36
+ PersistenceAdapter,
37
+ SessionRepository,
38
+ MessageRepository,
39
+ SessionData,
40
+ MessageData,
41
+ } from "../types/persistence.js";
42
+
43
+ /**
44
+ * OpenSearch Client interface (minimal typing for the official client)
45
+ */
46
+ export interface OpenSearchClient {
47
+ index(params: {
48
+ index: string;
49
+ id?: string;
50
+ body: Record<string, unknown>;
51
+ refresh?: boolean | "wait_for";
52
+ }): Promise<{ body: { _id: string; result: string } }>;
53
+
54
+ get(params: {
55
+ index: string;
56
+ id: string;
57
+ }): Promise<{ body: { _source: Record<string, unknown> } }>;
58
+
59
+ update(params: {
60
+ index: string;
61
+ id: string;
62
+ body: { doc: Record<string, unknown> };
63
+ refresh?: boolean | "wait_for";
64
+ }): Promise<{ body: { result: string } }>;
65
+
66
+ delete(params: {
67
+ index: string;
68
+ id: string;
69
+ refresh?: boolean | "wait_for";
70
+ }): Promise<{ body: { result: string } }>;
71
+
72
+ deleteByQuery(params: {
73
+ index: string;
74
+ body: { query: Record<string, unknown> };
75
+ refresh?: boolean | "wait_for";
76
+ }): Promise<{ body: { deleted: number } }>;
77
+
78
+ search(params: {
79
+ index: string;
80
+ body: {
81
+ query?: Record<string, unknown>;
82
+ sort?: Array<Record<string, unknown>>;
83
+ size?: number;
84
+ };
85
+ }): Promise<{
86
+ body: {
87
+ hits: {
88
+ hits: Array<{
89
+ _id: string;
90
+ _source: Record<string, unknown>;
91
+ }>;
92
+ };
93
+ };
94
+ }>;
95
+
96
+ indices: {
97
+ exists(params: { index: string }): Promise<{ body: boolean }>;
98
+ create(params: {
99
+ index: string;
100
+ body: { mappings?: Record<string, unknown> };
101
+ }): Promise<{ body: { acknowledged: boolean } }>;
102
+ };
103
+ }
104
+
105
+ /**
106
+ * Configuration options for the OpenSearch adapter
107
+ */
108
+ export interface OpenSearchAdapterOptions {
109
+ /**
110
+ * Index names for sessions and messages
111
+ * @default { sessions: 'agent_sessions', messages: 'agent_messages' }
112
+ */
113
+ indices?: {
114
+ sessions?: string;
115
+ messages?: string;
116
+ };
117
+
118
+ /**
119
+ * Automatically create indices with mappings if they don't exist
120
+ * @default true
121
+ */
122
+ autoCreateIndices?: boolean;
123
+
124
+ /**
125
+ * Refresh strategy for write operations
126
+ * - true: Refresh immediately (slower, good for testing)
127
+ * - false: Refresh in background (faster, eventual consistency)
128
+ * - 'wait_for': Wait for refresh (balanced)
129
+ * @default false
130
+ */
131
+ refresh?: boolean | "wait_for";
132
+ }
133
+
134
+ /**
135
+ * OpenSearch persistence adapter
136
+ *
137
+ * Stores sessions and messages as documents in OpenSearch indices.
138
+ * Compatible with OpenSearch 1.x, 2.x and Elasticsearch 7.x.
139
+ */
140
+ export class OpenSearchAdapter implements PersistenceAdapter {
141
+ readonly sessionRepository: SessionRepository;
142
+ readonly messageRepository: MessageRepository;
143
+
144
+ private readonly client: OpenSearchClient;
145
+ private readonly sessionIndex: string;
146
+ private readonly messageIndex: string;
147
+ private readonly autoCreateIndices: boolean;
148
+ private readonly refresh: boolean | "wait_for";
149
+
150
+ constructor(
151
+ client: OpenSearchClient,
152
+ options: OpenSearchAdapterOptions = {}
153
+ ) {
154
+ this.client = client;
155
+ this.sessionIndex = options.indices?.sessions || "agent_sessions";
156
+ this.messageIndex = options.indices?.messages || "agent_messages";
157
+ this.autoCreateIndices = options.autoCreateIndices ?? true;
158
+ this.refresh = options.refresh ?? false;
159
+
160
+ this.sessionRepository = new OpenSearchSessionRepository(
161
+ this.client,
162
+ this.sessionIndex,
163
+ this.refresh
164
+ );
165
+
166
+ this.messageRepository = new OpenSearchMessageRepository(
167
+ this.client,
168
+ this.messageIndex,
169
+ this.refresh
170
+ );
171
+ }
172
+
173
+ async initialize(): Promise<void> {
174
+ if (!this.autoCreateIndices) {
175
+ return;
176
+ }
177
+
178
+ // Create sessions index with mappings
179
+ const sessionExists = await this.client.indices.exists({
180
+ index: this.sessionIndex,
181
+ });
182
+
183
+ if (!sessionExists.body) {
184
+ await this.client.indices.create({
185
+ index: this.sessionIndex,
186
+ body: {
187
+ mappings: {
188
+ properties: {
189
+ id: { type: "keyword" },
190
+ userId: { type: "keyword" },
191
+ agentName: { type: "keyword" },
192
+ status: { type: "keyword" },
193
+ currentRoute: { type: "keyword" },
194
+ currentState: { type: "keyword" },
195
+ collectedData: { type: "object", enabled: false },
196
+ messageCount: { type: "integer" },
197
+ createdAt: { type: "date" },
198
+ updatedAt: { type: "date" },
199
+ lastMessageAt: { type: "date" },
200
+ completedAt: { type: "date" },
201
+ },
202
+ },
203
+ },
204
+ });
205
+ }
206
+
207
+ // Create messages index with mappings
208
+ const messageExists = await this.client.indices.exists({
209
+ index: this.messageIndex,
210
+ });
211
+
212
+ if (!messageExists.body) {
213
+ await this.client.indices.create({
214
+ index: this.messageIndex,
215
+ body: {
216
+ mappings: {
217
+ properties: {
218
+ id: { type: "keyword" },
219
+ sessionId: { type: "keyword" },
220
+ userId: { type: "keyword" },
221
+ role: { type: "keyword" },
222
+ content: { type: "text" },
223
+ route: { type: "keyword" },
224
+ state: { type: "keyword" },
225
+ toolCalls: { type: "object", enabled: false },
226
+ event: { type: "object", enabled: false },
227
+ createdAt: { type: "date" },
228
+ },
229
+ },
230
+ },
231
+ });
232
+ }
233
+ }
234
+
235
+ async disconnect(): Promise<void> {
236
+ // OpenSearch client doesn't have a close method like some other clients
237
+ // Connection pooling is managed automatically
238
+ await Promise.resolve();
239
+ }
240
+ }
241
+
242
+ /**
243
+ * OpenSearch-based session repository implementation
244
+ */
245
+ class OpenSearchSessionRepository implements SessionRepository {
246
+ constructor(
247
+ private client: OpenSearchClient,
248
+ private index: string,
249
+ private refresh: boolean | "wait_for"
250
+ ) {}
251
+
252
+ async create(
253
+ data: Omit<SessionData, "id" | "createdAt">
254
+ ): Promise<SessionData> {
255
+ const id = `sess_${Date.now()}_${Math.random().toString(36).slice(2)}`;
256
+ const now = new Date();
257
+
258
+ const session: SessionData = {
259
+ id,
260
+ ...data,
261
+ createdAt: now,
262
+ };
263
+
264
+ await this.client.index({
265
+ index: this.index,
266
+ id,
267
+ body: this.serializeSession(session),
268
+ refresh: this.refresh,
269
+ });
270
+
271
+ return session;
272
+ }
273
+
274
+ async findById(id: string): Promise<SessionData | null> {
275
+ try {
276
+ const response = await this.client.get({
277
+ index: this.index,
278
+ id,
279
+ });
280
+
281
+ return this.deserializeSession(response.body._source);
282
+ } catch (error) {
283
+ if (this.isNotFoundError(error)) {
284
+ return null;
285
+ }
286
+ throw error;
287
+ }
288
+ }
289
+
290
+ async findActiveByUserId(userId: string): Promise<SessionData | null> {
291
+ const response = await this.client.search({
292
+ index: this.index,
293
+ body: {
294
+ query: {
295
+ bool: {
296
+ must: [{ term: { userId } }, { term: { status: "active" } }],
297
+ },
298
+ },
299
+ sort: [{ createdAt: { order: "desc" } }],
300
+ size: 1,
301
+ },
302
+ });
303
+
304
+ const hits = response.body.hits.hits;
305
+ if (hits.length === 0) {
306
+ return null;
307
+ }
308
+
309
+ return this.deserializeSession(hits[0]._source);
310
+ }
311
+
312
+ async findByUserId(userId: string, limit = 100): Promise<SessionData[]> {
313
+ const response = await this.client.search({
314
+ index: this.index,
315
+ body: {
316
+ query: {
317
+ term: { userId },
318
+ },
319
+ sort: [{ createdAt: { order: "desc" } }],
320
+ size: limit,
321
+ },
322
+ });
323
+
324
+ return response.body.hits.hits.map((hit) =>
325
+ this.deserializeSession(hit._source)
326
+ );
327
+ }
328
+
329
+ async update(
330
+ id: string,
331
+ updates: Partial<Omit<SessionData, "id" | "createdAt">>
332
+ ): Promise<SessionData | null> {
333
+ const doc: Record<string, unknown> = {
334
+ ...updates,
335
+ updatedAt: new Date().toISOString(),
336
+ };
337
+
338
+ // Serialize dates
339
+ if (updates.completedAt) {
340
+ doc.completedAt = updates.completedAt.toISOString();
341
+ }
342
+ if (updates.lastMessageAt) {
343
+ doc.lastMessageAt = updates.lastMessageAt.toISOString();
344
+ }
345
+
346
+ await this.client.update({
347
+ index: this.index,
348
+ id,
349
+ body: { doc },
350
+ refresh: this.refresh,
351
+ });
352
+
353
+ return await this.findById(id);
354
+ }
355
+
356
+ async updateStatus(
357
+ id: string,
358
+ status: SessionData["status"],
359
+ completedAt?: Date
360
+ ): Promise<SessionData | null> {
361
+ const doc: Record<string, unknown> = {
362
+ status,
363
+ updatedAt: new Date().toISOString(),
364
+ };
365
+
366
+ if (completedAt) {
367
+ doc.completedAt = completedAt.toISOString();
368
+ }
369
+
370
+ await this.client.update({
371
+ index: this.index,
372
+ id,
373
+ body: { doc },
374
+ refresh: this.refresh,
375
+ });
376
+
377
+ return await this.findById(id);
378
+ }
379
+
380
+ async updateCollectedData(
381
+ id: string,
382
+ collectedData: Record<string, unknown>
383
+ ): Promise<SessionData | null> {
384
+ await this.client.update({
385
+ index: this.index,
386
+ id,
387
+ body: {
388
+ doc: {
389
+ collectedData,
390
+ updatedAt: new Date().toISOString(),
391
+ },
392
+ },
393
+ refresh: this.refresh,
394
+ });
395
+
396
+ return await this.findById(id);
397
+ }
398
+
399
+ async updateRouteState(
400
+ id: string,
401
+ route?: string,
402
+ state?: string
403
+ ): Promise<SessionData | null> {
404
+ const doc: Record<string, unknown> = {
405
+ updatedAt: new Date().toISOString(),
406
+ };
407
+
408
+ if (route !== undefined) {
409
+ doc.currentRoute = route;
410
+ }
411
+ if (state !== undefined) {
412
+ doc.currentState = state;
413
+ }
414
+
415
+ await this.client.update({
416
+ index: this.index,
417
+ id,
418
+ body: { doc },
419
+ refresh: this.refresh,
420
+ });
421
+
422
+ return await this.findById(id);
423
+ }
424
+
425
+ async incrementMessageCount(id: string): Promise<SessionData | null> {
426
+ const session = await this.findById(id);
427
+ if (!session) {
428
+ return null;
429
+ }
430
+
431
+ const newCount = (session.messageCount || 0) + 1;
432
+
433
+ await this.client.update({
434
+ index: this.index,
435
+ id,
436
+ body: {
437
+ doc: {
438
+ messageCount: newCount,
439
+ lastMessageAt: new Date().toISOString(),
440
+ updatedAt: new Date().toISOString(),
441
+ },
442
+ },
443
+ refresh: this.refresh,
444
+ });
445
+
446
+ return await this.findById(id);
447
+ }
448
+
449
+ async delete(id: string): Promise<boolean> {
450
+ try {
451
+ await this.client.delete({
452
+ index: this.index,
453
+ id,
454
+ refresh: this.refresh,
455
+ });
456
+ return true;
457
+ } catch (error) {
458
+ if (this.isNotFoundError(error)) {
459
+ return false;
460
+ }
461
+ throw error;
462
+ }
463
+ }
464
+
465
+ private serializeSession(session: SessionData): Record<string, unknown> {
466
+ return {
467
+ ...session,
468
+ createdAt: session.createdAt.toISOString(),
469
+ updatedAt: session.updatedAt?.toISOString(),
470
+ completedAt: session.completedAt?.toISOString(),
471
+ };
472
+ }
473
+
474
+ private deserializeSession(doc: Record<string, unknown>): SessionData {
475
+ return {
476
+ id: doc.id as string,
477
+ userId: doc.userId as string | undefined,
478
+ agentName: doc.agentName as string | undefined,
479
+ status: doc.status as SessionData["status"],
480
+ currentRoute: doc.currentRoute as string | undefined,
481
+ currentState: doc.currentState as string | undefined,
482
+ collectedData: doc.collectedData as Record<string, unknown> | undefined,
483
+ messageCount: doc.messageCount as number | undefined,
484
+ createdAt: new Date(doc.createdAt as string),
485
+ updatedAt: new Date(doc.updatedAt as string),
486
+ lastMessageAt: doc.lastMessageAt
487
+ ? new Date(doc.lastMessageAt as string)
488
+ : undefined,
489
+ completedAt: doc.completedAt
490
+ ? new Date(doc.completedAt as string)
491
+ : undefined,
492
+ };
493
+ }
494
+
495
+ private isNotFoundError(error: unknown): boolean {
496
+ return (
497
+ typeof error === "object" &&
498
+ error !== null &&
499
+ "statusCode" in error &&
500
+ error.statusCode === 404
501
+ );
502
+ }
503
+ }
504
+
505
+ /**
506
+ * OpenSearch-based message repository implementation
507
+ */
508
+ class OpenSearchMessageRepository implements MessageRepository {
509
+ constructor(
510
+ private client: OpenSearchClient,
511
+ private index: string,
512
+ private refresh: boolean | "wait_for"
513
+ ) {}
514
+
515
+ async create(
516
+ data: Omit<MessageData, "id" | "createdAt">
517
+ ): Promise<MessageData> {
518
+ const id = `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`;
519
+ const now = new Date();
520
+
521
+ const message: MessageData = {
522
+ id,
523
+ ...data,
524
+ createdAt: now,
525
+ };
526
+
527
+ await this.client.index({
528
+ index: this.index,
529
+ id,
530
+ body: this.serializeMessage(message),
531
+ refresh: this.refresh,
532
+ });
533
+
534
+ return message;
535
+ }
536
+
537
+ async findById(id: string): Promise<MessageData | null> {
538
+ try {
539
+ const response = await this.client.get({
540
+ index: this.index,
541
+ id,
542
+ });
543
+
544
+ return this.deserializeMessage(response.body._source);
545
+ } catch (error) {
546
+ if (this.isNotFoundError(error)) {
547
+ return null;
548
+ }
549
+ throw error;
550
+ }
551
+ }
552
+
553
+ async findBySessionId(
554
+ sessionId: string,
555
+ limit = 1000
556
+ ): Promise<MessageData[]> {
557
+ const response = await this.client.search({
558
+ index: this.index,
559
+ body: {
560
+ query: {
561
+ term: { sessionId },
562
+ },
563
+ sort: [{ createdAt: { order: "asc" } }],
564
+ size: limit,
565
+ },
566
+ });
567
+
568
+ return response.body.hits.hits.map((hit) =>
569
+ this.deserializeMessage(hit._source)
570
+ );
571
+ }
572
+
573
+ async findByUserId(userId: string, limit = 100): Promise<MessageData[]> {
574
+ const response = await this.client.search({
575
+ index: this.index,
576
+ body: {
577
+ query: {
578
+ term: { userId },
579
+ },
580
+ sort: [{ createdAt: { order: "desc" } }],
581
+ size: limit,
582
+ },
583
+ });
584
+
585
+ return response.body.hits.hits.map((hit) =>
586
+ this.deserializeMessage(hit._source)
587
+ );
588
+ }
589
+
590
+ async delete(id: string): Promise<boolean> {
591
+ try {
592
+ await this.client.delete({
593
+ index: this.index,
594
+ id,
595
+ refresh: this.refresh,
596
+ });
597
+ return true;
598
+ } catch (error) {
599
+ if (this.isNotFoundError(error)) {
600
+ return false;
601
+ }
602
+ throw error;
603
+ }
604
+ }
605
+
606
+ async deleteBySessionId(sessionId: string): Promise<number> {
607
+ const response = await this.client.deleteByQuery({
608
+ index: this.index,
609
+ body: {
610
+ query: {
611
+ term: { sessionId },
612
+ },
613
+ },
614
+ refresh: this.refresh,
615
+ });
616
+
617
+ return response.body.deleted;
618
+ }
619
+
620
+ async deleteByUserId(userId: string): Promise<number> {
621
+ const response = await this.client.deleteByQuery({
622
+ index: this.index,
623
+ body: {
624
+ query: {
625
+ term: { userId },
626
+ },
627
+ },
628
+ refresh: this.refresh,
629
+ });
630
+
631
+ return response.body.deleted;
632
+ }
633
+
634
+ private serializeMessage(message: MessageData): Record<string, unknown> {
635
+ return {
636
+ ...message,
637
+ createdAt: message.createdAt.toISOString(),
638
+ };
639
+ }
640
+
641
+ private deserializeMessage(doc: Record<string, unknown>): MessageData {
642
+ return {
643
+ id: doc.id as string,
644
+ sessionId: doc.sessionId as string,
645
+ userId: doc.userId as string | undefined,
646
+ role: doc.role as MessageData["role"],
647
+ content: doc.content as string,
648
+ route: doc.route as string | undefined,
649
+ state: doc.state as string | undefined,
650
+ toolCalls: doc.toolCalls as
651
+ | Array<{ toolName: string; arguments: Record<string, unknown> }>
652
+ | undefined,
653
+ event: doc.event as MessageData["event"] | undefined,
654
+ createdAt: new Date(doc.createdAt as string),
655
+ };
656
+ }
657
+
658
+ private isNotFoundError(error: unknown): boolean {
659
+ return (
660
+ typeof error === "object" &&
661
+ error !== null &&
662
+ "statusCode" in error &&
663
+ error.statusCode === 404
664
+ );
665
+ }
666
+ }