@ainetwork/adk-provider-memory-inmemory 0.1.2 → 0.1.4
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 +89 -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 +86 -15
- package/dist/index.js.map +1 -1
- package/implements/session.memory.ts +6 -6
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -20,31 +20,105 @@ 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, title) {
|
|
55
|
+
const now = Date.now();
|
|
56
|
+
const key = this.generateKey(userId, sessionId);
|
|
57
|
+
if (!this.userSessionIndex.has(userId)) {
|
|
58
|
+
this.userSessionIndex.set(userId, /* @__PURE__ */ new Set());
|
|
59
|
+
}
|
|
60
|
+
if (!this.sessions.has(key)) {
|
|
61
|
+
this.sessions.set(key, { chats: /* @__PURE__ */ new Map() });
|
|
62
|
+
const metadata = {
|
|
63
|
+
sessionId,
|
|
64
|
+
createdAt: now,
|
|
65
|
+
updatedAt: now
|
|
66
|
+
};
|
|
67
|
+
this.userSessionIndex.get(userId)?.add(metadata);
|
|
68
|
+
}
|
|
69
|
+
return { title, sessionId, updatedAt: now };
|
|
70
|
+
}
|
|
71
|
+
async addChatToSession(userId, sessionId, chat) {
|
|
72
|
+
const key = this.generateKey(userId, sessionId);
|
|
73
|
+
await this.createSession(userId, sessionId, "New chat");
|
|
38
74
|
const newChatId = (0, import_node_crypto.randomUUID)();
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
75
|
+
this.sessions.get(key)?.chats.set(newChatId, chat);
|
|
76
|
+
}
|
|
77
|
+
async deleteSession(userId, sessionId) {
|
|
78
|
+
const key = this.generateKey(userId, sessionId);
|
|
79
|
+
this.sessions.delete(key);
|
|
80
|
+
this.userSessionIndex.delete(sessionId);
|
|
81
|
+
}
|
|
82
|
+
async listSessions(userId) {
|
|
83
|
+
const sessions = this.userSessionIndex.get(userId);
|
|
84
|
+
if (sessions) {
|
|
85
|
+
return Array.from(sessions);
|
|
86
|
+
}
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// implements/intent.memory.ts
|
|
92
|
+
var import_node_crypto2 = require("crypto");
|
|
93
|
+
var InMemoryIntent = class {
|
|
94
|
+
intents = /* @__PURE__ */ new Map();
|
|
95
|
+
async connect() {
|
|
96
|
+
}
|
|
97
|
+
async disconnect() {
|
|
98
|
+
}
|
|
99
|
+
isConnected() {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
async getIntent(intentId) {
|
|
103
|
+
return this.intents.get(intentId);
|
|
104
|
+
}
|
|
105
|
+
async saveIntent(intent) {
|
|
106
|
+
const newId = (0, import_node_crypto2.randomUUID)();
|
|
107
|
+
this.intents.set(newId, intent);
|
|
108
|
+
}
|
|
109
|
+
async updateIntent(intentId, intent) {
|
|
110
|
+
this.intents.set(intentId, intent);
|
|
111
|
+
}
|
|
112
|
+
async deleteIntent(intentId) {
|
|
113
|
+
this.intents.delete(intentId);
|
|
42
114
|
}
|
|
43
|
-
async
|
|
115
|
+
async listIntents() {
|
|
116
|
+
return Array.from(this.intents.values());
|
|
44
117
|
}
|
|
45
118
|
};
|
|
46
119
|
// Annotate the CommonJS export names for ESM import in node:
|
|
47
120
|
0 && (module.exports = {
|
|
48
|
-
|
|
121
|
+
InMemoryIntent,
|
|
122
|
+
InMemorySession
|
|
49
123
|
});
|
|
50
124
|
//# 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, title: string): Promise<SessionMetadata> {\n const now = Date.now();\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: now, updatedAt: now,\n }\n this.userSessionIndex.get(userId)?.add(metadata);\n }\n\n return { title, sessionId, updatedAt: now };\n };\n\n\tpublic async addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void> {\n const key = this.generateKey(userId, sessionId);\n await this.createSession(userId, sessionId, \"New chat\");\n const newChatId = randomUUID();\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,WAAmB,OAAyC;AACpG,UAAM,MAAM,KAAK,IAAI;AACrB,UAAM,MAAM,KAAK,YAAY,QAAQ,SAAS;AAC9C,QAAI,CAAC,KAAK,iBAAiB,IAAI,MAAM,GAAG;AACtC,WAAK,iBAAiB,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,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, title: string): Promise<SessionMetadata>;
|
|
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, title: string): Promise<SessionMetadata>;
|
|
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,96 @@
|
|
|
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, title) {
|
|
28
|
+
const now = Date.now();
|
|
29
|
+
const key = this.generateKey(userId, sessionId);
|
|
30
|
+
if (!this.userSessionIndex.has(userId)) {
|
|
31
|
+
this.userSessionIndex.set(userId, /* @__PURE__ */ new Set());
|
|
32
|
+
}
|
|
33
|
+
if (!this.sessions.has(key)) {
|
|
34
|
+
this.sessions.set(key, { chats: /* @__PURE__ */ new Map() });
|
|
35
|
+
const metadata = {
|
|
36
|
+
sessionId,
|
|
37
|
+
createdAt: now,
|
|
38
|
+
updatedAt: now
|
|
39
|
+
};
|
|
40
|
+
this.userSessionIndex.get(userId)?.add(metadata);
|
|
41
|
+
}
|
|
42
|
+
return { title, sessionId, updatedAt: now };
|
|
43
|
+
}
|
|
44
|
+
async addChatToSession(userId, sessionId, chat) {
|
|
45
|
+
const key = this.generateKey(userId, sessionId);
|
|
46
|
+
await this.createSession(userId, sessionId, "New chat");
|
|
14
47
|
const newChatId = randomUUID();
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
48
|
+
this.sessions.get(key)?.chats.set(newChatId, chat);
|
|
49
|
+
}
|
|
50
|
+
async deleteSession(userId, sessionId) {
|
|
51
|
+
const key = this.generateKey(userId, sessionId);
|
|
52
|
+
this.sessions.delete(key);
|
|
53
|
+
this.userSessionIndex.delete(sessionId);
|
|
54
|
+
}
|
|
55
|
+
async listSessions(userId) {
|
|
56
|
+
const sessions = this.userSessionIndex.get(userId);
|
|
57
|
+
if (sessions) {
|
|
58
|
+
return Array.from(sessions);
|
|
59
|
+
}
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// implements/intent.memory.ts
|
|
65
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
66
|
+
var InMemoryIntent = class {
|
|
67
|
+
intents = /* @__PURE__ */ new Map();
|
|
68
|
+
async connect() {
|
|
69
|
+
}
|
|
70
|
+
async disconnect() {
|
|
71
|
+
}
|
|
72
|
+
isConnected() {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
async getIntent(intentId) {
|
|
76
|
+
return this.intents.get(intentId);
|
|
77
|
+
}
|
|
78
|
+
async saveIntent(intent) {
|
|
79
|
+
const newId = randomUUID2();
|
|
80
|
+
this.intents.set(newId, intent);
|
|
81
|
+
}
|
|
82
|
+
async updateIntent(intentId, intent) {
|
|
83
|
+
this.intents.set(intentId, intent);
|
|
84
|
+
}
|
|
85
|
+
async deleteIntent(intentId) {
|
|
86
|
+
this.intents.delete(intentId);
|
|
18
87
|
}
|
|
19
|
-
async
|
|
88
|
+
async listIntents() {
|
|
89
|
+
return Array.from(this.intents.values());
|
|
20
90
|
}
|
|
21
91
|
};
|
|
22
92
|
export {
|
|
23
|
-
|
|
93
|
+
InMemoryIntent,
|
|
94
|
+
InMemorySession
|
|
24
95
|
};
|
|
25
96
|
//# 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, title: string): Promise<SessionMetadata> {\n const now = Date.now();\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: now, updatedAt: now,\n }\n this.userSessionIndex.get(userId)?.add(metadata);\n }\n\n return { title, sessionId, updatedAt: now };\n };\n\n\tpublic async addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void> {\n const key = this.generateKey(userId, sessionId);\n await this.createSession(userId, sessionId, \"New chat\");\n const newChatId = randomUUID();\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,WAAmB,OAAyC;AACpG,UAAM,MAAM,KAAK,IAAI;AACrB,UAAM,MAAM,KAAK,YAAY,QAAQ,SAAS;AAC9C,QAAI,CAAC,KAAK,iBAAiB,IAAI,MAAM,GAAG;AACtC,WAAK,iBAAiB,IAAI,QAAQ,oBAAI,IAAI,CAAC;AAAA,IAC7C;AACA,QAAI,CA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AI,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"]}
|
|
@@ -39,7 +39,8 @@ export class InMemorySession implements ISessionMemory {
|
|
|
39
39
|
return undefined;
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
public async createSession(userId: string, sessionId: string): Promise<
|
|
42
|
+
public async createSession(userId: string, sessionId: string, title: string): Promise<SessionMetadata> {
|
|
43
|
+
const now = Date.now();
|
|
43
44
|
const key = this.generateKey(userId, sessionId);
|
|
44
45
|
if (!this.userSessionIndex.has(userId)) {
|
|
45
46
|
this.userSessionIndex.set(userId, new Set());
|
|
@@ -47,19 +48,18 @@ export class InMemorySession implements ISessionMemory {
|
|
|
47
48
|
if (!this.sessions.has(key)) {
|
|
48
49
|
this.sessions.set(key, { chats: new Map() });
|
|
49
50
|
const metadata: InMemorySessionMetadata = {
|
|
50
|
-
sessionId, createdAt:
|
|
51
|
+
sessionId, createdAt: now, updatedAt: now,
|
|
51
52
|
}
|
|
52
53
|
this.userSessionIndex.get(userId)?.add(metadata);
|
|
53
54
|
}
|
|
55
|
+
|
|
56
|
+
return { title, sessionId, updatedAt: now };
|
|
54
57
|
};
|
|
55
58
|
|
|
56
59
|
public async addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void> {
|
|
57
60
|
const key = this.generateKey(userId, sessionId);
|
|
61
|
+
await this.createSession(userId, sessionId, "New chat");
|
|
58
62
|
const newChatId = randomUUID();
|
|
59
|
-
if (!this.sessions.has(key)) {
|
|
60
|
-
await this.createSession(userId, sessionId);
|
|
61
|
-
}
|
|
62
|
-
const sessions = this.sessions.get(key);
|
|
63
63
|
this.sessions.get(key)?.chats.set(newChatId, chat);
|
|
64
64
|
};
|
|
65
65
|
|
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.4",
|
|
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.9"
|
|
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": "66a5fbc5f9a509bd412ff8dc91b002862133fbae"
|
|
34
34
|
}
|