openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Assistants/Streaming/RunStepDetailsUpdate.cs

78lines · modepreview

using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;

namespace OpenAI.Assistants;

/// <summary>
/// The update type presented when run step details, including tool call progress, have changed.
/// </summary>
public class RunStepDetailsUpdate : StreamingUpdate
{
    private readonly InternalRunStepDelta _delta;
    private readonly InternalRunStepDeltaStepDetailsMessageCreationObject _asMessageCreation;
    private readonly InternalRunStepDeltaStepDetailsToolCallsCodeObject _asCodeCall;
    private readonly InternalRunStepDeltaStepDetailsToolCallsFileSearchObject _asFileSearchCall;
    private readonly InternalRunStepDeltaStepDetailsToolCallsFunctionObject _asFunctionCall;

    /// <inheritdoc cref="InternalRunStepDelta.Id"/>
    public string StepId  => _delta?.Id;

    /// <inheritdoc cref="InternalRunStepDeltaStepDetailsMessageCreationObjectMessageCreation"/>
    public string CreatedMessageId => _asMessageCreation?.MessageCreation?.MessageId;

    /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObject.Id"/>
    public string ToolCallId => _asCodeCall?.Id ?? _asFileSearchCall?.Id ?? _asFunctionCall?.Id;

    /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObject.Index"/>
    public int? ToolCallIndex => _asCodeCall?.Index ?? _asFileSearchCall?.Index ?? _asFunctionCall?.Index;

    /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreter.Input"/>
    public string CodeInterpreterInput => _asCodeCall?.CodeInterpreter?.Input;

    /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreter.Outputs"/>
    public IReadOnlyList<RunStepUpdateCodeInterpreterOutput> CodeInterpreterOutputs
        => _asCodeCall?.CodeInterpreter?.Outputs;

    /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Name"/>
    public string FunctionName => _asFunctionCall.Function?.Name;

    /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Arguments"/>
    public string FunctionArguments => _asFunctionCall?.Function?.Arguments;

    /// <inheritdoc cref="InternalRunStepDeltaStepDetailsToolCallsFunctionObjectFunction.Output"/>
    public string FunctionOutput => _asFunctionCall?.Function?.Output;

    internal RunStepDetailsUpdate(
        InternalRunStepDelta stepDelta,
        InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject toolCall = null)
            : base(StreamingUpdateReason.RunStepUpdated)
    {
        _asMessageCreation = stepDelta?.Delta?.StepDetails as InternalRunStepDeltaStepDetailsMessageCreationObject;
        _asCodeCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsCodeObject;
        _asFileSearchCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsFileSearchObject;
        _asFunctionCall = toolCall as InternalRunStepDeltaStepDetailsToolCallsFunctionObject;
        _delta = stepDelta;
    }

    internal static IEnumerable<RunStepDetailsUpdate> DeserializeRunStepDetailsUpdates(
        JsonElement element,
        StreamingUpdateReason updateKind,
        ModelReaderWriterOptions options = null)
    {
        InternalRunStepDelta stepDelta = InternalRunStepDelta.DeserializeInternalRunStepDelta(element, options);
        List<RunStepDetailsUpdate> updates = [];
        if (stepDelta?.Delta?.StepDetails is InternalRunStepDeltaStepDetailsMessageCreationObject)
        {
            updates.Add(new RunStepDetailsUpdate(stepDelta));
        }
        else if (stepDelta?.Delta?.StepDetails is InternalRunStepDeltaStepDetailsToolCallsObject toolCalls)
        {
            foreach (InternalRunStepDeltaStepDetailsToolCallsObjectToolCallsObject toolCall in toolCalls.ToolCalls)
            {
                updates.Add(new RunStepDetailsUpdate(stepDelta, toolCall));
            }
        }
        return updates;
    }
}