@wabot-dev/framework 0.0.10 → 0.0.11

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.
@@ -29,7 +29,6 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
29
29
  }
30
30
  connect() {
31
31
  this.rl.on('line', async (input) => {
32
- debugger;
33
32
  const trimmedInput = input.trim();
34
33
  if (!trimmedInput) {
35
34
  this.rl.prompt();
@@ -12,7 +12,7 @@ let RamChatRepository = class RamChatRepository {
12
12
  throw new Error('Chat already created');
13
13
  }
14
14
  chat['data'].id = v4();
15
- chat['data'].createdAt = new Date();
15
+ chat['data'].createdAt = new Date().getTime();
16
16
  chat.validate();
17
17
  this.items.push(chat);
18
18
  const memory = {
@@ -10,7 +10,7 @@ let RamUserRepository = class RamUserRepository {
10
10
  throw new Error('User already created');
11
11
  }
12
12
  chat['data'].id = v4();
13
- chat['data'].createdAt = new Date();
13
+ chat['data'].createdAt = new Date().getTime();
14
14
  chat.validate();
15
15
  this.items.push(chat);
16
16
  }
@@ -4,6 +4,7 @@ import InterceptionOptions from 'tsyringe/dist/typings/types/interceptor-options
4
4
  import * as tsyringe from 'tsyringe';
5
5
  import { DependencyContainer as DependencyContainer$1 } from 'tsyringe';
6
6
  export { DependencyContainer } from 'tsyringe';
7
+ import { Pool } from 'pg';
7
8
  import { Database } from 'sqlite';
8
9
  import sqlite3 from 'sqlite3';
9
10
 
@@ -32,12 +33,11 @@ interface IReversibleMapper<T1, T2> {
32
33
 
33
34
  interface IPersistent {
34
35
  id?: string;
35
- createdAt?: Date | null;
36
- discardedAt?: Date | null;
36
+ createdAt?: number | null;
37
+ discardedAt?: number | null;
37
38
  }
38
39
  declare class Persistent<D extends IPersistent> {
39
40
  protected data: D;
40
- private originalData;
41
41
  constructor(data: D);
42
42
  getId(): string;
43
43
  getCreatedAt(): Date;
@@ -288,7 +288,7 @@ type ISystemFunctionCallItem = {
288
288
  };
289
289
  type IChatItemData = {
290
290
  id?: string;
291
- createdAt?: Date;
291
+ createdAt?: number;
292
292
  } & (ISystemMessageItem | IReceivedMessageItem | ISystemFunctionCallItem);
293
293
  type IChatItemType = IChatItemData['type'];
294
294
  declare class ChatItem extends Persistent<IChatItemData> {
@@ -552,6 +552,11 @@ declare class RegisterUserModule {
552
552
  registerUserWithEmail(request: RegisterUserWithEmailRequest): Promise<string>;
553
553
  }
554
554
 
555
+ interface IPgRecord {
556
+ id: string;
557
+ data: string;
558
+ }
559
+
555
560
  interface ICrudRepository<T> {
556
561
  find(id: string): Promise<T | null>;
557
562
  findAll(id: string): Promise<T[]>;
@@ -560,6 +565,35 @@ interface ICrudRepository<T> {
560
565
  discard(item: T): Promise<void>;
561
566
  }
562
567
 
568
+ declare class PgCrudRepository<P extends Persistent<IPersistent>> implements ICrudRepository<P> {
569
+ private readonly pool;
570
+ protected readonly tableName: string;
571
+ protected mapper: IReversibleMapper<P, string>;
572
+ private tableIsCreated;
573
+ constructor(pool: Pool, tableName: string, mapper: IReversibleMapper<P, string>);
574
+ find(id: string): Promise<P | null>;
575
+ findAll(): Promise<P[]>;
576
+ create(item: P): Promise<void>;
577
+ update(item: P): Promise<void>;
578
+ discard(item: P): Promise<void>;
579
+ protected getPool(): Promise<Pool>;
580
+ private createTableIfNotExists;
581
+ }
582
+
583
+ declare class PgPersistentMapper<P extends Persistent<IPersistent>> implements IReversibleMapper<P, string> {
584
+ private ctor;
585
+ constructor(ctor: IConstructor<P>);
586
+ map(input: P): string;
587
+ rev(input: string): P;
588
+ }
589
+ declare function pgMapperFor<P extends Persistent<IPersistent>>(ctor: IConstructor<P>): PgPersistentMapper<P>;
590
+
591
+ declare class PgChatRepository extends PgCrudRepository<Chat> implements IChatRepository {
592
+ constructor(pool: Pool);
593
+ findByConnection(query: IChatConnection): Promise<Chat | null>;
594
+ findMemory(chatId: string): Promise<IChatMemory | null>;
595
+ }
596
+
563
597
  declare class SqliteCrudRepository<P extends Persistent<IPersistent>> implements ICrudRepository<P> {
564
598
  private table;
565
599
  private dbPath;
@@ -576,7 +610,6 @@ declare class SqliteCrudRepository<P extends Persistent<IPersistent>> implements
576
610
  }
577
611
 
578
612
  declare class SqliteChatMemory extends SqliteCrudRepository<ChatItem> implements IChatMemory {
579
- private chatId;
580
613
  constructor(chatId: string);
581
614
  findLastItems(count: number): Promise<ChatItem[]>;
582
615
  static getDbPath(chatId: string): string;
@@ -600,4 +633,12 @@ interface ISqliteRecord {
600
633
  data: string;
601
634
  }
602
635
 
603
- export { AuthenticationModule, Chat, ChatBot, ChatBotAdapter, ChatBotMetadataStore, ChatItem, ChatMemory, ChatRepository, ChatResolver, CmdChannel, Container, ControllerMetadataStore, EmailService, type IChannelMetadata, type IChatBot, type IChatBotAdapter, type IChatBotMetadata, type IChatChannel, type IChatConnection, type IChatControllerMetadata, type IChatData, type IChatFunctionCall, type IChatItemData, type IChatItemType, type IChatMemory, type IChatMessage, type IChatRepository, type IChatType, type IConnectionChatMessage, type IConstructor, type ICrudRepository, type IEmailService, type IMessageContext, type IMindset, type IMindsetDecoration, type IMindsetFunctionConfig, type IMindsetFunctionDecoration, type IMindsetFunctionMetadata, type IMindsetFunctionParamMetadata, type IMindsetIdentity, type IMindsetMetadata, type IMindsetModuleConfig, type IMindsetModuleDecoration, type IMindsetModuleMetadata, type IOtpService, type IParamConfig, type IParamDecoration, type IPersistent, type IReceivedMessage, type IReceivedMessageItem, type IReversibleMapper, type ISendEmailRequest, type IServerConfig, type IServerProvider, type ISqliteRecord, type ISystemFunctionCallItem, type ISystemMessageItem, type ITelegramChannelConfig, type IUserConnection, type IUserData, type IUserRepository, type IchatControllerConfig, type IrunChannelProps, MINDSET_DECORATION_MINDSET, MINDSET_FUNCTION_DECORATION_FUNCTION, MINDSET_MODULE_DECORATION_MODULE, MessageContext, Mindset, MindsetMetadataStore, MindsetOperator, OpenaiChatBotAdapter, OtpService, PARAM_DECORATION_IS_OPTIONAL, PARAM_DECORATION_PARAM, Persistent, RamChatMemory, RamChatRepository, RamUserRepository, RegisterUserModule, RegisterUserWithEmailRequest, SendOneTimePasswordRequest, SqliteChatMemory, SqliteChatRepository, SqliteCrudRepository, SqliteUserRepository, TelegramChannel, TelegramChannelConfig, User, UserRepository, UserResolver, ValidateOneTimePasswordRequest, chatBot, chatController, cmd, container, inject, injectable, isOptional, mindset, mindsetFunction, mindsetModule, param, prepareChatContainer, runChannel, runServer, singleton, telegram };
636
+ declare class SqlitePersistentMapper<P extends Persistent<IPersistent>> implements IReversibleMapper<P, string> {
637
+ private ctor;
638
+ constructor(ctor: IConstructor<P>);
639
+ map(input: P): string;
640
+ rev(input: string): P;
641
+ }
642
+ declare function sqliteMapperFor<P extends Persistent<IPersistent>>(ctor: IConstructor<P>): SqlitePersistentMapper<P>;
643
+
644
+ export { AuthenticationModule, Chat, ChatBot, ChatBotAdapter, ChatBotMetadataStore, ChatItem, ChatMemory, ChatRepository, ChatResolver, CmdChannel, Container, ControllerMetadataStore, EmailService, type IChannelMetadata, type IChatBot, type IChatBotAdapter, type IChatBotMetadata, type IChatChannel, type IChatConnection, type IChatControllerMetadata, type IChatData, type IChatFunctionCall, type IChatItemData, type IChatItemType, type IChatMemory, type IChatMessage, type IChatRepository, type IChatType, type IConnectionChatMessage, type IConstructor, type ICrudRepository, type IEmailService, type IMessageContext, type IMindset, type IMindsetDecoration, type IMindsetFunctionConfig, type IMindsetFunctionDecoration, type IMindsetFunctionMetadata, type IMindsetFunctionParamMetadata, type IMindsetIdentity, type IMindsetMetadata, type IMindsetModuleConfig, type IMindsetModuleDecoration, type IMindsetModuleMetadata, type IOtpService, type IParamConfig, type IParamDecoration, type IPersistent, type IPgRecord, type IReceivedMessage, type IReceivedMessageItem, type IReversibleMapper, type ISendEmailRequest, type IServerConfig, type IServerProvider, type ISqliteRecord, type ISystemFunctionCallItem, type ISystemMessageItem, type ITelegramChannelConfig, type IUserConnection, type IUserData, type IUserRepository, type IchatControllerConfig, type IrunChannelProps, MINDSET_DECORATION_MINDSET, MINDSET_FUNCTION_DECORATION_FUNCTION, MINDSET_MODULE_DECORATION_MODULE, MessageContext, Mindset, MindsetMetadataStore, MindsetOperator, OpenaiChatBotAdapter, OtpService, PARAM_DECORATION_IS_OPTIONAL, PARAM_DECORATION_PARAM, Persistent, PgChatRepository, PgCrudRepository, PgPersistentMapper, RamChatMemory, RamChatRepository, RamUserRepository, RegisterUserModule, RegisterUserWithEmailRequest, SendOneTimePasswordRequest, SqliteChatMemory, SqliteChatRepository, SqliteCrudRepository, SqlitePersistentMapper, SqliteUserRepository, TelegramChannel, TelegramChannelConfig, User, UserRepository, UserResolver, ValidateOneTimePasswordRequest, chatBot, chatController, cmd, container, inject, injectable, isOptional, mindset, mindsetFunction, mindsetModule, param, pgMapperFor, prepareChatContainer, runChannel, runServer, singleton, sqliteMapperFor, telegram };
package/dist/src/index.js CHANGED
@@ -46,8 +46,12 @@ export { RegisterUserWithEmailRequest } from './pre-made/module/register-user/re
46
46
  export { RegisterUserModule } from './pre-made/module/register-user/RegisterUserModule.js';
47
47
  export { EmailService } from './pre-made/service/EmailService.js';
48
48
  export { OtpService } from './pre-made/service/OtpService.js';
49
+ export { PgCrudRepository } from './pre-made/repository/pg/PgCrudRepository.js';
50
+ export { PgPersistentMapper, pgMapperFor } from './pre-made/repository/pg/PgPersistentMapper.js';
51
+ export { PgChatRepository } from './pre-made/repository/pg/chat/PgChatRepository.js';
49
52
  export { SqliteChatMemory } from './pre-made/repository/sqlite/chat/SqliteChatMemory.js';
50
53
  export { SqliteChatRepository } from './pre-made/repository/sqlite/chat/SqliteChatRepository.js';
51
54
  export { SqliteUserRepository } from './pre-made/repository/sqlite/user/SqliteUserRepository.js';
52
55
  export { SqliteCrudRepository } from './pre-made/repository/sqlite/SqliteCrudRepository.js';
56
+ export { SqlitePersistentMapper, sqliteMapperFor } from './pre-made/repository/sqlite/SqlitePersistentMapper.js';
53
57
  export { Container } from './injection/Container.js';
@@ -0,0 +1,70 @@
1
+ import 'pg';
2
+
3
+ class PgCrudRepository {
4
+ pool;
5
+ tableName;
6
+ mapper;
7
+ tableIsCreated = false;
8
+ constructor(pool, tableName, mapper) {
9
+ this.pool = pool;
10
+ this.tableName = tableName;
11
+ this.mapper = mapper;
12
+ }
13
+ async find(id) {
14
+ const sql = `
15
+ SELECT id, data
16
+ FROM ${this.tableName}
17
+ WHERE id = $1
18
+ LIMIT 1
19
+ `;
20
+ await this.createTableIfNotExists();
21
+ const { rows } = await this.pool.query(sql, [id]);
22
+ if (rows.length === 0)
23
+ return null;
24
+ return this.mapper.rev(rows[0].data);
25
+ }
26
+ async findAll() {
27
+ const sql = `SELECT id, data FROM ${this.tableName}`;
28
+ await this.createTableIfNotExists();
29
+ const { rows } = await this.pool.query(sql);
30
+ return rows.map((r) => this.mapper.rev(r.data));
31
+ }
32
+ async create(item) {
33
+ const sql = `
34
+ INSERT INTO ${this.tableName}(id, data)
35
+ VALUES ($1, $2)
36
+ `;
37
+ const data = this.mapper.map(item);
38
+ await this.createTableIfNotExists();
39
+ await this.pool.query(sql, [item.getId(), data]);
40
+ }
41
+ async update(item) {
42
+ const sql = `
43
+ UPDATE ${this.tableName}
44
+ SET data = $1
45
+ WHERE id = $2
46
+ `;
47
+ const data = this.mapper.map(item);
48
+ await this.createTableIfNotExists();
49
+ await this.pool.query(sql, [data, item.getId()]);
50
+ }
51
+ async discard(item) {
52
+ item.discard();
53
+ await this.update(item);
54
+ }
55
+ async getPool() {
56
+ await this.createTableIfNotExists();
57
+ return this.pool;
58
+ }
59
+ async createTableIfNotExists() {
60
+ if (this.tableIsCreated) {
61
+ return;
62
+ }
63
+ await this.pool.query(`CREATE TABLE IF NOT EXISTS ${this.tableName} (
64
+ id TEXT PRIMARY KEY,
65
+ data JSONB NOT NULL
66
+ )`);
67
+ }
68
+ }
69
+
70
+ export { PgCrudRepository };
@@ -0,0 +1,17 @@
1
+ class PgPersistentMapper {
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 pgMapperFor(ctor) {
14
+ return new PgPersistentMapper(ctor);
15
+ }
16
+
17
+ export { PgPersistentMapper, pgMapperFor };
@@ -0,0 +1,30 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import '../../../../core/chat/repository/ram/RamChatRepository.js';
3
+ import '../../../../core/chat/repository/IChatRepository.js';
4
+ import { Chat } from '../../../../core/chat/Chat.js';
5
+ import '../../../../core/user/repository/ram/RamUserRepository.js';
6
+ import '../../../../core/user/repository/IUserRepository.js';
7
+ import { Pool } from 'pg';
8
+ import { PgCrudRepository } from '../PgCrudRepository.js';
9
+ import { pgMapperFor } from '../PgPersistentMapper.js';
10
+ import { singleton } from '../../../../injection/index.js';
11
+
12
+ let PgChatRepository = class PgChatRepository extends PgCrudRepository {
13
+ constructor(pool) {
14
+ super(pool, 'chat', pgMapperFor(Chat));
15
+ }
16
+ async findByConnection(query) {
17
+ await this.getPool();
18
+ throw new Error('Method not implemented.');
19
+ }
20
+ async findMemory(chatId) {
21
+ await this.getPool();
22
+ throw new Error('Method not implemented.');
23
+ }
24
+ };
25
+ PgChatRepository = __decorate([
26
+ singleton(),
27
+ __metadata("design:paramtypes", [Pool])
28
+ ], PgChatRepository);
29
+
30
+ export { PgChatRepository };
@@ -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 };
@@ -5,23 +5,11 @@ import '../../../../core/chat/repository/IChatRepository.js';
5
5
  import '../../../../core/user/repository/ram/RamUserRepository.js';
6
6
  import '../../../../core/user/repository/IUserRepository.js';
7
7
  import { SqliteCrudRepository } from '../SqliteCrudRepository.js';
8
+ import { sqliteMapperFor } from '../SqlitePersistentMapper.js';
8
9
 
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
10
  class SqliteChatMemory extends SqliteCrudRepository {
21
- chatId;
22
11
  constructor(chatId) {
23
- super('chat_item', SqliteChatMemory.getDbPath(chatId), chatItemSqliteMapper);
24
- this.chatId = chatId;
12
+ super('chat_item', SqliteChatMemory.getDbPath(chatId), sqliteMapperFor(ChatItem));
25
13
  }
26
14
  async findLastItems(count) {
27
15
  const allItems = await this.findAll();
@@ -5,22 +5,12 @@ import '../../../../core/user/repository/ram/RamUserRepository.js';
5
5
  import '../../../../core/user/repository/IUserRepository.js';
6
6
  import path from 'path';
7
7
  import { SqliteCrudRepository } from '../SqliteCrudRepository.js';
8
+ import { sqliteMapperFor } from '../SqlitePersistentMapper.js';
8
9
  import { SqliteChatMemory } from './SqliteChatMemory.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();
@@ -1,9 +1,7 @@
1
1
  class Persistent {
2
2
  data;
3
- originalData;
4
3
  constructor(data) {
5
4
  this.data = data;
6
- this.originalData = { ...data };
7
5
  }
8
6
  getId() {
9
7
  if (!this.data.id) {
@@ -15,7 +13,7 @@ class Persistent {
15
13
  if (!this.data.createdAt) {
16
14
  throw new Error('createdAt is required');
17
15
  }
18
- return this.data.createdAt;
16
+ return new Date(this.data.createdAt);
19
17
  }
20
18
  update(newData) {
21
19
  this.data = newData;
@@ -32,7 +30,7 @@ class Persistent {
32
30
  }
33
31
  }
34
32
  discard() {
35
- this.data.discardedAt = new Date();
33
+ this.data.discardedAt = new Date().getTime();
36
34
  }
37
35
  }
38
36
 
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.11",
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,6 +43,7 @@
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",
46
48
  "sqlite": "^5.1.1",
47
49
  "sqlite3": "^5.1.7",