1919import okhttp3 .sse .EventSourceListener ;
2020
2121/**
22- * sse
22+ * EventSource listener for chat-related events.
2323 *
2424 * @author plexpt
2525 */
2626@ Slf4j
2727public 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 ();
0 commit comments