openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Assistants/Streaming/RunStepDetailsUpdate.cs
121lines · modeblame
19fc7d54Jose Arriaga Maldonado2 years ago | 1 | using System; |
9f9f2936Jose Arriaga Maldonado2 years ago | 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; | |
79014abcShivangiReja1 years ago | 4 | using System.Diagnostics.CodeAnalysis; |
9f9f2936Jose Arriaga Maldonado2 years ago | 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> | |
79014abcShivangiReja1 years ago | 12 | [Experimental("OPENAI001")] |
9f9f2936Jose Arriaga Maldonado2 years ago | 13 | public class RunStepDetailsUpdate : StreamingUpdate |
| 14 | { | |
19fc7d54Jose Arriaga Maldonado2 years ago | 15 | internal readonly InternalRunStepDelta _delta; |
| 16 | internal readonly InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject _toolCall; | |
9f9f2936Jose Arriaga Maldonado2 years ago | 17 | private readonly InternalRunStepDeltaStepDetailsMessageCreationObject _asMessageCreation; |
bf3f0eddJose Arriaga Maldonado1 years ago | 18 | private readonly InternalRunStepDeltaStepDetailsToolCallsCodeObject _asCodeInterpreterCall; |
9f9f2936Jose Arriaga Maldonado2 years ago | 19 | private readonly InternalRunStepDeltaStepDetailsToolCallsFileSearchObject _asFileSearchCall; |
| 20 | private readonly InternalRunStepDeltaStepDetailsToolCallsFunctionObject _asFunctionCall; | |
| 21 | | |
bf3f0eddJose Arriaga Maldonado1 years ago | 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 | | |
9f9f2936Jose Arriaga Maldonado2 years ago | 38 | /// <inheritdoc cref="InternalRunStepDelta.Id"/> |
79014abcShivangiReja1 years ago | 39 | public string StepId => _delta?.Id; |
9f9f2936Jose Arriaga Maldonado2 years ago | 40 | |
| 41 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsMessageCreationObjectMessageCreation"/> | |
| 42 | public string CreatedMessageId => _asMessageCreation?.MessageCreation?.MessageId; | |
| 43 | | |
| 44 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObject.Id"/> | |
19fc7d54Jose Arriaga Maldonado2 years ago | 45 | public string ToolCallId |
bf3f0eddJose Arriaga Maldonado1 years ago | 46 | => _asCodeInterpreterCall?.Id |
19fc7d54Jose Arriaga Maldonado2 years ago | 47 | ?? _asFileSearchCall?.Id |
| 48 | ?? _asFunctionCall?.Id | |
| 49 | ?? (_toolCall?.SerializedAdditionalRawData?.TryGetValue("id", out BinaryData idData) == true | |
| 50 | ? idData.ToString() | |
| 51 | : null); | |
9f9f2936Jose Arriaga Maldonado2 years ago | 52 | |
| 53 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObject.Index"/> | |
bf3f0eddJose Arriaga Maldonado1 years ago | 54 | public int? ToolCallIndex |
| 55 | => _asCodeInterpreterCall?.Index | |
| 56 | ?? _asFileSearchCall?.Index | |
| 57 | ?? _asFunctionCall?.Index; | |
| 58 | | |
| 59 | #region Code Interpreter | |
9f9f2936Jose Arriaga Maldonado2 years ago | 60 | |
| 61 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreter.Input"/> | |
bf3f0eddJose Arriaga Maldonado1 years ago | 62 | public string CodeInterpreterInput => _asCodeInterpreterCall?.CodeInterpreter?.Input; |
9f9f2936Jose Arriaga Maldonado2 years ago | 63 | |
| 64 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreter.Outputs"/> | |
bf3f0eddJose Arriaga Maldonado1 years ago | 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 | |
9f9f2936Jose Arriaga Maldonado2 years ago | 87 | |
| 88 | /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Name"/> | |
448181b8Chris1 years ago | 89 | public string FunctionName => _asFunctionCall?.Function?.Name; |
9f9f2936Jose Arriaga Maldonado2 years ago | 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 | | |
bf3f0eddJose Arriaga Maldonado1 years ago | 97 | #endregion |
9f9f2936Jose Arriaga Maldonado2 years ago | 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 = []; | |
bf3f0eddJose Arriaga Maldonado1 years ago | 106 | |
9f9f2936Jose Arriaga Maldonado2 years ago | 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 | } | |
bf3f0eddJose Arriaga Maldonado1 years ago | 118 | |
9f9f2936Jose Arriaga Maldonado2 years ago | 119 | return updates; |
| 120 | } | |
| 121 | } |