@wabot-dev/framework 0.1.0-beta.64 → 0.1.0-beta.66

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.
@@ -1,6 +1,7 @@
1
1
  import { container } from '../../../core/injection/index.js';
2
2
  import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
3
3
  import '../../../feature/chat-controller/ChatResolver.js';
4
+ import '../../../core/auth/Auth.js';
4
5
  import '../../../feature/chat-bot/ChatBot.js';
5
6
  import 'uuid';
6
7
  import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
@@ -1,6 +1,7 @@
1
1
  import { container } from '../../../core/injection/index.js';
2
2
  import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
3
3
  import '../../../feature/chat-controller/ChatResolver.js';
4
+ import '../../../core/auth/Auth.js';
4
5
  import '../../../feature/chat-bot/ChatBot.js';
5
6
  import 'uuid';
6
7
  import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
@@ -46,6 +46,10 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
46
46
  reply: (message) => {
47
47
  socket.emit(this.config.channel, message);
48
48
  },
49
+ authInfo: socket.data.authInfo,
50
+ setAuthInfo: (authInfo) => {
51
+ socket.data.authInfo = authInfo;
52
+ },
49
53
  });
50
54
  });
51
55
  });
@@ -1,6 +1,7 @@
1
1
  import { container } from '../../../core/injection/index.js';
2
2
  import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
3
3
  import '../../../feature/chat-controller/ChatResolver.js';
4
+ import '../../../core/auth/Auth.js';
4
5
  import '../../../feature/chat-bot/ChatBot.js';
5
6
  import 'uuid';
6
7
  import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
@@ -2,6 +2,7 @@ import { container } from '../../../core/injection/index.js';
2
2
  import { WhatsappChannelConfig } from './WhatsAppChannelConfig.js';
3
3
  import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
4
4
  import '../../../feature/chat-controller/ChatResolver.js';
5
+ import '../../../core/auth/Auth.js';
5
6
  import '../../../feature/chat-bot/ChatBot.js';
6
7
  import 'uuid';
7
8
  import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
@@ -4,6 +4,7 @@ import { WhatsAppSender } from '../WhatsAppSender.js';
4
4
  import { singleton } from '../../../../core/injection/index.js';
5
5
  import '../../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
6
6
  import { ChatResolver } from '../../../../feature/chat-controller/ChatResolver.js';
7
+ import '../../../../core/auth/Auth.js';
7
8
  import '../../../../feature/chat-bot/ChatBot.js';
8
9
  import { ChatRepository } from '../../../../feature/chat-bot/ChatRepository.js';
9
10
  import 'uuid';
@@ -8,6 +8,7 @@ import 'uuid';
8
8
  import '../../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
9
9
  import '../../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
10
10
  import { ChatResolver } from '../../../../feature/chat-controller/ChatResolver.js';
11
+ import '../../../../core/auth/Auth.js';
11
12
  import 'reflect-metadata';
12
13
  import '../../../../feature/mindset/metadata/MindsetMetadataStore.js';
13
14
  import '../../../../feature/mindset/MindsetOperator.js';
@@ -1,3 +1,4 @@
1
+ import { Auth } from '../../core/auth/Auth.js';
1
2
  import { container } from '../../core/injection/index.js';
2
3
  import { Chat } from '../chat-bot/Chat.js';
3
4
  import { ChatBot } from '../chat-bot/ChatBot.js';
@@ -23,6 +24,10 @@ async function prepareChatContainer(container, messageContext, mindsetCtor) {
23
24
  throw new Error('Not found Chat Memory for Chat with Id=' + messageContext.chat.id);
24
25
  }
25
26
  chatContainer.registerInstance(ChatMemory, chatMemory);
27
+ if (messageContext.authInfo) {
28
+ const auth = chatContainer.resolve(Auth);
29
+ auth.assign(messageContext.authInfo);
30
+ }
26
31
  const chatBotMetadataStore = container.resolve(ChatBotMetadataStore);
27
32
  const chatBots = chatBotMetadataStore.getChatBotsMetadata();
28
33
  for (const chatBotMetadata of chatBots) {
@@ -55,9 +60,7 @@ function runChatControllers(controllers) {
55
60
  const chat = await chatResolver.resolve(channelMessage.chatConnection);
56
61
  const chatContainer = await prepareChatContainer(channelContainer, {
57
62
  chat,
58
- message: channelMessage.message,
59
- reply: channelMessage.reply,
60
- });
63
+ message: channelMessage.message});
61
64
  const chatController = chatContainer.resolve(channelMetadata.controllerConstructor);
62
65
  chatController[channelMetadata.functionName](channelMessage);
63
66
  });
@@ -111,13 +111,29 @@ let MindsetOperator = class MindsetOperator {
111
111
  if (!response) {
112
112
  return 'success';
113
113
  }
114
- return this.functionResponseToString(response);
114
+ return await this.functionResponseToString(response);
115
115
  }
116
116
  catch (error) {
117
- return `Error: ${error}`;
117
+ return await this.functionErrorToString(error);
118
118
  }
119
119
  }
120
- functionResponseToString(response) {
120
+ async functionResponseToString(response) {
121
+ if (response instanceof Response) {
122
+ const contentType = response.headers.get('Content-Type') || '';
123
+ let body;
124
+ try {
125
+ body = contentType.includes('application/json')
126
+ ? await response.json()
127
+ : await response.text();
128
+ }
129
+ catch {
130
+ body = { message: 'Unable to parse error body' };
131
+ }
132
+ return JSON.stringify({
133
+ httpCode: response.status,
134
+ body: typeof body === 'object' ? body : { message: body },
135
+ });
136
+ }
121
137
  const type = typeof response;
122
138
  if (type === 'string') {
123
139
  return response;
@@ -129,6 +145,22 @@ let MindsetOperator = class MindsetOperator {
129
145
  return JSON.stringify(response);
130
146
  }
131
147
  }
148
+ async functionErrorToString(error) {
149
+ if (error?.response && typeof error.response === 'object' && error.response.status) {
150
+ const { status, data } = error.response;
151
+ return JSON.stringify({
152
+ httpCode: status,
153
+ body: typeof data === 'object' ? data : { message: data?.toString?.() || 'Unknown error' },
154
+ });
155
+ }
156
+ if (error instanceof Response) {
157
+ return await this.functionResponseToString(error);
158
+ }
159
+ if (error?.message) {
160
+ return error.message;
161
+ }
162
+ return 'Unknown error occurred';
163
+ }
132
164
  };
133
165
  MindsetOperator = __decorate([
134
166
  injectable(),
@@ -566,7 +566,8 @@ declare class MindsetOperator implements IMindset {
566
566
  protected tool(fn: IMindsetFunctionMetadata, module: IMindsetModuleMetadata): IMindsetTool;
567
567
  protected toolParameter(param: IMindsetFunctionParamMetadata): IMindsetToolParameter;
568
568
  callFunction(name: string, params: string): Promise<string>;
569
- functionResponseToString(response: any): string;
569
+ functionResponseToString(response: any): Promise<string>;
570
+ functionErrorToString(error: any): Promise<string>;
570
571
  }
571
572
 
572
573
  interface IChatMessage extends IStorableData {
@@ -697,6 +698,8 @@ interface IReceivedMessage {
697
698
 
698
699
  interface IChannelMessage extends IReceivedMessage {
699
700
  chatConnection: IChatConnection;
701
+ authInfo?: IStorableData;
702
+ setAuthInfo?: (authInfo: IStorableData | undefined) => void;
700
703
  }
701
704
 
702
705
  interface IChatChannel {
@@ -732,6 +735,7 @@ declare class ChatResolver {
732
735
 
733
736
  interface IMessageContext extends IReceivedMessage {
734
737
  chat: Chat;
738
+ authInfo?: IStorableData;
735
739
  }
736
740
 
737
741
  declare function runChatControllers(controllers: IConstructor<any>[]): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.1.0-beta.64",
3
+ "version": "0.1.0-beta.66",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",