@tiledesk/tiledesk-tybot-connector 2.5.0 → 2.6.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.
- package/CHANGELOG.md +1 -52
- package/ExtUtil.js +1 -0
- package/TdCache copy.js +242 -0
- package/TiledeskExpression.js +1 -6
- package/TiledeskWhenExpression.js +1 -18
- package/engine/MongodbBotsDataSource.js +1 -3
- package/engine/MongodbIntentsMachine.js +2 -3
- package/engine/TiledeskChatbot.js +4 -76
- package/engine/TiledeskChatbotConst.js +1 -7
- package/engine/mock/MockBotsDataSource.js +6 -11
- package/index.js +36 -132
- package/models/faq_kb.js +0 -11
- package/package.json +1 -1
- package/services/AIController.js +1 -31
- package/services/AIService.js +33 -62
- package/services/IntegrationService.js +1 -4
- package/services/{LLMService.js → KbService.js} +3 -3
- package/services/RequestService.js +2 -23
- package/services/TilebotService.js +3 -19
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +17 -34
- package/tiledeskChatbotPlugs/directives/DirAiCondition.js +51 -18
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +52 -103
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +5 -108
- package/tiledeskChatbotPlugs/directives/DirCallSubagent.js +8 -34
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +11 -36
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +4 -16
- package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -12
- package/tiledeskChatbotPlugs/directives/DirIteration.js +10 -63
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +16 -69
- package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +2 -40
- package/tiledeskChatbotPlugs/directives/DirReply.js +6 -81
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +4 -58
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +0 -8
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +1 -11
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +418 -0
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +2 -5
- package/tiledeskChatbotPlugs/directives/Directives.js +0 -6
- package/utils/HttpUtils.js +17 -99
- package/utils/TiledeskChatbotUtil.js +5 -73
- package/utils/winston.js +2 -80
- package/services/InternalSubAgentService.js +0 -330
- package/tiledeskChatbotPlugs/directives/DirInviteHuman.js +0 -259
- package/tiledeskChatbotPlugs/directives/DirInvokeSubAgent.js +0 -345
- package/tiledeskChatbotPlugs/directives/DirJSONConditionMulti.js +0 -171
- package/tiledeskChatbotPlugs/directives/DirRemoveHuman.js +0 -129
- package/tiledeskChatbotPlugs/directives/DirReturn.js +0 -98
- package/utils/ConnectorsV3.js +0 -292
- package/utils/LlmStream.js +0 -215
- package/utils/SentenceChunker.js +0 -94
- package/utils/StreamSender.js +0 -109
package/utils/ConnectorsV3.js
DELETED
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
const winston = require('./winston');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* I connettori degli agenti V3 (Design Studio V3, dal 12/10/2026).
|
|
5
|
-
*
|
|
6
|
-
* Un agente V3 (`bot.ds_version === 'v3'`) tiene i suoi collegamenti nella LISTA `bot.connectors`, non nei campi
|
|
7
|
-
* delle action (`trueIntent`, `falseIntent`, `intentName`, `goToIntent`, `elseIntent`,
|
|
8
|
-
* `cases[].intent`, `nextBlockAction.intentName`, ...): sul server quei campi possono essere
|
|
9
|
-
* vuoti. Le direttive non cambiano di una riga: prima che un blocco venga eseguito, questo
|
|
10
|
-
* modulo RISCRIVE i campi delle sue action dalla lista, cosi' ogni direttiva legge quello che
|
|
11
|
-
* ha sempre letto. Per un agente che non e' V3, o che e' V3 ma non ha ancora la lista, non fa
|
|
12
|
-
* nulla: resta la logica V2 di sempre, campi alla mano.
|
|
13
|
-
*
|
|
14
|
-
* Regola (14/10/2026): la lista AGGIUNGE e SOSTITUISCE, non toglie. Un'uscita che ha un record
|
|
15
|
-
* nella lista prende quella destinazione; un'uscita senza record resta com'e' nel JSON del blocco
|
|
16
|
-
* (vuota se il Design Studio V3 l'ha ripulita, piena se il blocco viene da un flusso vecchio o da
|
|
17
|
-
* un import). Cosi' una lista vuota o incompleta non spegne mai un collegamento che il JSON porta
|
|
18
|
-
* ancora, e un record non puo' che aggiungere strada. Per lo stesso motivo nessun contenitore
|
|
19
|
-
* (`settings`, `nextBlockAction`) nasce se la lista non ha un record da metterci.
|
|
20
|
-
*
|
|
21
|
-
* Anche i bottoni delle reply passano da qui (dal 12/10/2026 sera): nel JSON di un V3 il bottone
|
|
22
|
-
* non porta piu' `action`; prima che la reply parta verso il widget, `button.action` viene
|
|
23
|
-
* riempito dalla lista con `#id` piu' l'eventuale coda `{...}` di parametri (`props.params` del
|
|
24
|
-
* record). Il widget riceve quello che riceveva. `DirReplyV2`, che alla seconda esecuzione
|
|
25
|
-
* rilegge `button.action`, rilegge la reply gia' riempita. Un bottone senza record resta com'e'.
|
|
26
|
-
*
|
|
27
|
-
* Il pallino del blocco: se il JSON non porta piu' il descrittore `attributes.nextBlockAction`
|
|
28
|
-
* ma la lista ha il record del blocco (`kind: 'block'`), il descrittore nasce dal record, cosi'
|
|
29
|
-
* `addConnectAction` trova quello che ha sempre trovato.
|
|
30
|
-
*
|
|
31
|
-
* Il catalogo delle uscite e' la copia di `connectors/connector-catalog.ts` del Design Studio:
|
|
32
|
-
* stessi tipi, stessi campi, stessi suffissi degli id. Se cambia di la', cambia anche qui.
|
|
33
|
-
*
|
|
34
|
-
* Formato della lista: `{ version: 1, updatedAt, items: [{ id, from: { intentId, actionId, slot },
|
|
35
|
-
* to: { intentId }, kind, props? }] }`, dove `id` = `${intentId}/${actionId}${suffisso}/${toIntentId}`.
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
const COLLECTION_VERSION = 1;
|
|
39
|
-
const DS_VERSION_V3 = 'v3';
|
|
40
|
-
const KIND_BLOCK = 'block';
|
|
41
|
-
|
|
42
|
-
const field = (suffix, name) => ({ kind: 'field', suffix, field: name });
|
|
43
|
-
const nested = (suffix, path) => ({ kind: 'nested', suffix, path });
|
|
44
|
-
const lastCommand = (suffix, name) => ({ kind: 'lastCommandSetting', suffix, field: name });
|
|
45
|
-
const array = (suffix, arrayName, idField, name) => ({ kind: 'array', suffix, array: arrayName, idField, field: name });
|
|
46
|
-
/** I bottoni della action, a qualunque profondita' (come `findButtons` del Design Studio): ancora `uid`, campo `action`. */
|
|
47
|
-
const BUTTONS = { kind: 'buttons' };
|
|
48
|
-
|
|
49
|
-
const TRUE_FALSE = [field('/true', 'trueIntent'), field('/false', 'falseIntent')];
|
|
50
|
-
const VOICE_SETTINGS = [nested('/noInput', ['settings', 'noInputIntent']), nested('/noMatch', ['settings', 'noMatchIntent'])];
|
|
51
|
-
const REPLY_V1 = [BUTTONS, ...VOICE_SETTINGS];
|
|
52
|
-
|
|
53
|
-
/** Le uscite di ogni tipo di action, come nel catalogo del Design Studio. */
|
|
54
|
-
const OUTPUT_CATALOG = {
|
|
55
|
-
intent: [field('', 'intentName')],
|
|
56
|
-
connect_block: [field('', 'intentName')],
|
|
57
|
-
reply: REPLY_V1,
|
|
58
|
-
randomreply: REPLY_V1,
|
|
59
|
-
replyv2: [BUTTONS, field('/noInput', 'noInputIntent'), field('/noMatch', 'noMatchIntent')],
|
|
60
|
-
ifonlineagents: TRUE_FALSE,
|
|
61
|
-
ifonlineagentsv2: TRUE_FALSE,
|
|
62
|
-
ifopenhours: TRUE_FALSE,
|
|
63
|
-
jsonconditionmulti: [array((c) => '/case/' + c._tdCaseId, 'cases', '_tdCaseId', 'intent'), field('/else', 'elseIntent')],
|
|
64
|
-
jsoncondition: TRUE_FALSE,
|
|
65
|
-
jsoncondition2: TRUE_FALSE,
|
|
66
|
-
invoke_subagent: TRUE_FALSE,
|
|
67
|
-
askgpt: TRUE_FALSE,
|
|
68
|
-
askgptv2: TRUE_FALSE,
|
|
69
|
-
gpt_task: TRUE_FALSE,
|
|
70
|
-
gpt_assistant: TRUE_FALSE,
|
|
71
|
-
ai_prompt: TRUE_FALSE,
|
|
72
|
-
ai_condition: [array((b) => '/' + b.label + '/true', 'intents', 'label', 'conditionIntentId'), field('/fallback', 'fallbackIntent'), field('/error', 'errorIntent')],
|
|
73
|
-
webrequestv2: TRUE_FALSE,
|
|
74
|
-
data_table: TRUE_FALSE,
|
|
75
|
-
make: TRUE_FALSE,
|
|
76
|
-
hubspot: TRUE_FALSE,
|
|
77
|
-
customerio: TRUE_FALSE,
|
|
78
|
-
brevo: TRUE_FALSE,
|
|
79
|
-
n8n: TRUE_FALSE,
|
|
80
|
-
qapla: TRUE_FALSE,
|
|
81
|
-
capture_user_reply: [field('', 'goToIntent')],
|
|
82
|
-
iteration: [field('/goto', 'goToIntent'), field('/fallback', 'fallbackIntent')],
|
|
83
|
-
dtmf_menu: [BUTTONS, ...VOICE_SETTINGS, lastCommand('/noInput', 'noInputIntent'), lastCommand('/noMatch', 'noMatchIntent')],
|
|
84
|
-
dtmf_form: [BUTTONS, ...VOICE_SETTINGS, lastCommand('/noInput', 'noInputIntent'), lastCommand('/noMatch', 'noMatchIntent')],
|
|
85
|
-
speech_form: [lastCommand('/noInput', 'noInputIntent'), lastCommand('/noMatch', 'noMatchIntent')],
|
|
86
|
-
blind_transfer: [BUTTONS, ...VOICE_SETTINGS, lastCommand('/true', 'trueIntent'), lastCommand('/false', 'falseIntent')],
|
|
87
|
-
send_whatsapp: TRUE_FALSE,
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Gli indici di una lista, calcolati una volta per oggetto lista:
|
|
92
|
-
* `outputs`: `fromId -> { toId, params }` (una destinazione per uscita);
|
|
93
|
-
* `blocks`: `intentId -> { toId, actionId }` per i record del pallino (`kind: 'block'`).
|
|
94
|
-
*/
|
|
95
|
-
const indexes = new WeakMap();
|
|
96
|
-
|
|
97
|
-
class ConnectorsV3 {
|
|
98
|
-
|
|
99
|
-
/** Il bot e' stato creato dal Design Studio V3: campo `ds_version` del bot (dal 15/10/2026; prima `attributes.dsVersion`). */
|
|
100
|
-
static isV3Bot(bot) {
|
|
101
|
-
const label = bot && bot.ds_version;
|
|
102
|
-
return typeof label === 'string' && label.trim().toLowerCase() === DS_VERSION_V3;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** La lista dei connettori del bot (campo `connectors`, dal 15/10/2026; prima `attributes.connectors`), se c'e' ed e' nel formato atteso. */
|
|
106
|
-
static collectionOf(bot) {
|
|
107
|
-
const collection = bot && bot.connectors;
|
|
108
|
-
if (!collection || typeof collection !== 'object' || !Array.isArray(collection.items)) { return null; }
|
|
109
|
-
if (collection.version !== COLLECTION_VERSION) { return null; }
|
|
110
|
-
return collection;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** Il bot va eseguito dalla lista: e' V3 e la lista c'e'. */
|
|
114
|
-
static usesCollection(bot) {
|
|
115
|
-
return ConnectorsV3.isV3Bot(bot) && ConnectorsV3.collectionOf(bot) !== null;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
static indexOf(bot) {
|
|
119
|
-
const collection = ConnectorsV3.collectionOf(bot);
|
|
120
|
-
if (!collection) { return null; }
|
|
121
|
-
let index = indexes.get(collection);
|
|
122
|
-
if (index) { return index; }
|
|
123
|
-
index = { outputs: new Map(), blocks: new Map() };
|
|
124
|
-
for (const record of collection.items) {
|
|
125
|
-
if (!record || typeof record.id !== 'string' || !record.to || !record.to.intentId) { continue; }
|
|
126
|
-
const cut = record.id.lastIndexOf('/');
|
|
127
|
-
if (cut <= 0) { continue; }
|
|
128
|
-
const params = record.props && typeof record.props.params === 'string' && record.props.params !== '' ? record.props.params : null;
|
|
129
|
-
index.outputs.set(record.id.substring(0, cut), { toId: record.to.intentId, params });
|
|
130
|
-
if (record.kind === KIND_BLOCK) {
|
|
131
|
-
const parts = record.id.split('/');
|
|
132
|
-
const from = record.from && typeof record.from === 'object' ? record.from : {};
|
|
133
|
-
const intentId = from.intentId || parts[0];
|
|
134
|
-
const actionId = from.actionId || (parts.length === 3 ? parts[1] : null);
|
|
135
|
-
if (intentId && actionId && !index.blocks.has(intentId)) { index.blocks.set(intentId, { toId: record.to.intentId, actionId }); }
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
indexes.set(collection, index);
|
|
139
|
-
return index;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Riscrive dalla lista i campi di destinazione del blocco che sta per essere eseguito: il
|
|
144
|
-
* pallino (`attributes.nextBlockAction.intentName`) e ogni uscita delle action che il catalogo
|
|
145
|
-
* conosce. Un'uscita che la lista non ha resta com'e' (vuota o piena); un valore senza `#` (un
|
|
146
|
-
* nome di blocco dei flussi vecchi) resta com'e' anche con il record. Per un bot non V3, o
|
|
147
|
-
* senza lista, non tocca nulla. Restituisce quante uscite ha riscritto, o null se non ha lavorato.
|
|
148
|
-
*/
|
|
149
|
-
static projectIntent(bot, intent) {
|
|
150
|
-
if (!intent || !ConnectorsV3.isV3Bot(bot)) { return null; }
|
|
151
|
-
const index = ConnectorsV3.indexOf(bot);
|
|
152
|
-
if (!index) { return null; }
|
|
153
|
-
const intentId = intent.intent_id;
|
|
154
|
-
if (!intentId) { return null; }
|
|
155
|
-
let rewritten = 0;
|
|
156
|
-
rewritten += projectBlockDot(index, intentId, intent);
|
|
157
|
-
for (const action of intent.actions || []) {
|
|
158
|
-
if (!action || typeof action !== 'object' || !action._tdActionId) { continue; }
|
|
159
|
-
const specs = OUTPUT_CATALOG[action._tdActionType] || [];
|
|
160
|
-
for (const spec of specs) {
|
|
161
|
-
for (const slot of resolve(spec, intentId, action, index.outputs)) {
|
|
162
|
-
const entry = index.outputs.get(slot.fromId);
|
|
163
|
-
rewritten += slot.button ? writeButton(slot.button, entry) : write(slot.holder, slot.field, entry);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
winston.debug("(ConnectorsV3) intent " + intentId + ": " + rewritten + " outputs rewritten from the bot's connectors list");
|
|
168
|
-
return rewritten;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Il pallino del blocco. Con il descrittore (`_tdActionId`) l'uscita si riscrive dal record della
|
|
174
|
-
* sua ancora; senza descrittore, se la lista ha il record del blocco, il descrittore nasce da li'
|
|
175
|
-
* (`addConnectAction` lo accoda alle action come sempre). Un descrittore vecchio senza `_tdActionId`
|
|
176
|
-
* ma con un nome di blocco resta com'e'.
|
|
177
|
-
*/
|
|
178
|
-
function projectBlockDot(index, intentId, intent) {
|
|
179
|
-
const next = intent.attributes && intent.attributes.nextBlockAction;
|
|
180
|
-
if (next && typeof next === 'object' && next._tdActionId) {
|
|
181
|
-
return write(next, 'intentName', index.outputs.get(intentId + '/' + next._tdActionId));
|
|
182
|
-
}
|
|
183
|
-
const block = index.blocks.get(intentId);
|
|
184
|
-
if (!block) { return 0; }
|
|
185
|
-
if (next && typeof next === 'object' && typeof next.intentName === 'string' && next.intentName !== '') { return 0; }
|
|
186
|
-
if (!intent.attributes || typeof intent.attributes !== 'object') { intent.attributes = {}; }
|
|
187
|
-
intent.attributes.nextBlockAction = { _tdActionType: 'intent', _tdActionId: block.actionId, intentName: '#' + block.toId };
|
|
188
|
-
return 1;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Le uscite di una action secondo una voce del catalogo: dove sta il campo e l'id dell'ancora.
|
|
193
|
-
* Un contenitore che manca (`settings` della action o dell'ultimo comando) nasce solo se la lista
|
|
194
|
-
* ha un record per quell'uscita; altrimenti l'uscita non c'e' e il JSON resta com'e'.
|
|
195
|
-
*/
|
|
196
|
-
function resolve(spec, intentId, action, outputs) {
|
|
197
|
-
const base = intentId + '/' + action._tdActionId;
|
|
198
|
-
switch (spec.kind) {
|
|
199
|
-
case 'field':
|
|
200
|
-
return [{ holder: action, field: spec.field, fromId: base + spec.suffix }];
|
|
201
|
-
case 'nested': {
|
|
202
|
-
const fromId = base + spec.suffix;
|
|
203
|
-
let holder = action;
|
|
204
|
-
for (let i = 0; i < spec.path.length - 1; i++) {
|
|
205
|
-
if (holder[spec.path[i]] === undefined || holder[spec.path[i]] === null) {
|
|
206
|
-
if (!outputs.has(fromId)) { return []; }
|
|
207
|
-
holder[spec.path[i]] = {};
|
|
208
|
-
}
|
|
209
|
-
holder = holder[spec.path[i]];
|
|
210
|
-
if (typeof holder !== 'object') { return []; }
|
|
211
|
-
}
|
|
212
|
-
return [{ holder, field: spec.path[spec.path.length - 1], fromId }];
|
|
213
|
-
}
|
|
214
|
-
case 'lastCommandSetting': {
|
|
215
|
-
const fromId = base + spec.suffix;
|
|
216
|
-
const commands = action.attributes && action.attributes.commands;
|
|
217
|
-
if (!Array.isArray(commands) || commands.length === 0) { return []; }
|
|
218
|
-
const last = commands[commands.length - 1];
|
|
219
|
-
if (!last || typeof last !== 'object') { return []; }
|
|
220
|
-
if (!last.settings || typeof last.settings !== 'object') {
|
|
221
|
-
if (!outputs.has(fromId)) { return []; }
|
|
222
|
-
last.settings = {};
|
|
223
|
-
}
|
|
224
|
-
return [{ holder: last.settings, field: spec.field, fromId }];
|
|
225
|
-
}
|
|
226
|
-
case 'buttons': {
|
|
227
|
-
const out = [];
|
|
228
|
-
for (const button of findButtons(action)) {
|
|
229
|
-
if (!button || typeof button !== 'object' || button.type === 'url') { continue; }
|
|
230
|
-
if (typeof button.uid !== 'string' || button.uid === '') { continue; }
|
|
231
|
-
out.push({ holder: button, field: 'action', fromId: base + '/' + button.uid, button });
|
|
232
|
-
}
|
|
233
|
-
return out;
|
|
234
|
-
}
|
|
235
|
-
case 'array': {
|
|
236
|
-
const list = action[spec.array];
|
|
237
|
-
if (!Array.isArray(list)) { return []; }
|
|
238
|
-
const out = [];
|
|
239
|
-
for (const item of list) {
|
|
240
|
-
if (!item || typeof item !== 'object') { continue; }
|
|
241
|
-
out.push({ holder: item, field: spec.field, fromId: base + spec.suffix(item) });
|
|
242
|
-
}
|
|
243
|
-
return out;
|
|
244
|
-
}
|
|
245
|
-
default:
|
|
246
|
-
return [];
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/** Ogni `buttons[]` dentro l'oggetto, a qualunque profondita', nell'ordine di visita. */
|
|
251
|
-
function findButtons(obj) {
|
|
252
|
-
let buttons = [];
|
|
253
|
-
if (Array.isArray(obj)) {
|
|
254
|
-
for (const element of obj) { buttons = buttons.concat(findButtons(element)); }
|
|
255
|
-
} else if (obj && typeof obj === 'object') {
|
|
256
|
-
if (Array.isArray(obj.buttons)) { buttons = buttons.concat(obj.buttons); }
|
|
257
|
-
for (const key in obj) { if (key !== 'buttons') { buttons = buttons.concat(findButtons(obj[key])); } }
|
|
258
|
-
}
|
|
259
|
-
return buttons;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Riempie `button.action` dalla lista (`#id` piu' la coda `{...}` del record) e rende il bottone
|
|
264
|
-
* di tipo `action`; senza record il bottone resta com'e'. Una coda gia' presente nel bottone
|
|
265
|
-
* sopravvive se il record non ne ha una (lista scritta prima che la coda viaggiasse li').
|
|
266
|
-
*/
|
|
267
|
-
function writeButton(button, entry) {
|
|
268
|
-
if (!entry) { return 0; }
|
|
269
|
-
const current = typeof button.action === 'string' ? button.action : '';
|
|
270
|
-
const brace = current.indexOf('{');
|
|
271
|
-
const currentId = brace > -1 ? current.substring(0, brace) : current;
|
|
272
|
-
const currentTail = brace > -1 ? current.substring(brace) : '';
|
|
273
|
-
const tail = entry.params !== null ? entry.params : (currentId === '#' + entry.toId ? currentTail : '');
|
|
274
|
-
const value = '#' + entry.toId + tail;
|
|
275
|
-
if (current === value && button.type === 'action') { return 0; }
|
|
276
|
-
button.action = value;
|
|
277
|
-
button.type = 'action';
|
|
278
|
-
return 1;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/** Scrive `#id` dal record; senza record, o su un nome di blocco dei flussi vecchi, non tocca nulla. Restituisce 1 se ha cambiato qualcosa. */
|
|
282
|
-
function write(holder, field, entry) {
|
|
283
|
-
if (!entry || !entry.toId) { return 0; }
|
|
284
|
-
const current = typeof holder[field] === 'string' ? holder[field] : '';
|
|
285
|
-
if (current !== '' && !current.startsWith('#')) { return 0; } // un nome di blocco dei flussi vecchi: non si tocca
|
|
286
|
-
const value = '#' + entry.toId;
|
|
287
|
-
if (current === value) { return 0; }
|
|
288
|
-
holder[field] = value;
|
|
289
|
-
return 1;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
module.exports = { ConnectorsV3, OUTPUT_CATALOG };
|
package/utils/LlmStream.js
DELETED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
const axios = require("axios").default;
|
|
2
|
-
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
3
|
-
const { TiledeskChatbotUtil } = require("./TiledeskChatbotUtil");
|
|
4
|
-
const { SentenceChunker } = require("./SentenceChunker");
|
|
5
|
-
const { StreamSender } = require("./StreamSender");
|
|
6
|
-
const winston = require('./winston');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Voice turns: streams an LLM answer to the caller while tiledesk-llm is still generating it. Shared by the AI Prompt
|
|
10
|
-
* (/ask) and Ask Knowledge Base (/qa) actions: both answer with SSE ("chunk" events, then a "metadata" event with
|
|
11
|
-
* status "completed"). Deltas are grouped by SentenceChunker and posted to tiledesk-server /stream by StreamSender.
|
|
12
|
-
*
|
|
13
|
-
* holdMarker: a marker the model may append at the very end of its answer (e.g. "<NOANS>" for a knowledge-base miss):
|
|
14
|
-
* any trailing text that could be the start of it is held back, so the marker is never spoken; the marker found at
|
|
15
|
-
* the end is reported as result.marker and removed from result.text.
|
|
16
|
-
*
|
|
17
|
-
* run() resolves with { stopped, error, text, completed, marker, streamed }: stopped = "superseded" (a newer caller
|
|
18
|
-
* message: the request is aborted) or "closed" (the request is closed) or null.
|
|
19
|
-
*/
|
|
20
|
-
class LlmStream {
|
|
21
|
-
|
|
22
|
-
constructor(directive, { label, holdMarker } = {}) {
|
|
23
|
-
this.directive = directive;
|
|
24
|
-
this.label = label || "LLM";
|
|
25
|
-
this.holdMarker = holdMarker || null;
|
|
26
|
-
this.controller = new AbortController();
|
|
27
|
-
this.sender = new StreamSender({
|
|
28
|
-
API_ENDPOINT: directive.API_ENDPOINT,
|
|
29
|
-
projectId: directive.projectId,
|
|
30
|
-
requestId: directive.requestId,
|
|
31
|
-
token: directive.token,
|
|
32
|
-
tdcache: directive.tdcache,
|
|
33
|
-
turnId: directive.context.turnId,
|
|
34
|
-
onStop: () => this.controller.abort()
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get streamId() {
|
|
39
|
-
return this.sender.streamId;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
_timing(event) {
|
|
43
|
-
winston.info("[VoiceTiming] " + this.directive.requestId + " " + this.directive.context.turnId + " " + event + " " + Date.now());
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Text that can be spoken now: all but a trailing part that could still turn into holdMarker. */
|
|
47
|
-
_release(held) {
|
|
48
|
-
const marker = this.holdMarker;
|
|
49
|
-
if (!marker) {
|
|
50
|
-
return { speak: held, keep: "" };
|
|
51
|
-
}
|
|
52
|
-
const at = held.indexOf(marker);
|
|
53
|
-
if (at !== -1) {
|
|
54
|
-
return { speak: held.slice(0, at), keep: held.slice(at) };
|
|
55
|
-
}
|
|
56
|
-
for (let n = Math.min(marker.length - 1, held.length); n > 0; n--) {
|
|
57
|
-
if (marker.startsWith(held.slice(held.length - n))) {
|
|
58
|
-
return { speak: held.slice(0, held.length - n), keep: held.slice(held.length - n) };
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return { speak: held, keep: "" };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async run(url, body, headers) {
|
|
65
|
-
let firstChunk = true;
|
|
66
|
-
const chunker = new SentenceChunker({
|
|
67
|
-
maxWaitMs: process.env.STREAM_CHUNK_MAX_WAIT_MS,
|
|
68
|
-
firstMaxWaitMs: process.env.STREAM_CHUNK_FIRST_MAX_WAIT_MS,
|
|
69
|
-
onChunk: (text) => {
|
|
70
|
-
if (firstChunk) {
|
|
71
|
-
firstChunk = false;
|
|
72
|
-
this._timing("first_chunk");
|
|
73
|
-
}
|
|
74
|
-
this.sender.send(text);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
let text = "";
|
|
78
|
-
let held = "";
|
|
79
|
-
let completed = null;
|
|
80
|
-
let error = null;
|
|
81
|
-
let firstDelta = true;
|
|
82
|
-
this._timing("llm_request");
|
|
83
|
-
try {
|
|
84
|
-
const response = await axios.post(url, { ...body, stream: true }, {
|
|
85
|
-
headers: headers,
|
|
86
|
-
responseType: 'stream',
|
|
87
|
-
signal: this.controller.signal,
|
|
88
|
-
timeout: 60000
|
|
89
|
-
});
|
|
90
|
-
await LlmStream.readSSE(response.data, (event, data) => {
|
|
91
|
-
if (event === "chunk" && typeof data.content === "string" && data.content) {
|
|
92
|
-
if (firstDelta) {
|
|
93
|
-
firstDelta = false;
|
|
94
|
-
this._timing("llm_first_delta");
|
|
95
|
-
}
|
|
96
|
-
text += data.content;
|
|
97
|
-
const out = this._release(held + data.content);
|
|
98
|
-
held = out.keep;
|
|
99
|
-
if (out.speak) {
|
|
100
|
-
chunker.push(out.speak);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
else if (event === "metadata" && data.status === "completed") {
|
|
104
|
-
completed = data;
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
} catch (err) {
|
|
108
|
-
if (!this.sender.stopped) {
|
|
109
|
-
error = err;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
let marker = null;
|
|
113
|
-
if (this.holdMarker && held.startsWith(this.holdMarker)) {
|
|
114
|
-
marker = this.holdMarker;
|
|
115
|
-
held = held.slice(this.holdMarker.length);
|
|
116
|
-
}
|
|
117
|
-
if (held) {
|
|
118
|
-
chunker.push(held);
|
|
119
|
-
}
|
|
120
|
-
this.sender.end(chunker.flush());
|
|
121
|
-
await this.sender.drain();
|
|
122
|
-
this._timing("llm_done");
|
|
123
|
-
|
|
124
|
-
if (this.holdMarker && text.includes(this.holdMarker)) {
|
|
125
|
-
marker = this.holdMarker;
|
|
126
|
-
text = text.split(this.holdMarker).join("");
|
|
127
|
-
}
|
|
128
|
-
return { stopped: this.sender.stopped || null, error, text: text.trim(), completed, marker,
|
|
129
|
-
streamed: this.sender.streamed && !this.sender.disabled, chunks: this.sender.seq };
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** The reply that speaks `answer` (DirReply) gets attributes.stream_id: the channel closes the stream instead of re-speaking it. */
|
|
133
|
-
async savePending(answer) {
|
|
134
|
-
if (this.sender.streamed && !this.sender.disabled) {
|
|
135
|
-
const { TiledeskChatbot } = require("../engine/TiledeskChatbot"); // lazy: engine <-> directives require cycle
|
|
136
|
-
await this.directive.tdcache.setJSON(TiledeskChatbot.pendingStreamCacheKey(this.directive.requestId),
|
|
137
|
-
{ stream_id: this.sender.streamId, text: answer }, { EX: 120 });
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/** Posts what the caller heard of an interrupted answer, so the conversation keeps it. */
|
|
142
|
-
async sendInterrupted() {
|
|
143
|
-
if (!this.sender.streamed) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
const d = this.directive;
|
|
147
|
-
const message = {
|
|
148
|
-
text: this.sender.sentText,
|
|
149
|
-
senderFullname: d.chatbot.bot.name,
|
|
150
|
-
attributes: { stream_id: this.sender.streamId, interrupted: true }
|
|
151
|
-
};
|
|
152
|
-
try {
|
|
153
|
-
await TiledeskChatbotUtil.updateConversationTranscript(d.chatbot, message);
|
|
154
|
-
const tdClient = new TiledeskClient({ projectId: d.projectId, token: d.token, APIURL: d.API_ENDPOINT, APIKEY: "___" });
|
|
155
|
-
await new Promise((resolve) => {
|
|
156
|
-
tdClient.sendSupportMessage(d.requestId, message, (err) => {
|
|
157
|
-
if (err) {
|
|
158
|
-
winston.error(this.label + " error sending the interrupted answer: " + (err.message || err));
|
|
159
|
-
}
|
|
160
|
-
resolve();
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
} catch (err) {
|
|
164
|
-
winston.error(this.label + " error sending the interrupted answer: " + err.message);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** Reads a text/event-stream body, calling onEvent(eventName, parsedData) for every event. */
|
|
169
|
-
static readSSE(stream, onEvent) {
|
|
170
|
-
return new Promise((resolve, reject) => {
|
|
171
|
-
let buffer = "";
|
|
172
|
-
const dispatch = (block) => {
|
|
173
|
-
let event = "message";
|
|
174
|
-
const data = [];
|
|
175
|
-
block.split(/\r?\n/).forEach((line) => {
|
|
176
|
-
if (line.startsWith("event:")) {
|
|
177
|
-
event = line.slice(6).trim();
|
|
178
|
-
} else if (line.startsWith("data:")) {
|
|
179
|
-
data.push(line.slice(5).replace(/^ /, ""));
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
if (data.length === 0) {
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
let parsed;
|
|
186
|
-
try {
|
|
187
|
-
parsed = JSON.parse(data.join("\n"));
|
|
188
|
-
} catch (err) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
onEvent(event, parsed);
|
|
192
|
-
};
|
|
193
|
-
stream.setEncoding("utf8");
|
|
194
|
-
stream.on("data", (chunk) => {
|
|
195
|
-
buffer += chunk;
|
|
196
|
-
let sep;
|
|
197
|
-
while ((sep = buffer.search(/\r?\n\r?\n/)) !== -1) {
|
|
198
|
-
const block = buffer.slice(0, sep);
|
|
199
|
-
buffer = buffer.slice(sep).replace(/^\r?\n\r?\n/, "");
|
|
200
|
-
dispatch(block);
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
stream.on("end", () => {
|
|
204
|
-
if (buffer.trim()) {
|
|
205
|
-
dispatch(buffer);
|
|
206
|
-
}
|
|
207
|
-
resolve();
|
|
208
|
-
});
|
|
209
|
-
stream.on("error", reject);
|
|
210
|
-
stream.on("close", resolve);
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
module.exports = { LlmStream };
|
package/utils/SentenceChunker.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Groups LLM deltas into speakable chunks for streaming channels (voice).
|
|
3
|
-
*
|
|
4
|
-
* A chunk is emitted at the last sentence/clause boundary in the buffer (". ! ? ; :" or a newline, followed by
|
|
5
|
-
* whitespace), or, when no boundary arrives within maxWaitMs from the first buffered character, at the last space
|
|
6
|
-
* (never inside a word). Emitted chunks concatenated are exactly the input text.
|
|
7
|
-
* firstMaxWaitMs (default: maxWaitMs) applies to the first chunk only: a low value starts the speech sooner without
|
|
8
|
-
* splitting the rest of the answer into many small chunks.
|
|
9
|
-
*
|
|
10
|
-
* Example:
|
|
11
|
-
* const chunker = new SentenceChunker({ maxWaitMs: 150, onChunk: (text) => send(text) });
|
|
12
|
-
* chunker.push(delta); ...; const rest = chunker.flush();
|
|
13
|
-
*/
|
|
14
|
-
const BOUNDARY = /[.!?;:\n]\s+|\n/g;
|
|
15
|
-
|
|
16
|
-
class SentenceChunker {
|
|
17
|
-
|
|
18
|
-
constructor(options) {
|
|
19
|
-
options = options || {};
|
|
20
|
-
this.maxWaitMs = Number(options.maxWaitMs) >= 0 ? Number(options.maxWaitMs) : 150;
|
|
21
|
-
const first = options.firstMaxWaitMs;
|
|
22
|
-
this.firstMaxWaitMs = (first !== undefined && first !== null && first !== "" && Number(first) >= 0) ? Number(first) : this.maxWaitMs;
|
|
23
|
-
this.emitted = 0;
|
|
24
|
-
this.onChunk = options.onChunk || (() => {});
|
|
25
|
-
this.buffer = "";
|
|
26
|
-
this.timer = null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
push(delta) {
|
|
30
|
-
if (!delta) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
this.buffer += delta;
|
|
34
|
-
const cut = SentenceChunker.lastBoundary(this.buffer);
|
|
35
|
-
if (cut > 0) {
|
|
36
|
-
this.#emit(cut);
|
|
37
|
-
}
|
|
38
|
-
this.#armTimer();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Stops the timer and returns what is left (possibly ""), without emitting it. */
|
|
42
|
-
flush() {
|
|
43
|
-
this.#clearTimer();
|
|
44
|
-
const rest = this.buffer;
|
|
45
|
-
this.buffer = "";
|
|
46
|
-
return rest;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
static lastBoundary(text) {
|
|
50
|
-
let cut = -1;
|
|
51
|
-
let match;
|
|
52
|
-
BOUNDARY.lastIndex = 0;
|
|
53
|
-
while ((match = BOUNDARY.exec(text)) !== null) {
|
|
54
|
-
cut = match.index + match[0].length;
|
|
55
|
-
}
|
|
56
|
-
return cut;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
#emit(cut) {
|
|
60
|
-
const chunk = this.buffer.slice(0, cut);
|
|
61
|
-
this.buffer = this.buffer.slice(cut);
|
|
62
|
-
this.#clearTimer();
|
|
63
|
-
if (chunk) {
|
|
64
|
-
this.emitted++;
|
|
65
|
-
this.onChunk(chunk);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
#armTimer() {
|
|
70
|
-
if (this.timer || !this.buffer) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
const wait = this.emitted === 0 ? this.firstMaxWaitMs : this.maxWaitMs;
|
|
74
|
-
this.timer = setTimeout(() => {
|
|
75
|
-
this.timer = null;
|
|
76
|
-
const space = this.buffer.lastIndexOf(" ");
|
|
77
|
-
if (space > 0) {
|
|
78
|
-
this.#emit(space + 1);
|
|
79
|
-
this.#armTimer();
|
|
80
|
-
}
|
|
81
|
-
// no word boundary yet: the next push arms the timer again (no busy loop with a 0 ms wait)
|
|
82
|
-
}, wait);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
#clearTimer() {
|
|
86
|
-
if (this.timer) {
|
|
87
|
-
clearTimeout(this.timer);
|
|
88
|
-
this.timer = null;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
module.exports = { SentenceChunker };
|