Public/Runs/Get-ThreadRunStep.ps1
function Get-ThreadRunStep { [CmdletBinding(DefaultParameterSetName = 'List')] [OutputType([pscustomobject])] param ( [Parameter(ParameterSetName = 'Get', Mandatory, ValueFromPipelineByPropertyName)] [Alias('step_id')] [ValidateNotNullOrEmpty()] [string][UrlEncodeTransformation()]$StepId, [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateScript({ ([string]$_.id).StartsWith('run_', [StringComparison]::Ordinal) -and ([string]$_.thread_id).StartsWith('thread_', [StringComparison]::Ordinal) })] [Alias('Run')] [Object]$InputObject, [Parameter(ParameterSetName = 'List')] [ValidateRange(1, 100)] [int]$Limit = 20, [Parameter(ParameterSetName = 'ListAll')] [switch]$All, [Parameter(ParameterSetName = 'ListAll', DontShow)] [string]$After, [Parameter(ParameterSetName = 'ListAll', DontShow)] [string]$Before, [Parameter(ParameterSetName = 'List')] [Parameter(ParameterSetName = 'ListAll')] [ValidateSet('asc', 'desc')] [string][LowerCaseTransformation()]$Order = 'asc', [Parameter()] [int]$TimeoutSec = 0, [Parameter()] [ValidateRange(0, 100)] [int]$MaxRetryCount = 0, [Parameter()] [OpenAIApiType]$ApiType = [OpenAIApiType]::OpenAI, [Parameter()] [System.Uri]$ApiBase, [Parameter(DontShow)] [string]$ApiVersion, [Parameter()] [ValidateSet('openai', 'azure', 'azure_ad')] [string]$AuthType = 'openai', [Parameter()] [securestring][SecureStringTransformation()]$ApiKey, [Parameter()] [Alias('OrgId')] [string]$Organization, [Parameter()] [System.Collections.IDictionary]$AdditionalQuery, [Parameter()] [System.Collections.IDictionary]$AdditionalHeaders, [Parameter()] [object]$AdditionalBody ) begin { # Initialize API Key [securestring]$SecureToken = Initialize-APIKey -ApiKey $ApiKey # Initialize API Base $ApiBase = Initialize-APIBase -ApiBase $ApiBase -ApiType $ApiType # Initialize Organization ID $Organization = Initialize-OrganizationID -OrgId $Organization # Get API context $OpenAIParameter = Get-OpenAIContext -EndpointName 'Runs' -ApiType $ApiType -AuthType $AuthType -ApiBase $ApiBase -ApiVersion $ApiVersion # Parse Common params $CommonParams = ParseCommonParams $PSBoundParameters } process { # Get Thread ID and Run ID [string][UrlEncodeTransformation()]$ThreadId = $InputObject.thread_id if (-not $ThreadId) { Write-Error -Exception ([System.ArgumentException]::new('Could not retrieve Thread ID.')) return } [string][UrlEncodeTransformation()]$RunId = $InputObject.id if (-not $RunId) { Write-Error -Exception ([System.ArgumentException]::new('Could not retrieve Run ID.')) return } #region Construct query url $QueryUri = ($OpenAIParameter.Uri.ToString() -f $ThreadId) $UriBuilder = [System.UriBuilder]::new($QueryUri) $UriBuilder.Path += "/$RunId/steps" if ($StepId.StartsWith('step_', [StringComparison]::Ordinal)) { $UriBuilder.Path += "/$StepId" $QueryUri = $UriBuilder.Uri } elseif ($PSCmdlet.ParameterSetName -eq 'ListAll') { $QueryParam = [System.Web.HttpUtility]::ParseQueryString($UriBuilder.Query) $QueryParam.Add('limit', '100'); $QueryParam.Add('order', $Order); if ($After) { $QueryParam.Add('after', $After); } if ($Before) { $QueryParam.Add('before', $Before); } $UriBuilder.Query = $QueryParam.ToString() $QueryUri = $UriBuilder.Uri } else { $QueryParam = [System.Web.HttpUtility]::ParseQueryString($UriBuilder.Query) $QueryParam.Add('limit', $Limit); $QueryParam.Add('order', $Order); $UriBuilder.Query = $QueryParam.ToString() $QueryUri = $UriBuilder.Uri } #endregion #region Send API Request $params = @{ Method = 'Get' Uri = $QueryUri ContentType = $OpenAIParameter.ContentType TimeoutSec = $TimeoutSec MaxRetryCount = $MaxRetryCount ApiKey = $SecureToken AuthType = $OpenAIParameter.AuthType Organization = $Organization Headers = @{'OpenAI-Beta' = 'assistants=v1' } AdditionalQuery = $AdditionalQuery AdditionalHeaders = $AdditionalHeaders AdditionalBody = $AdditionalBody } $Response = Invoke-OpenAIAPIRequest @params # error check if ($null -eq $Response) { return } #endregion #region Parse response object try { $Response = $Response | ConvertFrom-Json -ErrorAction Stop } catch { Write-Error -Exception $_.Exception } #endregion #region Output if ($Response.object -eq 'list' -and ($null -ne $Response.data)) { # List of object $Responses = @($Response.data) } else { # Single object $Responses = @($Response) } # parse objects foreach ($res in $Responses) { ParseThreadRunStepObject $res -CommonParams $CommonParams } #endregion #region Pagenation if ($Response.has_more) { if ($PSCmdlet.ParameterSetName -eq 'ListAll') { # pagenate $PagenationParam = $PSBoundParameters $PagenationParam.After = $Response.last_id PSOpenAI\Get-ThreadRunStep @PagenationParam } else { Write-Warning 'There is more data that has not been retrieved.' } } #endregion } end { } } |