@@ -105,22 +105,40 @@ function createOpenAIBodyStr(searchContent, bodyContentText = '') {
105105}
106106
107107async function fetchOpenAIStreamReader ( searchContent , bodyContentText = '' ) {
108+ const response = await fetch ( `${ context . config . BASE_URL } /chat/completions` , {
109+ headers : {
110+ 'Content-Type' : 'application/json' ,
111+ Authorization : `Bearer ${ context . config . API_KEY } `
112+ } ,
113+ method : 'post' ,
114+ body : createOpenAIBodyStr ( searchContent , bodyContentText )
115+ } )
116+
117+ if ( ! response . ok ) {
118+ throw new Error ( `${ response . status } - 网络响应不正常` )
119+ }
120+
121+ return response . body . getReader ( )
122+ }
123+
124+ async function handleProblem ( bodyContentText = '' ) {
125+ const el = document . createElement ( 'div' )
126+ el . setAttribute ( 'class' , 'item' )
127+ messageList . insertBefore ( el , messageList . firstElementChild )
128+
129+ searchBtn . disabled = context . isReplyState = true
130+
108131 try {
109- const response = await fetch (
110- `${ context . config . BASE_URL } /chat/completions` ,
111- {
112- headers : {
113- 'Content-Type' : 'application/json' ,
114- Authorization : `Bearer ${ context . config . API_KEY } `
115- } ,
116- method : 'post' ,
117- body : createOpenAIBodyStr ( searchContent , bodyContentText )
118- }
132+ const reader = await fetchOpenAIStreamReader (
133+ context . searchContent ,
134+ bodyContentText
119135 )
120-
121- return response . body . getReader ( )
136+ await handleStreamReaderAnswer ( el , reader )
122137 } catch ( error ) {
123- console . log ( `fetchOpenAIStreamReader error: ${ error . message } ` )
138+ el . innerText = `Error: ${ error . message } `
139+ } finally {
140+ context . isReplyState = false
141+ searchBtn . disabled = context . isSearchInputEmpty
124142 }
125143}
126144
@@ -171,44 +189,18 @@ function init() {
171189
172190 searchBtn . addEventListener ( 'click' , async ( ) => {
173191 context . searchContent = searchInput . value
192+ searchInput . value = ''
174193
175194 // 根据用户需要决定是否获取内容
176195 if ( context . config . READ_CONTEXT ) {
177196 chrome . tabs . sendMessage ( context . currentTab . id , 'get body content text' )
178197 } else {
179- const el = document . createElement ( 'div' )
180- el . setAttribute ( 'class' , 'item' )
181- messageList . insertBefore ( el , messageList . firstElementChild )
182-
183- searchBtn . disabled = context . isReplyState = true
184-
185- try {
186- const reader = await fetchOpenAIStreamReader ( context . searchContent )
187- await handleStreamReaderAnswer ( el , reader )
188- } finally {
189- context . isReplyState = false
190- searchBtn . disabled = context . isSearchInputEmpty
191- }
198+ handleProblem ( )
192199 }
193200 } )
194201
195202 chrome . runtime . onMessage . addListener ( async ( bodyContentText ) => {
196- const el = document . createElement ( 'div' )
197- el . setAttribute ( 'class' , 'item' )
198- messageList . insertBefore ( el , messageList . firstElementChild )
199-
200- searchBtn . disabled = context . isReplyState = true
201-
202- try {
203- const reader = await fetchOpenAIStreamReader (
204- context . searchContent ,
205- bodyContentText
206- )
207- await handleStreamReaderAnswer ( el , reader )
208- } finally {
209- context . isReplyState = false
210- searchBtn . disabled = context . isSearchInputEmpty
211- }
203+ handleProblem ( bodyContentText )
212204 } )
213205}
214206
0 commit comments