11<script setup lang="ts">
2- import { computed } from ' vue'
2+ import { computed , nextTick , ref } from ' vue'
33import { endsWith , startsWith } from ' lodash-es'
44import { useI18n } from ' vue-i18n'
5+ import { chatApi , ChatInfo } from ' @/api/chat.ts'
56
67const props = withDefaults (
78 defineProps <{
9+ recordId? : number
10+ currentChat? : ChatInfo
811 questions? : string
912 firstChat? : boolean
1013 }>(),
1114 {
15+ recordId: undefined ,
16+ currentChat : () => new ChatInfo (),
1217 questions: ' []' ,
1318 firstChat: false ,
1419 }
1520)
21+
22+ const emits = defineEmits ([' clickQuestion' , ' update:currentChat' ])
23+
24+ const loading = ref (false )
25+
26+ const _currentChat = computed ({
27+ get() {
28+ return props .currentChat
29+ },
30+ set(v ) {
31+ emits (' update:currentChat' , v )
32+ },
33+ })
34+
1635const computedQuestions = computed <string >(() => {
1736 if (
1837 props .questions &&
@@ -25,20 +44,98 @@ const computedQuestions = computed<string>(() => {
2544 return []
2645})
2746
28- const emits = defineEmits ([' clickQuestion' ])
29-
3047const { t } = useI18n ()
3148
3249function clickQuestion(question : string ): void {
3350 emits (' clickQuestion' , question )
3451}
52+
53+ async function getRecommendQuestions() {
54+ loading .value = true
55+ try {
56+ const response = await chatApi .recommendQuestions (props .recordId )
57+ const reader = response .body .getReader ()
58+ const decoder = new TextDecoder ()
59+
60+ while (true ) {
61+ const { done, value } = await reader .read ()
62+ if (done ) {
63+ break
64+ }
65+
66+ const chunk = decoder .decode (value )
67+
68+ let _list = [chunk ]
69+
70+ const lines = chunk .trim ().split (' }\n\n {' )
71+ if (lines .length > 1 ) {
72+ _list = []
73+ for (let line of lines ) {
74+ if (! line .trim ().startsWith (' {' )) {
75+ line = ' {' + line .trim ()
76+ }
77+ if (! line .trim ().endsWith (' }' )) {
78+ line = line .trim () + ' }'
79+ }
80+ _list .push (line )
81+ }
82+ }
83+
84+ for (const str of _list ) {
85+ let data
86+ try {
87+ data = JSON .parse (str )
88+ } catch (err ) {
89+ console .error (' JSON string:' , str )
90+ throw err
91+ }
92+
93+ if (data .code && data .code !== 200 ) {
94+ ElMessage ({
95+ message: data .msg ,
96+ type: ' error' ,
97+ showClose: true ,
98+ })
99+ return
100+ }
101+
102+ switch (data .type ) {
103+ case ' recommended_question' :
104+ if (
105+ data .content &&
106+ data .content .length > 0 &&
107+ startsWith (data .content .trim (), ' [' ) &&
108+ endsWith (data .content .trim (), ' ]' )
109+ ) {
110+ if (_currentChat .value ?.records ) {
111+ for (let record of _currentChat .value .records ) {
112+ if (record .id === props .recordId ) {
113+ record .recommended_question = data .content
114+
115+ await nextTick ()
116+ }
117+ }
118+ }
119+ }
120+ }
121+ }
122+ }
123+ } finally {
124+ loading .value = false
125+ }
126+ }
127+
128+ defineExpose ({ getRecommendQuestions , id : () => props .recordId })
35129 </script >
36130
37131<template >
38- <div v-if =" computedQuestions.length > 0" class =" recommend-questions" >
132+ <div v-if =" computedQuestions.length > 0 || loading " class =" recommend-questions" >
39133 <div v-if =" firstChat" >{{ t('qa.guess_u_ask') }}</div >
40134 <div v-else class =" continue-ask" >{{ t('qa.continue_to_ask') }}</div >
41- <div class =" question-grid" >
135+ <div v-if =" loading" >
136+ <el-button style =" min-width : unset " type =" primary" link loading />
137+ </div >
138+ <div v-else class =" question-grid" >
42139 <div
43140 v-for =" (question, index) in computedQuestions"
44141 :key =" index"
0 commit comments