@lobehub/chat 0.40.0 → 0.40.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ### [Version 0.40.1](https://github.com/lobehub/lobe-chat/compare/v0.40.0...v0.40.1)
6
+
7
+ <sup>Released on **2023-08-06**</sup>
8
+
9
+ #### 🐛 Bug Fixes
10
+
11
+ - **misc**: 优化 openai 接口的错误处理逻辑.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### What's fixed
19
+
20
+ - **misc**: 优化 openai 接口的错误处理逻辑 ([eae78fe](https://github.com/lobehub/lobe-chat/commit/eae78fe))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ## [Version 0.40.0](https://github.com/lobehub/lobe-chat/compare/v0.39.4...v0.40.0)
6
31
 
7
32
  <sup>Released on **2023-08-05**</sup>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/chat",
3
- "version": "0.40.0",
3
+ "version": "0.40.1",
4
4
  "description": "Lobe Chat is an open-source chatbot client using LangChain, Typescript and Next.js",
5
5
  "keywords": [
6
6
  "chatbot",
@@ -10,6 +10,7 @@ const getStatus = (errorType: ErrorType) => {
10
10
  return 577;
11
11
  }
12
12
  }
13
+ return errorType;
13
14
  };
14
15
 
15
16
  export const createErrorResponse = (errorType: ErrorType, body?: any) => {
@@ -65,20 +65,35 @@ export const createChatCompletion = async ({
65
65
 
66
66
  const requestParams = { functions, messages: formatMessages, stream: true, ...params };
67
67
 
68
+ let response: Response;
69
+
68
70
  try {
69
- const response = await openai.createChatCompletion(requestParams);
71
+ response = await openai.createChatCompletion(requestParams);
72
+ } catch (error) {
73
+ // 如果 await 超时报错,说明是 OpenAI 服务端的问题
74
+ return createErrorResponse(ErrorType.GatewayTimeout, { message: error });
75
+ }
70
76
 
71
- if (!response.ok) {
72
- const error = await response.json();
77
+ // ============ 4. 处理异常响应 ============ //
78
+ if (!response.ok) {
79
+ let error;
73
80
 
74
- return createErrorResponse(ErrorType.OpenAIBizError, error);
75
- }
81
+ try {
82
+ // 正常情况下应该是 OpenAI 的 JSON
83
+ error = await response.clone().json();
84
+ } catch {
85
+ // 如果不是 JSON,那么可能是其他接口端的响应,读 text 为结果
86
+ const result = await response.text();
76
87
 
77
- const stream = OpenAIStream(response, callbacks?.(requestParams));
88
+ error = { message: result };
89
+ }
78
90
 
79
- return new StreamingTextResponse(stream);
80
- } catch (error) {
81
- // 如果 await 超时报错,说明是 OpenAI 服务端的问题
82
- return createErrorResponse(ErrorType.GatewayTimeout, { message: error });
91
+ return createErrorResponse(ErrorType.OpenAIBizError, { ...error, endpoint });
83
92
  }
93
+
94
+ // ============ 5. 发送正常相应 ============ //
95
+
96
+ const stream = OpenAIStream(response, callbacks?.(requestParams));
97
+
98
+ return new StreamingTextResponse(stream);
84
99
  };