@@ -8,7 +8,7 @@ import { toPng, toSvg } from 'html-to-image'
88import { Button , Dropdown , Form , Input , Space , message } from 'antd'
99import { DownloadOutlined , CopyOutlined } from '@ant-design/icons'
1010import { copyTextToClipboard } from '@utils/aidFn'
11- import initSSE from './sse '
11+ import initSSE from './fixSse '
1212
1313const ChatGpt = ( ) => {
1414 const markmapRef = useRef ( null )
@@ -45,24 +45,41 @@ const ChatGpt = () => {
4545 structureResultRef . current = ''
4646 const source = initSSE ( apiKey , text )
4747 source . addEventListener ( 'message' , ( e ) => {
48+ console . log ( 'message' , e . data )
4849 if ( e . data !== '[DONE]' ) {
50+ console . log ( 'NOT DONE' )
4951 const payload = JSON . parse ( e . data )
5052 const {
5153 delta : { content } ,
52- } = payload . choices [ 0 ]
54+ } = payload ? .choices [ 0 ] || { delta : { content : '' } }
5355 if ( content ) {
5456 apiResultRef . current += content
5557 setApiResult ( apiResultRef . current )
5658 }
5759 } else {
60+ console . log ( 'DONE' )
61+ setReadyState ( 2 )
5862 source . close ( )
5963 }
6064 } )
6165
6266 source . addEventListener ( 'readystatechange' , ( e ) => {
6367 setReadyState ( e . readyState )
64- // if (e.readyState === 2) {
65- // }
68+ console . log ( 'readyState' , e . readyState )
69+ if ( e . readyState === 2 ) {
70+ console . log ( 'SSE connection closed' )
71+ }
72+ } )
73+
74+ source . addEventListener ( 'error' , ( e ) => {
75+ console . error ( 'SSE error:' , e )
76+ source . close ( )
77+ } )
78+ source . addEventListener ( 'open' , ( e ) => {
79+ console . log ( 'SSE connection opened:' , e )
80+ } )
81+ source . addEventListener ( 'abort' , ( e ) => {
82+ console . log ( 'SSE connection closed:' , e )
6683 } )
6784
6885 source . stream ( )
@@ -126,12 +143,13 @@ const ChatGpt = () => {
126143 const payload = JSON . parse ( e . data )
127144 const {
128145 delta : { content } ,
129- } = payload . choices [ 0 ]
146+ } = payload ? .choices [ 0 ] || { delta : { content : '' } }
130147 if ( content ) {
131148 structureResultRef . current += content
132149 setStructureResult ( structureResultRef . current )
133150 }
134151 } else {
152+ setStructureReadyState ( 2 )
135153 source . close ( )
136154 }
137155 } )
@@ -268,7 +286,7 @@ const ChatGpt = () => {
268286 </ Space >
269287 ) }
270288 </ section >
271- { /* { JSON.stringify(structureReadyState , null, 2)} */ }
289+ { JSON . stringify ( structureResult , null , 2 ) }
272290 { structureReadyState === 0 && < h3 > Ai 正在理解的需求...</ h3 > }
273291 { structureReadyState === 1 && < h3 > 生成中...</ h3 > }
274292 { structureResult && (
0 commit comments