@ainetwork/adk-provider-memory-inmemory 0.1.4 → 0.1.6

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 CHANGED
@@ -21,15 +21,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  InMemoryIntent: () => InMemoryIntent,
24
- InMemorySession: () => InMemorySession
24
+ InMemoryThread: () => InMemoryThread
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
27
 
28
- // implements/session.memory.ts
28
+ // implements/thread.memory.ts
29
29
  var import_node_crypto = require("crypto");
30
- var InMemorySession = class {
31
- sessions = /* @__PURE__ */ new Map();
32
- userSessionIndex = /* @__PURE__ */ new Map();
30
+ var InMemoryThread = class {
31
+ threads = /* @__PURE__ */ new Map();
32
+ userThreadIndex = /* @__PURE__ */ new Map();
33
33
  async connect() {
34
34
  }
35
35
  async disconnect() {
@@ -37,52 +37,58 @@ var InMemorySession = class {
37
37
  isConnected() {
38
38
  return true;
39
39
  }
40
- generateKey(userId, sessionId) {
41
- return `${userId}:${sessionId}`;
40
+ generateKey(userId, threadId) {
41
+ return `${userId}:${threadId}`;
42
42
  }
43
- async getSession(userId, sessionId) {
44
- const key = this.generateKey(userId, sessionId);
45
- const res = this.sessions.get(key);
43
+ async getThread(type, userId, threadId) {
44
+ const key = this.generateKey(userId, threadId);
45
+ const res = this.threads.get(key);
46
46
  if (res) {
47
- const sessionObject = {
48
- chats: Object.fromEntries(res.chats)
47
+ const threadObject = {
48
+ type: res.type,
49
+ title: res.title,
50
+ messages: Object.fromEntries(res.messages)
49
51
  };
50
- return sessionObject;
52
+ return threadObject;
51
53
  }
52
54
  return void 0;
53
55
  }
54
- async createSession(userId, sessionId, title) {
56
+ async createThread(type, userId, threadId, title) {
55
57
  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());
58
+ const key = this.generateKey(userId, threadId);
59
+ if (!this.userThreadIndex.has(userId)) {
60
+ this.userThreadIndex.set(userId, /* @__PURE__ */ new Set());
59
61
  }
60
- if (!this.sessions.has(key)) {
61
- this.sessions.set(key, { chats: /* @__PURE__ */ new Map() });
62
+ if (!this.threads.has(key)) {
63
+ this.threads.set(key, { type, title, messages: /* @__PURE__ */ new Map() });
62
64
  const metadata = {
63
- sessionId,
65
+ type,
66
+ threadId,
67
+ title,
64
68
  createdAt: now,
65
69
  updatedAt: now
66
70
  };
67
- this.userSessionIndex.get(userId)?.add(metadata);
71
+ this.userThreadIndex.get(userId)?.add(metadata);
68
72
  }
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");
74
- const newChatId = (0, import_node_crypto.randomUUID)();
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);
73
+ return { type, title, threadId, updatedAt: now };
74
+ }
75
+ async addMessagesToThread(userId, threadId, messages) {
76
+ const key = this.generateKey(userId, threadId);
77
+ const thread = this.threads.get(key);
78
+ for (const message of messages) {
79
+ const newMessageId = (0, import_node_crypto.randomUUID)();
80
+ thread?.messages.set(newMessageId, message);
81
+ }
82
+ }
83
+ async deleteThread(userId, threadId) {
84
+ const key = this.generateKey(userId, threadId);
85
+ this.threads.delete(key);
86
+ this.userThreadIndex.delete(threadId);
87
+ }
88
+ async listThreads(userId) {
89
+ const threads = this.userThreadIndex.get(userId);
90
+ if (threads) {
91
+ return Array.from(threads);
86
92
  }
87
93
  return [];
88
94
  }
@@ -119,6 +125,6 @@ var InMemoryIntent = class {
119
125
  // Annotate the CommonJS export names for ESM import in node:
120
126
  0 && (module.exports = {
121
127
  InMemoryIntent,
122
- InMemorySession
128
+ InMemoryThread
123
129
  });
124
130
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
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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ACxB,SAAK,iBAAiB,OAAO,SAAS;AAAA,EACxC;AAAA,EAED,MAAa,aAAa,QAA4C;AACnE,UAAM,WAAW,KAAK,iBAAiB,IAAI,MAAM;AACjD,QAAI,UAAU;AACZ,aAAO,MAAM,KAAK,QAAQ;AAAA,IAC5B;AACA,WAAO,CAAC;AAAA,EACV;AACF;;;AC9EA,IAAAA,sBAA2B;AAIpB,IAAM,iBAAN,MAA8C;AAAA,EAC7C,UAA+B,oBAAI,IAAI;AAAA,EAE7C,MAAa,UAAyB;AAAA,EAAC;AAAA,EACvC,MAAa,aAA4B;AAAA,EAAC;AAAA,EACnC,cAAuB;AAC5B,WAAO;AAAA,EACT;AAAA,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"]}
1
+ {"version":3,"sources":["../index.ts","../implements/thread.memory.ts","../implements/intent.memory.ts"],"sourcesContent":["export { InMemoryThread } from \"./implements/thread.memory\";\nexport { InMemoryIntent } from \"./implements/intent.memory\";","import { randomUUID } from \"node:crypto\";\nimport type { MessageObject, ThreadObject, ThreadMetadata, ThreadType } from \"@ainetwork/adk/types/memory\";\nimport { IThreadMemory } from \"@ainetwork/adk/modules\";\n\ntype InMemoryThreadObject = {\n type: ThreadType;\n title: string;\n messages: Map<string, MessageObject>\n}\n\ntype InMemoryThreadMetadata = {\n type: ThreadType;\n threadId: string;\n title: string;\n updatedAt: number;\n createdAt: number;\n}\n\nexport class InMemoryThread implements IThreadMemory {\n\tpublic threads: Map<string, InMemoryThreadObject> = new Map();\n public userThreadIndex: Map<string, Set<InMemoryThreadMetadata>> = 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, threadId: string) {\n return `${userId}:${threadId}`;\n }\n\n public async getThread(\n type: ThreadType,\n userId: string,\n threadId: string\n ): Promise<ThreadObject | undefined> {\n const key = this.generateKey(userId, threadId);\n const res = this.threads.get(key);\n if (res) {\n const threadObject: ThreadObject = {\n type: res.type,\n title: res.title,\n messages: Object.fromEntries(res.messages)\n };\n return threadObject;\n }\n return undefined;\n };\n\n\tpublic async createThread(\n type: ThreadType,\n userId: string,\n threadId: string,\n title: string\n ): Promise<ThreadMetadata> {\n const now = Date.now();\n const key = this.generateKey(userId, threadId);\n if (!this.userThreadIndex.has(userId)) {\n this.userThreadIndex.set(userId, new Set());\n }\n if (!this.threads.has(key)) {\n this.threads.set(key, { type, title, messages: new Map() });\n const metadata: InMemoryThreadMetadata = {\n type, threadId, title, createdAt: now, updatedAt: now,\n }\n this.userThreadIndex.get(userId)?.add(metadata);\n }\n\n return { type, title, threadId, updatedAt: now };\n };\n\n\tpublic async addMessagesToThread(\n userId: string,\n threadId: string,\n messages: MessageObject[]\n ): Promise<void> {\n const key = this.generateKey(userId, threadId);\n const thread = this.threads.get(key);\n for (const message of messages) {\n const newMessageId = randomUUID();\n thread?.messages.set(newMessageId, message);\n }\n };\n\n\tpublic async deleteThread(userId: string, threadId: string): Promise<void> {\n const key = this.generateKey(userId, threadId);\n this.threads.delete(key);\n this.userThreadIndex.delete(threadId);\n };\n\n\tpublic async listThreads(userId: string): Promise<ThreadMetadata[]> {\n const threads = this.userThreadIndex.get(userId);\n if (threads) {\n return Array.from(threads);\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;AAkBpB,IAAM,iBAAN,MAA8C;AAAA,EAC7C,UAA6C,oBAAI,IAAI;AAAA,EACpD,kBAA4D,oBAAI,IAAI;AAAA,EAE3E,MAAa,UAAyB;AAAA,EAAC;AAAA,EACvC,MAAa,aAA4B;AAAA,EAAC;AAAA,EACnC,cAAuB;AAC5B,WAAO;AAAA,EACT;AAAA,EAEQ,YAAY,QAAgB,UAAkB;AACpD,WAAO,GAAG,MAAM,IAAI,QAAQ;AAAA,EAC9B;AAAA,EAEA,MAAa,UACX,MACA,QACA,UACmC;AACnC,UAAM,MAAM,KAAK,YAAY,QAAQ,QAAQ;AAC7C,UAAM,MAAM,KAAK,QAAQ,IAAI,GAAG;AAChC,QAAI,KAAK;AACP,YAAM,eAA6B;AAAA,QACjC,MAAM,IAAI;AAAA,QACV,OAAO,IAAI;AAAA,QACX,UAAU,OAAO,YAAY,IAAI,QAAQ;AAAA,MAC3C;AACA,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT;AAAA,EAED,MAAa,aACV,MACA,QACA,UACA,OACyB;A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,EAAC;AAAA,EACvC,MAAa,aAA4B;AAAA,EAAC;AAAA,EACnC,cAAuB;AAC5B,WAAO;AAAA,EACT;AAAA,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,27 +1,30 @@
1
- import { ChatObject, SessionObject, SessionMetadata, Intent } from '@ainetwork/adk/types/memory';
2
- import { ISessionMemory, IIntentMemory } from '@ainetwork/adk/modules';
1
+ import { ThreadType, MessageObject, ThreadObject, ThreadMetadata, Intent } from '@ainetwork/adk/types/memory';
2
+ import { IThreadMemory, IIntentMemory } from '@ainetwork/adk/modules';
3
3
 
4
- type InMemorySessionObject = {
5
- chats: Map<string, ChatObject>;
4
+ type InMemoryThreadObject = {
5
+ type: ThreadType;
6
+ title: string;
7
+ messages: Map<string, MessageObject>;
6
8
  };
7
- type InMemorySessionMetadata = {
8
- sessionId: string;
9
- title?: string;
9
+ type InMemoryThreadMetadata = {
10
+ type: ThreadType;
11
+ threadId: string;
12
+ title: string;
10
13
  updatedAt: number;
11
14
  createdAt: number;
12
15
  };
13
- declare class InMemorySession implements ISessionMemory {
14
- sessions: Map<string, InMemorySessionObject>;
15
- userSessionIndex: Map<string, Set<InMemorySessionMetadata>>;
16
+ declare class InMemoryThread implements IThreadMemory {
17
+ threads: Map<string, InMemoryThreadObject>;
18
+ userThreadIndex: Map<string, Set<InMemoryThreadMetadata>>;
16
19
  connect(): Promise<void>;
17
20
  disconnect(): Promise<void>;
18
21
  isConnected(): boolean;
19
22
  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[]>;
23
+ getThread(type: ThreadType, userId: string, threadId: string): Promise<ThreadObject | undefined>;
24
+ createThread(type: ThreadType, userId: string, threadId: string, title: string): Promise<ThreadMetadata>;
25
+ addMessagesToThread(userId: string, threadId: string, messages: MessageObject[]): Promise<void>;
26
+ deleteThread(userId: string, threadId: string): Promise<void>;
27
+ listThreads(userId: string): Promise<ThreadMetadata[]>;
25
28
  }
26
29
 
27
30
  declare class InMemoryIntent implements IIntentMemory {
@@ -36,4 +39,4 @@ declare class InMemoryIntent implements IIntentMemory {
36
39
  listIntents(): Promise<Intent[]>;
37
40
  }
38
41
 
39
- export { InMemoryIntent, InMemorySession };
42
+ export { InMemoryIntent, InMemoryThread };
package/dist/index.d.ts CHANGED
@@ -1,27 +1,30 @@
1
- import { ChatObject, SessionObject, SessionMetadata, Intent } from '@ainetwork/adk/types/memory';
2
- import { ISessionMemory, IIntentMemory } from '@ainetwork/adk/modules';
1
+ import { ThreadType, MessageObject, ThreadObject, ThreadMetadata, Intent } from '@ainetwork/adk/types/memory';
2
+ import { IThreadMemory, IIntentMemory } from '@ainetwork/adk/modules';
3
3
 
4
- type InMemorySessionObject = {
5
- chats: Map<string, ChatObject>;
4
+ type InMemoryThreadObject = {
5
+ type: ThreadType;
6
+ title: string;
7
+ messages: Map<string, MessageObject>;
6
8
  };
7
- type InMemorySessionMetadata = {
8
- sessionId: string;
9
- title?: string;
9
+ type InMemoryThreadMetadata = {
10
+ type: ThreadType;
11
+ threadId: string;
12
+ title: string;
10
13
  updatedAt: number;
11
14
  createdAt: number;
12
15
  };
13
- declare class InMemorySession implements ISessionMemory {
14
- sessions: Map<string, InMemorySessionObject>;
15
- userSessionIndex: Map<string, Set<InMemorySessionMetadata>>;
16
+ declare class InMemoryThread implements IThreadMemory {
17
+ threads: Map<string, InMemoryThreadObject>;
18
+ userThreadIndex: Map<string, Set<InMemoryThreadMetadata>>;
16
19
  connect(): Promise<void>;
17
20
  disconnect(): Promise<void>;
18
21
  isConnected(): boolean;
19
22
  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[]>;
23
+ getThread(type: ThreadType, userId: string, threadId: string): Promise<ThreadObject | undefined>;
24
+ createThread(type: ThreadType, userId: string, threadId: string, title: string): Promise<ThreadMetadata>;
25
+ addMessagesToThread(userId: string, threadId: string, messages: MessageObject[]): Promise<void>;
26
+ deleteThread(userId: string, threadId: string): Promise<void>;
27
+ listThreads(userId: string): Promise<ThreadMetadata[]>;
25
28
  }
26
29
 
27
30
  declare class InMemoryIntent implements IIntentMemory {
@@ -36,4 +39,4 @@ declare class InMemoryIntent implements IIntentMemory {
36
39
  listIntents(): Promise<Intent[]>;
37
40
  }
38
41
 
39
- export { InMemoryIntent, InMemorySession };
42
+ export { InMemoryIntent, InMemoryThread };
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- // implements/session.memory.ts
1
+ // implements/thread.memory.ts
2
2
  import { randomUUID } from "crypto";
3
- var InMemorySession = class {
4
- sessions = /* @__PURE__ */ new Map();
5
- userSessionIndex = /* @__PURE__ */ new Map();
3
+ var InMemoryThread = class {
4
+ threads = /* @__PURE__ */ new Map();
5
+ userThreadIndex = /* @__PURE__ */ new Map();
6
6
  async connect() {
7
7
  }
8
8
  async disconnect() {
@@ -10,52 +10,58 @@ var InMemorySession = class {
10
10
  isConnected() {
11
11
  return true;
12
12
  }
13
- generateKey(userId, sessionId) {
14
- return `${userId}:${sessionId}`;
13
+ generateKey(userId, threadId) {
14
+ return `${userId}:${threadId}`;
15
15
  }
16
- async getSession(userId, sessionId) {
17
- const key = this.generateKey(userId, sessionId);
18
- const res = this.sessions.get(key);
16
+ async getThread(type, userId, threadId) {
17
+ const key = this.generateKey(userId, threadId);
18
+ const res = this.threads.get(key);
19
19
  if (res) {
20
- const sessionObject = {
21
- chats: Object.fromEntries(res.chats)
20
+ const threadObject = {
21
+ type: res.type,
22
+ title: res.title,
23
+ messages: Object.fromEntries(res.messages)
22
24
  };
23
- return sessionObject;
25
+ return threadObject;
24
26
  }
25
27
  return void 0;
26
28
  }
27
- async createSession(userId, sessionId, title) {
29
+ async createThread(type, userId, threadId, title) {
28
30
  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());
31
+ const key = this.generateKey(userId, threadId);
32
+ if (!this.userThreadIndex.has(userId)) {
33
+ this.userThreadIndex.set(userId, /* @__PURE__ */ new Set());
32
34
  }
33
- if (!this.sessions.has(key)) {
34
- this.sessions.set(key, { chats: /* @__PURE__ */ new Map() });
35
+ if (!this.threads.has(key)) {
36
+ this.threads.set(key, { type, title, messages: /* @__PURE__ */ new Map() });
35
37
  const metadata = {
36
- sessionId,
38
+ type,
39
+ threadId,
40
+ title,
37
41
  createdAt: now,
38
42
  updatedAt: now
39
43
  };
40
- this.userSessionIndex.get(userId)?.add(metadata);
44
+ this.userThreadIndex.get(userId)?.add(metadata);
41
45
  }
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");
47
- const newChatId = randomUUID();
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);
46
+ return { type, title, threadId, updatedAt: now };
47
+ }
48
+ async addMessagesToThread(userId, threadId, messages) {
49
+ const key = this.generateKey(userId, threadId);
50
+ const thread = this.threads.get(key);
51
+ for (const message of messages) {
52
+ const newMessageId = randomUUID();
53
+ thread?.messages.set(newMessageId, message);
54
+ }
55
+ }
56
+ async deleteThread(userId, threadId) {
57
+ const key = this.generateKey(userId, threadId);
58
+ this.threads.delete(key);
59
+ this.userThreadIndex.delete(threadId);
60
+ }
61
+ async listThreads(userId) {
62
+ const threads = this.userThreadIndex.get(userId);
63
+ if (threads) {
64
+ return Array.from(threads);
59
65
  }
60
66
  return [];
61
67
  }
@@ -91,6 +97,6 @@ var InMemoryIntent = class {
91
97
  };
92
98
  export {
93
99
  InMemoryIntent,
94
- InMemorySession
100
+ InMemoryThread
95
101
  };
96
102
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
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,CAAC,KAAK,SAAS,IAAI,GAAG,GAAG;AAC3B,WAAK,SAAS,IAAI,KAAK,EAAE,OAAO,oBAAI,IAAI,EAAE,CAAC;AAC3C,YAAM,WAAoC;AAAA,QACxC;AAAA,QAAW,WAAW;AAAA,QAAK,WAAW;AAAA,MACxC;AACA,WAAK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AA,EAED,MAAa,cAAiC;AAC3C,WAAO,MAAM,KAAK,KAAK,QAAQ,OAAO,CAAC;AAAA,EACzC;AACF;","names":["randomUUID"]}
1
+ {"version":3,"sources":["../implements/thread.memory.ts","../implements/intent.memory.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { MessageObject, ThreadObject, ThreadMetadata, ThreadType } from \"@ainetwork/adk/types/memory\";\nimport { IThreadMemory } from \"@ainetwork/adk/modules\";\n\ntype InMemoryThreadObject = {\n type: ThreadType;\n title: string;\n messages: Map<string, MessageObject>\n}\n\ntype InMemoryThreadMetadata = {\n type: ThreadType;\n threadId: string;\n title: string;\n updatedAt: number;\n createdAt: number;\n}\n\nexport class InMemoryThread implements IThreadMemory {\n\tpublic threads: Map<string, InMemoryThreadObject> = new Map();\n public userThreadIndex: Map<string, Set<InMemoryThreadMetadata>> = 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, threadId: string) {\n return `${userId}:${threadId}`;\n }\n\n public async getThread(\n type: ThreadType,\n userId: string,\n threadId: string\n ): Promise<ThreadObject | undefined> {\n const key = this.generateKey(userId, threadId);\n const res = this.threads.get(key);\n if (res) {\n const threadObject: ThreadObject = {\n type: res.type,\n title: res.title,\n messages: Object.fromEntries(res.messages)\n };\n return threadObject;\n }\n return undefined;\n };\n\n\tpublic async createThread(\n type: ThreadType,\n userId: string,\n threadId: string,\n title: string\n ): Promise<ThreadMetadata> {\n const now = Date.now();\n const key = this.generateKey(userId, threadId);\n if (!this.userThreadIndex.has(userId)) {\n this.userThreadIndex.set(userId, new Set());\n }\n if (!this.threads.has(key)) {\n this.threads.set(key, { type, title, messages: new Map() });\n const metadata: InMemoryThreadMetadata = {\n type, threadId, title, createdAt: now, updatedAt: now,\n }\n this.userThreadIndex.get(userId)?.add(metadata);\n }\n\n return { type, title, threadId, updatedAt: now };\n };\n\n\tpublic async addMessagesToThread(\n userId: string,\n threadId: string,\n messages: MessageObject[]\n ): Promise<void> {\n const key = this.generateKey(userId, threadId);\n const thread = this.threads.get(key);\n for (const message of messages) {\n const newMessageId = randomUUID();\n thread?.messages.set(newMessageId, message);\n }\n };\n\n\tpublic async deleteThread(userId: string, threadId: string): Promise<void> {\n const key = this.generateKey(userId, threadId);\n this.threads.delete(key);\n this.userThreadIndex.delete(threadId);\n };\n\n\tpublic async listThreads(userId: string): Promise<ThreadMetadata[]> {\n const threads = this.userThreadIndex.get(userId);\n if (threads) {\n return Array.from(threads);\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;AAkBpB,IAAM,iBAAN,MAA8C;AAAA,EAC7C,UAA6C,oBAAI,IAAI;AAAA,EACpD,kBAA4D,oBAAI,IAAI;AAAA,EAE3E,MAAa,UAAyB;AAAA,EAAC;AAAA,EACvC,MAAa,aAA4B;AAAA,EAAC;AAAA,EACnC,cAAuB;AAC5B,WAAO;AAAA,EACT;AAAA,EAEQ,YAAY,QAAgB,UAAkB;AACpD,WAAO,GAAG,MAAM,IAAI,QAAQ;AAAA,EAC9B;AAAA,EAEA,MAAa,UAC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ACvB,SAAK,gBAAgB,OAAO,QAAQ;AAAA,EACtC;AAAA,EAED,MAAa,YAAY,QAA2C;AACjE,UAAM,UAAU,KAAK,gBAAgB,IAAI,MAAM;AAC/C,QAAI,SAAS;AACX,aAAO,MAAM,KAAK,OAAO;AAAA,IAC3B;AACA,WAAO,CAAC;AAAA,EACV;AACF;;;AClGA,SAAS,cAAAA,mBAAkB;AAIpB,IAAM,iBAAN,MAA8C;AAAA,EAC7C,UAA+B,oBAAI,IAAI;AAAA,EAE7C,MAAa,UAAyB;AAAA,EAAC;AAAA,EACvC,MAAa,aAA4B;AAAA,EAAC;AAAA,EACnC,cAAuB;AAC5B,WAAO;AAAA,EACT;AAAA,EAEA,MAAa,UAAU,UAA+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,99 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import type { MessageObject, ThreadObject, ThreadMetadata, ThreadType } from "@ainetwork/adk/types/memory";
3
+ import { IThreadMemory } from "@ainetwork/adk/modules";
4
+
5
+ type InMemoryThreadObject = {
6
+ type: ThreadType;
7
+ title: string;
8
+ messages: Map<string, MessageObject>
9
+ }
10
+
11
+ type InMemoryThreadMetadata = {
12
+ type: ThreadType;
13
+ threadId: string;
14
+ title: string;
15
+ updatedAt: number;
16
+ createdAt: number;
17
+ }
18
+
19
+ export class InMemoryThread implements IThreadMemory {
20
+ public threads: Map<string, InMemoryThreadObject> = new Map();
21
+ public userThreadIndex: Map<string, Set<InMemoryThreadMetadata>> = new Map();
22
+
23
+ public async connect(): Promise<void> {}
24
+ public async disconnect(): Promise<void> {}
25
+ public isConnected(): boolean {
26
+ return true;
27
+ }
28
+
29
+ private generateKey(userId: string, threadId: string) {
30
+ return `${userId}:${threadId}`;
31
+ }
32
+
33
+ public async getThread(
34
+ type: ThreadType,
35
+ userId: string,
36
+ threadId: string
37
+ ): Promise<ThreadObject | undefined> {
38
+ const key = this.generateKey(userId, threadId);
39
+ const res = this.threads.get(key);
40
+ if (res) {
41
+ const threadObject: ThreadObject = {
42
+ type: res.type,
43
+ title: res.title,
44
+ messages: Object.fromEntries(res.messages)
45
+ };
46
+ return threadObject;
47
+ }
48
+ return undefined;
49
+ };
50
+
51
+ public async createThread(
52
+ type: ThreadType,
53
+ userId: string,
54
+ threadId: string,
55
+ title: string
56
+ ): Promise<ThreadMetadata> {
57
+ const now = Date.now();
58
+ const key = this.generateKey(userId, threadId);
59
+ if (!this.userThreadIndex.has(userId)) {
60
+ this.userThreadIndex.set(userId, new Set());
61
+ }
62
+ if (!this.threads.has(key)) {
63
+ this.threads.set(key, { type, title, messages: new Map() });
64
+ const metadata: InMemoryThreadMetadata = {
65
+ type, threadId, title, createdAt: now, updatedAt: now,
66
+ }
67
+ this.userThreadIndex.get(userId)?.add(metadata);
68
+ }
69
+
70
+ return { type, title, threadId, updatedAt: now };
71
+ };
72
+
73
+ public async addMessagesToThread(
74
+ userId: string,
75
+ threadId: string,
76
+ messages: MessageObject[]
77
+ ): Promise<void> {
78
+ const key = this.generateKey(userId, threadId);
79
+ const thread = this.threads.get(key);
80
+ for (const message of messages) {
81
+ const newMessageId = randomUUID();
82
+ thread?.messages.set(newMessageId, message);
83
+ }
84
+ };
85
+
86
+ public async deleteThread(userId: string, threadId: string): Promise<void> {
87
+ const key = this.generateKey(userId, threadId);
88
+ this.threads.delete(key);
89
+ this.userThreadIndex.delete(threadId);
90
+ };
91
+
92
+ public async listThreads(userId: string): Promise<ThreadMetadata[]> {
93
+ const threads = this.userThreadIndex.get(userId);
94
+ if (threads) {
95
+ return Array.from(threads);
96
+ }
97
+ return [];
98
+ };
99
+ }
package/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { InMemorySession } from "./implements/session.memory";
1
+ export { InMemoryThread } from "./implements/thread.memory";
2
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.4",
3
+ "version": "0.1.6",
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.9"
24
+ "@ainetwork/adk": "^0.1.13"
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": "66a5fbc5f9a509bd412ff8dc91b002862133fbae"
33
+ "gitHead": "d27f9542cbc4049593be4015b78e607e19ac22e6"
34
34
  }
@@ -1,79 +0,0 @@
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, title: string): Promise<SessionMetadata> {
43
- const now = Date.now();
44
- const key = this.generateKey(userId, sessionId);
45
- if (!this.userSessionIndex.has(userId)) {
46
- this.userSessionIndex.set(userId, new Set());
47
- }
48
- if (!this.sessions.has(key)) {
49
- this.sessions.set(key, { chats: new Map() });
50
- const metadata: InMemorySessionMetadata = {
51
- sessionId, createdAt: now, updatedAt: now,
52
- }
53
- this.userSessionIndex.get(userId)?.add(metadata);
54
- }
55
-
56
- return { title, sessionId, updatedAt: now };
57
- };
58
-
59
- public async addChatToSession(userId: string, sessionId: string, chat: ChatObject): Promise<void> {
60
- const key = this.generateKey(userId, sessionId);
61
- await this.createSession(userId, sessionId, "New chat");
62
- const newChatId = randomUUID();
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
- }