Skip to content
Merged
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
39 changes: 39 additions & 0 deletions dotnet/src/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ async Task<Connection> StartCoreAsync(CancellationToken ct)

// Verify protocol version compatibility
await VerifyProtocolVersionAsync(connection, ct);
await ConfigureSessionFsAsync(ct);

_logger.LogInformation("Copilot client connected");
return connection;
Expand Down Expand Up @@ -474,6 +475,7 @@ public async Task<CopilotSession> CreateSessionAsync(SessionConfig config, Cance
{
session.On(config.OnEvent);
}
ConfigureSessionFsHandlers(session, config.CreateSessionFsHandler);
_sessions[sessionId] = session;

try
Expand Down Expand Up @@ -594,6 +596,7 @@ public async Task<CopilotSession> ResumeSessionAsync(string sessionId, ResumeSes
{
session.On(config.OnEvent);
}
ConfigureSessionFsHandlers(session, config.CreateSessionFsHandler);
_sessions[sessionId] = session;

try
Expand Down Expand Up @@ -1078,6 +1081,37 @@ private Task<Connection> EnsureConnectedAsync(CancellationToken cancellationToke
return (Task<Connection>)StartAsync(cancellationToken);
}

private async Task ConfigureSessionFsAsync(CancellationToken cancellationToken)
{
if (_options.SessionFs is null)
{
return;
}

await Rpc.SessionFs.SetProviderAsync(
_options.SessionFs.InitialCwd,
_options.SessionFs.SessionStatePath,
_options.SessionFs.Conventions,
Comment on lines +1091 to +1094
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConfigureSessionFsAsync forwards SessionFs values directly to sessionFs.setProvider without validating required fields (non-empty InitialCwd/SessionStatePath) or guarding invalid enum values. Other SDKs in this PR validate early and throw a clear configuration error; adding equivalent validation here would prevent late/opaque failures from the RPC call.

Suggested change
await Rpc.SessionFs.SetProviderAsync(
_options.SessionFs.InitialCwd,
_options.SessionFs.SessionStatePath,
_options.SessionFs.Conventions,
var sessionFs = _options.SessionFs;
if (string.IsNullOrWhiteSpace(sessionFs.InitialCwd))
{
throw new InvalidOperationException(
"CopilotClientOptions.SessionFs.InitialCwd is required when CopilotClientOptions.SessionFs is configured.");
}
if (string.IsNullOrWhiteSpace(sessionFs.SessionStatePath))
{
throw new InvalidOperationException(
"CopilotClientOptions.SessionFs.SessionStatePath is required when CopilotClientOptions.SessionFs is configured.");
}
object? conventions = sessionFs.Conventions;
if (conventions is null || !System.Enum.IsDefined(conventions.GetType(), conventions))
{
throw new InvalidOperationException(
$"CopilotClientOptions.SessionFs.Conventions has an invalid value '{sessionFs.Conventions}'.");
}
await Rpc.SessionFs.SetProviderAsync(
sessionFs.InitialCwd,
sessionFs.SessionStatePath,
sessionFs.Conventions,

Copilot uses AI. Check for mistakes.
cancellationToken);
}

private void ConfigureSessionFsHandlers(CopilotSession session, Func<CopilotSession, ISessionFsHandler>? createSessionFsHandler)
{
if (_options.SessionFs is null)
{
return;
}

if (createSessionFsHandler is null)
{
throw new InvalidOperationException(
"CreateSessionFsHandler is required in the session config when CopilotClientOptions.SessionFs is configured.");
}

session.ClientSessionApis.SessionFs = createSessionFsHandler(session)
?? throw new InvalidOperationException("CreateSessionFsHandler returned null.");
}

private async Task VerifyProtocolVersionAsync(Connection connection, CancellationToken cancellationToken)
{
var maxVersion = SdkProtocolVersion.GetVersion();
Expand Down Expand Up @@ -1319,6 +1353,11 @@ private async Task<Connection> ConnectToServerAsync(Process? cliProcess, string?
rpc.AddLocalRpcMethod("userInput.request", handler.OnUserInputRequest);
rpc.AddLocalRpcMethod("hooks.invoke", handler.OnHooksInvoke);
rpc.AddLocalRpcMethod("systemMessage.transform", handler.OnSystemMessageTransform);
ClientSessionApiRegistration.RegisterClientSessionApiHandlers(rpc, sessionId =>
{
var session = GetSession(sessionId) ?? throw new ArgumentException($"Unknown session {sessionId}");
return session.ClientSessionApis;
});
rpc.StartListening();

// Transition state to Disconnected if the JSON-RPC connection drops
Expand Down
Loading
Loading