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