Feature: In-memory background job system for Ollama installs with SSE streaming support #74
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Feature: In-memory background job system for Ollama installs with SSE streaming support
Summary
This PR introduces a new in-memory background job system for handling Ollama model installations. The new system allows installs to run asynchronously, provides real-time feedback via server-sent events (SSE), and supports cancelation. It also improves resiliency in provider model lookups by falling back to direct SQL when ORM queries return no results.
Changes
🔹 AI Providers (
ai_providers.py)Updated
get_all_models:get_all_parameterized.get_allif ORM returns no settings.🔹 Ollama Endpoints (
ollama.py)Added in-memory job model (
InstallTaskdataclass) with:queued,running,downloading,verifying,extracting,completed,error,canceled).Implemented background install worker:
/api/pull.Added new API routes:
POST /install→ enqueue background install, returntask_id.GET /install/{task_id}→ retrieve current status.GET /install/{task_id}/events→ stream live events via SSE.DELETE /install/{task_id}→ cancel running task.Refined
check_model_existsto use normalized server base URL.Normalized server base URL handling (
normalize_server_base).🔹 Frontend API Service (
ApiService.ts)Added
getSsehelper:onMessagecallback for streamed events.Motivation
The previous install endpoint tied the client request lifetime to long-running downloads, causing blocked UIs and unreliable progress reporting.
The new system decouples installs from request lifetimes:
task_id.Adds cancelation support for installs.
Makes provider model fetching more robust by falling back to direct SQL queries when ORM scope filters fail.
Testing & Validation
Verified new
/installendpoint:task_idimmediately./install/{task_id}.DELETE /install/{task_id}.Confirmed tasks transition through correct states (
queued → running → downloading → verifying → completed).Simulated server errors, timeouts, and malformed responses — errors surfaced in event stream.
Frontend
getSsetested against install events stream:Impact
ApiServiceto handle SSE.Checklist
/install,/install/{task_id},/install/{task_id}/events,/install/{task_id}DELETE endpointsgetSsehelper for event streaming