|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, nextTick, onBeforeUnmount, ref } from 'vue' |
| 3 | +import { endsWith, startsWith } from 'lodash-es' |
| 4 | +import { chatApi } from '@/api/chat.ts' |
| 5 | +
|
| 6 | +const props = withDefaults( |
| 7 | + defineProps<{ |
| 8 | + recordId?: number |
| 9 | + disabled?: boolean |
| 10 | + }>(), |
| 11 | + { |
| 12 | + recordId: undefined, |
| 13 | + disabled: false, |
| 14 | + } |
| 15 | +) |
| 16 | +
|
| 17 | +const emits = defineEmits(['clickQuestion', 'stop', 'loadingOver']) |
| 18 | +
|
| 19 | +const loading = ref(false) |
| 20 | +
|
| 21 | +const questions = ref('[]') |
| 22 | +
|
| 23 | +const computedQuestions = computed<string>(() => { |
| 24 | + if ( |
| 25 | + questions.value && |
| 26 | + questions.value.length > 0 && |
| 27 | + startsWith(questions.value.trim(), '[') && |
| 28 | + endsWith(questions.value.trim(), ']') |
| 29 | + ) { |
| 30 | + return JSON.parse(questions.value) |
| 31 | + } |
| 32 | + return [] |
| 33 | +}) |
| 34 | +
|
| 35 | +function clickQuestion(question: string): void { |
| 36 | + if (!props.disabled) { |
| 37 | + emits('clickQuestion', question) |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +const stopFlag = ref(false) |
| 42 | +
|
| 43 | +async function getRecommendQuestions(articles_number: number) { |
| 44 | + stopFlag.value = false |
| 45 | + loading.value = true |
| 46 | + try { |
| 47 | + const controller: AbortController = new AbortController() |
| 48 | + const params = articles_number ? '?articles_number=' + articles_number : '' |
| 49 | + const response = await chatApi.recommendQuestions(props.recordId, controller, params) |
| 50 | + const reader = response.body.getReader() |
| 51 | + const decoder = new TextDecoder('utf-8') |
| 52 | +
|
| 53 | + let tempResult = '' |
| 54 | +
|
| 55 | + while (true) { |
| 56 | + if (stopFlag.value) { |
| 57 | + controller.abort() |
| 58 | + loading.value = false |
| 59 | + break |
| 60 | + } |
| 61 | +
|
| 62 | + const { done, value } = await reader.read() |
| 63 | + if (done) { |
| 64 | + break |
| 65 | + } |
| 66 | +
|
| 67 | + let chunk = decoder.decode(value, { stream: true }) |
| 68 | + tempResult += chunk |
| 69 | + const split = tempResult.match(/data:.*}\n\n/g) |
| 70 | + if (split) { |
| 71 | + chunk = split.join('') |
| 72 | + tempResult = tempResult.replace(chunk, '') |
| 73 | + } else { |
| 74 | + continue |
| 75 | + } |
| 76 | +
|
| 77 | + if (chunk && chunk.startsWith('data:{')) { |
| 78 | + if (split) { |
| 79 | + for (const str of split) { |
| 80 | + let data |
| 81 | + try { |
| 82 | + data = JSON.parse(str.replace('data:{', '{')) |
| 83 | + } catch (err) { |
| 84 | + console.error('JSON string:', str) |
| 85 | + throw err |
| 86 | + } |
| 87 | +
|
| 88 | + if (data.code && data.code !== 200) { |
| 89 | + ElMessage({ |
| 90 | + message: data.msg, |
| 91 | + type: 'error', |
| 92 | + showClose: true, |
| 93 | + }) |
| 94 | + return |
| 95 | + } |
| 96 | +
|
| 97 | + switch (data.type) { |
| 98 | + case 'recommended_question': |
| 99 | + if ( |
| 100 | + data.content && |
| 101 | + data.content.length > 0 && |
| 102 | + startsWith(data.content.trim(), '[') && |
| 103 | + endsWith(data.content.trim(), ']') |
| 104 | + ) { |
| 105 | + questions.value = data.content |
| 106 | + await nextTick() |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } finally { |
| 114 | + loading.value = false |
| 115 | + emits('loadingOver') |
| 116 | + } |
| 117 | +} |
| 118 | +
|
| 119 | +function stop() { |
| 120 | + stopFlag.value = true |
| 121 | + loading.value = false |
| 122 | + emits('stop') |
| 123 | +} |
| 124 | +
|
| 125 | +onBeforeUnmount(() => { |
| 126 | + stop() |
| 127 | +}) |
| 128 | +
|
| 129 | +defineExpose({ getRecommendQuestions, id: () => props.recordId, stop }) |
| 130 | +</script> |
| 131 | + |
| 132 | +<template> |
| 133 | + <div v-if="computedQuestions.length > 0 || loading" class="recommend-questions"> |
| 134 | + <div v-if="loading"> |
| 135 | + <el-button style="min-width: unset" type="primary" link loading /> |
| 136 | + </div> |
| 137 | + <div v-else class="question-grid-input"> |
| 138 | + <div |
| 139 | + v-for="(question, index) in computedQuestions" |
| 140 | + :key="index" |
| 141 | + class="question" |
| 142 | + :class="{ disabled: disabled }" |
| 143 | + @click="clickQuestion(question)" |
| 144 | + > |
| 145 | + {{ question }} |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + <div v-else class="recommend-questions-error"> |
| 150 | + {{ $t('qa.retrieve_error') }} |
| 151 | + </div> |
| 152 | +</template> |
| 153 | + |
| 154 | +<style scoped lang="less"> |
| 155 | +.recommend-questions { |
| 156 | + width: 100%; |
| 157 | + font-size: 14px; |
| 158 | + font-weight: 500; |
| 159 | + line-height: 22px; |
| 160 | + display: flex; |
| 161 | + flex-direction: column; |
| 162 | + gap: 4px; |
| 163 | +
|
| 164 | + .continue-ask { |
| 165 | + color: rgba(100, 106, 115, 1); |
| 166 | + font-weight: 400; |
| 167 | + } |
| 168 | +
|
| 169 | + .question-grid-input { |
| 170 | + display: grid; |
| 171 | + grid-gap: 12px; |
| 172 | + grid-template-columns: repeat(1, calc(100% - 6px)); |
| 173 | + } |
| 174 | +
|
| 175 | + .question-grid { |
| 176 | + display: grid; |
| 177 | + grid-gap: 12px; |
| 178 | + grid-template-columns: repeat(2, calc(50% - 6px)); |
| 179 | + } |
| 180 | +
|
| 181 | + .question { |
| 182 | + font-weight: 400; |
| 183 | + cursor: pointer; |
| 184 | + background: rgba(245, 246, 247, 1); |
| 185 | + min-height: 32px; |
| 186 | + border-radius: 6px; |
| 187 | + padding: 5px 12px; |
| 188 | + line-height: 22px; |
| 189 | + &:hover { |
| 190 | + background: rgba(31, 35, 41, 0.1); |
| 191 | + } |
| 192 | + &.disabled { |
| 193 | + cursor: not-allowed; |
| 194 | + background: rgba(245, 246, 247, 1); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | +
|
| 199 | +.recommend-questions-error { |
| 200 | + font-size: 12px; |
| 201 | + font-weight: 500; |
| 202 | + color: rgba(100, 106, 115, 1); |
| 203 | + margin-top: 70px; |
| 204 | + display: flex; |
| 205 | + align-items: center; |
| 206 | + justify-content: center; |
| 207 | + width: 100%; |
| 208 | +} |
| 209 | +</style> |
0 commit comments