openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Assistants/Streaming/StreamingUpdate.cs

105lines · modecode

1using System.Collections.Generic;
2using System.Diagnostics.CodeAnalysis;
3using System.Net.ServerSentEvents;
4using System.Text.Json;
5
6namespace OpenAI.Assistants;
7
8/// <summary>
9/// Represents a single item of streamed Assistants API data.
10/// </summary>
11/// <remarks>
12/// Please note that this is the abstract base type. To access data, downcast an instance of this type to an
13/// appropriate, derived update type:
14/// <para>
15/// For messages: <see cref="MessageStatusUpdate"/>, <see cref="MessageContentUpdate"/>
16/// </para>
17/// <para>
18/// For runs and run steps: <see cref="RunUpdate"/>, <see cref="RunStepUpdate"/>, <see cref="RunStepDetailsUpdate"/>,
19/// <see cref="RequiredActionUpdate"/>
20/// </para>
21/// <para>
22/// For threads: <see cref="ThreadUpdate"/>
23/// </para>
24/// </remarks>
25[Experimental("OPENAI001")]
26public abstract partial class StreamingUpdate
27{
28 /// <summary>
29 /// A value indicating what type of event this update represents.
30 /// </summary>
31 /// <remarks>
32 /// Many events share the same response type. For example, <see cref="StreamingUpdateReason.RunCreated"/> and
33 /// <see cref="StreamingUpdateReason.RunCompleted"/> are both associated with a <see cref="ThreadRun"/> instance.
34 /// You can use the value of <see cref="UpdateKind"/> to differentiate between these events when the type is not
35 /// sufficient to do so.
36 /// </remarks>
37 public StreamingUpdateReason UpdateKind { get; }
38
39 internal StreamingUpdate(StreamingUpdateReason updateKind)
40 {
41 UpdateKind = updateKind;
42 }
43
44 internal static IEnumerable<StreamingUpdate> FromEvent(SseItem<byte[]> sseItem)
45 {
46 StreamingUpdateReason updateKind = StreamingUpdateReasonExtensions.FromSseEventLabel(sseItem.EventType);
47 using JsonDocument dataDocument = JsonDocument.Parse(sseItem.Data);
48 JsonElement e = dataDocument.RootElement;
49
50 return updateKind switch
51 {
52 StreamingUpdateReason.ThreadCreated => ThreadUpdate.DeserializeThreadCreationUpdates(e, updateKind),
53 StreamingUpdateReason.RunCreated
54 or StreamingUpdateReason.RunQueued
55 or StreamingUpdateReason.RunInProgress
56 or StreamingUpdateReason.RunCompleted
57 or StreamingUpdateReason.RunIncomplete
58 or StreamingUpdateReason.RunFailed
59 or StreamingUpdateReason.RunCancelling
60 or StreamingUpdateReason.RunCancelled
61 or StreamingUpdateReason.RunExpired => RunUpdate.DeserializeRunUpdates(e, updateKind),
62 StreamingUpdateReason.RunRequiresAction => RequiredActionUpdate.DeserializeRequiredActionUpdates(e),
63 StreamingUpdateReason.RunStepCreated
64 or StreamingUpdateReason.RunStepInProgress
65 or StreamingUpdateReason.RunStepCompleted
66 or StreamingUpdateReason.RunStepFailed
67 or StreamingUpdateReason.RunStepCancelled
68 or StreamingUpdateReason.RunStepExpired => RunStepUpdate.DeserializeRunStepUpdates(e, updateKind),
69 StreamingUpdateReason.MessageCreated
70 or StreamingUpdateReason.MessageInProgress
71 or StreamingUpdateReason.MessageCompleted
72 or StreamingUpdateReason.MessageFailed => MessageStatusUpdate.DeserializeMessageStatusUpdates(e, updateKind),
73 StreamingUpdateReason.RunStepUpdated => RunStepDetailsUpdate.DeserializeRunStepDetailsUpdates(e, updateKind),
74 StreamingUpdateReason.MessageUpdated => MessageContentUpdate.DeserializeMessageContentUpdates(e, updateKind),
75 _ => null,
76 };
77 }
78}
79
80/// <summary>
81/// Represents a single item of streamed data that encapsulates an underlying response value type.
82/// </summary>
83/// <typeparam name="T"> The response value type of the "delta" payload. </typeparam>
84[Experimental("OPENAI001")]
85public partial class StreamingUpdate<T> : StreamingUpdate
86 where T : class
87{
88 /// <summary>
89 /// The underlying response value received with the streaming event.
90 /// </summary>
91 public T Value { get; }
92
93 internal StreamingUpdate(T value, StreamingUpdateReason updateKind)
94 : base(updateKind)
95 {
96 Value = value;
97 }
98
99 /// <summary>
100 /// Implicit operator that allows the underlying value type of the <see cref="StreamingUpdate{T}"/> to be used
101 /// directly.
102 /// </summary>
103 /// <param name="update"></param>
104 public static implicit operator T(StreamingUpdate<T> update) => update.Value;
105}
106