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

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.
@@ -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 {
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.65",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",