@@ -63,7 +63,9 @@ async function getRecommendQuestions() {
6363 const controller: AbortController = new AbortController ()
6464 const response = await chatApi .recommendQuestions (props .recordId , controller )
6565 const reader = response .body .getReader ()
66- const decoder = new TextDecoder ()
66+ const decoder = new TextDecoder (' utf-8' )
67+
68+ let tempResult = ' '
6769
6870 while (true ) {
6971 if (stopFlag .value ) {
@@ -77,60 +79,56 @@ async function getRecommendQuestions() {
7779 break
7880 }
7981
80- const chunk = decoder .decode (value )
81-
82- let _list = [chunk ]
83-
84- const lines = chunk .trim ().split (' }\n\n {' )
85- if (lines .length > 1 ) {
86- _list = []
87- for (let line of lines ) {
88- if (! line .trim ().startsWith (' {' )) {
89- line = ' {' + line .trim ()
90- }
91- if (! line .trim ().endsWith (' }' )) {
92- line = line .trim () + ' }'
93- }
94- _list .push (line )
95- }
82+ let chunk = decoder .decode (value , { stream: true })
83+ tempResult += chunk
84+ const split = tempResult .match (/ data:. * }\n\n / g )
85+ if (split ) {
86+ chunk = split .join (' ' )
87+ tempResult = tempResult .replace (chunk , ' ' )
88+ } else {
89+ continue
9690 }
9791
98- for (const str of _list ) {
99- let data
100- try {
101- data = JSON .parse (str )
102- } catch (err ) {
103- console .error (' JSON string:' , str )
104- throw err
105- }
92+ if (chunk && chunk .startsWith (' data:{' )) {
93+ if (split ) {
94+ for (const str of split ) {
95+ let data
96+ try {
97+ data = JSON .parse (str .replace (' data:{' , ' {' ))
98+ } catch (err ) {
99+ console .error (' JSON string:' , str )
100+ throw err
101+ }
106102
107- if (data .code && data .code !== 200 ) {
108- ElMessage ({
109- message: data .msg ,
110- type: ' error' ,
111- showClose: true ,
112- })
113- return
114- }
103+ if (data .code && data .code !== 200 ) {
104+ ElMessage ({
105+ message: data .msg ,
106+ type: ' error' ,
107+ showClose: true ,
108+ })
109+ return
110+ }
115111
116- switch (data .type ) {
117- case ' recommended_question' :
118- if (
119- data .content &&
120- data .content .length > 0 &&
121- startsWith (data .content .trim (), ' [' ) &&
122- endsWith (data .content .trim (), ' ]' )
123- ) {
124- if (_currentChat .value ?.records ) {
125- for (let record of _currentChat .value .records ) {
126- if (record .id === props .recordId ) {
127- record .recommended_question = data .content
128-
129- await nextTick ()
112+ switch (data .type ) {
113+ case ' recommended_question' :
114+ if (
115+ data .content &&
116+ data .content .length > 0 &&
117+ startsWith (data .content .trim (), ' [' ) &&
118+ endsWith (data .content .trim (), ' ]' )
119+ ) {
120+ if (_currentChat .value ?.records ) {
121+ for (let record of _currentChat .value .records ) {
122+ if (record .id === props .recordId ) {
123+ record .recommended_question = data .content
124+
125+ await nextTick ()
126+ }
127+ }
130128 }
131129 }
132- }
133130 }
131+ }
134132 }
135133 }
136134 }
0 commit comments