Skip to content

Conversation

@DJJones66
Copy link
Contributor

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:

    • Now prefers ORM-based get_all_parameterized.
    • Falls back to direct SQL get_all if ORM returns no settings.
    • Improves reliability across environments with legacy or mixed scope storage.

🔹 Ollama Endpoints (ollama.py)

  • Added in-memory job model (InstallTask dataclass) with:

    • Task state tracking (queued, running, downloading, verifying, extracting, completed, error, canceled).
    • Progress, messages, error handling.
    • Subscriber support for event streaming with a ring buffer.
    • Deduplication logic to avoid duplicate installs for the same model/server.
    • Cleanup of old tasks after TTL expiry.
  • Implemented background install worker:

    • Manages streaming downloads from Ollama /api/pull.
    • Emits structured events for state/progress.
    • Handles connection errors, server errors, cancelation, and success conditions.
  • Added new API routes:

    • POST /install → enqueue background install, return task_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_exists to use normalized server base URL.

  • Normalized server base URL handling (normalize_server_base).

🔹 Frontend API Service (ApiService.ts)

  • Added getSse helper:

    • Simple wrapper for GET-based SSE endpoints.
    • Automatically attaches bearer token if present.
    • Provides onMessage callback for streamed events.
    • Handles unexpected responses, connection errors, and closes cleanly.

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:

    • Users immediately get a task_id.
    • Install progress streams back via SSE or can be polled.
    • Tasks are tracked in-memory with state, progress, and error handling.
  • 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 /install endpoint:

    • Returns task_id immediately.
    • Dedupes duplicate installs.
    • Streams progress events via SSE.
    • Polling fallback tested with /install/{task_id}.
    • Cancelation tested via 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 getSse tested against install events stream:

    • Correctly parses and forwards event data to callback.
    • Logs unexpected responses and errors for debugging.

Impact

  • Backward-compatible: existing install endpoints replaced with async equivalents, but client integrations updated in ApiService to handle SSE.
  • Greatly improves UX for model installs with responsive UI and real-time progress updates.
  • Provides foundation for other background tasks to be managed in the same way.

Checklist

  • Add in-memory job system for Ollama installs
  • Implement background worker and event emission
  • Add /install, /install/{task_id}, /install/{task_id}/events, /install/{task_id} DELETE endpoints
  • Add cancelation and cleanup support
  • Add frontend getSse helper for event streaming
  • Improve ORM fallback for provider model lookups
  • Validate across success, error, and cancelation scenarios

@DJJones66 DJJones66 merged commit c1eb5c1 into main Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants