@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/.eslintrc.cjs
CHANGED
package/README.md
CHANGED
|
@@ -562,26 +562,60 @@ The configuration necessary to create a conversation.
|
|
|
562
562
|
optional applicationUrl: string;
|
|
563
563
|
```
|
|
564
564
|
|
|
565
|
-
The URL at which your conversational application is running.
|
|
566
|
-
|
|
565
|
+
The URL at which your conversational application is running. Fetch this from the application's API channel tab.
|
|
566
|
+
Currently, there are a few ways to specify the application URL:
|
|
567
567
|
|
|
568
|
-
|
|
568
|
+
- (recommended) leave out `applicationUrl` and specify `protocol`, `host`, `deploymentKey` and `channelKey`.
|
|
569
|
+
- specify the full `applicationUrl` as well as the `protocol`.
|
|
570
|
+
- (legacy) specify the `applicationUrl` generated either as an HTTP or websocket URL. Use `experimental.streamHttp` to control streaming.
|
|
571
|
+
|
|
572
|
+
##### protocol?
|
|
569
573
|
|
|
570
574
|
```ts
|
|
571
|
-
|
|
575
|
+
optional protocol: Protocol;
|
|
572
576
|
```
|
|
573
577
|
|
|
574
|
-
|
|
578
|
+
Specify the protocol (http, websocket or httpWithStreaming)
|
|
575
579
|
|
|
576
|
-
|
|
580
|
+
##### host?
|
|
581
|
+
|
|
582
|
+
```ts
|
|
583
|
+
optional host: string;
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
Hostname of the application deployment, without a leading `https://`.
|
|
587
|
+
|
|
588
|
+
##### deploymentKey?
|
|
589
|
+
|
|
590
|
+
```ts
|
|
591
|
+
optional deploymentKey: string;
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
Deployment key.
|
|
595
|
+
|
|
596
|
+
##### channelKey?
|
|
597
|
+
|
|
598
|
+
```ts
|
|
599
|
+
optional channelKey: string;
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
Channel key.
|
|
603
|
+
|
|
604
|
+
##### apiKey?
|
|
605
|
+
|
|
606
|
+
```ts
|
|
607
|
+
optional apiKey: string;
|
|
608
|
+
```
|
|
577
609
|
|
|
578
|
-
|
|
610
|
+
API key.
|
|
611
|
+
|
|
612
|
+
##### headers?
|
|
579
613
|
|
|
580
614
|
```ts
|
|
581
|
-
|
|
615
|
+
optional headers: Record<string, string>;
|
|
582
616
|
```
|
|
583
617
|
|
|
584
|
-
|
|
618
|
+
Headers to forward to the NLX API.
|
|
585
619
|
|
|
586
620
|
##### conversationId?
|
|
587
621
|
|
|
@@ -698,7 +732,7 @@ The slots to populate
|
|
|
698
732
|
sendChoice: (choiceId, context?, metadata?) => void;
|
|
699
733
|
```
|
|
700
734
|
|
|
701
|
-
Respond to [a choice](https://docs.
|
|
735
|
+
Respond to [a choice](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/nodes#user-choice) from the application.
|
|
702
736
|
|
|
703
737
|
###### Parameters
|
|
704
738
|
|
|
@@ -758,13 +792,13 @@ Trigger a specific flow.
|
|
|
758
792
|
|
|
759
793
|
`string`
|
|
760
794
|
|
|
761
|
-
the flow to trigger. The id is the name under the application's
|
|
795
|
+
the flow to trigger. The id is the name under the application's _Flows_.
|
|
762
796
|
|
|
763
797
|
###### context?
|
|
764
798
|
|
|
765
799
|
[`Context`](#context)
|
|
766
800
|
|
|
767
|
-
[Context](https://docs.nlx.ai/platform/nlx-platform-guide/build-with-nlx/flows/context-variables#what-are-context-variables) for usage later in the
|
|
801
|
+
[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.
|
|
768
802
|
|
|
769
803
|
###### Returns
|
|
770
804
|
|
|
@@ -784,7 +818,7 @@ Send context without sending a message
|
|
|
784
818
|
|
|
785
819
|
[`Context`](#context)
|
|
786
820
|
|
|
787
|
-
[Context](https://docs.nlx.ai/platform/nlx-platform-guide/build-with-nlx/flows/context-variables#what-are-context-variables) for usage later in the
|
|
821
|
+
[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.
|
|
788
822
|
|
|
789
823
|
###### Returns
|
|
790
824
|
|
|
@@ -817,7 +851,7 @@ the response with optional timestamps.
|
|
|
817
851
|
sendStructured: (request, context?) => void;
|
|
818
852
|
```
|
|
819
853
|
|
|
820
|
-
Send a combination of choice, slots, and
|
|
854
|
+
Send a combination of choice, slots, and flow in one request.
|
|
821
855
|
|
|
822
856
|
###### Parameters
|
|
823
857
|
|
|
@@ -829,7 +863,7 @@ Send a combination of choice, slots, and intent in one request.
|
|
|
829
863
|
|
|
830
864
|
[`Context`](#context)
|
|
831
865
|
|
|
832
|
-
[Context](https://docs.
|
|
866
|
+
[Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
833
867
|
|
|
834
868
|
###### Returns
|
|
835
869
|
|
|
@@ -1070,7 +1104,7 @@ Remove a listener to one of the handler's custom events
|
|
|
1070
1104
|
|
|
1071
1105
|
### SlotValue
|
|
1072
1106
|
|
|
1073
|
-
Values to fill an
|
|
1107
|
+
Values to fill an flow's [attached slots](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/setup#attached-slots).
|
|
1074
1108
|
|
|
1075
1109
|
An array of `SlotValue` objects is equivalent to a [SlotsRecord](#slotsrecord).
|
|
1076
1110
|
|
|
@@ -1090,7 +1124,7 @@ The attached slot's name
|
|
|
1090
1124
|
value: any;
|
|
1091
1125
|
```
|
|
1092
1126
|
|
|
1093
|
-
Usually this will be a discrete value matching the slots's [type](https://docs.
|
|
1127
|
+
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).
|
|
1094
1128
|
for custom slots, this can optionally be the value's ID.
|
|
1095
1129
|
|
|
1096
1130
|
---
|
|
@@ -1178,7 +1212,7 @@ as well as whether the client should poll for more application responses.
|
|
|
1178
1212
|
optional payload: string;
|
|
1179
1213
|
```
|
|
1180
1214
|
|
|
1181
|
-
If configured, the [node's payload
|
|
1215
|
+
If configured, the [node's payload](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/nodes#node-payload).
|
|
1182
1216
|
|
|
1183
1217
|
##### modalities?
|
|
1184
1218
|
|
|
@@ -1211,7 +1245,7 @@ as well as whether the client should poll for more application responses.
|
|
|
1211
1245
|
optional intentId: string;
|
|
1212
1246
|
```
|
|
1213
1247
|
|
|
1214
|
-
The conversation's
|
|
1248
|
+
The conversation's flow ID (called `intentId` here for legacy reasons).
|
|
1215
1249
|
|
|
1216
1250
|
##### escalation?
|
|
1217
1251
|
|
|
@@ -1342,7 +1376,7 @@ as well as whether the client should poll for more application responses.
|
|
|
1342
1376
|
optional intentId: string;
|
|
1343
1377
|
```
|
|
1344
1378
|
|
|
1345
|
-
The message node's
|
|
1379
|
+
The message node's flow ID (called `intentId` here for legacy reasons).
|
|
1346
1380
|
|
|
1347
1381
|
---
|
|
1348
1382
|
|
|
@@ -1655,7 +1689,7 @@ Label for comment
|
|
|
1655
1689
|
### StructuredRequest
|
|
1656
1690
|
|
|
1657
1691
|
The body of `sendStructured`
|
|
1658
|
-
Includes a combination of choice, slots, and
|
|
1692
|
+
Includes a combination of choice, slots, and flow in one request.
|
|
1659
1693
|
|
|
1660
1694
|
#### Properties
|
|
1661
1695
|
|
|
@@ -1871,13 +1905,25 @@ optional nodeId: string;
|
|
|
1871
1905
|
Required if you want to change a choice that's already been sent.
|
|
1872
1906
|
The `nodeId` can be found in the corresponding [ApplicationMessage](#applicationmessage).
|
|
1873
1907
|
|
|
1874
|
-
##### intentId
|
|
1908
|
+
##### ~~intentId?~~
|
|
1875
1909
|
|
|
1876
1910
|
```ts
|
|
1877
1911
|
optional intentId: string;
|
|
1878
1912
|
```
|
|
1879
1913
|
|
|
1880
|
-
Intent ID, used for sending to the NLU to allow it to double-check
|
|
1914
|
+
Intent ID, used for sending to the NLU to allow it to double-check.
|
|
1915
|
+
|
|
1916
|
+
###### Deprecated
|
|
1917
|
+
|
|
1918
|
+
use `flowId` instead.
|
|
1919
|
+
|
|
1920
|
+
##### flowId?
|
|
1921
|
+
|
|
1922
|
+
```ts
|
|
1923
|
+
optional flowId: string;
|
|
1924
|
+
```
|
|
1925
|
+
|
|
1926
|
+
Flow ID, used for sending to the NLU to allow it to double-check.
|
|
1881
1927
|
|
|
1882
1928
|
---
|
|
1883
1929
|
|
|
@@ -1921,6 +1967,38 @@ Interim message event handler
|
|
|
1921
1967
|
|
|
1922
1968
|
## Enumerations
|
|
1923
1969
|
|
|
1970
|
+
### Protocol
|
|
1971
|
+
|
|
1972
|
+
The protocol used to communicate with the application
|
|
1973
|
+
|
|
1974
|
+
#### Enumeration Members
|
|
1975
|
+
|
|
1976
|
+
##### Https
|
|
1977
|
+
|
|
1978
|
+
```ts
|
|
1979
|
+
Https: "https";
|
|
1980
|
+
```
|
|
1981
|
+
|
|
1982
|
+
Regular encrypted HTTPS, without support for post-escalation message handling, interim messages and other streaming features.
|
|
1983
|
+
|
|
1984
|
+
##### HttpsWithStreaming
|
|
1985
|
+
|
|
1986
|
+
```ts
|
|
1987
|
+
HttpsWithStreaming: "httpsWithStreaming";
|
|
1988
|
+
```
|
|
1989
|
+
|
|
1990
|
+
Encrypted HTTPS with streaming enabled. This is the default setting and supports interim messages. Does not support post-escalation message handling.
|
|
1991
|
+
|
|
1992
|
+
##### Websocket
|
|
1993
|
+
|
|
1994
|
+
```ts
|
|
1995
|
+
Websocket: "websocket";
|
|
1996
|
+
```
|
|
1997
|
+
|
|
1998
|
+
Websocket, with support for post-escalation message handling.
|
|
1999
|
+
|
|
2000
|
+
---
|
|
2001
|
+
|
|
1924
2002
|
### ResponseType
|
|
1925
2003
|
|
|
1926
2004
|
Response type
|
|
@@ -1970,7 +2048,7 @@ voicePlusCommand
|
|
|
1970
2048
|
type Context = Record<string, any>;
|
|
1971
2049
|
```
|
|
1972
2050
|
|
|
1973
|
-
[Context](https://docs.
|
|
2051
|
+
[Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
1974
2052
|
|
|
1975
2053
|
---
|
|
1976
2054
|
|
|
@@ -1980,11 +2058,11 @@ type Context = Record<string, any>;
|
|
|
1980
2058
|
type SlotsRecord = Record<string, any>;
|
|
1981
2059
|
```
|
|
1982
2060
|
|
|
1983
|
-
Values to fill an
|
|
2061
|
+
Values to fill an flow's [attached slots](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/setup#attached-slots).
|
|
1984
2062
|
|
|
1985
2063
|
`SlotRecord` Keys are the attached slot's name
|
|
1986
2064
|
|
|
1987
|
-
`SlotRecord` Values are usually a discrete value matching the slots's [type](https://docs.
|
|
2065
|
+
`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).
|
|
1988
2066
|
for custom slots, this can optionally be the value's ID.
|
|
1989
2067
|
|
|
1990
2068
|
A `SlotsRecord` is equivalent to an array of [SlotValue](#slotvalue) objects.
|
|
@@ -1997,7 +2075,7 @@ A `SlotsRecord` is equivalent to an array of [SlotValue](#slotvalue) objects.
|
|
|
1997
2075
|
type SlotsRecordOrArray = SlotsRecord | SlotValue[];
|
|
1998
2076
|
```
|
|
1999
2077
|
|
|
2000
|
-
Values to fill an intent's [attached slots](https://docs.
|
|
2078
|
+
Values to fill an intent's [attached slots](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/setup#attached-slots).
|
|
2001
2079
|
|
|
2002
2080
|
Supports either a [SlotsRecord](#slotsrecord) or an array of [SlotValue](#slotvalue) objects
|
|
2003
2081
|
|
|
@@ -2064,7 +2142,7 @@ The user's message
|
|
|
2064
2142
|
optional context: Context;
|
|
2065
2143
|
```
|
|
2066
2144
|
|
|
2067
|
-
[Context](https://docs.
|
|
2145
|
+
[Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
2068
2146
|
|
|
2069
2147
|
```ts
|
|
2070
2148
|
{
|
|
@@ -2097,7 +2175,7 @@ Correlates to a `choiceId` in the [ApplicationResponse](#applicationresponse)'s
|
|
|
2097
2175
|
optional context: Context;
|
|
2098
2176
|
```
|
|
2099
2177
|
|
|
2100
|
-
[Context](https://docs.
|
|
2178
|
+
[Context](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/advanced/context-variables) for usage later in the flow.
|
|
2101
2179
|
|
|
2102
2180
|
`object` & [`StructuredRequest`](#structuredrequest)
|
|
2103
2181
|
|
package/lib/index.cjs
CHANGED
|
@@ -6,7 +6,7 @@ var ReconnectingWebSocket = require('reconnecting-websocket');
|
|
|
6
6
|
var uuid = require('uuid');
|
|
7
7
|
|
|
8
8
|
var name = "@nlxai/core";
|
|
9
|
-
var version$1 = "1.2.
|
|
9
|
+
var version$1 = "1.2.6";
|
|
10
10
|
var description = "Low-level SDK for building NLX experiences";
|
|
11
11
|
var type = "module";
|
|
12
12
|
var main = "lib/index.cjs";
|
|
@@ -34,20 +34,20 @@ var scripts = {
|
|
|
34
34
|
var author = "Peter Szerzo <peter@nlx.ai>";
|
|
35
35
|
var license = "MIT";
|
|
36
36
|
var devDependencies = {
|
|
37
|
+
"@nlxai/eslint-config": "*",
|
|
38
|
+
"@nlxai/rollup-config": "*",
|
|
37
39
|
"@rollup/plugin-commonjs": "^25.0.7",
|
|
38
|
-
"@rollup/plugin-json": "^6.0
|
|
40
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
39
41
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
40
42
|
"@rollup/plugin-replace": "^5.0.5",
|
|
41
43
|
"@rollup/plugin-terser": "^0.4.4",
|
|
42
|
-
"@rollup/plugin-typescript": "^11.1.
|
|
44
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
43
45
|
"@types/isomorphic-fetch": "^0.0.39",
|
|
44
46
|
"@types/node": "^24.10.1",
|
|
45
47
|
"@types/ramda": "0.31.1",
|
|
46
48
|
"@types/uuid": "^9.0.7",
|
|
47
|
-
"eslint-config-nlx": "*",
|
|
48
49
|
prettier: "^3.1.0",
|
|
49
|
-
rollup: "^4.
|
|
50
|
-
"rollup-config-nlx": "*",
|
|
50
|
+
rollup: "^4.59.0",
|
|
51
51
|
"rollup-plugin-node-polyfills": "^0.2.1",
|
|
52
52
|
typedoc: "^0.28.14",
|
|
53
53
|
"typedoc-plugin-markdown": "^4.9.0",
|
|
@@ -62,7 +62,7 @@ var dependencies = {
|
|
|
62
62
|
var publishConfig = {
|
|
63
63
|
access: "public"
|
|
64
64
|
};
|
|
65
|
-
var gitHead = "
|
|
65
|
+
var gitHead = "fb9a0b00ca2184fb69ffd1e11e8f55d30fe1d85e";
|
|
66
66
|
var packageJson = {
|
|
67
67
|
name: name,
|
|
68
68
|
version: version$1,
|
|
@@ -93,6 +93,10 @@ const Console = console;
|
|
|
93
93
|
*/
|
|
94
94
|
exports.Protocol = void 0;
|
|
95
95
|
(function (Protocol) {
|
|
96
|
+
/**
|
|
97
|
+
* Supported for development purposes only
|
|
98
|
+
*/
|
|
99
|
+
Protocol["Http"] = "http";
|
|
96
100
|
/**
|
|
97
101
|
* Regular encrypted HTTPS, without support for post-escalation message handling, interim messages and other streaming features.
|
|
98
102
|
*/
|
|
@@ -152,48 +156,82 @@ const safeJsonParse = (val) => {
|
|
|
152
156
|
return null;
|
|
153
157
|
}
|
|
154
158
|
};
|
|
155
|
-
const
|
|
159
|
+
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";
|
|
156
160
|
/**
|
|
157
|
-
*
|
|
158
|
-
* @param
|
|
159
|
-
* @returns
|
|
161
|
+
* Parse configuration into structured connection information, taking into account `applicationUrl`-based configs.
|
|
162
|
+
* @param config - client configuration.
|
|
163
|
+
* @returns connection - connection information, or `null` if the configuration is invalid.
|
|
160
164
|
*/
|
|
161
|
-
const
|
|
165
|
+
const parseConnection = (config) => {
|
|
166
|
+
const applicationUrl = config.applicationUrl ?? "";
|
|
167
|
+
const apiKey = config.apiKey ?? config.headers?.["nlx-api-key"] ?? "";
|
|
168
|
+
const protocol = config.protocol ??
|
|
169
|
+
/**
|
|
170
|
+
* Backwards-compatibility: if a websocket URL was specified, assume it's websocket. Otherwise, look at the legacy experimental streamsetting
|
|
171
|
+
* and only assume non-streaming if it's explicitly set to false.
|
|
172
|
+
*/
|
|
173
|
+
(isWebsocketUrl(applicationUrl)
|
|
174
|
+
? exports.Protocol.Websocket
|
|
175
|
+
: config.experimental?.streamHttp === false
|
|
176
|
+
? exports.Protocol.Https
|
|
177
|
+
: exports.Protocol.HttpsWithStreaming);
|
|
178
|
+
if (config.host != null &&
|
|
179
|
+
config.channelKey != null &&
|
|
180
|
+
config.deploymentKey != null) {
|
|
181
|
+
return {
|
|
182
|
+
protocol,
|
|
183
|
+
apiKey,
|
|
184
|
+
host: config.host,
|
|
185
|
+
channelKey: config.channelKey,
|
|
186
|
+
deploymentKey: config.deploymentKey,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
// `applicationUrl`-based definition: websocket case
|
|
162
190
|
if (isWebsocketUrl(applicationUrl)) {
|
|
163
|
-
|
|
191
|
+
const host = getHost(applicationUrl);
|
|
192
|
+
const url = new URL(applicationUrl);
|
|
193
|
+
const params = new URLSearchParams(url.search);
|
|
194
|
+
const channelKey = params.get("channelKey");
|
|
195
|
+
const deploymentKey = params.get("deploymentKey");
|
|
196
|
+
if (channelKey != null && deploymentKey != null) {
|
|
197
|
+
return { protocol, channelKey, deploymentKey, host, apiKey };
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
164
200
|
}
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
201
|
+
// `applicationUrl`-based definition: http case
|
|
202
|
+
const urlObject = new URL(applicationUrl);
|
|
203
|
+
const host = urlObject.host;
|
|
204
|
+
const parseResult = new URLPattern({
|
|
205
|
+
pathname: "/c/:deploymentKey/:channelKey",
|
|
206
|
+
}).exec(applicationUrl);
|
|
207
|
+
if (parseResult?.pathname.groups.channelKey != null &&
|
|
208
|
+
parseResult?.pathname.groups.deploymentKey != null) {
|
|
209
|
+
return {
|
|
210
|
+
protocol: urlObject.protocol === "http:" ? exports.Protocol.Http : protocol,
|
|
211
|
+
channelKey: parseResult.pathname.groups.channelKey,
|
|
212
|
+
deploymentKey: parseResult.pathname.groups.deploymentKey,
|
|
213
|
+
host,
|
|
214
|
+
apiKey,
|
|
215
|
+
};
|
|
180
216
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
217
|
+
return null;
|
|
218
|
+
};
|
|
219
|
+
const toWebsocketUrl = (connection) => {
|
|
220
|
+
return `wss://us-east-1-ws.${connection.host}?deploymentKey=${connection.deploymentKey}&channelKey=${connection.channelKey}&apiKey=${connection.apiKey}`;
|
|
221
|
+
};
|
|
222
|
+
const toHttpUrl = (connection) => {
|
|
223
|
+
return `${connection.protocol === exports.Protocol.Http ? "http" : "https"}://${connection.host}/c/${connection.deploymentKey}/${connection.channelKey}`;
|
|
187
224
|
};
|
|
188
225
|
const isWebsocketUrl = (url) => {
|
|
189
226
|
return url.indexOf("wss://") === 0;
|
|
190
227
|
};
|
|
191
|
-
const fetchUserMessage = async ({ fullApplicationUrl, headers, body, stream, eventListeners, }) => {
|
|
228
|
+
const fetchUserMessage = async ({ fullApplicationUrl, apiKey, headers, body, stream, eventListeners, }) => {
|
|
192
229
|
const streamRequest = async (body) => {
|
|
193
230
|
const response = await fetch(fullApplicationUrl, {
|
|
194
231
|
method: "POST",
|
|
195
232
|
headers: {
|
|
196
233
|
...headers,
|
|
234
|
+
"nlx-api-key": apiKey,
|
|
197
235
|
"Content-Type": "application/json",
|
|
198
236
|
// Legacy header
|
|
199
237
|
"nlx-sdk-version": packageJson.version,
|
|
@@ -279,6 +317,7 @@ const fetchUserMessage = async ({ fullApplicationUrl, headers, body, stream, eve
|
|
|
279
317
|
method: "POST",
|
|
280
318
|
headers: {
|
|
281
319
|
...(headers ?? {}),
|
|
320
|
+
"nlx-api-key": apiKey,
|
|
282
321
|
Accept: "application/json",
|
|
283
322
|
"Content-Type": "application/json",
|
|
284
323
|
// Legacy header
|
|
@@ -318,31 +357,13 @@ function createConversation(configuration) {
|
|
|
318
357
|
let voicePlusSocket;
|
|
319
358
|
let voicePlusSocketMessageQueue = [];
|
|
320
359
|
let voicePlusSocketMessageQueueCheckInterval = null;
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
(
|
|
327
|
-
|
|
328
|
-
: configuration.experimental?.streamHttp === false
|
|
329
|
-
? exports.Protocol.Https
|
|
330
|
-
: exports.Protocol.HttpsWithStreaming);
|
|
331
|
-
/**
|
|
332
|
-
* TODO: Instead of re-serializing the host/deploymentKey/channelKey combo and normalizing them again into various socket and HTTP URL's,
|
|
333
|
-
* 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
|
|
334
|
-
* parsing again.
|
|
335
|
-
*/
|
|
336
|
-
const applicationUrl = (() => {
|
|
337
|
-
if (configuration.host != null &&
|
|
338
|
-
configuration.deploymentKey != null &&
|
|
339
|
-
configuration.channelKey != null) {
|
|
340
|
-
return `https://${configuration.host}/c/${configuration.deploymentKey}/${configuration.channelKey}`;
|
|
341
|
-
}
|
|
342
|
-
return configuration.applicationUrl ?? "";
|
|
343
|
-
})();
|
|
344
|
-
const websocketApplicationUrl = normalizeToWebsocket(applicationUrl);
|
|
345
|
-
const httpApplicationUrl = normalizeToHttp(applicationUrl);
|
|
360
|
+
const connection = parseConnection(configuration);
|
|
361
|
+
const websocketApplicationUrl = connection != null
|
|
362
|
+
? toWebsocketUrl(connection)
|
|
363
|
+
: configuration.applicationUrl ?? "";
|
|
364
|
+
const httpApplicationUrl = connection != null
|
|
365
|
+
? toHttpUrl(connection)
|
|
366
|
+
: configuration.applicationUrl ?? "";
|
|
346
367
|
// Check if the application URL has a language code appended to it
|
|
347
368
|
if (/[-|_][a-z]{2,}[-|_][A-Z]{2,}$/.test(httpApplicationUrl)) {
|
|
348
369
|
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.");
|
|
@@ -452,7 +473,7 @@ function createConversation(configuration) {
|
|
|
452
473
|
channelType: configuration.experimental?.channelType,
|
|
453
474
|
environment: configuration.environment,
|
|
454
475
|
};
|
|
455
|
-
if (protocol === exports.Protocol.Websocket) {
|
|
476
|
+
if (connection?.protocol === exports.Protocol.Websocket) {
|
|
456
477
|
if (socket?.readyState === 1) {
|
|
457
478
|
socket.send(JSON.stringify(bodyWithContext));
|
|
458
479
|
}
|
|
@@ -464,8 +485,9 @@ function createConversation(configuration) {
|
|
|
464
485
|
try {
|
|
465
486
|
const json = await fetchUserMessage({
|
|
466
487
|
fullApplicationUrl: fullApplicationHttpUrl(),
|
|
488
|
+
apiKey: connection?.apiKey ?? "",
|
|
467
489
|
headers: configuration.headers ?? {},
|
|
468
|
-
stream: protocol === exports.Protocol.HttpsWithStreaming,
|
|
490
|
+
stream: connection?.protocol === exports.Protocol.HttpsWithStreaming,
|
|
469
491
|
eventListeners,
|
|
470
492
|
body: bodyWithContext,
|
|
471
493
|
});
|
|
@@ -500,10 +522,6 @@ function createConversation(configuration) {
|
|
|
500
522
|
url.searchParams.set("channelKey", `${url.searchParams.get("channelKey") ?? ""}-${state.languageCode}`);
|
|
501
523
|
}
|
|
502
524
|
url.searchParams.set("conversationId", state.conversationId);
|
|
503
|
-
const apiKey = configuration.headers["nlx-api-key"];
|
|
504
|
-
if (apiKey != null) {
|
|
505
|
-
url.searchParams.set("apiKey", apiKey);
|
|
506
|
-
}
|
|
507
525
|
socket = new ReconnectingWebSocket(url.href);
|
|
508
526
|
socketMessageQueueCheckInterval = setInterval(() => {
|
|
509
527
|
void checkSocketQueue();
|
|
@@ -527,9 +545,8 @@ function createConversation(configuration) {
|
|
|
527
545
|
}
|
|
528
546
|
url.searchParams.set("conversationId", state.conversationId);
|
|
529
547
|
url.searchParams.set("type", "voice-plus");
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
url.searchParams.set("apiKey", apiKey);
|
|
548
|
+
if (connection?.apiKey != null) {
|
|
549
|
+
url.searchParams.set("apiKey", connection.apiKey);
|
|
533
550
|
}
|
|
534
551
|
voicePlusSocket = new ReconnectingWebSocket(url.href);
|
|
535
552
|
voicePlusSocketMessageQueueCheckInterval = setInterval(() => {
|
|
@@ -566,7 +583,7 @@ function createConversation(configuration) {
|
|
|
566
583
|
voicePlusSocket = undefined;
|
|
567
584
|
}
|
|
568
585
|
};
|
|
569
|
-
if (protocol === exports.Protocol.Websocket) {
|
|
586
|
+
if (connection?.protocol === exports.Protocol.Websocket) {
|
|
570
587
|
setupWebsocket();
|
|
571
588
|
}
|
|
572
589
|
setupCommandWebsocket();
|
|
@@ -649,7 +666,7 @@ function createConversation(configuration) {
|
|
|
649
666
|
request: {
|
|
650
667
|
structured: {
|
|
651
668
|
nodeId: metadata?.nodeId,
|
|
652
|
-
intentId: metadata?.intentId,
|
|
669
|
+
intentId: metadata?.flowId ?? metadata?.intentId,
|
|
653
670
|
choiceId,
|
|
654
671
|
},
|
|
655
672
|
},
|
|
@@ -672,6 +689,7 @@ function createConversation(configuration) {
|
|
|
672
689
|
method: "POST",
|
|
673
690
|
headers: {
|
|
674
691
|
...(configuration.headers ?? {}),
|
|
692
|
+
"nlx-api-key": connection?.apiKey ?? "",
|
|
675
693
|
Accept: "application/json",
|
|
676
694
|
"Content-Type": "application/json",
|
|
677
695
|
"nlx-conversation-id": state.conversationId,
|
|
@@ -757,7 +775,7 @@ function createConversation(configuration) {
|
|
|
757
775
|
Console.warn("Attempted to set language code to the one already active.");
|
|
758
776
|
return;
|
|
759
777
|
}
|
|
760
|
-
if (protocol === exports.Protocol.Websocket) {
|
|
778
|
+
if (connection?.protocol === exports.Protocol.Websocket) {
|
|
761
779
|
setupWebsocket();
|
|
762
780
|
}
|
|
763
781
|
setupCommandWebsocket();
|
|
@@ -771,6 +789,7 @@ function createConversation(configuration) {
|
|
|
771
789
|
method: "POST",
|
|
772
790
|
headers: {
|
|
773
791
|
...(configuration.headers ?? {}),
|
|
792
|
+
"nlx-api-key": connection?.apiKey ?? "",
|
|
774
793
|
Accept: "application/json",
|
|
775
794
|
"Content-Type": "application/json",
|
|
776
795
|
"nlx-conversation-id": state.conversationId,
|
|
@@ -806,14 +825,14 @@ function createConversation(configuration) {
|
|
|
806
825
|
conversationId: uuid.v4(),
|
|
807
826
|
responses: options?.clearResponses === true ? [] : state.responses,
|
|
808
827
|
});
|
|
809
|
-
if (protocol === exports.Protocol.Websocket) {
|
|
828
|
+
if (connection?.protocol === exports.Protocol.Websocket) {
|
|
810
829
|
setupWebsocket();
|
|
811
830
|
}
|
|
812
831
|
setupCommandWebsocket();
|
|
813
832
|
},
|
|
814
833
|
destroy: () => {
|
|
815
834
|
subscribers = [];
|
|
816
|
-
if (protocol === exports.Protocol.Websocket) {
|
|
835
|
+
if (connection?.protocol === exports.Protocol.Websocket) {
|
|
817
836
|
teardownWebsocket();
|
|
818
837
|
}
|
|
819
838
|
teardownCommandWebsocket();
|
|
@@ -838,13 +857,7 @@ function createConversation(configuration) {
|
|
|
838
857
|
* @returns Whether the configuration is valid?
|
|
839
858
|
*/
|
|
840
859
|
const isConfigValid = (configuration) => {
|
|
841
|
-
|
|
842
|
-
configuration.deploymentKey != null &&
|
|
843
|
-
configuration.channelKey != null) {
|
|
844
|
-
return true;
|
|
845
|
-
}
|
|
846
|
-
const applicationUrl = configuration.applicationUrl ?? "";
|
|
847
|
-
return applicationUrl.length > 0;
|
|
860
|
+
return parseConnection(configuration) != null;
|
|
848
861
|
};
|
|
849
862
|
/**
|
|
850
863
|
* Helper method to decide when a new {@link Config} requires creating a new {@link ConversationHandler} or whether the old `Config`'s
|