From 02d4f7f2da3bf3974fd54aa0b74045dd8e617dfd Mon Sep 17 00:00:00 2001 From: sarfaraz siddiqui Date: Wed, 3 Apr 2024 18:08:46 +0530 Subject: [PATCH] functions can accept null --- frontend/src/conversation/conversationApi.ts | 113 ++++++++++--------- 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/frontend/src/conversation/conversationApi.ts b/frontend/src/conversation/conversationApi.ts index e586366b..e3a82191 100644 --- a/frontend/src/conversation/conversationApi.ts +++ b/frontend/src/conversation/conversationApi.ts @@ -6,7 +6,7 @@ const apiHost = import.meta.env.VITE_API_HOST || 'https://docsapi.arc53.com'; export function fetchAnswerApi( question: string, signal: AbortSignal, - selectedDocs: Doc, + selectedDocs: Doc | null, history: Array = [], conversationId: string | null, promptId: string | null, @@ -28,24 +28,26 @@ export function fetchAnswerApi( title: any; } > { - let namePath = selectedDocs.name; - if (selectedDocs.language === namePath) { - namePath = '.project'; - } - let docPath = 'default'; - if (selectedDocs.location === 'local') { - docPath = 'local' + '/' + selectedDocs.name + '/'; - } else if (selectedDocs.location === 'remote') { - docPath = - selectedDocs.language + - '/' + - namePath + - '/' + - selectedDocs.version + - '/' + - selectedDocs.model + - '/'; + + if (selectedDocs) { + let namePath = selectedDocs.name; + if (selectedDocs.language === namePath) { + namePath = '.project'; + } + if (selectedDocs.location === 'local') { + docPath = 'local' + '/' + selectedDocs.name + '/'; + } else if (selectedDocs.location === 'remote') { + docPath = + selectedDocs.language + + '/' + + namePath + + '/' + + selectedDocs.version + + '/' + + selectedDocs.model + + '/'; + } } //in history array remove all keys except prompt and response history = history.map((item) => { @@ -89,31 +91,33 @@ export function fetchAnswerApi( export function fetchAnswerSteaming( question: string, signal: AbortSignal, - selectedDocs: Doc, + selectedDocs: Doc | null, history: Array = [], conversationId: string | null, promptId: string | null, chunks: string, onEvent: (event: MessageEvent) => void, ): Promise { - let namePath = selectedDocs.name; - if (selectedDocs.language === namePath) { - namePath = '.project'; - } - let docPath = 'default'; - if (selectedDocs.location === 'local') { - docPath = 'local' + '/' + selectedDocs.name + '/'; - } else if (selectedDocs.location === 'remote') { - docPath = - selectedDocs.language + - '/' + - namePath + - '/' + - selectedDocs.version + - '/' + - selectedDocs.model + - '/'; + + if (selectedDocs) { + let namePath = selectedDocs.name; + if (selectedDocs.language === namePath) { + namePath = '.project'; + } + if (selectedDocs.location === 'local') { + docPath = 'local' + '/' + selectedDocs.name + '/'; + } else if (selectedDocs.location === 'remote') { + docPath = + selectedDocs.language + + '/' + + namePath + + '/' + + selectedDocs.version + + '/' + + selectedDocs.model + + '/'; + } } history = history.map((item) => { @@ -186,7 +190,7 @@ export function fetchAnswerSteaming( } export function searchEndpoint( question: string, - selectedDocs: Doc, + selectedDocs: Doc | null, conversation_id: string | null, history: Array = [], chunks: string, @@ -196,24 +200,25 @@ export function searchEndpoint( "question": "Summarise", "conversation_id": null, "history": "[]" */ - let namePath = selectedDocs.name; - if (selectedDocs.language === namePath) { - namePath = '.project'; - } - let docPath = 'default'; - if (selectedDocs.location === 'local') { - docPath = 'local' + '/' + selectedDocs.name + '/'; - } else if (selectedDocs.location === 'remote') { - docPath = - selectedDocs.language + - '/' + - namePath + - '/' + - selectedDocs.version + - '/' + - selectedDocs.model + - '/'; + if (selectedDocs) { + let namePath = selectedDocs.name; + if (selectedDocs.language === namePath) { + namePath = '.project'; + } + if (selectedDocs.location === 'local') { + docPath = 'local' + '/' + selectedDocs.name + '/'; + } else if (selectedDocs.location === 'remote') { + docPath = + selectedDocs.language + + '/' + + namePath + + '/' + + selectedDocs.version + + '/' + + selectedDocs.model + + '/'; + } } const body = {