Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/main/java/com/google/genai/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,13 @@ private com.google.genai.interactions.core.ClientOptions createInteractionsClien
.baseUrl(httpOptions.baseUrl())
.apiKey(apiKey);

// BEGIN_SDK_PRIVATE_ONLY
// clientOptions
// .flatMap(ClientOptions::streamHandlerExecutor)
// .ifPresent(
// streamHandlerExecutor -> {
// com.google.genai.interactions.core.ClientOptions.Builder unused =
// builder.streamHandlerExecutor(streamHandlerExecutor);
// });
// END_SDK_PRIVATE_ONLY
clientOptions
.flatMap(ClientOptions::streamHandlerExecutor)
.ifPresent(
streamHandlerExecutor -> {
com.google.genai.interactions.core.ClientOptions.Builder unused =
builder.streamHandlerExecutor(streamHandlerExecutor);
});

httpOptions
.apiVersion()
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/google/genai/types/ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
package com.google.genai.types;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.genai.JsonSerializable;
import java.util.Optional;
import java.util.concurrent.Executor;

/** Client options to be used in the client instantiation. */
@AutoValue
Expand All @@ -42,6 +44,10 @@ public abstract class ClientOptions extends JsonSerializable {
@JsonProperty("proxyOptions")
public abstract Optional<ProxyOptions> proxyOptions();

/** EXPERIMENTAL the executor to use for running async stream handler callbacks. */
@JsonIgnore
public abstract Optional<Executor> streamHandlerExecutor();

/** Instantiates a builder for ClientOptions. */
@ExcludeFromGeneratedCoverageReport
public static Builder builder() {
Expand Down Expand Up @@ -124,6 +130,25 @@ public Builder clearProxyOptions() {
return proxyOptions(Optional.empty());
}

/**
* Setter for streamHandlerExecutor.
*
* <p>streamHandlerExecutor: EXPERIMENTAL the executor to use for running async stream handler
* callbacks.
*/
@JsonIgnore
public abstract Builder streamHandlerExecutor(Executor streamHandlerExecutor);

@ExcludeFromGeneratedCoverageReport
abstract Builder streamHandlerExecutor(Optional<Executor> streamHandlerExecutor);

/** Clears the value of streamHandlerExecutor field. */
@ExcludeFromGeneratedCoverageReport
@CanIgnoreReturnValue
public Builder clearStreamHandlerExecutor() {
return streamHandlerExecutor(Optional.empty());
}

public abstract ClientOptions build();
}

Expand Down
36 changes: 35 additions & 1 deletion src/test/java/com/google/genai/HttpApiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,41 @@ public void testCloseClient() {
assertTrue(client.httpClient().dispatcher().executorService().isShutdown());
assertEquals(0, client.httpClient().connectionPool().connectionCount());
if (client.interactionsClientOptions.streamHandlerExecutor() instanceof ExecutorService) {
assertTrue(((ExecutorService) client.interactionsClientOptions.streamHandlerExecutor()).isShutdown());
assertTrue(
((ExecutorService) client.interactionsClientOptions.streamHandlerExecutor())
.isShutdown());
}
}

@Test
public void testCloseClient_withExecutorService() {
ExecutorService customExecutor = java.util.concurrent.Executors.newSingleThreadExecutor();
HttpApiClient client =
new HttpApiClient(
Optional.empty(),
Optional.of(PROJECT),
Optional.of(LOCATION),
Optional.of(CREDENTIALS),
Optional.empty(),
Optional.of(ClientOptions.builder().streamHandlerExecutor(customExecutor).build()));

client.close();

assertTrue(customExecutor.isShutdown());
}

@Test
public void testStreamHandlerExecutorPassthrough() {
java.util.concurrent.Executor customExecutor = Runnable::run;
HttpApiClient client =
new HttpApiClient(
Optional.empty(),
Optional.of(PROJECT),
Optional.of(LOCATION),
Optional.of(CREDENTIALS),
Optional.empty(),
Optional.of(ClientOptions.builder().streamHandlerExecutor(customExecutor).build()));

assertEquals(customExecutor, client.interactionsClientOptions.streamHandlerExecutor());
}
}
Loading