Skip to content

Commit 022c958

Browse files
committed
add sources
1 parent 0084925 commit 022c958

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/components/SupportAI.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Markdown from "react-markdown";
33
import { fetchEventSource } from "@microsoft/fetch-event-source";
44

55
type Messages = { role: "user" | "assistant"; content: string }[];
6+
type Sources = { title: string; file_path: string }[];
67

78
function Messages({ messages }: { messages: Messages }) {
89
return (
@@ -39,9 +40,11 @@ export default function SupportAI() {
3940
const { signal } = controller;
4041

4142
let chunkedAnswer = "";
43+
let sources: Sources = [];
4244

4345
await fetchEventSource(
44-
"https://support-ai.cloudflaresupport.workers.dev/devdocs/ask",
46+
"http://localhost:8010/proxy/devdocs/ask",
47+
// "https://support-ai.cloudflaresupport.workers.dev/devdocs/ask",
4548
{
4649
method: "POST",
4750
body: JSON.stringify({
@@ -73,9 +76,26 @@ export default function SupportAI() {
7376
onmessage(ev) {
7477
if (ev.data === "[DONE]") {
7578
controller.abort();
79+
80+
setMessages((messages) => {
81+
const newMessages = [...messages];
82+
newMessages[newMessages.length - 1].content += [
83+
"\n\n",
84+
"I used these sources to answer your question, please review them if you need more information:",
85+
"\n\n",
86+
sources
87+
.map((source) => `- [${source.title}](${source.file_path})`)
88+
.join("\n"),
89+
].join("\n");
90+
return newMessages;
91+
});
7692
}
7793

78-
const { threadId, response } = JSON.parse(ev.data);
94+
const { threadId, response, botResponse } = JSON.parse(ev.data);
95+
96+
if (botResponse?.sources) {
97+
sources = botResponse.sources;
98+
}
7999

80100
if (threadId) {
81101
setThreadId(threadId);

0 commit comments

Comments
 (0)