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