openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Assistants/Streaming/RunStepDetailsUpdate.cs
121lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | using System.Text.Json; |
| 6 | |
| 7 | namespace 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")] |
| 13 | public class RunStepDetailsUpdate : StreamingUpdate |
| 14 | { |
| 15 | internal readonly InternalRunStepDelta _delta; |
| 16 | internal readonly InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject _toolCall; |
| 17 | private readonly InternalRunStepDeltaStepDetailsMessageCreationObject _asMessageCreation; |
| 18 | private readonly InternalRunStepDeltaStepDetailsToolCallsCodeObject _asCodeInterpreterCall; |
| 19 | private readonly InternalRunStepDeltaStepDetailsToolCallsFileSearchObject _asFileSearchCall; |
| 20 | private readonly InternalRunStepDeltaStepDetailsToolCallsFunctionObject _asFunctionCall; |
| 21 | |
| 22 | private IReadOnlyList<RunStepUpdateCodeInterpreterOutput> _codeInterpreterOutputs; |
| 23 | private IReadOnlyList<RunStepFileSearchResult> _fileSearchResults; |
| 24 | |
| 25 | internal RunStepDetailsUpdate(InternalRunStepDelta stepDelta, InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject toolCall = null) |
| 26 | : base(StreamingUpdateReason.RunStepUpdated) |
| 27 | { |
| 28 | _delta = stepDelta; |
| 29 | _toolCall = toolCall; |
| 30 | |
| 31 | _asMessageCreation = stepDelta?.Delta?.StepDetails as InternalRunStepDeltaStepDetailsMessageCreationObject; |
| 32 | |
| 33 | _asCodeInterpreterCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsCodeObject; |
| 34 | _asFileSearchCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsFileSearchObject; |
| 35 | _asFunctionCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsFunctionObject; |
| 36 | } |
| 37 | |
| 38 | /// <inheritdoc cref="InternalRunStepDelta.Id"/> |
| 39 | public string StepId => _delta?.Id; |
| 40 | |
| 41 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsMessageCreationObjectMessageCreation"/> |
| 42 | public string CreatedMessageId => _asMessageCreation?.MessageCreation?.MessageId; |
| 43 | |
| 44 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObject.Id"/> |
| 45 | public string ToolCallId |
| 46 | => _asCodeInterpreterCall?.Id |
| 47 | ?? _asFileSearchCall?.Id |
| 48 | ?? _asFunctionCall?.Id |
| 49 | ?? (_toolCall?.SerializedAdditionalRawData?.TryGetValue("id", out BinaryData idData) == true |
| 50 | ? idData.ToString() |
| 51 | : null); |
| 52 | |
| 53 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObject.Index"/> |
| 54 | public int? ToolCallIndex |
| 55 | => _asCodeInterpreterCall?.Index |
| 56 | ?? _asFileSearchCall?.Index |
| 57 | ?? _asFunctionCall?.Index; |
| 58 | |
| 59 | #region Code Interpreter |
| 60 | |
| 61 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreter.Input"/> |
| 62 | public string CodeInterpreterInput => _asCodeInterpreterCall?.CodeInterpreter?.Input; |
| 63 | |
| 64 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreter.Outputs"/> |
| 65 | public IReadOnlyList<RunStepUpdateCodeInterpreterOutput> CodeInterpreterOutputs => |
| 66 | _codeInterpreterOutputs |
| 67 | ??= _asCodeInterpreterCall?.CodeInterpreter?.Outputs |
| 68 | ?? new ChangeTrackingList<RunStepUpdateCodeInterpreterOutput>(); |
| 69 | |
| 70 | #endregion |
| 71 | |
| 72 | #region File Search |
| 73 | |
| 74 | // CUSTOM: Spread. |
| 75 | public FileSearchRankingOptions FileSearchRankingOptions => _asFileSearchCall?.FileSearch?.RankingOptions; |
| 76 | |
| 77 | // CUSTOM: Spread. |
| 78 | /// <summary> The results of the file search. </summary> |
| 79 | public IReadOnlyList<RunStepFileSearchResult> FileSearchResults => |
| 80 | _fileSearchResults |
| 81 | ??= _asFileSearchCall?.FileSearch?.Results |
| 82 | ?? new ChangeTrackingList<RunStepFileSearchResult>(); |
| 83 | |
| 84 | #endregion |
| 85 | |
| 86 | #region Function |
| 87 | |
| 88 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Name"/> |
| 89 | public string FunctionName => _asFunctionCall?.Function?.Name; |
| 90 | |
| 91 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Arguments"/> |
| 92 | public string FunctionArguments => _asFunctionCall?.Function?.Arguments; |
| 93 | |
| 94 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Output"/> |
| 95 | public string FunctionOutput => _asFunctionCall?.Function?.Output; |
| 96 | |
| 97 | #endregion |
| 98 | |
| 99 | internal static IEnumerable<RunStepDetailsUpdate> DeserializeRunStepDetailsUpdates( |
| 100 | JsonElement element, |
| 101 | StreamingUpdateReason updateKind, |
| 102 | ModelReaderWriterOptions options = null) |
| 103 | { |
| 104 | InternalRunStepDelta stepDelta = InternalRunStepDelta.DeserializeInternalRunStepDelta(element, options); |
| 105 | List<RunStepDetailsUpdate> updates = []; |
| 106 | |
| 107 | if (stepDelta?.Delta?.StepDetails is InternalRunStepDeltaStepDetailsMessageCreationObject) |
| 108 | { |
| 109 | updates.Add(new RunStepDetailsUpdate(stepDelta)); |
| 110 | } |
| 111 | else if (stepDelta?.Delta?.StepDetails is InternalRunStepDeltaStepDetailsToolCallsObject toolCalls) |
| 112 | { |
| 113 | foreach (InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject toolCall in toolCalls.ToolCalls) |
| 114 | { |
| 115 | updates.Add(new RunStepDetailsUpdate(stepDelta, toolCall)); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return updates; |
| 120 | } |
| 121 | } |
| 122 | |