-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathuseChatbot.ts
134 lines (123 loc) · 3.84 KB
/
useChatbot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import axios from "axios";
import { useState } from "react";
import type { MessageProps } from "@patternfly/virtual-assistant/dist/dynamic/Message";
import type {
ExtendedMessage,
ChatRequest,
ChatResponse,
} from "../types/Message";
import logo from "../assets/lightspeed.svg";
const userName = document.getElementById("user_name")?.innerText ?? "User";
const botName =
document.getElementById("bot_name")?.innerText ?? "Ansible Lightspeed";
export const readCookie = (name: string): string | null => {
const nameEQ = name + "=";
const ca = document.cookie.split(";");
for (const c of ca) {
const cookie = c.trim();
if (cookie.startsWith(nameEQ)) {
return cookie.substring(nameEQ.length, cookie.length);
}
}
return null;
};
const getTimestamp = () => {
const date = new Date();
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
};
export const botMessage = (content: string): MessageProps => ({
role: "bot",
content,
name: botName,
avatar: logo,
timestamp: getTimestamp(),
actions: {
positive: { onClick: () => console.log("Good response") },
negative: { onClick: () => console.log("Bad response") },
},
});
type AlertMessage = {
title: string;
message: string;
variant: "success" | "danger" | "warning" | "info" | "custom";
};
const INITIAL_NOTICE: AlertMessage = {
title: "Notice",
message: `<This is a placeholder to inform the user of our chatbot disclaimer,
to be determined at some point before we release to external customers.>
Please do not include any personal or confidential information
in your interaction with the virtual assistant. The tool is
intended to assist with general queries.`,
variant: "info",
};
export const useChatbot = () => {
const [messages, setMessages] = useState<ExtendedMessage[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [alertMessage, setAlertMessage] = useState<AlertMessage | undefined>(
INITIAL_NOTICE,
);
const [conversationId, setConversationId] = useState<
string | null | undefined
>(undefined);
const handleSend = async (message: string) => {
const userMessage: ExtendedMessage = {
role: "user",
content: message,
name: userName,
avatar:
"https://developers.redhat.com/sites/default/files/inline-images/Skill%20development_0.png",
timestamp: getTimestamp(),
referenced_documents: [],
};
setMessages((msgs: ExtendedMessage[]) => [...msgs, userMessage]);
const chatRequest: ChatRequest = {
conversation_id: conversationId,
query: message,
};
setIsLoading(true);
try {
const csrfToken = readCookie("csrftoken");
const resp = await axios.post(
import.meta.env.PROD
? "/api/v0/ai/chat/"
: "http://localhost:8080/v1/query/",
chatRequest,
{
headers: {
"Content-Type": "application/json",
"X-CSRFToken": csrfToken,
},
},
);
if (resp.status === 200) {
const chatResponse: ChatResponse = resp.data;
const referenced_documents = chatResponse.referenced_documents;
if (!conversationId) {
setConversationId(chatResponse.conversation_id);
}
setMessages((msgs: ExtendedMessage[]) => [
...msgs,
{
referenced_documents,
...botMessage(chatResponse.response),
},
]);
} else {
setAlertMessage({
title: "Error",
message: `Bot returned status_code ${resp.status}`,
variant: "danger",
});
}
} catch (e) {
setAlertMessage({
title: "Error",
message: `An unexpected error occured: ${e}`,
variant: "danger",
});
} finally {
setIsLoading(false);
}
};
return { messages, isLoading, handleSend, alertMessage, setAlertMessage };
};