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