Public/Runs/Get-ThreadRunStep.ps1
function Get-ThreadRunStep { [CmdletBinding(DefaultParameterSetName = 'List')] [OutputType([pscustomobject])] param ( [Parameter(ParameterSetName = 'Get', Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias('step_id')] [ValidateNotNullOrEmpty()] [string][UrlEncodeTransformation()]$StepId, [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [ValidateScript({ ([string]$_.id).StartsWith('run_') -and ([string]$_.thread_id).StartsWith('thread_') })] [Alias('Run')] [Object]$InputObject, [Parameter(ParameterSetName = 'List')] [ValidateRange(1, 100)] [int]$Limit = 20, [Parameter(ParameterSetName = 'ListAll')] [switch]$All, [Parameter(ParameterSetName = 'ListAll', DontShow = $true)] [string]$After, [Parameter(ParameterSetName = 'ListAll', DontShow = $true)] [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(DontShow = $true)] [OpenAIApiType]$ApiType = [OpenAIApiType]::OpenAI, [Parameter()] [System.Uri]$ApiBase, [Parameter(DontShow = $true)] [string]$ApiVersion, [Parameter(DontShow = $true)] [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 endpoint if ($ApiType -eq [OpenAIApiType]::Azure) { $OpenAIParameter = Get-AzureOpenAIAPIEndpoint -EndpointName 'Runs' -ApiBase $ApiBase -ApiVersion $ApiVersion } else { $OpenAIParameter = Get-OpenAIAPIEndpoint -EndpointName 'Runs' -ApiBase $ApiBase } # 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_')) { $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 $Response = Invoke-OpenAIAPIRequest ` -Method 'Get' ` -Uri $QueryUri ` -ContentType $OpenAIParameter.ContentType ` -TimeoutSec $TimeoutSec ` -MaxRetryCount $MaxRetryCount ` -ApiKey $SecureToken ` -AuthType $AuthType ` -Organization $Organization ` -Headers (@{'OpenAI-Beta' = 'assistants=v1' }) ` -AdditionalQuery $AdditionalQuery -AdditionalHeaders $AdditionalHeaders -AdditionalBody $AdditionalBody # 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 { } } |