Skip to content

Commit

Permalink
Add separate handling for failure messages
Browse files Browse the repository at this point in the history
  • Loading branch information
peterszerzo committed Dec 29, 2023
1 parent 45cf3de commit a4eba0d
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 156 deletions.
50 changes: 16 additions & 34 deletions packages/chat-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ export type UserResponsePayload =
context?: Context;
} & StructuredRequest);

export type Response = BotResponse | UserResponse;
// Failure message

export interface FailureMessage {
type: "failure";
payload: {
text: string;
};
receivedAt: Time;
}

export type Response = BotResponse | UserResponse | FailureMessage;

export type Time = number;

Expand All @@ -88,8 +98,7 @@ export interface Config {
conversationId?: string;
userId?: string;
responses?: Response[];
failureMessages?: string[];
greetingMessages?: string[];
failureMessage?: string;
environment?: Environment;
headers?: {
[key: string]: string;
Expand All @@ -106,9 +115,7 @@ export interface Config {

const welcomeIntent = "NLX.Welcome";

const defaultFailureMessages = [
"We encountered an issue. Please try again soon.",
];
const defaultFailureMessage = "We encountered an issue. Please try again soon.";

export type State = Response[];

Expand Down Expand Up @@ -214,27 +221,7 @@ export const createConversation = (config: Config): ConversationHandler => {
const initialConversationId = config.conversationId || uuid();

let state: InternalState = {
responses:
config.responses ||
(config.greetingMessages && config.greetingMessages.length > 0
? [
{
type: "bot",
receivedAt: new Date().getTime(),
payload: {
conversationId: initialConversationId,
messages: config.greetingMessages.map(
(greetingMessage: string) => ({
messageId: undefined,
text: greetingMessage,
choices: [] as Array<Choice>,
selectedChoiceId: undefined,
}),
),
},
},
]
: []),
responses: config.responses || [],
userId: config.userId,
conversationId: initialConversationId,
};
Expand All @@ -255,15 +242,10 @@ export const createConversation = (config: Config): ConversationHandler => {

const failureHandler = () => {
const newResponse: Response = {
type: "bot",
type: "failure",
receivedAt: new Date().getTime(),
payload: {
messages: (config.failureMessages || defaultFailureMessages).map(
(messageBody: string): BotMessage => ({
text: messageBody,
choices: [] as Array<Choice>,
}),
),
text: config.failureMessage || defaultFailureMessages,
},
};
setState(
Expand Down
129 changes: 70 additions & 59 deletions packages/chat-widget/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,70 +110,81 @@ const MessageGroups: FC<{
customModalities: Record<string, CustomModalityComponent>;
}> = (props) => (
<C.MessageGroups>
{props.chat.responses.map((response, responseIndex) =>
response.type === "bot" ? (
<C.MessageGroup key={responseIndex}>
{response.payload.messages.map((botMessage, botMessageIndex) => (
<C.Message type="bot" key={botMessageIndex}>
{props.chat.responses.map((response, responseIndex) => {
if (response.type === "bot") {
return (
<C.MessageGroup key={responseIndex}>
{response.payload.messages.map((botMessage, botMessageIndex) => (
<C.Message type="bot" key={botMessageIndex}>
<C.MessageBody
dangerouslySetInnerHTML={{
__html: marked(botMessage.text),
}}
/>
{botMessage.choices.length > 0 && (
<C.ChoicesContainer>
{botMessage.choices.map((choice, choiceIndex) => (
<C.ChoiceButton
key={choiceIndex}
{...(() => {
return botMessage.selectedChoiceId
? {
disabled: true,
selected:
botMessage.selectedChoiceId ===
choice.choiceId,
}
: {
onClick: () => {
props.chat.conversationHandler.sendChoice(
choice.choiceId,
);
},
};
})()}
dangerouslySetInnerHTML={{
__html: marked(
choice.choiceText +
(false ? " asdf fadsfds fdsa fdsa fdsa " : ""),
),
}}
></C.ChoiceButton>
))}
</C.ChoicesContainer>
)}
</C.Message>
))}
{Object.entries(response.payload.modalities || {}).map(
([key, value]) => {
const Component = props.customModalities[key];
if (Component) {
return <Component key={key} data={value} />;
}
return null;
},
)}
</C.MessageGroup>
);
}

if (response.type === "failure") {
return <p>{response.payload.text}</p>;
}

if (response.type === "user" && response.payload.type === "text") {
return (
<C.MessageGroup key={responseIndex}>
<C.Message type="user">
<C.MessageBody
dangerouslySetInnerHTML={{
__html: marked(botMessage.text),
__html: marked(response.payload.text),
}}
/>
{botMessage.choices.length > 0 && (
<C.ChoicesContainer>
{botMessage.choices.map((choice, choiceIndex) => (
<C.ChoiceButton
key={choiceIndex}
{...(() => {
return botMessage.selectedChoiceId
? {
disabled: true,
selected:
botMessage.selectedChoiceId === choice.choiceId,
}
: {
onClick: () => {
props.chat.conversationHandler.sendChoice(
choice.choiceId,
);
},
};
})()}
dangerouslySetInnerHTML={{
__html: marked(
choice.choiceText +
(false ? " asdf fadsfds fdsa fdsa fdsa " : ""),
),
}}
></C.ChoiceButton>
))}
</C.ChoicesContainer>
)}
</C.Message>
))}
{Object.entries(response.payload.modalities || {}).map(
([key, value]) => {
const Component = props.customModalities[key];
if (Component) {
return <Component key={key} data={value} />;
}
return null;
},
)}
</C.MessageGroup>
) : response.payload.type === "text" ? (
<C.MessageGroup key={responseIndex}>
<C.Message type="user">
<C.MessageBody
dangerouslySetInnerHTML={{
__html: marked(response.payload.text),
}}
/>
</C.Message>
</C.MessageGroup>
) : null,
)}
</C.MessageGroup>
);
}
})}
{props.children}
</C.MessageGroups>
);
Expand Down
Loading

0 comments on commit a4eba0d

Please sign in to comment.