mirror of
https://gitee.com/agents-flex/agents-flex.git
synced 2024-11-29 18:38:17 +08:00
refactor: optimize HttpClient.java And SseClient.java
This commit is contained in:
parent
9d7ef09570
commit
f445d6fa33
@ -75,8 +75,8 @@ public class HttpClient {
|
||||
request = builder.method(method, body).build();
|
||||
}
|
||||
|
||||
try (Response response = okHttpClient.newCall(request).execute()) {
|
||||
ResponseBody body = response.body();
|
||||
try (Response response = okHttpClient.newCall(request).execute();
|
||||
ResponseBody body = response.body()) {
|
||||
if (body != null) {
|
||||
return body.string();
|
||||
}
|
||||
|
@ -16,10 +16,8 @@
|
||||
package com.agentsflex.llm.client.impl;
|
||||
|
||||
import com.agentsflex.llm.LlmConfig;
|
||||
import com.agentsflex.llm.client.LLMClientException;
|
||||
import com.agentsflex.llm.client.LlmClient;
|
||||
import com.agentsflex.llm.client.LlmClientListener;
|
||||
import com.agentsflex.util.StringUtil;
|
||||
import okhttp3.*;
|
||||
import okhttp3.sse.EventSource;
|
||||
import okhttp3.sse.EventSourceListener;
|
||||
@ -27,7 +25,6 @@ import okhttp3.sse.EventSources;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ -102,26 +99,7 @@ public class SseClient extends EventSourceListener implements LlmClient {
|
||||
|
||||
@Override
|
||||
public void onFailure(@NotNull EventSource eventSource, @Nullable Throwable t, @Nullable Response response) {
|
||||
if (t == null && response != null) {
|
||||
String errMessage = "Response code: " + response.code();
|
||||
String message = response.message();
|
||||
if (StringUtil.hasText(message)) {
|
||||
errMessage += ", message: " + message;
|
||||
}
|
||||
try {
|
||||
ResponseBody body = response.body();
|
||||
if (body != null) {
|
||||
String string = body.string();
|
||||
if (StringUtil.hasText(string)) {
|
||||
errMessage += ", body: " + string;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
t = new LLMClientException(errMessage);
|
||||
}
|
||||
this.listener.onFailure(this, t);
|
||||
this.listener.onFailure(this, Util.getFailureThrowable(t, response));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.agentsflex.llm.client.impl;
|
||||
|
||||
import com.agentsflex.llm.client.LLMClientException;
|
||||
import com.agentsflex.util.StringUtil;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class Util {
|
||||
|
||||
public static Throwable getFailureThrowable(Throwable t, Response response) {
|
||||
if (t != null) {
|
||||
return t;
|
||||
}
|
||||
|
||||
if (response != null) {
|
||||
String errMessage = "Response code: " + response.code();
|
||||
String message = response.message();
|
||||
if (StringUtil.hasText(message)) {
|
||||
errMessage += ", message: " + message;
|
||||
}
|
||||
try (ResponseBody body = response.body()) {
|
||||
if (body != null) {
|
||||
String string = body.string();
|
||||
if (StringUtil.hasText(string)) {
|
||||
errMessage += ", body: " + string;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
t = new LLMClientException(errMessage);
|
||||
}
|
||||
|
||||
return t;
|
||||
|
||||
}
|
||||
}
|
@ -50,7 +50,7 @@ public class WebSocketClient extends WebSocketListener implements LlmClient {
|
||||
this.webSocket = client.newWebSocket(request, this);
|
||||
this.isStop = false;
|
||||
|
||||
if (this.config.isDebug()){
|
||||
if (this.config.isDebug()) {
|
||||
System.out.println(">>>>send payload:" + payload);
|
||||
}
|
||||
}
|
||||
@ -76,7 +76,7 @@ public class WebSocketClient extends WebSocketListener implements LlmClient {
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, String text) {
|
||||
if (this.config.isDebug()){
|
||||
if (this.config.isDebug()) {
|
||||
System.out.println(">>>>receive payload:" + text);
|
||||
}
|
||||
this.listener.onMessage(this, text);
|
||||
@ -84,7 +84,7 @@ public class WebSocketClient extends WebSocketListener implements LlmClient {
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, ByteString bytes) {
|
||||
this.listener.onMessage(this, bytes.utf8());
|
||||
this.onMessage(webSocket, bytes.utf8());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -100,8 +100,7 @@ public class WebSocketClient extends WebSocketListener implements LlmClient {
|
||||
if (!isStop) {
|
||||
this.isStop = true;
|
||||
this.listener.onStop(this);
|
||||
|
||||
this.listener.onFailure(this, t);
|
||||
this.listener.onFailure(this, Util.getFailureThrowable(t, response));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user