2020 <el-main class =" chat-record-list" >
2121 <el-scrollbar >
2222 <template v-for =" (message , _index ) in computedMessages " :key =" _index " >
23- <ChatRow :current-chat =" currentChat" v-model:datasource =" currentChat.datasource" :msg =" message" />
23+ <ChatRow :current-chat =" currentChat" v-model:datasource =" currentChat.datasource" :msg =" message" >
24+ <template v-if =" message .role === ' assistant' " >
25+ <div v-if =" message.isTyping" >Thinking ...</div >
26+ <template v-if =" message .record " >
27+ <div >
28+ <div v-if =" message.record.sql_answer" >{{ message.record.sql_answer }}</div >
29+ <div v-if =" message.record.sql" >{{ message.record.sql }}</div >
30+ </div >
31+ <div >
32+ <div v-if =" message.record.data" >{{ message.record.data }}</div >
33+ </div >
34+ <div >
35+ <div v-if =" message.record.chart_answer" >{{ message.record.chart_answer }}</div >
36+ <div v-if =" message.record.chart" >{{ message.record.chart }}</div >
37+ </div >
38+ <div v-if =" message.record.error" style =" color : red " >{{ message.record.error }}</div >
39+ </template >
40+ </template >
41+ </ChatRow >
2442 </template >
2543 </el-scrollbar >
2644 </el-main >
2745 <el-footer class =" chat-footer" >
2846 <div style =" height : 24px ;" >
29- <template v-if =" currentChat .datasource " >
47+ <template v-if =" currentChat .datasource && currentChat . datasource_name " >
3048 使用数据源:{{ currentChat.datasource_name }}
3149 </template >
3250 </div >
5573</template >
5674
5775<script setup lang="ts">
58- import {ref , computed , nextTick , watch , onMounted } from ' vue'
76+ import {computed , nextTick , onMounted , ref } from ' vue'
5977import {Plus , Position } from ' @element-plus/icons-vue'
6078import {Chat , chatApi , ChatInfo , type ChatMessage , ChatRecord , questionApi } from ' @/api/chat'
6179import ChatList from ' ./ChatList.vue'
@@ -82,7 +100,6 @@ const computedMessages = computed<Array<ChatMessage>>(() => {
82100 if (currentChatId .value === undefined ) {
83101 return messages
84102 }
85- let appendThinking = false
86103 for (let i = 0 ; i < currentChat .value .records .length ; i ++ ) {
87104 const record = currentChat .value .records [i ]
88105 if (record .question !== undefined ) {
@@ -92,24 +109,16 @@ const computedMessages = computed<Array<ChatMessage>>(() => {
92109 content: record .question ,
93110 })
94111 }
95- if (record .answer !== undefined && record .answer !== ' ' ) {
96- messages .push ({
97- role: ' assistant' ,
98- create_time: record .create_time ,
99- content: record .answer ,
100- isTyping: i === currentChat .value .records .length - 1 && isTyping .value
101- })
102- } else {
103- appendThinking = true
104- }
105- }
106- if (isTyping .value && appendThinking ) {
107112 messages .push ({
108113 role: ' assistant' ,
109- content: ' Thinking...' ,
110- isTyping: true
114+ create_time: record .create_time ,
115+ record: record ,
116+ isTyping: i === currentChat .value .records .length - 1 && isTyping .value
111117 })
112118 }
119+
120+ console .log (messages )
121+
113122 return messages
114123})
115124
@@ -138,6 +147,7 @@ function onClickHistory(chat: Chat) {
138147 chatApi .get (chat .id )
139148 .then ((res ) => {
140149 const info = chatApi .toChatInfo (res )
150+ console .log (info )
141151 if (info ) {
142152 currentChat .value = info
143153 }
@@ -174,12 +184,6 @@ onMounted(() => {
174184})
175185
176186
177- const updateMessageContent = (content : string ) => {
178- if (currentChat .value ) {
179- currentChat .value .records [currentChat .value .records .length - 1 ].answer = content
180- }
181- }
182-
183187const sendMessage = async () => {
184188 if (! inputMessage .value .trim ()) return
185189 if (computedMessages .value [0 ].content === undefined ) return
@@ -191,14 +195,17 @@ const sendMessage = async () => {
191195 currentRecord .create_time = new Date ()
192196 currentRecord .chat_id = currentChatId .value
193197 currentRecord .question = inputMessage .value
194- currentRecord .answer = ' '
198+ currentRecord .sql_answer = ' '
199+ currentRecord .sql = ' '
200+ currentRecord .chart_answer = ' '
201+ currentRecord .chart = ' '
195202
196203 currentChat .value .records .push (currentRecord )
197204 inputMessage .value = ' '
198205
199206 let error = false
200207 if (currentChatId .value === undefined ) {
201- chatApi .startChat ({question: currentRecord .question .trim (), datasource: currentChat .value .datasource })
208+ await chatApi .startChat ({question: currentRecord .question .trim (), datasource: currentChat .value .datasource })
202209 .then ((res ) => {
203210 const chat = chatApi .toChatInfo (res )
204211 if (chat !== undefined ) {
@@ -228,50 +235,60 @@ const sendMessage = async () => {
228235
229236 while (true ) {
230237 const {done, value} = await reader .read ()
238+ console .log (done )
231239 if (done ) {
232240 isTyping .value = false
233241 break
234242 }
235243
236244 const chunk = decoder .decode (value )
237- const lines = chunk .split (' \n\n ' )
238-
239- for (const line of lines ) {
240- if (line .startsWith (' data: ' )) {
241- const data = JSON .parse (line .replace (' data:' , ' ' ).trim ())
242- let realContent = data .content
243-
244- switch (data .type ) {
245- case ' html' :
246- realContent = ' \n ' + data .content
247- break
248-
249- case ' error' :
250- realContent = ' \n Error: ' + data .content
251- break
252-
253- default :
254- if (data .content ) {
255- const newContent = currentRecord .answer + realContent
256-
257- updateMessageContent (newContent )
258- await nextTick ()
259- }
260- }
245+ console .log (chunk )
246+ const data = JSON .parse (chunk )
247+
248+ await nextTick (() => {
249+ switch (data .type ) {
250+ case ' info' :
251+ console .log (data .msg )
252+ break
253+ case ' error' :
254+ currentRecord .error = data .content
255+ isTyping .value = false
256+ break
257+ case ' sql-result' :
258+ currentRecord .sql_answer = currentRecord .sql_answer + data .content
259+ break
260+ case ' sql' :
261+ currentRecord .sql = data .content
262+ break
263+ case ' sql-data' :
264+ currentRecord .data = data .content
265+ break
266+ case ' chart-result' :
267+ currentRecord .chart_answer = currentRecord .chart_answer + data .content
268+ break
269+ case ' chart' :
270+ currentRecord .chart = data .content
271+ break
272+ case ' finish' :
273+ isTyping .value = false
274+ break
261275 }
262- }
276+ })
277+
263278 }
264279 } catch (error ) {
265- updateMessageContent (currentRecord .answer + ' \n Error: Failed to get response' )
280+ if (! currentRecord .error ) {
281+ currentRecord .error = ' '
282+ }
283+ if (currentRecord .error .trim ().length !== 0 ) {
284+ currentRecord .error = currentRecord .error + ' \n '
285+ }
286+ currentRecord .error = currentRecord .error + ' Error:' + error
266287 console .error (' Error:' , error )
267288 isTyping .value = false
268289 }
269290}
270291
271- watch (() => currentChat .value ?.records [currentChat .value .records .length - 1 ]?.answer , () => {
272- // scrollToBottom()
273- }, {deep: true })
274-
275292// @ts-ignore
276293const formatMessage = (content : string ) => {
277294 if (! content ) return ' '
0 commit comments