Skip to main content

Interceptors

Message transactions can be intercepted to change their contents or execute other code.

requestInterceptor

Triggered before a user message is sent out. This method MUST either return a RequestDetails object or an object that contains an error property to cancel the request.

Video demo

Example

Console:
>
chatElementRef.requestInterceptor = (requestDetails) => {
console.log(requestDetails); // printed above
requestDetails.body = {prompt: requestDetails.body.messages[0].text}; // custom body
return requestDetails;
};

RequestDetails

  • Type: {body: any, headers: {[key: string]: string}}

body is the outgoing requests's message contents.
headers is the outgoing requests's header contents.

responseInterceptor

  • Type: (response: any) => determined

Triggered when a message has been received from the target service. The types for the return argument is determined by the connection variety used:

  • If you are connecting to a server via the request property - the type will be Response.
  • If you are connecting via the directConnection property - the type will be defined by the chosen service API.

Video demo

Example

Console:
>
chatElementRef.responseInterceptor = (response) => {
console.log(response); // printed above
return response;
};

validateInput

  • Type: (text?: string, files?: File[]) => boolean

Triggered when the user changes input text or files that are going to be sent to the target service.
The method must return a boolean value with either true or false for whether the input contents are valid.

Example

chatElementRef.validateInput = (text, files) => {
return text || files.length > 0;
};