Skip to content

[TASK] Refactor extract model-file downloads from internal/audio into internal/platform #1258

Closed

Description

@edenreich

Summary

Move model-file downloading out of internal/audio into internal/platform, so a capability that is not audio can fetch models from Hugging Face without importing the speech package. Pure refactor: no behaviour, config, flag or output change. It is split out of #1257 (AvatarGeneration), which will be the third caller and the first non-audio one, and it should land first on its own so that PR stays a feature and this one stays a no-op move.

What is wrong today

internal/audio owns three things that are not speech:

  • download.go: downloadToFile (atomic temp file + rename, Content-Length check) and progressReader (reports through agentdomain.ToolProgressCallback). Generic transfer code.
  • The Hugging Face URL shape, hardcoded twice as https://huggingface.co/<repo>/resolve/main: huggingFaceWhisperBase in model.go and qwen3TTSBase in ttsmodel.go.
  • The same "stat, check auto_download, mkdir, download" sequence, written once in ModelManager.EnsureModel and again in TTSModelManager.ensureFile.

binary.go also uses newProgressReader, for GitHub release assets. So the progress reader is not Hugging Face specific either, which is why this is two packages and not one.

Target boundary

Fetching files is a generic subdomain (commodity, no business rules), and per CLAUDE.md "platform/ is shared infrastructure". What a model is stays with the capability that uses it.

Concern Lives in Why
Atomic download to a file, progress reporting internal/platform/download Used by Hugging Face fetches and by BinaryManager (GitHub releases). Knows nothing about either.
huggingface.co/<repo>/resolve/<revision>/<file>, ensuring a repo file is cached in a dir internal/platform/huggingface The anti-corruption layer: the only package that knows how Hugging Face addresses a file. If the URL shape, a mirror or auth ever changes, it changes here.
Which files a model is: modelFileName (tiny -> ggml-tiny.bin), ttsModelFiles presets and the quant-suffix to mmproj rule internal/audio (unchanged) Speech knowledge. It must not move to platform.
Whether a download is allowed (speech_to_text.auto_download, text_to_speech.auto_download) and the error naming that config key internal/audio (unchanged) Policy and its wording belong to the capability. platform is told, it does not decide.
Where models are cached (modelsDir) internal/audio (unchanged) Config of the capability.

Proposed surface, small on purpose:

// internal/platform/download
func ToFile(ctx context.Context, client *http.Client, url, dstPath, label string) error
func NewProgressReader(ctx context.Context, src io.Reader, label string, total int64) io.Reader

// internal/platform/huggingface
type Repo struct {
    ID       string // "ggerganov/whisper.cpp"
    Revision string // "" means "main"
}

type Client struct {
    BaseURL string       // "https://huggingface.co", overridable in tests
    HTTP    *http.Client
}

var ErrNotCached = errors.New("file not cached and download not allowed")

// EnsureFile returns the local path of file inside dir, downloading it from repo
// when it is missing and allowDownload is true; ErrNotCached otherwise.
func (c *Client) EnsureFile(ctx context.Context, repo Repo, file, dir, label string, allowDownload bool) (string, error)

ModelManager and TTSModelManager keep their types, constructors and public methods. Internally they drop baseURL/client for a *huggingface.Client plus a huggingface.Repo, call EnsureFile(..., cfg.AutoDownload), and turn ErrNotCached into their existing message. Repo is a value object: two repos with the same ID and revision are the same repo.

Not in scope: BinaryManager stays in internal/audio (it only swaps its progress reader import); no resume or retry (ponytail: no resume, add an HTTP Range retry in download.ToFile if multi-GB restarts annoy people); no hf CLI; no new config keys; no avatar code.

Acceptance Criteria

  • internal/platform/download holds ToFile and NewProgressReader, moved from internal/audio/download.go with their tests (download_test.go). internal/audio/download.go is deleted.
  • internal/platform/huggingface holds Repo, Client and EnsureFile. It is the only non-test Go file in the repo containing huggingface.co or /resolve/; huggingFaceWhisperBase and qwen3TTSBase are gone.
  • internal/audio/model.go and ttsmodel.go contain no URL building, no os.Stat/MkdirAll/download sequence of their own, and still own modelFileName, ttsModelFiles, modelsDir and the auto_download error text.
  • binary.go uses download.NewProgressReader; BinaryManager is otherwise untouched.
  • No behaviour change: same URLs requested, same cache paths (~/.infer/models/whisper, ~/.infer/models/tts), same progress messages, same error strings including speech_to_text.auto_download is disabled and text_to_speech.auto_download is disabled. The existing model_test.go, transcriber_test.go and synthesizer_test.go pass with only their test-server wiring adapted (a huggingface.Client with BaseURL set to the httptest server).
  • huggingface tests cover: file already cached (no request made), missing + allowDownload=false returns ErrNotCached, missing + allowed downloads to dir/file, empty Revision resolves to main, a non-200 leaves no file behind.
  • Dependency direction holds: internal/platform/* does not import internal/audio; internal/audio imports both new packages. New imports carry explicit aliases and task lint:imports passes.
  • task lint and task test pass. PR body states no docs ticket: internal-only refactor.

Activity

  1. added
    refactor
    on Sep 21, 2026
  2. added theissue type on Sep 21, 2026
  3. moved this to Todo in 2026on Sep 21, 2026
  4. moved this from Todo to In progress in 2026on Sep 21, 2026
  5. moved this from In progress to QA in 2026on Sep 21, 2026
  6. added a commit that references this issue on Sep 21, 2026
    c66cb44
  7. moved this from QA to Done in 2026on Sep 22, 2026
  8. inference-gateway-releaser commented on Sep 22, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.205.2 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorreleased

    Type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions