aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/ViewManipulator.ts
blob: 67f183412700a098fbe491d82f7ed40e5fc864b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
  
  
  processGptResponse = (docView: DocumentView, textToDocMap: Map<string, Doc>, gptOutput: string, questionType: GPTDocCommand) =>
        undoable(() => {
            switch (questionType) { // reset collection based on question typefc
                case GPTDocCommand.Sort:
                    docView.Document[docView.ComponentView?.fieldKey + '_sort'] = docSortings.Chat;
                    break;
                case GPTDocCommand.Filter:
                    docView.ComponentView?.hasChildDocs?.().forEach(d => TagItem.removeTagFromDoc(d, GPTPopup.ChatTag));
                    break;
            } // prettier-ignore

            gptOutput.split(DescriptionSeperator).filter(item => item.trim() !== '')  // Split output into individual document contents
                .map(docContentRaw => docContentRaw.replace(/\n/g, ' ').trim())
                .map(docContentRaw => ({doc: textToDocMap.get(docContentRaw.split(DataSeperator)[0]), data: docContentRaw.split(DataSeperator)[1] })) // the find the corresponding Doc using textToDoc map
                .filter(({doc}) => doc).map(({doc, data}) => ({doc:doc!, data})) // filter out undefined values
                .forEach(({doc, data}, index) => {
                    switch (questionType) {
                        case GPTDocCommand.Sort:
                            doc[ChatSortField] = index;
                            break;
                        case GPTDocCommand.AssignTags:
                            data && TagItem.addTagToDoc(doc, data.startsWith('#') ? data : '#'+data[0].toLowerCase()+data.slice(1) );
                            break;
                        case GPTDocCommand.Filter:
                            TagItem.addTagToDoc(doc, GPTPopup.ChatTag);
                            Doc.setDocFilter(docView.Document, 'tags', GPTPopup.ChatTag, 'check');
                            break;
                    }
                }); // prettier-ignore
        }, '')();