@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.
- package/README.md +18 -6
- package/dist/adapters/MemoryAdapter.d.ts +47 -0
- package/dist/adapters/MemoryAdapter.d.ts.map +1 -0
- package/dist/adapters/MemoryAdapter.js +178 -0
- package/dist/adapters/MemoryAdapter.js.map +1 -0
- package/dist/adapters/OpenSearchAdapter.d.ts +169 -0
- package/dist/adapters/OpenSearchAdapter.d.ts.map +1 -0
- package/dist/adapters/OpenSearchAdapter.js +457 -0
- package/dist/adapters/OpenSearchAdapter.js.map +1 -0
- package/dist/adapters/SQLiteAdapter.d.ts +69 -0
- package/dist/adapters/SQLiteAdapter.d.ts.map +1 -0
- package/dist/adapters/SQLiteAdapter.js +307 -0
- package/dist/adapters/SQLiteAdapter.js.map +1 -0
- package/dist/adapters/index.d.ts +5 -0
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js +3 -0
- package/dist/adapters/index.js.map +1 -1
- package/dist/cjs/adapters/MemoryAdapter.d.ts +47 -0
- package/dist/cjs/adapters/MemoryAdapter.d.ts.map +1 -0
- package/dist/cjs/adapters/MemoryAdapter.js +182 -0
- package/dist/cjs/adapters/MemoryAdapter.js.map +1 -0
- package/dist/cjs/adapters/OpenSearchAdapter.d.ts +169 -0
- package/dist/cjs/adapters/OpenSearchAdapter.d.ts.map +1 -0
- package/dist/cjs/adapters/OpenSearchAdapter.js +461 -0
- package/dist/cjs/adapters/OpenSearchAdapter.js.map +1 -0
- package/dist/cjs/adapters/SQLiteAdapter.d.ts +69 -0
- package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +1 -0
- package/dist/cjs/adapters/SQLiteAdapter.js +311 -0
- package/dist/cjs/adapters/SQLiteAdapter.js.map +1 -0
- package/dist/cjs/adapters/index.d.ts +5 -0
- package/dist/cjs/adapters/index.d.ts.map +1 -1
- package/dist/cjs/adapters/index.js +7 -1
- package/dist/cjs/adapters/index.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +7 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/docs/ADAPTERS.md +39 -3
- package/docs/API_REFERENCE.md +179 -0
- package/docs/PERSISTENCE.md +154 -7
- package/docs/README.md +27 -2
- package/examples/opensearch-persistence.ts +175 -0
- package/package.json +10 -2
- package/src/adapters/MemoryAdapter.ts +245 -0
- package/src/adapters/OpenSearchAdapter.ts +666 -0
- package/src/adapters/SQLiteAdapter.ts +449 -0
- package/src/adapters/index.ts +15 -0
- package/src/index.ts +12 -0
package/docs/API_REFERENCE.md
CHANGED
|
@@ -947,6 +947,185 @@ const agent = new Agent({
|
|
|
947
947
|
|
|
948
948
|
---
|
|
949
949
|
|
|
950
|
+
### `SQLiteAdapter`
|
|
951
|
+
|
|
952
|
+
Lightweight, file-based database for local development.
|
|
953
|
+
|
|
954
|
+
#### Constructor
|
|
955
|
+
|
|
956
|
+
```typescript
|
|
957
|
+
new SQLiteAdapter(options: SQLiteAdapterOptions)
|
|
958
|
+
|
|
959
|
+
interface SQLiteAdapterOptions {
|
|
960
|
+
db: SqliteDatabase; // better-sqlite3 database
|
|
961
|
+
tables?: {
|
|
962
|
+
sessions?: string; // Default: "agent_sessions"
|
|
963
|
+
messages?: string; // Default: "agent_messages"
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
```
|
|
967
|
+
|
|
968
|
+
#### Methods
|
|
969
|
+
|
|
970
|
+
##### `initialize(): Promise<void>`
|
|
971
|
+
|
|
972
|
+
Creates tables and indexes if they don't exist.
|
|
973
|
+
|
|
974
|
+
#### Example
|
|
975
|
+
|
|
976
|
+
```typescript
|
|
977
|
+
import { SQLiteAdapter } from "@falai/agent";
|
|
978
|
+
import Database from "better-sqlite3";
|
|
979
|
+
|
|
980
|
+
const db = new Database("agent.db");
|
|
981
|
+
const adapter = new SQLiteAdapter({ db });
|
|
982
|
+
|
|
983
|
+
// Auto-create tables
|
|
984
|
+
await adapter.initialize();
|
|
985
|
+
|
|
986
|
+
const agent = new Agent({
|
|
987
|
+
persistence: { adapter },
|
|
988
|
+
});
|
|
989
|
+
```
|
|
990
|
+
|
|
991
|
+
**Install:** `npm install better-sqlite3`
|
|
992
|
+
|
|
993
|
+
**Perfect for:** Local development, testing, desktop apps, single-user applications
|
|
994
|
+
|
|
995
|
+
---
|
|
996
|
+
|
|
997
|
+
### `OpenSearchAdapter`
|
|
998
|
+
|
|
999
|
+
Full-text search and analytics-powered persistence. Compatible with OpenSearch and Elasticsearch 7.x.
|
|
1000
|
+
|
|
1001
|
+
#### Constructor
|
|
1002
|
+
|
|
1003
|
+
```typescript
|
|
1004
|
+
new OpenSearchAdapter(client: OpenSearchClient, options?: OpenSearchAdapterOptions)
|
|
1005
|
+
|
|
1006
|
+
interface OpenSearchAdapterOptions {
|
|
1007
|
+
indices?: {
|
|
1008
|
+
sessions?: string; // Default: "agent_sessions"
|
|
1009
|
+
messages?: string; // Default: "agent_messages"
|
|
1010
|
+
};
|
|
1011
|
+
autoCreateIndices?: boolean; // Default: true
|
|
1012
|
+
refresh?: boolean | "wait_for"; // Default: false
|
|
1013
|
+
}
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
#### Methods
|
|
1017
|
+
|
|
1018
|
+
##### `initialize(): Promise<void>`
|
|
1019
|
+
|
|
1020
|
+
Creates indices with proper mappings if they don't exist.
|
|
1021
|
+
|
|
1022
|
+
##### `disconnect(): Promise<void>`
|
|
1023
|
+
|
|
1024
|
+
Gracefully disconnect (no-op for OpenSearch - connection pooling is automatic).
|
|
1025
|
+
|
|
1026
|
+
#### Example
|
|
1027
|
+
|
|
1028
|
+
```typescript
|
|
1029
|
+
import { OpenSearchAdapter } from "@falai/agent";
|
|
1030
|
+
import { Client } from "@opensearch-project/opensearch";
|
|
1031
|
+
|
|
1032
|
+
const client = new Client({
|
|
1033
|
+
node: "https://localhost:9200",
|
|
1034
|
+
auth: {
|
|
1035
|
+
username: "admin",
|
|
1036
|
+
password: "admin",
|
|
1037
|
+
},
|
|
1038
|
+
});
|
|
1039
|
+
|
|
1040
|
+
const adapter = new OpenSearchAdapter(client, {
|
|
1041
|
+
indices: {
|
|
1042
|
+
sessions: "agent_sessions",
|
|
1043
|
+
messages: "agent_messages",
|
|
1044
|
+
},
|
|
1045
|
+
autoCreateIndices: true,
|
|
1046
|
+
refresh: "wait_for", // Wait for documents to be searchable
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
// Auto-create indices with mappings
|
|
1050
|
+
await adapter.initialize();
|
|
1051
|
+
|
|
1052
|
+
const agent = new Agent({
|
|
1053
|
+
persistence: { adapter },
|
|
1054
|
+
});
|
|
1055
|
+
```
|
|
1056
|
+
|
|
1057
|
+
**Install:** `npm install @opensearch-project/opensearch`
|
|
1058
|
+
|
|
1059
|
+
**Perfect for:** Full-text search, analytics, time-series analysis, AWS OpenSearch Service, Elasticsearch 7.x users
|
|
1060
|
+
|
|
1061
|
+
**Full Example:** See [examples/opensearch-persistence.ts](../examples/opensearch-persistence.ts)
|
|
1062
|
+
|
|
1063
|
+
---
|
|
1064
|
+
|
|
1065
|
+
### `MemoryAdapter`
|
|
1066
|
+
|
|
1067
|
+
Zero-dependency in-memory storage for testing and development.
|
|
1068
|
+
|
|
1069
|
+
#### Constructor
|
|
1070
|
+
|
|
1071
|
+
```typescript
|
|
1072
|
+
new MemoryAdapter();
|
|
1073
|
+
```
|
|
1074
|
+
|
|
1075
|
+
No options needed - it's ready to go! āØ
|
|
1076
|
+
|
|
1077
|
+
#### Methods
|
|
1078
|
+
|
|
1079
|
+
##### `clear(): void`
|
|
1080
|
+
|
|
1081
|
+
Clears all stored data (useful for testing).
|
|
1082
|
+
|
|
1083
|
+
##### `getSnapshot(): { sessions: SessionData[]; messages: MessageData[] }`
|
|
1084
|
+
|
|
1085
|
+
Returns a snapshot of all data (useful for debugging/testing).
|
|
1086
|
+
|
|
1087
|
+
#### Example
|
|
1088
|
+
|
|
1089
|
+
```typescript
|
|
1090
|
+
import { MemoryAdapter } from "@falai/agent";
|
|
1091
|
+
|
|
1092
|
+
const adapter = new MemoryAdapter();
|
|
1093
|
+
|
|
1094
|
+
const agent = new Agent({
|
|
1095
|
+
persistence: { adapter },
|
|
1096
|
+
});
|
|
1097
|
+
|
|
1098
|
+
// Perfect for unit tests!
|
|
1099
|
+
```
|
|
1100
|
+
|
|
1101
|
+
**Testing Example:**
|
|
1102
|
+
|
|
1103
|
+
```typescript
|
|
1104
|
+
describe("Agent", () => {
|
|
1105
|
+
const adapter = new MemoryAdapter();
|
|
1106
|
+
|
|
1107
|
+
afterEach(() => {
|
|
1108
|
+
adapter.clear(); // Reset between tests
|
|
1109
|
+
});
|
|
1110
|
+
|
|
1111
|
+
it("should persist messages", async () => {
|
|
1112
|
+
const agent = new Agent({
|
|
1113
|
+
persistence: { adapter },
|
|
1114
|
+
});
|
|
1115
|
+
|
|
1116
|
+
// ... test logic ...
|
|
1117
|
+
|
|
1118
|
+
const { sessions, messages } = adapter.getSnapshot();
|
|
1119
|
+
expect(sessions).toHaveLength(1);
|
|
1120
|
+
expect(messages).toHaveLength(2);
|
|
1121
|
+
});
|
|
1122
|
+
});
|
|
1123
|
+
```
|
|
1124
|
+
|
|
1125
|
+
**No installation required** - built into the framework!
|
|
1126
|
+
|
|
1127
|
+
---
|
|
1128
|
+
|
|
950
1129
|
### Persistence Types
|
|
951
1130
|
|
|
952
1131
|
#### `SessionData`
|
package/docs/PERSISTENCE.md
CHANGED
|
@@ -434,16 +434,17 @@ const agent = new Agent({
|
|
|
434
434
|
|
|
435
435
|
**Install:** `npm install ioredis` or `npm install redis`
|
|
436
436
|
|
|
437
|
-
###
|
|
437
|
+
### More Adapters
|
|
438
438
|
|
|
439
|
-
|
|
439
|
+
All adapters are production-ready and available now:
|
|
440
440
|
|
|
441
|
-
- **MongoDB**: Document-based storage ā
|
|
442
|
-
- **PostgreSQL**: Raw SQL for custom schemas ā
|
|
443
|
-
- **
|
|
444
|
-
- **
|
|
441
|
+
- **MongoDB**: Document-based storage ā
|
|
442
|
+
- **PostgreSQL**: Raw SQL for custom schemas ā
|
|
443
|
+
- **SQLite**: Lightweight file-based database ā
|
|
444
|
+
- **OpenSearch**: Full-text search & analytics ā
|
|
445
|
+
- **Memory**: Built-in for testing ā
|
|
445
446
|
|
|
446
|
-
|
|
447
|
+
Create your own adapter by implementing the `PersistenceAdapter` interface!
|
|
447
448
|
|
|
448
449
|
### MongoDB
|
|
449
450
|
|
|
@@ -511,3 +512,149 @@ const agent = new Agent({
|
|
|
511
512
|
**Install:** `npm install pg`
|
|
512
513
|
|
|
513
514
|
**Note:** PostgreSQL adapter includes `initialize()` method to auto-create tables with proper indexes and foreign keys.
|
|
515
|
+
|
|
516
|
+
### SQLite
|
|
517
|
+
|
|
518
|
+
Lightweight, file-based database for local development:
|
|
519
|
+
|
|
520
|
+
```typescript
|
|
521
|
+
import { SQLiteAdapter } from "@falai/agent";
|
|
522
|
+
import Database from "better-sqlite3";
|
|
523
|
+
|
|
524
|
+
const db = new Database("agent.db");
|
|
525
|
+
|
|
526
|
+
const adapter = new SQLiteAdapter({ db });
|
|
527
|
+
|
|
528
|
+
// Auto-create tables
|
|
529
|
+
await adapter.initialize();
|
|
530
|
+
|
|
531
|
+
const agent = new Agent({
|
|
532
|
+
persistence: { adapter },
|
|
533
|
+
});
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
**Install:** `npm install better-sqlite3`
|
|
537
|
+
|
|
538
|
+
**Perfect for:**
|
|
539
|
+
|
|
540
|
+
- Local development
|
|
541
|
+
- Testing
|
|
542
|
+
- Desktop applications
|
|
543
|
+
- Single-user apps
|
|
544
|
+
|
|
545
|
+
### OpenSearch
|
|
546
|
+
|
|
547
|
+
Full-text search and analytics-powered persistence. Also compatible with Elasticsearch 7.x:
|
|
548
|
+
|
|
549
|
+
```typescript
|
|
550
|
+
import { OpenSearchAdapter } from "@falai/agent";
|
|
551
|
+
import { Client } from "@opensearch-project/opensearch";
|
|
552
|
+
|
|
553
|
+
const client = new Client({
|
|
554
|
+
node: "https://localhost:9200",
|
|
555
|
+
auth: {
|
|
556
|
+
username: "admin",
|
|
557
|
+
password: "admin",
|
|
558
|
+
},
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
const adapter = new OpenSearchAdapter(client, {
|
|
562
|
+
indices: {
|
|
563
|
+
sessions: "agent_sessions",
|
|
564
|
+
messages: "agent_messages",
|
|
565
|
+
},
|
|
566
|
+
autoCreateIndices: true, // Auto-create indices with mappings
|
|
567
|
+
refresh: "wait_for", // Ensure documents are searchable immediately
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
// Auto-create indices with mappings
|
|
571
|
+
await adapter.initialize();
|
|
572
|
+
|
|
573
|
+
const agent = new Agent({
|
|
574
|
+
persistence: { adapter },
|
|
575
|
+
});
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
**Install:** `npm install @opensearch-project/opensearch`
|
|
579
|
+
|
|
580
|
+
**Perfect for:**
|
|
581
|
+
|
|
582
|
+
- Full-text search across conversations
|
|
583
|
+
- Analytics and aggregations
|
|
584
|
+
- Time-series analysis
|
|
585
|
+
- AWS OpenSearch Service
|
|
586
|
+
- Elasticsearch 7.x users
|
|
587
|
+
|
|
588
|
+
**Advanced features:**
|
|
589
|
+
|
|
590
|
+
```typescript
|
|
591
|
+
// Get OpenSearch client for custom queries
|
|
592
|
+
const pm = agent.getPersistenceManager();
|
|
593
|
+
if (pm) {
|
|
594
|
+
const messages = await pm.getSessionMessages(sessionId);
|
|
595
|
+
|
|
596
|
+
// Now use the client directly for advanced queries
|
|
597
|
+
const results = await client.search({
|
|
598
|
+
index: "agent_messages",
|
|
599
|
+
body: {
|
|
600
|
+
query: {
|
|
601
|
+
match: {
|
|
602
|
+
content: "flight booking",
|
|
603
|
+
},
|
|
604
|
+
},
|
|
605
|
+
aggregations: {
|
|
606
|
+
by_route: {
|
|
607
|
+
terms: { field: "route" },
|
|
608
|
+
},
|
|
609
|
+
},
|
|
610
|
+
},
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
### Memory (Built-in)
|
|
616
|
+
|
|
617
|
+
Zero-dependency in-memory storage for testing:
|
|
618
|
+
|
|
619
|
+
```typescript
|
|
620
|
+
import { MemoryAdapter } from "@falai/agent";
|
|
621
|
+
|
|
622
|
+
const agent = new Agent({
|
|
623
|
+
persistence: {
|
|
624
|
+
adapter: new MemoryAdapter(),
|
|
625
|
+
userId: "test_user",
|
|
626
|
+
},
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
// Perfect for unit tests - no database setup required!
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
**Features:**
|
|
633
|
+
|
|
634
|
+
- No installation required āØ
|
|
635
|
+
- Perfect for testing
|
|
636
|
+
- Data snapshot for debugging
|
|
637
|
+
- Clear method for test cleanup
|
|
638
|
+
|
|
639
|
+
**Example test:**
|
|
640
|
+
|
|
641
|
+
```typescript
|
|
642
|
+
describe("Agent persistence", () => {
|
|
643
|
+
const adapter = new MemoryAdapter();
|
|
644
|
+
|
|
645
|
+
afterEach(() => {
|
|
646
|
+
adapter.clear(); // Clean state between tests
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
it("should save session", async () => {
|
|
650
|
+
const agent = new Agent({
|
|
651
|
+
persistence: { adapter },
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
// Test your logic...
|
|
655
|
+
|
|
656
|
+
const snapshot = adapter.getSnapshot();
|
|
657
|
+
expect(snapshot.sessions).toHaveLength(1);
|
|
658
|
+
});
|
|
659
|
+
});
|
|
660
|
+
```
|
package/docs/README.md
CHANGED
|
@@ -16,7 +16,9 @@ Welcome to the `@falai/agent` documentation!
|
|
|
16
16
|
### Reference
|
|
17
17
|
|
|
18
18
|
- **[API Reference](./API_REFERENCE.md)** - Complete API documentation for all classes, methods, and types
|
|
19
|
-
- **[AI Providers Guide](./PROVIDERS.md)** - Gemini, OpenAI, and custom providers
|
|
19
|
+
- **[AI Providers Guide](./PROVIDERS.md)** - Gemini, OpenAI, Anthropic, and custom providers
|
|
20
|
+
- **[Persistence Guide](./PERSISTENCE.md)** - Auto-save sessions and messages to any database
|
|
21
|
+
- **[Database Adapters](./ADAPTERS.md)** - Adapter comparison and configuration examples
|
|
20
22
|
- **[Contributing Guide](./CONTRIBUTING.md)** - How to contribute to the project
|
|
21
23
|
- **[Publishing Guide](./PUBLISHING.md)** - How to publish updates to npm
|
|
22
24
|
|
|
@@ -36,21 +38,44 @@ Welcome to the `@falai/agent` documentation!
|
|
|
36
38
|
**Understanding the codebase?**
|
|
37
39
|
ā Read [Package Structure](./STRUCTURE.md)
|
|
38
40
|
|
|
41
|
+
**Need persistence?**
|
|
42
|
+
ā See [Persistence Guide](./PERSISTENCE.md)
|
|
43
|
+
|
|
39
44
|
### By Topic
|
|
40
45
|
|
|
41
46
|
- **Agent Configuration**: [Constructor Options](./CONSTRUCTOR_OPTIONS.md)
|
|
42
47
|
- **Conversation Flows**: [API Reference - Routes](./API_REFERENCE.md#route)
|
|
43
48
|
- **Tools & Functions**: [API Reference - Tools](./API_REFERENCE.md#definetool)
|
|
44
49
|
- **Disambiguation**: [API Reference - Observations](./API_REFERENCE.md#observation)
|
|
45
|
-
- **AI Providers**: [API Reference
|
|
50
|
+
- **AI Providers**: [Providers Guide](./PROVIDERS.md) | [API Reference](./API_REFERENCE.md#geminiprovider)
|
|
51
|
+
- **Database Persistence**: [Persistence Guide](./PERSISTENCE.md) | [Adapters](./ADAPTERS.md)
|
|
46
52
|
|
|
47
53
|
## š” Examples
|
|
48
54
|
|
|
49
55
|
Check out the [`examples/`](../examples/) directory for complete, runnable examples:
|
|
50
56
|
|
|
57
|
+
### Core Examples
|
|
58
|
+
|
|
51
59
|
- **[Declarative Agent](../examples/declarative-agent.ts)** - Full constructor-based configuration
|
|
52
60
|
- **[Travel Agent](../examples/travel-agent.ts)** - Complex multi-route travel booking system
|
|
53
61
|
- **[Healthcare Agent](../examples/healthcare-agent.ts)** - Disambiguation with observations
|
|
62
|
+
- **[Streaming Agent](../examples/streaming-agent.ts)** - Real-time streaming responses
|
|
63
|
+
|
|
64
|
+
### Persistence Examples
|
|
65
|
+
|
|
66
|
+
- **[Prisma Persistence](../examples/prisma-persistence.ts)** - Auto-save with Prisma ORM
|
|
67
|
+
- **[Redis Persistence](../examples/redis-persistence.ts)** - Fast in-memory persistence
|
|
68
|
+
|
|
69
|
+
### Provider Examples
|
|
70
|
+
|
|
71
|
+
- **[OpenAI Agent](../examples/openai-agent.ts)** - GPT-5 integration
|
|
72
|
+
- **[Gemini Agent](../examples/gemini-agent.ts)** - Google Gemini integration
|
|
73
|
+
- **[Anthropic Agent](../examples/healthcare-agent.ts)** - Claude 3.5 Sonnet
|
|
74
|
+
|
|
75
|
+
### Advanced Examples
|
|
76
|
+
|
|
77
|
+
- **[Domain Scoping](../examples/domain-scoping.ts)** - Control tool access per route
|
|
78
|
+
- **[Rules & Prohibitions](../examples/rules-prohibitions.ts)** - Fine-grained behavior control
|
|
54
79
|
|
|
55
80
|
## š¤ Contributing
|
|
56
81
|
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenSearch Persistence Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to use the OpenSearchAdapter for persistent storage.
|
|
5
|
+
* Also compatible with Elasticsearch 7.x.
|
|
6
|
+
*
|
|
7
|
+
* Setup:
|
|
8
|
+
* 1. Install the OpenSearch client: npm install @opensearch-project/opensearch
|
|
9
|
+
* 2. Run OpenSearch locally:
|
|
10
|
+
* docker run -d -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" opensearchproject/opensearch:latest
|
|
11
|
+
* 3. Run this example: bun run examples/opensearch-persistence.ts
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// @ts-ignore - OpenSearch is a peer dependency
|
|
15
|
+
import { Client } from "@opensearch-project/opensearch";
|
|
16
|
+
import {
|
|
17
|
+
Agent,
|
|
18
|
+
GeminiProvider,
|
|
19
|
+
OpenSearchAdapter,
|
|
20
|
+
defineTool,
|
|
21
|
+
createMessageEvent,
|
|
22
|
+
EventSource,
|
|
23
|
+
} from "../src/index.js";
|
|
24
|
+
|
|
25
|
+
// Initialize OpenSearch client
|
|
26
|
+
const client = new Client({
|
|
27
|
+
node: process.env.OPENSEARCH_URL || "https://localhost:9200",
|
|
28
|
+
auth: {
|
|
29
|
+
username: process.env.OPENSEARCH_USERNAME || "admin",
|
|
30
|
+
password: process.env.OPENSEARCH_PASSWORD || "admin",
|
|
31
|
+
},
|
|
32
|
+
ssl: {
|
|
33
|
+
rejectUnauthorized: false, // For development only!
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Create adapter with custom index names
|
|
38
|
+
const adapter = new OpenSearchAdapter(client, {
|
|
39
|
+
indices: {
|
|
40
|
+
sessions: "my_agent_sessions",
|
|
41
|
+
messages: "my_agent_messages",
|
|
42
|
+
},
|
|
43
|
+
autoCreateIndices: true, // Automatically create indices with mappings
|
|
44
|
+
refresh: "wait_for", // Wait for documents to be searchable (slower but consistent)
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Define context type
|
|
48
|
+
interface TravelContext {
|
|
49
|
+
userId: string;
|
|
50
|
+
userName: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Define a simple tool
|
|
54
|
+
const bookFlight = defineTool<
|
|
55
|
+
TravelContext,
|
|
56
|
+
[destination: string, date: string],
|
|
57
|
+
{ success: boolean; confirmation: string }
|
|
58
|
+
>(
|
|
59
|
+
"book_flight",
|
|
60
|
+
async ({ context }, destination, date) => {
|
|
61
|
+
console.log(`š Booking flight for ${context.userName}...`);
|
|
62
|
+
return {
|
|
63
|
+
data: {
|
|
64
|
+
success: true,
|
|
65
|
+
confirmation: `Flight to ${destination} booked for ${date}`,
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
description: "Book a flight for the user",
|
|
71
|
+
parameters: {
|
|
72
|
+
type: "object",
|
|
73
|
+
properties: {
|
|
74
|
+
destination: { type: "string", description: "Destination city" },
|
|
75
|
+
date: { type: "string", description: "Travel date" },
|
|
76
|
+
},
|
|
77
|
+
required: ["destination", "date"],
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// Create agent with OpenSearch persistence
|
|
83
|
+
const agent = new Agent<TravelContext>({
|
|
84
|
+
name: "Travel Assistant",
|
|
85
|
+
description: "A helpful travel booking assistant",
|
|
86
|
+
ai: new GeminiProvider({
|
|
87
|
+
apiKey: process.env.GEMINI_API_KEY || "",
|
|
88
|
+
model: "gemini-2.0-flash-exp",
|
|
89
|
+
}),
|
|
90
|
+
context: {
|
|
91
|
+
userId: "user_123",
|
|
92
|
+
userName: "Alex",
|
|
93
|
+
},
|
|
94
|
+
persistence: {
|
|
95
|
+
adapter,
|
|
96
|
+
autoSave: true,
|
|
97
|
+
userId: "user_123",
|
|
98
|
+
},
|
|
99
|
+
capabilities: [
|
|
100
|
+
{
|
|
101
|
+
title: "Flight Booking",
|
|
102
|
+
description: "Book flights for travel",
|
|
103
|
+
tools: [bookFlight],
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
async function main() {
|
|
109
|
+
try {
|
|
110
|
+
console.log("š Starting OpenSearch persistence example...\n");
|
|
111
|
+
|
|
112
|
+
// First conversation
|
|
113
|
+
console.log("š¬ User: I need to book a flight to Tokyo");
|
|
114
|
+
const history1 = [
|
|
115
|
+
createMessageEvent(
|
|
116
|
+
EventSource.CUSTOMER,
|
|
117
|
+
"Alex",
|
|
118
|
+
"I need to book a flight to Tokyo"
|
|
119
|
+
),
|
|
120
|
+
];
|
|
121
|
+
let response = await agent.respond({ history: history1 });
|
|
122
|
+
console.log("š¤ Agent:", response.message);
|
|
123
|
+
console.log();
|
|
124
|
+
|
|
125
|
+
// Continue conversation (uses same session)
|
|
126
|
+
console.log("š¬ User: Make it for next Monday");
|
|
127
|
+
const history2 = [
|
|
128
|
+
...history1,
|
|
129
|
+
createMessageEvent(
|
|
130
|
+
EventSource.AI_AGENT,
|
|
131
|
+
"Travel Assistant",
|
|
132
|
+
response.message
|
|
133
|
+
),
|
|
134
|
+
createMessageEvent(
|
|
135
|
+
EventSource.CUSTOMER,
|
|
136
|
+
"Alex",
|
|
137
|
+
"Make it for next Monday"
|
|
138
|
+
),
|
|
139
|
+
];
|
|
140
|
+
response = await agent.respond({ history: history2 });
|
|
141
|
+
console.log("š¤ Agent:", response.message);
|
|
142
|
+
console.log();
|
|
143
|
+
|
|
144
|
+
// Get persistence manager
|
|
145
|
+
const pm = agent.getPersistenceManager();
|
|
146
|
+
if (pm) {
|
|
147
|
+
// Query sessions
|
|
148
|
+
const sessions = await pm.getUserSessions("user_123");
|
|
149
|
+
console.log(`š Found ${sessions.length} session(s) for user_123`);
|
|
150
|
+
|
|
151
|
+
if (sessions.length > 0) {
|
|
152
|
+
const session = sessions[0];
|
|
153
|
+
console.log(` Session ID: ${session.id}`);
|
|
154
|
+
console.log(` Status: ${session.status}`);
|
|
155
|
+
console.log(` Messages: ${session.messageCount || 0}`);
|
|
156
|
+
console.log();
|
|
157
|
+
|
|
158
|
+
// Query messages
|
|
159
|
+
const messages = await pm.getSessionMessages(session.id);
|
|
160
|
+
console.log(`š Session has ${messages.length} message(s):`);
|
|
161
|
+
messages.forEach((msg, idx) => {
|
|
162
|
+
console.log(
|
|
163
|
+
` ${idx + 1}. [${msg.role}] ${msg.content.substring(0, 50)}...`
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
console.log("\nā
OpenSearch persistence example completed!");
|
|
170
|
+
} catch (error) {
|
|
171
|
+
console.error("ā Error:", error);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@falai/agent",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.22",
|
|
4
4
|
"description": "Standalone, strongly-typed AI Agent framework with route DSL and AI provider strategy",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -87,7 +87,9 @@
|
|
|
87
87
|
"redis": "^4.6.0 || ^5.0.0",
|
|
88
88
|
"mongodb": "^6.0.0 || ^7.0.0",
|
|
89
89
|
"pg": "^8.11.0",
|
|
90
|
-
"mysql2": "^3.2.0"
|
|
90
|
+
"mysql2": "^3.2.0",
|
|
91
|
+
"better-sqlite3": "^11.0.0 || ^12.0.0",
|
|
92
|
+
"@opensearch-project/opensearch": "^2.0.0"
|
|
91
93
|
},
|
|
92
94
|
"peerDependenciesMeta": {
|
|
93
95
|
"@prisma/client": {
|
|
@@ -107,6 +109,12 @@
|
|
|
107
109
|
},
|
|
108
110
|
"mysql2": {
|
|
109
111
|
"optional": true
|
|
112
|
+
},
|
|
113
|
+
"better-sqlite3": {
|
|
114
|
+
"optional": true
|
|
115
|
+
},
|
|
116
|
+
"@opensearch-project/opensearch": {
|
|
117
|
+
"optional": true
|
|
110
118
|
}
|
|
111
119
|
}
|
|
112
120
|
}
|