openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.12

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Assistants/Streaming/RunStepDetailsUpdate.cs

89lines · modecode

1using System;
2using System.ClientModel.Primitives;
3using System.Collections.Generic;
4using System.Diagnostics.CodeAnalysis;
5using System.Text.Json;
6
7namespace OpenAI.Assistants;
8
9/// <summary>
10/// The update type presented when run step details, including tool call progress, have changed.
11/// </summary>
12[Experimental("OPENAI001")]
13public class RunStepDetailsUpdate : StreamingUpdate
14{
15 internal readonly InternalRunStepDelta _delta;
16 internal readonly InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject _toolCall;
17 private readonly InternalRunStepDeltaStepDetailsMessageCreationObject _asMessageCreation;
18 private readonly InternalRunStepDeltaStepDetailsToolCallsCodeObject _asCodeCall;
19 private readonly InternalRunStepDeltaStepDetailsToolCallsFileSearchObject _asFileSearchCall;
20 private readonly InternalRunStepDeltaStepDetailsToolCallsFunctionObject _asFunctionCall;
21
22 /// <inheritdoc cref="InternalRunStepDelta.Id"/>
23 public string StepId => _delta?.Id;
24
25 /// <inheritdoc cref="InternalRunStepDeltaStepDetailsMessageCreationObjectMessageCreation"/>
26 public string CreatedMessageId => _asMessageCreation?.MessageCreation?.MessageId;
27
28 /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObject.Id"/>
29 public string ToolCallId
30 => _asCodeCall?.Id
31 ?? _asFileSearchCall?.Id
32 ?? _asFunctionCall?.Id
33 ?? (_toolCall?.SerializedAdditionalRawData?.TryGetValue("id", out BinaryData idData) == true
34 ? idData.ToString()
35 : null);
36
37 /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObject.Index"/>
38 public int? ToolCallIndex => _asCodeCall?.Index ?? _asFileSearchCall?.Index ?? _asFunctionCall?.Index;
39
40 /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreter.Input"/>
41 public string CodeInterpreterInput => _asCodeCall?.CodeInterpreter?.Input;
42
43 /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreter.Outputs"/>
44 public IReadOnlyList<RunStepUpdateCodeInterpreterOutput> CodeInterpreterOutputs
45 => _asCodeCall?.CodeInterpreter?.Outputs;
46
47 /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Name"/>
48 public string FunctionName => _asFunctionCall.Function?.Name;
49
50 /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Arguments"/>
51 public string FunctionArguments => _asFunctionCall?.Function?.Arguments;
52
53 /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Output"/>
54 public string FunctionOutput => _asFunctionCall?.Function?.Output;
55
56 internal RunStepDetailsUpdate(
57 InternalRunStepDelta stepDelta,
58 InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject toolCall = null)
59 : base(StreamingUpdateReason.RunStepUpdated)
60 {
61 _asMessageCreation = stepDelta?.Delta?.StepDetails as InternalRunStepDeltaStepDetailsMessageCreationObject;
62 _asCodeCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsCodeObject;
63 _asFileSearchCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsFileSearchObject;
64 _asFunctionCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsFunctionObject;
65 _delta = stepDelta;
66 _toolCall = toolCall;
67 }
68
69 internal static IEnumerable<RunStepDetailsUpdate> DeserializeRunStepDetailsUpdates(
70 JsonElement element,
71 StreamingUpdateReason updateKind,
72 ModelReaderWriterOptions options = null)
73 {
74 InternalRunStepDelta stepDelta = InternalRunStepDelta.DeserializeInternalRunStepDelta(element, options);
75 List<RunStepDetailsUpdate> updates = [];
76 if (stepDelta?.Delta?.StepDetails is InternalRunStepDeltaStepDetailsMessageCreationObject)
77 {
78 updates.Add(new RunStepDetailsUpdate(stepDelta));
79 }
80 else if (stepDelta?.Delta?.StepDetails is InternalRunStepDeltaStepDetailsToolCallsObject toolCalls)
81 {
82 foreach (InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject toolCall in toolCalls.ToolCalls)
83 {
84 updates.Add(new RunStepDetailsUpdate(stepDelta, toolCall));
85 }
86 }
87 return updates;
88 }
89}
90