quickblox 2.16.4 → 2.17.0-beta.2

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.
@@ -0,0 +1,134 @@
1
+ 'use strict';
2
+
3
+ var Utils = require('../qbUtils');
4
+
5
+ var AI_API_URL = 'ai/ai_extensions';
6
+
7
+ function AIProxy(service) {
8
+ this.service = service;
9
+ }
10
+
11
+ /**
12
+ * @namespace QB.ai
13
+ **/
14
+ AIProxy.prototype = {
15
+ /**
16
+ * Provides answer assistant functionality that helps users effortlessly send various answers considering({@link https://docs.quickblox.com/docs/js-sdk-ai-features#ai-assist-answer read more}).
17
+ * @memberof QB.ai
18
+ * @param {String} smartChatAssistantId - Smart Chat Assistant id.
19
+ * @param {String} message - Message you want to get answer for.
20
+ * @param {Object[]} history - Conversation history. Used to add context.
21
+ * @param {answerAssistCallback} callback - The callback function.
22
+ * @example
23
+ * var history = [
24
+ * {role: "user", message: "Hello"},
25
+ * {role: "assistant", message: "Hi"}
26
+ * ];
27
+ * var messageToAssist = 'Where is my order?';
28
+ * QB.ai.answerAssist(smartChatAssistantId, messageToAssist, history, callback);
29
+ * // or third parameters can be null
30
+ * QB.ai.answerAssist(smartChatAssistantId, messageToAssist, null, callback);
31
+ * */
32
+ answerAssist: function(smartChatAssistantId, message, history, callback) {
33
+ /**
34
+ * Callback for QB.ai.answerAssist().
35
+ * @param {Object} error - The error object.
36
+ * @param {Object} response - The server response object.
37
+ * @param {String} [response.answer] - assist answer for message
38
+ * @callback answerAssistCallback
39
+ * */
40
+ if (!callback || typeof callback !== 'function') {
41
+ throw new Error('Callback function is required and must be a function');
42
+ }
43
+ function validateHistory(history) {
44
+ var AIRole = {
45
+ user: 'user',
46
+ assistant: 'assistant'
47
+ };
48
+ if (history !== null && history !== undefined) {
49
+ if (!Array.isArray(history)) {
50
+ throw new Error('History must be an array');
51
+ }
52
+ for (var i = 0; i < history.length; i++) {
53
+ var item = history[i];
54
+ if (typeof item !== 'object' || item === null || Array.isArray(item)) {
55
+ throw new Error('Each element of history must be an object');
56
+ }
57
+ if (!('role' in item) || !('message' in item)) {
58
+ throw new Error('Each element of history must have an role and message fields');
59
+ }
60
+ if (!(item.role === AIRole.user || item.role === AIRole.assistant)) {
61
+ throw new Error('Invalid role in history item');
62
+ }
63
+ if (typeof item.message !== 'string') {
64
+ throw new Error('Message of history item must be a string');
65
+ }
66
+ }
67
+ }
68
+ return true;
69
+ }
70
+ if (!validateHistory(history)) {
71
+ return;
72
+ }
73
+
74
+ var data = history ? {
75
+ smart_chat_assistant_id: smartChatAssistantId,
76
+ message: message,
77
+ history: history,
78
+ }:{
79
+ smart_chat_assistant_id: smartChatAssistantId,
80
+ message: message,
81
+ };
82
+ var attrAjax = {
83
+ 'type': 'POST',
84
+ 'url': Utils.formatUrl(AI_API_URL + '/ai_answer_assist'),
85
+ 'data': data,
86
+ 'contentType': 'application/json; charset=utf-8',
87
+ 'isNeedStringify': true
88
+ };
89
+ this.service.ajax(attrAjax, callback);
90
+ },
91
+
92
+ /**
93
+ * Offers translation functionality that helps users easily translate text messages in chat({@link https://docs.quickblox.com/docs/js-sdk-ai-features#ai-translate read more}).
94
+ * @memberof QB.ai
95
+ * @param {String} smartChatAssistantId - Smart Chat Assistant id.
96
+ * @param {String} text - Text to translate.
97
+ * @param {String} languageCode - Translation language code. All list see on page: {@link https://docs.quickblox.com/docs/js-sdk-ai-features#ai-translate }
98
+ * @param {translateCallback} callback - The callback function.
99
+ *
100
+ * */
101
+
102
+ translate: function(smartChatAssistantId, text, languageCode, callback) {
103
+ /**
104
+ * Callback for QB.ai.translate().
105
+ * @param {Object} error - The error object.
106
+ * @param {Object} response - The server response object.
107
+ * @param {String} [response.answer] - translated message
108
+ * @callback translateCallback
109
+ * @example
110
+ * var textToTranslate = 'Hola!';
111
+ * var languageCode = 'en';
112
+ * QB.ai.translate(smartChatAssistantId, textToTranslate, languageCode, callback);
113
+ * */
114
+ if (!callback || typeof callback !== 'function') {
115
+ throw new Error('Callback function is required and must be a function');
116
+ }
117
+ var data = {
118
+ smart_chat_assistant_id: smartChatAssistantId,
119
+ text: text,
120
+ to_language: languageCode || 'en',
121
+ };
122
+ var attrAjax = {
123
+ 'type': 'POST',
124
+ 'url': Utils.formatUrl(AI_API_URL + '/ai_translate'),
125
+ 'data': data,
126
+ };
127
+
128
+ this.service.ajax(attrAjax, callback);
129
+ },
130
+
131
+
132
+ };
133
+
134
+ module.exports = AIProxy;
package/src/qbMain.js CHANGED
@@ -8,6 +8,7 @@
8
8
  */
9
9
  var config = require('./qbConfig');
10
10
  var Utils = require('./qbUtils');
11
+ const MessageProxy = require("./modules/chat/qbMessage");
11
12
 
12
13
  // Actual QuickBlox API starts here
13
14
  function QuickBlox() {}
@@ -63,7 +64,8 @@ QuickBlox.prototype = {
63
64
  AddressBook = require('./modules/qbAddressBook'),
64
65
  Chat = require('./modules/chat/qbChat'),
65
66
  DialogProxy = require('./modules/chat/qbDialog'),
66
- MessageProxy = require('./modules/chat/qbMessage');
67
+ MessageProxy = require('./modules/chat/qbMessage'),
68
+ AIProxy = require('./modules/qbAI');
67
69
 
68
70
  this.service = new Proxy();
69
71
  this.auth = new Auth(this.service);
@@ -75,6 +77,7 @@ QuickBlox.prototype = {
75
77
  this.chat = new Chat(this.service);
76
78
  this.chat.dialog = new DialogProxy(this.service);
77
79
  this.chat.message = new MessageProxy(this.service);
80
+ this.ai = new AIProxy(this.service);
78
81
 
79
82
  if (Utils.getEnv().browser) {
80
83
  /** add adapter.js*/
package/src/qbProxy.js CHANGED
@@ -135,7 +135,39 @@ ServiceProxy.prototype = {
135
135
  if (config.timeout) {
136
136
  qbRequest.timeout = config.timeout;
137
137
  }
138
-
138
+ //browser only version
139
+ // fetch(qbUrl, qbRequest)
140
+ // .then(function(response){
141
+ // qbResponse = response;
142
+ // if (qbRequest.method === 'GET' || qbRequest.method === 'POST'){
143
+ // var qbTokenExpirationDate = qbResponse.headers.get('qb-token-expirationdate');
144
+ // var headerHasToken = !(qbTokenExpirationDate === null ||
145
+ // typeof qbTokenExpirationDate === 'undefined');
146
+ // qbTokenExpirationDate = headerHasToken ? qbTokenExpirationDate : new Date();
147
+ // self.qbInst.config.updateSessionExpirationDate(qbTokenExpirationDate, headerHasToken);
148
+ // console.log('[Request][fetch]','header has token:',headerHasToken );
149
+ // console.log('[Request][fetch]','updateSessionExpirationDate ... Set value: ', self.qbInst.config.qbTokenExpirationDate );
150
+ // }
151
+ // if (qbDataType === 'text') {
152
+ // return response.text();
153
+ // } else {
154
+ // return response.json();
155
+ // }
156
+ // }).catch((error) => {
157
+ // console.log('fetch Error: ', error);
158
+ // qbResponse = {
159
+ // status: 200
160
+ // };
161
+ // console.log('reason: ', error);
162
+ // return ' ';
163
+ // }).then(function(body){
164
+ // _requestCallback(null, qbResponse, body);
165
+ // }).catch((error) => {
166
+ // console.log('Fetch error: ', error);
167
+ // _requestCallback(error);
168
+ // });
169
+
170
+ // original version
139
171
  qbFetch(qbUrl, qbRequest)
140
172
  .then(function(response) {
141
173
  qbResponse = response;
package/src/qbUtils.js CHANGED
@@ -125,6 +125,11 @@ var Utils = {
125
125
  return 'https://' + config.endpoints.api + '/' + base + resource + config.urls.type;
126
126
  },
127
127
 
128
+ formatUrl: function(base, id) {
129
+ var resource = id ? '/' + id : '';
130
+ return 'https://' + config.endpoints.api + '/' + base + resource;
131
+ },
132
+
128
133
  isArray: function(arg) {
129
134
  return Object.prototype.toString.call(arg) === '[object Array]';
130
135
  },