/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. *--------------------------------------------------------------------------------------------*/ package com.github.copilot.sdk.json; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.function.Supplier; import com.fasterxml.jackson.annotation.JsonInclude; /** * Configuration options for creating a * {@link com.github.copilot.sdk.CopilotClient}. *
* This class provides a fluent API for configuring how the client connects to * and manages the Copilot CLI server. All setter methods return {@code this} * for method chaining. * *
{@code
* var options = new CopilotClientOptions().setCliPath("/usr/local/bin/copilot").setLogLevel("debug")
* .setAutoStart(true);
*
* var client = new CopilotClient(options);
* }
*
* @see com.github.copilot.sdk.CopilotClient
* @since 1.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CopilotClientOptions {
@Deprecated
private boolean autoRestart;
private boolean autoStart = true;
private String[] cliArgs;
private String cliPath;
private String cliUrl;
private String cwd;
private Map* These arguments are prepended before SDK-managed flags. * * @param cliArgs * the extra arguments to pass * @return this options instance for method chaining */ public CopilotClientOptions setCliArgs(String[] cliArgs) { this.cliArgs = cliArgs; return this; } /** * Gets the path to the Copilot CLI executable. * * @return the CLI path, or {@code null} to use "copilot" from PATH */ public String getCliPath() { return cliPath; } /** * Sets the path to the Copilot CLI executable. * * @param cliPath * the path to the CLI executable, or {@code null} to use "copilot" * from PATH * @return this options instance for method chaining */ public CopilotClientOptions setCliPath(String cliPath) { this.cliPath = cliPath; return this; } /** * Gets the URL of an existing CLI server to connect to. * * @return the CLI server URL, or {@code null} to spawn a new process */ public String getCliUrl() { return cliUrl; } /** * Sets the URL of an existing CLI server to connect to. *
* When provided, the client will not spawn a CLI process but will connect to * the specified URL instead. Format: "host:port" or "http://host:port". *
* Note: This is mutually exclusive with
* {@link #setUseStdio(boolean)} and {@link #setCliPath(String)}.
*
* @param cliUrl
* the CLI server URL to connect to
* @return this options instance for method chaining
*/
public CopilotClientOptions setCliUrl(String cliUrl) {
this.cliUrl = cliUrl;
return this;
}
/**
* Gets the working directory for the CLI process.
*
* @return the working directory path
*/
public String getCwd() {
return cwd;
}
/**
* Sets the working directory for the CLI process.
*
* @param cwd
* the working directory path
* @return this options instance for method chaining
*/
public CopilotClientOptions setCwd(String cwd) {
this.cwd = cwd;
return this;
}
/**
* Gets the environment variables for the CLI process.
*
* @return the environment variables map
*/
public Map
* When set, these environment variables replace the inherited environment.
*
* @param environment
* the environment variables map
* @return this options instance for method chaining
*/
public CopilotClientOptions setEnvironment(Map
* When provided, the SDK uses this executor for all internal
* {@code CompletableFuture} combinators instead of the default
* {@code ForkJoinPool.commonPool()}. This allows callers to isolate SDK work
* onto a dedicated thread pool or integrate with container-managed threading.
*
* @param executor
* the executor to use, or {@code null} for the default
* @return this options instance for fluent chaining
*/
public CopilotClientOptions setExecutor(Executor executor) {
this.executor = executor;
return this;
}
/**
* Gets the GitHub token for authentication.
*
* @return the GitHub token, or {@code null} to use other authentication methods
*/
public String getGitHubToken() {
return gitHubToken;
}
/**
* Sets the GitHub token to use for authentication.
*
* When provided, the token is passed to the CLI server via environment
* variable. This takes priority over other authentication methods.
*
* @param gitHubToken
* the GitHub token
* @return this options instance for method chaining
*/
public CopilotClientOptions setGitHubToken(String gitHubToken) {
this.gitHubToken = gitHubToken;
return this;
}
/**
* Gets the GitHub token for authentication.
*
* @return the GitHub token, or {@code null} to use other authentication methods
* @deprecated Use {@link #getGitHubToken()} instead.
*/
@Deprecated
public String getGithubToken() {
return gitHubToken;
}
/**
* Sets the GitHub token to use for authentication.
*
* @param githubToken
* the GitHub token
* @return this options instance for method chaining
* @deprecated Use {@link #setGitHubToken(String)} instead.
*/
@Deprecated
public CopilotClientOptions setGithubToken(String githubToken) {
this.gitHubToken = githubToken;
return this;
}
/**
* Gets the log level for the CLI process.
*
* @return the log level (default: "info")
*/
public String getLogLevel() {
return logLevel;
}
/**
* Sets the log level for the CLI process.
*
* Valid levels include: "error", "warn", "info", "debug", "trace".
*
* @param logLevel
* the log level
* @return this options instance for method chaining
*/
public CopilotClientOptions setLogLevel(String logLevel) {
this.logLevel = logLevel;
return this;
}
/**
* Gets the custom handler for listing available models.
*
* @return the handler, or {@code null} if not set
*/
public Supplier
* When provided, {@code listModels()} calls this handler instead of querying
* the CLI server. Useful in BYOK (Bring Your Own Key) mode to return models
* available from your custom provider.
*
* @param onListModels
* the handler that returns the list of available models
* @return this options instance for method chaining
*/
public CopilotClientOptions setOnListModels(Supplier
* This is only used when {@link #isUseStdio()} is {@code false}.
*
* @param port
* the port number, or 0 for a random port
* @return this options instance for method chaining
*/
public CopilotClientOptions setPort(int port) {
this.port = port;
return this;
}
/**
* Gets the OpenTelemetry configuration for the CLI server.
*
* @return the telemetry config, or {@code null}
* @since 1.2.0
*/
public TelemetryConfig getTelemetry() {
return telemetry;
}
/**
* Sets the OpenTelemetry configuration for the CLI server.
*
* When set to a non-{@code null} value, the CLI server is started with
* OpenTelemetry instrumentation enabled using the provided settings.
*
* @param telemetry
* the telemetry configuration
* @return this options instance for method chaining
* @since 1.2.0
*/
public CopilotClientOptions setTelemetry(TelemetryConfig telemetry) {
this.telemetry = telemetry;
return this;
}
/**
* Returns whether to use the logged-in user for authentication.
*
* @return {@code true} to use logged-in user auth, {@code false} to use only
* explicit tokens, or {@code null} to use default behavior
*/
public Boolean getUseLoggedInUser() {
return useLoggedInUser;
}
/**
* Sets whether to use the logged-in user for authentication.
*
* When true, the CLI server will attempt to use stored OAuth tokens or gh CLI
* auth. When false, only explicit tokens (gitHubToken or environment variables)
* are used. Default: true (but defaults to false when gitHubToken is provided).
*
* @param useLoggedInUser
* {@code true} to use logged-in user auth, {@code false} otherwise
* @return this options instance for method chaining
*/
public CopilotClientOptions setUseLoggedInUser(Boolean useLoggedInUser) {
this.useLoggedInUser = useLoggedInUser;
return this;
}
/**
* Returns whether to use stdio transport instead of TCP.
*
* @return {@code true} to use stdio (default), {@code false} to use TCP
*/
public boolean isUseStdio() {
return useStdio;
}
/**
* Sets whether to use stdio transport instead of TCP.
*
* Stdio transport is more efficient and is the default. TCP transport can be
* useful for debugging or connecting to remote servers.
*
* @param useStdio
* {@code true} to use stdio, {@code false} to use TCP
* @return this options instance for method chaining
*/
public CopilotClientOptions setUseStdio(boolean useStdio) {
this.useStdio = useStdio;
return this;
}
/**
* Creates a shallow clone of this {@code CopilotClientOptions} instance.
*
* Array properties (like {@code cliArgs}) are copied into new arrays so that
* modifications to the clone do not affect the original. The
* {@code environment} map is also copied to a new map instance. Other
* reference-type properties are shared between the original and clone.
*
* @return a clone of this options instance
*/
@Override
public CopilotClientOptions clone() {
CopilotClientOptions copy = new CopilotClientOptions();
copy.autoRestart = this.autoRestart;
copy.autoStart = this.autoStart;
copy.cliArgs = this.cliArgs != null ? this.cliArgs.clone() : null;
copy.cliPath = this.cliPath;
copy.cliUrl = this.cliUrl;
copy.cwd = this.cwd;
copy.environment = this.environment != null ? new java.util.HashMap<>(this.environment) : null;
copy.executor = this.executor;
copy.gitHubToken = this.gitHubToken;
copy.logLevel = this.logLevel;
copy.onListModels = this.onListModels;
copy.port = this.port;
copy.telemetry = this.telemetry;
copy.useLoggedInUser = this.useLoggedInUser;
copy.useStdio = this.useStdio;
return copy;
}
}