openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Assistants/Streaming/StreamingUpdate.cs

101lines · modecode

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