@@ -105,22 +105,40 @@ function createOpenAIBodyStr(searchContent, bodyContentText = '') {
105
105
}
106
106
107
107
async 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
+
108
131
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
119
135
)
120
-
121
- return response . body . getReader ( )
136
+ await handleStreamReaderAnswer ( el , reader )
122
137
} 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
124
142
}
125
143
}
126
144
@@ -171,44 +189,18 @@ function init() {
171
189
172
190
searchBtn . addEventListener ( 'click' , async ( ) => {
173
191
context . searchContent = searchInput . value
192
+ searchInput . value = ''
174
193
175
194
// 根据用户需要决定是否获取内容
176
195
if ( context . config . READ_CONTEXT ) {
177
196
chrome . tabs . sendMessage ( context . currentTab . id , 'get body content text' )
178
197
} 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 ( )
192
199
}
193
200
} )
194
201
195
202
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 )
212
204
} )
213
205
}
214
206
0 commit comments