|
13 | 13 | package com.tinyengine.it.service.app.impl.v1; |
14 | 14 |
|
15 | 15 | import com.fasterxml.jackson.databind.JsonNode; |
| 16 | +import com.tinyengine.it.common.exception.ServiceException; |
16 | 17 | import com.tinyengine.it.common.log.SystemServiceLog; |
17 | 18 | import com.tinyengine.it.common.utils.JsonUtils; |
18 | 19 | import com.tinyengine.it.common.utils.SM4Utils; |
@@ -174,83 +175,68 @@ private String buildRequestBody(ChatRequest request) { |
174 | 175 | return JsonUtils.encode(body); |
175 | 176 | } |
176 | 177 |
|
177 | | - private JsonNode processStandardResponse(HttpRequest.Builder requestBuilder) |
178 | | - throws Exception { |
179 | | - HttpResponse<String> response = httpClient.send( |
180 | | - requestBuilder.build(), HttpResponse.BodyHandlers.ofString()); |
| 178 | + private JsonNode processStandardResponse(HttpRequest.Builder requestBuilder) { |
| 179 | + HttpResponse<String> response = null; |
| 180 | + String code = null; |
| 181 | + String message = null; |
| 182 | + try { |
| 183 | + response = httpClient.send( |
| 184 | + requestBuilder.build(), HttpResponse.BodyHandlers.ofString()); |
| 185 | + code = String.valueOf(response.statusCode()); |
| 186 | + if (response.statusCode() != 200) { |
| 187 | + String errorBody = response.body(); |
181 | 188 |
|
182 | | - // 添加状态码检查 |
183 | | - if (response.statusCode() != 200) { |
184 | | - String errorBody = response.body(); |
185 | | - try { |
186 | 189 | // 尝试解析错误JSON |
187 | 190 | JsonNode errorNode = JsonUtils.MAPPER.readTree(errorBody); |
188 | | - throw new IOException("API请求失败: " + response.statusCode() + " - " + |
189 | | - errorNode.get("error").get("message").asText()); |
190 | | - } catch (Exception e) { |
191 | | - // 如果无法解析JSON,返回原始错误信息 |
192 | | - throw new IOException("API请求失败: " + response.statusCode() + " - " + errorBody); |
| 191 | + message = errorNode.get("error").get("message").asText(); |
| 192 | + throw new ServiceException(code, message); |
193 | 193 | } |
| 194 | + return JsonUtils.MAPPER.readTree(response.body()); |
| 195 | + } catch (IOException | InterruptedException e) { |
| 196 | + throw new ServiceException(code, message); |
194 | 197 | } |
195 | 198 |
|
196 | | - return JsonUtils.MAPPER.readTree(response.body()); |
| 199 | + |
197 | 200 | } |
| 201 | + |
198 | 202 | private StreamingResponseBody processStreamResponse(HttpRequest.Builder requestBuilder) { |
199 | 203 | return outputStream -> { |
| 204 | + HttpResponse<InputStream> response = null; |
200 | 205 | try { |
201 | | - // 使用相同的httpClient实例,确保配置一致 |
202 | | - HttpResponse<InputStream> response = httpClient.send( |
203 | | - requestBuilder.build(), |
204 | | - HttpResponse.BodyHandlers.ofInputStream() |
| 206 | + response = httpClient.send( |
| 207 | + requestBuilder.build(), HttpResponse.BodyHandlers.ofInputStream() |
205 | 208 | ); |
| 209 | + } catch (InterruptedException e) { |
| 210 | + throw new ServiceException("500", e.getMessage()); |
| 211 | + } |
206 | 212 |
|
207 | | - // 立即检查状态码 |
208 | | - if (response.statusCode() != 200) { |
209 | | - String errorBody = new String(response.body().readAllBytes(), StandardCharsets.UTF_8); |
210 | | - // 格式化为正确的SSE错误事件 |
211 | | - String errorEvent = formatSSEError(response.statusCode(), errorBody); |
212 | | - outputStream.write(errorEvent.getBytes(StandardCharsets.UTF_8)); |
213 | | - outputStream.flush(); |
214 | | - return; // 重要:立即返回,不再处理流 |
215 | | - } |
| 213 | + log.info("收到AI API响应,状态码: {}", response.statusCode()); |
216 | 214 |
|
217 | | - // 正常流处理逻辑 |
218 | | - try (InputStream inputStream = response.body()) { |
219 | | - byte[] buffer = new byte[8192]; |
220 | | - int bytesRead; |
221 | | - while ((bytesRead = inputStream.read(buffer)) != -1) { |
222 | | - outputStream.write(buffer, 0, bytesRead); |
223 | | - outputStream.flush(); |
224 | | - } |
225 | | - } |
226 | | - } catch (Exception e) { |
227 | | - try { |
228 | | - // 格式化为标准SSE错误格式 |
229 | | - String errorEvent = formatSSEError(500, e.getMessage()); |
230 | | - outputStream.write(errorEvent.getBytes(StandardCharsets.UTF_8)); |
| 215 | + if (response.statusCode() != 200) { |
| 216 | + String errorBody = new String(response.body().readAllBytes(), StandardCharsets.UTF_8); |
| 217 | + |
| 218 | + log.info("错误响应内容: {}", errorBody); |
| 219 | + |
| 220 | + JsonNode errorNode = JsonUtils.MAPPER.readTree(errorBody); |
| 221 | + throw new ServiceException(String.valueOf(response.statusCode()), errorNode.get("error").get("message").asText()); |
| 222 | + } |
| 223 | + |
| 224 | + // 正常流处理逻辑 |
| 225 | + try (InputStream inputStream = response.body()) { |
| 226 | + byte[] buffer = new byte[8192]; |
| 227 | + int bytesRead; |
| 228 | + while ((bytesRead = inputStream.read(buffer)) != -1) { |
| 229 | + outputStream.write(buffer, 0, bytesRead); |
231 | 230 | outputStream.flush(); |
232 | | - } catch (IOException ioException) { |
233 | | - log.error("无法发送错误信息: " + ioException.getMessage()); |
234 | 231 | } |
| 232 | + // 流正常结束时发送结束标记 |
| 233 | + String doneEvent = "data: [DONE]\n\n"; |
| 234 | + log.info("发送DONE事件: {}", doneEvent); |
| 235 | + outputStream.write(doneEvent.getBytes(StandardCharsets.UTF_8)); |
| 236 | + outputStream.flush(); |
235 | 237 | } |
236 | 238 | }; |
237 | 239 | } |
238 | | - /** |
239 | | - * 格式化SSE错误事件 |
240 | | - */ |
241 | | - private String formatSSEError(int statusCode, String errorBody) { |
242 | | - try { |
243 | | - // 尝试解析API错误信息 |
244 | | - JsonNode errorNode = JsonUtils.MAPPER.readTree(errorBody); |
245 | | - String errorMessage = errorNode.get("error").get("message").asText(); |
246 | | - return String.format("data: {\"error\": {\"code\": %d, \"message\": \"%s\"}}\n\n", |
247 | | - statusCode, errorMessage); |
248 | | - } catch (Exception e) { |
249 | | - // 如果无法解析,返回原始错误 |
250 | | - return String.format("data: {\"error\": {\"code\": %d, \"message\": \"%s\"}}\n\n", |
251 | | - statusCode, errorBody.replace("\"", "\\\"")); |
252 | | - } |
253 | | - } |
254 | 240 |
|
255 | 241 | private String getApiKey(String encryptApiKey) throws Exception { |
256 | 242 | String sm4Key = System.getenv("SM4KEY"); |
|
0 commit comments