openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Assistants/Streaming/RequiredActionUpdate.cs
54lines · modecode
| 1 | using System.Collections.Generic; |
| 2 | using System.Diagnostics.CodeAnalysis; |
| 3 | using System.Text.Json; |
| 4 | |
| 5 | namespace OpenAI.Assistants; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// The update type presented when the status of a <see cref="ThreadRun"/> has changed to <c>requires_action</c>, |
| 9 | /// indicating that tool output submission or another intervention is needed for the run to continue. |
| 10 | /// </summary> |
| 11 | /// <remarks> |
| 12 | /// Distinct <see cref="RequiredActionUpdate"/> instances will generated for each required action, meaning that |
| 13 | /// parallel function calling will present multiple updates even if the tool calls arrive at the same time. |
| 14 | /// </remarks> |
| 15 | [Experimental("OPENAI001")] |
| 16 | public class RequiredActionUpdate : RunUpdate |
| 17 | { |
| 18 | /// <inheritdoc cref="InternalRequiredFunctionToolCall.InternalName"/> |
| 19 | public string FunctionName => AsFunctionCall?.FunctionName; |
| 20 | |
| 21 | /// <inheritdoc cref="InternalRequiredFunctionToolCall.InternalArguments"/> |
| 22 | public string FunctionArguments => AsFunctionCall?.FunctionArguments; |
| 23 | |
| 24 | /// <inheritdoc cref="InternalRequiredFunctionToolCall.Id"/> |
| 25 | public string ToolCallId => AsFunctionCall?.Id; |
| 26 | |
| 27 | private InternalRequiredFunctionToolCall AsFunctionCall => _requiredAction as InternalRequiredFunctionToolCall; |
| 28 | |
| 29 | private readonly RequiredAction _requiredAction; |
| 30 | |
| 31 | internal RequiredActionUpdate(ThreadRun run, RequiredAction action) |
| 32 | : base(run, StreamingUpdateReason.RunRequiresAction) |
| 33 | { |
| 34 | _requiredAction = action; |
| 35 | } |
| 36 | |
| 37 | /// <summary> |
| 38 | /// Gets the full, deserialized <see cref="ThreadRun"/> instance associated with this streaming required action |
| 39 | /// update. |
| 40 | /// </summary> |
| 41 | /// <returns></returns> |
| 42 | public ThreadRun GetThreadRun() => Value; |
| 43 | |
| 44 | internal static IEnumerable<RequiredActionUpdate> DeserializeRequiredActionUpdates(JsonElement element) |
| 45 | { |
| 46 | ThreadRun run = ThreadRun.DeserializeThreadRun(element, ModelSerializationExtensions.WireOptions); |
| 47 | List<RequiredActionUpdate> updates = []; |
| 48 | foreach (RequiredAction action in run.RequiredActions ?? []) |
| 49 | { |
| 50 | updates.Add(new(run, action)); |
| 51 | } |
| 52 | return updates; |
| 53 | } |
| 54 | } |