Add CallToolResult.ToChatMessage extension method (#575)
To simplify non-AIFunction usage getting a ChatMessage with the appropriate FunctionResultContent.
This commit is contained in:
parent
92fd8b246f
commit
4e290f8de9
1 changed files with 51 additions and 0 deletions
|
|
@ -39,6 +39,29 @@ public static class AIContentExtensions
|
|||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="CallToolResult"/> to a <see cref="ChatMessage"/> object.
|
||||
/// </summary>
|
||||
/// <param name="result">The tool result to convert.</param>
|
||||
/// <param name="callId">The identifier for the function call request that triggered the tool invocation.</param>
|
||||
/// <returns>A <see cref="ChatMessage"/> object created from the tool result.</returns>
|
||||
/// <remarks>
|
||||
/// This method transforms a protocol-specific <see cref="CallToolResult"/> from the Model Context Protocol
|
||||
/// into a standard <see cref="ChatMessage"/> object that can be used with AI client libraries. It produces a
|
||||
/// <see cref="ChatRole.Tool"/> message containing a <see cref="FunctionResultContent"/> with result as a
|
||||
/// serialized <see cref="JsonElement"/>.
|
||||
/// </remarks>
|
||||
public static ChatMessage ToChatMessage(this CallToolResult result, string callId)
|
||||
{
|
||||
Throw.IfNull(result);
|
||||
Throw.IfNull(callId);
|
||||
|
||||
return new(ChatRole.Tool, [new FunctionResultContent(callId, JsonSerializer.SerializeToElement(result, McpJsonUtilities.JsonContext.Default.CallToolResult))
|
||||
{
|
||||
RawRepresentation = result,
|
||||
}]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="GetPromptResult"/> to a list of <see cref="ChatMessage"/> objects.
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Reference in a new issue