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