diff --git a/backend/apps/chat/api/chat.py b/backend/apps/chat/api/chat.py index acb5608a..3517eef8 100644 --- a/backend/apps/chat/api/chat.py +++ b/backend/apps/chat/api/chat.py @@ -63,7 +63,7 @@ async def start_chat(session: SessionDep, current_user: CurrentUser, create_chat return create_chat(session, current_user, create_chat_obj) except Exception as e: raise HTTPException( - status_code=400, + status_code=500, detail=str(e) ) diff --git a/backend/apps/chat/curd/chat.py b/backend/apps/chat/curd/chat.py index 9f9be02e..f3c69256 100644 --- a/backend/apps/chat/curd/chat.py +++ b/backend/apps/chat/curd/chat.py @@ -79,7 +79,7 @@ def create_chat(session: SessionDep, current_user: CurrentUser, create_chat_obj: raise Exception("Datasource cannot be None") if not create_chat_obj.question or create_chat_obj.question.strip() == '': - raise Exception("Question cannot be Empty") + create_chat_obj.question = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") chat = Chat(create_time=datetime.datetime.now(), create_by=current_user.id, diff --git a/backend/apps/chat/models/chat_model.py b/backend/apps/chat/models/chat_model.py index e6c51fca..43d3bea3 100644 --- a/backend/apps/chat/models/chat_model.py +++ b/backend/apps/chat/models/chat_model.py @@ -52,7 +52,7 @@ class ChatRecord(SQLModel, table=True): class CreateChat(BaseModel): id: int = None - question: str = '' + question: str = None datasource: int = None diff --git a/frontend/src/views/chat/ChatCreator.vue b/frontend/src/views/chat/ChatCreator.vue new file mode 100644 index 00000000..1430a78f --- /dev/null +++ b/frontend/src/views/chat/ChatCreator.vue @@ -0,0 +1,153 @@ + + + + + + + + Choose Datasource + + + + + + + + + + + + + + + + + diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index 7f6f0878..d2621287 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -24,7 +24,20 @@ - + + + SQLBot + {{ t('qa.greeting') }} + {{ t('qa.description') }} + + + + + {{ t('qa.New Conversation') }} + + + + - + {{ t('ds.title') }}:{{ currentChat.datasource_name }} @@ -115,6 +128,8 @@ + + @@ -127,6 +142,7 @@ import ChatRow from './ChatRow.vue' import ChatAnswer from './ChatAnswer.vue' import MdComponent from './component/MdComponent.vue' import PredictChartBlock from './component/PredictChartBlock.vue' +import ChatCreator from './ChatCreator.vue' import { useI18n } from 'vue-i18n' import { find } from 'lodash-es' @@ -135,6 +151,7 @@ const { t } = useI18n() const inputMessage = ref('') const chatListRef = ref() +const chatCreatorRef = ref() function scrollToBottom() { nextTick(() => { @@ -155,17 +172,18 @@ const isAnalysisTyping = ref(false) const isPredictTyping = ref(false) const computedMessages = computed>(() => { - const welcome: ChatMessage = { + const firstMessage: ChatMessage = { role: 'assistant', create_time: currentChat.value?.create_time, content: currentChat.value?.datasource, isTyping: false, isWelcome: true, } - const messages: Array = [welcome] + const messages: Array = [] if (currentChatId.value === undefined) { return messages } + messages.push(firstMessage) for (let i = 0; i < currentChat.value.records.length; i++) { const record = currentChat.value.records[i] if (record.question !== undefined) { @@ -186,12 +204,17 @@ const computedMessages = computed>(() => { return messages }) -const createNewChat = () => { +const goEmpty = () => { currentChat.value = new ChatInfo() currentChatId.value = undefined inputMessage.value = '' } +const createNewChat = () => { + goEmpty() + chatCreatorRef.value?.showDs() +} + function getChatList() { loading.value = true chatApi @@ -229,9 +252,12 @@ function onChatDeleted(id: number) { for (let i = 0; i < chatList.value.length; i++) { if (chatList.value[i].id === id) { chatList.value.splice(i, 1) - return + break } } + if (id === currentChatId.value) { + goEmpty() + } } function onChatRenamed(chat: Chat) { @@ -245,6 +271,12 @@ function onChatRenamed(chat: Chat) { } } +function onChatCreated(chat: ChatInfo) { + chatList.value.unshift(chat) + currentChatId.value = chat.id + currentChat.value = chat +} + onMounted(() => { getChatList() }) @@ -268,32 +300,9 @@ const sendMessage = async () => { currentChat.value.records.push(currentRecord) inputMessage.value = '' - let error = false + let error: boolean = false if (currentChatId.value === undefined) { - await chatApi - .startChat({ - question: currentRecord.question.trim(), - datasource: currentChat.value.datasource, - }) - .then((res) => { - const chat = chatApi.toChatInfo(res) - if (chat !== undefined) { - chatList.value.unshift(chat) - currentChatId.value = chat.id - chat.records.push(currentRecord) - currentChat.value = chat - } else { - error = true - } - }) - .catch((e) => { - isTyping.value = false - error = true - console.error(e) - }) - .finally(() => { - loading.value = false - }) + error = true } if (error) return @@ -683,4 +692,36 @@ const handleCtrlEnter = (e: KeyboardEvent) => { line-height: 1.7692307692; padding: 16px 22px; } + +.welcome-content-block { + height: 100%; + width: 100%; + + display: flex; + justify-content: center; + align-items: center; + + .welcome-content { + padding: 12px; + + width: fit-content; + display: flex; + gap: 16px; + align-items: center; + flex-direction: column; + + .logo { + line-height: 60px; + font-size: 3em; + font-weight: bold; + color: var(--el-color-primary); + text-align: left; + } + + .sub { + color: grey; + font-size: 0.8em; + } + } +}