@nlxai/core 1.2.4-alpha.9 → 1.2.6
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/.eslintrc.cjs +1 -1
- package/README.md +107 -29
- package/lib/index.cjs +94 -81
- package/lib/index.d.ts +34 -26
- package/lib/index.esm.js +98 -85
- package/lib/index.umd.js +2 -2
- package/package.json +7 -7
- package/rollup.config.ts +1 -1
- package/docs/README.md +0 -1930
package/lib/index.d.ts
CHANGED
|
@@ -30,15 +30,14 @@ export interface Config {
|
|
|
30
30
|
* Channel key.
|
|
31
31
|
*/
|
|
32
32
|
channelKey?: string;
|
|
33
|
+
/**
|
|
34
|
+
* API key.
|
|
35
|
+
*/
|
|
36
|
+
apiKey?: string;
|
|
33
37
|
/**
|
|
34
38
|
* Headers to forward to the NLX API.
|
|
35
39
|
*/
|
|
36
|
-
headers
|
|
37
|
-
/**
|
|
38
|
-
* The `nlx-api-key` is required. Fetch this from the application's API channel tab.
|
|
39
|
-
*/
|
|
40
|
-
"nlx-api-key": string;
|
|
41
|
-
};
|
|
40
|
+
headers?: Record<string, string>;
|
|
42
41
|
/**
|
|
43
42
|
* Set `conversationId` to continue an existing conversation. If not set, a new conversation will be started (and a new conversationId will be generated internally).
|
|
44
43
|
*/
|
|
@@ -106,7 +105,7 @@ export interface ConversationHandler {
|
|
|
106
105
|
*/
|
|
107
106
|
sendSlots: (slots: SlotsRecordOrArray, context?: Context) => void;
|
|
108
107
|
/**
|
|
109
|
-
* Respond to [a choice](https://docs.
|
|
108
|
+
* Respond to [a choice](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/nodes#user-choice) from the application.
|
|
110
109
|
* @param choiceId - The `choiceId` is in the {@link ApplicationResponse}'s `.payload.messages[].choices[].choiceId` fields
|
|
111
110
|
* @param context - [Context](https://docs.nlx.ai/platform/nlx-platform-guide/build-with-nlx/flows/context-variables#what-are-context-variables) for usage later in the flow.
|
|
112
111
|
* @param metadata - links the choice to the specific message and node in the conversation.
|
|
@@ -126,8 +125,8 @@ export interface ConversationHandler {
|
|
|
126
125
|
sendWelcomeIntent: (context?: Context) => void;
|
|
127
126
|
/**
|
|
128
127
|
* Trigger a specific flow.
|
|
129
|
-
* @param flowId - the flow to trigger. The id is the name under the application's
|
|
130
|
-
* @param context - [Context](https://docs.nlx.ai/platform/nlx-platform-guide/build-with-nlx/flows/context-variables#what-are-context-variables) for usage later in the
|
|
128
|
+
* @param flowId - the flow to trigger. The id is the name under the application's _Flows_.
|
|
129
|
+
* @param context - [Context](https://docs.nlx.ai/platform/nlx-platform-guide/build-with-nlx/flows/context-variables#what-are-context-variables) for usage later in the flow.
|
|
131
130
|
*/
|
|
132
131
|
sendFlow: (flowId: string, context?: Context) => void;
|
|
133
132
|
/**
|
|
@@ -140,7 +139,7 @@ export interface ConversationHandler {
|
|
|
140
139
|
sendIntent: (intentId: string, context?: Context) => void;
|
|
141
140
|
/**
|
|
142
141
|
* Send context without sending a message
|
|
143
|
-
* @param context - [Context](https://docs.nlx.ai/platform/nlx-platform-guide/build-with-nlx/flows/context-variables#what-are-context-variables) for usage later in the
|
|
142
|
+
* @param context - [Context](https://docs.nlx.ai/platform/nlx-platform-guide/build-with-nlx/flows/context-variables#what-are-context-variables) for usage later in the flow.
|
|
144
143
|
*/
|
|
145
144
|
sendContext: (context: Context) => Promise<void>;
|
|
146
145
|
/**
|
|
@@ -164,9 +163,9 @@ export interface ConversationHandler {
|
|
|
164
163
|
receivedAt?: Time;
|
|
165
164
|
})) => void;
|
|
166
165
|
/**
|
|
167
|
-
* Send a combination of choice, slots, and
|
|
166
|
+
* Send a combination of choice, slots, and flow in one request.
|
|
168
167
|
* @param request -
|
|
169
|
-
* @param context - [Context](https://docs.
|
|
168
|
+
* @param context - [Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
170
169
|
*/
|
|
171
170
|
sendStructured: (request: StructuredRequest, context?: Context) => void;
|
|
172
171
|
/**
|
|
@@ -238,11 +237,11 @@ export interface ConversationHandler {
|
|
|
238
237
|
sendVoicePlusContext: (context: VoicePlusContext) => void;
|
|
239
238
|
}
|
|
240
239
|
/**
|
|
241
|
-
* [Context](https://docs.
|
|
240
|
+
* [Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
242
241
|
*/
|
|
243
242
|
export type Context = Record<string, any>;
|
|
244
243
|
/**
|
|
245
|
-
* Values to fill an
|
|
244
|
+
* Values to fill an flow's [attached slots](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/setup#attached-slots).
|
|
246
245
|
*
|
|
247
246
|
* An array of `SlotValue` objects is equivalent to a {@link SlotsRecord}.
|
|
248
247
|
*/
|
|
@@ -252,7 +251,7 @@ export interface SlotValue {
|
|
|
252
251
|
*/
|
|
253
252
|
slotId: string;
|
|
254
253
|
/**
|
|
255
|
-
* Usually this will be a discrete value matching the slots's [type](https://docs.
|
|
254
|
+
* Usually this will be a discrete value matching the slots's [type](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/setup#custom-vs-built-in-slots).
|
|
256
255
|
* for custom slots, this can optionally be the value's ID.
|
|
257
256
|
*/
|
|
258
257
|
value: any;
|
|
@@ -261,6 +260,10 @@ export interface SlotValue {
|
|
|
261
260
|
* The protocol used to communicate with the application
|
|
262
261
|
*/
|
|
263
262
|
export declare enum Protocol {
|
|
263
|
+
/**
|
|
264
|
+
* Supported for development purposes only
|
|
265
|
+
*/
|
|
266
|
+
Http = "http",
|
|
264
267
|
/**
|
|
265
268
|
* Regular encrypted HTTPS, without support for post-escalation message handling, interim messages and other streaming features.
|
|
266
269
|
*/
|
|
@@ -292,18 +295,18 @@ export declare enum ResponseType {
|
|
|
292
295
|
Failure = "failure"
|
|
293
296
|
}
|
|
294
297
|
/**
|
|
295
|
-
* Values to fill an
|
|
298
|
+
* Values to fill an flow's [attached slots](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/setup#attached-slots).
|
|
296
299
|
*
|
|
297
300
|
* `SlotRecord` Keys are the attached slot's name
|
|
298
301
|
*
|
|
299
|
-
* `SlotRecord` Values are usually a discrete value matching the slots's [type](https://docs.
|
|
302
|
+
* `SlotRecord` Values are usually a discrete value matching the slots's [type](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/setup#custom-vs-built-in-slots).
|
|
300
303
|
* for custom slots, this can optionally be the value's ID.
|
|
301
304
|
*
|
|
302
305
|
* A `SlotsRecord` is equivalent to an array of {@link SlotValue} objects.
|
|
303
306
|
*/
|
|
304
307
|
export type SlotsRecord = Record<string, any>;
|
|
305
308
|
/**
|
|
306
|
-
* Values to fill an
|
|
309
|
+
* Values to fill an flow's [attached slots](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/setup#attached-slots).
|
|
307
310
|
*
|
|
308
311
|
* Supports either a {@link SlotsRecord} or an array of {@link SlotValue} objects
|
|
309
312
|
*/
|
|
@@ -352,7 +355,7 @@ export interface ApplicationResponsePayload {
|
|
|
352
355
|
*/
|
|
353
356
|
metadata?: ApplicationResponseMetadata;
|
|
354
357
|
/**
|
|
355
|
-
* If configured, the [node's payload
|
|
358
|
+
* If configured, the [node's payload](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/nodes#node-payload).
|
|
356
359
|
*/
|
|
357
360
|
payload?: string;
|
|
358
361
|
/**
|
|
@@ -374,7 +377,7 @@ export type ModalityPayloads = Record<string, any>;
|
|
|
374
377
|
*/
|
|
375
378
|
export interface ApplicationResponseMetadata {
|
|
376
379
|
/**
|
|
377
|
-
* The conversation's
|
|
380
|
+
* The conversation's flow ID (called `intentId` here for legacy reasons).
|
|
378
381
|
*/
|
|
379
382
|
intentId?: string;
|
|
380
383
|
/**
|
|
@@ -443,7 +446,7 @@ export interface KnowledgeBaseResponseSource {
|
|
|
443
446
|
*/
|
|
444
447
|
export interface ApplicationMessageMetadata {
|
|
445
448
|
/**
|
|
446
|
-
* The message node's
|
|
449
|
+
* The message node's flow ID (called `intentId` here for legacy reasons).
|
|
447
450
|
*/
|
|
448
451
|
intentId?: string;
|
|
449
452
|
}
|
|
@@ -544,7 +547,7 @@ export type UserResponsePayload = {
|
|
|
544
547
|
*/
|
|
545
548
|
text: string;
|
|
546
549
|
/**
|
|
547
|
-
* [Context](https://docs.
|
|
550
|
+
* [Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
548
551
|
*/
|
|
549
552
|
context?: Context;
|
|
550
553
|
} | {
|
|
@@ -558,7 +561,7 @@ export type UserResponsePayload = {
|
|
|
558
561
|
*/
|
|
559
562
|
choiceId: string;
|
|
560
563
|
/**
|
|
561
|
-
* [Context](https://docs.
|
|
564
|
+
* [Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
562
565
|
*/
|
|
563
566
|
context?: Context;
|
|
564
567
|
} | ({
|
|
@@ -567,7 +570,7 @@ export type UserResponsePayload = {
|
|
|
567
570
|
*/
|
|
568
571
|
type: "structured";
|
|
569
572
|
/**
|
|
570
|
-
* [Context](https://docs.
|
|
573
|
+
* [Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
571
574
|
*/
|
|
572
575
|
context?: Context;
|
|
573
576
|
} & StructuredRequest);
|
|
@@ -653,7 +656,7 @@ export interface BinaryFeedbackConfig {
|
|
|
653
656
|
export type Environment = "production" | "development";
|
|
654
657
|
/**
|
|
655
658
|
* The body of `sendStructured`
|
|
656
|
-
* Includes a combination of choice, slots, and
|
|
659
|
+
* Includes a combination of choice, slots, and flow in one request.
|
|
657
660
|
*/
|
|
658
661
|
export interface StructuredRequest {
|
|
659
662
|
/**
|
|
@@ -784,9 +787,14 @@ export interface ChoiceRequestMetadata {
|
|
|
784
787
|
*/
|
|
785
788
|
nodeId?: string;
|
|
786
789
|
/**
|
|
787
|
-
* Intent ID, used for sending to the NLU to allow it to double-check
|
|
790
|
+
* Intent ID, used for sending to the NLU to allow it to double-check.
|
|
791
|
+
* @deprecated use `flowId` instead.
|
|
788
792
|
*/
|
|
789
793
|
intentId?: string;
|
|
794
|
+
/**
|
|
795
|
+
* Flow ID, used for sending to the NLU to allow it to double-check.
|
|
796
|
+
*/
|
|
797
|
+
flowId?: string;
|
|
790
798
|
}
|
|
791
799
|
/**
|
|
792
800
|
* Language code named for clarity, may restrict it to a finite list
|
package/lib/index.esm.js
CHANGED
|
@@ -4,14 +4,14 @@ import ReconnectingWebSocket from 'reconnecting-websocket';
|
|
|
4
4
|
import { v4 } from 'uuid';
|
|
5
5
|
|
|
6
6
|
var name = "@nlxai/core";
|
|
7
|
-
var version$1 = "1.2.
|
|
7
|
+
var version$1 = "1.2.6";
|
|
8
8
|
var description = "Low-level SDK for building NLX experiences";
|
|
9
9
|
var type = "module";
|
|
10
10
|
var main = "lib/index.cjs";
|
|
11
|
-
var module = "lib/index.esm.js";
|
|
11
|
+
var module$1 = "lib/index.esm.js";
|
|
12
12
|
var browser = "lib/index.umd.js";
|
|
13
13
|
var types = "lib/index.d.ts";
|
|
14
|
-
var exports = {
|
|
14
|
+
var exports$1 = {
|
|
15
15
|
".": {
|
|
16
16
|
types: "./lib/index.d.ts",
|
|
17
17
|
"import": "./lib/index.esm.js",
|
|
@@ -32,20 +32,20 @@ var scripts = {
|
|
|
32
32
|
var author = "Peter Szerzo <peter@nlx.ai>";
|
|
33
33
|
var license = "MIT";
|
|
34
34
|
var devDependencies = {
|
|
35
|
+
"@nlxai/eslint-config": "*",
|
|
36
|
+
"@nlxai/rollup-config": "*",
|
|
35
37
|
"@rollup/plugin-commonjs": "^25.0.7",
|
|
36
|
-
"@rollup/plugin-json": "^6.0
|
|
38
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
37
39
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
38
40
|
"@rollup/plugin-replace": "^5.0.5",
|
|
39
41
|
"@rollup/plugin-terser": "^0.4.4",
|
|
40
|
-
"@rollup/plugin-typescript": "^11.1.
|
|
42
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
41
43
|
"@types/isomorphic-fetch": "^0.0.39",
|
|
42
44
|
"@types/node": "^24.10.1",
|
|
43
45
|
"@types/ramda": "0.31.1",
|
|
44
46
|
"@types/uuid": "^9.0.7",
|
|
45
|
-
"eslint-config-nlx": "*",
|
|
46
47
|
prettier: "^3.1.0",
|
|
47
|
-
rollup: "^4.
|
|
48
|
-
"rollup-config-nlx": "*",
|
|
48
|
+
rollup: "^4.59.0",
|
|
49
49
|
"rollup-plugin-node-polyfills": "^0.2.1",
|
|
50
50
|
typedoc: "^0.28.14",
|
|
51
51
|
"typedoc-plugin-markdown": "^4.9.0",
|
|
@@ -60,17 +60,17 @@ var dependencies = {
|
|
|
60
60
|
var publishConfig = {
|
|
61
61
|
access: "public"
|
|
62
62
|
};
|
|
63
|
-
var gitHead = "
|
|
63
|
+
var gitHead = "fb9a0b00ca2184fb69ffd1e11e8f55d30fe1d85e";
|
|
64
64
|
var packageJson = {
|
|
65
65
|
name: name,
|
|
66
66
|
version: version$1,
|
|
67
67
|
description: description,
|
|
68
68
|
type: type,
|
|
69
69
|
main: main,
|
|
70
|
-
module: module,
|
|
70
|
+
module: module$1,
|
|
71
71
|
browser: browser,
|
|
72
72
|
types: types,
|
|
73
|
-
exports: exports,
|
|
73
|
+
exports: exports$1,
|
|
74
74
|
scripts: scripts,
|
|
75
75
|
author: author,
|
|
76
76
|
license: license,
|
|
@@ -91,6 +91,10 @@ const Console = console;
|
|
|
91
91
|
*/
|
|
92
92
|
var Protocol;
|
|
93
93
|
(function (Protocol) {
|
|
94
|
+
/**
|
|
95
|
+
* Supported for development purposes only
|
|
96
|
+
*/
|
|
97
|
+
Protocol["Http"] = "http";
|
|
94
98
|
/**
|
|
95
99
|
* Regular encrypted HTTPS, without support for post-escalation message handling, interim messages and other streaming features.
|
|
96
100
|
*/
|
|
@@ -150,48 +154,82 @@ const safeJsonParse = (val) => {
|
|
|
150
154
|
return null;
|
|
151
155
|
}
|
|
152
156
|
};
|
|
153
|
-
const
|
|
157
|
+
const getHost = (url) => url.match(/(bots\.dev\.studio\.nlx\.ai|bots\.studio\.nlx\.ai|apps\.nlx\.ai|dev\.apps\.nlx\.ai)/g)?.[0] ?? "apps.nlx.ai";
|
|
154
158
|
/**
|
|
155
|
-
*
|
|
156
|
-
* @param
|
|
157
|
-
* @returns
|
|
159
|
+
* Parse configuration into structured connection information, taking into account `applicationUrl`-based configs.
|
|
160
|
+
* @param config - client configuration.
|
|
161
|
+
* @returns connection - connection information, or `null` if the configuration is invalid.
|
|
158
162
|
*/
|
|
159
|
-
const
|
|
163
|
+
const parseConnection = (config) => {
|
|
164
|
+
const applicationUrl = config.applicationUrl ?? "";
|
|
165
|
+
const apiKey = config.apiKey ?? config.headers?.["nlx-api-key"] ?? "";
|
|
166
|
+
const protocol = config.protocol ??
|
|
167
|
+
/**
|
|
168
|
+
* Backwards-compatibility: if a websocket URL was specified, assume it's websocket. Otherwise, look at the legacy experimental streamsetting
|
|
169
|
+
* and only assume non-streaming if it's explicitly set to false.
|
|
170
|
+
*/
|
|
171
|
+
(isWebsocketUrl(applicationUrl)
|
|
172
|
+
? Protocol.Websocket
|
|
173
|
+
: config.experimental?.streamHttp === false
|
|
174
|
+
? Protocol.Https
|
|
175
|
+
: Protocol.HttpsWithStreaming);
|
|
176
|
+
if (config.host != null &&
|
|
177
|
+
config.channelKey != null &&
|
|
178
|
+
config.deploymentKey != null) {
|
|
179
|
+
return {
|
|
180
|
+
protocol,
|
|
181
|
+
apiKey,
|
|
182
|
+
host: config.host,
|
|
183
|
+
channelKey: config.channelKey,
|
|
184
|
+
deploymentKey: config.deploymentKey,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
// `applicationUrl`-based definition: websocket case
|
|
160
188
|
if (isWebsocketUrl(applicationUrl)) {
|
|
161
|
-
|
|
189
|
+
const host = getHost(applicationUrl);
|
|
190
|
+
const url = new URL(applicationUrl);
|
|
191
|
+
const params = new URLSearchParams(url.search);
|
|
192
|
+
const channelKey = params.get("channelKey");
|
|
193
|
+
const deploymentKey = params.get("deploymentKey");
|
|
194
|
+
if (channelKey != null && deploymentKey != null) {
|
|
195
|
+
return { protocol, channelKey, deploymentKey, host, apiKey };
|
|
196
|
+
}
|
|
197
|
+
return null;
|
|
162
198
|
}
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
199
|
+
// `applicationUrl`-based definition: http case
|
|
200
|
+
const urlObject = new URL(applicationUrl);
|
|
201
|
+
const host = urlObject.host;
|
|
202
|
+
const parseResult = new URLPattern({
|
|
203
|
+
pathname: "/c/:deploymentKey/:channelKey",
|
|
204
|
+
}).exec(applicationUrl);
|
|
205
|
+
if (parseResult?.pathname.groups.channelKey != null &&
|
|
206
|
+
parseResult?.pathname.groups.deploymentKey != null) {
|
|
207
|
+
return {
|
|
208
|
+
protocol: urlObject.protocol === "http:" ? Protocol.Http : protocol,
|
|
209
|
+
channelKey: parseResult.pathname.groups.channelKey,
|
|
210
|
+
deploymentKey: parseResult.pathname.groups.deploymentKey,
|
|
211
|
+
host,
|
|
212
|
+
apiKey,
|
|
213
|
+
};
|
|
178
214
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
215
|
+
return null;
|
|
216
|
+
};
|
|
217
|
+
const toWebsocketUrl = (connection) => {
|
|
218
|
+
return `wss://us-east-1-ws.${connection.host}?deploymentKey=${connection.deploymentKey}&channelKey=${connection.channelKey}&apiKey=${connection.apiKey}`;
|
|
219
|
+
};
|
|
220
|
+
const toHttpUrl = (connection) => {
|
|
221
|
+
return `${connection.protocol === Protocol.Http ? "http" : "https"}://${connection.host}/c/${connection.deploymentKey}/${connection.channelKey}`;
|
|
185
222
|
};
|
|
186
223
|
const isWebsocketUrl = (url) => {
|
|
187
224
|
return url.indexOf("wss://") === 0;
|
|
188
225
|
};
|
|
189
|
-
const fetchUserMessage = async ({ fullApplicationUrl, headers, body, stream, eventListeners, }) => {
|
|
226
|
+
const fetchUserMessage = async ({ fullApplicationUrl, apiKey, headers, body, stream, eventListeners, }) => {
|
|
190
227
|
const streamRequest = async (body) => {
|
|
191
228
|
const response = await fetch(fullApplicationUrl, {
|
|
192
229
|
method: "POST",
|
|
193
230
|
headers: {
|
|
194
231
|
...headers,
|
|
232
|
+
"nlx-api-key": apiKey,
|
|
195
233
|
"Content-Type": "application/json",
|
|
196
234
|
// Legacy header
|
|
197
235
|
"nlx-sdk-version": packageJson.version,
|
|
@@ -277,6 +315,7 @@ const fetchUserMessage = async ({ fullApplicationUrl, headers, body, stream, eve
|
|
|
277
315
|
method: "POST",
|
|
278
316
|
headers: {
|
|
279
317
|
...(headers ?? {}),
|
|
318
|
+
"nlx-api-key": apiKey,
|
|
280
319
|
Accept: "application/json",
|
|
281
320
|
"Content-Type": "application/json",
|
|
282
321
|
// Legacy header
|
|
@@ -316,31 +355,13 @@ function createConversation(configuration) {
|
|
|
316
355
|
let voicePlusSocket;
|
|
317
356
|
let voicePlusSocketMessageQueue = [];
|
|
318
357
|
let voicePlusSocketMessageQueueCheckInterval = null;
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
(
|
|
325
|
-
|
|
326
|
-
: configuration.experimental?.streamHttp === false
|
|
327
|
-
? Protocol.Https
|
|
328
|
-
: Protocol.HttpsWithStreaming);
|
|
329
|
-
/**
|
|
330
|
-
* TODO: Instead of re-serializing the host/deploymentKey/channelKey combo and normalizing them again into various socket and HTTP URL's,
|
|
331
|
-
* parse them out of the `applicationUrl` if specified, and use them to build up the URL's in use. This way we avoid serializing and then
|
|
332
|
-
* parsing again.
|
|
333
|
-
*/
|
|
334
|
-
const applicationUrl = (() => {
|
|
335
|
-
if (configuration.host != null &&
|
|
336
|
-
configuration.deploymentKey != null &&
|
|
337
|
-
configuration.channelKey != null) {
|
|
338
|
-
return `https://${configuration.host}/c/${configuration.deploymentKey}/${configuration.channelKey}`;
|
|
339
|
-
}
|
|
340
|
-
return configuration.applicationUrl ?? "";
|
|
341
|
-
})();
|
|
342
|
-
const websocketApplicationUrl = normalizeToWebsocket(applicationUrl);
|
|
343
|
-
const httpApplicationUrl = normalizeToHttp(applicationUrl);
|
|
358
|
+
const connection = parseConnection(configuration);
|
|
359
|
+
const websocketApplicationUrl = connection != null
|
|
360
|
+
? toWebsocketUrl(connection)
|
|
361
|
+
: configuration.applicationUrl ?? "";
|
|
362
|
+
const httpApplicationUrl = connection != null
|
|
363
|
+
? toHttpUrl(connection)
|
|
364
|
+
: configuration.applicationUrl ?? "";
|
|
344
365
|
// Check if the application URL has a language code appended to it
|
|
345
366
|
if (/[-|_][a-z]{2,}[-|_][A-Z]{2,}$/.test(httpApplicationUrl)) {
|
|
346
367
|
Console.warn("Since v1.0.0, the language code is no longer added at the end of the application URL. Please remove the modifier (e.g. '-en-US') from the URL, and specify it in the `languageCode` parameter instead.");
|
|
@@ -450,7 +471,7 @@ function createConversation(configuration) {
|
|
|
450
471
|
channelType: configuration.experimental?.channelType,
|
|
451
472
|
environment: configuration.environment,
|
|
452
473
|
};
|
|
453
|
-
if (protocol === Protocol.Websocket) {
|
|
474
|
+
if (connection?.protocol === Protocol.Websocket) {
|
|
454
475
|
if (socket?.readyState === 1) {
|
|
455
476
|
socket.send(JSON.stringify(bodyWithContext));
|
|
456
477
|
}
|
|
@@ -462,8 +483,9 @@ function createConversation(configuration) {
|
|
|
462
483
|
try {
|
|
463
484
|
const json = await fetchUserMessage({
|
|
464
485
|
fullApplicationUrl: fullApplicationHttpUrl(),
|
|
486
|
+
apiKey: connection?.apiKey ?? "",
|
|
465
487
|
headers: configuration.headers ?? {},
|
|
466
|
-
stream: protocol === Protocol.HttpsWithStreaming,
|
|
488
|
+
stream: connection?.protocol === Protocol.HttpsWithStreaming,
|
|
467
489
|
eventListeners,
|
|
468
490
|
body: bodyWithContext,
|
|
469
491
|
});
|
|
@@ -498,10 +520,6 @@ function createConversation(configuration) {
|
|
|
498
520
|
url.searchParams.set("channelKey", `${url.searchParams.get("channelKey") ?? ""}-${state.languageCode}`);
|
|
499
521
|
}
|
|
500
522
|
url.searchParams.set("conversationId", state.conversationId);
|
|
501
|
-
const apiKey = configuration.headers["nlx-api-key"];
|
|
502
|
-
if (apiKey != null) {
|
|
503
|
-
url.searchParams.set("apiKey", apiKey);
|
|
504
|
-
}
|
|
505
523
|
socket = new ReconnectingWebSocket(url.href);
|
|
506
524
|
socketMessageQueueCheckInterval = setInterval(() => {
|
|
507
525
|
void checkSocketQueue();
|
|
@@ -525,9 +543,8 @@ function createConversation(configuration) {
|
|
|
525
543
|
}
|
|
526
544
|
url.searchParams.set("conversationId", state.conversationId);
|
|
527
545
|
url.searchParams.set("type", "voice-plus");
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
url.searchParams.set("apiKey", apiKey);
|
|
546
|
+
if (connection?.apiKey != null) {
|
|
547
|
+
url.searchParams.set("apiKey", connection.apiKey);
|
|
531
548
|
}
|
|
532
549
|
voicePlusSocket = new ReconnectingWebSocket(url.href);
|
|
533
550
|
voicePlusSocketMessageQueueCheckInterval = setInterval(() => {
|
|
@@ -564,7 +581,7 @@ function createConversation(configuration) {
|
|
|
564
581
|
voicePlusSocket = undefined;
|
|
565
582
|
}
|
|
566
583
|
};
|
|
567
|
-
if (protocol === Protocol.Websocket) {
|
|
584
|
+
if (connection?.protocol === Protocol.Websocket) {
|
|
568
585
|
setupWebsocket();
|
|
569
586
|
}
|
|
570
587
|
setupCommandWebsocket();
|
|
@@ -647,7 +664,7 @@ function createConversation(configuration) {
|
|
|
647
664
|
request: {
|
|
648
665
|
structured: {
|
|
649
666
|
nodeId: metadata?.nodeId,
|
|
650
|
-
intentId: metadata?.intentId,
|
|
667
|
+
intentId: metadata?.flowId ?? metadata?.intentId,
|
|
651
668
|
choiceId,
|
|
652
669
|
},
|
|
653
670
|
},
|
|
@@ -670,6 +687,7 @@ function createConversation(configuration) {
|
|
|
670
687
|
method: "POST",
|
|
671
688
|
headers: {
|
|
672
689
|
...(configuration.headers ?? {}),
|
|
690
|
+
"nlx-api-key": connection?.apiKey ?? "",
|
|
673
691
|
Accept: "application/json",
|
|
674
692
|
"Content-Type": "application/json",
|
|
675
693
|
"nlx-conversation-id": state.conversationId,
|
|
@@ -755,7 +773,7 @@ function createConversation(configuration) {
|
|
|
755
773
|
Console.warn("Attempted to set language code to the one already active.");
|
|
756
774
|
return;
|
|
757
775
|
}
|
|
758
|
-
if (protocol === Protocol.Websocket) {
|
|
776
|
+
if (connection?.protocol === Protocol.Websocket) {
|
|
759
777
|
setupWebsocket();
|
|
760
778
|
}
|
|
761
779
|
setupCommandWebsocket();
|
|
@@ -769,6 +787,7 @@ function createConversation(configuration) {
|
|
|
769
787
|
method: "POST",
|
|
770
788
|
headers: {
|
|
771
789
|
...(configuration.headers ?? {}),
|
|
790
|
+
"nlx-api-key": connection?.apiKey ?? "",
|
|
772
791
|
Accept: "application/json",
|
|
773
792
|
"Content-Type": "application/json",
|
|
774
793
|
"nlx-conversation-id": state.conversationId,
|
|
@@ -804,14 +823,14 @@ function createConversation(configuration) {
|
|
|
804
823
|
conversationId: v4(),
|
|
805
824
|
responses: options?.clearResponses === true ? [] : state.responses,
|
|
806
825
|
});
|
|
807
|
-
if (protocol === Protocol.Websocket) {
|
|
826
|
+
if (connection?.protocol === Protocol.Websocket) {
|
|
808
827
|
setupWebsocket();
|
|
809
828
|
}
|
|
810
829
|
setupCommandWebsocket();
|
|
811
830
|
},
|
|
812
831
|
destroy: () => {
|
|
813
832
|
subscribers = [];
|
|
814
|
-
if (protocol === Protocol.Websocket) {
|
|
833
|
+
if (connection?.protocol === Protocol.Websocket) {
|
|
815
834
|
teardownWebsocket();
|
|
816
835
|
}
|
|
817
836
|
teardownCommandWebsocket();
|
|
@@ -836,13 +855,7 @@ function createConversation(configuration) {
|
|
|
836
855
|
* @returns Whether the configuration is valid?
|
|
837
856
|
*/
|
|
838
857
|
const isConfigValid = (configuration) => {
|
|
839
|
-
|
|
840
|
-
configuration.deploymentKey != null &&
|
|
841
|
-
configuration.channelKey != null) {
|
|
842
|
-
return true;
|
|
843
|
-
}
|
|
844
|
-
const applicationUrl = configuration.applicationUrl ?? "";
|
|
845
|
-
return applicationUrl.length > 0;
|
|
858
|
+
return parseConnection(configuration) != null;
|
|
846
859
|
};
|
|
847
860
|
/**
|
|
848
861
|
* Helper method to decide when a new {@link Config} requires creating a new {@link ConversationHandler} or whether the old `Config`'s
|