import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { Button, EditableText, IconButton, Size, Type } from 'browndash-components'; import { action, makeObservable, observable } from 'mobx'; import { observer } from 'mobx-react'; import * as React from 'react'; import { CgClose } from 'react-icons/cg'; import ReactLoading from 'react-loading'; import { TypeAnimation } from 'react-type-animation'; import { Utils } from '../../../../Utils'; import { Doc } from '../../../../fields/Doc'; import { NumCast, StrCast } from '../../../../fields/Types'; import { Networking } from '../../../Network'; import { GPTCallType, gptAPICall, gptImageCall } from '../../../apis/gpt/GPT'; import { DocUtils, Docs } from '../../../documents/Documents'; import { ObservableReactComponent } from '../../ObservableReactComponent'; import { AnchorMenu } from '../AnchorMenu'; import './GPTPopup.scss'; export enum GPTPopupMode { SUMMARY, EDIT, IMAGE, DATA, SORT } interface GPTPopupProps {} @observer export class GPTPopup extends ObservableReactComponent { static Instance: GPTPopup; @observable private chatMode: boolean = false; @observable public visible: boolean = false; @action public setVisible = (vis: boolean) => { this.visible = vis; }; @observable public loading: boolean = false; @action public setLoading = (loading: boolean) => { this.loading = loading; }; @observable public text: string = ''; @action public setText = (text: string) => { this.text = text; }; @observable public selectedText: string = ''; @action public setSelectedText = (text: string) => { this.selectedText = text; }; @observable public dataJson: string = ''; public dataChatPrompt: string | null = null; @action public setDataJson = (text: string) => { if (text=="") this.dataChatPrompt = ""; this.dataJson = text; }; @observable public imgDesc: string = ''; @action public setImgDesc = (text: string) => { this.imgDesc = text; }; @observable public imgUrls: string[][] = []; @action public setImgUrls = (imgs: string[][]) => { this.imgUrls = imgs; }; @observable public mode: GPTPopupMode = GPTPopupMode.SUMMARY; @action public setMode = (mode: GPTPopupMode) => { this.mode = mode; }; @observable public highlightRange: number[] = []; @action callSummaryApi = () => {}; @action callEditApi = () => {}; @action replaceText = (replacement: string) => {}; @observable private done: boolean = false; @action public setDone = (done: boolean) => { console.log("HIIIIIIIII") this.done = done; this.chatMode = false; }; @observable private sortDone: boolean = false // this is so redundant but the og done variable was causing weird unknown problems and im just a girl @action public setSortDone = (done: boolean) => { console.log("HIIIIIIIII") this.sortDone = done; }; // change what can be a ref into a ref @observable private sidebarId: string = ''; @action public setSidebarId = (id: string) => { this.sidebarId = id; }; @observable private imgTargetDoc: Doc | undefined; @action public setImgTargetDoc = (anchor: Doc) => { this.imgTargetDoc = anchor; }; @observable private textAnchor: Doc | undefined; @action public setTextAnchor = (anchor: Doc) => { this.textAnchor = anchor; }; @observable public sortPrompt: string = ''; @action public setSortPrompt = ((e: React.ChangeEvent) => { this.sortPrompt = e.target.value; }); @observable public sortText: string = '' @action public setSortText = (t: string) => { this.sortText = t } @observable public sortDesc: string = '' @action public setSortDesc = (t:string) => { this.sortDesc = t } @observable onSortComplete?: (sortResult: string) => void; @observable cardsDoneLoading = false; @action setCardsDoneLoading(done: boolean) { console.log(done + "HI HIHI") this.cardsDoneLoading = done; } public addDoc: (doc: Doc | Doc[], sidebarKey?: string | undefined) => boolean = () => false; public addToCollection: ((doc: Doc | Doc[], annotationKey?: string | undefined) => boolean) | undefined; generateSort = async () => { this.setLoading(true); this.setSortDone(false); try { const res = await gptAPICall(this.sortDesc, GPTCallType.SORT); this.setSortText(res || 'Something went wrong :('); // Trigger the callback with the result if (this.onSortComplete) { this.onSortComplete(res || 'Something went wrong :('); console.log(res) } } catch (err) { console.error(err); this.setSortText('Something went wrong :('); } this.setLoading(false); this.setSortDone(true); } /** * Generates a Dalle image and uploads it to the server. */ generateImage = async () => { if (this.imgDesc === '') return; this.setImgUrls([]); this.setMode(GPTPopupMode.IMAGE); this.setVisible(true); this.setLoading(true); try { let image_urls = await gptImageCall(this.imgDesc); console.log('Image urls: ', image_urls); if (image_urls && image_urls[0]) { const [result] = await Networking.PostToServer('/uploadRemoteImage', { sources: [image_urls[0]] }); console.log('Upload result: ', result); const source = Utils.prepend(result.accessPaths.agnostic.client); console.log('Upload source: ', source); this.setImgUrls([[image_urls[0], source]]); } } catch (err) { console.error(err); } this.setLoading(false); }; generateSummary = async () => { GPTPopup.Instance.setVisible(true); GPTPopup.Instance.setMode(GPTPopupMode.SUMMARY); GPTPopup.Instance.setLoading(true); try { const res = await gptAPICall(this.selectedText, GPTCallType.SUMMARY); GPTPopup.Instance.setText(res || 'Something went wrong.'); } catch (err) { console.error(err); } GPTPopup.Instance.setLoading(false); } generateDataAnalysis = async () => { GPTPopup.Instance.setVisible(true); GPTPopup.Instance.setLoading(true); try { let res = await gptAPICall(this.dataJson, GPTCallType.DATA, this.dataChatPrompt); GPTPopup.Instance.setText(res || 'Something went wrong.'); } catch (err) { console.error(err); } GPTPopup.Instance.setLoading(false); } /** * Transfers the summarization text to a sidebar annotation text document. */ private transferToText = () => { const newDoc = Docs.Create.TextDocument(this.text.trim(), { _width: 200, _height: 50, _layout_fitWidth: true, _layout_autoHeight: true, }); this.addDoc(newDoc, this.sidebarId); const anchor = AnchorMenu.Instance?.GetAnchor(undefined, false); if (anchor) { DocUtils.MakeLink(newDoc, anchor, { link_relationship: 'GPT Summary', }); } }; /** * Transfers the image urls to actual image docs */ private transferToImage = (source: string) => { const textAnchor = this.imgTargetDoc; if (!textAnchor) return; const newDoc = Docs.Create.ImageDocument(source, { x: NumCast(textAnchor.x) + NumCast(textAnchor._width) + 10, y: NumCast(textAnchor.y), _height: 200, _width: 200, data_nativeWidth: 1024, data_nativeHeight: 1024, }); if (Doc.IsInMyOverlay(textAnchor)) { newDoc.overlayX = textAnchor.x; newDoc.overlayY = NumCast(textAnchor.y) + NumCast(textAnchor._height); Doc.AddToMyOverlay(newDoc); } else { this.addToCollection?.(newDoc); } // Create link between prompt and image DocUtils.MakeLink(textAnchor, newDoc, { link_relationship: 'Image Prompt' }); }; /** * Creates a chatbox for analyzing data so that users can ask specific questions. */ private chatWithAI = () => { this.chatMode = true; } dataPromptChanged = action((e: React.ChangeEvent) => { this.dataChatPrompt = e.target.value; }); private getPreviewUrl = (source: string) => source.split('.').join('_m.'); constructor(props: GPTPopupProps) { super(props); makeObservable(this); GPTPopup.Instance = this; } componentDidUpdate = () => { if (this.loading) { this.setDone(false); } }; sortBox = () => ( <>
{this.heading("SORTING")} {this.loading ? (
Loading...
) : ( <> {!this.cardsDoneLoading ? (
Reading Cards...
) : ( !this.sortDone && (
) )} {this.sortDone && (

{this.text === "Something went wrong :(" ? "Something went wrong :(" : "Sorting done! Feel free to move things around / regenerate :) !"}

this.setSortDone(false)} icon={} color={StrCast(Doc.UserDoc().userVariantColor)} />
)} )}
); // <> //