From 825463b4f7bb609082d16b302d40c6af56c98b26 Mon Sep 17 00:00:00 2001
From: aidahosa1 <aisosa_idahosa@brown.edu>
Date: Fri, 17 May 2024 02:31:22 -0400
Subject: we did it joe

---
 .../views/collections/CollectionCardDeckView.tsx   | 188 ++++++++++++++-------
 1 file changed, 125 insertions(+), 63 deletions(-)

(limited to 'src/client/views/collections/CollectionCardDeckView.tsx')

diff --git a/src/client/views/collections/CollectionCardDeckView.tsx b/src/client/views/collections/CollectionCardDeckView.tsx
index e7e2cba4a..e64c013f1 100644
--- a/src/client/views/collections/CollectionCardDeckView.tsx
+++ b/src/client/views/collections/CollectionCardDeckView.tsx
@@ -24,7 +24,10 @@ import { ImageField, PdfField, URLField } from '../../../fields/URLField';
 
 @observer
 export class CollectionCardView extends CollectionSubView() {
-    @observable selectedNodeIndex = -1;
+
+    //TODO: for saving custom groups instead of string field hashmap nonsense should instead just set teh index for each child document per group as a fie;d
+    // so like, group1Ibndex = 1 etc etc as they change. That way, if the documents change it doesnt matter since the idnexes are tied to the 
+    // documents themselves
 
     @observable hoveredNodeIndex = -1;
 
@@ -223,40 +226,34 @@ export class CollectionCardView extends CollectionSubView() {
         // return 0;
     };
 
+    @observable sortedDocs = [] as { layout: Doc; data: Doc }[];
+
+
     @computed get sortedDocsType() {
+        if (this._props.Document.cardSort === 'chat' && this.sortedDocs.length === 0) {
+            this.smartSort(); // Trigger the sorting if it hasn't been done yet
+            return { docs: [] }; // Return an empty array or a loading state
+        }
+    
         const desc = BoolCast(this.layoutDoc.sortDesc);
         let sorted = [];
         let docs = [];
-
+    
         for (let i = 0; i < this.myChildLayoutPairs.length; i++) {
             //copying everything in childlayout pairs to sorted so that i can use the sort function without altering the original list
             sorted[i] = this.myChildLayoutPairs[i];
         }
-
+    
         switch (this._props.Document.cardSort) {
             case 'type':
                 // Copy and sort documents by type
                 return this.sort(sorted, 'type', desc);
             case 'color':
                 return this.sort(sorted, 'color', desc);
-            // case "links":
-            //     console.log("hi")
-
-            //    let links = LinkManager.Instance.getAllRelatedLinks(this.myChildLayoutPairs[0].layout)
-
-            //    console.log(links)
-
-            // }
-
             case 'custom':
-                // console.log('hiiiii');
                 return this.sort(sorted, 'custom', desc);
-
             case 'chat':
-                this.getImageDesc()
-                docs = this.myChildLayoutPairs;
-                return {docs}
-
+                return { docs: this.sortedDocs }; // Use the sorted docs from the observable
             default:
                 docs = this.myChildLayoutPairs;
                 return { docs };
@@ -314,7 +311,21 @@ export class CollectionCardView extends CollectionSubView() {
                     // console.log(typeA + "A")
                     typeB = this.customGroupDictionary[NumCast(this._props.Document.customSortNumber)].get(docs.indexOf(docB)) ?? '9999';
                     // console.log(typeB + 'b')
-                    break;                    
+                    break;   
+                    
+                case 'gpt':
+
+                // console.log(this.customGroupDictionary[NumCast(this._props.Document.customSortNumber)])
+                    typeA = this.gptGroups.get(docA.layout) ?? '9999';
+                    console.log(typeA + "typea")
+                    typeB = this.gptGroups.get(docB.layout) ?? '9999';
+                    console.log(typeB + "typeB")
+
+
+                    // console.log(typeA + "A")
+                    // typeB = this.customGroupDictionary[NumCast(this._props.Document.customSortNumber)].get(docs.indexOf(docB)) ?? '9999';
+                    // console.log(typeB + 'b')
+                    break;  
 
                 default:
                     typeA = docA.layout.type ?? ''; // If docA.type is undefined, use an empty string
@@ -488,6 +499,8 @@ export class CollectionCardView extends CollectionSubView() {
 
         // Cast(selected.Document.data, WebField, null)?.url?.href)
 
+        //note richtext
+
         // StrCast(selected.Document.data, Cast(selected.Document.data, WebField, null)?.url?.href)
 
 
@@ -506,23 +519,42 @@ export class CollectionCardView extends CollectionSubView() {
 
 
         // const pdf = (StrCast(this.myChildLayoutPairs[0].layout.text)); //pdf
-        // const queryText = (RTFCast(this.myChildLayoutPairs[0].layout.text)); //everything else
+        const queryText = this.myChildLayoutPairs[0].layout.type; //everything else
 
         // const queryText = RTFCast(DocCast(this.dataDoc[this.fieldKey + '_1']).text)?.Text;
 
-        console.log(perchance ?? "sad")
+        console.log(queryText ?? "sad")
     }
 
-    childPairStringList () {
-        let string = ""
-        for (let i=0; i < this.myChildLayoutPairs.length; i++){
-
-            
-            let text = StrCast((RTFCast(this.myChildLayoutPairs[i].layout.text)).Text)
-            string += StrCast((RTFCast(this.myChildLayoutPairs[i].layout.text)).Text) + ", "
-            this.textToDoc.has()
+    async childPairStringList(): Promise<string> {
+        let string = "";
+        for (let i = 0; i < this.childDocs.length; i++) {
+            switch (this.childDocs[i].type) {
+                case DocumentType.IMG:
+                    string += `======${await this.getImageDesc(this.childDocs[i])}======`;
+                    break;
+    
+                case DocumentType.PDF:
+                    let pdfText = StrCast(this.childDocs[i].text);
+                    let words = pdfText.split(/\s+/);
+                    let first50Words = words.slice(0, 50);
+                    string += `======${first50Words.join(' ')}======`;
+    
+                    this.textToDoc.set(first50Words.join(' ').trim(), this.childDocs[i]);
+                    break;
+    
+                case DocumentType.RTF:
+                    let rtfText = StrCast((RTFCast(this.childDocs[i].text)).Text);
+                    string += `======${StrCast((RTFCast(this.childDocs[i].text)).Text)}======`;
+                    this.textToDoc.set(rtfText.trim(), this.childDocs[i]);
+                    break;
+    
+                default:
+                    string += `======${StrCast(this.childDocs[i].title)}======`;
+                    this.textToDoc.set(StrCast(this.childDocs[i].title).trim(), this.childDocs[i]);
+            }
         }
-        return string
+        return string;
     }
 
     @observable isLoading = false
@@ -547,21 +579,20 @@ export class CollectionCardView extends CollectionSubView() {
     textToDoc = new Map<string, Doc>()
     gptProccessedImages = new Set<Doc>()
 
-    @action async getImageDesc(){
-        const images = this.childDocs.filter(d => d.type == DocumentType.IMG).filter(d=> !this.gptProccessedImages.has(d));
-        images.map(doc => {
-            let href = (doc['data'] as URLField).url.href;
+    @action async getImageDesc(image: Doc){
+            let href = (image['data'] as URLField).url.href;
             let hrefParts = href.split('.');
             let hrefComplete = `${hrefParts[0]}_o.${hrefParts[1]}`;
-            return this.imageUrlToBase64(hrefComplete).then(hrefBase64 => {
-                return gptImageLabel(hrefBase64).then(response => {
-                    this.textToDoc.set(response, doc)
-                    this.gptProccessedImages.add(doc)
-                    console.log(response);
-                    
-                });
-            });
-        });
+            try {
+                let hrefBase64 = await this.imageUrlToBase64(hrefComplete);
+                let response = await gptImageLabel(hrefBase64);
+                this.textToDoc.set(response.trim(), image);
+                this.gptProccessedImages.add(image);
+                console.log(response);
+                return response; // Return the response from gptImageLabel
+            } catch (error) {
+                console.log("bad things have happened");
+            }
     }
 
    
@@ -572,29 +603,60 @@ export class CollectionCardView extends CollectionSubView() {
 
     @action async smartSort() {
         this.isLoading = true;
-        console.log("loading")
-        let prompt= ""
-
-        if (this.childPairStringList() == ""){
-            console.log("no child pairs :(")
+        console.log("loading");
+        
+        // Store the result of childPairStringList in a variable
+        const childPairStrings = await this.childPairStringList();
+        
+        if (childPairStrings === "") {
+            console.log("no child pairs :(");
+        } else {
+            console.log(childPairStrings + " og list");
+            let prompt = childPairStrings;
+        
+            let res = await gptAPICall(prompt, GPTCallType.SORT);
+            this.isLoading = false;
+            if (res == 'Error connecting with API.') {
+                // If GPT call failed
+                console.error('GPT call failed');
+            } else if (res != null) {
+                console.log(res);
+                this.processGptOutput(res);
+                // Update the observable with the sorted documents
+                this.sortedDocs = this.sort(this.myChildLayoutPairs, 'gpt', BoolCast(this.layoutDoc.sortDesc)).docs;
+            }
+            this.isLoading = false;
         }
+    }
 
-        else{
-            console.log(this.childPairStringList() + "og list")
-            prompt =  "Sort this list of strings by shared content " + this.childPairStringList()
-        }
-        
+    gptGroups = new Map<Doc, number>
+
+     // Method to convert the GPT-produced string into a map
+    processGptOutput(gptOutput: string) {
+
+        // Split the string into individual list items
+        const listItems = gptOutput.split('======').filter(item => item.trim() !== '');
+
+        listItems.forEach((item, index) => {
+            // Split the item by '~~~~~~' to get all descriptors
+            const parts = item.split('~~~~~~').map(part => part.trim());
+
+            parts.forEach(part => {
+                console.log(part + "part")
+                // Find the corresponding Doc in the textToDoc map
+                if (this.textToDoc.has(part)) {
+                    const doc = this.textToDoc.get(part);
+                    console.log("hewoooooo")
+                    if (doc) {
+                        console.log('hi')
+                        this.gptGroups.set(doc, index);
+                    }
+                }
+            });
+        });
+    }
 
-        let res = await gptAPICall(prompt, GPTCallType.SORT);
-        this.isLoading = false;
-        if (res == 'Error connecting with API.') {
-            // If GPT call failed
-            console.error('GPT call failed');
-        } else if (res != null) {
-           console.log(res)
-        }
-        this.isLoading = false;
-    };
+    
 
     renderButtons(childPairIndex: number) {
         const buttons = []; // Array to hold the button elements
-- 
cgit v1.2.3-70-g09d2

