@wabot-dev/framework 0.0.10 → 0.0.12

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.
Files changed (49) hide show
  1. package/dist/src/ai/openia/OpenaiChatBotAdapter.js +2 -4
  2. package/dist/src/channels/cmd/@cmd.js +2 -0
  3. package/dist/src/channels/cmd/CmdChannel.js +2 -5
  4. package/dist/src/channels/telegram/@telegram.js +2 -0
  5. package/dist/src/channels/telegram/TelegramChannel.js +2 -4
  6. package/dist/src/chatbot/ChatBot.js +2 -4
  7. package/dist/src/chatbot/ChatBotAdapter.js +1 -3
  8. package/dist/src/chatbot/metadata/@chatBot.js +2 -0
  9. package/dist/src/chatbot/metadata/ChatBotMetadataStore.js +2 -0
  10. package/dist/src/controller/channel/ChatResolver.js +1 -3
  11. package/dist/src/controller/channel/UserResolver.js +1 -3
  12. package/dist/src/controller/metadata/ControllerMetadataStore.js +2 -0
  13. package/dist/src/controller/metadata/controller/@chatController.js +2 -0
  14. package/dist/src/core/IMessageContext.js +1 -2
  15. package/dist/src/{shared → core}/Persistent.js +2 -4
  16. package/dist/src/core/chat/Chat.js +1 -1
  17. package/dist/src/core/chat/ChatItem.js +1 -1
  18. package/dist/src/core/user/{repository/IUserRepository.js → IUserRepository.js} +1 -1
  19. package/dist/src/core/user/User.js +1 -1
  20. package/dist/src/index.d.ts +235 -174
  21. package/dist/src/index.js +17 -15
  22. package/dist/src/mindset/MindsetOperator.js +2 -0
  23. package/dist/src/mindset/metadata/MindsetMetadataStore.js +2 -0
  24. package/dist/src/mindset/metadata/mindsets/@mindset.js +2 -0
  25. package/dist/src/mindset/metadata/modules/@mindsetModule.js +2 -0
  26. package/dist/src/pre-made/module/authentication/AuthenticationModule.js +1 -3
  27. package/dist/src/pre-made/module/authentication/requests/SendOneTimePasswordRequest.js +2 -0
  28. package/dist/src/pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js +2 -0
  29. package/dist/src/pre-made/module/register-user/RegisterUserModule.js +1 -3
  30. package/dist/src/pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js +2 -0
  31. package/dist/src/pre-made/repository/chat/pg/PgChatMemory.js +43 -0
  32. package/dist/src/pre-made/repository/chat/pg/PgChatRepository.js +42 -0
  33. package/dist/src/{core/chat/repository → pre-made/repository/chat}/ram/RamChatRepository.js +1 -2
  34. package/dist/src/pre-made/repository/chat/sqlite/SqliteChatMemory.js +23 -0
  35. package/dist/src/pre-made/repository/{sqlite/chat → chat/sqlite}/SqliteChatRepository.js +6 -16
  36. package/dist/src/pre-made/repository/user/pg/PgUserRepository.js +38 -0
  37. package/dist/src/{core/user/repository → pre-made/repository/user}/ram/RamUserRepository.js +1 -2
  38. package/dist/src/pre-made/repository/user/sqlite/SqliteUserRepository.js +23 -0
  39. package/dist/src/repository/pg/PgCrudRepository.js +63 -0
  40. package/dist/src/repository/pg/PgRepositoryBase.js +89 -0
  41. package/dist/src/{pre-made/repository → repository}/sqlite/SqliteCrudRepository.js +1 -1
  42. package/dist/src/repository/sqlite/SqlitePersistentMapper.js +17 -0
  43. package/dist/src/server/prepareChatContainer.js +4 -6
  44. package/dist/src/server/runChannel.js +2 -4
  45. package/dist/src/server/runServer.js +2 -4
  46. package/package.json +4 -1
  47. package/dist/src/pre-made/repository/sqlite/chat/SqliteChatMemory.js +0 -35
  48. package/dist/src/pre-made/repository/sqlite/user/SqliteUserRepository.js +0 -33
  49. /package/dist/src/{core/chat/repository → pre-made/repository/chat}/ram/RamChatMemory.js +0 -0
@@ -0,0 +1,42 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import '../../../../core/chat/repository/IChatRepository.js';
3
+ import { Chat } from '../../../../core/chat/Chat.js';
4
+ import '../../../../core/user/IUserRepository.js';
5
+ import { singleton } from '../../../../injection/index.js';
6
+ import { Pool } from 'pg';
7
+ import { PgChatMemory } from './PgChatMemory.js';
8
+ import { PgCrudRepository } from '../../../../repository/pg/PgCrudRepository.js';
9
+ import 'fs';
10
+ import 'path';
11
+ import 'sqlite';
12
+ import 'sqlite3';
13
+ import 'uuid';
14
+
15
+ let PgChatRepository = class PgChatRepository extends PgCrudRepository {
16
+ constructor(pool) {
17
+ super(pool, {
18
+ table: 'chat',
19
+ schema: 'wabot',
20
+ constructor: Chat,
21
+ });
22
+ }
23
+ async findByConnection(query) {
24
+ const sql = `
25
+ SELECT ${this.columns}
26
+ FROM ${this.table}
27
+ WHERE data->'connections' @> $1::jsonb
28
+ LIMIT 1
29
+ `;
30
+ const items = await this.query(sql, [JSON.stringify([query])]);
31
+ return items.at(0) ?? null;
32
+ }
33
+ async findMemory(chatId) {
34
+ return new PgChatMemory(this.pool, chatId);
35
+ }
36
+ };
37
+ PgChatRepository = __decorate([
38
+ singleton(),
39
+ __metadata("design:paramtypes", [Pool])
40
+ ], PgChatRepository);
41
+
42
+ export { PgChatRepository };
@@ -1,6 +1,5 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { v4 } from 'uuid';
3
- import '../IChatRepository.js';
4
3
  import { RamChatMemory } from './RamChatMemory.js';
5
4
  import { singleton } from '../../../../injection/index.js';
6
5
 
@@ -12,7 +11,7 @@ let RamChatRepository = class RamChatRepository {
12
11
  throw new Error('Chat already created');
13
12
  }
14
13
  chat['data'].id = v4();
15
- chat['data'].createdAt = new Date();
14
+ chat['data'].createdAt = new Date().getTime();
16
15
  chat.validate();
17
16
  this.items.push(chat);
18
17
  const memory = {
@@ -0,0 +1,23 @@
1
+ import path from 'path';
2
+ import { ChatItem } from '../../../../core/chat/ChatItem.js';
3
+ import '../../../../core/chat/repository/IChatRepository.js';
4
+ import '../../../../core/user/IUserRepository.js';
5
+ import 'pg';
6
+ import 'short-uuid';
7
+ import { SqliteCrudRepository } from '../../../../repository/sqlite/SqliteCrudRepository.js';
8
+ import { sqliteMapperFor } from '../../../../repository/sqlite/SqlitePersistentMapper.js';
9
+
10
+ class SqliteChatMemory extends SqliteCrudRepository {
11
+ constructor(chatId) {
12
+ super('chat_item', SqliteChatMemory.getDbPath(chatId), sqliteMapperFor(ChatItem));
13
+ }
14
+ async findLastItems(count) {
15
+ const allItems = await this.findAll();
16
+ return allItems.slice(allItems.length - count, allItems.length);
17
+ }
18
+ static getDbPath(chatId) {
19
+ return path.join(process.cwd(), '.sqlite', 'chat-memory', chatId + '.db');
20
+ }
21
+ }
22
+
23
+ export { SqliteChatMemory };
@@ -1,26 +1,16 @@
1
- import '../../../../core/chat/repository/ram/RamChatRepository.js';
2
1
  import '../../../../core/chat/repository/IChatRepository.js';
3
2
  import { Chat } from '../../../../core/chat/Chat.js';
4
- import '../../../../core/user/repository/ram/RamUserRepository.js';
5
- import '../../../../core/user/repository/IUserRepository.js';
3
+ import '../../../../core/user/IUserRepository.js';
6
4
  import path from 'path';
7
- import { SqliteCrudRepository } from '../SqliteCrudRepository.js';
8
5
  import { SqliteChatMemory } from './SqliteChatMemory.js';
6
+ import 'pg';
7
+ import 'short-uuid';
8
+ import { SqliteCrudRepository } from '../../../../repository/sqlite/SqliteCrudRepository.js';
9
+ import { sqliteMapperFor } from '../../../../repository/sqlite/SqlitePersistentMapper.js';
9
10
 
10
- const chatSqliteMapper = {
11
- map(input) {
12
- return JSON.stringify(input['data']);
13
- },
14
- rev(input) {
15
- const data = JSON.parse(input);
16
- data.createdAt = new Date(data.createdAt);
17
- data.discardedAt = data.discardedAt && new Date(data.discardedAt);
18
- return new Chat(data);
19
- },
20
- };
21
11
  class SqliteChatRepository extends SqliteCrudRepository {
22
12
  constructor() {
23
- super('chat', SqliteChatRepository.getDbPath(), chatSqliteMapper);
13
+ super('chat', SqliteChatRepository.getDbPath(), sqliteMapperFor(Chat));
24
14
  }
25
15
  async findByConnection(query) {
26
16
  const allChats = await this.findAll();
@@ -0,0 +1,38 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import '../../../../core/chat/repository/IChatRepository.js';
3
+ import { User } from '../../../../core/user/User.js';
4
+ import '../../../../core/user/IUserRepository.js';
5
+ import { singleton } from '../../../../injection/index.js';
6
+ import { PgCrudRepository } from '../../../../repository/pg/PgCrudRepository.js';
7
+ import 'fs';
8
+ import 'path';
9
+ import 'sqlite';
10
+ import 'sqlite3';
11
+ import 'uuid';
12
+ import { Pool } from 'pg';
13
+
14
+ let PgUserRepository = class PgUserRepository extends PgCrudRepository {
15
+ constructor(pool) {
16
+ super(pool, {
17
+ table: 'user',
18
+ schema: 'wabot',
19
+ constructor: User,
20
+ });
21
+ }
22
+ async findByConnection(query) {
23
+ const sql = `
24
+ SELECT ${this.columns}
25
+ FROM ${this.table}
26
+ WHERE data->'connections' @> $1::jsonb
27
+ LIMIT 1
28
+ `;
29
+ const items = await this.query(sql, [JSON.stringify([query])]);
30
+ return items.at(0) ?? null;
31
+ }
32
+ };
33
+ PgUserRepository = __decorate([
34
+ singleton(),
35
+ __metadata("design:paramtypes", [Pool])
36
+ ], PgUserRepository);
37
+
38
+ export { PgUserRepository };
@@ -1,6 +1,5 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { v4 } from 'uuid';
3
- import '../IUserRepository.js';
4
3
  import { singleton } from '../../../../injection/index.js';
5
4
 
6
5
  let RamUserRepository = class RamUserRepository {
@@ -10,7 +9,7 @@ let RamUserRepository = class RamUserRepository {
10
9
  throw new Error('User already created');
11
10
  }
12
11
  chat['data'].id = v4();
13
- chat['data'].createdAt = new Date();
12
+ chat['data'].createdAt = new Date().getTime();
14
13
  chat.validate();
15
14
  this.items.push(chat);
16
15
  }
@@ -0,0 +1,23 @@
1
+ import '../../../../core/chat/repository/IChatRepository.js';
2
+ import { User } from '../../../../core/user/User.js';
3
+ import '../../../../core/user/IUserRepository.js';
4
+ import 'pg';
5
+ import 'short-uuid';
6
+ import { SqliteCrudRepository } from '../../../../repository/sqlite/SqliteCrudRepository.js';
7
+ import { sqliteMapperFor } from '../../../../repository/sqlite/SqlitePersistentMapper.js';
8
+ import path from 'path';
9
+
10
+ class SqliteUserRepository extends SqliteCrudRepository {
11
+ constructor() {
12
+ super('user', SqliteUserRepository.getDbPath(), sqliteMapperFor(User));
13
+ }
14
+ async findByConnection(query) {
15
+ const allItems = await this.findAll();
16
+ return allItems.find((item) => item.hasConnection(query)) ?? null;
17
+ }
18
+ static getDbPath() {
19
+ return path.join(process.cwd(), '.sqlite', 'user.db');
20
+ }
21
+ }
22
+
23
+ export { SqliteUserRepository };
@@ -0,0 +1,63 @@
1
+ import 'pg';
2
+ import * as shortUUID from 'short-uuid';
3
+ import { PgRepositoryBase } from './PgRepositoryBase.js';
4
+
5
+ class PgCrudRepository extends PgRepositoryBase {
6
+ config;
7
+ constructor(pool, config) {
8
+ super(pool, config);
9
+ this.config = config;
10
+ }
11
+ async find(id) {
12
+ const sql = `
13
+ SELECT ${this.columns}
14
+ FROM ${this.table}
15
+ WHERE id = $1
16
+ LIMIT 1
17
+ `;
18
+ const items = await this.query(sql, [id]);
19
+ return items.at(0) ?? null;
20
+ }
21
+ async findAll() {
22
+ const sql = `
23
+ SELECT ${this.columns}
24
+ FROM ${this.table}
25
+ `;
26
+ const items = await this.query(sql, []);
27
+ return items;
28
+ }
29
+ async create(item) {
30
+ if (item.wasCreated()) {
31
+ throw new Error('Item already created');
32
+ }
33
+ item['data'].id = shortUUID.default.generate();
34
+ item['data'].createdAt = new Date().getTime();
35
+ item.validate();
36
+ const sql = `
37
+ INSERT INTO
38
+ ${this.table}(${this.columns})
39
+ VALUES (${this.vars})
40
+ `;
41
+ await this.exec(sql, this.values(item));
42
+ }
43
+ async update(item) {
44
+ item.validate();
45
+ const sql = `
46
+ UPDATE ${this.table}
47
+ SET ${this.updates}
48
+ WHERE id = $${this.columnsList.length + 1}
49
+ `;
50
+ await this.exec(sql, [...this.values(item), item.getId()]);
51
+ }
52
+ async discard(item) {
53
+ const _item = await this.find(item.getId());
54
+ if (!_item) {
55
+ throw new Error('Not found');
56
+ }
57
+ item.discard();
58
+ _item.discard();
59
+ await this.update(_item);
60
+ }
61
+ }
62
+
63
+ export { PgCrudRepository };
@@ -0,0 +1,89 @@
1
+ class PgRepositoryBase {
2
+ pool;
3
+ config;
4
+ tableIsCreated = false;
5
+ schema;
6
+ table;
7
+ columnsList;
8
+ columnsAndTypes;
9
+ columns;
10
+ vars;
11
+ updates;
12
+ addColumns;
13
+ constructor(pool, config) {
14
+ this.pool = pool;
15
+ this.config = config;
16
+ this.schema = `"${config.schema ?? 'public'}"`;
17
+ this.table = [config.schema, config.table]
18
+ .filter((x) => x && x.trim())
19
+ .map((x) => `"${x}"`)
20
+ .join('.');
21
+ this.addColumns = config.add?.columns ?? {};
22
+ this.columnsList = ['id', 'created_at', 'data', ...Object.keys(this.addColumns)];
23
+ this.columnsAndTypes = [
24
+ '"id" TEXT PRIMARY KEY',
25
+ '"created_at" TIMESTAMP',
26
+ '"data" JSONB',
27
+ ...this.columnsList.slice(3).map((x) => `${x} ${this.addColumns[x].type}`),
28
+ ].join(', ');
29
+ this.columns = this.columnsList.map((x) => `"${x}"`).join(', ');
30
+ this.vars = this.columnsList.map((_, i) => `$${i + 1}`).join(', ');
31
+ this.updates = this.columnsList.map((x, i) => `"${x}" = $${i + 1}`).join(', ');
32
+ }
33
+ values(item) {
34
+ return [
35
+ item.getId(),
36
+ item.getCreatedAt(),
37
+ JSON.stringify(item['data']),
38
+ ...this.columnsList.slice(3).map((x) => this.addColumns[x].value(item)),
39
+ ];
40
+ }
41
+ async exec(sql, values) {
42
+ const conn = await this.connect();
43
+ await conn.query(sql, values);
44
+ }
45
+ async query(sql, values) {
46
+ const conn = await this.connect();
47
+ const { rows } = await conn.query(sql, values);
48
+ return rows.map((row) => new this.config.constructor(row.data));
49
+ }
50
+ async connect() {
51
+ await this.ensureTable();
52
+ return this.pool;
53
+ }
54
+ async ensureTable() {
55
+ if (this.tableIsCreated) {
56
+ return;
57
+ }
58
+ const schemaQuery = `
59
+ CREATE SCHEMA IF NOT EXISTS ${this.schema}
60
+ `;
61
+ const tableQuery = `
62
+ CREATE TABLE IF NOT EXISTS ${this.table} (
63
+ ${this.columnsAndTypes}
64
+ )
65
+ `;
66
+ await this.pool.query(schemaQuery);
67
+ await this.pool.query(tableQuery);
68
+ await this.ensureColumns();
69
+ }
70
+ async ensureColumns() {
71
+ const { rows: existing } = await this.pool.query(`
72
+ SELECT column_name
73
+ FROM information_schema.columns
74
+ WHERE table_name = $1 AND table_schema = $2
75
+ `, [this.config.table, this.config.schema ?? 'public']);
76
+ const existingColumns = new Set(existing.map((col) => col.column_name));
77
+ for (let i = 0; i < this.columnsList.length; i++) {
78
+ const col = this.columnsList[i];
79
+ const columnAndType = this.columnsAndTypes[0];
80
+ if (!existingColumns.has(col)) {
81
+ const alterSql = `ALTER TABLE ${this.table} ADD COLUMN ${columnAndType}`;
82
+ console.log(`[INFO] Adding column: ${alterSql}`);
83
+ await this.pool.query(alterSql);
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ export { PgRepositoryBase };
@@ -38,7 +38,7 @@ class SqliteCrudRepository {
38
38
  throw new Error('Item already created');
39
39
  }
40
40
  item['data'].id = v4();
41
- item['data'].createdAt = new Date();
41
+ item['data'].createdAt = new Date().getTime();
42
42
  item.validate();
43
43
  const db = await this.getDb();
44
44
  await db.run(`INSERT INTO ${this.table} VALUES (?, ?, ?)`, [
@@ -0,0 +1,17 @@
1
+ class SqlitePersistentMapper {
2
+ ctor;
3
+ constructor(ctor) {
4
+ this.ctor = ctor;
5
+ }
6
+ map(input) {
7
+ return JSON.stringify(input['data']);
8
+ }
9
+ rev(input) {
10
+ return new this.ctor(JSON.parse(input));
11
+ }
12
+ }
13
+ function sqliteMapperFor(ctor) {
14
+ return new SqlitePersistentMapper(ctor);
15
+ }
16
+
17
+ export { SqlitePersistentMapper, sqliteMapperFor };
@@ -1,14 +1,12 @@
1
+ import { ChatMemory } from '../core/chat/repository/IChatMemory.js';
2
+ import { ChatRepository } from '../core/chat/repository/IChatRepository.js';
3
+ import '../core/user/IUserRepository.js';
4
+ import { MessageContext } from '../core/IMessageContext.js';
1
5
  import '../injection/index.js';
2
6
  import 'reflect-metadata';
3
7
  import '../mindset/metadata/MindsetMetadataStore.js';
4
8
  import { Mindset } from '../mindset/IMindset.js';
5
9
  import '../mindset/MindsetOperator.js';
6
- import { ChatMemory } from '../core/chat/repository/IChatMemory.js';
7
- import '../core/chat/repository/ram/RamChatRepository.js';
8
- import { ChatRepository } from '../core/chat/repository/IChatRepository.js';
9
- import '../core/user/repository/ram/RamUserRepository.js';
10
- import '../core/user/repository/IUserRepository.js';
11
- import { MessageContext } from '../core/IMessageContext.js';
12
10
  import 'uuid';
13
11
  import { ChatBotMetadataStore } from '../chatbot/metadata/ChatBotMetadataStore.js';
14
12
  import { ChatBot } from '../chatbot/ChatBot.js';
@@ -2,15 +2,13 @@ import dotenv from 'dotenv';
2
2
  import '../controller/channel/ChatResolver.js';
3
3
  import '../controller/channel/UserResolver.js';
4
4
  import { container } from '../injection/index.js';
5
+ import { ChatRepository } from '../core/chat/repository/IChatRepository.js';
6
+ import '../core/user/IUserRepository.js';
5
7
  import '../controller/metadata/ControllerMetadataStore.js';
6
8
  import 'reflect-metadata';
7
9
  import '../mindset/metadata/MindsetMetadataStore.js';
8
10
  import '../mindset/MindsetOperator.js';
9
11
  import { prepareChatContainer } from './prepareChatContainer.js';
10
- import '../core/chat/repository/ram/RamChatRepository.js';
11
- import { ChatRepository } from '../core/chat/repository/IChatRepository.js';
12
- import '../core/user/repository/ram/RamUserRepository.js';
13
- import '../core/user/repository/IUserRepository.js';
14
12
  import 'uuid';
15
13
  import '../chatbot/metadata/ChatBotMetadataStore.js';
16
14
  import { ChatBot } from '../chatbot/ChatBot.js';
@@ -2,11 +2,9 @@ import dotenv from 'dotenv';
2
2
  import '../controller/channel/ChatResolver.js';
3
3
  import '../controller/channel/UserResolver.js';
4
4
  import { container } from '../injection/index.js';
5
- import { ControllerMetadataStore } from '../controller/metadata/ControllerMetadataStore.js';
6
- import '../core/chat/repository/ram/RamChatRepository.js';
7
5
  import '../core/chat/repository/IChatRepository.js';
8
- import '../core/user/repository/ram/RamUserRepository.js';
9
- import '../core/user/repository/IUserRepository.js';
6
+ import '../core/user/IUserRepository.js';
7
+ import { ControllerMetadataStore } from '../controller/metadata/ControllerMetadataStore.js';
10
8
  import { prepareChatContainer } from './prepareChatContainer.js';
11
9
 
12
10
  dotenv.config();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",
@@ -35,6 +35,7 @@
35
35
  "typescript": "5.8.3"
36
36
  },
37
37
  "peerDependencies": {
38
+ "@types/pg": "^8.11.14",
38
39
  "@yucacodes/ts": "^0.0.4",
39
40
  "class-transformer": "^0.5.1",
40
41
  "class-validator": "^0.14.1",
@@ -42,7 +43,9 @@
42
43
  "express": "^5.1.0",
43
44
  "grammy": "^1.36.0",
44
45
  "openai": "^4.93.0",
46
+ "pg": "^8.15.6",
45
47
  "reflect-metadata": "^0.2.2",
48
+ "short-uuid": "^5.2.0",
46
49
  "sqlite": "^5.1.1",
47
50
  "sqlite3": "^5.1.7",
48
51
  "tslib": "^2.8.1",
@@ -1,35 +0,0 @@
1
- import path from 'path';
2
- import { ChatItem } from '../../../../core/chat/ChatItem.js';
3
- import '../../../../core/chat/repository/ram/RamChatRepository.js';
4
- import '../../../../core/chat/repository/IChatRepository.js';
5
- import '../../../../core/user/repository/ram/RamUserRepository.js';
6
- import '../../../../core/user/repository/IUserRepository.js';
7
- import { SqliteCrudRepository } from '../SqliteCrudRepository.js';
8
-
9
- const chatItemSqliteMapper = {
10
- map(input) {
11
- return JSON.stringify(input['data']);
12
- },
13
- rev(input) {
14
- const data = JSON.parse(input);
15
- data.createdAt = new Date(data.createdAt);
16
- data.discardedAt = data.discardedAt && new Date(data.discardedAt);
17
- return new ChatItem(data);
18
- },
19
- };
20
- class SqliteChatMemory extends SqliteCrudRepository {
21
- chatId;
22
- constructor(chatId) {
23
- super('chat_item', SqliteChatMemory.getDbPath(chatId), chatItemSqliteMapper);
24
- this.chatId = chatId;
25
- }
26
- async findLastItems(count) {
27
- const allItems = await this.findAll();
28
- return allItems.slice(allItems.length - count, allItems.length);
29
- }
30
- static getDbPath(chatId) {
31
- return path.join(process.cwd(), '.sqlite', 'chat-memory', chatId + '.db');
32
- }
33
- }
34
-
35
- export { SqliteChatMemory };
@@ -1,33 +0,0 @@
1
- import '../../../../core/chat/repository/ram/RamChatRepository.js';
2
- import '../../../../core/chat/repository/IChatRepository.js';
3
- import { User } from '../../../../core/user/User.js';
4
- import '../../../../core/user/repository/ram/RamUserRepository.js';
5
- import '../../../../core/user/repository/IUserRepository.js';
6
- import path from 'path';
7
- import { SqliteCrudRepository } from '../SqliteCrudRepository.js';
8
-
9
- const userSqliteMapper = {
10
- map(input) {
11
- return JSON.stringify(input['data']);
12
- },
13
- rev(input) {
14
- const data = JSON.parse(input);
15
- data.createdAt = new Date(data.createdAt);
16
- data.discardedAt = data.discardedAt && new Date(data.discardedAt);
17
- return new User(data);
18
- },
19
- };
20
- class SqliteUserRepository extends SqliteCrudRepository {
21
- constructor() {
22
- super('user', SqliteUserRepository.getDbPath(), userSqliteMapper);
23
- }
24
- async findByConnection(query) {
25
- const allItems = await this.findAll();
26
- return allItems.find((item) => item.hasConnection(query)) ?? null;
27
- }
28
- static getDbPath() {
29
- return path.join(process.cwd(), '.sqlite', 'user.db');
30
- }
31
- }
32
-
33
- export { SqliteUserRepository };