@wabot-dev/framework 0.0.8 → 0.0.9

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 (24) hide show
  1. package/dist/src/ai/openia/OpenaiChatBotAdapter.js +13 -12
  2. package/dist/src/channels/cmd/CmdChannel.js +3 -3
  3. package/dist/src/chatbot/ChatBot.js +10 -10
  4. package/dist/src/chatbot/ChatBotAdapter.js +9 -13
  5. package/dist/src/core/chat/Chat.js +5 -18
  6. package/dist/src/core/chat/ChatItem.js +15 -0
  7. package/dist/src/core/chat/repository/IChatMemory.js +1 -1
  8. package/dist/src/core/chat/repository/ram/RamChatMemory.js +1 -1
  9. package/dist/src/core/user/User.js +4 -20
  10. package/dist/src/index.d.ts +97 -44
  11. package/dist/src/index.js +13 -7
  12. package/dist/src/pre-made/{modules → module}/authentication/AuthenticationModule.js +2 -2
  13. package/dist/src/pre-made/repository/sqlite/SqliteCrudRepository.js +85 -0
  14. package/dist/src/pre-made/repository/sqlite/chat/SqliteChatMemory.js +35 -0
  15. package/dist/src/pre-made/repository/sqlite/chat/SqliteChatRepository.js +40 -0
  16. package/dist/src/pre-made/repository/sqlite/user/SqliteUserRepository.js +40 -0
  17. package/dist/src/shared/Persistent.js +39 -0
  18. package/package.json +4 -5
  19. /package/dist/src/pre-made/{modules → module}/authentication/requests/SendOneTimePasswordRequest.js +0 -0
  20. /package/dist/src/pre-made/{modules → module}/authentication/requests/ValidateOneTimePasswordRequest.js +0 -0
  21. /package/dist/src/pre-made/{modules → module}/register-user/RegisterUserModule.js +0 -0
  22. /package/dist/src/pre-made/{modules → module}/register-user/requests/RegisterUserWithEmailRequest.js +0 -0
  23. /package/dist/src/pre-made/{services → service}/EmailService.js +0 -0
  24. /package/dist/src/pre-made/{services → service}/OtpService.js +0 -0
@@ -50,29 +50,30 @@ let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
50
50
  mapChatItems(chatItems) {
51
51
  const openIaInput = [];
52
52
  for (const item of chatItems) {
53
- if (item.type === 'CONNECTION_MESSAGE') {
54
- if (!item.content.text) {
53
+ const itemData = item.getData();
54
+ if (itemData.type === 'CONNECTION_MESSAGE') {
55
+ if (!itemData.content.text) {
55
56
  throw new Error('System message content is empty');
56
57
  }
57
- openIaInput.push({ role: 'user', content: item.content.text });
58
+ openIaInput.push({ role: 'user', content: itemData.content.text });
58
59
  }
59
- else if (item.type === 'BOT_MESSAGE') {
60
- if (!item.content.text) {
60
+ else if (itemData.type === 'BOT_MESSAGE') {
61
+ if (!itemData.content.text) {
61
62
  throw new Error('System message content is empty');
62
63
  }
63
- openIaInput.push({ role: 'assistant', content: item.content.text });
64
+ openIaInput.push({ role: 'assistant', content: itemData.content.text });
64
65
  }
65
- if (item.type === 'FUNCTION_CALL') {
66
+ if (itemData.type === 'FUNCTION_CALL') {
66
67
  openIaInput.push({
67
68
  type: 'function_call',
68
- call_id: item.content.id,
69
- name: item.content.name,
70
- arguments: JSON.stringify(item.content.arguments),
69
+ call_id: itemData.content.id,
70
+ name: itemData.content.name,
71
+ arguments: JSON.stringify(itemData.content.arguments),
71
72
  });
72
73
  openIaInput.push({
73
74
  type: 'function_call_output',
74
- call_id: item.content.id,
75
- output: item.content.result,
75
+ call_id: itemData.content.id,
76
+ output: itemData.content.result,
76
77
  });
77
78
  }
78
79
  }
@@ -3,7 +3,6 @@ import { ChatResolver } from '../../controller/channel/ChatResolver.js';
3
3
  import { UserResolver } from '../../controller/channel/UserResolver.js';
4
4
  import { injectable } from '../../injection/index.js';
5
5
  import '../../controller/metadata/ControllerMetadataStore.js';
6
- import { v4 } from 'uuid';
7
6
  import '../../core/chat/repository/ram/RamChatRepository.js';
8
7
  import '../../core/chat/repository/IChatRepository.js';
9
8
  import '../../core/user/repository/ram/RamUserRepository.js';
@@ -11,8 +10,8 @@ import '../../core/user/repository/IUserRepository.js';
11
10
  import * as readline from 'readline';
12
11
 
13
12
  var CmdChannel_1;
14
- const chatId = v4();
15
- const userId = v4();
13
+ const chatId = 'cmd';
14
+ const userId = 'cmd';
16
15
  let CmdChannel = CmdChannel_1 = class CmdChannel {
17
16
  chatResolver;
18
17
  userResolver;
@@ -30,6 +29,7 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
30
29
  }
31
30
  connect() {
32
31
  this.rl.on('line', async (input) => {
32
+ debugger;
33
33
  const trimmedInput = input.trim();
34
34
  if (!trimmedInput) {
35
35
  this.rl.prompt();
@@ -1,11 +1,11 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
+ import { ChatItem } from '../core/chat/ChatItem.js';
2
3
  import { ChatMemory } from '../core/chat/repository/IChatMemory.js';
3
4
  import '../core/chat/repository/ram/RamChatRepository.js';
4
5
  import '../core/chat/repository/IChatRepository.js';
5
6
  import '../core/user/repository/ram/RamUserRepository.js';
6
7
  import '../core/user/repository/IUserRepository.js';
7
8
  import { injectable } from '../injection/index.js';
8
- import { v4 } from 'uuid';
9
9
  import { ChatBotAdapter } from './ChatBotAdapter.js';
10
10
 
11
11
  let ChatBot = class ChatBot {
@@ -17,13 +17,11 @@ let ChatBot = class ChatBot {
17
17
  this.memory = memory;
18
18
  }
19
19
  async sendMessage(message, callback) {
20
- const newChatItem = {
21
- id: v4(),
22
- createdAt: new Date(),
20
+ const newChatItem = new ChatItem({
23
21
  type: 'CONNECTION_MESSAGE',
24
22
  content: message,
25
- };
26
- await this.memory.saveItem(newChatItem);
23
+ });
24
+ await this.memory.create(newChatItem);
27
25
  this.processLoop(callback);
28
26
  }
29
27
  async processLoop(callback) {
@@ -32,13 +30,15 @@ let ChatBot = class ChatBot {
32
30
  return;
33
31
  }
34
32
  const lastChatItem = prevChatItems[prevChatItems.length - 1];
35
- if (lastChatItem.type === 'BOT_MESSAGE') {
33
+ const lastItemType = lastChatItem.getType();
34
+ if (lastItemType === 'BOT_MESSAGE') {
36
35
  return;
37
36
  }
38
37
  const newChatItem = await this.adapter.generateNextChatItem(prevChatItems);
39
- await this.memory.saveItem(newChatItem);
40
- if (newChatItem.type === 'BOT_MESSAGE') {
41
- callback(newChatItem.content);
38
+ await this.memory.create(newChatItem);
39
+ const newChatItemData = newChatItem.getData();
40
+ if (newChatItemData.type === 'BOT_MESSAGE') {
41
+ callback(newChatItemData.content);
42
42
  return;
43
43
  }
44
44
  this.processLoop(callback);
@@ -1,12 +1,12 @@
1
- import 'reflect-metadata';
2
- import '../injection/index.js';
3
- import '../mindset/metadata/MindsetMetadataStore.js';
4
- import '../mindset/MindsetOperator.js';
5
- import { v4 } from 'uuid';
1
+ import { ChatItem } from '../core/chat/ChatItem.js';
6
2
  import '../core/chat/repository/ram/RamChatRepository.js';
7
3
  import '../core/chat/repository/IChatRepository.js';
8
4
  import '../core/user/repository/ram/RamUserRepository.js';
9
5
  import '../core/user/repository/IUserRepository.js';
6
+ import 'reflect-metadata';
7
+ import '../injection/index.js';
8
+ import '../mindset/metadata/MindsetMetadataStore.js';
9
+ import '../mindset/MindsetOperator.js';
10
10
 
11
11
  class ChatBotAdapter {
12
12
  mindset;
@@ -51,22 +51,18 @@ class ChatBotAdapter {
51
51
  }
52
52
  async buildBotMessageItem(text) {
53
53
  const senderName = (await this.mindset.identity()).name;
54
- const newBotMessage = {
55
- id: v4(),
56
- createdAt: new Date(),
54
+ const newBotMessage = new ChatItem({
57
55
  type: 'BOT_MESSAGE',
58
56
  content: {
59
57
  senderName,
60
58
  text,
61
59
  },
62
- };
60
+ });
63
61
  return newBotMessage;
64
62
  }
65
63
  async buildFunctionCallItem(id, functionName, functionArguments) {
66
64
  const functionResult = await this.mindset.callFunction(functionName, functionArguments);
67
- const newFunctionCall = {
68
- id: v4(),
69
- createdAt: new Date(),
65
+ const newFunctionCall = new ChatItem({
70
66
  type: 'FUNCTION_CALL',
71
67
  content: {
72
68
  id,
@@ -74,7 +70,7 @@ class ChatBotAdapter {
74
70
  arguments: functionArguments,
75
71
  result: functionResult,
76
72
  },
77
- };
73
+ });
78
74
  return newFunctionCall;
79
75
  }
80
76
  }
@@ -1,7 +1,8 @@
1
- class Chat {
2
- data;
1
+ import { Persistent } from '../../shared/Persistent.js';
2
+
3
+ class Chat extends Persistent {
3
4
  constructor(data) {
4
- this.data = data;
5
+ super(data);
5
6
  }
6
7
  isPrivate() {
7
8
  return this.data.type === 'PRIVATE';
@@ -27,22 +28,8 @@ class Chat {
27
28
  throw new Error('GROUP chat should have exactly one connection');
28
29
  }
29
30
  }
30
- getId() {
31
- if (!this.data.id) {
32
- throw new Error('Chat ID is required');
33
- }
34
- return this.data.id;
35
- }
36
- wasCreated() {
37
- return !!this.data.createdAt || !!this.data.id;
38
- }
39
31
  validate() {
40
- if (!this.data.id) {
41
- throw new Error('Chat ID is required');
42
- }
43
- if (!this.data.createdAt) {
44
- throw new Error('Chat createdAt is required');
45
- }
32
+ super.validate();
46
33
  if (this.data.type === 'PRIVATE') {
47
34
  this.validatePrivateChat();
48
35
  }
@@ -0,0 +1,15 @@
1
+ import { Persistent } from '../../shared/Persistent.js';
2
+
3
+ class ChatItem extends Persistent {
4
+ getType() {
5
+ return this.data.type;
6
+ }
7
+ getContent() {
8
+ return this.data.content;
9
+ }
10
+ getData() {
11
+ return this.data;
12
+ }
13
+ }
14
+
15
+ export { ChatItem };
@@ -2,7 +2,7 @@ class ChatMemory {
2
2
  findLastItems(count) {
3
3
  throw new Error('Method not implemented.');
4
4
  }
5
- saveItem(item) {
5
+ create(item) {
6
6
  throw new Error('Method not implemented.');
7
7
  }
8
8
  }
@@ -3,7 +3,7 @@ class RamChatMemory {
3
3
  async findLastItems(count) {
4
4
  return this.memory.slice(-count);
5
5
  }
6
- async saveItem(item) {
6
+ async create(item) {
7
7
  this.memory.push(item);
8
8
  }
9
9
  async clearMemory() {
@@ -1,13 +1,8 @@
1
- class User {
2
- data;
1
+ import { Persistent } from '../../shared/Persistent.js';
2
+
3
+ class User extends Persistent {
3
4
  constructor(data) {
4
- this.data = data;
5
- }
6
- getId() {
7
- if (!this.data.id) {
8
- throw new Error('User have not ID');
9
- }
10
- return this.data.id;
5
+ super(data);
11
6
  }
12
7
  hasConnection(connection) {
13
8
  for (const con of this.data.connections) {
@@ -17,17 +12,6 @@ class User {
17
12
  }
18
13
  return false;
19
14
  }
20
- wasCreated() {
21
- return !!this.data.createdAt || !!this.data.id;
22
- }
23
- validate() {
24
- if (!this.data.id) {
25
- throw new Error('User ID is required');
26
- }
27
- if (!this.data.createdAt) {
28
- throw new Error('User createdAt is required');
29
- }
30
- }
31
15
  getValue(key) {
32
16
  return this.data.keyValueData[key];
33
17
  }
@@ -4,6 +4,8 @@ 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 { Database } from 'sqlite';
8
+ import sqlite3 from 'sqlite3';
7
9
 
8
10
  interface IMindsetFunctionConfig {
9
11
  description: string;
@@ -23,6 +25,28 @@ interface IMindsetFunctionDecoration {
23
25
 
24
26
  type IConstructor<T> = new (...args: any[]) => T;
25
27
 
28
+ interface IReversibleMapper<T1, T2> {
29
+ map(input: T1): T2;
30
+ rev(input: T2): T1;
31
+ }
32
+
33
+ interface IPersistent {
34
+ id?: string;
35
+ createdAt?: Date | null;
36
+ discardedAt?: Date | null;
37
+ }
38
+ declare class Persistent<D extends IPersistent> {
39
+ protected data: D;
40
+ private originalData;
41
+ constructor(data: D);
42
+ getId(): string;
43
+ getCreatedAt(): Date;
44
+ update(newData: D): void;
45
+ wasCreated(): boolean;
46
+ validate(): void;
47
+ discard(): void;
48
+ }
49
+
26
50
  interface IMindsetIdentity {
27
51
  name: string;
28
52
  language: string;
@@ -192,22 +216,17 @@ interface IChatConnection {
192
216
  channelName: string;
193
217
  id: string;
194
218
  }
195
- interface IChatData {
196
- id?: string;
197
- createdAt?: Date;
219
+ interface IChatData extends IPersistent {
198
220
  type: IChatType;
199
221
  connections: IChatConnection[];
200
222
  }
201
- declare class Chat {
202
- private data;
223
+ declare class Chat extends Persistent<IChatData> {
203
224
  constructor(data: IChatData);
204
225
  isPrivate(): boolean;
205
226
  isGroup(): boolean;
206
227
  hasConnection(connection: IChatConnection): boolean;
207
228
  private validatePrivateChat;
208
229
  private validateGroupChat;
209
- getId(): string;
210
- wasCreated(): boolean;
211
230
  validate(): void;
212
231
  }
213
232
 
@@ -221,22 +240,16 @@ interface IUserConnection {
221
240
  channelName: string;
222
241
  id: string;
223
242
  }
224
- interface IUserData {
225
- id?: string;
226
- createdAt?: Date;
243
+ interface IUserData extends IPersistent {
227
244
  shortName: string;
228
245
  connections: IUserConnection[];
229
246
  keyValueData: {
230
247
  [key: string]: string;
231
248
  };
232
249
  }
233
- declare class User {
234
- private data;
250
+ declare class User extends Persistent<IUserData> {
235
251
  constructor(data: IUserData);
236
- getId(): string;
237
252
  hasConnection(connection: IUserConnection): boolean;
238
- wasCreated(): boolean;
239
- validate(): void;
240
253
  getValue(key: string): string;
241
254
  setValue(key: string, value: string): void;
242
255
  addConnection(connection: IUserConnection): void;
@@ -273,25 +286,30 @@ type ISystemFunctionCallItem = {
273
286
  type: 'FUNCTION_CALL';
274
287
  content: IChatFunctionCall;
275
288
  };
276
- type IChatItem = {
277
- id: string;
278
- createdAt: Date;
289
+ type IChatItemData = {
290
+ id?: string;
291
+ createdAt?: Date;
279
292
  } & (ISystemMessageItem | IReceivedMessageItem | ISystemFunctionCallItem);
280
- type IChatItemType = IChatItem['type'];
293
+ type IChatItemType = IChatItemData['type'];
294
+ declare class ChatItem extends Persistent<IChatItemData> {
295
+ getType(): "BOT_MESSAGE" | "CONNECTION_MESSAGE" | "FUNCTION_CALL";
296
+ getContent(): IChatMessage | IConnectionChatMessage | IChatFunctionCall;
297
+ getData(): IChatItemData;
298
+ }
281
299
 
282
300
  interface IChatMemory {
283
- findLastItems(count: number): Promise<IChatItem[]>;
284
- saveItem(item: IChatItem): Promise<void>;
301
+ findLastItems(count: number): Promise<ChatItem[]>;
302
+ create(item: ChatItem): Promise<void>;
285
303
  }
286
304
  declare class ChatMemory implements IChatMemory {
287
- findLastItems(count: number): Promise<IChatItem[]>;
288
- saveItem(item: IChatItem): Promise<void>;
305
+ findLastItems(count: number): Promise<ChatItem[]>;
306
+ create(item: ChatItem): Promise<void>;
289
307
  }
290
308
 
291
309
  declare class RamChatMemory implements IChatMemory {
292
310
  private memory;
293
- findLastItems(count: number): Promise<IChatItem[]>;
294
- saveItem(item: IChatItem): Promise<void>;
311
+ findLastItems(count: number): Promise<ChatItem[]>;
312
+ create(item: ChatItem): Promise<void>;
295
313
  clearMemory(): Promise<void>;
296
314
  }
297
315
 
@@ -346,28 +364,15 @@ declare class MessageContext implements IMessageContext {
346
364
  }
347
365
 
348
366
  interface IChatBotAdapter {
349
- generateNextChatItem(chatItems: IChatItem[]): Promise<IChatItem>;
367
+ generateNextChatItem(chatItems: ChatItem[]): Promise<ChatItem>;
350
368
  }
351
369
  declare class ChatBotAdapter implements IChatBotAdapter {
352
370
  protected mindset: MindsetOperator;
353
371
  constructor(mindset: MindsetOperator);
354
- generateNextChatItem(chatItems: IChatItem[]): Promise<IChatItem>;
372
+ generateNextChatItem(chatItems: ChatItem[]): Promise<ChatItem>;
355
373
  protected systemPrompt(): Promise<string>;
356
- protected buildBotMessageItem(text: string): Promise<{
357
- id: string;
358
- createdAt: Date;
359
- } & ISystemMessageItem>;
360
- protected buildFunctionCallItem(id: string, functionName: string, functionArguments: string): Promise<{
361
- readonly id: string;
362
- readonly createdAt: Date;
363
- readonly type: "FUNCTION_CALL";
364
- readonly content: {
365
- readonly id: string;
366
- readonly name: string;
367
- readonly arguments: string;
368
- readonly result: string;
369
- };
370
- }>;
374
+ protected buildBotMessageItem(text: string): Promise<ChatItem>;
375
+ protected buildFunctionCallItem(id: string, functionName: string, functionArguments: string): Promise<ChatItem>;
371
376
  }
372
377
 
373
378
  interface IChatBot {
@@ -386,7 +391,7 @@ declare class OpenaiChatBotAdapter extends ChatBotAdapter {
386
391
  private openai;
387
392
  private model;
388
393
  constructor(mindset: MindsetOperator);
389
- generateNextChatItem(chatItems: IChatItem[]): Promise<IChatItem>;
394
+ generateNextChatItem(chatItems: ChatItem[]): Promise<ChatItem>;
390
395
  private mapChatItems;
391
396
  }
392
397
 
@@ -547,4 +552,52 @@ declare class RegisterUserModule {
547
552
  registerUserWithEmail(request: RegisterUserWithEmailRequest): Promise<string>;
548
553
  }
549
554
 
550
- export { AuthenticationModule, Chat, ChatBot, ChatBotAdapter, ChatBotMetadataStore, 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 IChatItem, type IChatItemType, type IChatMemory, type IChatMessage, type IChatRepository, type IChatType, type IConnectionChatMessage, type IConstructor, 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 IReceivedMessage, type IReceivedMessageItem, type ISendEmailRequest, type IServerConfig, type IServerProvider, 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, RamChatMemory, RamChatRepository, RamUserRepository, RegisterUserModule, RegisterUserWithEmailRequest, SendOneTimePasswordRequest, TelegramChannel, TelegramChannelConfig, User, UserRepository, UserResolver, ValidateOneTimePasswordRequest, chatBot, chatController, cmd, container, inject, injectable, isOptional, mindset, mindsetFunction, mindsetModule, param, prepareChatContainer, runChannel, runServer, singleton, telegram };
555
+ interface ICrudRepository<T> {
556
+ find(id: string): Promise<T | null>;
557
+ findAll(id: string): Promise<T[]>;
558
+ create(item: T): Promise<void>;
559
+ update(item: T): Promise<void>;
560
+ discard(item: T): Promise<void>;
561
+ }
562
+
563
+ declare class SqliteCrudRepository<P extends Persistent<IPersistent>> implements ICrudRepository<P> {
564
+ private table;
565
+ private dbPath;
566
+ private mapper;
567
+ private tableCreated;
568
+ constructor(table: string, dbPath: string, mapper: IReversibleMapper<P, string>);
569
+ find(id: string): Promise<P | null>;
570
+ findAll(): Promise<P[]>;
571
+ create(item: P): Promise<void>;
572
+ update(item: P): Promise<void>;
573
+ discard(item: P): Promise<void>;
574
+ protected getDb(): Promise<Database<sqlite3.Database, sqlite3.Statement>>;
575
+ protected createTable(db: Database<sqlite3.Database, sqlite3.Statement>): Promise<void>;
576
+ }
577
+
578
+ declare class SqliteChatMemory extends SqliteCrudRepository<ChatItem> implements IChatMemory {
579
+ private chatId;
580
+ constructor(chatId: string);
581
+ findLastItems(count: number): Promise<ChatItem[]>;
582
+ static getDbPath(chatId: string): string;
583
+ }
584
+
585
+ declare class SqliteChatRepository extends SqliteCrudRepository<Chat> implements IChatRepository {
586
+ constructor();
587
+ findByConnection(query: IChatConnection): Promise<Chat | null>;
588
+ findMemory(chatId: string): Promise<IChatMemory | null>;
589
+ static getDbPath(): string;
590
+ }
591
+
592
+ declare class SqliteUserRepository extends SqliteCrudRepository<User> implements IUserRepository {
593
+ constructor();
594
+ findByConnection(query: IUserConnection): Promise<User | null>;
595
+ static getDbPath(): string;
596
+ }
597
+
598
+ interface ISqliteRecord {
599
+ id: string;
600
+ data: string;
601
+ }
602
+
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 };
package/dist/src/index.js CHANGED
@@ -10,6 +10,7 @@ export { mindsetFunction } from './mindset/metadata/functions/@mindsetFunction.j
10
10
  export { MINDSET_FUNCTION_DECORATION_FUNCTION } from './mindset/metadata/functions/decoratorNames.js';
11
11
  export { mindset } from './mindset/metadata/mindsets/@mindset.js';
12
12
  export { MINDSET_DECORATION_MINDSET } from './mindset/metadata/mindsets/decoratorNames.js';
13
+ export { Persistent } from './shared/Persistent.js';
13
14
  export { mindsetModule } from './mindset/metadata/modules/@mindsetModule.js';
14
15
  export { MINDSET_MODULE_DECORATION_MODULE } from './mindset/metadata/modules/decoratorNames.js';
15
16
  export { isOptional } from './mindset/metadata/params/@isOptional.js';
@@ -25,6 +26,7 @@ export { RamChatRepository } from './core/chat/repository/ram/RamChatRepository.
25
26
  export { ChatMemory } from './core/chat/repository/IChatMemory.js';
26
27
  export { ChatRepository } from './core/chat/repository/IChatRepository.js';
27
28
  export { Chat } from './core/chat/Chat.js';
29
+ export { ChatItem } from './core/chat/ChatItem.js';
28
30
  export { User } from './core/user/User.js';
29
31
  export { RamUserRepository } from './core/user/repository/ram/RamUserRepository.js';
30
32
  export { UserRepository } from './core/user/repository/IUserRepository.js';
@@ -37,11 +39,15 @@ export { container, inject, injectable, singleton } from './injection/index.js';
37
39
  export { prepareChatContainer } from './server/prepareChatContainer.js';
38
40
  export { runChannel } from './server/runChannel.js';
39
41
  export { runServer } from './server/runServer.js';
40
- export { SendOneTimePasswordRequest } from './pre-made/modules/authentication/requests/SendOneTimePasswordRequest.js';
41
- export { ValidateOneTimePasswordRequest } from './pre-made/modules/authentication/requests/ValidateOneTimePasswordRequest.js';
42
- export { AuthenticationModule } from './pre-made/modules/authentication/AuthenticationModule.js';
43
- export { RegisterUserWithEmailRequest } from './pre-made/modules/register-user/requests/RegisterUserWithEmailRequest.js';
44
- export { RegisterUserModule } from './pre-made/modules/register-user/RegisterUserModule.js';
45
- export { EmailService } from './pre-made/services/EmailService.js';
46
- export { OtpService } from './pre-made/services/OtpService.js';
42
+ export { SendOneTimePasswordRequest } from './pre-made/module/authentication/requests/SendOneTimePasswordRequest.js';
43
+ export { ValidateOneTimePasswordRequest } from './pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js';
44
+ export { AuthenticationModule } from './pre-made/module/authentication/AuthenticationModule.js';
45
+ export { RegisterUserWithEmailRequest } from './pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js';
46
+ export { RegisterUserModule } from './pre-made/module/register-user/RegisterUserModule.js';
47
+ export { EmailService } from './pre-made/service/EmailService.js';
48
+ export { OtpService } from './pre-made/service/OtpService.js';
49
+ export { SqliteChatMemory } from './pre-made/repository/sqlite/chat/SqliteChatMemory.js';
50
+ export { SqliteChatRepository } from './pre-made/repository/sqlite/chat/SqliteChatRepository.js';
51
+ export { SqliteUserRepository } from './pre-made/repository/sqlite/user/SqliteUserRepository.js';
52
+ export { SqliteCrudRepository } from './pre-made/repository/sqlite/SqliteCrudRepository.js';
47
53
  export { Container } from './injection/Container.js';
@@ -10,8 +10,8 @@ import '../../../injection/index.js';
10
10
  import '../../../mindset/metadata/MindsetMetadataStore.js';
11
11
  import { mindsetModule } from '../../../mindset/metadata/modules/@mindsetModule.js';
12
12
  import '../../../mindset/MindsetOperator.js';
13
- import { EmailService } from '../../services/EmailService.js';
14
- import { OtpService } from '../../services/OtpService.js';
13
+ import { EmailService } from '../../service/EmailService.js';
14
+ import { OtpService } from '../../service/OtpService.js';
15
15
  import { SendOneTimePasswordRequest } from './requests/SendOneTimePasswordRequest.js';
16
16
  import { ValidateOneTimePasswordRequest } from './requests/ValidateOneTimePasswordRequest.js';
17
17
 
@@ -0,0 +1,85 @@
1
+ import { promises } from 'fs';
2
+ import path from 'path';
3
+ import { open } from 'sqlite';
4
+ import sqlite3 from 'sqlite3';
5
+ import { v4 } from 'uuid';
6
+
7
+ class SqliteCrudRepository {
8
+ table;
9
+ dbPath;
10
+ mapper;
11
+ tableCreated = false;
12
+ constructor(table, dbPath, mapper) {
13
+ this.table = table;
14
+ this.dbPath = dbPath;
15
+ this.mapper = mapper;
16
+ }
17
+ async find(id) {
18
+ const db = await this.getDb();
19
+ const result = await db.all(`SELECT id, data FROM ${this.table} WHERE id=?`, [
20
+ id,
21
+ ]);
22
+ db.close();
23
+ if (result.length < 1) {
24
+ return null;
25
+ }
26
+ const item = this.mapper.rev(result[0].data);
27
+ return item;
28
+ }
29
+ async findAll() {
30
+ const db = await this.getDb();
31
+ const result = await db.all(`SELECT id, data FROM ${this.table}`);
32
+ db.close();
33
+ const items = result.map((item) => this.mapper.rev(item.data));
34
+ return items;
35
+ }
36
+ async create(item) {
37
+ if (item.wasCreated()) {
38
+ throw new Error('Item already created');
39
+ }
40
+ item['data'].id = v4();
41
+ item['data'].createdAt = new Date();
42
+ item.validate();
43
+ const db = await this.getDb();
44
+ await db.run(`INSERT INTO ${this.table} VALUES (?, ?, ?)`, [
45
+ item.getId(),
46
+ item.getCreatedAt().getTime(),
47
+ this.mapper.map(item),
48
+ ]);
49
+ db.close();
50
+ }
51
+ async update(item) {
52
+ if (!item.wasCreated()) {
53
+ throw new Error('Item is not created');
54
+ }
55
+ item.validate();
56
+ const db = await this.getDb();
57
+ await db.run(`UPDATE ${this.table} SET data=? WHERE id=?`, [
58
+ this.mapper.map(item),
59
+ item.getId(),
60
+ ]);
61
+ db.close();
62
+ }
63
+ async discard(item) {
64
+ item.discard();
65
+ await this.update(item);
66
+ }
67
+ async getDb() {
68
+ const dbDirPath = path.dirname(this.dbPath);
69
+ await promises.mkdir(dbDirPath, { recursive: true });
70
+ const db = await open({
71
+ filename: this.dbPath,
72
+ driver: sqlite3.Database,
73
+ });
74
+ if (!this.tableCreated) {
75
+ this.tableCreated = true;
76
+ await this.createTable(db);
77
+ }
78
+ return db;
79
+ }
80
+ async createTable(db) {
81
+ await db.exec(`CREATE TABLE IF NOT EXISTS ${this.table}(id TEXT, created_at INTEGER, data TEXT)`);
82
+ }
83
+ }
84
+
85
+ export { SqliteCrudRepository };
@@ -0,0 +1,35 @@
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 };
@@ -0,0 +1,40 @@
1
+ import '../../../../core/chat/repository/ram/RamChatRepository.js';
2
+ import '../../../../core/chat/repository/IChatRepository.js';
3
+ import { Chat } from '../../../../core/chat/Chat.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
+ import { SqliteChatMemory } from './SqliteChatMemory.js';
9
+
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
+ class SqliteChatRepository extends SqliteCrudRepository {
22
+ constructor() {
23
+ super('chat', SqliteChatRepository.getDbPath(), chatSqliteMapper);
24
+ }
25
+ async findByConnection(query) {
26
+ const allChats = await this.findAll();
27
+ return allChats.find((chat) => chat.hasConnection(query)) ?? null;
28
+ }
29
+ async findMemory(chatId) {
30
+ const chat = await this.find(chatId);
31
+ if (!chat)
32
+ return null;
33
+ return new SqliteChatMemory(chatId);
34
+ }
35
+ static getDbPath() {
36
+ return path.join(process.cwd(), '.sqlite', 'chat.db');
37
+ }
38
+ }
39
+
40
+ export { SqliteChatRepository };
@@ -0,0 +1,40 @@
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 '../../../module/authentication/requests/SendOneTimePasswordRequest.js';
7
+ import '../../../module/authentication/requests/ValidateOneTimePasswordRequest.js';
8
+ import '../../../module/authentication/AuthenticationModule.js';
9
+ import '../../../module/register-user/requests/RegisterUserWithEmailRequest.js';
10
+ import '../../../module/register-user/RegisterUserModule.js';
11
+ import '../../../service/EmailService.js';
12
+ import '../../../service/OtpService.js';
13
+ import path from 'path';
14
+ import { SqliteCrudRepository } from '../SqliteCrudRepository.js';
15
+
16
+ const userSqliteMapper = {
17
+ map(input) {
18
+ return JSON.stringify(input['data']);
19
+ },
20
+ rev(input) {
21
+ const data = JSON.parse(input);
22
+ data.createdAt = new Date(data.createdAt);
23
+ data.discardedAt = data.discardedAt && new Date(data.discardedAt);
24
+ return new User(data);
25
+ },
26
+ };
27
+ class SqliteUserRepository extends SqliteCrudRepository {
28
+ constructor() {
29
+ super('user', SqliteUserRepository.getDbPath(), userSqliteMapper);
30
+ }
31
+ async findByConnection(query) {
32
+ const allItems = await this.findAll();
33
+ return allItems.find((item) => item.hasConnection(query)) ?? null;
34
+ }
35
+ static getDbPath() {
36
+ return path.join(process.cwd(), '.sqlite', 'user.db');
37
+ }
38
+ }
39
+
40
+ export { SqliteUserRepository };
@@ -0,0 +1,39 @@
1
+ class Persistent {
2
+ data;
3
+ originalData;
4
+ constructor(data) {
5
+ this.data = data;
6
+ this.originalData = { ...data };
7
+ }
8
+ getId() {
9
+ if (!this.data.id) {
10
+ throw new Error('id is required');
11
+ }
12
+ return this.data.id;
13
+ }
14
+ getCreatedAt() {
15
+ if (!this.data.createdAt) {
16
+ throw new Error('createdAt is required');
17
+ }
18
+ return this.data.createdAt;
19
+ }
20
+ update(newData) {
21
+ this.data = newData;
22
+ }
23
+ wasCreated() {
24
+ return !!this.data.createdAt || !!this.data.id;
25
+ }
26
+ validate() {
27
+ if (!this.data.id) {
28
+ throw new Error('id is required');
29
+ }
30
+ if (!this.data.createdAt) {
31
+ throw new Error('createdAt is required');
32
+ }
33
+ }
34
+ discard() {
35
+ this.data.discardedAt = new Date();
36
+ }
37
+ }
38
+
39
+ export { Persistent };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
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
+ "@yucacodes/ts": "^0.0.4",
38
39
  "class-transformer": "^0.5.1",
39
40
  "class-validator": "^0.14.1",
40
41
  "dotenv": "^16.5.0",
@@ -43,11 +44,9 @@
43
44
  "openai": "^4.93.0",
44
45
  "reflect-metadata": "^0.2.2",
45
46
  "sqlite": "^5.1.1",
46
- "sqlite3": "npm:sqlite3-offline-next@^5.0.3",
47
- "sqlite3-offline-next": "^5.0.3",
47
+ "sqlite3": "^5.1.7",
48
48
  "tslib": "^2.8.1",
49
49
  "tsyringe": "^4.9.1",
50
- "uuid": "^11.1.0",
51
- "@yucacodes/ts": "^0.0.4"
50
+ "uuid": "^11.1.0"
52
51
  }
53
52
  }