-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimrasa.html
More file actions
500 lines (461 loc) · 18.8 KB
/
Copy pathsimrasa.html
File metadata and controls
500 lines (461 loc) · 18.8 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<!-- node-red-contrib-rasa-actionserver v1.3.0
Copyright (c) 2023 Irene Weber
MIT License (http://www.opensource.org/licenses/mit-license.php)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -->
<script type="text/html" data-template-name="simrasa">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> name</label>
<input type="text" id="node-input-name">
</div>
<div class="form-row" id="node-once">
<label for="node-input-once">send errors to catch node</label>
<input type="checkbox" id="node-input-errtocatch" style="display:inline-block; width:15px; vertical-align:baseline;">
</div>
<div class="form-row">
<label for="node-input-action"><i class="fa fa-cog"></i> action</label>
<input type="text" id="node-input-action">
</div>
<div class="form-row node-input-slot-container-row">
<ol id="node-input-slot-container"></ol>
</div>
</script>
</script>
<script type="text/javascript">
(function () {
function prepareProps(actn, slts) {
var theprops = {};
var theslots = [];
slts.each(function (i) {
var slot = $(this);
var slt_name_v = slot.find(".node-input-slot-slotname").typedInput('value');
var slt_name_t = slot.find(".node-input-slot-slotname").typedInput('type');
var slt_value_v = slot.find(".node-input-slot-slotvalue").typedInput('value');
var slt_value_t = slot.find(".node-input-slot-slotvalue").typedInput('type');
theslots.push({ "slotname": slt_name_v, "slotname-type": slt_name_t, "slotvalue": slt_value_v, "slotvalue-type": slt_value_t });
});
theprops["next_action"] = actn.typedInput('value');
theprops["slots"] = theslots;
return theprops;
}
/** Perform inject */
function doInject(node, customMsg) {
var label = node._def.label.call(node, customMsg ? customMsg.__user_inject_props__ : undefined);
if (label.length > 30) {
label = label.substring(0, 50) + "...";
}
label = label.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
$.ajax({
url: "inject/" + node.id,
type: "POST",
data: JSON.stringify(customMsg || {}),
contentType: "application/json; charset=utf-8",
success: function (resp) {
RED.notify(node._("inject.success", { label: label }), { type: "success", id: "inject", timeout: 2000 });
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 404) {
RED.notify(node._("common.notification.error", { message: node._("common.notification.errors.not-deployed") }), "error");
} else if (jqXHR.status == 500) {
RED.notify(node._("common.notification.error", { message: node._("inject.errors.failed") }), "error");
} else if (jqXHR.status == 0) {
RED.notify(node._("common.notification.error", { message: node._("common.notification.errors.no-response") }), "error");
} else {
RED.notify(node._("common.notification.error", { message: node._("common.notification.errors.unexpected", { status: jqXHR.status, message: textStatus }) }), "error");
}
}
});
}
RED.nodes.registerType('simrasa', {
category: 'rasaas',
color: "#baaff9",
defaults: {
name: { value: "" },
action: {
value: "",
required: true
},
errtocatch: { value: false },
slots: {
value: [{ slotname: "", "slotname-type": "str", slotvalue: "", "slotvalue-type": "str" }], validate: function (ss) {
if (!ss || ss.length === 0) { return true }
for (var i = 0; i < ss.length; i++) {
if (!ss[i].slotname || ss[i].slotname.length === 0) { return RED._("node-red:inject.errors.invalid-prop", { prop: 'msg.' + ss[i], error: "Slot " + i }) }
else if (ss[i]["slotvalue-type"] === "num") {
if (!/^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(ss[i].slotvalue)) {
return RED._("node-red:inject.errors.invalid-prop", { prop: 'msg.' + ss[i], error: "Slot " + i });
}
}
}
return true;
}
},
},
inputs: 0,
outputs: 1,
icon: "font-awesome/fa-quote-right",
label: function () {
return this.name || "simrasa";
},
labelStyle: function () {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function () {
var node = this;
var slots = node.slots;
$('#node-input-action').typedInput({
default: '',
typeField: $("#node-input-actionType"),
types: ['str']
});
$("#node-input-slot-container").css('min-height', '100px').css('min-width', '450px').editableList({
buttons: [
{
id: "node-simrasa-test-inject-button",
icon: "fa fa-quote-right",
label: "Send now (never to catch node!)",
click: function (e) {
let actn = $('#node-input-action');
let items = $("#node-input-slot-container").editableList('items');
let props = prepareProps(actn, items);
let m = { __user_inject_props__: props };
doInject(node, m);
}
}
],
addButton: "add slot",
header: $("<div>").append($.parseHTML("<div style='width:65%; display: inline-grid'> slot name</div><div style='display: inline-grid'>slot value</div>")),
addItem: function (row, index, data) {
var slt = {}
if (data.hasOwnProperty("slot")) {
slt.slotname = data.slot.slotname;
slt["slotname-type"] = data.slot["slotname-type"];
slt.slotvalue = data.slot.slotvalue;
slt["slotvalue-type"] = data.slot["slotvalue-type"];
} else {
slt.slotname = null;
slt["slotname-type"] = "str";
slt.slotvalue = null;
slt["slotvalue-type"] = "str";
}
row.css({
overflow: 'hidden',
whiteSpace: 'nowrap'
});
var slotnameField = $('<slotname/>', { class: "node-input-slot-slotname", type: "text", style: "margin-left: 5px; width:200px;" }).appendTo(row).typedInput({ default: 'str', types: ['str'], required: true });
slotnameField.typedInput('value', slt.slotname);
slotnameField.typedInput('type', slt["slotname-type"]);
$('<div/>', { style: 'display:inline-block; padding:0px 6px;' }).text('=').appendTo(row)
var slotvalueField = $('<slotvalue/>', { class: "node-input-slot-slotvalue", type: "text", style: "width:200px; padding-right:8px; box-sizing:border-box;" }).appendTo(row).typedInput({ default: 'str', types: ['str', 'num', 'bool'], required: true });
slotvalueField.typedInput('value', slt.slotvalue);
slotvalueField.typedInput('type', slt["slotvalue-type"]);
var finalspan = $('<span/>', { style: "float: right;margin-top: 6px;" }).appendTo(row);
},
sortable: true,
removable: true
});
for (var i = 0; i < node.slots.length; i++) {
var slot = node.slots[i];
$("#node-input-slot-container").editableList('addItem', { slot: slot, i: i });
};
},
oneditsave: function () {
var node = this;
$("ol.node-input-slot-container").editableList('height', '200px');
var slts = $("#node-input-slot-container").editableList('items');
node.slots = [];
slts.each(function (i) {
let slt = $(this);
let s = {};
s.slotname = slt.find(".node-input-slot-slotname").typedInput('value');
let slotnametype = slt.find(".node-input-slot-slotname").typedInput('type');
s["slotname-type"] = slotnametype;
s.slotvalue = slt.find(".node-input-slot-slotvalue").typedInput('value');
let slotvaluetype = slt.find(".node-input-slot-slotvalue").typedInput('type');
s["slotvalue-type"] = slotvaluetype;
node.slots.push(s);
});
},
oneditresize: function (size) {
var rows = $("#dialog-form>div:not(.node-input-slot-container-row)");
var height = size.height;
for (var i = 0; i < rows.length; i++) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-input-slot-container-row");
height -= (parseInt(editorRow.css("marginTop")) + parseInt(editorRow.css("marginBottom")));
height += 16;
$("#node-input-slot-container").editableList('height', height);
},
button: {
enabled: function () {
return !this.changed
},
onclick: function () {
if (this.changed) {
return RED.notify(RED._("notification.warning", { message: RED._("notification.warnings.undeployedChanges") }), "warning");
}
doInject(this);
}
},
});
})();
</script>
<script type="text/html" data-help-name="simrasa">
Simulates a Rasa-like message for testing action servers without requiring a full Rasa instance.
It lets you inject an action and set slots with values, making it easier to debug and test action server flows in Node-RED.
</p>
<h3>Inputs</h3>
<p>This node does not require an external input and acts as a standalone injector node, similar to the <code>Inject</code> node.</p>
<dl class="message-properties">
<dt>Action</dt>
<dd>You must configure an action name in the node settings.</dd>
<dt>Slots</dt>
<dd>You can configure an arbitrary number of slots, like the slot data sent by Rasa.</dd>
</dl>
<h3>Outputs</h3>
<ol class="node-ports">
<li>
Output Message
<dl class="message-properties">
<dt>msg.payload.next_action</dt>
<dd>The name of the action being tested.</dd>
<dt>msg.payload.tracker.slots</dt>
<dd>
An object representing the slots and their values. For example:
<pre>
{
"name": "Kim",
"age": 45
}
</pre>
</dd>
</dl>
</li>
</ol>
<h3>Details</h3>
<p>
This node generates a <code>msg</code> object that contains a minimal set of elements required for testing Rasa action server flows locally.
<ol>
<li>Use this node in place of the <code>http in</code> node of your action server flow. </li>
<li>Use a <code>debug</code> node to show the <code>msg.payload</code> generated by your entire flow. This <code>msg.payload</code> contains the results of the actions that will be sent to Rasa.</li>
<li>Unwire the <code>http response</code> node for testing purposes, as otherwise it will generate an error.</li>
</ol>
</p>
<h4>Configuration Options:</h4>
<dl class="message-properties">
<dt>Name</dt>
<dd>An optional name for the node.</dd>
<dt>Action</dt>
<dd>The name of the action to simulate (mandatory).</dd>
<dt>Slots</dt>
<dd>
Configure slot names and values. Each slot can be:
<ul>
<li>A static value (e.g., string, number, or boolean).</li>
<li>Dynamically resolved from the <code>msg</code> object or context.</li>
</ul>
</dd>
<dt>Send Errors to Catch Node</dt>
<dd>
If enabled, errors encountered during slot evaluation will be routed to a connected <code>catch</code> node
instead of propagating to the flow.
</dd>
</dl>
<h3>Example Output</h3>
<p>
When configured with the action <code>greet</code> and slots <code>{"location": "Paris", "persons": 3}</code>,
the node produces the following output:
</p>
<pre>
{
"payload": {
"next_action": "greet",
"tracker": {
"slots": {
"location": "Paris",
"persons": 3
}
}
},
"req": "dummy",
"res": "dummy"
}
</pre>
<h3>Example Flow</h3>
<pre>
[
{
"id": "b82219eabc977146",
"type": "simrasa",
"z": "039d55cbf649dc40",
"name": "",
"action": "some_action",
"errtocatch": false,
"slots": [
{
"slotname": "location",
"slotname-type": "str",
"slotvalue": "Paris",
"slotvalue-type": "str"
},
{
"slotname": "persons",
"slotname-type": "str",
"slotvalue": "3",
"slotvalue-type": "num"
}
],
"x": 170,
"y": 60,
"wires": [
[
"35d6951107d050a0"
]
]
},
{
"id": "35d6951107d050a0",
"type": "init",
"z": "039d55cbf649dc40",
"name": "",
"x": 310,
"y": 120,
"wires": [
[
"e8806d6d2cca28a8"
]
]
},
{
"id": "e8806d6d2cca28a8",
"type": "template",
"z": "039d55cbf649dc40",
"name": "",
"field": "url",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "https://en.wikipedia.org/w/api.php?action=opensearch&limit=1&namespace=0&format=json&search={{slots.location}}",
"output": "str",
"x": 440,
"y": 120,
"wires": [
[
"31553d2c7681a571"
]
]
},
{
"id": "31553d2c7681a571",
"type": "http request",
"z": "039d55cbf649dc40",
"name": "",
"method": "GET",
"ret": "obj",
"paytoqs": "ignore",
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"x": 590,
"y": 120,
"wires": [
[
"c9d614e98b1ec9b8"
]
]
},
{
"id": "c9d614e98b1ec9b8",
"type": "sendextra",
"z": "039d55cbf649dc40",
"name": "",
"position": "",
"positionType": "num",
"text": "You can find info here!",
"textType": "str",
"image": "",
"imageType": "str",
"attachment": "payload[3][0]",
"attachmentType": "msg",
"senderr": false,
"x": 740,
"y": 120,
"wires": [
[
"ecddc323b33832dc"
]
]
},
{
"id": "ecddc323b33832dc",
"type": "finish",
"z": "039d55cbf649dc40",
"name": "",
"x": 870,
"y": 120,
"wires": [
[
"3f879572c79059d1"
]
]
},
{
"id": "3f879572c79059d1",
"type": "debug",
"z": "039d55cbf649dc40",
"name": "msg.payload",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 970,
"y": 40,
"wires": []
},
{
"id": "9becf6602a92b5af",
"type": "http in",
"z": "039d55cbf649dc40",
"name": "",
"url": "/webhook",
"method": "post",
"upload": false,
"swaggerDoc": "",
"x": 160,
"y": 180,
"wires": [
[
"35d6951107d050a0"
]
]
},
{
"id": "7704ba2aaaa27434",
"type": "http response",
"z": "039d55cbf649dc40",
"name": "",
"statusCode": "",
"headers": {},
"x": 970,
"y": 180,
"wires": []
}
]
</pre>
<h3>References</h3>
<ul>
<li><a href="https://rasa.com/docs/action-server/http-api-spec" target="_blank">Rasa Action Server HTTP API</a></li>
<li><a href="https://github.com/weberi/node-red-contrib-rasa-actionserver.git" target="_blank">GitHub Repository</a></li>
</ul>
</script>