lisichatbot 1.5.1 → 1.5.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +80 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisichatbot",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -1880,6 +1880,36 @@ async function handleNext() {
1880
1880
  }
1881
1881
  }
1882
1882
 
1883
+ // ✅ NEW: Add user message BEFORE onNext executes (for autoAdvance flows)
1884
+ if (chatState.currentSelection) {
1885
+ const inputConfig = currentStep.input || {};
1886
+ const valueType = inputConfig.selectedInputValueType;
1887
+ const prefix = inputConfig.selectedInputPrefix || '';
1888
+ const suffix = inputConfig.selectedInputSuffix || '';
1889
+
1890
+ let displayName = chatState.currentSelection.name;
1891
+
1892
+ if (valueType === 'arrayRange' && Array.isArray(chatState.currentSelection.value)) {
1893
+ const [min, max] = chatState.currentSelection.value;
1894
+ const formattedMin = min.toLocaleString();
1895
+ const formattedMax = max.toLocaleString();
1896
+ displayName = `${formattedMin} - ${formattedMax}`;
1897
+ }
1898
+
1899
+ if (prefix) displayName = `${prefix} ${displayName}`;
1900
+ if (suffix) displayName = `${displayName} ${suffix}`;
1901
+
1902
+ console.log(`📝 Adding user message before onNext: "${displayName}"`);
1903
+ addMessage(displayName, 'user', false, chatState.step);
1904
+
1905
+ chatState.history.push({
1906
+ step: chatState.step,
1907
+ field: chatState.currentSelection.field,
1908
+ value: chatState.currentSelection.value,
1909
+ displayName: displayName
1910
+ });
1911
+ }
1912
+
1883
1913
  if (currentStep.onNext) {
1884
1914
  try {
1885
1915
  disableNextButton();
@@ -1895,6 +1925,23 @@ async function handleNext() {
1895
1925
  scrollToBottom();
1896
1926
  }
1897
1927
 
1928
+ // ✅ NEW: Check if onNext wants to jump to a specific step
1929
+ if (result && typeof result === 'object' && typeof result.goToStep === 'number') {
1930
+ console.log(`🎯 onNext requested jump to step ${result.goToStep}`);
1931
+
1932
+ const targetStep = result.goToStep;
1933
+
1934
+ if (targetStep >= 0 && targetStep < flowData.flow.length) {
1935
+ chatState.step = targetStep;
1936
+ chatState.currentSelection = null;
1937
+ disableNextButton();
1938
+ await showNextStep();
1939
+ return;
1940
+ } else {
1941
+ console.error(`Invalid step number: ${targetStep}`);
1942
+ }
1943
+ }
1944
+
1898
1945
  // ✅ NEW: Check if onNext returned data to update next step
1899
1946
  if (result && typeof result === 'object' && result.updateNextStep) {
1900
1947
  console.log('📝 onNext returned data to update next step:', result);
@@ -2002,34 +2049,8 @@ async function handleNext() {
2002
2049
  }
2003
2050
  }
2004
2051
 
2005
- if (chatState.currentSelection) {
2006
- const inputConfig = currentStep.input || {};
2007
- const valueType = inputConfig.selectedInputValueType;
2008
- const prefix = inputConfig.selectedInputPrefix || '';
2009
- const suffix = inputConfig.selectedInputSuffix || '';
2010
-
2011
- let displayName = chatState.currentSelection.name;
2012
-
2013
- if (valueType === 'arrayRange' && Array.isArray(chatState.currentSelection.value)) {
2014
- const [min, max] = chatState.currentSelection.value;
2015
- const formattedMin = min.toLocaleString();
2016
- const formattedMax = max.toLocaleString();
2017
- displayName = `${formattedMin} - ${formattedMax}`;
2018
- }
2019
-
2020
- if (prefix) displayName = `${prefix} ${displayName}`;
2021
- if (suffix) displayName = `${displayName} ${suffix}`;
2022
-
2023
- addMessage(displayName, 'user', false, chatState.step);
2024
-
2025
- chatState.history.push({
2026
- step: chatState.step,
2027
- field: chatState.currentSelection.field,
2028
- value: chatState.currentSelection.value,
2029
- name: displayName
2030
- });
2031
- }
2032
-
2052
+ // ✅ User message already added before onNext (line ~1883)
2053
+ // Clear current selection to prevent duplicate messages
2033
2054
  chatState.currentSelection = null;
2034
2055
  disableNextButton();
2035
2056
 
@@ -2283,6 +2304,23 @@ async function showNextStep() {
2283
2304
  scrollToBottom();
2284
2305
  }
2285
2306
 
2307
+ // ✅ NEW: Check if onStart wants to jump to a specific step
2308
+ if (result && typeof result === 'object' && typeof result.goToStep === 'number') {
2309
+ console.log(`🎯 onStart requested jump to step ${result.goToStep}`);
2310
+
2311
+ const targetStep = result.goToStep;
2312
+
2313
+ if (targetStep >= 0 && targetStep < flowData.flow.length) {
2314
+ chatState.step = targetStep;
2315
+ chatState.currentSelection = null;
2316
+ disableNextButton();
2317
+ await showNextStep();
2318
+ return;
2319
+ } else {
2320
+ console.error(`Invalid step number: ${targetStep}`);
2321
+ }
2322
+ }
2323
+
2286
2324
  // ✅ NEW: Check if onStart wants to block this step and skip to next
2287
2325
  if (result && typeof result === 'object' && result.blockStep === true) {
2288
2326
  console.log('⏭️ Step blocked by onStart - skipping to next step');
@@ -2736,10 +2774,23 @@ if (typeof document !== 'undefined') {
2736
2774
  // EXPORTS
2737
2775
  // =============================================================================
2738
2776
 
2777
+ // ✅ NEW: Helper function to display messages in chat from callbacks
2778
+ function displayMessage(message) {
2779
+ if (!elements.messages) {
2780
+ console.error('Cannot display message - chat not initialized');
2781
+ return;
2782
+ }
2783
+
2784
+ addMessage(message, 'bot', false, null);
2785
+ scrollToBottom();
2786
+ console.log('💬 Message displayed:', message);
2787
+ }
2788
+
2739
2789
  module.exports = {
2740
2790
  init,
2741
2791
  getState,
2742
2792
  reset,
2743
2793
  goToStep,
2744
- editStep
2794
+ editStep,
2795
+ displayMessage // ✅ NEW: Export helper function
2745
2796
  };