This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathMessageSet.js
More file actions
executable file
·249 lines (245 loc) · 7.81 KB
/
MessageSet.js
File metadata and controls
executable file
·249 lines (245 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const MessageStateWrapper = require('./MessageStateWrapper.js');
const MessageSetMessage = require('./MessageSetMessage.js');
const MessageSink = require('./MessageSink.js');
const MessageLifecycleState = require('./MessageLifecycleState.js');
const MessageFactory = require('./MessageFactory.js');
const Transport = require('./Transport.js');
const last = require('lodash.last');
const every = require('lodash.every');
const transform = require('lodash.transform');
const errors = require('./MessageSet/errors.js');
const logger = require('./logger.js')('MessageSet');
/**
* A set of messages that will be grouped together and sent to the Chatbase
* create API
* @class MessageSet
*/
class MessageSet extends MessageFactory {
static sortMixedResponsePayload (responses) {
return transform(responses,
function (acc, msgResp) {
if (msgResp.status !== 'success') {
acc.failed.push(msgResp);
} else {
acc.succeeded.push(msgResp);
}
}
, {
succeeded: []
, failed: []
}
);
}
static validateCreateResponse (response) {
if (!response || !response.body) {
return new errors.EmptyCreateResponse();
} else if (response.body.status !== 200) {
return new errors.BadStatusCreateResponse(response.body.status);
} else if (response.body.all_succeeded !== true) {
return new errors.PartialPayloadFailure(MessageSet.
sortMixedResponsePayload(response.body.responses));
}
return true;
}
static extractPayloadsFromMessageSet (messages) {
var messagePayloadSet = [];
for (var i = 0; i < messages.length; i++) {
messagePayloadSet.push(messages[i].exportCreatePayload());
if (last(messagePayloadSet) instanceof Error) {
// return the error
return last(messagePayloadSet);
}
}
return messagePayloadSet;
}
constructor () {
super();
/**
* @property {Array<MessageStateWrapper>} messages - the queued messages
* to be sent to the Chatbase API
*/
this.messages = [];
this._state = new MessageLifecycleState();
Transport.CREATE_ENDPOINT = this.create_endpoint;
Transport.CREATE_SET_ENDPOINT = this.create_set_endpoint;
Transport.UPDATE_ENDPOINT = this.update_endpoint
}
/**
* Set the API Key field for all new messages produced by this interface
* @function setApiKey
* @param {String} apiKey - the Chatbase API key
* @chainable
*/
setApiKey (apiKey) {
this.api_key = apiKey;
return this;
}
/**
* Set the user id field for all new messages produced by this interface
* @function setUserId
* @param {String} userId - the user id for this client instance
* @chainable
*/
setUserId (userId) {
this.user_id = userId;
return this;
}
/**
* Set the platform field for all new messages produced by the interface
* @function setPlatform
* @param {String} platform - the platform the client is operating on
* @chainable
*/
setPlatform (platform) {
this.platform = platform;
return this;
}
/**
* Set the user type field for all new messages produced by the interface
* @function setAsTypeUser
* @chainable
*/
setAsTypeUser () {
this.type = MessageSink.messageTypes().user;
return this;
}
/**
* Set the user type field for all new messages produced by the interface
* @function setAsTypeAgent
* @chainable
*/
setAsTypeAgent () {
this.type = MessageSink.messageTypes().agent;
return this;
}
/**
* Set the version for all new messages produced by the interface
* @function setVersion
* @chainable
*/
setVersion (version) {
this.version = version;
return this;
}
/**
* Set the intent for all new messages produced by the interface
* @function setInent
* @chainable
*/
setIntent (intent) {
this.intent = intent;
return this;
}
/**
* Set the custom session id for all new messages produced by the interface
* @function setCustomSessionId
* @chainable
*/
setCustomSessionId (customSessionId) {
this.session_id = customSessionId;
return this;
}
/**
* Set the timeout for requests to the Chatbase API.
* @function setClientTimeout
* @param {Number} t - the timeout for requests to the Chatbase API.
* @chainable
*/
setClientTimeout (t) {
this.transport_timeout = t;
return this;
}
/**
* Rejection handler for creation errors. Wraps parent state sink method while
* handling interface logic branching.
* @function _handleCreateError
* @private
* @param {Error} error - the error that occurred during the create request
* @param {Function} reject - the reject function for the promise given to the
* invoking function
* @returns {Undefined} - does not return anything
*/
_handleCreateError (error, reject) {
logger('Encountered an error in creation request:');
logger(error);
this._state.setAsCreateErrored(error);
return reject(error.error);
}
getCreateResponse () {
return this._state.getCreateResponse();
}
createEntryStarted () {
return this._state.createEntryStarted();
}
createEntryCompleted () {
return this._state.createEntryCompleted();
}
/**
* Create a new message instance which will have any default values set on
* the interface by any applicable setters exposed publically. Optionally, API
* key and User Id can be supplied at function invocation to override any
* values set for these fields on the interface.
* @function newMessage
* @param {String} [apiKey] - the Chatbase API key
* @param {String} [userId] - the Chatbase user id
* @returns {MessageStateWrapper} - a new instance of MessageStateWrapper
*/
newMessage (apiKey=null, userId=null) {
const key = apiKey ? apiKey : this.api_key;
const id = userId ? userId : this.user_id;
const msg = new MessageSetMessage(key, id)
.setPlatform(this.platform)
.setVersion(this.version)
.setIntent(this.intent)
.setCustomSessionId(this.session_id)
.setClientTimeout(this.transport_timeout);
if (this.type === MessageSink.messageTypes().agent) {
msg.setAsTypeAgent();
} else if (this.type === MessageSink.messageTypes().user) {
msg.setAsTypeUser();
}
this.messages.push(msg);
return last(this.messages);
}
sendMessageSet () {
return new Promise((resolve, reject) => {
if (this._state.createEntryStarted()) {
return reject(new errors.MessageSetHasAlreadyBeenSent());
}
const messagePayloadSet = MessageSet
.extractPayloadsFromMessageSet(this.messages);
if (messagePayloadSet instanceof Error) {
return reject(messagePayloadSet);
}
this._state.setAsCreateStarted();
Transport.sendMessageSet({messages: messagePayloadSet},
this.transport_timeout)
.then(response => {
const e = MessageSet.validateCreateResponse(response);
if (e instanceof Error) {
return this._handleCreateError({response: response, error: e}, reject);
}
this._state.setAsCreateCompleted(response.body);
return resolve(this);
})
.catch(error => this._handleCreateError({error: error}, reject));
});
}
}
module.exports = MessageSet;