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
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ await this.OnAutoFunctionInvocationAsync(
private void AddFunctionCallResultToChatHistory(ChatHistory chatHistory, FunctionResultContext resultContext)
{
var message = new ChatMessageContent(role: AuthorRole.Tool, content: resultContext.Result);
message.Metadata = resultContext.Context.Result.Metadata;
message.Items.Add(this.GenerateResultContent(resultContext));
chatHistory.Add(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading;
Expand Down Expand Up @@ -855,6 +856,54 @@ public void ItShouldSerializeFunctionResultsWithStringProperties()
Assert.Equal("{\"Text\":\"テスト\"}", result);
}

[Fact]
public async Task ItShouldPropagateFunctionResultMetadataToChatHistoryAsync()
{
// Arrange
var expectedMetadata = new Dictionary<string, object?>
{
["key1"] = "value1",
["key2"] = 42
};

var function1 = KernelFunctionFactory.CreateFromMethod((string parameter) => parameter, "Function1");
var plugin = KernelPluginFactory.CreateFromFunctions("MyPlugin", [function1]);

var kernel = CreateKernel(plugin, async (context, next) =>
{
await next(context);

// Filter sets metadata on the FunctionResult
context.Result = new FunctionResult(context.Function, context.Result.GetValue<object>(), context.Result.Culture, expectedMetadata);
});

var chatMessageContent = new ChatMessageContent();
chatMessageContent.Items.Add(new FunctionCallContent("Function1", "MyPlugin", arguments: new KernelArguments() { ["parameter"] = "function1-result" }));

var chatHistory = new ChatHistory();

// Act
await this._sut.ProcessFunctionCallsAsync(
chatMessageContent: chatMessageContent,
executionSettings: this._promptExecutionSettings,
chatHistory: chatHistory,
requestIndex: 0,
checkIfFunctionAdvertised: (_) => true,
options: this._functionChoiceBehaviorOptions,
kernel: kernel!,
isStreaming: false,
cancellationToken: CancellationToken.None);

// Assert
Assert.Equal(2, chatHistory.Count);

var toolMessage = chatHistory[1]; // First is the assistant message, second is the tool result
Assert.Equal(AuthorRole.Tool, toolMessage.Role);
Assert.NotNull(toolMessage.Metadata);
Assert.Equal("value1", toolMessage.Metadata["key1"]);
Assert.Equal(42, toolMessage.Metadata["key2"]);
}

[Fact]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test only covers the case where metadata is explicitly set by a filter. Consider adding analogous test without the filter (or with a no-op filter) to assert that toolMessage.Metadata is null when no metadata is provided, validating the change doesn't regress default behavior.

Suggested change
[Fact]

public async Task ItShouldPassPromptExecutionSettingsToAutoFunctionInvocationFilterAsync()
{
Expand Down
Loading