Headless API
API reference
@nlxai/chat-core
Interfaces
- SlotValue
- BotResponse
- BotResponsePayload
- BotResponseMetadata
- BotMessageMetadata
- BotMessage
- UploadUrl
- Choice
- UserResponse
- FailureMessage
- Config
- StructuredRequest
- BotRequest
- ChoiceRequestMetadata
- ConversationHandler
Type Aliases
Context
Ƭ Context: Record
<string
, any
>
Context for usage later in the intent.
Defined in
SlotsRecord
Ƭ SlotsRecord: Record
<string
, any
>
Values to fill an intent's attached slots.
SlotRecord
Keys are the attached slot's name
SlotRecord
Values are usually a discrete value matching the slots's type.
for custom slots, this can optionally be the value's ID.
A SlotsRecord
is equivalent to an array of SlotValue objects.
Defined in
SlotsRecordOrArray
Ƭ SlotsRecordOrArray: SlotsRecord
| SlotValue
[]
Values to fill an intent's attached slots.
Supports either a SlotsRecord or an array of SlotValue objects
Defined in
UserResponsePayload
Ƭ UserResponsePayload: { type
: "text"
; text
: string
; context?
: Context
} | { type
: "choice"
; choiceId
: string
; context?
: Context
} | { type
: "structured"
; context?
: Context
} & StructuredRequest
The payload of the user response
Defined in
Response
Ƭ Response: BotResponse
| UserResponse
| FailureMessage
A response from the bot or the user.
Defined in
Time
Ƭ Time: number
The time value in milliseconds since midnight, January 1, 1970 UTC.
Defined in
NormalizedStructuredRequest
Ƭ NormalizedStructuredRequest: StructuredRequest
& { slots?
: SlotValue
[] }
Normalized structured request with a single way to represent slots
Defined in
BotRequestOverride
Ƭ BotRequestOverride: (botRequest
: BotRequest
, appendBotResponse
: (res
: BotResponsePayload
) => void
) => void
Instead of sending a request to the bot, handle it in a custom fashion
Type declaration
▸ (botRequest
, appendBotResponse
): void
Parameters
Name | Type | Description |
---|---|---|
botRequest | BotRequest | The BotRequest that is being overridden |
appendBotResponse | (res : BotResponsePayload ) => void | - |
Returns
void
Defined in
Subscriber
Ƭ Subscriber: (response
: Response
[], newResponse?
: Response
) => void
The callback function for listening to all responses.
Type declaration
▸ (response
, newResponse?
): void
Parameters
Name | Type |
---|---|
response | Response [] |
newResponse? | Response |
Returns
void
Defined in
Functions
shouldReinitialize
▸ shouldReinitialize(config1
, config2
): boolean
Helper method to decide when a new Config requires creating a new ConversationHandler or whether the old Config
's
ConversationHandler
can be used.
The order of configs doesn't matter.
Parameters
Name | Type |
---|---|
config1 | Config |
config2 | Config |
Returns
boolean
true if createConversation
should be called again
Defined in
createConversation
▸ createConversation(config
): ConversationHandler
Call this to create a conversation handler.
Parameters
Name | Type |
---|---|
config | Config |
Returns
The ConversationHandler is a bundle of functions to interact with the conversation.
Defined in
getCurrentExpirationTimestamp
▸ getCurrentExpirationTimestamp(responses
): null
| number
Get current expiration timestamp from the current list of reponses
Parameters
Name | Type | Description |
---|---|---|
responses | Response [] | the current list of user and bot responses (first argument in the subscribe callback) |
Returns
null
| number
an expiration timestamp in Unix Epoch (new Date().getTime()
), or null
if this is not known (typically occurs if the bot has not responded yet)
Defined in
promisify
▸ promisify<T
>(fn
, convo
, timeout?
): (payload
: T
) => Promise
<Response
| null
>
This package is intentionally designed with a subscription-based API as opposed to a promise-based one where each message corresponds to a single bot response, available asynchronously.
If you need a promise-based wrapper, you can use the promisify
helper available in the package:
Type parameters
Name | Description |
---|---|
T | the type of the function's params, e.g. for sendText it's text: string, context?: Context |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
fn | (payload : T ) => void | undefined | the function to wrap (e.g. convo.sendText , convo.sendChoice , etc.) |
convo | ConversationHandler | undefined | the ConversationHandler (from createConversation) |
timeout | number | 10000 | the timeout in milliseconds |
Returns
fn
A promise-wrapped version of the function. The function, when called, returns a promise that resolves to the Conversation's next response.
▸ (payload
): Promise
<Response
| null
>
Parameters
Name | Type |
---|---|
payload | T |
Returns
Promise
<Response
| null
>
Example
1import { createConversation, promisify } from "@nlxai/chat-core"; 2 3const convo = createConversation(config); 4 5const sendTextWrapped = promisify(convo.sendText, convo); 6 7sendTextWrapped("Hello").then((response) => { 8 console.log(response); 9});
Defined in
Interfaces
Interface: BotMessage
A message from the bot, as well as any choices the user can make.
Properties
messageId
• Optional
messageId: string
A unique identifier for the message.
Defined in
nodeId
• Optional
nodeId: string
The node id that this message is associated with. This is must be sent with a choice when the user is changing a previously sent choice.
Defined in
text
• text: string
The body of the message. Show this to the user.
Defined in
choices
• choices: Choice
[]
A selection of choices to show to the user. They may choose one of them.
Defined in
metadata
• Optional
metadata: BotMessageMetadata
Metadata
Defined in
selectedChoiceId
• Optional
selectedChoiceId: string
After a choice has been made by the user, this will be updated locally to the selected choice id. This field is set locally and does not come from the bot.
Defined in
Interface: BotMessageMetadata
Metadata for the individual bot message as well as whether the client should poll for more bot responses.
Properties
intentId
• Optional
intentId: string
The message node's intent
Defined in
Interface: BotRequest
The request data actually sent to the bot, slightly different from UserResponsePayload, which includes some UI-specific information
Properties
conversationId
• Optional
conversationId: string
The current conversation ID
Defined in
userId
• Optional
userId: string
The current user ID
Defined in
context
• Optional
context: Context
Request context, if applicable
Defined in
request
• request: Object
Main request
Type declaration
Name | Type | Description |
---|---|---|
unstructured? | { text : string } | Unstructured request |
unstructured.text | string | Request body text |
structured? | StructuredRequest & { slots? : SlotValue [] } | Structured request |
Defined in
Interface: BotResponse
A message from the bot
See also:
Properties
type
• type: "bot"
The type of the response is "bot"
for bot and "user"
for user, and "failure" for failure.
Defined in
receivedAt
• receivedAt: number
When the response was received
Defined in
payload
• payload: BotResponsePayload
The payload of the response
Defined in
Interface: BotResponseMetadata
Global state about the current conversation as well as whether the client should poll for more bot responses.
Properties
intentId
• Optional
intentId: string
The conversation's intent
Defined in
escalation
• Optional
escalation: boolean
Whether the current conversation has been marked as incomprehension.
Defined in
frustration
• Optional
frustration: boolean
Whether the current conversation has been marked frustrated
Defined in
incomprehension
• Optional
incomprehension: boolean
Whether the current conversation has been marked as incomprehension.
Defined in
uploadUrls
• uploadUrls: UploadUrl
[]
Upload URL's
Defined in
hasPendingDataRequest
• Optional
hasPendingDataRequest: boolean
Whether the client should poll for more bot responses.
Defined in
Interface: BotResponsePayload
The payload of the bot response
Properties
expirationTimestamp
• Optional
expirationTimestamp: number
If there isn't some interaction by this time, the conversation will expire.
Defined in
conversationId
• Optional
conversationId: string
The active conversation ID. If not set, a new conversation will be started.
Defined in
messages
• messages: BotMessage
[]
Any messages from the bot.
Defined in
metadata
• Optional
metadata: BotResponseMetadata
Global state about the current conversation as well as whether the client should poll for more bot responses.
Defined in
payload
• Optional
payload: string
If configured, the node's payload.
Defined in
modalities
• Optional
modalities: Record
<string
, any
>
If configured, the node's modalities and their payloads.
Defined in
context
• Optional
context: Context
If the node is set to send context, the whole context associated with the conversation.
Defined in
Interface: Choice
A choices to show to the user.
Properties
choiceId
• choiceId: string
choiceId
is used by sendChoice
to let the user choose this choice.
Defined in
choiceText
• choiceText: string
The text of the choice
Defined in
choicePayload
• Optional
choicePayload: any
An optional, schemaless payload for the choice.
Defined in
Interface: ChoiceRequestMetadata
Helps link the choice to the specific message in the conversation.
Properties
responseIndex
• Optional
responseIndex: number
The index of the Response associated with this choice.
Setting this ensures that local state's selectedChoiceId
on the corresponding BotResponse is set.
It is not sent to the bot.
Defined in
messageIndex
• Optional
messageIndex: number
The index of the BotMessage associated with this choice.
Setting this ensures that local state's selectedChoiceId
on the corresponding BotResponse is set.
It is not sent to the bot.
Defined in
nodeId
• Optional
nodeId: string
Required if you want to change a choice that's already been sent.
The nodeId
can be found in the corresponding BotMessage.
Defined in
intentId
• Optional
intentId: string
Intent ID, used for sending to the NLU to allow it to double-check
Defined in
Interface: Config
The configuration to create a conversation.
Properties
botUrl
• botUrl: string
Fetch this from the bot's Deployment page.
Defined in
headers
• headers: Record
<string
, string
> & { nlx-api-key
: string
}
Headers to forward to the NLX API.
Defined in
conversationId
• Optional
conversationId: string
Set conversationId
to continue an existing conversation. If not set, a new conversation will be started.
Defined in
userId
• Optional
userId: string
Setting the userID
allows it to be searchable in bot history, as well as usable via {System.userId}
in the intent.
Defined in
responses
• Optional
responses: Response
[]
When responses
is set, initialize the chatHandler with historical messages.
Defined in
failureMessage
• Optional
failureMessage: string
When set, this overrides the default failure message ("We encountered an issue. Please try again soon.").
Defined in
languageCode
• languageCode: string
The language code to use for the bot. In the browser this can be fetched with navigator.language
.
If you don't have translations, hard-code this to the language code you support.
Defined in
experimental
• Optional
experimental: Object
Experimental settings
Type declaration
Name | Type | Description |
---|---|---|
channelType? | string | Simulate alternative channel types |
completeBotUrl? | boolean | Prevent the languageCode parameter to be appended to the bot URL - used in special deployment environments such as the sandbox chat inside Dialog Studio |
Defined in
Interface: ConversationHandler
A bundle of functions to interact with a conversation, created by createConversation.
Properties
sendText
• sendText: (text
: string
, context?
: Context
) => void
Send user's message
Type declaration
▸ (text
, context?
): void
Parameters
Name | Type | Description |
---|---|---|
text | string | the user's message |
context? | Context | Context for usage later in the intent. |
Returns
void
Defined in
sendSlots
• sendSlots: (slots
: SlotsRecordOrArray
, context?
: Context
) => void
Send slots to the bot.
Type declaration
▸ (slots
, context?
): void
Parameters
Name | Type | Description |
---|---|---|
slots | SlotsRecordOrArray | The slots to populate |
context? | Context | Context for usage later in the intent. |
Returns
void
Defined in
sendChoice
• sendChoice: (choiceId
: string
, context?
: Context
, metadata?
: ChoiceRequestMetadata
) => void
Respond to a choice from the bot.
Type declaration
▸ (choiceId
, context?
, metadata?
): void
Parameters
Name | Type | Description |
---|---|---|
choiceId | string | - |
context? | Context | Context for usage later in the intent. |
metadata? | ChoiceRequestMetadata | links the choice to the specific message and node in the conversation. |
Returns
void
Defined in
sendWelcomeIntent
• sendWelcomeIntent: (context?
: Context
) => void
Trigger the welcome intent. This should be done when the user starts interacting with the chat.
Type declaration
▸ (context?
): void
Parameters
Name | Type | Description |
---|---|---|
context? | Context | Context for usage later in the intent. |
Returns
void
Defined in
sendIntent
• sendIntent: (intentId
: string
, context?
: Context
) => void
Trigger a specific intent.
Type declaration
▸ (intentId
, context?
): void
Parameters
Name | Type | Description |
---|---|---|
intentId | string | the intent to trigger. The id is the name under the Bot's Intents. |
context? | Context | Context for usage later in the intent. |
Returns
void
Defined in
sendStructured
• sendStructured: (request
: StructuredRequest
, context?
: Context
) => void
Send a combination of choice, slots, and intent in one request.
Type declaration
▸ (request
, context?
): void
Parameters
Name | Type | Description |
---|---|---|
request | StructuredRequest | |
context? | Context | Context for usage later in the intent. |
Returns
void
Defined in
subscribe
• subscribe: (subscriber
: Subscriber
) => () => void
Subscribe a callback to the conversation. On subscribe, the subscriber will receive all of the Responses that the conversation has already received.
Type declaration
▸ (subscriber
): () => void
Parameters
Name | Type | Description |
---|---|---|
subscriber | Subscriber | The callback to subscribe |
Returns
fn
▸ (): void
Returns
void
Defined in
unsubscribe
• unsubscribe: (subscriber
: Subscriber
) => void
Unsubscribe a callback from the conversation.
Type declaration
▸ (subscriber
): void
Parameters
Name | Type | Description |
---|---|---|
subscriber | Subscriber | The callback to unsubscribe |
Returns
void
Defined in
unsubscribeAll
• unsubscribeAll: () => void
Unsubscribe all callback from the conversation.
Type declaration
▸ (): void
Returns
void
Defined in
currentConversationId
• currentConversationId: () => undefined
| string
Get the current conversation ID if it's set, or undefined if there is no conversation.
Type declaration
▸ (): undefined
| string
Returns
undefined
| string
Defined in
reset
• reset: (options?
: { clearResponses?
: boolean
}) => void
Forces a new conversation. If clearResponses
is set to true, will also clear historical responses passed to subscribers.
Retains all existing subscribers.
Type declaration
▸ (options?
): void
Parameters
Name | Type | Description |
---|---|---|
options? | Object | - |
options.clearResponses? | boolean | If set to true, will clear historical responses passed to subscribers. |
Returns
void
Defined in
destroy
• destroy: () => void
Removes all subscribers and, if using websockets, closes the connection.
Type declaration
▸ (): void
Returns
void
Defined in
setBotRequestOverride
• setBotRequestOverride: (override
: undefined
| BotRequestOverride
) => void
Optional BotRequestOverride function used to bypass the bot request and handle them in a custom fashion
Type declaration
▸ (override
): void
Parameters
Name | Type |
---|---|
override | undefined | BotRequestOverride |
Returns
void
Defined in
Interface: FailureMessage
A failure message is received when the NLX api is unreachable, or sends an unparsable response.
Properties
type
• type: "failure"
The type of the response is "bot"
for bot and "user"
for user.
Defined in
payload
• payload: Object
The payload only includes an error message.
Type declaration
Name | Type | Description |
---|---|---|
text | string | The error message is either the default, or the failureMessage set in the Config. |
Defined in
receivedAt
• receivedAt: number
When the failure occurred.
Defined in
Interface: SlotValue
Values to fill an intent's attached slots.
An array of SlotValue
objects is equivalent to a SlotsRecord.
Properties
slotId
• slotId: string
The attached slot's name
Defined in
value
• value: any
Usually this will be a discrete value matching the slots's type. for custom slots, this can optionally be the value's ID.
Defined in
Interface: StructuredRequest
The body of sendStructured
Includes a combination of choice, slots, and intent in one request.
Properties
choiceId
• Optional
choiceId: string
The choiceId
is in the BotResponse's .payload.messages[].choices[].choiceId
fields
Defined in
nodeId
• Optional
nodeId: string
Required if you want to change a choice that's already been sent.
The nodeId
can be found in the corresponding BotMessage.
Defined in
intentId
• Optional
intentId: string
The intent to trigger. The intentId
is the name under the Bot's Intents.
Defined in
slots
• Optional
slots: SlotsRecordOrArray
The slots to populate
Defined in
uploadIds
• Optional
uploadIds: string
[]
Upload ID
Defined in
utterance
• Optional
utterance: string
Upload utterance
Defined in
Interface: UploadUrl
The upload destination for handling conversing with files
Properties
url
• url: string
The URL of the upload
Defined in
uploadId
• uploadId: string
The ID of the upload
Defined in
Interface: UserResponse
A message from the user
See also:
Properties
type
• type: "user"
The type of the response is "bot"
for bot and "user"
for user, and "failure" for failure.
Defined in
receivedAt
• receivedAt: number
When the response was received
Defined in
payload
• payload: UserResponsePayload
The payload of the response
Defined in
- Previous
- Headless API: Getting started