@ainetwork/adk-provider-memory-inmemory 0.1.1 → 0.1.3
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/dist/index.cjs +90 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +36 -9
- package/dist/index.d.ts +36 -9
- package/dist/index.js +87 -15
- package/dist/index.js.map +1 -1
- package/implements/intent.memory.ts +34 -0
- package/implements/session.memory.ts +79 -0
- package/index.ts +2 -32
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -20,31 +20,106 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
|
|
23
|
+
InMemoryIntent: () => InMemoryIntent,
|
|
24
|
+
InMemorySession: () => InMemorySession
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// implements/session.memory.ts
|
|
26
29
|
var import_node_crypto = require("crypto");
|
|
27
|
-
var
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
var InMemorySession = class {
|
|
31
|
+
sessions = /* @__PURE__ */ new Map();
|
|
32
|
+
userSessionIndex = /* @__PURE__ */ new Map();
|
|
33
|
+
async connect() {
|
|
34
|
+
}
|
|
35
|
+
async disconnect() {
|
|
36
|
+
}
|
|
37
|
+
isConnected() {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
generateKey(userId, sessionId) {
|
|
41
|
+
return `${userId}:${sessionId}`;
|
|
33
42
|
}
|
|
34
|
-
async
|
|
35
|
-
|
|
43
|
+
async getSession(userId, sessionId) {
|
|
44
|
+
const key = this.generateKey(userId, sessionId);
|
|
45
|
+
const res = this.sessions.get(key);
|
|
46
|
+
if (res) {
|
|
47
|
+
const sessionObject = {
|
|
48
|
+
chats: Object.fromEntries(res.chats)
|
|
49
|
+
};
|
|
50
|
+
return sessionObject;
|
|
51
|
+
}
|
|
52
|
+
return void 0;
|
|
36
53
|
}
|
|
37
|
-
async
|
|
54
|
+
async createSession(userId, sessionId) {
|
|
55
|
+
const key = this.generateKey(userId, sessionId);
|
|
56
|
+
if (!this.userSessionIndex.has(userId)) {
|
|
57
|
+
this.userSessionIndex.set(userId, /* @__PURE__ */ new Set());
|
|
58
|
+
}
|
|
59
|
+
if (!this.sessions.has(key)) {
|
|
60
|
+
this.sessions.set(key, { chats: /* @__PURE__ */ new Map() });
|
|
61
|
+
const metadata = {
|
|
62
|
+
sessionId,
|
|
63
|
+
createdAt: Date.now(),
|
|
64
|
+
updatedAt: Date.now()
|
|
65
|
+
};
|
|
66
|
+
this.userSessionIndex.get(userId)?.add(metadata);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async addChatToSession(userId, sessionId, chat) {
|
|
70
|
+
const key = this.generateKey(userId, sessionId);
|
|
38
71
|
const newChatId = (0, import_node_crypto.randomUUID)();
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
72
|
+
if (!this.sessions.has(key)) {
|
|
73
|
+
await this.createSession(userId, sessionId);
|
|
74
|
+
}
|
|
75
|
+
const sessions = this.sessions.get(key);
|
|
76
|
+
this.sessions.get(key)?.chats.set(newChatId, chat);
|
|
77
|
+
}
|
|
78
|
+
async deleteSession(userId, sessionId) {
|
|
79
|
+
const key = this.generateKey(userId, sessionId);
|
|
80
|
+
this.sessions.delete(key);
|
|
81
|
+
this.userSessionIndex.delete(sessionId);
|
|
82
|
+
}
|
|
83
|
+
async listSessions(userId) {
|
|
84
|
+
const sessions = this.userSessionIndex.get(userId);
|
|
85
|
+
if (sessions) {
|
|
86
|
+
return Array.from(sessions);
|
|
87
|
+
}
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// implements/intent.memory.ts
|
|
93
|
+
var import_node_crypto2 = require("crypto");
|
|
94
|
+
var InMemoryIntent = class {
|
|
95
|
+
intents = /* @__PURE__ */ new Map();
|
|
96
|
+
async connect() {
|
|
97
|
+
}
|
|
98
|
+
async disconnect() {
|
|
99
|
+
}
|
|
100
|
+
isConnected() {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
async getIntent(intentId) {
|
|
104
|
+
return this.intents.get(intentId);
|
|
105
|
+
}
|
|
106
|
+
async saveIntent(intent) {
|
|
107
|
+
const newId = (0, import_node_crypto2.randomUUID)();
|
|
108
|
+
this.intents.set(newId, intent);
|
|
109
|
+
}
|
|
110
|
+
async updateIntent(intentId, intent) {
|
|
111
|
+
this.intents.set(intentId, intent);
|
|
112
|
+
}
|
|
113
|
+
async deleteIntent(intentId) {
|
|
114
|
+
this.intents.delete(intentId);
|
|
42
115
|
}
|
|
43
|
-
async
|
|
116
|
+
async listIntents() {
|
|
117
|
+
return Array.from(this.intents.values());
|
|
44
118
|
}
|
|
45
119
|
};
|
|
46
120
|
// Annotate the CommonJS export names for ESM import in node:
|
|
47
121
|
0 && (module.exports = {
|
|
48
|
-
|
|
122
|
+
InMemoryIntent,
|
|
123
|
+
InMemorySession
|
|
49
124
|
});
|
|
50
125
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { ChatObject, SessionObject } from \"@ainetwork/adk/types/memory\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../index.ts","../implements/session.memory.ts","../implements/intent.memory.ts"],"sourcesContent":["export { InMemorySession } from \"./implements/session.memory\";\nexport { InMemoryIntent } from \"./implements/intent.memory\";","import { randomUUID } from \"node:crypto\";\nimport type { ChatObject, SessionObject, SessionMetadata } from \"@ainetwork/adk/types/memory\";\nimport { ISessionMemory } from \"@ainetwork/adk/modules\";\n\ntype InMemorySessionObject = {\n chats: Map<string, ChatObject>\n}\n\ntype InMemorySessionMetadata = {\n sessionId: string;\n title?: string;\n updatedAt: number;\n createdAt: number;\n}\n\nexport class InMemorySession implements ISessionMemory {\n\tpublic sessions: Map<string, InMemorySessionObject> = new Map();\n public userSessionIndex: Map<string, Set<InMemorySessionMetadata>> = new Map();\n\n public async connect(): Promise<void> {}\n public async disconnect(): Promise<void> {}\n public isConnected(): boolean {\n return true;\n }\n\n private generateKey(userId: string, sessionId: string) {\n return `${userId}:${sessionId}`;\n }\n\n public async getSession(userId: string, sessionId: string): Promise<SessionObject | undefined> {\n const key = this.generateKey(userId, sessionId);\n const res = this.sessions.get(key);\n if (res) {\n const sessionObject: SessionObject = {\n chats: Object.fromEntries(res.chats)\n };\n return sessionObject;\n }\n return undefined;\n };\n\n\tpublic async createSession(userId: string, sessionId: string): Promise<void> {\n const key = this.generateKey(userId, sessionId);\n if (!this.userSessionIndex.has(userId)) {\n this.userSessionIndex.set(userId, new Set());\n }\n if (!this.sessions.has(key)) {\n this.sessions.set(key, { chats: new Map() });\n const metadata: InMemorySessionMetadata = {\n sessionId, createdAt: Date.now(), updatedAt: Date.now(),\n }\n this.userSessionIndex.get(userId)?.add(metadata);\n }\n };\n\n\tpublic async addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void> {\n const key = this.generateKey(userId, sessionId);\n const newChatId = randomUUID();\n if (!this.sessions.has(key)) {\n await this.createSession(userId, sessionId);\n }\n const sessions = this.sessions.get(key);\n this.sessions.get(key)?.chats.set(newChatId, chat);\n };\n\n\tpublic async deleteSession(userId: string, sessionId: string): Promise<void> {\n const key = this.generateKey(userId, sessionId);\n this.sessions.delete(key);\n this.userSessionIndex.delete(sessionId);\n };\n\n\tpublic async listSessions(userId: string): Promise<SessionMetadata[]> {\n const sessions = this.userSessionIndex.get(userId);\n if (sessions) {\n return Array.from(sessions);\n }\n return [];\n };\n}","import { randomUUID } from \"node:crypto\";\nimport type { Intent } from \"@ainetwork/adk/types/memory\";\nimport { IIntentMemory } from \"@ainetwork/adk/modules\";\n\nexport class InMemoryIntent implements IIntentMemory {\n\tpublic intents: Map<string, Intent> = new Map();\n\n public async connect(): Promise<void> {}\n public async disconnect(): Promise<void> {}\n public isConnected(): boolean {\n return true;\n }\n\n public async getIntent(intentId: string): Promise<Intent | undefined> {\n return this.intents.get(intentId);\n };\n\n\tpublic async saveIntent(intent: Intent): Promise<void> {\n const newId = randomUUID();\n this.intents.set(newId, intent);\n };\n\n\tpublic async updateIntent(intentId: string, intent: Intent): Promise<void> {\n this.intents.set(intentId, intent);\n };\n\n\tpublic async deleteIntent(intentId: string): Promise<void> {\n this.intents.delete(intentId);\n };\n\n\tpublic async listIntents(): Promise<Intent[]> {\n return Array.from(this.intents.values());\n };\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,yBAA2B;AAepB,IAAM,kBAAN,MAAgD;AAAA,EAC/C,WAA+C,oBAAI,IAAI;AAAA,EACtD,mBAA8D,oBAAI,IAAI;AAAA,EAE7E,MAAa,UAAyB;AAAA,EAAC;AAAA,EACvC,MAAa,aAA4B;AAAA,EAAC;AAAA,EACnC,cAAuB;AAC5B,WAAO;AAAA,EACT;AAAA,EAEQ,YAAY,QAAgB,WAAmB;AACrD,WAAO,GAAG,MAAM,IAAI,SAAS;AAAA,EAC/B;AAAA,EAEA,MAAa,WAAW,QAAgB,WAAuD;AAC7F,UAAM,MAAM,KAAK,YAAY,QAAQ,SAAS;AAC9C,UAAM,MAAM,KAAK,SAAS,IAAI,GAAG;AACjC,QAAI,KAAK;AACP,YAAM,gBAA+B;AAAA,QACnC,OAAO,OAAO,YAAY,IAAI,KAAK;AAAA,MACrC;AACA,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT;AAAA,EAED,MAAa,cAAc,QAAgB,WAAkC;AAC1E,UAAM,MAAM,KAAK,YAAY,QAAQ,SAAS;AAC9C,QAAI,CAAC,KAAK,iBAAiB,IAAI,MAAM,GAAG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,EAEA,MAAa,UAAU,UAA+C;AACpE,WAAO,KAAK,QAAQ,IAAI,QAAQ;AAAA,EAClC;AAAA,EAED,MAAa,WAAW,QAA+B;AACpD,UAAM,YAAQ,gCAAW;AACzB,SAAK,QAAQ,IAAI,OAAO,MAAM;AAAA,EAChC;AAAA,EAED,MAAa,aAAa,UAAkB,QAA+B;AACxE,SAAK,QAAQ,IAAI,UAAU,MAAM;AAAA,EACnC;AAAA,EAED,MAAa,aAAa,UAAiC;AACxD,SAAK,QAAQ,OAAO,QAAQ;AAAA,EAC9B;AAAA,EAED,MAAa,cAAiC;AAC3C,WAAO,MAAM,KAAK,KAAK,QAAQ,OAAO,CAAC;AAAA,EACzC;AACF;","names":["import_node_crypto"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,39 @@
|
|
|
1
|
-
import { SessionObject,
|
|
2
|
-
import {
|
|
1
|
+
import { ChatObject, SessionObject, SessionMetadata, Intent } from '@ainetwork/adk/types/memory';
|
|
2
|
+
import { ISessionMemory, IIntentMemory } from '@ainetwork/adk/modules';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
type InMemorySessionObject = {
|
|
5
|
+
chats: Map<string, ChatObject>;
|
|
6
|
+
};
|
|
7
|
+
type InMemorySessionMetadata = {
|
|
8
|
+
sessionId: string;
|
|
9
|
+
title?: string;
|
|
10
|
+
updatedAt: number;
|
|
11
|
+
createdAt: number;
|
|
12
|
+
};
|
|
13
|
+
declare class InMemorySession implements ISessionMemory {
|
|
14
|
+
sessions: Map<string, InMemorySessionObject>;
|
|
15
|
+
userSessionIndex: Map<string, Set<InMemorySessionMetadata>>;
|
|
16
|
+
connect(): Promise<void>;
|
|
17
|
+
disconnect(): Promise<void>;
|
|
18
|
+
isConnected(): boolean;
|
|
19
|
+
private generateKey;
|
|
20
|
+
getSession(userId: string, sessionId: string): Promise<SessionObject | undefined>;
|
|
21
|
+
createSession(userId: string, sessionId: string): Promise<void>;
|
|
22
|
+
addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void>;
|
|
23
|
+
deleteSession(userId: string, sessionId: string): Promise<void>;
|
|
24
|
+
listSessions(userId: string): Promise<SessionMetadata[]>;
|
|
10
25
|
}
|
|
11
26
|
|
|
12
|
-
|
|
27
|
+
declare class InMemoryIntent implements IIntentMemory {
|
|
28
|
+
intents: Map<string, Intent>;
|
|
29
|
+
connect(): Promise<void>;
|
|
30
|
+
disconnect(): Promise<void>;
|
|
31
|
+
isConnected(): boolean;
|
|
32
|
+
getIntent(intentId: string): Promise<Intent | undefined>;
|
|
33
|
+
saveIntent(intent: Intent): Promise<void>;
|
|
34
|
+
updateIntent(intentId: string, intent: Intent): Promise<void>;
|
|
35
|
+
deleteIntent(intentId: string): Promise<void>;
|
|
36
|
+
listIntents(): Promise<Intent[]>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { InMemoryIntent, InMemorySession };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,39 @@
|
|
|
1
|
-
import { SessionObject,
|
|
2
|
-
import {
|
|
1
|
+
import { ChatObject, SessionObject, SessionMetadata, Intent } from '@ainetwork/adk/types/memory';
|
|
2
|
+
import { ISessionMemory, IIntentMemory } from '@ainetwork/adk/modules';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
type InMemorySessionObject = {
|
|
5
|
+
chats: Map<string, ChatObject>;
|
|
6
|
+
};
|
|
7
|
+
type InMemorySessionMetadata = {
|
|
8
|
+
sessionId: string;
|
|
9
|
+
title?: string;
|
|
10
|
+
updatedAt: number;
|
|
11
|
+
createdAt: number;
|
|
12
|
+
};
|
|
13
|
+
declare class InMemorySession implements ISessionMemory {
|
|
14
|
+
sessions: Map<string, InMemorySessionObject>;
|
|
15
|
+
userSessionIndex: Map<string, Set<InMemorySessionMetadata>>;
|
|
16
|
+
connect(): Promise<void>;
|
|
17
|
+
disconnect(): Promise<void>;
|
|
18
|
+
isConnected(): boolean;
|
|
19
|
+
private generateKey;
|
|
20
|
+
getSession(userId: string, sessionId: string): Promise<SessionObject | undefined>;
|
|
21
|
+
createSession(userId: string, sessionId: string): Promise<void>;
|
|
22
|
+
addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void>;
|
|
23
|
+
deleteSession(userId: string, sessionId: string): Promise<void>;
|
|
24
|
+
listSessions(userId: string): Promise<SessionMetadata[]>;
|
|
10
25
|
}
|
|
11
26
|
|
|
12
|
-
|
|
27
|
+
declare class InMemoryIntent implements IIntentMemory {
|
|
28
|
+
intents: Map<string, Intent>;
|
|
29
|
+
connect(): Promise<void>;
|
|
30
|
+
disconnect(): Promise<void>;
|
|
31
|
+
isConnected(): boolean;
|
|
32
|
+
getIntent(intentId: string): Promise<Intent | undefined>;
|
|
33
|
+
saveIntent(intent: Intent): Promise<void>;
|
|
34
|
+
updateIntent(intentId: string, intent: Intent): Promise<void>;
|
|
35
|
+
deleteIntent(intentId: string): Promise<void>;
|
|
36
|
+
listIntents(): Promise<Intent[]>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { InMemoryIntent, InMemorySession };
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,97 @@
|
|
|
1
|
-
//
|
|
1
|
+
// implements/session.memory.ts
|
|
2
2
|
import { randomUUID } from "crypto";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
super();
|
|
8
|
-
this.sessionHistory = /* @__PURE__ */ new Map();
|
|
3
|
+
var InMemorySession = class {
|
|
4
|
+
sessions = /* @__PURE__ */ new Map();
|
|
5
|
+
userSessionIndex = /* @__PURE__ */ new Map();
|
|
6
|
+
async connect() {
|
|
9
7
|
}
|
|
10
|
-
async
|
|
11
|
-
return this.sessionHistory.get(sessionId) || { chats: {} };
|
|
8
|
+
async disconnect() {
|
|
12
9
|
}
|
|
13
|
-
|
|
10
|
+
isConnected() {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
generateKey(userId, sessionId) {
|
|
14
|
+
return `${userId}:${sessionId}`;
|
|
15
|
+
}
|
|
16
|
+
async getSession(userId, sessionId) {
|
|
17
|
+
const key = this.generateKey(userId, sessionId);
|
|
18
|
+
const res = this.sessions.get(key);
|
|
19
|
+
if (res) {
|
|
20
|
+
const sessionObject = {
|
|
21
|
+
chats: Object.fromEntries(res.chats)
|
|
22
|
+
};
|
|
23
|
+
return sessionObject;
|
|
24
|
+
}
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
async createSession(userId, sessionId) {
|
|
28
|
+
const key = this.generateKey(userId, sessionId);
|
|
29
|
+
if (!this.userSessionIndex.has(userId)) {
|
|
30
|
+
this.userSessionIndex.set(userId, /* @__PURE__ */ new Set());
|
|
31
|
+
}
|
|
32
|
+
if (!this.sessions.has(key)) {
|
|
33
|
+
this.sessions.set(key, { chats: /* @__PURE__ */ new Map() });
|
|
34
|
+
const metadata = {
|
|
35
|
+
sessionId,
|
|
36
|
+
createdAt: Date.now(),
|
|
37
|
+
updatedAt: Date.now()
|
|
38
|
+
};
|
|
39
|
+
this.userSessionIndex.get(userId)?.add(metadata);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async addChatToSession(userId, sessionId, chat) {
|
|
43
|
+
const key = this.generateKey(userId, sessionId);
|
|
14
44
|
const newChatId = randomUUID();
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
45
|
+
if (!this.sessions.has(key)) {
|
|
46
|
+
await this.createSession(userId, sessionId);
|
|
47
|
+
}
|
|
48
|
+
const sessions = this.sessions.get(key);
|
|
49
|
+
this.sessions.get(key)?.chats.set(newChatId, chat);
|
|
50
|
+
}
|
|
51
|
+
async deleteSession(userId, sessionId) {
|
|
52
|
+
const key = this.generateKey(userId, sessionId);
|
|
53
|
+
this.sessions.delete(key);
|
|
54
|
+
this.userSessionIndex.delete(sessionId);
|
|
55
|
+
}
|
|
56
|
+
async listSessions(userId) {
|
|
57
|
+
const sessions = this.userSessionIndex.get(userId);
|
|
58
|
+
if (sessions) {
|
|
59
|
+
return Array.from(sessions);
|
|
60
|
+
}
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// implements/intent.memory.ts
|
|
66
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
67
|
+
var InMemoryIntent = class {
|
|
68
|
+
intents = /* @__PURE__ */ new Map();
|
|
69
|
+
async connect() {
|
|
70
|
+
}
|
|
71
|
+
async disconnect() {
|
|
72
|
+
}
|
|
73
|
+
isConnected() {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
async getIntent(intentId) {
|
|
77
|
+
return this.intents.get(intentId);
|
|
78
|
+
}
|
|
79
|
+
async saveIntent(intent) {
|
|
80
|
+
const newId = randomUUID2();
|
|
81
|
+
this.intents.set(newId, intent);
|
|
82
|
+
}
|
|
83
|
+
async updateIntent(intentId, intent) {
|
|
84
|
+
this.intents.set(intentId, intent);
|
|
85
|
+
}
|
|
86
|
+
async deleteIntent(intentId) {
|
|
87
|
+
this.intents.delete(intentId);
|
|
18
88
|
}
|
|
19
|
-
async
|
|
89
|
+
async listIntents() {
|
|
90
|
+
return Array.from(this.intents.values());
|
|
20
91
|
}
|
|
21
92
|
};
|
|
22
93
|
export {
|
|
23
|
-
|
|
94
|
+
InMemoryIntent,
|
|
95
|
+
InMemorySession
|
|
24
96
|
};
|
|
25
97
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../
|
|
1
|
+
{"version":3,"sources":["../implements/session.memory.ts","../implements/intent.memory.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { ChatObject, SessionObject, SessionMetadata } from \"@ainetwork/adk/types/memory\";\nimport { ISessionMemory } from \"@ainetwork/adk/modules\";\n\ntype InMemorySessionObject = {\n chats: Map<string, ChatObject>\n}\n\ntype InMemorySessionMetadata = {\n sessionId: string;\n title?: string;\n updatedAt: number;\n createdAt: number;\n}\n\nexport class InMemorySession implements ISessionMemory {\n\tpublic sessions: Map<string, InMemorySessionObject> = new Map();\n public userSessionIndex: Map<string, Set<InMemorySessionMetadata>> = new Map();\n\n public async connect(): Promise<void> {}\n public async disconnect(): Promise<void> {}\n public isConnected(): boolean {\n return true;\n }\n\n private generateKey(userId: string, sessionId: string) {\n return `${userId}:${sessionId}`;\n }\n\n public async getSession(userId: string, sessionId: string): Promise<SessionObject | undefined> {\n const key = this.generateKey(userId, sessionId);\n const res = this.sessions.get(key);\n if (res) {\n const sessionObject: SessionObject = {\n chats: Object.fromEntries(res.chats)\n };\n return sessionObject;\n }\n return undefined;\n };\n\n\tpublic async createSession(userId: string, sessionId: string): Promise<void> {\n const key = this.generateKey(userId, sessionId);\n if (!this.userSessionIndex.has(userId)) {\n this.userSessionIndex.set(userId, new Set());\n }\n if (!this.sessions.has(key)) {\n this.sessions.set(key, { chats: new Map() });\n const metadata: InMemorySessionMetadata = {\n sessionId, createdAt: Date.now(), updatedAt: Date.now(),\n }\n this.userSessionIndex.get(userId)?.add(metadata);\n }\n };\n\n\tpublic async addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void> {\n const key = this.generateKey(userId, sessionId);\n const newChatId = randomUUID();\n if (!this.sessions.has(key)) {\n await this.createSession(userId, sessionId);\n }\n const sessions = this.sessions.get(key);\n this.sessions.get(key)?.chats.set(newChatId, chat);\n };\n\n\tpublic async deleteSession(userId: string, sessionId: string): Promise<void> {\n const key = this.generateKey(userId, sessionId);\n this.sessions.delete(key);\n this.userSessionIndex.delete(sessionId);\n };\n\n\tpublic async listSessions(userId: string): Promise<SessionMetadata[]> {\n const sessions = this.userSessionIndex.get(userId);\n if (sessions) {\n return Array.from(sessions);\n }\n return [];\n };\n}","import { randomUUID } from \"node:crypto\";\nimport type { Intent } from \"@ainetwork/adk/types/memory\";\nimport { IIntentMemory } from \"@ainetwork/adk/modules\";\n\nexport class InMemoryIntent implements IIntentMemory {\n\tpublic intents: Map<string, Intent> = new Map();\n\n public async connect(): Promise<void> {}\n public async disconnect(): Promise<void> {}\n public isConnected(): boolean {\n return true;\n }\n\n public async getIntent(intentId: string): Promise<Intent | undefined> {\n return this.intents.get(intentId);\n };\n\n\tpublic async saveIntent(intent: Intent): Promise<void> {\n const newId = randomUUID();\n this.intents.set(newId, intent);\n };\n\n\tpublic async updateIntent(intentId: string, intent: Intent): Promise<void> {\n this.intents.set(intentId, intent);\n };\n\n\tpublic async deleteIntent(intentId: string): Promise<void> {\n this.intents.delete(intentId);\n };\n\n\tpublic async listIntents(): Promise<Intent[]> {\n return Array.from(this.intents.values());\n };\n}"],"mappings":";AAAA,SAAS,kBAAkB;AAepB,IAAM,kBAAN,MAAgD;AAAA,EAC/C,WAA+C,oBAAI,IAAI;AAAA,EACtD,mBAA8D,oBAAI,IAAI;AAAA,EAE7E,MAAa,UAAyB;AAAA,EAAC;AAAA,EACvC,MAAa,aAA4B;AAAA,EAAC;AAAA,EACnC,cAAuB;AAC5B,WAAO;AAAA,EACT;AAAA,EAEQ,YAAY,QAAgB,WAAmB;AACrD,WAAO,GAAG,MAAM,IAAI,SAAS;AAAA,EAC/B;AAAA,EAEA,MAAa,WAAW,QAAgB,WAAuD;AAC7F,UAAM,MAAM,KAAK,YAAY,QAAQ,SAAS;AAC9C,UAAM,MAAM,KAAK,SAAS,IAAI,GAAG;AACjC,QAAI,KAAK;AACP,YAAM,gBAA+B;AAAA,QACnC,OAAO,OAAO,YAAY,IAAI,KAAK;AAAA,MACrC;AACA,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT;AAAA,EAED,MAAa,cAAc,QAAgB,WAAkC;AAC1E,UAAM,MAAM,KAAK,YAAY,QAAQ,SAAS;AAC9C,QAAI,CAAC,KAAK,iBAAiB,IAAI,MAAM,GAAG;AACtC,WAAK,iBAAiB,IA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AA+C;AACpE,WAAO,KAAK,QAAQ,IAAI,QAAQ;AAAA,EAClC;AAAA,EAED,MAAa,WAAW,QAA+B;AACpD,UAAM,QAAQA,YAAW;AACzB,SAAK,QAAQ,IAAI,OAAO,MAAM;AAAA,EAChC;AAAA,EAED,MAAa,aAAa,UAAkB,QAA+B;AACxE,SAAK,QAAQ,IAAI,UAAU,MAAM;AAAA,EACnC;AAAA,EAED,MAAa,aAAa,UAAiC;AACxD,SAAK,QAAQ,OAAO,QAAQ;AAAA,EAC9B;AAAA,EAED,MAAa,cAAiC;AAC3C,WAAO,MAAM,KAAK,KAAK,QAAQ,OAAO,CAAC;AAAA,EACzC;AACF;","names":["randomUUID"]}
|
|
@@ -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.3",
|
|
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": "179c9e22bac074444e9a1ddf656eb52d6fe8d34a"
|
|
34
34
|
}
|