@tiledesk/tiledesk-tybot-connector 2.1.10 → 2.1.12

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 CHANGED
@@ -7,7 +7,7 @@ available on:
7
7
 
8
8
 
9
9
  # this branch
10
- - added: JSON Condition V2 runtime — new `jsoncondition2` type routed to `DirJSONConditionV2` (evaluates the `when` expression via `TiledeskWhenExpression`, no eval/vm2); V1 `jsoncondition` dispatch left unchanged (`DirJSONCondition`) for full backward-compatibility
10
+ - added: Web Request V2 — support for `bodyType: 'raw'` with `rawType` `'text' | 'javascript' | 'json' | 'html' | 'xml'` (raw string body sent verbatim with the Content-Type set in headers; `'json'` keeps the legacy parse/falseIntent semantics); existing `'json'`, `'form-data'` and `'none'` body types unchanged
11
11
 
12
12
  # 2.1.0
13
13
  - added: support to analytics
@@ -0,0 +1,18 @@
1
+ var mongoose = require('mongoose');
2
+ var Schema = mongoose.Schema;
3
+
4
+ /**
5
+ * Read-only mirror of the `integrations` collection owned by tiledesk-server
6
+ * (models/integrations.js). Integrations are created and updated exclusively
7
+ * through the server API — the connector only reads them — so the schema
8
+ * declares just what it needs and stays non-strict for everything else.
9
+ */
10
+ var IntegrationSchema = new Schema({
11
+ id_project: String,
12
+ name: String,
13
+ value: Object
14
+ }, { strict: false, collection: 'integrations' });
15
+
16
+ var integration = mongoose.model('integration', IntegrationSchema, 'integrations');
17
+
18
+ module.exports = integration
@@ -0,0 +1,31 @@
1
+ var mongoose = require('mongoose');
2
+ var Schema = mongoose.Schema;
3
+
4
+ /**
5
+ * Read-only mirror of the `namespaces` collection owned by tiledesk-server
6
+ * (models/kb_setting.js). The connector only reads from it — namespaces are
7
+ * created and updated exclusively through the server API — so the schema
8
+ * declares just the fields it needs and stays non-strict for everything else.
9
+ */
10
+ var EngineSchema = new Schema({
11
+ name: String,
12
+ type: String,
13
+ apikey: String,
14
+ vector_size: Number,
15
+ index_name: String,
16
+ host: String,
17
+ port: String,
18
+ deployment: String
19
+ }, { _id: false, strict: false });
20
+
21
+ var NamespaceSchema = new Schema({
22
+ id: String,
23
+ id_project: String,
24
+ name: String,
25
+ hybrid: Boolean,
26
+ engine: EngineSchema
27
+ }, { strict: false, collection: 'namespaces' });
28
+
29
+ var namespace = mongoose.model('namespace', NamespaceSchema, 'namespaces');
30
+
31
+ module.exports = namespace
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "2.1.10",
3
+ "version": "2.1.12",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,44 +1,75 @@
1
+ const mongoose = require('mongoose');
1
2
  const httpUtils = require('../utils/HttpUtils');
2
3
  const winston = require('../utils/winston');
4
+ const Integration = require('../models/integration');
3
5
  const API_ENDPOINT = process.env.API_ENDPOINT;
4
6
 
7
+ // Secret fields the API masks (utils/maskIntegrationSecrets.js in tiledesk-server).
8
+ const SECRET_KEYS = ['apikey', 'api_key', 'token', 'secret', 'client_secret', 'password'];
9
+
10
+ /**
11
+ * Integrations hold the credentials a project configured: provider api keys,
12
+ * ollama/vllm tokens, third party keys.
13
+ *
14
+ * GET /:id_project/integration/name/:name masks them (`sk-********2bVU`) since
15
+ * tiledesk-server 2.21.0, so that a credential cannot be read back out of the
16
+ * API. A masked key is still a non-empty string: nothing here would fail, the
17
+ * provider would just answer 401. So the values are read from the database this
18
+ * connector is already connected to, and the API is kept as the fallback for
19
+ * when there is no database (static bots, tests, embedded usage).
20
+ */
5
21
  class IntegrationService {
6
22
 
7
- constructor() { }
23
+ constructor() {
24
+ // Instance fields so tests can substitute stubs.
25
+ this.model = Integration;
26
+ this.httpUtils = httpUtils;
27
+ }
8
28
 
9
- async getKeyFromIntegrations(id_project, integration_name, token) {
10
- return new Promise((resolve) => {
29
+ isConnected() {
30
+ return mongoose.connection.readyState === 1;
31
+ }
11
32
 
12
- const INTEGRATIONS_HTTPREQUEST = {
13
- url: API_ENDPOINT + "/" + id_project + "/integration/name/" + integration_name,
14
- headers: {
15
- 'Content-Type': 'application/json',
16
- 'Authorization': 'JWT ' + token
17
- },
18
- method: "GET"
19
- }
20
- winston.debug("Integration HttpRequest ", INTEGRATIONS_HTTPREQUEST)
33
+ // Same rule as isMaskedSecret in tiledesk-server: masking inserts asterisks.
34
+ looksMasked(value) {
35
+ return typeof value === 'string' && value.includes('*');
36
+ }
21
37
 
22
- httpUtils.request(
23
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
24
- if (err) {
25
- resolve(null);
26
- } else {
38
+ /**
39
+ * The integration document, credentials included, or null when the database
40
+ * cannot answer. Scoped by project: an integration name alone must never be
41
+ * enough to reach another project's credentials.
42
+ */
43
+ async getIntegrationFromDb(id_project, integration_name) {
44
+ if (!id_project || !integration_name) {
45
+ winston.debug("IntegrationService: id_project and integration_name are both required");
46
+ return null;
47
+ }
27
48
 
28
- if (integration &&
29
- integration.value) {
30
- resolve(integration.value.apikey)
31
- }
32
- else {
33
- resolve(null)
34
- }
35
- }
36
- })
37
- })
38
- }
49
+ if (!this.isConnected()) {
50
+ winston.verbose("IntegrationService: no database connection, falling back to the API");
51
+ return null;
52
+ }
39
53
 
40
- async getIntegration(id_project, integration_name, token) {
54
+ try {
55
+ const integration = await this.model
56
+ .findOne({ id_project: id_project, name: integration_name })
57
+ .lean()
58
+ .exec();
59
+
60
+ if (!integration) {
61
+ winston.verbose("IntegrationService: integration " + integration_name + " not found for project " + id_project);
62
+ return null;
63
+ }
41
64
 
65
+ return integration;
66
+ } catch (err) {
67
+ winston.error("IntegrationService getIntegrationFromDb error: ", err);
68
+ return null;
69
+ }
70
+ }
71
+
72
+ async getIntegrationFromApi(id_project, integration_name, token) {
42
73
  return new Promise((resolve) => {
43
74
 
44
75
  const INTEGRATIONS_HTTPREQUEST = {
@@ -51,18 +82,56 @@ class IntegrationService {
51
82
  }
52
83
  winston.debug("Integration HttpRequest ", INTEGRATIONS_HTTPREQUEST)
53
84
 
54
- httpUtils.request(
85
+ this.httpUtils.request(
55
86
  INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
56
87
  if (err) {
57
88
  resolve(null);
58
89
  } else {
90
+ this.warnIfMasked(integration, id_project, integration_name);
59
91
  resolve(integration)
60
92
  }
61
93
  })
62
94
  })
63
95
  }
64
96
 
97
+ // The API answer is the fallback, and it carries masked credentials: say so,
98
+ // otherwise the provider's 401 is the only clue left.
99
+ warnIfMasked(integration, id_project, integration_name) {
100
+ const value = integration && integration.value;
101
+ if (!value || typeof value !== 'object') {
102
+ return;
103
+ }
104
+
105
+ const masked = SECRET_KEYS.some((key) => this.looksMasked(value[key])) ||
106
+ (Array.isArray(value.servers) && value.servers.some((server) => server && this.looksMasked(server.apikey)));
107
+
108
+ if (masked) {
109
+ winston.error("IntegrationService: the API returned a masked credential for integration " +
110
+ integration_name + " of project " + id_project +
111
+ ". The database was not reachable, so the credential cannot be resolved and the provider will reject it.");
112
+ }
113
+ }
114
+
115
+ async getIntegration(id_project, integration_name, token) {
116
+ const integration = await this.getIntegrationFromDb(id_project, integration_name);
117
+ if (integration) {
118
+ return integration;
119
+ }
120
+
121
+ return await this.getIntegrationFromApi(id_project, integration_name, token);
122
+ }
123
+
124
+ async getKeyFromIntegrations(id_project, integration_name, token) {
125
+ const integration = await this.getIntegration(id_project, integration_name, token);
126
+
127
+ if (integration && integration.value) {
128
+ return integration.value.apikey;
129
+ }
130
+
131
+ return null;
132
+ }
133
+
65
134
  }
66
135
 
67
136
  const integrationService = new IntegrationService();
68
- module.exports = integrationService;
137
+ module.exports = integrationService;
@@ -0,0 +1,67 @@
1
+ const mongoose = require('mongoose');
2
+ const Namespace = require("../models/namespace");
3
+ const winston = require('../utils/winston');
4
+
5
+ /**
6
+ * Namespace lookups that go straight to MongoDB.
7
+ *
8
+ * The engine of a namespace carries the vector store credential. Since
9
+ * tiledesk-server 2.22.7 the API scrubs it (`engine.apikey`) from every
10
+ * namespace it returns, so that the platform-wide key cannot be harvested by
11
+ * anything holding a token — see stripNamespaceSecrets in routes/kb.js. The
12
+ * connector still has to put that key in the request it sends to the LLM
13
+ * microservice, so it reads it from the database it is already connected to
14
+ * instead of having the API hand it out over HTTP.
15
+ */
16
+ class NamespaceService {
17
+
18
+ constructor() {
19
+ // Instance field so tests can substitute a stub for the model.
20
+ this.model = Namespace;
21
+ }
22
+
23
+ // The connector runs without a database when it is fed static bots (tests,
24
+ // embedded usage): querying then would just stall on mongoose buffering.
25
+ isConnected() {
26
+ return mongoose.connection.readyState === 1;
27
+ }
28
+
29
+ /**
30
+ * Engine of a namespace, credential included, or null when it cannot be read.
31
+ * Always scoped by project: a namespace id alone must never be enough to
32
+ * reach another project's namespace.
33
+ */
34
+ async getEngine(namespace_id, id_project) {
35
+ if (!namespace_id || !id_project) {
36
+ winston.debug("NamespaceService getEngine: namespace_id and id_project are both required");
37
+ return null;
38
+ }
39
+
40
+ if (!this.isConnected()) {
41
+ winston.verbose("NamespaceService getEngine: no database connection");
42
+ return null;
43
+ }
44
+
45
+ try {
46
+ const namespace = await this.model
47
+ .findOne({ id: namespace_id, id_project: id_project })
48
+ .select('engine')
49
+ .lean()
50
+ .exec();
51
+
52
+ if (!namespace) {
53
+ winston.verbose("NamespaceService getEngine: namespace " + namespace_id + " not found for project " + id_project);
54
+ return null;
55
+ }
56
+
57
+ return namespace.engine || null;
58
+ } catch (err) {
59
+ winston.error("NamespaceService getEngine error: ", err);
60
+ return null;
61
+ }
62
+ }
63
+
64
+ }
65
+
66
+ var namespaceService = new NamespaceService();
67
+ module.exports = namespaceService;
@@ -15,6 +15,7 @@ const { Logger } = require("../../Logger");
15
15
  const kbService = require("../../services/KbService");
16
16
  const quotasService = require("../../services/QuotasService");
17
17
  const aiController = require("../../services/AIController");
18
+ const namespaceService = require("../../services/NamespaceService");
18
19
  const default_engine = require('../../config/kb/engine');
19
20
  const default_engine_hybrid = require('../../config/kb/engine.hybrid');
20
21
  const default_embedding = require("../../config/kb/embedding");
@@ -277,7 +278,7 @@ class DirAskGPTV2 {
277
278
  }
278
279
 
279
280
  if (ns.engine) {
280
- engine = ns.engine;
281
+ engine = await this.resolveEngineApikey(ns);
281
282
  } else {
282
283
  engine = await this.setDefaultEngine(ns.hybrid);
283
284
  }
@@ -380,6 +381,7 @@ class DirAskGPTV2 {
380
381
  }
381
382
 
382
383
  winston.debug("DirAskGPTV2 json:", json);
384
+ console.log("DirAskGPTV2 json:", JSON.stringify(json));
383
385
 
384
386
  let kb_endpoint = process.env.KB_ENDPOINT_QA;
385
387
  if (ns.hybrid === true) {
@@ -673,6 +675,49 @@ class DirAskGPTV2 {
673
675
  return default_engine;
674
676
  }
675
677
 
678
+ /**
679
+ * Returns the namespace engine with an apikey field that tilellm can read.
680
+ *
681
+ * An EMPTY apikey is the normal Tiledesk setup: the vector store credential
682
+ * lives in the LLM microservice environment (PINECONE_API_KEY) and the client
683
+ * falls back to it, which is why every namespace is stored with apikey "".
684
+ * What breaks tilellm is the field being ABSENT: it parses as None and the
685
+ * pinecone path dereferences it unguarded, so /api/qa answers 400 with
686
+ * "'NoneType' object has no attribute 'get_secret_value'". That is what
687
+ * tiledesk-server 2.22.7 introduced, by deleting engine.apikey from
688
+ * GET /kb/namespace/all.
689
+ *
690
+ * So: a value the API provides is kept as it is, empty included. Only a
691
+ * missing one is resolved — from the namespace document in MongoDB, which
692
+ * this connector is already connected to and which holds the credential for
693
+ * the deployments that set one per namespace, then from the local
694
+ * configuration, and finally as the empty string the microservice expects.
695
+ *
696
+ * The engine is copied rather than patched in place: `ns` comes from the API
697
+ * response and is reused by the caller.
698
+ */
699
+ async resolveEngineApikey(ns) {
700
+ if (typeof ns.engine.apikey === 'string') {
701
+ return ns.engine;
702
+ }
703
+
704
+ let engine = Object.assign({}, ns.engine);
705
+
706
+ let storedEngine = await namespaceService.getEngine(ns.id, this.context.projectId);
707
+ if (storedEngine && typeof storedEngine.apikey === 'string') {
708
+ engine.apikey = storedEngine.apikey;
709
+ winston.verbose("DirAskGPTV2 - Vector store apikey read from the database for namespace " + ns.id);
710
+ return engine;
711
+ }
712
+
713
+ let fallbackEngine = await this.setDefaultEngine(ns.hybrid);
714
+ engine.apikey = fallbackEngine.apikey || "";
715
+ winston.verbose("DirAskGPTV2 - Vector store apikey taken from the local configuration for namespace " +
716
+ ns.id + (engine.apikey ? "" : " (empty: the LLM microservice uses its own key)"));
717
+
718
+ return engine;
719
+ }
720
+
676
721
  }
677
722
 
678
723
  module.exports = { DirAskGPTV2 }
@@ -223,6 +223,28 @@ class DirWebRequestV2 {
223
223
  reject("Error parsing formData");
224
224
  }
225
225
  }
226
+ else if (action.bodyType == "raw") {
227
+ const rawType = action.rawType || "json";
228
+ if (!action.jsonBody) {
229
+ resolve(null);
230
+ return;
231
+ }
232
+ let rawBody = filler.fill(action.jsonBody, requestAttributes);
233
+ if (rawType === "json") {
234
+ try {
235
+ let json = JSON.parse(rawBody);
236
+ resolve(json);
237
+ }
238
+ catch (err) {
239
+ winston.error("DirWebRequestV2 Error parsing webRequest raw jsonBody: " + JSON.stringify(rawBody) + "\nError: " + JSON.stringify(err));
240
+ reject("Error parsing jsonBody");
241
+ }
242
+ }
243
+ else {
244
+ // text | xml | html | javascript: raw string sent as-is, Content-Type comes from headers
245
+ resolve(rawBody);
246
+ }
247
+ }
226
248
  else {
227
249
  resolve(null);
228
250
  }