Elffuss BraidHub

kikocis/continue public

37eef76c38ed · 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("Clicked reload config from stream error dialog");
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 element: selectedModel ?? undefined,
81 });
82 }}
83 >
84 <Cog6ToothIcon className="mr-1.5 h-3.5 w-3.5" />
85 <span>View config</span>
86 </GhostButton>
87 );
88
89 const resubmitButton = (
90 <GhostButton
91 className="flex items-center"
92 onClick={() => {
93 let index = -1;
94 for (let i = history.length - 1; i >= 0; i--) {
95 if (
96 history[i].message.role === "user" ||
97 history[i].message.role === "tool"
98 ) {
99 index = i;
100 break;
101 }
102 }
103
104 if (!mainEditor) {
105 console.error("Main editor not found, cannot resubmit message.");
106 return;
107 }
108
109 const editorState =
110 index === -1 ? mainEditor.getJSON() : history[index].editorState;
111
112 void dispatch(
113 streamResponseThunk({
114 editorState,
115 modifiers: {
116 noContext: true,
117 useCodebase: false,
118 },
119 index: index === -1 ? 0 : index,
120 }),
121 );
122 dispatch(setShowDialog(false));
123 dispatch(setDialogMessage(undefined));
124 }}
125 >
126 <ArrowPathIcon className="mr-1.5 h-3.5 w-3.5" />
127 <span>Resubmit last message</span>
128 </GhostButton>
129 );
130
131 if (
132 parsedError === "You have exceeded the chat limit for the Models Add-On."
133 ) {
134 return <ModelsAddOnLimitDialog />;
135 }
136
137 let errorContent = (
138 <div className="mb-1 mt-3">
139 <div className="m-0 p-0">
140 <p className="m-0 mb-2 p-0">
141 There was an error handling the response from{" "}
142 {selectedModel?.title || "the model"}.
143 </p>
144 <p className="m-0 p-0">
145 Please try to submit your message again, and if the error persists,
146 let us know by reporting the issue using the buttons below.
147 </p>
148 <div className="mt-3">{resubmitButton}</div>
149 </div>
150 </div>
151 );
152
153 // Display components for specific errors
154 if (statusCode === 429) {
155 errorContent = (
156 <div className="flex flex-col gap-2">
157 <span>
158 {`This might mean your ${modelTitle} usage has been rate limited
159 by ${providerName}.`}
160 </span>
161 <div className="flex flex-row flex-wrap justify-start gap-3 py-4">
162 {checkKeysButton}
163 {configButton}
164 </div>
165 </div>
166 );
167 }
168
169 if (statusCode === 404) {
170 errorContent = (
171 <div className="flex flex-col gap-2">
172 <span>Likely causes:</span>
173 <ul className="m-0">
174 <li>
175 <span>Invalid</span>
176 <code>apiBase</code>
177 {selectedModel && (
178 <>
179 <span>{`: `}</span>
180 <code>{selectedModel.apiBase}</code>
181 </>
182 )}
183 </li>
184 <li>
185 <span>Model/deployment not found</span>
186 {selectedModel && (
187 <>
188 <span>{` for: `}</span>
189 <code>{selectedModel.model}</code>
190 </>
191 )}
192 </li>
193 </ul>
194 <div>{configButton}</div>
195 </div>
196 );
197 }
198
199 if (statusCode === 401) {
200 errorContent = (
201 <div className="flex flex-col gap-2">
202 {session && selectedProfile && !isLocalProfile(selectedProfile) && (
203 <div className="flex flex-col gap-1">
204 <span>{`If your hub secret values may have changed, refresh your assistants`}</span>
205 <SecondaryButton onClick={handleRefreshProfiles}>
206 Refresh assistant secrets
207 </SecondaryButton>
208 </div>
209 )}
210 <span>{`It's possible that your API key is invalid.`}</span>
211 <div className="flex flex-row flex-wrap gap-2">
212 {checkKeysButton}
213 {configButton}
214 </div>
215 </div>
216 );
217 }
218
219 if (statusCode === 403) {
220 errorContent = (
221 <div className="flex flex-col gap-2">
222 <span>{`Likely cause: not authorized to access the model deployment.`}</span>
223 <div className="flex flex-row flex-wrap gap-2">
224 {checkKeysButton}
225 {configButton}
226 </div>
227 </div>
228 );
229 }
230
231 if (
232 message &&
233 (message.toLowerCase().includes("overloaded") ||
234 message.toLowerCase().includes("malformed json"))
235 ) {
236 errorContent = (
237 <div className="flex flex-col gap-2">
238 <span>{`Most likely, the provider's server(s) are overloaded and streaming was interrupted. Try again later`}</span>
239 {selectedModel ? (
240 <span>
241 {`Provider: `}
242 <code>{selectedModel.provider}</code>
243 </span>
244 ) : null}
245 {/* TODO: status page links for providers? */}
246 </div>
247 );
248 }
249
250 return (
251 <div className="flex flex-col gap-4 px-3 pb-3 pt-3">
252 {/* Concise error title */}
253 <h3 className="text-error m-0 p-0 text-lg font-medium">
254 Error handling model response
255 </h3>
256
257 {errorContent}
258
259 {/* Expandable technical details using ToggleDiv */}
260 {message && (
261 <div className="mb-2">
262 <ToggleDiv title="View error output" testId="error-output-toggle">
263 <div className="flex flex-col gap-0 rounded-sm">
264 <code className="text-editor-foreground block max-h-48 overflow-y-auto p-3 font-mono text-xs">
265 {parsedError}
266 </code>
267
268 <div className="flex flex-row items-center justify-end gap-2 p-2">
269 <GhostButton
270 onClick={copyErrorToClipboard}
271 className="flex items-center"
272 >
273 <ClipboardIcon className="mr-1.5 h-3.5 w-3.5" />
274 <span>Copy output</span>
275 </GhostButton>
276
277 <GhostButton
278 onClick={() => {
279 ideMessenger.post("toggleDevTools", undefined);
280 }}
281 className="flex items-center"
282 >
283 <ArrowTopRightOnSquareIcon className="mr-1.5 h-4 w-4" />
284 <span className="text-xs">View Logs</span>
285 </GhostButton>
286 </div>
287 </div>
288 </ToggleDiv>
289 </div>
290 )}
291
292 <div>
293 <span className="text-base font-medium">Report this error</span>
294 <div className="mt-2 flex flex-row flex-wrap items-center gap-2">
295 <GhostButton
296 className="flex flex-row items-center gap-2 rounded px-3 py-1.5"
297 onClick={() => {
298 ideMessenger.post("controlPlane/openUrl", {
299 path: GITHUB_LINK,
300 orgSlug: undefined,
301 });
302 }}
303 >
304 <GithubIcon className="h-5 w-5" />
305 <span className="xs:flex hidden">Github</span>
306 </GhostButton>
307 <GhostButton
308 className="flex flex-row items-center gap-2 rounded px-3 py-1.5"
309 onClick={() => {
310 ideMessenger.post("controlPlane/openUrl", {
311 path: DISCORD_LINK,
312 orgSlug: undefined,
313 });
314 }}
315 >
316 <DiscordIcon className="h-5 w-5" />
317 <span className="xs:flex hidden">Discord</span>
318 </GhostButton>
319 </div>
320 </div>
321 </div>
322 );
323};
324
325export default StreamErrorDialog;