Skip to content

Commit 383a31c

Browse files
committed
up 4.0.2
1 parent 43403b6 commit 383a31c

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

src/main/java/com/plexpt/chatgpt/exception/ChatException.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.plexpt.chatgpt.exception;
22

33
/**
4-
* 异常
4+
* Custom exception class for chat-related errors
55
*
66
* @author plexpt
77
*/
88
public class ChatException extends RuntimeException {
99

1010

11+
/**
12+
* Constructs a new ChatException with the specified detail message.
13+
*
14+
* @param message the detail message (which is saved for later retrieval by the getMessage() method)
15+
*/
1116
public ChatException(String msg) {
1217
super(msg);
1318
}

src/main/java/com/plexpt/chatgpt/listener/AbstractStreamListener.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,59 @@
1919
import okhttp3.sse.EventSourceListener;
2020

2121
/**
22-
* sse
22+
* EventSource listener for chat-related events.
2323
*
2424
* @author plexpt
2525
*/
2626
@Slf4j
2727
public abstract class AbstractStreamListener extends EventSourceListener {
2828

29-
protected String last = "";
29+
protected String lastMessage = "";
30+
31+
32+
/**
33+
* Called when all new message are received.
34+
*
35+
* @param message the new message
36+
*/
3037
@Setter
3138
@Getter
3239
protected Consumer<String> onComplate = s -> {
3340

3441
};
3542

36-
3743
/**
44+
* Called when a new message is received.
3845
* 收到消息 单个字
46+
*
47+
* @param message the new message
3948
*/
40-
public abstract void onMsg(String msg);
41-
49+
public abstract void onMsg(String message);
4250

4351
/**
44-
* 出错了
52+
* Called when an error occurs.
53+
* 出错时调用
54+
*
55+
* @param throwable the throwable that caused the error
56+
* @param response the response associated with the error, if any
4557
*/
46-
public abstract void onError(Throwable t, String response);
58+
public abstract void onError(Throwable throwable, String response);
4759

4860
@Override
4961
public void onOpen(EventSource eventSource, Response response) {
62+
// do nothing
5063
}
5164

5265
@Override
5366
public void onClosed(EventSource eventSource) {
67+
// do nothing
5468
}
5569

5670
@Override
5771
public void onEvent(EventSource eventSource, String id, String type, String data) {
5872
if (data.equals("[DONE]")) {
59-
log.info("回答完成:{}", last);
60-
onComplate.accept(last);
73+
log.info("Chat session completed: {}", lastMessage);
74+
onComplate.accept(lastMessage);
6175
return;
6276
}
6377

@@ -71,7 +85,7 @@ public void onEvent(EventSource eventSource, String id, String type, String data
7185
String text = delta.getContent();
7286

7387
if (text != null) {
74-
last += text;
88+
lastMessage += text;
7589

7690
onMsg(text);
7791

@@ -82,28 +96,37 @@ public void onEvent(EventSource eventSource, String id, String type, String data
8296

8397
@SneakyThrows
8498
@Override
85-
public void onFailure(EventSource eventSource, Throwable t, Response response) {
99+
public void onFailure(EventSource eventSource, Throwable throwable, Response response) {
86100

87101
try {
88-
log.error("stream连接异常:{}", t);
102+
log.error("Stream connection error: {}", throwable);
89103

90-
String res = "";
104+
String responseText = "";
91105

92106
if (Objects.nonNull(response)) {
93-
res = response.body().string();
107+
responseText = response.body().string();
94108
}
95109

96-
log.error("response:{}", res);
110+
log.error("response:{}", responseText);
97111

98-
String seq = "Your access was terminated due to violation of our policies";
112+
String forbiddenText = "Your access was terminated due to violation of our policies";
99113

100-
if (StrUtil.contains(res, seq)) {
114+
if (StrUtil.contains(responseText, forbiddenText)) {
115+
log.error("Chat session has been terminated due to policy violation");
101116
log.error("检测到号被封了");
102117
}
103118

104-
onError(t, res);
119+
String overloadedText = "That model is currently overloaded with other requests.";
120+
121+
if (StrUtil.contains(responseText, overloadedText)) {
122+
log.error("检测到官方超载了,赶紧优化你的代码,做重试吧");
123+
}
124+
125+
this.onError(throwable, responseText);
105126

106127
} catch (Exception e) {
128+
log.warn("onFailure error:{}", e);
129+
// do nothing
107130

108131
} finally {
109132
eventSource.cancel();

src/main/java/com/plexpt/chatgpt/listener/ConsoleStreamListener.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
/**
66
* 控制台测试
7+
* Console Stream Test Listener
78
*
89
* @author plexpt
910
*/
@@ -12,12 +13,12 @@ public class ConsoleStreamListener extends AbstractStreamListener {
1213

1314

1415
@Override
15-
public void onMsg(String msg) {
16-
System.out.print(msg);
16+
public void onMsg(String message) {
17+
System.out.print(message);
1718
}
1819

1920
@Override
20-
public void onError(Throwable t, String response) {
21+
public void onError(Throwable throwable, String response) {
2122

2223
}
2324

src/main/java/com/plexpt/chatgpt/listener/SseStreamListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public class SseStreamListener extends AbstractStreamListener {
2020

2121

2222
@Override
23-
public void onMsg(String msg) {
24-
SseHelper.send(sseEmitter, msg);
23+
public void onMsg(String message) {
24+
SseHelper.send(sseEmitter, message);
2525
}
2626

2727
@Override
28-
public void onError(Throwable t, String response) {
28+
public void onError(Throwable throwable, String response) {
2929
SseHelper.complete(sseEmitter);
3030
}
3131

0 commit comments

Comments
 (0)