Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,28 @@ public override ValueTask InvokedAsync(InvokedContext context, CancellationToken
{
Throw.IfNull(context);

// Add the request and response messages to the recent messages queue
foreach (var message in context.RequestMessages)
// Only add messages to the recent messages queue if the invocation succeeded
if (context.InvokeException is null)
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot Instead of having an if around all the code, just have an if that checks if InvokeException is not null and return if so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored to use early return pattern in commit 70b0e76.

{
this._recentMessages.Enqueue(message);
}

if (context.ResponseMessages is not null)
{
foreach (var message in context.ResponseMessages)
// Add the request and response messages to the recent messages queue
foreach (var message in context.RequestMessages)
{
this._recentMessages.Enqueue(message);
}
}

// If there are more messages than the configured limit, remove the oldest ones
while (this._recentMessages.Count > this._options.NumberOfRecentMessagesInContext)
{
this._recentMessages.TryDequeue(out _);
if (context.ResponseMessages is not null)
{
foreach (var message in context.ResponseMessages)
{
this._recentMessages.Enqueue(message);
}
}

// If there are more messages than the configured limit, remove the oldest ones
while (this._recentMessages.Count > this._options.NumberOfRecentMessagesInContext)
{
this._recentMessages.TryDequeue(out _);
}
}

return default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,46 @@ public async Task ContextEmbeddingValueProvider_ReceivesRecentAndNewMessages_Asy
Assert.Equal("msg5", capturedNewMessages.ElementAt(1).Text);
}

[Fact]
public async Task InvokedAsync_ShouldNotAddMessages_WhenExceptionIsPresent_Async()
{
// Arrange
var functions = new List<AIFunction> { CreateFunction("f1") };
var options = new ContextualFunctionProviderOptions
{
NumberOfRecentMessagesInContext = 5
};

var provider = new ContextualFunctionProvider(
vectorStore: this._vectorStoreMock.Object,
vectorDimensions: 1536,
functions: functions,
maxNumberOfFunctions: 5,
options: options);

var message1 = new ChatMessage() { Contents = [new TextContent("msg1")] };
var message2 = new ChatMessage() { Contents = [new TextContent("msg2")] };
var message3 = new ChatMessage() { Contents = [new TextContent("msg3")] };

// Add successful invocations first
await provider.InvokedAsync(new AIContextProvider.InvokedContext([message1], null) { ResponseMessages = [] });
await provider.InvokedAsync(new AIContextProvider.InvokedContext([message2], null) { ResponseMessages = [] });

// Act - Add an invocation with an exception
await provider.InvokedAsync(new AIContextProvider.InvokedContext([message3], null)
{
ResponseMessages = [],
InvokeException = new InvalidOperationException("Test exception")
});

// Assert - The exception-causing message should not be added to recent messages
var invokingContext = new AIContextProvider.InvokingContext([new() { Contents = [new TextContent("new message")] }]);
await provider.InvokingAsync(invokingContext);

var expected = string.Join(Environment.NewLine, ["msg1", "msg2", "new message"]);
this._collectionMock.Verify(c => c.SearchAsync(expected, It.IsAny<int>(), null, It.IsAny<CancellationToken>()), Times.Once);
}

private static AIFunction CreateFunction(string name, string description = "")
{
return AIFunctionFactory.Create(() => { }, name, description);
Expand Down