@tiledesk/tiledesk-tybot-connector 0.2.88 → 0.2.91-rc1

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.
@@ -218,6 +218,10 @@ class TiledeskExpression {
218
218
  name: "roundAsNumber",
219
219
  applyPattern: "TiledeskMath.round(Number(#1))"
220
220
  },
221
+ "convertToNumber": {
222
+ name: "convertToNumber",
223
+ applyPattern: "Number(#1)"
224
+ },
221
225
  "JSONparse": {
222
226
  name: "JSONparse",
223
227
  applyPattern: "JSON.parse(String(#1))"
@@ -435,6 +435,8 @@ class TiledeskChatbotUtil {
435
435
  let fullName = message.senderFullname;
436
436
  if (fullName.trim() === chatbot_name) {
437
437
  fullName = "bot:" + fullName;
438
+ } else {
439
+ fullName = "user:" + fullName;
438
440
  }
439
441
  else {
440
442
  fullName = "user:" + fullName;
@@ -443,7 +445,7 @@ class TiledeskChatbotUtil {
443
445
  }
444
446
  }
445
447
 
446
- static transcriptJSON(transcript) {
448
+ static async transcriptJSON(transcript) {
447
449
  const regexp = /(<.*>)/gm;
448
450
  const parts = transcript.split(regexp);
449
451
  // console.log("parts:", parts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.88",
3
+ "version": "0.2.91-rc1",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -57,6 +57,7 @@ class DirContactUpdate {
57
57
  let filled_value = filler.fill(value, requestAttributes);
58
58
  if (this.log) {console.log("(DirContactUpdate) setting property key:",key, "with value:", value, "filled value:", filled_value); }
59
59
  updateProperties[key] = filled_value;
60
+ // it's important that all the lead's properties are immediatly updated in the current flow invocation so the updated values will be available in the next actions
60
61
  if (key === "fullname") {
61
62
  await this.context.chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, filled_value);
62
63
  if (this.log) {console.log("(DirContactUpdate) updating attribute:",TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, "with property key:", key, "and value:", filled_value); }
@@ -3,6 +3,8 @@ const { TiledeskChatbot } = require("../../models/TiledeskChatbot");
3
3
  const { Filler } = require("../Filler");
4
4
  let https = require("https");
5
5
  const { DirIntent } = require("./DirIntent");
6
+ const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
7
+ const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
6
8
  require('dotenv').config();
7
9
 
8
10
  class DirGptTask {
@@ -12,6 +14,7 @@ class DirGptTask {
12
14
  throw new Error('context object is mandatory');
13
15
  }
14
16
  this.context = context;
17
+ this.chatbot = this.context.chatbot;
15
18
  this.tdcache = this.context.tdcache;
16
19
  this.requestId = this.context.requestId;
17
20
  this.intentDir = new DirIntent(context);
@@ -47,6 +50,7 @@ class DirGptTask {
47
50
  let falseIntent = action.falseIntent;
48
51
  let trueIntentAttributes = action.trueIntentAttributes;
49
52
  let falseIntentAttributes = action.falseIntentAttributes;
53
+ let transcript;
50
54
 
51
55
  if (this.log) {
52
56
  console.log("DirGptTask trueIntent", trueIntent)
@@ -62,6 +66,7 @@ class DirGptTask {
62
66
  if (!action.question || action.question === '') {
63
67
  console.error("Error: DirGptTask question attribute is mandatory. Executing condition false...")
64
68
  if (falseIntent) {
69
+ await this.chatbot.addParameter("flowError", "GPT Error: question attribute is undefined");
65
70
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
66
71
  callback(true);
67
72
  return;
@@ -92,6 +97,18 @@ class DirGptTask {
92
97
  console.log("DirGptTask temperature: ", temperature);
93
98
  }
94
99
 
100
+ if (action.history) {
101
+ let transcript_string = await TiledeskChatbot.getParameterStatic(
102
+ this.context.tdcache,
103
+ this.context.requestId,
104
+ TiledeskChatbotConst.REQ_TRANSCRIPT_KEY);
105
+ if (this.log) { console.log("DirGptTask transcript string: ", transcript_string) }
106
+
107
+ transcript = await TiledeskChatbotUtil.transcriptJSON(transcript_string);
108
+ if (this.log) { console.log("DirGptTask transcript: ", transcript) }
109
+ }
110
+
111
+
95
112
  const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
96
113
  const openai_url = process.env.OPENAI_ENDPOINT + "/chat/completions";
97
114
  if (this.log) {
@@ -115,6 +132,7 @@ class DirGptTask {
115
132
  console.error("DirGptTask gptkey is mandatory");
116
133
  await this.#assignAttributes(action, answer);
117
134
  if (falseIntent) {
135
+ await this.chatbot.addParameter("flowError", "GPT Error: gpt apikey is undefined");
118
136
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
119
137
  callback(true);
120
138
  return;
@@ -134,22 +152,39 @@ class DirGptTask {
134
152
 
135
153
  let json = {
136
154
  model: action.model,
137
- messages: [
138
- {
139
- role: "user",
140
- content: filled_question
141
- }
142
- ],
155
+ messages: [],
156
+ // messages: [
157
+ // {
158
+ // role: "user",
159
+ // content: filled_question
160
+ // }
161
+ // ],
143
162
  max_tokens: action.max_tokens,
144
- temperature: action.temperature
163
+ temperature: action.temperature,
145
164
  }
146
165
 
147
166
  let message = { role: "", content: "" };
167
+
148
168
  if (action.context) {
149
- message.role = "system";
150
- message.content = filled_context;
151
- json.messages.unshift(message);
169
+ let message = {
170
+ role: "system",
171
+ content: filled_context
172
+ }
173
+ json.messages.push(message);
152
174
  }
175
+
176
+ if (transcript) {
177
+ transcript.forEach(msg => {
178
+ if (!msg.content.startsWith('/')) {
179
+ let message = {
180
+ role: msg.role,
181
+ content: msg.content
182
+ }
183
+ json.messages.push(message)
184
+ }
185
+ })
186
+ }
187
+
153
188
  if (this.log) { console.log("DirGptTask json: ", json) }
154
189
 
155
190
  const HTTPREQUEST = {
@@ -167,10 +202,11 @@ class DirGptTask {
167
202
  if (err) {
168
203
  if (this.log) {
169
204
  console.error("(httprequest) DirGptTask openai err:", err);
170
- console.error("(httprequest) DirGptTask openai err:", err.response.data);
205
+ console.error("(httprequest) DirGptTask openai err:", err.response?.data?.error?.message);
171
206
  }
172
207
  await this.#assignAttributes(action, answer);
173
208
  if (falseIntent) {
209
+ await this.chatbot.addParameter("flowError", "GPT Error: " + err.response?.data?.error?.message);
174
210
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
175
211
  callback(true);
176
212
  return;
@@ -97,10 +97,28 @@ class DirSetAttributeV2 {
97
97
 
98
98
  async go(action, callback) {
99
99
  if (this.log) {console.log("(DirSetAttribute) action before filling:", JSON.stringify(action));}
100
+ if (action && !action.operation) {
101
+ if (this.log) {console.log("(SetAttributeV2) Error operation is mandatory");}
102
+ callback();
103
+ }
100
104
  if (action && action.operation && action.operation.operands) {
101
- if (this.log) {console.log("filling in setattribute...");}
105
+ if (this.log) {console.log("(SetAttributeV2) filling in setattribute...");}
102
106
  await this.fillValues(action.operation.operands);
103
107
  }
108
+ console.log("action.operation.operands.length", action.operation.operands.length);
109
+ console.log("action.operation.operands[0].type", action.operation.operands[0].type);
110
+
111
+ // FUN FACT: THIS TOOK A LOT OF EFFERT BUT IT WAS NEVER USED. YOU CAN SIMPLY CREATE A JSON ATTRIBUTE APPLYING
112
+ // JSONparse FUNCTION TO AN ATTRIBUTE.
113
+ if (action.operation.operands && action.operation.operands.length === 1 && action.operation.operands[0].type === "json") {
114
+ if (this.log) {console.log("(SetAttributeV2) setting json value...");}
115
+ console.log("(SetAttributeV2) setting json value... destination:", action.destination);
116
+ const json_value = JSON.parse(action.operation.operands[0].value);
117
+ console.log("(SetAttributeV2) json_value:", json_value);
118
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.destination, json_value);
119
+ callback();
120
+ return; // on json types no operations are permitted beyond assignment
121
+ }
104
122
  if (this.log) {console.log("filled in setattribute:", action.operation);}
105
123
  // let res = validate(action, schema);
106
124
  // if (res.errors) {
@@ -201,6 +219,44 @@ class DirSetAttributeV2 {
201
219
  console.error("Error while filling operands:", error);
202
220
  }
203
221
  }
222
+
223
+ convertOperandValues(operands) {
224
+ console.log("Converting operands:", operands);
225
+ // operation: {
226
+ // operators: ["addAsNumber", "subtractAsNumber", "divideAsNumber", "multiplyAsNumber"],
227
+ // operands: [
228
+ // {
229
+ // value: "previous",
230
+ // isVariable: true,
231
+ // type: "string"
232
+ // }
233
+ // ]
234
+ try {
235
+ operands.forEach(operand => {
236
+ if (operand.type) {
237
+ console.log("Converting operands - operand.type:", operand.type.toLowerCase());
238
+ if (operand.type.toLowerCase() === "number") {
239
+ console.log("Converting operands - number");
240
+ operand.value = Number(operand.value);
241
+ console.log("new value:", operand.value);
242
+ console.log("new value type:", typeof operand.value);
243
+ }
244
+ else if (operand.type.toLowerCase() === "json") {
245
+ console.log("Converting operands - json, value =", operand.value);
246
+ operand.value = JSON.parse(operand.value);
247
+ console.log("new value:", operand.value);
248
+ console.log("new value type:", typeof operand.value);
249
+ }
250
+ else {
251
+ console.log("Converting operands - ??");
252
+ }
253
+ }
254
+ });
255
+ }
256
+ catch(error) {
257
+ console.error("Error while converting operands:", error);
258
+ }
259
+ }
204
260
  }
205
261
 
206
262
  module.exports = { DirSetAttributeV2 };