@falai/agent 0.3.12 → 0.3.20
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 +72 -0
- package/dist/adapters/MongoAdapter.d.ts +97 -0
- package/dist/adapters/MongoAdapter.d.ts.map +1 -0
- package/dist/adapters/MongoAdapter.js +163 -0
- package/dist/adapters/MongoAdapter.js.map +1 -0
- package/dist/adapters/PostgreSQLAdapter.d.ts +71 -0
- package/dist/adapters/PostgreSQLAdapter.d.ts.map +1 -0
- package/dist/adapters/PostgreSQLAdapter.js +256 -0
- package/dist/adapters/PostgreSQLAdapter.js.map +1 -0
- package/dist/adapters/RedisAdapter.d.ts +71 -0
- package/dist/adapters/RedisAdapter.d.ts.map +1 -0
- package/dist/adapters/RedisAdapter.js +226 -0
- package/dist/adapters/RedisAdapter.js.map +1 -0
- package/dist/adapters/index.d.ts +6 -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/MongoAdapter.d.ts +97 -0
- package/dist/cjs/adapters/MongoAdapter.d.ts.map +1 -0
- package/dist/cjs/adapters/MongoAdapter.js +167 -0
- package/dist/cjs/adapters/MongoAdapter.js.map +1 -0
- package/dist/cjs/adapters/PostgreSQLAdapter.d.ts +71 -0
- package/dist/cjs/adapters/PostgreSQLAdapter.d.ts.map +1 -0
- package/dist/cjs/adapters/PostgreSQLAdapter.js +260 -0
- package/dist/cjs/adapters/PostgreSQLAdapter.js.map +1 -0
- package/dist/cjs/adapters/RedisAdapter.d.ts +71 -0
- package/dist/cjs/adapters/RedisAdapter.d.ts.map +1 -0
- package/dist/cjs/adapters/RedisAdapter.js +230 -0
- package/dist/cjs/adapters/RedisAdapter.js.map +1 -0
- package/dist/cjs/adapters/index.d.ts +6 -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 +6 -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 +6 -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 +127 -0
- package/docs/API_REFERENCE.md +337 -0
- package/docs/PERSISTENCE.md +100 -6
- package/examples/redis-persistence.ts +89 -0
- package/package.json +22 -2
- package/src/adapters/MongoAdapter.ts +295 -0
- package/src/adapters/PostgreSQLAdapter.ts +417 -0
- package/src/adapters/RedisAdapter.ts +365 -0
- package/src/adapters/index.ts +18 -0
- package/src/index.ts +15 -0
package/README.md
CHANGED
|
@@ -902,6 +902,78 @@ Control agent behavior and communication style per route:
|
|
|
902
902
|
- 💬 Different communication styles per route
|
|
903
903
|
- 🎨 Perfect for multi-channel bots (WhatsApp, email, chat)
|
|
904
904
|
|
|
905
|
+
### 💾 [Prisma Persistence](./examples/prisma-persistence.ts)
|
|
906
|
+
|
|
907
|
+
Complete example of auto-saving sessions and messages with Prisma ORM:
|
|
908
|
+
|
|
909
|
+
- 💾 Auto-save sessions and messages to database
|
|
910
|
+
- 🔄 Load conversation history on agent initialization
|
|
911
|
+
- 📊 Track conversation state across restarts
|
|
912
|
+
- 🎯 Full example with lifecycle hooks
|
|
913
|
+
- 📝 Includes schema example
|
|
914
|
+
|
|
915
|
+
### ⚡ [Redis Persistence](./examples/redis-persistence.ts)
|
|
916
|
+
|
|
917
|
+
Fast, in-memory persistence for high-throughput applications:
|
|
918
|
+
|
|
919
|
+
- 🚀 Lightning-fast session storage
|
|
920
|
+
- ⏰ Configurable TTLs for auto-cleanup
|
|
921
|
+
- 🔑 Custom key prefixes
|
|
922
|
+
- 💨 Perfect for real-time chat applications
|
|
923
|
+
|
|
924
|
+
---
|
|
925
|
+
|
|
926
|
+
## 💾 Database Adapters
|
|
927
|
+
|
|
928
|
+
**Optional persistence** - Choose the database that fits your needs. All adapters follow the same simple provider pattern:
|
|
929
|
+
|
|
930
|
+
### 🎯 Available Adapters
|
|
931
|
+
|
|
932
|
+
| Adapter | Use Case | Install |
|
|
933
|
+
| --------------------- | --------------------------------- | ---------------------------- |
|
|
934
|
+
| **PrismaAdapter** | Type-safe ORM with migrations | `npm install @prisma/client` |
|
|
935
|
+
| **RedisAdapter** | Fast in-memory for real-time apps | `npm install ioredis` |
|
|
936
|
+
| **MongoAdapter** | Flexible document storage | `npm install mongodb` |
|
|
937
|
+
| **PostgreSQLAdapter** | Raw SQL with auto table creation | `npm install pg` |
|
|
938
|
+
|
|
939
|
+
### ⚡ Quick Setup
|
|
940
|
+
|
|
941
|
+
```typescript
|
|
942
|
+
import { Agent, PrismaAdapter } from "@falai/agent";
|
|
943
|
+
import { PrismaClient } from "@prisma/client";
|
|
944
|
+
|
|
945
|
+
const prisma = new PrismaClient();
|
|
946
|
+
|
|
947
|
+
const agent = new Agent({
|
|
948
|
+
name: "My Agent",
|
|
949
|
+
ai: provider,
|
|
950
|
+
persistence: {
|
|
951
|
+
adapter: new PrismaAdapter({ prisma }),
|
|
952
|
+
userId: "user_123",
|
|
953
|
+
autoSave: true, // Automatically save all messages
|
|
954
|
+
},
|
|
955
|
+
});
|
|
956
|
+
```
|
|
957
|
+
|
|
958
|
+
That's it! Sessions and messages are now automatically persisted.
|
|
959
|
+
|
|
960
|
+
### 📚 Full Documentation
|
|
961
|
+
|
|
962
|
+
See [**docs/PERSISTENCE.md**](./docs/PERSISTENCE.md) for:
|
|
963
|
+
|
|
964
|
+
- Complete adapter API reference
|
|
965
|
+
- Custom table names & field mappings
|
|
966
|
+
- Lifecycle hooks for persistence
|
|
967
|
+
- Creating custom adapters
|
|
968
|
+
- Migration guides
|
|
969
|
+
|
|
970
|
+
See [**docs/ADAPTERS.md**](./docs/ADAPTERS.md) for:
|
|
971
|
+
|
|
972
|
+
- Detailed adapter comparison
|
|
973
|
+
- Configuration examples for each database
|
|
974
|
+
- Performance characteristics
|
|
975
|
+
- Type safety guarantees
|
|
976
|
+
|
|
905
977
|
---
|
|
906
978
|
|
|
907
979
|
## 🏗️ Architecture
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MongoDB adapter for persistence
|
|
3
|
+
* Document-based storage with flexible schema
|
|
4
|
+
*/
|
|
5
|
+
import type { SessionRepository, MessageRepository, PersistenceAdapter } from "../types/persistence";
|
|
6
|
+
/**
|
|
7
|
+
* MongoDB collection interface - matches mongodb driver
|
|
8
|
+
*/
|
|
9
|
+
export interface MongoCollection<T = Record<string, unknown>> {
|
|
10
|
+
insertOne(doc: T): Promise<{
|
|
11
|
+
insertedId: unknown;
|
|
12
|
+
}>;
|
|
13
|
+
findOne(filter: Record<string, unknown>): Promise<T | null>;
|
|
14
|
+
find(filter: Record<string, unknown>): {
|
|
15
|
+
sort(sort: Record<string, number>): {
|
|
16
|
+
limit(limit: number): {
|
|
17
|
+
toArray(): Promise<T[]>;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
toArray(): Promise<T[]>;
|
|
21
|
+
};
|
|
22
|
+
updateOne(filter: Record<string, unknown>, update: Record<string, unknown>): Promise<{
|
|
23
|
+
matchedCount: number;
|
|
24
|
+
}>;
|
|
25
|
+
deleteOne(filter: Record<string, unknown>): Promise<{
|
|
26
|
+
deletedCount: number;
|
|
27
|
+
}>;
|
|
28
|
+
deleteMany(filter: Record<string, unknown>): Promise<{
|
|
29
|
+
deletedCount: number;
|
|
30
|
+
}>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* MongoDB database interface
|
|
34
|
+
*/
|
|
35
|
+
export interface MongoDatabase {
|
|
36
|
+
collection<T = Record<string, unknown>>(name: string): MongoCollection<T>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* MongoDB client interface
|
|
40
|
+
*/
|
|
41
|
+
export interface MongoClient {
|
|
42
|
+
db(name?: string): MongoDatabase;
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Options for MongoDB adapter
|
|
47
|
+
*/
|
|
48
|
+
export interface MongoAdapterOptions {
|
|
49
|
+
/**
|
|
50
|
+
* MongoDB client instance
|
|
51
|
+
*/
|
|
52
|
+
client: MongoClient;
|
|
53
|
+
/**
|
|
54
|
+
* Database name
|
|
55
|
+
*/
|
|
56
|
+
databaseName: string;
|
|
57
|
+
/**
|
|
58
|
+
* Collection names (default: "agent_sessions" and "agent_messages")
|
|
59
|
+
*/
|
|
60
|
+
collections?: {
|
|
61
|
+
sessions?: string;
|
|
62
|
+
messages?: string;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* MongoDB Adapter - Provider-style API for MongoDB persistence
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import { MongoClient } from 'mongodb';
|
|
71
|
+
* import { Agent, MongoAdapter } from '@falai/agent';
|
|
72
|
+
*
|
|
73
|
+
* const client = new MongoClient('mongodb://localhost:27017');
|
|
74
|
+
* await client.connect();
|
|
75
|
+
*
|
|
76
|
+
* const agent = new Agent({
|
|
77
|
+
* name: "My Agent",
|
|
78
|
+
* ai: provider,
|
|
79
|
+
* persistence: {
|
|
80
|
+
* adapter: new MongoAdapter({
|
|
81
|
+
* client,
|
|
82
|
+
* databaseName: 'myapp',
|
|
83
|
+
* }),
|
|
84
|
+
* userId: "user_123",
|
|
85
|
+
* },
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare class MongoAdapter implements PersistenceAdapter {
|
|
90
|
+
readonly sessionRepository: SessionRepository;
|
|
91
|
+
readonly messageRepository: MessageRepository;
|
|
92
|
+
private client;
|
|
93
|
+
private db;
|
|
94
|
+
constructor(options: MongoAdapterOptions);
|
|
95
|
+
disconnect(): Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=MongoAdapter.d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,EAAE,OAAO,CAAC,GAC9B,OAAO,CAAC;QAAE,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACtC;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,UAAU,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;CAC3E;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;IACjC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,MAAM,EAAE,WAAW,CAAC;IAEpB;;OAEG;IACH,YAAY,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,WAAW,CAAC,EAAE;QACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,CAAC;CACH;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,YAAa,YAAW,kBAAkB;IACrD,SAAgB,iBAAiB,EAAE,iBAAiB,CAAC;IACrD,SAAgB,iBAAiB,EAAE,iBAAiB,CAAC;IACrD,OAAO,CAAC,MAAM,CAAc;IAC5B,OAAO,CAAC,EAAE,CAAgB;gBAEd,OAAO,EAAE,mBAAmB;IAgBlC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;CAGlC"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MongoDB adapter for persistence
|
|
3
|
+
* Document-based storage with flexible schema
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* MongoDB Adapter - Provider-style API for MongoDB persistence
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { MongoClient } from 'mongodb';
|
|
11
|
+
* import { Agent, MongoAdapter } from '@falai/agent';
|
|
12
|
+
*
|
|
13
|
+
* const client = new MongoClient('mongodb://localhost:27017');
|
|
14
|
+
* await client.connect();
|
|
15
|
+
*
|
|
16
|
+
* const agent = new Agent({
|
|
17
|
+
* name: "My Agent",
|
|
18
|
+
* ai: provider,
|
|
19
|
+
* persistence: {
|
|
20
|
+
* adapter: new MongoAdapter({
|
|
21
|
+
* client,
|
|
22
|
+
* databaseName: 'myapp',
|
|
23
|
+
* }),
|
|
24
|
+
* userId: "user_123",
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export class MongoAdapter {
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.client = options.client;
|
|
32
|
+
this.db = options.client.db(options.databaseName);
|
|
33
|
+
const sessionCollection = options.collections?.sessions || "agent_sessions";
|
|
34
|
+
const messageCollection = options.collections?.messages || "agent_messages";
|
|
35
|
+
this.sessionRepository = new MongoSessionRepository(this.db.collection(sessionCollection));
|
|
36
|
+
this.messageRepository = new MongoMessageRepository(this.db.collection(messageCollection));
|
|
37
|
+
}
|
|
38
|
+
async disconnect() {
|
|
39
|
+
await this.client.close();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* MongoDB Session Repository
|
|
44
|
+
*/
|
|
45
|
+
class MongoSessionRepository {
|
|
46
|
+
constructor(collection) {
|
|
47
|
+
this.collection = collection;
|
|
48
|
+
}
|
|
49
|
+
async create(data) {
|
|
50
|
+
const now = new Date();
|
|
51
|
+
const session = {
|
|
52
|
+
...data,
|
|
53
|
+
id: `session_${Date.now()}_${Math.random().toString(36).slice(2)}`,
|
|
54
|
+
status: data.status || "active",
|
|
55
|
+
messageCount: data.messageCount || 0,
|
|
56
|
+
createdAt: now,
|
|
57
|
+
updatedAt: now,
|
|
58
|
+
};
|
|
59
|
+
await this.collection.insertOne(session);
|
|
60
|
+
return session;
|
|
61
|
+
}
|
|
62
|
+
async findById(id) {
|
|
63
|
+
return await this.collection.findOne({ id });
|
|
64
|
+
}
|
|
65
|
+
async findActiveByUserId(userId) {
|
|
66
|
+
return await this.collection.findOne({ userId, status: "active" });
|
|
67
|
+
}
|
|
68
|
+
async findByUserId(userId, limit = 100) {
|
|
69
|
+
return await this.collection
|
|
70
|
+
.find({ userId })
|
|
71
|
+
.sort({ createdAt: -1 })
|
|
72
|
+
.limit(limit)
|
|
73
|
+
.toArray();
|
|
74
|
+
}
|
|
75
|
+
async update(id, data) {
|
|
76
|
+
const result = await this.collection.updateOne({ id }, { $set: { ...data, updatedAt: new Date() } });
|
|
77
|
+
if (result.matchedCount === 0)
|
|
78
|
+
return null;
|
|
79
|
+
return await this.findById(id);
|
|
80
|
+
}
|
|
81
|
+
async updateStatus(id, status, completedAt) {
|
|
82
|
+
const updateData = {
|
|
83
|
+
status,
|
|
84
|
+
updatedAt: new Date(),
|
|
85
|
+
};
|
|
86
|
+
if (completedAt) {
|
|
87
|
+
updateData.completedAt = completedAt;
|
|
88
|
+
}
|
|
89
|
+
const result = await this.collection.updateOne({ id }, { $set: updateData });
|
|
90
|
+
if (result.matchedCount === 0)
|
|
91
|
+
return null;
|
|
92
|
+
return await this.findById(id);
|
|
93
|
+
}
|
|
94
|
+
async updateCollectedData(id, collectedData) {
|
|
95
|
+
return await this.update(id, { collectedData });
|
|
96
|
+
}
|
|
97
|
+
async updateRouteState(id, route, state) {
|
|
98
|
+
return await this.update(id, {
|
|
99
|
+
currentRoute: route,
|
|
100
|
+
currentState: state,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
async incrementMessageCount(id) {
|
|
104
|
+
const result = await this.collection.updateOne({ id }, {
|
|
105
|
+
$inc: { messageCount: 1 },
|
|
106
|
+
$set: { lastMessageAt: new Date(), updatedAt: new Date() },
|
|
107
|
+
});
|
|
108
|
+
if (result.matchedCount === 0)
|
|
109
|
+
return null;
|
|
110
|
+
return await this.findById(id);
|
|
111
|
+
}
|
|
112
|
+
async delete(id) {
|
|
113
|
+
const result = await this.collection.deleteOne({ id });
|
|
114
|
+
return result.deletedCount > 0;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* MongoDB Message Repository
|
|
119
|
+
*/
|
|
120
|
+
class MongoMessageRepository {
|
|
121
|
+
constructor(collection) {
|
|
122
|
+
this.collection = collection;
|
|
123
|
+
}
|
|
124
|
+
async create(data) {
|
|
125
|
+
const message = {
|
|
126
|
+
...data,
|
|
127
|
+
id: `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`,
|
|
128
|
+
createdAt: new Date(),
|
|
129
|
+
};
|
|
130
|
+
await this.collection.insertOne(message);
|
|
131
|
+
return message;
|
|
132
|
+
}
|
|
133
|
+
async findById(id) {
|
|
134
|
+
return await this.collection.findOne({ id });
|
|
135
|
+
}
|
|
136
|
+
async findBySessionId(sessionId, limit = 1000) {
|
|
137
|
+
return await this.collection
|
|
138
|
+
.find({ sessionId })
|
|
139
|
+
.sort({ createdAt: 1 })
|
|
140
|
+
.limit(limit)
|
|
141
|
+
.toArray();
|
|
142
|
+
}
|
|
143
|
+
async findByUserId(userId, limit = 100) {
|
|
144
|
+
return await this.collection
|
|
145
|
+
.find({ userId })
|
|
146
|
+
.sort({ createdAt: -1 })
|
|
147
|
+
.limit(limit)
|
|
148
|
+
.toArray();
|
|
149
|
+
}
|
|
150
|
+
async delete(id) {
|
|
151
|
+
const result = await this.collection.deleteOne({ id });
|
|
152
|
+
return result.deletedCount > 0;
|
|
153
|
+
}
|
|
154
|
+
async deleteBySessionId(sessionId) {
|
|
155
|
+
const result = await this.collection.deleteMany({ sessionId });
|
|
156
|
+
return result.deletedCount;
|
|
157
|
+
}
|
|
158
|
+
async deleteByUserId(userId) {
|
|
159
|
+
const result = await this.collection.deleteMany({ userId });
|
|
160
|
+
return result.deletedCount;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=MongoAdapter.js.map
|
|
@@ -0,0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EAAE,CAAC;aACvB,KAAK,CAAC,KAAK,CAAC;aACZ,OAAO,EAAE,CAAC;IACf,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,EAAU;QACrB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QACvD,OAAO,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,SAAiB;QACvC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC,YAAY,CAAC;IAC7B,CAAC;IAED,KAAK,CAAC,cAAc,CAAC,MAAc;QACjC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QAC5D,OAAO,MAAM,CAAC,YAAY,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostgreSQL adapter for persistence
|
|
3
|
+
* Raw SQL adapter for PostgreSQL with custom schemas
|
|
4
|
+
*/
|
|
5
|
+
import type { SessionRepository, MessageRepository, PersistenceAdapter } from "../types/persistence";
|
|
6
|
+
/**
|
|
7
|
+
* PostgreSQL query result interface
|
|
8
|
+
*/
|
|
9
|
+
export interface PgQueryResult<T = Record<string, unknown>> {
|
|
10
|
+
rows: T[];
|
|
11
|
+
rowCount: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* PostgreSQL client interface - matches pg (node-postgres)
|
|
15
|
+
*/
|
|
16
|
+
export interface PgClient {
|
|
17
|
+
query<T = Record<string, unknown>>(sql: string, values?: unknown[]): Promise<PgQueryResult<T>>;
|
|
18
|
+
end(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Options for PostgreSQL adapter
|
|
22
|
+
*/
|
|
23
|
+
export interface PostgreSQLAdapterOptions {
|
|
24
|
+
/**
|
|
25
|
+
* PostgreSQL client instance (from 'pg' package)
|
|
26
|
+
*/
|
|
27
|
+
client: PgClient;
|
|
28
|
+
/**
|
|
29
|
+
* Table names (default: "agent_sessions" and "agent_messages")
|
|
30
|
+
*/
|
|
31
|
+
tables?: {
|
|
32
|
+
sessions?: string;
|
|
33
|
+
messages?: string;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* PostgreSQL Adapter - Provider-style API for PostgreSQL persistence
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { Client } from 'pg';
|
|
42
|
+
* import { Agent, PostgreSQLAdapter } from '@falai/agent';
|
|
43
|
+
*
|
|
44
|
+
* const client = new Client({
|
|
45
|
+
* host: 'localhost',
|
|
46
|
+
* port: 5432,
|
|
47
|
+
* database: 'myapp',
|
|
48
|
+
* user: 'postgres',
|
|
49
|
+
* password: 'password',
|
|
50
|
+
* });
|
|
51
|
+
* await client.connect();
|
|
52
|
+
*
|
|
53
|
+
* const agent = new Agent({
|
|
54
|
+
* name: "My Agent",
|
|
55
|
+
* ai: provider,
|
|
56
|
+
* persistence: {
|
|
57
|
+
* adapter: new PostgreSQLAdapter({ client }),
|
|
58
|
+
* userId: "user_123",
|
|
59
|
+
* },
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare class PostgreSQLAdapter implements PersistenceAdapter {
|
|
64
|
+
readonly sessionRepository: SessionRepository;
|
|
65
|
+
readonly messageRepository: MessageRepository;
|
|
66
|
+
private client;
|
|
67
|
+
constructor(options: PostgreSQLAdapterOptions);
|
|
68
|
+
initialize(): Promise<void>;
|
|
69
|
+
disconnect(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=PostgreSQLAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
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apter for persistence
|
|
3
|
+
* Raw SQL adapter for PostgreSQL with custom schemas
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* PostgreSQL Adapter - Provider-style API for PostgreSQL persistence
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { Client } from 'pg';
|
|
11
|
+
* import { Agent, PostgreSQLAdapter } from '@falai/agent';
|
|
12
|
+
*
|
|
13
|
+
* const client = new Client({
|
|
14
|
+
* host: 'localhost',
|
|
15
|
+
* port: 5432,
|
|
16
|
+
* database: 'myapp',
|
|
17
|
+
* user: 'postgres',
|
|
18
|
+
* password: 'password',
|
|
19
|
+
* });
|
|
20
|
+
* await client.connect();
|
|
21
|
+
*
|
|
22
|
+
* const agent = new Agent({
|
|
23
|
+
* name: "My Agent",
|
|
24
|
+
* ai: provider,
|
|
25
|
+
* persistence: {
|
|
26
|
+
* adapter: new PostgreSQLAdapter({ client }),
|
|
27
|
+
* userId: "user_123",
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class PostgreSQLAdapter {
|
|
33
|
+
constructor(options) {
|
|
34
|
+
this.client = options.client;
|
|
35
|
+
const sessionTable = options.tables?.sessions || "agent_sessions";
|
|
36
|
+
const messageTable = options.tables?.messages || "agent_messages";
|
|
37
|
+
this.sessionRepository = new PostgreSQLSessionRepository(this.client, sessionTable);
|
|
38
|
+
this.messageRepository = new PostgreSQLMessageRepository(this.client, messageTable);
|
|
39
|
+
}
|
|
40
|
+
async initialize() {
|
|
41
|
+
// Create tables if they don't exist
|
|
42
|
+
const sessionTable = "agent_sessions";
|
|
43
|
+
const messageTable = "agent_messages";
|
|
44
|
+
await this.client.query(`
|
|
45
|
+
CREATE TABLE IF NOT EXISTS ${sessionTable} (
|
|
46
|
+
id VARCHAR(255) PRIMARY KEY,
|
|
47
|
+
user_id VARCHAR(255),
|
|
48
|
+
agent_name VARCHAR(255),
|
|
49
|
+
status VARCHAR(50) DEFAULT 'active',
|
|
50
|
+
current_route VARCHAR(255),
|
|
51
|
+
current_state VARCHAR(255),
|
|
52
|
+
collected_data JSONB,
|
|
53
|
+
message_count INTEGER DEFAULT 0,
|
|
54
|
+
last_message_at TIMESTAMP,
|
|
55
|
+
completed_at TIMESTAMP,
|
|
56
|
+
created_at TIMESTAMP DEFAULT NOW(),
|
|
57
|
+
updated_at TIMESTAMP DEFAULT NOW()
|
|
58
|
+
)
|
|
59
|
+
`);
|
|
60
|
+
await this.client.query(`
|
|
61
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON ${sessionTable}(user_id);
|
|
62
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_status ON ${sessionTable}(status);
|
|
63
|
+
`);
|
|
64
|
+
await this.client.query(`
|
|
65
|
+
CREATE TABLE IF NOT EXISTS ${messageTable} (
|
|
66
|
+
id VARCHAR(255) PRIMARY KEY,
|
|
67
|
+
session_id VARCHAR(255) NOT NULL,
|
|
68
|
+
user_id VARCHAR(255),
|
|
69
|
+
role VARCHAR(50) NOT NULL,
|
|
70
|
+
content TEXT NOT NULL,
|
|
71
|
+
route VARCHAR(255),
|
|
72
|
+
state VARCHAR(255),
|
|
73
|
+
tool_calls JSONB,
|
|
74
|
+
event JSONB,
|
|
75
|
+
created_at TIMESTAMP DEFAULT NOW(),
|
|
76
|
+
FOREIGN KEY (session_id) REFERENCES ${sessionTable}(id) ON DELETE CASCADE
|
|
77
|
+
)
|
|
78
|
+
`);
|
|
79
|
+
await this.client.query(`
|
|
80
|
+
CREATE INDEX IF NOT EXISTS idx_messages_session_id ON ${messageTable}(session_id);
|
|
81
|
+
CREATE INDEX IF NOT EXISTS idx_messages_user_id ON ${messageTable}(user_id);
|
|
82
|
+
`);
|
|
83
|
+
}
|
|
84
|
+
async disconnect() {
|
|
85
|
+
await this.client.end();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* PostgreSQL Session Repository
|
|
90
|
+
*/
|
|
91
|
+
class PostgreSQLSessionRepository {
|
|
92
|
+
constructor(client, tableName) {
|
|
93
|
+
this.client = client;
|
|
94
|
+
this.tableName = tableName;
|
|
95
|
+
}
|
|
96
|
+
async create(data) {
|
|
97
|
+
const id = `session_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
98
|
+
const now = new Date();
|
|
99
|
+
const result = await this.client.query(`INSERT INTO ${this.tableName}
|
|
100
|
+
(id, user_id, agent_name, status, collected_data, message_count, created_at, updated_at)
|
|
101
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
102
|
+
RETURNING *`, [
|
|
103
|
+
id,
|
|
104
|
+
data.userId || null,
|
|
105
|
+
data.agentName || null,
|
|
106
|
+
data.status || "active",
|
|
107
|
+
JSON.stringify(data.collectedData || {}),
|
|
108
|
+
data.messageCount || 0,
|
|
109
|
+
now,
|
|
110
|
+
now,
|
|
111
|
+
]);
|
|
112
|
+
return result.rows[0];
|
|
113
|
+
}
|
|
114
|
+
async findById(id) {
|
|
115
|
+
const result = await this.client.query(`SELECT * FROM ${this.tableName} WHERE id = $1`, [id]);
|
|
116
|
+
return result.rows[0] || null;
|
|
117
|
+
}
|
|
118
|
+
async findActiveByUserId(userId) {
|
|
119
|
+
const result = await this.client.query(`SELECT * FROM ${this.tableName}
|
|
120
|
+
WHERE user_id = $1 AND status = 'active'
|
|
121
|
+
ORDER BY created_at DESC
|
|
122
|
+
LIMIT 1`, [userId]);
|
|
123
|
+
return result.rows[0] || null;
|
|
124
|
+
}
|
|
125
|
+
async findByUserId(userId, limit = 100) {
|
|
126
|
+
const result = await this.client.query(`SELECT * FROM ${this.tableName}
|
|
127
|
+
WHERE user_id = $1
|
|
128
|
+
ORDER BY created_at DESC
|
|
129
|
+
LIMIT $2`, [userId, limit]);
|
|
130
|
+
return result.rows;
|
|
131
|
+
}
|
|
132
|
+
async update(id, data) {
|
|
133
|
+
const fields = [];
|
|
134
|
+
const values = [];
|
|
135
|
+
let paramIndex = 1;
|
|
136
|
+
if (data.status !== undefined) {
|
|
137
|
+
fields.push(`status = $${paramIndex++}`);
|
|
138
|
+
values.push(data.status);
|
|
139
|
+
}
|
|
140
|
+
if (data.collectedData !== undefined) {
|
|
141
|
+
fields.push(`collected_data = $${paramIndex++}`);
|
|
142
|
+
values.push(JSON.stringify(data.collectedData));
|
|
143
|
+
}
|
|
144
|
+
if (data.currentRoute !== undefined) {
|
|
145
|
+
fields.push(`current_route = $${paramIndex++}`);
|
|
146
|
+
values.push(data.currentRoute);
|
|
147
|
+
}
|
|
148
|
+
if (data.currentState !== undefined) {
|
|
149
|
+
fields.push(`current_state = $${paramIndex++}`);
|
|
150
|
+
values.push(data.currentState);
|
|
151
|
+
}
|
|
152
|
+
if (data.messageCount !== undefined) {
|
|
153
|
+
fields.push(`message_count = $${paramIndex++}`);
|
|
154
|
+
values.push(data.messageCount);
|
|
155
|
+
}
|
|
156
|
+
if (data.lastMessageAt !== undefined) {
|
|
157
|
+
fields.push(`last_message_at = $${paramIndex++}`);
|
|
158
|
+
values.push(data.lastMessageAt);
|
|
159
|
+
}
|
|
160
|
+
if (data.completedAt !== undefined) {
|
|
161
|
+
fields.push(`completed_at = $${paramIndex++}`);
|
|
162
|
+
values.push(data.completedAt);
|
|
163
|
+
}
|
|
164
|
+
fields.push(`updated_at = $${paramIndex++}`);
|
|
165
|
+
values.push(new Date());
|
|
166
|
+
values.push(id);
|
|
167
|
+
const result = await this.client.query(`UPDATE ${this.tableName}
|
|
168
|
+
SET ${fields.join(", ")}
|
|
169
|
+
WHERE id = $${paramIndex}
|
|
170
|
+
RETURNING *`, values);
|
|
171
|
+
return result.rows[0] || null;
|
|
172
|
+
}
|
|
173
|
+
async updateStatus(id, status, completedAt) {
|
|
174
|
+
return await this.update(id, { status, completedAt });
|
|
175
|
+
}
|
|
176
|
+
async updateCollectedData(id, collectedData) {
|
|
177
|
+
return await this.update(id, { collectedData });
|
|
178
|
+
}
|
|
179
|
+
async updateRouteState(id, route, state) {
|
|
180
|
+
return await this.update(id, {
|
|
181
|
+
currentRoute: route,
|
|
182
|
+
currentState: state,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
async incrementMessageCount(id) {
|
|
186
|
+
const result = await this.client.query(`UPDATE ${this.tableName}
|
|
187
|
+
SET message_count = message_count + 1,
|
|
188
|
+
last_message_at = NOW(),
|
|
189
|
+
updated_at = NOW()
|
|
190
|
+
WHERE id = $1
|
|
191
|
+
RETURNING *`, [id]);
|
|
192
|
+
return result.rows[0] || null;
|
|
193
|
+
}
|
|
194
|
+
async delete(id) {
|
|
195
|
+
const result = await this.client.query(`DELETE FROM ${this.tableName} WHERE id = $1`, [id]);
|
|
196
|
+
return result.rowCount > 0;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* PostgreSQL Message Repository
|
|
201
|
+
*/
|
|
202
|
+
class PostgreSQLMessageRepository {
|
|
203
|
+
constructor(client, tableName) {
|
|
204
|
+
this.client = client;
|
|
205
|
+
this.tableName = tableName;
|
|
206
|
+
}
|
|
207
|
+
async create(data) {
|
|
208
|
+
const id = `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
209
|
+
const result = await this.client.query(`INSERT INTO ${this.tableName}
|
|
210
|
+
(id, session_id, user_id, role, content, route, state, tool_calls, event, created_at)
|
|
211
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW())
|
|
212
|
+
RETURNING *`, [
|
|
213
|
+
id,
|
|
214
|
+
data.sessionId,
|
|
215
|
+
data.userId || null,
|
|
216
|
+
data.role,
|
|
217
|
+
data.content,
|
|
218
|
+
data.route || null,
|
|
219
|
+
data.state || null,
|
|
220
|
+
JSON.stringify(data.toolCalls || null),
|
|
221
|
+
JSON.stringify(data.event || null),
|
|
222
|
+
]);
|
|
223
|
+
return result.rows[0];
|
|
224
|
+
}
|
|
225
|
+
async findById(id) {
|
|
226
|
+
const result = await this.client.query(`SELECT * FROM ${this.tableName} WHERE id = $1`, [id]);
|
|
227
|
+
return result.rows[0] || null;
|
|
228
|
+
}
|
|
229
|
+
async findBySessionId(sessionId, limit = 1000) {
|
|
230
|
+
const result = await this.client.query(`SELECT * FROM ${this.tableName}
|
|
231
|
+
WHERE session_id = $1
|
|
232
|
+
ORDER BY created_at ASC
|
|
233
|
+
LIMIT $2`, [sessionId, limit]);
|
|
234
|
+
return result.rows;
|
|
235
|
+
}
|
|
236
|
+
async findByUserId(userId, limit = 100) {
|
|
237
|
+
const result = await this.client.query(`SELECT * FROM ${this.tableName}
|
|
238
|
+
WHERE user_id = $1
|
|
239
|
+
ORDER BY created_at DESC
|
|
240
|
+
LIMIT $2`, [userId, limit]);
|
|
241
|
+
return result.rows;
|
|
242
|
+
}
|
|
243
|
+
async delete(id) {
|
|
244
|
+
const result = await this.client.query(`DELETE FROM ${this.tableName} WHERE id = $1`, [id]);
|
|
245
|
+
return result.rowCount > 0;
|
|
246
|
+
}
|
|
247
|
+
async deleteBySessionId(sessionId) {
|
|
248
|
+
const result = await this.client.query(`DELETE FROM ${this.tableName} WHERE session_id = $1`, [sessionId]);
|
|
249
|
+
return result.rowCount;
|
|
250
|
+
}
|
|
251
|
+
async deleteByUserId(userId) {
|
|
252
|
+
const result = await this.client.query(`DELETE FROM ${this.tableName} WHERE user_id = $1`, [userId]);
|
|
253
|
+
return result.rowCount;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
//# sourceMappingURL=PostgreSQLAdapter.js.map
|