functions/LemonAI-InstallAIModel.ps1

function LemonAI-InstallAIModel($module){
    Write-Host "Pulling $module model" -ForegroundColor Yellow
    $PULLjob=Start-Job -Name "pull_job" -ArgumentList $module -ScriptBlock {
        $cmdJob = Start-Job -Name "cmdjob" -ArgumentList $args -ScriptBlock{
            $module = $args[0]
            ollama pull $module
        }
        $errind=0
        while($cmdJob.State -eq "Running"){
            Start-Sleep -Milliseconds 100
            Write-Output $(Receive-Job $cmdJob)
            $NewMessages=@()
            $MsgsTrans=$cmdJob.ChildJobs[0].Error
            $MsgsTransCount=$MsgsTrans.Count
            if($MsgsTransCount -gt 0){
                for($MsgInd=$errind;$MsgInd -lt $MsgsTransCount;$MsgInd++){
                    $Msg=$MsgsTrans[$MsgInd]
                    $NewMsg=$Msg.TargetObject
                    $NewMessages+=([string]$NewMsg)
                }
                $errind=$MsgsTransCount-1
                $RecentMsgs=$NewMessages | Select -Unique
                if($RecentMsgs){
                    foreach($newOutput in $RecentMsgs){
                        if($newOutput -like "*success*"){
                            Stop-Process (Get-Process -Name "ollama*")
                            Stop-Job $cmdJob
                            Remove-Job $cmdJob
                        }
                    }
                }
            }
        }
    }
    return $PULLjob
}