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
14 changes: 14 additions & 0 deletions dotnet/src/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ public async Task<CopilotSession> CreateSessionAsync(SessionConfig? config = nul
{
var connection = await EnsureConnectedAsync(cancellationToken);

if (!string.IsNullOrEmpty(config?.Model))
{
// ListModelsAsync caches results after the first call, so this validation has minimal overhead
var availableModels = await ListModelsAsync(cancellationToken).ConfigureAwait(false);
var validModelIds = new HashSet<string>(availableModels.Select(m => m.Id), StringComparer.OrdinalIgnoreCase);

if (!validModelIds.Contains(config.Model))
{
throw new ArgumentException(
$"Invalid model '{config.Model}'. Available models: {string.Join(", ", validModelIds)}",
nameof(config));
}
}

var hasHooks = config?.Hooks != null && (
config.Hooks.OnPreToolUse != null ||
config.Hooks.OnPostToolUse != null ||
Expand Down
17 changes: 16 additions & 1 deletion dotnet/test/SessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SessionTests(E2ETestFixture fixture, ITestOutputHelper output) : E2
[Fact]
public async Task ShouldCreateAndDestroySessions()
{
var session = await Client.CreateSessionAsync(new SessionConfig { Model = "fake-test-model" });
var session = await Client.CreateSessionAsync(new SessionConfig { Model = "claude-sonnet-4.5" });

Assert.Matches(@"^[a-f0-9-]+$", session.SessionId);

Expand Down Expand Up @@ -395,4 +395,19 @@ public async Task Should_Create_Session_With_Custom_Config_Dir()
Assert.NotNull(assistantMessage);
Assert.Contains("2", assistantMessage!.Data.Content);
}

[Fact]
public async Task CreateSessionAsync_WithInvalidModel_ThrowsArgumentException()
{
var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>
{
await Client.CreateSessionAsync(new SessionConfig
{
Model = "INVALID_MODEL_THAT_DOES_NOT_EXIST"
});
});

Assert.Contains("Invalid model", exception.Message);
Assert.Contains("INVALID_MODEL_THAT_DOES_NOT_EXIST", exception.Message);
}
}