-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathindex.jsx
More file actions
202 lines (191 loc) · 5.82 KB
/
index.jsx
File metadata and controls
202 lines (191 loc) · 5.82 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { useEffect, useRef, useState } from "react";
import "./styles.css";
import { DEFAULT_SUGGESTED, getTheme } from "./theme";
import { makeMarkdownComponents } from "./markdown";
import { streamAnswer } from "./streamAnswer";
import { sendFeedback } from "./sendFeedback";
import LauncherButton from "./LauncherButton";
import Panel from "./Panel";
export default function AztecDocsWidget({
apiHost,
apiKey,
title = "Ask about Aztec",
heroTitle = "Aztec Docs Assistant",
heroDescription = "Ask me anything about building on the privacy network — Noir, rollups, nullifiers, testnet setup.",
suggestedPrompts = DEFAULT_SUGGESTED,
theme = "ink",
accent = "chartreuse",
buttonStyle = "symbol",
size = "roomy",
position = "br",
motif = true,
}) {
const [open, setOpen] = useState(false);
const [expanded, setExpanded] = useState(false);
const [input, setInput] = useState("");
const [messages, setMessages] = useState([]);
const [streaming, setStreaming] = useState(false);
const [streamText, setStreamText] = useState("");
const [streamSources, setStreamSources] = useState([]);
const [conversationId, setConversationId] = useState(null);
const [feedbackByIndex, setFeedbackByIndex] = useState({});
const [feedbackErrorsByIndex, setFeedbackErrorsByIndex] = useState({});
const scrollRef = useRef(null);
const abortRef = useRef(null);
const tokens = React.useMemo(() => getTheme(theme, accent), [theme, accent]);
const mdComponents = React.useMemo(
() => makeMarkdownComponents(tokens.isInk, tokens.accentColor),
[tokens.isInk, tokens.accentColor],
);
// Only auto-scroll to the bottom when a new message is appended (user
// sends a question). Don't follow streaming tokens — the user should
// be free to scroll away while the response is generating.
useEffect(() => {
if (scrollRef.current)
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}, [messages.length]);
useEffect(() => () => abortRef.current?.abort(), []);
async function handleSend(text) {
const question = (text ?? input).trim();
if (!question || streaming) return;
setInput("");
const nextHistory = messages.map((m) => ({
prompt: m.prompt,
response: m.response,
}));
const nextMessages = [
...messages,
{ prompt: question, response: "", sources: [] },
];
setMessages(nextMessages);
setStreaming(true);
setStreamText("");
setStreamSources([]);
abortRef.current?.abort();
const controller = new AbortController();
abortRef.current = controller;
let acc = "";
let sources = [];
let errorMessage = null;
try {
await streamAnswer({
apiHost,
apiKey,
question,
history: nextHistory,
conversationId,
signal: controller.signal,
onToken: (chunk) => {
acc += chunk;
setStreamText(acc);
},
onSource: (src) => {
sources = sources.concat(src);
setStreamSources(sources);
},
onConversationId: (id) => setConversationId(id),
onError: (message) => {
errorMessage = message;
},
onDone: () => {},
});
} catch (err) {
if (err.name !== "AbortError") {
errorMessage =
errorMessage ||
"Something went wrong fetching an answer. Please try again.";
}
}
setMessages((prev) => {
const copy = [...prev];
copy[copy.length - 1] = {
prompt: question,
response: acc,
sources,
error: errorMessage,
};
return copy;
});
setStreaming(false);
setStreamText("");
setStreamSources([]);
}
function handleReset() {
abortRef.current?.abort();
setMessages([]);
setStreaming(false);
setStreamText("");
setStreamSources([]);
setConversationId(null);
setFeedbackByIndex({});
setFeedbackErrorsByIndex({});
}
async function handleFeedback(messageIndex, kind) {
if (!conversationId) return;
if (feedbackByIndex[messageIndex]) return;
setFeedbackByIndex((prev) => ({ ...prev, [messageIndex]: kind }));
setFeedbackErrorsByIndex((prev) => {
if (!prev[messageIndex]) return prev;
const copy = { ...prev };
delete copy[messageIndex];
return copy;
});
try {
await sendFeedback({
apiHost,
apiKey,
conversationId,
questionIndex: messageIndex,
feedback: kind,
});
} catch (err) {
setFeedbackByIndex((prev) => {
const copy = { ...prev };
delete copy[messageIndex];
return copy;
});
setFeedbackErrorsByIndex((prev) => ({ ...prev, [messageIndex]: true }));
}
}
return (
<div className="azw">
{!open && (
<LauncherButton
buttonStyle={buttonStyle}
position={position}
onOpen={() => setOpen(true)}
/>
)}
{open && (
<Panel
title={title}
heroTitle={heroTitle}
heroDescription={heroDescription}
suggestedPrompts={suggestedPrompts}
motif={motif}
position={position}
size={size}
tokens={tokens}
mdComponents={mdComponents}
messages={messages}
streaming={streaming}
streamText={streamText}
streamSources={streamSources}
input={input}
onInputChange={setInput}
onSend={handleSend}
onSuggest={handleSend}
onReset={handleReset}
onClose={() => setOpen(false)}
expanded={expanded}
onToggleExpanded={() => setExpanded((v) => !v)}
scrollRef={scrollRef}
conversationId={conversationId}
feedbackByIndex={feedbackByIndex}
feedbackErrorsByIndex={feedbackErrorsByIndex}
onFeedback={handleFeedback}
/>
)}
</div>
);
}