Skip to content

CopilotChatClient

NexusLabs.Needlr.Copilot

CopilotChatClient Class

Microsoft.Extensions.AI.IChatClient implementation that calls the GitHub Copilot chat completions API directly (raw model endpoint, no CLI agent harness). Only tools explicitly provided via Microsoft.Extensions.AI.ChatOptions.Tools are sent; no built-in Copilot CLI tools are injected.

public sealed class CopilotChatClient : Microsoft.Extensions.AI.IChatClient, System.IDisposable

Inheritance System.Object 🡒 CopilotChatClient

Implements Microsoft.Extensions.AI.IChatClient, System.IDisposable

Example

// Minimal usage — auto-discovers token from Copilot CLI login:
IChatClient client = new CopilotChatClient();

// With explicit model and token:
IChatClient client = new CopilotChatClient(new CopilotChatClientOptions
{
    DefaultModel = "gpt-5.4",
    GitHubToken = "gho_xxx",
});

Remarks

Authentication follows a two-step flow: a GitHub OAuth token (discovered from apps.json, environment variables, or an explicit value) is exchanged for a short-lived Copilot API bearer token via the internal GitHub API endpoint.

Plug this into Needlr's agent framework via .UsingChatClient(new CopilotChatClient()) — no Copilot-specific syringe extensions required.

Constructors

CopilotChatClient(CopilotChatClientOptions, HttpClient) Constructor

Creates a new CopilotChatClient with optional configuration and HTTP client. Token discovery uses TokenSource.

public CopilotChatClient(NexusLabs.Needlr.Copilot.CopilotChatClientOptions? options=null, System.Net.Http.HttpClient? httpClient=null);

Parameters

options CopilotChatClientOptions

Configuration options. Uses defaults when null.

httpClient System.Net.Http.HttpClient

Optional HTTP client (shared with token provider). Created internally if null.

CopilotChatClient(ICopilotTokenProvider, CopilotChatClientOptions, HttpClient) Constructor

Creates a new CopilotChatClient with a custom token provider.

public CopilotChatClient(NexusLabs.Needlr.Copilot.ICopilotTokenProvider tokenProvider, NexusLabs.Needlr.Copilot.CopilotChatClientOptions? options=null, System.Net.Http.HttpClient? httpClient=null);

Parameters

tokenProvider ICopilotTokenProvider

Supplies Copilot API bearer tokens.

options CopilotChatClientOptions

Configuration options. Uses defaults when null.

httpClient System.Net.Http.HttpClient

Optional HTTP client. Created internally if null.

Methods

CopilotChatClient.GetResponseAsync(IEnumerable<ChatMessage>, ChatOptions, CancellationToken) Method

Sends chat messages and returns the response.

public System.Threading.Tasks.Task<Microsoft.Extensions.AI.ChatResponse> GetResponseAsync(System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage> chatMessages, Microsoft.Extensions.AI.ChatOptions? options=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

chatMessages System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage>

options Microsoft.Extensions.AI.ChatOptions

The chat options with which to configure the request.

cancellationToken System.Threading.CancellationToken

The System.Threading.CancellationToken to monitor for cancellation requests. The default is System.Threading.CancellationToken.None.

Implements GetResponseAsync(IEnumerable<ChatMessage>, ChatOptions, CancellationToken)

Returns

System.Threading.Tasks.Task<Microsoft.Extensions.AI.ChatResponse>
The response messages generated by the client.

Exceptions

System.ArgumentNullException
messages is null.

CopilotChatClient.GetService(Type, object) Method

Asks the Microsoft.Extensions.AI.IChatClient for an object of the specified type serviceType.

public object? GetService(System.Type serviceType, object? key=null);

Parameters

serviceType System.Type

The type of object being requested.

key System.Object

Implements GetService(Type, object)

Returns

System.Object
The found object, otherwise null.

Exceptions

System.ArgumentNullException
serviceType is null.

Remarks

The purpose of this method is to allow for the retrieval of strongly typed services that might be provided by the Microsoft.Extensions.AI.IChatClient, including itself or any services it might be wrapping. For example, to access the Microsoft.Extensions.AI.ChatClientMetadata for the instance, Microsoft.Extensions.AI.IChatClient.GetService(System.Type,System.Object) may be used to request it.

CopilotChatClient.GetStreamingResponseAsync(IEnumerable<ChatMessage>, ChatOptions, CancellationToken) Method

Sends chat messages and streams the response.

public System.Collections.Generic.IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate> GetStreamingResponseAsync(System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage> chatMessages, Microsoft.Extensions.AI.ChatOptions? options=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

chatMessages System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage>

options Microsoft.Extensions.AI.ChatOptions

The chat options with which to configure the request.

cancellationToken System.Threading.CancellationToken

The System.Threading.CancellationToken to monitor for cancellation requests. The default is System.Threading.CancellationToken.None.

Implements GetStreamingResponseAsync(IEnumerable<ChatMessage>, ChatOptions, CancellationToken)

Returns

System.Collections.Generic.IAsyncEnumerable<Microsoft.Extensions.AI.ChatResponseUpdate>
The response messages generated by the client.

Exceptions

System.ArgumentNullException
messages is null.