// Copyright (c) Microsoft. All rights reserved. using System.Text.Json.Serialization; using OpenAI.Responses; namespace Microsoft.Agents.AI.Hosting.OpenAI.Responses.Model; /// /// Abstract base class for all streaming response events in the OpenAI Responses API. /// Provides common properties shared across all streaming event types. /// [JsonPolymorphic(UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization)] [JsonDerivedType(typeof(StreamingOutputItemAddedResponse), StreamingOutputItemAddedResponse.EventType)] [JsonDerivedType(typeof(StreamingOutputItemDoneResponse), StreamingOutputItemDoneResponse.EventType)] [JsonDerivedType(typeof(StreamingCreatedResponse), StreamingCreatedResponse.EventType)] [JsonDerivedType(typeof(StreamingCompletedResponse), StreamingCompletedResponse.EventType)] internal abstract class StreamingResponseEventBase { /// /// Gets or sets the type identifier for the streaming response event. /// This property is used to discriminate between different event types during serialization. /// [JsonPropertyName("type")] public string Type { get; set; } /// /// Gets or sets the sequence number of this event in the streaming response. /// Events are numbered sequentially starting from 1 to maintain ordering. /// [JsonPropertyName("sequence_number")] public int SequenceNumber { get; set; } /// /// Initializes a new instance of the class. /// /// The type identifier for this streaming response event. /// The sequence number of this event in the streaming response. [JsonConstructor] public StreamingResponseEventBase(string type, int sequenceNumber) { this.Type = type; this.SequenceNumber = sequenceNumber; } } /// /// Represents a streaming response event indicating that a new output item has been added to the response. /// This event is sent when the AI agent produces a new piece of content during streaming. /// internal sealed class StreamingOutputItemAddedResponse : StreamingResponseEventBase { /// /// The constant event type identifier for output item added events. /// public const string EventType = "response.output_item.added"; /// /// Initializes a new instance of the class. /// /// The sequence number of this event in the streaming response. public StreamingOutputItemAddedResponse(int sequenceNumber) : base(EventType, sequenceNumber) { } /// /// Gets or sets the index of the output in the response where this item was added. /// Multiple outputs can exist in a single response, and this identifies which one. /// [JsonPropertyName("output_index")] public int OutputIndex { get; set; } /// /// Gets or sets the response item that was added to the output. /// This contains the actual content or data produced by the AI agent. /// [JsonPropertyName("item")] public ResponseItem? Item { get; set; } } /// /// Represents a streaming response event indicating that an output item has been completed. /// This event is sent when the AI agent finishes producing a particular piece of content. /// internal sealed class StreamingOutputItemDoneResponse : StreamingResponseEventBase { /// /// The constant event type identifier for output item done events. /// public const string EventType = "response.output_item.done"; /// /// Initializes a new instance of the class. /// /// The sequence number of this event in the streaming response. public StreamingOutputItemDoneResponse(int sequenceNumber) : base(EventType, sequenceNumber) { } /// /// Gets or sets the index of the output in the response where this item was completed. /// This corresponds to the same output index from the associated . /// [JsonPropertyName("output_index")] public int OutputIndex { get; set; } /// /// Gets or sets the completed response item. /// This contains the final version of the content produced by the AI agent. /// [JsonPropertyName("item")] public ResponseItem? Item { get; set; } } /// /// Represents a streaming response event indicating that a new response has been created and streaming has begun. /// This is typically the first event sent in a streaming response sequence. /// internal sealed class StreamingCreatedResponse : StreamingResponseEventBase { /// /// The constant event type identifier for response created events. /// public const string EventType = "response.created"; /// /// Initializes a new instance of the class. /// /// The sequence number of this event in the streaming response. public StreamingCreatedResponse(int sequenceNumber) : base(EventType, sequenceNumber) { } /// /// Gets or sets the OpenAI response object that was created. /// This contains metadata about the response including ID, creation timestamp, and other properties. /// [JsonPropertyName("response")] public required OpenAIResponse Response { get; set; } } /// /// Represents a streaming response event indicating that the response has been completed. /// This is typically the last event sent in a streaming response sequence. /// internal sealed class StreamingCompletedResponse : StreamingResponseEventBase { /// /// The constant event type identifier for response completed events. /// public const string EventType = "response.completed"; /// /// Initializes a new instance of the class. /// /// The sequence number of this event in the streaming response. public StreamingCompletedResponse(int sequenceNumber) : base(EventType, sequenceNumber) { } /// /// Gets or sets the completed OpenAI response object. /// This contains the final state of the response including all generated content and metadata. /// [JsonPropertyName("response")] public required OpenAIResponse Response { get; set; } }