lisichatbot 1.5.7 → 1.5.9

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 +92 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisichatbot",
3
- "version": "1.5.7",
3
+ "version": "1.5.9",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -1975,14 +1975,24 @@ async function handleNext() {
1975
1975
  if (currentStep.onNext) {
1976
1976
  try {
1977
1977
  disableNextButton();
1978
+
1979
+ // ✅ NEW: Create showMessage helper function
1980
+ const showMessage = (message) => {
1981
+ console.log('💬 showMessage called:', message);
1982
+ addMessage(message, 'bot', false, null);
1983
+ scrollToBottom();
1984
+ };
1985
+
1986
+ // ✅ Pass showMessage as third parameter
1978
1987
  const result = await currentStep.onNext(
1979
1988
  chatState.currentSelection ? chatState.currentSelection.value : null,
1980
- chatState.data
1989
+ chatState.data,
1990
+ showMessage // ✅ NEW: Third parameter
1981
1991
  );
1982
1992
 
1983
- // ✅ NEW: Check if onNext wants to display a message in chat
1993
+ // ✅ Still support showMessage in return object for backwards compatibility
1984
1994
  if (result && typeof result === 'object' && result.showMessage) {
1985
- console.log('💬 onNext displaying message:', result.showMessage);
1995
+ console.log('💬 onNext displaying message from return:', result.showMessage);
1986
1996
  addMessage(result.showMessage, 'bot', false, null);
1987
1997
  scrollToBottom();
1988
1998
  }
@@ -2041,6 +2051,25 @@ async function handleNext() {
2041
2051
  });
2042
2052
  }
2043
2053
 
2054
+ // ✅ NEW: Check if onNext wants to jump to step by name
2055
+ if (result && typeof result === 'object' && result.goToStepName) {
2056
+ console.log(`🎯 onNext requested jump to step name: "${result.goToStepName}"`);
2057
+
2058
+ const stepName = result.goToStepName;
2059
+ const targetStepIndex = flowData.flow.findIndex(step => step.name === stepName);
2060
+
2061
+ if (targetStepIndex !== -1) {
2062
+ console.log(` ✅ Found step "${stepName}" at index ${targetStepIndex}`);
2063
+ chatState.step = targetStepIndex;
2064
+ chatState.currentSelection = null;
2065
+ disableNextButton();
2066
+ await showNextStep();
2067
+ return;
2068
+ } else {
2069
+ console.error(`❌ No step found with name: "${stepName}"`);
2070
+ }
2071
+ }
2072
+
2044
2073
  // ✅ NEW: Check if onNext wants to jump to a specific step
2045
2074
  if (result && typeof result === 'object' && typeof result.goToStep === 'number') {
2046
2075
  console.log(`🎯 onNext requested jump to step ${result.goToStep}`);
@@ -2317,7 +2346,22 @@ async function showNextStep() {
2317
2346
 
2318
2347
  if (nextStep.onStart) {
2319
2348
  try {
2320
- const result = await nextStep.onStart(chatState.data);
2349
+ // NEW: Create showMessage helper function
2350
+ const showMessage = (message) => {
2351
+ console.log('💬 showMessage called:', message);
2352
+ addMessage(message, 'bot', false, null);
2353
+ scrollToBottom();
2354
+ };
2355
+
2356
+ // ✅ Pass showMessage as second parameter
2357
+ const result = await nextStep.onStart(chatState.data, showMessage);
2358
+
2359
+ // ✅ Still support showMessage in return object for backwards compatibility
2360
+ if (result && typeof result === 'object' && result.showMessage) {
2361
+ console.log('💬 onStart displaying message from return:', result.showMessage);
2362
+ addMessage(result.showMessage, 'bot', false, null);
2363
+ scrollToBottom();
2364
+ }
2321
2365
 
2322
2366
  // ✅ NEW: Check if onStart wants to update steps by field name
2323
2367
  if (result && typeof result === 'object' && result.updateStepByField) {
@@ -2467,11 +2511,23 @@ async function showNextStep() {
2467
2511
  }
2468
2512
  }
2469
2513
 
2470
- // ✅ NEW: Check if onStart wants to display a message in chat
2471
- if (result && typeof result === 'object' && result.showMessage) {
2472
- console.log('💬 onStart displaying message:', result.showMessage);
2473
- addMessage(result.showMessage, 'bot', false, null);
2474
- scrollToBottom();
2514
+ // ✅ NEW: Check if onStart wants to jump to step by name
2515
+ if (result && typeof result === 'object' && result.goToStepName) {
2516
+ console.log(`🎯 onStart requested jump to step name: "${result.goToStepName}"`);
2517
+
2518
+ const stepName = result.goToStepName;
2519
+ const targetStepIndex = flowData.flow.findIndex(step => step.name === stepName);
2520
+
2521
+ if (targetStepIndex !== -1) {
2522
+ console.log(` ✅ Found step "${stepName}" at index ${targetStepIndex}`);
2523
+ chatState.step = targetStepIndex;
2524
+ chatState.currentSelection = null;
2525
+ disableNextButton();
2526
+ await showNextStep();
2527
+ return;
2528
+ } else {
2529
+ console.error(`❌ No step found with name: "${stepName}"`);
2530
+ }
2475
2531
  }
2476
2532
 
2477
2533
  // ✅ NEW: Check if onStart wants to jump to a specific step
@@ -2531,7 +2587,33 @@ async function showNextStep() {
2531
2587
  });
2532
2588
 
2533
2589
  const hasInput = !!nextStep.input;
2534
- addMessage(nextStep.message, 'bot', hasInput, chatState.step);
2590
+
2591
+ // ✅ NEW: Support dynamic messages
2592
+ let messageToDisplay = nextStep.message;
2593
+
2594
+ if (typeof nextStep.message === 'function') {
2595
+ // ✅ Message as function: message: (data) => `Hello ${data.name}`
2596
+ try {
2597
+ messageToDisplay = nextStep.message(chatState.data);
2598
+ console.log(` 💬 Dynamic message (function): "${messageToDisplay}"`);
2599
+ } catch (error) {
2600
+ console.error('Error in message function:', error);
2601
+ messageToDisplay = 'Error loading message';
2602
+ }
2603
+ } else if (typeof nextStep.message === 'string' && nextStep.message.includes('${')) {
2604
+ // ✅ Message with template variables: message: "Hello ${name}"
2605
+ try {
2606
+ // Create function from template string
2607
+ const templateFunc = new Function('data', `return \`${nextStep.message}\`;`);
2608
+ messageToDisplay = templateFunc(chatState.data);
2609
+ console.log(` 💬 Dynamic message (template): "${messageToDisplay}"`);
2610
+ } catch (error) {
2611
+ console.error('Error parsing template message:', error);
2612
+ messageToDisplay = nextStep.message; // Fallback to original
2613
+ }
2614
+ }
2615
+
2616
+ addMessage(messageToDisplay, 'bot', hasInput, chatState.step);
2535
2617
 
2536
2618
  const inputRequired = nextStep.inputRequired === true;
2537
2619