diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/views/nodes/ChatBox/ChatBox.tsx | 159 |
1 files changed, 75 insertions, 84 deletions
diff --git a/src/client/views/nodes/ChatBox/ChatBox.tsx b/src/client/views/nodes/ChatBox/ChatBox.tsx index 4c1550321..3a2508c37 100644 --- a/src/client/views/nodes/ChatBox/ChatBox.tsx +++ b/src/client/views/nodes/ChatBox/ChatBox.tsx @@ -20,6 +20,7 @@ import { FieldView, FieldViewProps } from '../FieldView'; import './ChatBox.scss'; import MessageComponent from './MessageComponent'; import { ANNOTATION_LINK_TYPE, ASSISTANT_ROLE, AssistantMessage, DOWNLOAD_TYPE } from './types'; +import { Annotation } from 'mobx/dist/internal'; @observer export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { @@ -115,28 +116,6 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { } }; - createLink = (linkInfo: string, annotationText: string, linkType: ANNOTATION_LINK_TYPE) => { - console.log(this.current_message); - const text = this.current_message?.text; - console.log(text); - - const subString = annotationText; - if (!text) return; - const textToDisplay = `DASHLINK`; - let fileInfo = linkInfo; - const fileName = subString.split('/')[subString.split('/').length - 1]; - if (linkType === ANNOTATION_LINK_TYPE.DOWNLOAD_FILE) { - fileInfo = linkInfo + '!!!' + fileName; - } - - const formattedLink = `[${textToDisplay}](${fileInfo}~~~${linkType})`; - console.log(formattedLink); - const newText = text.split(subString).join(formattedLink); - runInAction(() => { - if (this.current_message) this.current_message.text = newText; - }); - }; - @action createAssistant = async () => { this.isInitializing = true; @@ -181,7 +160,6 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { } }; - @action runAssistant = async (inputText: string) => { // Ensure an assistant and thread are created if (!this.assistantID || !this.threadID || !this.vectorStoreID) { @@ -212,34 +190,32 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { }); }) .on('toolCallDelta', (toolCallDelta, snapshot) => { - runInAction(() => (this.isLoading = false)); - if (toolCallDelta.type === 'code_interpreter') { - if (toolCallDelta.code_interpreter?.input) { - currentToolCallMessage += toolCallDelta.code_interpreter.input; - runInAction(() => { + runInAction(() => { + if (toolCallDelta.type === 'code_interpreter') { + if (toolCallDelta.code_interpreter?.input) { + currentToolCallMessage += toolCallDelta.code_interpreter.input; + if (this.current_message) { this.current_message.tool_logs = currentToolCallMessage; } - }); - } - if (toolCallDelta.code_interpreter?.outputs) { - currentToolCallMessage += '\n Code interpreter output:'; - toolCallDelta.code_interpreter.outputs.forEach(output => { - if (output.type === 'logs') { - runInAction(() => { + } + + if (toolCallDelta.code_interpreter?.outputs) { + currentToolCallMessage += '\n Code interpreter output:'; + toolCallDelta.code_interpreter.outputs.forEach(output => { + if (output.type === 'logs') { if (this.current_message) { this.current_message.tool_logs += '\n|' + output.logs; } - }); - } - }); + } + }); + } } - } + }); }) .on('textDelta', (textDelta, snapshot) => { - this.isLoading = false; - currentText += textDelta.value; runInAction(() => { + currentText += textDelta.value; if (this.current_message) { this.current_message.text = currentText; } @@ -252,11 +228,9 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { const { text } = textItem; console.log(text.value); try { - runInAction(() => { - if (this.current_message) { - this.current_message.text = text.value; - } - }); + if (this.current_message) { + this.current_message.text = text.value; + } } catch (e) { console.error('Error parsing JSON response:', e); } @@ -264,47 +238,28 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { const { annotations } = text; console.log('Annotations: ' + annotations); - const uniqueAnnotations = annotations.filter( - (annotation => { - const seenAnnotationTexts = new Set<string>(); - return annotation => { - if (seenAnnotationTexts.has(annotation.text)) { - return false; - } else { - seenAnnotationTexts.add(annotation.text); - return true; - } - }; - })() - ); - uniqueAnnotations.forEach(async annotation => { - if (annotation.type === 'file_path') { - const { file_path: filePath } = annotation; - const fileToDownload = filePath.file_id; - console.log(fileToDownload); - if (filePath) { - console.log(filePath); - console.log(fileToDownload); - console.log(annotation.text); - this.createLink(fileToDownload, annotation.text, ANNOTATION_LINK_TYPE.DOWNLOAD_FILE); - } - } else { - const { file_citation: fileCitation } = annotation; - if (fileCitation) { - const citedFile = await this.openai.files.retrieve(fileCitation.file_id); - const citationUrl = citedFile.filename; - console.log(annotation.text); - console.log(this.current_message + 'fewjfjec'); - this.createLink(citationUrl, annotation.text, ANNOTATION_LINK_TYPE.DASH_DOC); - } - } - }); + await this.createLinks( + annotations.filter( + (annotation => { + const seenAnnotationTexts = new Set<string>(); + return annotation => { + if (seenAnnotationTexts.has(annotation.text)) { + return false; + } else { + seenAnnotationTexts.add(annotation.text); + return true; + } + }; + })() + ) + ); } runInAction(() => { - if (this.current_message?.text) { + if (this.current_message) { + console.log(this.current_message); this.history.push({ ...this.current_message }); - //this.current_message = undefined; + this.current_message = undefined; } }); }) @@ -312,7 +267,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { runInAction(() => { if (this.current_message?.tool_logs) { this.history.push({ ...this.current_message }); - //this.current_message = undefined; + this.current_message = undefined; } }); }) @@ -321,6 +276,42 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { }); }; + createLinks = async (annotations: OpenAI.Beta.Threads.Messages.Annotation[]) => { + console.log(this.current_message); + let text = this.current_message?.text; + console.log(text); + await Promise.all( + annotations.map(async annotation => { + const subString = annotation.text; + const textToDisplay = `DASHLINK`; + let fileInfo = ''; + let formattedLink = ''; + const fileName = subString.split('/')[subString.split('/').length - 1]; + + if (annotation.type === 'file_path') { + const { file_path: filePath } = annotation; + if (filePath) { + fileInfo = filePath.file_id + '!!!' + fileName; + formattedLink = `[${textToDisplay}](${fileInfo}~~~${ANNOTATION_LINK_TYPE.DOWNLOAD_FILE})`; + } + } else { + const { file_citation: fileCitation } = annotation; + if (fileCitation) { + const citedFile = await this.openai.files.retrieve(fileCitation.file_id); + formattedLink = `[${textToDisplay}](${citedFile.filename}~~~${ANNOTATION_LINK_TYPE.DASH_DOC})`; + } + } + + console.log(formattedLink); + text = text?.split(subString).join(formattedLink); + console.log(text); + }) + ); + runInAction(() => { + if (this.current_message) this.current_message.text = text || ''; + }); + }; + @action goToLinkedDoc = async (link: string) => { const linkedDocs = LinkManager.Instance.getAllRelatedLinks(this.Document) |