Elffuss BraidHub

kikocis/continue public

abc6d87d99a5 · 9.8 KiBRawPermalink
1import {
2 ArrowPathIcon,
3 ArrowTopRightOnSquareIcon,
4 ClipboardIcon,
5 Cog6ToothIcon,
6 KeyIcon,
7} from "@heroicons/react/24/outline";
8import { DISCORD_LINK, GITHUB_LINK } from "core/util/constants";
9import { useContext, useMemo } from "react";
10import { GhostButton, SecondaryButton } from "../../components";
11import { useMainEditor } from "../../components/mainInput/TipTapEditor";
12import { DiscordIcon } from "../../components/svg/DiscordIcon";
13import { GithubIcon } from "../../components/svg/GithubIcon";
14import ToggleDiv from "../../components/ToggleDiv";
15import { useAuth } from "../../context/Auth";
16import { IdeMessengerContext } from "../../context/IdeMessenger";
17import { useAppDispatch, useAppSelector } from "../../redux/hooks";
18import { selectSelectedChatModel } from "../../redux/slices/configSlice";
19import { selectSelectedProfile } from "../../redux/slices/profilesSlice";
20import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice";
21import { streamResponseThunk } from "../../redux/thunks/streamResponse";
22import { isLocalProfile } from "../../util";
23import { analyzeError } from "../../util/errorAnalysis";
24import { ModelsAddOnLimitDialog } from "./ModelsAddOnLimitDialog";
25
26interface StreamErrorProps {
27 error: unknown;
28}
29
30const StreamErrorDialog = ({ error }: StreamErrorProps) => {
31 const dispatch = useAppDispatch();
32 const ideMessenger = useContext(IdeMessengerContext);
33 const selectedModel = useAppSelector(selectSelectedChatModel);
34 const selectedProfile = useAppSelector(selectSelectedProfile);
35 const { session, refreshProfiles } = useAuth();
36 const { mainEditor } = useMainEditor();
37
38 const {
39 parsedError,
40 statusCode,
41 message,
42 modelTitle,
43 providerName,
44 apiKeyUrl,
45 } = useMemo(() => analyzeError(error, selectedModel), [error, selectedModel]);
46
47 const handleRefreshProfiles = () => {
48 void refreshProfiles();
49 dispatch(setShowDialog(false));
50 dispatch(setDialogMessage(undefined));
51 };
52
53 const copyErrorToClipboard = () => {
54 void navigator.clipboard.writeText(parsedError);
55 };
56
57 const history = useAppSelector((store) => store.session.history);
58
59 const checkKeysButton = apiKeyUrl ? (
60 <GhostButton
61 className="flex items-center"
62 onClick={() => {
63 ideMessenger.post("controlPlane/openUrl", {
64 path: apiKeyUrl,
65 orgSlug: undefined,
66 });
67 }}
68 >
69 <KeyIcon className="mr-1.5 h-3.5 w-3.5" />
70 <span>View key</span>
71 </GhostButton>
72 ) : null;
73
74 const configButton = (
75 <GhostButton
76 className="flex items-center"
77 onClick={() => {
78 ideMessenger.post("config/openProfile", {
79 profileId: undefined,
80 });
81 }}
82 >
83 <Cog6ToothIcon className="mr-1.5 h-3.5 w-3.5" />
84 <span>View config</span>
85 </GhostButton>
86 );
87
88 const resubmitButton = (
89 <GhostButton
90 className="flex items-center"
91 onClick={() => {
92 let index = -1;
93 for (let i = history.length - 1; i >= 0; i--) {
94 if (
95 history[i].message.role === "user" ||
96 history[i].message.role === "tool"
97 ) {
98 index = i;
99 break;
100 }
101 }
102
103 if (!mainEditor) {
104 console.error("Main editor not found, cannot resubmit message.");
105 return;
106 }
107
108 const editorState =
109 index === -1 ? mainEditor.getJSON() : history[index].editorState;
110
111 void dispatch(
112 streamResponseThunk({
113 editorState,
114 modifiers: {
115 noContext: true,
116 useCodebase: false,
117 },
118 index: index === -1 ? 0 : index,
119 }),
120 );
121 dispatch(setShowDialog(false));
122 dispatch(setDialogMessage(undefined));
123 }}
124 >
125 <ArrowPathIcon className="mr-1.5 h-3.5 w-3.5" />
126 <span>Resubmit last message</span>
127 </GhostButton>
128 );
129
130 if (
131 parsedError === "You have exceeded the chat limit for the Models Add-On."
132 ) {
133 return <ModelsAddOnLimitDialog />;
134 }
135
136 let errorContent = (
137 <div className="mb-1 mt-3">
138 <div className="m-0 p-0">
139 <p className="m-0 mb-2 p-0">
140 There was an error handling the response from{" "}
141 {selectedModel?.title || "the model"}.
142 </p>
143 <p className="m-0 p-0">
144 Please try to submit your message again, and if the error persists,
145 let us know by reporting the issue using the buttons below.
146 </p>
147 <div className="mt-3">{resubmitButton}</div>
148 </div>
149 </div>
150 );
151
152 // Display components for specific errors
153 if (statusCode === 429) {
154 errorContent = (
155 <div className="flex flex-col gap-2">
156 <span>
157 {`This might mean your ${modelTitle} usage has been rate limited
158 by ${providerName}.`}
159 </span>
160 <div className="flex flex-row flex-wrap justify-start gap-3 py-4">
161 {checkKeysButton}
162 {configButton}
163 </div>
164 </div>
165 );
166 }
167
168 if (statusCode === 404) {
169 errorContent = (
170 <div className="flex flex-col gap-2">
171 <span>Likely causes:</span>
172 <ul className="m-0">
173 <li>
174 <span>Invalid</span>
175 <code>apiBase</code>
176 {selectedModel && (
177 <>
178 <span>{`: `}</span>
179 <code>{selectedModel.apiBase}</code>
180 </>
181 )}
182 </li>
183 <li>
184 <span>Model/deployment not found</span>
185 {selectedModel && (
186 <>
187 <span>{` for: `}</span>
188 <code>{selectedModel.model}</code>
189 </>
190 )}
191 </li>
192 </ul>
193 <div>{configButton}</div>
194 </div>
195 );
196 }
197
198 if (statusCode === 401) {
199 errorContent = (
200 <div className="flex flex-col gap-2">
201 {session && selectedProfile && !isLocalProfile(selectedProfile) && (
202 <div className="flex flex-col gap-1">
203 <span>{`If your hub secret values may have changed, refresh your assistants`}</span>
204 <SecondaryButton onClick={handleRefreshProfiles}>
205 Refresh assistant secrets
206 </SecondaryButton>
207 </div>
208 )}
209 <span>{`It's possible that your API key is invalid.`}</span>
210 <div className="flex flex-row flex-wrap gap-2">
211 {checkKeysButton}
212 {configButton}
213 </div>
214 </div>
215 );
216 }
217
218 if (statusCode === 403) {
219 errorContent = (
220 <div className="flex flex-col gap-2">
221 <span>{`Likely cause: not authorized to access the model deployment.`}</span>
222 <div className="flex flex-row flex-wrap gap-2">
223 {checkKeysButton}
224 {configButton}
225 </div>
226 </div>
227 );
228 }
229
230 if (
231 message &&
232 (message.toLowerCase().includes("overloaded") ||
233 message.toLowerCase().includes("malformed json"))
234 ) {
235 errorContent = (
236 <div className="flex flex-col gap-2">
237 <span>{`Most likely, the provider's server(s) are overloaded and streaming was interrupted. Try again later`}</span>
238 {selectedModel ? (
239 <span>
240 {`Provider: `}
241 <code>{selectedModel.provider}</code>
242 </span>
243 ) : null}
244 {/* TODO: status page links for providers? */}
245 </div>
246 );
247 }
248
249 console.log({ message });
250
251 return (
252 <div className="flex flex-col gap-4 px-3 pb-3 pt-3">
253 {/* Concise error title */}
254 <h3 className="text-error m-0 p-0 text-lg font-medium">
255 Error handling model response
256 </h3>
257
258 {errorContent}
259
260 {/* Expandable technical details using ToggleDiv */}
261 {message && (
262 <div className="mb-2">
263 <ToggleDiv title="View error output" testId="error-output-toggle">
264 <div className="flex flex-col gap-0 rounded-sm">
265 <code className="text-editor-foreground block max-h-48 overflow-y-auto p-3 font-mono text-xs">
266 {parsedError}
267 </code>
268
269 <div className="flex flex-row items-center justify-end gap-2 p-2">
270 <GhostButton
271 onClick={copyErrorToClipboard}
272 className="flex items-center"
273 >
274 <ClipboardIcon className="mr-1.5 h-3.5 w-3.5" />
275 <span>Copy output</span>
276 </GhostButton>
277
278 <GhostButton
279 onClick={() => {
280 ideMessenger.post("toggleDevTools", undefined);
281 }}
282 className="flex items-center"
283 >
284 <ArrowTopRightOnSquareIcon className="mr-1.5 h-4 w-4" />
285 <span className="text-xs">View Logs</span>
286 </GhostButton>
287 </div>
288 </div>
289 </ToggleDiv>
290 </div>
291 )}
292
293 <div>
294 <span className="text-base font-medium">Report this error</span>
295 <div className="mt-2 flex flex-row flex-wrap items-center gap-2">
296 <GhostButton
297 className="flex flex-row items-center gap-2 rounded px-3 py-1.5"
298 onClick={() => {
299 ideMessenger.post("controlPlane/openUrl", {
300 path: GITHUB_LINK,
301 orgSlug: undefined,
302 });
303 }}
304 >
305 <GithubIcon className="h-5 w-5" />
306 <span className="xs:flex hidden">Github</span>
307 </GhostButton>
308 <GhostButton
309 className="flex flex-row items-center gap-2 rounded px-3 py-1.5"
310 onClick={() => {
311 ideMessenger.post("controlPlane/openUrl", {
312 path: DISCORD_LINK,
313 orgSlug: undefined,
314 });
315 }}
316 >
317 <DiscordIcon className="h-5 w-5" />
318 <span className="xs:flex hidden">Discord</span>
319 </GhostButton>
320 </div>
321 </div>
322 </div>
323 );
324};
325
326export default StreamErrorDialog;