wingbot 3.69.7 → 3.70.0-alpha.4

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.
@@ -4,8 +4,9 @@
4
4
  'use strict';
5
5
 
6
6
  function include (params, context, plugins) {
7
- const includedRouter = context.blocks
8
- .find((block) => block.staticBlockId === params.staticBlockId);
7
+ const includedRouter = context.nestedBlocksByStaticId
8
+ ? context.nestedBlocksByStaticId.get(params.staticBlockId)
9
+ : context.blocks.find((block) => block.staticBlockId === params.staticBlockId);
9
10
 
10
11
  if (!includedRouter) {
11
12
  throw new Error(`Block ${params.staticBlockId} not found!`);
@@ -17,10 +17,12 @@ const subscribtions = require('./subscribtions');
17
17
  const setState = require('./setState');
18
18
  const expectedInput = require('./expectedInput');
19
19
  const skip = require('./skip');
20
+ const contextMessage = require('./contextMessage');
20
21
 
21
22
  module.exports = {
22
23
  path,
23
24
  message,
25
+ contextMessage,
24
26
  include,
25
27
  postback,
26
28
  expected,
@@ -270,12 +270,23 @@ function selectTranslation (resolverId, params, texts, data) {
270
270
  ];
271
271
  }
272
272
 
273
- /** @typedef {import('../BuildRouter').BotContext} BotContext */
274
- /** @typedef {import('../Router').Resolver} Resolver */
273
+ /** @typedef {import('../BuildRouter').BotContext<any>} BotContext */
274
+ /** @typedef {import('../Router').Resolver<any>} Resolver */
275
275
 
276
276
  /**
277
277
  *
278
278
  * @param {object} params
279
+ * @param {any} params.text
280
+ * @param {boolean} [params.hasCondition]
281
+ * @param {string} [params.conditionFn]
282
+ * @param {string} [params.conditionDesc]
283
+ * @param {boolean} [params.hasEditableCondition]
284
+ * @param {any} [params.editableCondition]
285
+ * @param {string} [params.mode]
286
+ * @param {string} [params.persist]
287
+ * @param {any[]} [params.replies]
288
+ * @param {"message" | "prompt"} [params.type]
289
+ * @param {string} [params.llmContextType]
279
290
  * @param {BotContext} context
280
291
  * @returns {Resolver}
281
292
  */
@@ -284,20 +295,18 @@ function message (params, context = {}) {
284
295
  // @ts-ignore
285
296
  isLastIndex, isLastMessage, linksMap, configuration, resolverId
286
297
  } = context;
298
+
287
299
  if (typeof params.text !== 'string' && !Array.isArray(params.text)) {
288
300
  throw new Error('Message should be a text!');
289
301
  }
290
302
 
291
303
  // parse quick replies
292
- let quickReplies = null;
304
+ let quickReplies;
293
305
  if (params.replies && !Array.isArray(params.replies)) {
294
306
  throw new Error('Replies should be an array');
295
- } else if (params.replies && params.replies.length > 0) {
296
- quickReplies = parseReplies(params.replies, linksMap, context);
297
307
  }
298
308
 
299
- // compile condition
300
- const condition = getCondition(params, context, 'Message condition');
309
+ let condition;
301
310
 
302
311
  const ret = isLastIndex ? Router.END : Router.CONTINUE;
303
312
 
@@ -305,7 +314,19 @@ function message (params, context = {}) {
305
314
  * @param {Request} req
306
315
  * @param {Responder} res
307
316
  */
308
- return (req, res) => {
317
+ return async (req, res) => {
318
+ if (condition === undefined) {
319
+ condition = getCondition(params, context, 'Message condition');
320
+ }
321
+
322
+ if (quickReplies === undefined) {
323
+ if (params.replies && params.replies.length > 0) {
324
+ quickReplies = parseReplies(params.replies, linksMap, context);
325
+ } else {
326
+ quickReplies = null;
327
+ }
328
+ }
329
+
309
330
  const data = stateData(req, res, configuration);
310
331
 
311
332
  // filter supported messages
@@ -315,7 +336,8 @@ function message (params, context = {}) {
315
336
  data.lang
316
337
  );
317
338
 
318
- const [text, seqState] = selectTranslation(
339
+ // eslint-disable-next-line prefer-const
340
+ let [text, seqState] = selectTranslation(
319
341
  resolverId,
320
342
  params,
321
343
  supportedText.translations,
@@ -394,6 +416,20 @@ function message (params, context = {}) {
394
416
  };
395
417
  }
396
418
 
419
+ if (params.type === 'prompt') {
420
+ const session = await res.llmSessionWithHistory(params.llmContextType);
421
+
422
+ const response = await session.systemPrompt(text)
423
+ .debug()
424
+ .generate();
425
+
426
+ // if (!response.content) {
427
+ // // no response?
428
+ // }
429
+
430
+ text = response.content;
431
+ }
432
+
397
433
  res.text(text, sendReplies, voiceControl);
398
434
 
399
435
  if (isLastMessage && !req.actionData()._resolverTag) {
@@ -32,11 +32,15 @@ function postback (params, context) {
32
32
  }
33
33
  }
34
34
 
35
- const condition = getCondition(params, context, 'postback');
35
+ let condition;
36
36
 
37
37
  const ret = isLastIndex ? Router.END : Router.CONTINUE;
38
38
 
39
39
  return (req, res, postBack) => {
40
+ if (condition === undefined) {
41
+ condition = getCondition(params, context, 'postback');
42
+ }
43
+
40
44
  if (!action || !shouldExecuteResolver(req, params)) {
41
45
  return ret;
42
46
  }
@@ -62,7 +62,7 @@ function isTextObjectEmpty (text) {
62
62
  * @param {Translations} translations
63
63
  * @param {string} [lang]
64
64
  * @param {boolean} [disableDefaulting] - it will try to find translation for other language
65
- * @returns {null|string}
65
+ * @returns {null|string|string[]}
66
66
  */
67
67
  function getLanguageText (translations, lang = null, disableDefaulting = false) {
68
68
  let foundText;
@@ -20,16 +20,21 @@ const { dateToISO8601String, zeroHourDate } = require('./datetime');
20
20
 
21
21
  /**
22
22
  *
23
- * @param {IStateRequest} req
24
- * @param {Responder} res
25
- * @param {object} configuration
23
+ * @param {IStateRequest} [req]
24
+ * @param {Responder} [res]
25
+ * @param {object} [configuration]
26
26
  * @param {object} [stateOverride]
27
27
  * @returns {object}
28
28
  */
29
- module.exports = function stateData (req, res = null, configuration = null, stateOverride = {}) {
30
- const c = configuration || req.configuration;
29
+ module.exports = function stateData (
30
+ req = null,
31
+ res = null,
32
+ configuration = null,
33
+ stateOverride = {}
34
+ ) {
35
+ const c = configuration || (req && req.configuration) || (res && res._configuration);
31
36
 
32
- const $this = req.text();
37
+ const $this = req ? req.text() : '';
33
38
 
34
39
  const now = new Date();
35
40
 
@@ -42,15 +47,20 @@ module.exports = function stateData (req, res = null, configuration = null, stat
42
47
 
43
48
  const {
44
49
  senderId,
45
- pageId
46
- } = req;
50
+ pageId,
51
+ state
52
+ } = req || {
53
+ senderId: res._senderId,
54
+ pageId: res._pageId,
55
+ state: res.options.state
56
+ };
47
57
 
48
58
  return {
49
59
  senderId,
50
60
  pageId,
51
61
  c,
52
62
  configuration: c,
53
- ...req.state,
63
+ ...state,
54
64
  ...(res ? res.newState : {}),
55
65
  ...stateOverride,
56
66
  $this,
@@ -59,7 +69,7 @@ module.exports = function stateData (req, res = null, configuration = null, stat
59
69
  $tomorrow,
60
70
  $yesterday,
61
71
  // yes - res because of circular dependency
62
- ...(res && req.actionData()),
72
+ ...(res && req && req.actionData()),
63
73
  ...(res ? res.data : {}),
64
74
  $input: $this
65
75
  };