// Copyright (c) Microsoft. All rights reserved. using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.Agents.AI.Hosting.OpenAI.Responses.Converters; using Microsoft.Extensions.AI; namespace Microsoft.Agents.AI.Hosting.OpenAI.Responses.Models; /// /// Base class for all item resources (output items from a response). /// [JsonConverter(typeof(ItemResourceConverter))] internal abstract record ItemResource { /// /// The unique identifier for the item. /// [JsonPropertyName("id")] public string Id { get; init; } = string.Empty; /// /// The type of the item. /// [JsonPropertyName("type")] public abstract string Type { get; } } /// /// Base class for message item resources. /// [JsonConverter(typeof(ResponsesMessageItemResourceConverter))] internal abstract record ResponsesMessageItemResource : ItemResource { /// /// The constant item type identifier for message items. /// public const string ItemType = "message"; /// public override string Type => ItemType; /// /// The status of the message. /// [JsonPropertyName("status")] public ResponsesMessageItemResourceStatus Status { get; init; } /// /// The role of the message sender. /// [JsonPropertyName("role")] public abstract ChatRole Role { get; } } /// /// An assistant message item resource. /// internal sealed record ResponsesAssistantMessageItemResource : ResponsesMessageItemResource { /// /// The constant role type identifier for assistant messages. /// public const string RoleType = "assistant"; /// public override ChatRole Role => ChatRole.Assistant; /// /// The content of the message. /// [JsonPropertyName("content")] public required IList Content { get; init; } } /// /// A user message item resource. /// internal sealed record ResponsesUserMessageItemResource : ResponsesMessageItemResource { /// /// The constant role type identifier for user messages. /// public const string RoleType = "user"; /// public override ChatRole Role => ChatRole.User; /// /// The content of the message. /// [JsonPropertyName("content")] public required IList Content { get; init; } } /// /// A system message item resource. /// internal sealed record ResponsesSystemMessageItemResource : ResponsesMessageItemResource { /// /// The constant role type identifier for system messages. /// public const string RoleType = "system"; /// public override ChatRole Role => ChatRole.System; /// /// The content of the message. /// [JsonPropertyName("content")] public required IList Content { get; init; } } /// /// A developer message item resource. /// internal sealed record ResponsesDeveloperMessageItemResource : ResponsesMessageItemResource { /// /// The constant role type identifier for developer messages. /// public const string RoleType = "developer"; /// public override ChatRole Role => new(RoleType); /// /// The content of the message. /// [JsonPropertyName("content")] public required IList Content { get; init; } } /// /// A function tool call item resource. /// internal sealed record FunctionToolCallItemResource : ItemResource { /// /// The constant item type identifier for function call items. /// public const string ItemType = "function_call"; /// public override string Type => ItemType; /// /// The status of the function call. /// [JsonPropertyName("status")] public FunctionToolCallItemResourceStatus Status { get; init; } /// /// The call ID of the function. /// [JsonPropertyName("call_id")] public required string CallId { get; init; } /// /// The name of the function. /// [JsonPropertyName("name")] public required string Name { get; init; } /// /// The arguments to the function. /// [JsonPropertyName("arguments")] public required string Arguments { get; init; } } /// /// A function tool call output item resource. /// internal sealed record FunctionToolCallOutputItemResource : ItemResource { /// /// The constant item type identifier for function call output items. /// public const string ItemType = "function_call_output"; /// public override string Type => ItemType; /// /// The status of the function call output. /// [JsonPropertyName("status")] public FunctionToolCallOutputItemResourceStatus Status { get; init; } /// /// The call ID of the function. /// [JsonPropertyName("call_id")] public required string CallId { get; init; } /// /// The output of the function. /// [JsonPropertyName("output")] public required string Output { get; init; } } /// /// The status of a message item resource. /// [JsonConverter(typeof(SnakeCaseEnumConverter))] public enum ResponsesMessageItemResourceStatus { /// /// The message is completed. /// Completed, /// /// The message is in progress. /// InProgress, /// /// The message is incomplete. /// Incomplete } /// /// The status of a function tool call item resource. /// [JsonConverter(typeof(SnakeCaseEnumConverter))] public enum FunctionToolCallItemResourceStatus { /// /// The function call is completed. /// Completed, /// /// The function call is in progress. /// InProgress } /// /// The status of a function tool call output item resource. /// [JsonConverter(typeof(SnakeCaseEnumConverter))] public enum FunctionToolCallOutputItemResourceStatus { /// /// The function call output is completed. /// Completed } /// /// Base class for item content. /// [JsonPolymorphic(TypeDiscriminatorPropertyName = "type", UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization)] [JsonDerivedType(typeof(ItemContentInputText), "input_text")] [JsonDerivedType(typeof(ItemContentInputAudio), "input_audio")] [JsonDerivedType(typeof(ItemContentInputImage), "input_image")] [JsonDerivedType(typeof(ItemContentInputFile), "input_file")] [JsonDerivedType(typeof(ItemContentOutputText), "output_text")] [JsonDerivedType(typeof(ItemContentOutputAudio), "output_audio")] [JsonDerivedType(typeof(ItemContentRefusal), "refusal")] internal abstract record ItemContent { /// /// The type of the content. /// [JsonIgnore] public abstract string Type { get; } /// /// Gets or sets the original representation of the content, if applicable. /// This property is not serialized and is used for round-tripping conversions. /// [JsonIgnore] public object? RawRepresentation { get; set; } } /// /// Text input content. /// internal sealed record ItemContentInputText : ItemContent { /// [JsonIgnore] public override string Type => "input_text"; /// /// The text content. /// [JsonPropertyName("text")] public required string Text { get; init; } } /// /// Audio input content. /// internal sealed record ItemContentInputAudio : ItemContent { /// [JsonIgnore] public override string Type => "input_audio"; /// /// Base64-encoded audio data. /// [JsonPropertyName("data")] public required string Data { get; init; } /// /// The format of the audio data. /// [JsonPropertyName("format")] public required string Format { get; init; } } /// /// Image input content. /// internal sealed record ItemContentInputImage : ItemContent { /// [JsonIgnore] public override string Type => "input_image"; /// /// The URL of the image to be sent to the model. A fully qualified URL or base64 encoded image in a data URL. /// [JsonPropertyName("image_url")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1056:URI-like properties should not be strings", Justification = "OpenAI API uses string for image_url")] public string? ImageUrl { get; init; } /// /// The ID of the file to be sent to the model. /// [JsonPropertyName("file_id")] public string? FileId { get; init; } /// /// The detail level of the image to be sent to the model. One of 'high', 'low', or 'auto'. Defaults to 'auto'. /// [JsonPropertyName("detail")] public string? Detail { get; init; } } /// /// File input content. /// internal sealed record ItemContentInputFile : ItemContent { /// [JsonIgnore] public override string Type => "input_file"; /// /// The ID of the file to be sent to the model. /// [JsonPropertyName("file_id")] public string? FileId { get; init; } /// /// The name of the file to be sent to the model. /// [JsonPropertyName("filename")] public string? Filename { get; init; } /// /// The content of the file to be sent to the model. /// [JsonPropertyName("file_data")] public string? FileData { get; init; } } /// /// Text output content. /// internal sealed record ItemContentOutputText : ItemContent { /// [JsonIgnore] public override string Type => "output_text"; /// /// The text content. /// [JsonPropertyName("text")] public required string Text { get; init; } /// /// The annotations. /// [JsonPropertyName("annotations")] public required IList Annotations { get; init; } /// /// Log probability information for the output tokens. /// [JsonPropertyName("logprobs")] public IList Logprobs { get; init; } = []; } /// /// Audio output content. /// internal sealed record ItemContentOutputAudio : ItemContent { /// [JsonIgnore] public override string Type => "output_audio"; /// /// Base64-encoded audio data from the model. /// [JsonPropertyName("data")] public required string Data { get; init; } /// /// The transcript of the audio data from the model. /// [JsonPropertyName("transcript")] public required string Transcript { get; init; } } /// /// Refusal content. /// internal sealed record ItemContentRefusal : ItemContent { /// [JsonIgnore] public override string Type => "refusal"; /// /// The refusal explanation from the model. /// [JsonPropertyName("refusal")] public required string Refusal { get; init; } } // Additional ItemResource types from TypeSpec /// /// A file search tool call item resource. /// internal sealed record FileSearchToolCallItemResource : ItemResource { /// /// The constant item type identifier for file search call items. /// public const string ItemType = "file_search_call"; /// public override string Type => ItemType; /// /// The status of the file search. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// A computer tool call item resource. /// internal sealed record ComputerToolCallItemResource : ItemResource { /// /// The constant item type identifier for computer call items. /// public const string ItemType = "computer_call"; /// public override string Type => ItemType; /// /// The status of the computer call. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// A computer tool call output item resource. /// internal sealed record ComputerToolCallOutputItemResource : ItemResource { /// /// The constant item type identifier for computer call output items. /// public const string ItemType = "computer_call_output"; /// public override string Type => ItemType; /// /// The status of the computer call output. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// A web search tool call item resource. /// internal sealed record WebSearchToolCallItemResource : ItemResource { /// /// The constant item type identifier for web search call items. /// public const string ItemType = "web_search_call"; /// public override string Type => ItemType; /// /// The status of the web search. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// A reasoning item resource. /// internal sealed record ReasoningItemResource : ItemResource { /// /// The constant item type identifier for reasoning items. /// public const string ItemType = "reasoning"; /// public override string Type => ItemType; /// /// The status of the reasoning. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// An item reference item resource. /// internal sealed record ItemReferenceItemResource : ItemResource { /// /// The constant item type identifier for item reference items. /// public const string ItemType = "item_reference"; /// public override string Type => ItemType; } /// /// An image generation tool call item resource. /// internal sealed record ImageGenerationToolCallItemResource : ItemResource { /// /// The constant item type identifier for image generation call items. /// public const string ItemType = "image_generation_call"; /// public override string Type => ItemType; /// /// The status of the image generation. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// A code interpreter tool call item resource. /// internal sealed record CodeInterpreterToolCallItemResource : ItemResource { /// /// The constant item type identifier for code interpreter call items. /// public const string ItemType = "code_interpreter_call"; /// public override string Type => ItemType; /// /// The status of the code interpreter. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// A local shell tool call item resource. /// internal sealed record LocalShellToolCallItemResource : ItemResource { /// /// The constant item type identifier for local shell call items. /// public const string ItemType = "local_shell_call"; /// public override string Type => ItemType; /// /// The status of the local shell call. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// A local shell tool call output item resource. /// internal sealed record LocalShellToolCallOutputItemResource : ItemResource { /// /// The constant item type identifier for local shell call output items. /// public const string ItemType = "local_shell_call_output"; /// public override string Type => ItemType; /// /// The status of the local shell call output. /// [JsonPropertyName("status")] public string? Status { get; init; } } /// /// An MCP list tools item resource. /// internal sealed record MCPListToolsItemResource : ItemResource { /// /// The constant item type identifier for MCP list tools items. /// public const string ItemType = "mcp_list_tools"; /// public override string Type => ItemType; } /// /// An MCP approval request item resource. /// internal sealed record MCPApprovalRequestItemResource : ItemResource { /// /// The constant item type identifier for MCP approval request items. /// public const string ItemType = "mcp_approval_request"; /// public override string Type => ItemType; } /// /// An MCP approval response item resource. /// internal sealed record MCPApprovalResponseItemResource : ItemResource { /// /// The constant item type identifier for MCP approval response items. /// public const string ItemType = "mcp_approval_response"; /// public override string Type => ItemType; } /// /// An MCP call item resource. /// internal sealed record MCPCallItemResource : ItemResource { /// /// The constant item type identifier for MCP call items. /// public const string ItemType = "mcp_call"; /// public override string Type => ItemType; }