Skip to content

Commit 73219d6

Browse files
committed
feat: improve chat record list display
1 parent 693ac3f commit 73219d6

File tree

5 files changed

+101
-40
lines changed

5 files changed

+101
-40
lines changed

backend/apps/chat/curd/chat.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import datetime
2-
import sqlparse
32
from typing import List
43

54
import orjson
6-
from sqlalchemy import and_
7-
from sqlalchemy.orm import load_only
5+
import sqlparse
6+
from sqlalchemy import and_, select
87

98
from apps.chat.models.chat_model import Chat, ChatRecord, CreateChat, ChatInfo, RenameChat, ChatQuestion
109
from apps.datasource.models.datasource import CoreDatasource
@@ -47,26 +46,29 @@ def delete_chat(session, chart_id) -> str:
4746

4847

4948
def get_chat_chart_data(session: SessionDep, chart_record_id: int):
50-
res = session.query(ChatRecord).options(load_only(ChatRecord.data)).get(chart_record_id)
51-
if res:
49+
stmt = select(ChatRecord.data).where(and_(ChatRecord.id == chart_record_id))
50+
res = session.execute(stmt)
51+
for row in res:
5252
try:
53-
return orjson.loads(res.data)
53+
return orjson.loads(row.data)
5454
except Exception:
5555
pass
5656
return {}
5757

5858

5959
def get_chat_predict_data(session: SessionDep, chart_record_id: int):
60-
res = session.query(ChatRecord).options(load_only(ChatRecord.predict_data)).get(chart_record_id)
61-
if res:
60+
stmt = select(ChatRecord.predict_data).where(and_(ChatRecord.id == chart_record_id))
61+
res = session.execute(stmt)
62+
for row in res:
6263
try:
63-
return orjson.loads(res.predict_data)
64+
return orjson.loads(row.predict_data)
6465
except Exception:
6566
pass
66-
return ''
67+
return {}
6768

6869

69-
def get_chat_with_records(session: SessionDep, chart_id: int, current_user: CurrentUser, current_assistant: CurrentAssistant) -> ChatInfo:
70+
def get_chat_with_records(session: SessionDep, chart_id: int, current_user: CurrentUser,
71+
current_assistant: CurrentAssistant) -> ChatInfo:
7072
chat = session.get(Chat, chart_id)
7173
if not chat:
7274
raise Exception(f"Chat with id {chart_id} not found")
@@ -78,7 +80,7 @@ def get_chat_with_records(session: SessionDep, chart_id: int, current_user: Curr
7880
ds = out_ds_instance.get_ds(chat.datasource)
7981
else:
8082
ds = session.get(CoreDatasource, chat.datasource) if chat.datasource else None
81-
83+
8284
if not ds:
8385
chat_info.datasource_exists = False
8486
chat_info.datasource_name = 'Datasource not exist'
@@ -87,14 +89,25 @@ def get_chat_with_records(session: SessionDep, chart_id: int, current_user: Curr
8789
chat_info.datasource_name = ds.name
8890
chat_info.ds_type = ds.type
8991

90-
record_list = session.query(ChatRecord).options(
91-
load_only(ChatRecord.id, ChatRecord.chat_id, ChatRecord.create_time, ChatRecord.finish_time,
92-
ChatRecord.question, ChatRecord.sql_answer, ChatRecord.sql, ChatRecord.data,
92+
stmt = select(ChatRecord.id, ChatRecord.chat_id, ChatRecord.create_time, ChatRecord.finish_time,
93+
ChatRecord.question, ChatRecord.sql_answer, ChatRecord.sql,
9394
ChatRecord.chart_answer, ChatRecord.chart, ChatRecord.analysis, ChatRecord.predict,
9495
ChatRecord.datasource_select_answer, ChatRecord.analysis_record_id, ChatRecord.predict_record_id,
9596
ChatRecord.recommended_question, ChatRecord.first_chat,
96-
ChatRecord.predict_data, ChatRecord.finish, ChatRecord.error)).filter(
97-
and_(Chat.create_by == current_user.id, ChatRecord.chat_id == chart_id)).order_by(ChatRecord.create_time).all()
97+
ChatRecord.finish, ChatRecord.error).where(
98+
and_(ChatRecord.create_by == current_user.id, ChatRecord.chat_id == chart_id)).order_by(ChatRecord.create_time)
99+
result = session.execute(stmt).all()
100+
record_list: list[ChatRecord] = []
101+
for row in result:
102+
record_list.append(
103+
ChatRecord(id=row.id, chat_id=row.chat_id, create_time=row.create_time, finish_time=row.finish_time,
104+
question=row.question, sql_answer=row.sql_answer, sql=row.sql,
105+
chart_answer=row.chart_answer, chart=row.chart,
106+
analysis=row.analysis, predict=row.predict,
107+
datasource_select_answer=row.datasource_select_answer,
108+
analysis_record_id=row.analysis_record_id, predict_record_id=row.predict_record_id,
109+
recommended_question=row.recommended_question, first_chat=row.first_chat,
110+
finish=row.finish, error=row.error))
98111

99112
result = list(map(format_record, record_list))
100113

frontend/src/views/chat/answer/ChartAnswer.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<script setup lang="ts">
22
import BaseAnswer from './BaseAnswer.vue'
33
import { Chat, chatApi, ChatInfo, type ChatMessage, ChatRecord, questionApi } from '@/api/chat.ts'
4-
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
4+
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
5+
import ChartBlock from '@/views/chat/chat-block/ChartBlock.vue'
56
const props = withDefaults(
67
defineProps<{
78
chatList?: Array<ChatInfo>
@@ -234,11 +235,18 @@ onBeforeUnmount(() => {
234235
stop()
235236
})
236237
238+
onMounted(() => {
239+
if (props.message?.record?.id && props.message?.record?.finish) {
240+
getChatData(props.message.record.id)
241+
}
242+
})
243+
237244
defineExpose({ sendMessage, index: () => index.value, stop })
238245
</script>
239246

240247
<template>
241248
<BaseAnswer v-if="message" :message="message" :reasoning-name="reasoningName" :loading="_loading">
249+
<ChartBlock style="margin-top: 12px" :message="message" />
242250
<slot></slot>
243251
<template #tool>
244252
<slot name="tool"></slot>

frontend/src/views/chat/answer/PredictAnswer.vue

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script setup lang="ts">
22
import BaseAnswer from './BaseAnswer.vue'
33
import { chatApi, ChatInfo, type ChatMessage, ChatRecord } from '@/api/chat.ts'
4-
import { computed, nextTick, onBeforeUnmount, ref } from 'vue'
4+
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
55
import MdComponent from '@/views/chat/component/MdComponent.vue'
6+
import ChartBlock from '@/views/chat/chat-block/ChartBlock.vue'
67
const props = withDefaults(
78
defineProps<{
89
chatList?: Array<ChatInfo>
@@ -174,6 +175,7 @@ const sendMessage = async () => {
174175
break
175176
case 'predict-success':
176177
//currentChat.value.records[_index].predict_data = data.content
178+
getChatPredictData(_currentChat.value.records[index.value].id)
177179
emits('finish', currentRecord.id)
178180
break
179181
case 'predict_finish':
@@ -198,6 +200,33 @@ const sendMessage = async () => {
198200
}
199201
}
200202
203+
const chartBlockRef = ref()
204+
205+
function getChatPredictData(recordId?: number) {
206+
chatApi.get_chart_predict_data(recordId).then((response) => {
207+
_currentChat.value.records.forEach((record) => {
208+
if (record.id === recordId) {
209+
record.predict_data = response ?? []
210+
211+
if (record.predict_data.length > 1) {
212+
getChatData(recordId)
213+
}
214+
}
215+
})
216+
})
217+
}
218+
219+
function getChatData(recordId?: number) {
220+
chatApi.get_chart_data(recordId).then((response) => {
221+
_currentChat.value.records.forEach((record) => {
222+
if (record.id === recordId) {
223+
record.data = response
224+
console.log(record.data)
225+
}
226+
})
227+
})
228+
}
229+
201230
function stop() {
202231
stopFlag.value = true
203232
_loading.value = false
@@ -208,12 +237,25 @@ onBeforeUnmount(() => {
208237
stop()
209238
})
210239
240+
onMounted(() => {
241+
if (props.message?.record?.id && props.message?.record?.finish) {
242+
getChatPredictData(props.message.record.id)
243+
}
244+
})
245+
211246
defineExpose({ sendMessage, index: () => index.value, chatList: () => _chatList, stop })
212247
</script>
213248

214249
<template>
215250
<BaseAnswer v-if="message" :message="message" :reasoning-name="['predict']" :loading="_loading">
216251
<MdComponent :message="message.record?.predict_content" style="margin-top: 12px" />
252+
<ChartBlock
253+
v-if="message.record?.predict_data?.length > 0 && message.record?.data"
254+
ref="chartBlockRef"
255+
style="margin-top: 12px"
256+
:message="message"
257+
is-predict
258+
/>
217259
<slot></slot>
218260
<template #tool>
219261
<slot name="tool"></slot>

frontend/src/views/chat/chat-block/ChartBlock.vue

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,17 @@ const data = computed(() => {
8181
}
8282
}
8383
} else {
84-
if (props.message?.record?.predict_data.length > 0) {
84+
if (props.message?.record?.predict_data?.length > 0) {
8585
_list = props.message?.record?.predict_data
8686
}
8787
}
88-
8988
if (_list.length == 0) {
9089
return _list
9190
}
9291
93-
if (dataObject.value.data && dataObject.value.data.length > 0) {
92+
if (dataObject.value.data && dataObject.value.data?.length > 0) {
9493
return concat(dataObject.value.data, _list)
9594
}
96-
9795
return _list
9896
} else {
9997
return dataObject.value.data
@@ -180,6 +178,10 @@ function onTypeChange(val: any) {
180178
chartRef.value?.onTypeChange()
181179
}
182180
181+
function reloadChart() {
182+
chartRef.value?.onTypeChange()
183+
}
184+
183185
const dialogVisible = ref(false)
184186
185187
function openFullScreen() {
@@ -274,6 +276,10 @@ function exportToImage() {
274276
exportRef.value?.hide()
275277
}
276278
279+
defineExpose({
280+
reloadChart,
281+
})
282+
277283
watch(
278284
() => chartObject.value?.type,
279285
(val) => {

frontend/src/views/chat/index.vue

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@
160160
@error="onChartAnswerError"
161161
@stop="onChatStop"
162162
>
163-
<ChartBlock style="margin-top: 12px" :message="message" />
164163
<ErrorInfo :error="message.record?.error" class="error-container" />
165164
<template #tool>
166165
<ChatToolBar v-if="!message.isTyping" :message="message">
@@ -265,7 +264,6 @@
265264
@error="onPredictAnswerError"
266265
@stop="onChatStop"
267266
>
268-
<ChartBlock style="margin-top: 12px" :message="message" is-predict />
269267
<ErrorInfo :error="message.record?.error" class="error-container" />
270268
<template #tool>
271269
<ChatToolBar v-if="!message.isTyping" :message="message" />
@@ -337,15 +335,14 @@ import ChartAnswer from './answer/ChartAnswer.vue'
337335
import AnalysisAnswer from './answer/AnalysisAnswer.vue'
338336
import PredictAnswer from './answer/PredictAnswer.vue'
339337
import UserChat from './chat-block/UserChat.vue'
340-
import ChartBlock from './chat-block/ChartBlock.vue'
341338
import RecommendQuestion from './RecommendQuestion.vue'
342339
import ChatListContainer from './ChatListContainer.vue'
343340
import ChatCreator from '@/views/chat/ChatCreator.vue'
344341
import ErrorInfo from './ErrorInfo.vue'
345342
import ChatToolBar from './ChatToolBar.vue'
346343
import { dsTypeWithImg } from '@/views/ds/js/ds-type'
347344
import { useI18n } from 'vue-i18n'
348-
import { find } from 'lodash-es'
345+
import { find, forEach } from 'lodash-es'
349346
import icon_new_chat_outlined from '@/assets/svg/icon_new_chat_outlined.svg'
350347
import icon_sidebar_outlined from '@/assets/svg/icon_sidebar_outlined.svg'
351348
import icon_replace_outlined from '@/assets/svg/icon_replace_outlined.svg'
@@ -490,9 +487,14 @@ function getChatList(callback?: () => void) {
490487
})
491488
}
492489
493-
function onClickHistory(chat: Chat) {
490+
function onClickHistory(chat: ChatInfo) {
494491
scrollToBottom()
495-
console.debug('click history', chat)
492+
forEach(chat?.records, (record: ChatRecord) => {
493+
// getChatData(record.id)
494+
if (record.predict_record_id) {
495+
// getChatPredictData(record.id)
496+
}
497+
})
496498
}
497499
498500
const currentChatEngineType = computed(() => {
@@ -629,16 +631,6 @@ const sendMessage = async ($event: any = {}) => {
629631
})
630632
}
631633
632-
function getChatPredictData(recordId?: number) {
633-
chatApi.get_chart_predict_data(recordId).then((response) => {
634-
currentChat.value.records.forEach((record) => {
635-
if (record.id === recordId) {
636-
record.predict_data = response ?? []
637-
}
638-
})
639-
})
640-
}
641-
642634
const analysisAnswerRef = ref()
643635
644636
async function onAnalysisAnswerFinish(id: number) {
@@ -704,7 +696,7 @@ const predictAnswerRef = ref()
704696
async function onPredictAnswerFinish(id: number) {
705697
loading.value = false
706698
isTyping.value = false
707-
getChatPredictData(id)
699+
console.debug('onPredictAnswerFinish: ', id)
708700
//await getRecommendQuestions(id)
709701
}
710702
function onPredictAnswerError() {

0 commit comments

Comments
 (0)