@clt-chatbot/scenario-core 1.0.0

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,12 @@
1
+ import { ScenarioData, ScenarioNode } from '../types/index';
2
+ export declare class ChatbotEngine {
3
+ private scenario;
4
+ constructor(scenario: ScenarioData);
5
+ getNodeById(nodeId: string): ScenarioNode | undefined;
6
+ interpolateMessage(message: string, slots: Record<string, any>): string;
7
+ getDeepValue(obj: any, path: string): any;
8
+ evaluateCondition(slotValue: any, operator: string, conditionValue: any): boolean;
9
+ getNextNode(currentNodeId: string, sourceHandle?: string | null, slots?: any): ScenarioNode | null;
10
+ isInteractiveNode(node: ScenarioNode | undefined): boolean;
11
+ isAutoPassthroughNode(node: ScenarioNode | undefined): boolean;
12
+ }
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChatbotEngine = void 0;
4
+ class ChatbotEngine {
5
+ constructor(scenario) {
6
+ this.scenario = scenario;
7
+ }
8
+ getNodeById(nodeId) {
9
+ return this.scenario.nodes.find(n => n.id === nodeId);
10
+ }
11
+ interpolateMessage(message, slots) {
12
+ if (typeof message !== 'string')
13
+ return String(message ?? '');
14
+ return message.replace(/\{\{([^}]+)\}\}/g, (_, path) => {
15
+ const val = this.getDeepValue(slots, path.trim());
16
+ return val !== undefined && val !== null ? String(val) : `{{${path}}}`;
17
+ });
18
+ }
19
+ getDeepValue(obj, path) {
20
+ if (!path)
21
+ return undefined;
22
+ const keys = path.split('.');
23
+ let current = obj;
24
+ for (const key of keys) {
25
+ if (current === null || current === undefined)
26
+ return undefined;
27
+ current = current[key];
28
+ }
29
+ return current;
30
+ }
31
+ evaluateCondition(slotValue, operator, conditionValue) {
32
+ const lowerCaseConditionValue = String(conditionValue ?? '').toLowerCase();
33
+ const boolConditionValue = lowerCaseConditionValue === 'true';
34
+ const boolSlotValue = String(slotValue ?? '').toLowerCase() === 'true';
35
+ if (lowerCaseConditionValue === 'true' || lowerCaseConditionValue === 'false') {
36
+ switch (operator) {
37
+ case '==': return boolSlotValue === boolConditionValue;
38
+ case '!=': return boolSlotValue !== boolConditionValue;
39
+ default: return false;
40
+ }
41
+ }
42
+ const numSlotValue = slotValue !== null && slotValue !== undefined && slotValue !== '' ? parseFloat(slotValue) : NaN;
43
+ const numConditionValue = conditionValue !== null && conditionValue !== undefined && conditionValue !== '' ? parseFloat(conditionValue) : NaN;
44
+ const bothAreNumbers = !isNaN(numSlotValue) && !isNaN(numConditionValue);
45
+ switch (operator) {
46
+ case '==': return String(slotValue ?? '') == String(conditionValue ?? '');
47
+ case '!=': return String(slotValue ?? '') != String(conditionValue ?? '');
48
+ case '>': return bothAreNumbers && numSlotValue > numConditionValue;
49
+ case '<': return bothAreNumbers && numSlotValue < numConditionValue;
50
+ case '>=': return bothAreNumbers && numSlotValue >= numConditionValue;
51
+ case '<=': return bothAreNumbers && numSlotValue <= numConditionValue;
52
+ case 'contains': return slotValue != null && String(slotValue).includes(String(conditionValue ?? ''));
53
+ case '!contains': return slotValue == null || !String(slotValue).includes(String(conditionValue ?? ''));
54
+ default:
55
+ console.warn(`Unsupported operator used in condition: ${operator}`);
56
+ return false;
57
+ }
58
+ }
59
+ getNextNode(currentNodeId, sourceHandle = null, slots = {}) {
60
+ const { nodes, edges } = this.scenario;
61
+ const outgoingEdges = edges.filter(e => e.source === currentNodeId);
62
+ if (outgoingEdges.length === 0)
63
+ return null;
64
+ const sourceNode = this.getNodeById(currentNodeId);
65
+ // Branch node with CONDITION evaluation
66
+ if (sourceNode?.type === 'branch' && sourceNode.data?.evaluationType === 'CONDITION') {
67
+ const conditions = sourceNode.data.conditions || [];
68
+ for (const condition of conditions) {
69
+ const slotValue = this.getDeepValue(slots, condition.slot);
70
+ const valueToCompare = condition.valueType === 'slot' ? this.getDeepValue(slots, condition.value) : condition.value;
71
+ if (this.evaluateCondition(slotValue, condition.operator, valueToCompare)) {
72
+ const condIdx = conditions.indexOf(condition);
73
+ const handleId = sourceNode.data.replies?.[condIdx]?.value;
74
+ if (handleId) {
75
+ const edge = outgoingEdges.find(e => e.sourceHandle === handleId);
76
+ if (edge)
77
+ return this.getNodeById(edge.target) || null;
78
+ }
79
+ }
80
+ }
81
+ const defaultEdge = outgoingEdges.find(e => e.sourceHandle === 'default');
82
+ if (defaultEdge)
83
+ return this.getNodeById(defaultEdge.target) || null;
84
+ }
85
+ // Single edge case
86
+ if (outgoingEdges.length === 1) {
87
+ return this.getNodeById(outgoingEdges[0].target) || null;
88
+ }
89
+ // Explicit sourceHandle case
90
+ if (sourceHandle) {
91
+ const selectedEdge = outgoingEdges.find(e => e.sourceHandle === sourceHandle);
92
+ if (selectedEdge)
93
+ return this.getNodeById(selectedEdge.target) || null;
94
+ }
95
+ // Fallback to first edge
96
+ return this.getNodeById(outgoingEdges[0].target) || null;
97
+ }
98
+ isInteractiveNode(node) {
99
+ if (!node)
100
+ return false;
101
+ if (node.type === 'message') {
102
+ return !!(node.data?.replies && node.data.replies.length > 0);
103
+ }
104
+ if (node.type === 'form')
105
+ return true;
106
+ if (node.type === 'branch') {
107
+ const evalType = node.data?.evaluationType;
108
+ return evalType === 'BUTTON' || evalType === 'BUTTON_CLICK';
109
+ }
110
+ return node.type === 'slotfilling';
111
+ }
112
+ isAutoPassthroughNode(node) {
113
+ if (!node)
114
+ return false;
115
+ return ['setSlot', 'set-slot', 'delay', 'api', 'toast', 'link'].includes(node.type);
116
+ }
117
+ }
118
+ exports.ChatbotEngine = ChatbotEngine;
@@ -0,0 +1,2 @@
1
+ export * from './types';
2
+ export * from './engine/ChatbotEngine';
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./engine/ChatbotEngine"), exports);
@@ -0,0 +1,18 @@
1
+ export interface ScenarioNode {
2
+ id: string;
3
+ type: string;
4
+ data: any;
5
+ parentNode?: string;
6
+ }
7
+ export interface ScenarioEdge {
8
+ id: string;
9
+ source: string;
10
+ target: string;
11
+ sourceHandle?: string | null;
12
+ }
13
+ export interface ScenarioData {
14
+ version: string;
15
+ nodes: ScenarioNode[];
16
+ edges: ScenarioEdge[];
17
+ startNodeId?: string | null;
18
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@clt-chatbot/scenario-core",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "files": ["dist"],
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "prepare": "npm run build"
10
+ }
11
+ }