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
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.Agents.AI.Hosting.OpenAI.Responses.Models;
using Microsoft.Extensions.AI;

Expand Down Expand Up @@ -69,6 +71,13 @@ private static string MediaTypeToAudioFormat(string mediaType) =>
new DataContent(inputAudio.Data, AudioFormatToMediaType(inputAudio.Format)),
ItemContentOutputAudio outputAudio =>
new DataContent(outputAudio.Data, "audio/*"),
ItemContentFunctionApprovalResponse functionApprovalResponse => new ToolApprovalResponseContent(
functionApprovalResponse.RequestId,
functionApprovalResponse.Approved,
new FunctionCallContent(
functionApprovalResponse.FunctionCall.Id,
functionApprovalResponse.FunctionCall.Name,
ParseArguments(functionApprovalResponse.FunctionCall.Arguments))),

_ => null
};
Expand Down Expand Up @@ -159,4 +168,36 @@ DataContent audioData when audioData.HasTopLevelMediaType("audio") =>

return null;
}

private static Dictionary<string, object?>? ParseArguments(JsonElement argumentsJson)
{
if (argumentsJson.ValueKind is not JsonValueKind.Object)
{
return null;
}

try
{
using var doc = JsonDocument.Parse(argumentsJson.GetRawText());
var result = new Dictionary<string, object?>();
foreach (var property in doc.RootElement.EnumerateObject())
{
result[property.Name] = property.Value.ValueKind switch
{
JsonValueKind.String => property.Value.GetString(),
JsonValueKind.Number => property.Value.GetDouble(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Null => null,
_ => property.Value.GetRawText()
};
}

return result;
}
catch (JsonException)
{
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ internal enum FunctionToolCallOutputItemResourceStatus
[JsonDerivedType(typeof(ItemContentOutputText), "output_text")]
[JsonDerivedType(typeof(ItemContentOutputAudio), "output_audio")]
[JsonDerivedType(typeof(ItemContentRefusal), "refusal")]
[JsonDerivedType(typeof(ItemContentFunctionApprovalResponse), "function_approval_response")]
internal abstract class ItemContent
{
/// <summary>
Expand Down Expand Up @@ -443,6 +444,58 @@ internal sealed class ItemContentRefusal : ItemContent
public required string Refusal { get; init; }
}

/// <summary>
/// Represents function call information for approval response item.
/// </summary>
internal sealed class FunctionCall
{
Comment on lines +447 to +451
/// <summary>
/// Gets or initializes the function call ID.
/// </summary>
[JsonPropertyName("id")]
public required string Id { get; init; }

/// <summary>
/// Gets or initializes the function name.
/// </summary>
[JsonPropertyName("name")]
public required string Name { get; init; }

/// <summary>
/// Gets or initializes the function arguments.
/// </summary>
[JsonPropertyName("arguments")]
public required JsonElement Arguments { get; init; }
}

/// <summary>
/// Function approval response content.
/// </summary>
internal sealed class ItemContentFunctionApprovalResponse : ItemContent
{
/// <inheritdoc/>
[JsonIgnore]
public override string Type => "function_approval_response";

/// <summary>
/// Gets or initializes the value indicating whether the function call was approved for execution.
/// </summary>
[JsonPropertyName("approved")]
public required bool Approved { get; init; }

/// <summary>
/// Gets or initializes the function call that was subject to approval.
/// </summary>
[JsonPropertyName("function_call")]
public required FunctionCall FunctionCall { get; init; }

/// <summary>
/// Gets or initializes the unique identifier that correlates this response with its corresponding request.
/// </summary>
[JsonPropertyName("request_id")]
public required string RequestId { get; init; }
}

// Additional ItemResource types from TypeSpec

/// <summary>
Expand Down