@ainetwork/adk-provider-memory-inmemory 0.1.1 → 0.1.2
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/implements/intent.memory.ts +34 -0
- package/implements/session.memory.ts +79 -0
- package/index.ts +2 -32
- package/package.json +3 -3
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { Intent } from "@ainetwork/adk/types/memory";
|
|
3
|
+
import { IIntentMemory } from "@ainetwork/adk/modules";
|
|
4
|
+
|
|
5
|
+
export class InMemoryIntent implements IIntentMemory {
|
|
6
|
+
public intents: Map<string, Intent> = new Map();
|
|
7
|
+
|
|
8
|
+
public async connect(): Promise<void> {}
|
|
9
|
+
public async disconnect(): Promise<void> {}
|
|
10
|
+
public isConnected(): boolean {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public async getIntent(intentId: string): Promise<Intent | undefined> {
|
|
15
|
+
return this.intents.get(intentId);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
public async saveIntent(intent: Intent): Promise<void> {
|
|
19
|
+
const newId = randomUUID();
|
|
20
|
+
this.intents.set(newId, intent);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
public async updateIntent(intentId: string, intent: Intent): Promise<void> {
|
|
24
|
+
this.intents.set(intentId, intent);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
public async deleteIntent(intentId: string): Promise<void> {
|
|
28
|
+
this.intents.delete(intentId);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
public async listIntents(): Promise<Intent[]> {
|
|
32
|
+
return Array.from(this.intents.values());
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { ChatObject, SessionObject, SessionMetadata } from "@ainetwork/adk/types/memory";
|
|
3
|
+
import { ISessionMemory } from "@ainetwork/adk/modules";
|
|
4
|
+
|
|
5
|
+
type InMemorySessionObject = {
|
|
6
|
+
chats: Map<string, ChatObject>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type InMemorySessionMetadata = {
|
|
10
|
+
sessionId: string;
|
|
11
|
+
title?: string;
|
|
12
|
+
updatedAt: number;
|
|
13
|
+
createdAt: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class InMemorySession implements ISessionMemory {
|
|
17
|
+
public sessions: Map<string, InMemorySessionObject> = new Map();
|
|
18
|
+
public userSessionIndex: Map<string, Set<InMemorySessionMetadata>> = new Map();
|
|
19
|
+
|
|
20
|
+
public async connect(): Promise<void> {}
|
|
21
|
+
public async disconnect(): Promise<void> {}
|
|
22
|
+
public isConnected(): boolean {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private generateKey(userId: string, sessionId: string) {
|
|
27
|
+
return `${userId}:${sessionId}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public async getSession(userId: string, sessionId: string): Promise<SessionObject | undefined> {
|
|
31
|
+
const key = this.generateKey(userId, sessionId);
|
|
32
|
+
const res = this.sessions.get(key);
|
|
33
|
+
if (res) {
|
|
34
|
+
const sessionObject: SessionObject = {
|
|
35
|
+
chats: Object.fromEntries(res.chats)
|
|
36
|
+
};
|
|
37
|
+
return sessionObject;
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
public async createSession(userId: string, sessionId: string): Promise<void> {
|
|
43
|
+
const key = this.generateKey(userId, sessionId);
|
|
44
|
+
if (!this.userSessionIndex.has(userId)) {
|
|
45
|
+
this.userSessionIndex.set(userId, new Set());
|
|
46
|
+
}
|
|
47
|
+
if (!this.sessions.has(key)) {
|
|
48
|
+
this.sessions.set(key, { chats: new Map() });
|
|
49
|
+
const metadata: InMemorySessionMetadata = {
|
|
50
|
+
sessionId, createdAt: Date.now(), updatedAt: Date.now(),
|
|
51
|
+
}
|
|
52
|
+
this.userSessionIndex.get(userId)?.add(metadata);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
public async addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void> {
|
|
57
|
+
const key = this.generateKey(userId, sessionId);
|
|
58
|
+
const newChatId = randomUUID();
|
|
59
|
+
if (!this.sessions.has(key)) {
|
|
60
|
+
await this.createSession(userId, sessionId);
|
|
61
|
+
}
|
|
62
|
+
const sessions = this.sessions.get(key);
|
|
63
|
+
this.sessions.get(key)?.chats.set(newChatId, chat);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
public async deleteSession(userId: string, sessionId: string): Promise<void> {
|
|
67
|
+
const key = this.generateKey(userId, sessionId);
|
|
68
|
+
this.sessions.delete(key);
|
|
69
|
+
this.userSessionIndex.delete(sessionId);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
public async listSessions(userId: string): Promise<SessionMetadata[]> {
|
|
73
|
+
const sessions = this.userSessionIndex.get(userId);
|
|
74
|
+
if (sessions) {
|
|
75
|
+
return Array.from(sessions);
|
|
76
|
+
}
|
|
77
|
+
return [];
|
|
78
|
+
};
|
|
79
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,32 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { BaseMemory } from "@ainetwork/adk/modules";
|
|
4
|
-
|
|
5
|
-
export class InMemoryMemory extends BaseMemory {
|
|
6
|
-
public sessionHistory: Map<string, SessionObject>;
|
|
7
|
-
|
|
8
|
-
constructor() {
|
|
9
|
-
super();
|
|
10
|
-
this.sessionHistory = new Map<string, SessionObject>();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public async getSessionHistory(sessionId: string): Promise<SessionObject> {
|
|
14
|
-
return this.sessionHistory.get(sessionId) || { chats: {} };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public async updateSessionHistory(
|
|
18
|
-
sessionId: string,
|
|
19
|
-
chat: ChatObject,
|
|
20
|
-
): Promise<void> {
|
|
21
|
-
const newChatId = randomUUID();
|
|
22
|
-
const history = await this.getSessionHistory(sessionId);
|
|
23
|
-
history.chats[newChatId] = chat;
|
|
24
|
-
this.sessionHistory.set(sessionId, history);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public async storeQueryAndIntent(
|
|
28
|
-
query: string,
|
|
29
|
-
intent: string,
|
|
30
|
-
sessionId: string,
|
|
31
|
-
) {}
|
|
32
|
-
}
|
|
1
|
+
export { InMemorySession } from "./implements/session.memory";
|
|
2
|
+
export { InMemoryIntent } from "./implements/intent.memory";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ainetwork/adk-provider-memory-inmemory",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"author": "AI Network (https://ainetwork.ai)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"clean": "rm -rf dist"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@ainetwork/adk": "^0.1.
|
|
24
|
+
"@ainetwork/adk": "^0.1.7"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"typescript": "^5.0.0"
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "4098aacea7a18099aa020ef5d331cdce0d59c3f3"
|
|
34
34
|
}
|