@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.
Files changed (50) hide show
  1. package/CHANGELOG.md +1 -52
  2. package/ExtUtil.js +1 -0
  3. package/TdCache copy.js +242 -0
  4. package/TiledeskExpression.js +1 -6
  5. package/TiledeskWhenExpression.js +1 -18
  6. package/engine/MongodbBotsDataSource.js +1 -3
  7. package/engine/MongodbIntentsMachine.js +2 -3
  8. package/engine/TiledeskChatbot.js +4 -76
  9. package/engine/TiledeskChatbotConst.js +1 -7
  10. package/engine/mock/MockBotsDataSource.js +6 -11
  11. package/index.js +36 -132
  12. package/models/faq_kb.js +0 -11
  13. package/package.json +1 -1
  14. package/services/AIController.js +1 -31
  15. package/services/AIService.js +33 -62
  16. package/services/IntegrationService.js +1 -4
  17. package/services/{LLMService.js → KbService.js} +3 -3
  18. package/services/RequestService.js +2 -23
  19. package/services/TilebotService.js +3 -19
  20. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +17 -34
  21. package/tiledeskChatbotPlugs/directives/DirAiCondition.js +51 -18
  22. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +52 -103
  23. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +5 -108
  24. package/tiledeskChatbotPlugs/directives/DirCallSubagent.js +8 -34
  25. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +11 -36
  26. package/tiledeskChatbotPlugs/directives/DirGptTask.js +4 -16
  27. package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -12
  28. package/tiledeskChatbotPlugs/directives/DirIteration.js +10 -63
  29. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +16 -69
  30. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +2 -40
  31. package/tiledeskChatbotPlugs/directives/DirReply.js +6 -81
  32. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +4 -58
  33. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +0 -8
  34. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +1 -11
  35. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +418 -0
  36. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +2 -5
  37. package/tiledeskChatbotPlugs/directives/Directives.js +0 -6
  38. package/utils/HttpUtils.js +17 -99
  39. package/utils/TiledeskChatbotUtil.js +5 -73
  40. package/utils/winston.js +2 -80
  41. package/services/InternalSubAgentService.js +0 -330
  42. package/tiledeskChatbotPlugs/directives/DirInviteHuman.js +0 -259
  43. package/tiledeskChatbotPlugs/directives/DirInvokeSubAgent.js +0 -345
  44. package/tiledeskChatbotPlugs/directives/DirJSONConditionMulti.js +0 -171
  45. package/tiledeskChatbotPlugs/directives/DirRemoveHuman.js +0 -129
  46. package/tiledeskChatbotPlugs/directives/DirReturn.js +0 -98
  47. package/utils/ConnectorsV3.js +0 -292
  48. package/utils/LlmStream.js +0 -215
  49. package/utils/SentenceChunker.js +0 -94
  50. package/utils/StreamSender.js +0 -109
@@ -1,109 +0,0 @@
1
- const axios = require("axios").default;
2
- const { v4: uuidv4 } = require('uuid');
3
- const winston = require('./winston');
4
- const { TiledeskChatbot } = require('../engine/TiledeskChatbot');
5
-
6
- /**
7
- * Posts the chunks of one streamed answer to tiledesk-server (POST /:project_id/requests/:request_id/stream),
8
- * one at a time and in order, without blocking the caller (send() only queues).
9
- *
10
- * Before every chunk it checks the voice turn: when a newer caller message superseded it, nothing more is sent and
11
- * onStop("superseded") fires. The route's 404 (request missing/closed) stops with onStop("closed"). Any other error
12
- * (503: stream not available; 404 of a server without the route) disables streaming for this answer: the complete
13
- * message is still sent by the flow as usual.
14
- */
15
- class StreamSender {
16
-
17
- constructor(options) {
18
- this.url = options.API_ENDPOINT + "/" + options.projectId + "/requests/" + options.requestId + "/stream";
19
- this.token = options.token;
20
- this.tdcache = options.tdcache;
21
- this.requestId = options.requestId;
22
- this.turnId = options.turnId;
23
- this.onStop = options.onStop || (() => {});
24
- this.timeout = options.timeout || 5000;
25
- this.streamId = options.streamId || uuidv4();
26
- this.seq = 0;
27
- this.sentText = "";
28
- this.ended = false;
29
- this.disabled = false;
30
- this.stopped = null; // "superseded" | "closed"
31
- this.chain = Promise.resolve();
32
- }
33
-
34
- send(delta, streamEnd) {
35
- this.chain = this.chain.then(() => this.#post(delta, streamEnd === true));
36
- return this.chain;
37
- }
38
-
39
- end(delta) {
40
- return this.send(delta || "", true);
41
- }
42
-
43
- /** Resolves when every queued chunk has been handled. */
44
- drain() {
45
- return this.chain;
46
- }
47
-
48
- /** true when the caller has heard (part of) this answer through the stream. */
49
- get streamed() {
50
- return this.seq > 0;
51
- }
52
-
53
- async #post(delta, streamEnd) {
54
- if (this.disabled || this.stopped || this.ended) {
55
- return;
56
- }
57
- if (!delta && !streamEnd) {
58
- return;
59
- }
60
- if (streamEnd && this.seq === 0 && !delta) {
61
- // nothing was streamed: no stream to close
62
- this.ended = true;
63
- return;
64
- }
65
- try {
66
- if (await TiledeskChatbot.isTurnSuperseded(this.tdcache, this.requestId, this.turnId)) {
67
- this.#stop("superseded");
68
- return;
69
- }
70
- } catch (err) {
71
- winston.error("(StreamSender) Error checking the voice turn: " + err.message);
72
- }
73
- try {
74
- await axios.post(this.url,
75
- { stream_id: this.streamId, seq: this.seq, delta: delta, stream_end: streamEnd },
76
- { headers: { "Content-Type": "application/json", "Authorization": "JWT " + this.token }, timeout: this.timeout });
77
- this.seq++;
78
- this.sentText += delta;
79
- if (streamEnd) {
80
- this.ended = true;
81
- }
82
- } catch (err) {
83
- const status = err.response && err.response.status;
84
- const body = err.response && err.response.data;
85
- // tiledesk-server's stream route answers 404 with this message when the request is missing or closed; any other
86
- // 404 means the route does not exist (server without streaming): not a reason to stop the conversation
87
- if (status === 404 && body && body.msg === "Request not found or closed.") {
88
- this.#stop("closed");
89
- } else {
90
- // never log err.config: it carries the Authorization header
91
- winston.warn("(StreamSender) streaming disabled for " + this.streamId + ": " + (status || err.code || err.message));
92
- this.disabled = true;
93
- }
94
- }
95
- }
96
-
97
- #stop(reason) {
98
- this.stopped = reason;
99
- winston.verbose("(StreamSender) stream " + this.streamId + " stopped: " + reason);
100
- try {
101
- this.onStop(reason);
102
- } catch (err) {
103
- winston.error("(StreamSender) onStop error: " + err.message);
104
- }
105
- }
106
-
107
- }
108
-
109
- module.exports = { StreamSender };