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
40 changes: 26 additions & 14 deletions src/ClaudeCodeSdk.MAF/ClaudeCodeAIAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ protected override async Task<AgentResponse> RunCoreAsync(

await foreach (var claudeMessage in asyncEnumMsgs)
{
if (client != null && cancellationToken.IsCancellationRequested)
{
await InterruptAsync(client);
}

if (claudeMessage is ResultMessage resultMessage)
{
usageDetails = resultMessage.ToUsageDetails();
Expand Down Expand Up @@ -150,7 +145,7 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
if (!string.IsNullOrWhiteSpace(content))
{
var (asyncEnumMsgs, client) = await SendUserInput(claudeThread, content, cancellationToken);

if (client != null && cancellationToken.IsCancellationRequested)
{
await InterruptAsync(client);
Expand All @@ -160,11 +155,6 @@ protected override async IAsyncEnumerable<AgentResponseUpdate> RunCoreStreamingA
// Receive and yield responses
await foreach (var claudeMessage in asyncEnumMsgs)
{
if (client != null && cancellationToken.IsCancellationRequested)
{
await InterruptAsync(client);
}

var update = claudeMessage.ToAgentRunResponseUpdate();
if (update != null)
{
Expand Down Expand Up @@ -203,15 +193,37 @@ private async Task InterruptAsync(ClaudeSdkClient client)
}
else
{
client = await _clientManager.GetClientAsync(claudeThread, cancellationToken);
client = await _clientManager.GetClientAsync(claudeThread, CancellationToken.None);

await client.QueryAsync(content,
sessionId: claudeThread.SessionId.ToString(),
cancellationToken: cancellationToken);
cancellationToken: CancellationToken.None);

asyncEnumMsgs = client.ReceiveResponseAsync(cancellationToken);
asyncEnumMsgs = client.ReceiveResponseAsync(CancellationToken.None);
}

//
var interruptRequested = 0;
using var cancellationRegistration = cancellationToken.Register(() =>
{
if (client == null) return;
if (Interlocked.Exchange(ref interruptRequested, 1) != 0)
return;

_ = Task.Run(async () =>
{
try
{
await InterruptAsync(client);
}
catch (Exception ex)
{
_logger?.LogDebug(ex, "Failed to interrupt Claude SDK client during streaming cancellation");
}
});
});


return (asyncEnumMsgs, client);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ClaudeCodeSdk/ClaudeSDKClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public async Task InterruptAsync(CancellationToken cancellationToken = default)

ConnectStatus = ConnectStatus.DisConnecting;
await _process.InterruptAsync();
await this.DisposeAsync();
ConnectStatus = ConnectStatus.DisConnected;
}

/// <summary>
Expand Down Expand Up @@ -171,4 +171,4 @@ public async ValueTask DisposeAsync()
}
GC.SuppressFinalize(this);
}
}
}
Loading