wjc
2026-03-06 b9408687a3c0490289206b7e3b623d1490b38afd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package com.hdl.photovoltaic.internet;
 
import android.text.TextUtils;
import android.util.Log;
 
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.hdl.photovoltaic.config.UserConfigManage;
 
import org.jetbrains.annotations.NotNull;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
 
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
 
/**
 * AI 聊天流式请求工具类
 * 支持 SSE (Server-Sent Events) 流式响应
 * 类似 ChatGPT 的流式输出效果
 */
public class ChatStreamClient {
    // 单例实例
    private static volatile ChatStreamClient instance;
 
    /**
     * 获取单例实例
     */
    public static ChatStreamClient getInstance() {
        if (instance == null) {
            synchronized (ChatStreamClient.class) {
                if (instance == null) {
                    instance = new ChatStreamClient();
                }
            }
        }
        return instance;
    }
 
    // ==================== 常量定义 ====================
    private static final MediaType JSON = MediaType.get("application/json");
    private static final String SSE_MEDIA_TYPE = "text/event-stream";
    private static final String JSON_MEDIA_TYPE = "application/json";
    private static final String DONE_FLAG = "[DONE]";
 
    private final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(0, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
//                .connectionPool(new ConnectionPool(
//                        builder.maxIdleConnections,
//                        builder.keepAliveDuration,
//                        builder.timeUnit
//                ))
//                .retryOnConnectionFailure(builder.retryOnFailure)
//                .addInterceptor(new HttpLoggingInterceptor()) // 可选:添加日志
            .build();
    ;      // HTTP 客户端
    private final Gson gson = new Gson();                       // JSON 解析器
    private final String apiKey = "Bearer " + UserConfigManage.getInstance().getAgentSecret();                    // API 密钥
    private final String baseUrl = UserConfigManage.getInstance().getAgentUrl();// "https://agent.hdlcontrol.com/v1";;                    // 基础 URL
 
 
    // ==================== 回调接口 ====================
    public interface ChatCallback {
        /**
         * 收到消息片段时回调(流式输出)
         *
         * @param content 消息内容片段
         */
        void onMessage(String content);
 
        /**
         * 消息完成时回调
         */
        default void onComplete() {
        }
 
        /**
         * 发生错误时回调
         *
         * @param error 错误信息
         */
        default void onError(String error) {
        }
 
        /**
         * 收到完整消息时回调(非流式模式使用)
         *
         * @param fullMessage 完整消息
         */
        default void onFullMessage(String fullMessage) {
        }
    }
 
    // ==================== 请求参数类 ====================
    public static class ChatMode {
        public boolean stream = true;
        public boolean isGet = false;
        public String url = "";
        public Object data = null;
    }
 
 
    /**
     * 发送流式聊天请求(完整参数)
     *
     * @param chatMode 请求参数
     * @param callback 回调接口
     * @return Cancelable 可取消的对象
     */
    public Cancelable streamChat(ChatMode chatMode, ChatCallback callback) {
//        // 确保是流式请求
//        chatMode.stream = true;
 
        // 构建 HTTP 请求
        Request httpRequest = buildHttpRequest(chatMode);
//        try {
//            // 获取请求体
//            if (httpRequest.body() != null) {
//                Buffer buffer = new Buffer();
//                httpRequest.body().writeTo(buffer);
//                String body = buffer.readUtf8();
//                // 注意:读取后记得关闭 buffer
//                buffer.close();
//                System.out.println("Request Body: " + body);
//
//            }
//        } catch (Exception e) {
//
//        }
        // 创建可取消的 Call
        Call call = okHttpClient.newCall(httpRequest);
 
        // 执行异步请求
        call.enqueue(new StreamCallbackHandler(call, chatMode, callback));
 
        // 返回可取消对象
        return () -> {
            if (!call.isCanceled()) {
                call.cancel();
            }
        };
    }
 
    /**
     * 发送非流式聊天请求(一次性返回)
     *
     * @param request 请求参数
     * @return 完整响应
     */
    public String chatSync(ChatMode request) {
//        request.stream = false;
        Request httpRequest = buildHttpRequest(request);
        try (Response response = okHttpClient.newCall(httpRequest).execute()) {
            if (!response.isSuccessful()) {
                return response.message() + "(" + response.code() + ")";
            }
            return Objects.requireNonNull(response.body()).string();
        } catch (Exception e) {
            return e.getMessage();
        }
    }
 
 
    /**
     * 构建 HTTP 请求
     */
    private Request buildHttpRequest(ChatMode ChatMode) {
 
        String jsonBody = "";
        if (ChatMode.data != null) {
            jsonBody = gson.toJson(ChatMode.data);
        }
        String newUrl = baseUrl + ChatMode.url;
        if (ChatMode.isGet) {
            return new Request.Builder()
                    .url(newUrl)
                    .get()
                    .addHeader("Authorization", apiKey)
                    .addHeader("Cache-Control", "no-cache")
                    .addHeader("Connection", "keep-alive")
                    .build();
        } else {
            return new Request.Builder()
                    .url(newUrl)
                    .post(RequestBody.create(jsonBody, JSON))
                    .addHeader("Authorization", apiKey)
                    .addHeader("Accept", ChatMode.stream ? SSE_MEDIA_TYPE : JSON_MEDIA_TYPE)
                    .addHeader("Cache-Control", "no-cache")
                    .addHeader("Connection", "keep-alive")
                    .build();
        }
    }
 
 
    /**
     * 解析流式数据块
     */
    private String parseStreamChunk(String data) {
        if (data == null || data.isEmpty() || data.equals(DONE_FLAG)) {
            return "";
        }
        try {
            JsonObject json = JsonParser.parseString(data).getAsJsonObject();
            String event = json.has("event") ? json.get("event").getAsString() : "";
            if (event.equals("message")) {
                return json.getAsString();
            } else if (event.equals("message_end")) {
                return DONE_FLAG;
            } else if (event.equals("error")) {
                return "error";
            } else {
                return "";
            }
 
        } catch (Exception e) {
            // 解析失败,返回原始数据
            return data;
        }
    }
 
    // ==================== 流式响应处理器 ====================
    private class StreamCallbackHandler implements Callback {
        private final Call call;
        private final ChatMode request;
        private final ChatCallback callback;
        private final StringBuilder fullContent = new StringBuilder();
 
        public StreamCallbackHandler(Call call, ChatMode request, ChatCallback callback) {
            this.call = call;
            this.request = request;
            this.callback = callback;
        }
 
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            callback.onError("Network error: " + e.getMessage());
 
        }
 
        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) {
            if (!response.isSuccessful()) {
                callback.onError("HTTP error: " + response.code());
                response.close();
                return;
            }
            // 检查内容类型
            MediaType contentType = response.body().contentType();
            if (contentType == null || !contentType.toString().startsWith(SSE_MEDIA_TYPE)) {
                // 如果不是流式,可能是普通 JSON
                try {
                    String body = response.body().string();
                    callback.onFullMessage(body);
                } catch (IOException e) {
                    callback.onError("Parse error: " + e.getMessage());
                }
                response.close();
                return;
            }
 
            // 流式处理
            try (ResponseBody responseBody = response.body()) {
                BufferedReader reader = new BufferedReader(responseBody.charStream());
                String line;
                while ((line = reader.readLine()) != null) {
                    if (call.isCanceled()) {
                        break;
                    }
 
                    if (line.startsWith("data:")) {
                        String data = line.substring(5).trim();
                        Log.d("流式处理==", line);
                        if (data.equals(DONE_FLAG)) {
                            callback.onComplete();
                            break;
                        }
                        String content = parseStreamChunk(data);
                        if (!TextUtils.isEmpty(content)) {
                            if (content.equals(DONE_FLAG)) {
                                callback.onComplete();
                                break;
                            } else if (content.equals("error")) {
                                callback.onError(data);
                                break;
                            }
                            fullContent.append(content);
                            callback.onMessage(content);
                        }
                    }
                }
 
//                // 如果没收到 DONE 但流结束了,也回调 complete
//                if (isActive.get()) {
//                    callback.onComplete();
//                }
 
            } catch (IOException e) {
                callback.onError("Stream error: " + e.getMessage());
            }
        }
    }
 
    // ==================== 可取消接口 ====================
    public interface Cancelable {
        void cancel();
    }
 
 
    /**
     * 释放资源(应用退出时调用)
     */
    public void shutdown() {
        okHttpClient.dispatcher().executorService().shutdown();
        okHttpClient.connectionPool().evictAll();
        try {
            Cache cache = okHttpClient.cache();
            if (cache != null) {
                cache.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}