| 1 | import { |
| 2 | ArrowLeftIcon, |
| 3 | ChatBubbleOvalLeftIcon, |
| 4 | CodeBracketSquareIcon, |
| 5 | ExclamationTriangleIcon, |
| 6 | } from "@heroicons/react/24/outline"; |
| 7 | import { JSONContent } from "@tiptap/react"; |
| 8 | import { InputModifiers } from "core"; |
| 9 | import { usePostHog } from "posthog-js/react"; |
| 10 | import { |
| 11 | Fragment, |
| 12 | useCallback, |
| 13 | useContext, |
| 14 | useEffect, |
| 15 | useRef, |
| 16 | useState, |
| 17 | } from "react"; |
| 18 | import { ErrorBoundary } from "react-error-boundary"; |
| 19 | import { useDispatch, useSelector } from "react-redux"; |
| 20 | import styled from "styled-components"; |
| 21 | import { |
| 22 | Button, |
| 23 | defaultBorderRadius, |
| 24 | lightGray, |
| 25 | vscBackground, |
| 26 | vscForeground, |
| 27 | } from "../components"; |
| 28 | import { ChatScrollAnchor } from "../components/ChatScrollAnchor"; |
| 29 | import StepContainer from "../components/gui/StepContainer"; |
| 30 | import TimelineItem from "../components/gui/TimelineItem"; |
| 31 | import ContinueInputBox from "../components/mainInput/ContinueInputBox"; |
| 32 | import { defaultInputModifiers } from "../components/mainInput/inputModifiers"; |
| 33 | import { TutorialCard } from "../components/mainInput/TutorialCard"; |
| 34 | import { |
| 35 | OnboardingCard, |
| 36 | useOnboardingCard, |
| 37 | } from "../components/OnboardingCard"; |
| 38 | import { IdeMessengerContext } from "../context/IdeMessenger"; |
| 39 | import useChatHandler from "../hooks/useChatHandler"; |
| 40 | import useHistory from "../hooks/useHistory"; |
| 41 | import { useTutorialCard } from "../hooks/useTutorialCard"; |
| 42 | import { useWebviewListener } from "../hooks/useWebviewListener"; |
| 43 | import { defaultModelSelector } from "../redux/selectors/modelSelectors"; |
| 44 | import { |
| 45 | clearLastResponse, |
| 46 | deleteMessage, |
| 47 | newSession, |
| 48 | setInactive, |
| 49 | } from "../redux/slices/stateSlice"; |
| 50 | import { |
| 51 | setDialogEntryOn, |
| 52 | setDialogMessage, |
| 53 | setShowDialog, |
| 54 | } from "../redux/slices/uiStateSlice"; |
| 55 | import { RootState } from "../redux/store"; |
| 56 | import { |
| 57 | getFontSize, |
| 58 | getMetaKeyLabel, |
| 59 | isJetBrains, |
| 60 | isMetaEquivalentKeyPressed, |
| 61 | } from "../util"; |
| 62 | import { FREE_TRIAL_LIMIT_REQUESTS } from "../util/freeTrial"; |
| 63 | import { getLocalStorage, setLocalStorage } from "../util/localStorage"; |
| 64 | |
| 65 | const TopGuiDiv = styled.div<{ |
| 66 | showScrollbar?: boolean; |
| 67 | }>` |
| 68 | overflow-y: auto; |
| 69 | height: 100%; |
| 70 | scrollbar-width: ${(props) => (props.showScrollbar ? "thin" : "none")}; |
| 71 | |
| 72 | &::-webkit-scrollbar { |
| 73 | display: ${(props) => (props.showScrollbar ? "block" : "none")}; |
| 74 | } |
| 75 | `; |
| 76 | |
| 77 | const StopButton = styled.div` |
| 78 | width: fit-content; |
| 79 | margin-right: auto; |
| 80 | margin-left: auto; |
| 81 | |
| 82 | font-size: ${getFontSize() - 2}px; |
| 83 | |
| 84 | border: 0.5px solid ${lightGray}; |
| 85 | border-radius: ${defaultBorderRadius}; |
| 86 | padding: 4px 8px; |
| 87 | color: ${lightGray}; |
| 88 | |
| 89 | cursor: pointer; |
| 90 | `; |
| 91 | |
| 92 | const StepsDiv = styled.div` |
| 93 | position: relative; |
| 94 | background-color: transparent; |
| 95 | |
| 96 | & > * { |
| 97 | position: relative; |
| 98 | } |
| 99 | |
| 100 | .thread-message { |
| 101 | margin: 8px 4px 0 4px; |
| 102 | padding-bottom: 8px; |
| 103 | } |
| 104 | `; |
| 105 | |
| 106 | const NewSessionButton = styled.div` |
| 107 | width: fit-content; |
| 108 | margin-right: auto; |
| 109 | margin-left: 6px; |
| 110 | margin-top: 2px; |
| 111 | margin-bottom: 8px; |
| 112 | font-size: ${getFontSize() - 2}px; |
| 113 | |
| 114 | border-radius: ${defaultBorderRadius}; |
| 115 | padding: 2px 6px; |
| 116 | color: ${lightGray}; |
| 117 | |
| 118 | &:hover { |
| 119 | background-color: ${lightGray}33; |
| 120 | color: ${vscForeground}; |
| 121 | } |
| 122 | |
| 123 | cursor: pointer; |
| 124 | `; |
| 125 | |
| 126 | function fallbackRender({ error, resetErrorBoundary }: any) { |
| 127 | // Call resetErrorBoundary() to reset the error boundary and retry the render. |
| 128 | |
| 129 | return ( |
| 130 | <div |
| 131 | role="alert" |
| 132 | className="px-2" |
| 133 | style={{ backgroundColor: vscBackground }} |
| 134 | > |
| 135 | <p>Something went wrong:</p> |
| 136 | <pre style={{ color: "red" }}>{error.message}</pre> |
| 137 | |
| 138 | <div className="text-center"> |
| 139 | <Button onClick={resetErrorBoundary}>Restart</Button> |
| 140 | </div> |
| 141 | </div> |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | function GUI() { |
| 146 | const posthog = usePostHog(); |
| 147 | const dispatch = useDispatch(); |
| 148 | const ideMessenger = useContext(IdeMessengerContext); |
| 149 | |
| 150 | const onboardingCard = useOnboardingCard(); |
| 151 | const { showTutorialCard, closeTutorialCard } = useTutorialCard(); |
| 152 | |
| 153 | const sessionState = useSelector((state: RootState) => state.state); |
| 154 | |
| 155 | const defaultModel = useSelector(defaultModelSelector); |
| 156 | |
| 157 | const ttsActive = useSelector((state: RootState) => state.state.ttsActive); |
| 158 | const active = useSelector((state: RootState) => state.state.active); |
| 159 | |
| 160 | const [stepsOpen, setStepsOpen] = useState<(boolean | undefined)[]>([]); |
| 161 | |
| 162 | const mainTextInputRef = useRef<HTMLInputElement>(null); |
| 163 | const topGuiDivRef = useRef<HTMLDivElement>(null); |
| 164 | |
| 165 | const [isAtBottom, setIsAtBottom] = useState<boolean>(false); |
| 166 | |
| 167 | const state = useSelector((state: RootState) => state.state); |
| 168 | |
| 169 | const handleScroll = () => { |
| 170 | // Temporary fix to account for additional height when code blocks are added |
| 171 | const OFFSET_HERUISTIC = 300; |
| 172 | if (!topGuiDivRef.current) return; |
| 173 | |
| 174 | const { scrollTop, scrollHeight, clientHeight } = topGuiDivRef.current; |
| 175 | const atBottom = |
| 176 | scrollHeight - clientHeight <= scrollTop + OFFSET_HERUISTIC; |
| 177 | |
| 178 | setIsAtBottom(atBottom); |
| 179 | }; |
| 180 | |
| 181 | useEffect(() => { |
| 182 | if (!active || !topGuiDivRef.current) return; |
| 183 | |
| 184 | const scrollAreaElement = topGuiDivRef.current; |
| 185 | |
| 186 | scrollAreaElement.scrollTop = |
| 187 | scrollAreaElement.scrollHeight - scrollAreaElement.clientHeight; |
| 188 | |
| 189 | setIsAtBottom(true); |
| 190 | }, [active]); |
| 191 | |
| 192 | useEffect(() => { |
| 193 | // Cmd + Backspace to delete current step |
| 194 | const listener = (e: any) => { |
| 195 | if ( |
| 196 | e.key === "Backspace" && |
| 197 | isMetaEquivalentKeyPressed(e) && |
| 198 | !e.shiftKey |
| 199 | ) { |
| 200 | dispatch(setInactive()); |
| 201 | } |
| 202 | }; |
| 203 | window.addEventListener("keydown", listener); |
| 204 | |
| 205 | return () => { |
| 206 | window.removeEventListener("keydown", listener); |
| 207 | }; |
| 208 | }, [active]); |
| 209 | |
| 210 | // #endregion |
| 211 | |
| 212 | const { streamResponse } = useChatHandler(dispatch, ideMessenger); |
| 213 | |
| 214 | const sendInput = useCallback( |
| 215 | (editorState: JSONContent, modifiers: InputModifiers) => { |
| 216 | if (defaultModel?.provider === "free-trial") { |
| 217 | const u = getLocalStorage("ftc"); |
| 218 | if (u) { |
| 219 | setLocalStorage("ftc", u + 1); |
| 220 | |
| 221 | if (u >= FREE_TRIAL_LIMIT_REQUESTS) { |
| 222 | onboardingCard.open("Best"); |
| 223 | posthog?.capture("ftc_reached"); |
| 224 | ideMessenger.ide.showToast( |
| 225 | "info", |
| 226 | "You've reached the free trial limit. Please configure a model to continue.", |
| 227 | ); |
| 228 | return; |
| 229 | } |
| 230 | } else { |
| 231 | setLocalStorage("ftc", 1); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | streamResponse(editorState, modifiers, ideMessenger); |
| 236 | |
| 237 | // Increment localstorage counter for popup |
| 238 | const currentCount = getLocalStorage("mainTextEntryCounter"); |
| 239 | if (currentCount) { |
| 240 | setLocalStorage("mainTextEntryCounter", currentCount + 1); |
| 241 | if (currentCount === 300) { |
| 242 | dispatch( |
| 243 | setDialogMessage( |
| 244 | <div className="text-center p-4"> |
| 245 | 👋 Thanks for using Continue. We are always trying to improve |
| 246 | and love hearing from users. If you're interested in speaking, |
| 247 | enter your name and email. We won't use this information for |
| 248 | anything other than reaching out. |
| 249 | <br /> |
| 250 | <br /> |
| 251 | <form |
| 252 | onSubmit={(e: any) => { |
| 253 | e.preventDefault(); |
| 254 | posthog?.capture("user_interest_form", { |
| 255 | name: e.target.elements[0].value, |
| 256 | email: e.target.elements[1].value, |
| 257 | }); |
| 258 | dispatch( |
| 259 | setDialogMessage( |
| 260 | <div className="text-center p-4"> |
| 261 | Thanks! We'll be in touch soon. |
| 262 | </div>, |
| 263 | ), |
| 264 | ); |
| 265 | }} |
| 266 | style={{ |
| 267 | display: "flex", |
| 268 | flexDirection: "column", |
| 269 | gap: "10px", |
| 270 | }} |
| 271 | > |
| 272 | <input |
| 273 | style={{ padding: "10px", borderRadius: "5px" }} |
| 274 | type="text" |
| 275 | name="name" |
| 276 | placeholder="Name" |
| 277 | required |
| 278 | /> |
| 279 | <input |
| 280 | style={{ padding: "10px", borderRadius: "5px" }} |
| 281 | type="email" |
| 282 | name="email" |
| 283 | placeholder="Email" |
| 284 | required |
| 285 | /> |
| 286 | <button |
| 287 | style={{ |
| 288 | padding: "10px", |
| 289 | borderRadius: "5px", |
| 290 | cursor: "pointer", |
| 291 | }} |
| 292 | type="submit" |
| 293 | > |
| 294 | Submit |
| 295 | </button> |
| 296 | </form> |
| 297 | </div>, |
| 298 | ), |
| 299 | ); |
| 300 | dispatch(setDialogEntryOn(false)); |
| 301 | dispatch(setShowDialog(true)); |
| 302 | } |
| 303 | } else { |
| 304 | setLocalStorage("mainTextEntryCounter", 1); |
| 305 | } |
| 306 | }, |
| 307 | [ |
| 308 | sessionState.history, |
| 309 | sessionState.contextItems, |
| 310 | defaultModel, |
| 311 | state, |
| 312 | streamResponse, |
| 313 | ], |
| 314 | ); |
| 315 | |
| 316 | const { saveSession, getLastSessionId, loadLastSession } = |
| 317 | useHistory(dispatch); |
| 318 | |
| 319 | useWebviewListener( |
| 320 | "newSession", |
| 321 | async () => { |
| 322 | saveSession(); |
| 323 | mainTextInputRef.current?.focus?.(); |
| 324 | }, |
| 325 | [saveSession], |
| 326 | ); |
| 327 | |
| 328 | const isLastUserInput = useCallback( |
| 329 | (index: number): boolean => { |
| 330 | let foundLaterUserInput = false; |
| 331 | for (let i = index + 1; i < state.history.length; i++) { |
| 332 | if (state.history[i].message.role === "user") { |
| 333 | foundLaterUserInput = true; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | return !foundLaterUserInput; |
| 338 | }, |
| 339 | [state.history], |
| 340 | ); |
| 341 | |
| 342 | return ( |
| 343 | <> |
| 344 | <TopGuiDiv |
| 345 | ref={topGuiDivRef} |
| 346 | onScroll={handleScroll} |
| 347 | showScrollbar={state.config.ui?.showChatScrollbar || false} |
| 348 | > |
| 349 | <div className="max-w-3xl m-auto"> |
| 350 | <StepsDiv> |
| 351 | {state.history.map((item, index: number) => { |
| 352 | return ( |
| 353 | <Fragment key={index}> |
| 354 | <ErrorBoundary |
| 355 | FallbackComponent={fallbackRender} |
| 356 | onReset={() => { |
| 357 | dispatch(newSession()); |
| 358 | }} |
| 359 | > |
| 360 | {item.message.role === "user" ? ( |
| 361 | <ContinueInputBox |
| 362 | onEnter={async (editorState, modifiers) => { |
| 363 | streamResponse( |
| 364 | editorState, |
| 365 | modifiers, |
| 366 | ideMessenger, |
| 367 | index, |
| 368 | ); |
| 369 | }} |
| 370 | isLastUserInput={isLastUserInput(index)} |
| 371 | isMainInput={false} |
| 372 | editorState={item.editorState} |
| 373 | contextItems={item.contextItems} |
| 374 | ></ContinueInputBox> |
| 375 | ) : ( |
| 376 | <div className="thread-message"> |
| 377 | <TimelineItem |
| 378 | item={item} |
| 379 | iconElement={ |
| 380 | false ? ( |
| 381 | <CodeBracketSquareIcon |
| 382 | width="16px" |
| 383 | height="16px" |
| 384 | /> |
| 385 | ) : false ? ( |
| 386 | <ExclamationTriangleIcon |
| 387 | width="16px" |
| 388 | height="16px" |
| 389 | color="red" |
| 390 | /> |
| 391 | ) : ( |
| 392 | <ChatBubbleOvalLeftIcon |
| 393 | width="16px" |
| 394 | height="16px" |
| 395 | /> |
| 396 | ) |
| 397 | } |
| 398 | open={ |
| 399 | typeof stepsOpen[index] === "undefined" |
| 400 | ? false |
| 401 | ? false |
| 402 | : true |
| 403 | : stepsOpen[index]! |
| 404 | } |
| 405 | onToggle={() => {}} |
| 406 | > |
| 407 | <StepContainer |
| 408 | index={index} |
| 409 | isLast={index === sessionState.history.length - 1} |
| 410 | isFirst={index === 0} |
| 411 | open={ |
| 412 | typeof stepsOpen[index] === "undefined" |
| 413 | ? true |
| 414 | : stepsOpen[index]! |
| 415 | } |
| 416 | key={index} |
| 417 | onUserInput={(input: string) => {}} |
| 418 | item={item} |
| 419 | onReverse={() => {}} |
| 420 | onRetry={() => { |
| 421 | streamResponse( |
| 422 | state.history[index - 1].editorState, |
| 423 | state.history[index - 1].modifiers ?? |
| 424 | defaultInputModifiers, |
| 425 | ideMessenger, |
| 426 | index - 1, |
| 427 | ); |
| 428 | }} |
| 429 | onContinueGeneration={() => { |
| 430 | window.postMessage( |
| 431 | { |
| 432 | messageType: "userInput", |
| 433 | data: { |
| 434 | input: |
| 435 | "Continue your response exactly where you left off:", |
| 436 | }, |
| 437 | }, |
| 438 | "*", |
| 439 | ); |
| 440 | }} |
| 441 | onDelete={() => { |
| 442 | dispatch(deleteMessage(index)); |
| 443 | }} |
| 444 | modelTitle={item.promptLogs?.[0]?.modelTitle ?? ""} |
| 445 | /> |
| 446 | </TimelineItem> |
| 447 | </div> |
| 448 | )} |
| 449 | </ErrorBoundary> |
| 450 | </Fragment> |
| 451 | ); |
| 452 | })} |
| 453 | </StepsDiv> |
| 454 | <ContinueInputBox |
| 455 | onEnter={(editorContent, modifiers) => { |
| 456 | sendInput(editorContent, modifiers); |
| 457 | }} |
| 458 | isLastUserInput={false} |
| 459 | isMainInput={true} |
| 460 | hidden={active} |
| 461 | ></ContinueInputBox> |
| 462 | |
| 463 | {active ? ( |
| 464 | <> |
| 465 | <br /> |
| 466 | <br /> |
| 467 | </> |
| 468 | ) : state.history.length > 0 ? ( |
| 469 | <div className="mt-2"> |
| 470 | <NewSessionButton |
| 471 | onClick={() => { |
| 472 | saveSession(); |
| 473 | }} |
| 474 | className="mr-auto" |
| 475 | > |
| 476 | New Session ({getMetaKeyLabel()} {isJetBrains() ? "J" : "L"}) |
| 477 | </NewSessionButton>{" "} |
| 478 | </div> |
| 479 | ) : ( |
| 480 | <> |
| 481 | {getLastSessionId() ? ( |
| 482 | <div className="mt-2"> |
| 483 | <NewSessionButton |
| 484 | onClick={async () => { |
| 485 | loadLastSession().catch((e) => |
| 486 | console.error(`Failed to load last session: ${e}`), |
| 487 | ); |
| 488 | }} |
| 489 | className="mr-auto flex items-center gap-2" |
| 490 | > |
| 491 | <ArrowLeftIcon width="11px" height="11px" /> |
| 492 | Last Session |
| 493 | </NewSessionButton> |
| 494 | </div> |
| 495 | ) : null} |
| 496 | |
| 497 | {onboardingCard.show && ( |
| 498 | <div className="mt-10 mx-2"> |
| 499 | <OnboardingCard activeTab={onboardingCard.activeTab} /> |
| 500 | </div> |
| 501 | )} |
| 502 | |
| 503 | {showTutorialCard !== false && !onboardingCard.open && ( |
| 504 | <div className="flex justify-center w-full"> |
| 505 | <TutorialCard onClose={closeTutorialCard} /> |
| 506 | </div> |
| 507 | )} |
| 508 | </> |
| 509 | )} |
| 510 | </div> |
| 511 | |
| 512 | <ChatScrollAnchor |
| 513 | scrollAreaRef={topGuiDivRef} |
| 514 | isAtBottom={isAtBottom} |
| 515 | trackVisibility={active} |
| 516 | /> |
| 517 | </TopGuiDiv> |
| 518 | {ttsActive && ( |
| 519 | <StopButton |
| 520 | className="mt-2 mb-4" |
| 521 | onClick={() => { |
| 522 | ideMessenger.post("tts/kill", undefined); |
| 523 | }} |
| 524 | > |
| 525 | ■ Stop TTS |
| 526 | </StopButton> |
| 527 | )} |
| 528 | {active && ( |
| 529 | <StopButton |
| 530 | className="mt-auto mb-4" |
| 531 | onClick={() => { |
| 532 | dispatch(setInactive()); |
| 533 | if ( |
| 534 | state.history[state.history.length - 1]?.message.content |
| 535 | .length === 0 |
| 536 | ) { |
| 537 | dispatch(clearLastResponse()); |
| 538 | } |
| 539 | }} |
| 540 | > |
| 541 | {getMetaKeyLabel()} ⌫ Cancel |
| 542 | </StopButton> |
| 543 | )} |
| 544 | </> |
| 545 | ); |
| 546 | } |
| 547 | |
| 548 | export default GUI; |
| 549 | |
| 550 | --- |
| 551 | |
| 552 | import { |
| 553 | ArrowLeftIcon, |
| 554 | ChatBubbleOvalLeftIcon, |
| 555 | CodeBracketSquareIcon, |
| 556 | ExclamationTriangleIcon, |
| 557 | } from "@heroicons/react/24/outline"; |
| 558 | import { JSONContent } from "@tiptap/react"; |
| 559 | import { InputModifiers } from "core"; |
| 560 | import { usePostHog } from "posthog-js/react"; |
| 561 | import { |
| 562 | Fragment, |
| 563 | useCallback, |
| 564 | useContext, |
| 565 | useEffect, |
| 566 | useRef, |
| 567 | useState, |
| 568 | } from "react"; |
| 569 | // ... existing imports ... |
| 570 | |
| 571 | function GUI() { |
| 572 | const posthog = usePostHog(); |
| 573 | const dispatch = useDispatch(); |
| 574 | const ideMessenger = useContext(IdeMessengerContext); |
| 575 | |
| 576 | // ... existing code ... |
| 577 | |
| 578 | const [hello, setHello] = useState(false); |
| 579 | |
| 580 | // ... existing code ... |
| 581 | |
| 582 | return ( |
| 583 | <> |
| 584 | <TopGuiDiv |
| 585 | ref={topGuiDivRef} |
| 586 | onScroll={handleScroll} |
| 587 | showScrollbar={state.config.ui?.showChatScrollbar || false} |
| 588 | > |
| 589 | <div className="max-w-3xl m-auto"> |
| 590 | <div className="mb-4"> |
| 591 | <label className="flex items-center"> |
| 592 | <input |
| 593 | type="checkbox" |
| 594 | checked={hello} |
| 595 | onChange={() => setHello(!hello)} |
| 596 | className="mr-2" |
| 597 | /> |
| 598 | Toggle Hello |
| 599 | </label> |
| 600 | {hello && <span className="ml-2">world</span>} |
| 601 | </div> |
| 602 | |
| 603 | <StepsDiv> |
| 604 | {state.history.map((item, index: number) => { |
| 605 | // ... existing code ... |
| 606 | })} |
| 607 | </StepsDiv> |
| 608 | |
| 609 | // ... rest of the existing code ... |
| 610 | </div> |
| 611 | |
| 612 | // ... existing code ... |
| 613 | </TopGuiDiv> |
| 614 | // ... rest of the existing code ... |
| 615 | </> |
| 616 | ); |
| 617 | } |
| 618 | |
| 619 | export default GUI; |
| 620 | |
| 621 | |
| 622 | --- |
| 623 | |
| 624 | import { |
| 625 | ArrowLeftIcon, |
| 626 | ChatBubbleOvalLeftIcon, |
| 627 | CodeBracketSquareIcon, |
| 628 | ExclamationTriangleIcon, |
| 629 | } from "@heroicons/react/24/outline"; |
| 630 | import { JSONContent } from "@tiptap/react"; |
| 631 | import { InputModifiers } from "core"; |
| 632 | import { usePostHog } from "posthog-js/react"; |
| 633 | import { |
| 634 | Fragment, |
| 635 | useCallback, |
| 636 | useContext, |
| 637 | useEffect, |
| 638 | useRef, |
| 639 | useState, |
| 640 | } from "react"; |
| 641 | import { ErrorBoundary } from "react-error-boundary"; |
| 642 | import { useDispatch, useSelector } from "react-redux"; |
| 643 | import styled from "styled-components"; |
| 644 | import { |
| 645 | Button, |
| 646 | defaultBorderRadius, |
| 647 | lightGray, |
| 648 | vscBackground, |
| 649 | vscForeground, |
| 650 | } from "../components"; |
| 651 | import { ChatScrollAnchor } from "../components/ChatScrollAnchor"; |
| 652 | import StepContainer from "../components/gui/StepContainer"; |
| 653 | import TimelineItem from "../components/gui/TimelineItem"; |
| 654 | import ContinueInputBox from "../components/mainInput/ContinueInputBox"; |
| 655 | import { defaultInputModifiers } from "../components/mainInput/inputModifiers"; |
| 656 | import { TutorialCard } from "../components/mainInput/TutorialCard"; |
| 657 | import { |
| 658 | OnboardingCard, |
| 659 | useOnboardingCard, |
| 660 | } from "../components/OnboardingCard"; |
| 661 | import { IdeMessengerContext } from "../context/IdeMessenger"; |
| 662 | import useChatHandler from "../hooks/useChatHandler"; |
| 663 | import useHistory from "../hooks/useHistory"; |
| 664 | import { useTutorialCard } from "../hooks/useTutorialCard"; |
| 665 | import { useWebviewListener } from "../hooks/useWebviewListener"; |
| 666 | import { defaultModelSelector } from "../redux/selectors/modelSelectors"; |
| 667 | import { |
| 668 | clearLastResponse, |
| 669 | deleteMessage, |
| 670 | newSession, |
| 671 | setInactive, |
| 672 | } from "../redux/slices/stateSlice"; |
| 673 | import { |
| 674 | setDialogEntryOn, |
| 675 | setDialogMessage, |
| 676 | setShowDialog, |
| 677 | } from "../redux/slices/uiStateSlice"; |
| 678 | import { RootState } from "../redux/store"; |
| 679 | import { |
| 680 | getFontSize, |
| 681 | getMetaKeyLabel, |
| 682 | isJetBrains, |
| 683 | isMetaEquivalentKeyPressed, |
| 684 | } from "../util"; |
| 685 | import { FREE_TRIAL_LIMIT_REQUESTS } from "../util/freeTrial"; |
| 686 | import { getLocalStorage, setLocalStorage } from "../util/localStorage"; |
| 687 | |
| 688 | const TopGuiDiv = styled.div<{ |
| 689 | showScrollbar?: boolean; |
| 690 | }>` |
| 691 | overflow-y: auto; |
| 692 | height: 100%; |
| 693 | scrollbar-width: ${(props) => (props.showScrollbar ? "thin" : "none")}; |
| 694 | |
| 695 | &::-webkit-scrollbar { |
| 696 | display: ${(props) => (props.showScrollbar ? "block" : "none")}; |
| 697 | } |
| 698 | `; |
| 699 | |
| 700 | const StopButton = styled.div` |
| 701 | width: fit-content; |
| 702 | margin-right: auto; |
| 703 | margin-left: auto; |
| 704 | |
| 705 | font-size: ${getFontSize() - 2}px; |
| 706 | |
| 707 | border: 0.5px solid ${lightGray}; |
| 708 | border-radius: ${defaultBorderRadius}; |
| 709 | padding: 4px 8px; |
| 710 | color: ${lightGray}; |
| 711 | |
| 712 | cursor: pointer; |
| 713 | `; |
| 714 | |
| 715 | const StepsDiv = styled.div` |
| 716 | position: relative; |
| 717 | background-color: transparent; |
| 718 | |
| 719 | & > * { |
| 720 | position: relative; |
| 721 | } |
| 722 | |
| 723 | .thread-message { |
| 724 | margin: 8px 4px 0 4px; |
| 725 | padding-bottom: 8px; |
| 726 | } |
| 727 | `; |
| 728 | |
| 729 | const NewSessionButton = styled.div` |
| 730 | width: fit-content; |
| 731 | margin-right: auto; |
| 732 | margin-left: 6px; |
| 733 | margin-top: 2px; |
| 734 | margin-bottom: 8px; |
| 735 | font-size: ${getFontSize() - 2}px; |
| 736 | |
| 737 | border-radius: ${defaultBorderRadius}; |
| 738 | padding: 2px 6px; |
| 739 | color: ${lightGray}; |
| 740 | |
| 741 | &:hover { |
| 742 | background-color: ${lightGray}33; |
| 743 | color: ${vscForeground}; |
| 744 | } |
| 745 | |
| 746 | cursor: pointer; |
| 747 | `; |
| 748 | |
| 749 | function fallbackRender({ error, resetErrorBoundary }: any) { |
| 750 | // Call resetErrorBoundary() to reset the error boundary and retry the render. |
| 751 | |
| 752 | return ( |
| 753 | <div |
| 754 | role="alert" |
| 755 | className="px-2" |
| 756 | style={{ backgroundColor: vscBackground }} |
| 757 | > |
| 758 | <p>Something went wrong:</p> |
| 759 | <pre style={{ color: "red" }}>{error.message}</pre> |
| 760 | |
| 761 | <div className="text-center"> |
| 762 | <Button onClick={resetErrorBoundary}>Restart</Button> |
| 763 | </div> |
| 764 | </div> |
| 765 | ); |
| 766 | } |
| 767 | |
| 768 | function GUI() { |
| 769 | const posthog = usePostHog(); |
| 770 | const dispatch = useDispatch(); |
| 771 | const ideMessenger = useContext(IdeMessengerContext); |
| 772 | |
| 773 | + const [hello, setHello] = useState(false); |
| 774 | |
| 775 | const onboardingCard = useOnboardingCard(); |
| 776 | const { showTutorialCard, closeTutorialCard } = useTutorialCard(); |
| 777 | |
| 778 | const sessionState = useSelector((state: RootState) => state.state); |
| 779 | |
| 780 | const defaultModel = useSelector(defaultModelSelector); |
| 781 | |
| 782 | const ttsActive = useSelector((state: RootState) => state.state.ttsActive); |
| 783 | const active = useSelector((state: RootState) => state.state.active); |
| 784 | |
| 785 | const [stepsOpen, setStepsOpen] = useState<(boolean | undefined)[]>([]); |
| 786 | |
| 787 | const mainTextInputRef = useRef<HTMLInputElement>(null); |
| 788 | const topGuiDivRef = useRef<HTMLDivElement>(null); |
| 789 | |
| 790 | const [isAtBottom, setIsAtBottom] = useState<boolean>(false); |
| 791 | |
| 792 | const state = useSelector((state: RootState) => state.state); |
| 793 | |
| 794 | const handleScroll = () => { |
| 795 | // Temporary fix to account for additional height when code blocks are added |
| 796 | const OFFSET_HERUISTIC = 300; |
| 797 | if (!topGuiDivRef.current) return; |
| 798 | |
| 799 | const { scrollTop, scrollHeight, clientHeight } = topGuiDivRef.current; |
| 800 | const atBottom = |
| 801 | scrollHeight - clientHeight <= scrollTop + OFFSET_HERUISTIC; |
| 802 | |
| 803 | setIsAtBottom(atBottom); |
| 804 | }; |
| 805 | |
| 806 | useEffect(() => { |
| 807 | if (!active || !topGuiDivRef.current) return; |
| 808 | |
| 809 | const scrollAreaElement = topGuiDivRef.current; |
| 810 | |
| 811 | scrollAreaElement.scrollTop = |
| 812 | scrollAreaElement.scrollHeight - scrollAreaElement.clientHeight; |
| 813 | |
| 814 | setIsAtBottom(true); |
| 815 | }, [active]); |
| 816 | |
| 817 | useEffect(() => { |
| 818 | // Cmd + Backspace to delete current step |
| 819 | const listener = (e: any) => { |
| 820 | if ( |
| 821 | e.key === "Backspace" && |
| 822 | isMetaEquivalentKeyPressed(e) && |
| 823 | !e.shiftKey |
| 824 | ) { |
| 825 | dispatch(setInactive()); |
| 826 | } |
| 827 | }; |
| 828 | window.addEventListener("keydown", listener); |
| 829 | |
| 830 | return () => { |
| 831 | window.removeEventListener("keydown", listener); |
| 832 | }; |
| 833 | }, [active]); |
| 834 | |
| 835 | // #endregion |
| 836 | |
| 837 | const { streamResponse } = useChatHandler(dispatch, ideMessenger); |
| 838 | |
| 839 | const sendInput = useCallback( |
| 840 | (editorState: JSONContent, modifiers: InputModifiers) => { |
| 841 | if (defaultModel?.provider === "free-trial") { |
| 842 | const u = getLocalStorage("ftc"); |
| 843 | if (u) { |
| 844 | setLocalStorage("ftc", u + 1); |
| 845 | |
| 846 | if (u >= FREE_TRIAL_LIMIT_REQUESTS) { |
| 847 | onboardingCard.open("Best"); |
| 848 | posthog?.capture("ftc_reached"); |
| 849 | ideMessenger.ide.showToast( |
| 850 | "info", |
| 851 | "You've reached the free trial limit. Please configure a model to continue.", |
| 852 | ); |
| 853 | return; |
| 854 | } |
| 855 | } else { |
| 856 | setLocalStorage("ftc", 1); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | streamResponse(editorState, modifiers, ideMessenger); |
| 861 | |
| 862 | // Increment localstorage counter for popup |
| 863 | const currentCount = getLocalStorage("mainTextEntryCounter"); |
| 864 | if (currentCount) { |
| 865 | setLocalStorage("mainTextEntryCounter", currentCount + 1); |
| 866 | if (currentCount === 300) { |
| 867 | dispatch( |
| 868 | setDialogMessage( |
| 869 | <div className="text-center p-4"> |
| 870 | 👋 Thanks for using Continue. We are always trying to improve |
| 871 | and love hearing from users. If you're interested in speaking, |
| 872 | enter your name and email. We won't use this information for |
| 873 | anything other than reaching out. |
| 874 | <br /> |
| 875 | <br /> |
| 876 | <form |
| 877 | onSubmit={(e: any) => { |
| 878 | e.preventDefault(); |
| 879 | posthog?.capture("user_interest_form", { |
| 880 | name: e.target.elements[0].value, |
| 881 | email: e.target.elements[1].value, |
| 882 | }); |
| 883 | dispatch( |
| 884 | setDialogMessage( |
| 885 | <div className="text-center p-4"> |
| 886 | Thanks! We'll be in touch soon. |
| 887 | </div>, |
| 888 | ), |
| 889 | ); |
| 890 | }} |
| 891 | style={{ |
| 892 | display: "flex", |
| 893 | flexDirection: "column", |
| 894 | gap: "10px", |
| 895 | }} |
| 896 | > |
| 897 | <input |
| 898 | style={{ padding: "10px", borderRadius: "5px" }} |
| 899 | type="text" |
| 900 | name="name" |
| 901 | placeholder="Name" |
| 902 | required |
| 903 | /> |
| 904 | <input |
| 905 | style={{ padding: "10px", borderRadius: "5px" }} |
| 906 | type="email" |
| 907 | name="email" |
| 908 | placeholder="Email" |
| 909 | required |
| 910 | /> |
| 911 | <button |
| 912 | style={{ |
| 913 | padding: "10px", |
| 914 | borderRadius: "5px", |
| 915 | cursor: "pointer", |
| 916 | }} |
| 917 | type="submit" |
| 918 | > |
| 919 | Submit |
| 920 | </button> |
| 921 | </form> |
| 922 | </div>, |
| 923 | ), |
| 924 | ); |
| 925 | dispatch(setDialogEntryOn(false)); |
| 926 | dispatch(setShowDialog(true)); |
| 927 | } |
| 928 | } else { |
| 929 | setLocalStorage("mainTextEntryCounter", 1); |
| 930 | } |
| 931 | }, |
| 932 | [ |
| 933 | sessionState.history, |
| 934 | sessionState.contextItems, |
| 935 | defaultModel, |
| 936 | state, |
| 937 | streamResponse, |
| 938 | ], |
| 939 | ); |
| 940 | |
| 941 | const { saveSession, getLastSessionId, loadLastSession } = |
| 942 | useHistory(dispatch); |
| 943 | |
| 944 | useWebviewListener( |
| 945 | "newSession", |
| 946 | async () => { |
| 947 | saveSession(); |
| 948 | mainTextInputRef.current?.focus?.(); |
| 949 | }, |
| 950 | [saveSession], |
| 951 | ); |
| 952 | |
| 953 | const isLastUserInput = useCallback( |
| 954 | (index: number): boolean => { |
| 955 | let foundLaterUserInput = false; |
| 956 | for (let i = index + 1; i < state.history.length; i++) { |
| 957 | if (state.history[i].message.role === "user") { |
| 958 | foundLaterUserInput = true; |
| 959 | break; |
| 960 | } |
| 961 | } |
| 962 | return !foundLaterUserInput; |
| 963 | }, |
| 964 | [state.history], |
| 965 | ); |
| 966 | |
| 967 | return ( |
| 968 | <> |
| 969 | <TopGuiDiv |
| 970 | ref={topGuiDivRef} |
| 971 | onScroll={handleScroll} |
| 972 | showScrollbar={state.config.ui?.showChatScrollbar || false} |
| 973 | > |
| 974 | <div className="max-w-3xl m-auto"> |
| 975 | + <div className="mb-4"> |
| 976 | + <label className="flex items-center"> |
| 977 | + <input |
| 978 | + type="checkbox" |
| 979 | + checked={hello} |
| 980 | + onChange={() => setHello(!hello)} |
| 981 | + className="mr-2" |
| 982 | + /> |
| 983 | + Toggle Hello |
| 984 | + </label> |
| 985 | + {hello && <span className="ml-2">world</span>} |
| 986 | + </div> |
| 987 | |
| 988 | <StepsDiv> |
| 989 | {state.history.map((item, index: number) => { |
| 990 | return ( |
| 991 | <Fragment key={index}> |
| 992 | <ErrorBoundary |
| 993 | FallbackComponent={fallbackRender} |
| 994 | onReset={() => { |
| 995 | dispatch(newSession()); |
| 996 | }} |
| 997 | > |
| 998 | {item.message.role === "user" ? ( |
| 999 | <ContinueInputBox |
| 1000 | onEnter={async (editorState, modifiers) => { |
| 1001 | streamResponse( |
| 1002 | editorState, |
| 1003 | modifiers, |
| 1004 | ideMessenger, |
| 1005 | index, |
| 1006 | ); |
| 1007 | }} |
| 1008 | isLastUserInput={isLastUserInput(index)} |
| 1009 | isMainInput={false} |
| 1010 | editorState={item.editorState} |
| 1011 | contextItems={item.contextItems} |
| 1012 | ></ContinueInputBox> |
| 1013 | ) : ( |
| 1014 | <div className="thread-message"> |
| 1015 | <TimelineItem |
| 1016 | item={item} |
| 1017 | iconElement={ |
| 1018 | false ? ( |
| 1019 | <CodeBracketSquareIcon |
| 1020 | width="16px" |
| 1021 | height="16px" |
| 1022 | /> |
| 1023 | ) : false ? ( |
| 1024 | <ExclamationTriangleIcon |
| 1025 | width="16px" |
| 1026 | height="16px" |
| 1027 | color="red" |
| 1028 | /> |
| 1029 | ) : ( |
| 1030 | <ChatBubbleOvalLeftIcon |
| 1031 | width="16px" |
| 1032 | height="16px" |
| 1033 | /> |
| 1034 | ) |
| 1035 | } |
| 1036 | open={ |
| 1037 | typeof stepsOpen[index] === "undefined" |
| 1038 | ? false |
| 1039 | ? false |
| 1040 | : true |
| 1041 | : stepsOpen[index]! |
| 1042 | } |
| 1043 | onToggle={() => {}} |
| 1044 | > |
| 1045 | <StepContainer |
| 1046 | index={index} |
| 1047 | isLast={index === sessionState.history.length - 1} |
| 1048 | isFirst={index === 0} |
| 1049 | open={ |
| 1050 | typeof stepsOpen[index] === "undefined" |
| 1051 | ? true |
| 1052 | : stepsOpen[index]! |
| 1053 | } |
| 1054 | key={index} |
| 1055 | onUserInput={(input: string) => {}} |
| 1056 | item={item} |
| 1057 | onReverse={() => {}} |
| 1058 | onRetry={() => { |
| 1059 | streamResponse( |
| 1060 | state.history[index - 1].editorState, |
| 1061 | state.history[index - 1].modifiers ?? |
| 1062 | defaultInputModifiers, |
| 1063 | ideMessenger, |
| 1064 | index - 1, |
| 1065 | ); |
| 1066 | }} |
| 1067 | onContinueGeneration={() => { |
| 1068 | window.postMessage( |
| 1069 | { |
| 1070 | messageType: "userInput", |
| 1071 | data: { |
| 1072 | input: |
| 1073 | "Continue your response exactly where you left off:", |
| 1074 | }, |
| 1075 | }, |
| 1076 | "*", |
| 1077 | ); |
| 1078 | }} |
| 1079 | onDelete={() => { |
| 1080 | dispatch(deleteMessage(index)); |
| 1081 | }} |
| 1082 | modelTitle={item.promptLogs?.[0]?.modelTitle ?? ""} |
| 1083 | /> |
| 1084 | </TimelineItem> |
| 1085 | </div> |
| 1086 | )} |
| 1087 | </ErrorBoundary> |
| 1088 | </Fragment> |
| 1089 | ); |
| 1090 | })} |
| 1091 | </StepsDiv> |
| 1092 | <ContinueInputBox |
| 1093 | onEnter={(editorContent, modifiers) => { |
| 1094 | sendInput(editorContent, modifiers); |
| 1095 | }} |
| 1096 | isLastUserInput={false} |
| 1097 | isMainInput={true} |
| 1098 | hidden={active} |
| 1099 | ></ContinueInputBox> |
| 1100 | |
| 1101 | {active ? ( |
| 1102 | <> |
| 1103 | <br /> |
| 1104 | <br /> |
| 1105 | </> |
| 1106 | ) : state.history.length > 0 ? ( |
| 1107 | <div className="mt-2"> |
| 1108 | <NewSessionButton |
| 1109 | onClick={() => { |
| 1110 | saveSession(); |
| 1111 | }} |
| 1112 | className="mr-auto" |
| 1113 | > |
| 1114 | New Session ({getMetaKeyLabel()} {isJetBrains() ? "J" : "L"}) |
| 1115 | </NewSessionButton>{" "} |
| 1116 | </div> |
| 1117 | ) : ( |
| 1118 | <> |
| 1119 | {getLastSessionId() ? ( |
| 1120 | <div className="mt-2"> |
| 1121 | <NewSessionButton |
| 1122 | onClick={async () => { |
| 1123 | loadLastSession().catch((e) => |
| 1124 | console.error(`Failed to load last session: ${e}`), |
| 1125 | ); |
| 1126 | }} |
| 1127 | className="mr-auto flex items-center gap-2" |
| 1128 | > |
| 1129 | <ArrowLeftIcon width="11px" height="11px" /> |
| 1130 | Last Session |
| 1131 | </NewSessionButton> |
| 1132 | </div> |
| 1133 | ) : null} |
| 1134 | |
| 1135 | {onboardingCard.show && ( |
| 1136 | <div className="mt-10 mx-2"> |
| 1137 | <OnboardingCard activeTab={onboardingCard.activeTab} /> |
| 1138 | </div> |
| 1139 | )} |
| 1140 | |
| 1141 | {showTutorialCard !== false && !onboardingCard.open && ( |
| 1142 | <div className="flex justify-center w-full"> |
| 1143 | <TutorialCard onClose={closeTutorialCard} /> |
| 1144 | </div> |
| 1145 | )} |
| 1146 | </> |
| 1147 | )} |
| 1148 | </div> |
| 1149 | |
| 1150 | <ChatScrollAnchor |
| 1151 | scrollAreaRef={topGuiDivRef} |
| 1152 | isAtBottom={isAtBottom} |
| 1153 | trackVisibility={active} |
| 1154 | /> |
| 1155 | </TopGuiDiv> |
| 1156 | {ttsActive && ( |
| 1157 | <StopButton |
| 1158 | className="mt-2 mb-4" |
| 1159 | onClick={() => { |
| 1160 | ideMessenger.post("tts/kill", undefined); |
| 1161 | }} |
| 1162 | > |
| 1163 | ■ Stop TTS |
| 1164 | </StopButton> |
| 1165 | )} |
| 1166 | {active && ( |
| 1167 | <StopButton |
| 1168 | className="mt-auto mb-4" |
| 1169 | onClick={() => { |
| 1170 | dispatch(setInactive()); |
| 1171 | if ( |
| 1172 | state.history[state.history.length - 1]?.message.content |
| 1173 | .length === 0 |
| 1174 | ) { |
| 1175 | dispatch(clearLastResponse()); |
| 1176 | } |
| 1177 | }} |
| 1178 | > |
| 1179 | {getMetaKeyLabel()} ⌫ Cancel |
| 1180 | </StopButton> |
| 1181 | )} |
| 1182 | </> |
| 1183 | ); |
| 1184 | } |
| 1185 | |
| 1186 | export default GUI; |