Files
LocalAI/core/trace/audio_snippet_test.go
Richard Palethorpe eb32cd9073 feat(realtime): eager blocking pipeline warm-up + /backend/load API (#10662)
Realtime sessions previously lazy-loaded each pipeline sub-model (VAD,
transcription, LLM, TTS) on first use, so every cold session paid a
per-request model-load stall and load errors only surfaced mid-stream.

Warm the whole pipeline eagerly and blockingly at session start
(including the voice-gate speaker-recognition model, which an enforced
gate blocks each utterance on; compaction's summary_model stays lazy
since it only runs off the response path):
- Add backend.PreloadModel / PreloadModelByName as the single load path
  for every modality (no transcription special-case; backend-omitted
  configs are deprecated).
- The realtime session blocks on Model.Warmup and returns a
  model_load_error to the client if any stage fails to load;
  updateSession warms in the background. Opt out per pipeline with
  pipeline.disable_warmup, exposed as a UI toggle via the
  config-metadata registry.

Add a LocalAI-native POST /backend/load (and /v1/backend/load) that
pre-loads a model -- expanding realtime pipelines into their sub-models
-- as the inverse of /backend/shutdown. There is one preload engine
(backend.PreloadStages): the realtime Warmup methods, /backend/load and
the --load-to-memory startup flag all use it, so --load-to-memory now
also expands pipeline models and records load-failure traces. Pipeline
sub-model alias resolution is likewise shared
(ModelConfigLoader.LoadResolvedModelConfig). Surface the endpoint
everywhere an admin manages models:
- MCP admin tool load_model (httpapi + inproc clients, safety/catalog
  prompts, catalog/dispatch tests).
- "Load into memory" action in the React models UI.
- Swagger regenerated; docs moved to the general backend-monitor page
  since it is not realtime-specific.

Fix a Traces UI crash ("json: unsupported value: -Inf"): audio-snippet
RMS/peak now floor at a finite dBFS, and backend-trace data is sanitized
to drop non-finite floats before marshaling. The sanitizer is
copy-on-write -- it runs on every RecordBackendTrace, so containers are
only re-allocated on the paths that actually changed.

Migrate core/http/openresponses_test.go onto the prebuilt mock-backend
the rest of the http suite already uses -- it was the last spec still
pointing at a real HuggingFace model, so it 404'd wherever no vision
backend was built -- and fix its item_reference specs to send the
spec's "id" field instead of "item_id", which the handler never
accepted.

Assisted-by: Claude:claude-opus-4-8 Claude Code

Signed-off-by: Richard Palethorpe <io@richiejp.com>
2026-07-03 18:00:37 +02:00

82 lines
2.8 KiB
Go

package trace_test
import (
"encoding/json"
"math"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/mudler/LocalAI/core/trace"
)
// One second of mono 16-bit PCM at 16 kHz: 32 KiB raw. After the 44-byte
// WAV header and base64 encoding the snippet runs ~42 KiB, which is well
// over the small caps used here and matches the smallest realistic TTS
// output size.
const (
snippetSampleRate = 16000
snippetSeconds = 1
)
func makePCM(seconds, sampleRate int) []byte {
return make([]byte, seconds*sampleRate*2) // int16 mono
}
var _ = Describe("AudioSnippetFromPCM byte cap", func() {
pcm := makePCM(snippetSeconds, snippetSampleRate)
totalPCM := len(pcm)
It("omits audio_wav_base64 when the encoded snippet would exceed the cap, keeping the metrics", func() {
out := trace.AudioSnippetFromPCM(pcm, snippetSampleRate, totalPCM, 1024)
Expect(out).ToNot(BeNil(), "metrics must still be returned even when the waveform is dropped")
Expect(out).ToNot(HaveKey("audio_wav_base64"), "oversized base64 must be dropped so the UI does not try to render invalid audio data")
Expect(out).To(HaveKey("audio_duration_s"))
Expect(out).To(HaveKey("audio_sample_rate"))
Expect(out).To(HaveKey("audio_rms_dbfs"))
})
It("includes audio_wav_base64 when the snippet fits under the cap", func() {
out := trace.AudioSnippetFromPCM(pcm, snippetSampleRate, totalPCM, 1024*1024)
Expect(out).To(HaveKey("audio_wav_base64"))
Expect(out["audio_wav_base64"]).ToNot(BeEmpty())
})
It("includes audio_wav_base64 when the cap is disabled (0)", func() {
out := trace.AudioSnippetFromPCM(pcm, snippetSampleRate, totalPCM, 0)
Expect(out).To(HaveKey("audio_wav_base64"))
})
})
// Silent audio (RMS/peak of zero) has a true level of -∞ dBFS, but emitting
// -Inf made the whole /api/backend-traces response fail to JSON-marshal and
// blanked the Traces UI. The metrics must instead be finite and serializable.
var _ = Describe("AudioSnippetFromPCM silent audio dBFS", func() {
pcm := makePCM(snippetSeconds, snippetSampleRate) // all zeros == digital silence
totalPCM := len(pcm)
It("reports finite dBFS for silence instead of -Inf", func() {
out := trace.AudioSnippetFromPCM(pcm, snippetSampleRate, totalPCM, 0)
rms, ok := out["audio_rms_dbfs"].(float64)
Expect(ok).To(BeTrue())
Expect(math.IsInf(rms, 0)).To(BeFalse(), "silent RMS must not be ±Inf")
Expect(math.IsNaN(rms)).To(BeFalse())
peak, ok := out["audio_peak_dbfs"].(float64)
Expect(ok).To(BeTrue())
Expect(math.IsInf(peak, 0)).To(BeFalse(), "silent peak must not be ±Inf")
Expect(math.IsNaN(peak)).To(BeFalse())
})
It("produces a snippet that round-trips through encoding/json", func() {
out := trace.AudioSnippetFromPCM(pcm, snippetSampleRate, totalPCM, 0)
_, err := json.Marshal(out)
Expect(err).ToNot(HaveOccurred(), "silent-audio metrics must be JSON-marshalable")
})
})