44import com .fasterxml .jackson .databind .DeserializationFeature ;
55import com .fasterxml .jackson .databind .ObjectMapper ;
66import io .github .easy4j .opencode .OpenCodeHttpClientConfig ;
7- import io .github .easy4j .opencode .OpenCodeOkHttpClientFactory ;
87import io .github .easy4j .opencode .HttpCallCancellation ;
8+ import io .github .easy4j .opencode .OpenCodeOkHttpClientFactory ;
99import io .github .easy4j .opencode .api .model .*;
1010import io .github .easy4j .opencode .exception .OpenCodeHttpException ;
1111import lombok .extern .slf4j .Slf4j ;
2222import java .util .Optional ;
2323
2424/**
25- * OpenCode Server HTTP 客户端,封装 REST API。
26- * <p>基于 OkHttp,支持外部传入 {@link OkHttpClient}(复用别的插件实例)。</p>
25+ * HTTP client for the OpenCode Server REST API.
26+ *
27+ * <p>Built on OkHttp; supports externally provided {@link OkHttpClient} instances for
28+ * connection pooling across plugins. All methods throw {@link io.github.easy4j.opencode.exception.OpenCodeHttpException}
29+ * on HTTP errors.</p>
2730 *
31+ * @author [@Loong Wan](https://github.com/loong10k)
32+ * @since 3.0.0
33+ * @see OpenCodeHttpClientConfig
34+ * @see io.github.easy4j.opencode.exception.OpenCodeHttpException
2835 * @see <a href="https://opencode.ai/docs/server/">opencode server docs</a>
2936 */
3037@ Slf4j
@@ -36,14 +43,12 @@ public class OpenCodeHttpClient implements AutoCloseable {
3643 private final OpenCodeHttpClientConfig config ;
3744 private final OkHttpClient httpClient ;
3845 private final ObjectMapper objectMapper ;
39- private final boolean ownsHttpClient ;
4046
4147 public OpenCodeHttpClient (OpenCodeHttpClientConfig config , ObjectMapper objectMapper , OkHttpClient httpClient ) {
4248 this .config = Objects .requireNonNull (config , "config" );
4349 this .objectMapper = Objects .isNull (objectMapper ) ? new ObjectMapper ()
4450 .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false ): objectMapper ;
45- this .ownsHttpClient = Objects .isNull (httpClient );
46- this .httpClient = this .ownsHttpClient ? buildOkHttpClient (config ) : httpClient ;
51+ this .httpClient = Objects .isNull (httpClient ) ? buildOkHttpClient (config ) : httpClient ;
4752 }
4853
4954 private static OkHttpClient buildOkHttpClient (OpenCodeHttpClientConfig config ) {
@@ -932,16 +937,6 @@ private String toJson(Object body) {
932937
933938 @ Override
934939 public void close () {
935- if (ownsHttpClient ) {
936- httpClient .dispatcher ().executorService ().shutdownNow ();
937- httpClient .connectionPool ().evictAll ();
938- if (Objects .nonNull (httpClient .cache ())) {
939- try {
940- httpClient .cache ().close ();
941- } catch (IOException error ) {
942- log .debug ("Failed to close OpenCode HTTP cache" , error );
943- }
944- }
945- }
940+ // 外部传入的 OkHttpClient 不关闭;自建的也不主动关闭(OkHttpClient 内部管理连接池)
946941 }
947942}
0 commit comments