-
Notifications
You must be signed in to change notification settings - Fork 671
Expand file tree
/
Copy pathChartAnswer.vue
More file actions
268 lines (241 loc) · 6.42 KB
/
ChartAnswer.vue
File metadata and controls
268 lines (241 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<script setup lang="ts">
import BaseAnswer from './BaseAnswer.vue'
import { Chat, chatApi, ChatInfo, type ChatMessage, ChatRecord, questionApi } from '@/api/chat.ts'
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import ChartBlock from '@/views/chat/chat-block/ChartBlock.vue'
const props = withDefaults(
defineProps<{
recordId?: number
chatList?: Array<ChatInfo>
currentChatId?: number
currentChat?: ChatInfo
message?: ChatMessage
loading?: boolean
reasoningName: 'sql_answer' | 'chart_answer' | Array<'sql_answer' | 'chart_answer'>
}>(),
{
recordId: undefined,
chatList: () => [],
currentChatId: undefined,
currentChat: () => new ChatInfo(),
message: undefined,
loading: false,
}
)
const emits = defineEmits([
'finish',
'error',
'stop',
'scrollBottom',
'update:loading',
'update:chatList',
'update:currentChat',
'update:currentChatId',
])
const index = computed(() => {
if (props.message?.index) {
return props.message.index
}
if (props.message?.index === 0) {
return 0
}
return -1
})
const _currentChatId = computed({
get() {
return props.currentChatId
},
set(v) {
emits('update:currentChatId', v)
},
})
const _currentChat = computed({
get() {
return props.currentChat
},
set(v) {
emits('update:currentChat', v)
},
})
const _chatList = computed({
get() {
return props.chatList
},
set(v) {
emits('update:chatList', v)
},
})
const _loading = computed({
get() {
return props.loading
},
set(v) {
emits('update:loading', v)
},
})
const stopFlag = ref(false)
const sendMessage = async () => {
stopFlag.value = false
_loading.value = true
if (index.value < 0) {
_loading.value = false
return
}
const currentRecord: ChatRecord = _currentChat.value.records[index.value]
let error: boolean = false
if (_currentChatId.value === undefined) {
error = true
}
if (error) return
try {
const controller: AbortController = new AbortController()
const param = {
question: currentRecord.question,
chat_id: _currentChatId.value,
}
const response = await questionApi.add(param, controller)
const reader = response.body.getReader()
const decoder = new TextDecoder('utf-8')
let sql_answer = ''
let chart_answer = ''
let tempResult = ''
while (true) {
if (stopFlag.value) {
controller.abort()
break
}
const { done, value } = await reader.read()
if (done) {
_loading.value = false
break
}
let chunk = decoder.decode(value, { stream: true })
tempResult += chunk
const split = tempResult.match(/data:.*}\n\n/g)
if (split) {
chunk = split.join('')
tempResult = tempResult.replace(chunk, '')
} else {
continue
}
if (chunk && chunk.startsWith('data:{')) {
if (split) {
for (const str of split) {
let data
try {
data = JSON.parse(str.replace('data:{', '{'))
} catch (err) {
console.error('JSON string:', str)
throw err
}
if (data.code && data.code !== 200) {
ElMessage({
message: data.msg,
type: 'error',
showClose: true,
})
_loading.value = false
return
}
switch (data.type) {
case 'id':
currentRecord.id = data.id
_currentChat.value.records[index.value].id = data.id
break
case 'info':
console.info(data.msg)
break
case 'brief':
_currentChat.value.brief = data.brief
_chatList.value.forEach((c: Chat) => {
if (c.id === _currentChat.value.id) {
c.brief = _currentChat.value.brief
}
})
break
case 'error':
currentRecord.error = data.content
emits('error')
break
case 'sql-result':
sql_answer += data.reasoning_content
_currentChat.value.records[index.value].sql_answer = sql_answer
break
case 'sql':
_currentChat.value.records[index.value].sql = data.content
break
case 'sql-data':
getChatData(_currentChat.value.records[index.value].id)
break
case 'chart-result':
chart_answer += data.reasoning_content
_currentChat.value.records[index.value].chart_answer = chart_answer
break
case 'chart':
_currentChat.value.records[index.value].chart = data.content
break
case 'finish':
emits('finish', currentRecord.id)
break
}
await nextTick()
}
}
}
}
} catch (error) {
if (!currentRecord.error) {
currentRecord.error = ''
}
if (currentRecord.error.trim().length !== 0) {
currentRecord.error = currentRecord.error + '\n'
}
currentRecord.error = currentRecord.error + 'Error:' + error
console.error('Error:', error)
emits('error')
} finally {
_loading.value = false
}
}
function getChatData(recordId?: number) {
chatApi
.get_chart_data(recordId)
.then((response) => {
_currentChat.value.records.forEach((record) => {
if (record.id === recordId) {
record.data = response
}
})
})
.finally(() => {
emits('scrollBottom')
})
}
function stop() {
stopFlag.value = true
_loading.value = false
emits('stop')
}
onBeforeUnmount(() => {
stop()
})
onMounted(() => {
if (props.message?.record?.id && props.message?.record?.finish) {
getChatData(props.message.record.id)
}
})
defineExpose({ sendMessage, index: () => index.value, stop })
</script>
<template>
<BaseAnswer v-if="message" :message="message" :reasoning-name="reasoningName" :loading="_loading">
<ChartBlock style="margin-top: 6px" :message="message" :record-id="recordId" />
<slot></slot>
<template #tool>
<slot name="tool"></slot>
</template>
<template #footer>
<slot name="footer"></slot>
</template>
</BaseAnswer>
</template>
<style scoped lang="less"></style>