Copilot.Client (Copilot SDK v2.3.0)

Copy Markdown

Main client for interacting with the Copilot CLI.

Copilot.Client is a GenServer that manages the lifecycle of the CLI subprocess, maintains a JSON-RPC connection over stdio, and provides session management (create, resume, delete, list).

Example

{:ok, client} = Copilot.Client.start_link()
{:ok, session} = Copilot.Client.create_session(client, %Copilot.Types.SessionConfig{})
:ok = Copilot.Session.send(session, %Copilot.Types.MessageOptions{prompt: "Hello!"})
Copilot.Session.destroy(session)
Copilot.Client.stop(client)

Summary

Functions

Returns a specification to start this module under a supervisor.

Create a new conversation session.

Delete a session and its data from disk.

Get current authentication status.

Get the foreground session ID.

Get the last session ID.

Get metadata for a session by ID.

Get the current connection state.

Get CLI status including version and protocol information.

List available models. Results are cached after the first call.

List all available sessions.

Send a ping to the server.

Set the foreground session ID.

Sets the session filesystem provider configuration.

Start the CLI server and establish a JSON-RPC connection.

Stop the CLI server and clean up all sessions.

Subscribe to session lifecycle events.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

create_session(client, config \\ %SessionConfig{})

@spec create_session(GenServer.server(), Copilot.Types.SessionConfig.t()) ::
  {:ok, pid()} | {:error, any()}

Create a new conversation session.

Returns {:ok, session_pid} where session_pid is a Copilot.Session GenServer.

delete_session(client, session_id)

@spec delete_session(GenServer.server(), String.t()) :: :ok | {:error, any()}

Delete a session and its data from disk.

get_auth_status(client)

@spec get_auth_status(GenServer.server()) ::
  {:ok, Copilot.Types.GetAuthStatusResponse.t()} | {:error, any()}

Get current authentication status.

get_foreground_session_id(client)

@spec get_foreground_session_id(GenServer.server()) ::
  {:ok, String.t() | nil} | {:error, any()}

Get the foreground session ID.

get_last_session_id(client)

@spec get_last_session_id(GenServer.server()) ::
  {:ok, String.t() | nil} | {:error, any()}

Get the last session ID.

get_session_metadata(client, session_id)

@spec get_session_metadata(GenServer.server(), String.t()) ::
  {:ok, map()} | {:error, any()}

Get metadata for a session by ID.

get_state(client)

@spec get_state(GenServer.server()) ::
  :disconnected | :connecting | :connected | :error

Get the current connection state.

get_status(client)

@spec get_status(GenServer.server()) ::
  {:ok, Copilot.Types.GetStatusResponse.t()} | {:error, any()}

Get CLI status including version and protocol information.

list_models(client)

@spec list_models(GenServer.server()) ::
  {:ok, [Copilot.Types.ModelInfo.t()]} | {:error, any()}

List available models. Results are cached after the first call.

list_sessions(client)

@spec list_sessions(GenServer.server()) ::
  {:ok, [Copilot.Types.SessionMetadata.t()]} | {:error, any()}

List all available sessions.

ping(client, message \\ nil)

@spec ping(GenServer.server(), String.t() | nil) ::
  {:ok, Copilot.Types.PingResponse.t()} | {:error, any()}

Send a ping to the server.

Returns {:ok, %PingResponse{}} or {:error, reason}.

resume_session(client, session_id, config \\ %ResumeSessionConfig{})

@spec resume_session(
  GenServer.server(),
  String.t(),
  Copilot.Types.ResumeSessionConfig.t()
) ::
  {:ok, pid()} | {:error, any()}

Resume an existing session by ID.

Returns {:ok, session_pid}.

set_foreground_session_id(client, session_id)

@spec set_foreground_session_id(GenServer.server(), String.t()) ::
  :ok | {:error, any()}

Set the foreground session ID.

set_session_fs_provider(client, config \\ %{})

@spec set_session_fs_provider(GenServer.server(), map()) :: :ok | {:error, any()}

Sets the session filesystem provider configuration.

Options

  • :initial_cwd - Optional initial working directory.
  • :session_state_path - Optional path for session state persistence.
  • :conventions - Optional list of convention strings.

start(client)

@spec start(GenServer.server()) :: :ok | {:error, any()}

Start the CLI server and establish a JSON-RPC connection.

start_link(options \\ %CopilotClientOptions{}, gen_opts \\ [])

Start the Copilot client GenServer.

Options

All fields from Copilot.Types.CopilotClientOptions are supported. Pass name: <name> in gen_opts to register the process.

stop(client)

@spec stop(GenServer.server()) :: :ok

Stop the CLI server and clean up all sessions.

subscribe_lifecycle(client, handler)

@spec subscribe_lifecycle(
  GenServer.server(),
  (Copilot.Types.SessionLifecycleEvent.t() -> any())
) ::
  reference()

Subscribe to session lifecycle events.

Returns a reference that can be used with unsubscribe_lifecycle/2.