Public/Start-MetroAIThreadRun.ps1
function Start-MetroAIThreadRun { <# .SYNOPSIS Initiates a run on a thread. .DESCRIPTION Starts a run on the specified thread and waits for completion unless Async is specified. .PARAMETER AssistantId The agent or assistant ID. .PARAMETER ThreadID The thread ID. .PARAMETER Async Run asynchronously. #> [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$AssistantId, [Parameter(Mandatory = $true)] [string]$ThreadID, [switch]$Async ) try { $body = @{ assistant_id = $AssistantId } $runResponse = Invoke-MetroAIApiCall -Service 'threads' ` -Operation 'threadStatus' -Path ("{0}/runs" -f $ThreadID) -Method Post ` -ContentType "application/json" -Body $body if (-not $Async) { $i = 0 do { Start-Sleep -Seconds 10 $runResult = Invoke-MetroAIApiCall -Service 'threads' -Operation 'threadStatus' -Path ("{0}/runs/{1}" -f $ThreadID, $runResponse.id) -Method Get $i++ } while ($runResult.status -ne "completed" -and $i -lt 100) if ($runResult.status -eq "completed") { $result = Invoke-MetroAIApiCall -Service 'threads' -Operation 'messages' -Path ("{0}/messages" -f $ThreadID) -Method Get return $result.data | ForEach-Object { $_.content.text } } else { Write-Error "Run did not complete in time." } } else { Write-Output "Run started asynchronously. Use Get-MetroAIThreadStatus to check." } return $runResponse } catch { Write-Error "Start-MetroAIThreadRun error: $_" } } |