feat: audio surfaces (TTS + transcription), imagegen.Editor, llamaswap health probe
CI / Tidy (pull_request) Successful in 10m9s
CI / Build & Test (pull_request) Successful in 11m16s
Adversarial Review (Gadfly) / review (pull_request) Successful in 14m51s

- New leaf package `audio` (ADR-0017): SpeechModel/SpeechProvider and
  TranscriptionModel/TranscriptionProvider with imagegen conventions
  (zero value = backend default, functional options + Apply, bytes
  in/out, never URLs). Root re-exports added.
- imagegen.Editor (ADR-0018): optional image-to-image interface —
  EditRequest carries the generation knobs plus Init image and
  denoising Strength; separate interface so existing Models keep
  compiling.
- provider/llamaswap implements all of it: POST /v1/audio/speech (JSON,
  raw-audio response, MIME from Content-Type with format fallback),
  POST /v1/audio/transcriptions (multipart, response_format=json),
  ListVoices (GET /v1/audio/voices?model=, tolerant of string-list and
  object-list shapes), POST /sdapi/v1/img2img (txt2img wire +
  init_images/denoising_strength, shared image decode), and Health(ctx)
  (GET /health) — a cheap liveness probe for often-offline hosts.
- Hermetic httptest coverage for every new wire shape and validation
  path; README sections + support-matrix footnote updated in the same
  commit (also corrects the stale /v1/images/generations claim — the
  image path has been SDAPI since the seed fix).

First consumer: mort's llamaswap media tool cluster (status / image /
TTS / STT agent tools against the netherstorm host).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AXQxVhXBw8PwFAtsVrXSmj
This commit is contained in:
2026-07-11 23:24:14 -04:00
parent 6abb399f5b
commit 434d721b99
16 changed files with 1317 additions and 8 deletions
+278
View File
@@ -0,0 +1,278 @@
package llamaswap
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"net/url"
"strings"
majaudio "gitea.stevedudenhoeffer.com/steve/majordomo/audio"
"gitea.stevedudenhoeffer.com/steve/majordomo/llm"
)
// SpeechModel implements audio.SpeechProvider, binding a text-to-speech model
// served by llama-swap (routed to a kokoro/chatterbox-style OpenAI-compatible
// upstream). The id is passed through verbatim and selects which upstream
// llama-swap loads.
func (p *Provider) SpeechModel(id string, opts ...majaudio.SpeechModelOption) (majaudio.SpeechModel, error) {
if p.baseURL == "" {
return nil, fmt.Errorf("llama-swap provider %q: no base URL configured (set one via WithBaseURL or an LLM_* env DSN)", p.name)
}
_ = majaudio.ApplySpeechModelOptions(opts)
return &speechModel{p: p, id: id}, nil
}
type speechModel struct {
p *Provider
id string
}
// speechRequest is the OpenAI /v1/audio/speech request shape. llama-swap
// routes by the `model` field in the body.
type speechRequest struct {
Model string `json:"model"`
Input string `json:"input"`
Voice string `json:"voice,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
Speed float64 `json:"speed,omitempty"`
}
// Speak implements audio.SpeechModel via POST {base}/v1/audio/speech.
func (m *speechModel) Speak(ctx context.Context, req majaudio.SpeechRequest, opts ...majaudio.SpeechOption) (*majaudio.SpeechResult, error) {
req = req.Apply(opts...)
if strings.TrimSpace(req.Input) == "" {
return nil, fmt.Errorf("%w: speech synthesis requires input text", llm.ErrUnsupported)
}
wire := speechRequest{
Model: m.id,
Input: req.Input,
Voice: req.Voice,
ResponseFormat: req.Format,
Speed: req.Speed,
}
body, err := json.Marshal(wire)
if err != nil {
return nil, fmt.Errorf("llama-swap: encode speech request: %w", err)
}
audioBytes, contentType, err := m.p.doRaw(ctx, http.MethodPost, "/v1/audio/speech", m.id, "application/json", bytes.NewReader(body))
if err != nil {
return nil, err
}
if len(audioBytes) == 0 {
return nil, &llm.APIError{Provider: m.p.name, Model: m.id, Message: "speech response contained no audio"}
}
return &majaudio.SpeechResult{Audio: audioBytes, MIME: speechMIME(contentType, req.Format)}, nil
}
// speechMIME resolves the result MIME type: the response Content-Type when it
// is a concrete audio type, else a mapping from the requested format, else
// audio/mpeg (the OpenAI endpoint's default container is mp3).
func speechMIME(contentType, format string) string {
if mt, _, err := mime.ParseMediaType(contentType); err == nil {
if strings.HasPrefix(mt, "audio/") || strings.HasPrefix(mt, "video/") {
return mt
}
}
switch strings.ToLower(strings.TrimSpace(format)) {
case "", "mp3":
return "audio/mpeg"
case "wav":
return "audio/wav"
case "opus":
return "audio/ogg"
case "aac":
return "audio/aac"
case "flac":
return "audio/flac"
case "pcm":
return "audio/pcm"
default:
return "audio/mpeg"
}
}
// TranscriptionModel implements audio.TranscriptionProvider, binding a
// speech-to-text model served by llama-swap (routed to a whisper.cpp-style
// upstream).
func (p *Provider) TranscriptionModel(id string, opts ...majaudio.TranscriptionModelOption) (majaudio.TranscriptionModel, error) {
if p.baseURL == "" {
return nil, fmt.Errorf("llama-swap provider %q: no base URL configured (set one via WithBaseURL or an LLM_* env DSN)", p.name)
}
_ = majaudio.ApplyTranscriptionModelOptions(opts)
return &transcriptionModel{p: p, id: id}, nil
}
type transcriptionModel struct {
p *Provider
id string
}
// Transcribe implements audio.TranscriptionModel via POST
// {base}/v1/audio/transcriptions (multipart/form-data — llama-swap routes by
// the `model` form field).
func (m *transcriptionModel) Transcribe(ctx context.Context, req majaudio.TranscriptionRequest, opts ...majaudio.TranscriptionOption) (*majaudio.TranscriptionResult, error) {
req = req.Apply(opts...)
if len(req.Audio) == 0 {
return nil, fmt.Errorf("%w: transcription requires audio bytes", llm.ErrUnsupported)
}
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
fw, err := w.CreateFormFile("file", transcriptionFilename(req))
if err != nil {
return nil, fmt.Errorf("llama-swap: build transcription form: %w", err)
}
if _, err := fw.Write(req.Audio); err != nil {
return nil, fmt.Errorf("llama-swap: build transcription form: %w", err)
}
fields := map[string]string{
"model": m.id,
"language": req.Language,
"prompt": req.Prompt,
"response_format": "json",
}
for k, v := range fields {
if v == "" {
continue
}
if err := w.WriteField(k, v); err != nil {
return nil, fmt.Errorf("llama-swap: build transcription form: %w", err)
}
}
if err := w.Close(); err != nil {
return nil, fmt.Errorf("llama-swap: build transcription form: %w", err)
}
raw, _, err := m.p.doRaw(ctx, http.MethodPost, "/v1/audio/transcriptions", m.id, w.FormDataContentType(), &buf)
if err != nil {
return nil, err
}
var out struct {
Text string `json:"text"`
}
if err := json.Unmarshal(raw, &out); err != nil {
return nil, fmt.Errorf("llama-swap: decode transcription response: %w", err)
}
return &majaudio.TranscriptionResult{Text: out.Text, Raw: json.RawMessage(raw)}, nil
}
// transcriptionFilename picks the multipart filename hint: the caller's, else
// one derived from the MIME subtype ("audio.mp3"), else "audio".
func transcriptionFilename(req majaudio.TranscriptionRequest) string {
if req.Filename != "" {
return req.Filename
}
switch strings.ToLower(req.MIME) {
case "audio/mpeg", "audio/mp3":
return "audio.mp3"
case "audio/wav", "audio/x-wav", "audio/wave":
return "audio.wav"
case "audio/ogg", "audio/opus":
return "audio.ogg"
case "audio/flac", "audio/x-flac":
return "audio.flac"
case "audio/mp4", "audio/m4a", "audio/x-m4a":
return "audio.m4a"
case "audio/webm", "video/webm":
return "audio.webm"
default:
return "audio"
}
}
// ListVoices returns the voices a TTS model offers (GET
// /v1/audio/voices?model=...). The decode is tolerant: upstreams answer with
// either a bare string list or a list of {id|name} objects.
func (p *Provider) ListVoices(ctx context.Context, model string) ([]string, error) {
if model == "" {
return nil, fmt.Errorf("llama-swap: ListVoices requires a model id")
}
raw, _, err := p.doRaw(ctx, http.MethodGet, "/v1/audio/voices?model="+url.QueryEscape(model), model, "", nil)
if err != nil {
return nil, err
}
return parseVoices(raw)
}
// parseVoices extracts voice names from the various shapes upstreams use:
// {"voices":[...]} or {"data":[...]} envelopes (or a bare array), holding
// either strings or objects keyed by id/name/voice_id.
func parseVoices(raw []byte) ([]string, error) {
var env struct {
Voices json.RawMessage `json:"voices"`
Data json.RawMessage `json:"data"`
}
list := json.RawMessage(raw)
if err := json.Unmarshal(raw, &env); err == nil {
if len(env.Voices) > 0 {
list = env.Voices
} else if len(env.Data) > 0 {
list = env.Data
}
}
var names []string
if err := json.Unmarshal(list, &names); err == nil {
return names, nil
}
// A failed decode above may have partially populated names — start fresh.
names = nil
var objs []struct {
ID string `json:"id"`
Name string `json:"name"`
VoiceID string `json:"voice_id"`
}
if err := json.Unmarshal(list, &objs); err == nil {
for _, o := range objs {
switch {
case o.ID != "":
names = append(names, o.ID)
case o.Name != "":
names = append(names, o.Name)
case o.VoiceID != "":
names = append(names, o.VoiceID)
}
}
return names, nil
}
return nil, fmt.Errorf("llama-swap: unrecognized voices payload shape")
}
// doRaw performs a request to a llama-swap endpoint and returns the raw
// response body and its Content-Type — the sibling of doJSON for endpoints
// whose success payload is not JSON (audio bytes) or whose shape varies.
// contentType sets the request Content-Type when body is non-nil.
func (p *Provider) doRaw(ctx context.Context, method, path, model, contentType string, body io.Reader) ([]byte, string, error) {
if p.baseURL == "" {
return nil, "", fmt.Errorf("llama-swap provider %q: no base URL configured (set one via WithBaseURL or an LLM_* env DSN)", p.name)
}
req, err := http.NewRequestWithContext(ctx, method, p.baseURL+path, body)
if err != nil {
return nil, "", fmt.Errorf("llama-swap: build request: %w", err)
}
if body != nil && contentType != "" {
req.Header.Set("Content-Type", contentType)
}
if p.token != "" {
req.Header.Set("Authorization", "Bearer "+p.token)
}
resp, err := p.client.Do(req)
if err != nil {
return nil, "", fmt.Errorf("llama-swap: do request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
return nil, "", p.apiError(resp, model)
}
data, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseBytes))
if err != nil {
return nil, "", fmt.Errorf("llama-swap: read response: %w", err)
}
return data, resp.Header.Get("Content-Type"), nil
}
+247
View File
@@ -0,0 +1,247 @@
package llamaswap
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"gitea.stevedudenhoeffer.com/steve/majordomo/audio"
"gitea.stevedudenhoeffer.com/steve/majordomo/llm"
)
func TestSpeak(t *testing.T) {
var gotBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/audio/speech" {
t.Errorf("path = %q", r.URL.Path)
}
if r.Header.Get("Authorization") != "Bearer tok" {
t.Errorf("auth = %q", r.Header.Get("Authorization"))
}
_ = json.NewDecoder(r.Body).Decode(&gotBody)
w.Header().Set("Content-Type", "audio/mpeg")
_, _ = w.Write([]byte("MP3BYTES"))
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithToken("tok"), WithHTTPClient(srv.Client()))
sm, err := p.SpeechModel("kokoro")
if err != nil {
t.Fatalf("SpeechModel: %v", err)
}
res, err := sm.Speak(context.Background(),
audio.SpeechRequest{Input: "hello world"},
audio.WithVoice("af_heart"), audio.WithFormat("mp3"), audio.WithSpeed(1.2),
)
if err != nil {
t.Fatalf("Speak: %v", err)
}
if string(res.Audio) != "MP3BYTES" || res.MIME != "audio/mpeg" {
t.Errorf("result = %q %q", res.Audio, res.MIME)
}
want := map[string]any{"model": "kokoro", "input": "hello world", "voice": "af_heart", "response_format": "mp3", "speed": 1.2}
for k, w := range want {
if gotBody[k] != w {
t.Errorf("%s = %v, want %v", k, gotBody[k], w)
}
}
}
func TestSpeakDefaultsOmitted(t *testing.T) {
var gotBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = json.NewDecoder(r.Body).Decode(&gotBody)
_, _ = w.Write([]byte("x"))
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()))
sm, _ := p.SpeechModel("kokoro")
res, err := sm.Speak(context.Background(), audio.SpeechRequest{Input: "hi"})
if err != nil {
t.Fatalf("Speak: %v", err)
}
for _, k := range []string{"voice", "response_format", "speed"} {
if v, ok := gotBody[k]; ok {
t.Errorf("unset request sent %q = %v, want omitted", k, v)
}
}
// No usable Content-Type and no requested format → mp3 default.
if res.MIME != "audio/mpeg" {
t.Errorf("MIME = %q, want audio/mpeg fallback", res.MIME)
}
}
func TestSpeakEmptyInput(t *testing.T) {
p := New(WithBaseURL("http://example.invalid"))
sm, _ := p.SpeechModel("kokoro")
if _, err := sm.Speak(context.Background(), audio.SpeechRequest{Input: " "}); !errors.Is(err, llm.ErrUnsupported) {
t.Errorf("err = %v, want ErrUnsupported", err)
}
}
func TestSpeechMIME(t *testing.T) {
cases := []struct {
contentType, format, want string
}{
{"audio/ogg; codecs=opus", "mp3", "audio/ogg"}, // concrete header wins
{"application/octet-stream", "wav", "audio/wav"},
{"", "", "audio/mpeg"},
{"", "opus", "audio/ogg"},
{"text/plain", "flac", "audio/flac"},
}
for _, tc := range cases {
if got := speechMIME(tc.contentType, tc.format); got != tc.want {
t.Errorf("speechMIME(%q, %q) = %q, want %q", tc.contentType, tc.format, got, tc.want)
}
}
}
func TestTranscribe(t *testing.T) {
var gotFields map[string]string
var gotFile []byte
var gotFilename string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/audio/transcriptions" {
t.Errorf("path = %q", r.URL.Path)
}
if err := r.ParseMultipartForm(1 << 20); err != nil {
t.Fatalf("parse multipart: %v", err)
}
gotFields = map[string]string{}
for k, v := range r.MultipartForm.Value {
gotFields[k] = v[0]
}
f, hdr, err := r.FormFile("file")
if err != nil {
t.Fatalf("form file: %v", err)
}
defer f.Close()
gotFile, _ = io.ReadAll(f)
gotFilename = hdr.Filename
_, _ = w.Write([]byte(`{"text":"hello there"}`))
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()))
tm, err := p.TranscriptionModel("whisper")
if err != nil {
t.Fatalf("TranscriptionModel: %v", err)
}
res, err := tm.Transcribe(context.Background(),
audio.TranscriptionRequest{Audio: []byte("AUDIO"), MIME: "audio/mpeg"},
audio.WithLanguage("en"), audio.WithPrompt("robot names"),
)
if err != nil {
t.Fatalf("Transcribe: %v", err)
}
if res.Text != "hello there" {
t.Errorf("text = %q", res.Text)
}
if string(gotFile) != "AUDIO" || gotFilename != "audio.mp3" {
t.Errorf("file = %q name = %q", gotFile, gotFilename)
}
want := map[string]string{"model": "whisper", "language": "en", "prompt": "robot names", "response_format": "json"}
for k, w := range want {
if gotFields[k] != w {
t.Errorf("%s = %q, want %q", k, gotFields[k], w)
}
}
}
func TestTranscribeEmptyAudio(t *testing.T) {
p := New(WithBaseURL("http://example.invalid"))
tm, _ := p.TranscriptionModel("whisper")
if _, err := tm.Transcribe(context.Background(), audio.TranscriptionRequest{}); !errors.Is(err, llm.ErrUnsupported) {
t.Errorf("err = %v, want ErrUnsupported", err)
}
}
func TestListVoices(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/audio/voices" {
t.Errorf("path = %q", r.URL.Path)
}
if got := r.URL.Query().Get("model"); got != "kokoro" {
t.Errorf("model = %q", got)
}
_, _ = w.Write([]byte(`{"voices":["af_heart","af_bella"]}`))
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()))
voices, err := p.ListVoices(context.Background(), "kokoro")
if err != nil {
t.Fatalf("ListVoices: %v", err)
}
if !reflect.DeepEqual(voices, []string{"af_heart", "af_bella"}) {
t.Errorf("voices = %v", voices)
}
if _, err := p.ListVoices(context.Background(), ""); err == nil {
t.Error("empty model: want error")
}
}
func TestParseVoicesShapes(t *testing.T) {
cases := []struct {
name string
raw string
want []string
}{
{"envelope strings", `{"voices":["a","b"]}`, []string{"a", "b"}},
{"bare array", `["a","b"]`, []string{"a", "b"}},
{"objects by id", `{"voices":[{"id":"a"},{"id":"b"}]}`, []string{"a", "b"}},
{"objects by name", `{"data":[{"name":"a"},{"voice_id":"b"}]}`, []string{"a", "b"}},
}
for _, tc := range cases {
got, err := parseVoices([]byte(tc.raw))
if err != nil {
t.Errorf("%s: %v", tc.name, err)
continue
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("%s: got %v, want %v", tc.name, got, tc.want)
}
}
if _, err := parseVoices([]byte(`"just a string"`)); err == nil {
t.Error("unparseable shape: want error")
}
}
func TestAudioAPIError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write([]byte(`{"error":{"message":"model is loading"}}`))
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()))
sm, _ := p.SpeechModel("kokoro")
_, err := sm.Speak(context.Background(), audio.SpeechRequest{Input: "x"})
var apiErr *llm.APIError
if !errors.As(err, &apiErr) {
t.Fatalf("err = %T %v, want *llm.APIError", err, err)
}
if apiErr.Status != http.StatusServiceUnavailable || apiErr.Message != "model is loading" || apiErr.Model != "kokoro" {
t.Errorf("apiErr = %+v", apiErr)
}
}
func TestAudioNoBaseURL(t *testing.T) {
p := New()
if _, err := p.SpeechModel("kokoro"); err == nil {
t.Error("SpeechModel: want error without base URL")
}
if _, err := p.TranscriptionModel("whisper"); err == nil {
t.Error("TranscriptionModel: want error without base URL")
}
if _, err := p.ListVoices(context.Background(), "kokoro"); err == nil {
t.Error("ListVoices: want error without base URL")
}
}
+110
View File
@@ -0,0 +1,110 @@
package llamaswap
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"gitea.stevedudenhoeffer.com/steve/majordomo/imagegen"
"gitea.stevedudenhoeffer.com/steve/majordomo/llm"
)
func editInit(t *testing.T) imagegen.Image {
t.Helper()
raw, err := base64.StdEncoding.DecodeString(onePixelPNG)
if err != nil {
t.Fatalf("decode fixture: %v", err)
}
return imagegen.Image{MIME: "image/png", Data: raw}
}
func TestImageEdit(t *testing.T) {
var gotBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/sdapi/v1/img2img" {
t.Errorf("path = %q", r.URL.Path)
}
_ = json.NewDecoder(r.Body).Decode(&gotBody)
_, _ = w.Write([]byte(`{"images":["` + onePixelPNG + `"]}`))
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()))
im, _ := p.ImageModel("sd")
ed, ok := im.(imagegen.Editor)
if !ok {
t.Fatal("imageModel does not implement imagegen.Editor")
}
res, err := ed.Edit(context.Background(),
imagegen.EditRequest{Prompt: "make it night", Init: editInit(t)},
imagegen.WithEditStrength(0.6),
)
if err != nil {
t.Fatalf("Edit: %v", err)
}
if len(res.Images) != 1 || res.Images[0].MIME != "image/png" {
t.Fatalf("images = %+v", res.Images)
}
if gotBody["model"] != "sd" || gotBody["prompt"] != "make it night" {
t.Errorf("model/prompt = %v/%v", gotBody["model"], gotBody["prompt"])
}
inits, ok := gotBody["init_images"].([]any)
if !ok || len(inits) != 1 || inits[0] != onePixelPNG {
t.Errorf("init_images = %v, want the b64 fixture", gotBody["init_images"])
}
if gotBody["denoising_strength"] != 0.6 {
t.Errorf("denoising_strength = %v, want 0.6", gotBody["denoising_strength"])
}
}
func TestImageEditOmitsUnsetOverrides(t *testing.T) {
var gotBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = json.NewDecoder(r.Body).Decode(&gotBody)
_, _ = w.Write([]byte(`{"images":["` + onePixelPNG + `"]}`))
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()))
im, _ := p.ImageModel("sd")
ed := im.(imagegen.Editor)
if _, err := ed.Edit(context.Background(), imagegen.EditRequest{Prompt: "x", Init: editInit(t)}); err != nil {
t.Fatalf("Edit: %v", err)
}
for _, k := range []string{"denoising_strength", "steps", "cfg_scale", "negative_prompt", "sample_method", "seed", "width", "height"} {
if v, ok := gotBody[k]; ok {
t.Errorf("unset request sent %q = %v, want omitted", k, v)
}
}
}
func TestImageEditValidation(t *testing.T) {
p := New(WithBaseURL("http://example.invalid"))
im, _ := p.ImageModel("sd")
ed := im.(imagegen.Editor)
cases := []struct {
name string
req imagegen.EditRequest
}{
{"empty prompt", imagegen.EditRequest{Prompt: " ", Init: imagegen.Image{Data: []byte{1}}}},
{"missing init", imagegen.EditRequest{Prompt: "x"}},
{"negative N", imagegen.EditRequest{Prompt: "x", Init: imagegen.Image{Data: []byte{1}}, N: -1}},
}
for _, tc := range cases {
if _, err := ed.Edit(context.Background(), tc.req); !errors.Is(err, llm.ErrUnsupported) {
t.Errorf("%s: err = %v, want ErrUnsupported", tc.name, err)
}
}
bad := 1.5
if _, err := ed.Edit(context.Background(), imagegen.EditRequest{Prompt: "x", Init: imagegen.Image{Data: []byte{1}}, Strength: &bad}); !errors.Is(err, llm.ErrUnsupported) {
t.Errorf("out-of-range strength: err = %v, want ErrUnsupported", err)
}
}
+45
View File
@@ -0,0 +1,45 @@
package llamaswap
import (
"context"
"fmt"
"io"
"net/http"
)
// Health reports whether the llama-swap instance is reachable (GET
// {base}/health, which answers "OK" without touching any model). A nil error
// means the proxy itself is up — it says nothing about how long a subsequent
// request will take (a cold model swap can still block for minutes). Bound
// the probe with a short context deadline; the client has no timeout by
// design.
func (p *Provider) Health(ctx context.Context) error {
if p.baseURL == "" {
return fmt.Errorf("llama-swap provider %q: no base URL configured (set one via WithBaseURL or an LLM_* env DSN)", p.name)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.baseURL+"/health", nil)
if err != nil {
return fmt.Errorf("llama-swap: build request: %w", err)
}
if p.token != "" {
req.Header.Set("Authorization", "Bearer "+p.token)
}
resp, err := p.client.Do(req)
if err != nil {
return fmt.Errorf("llama-swap: health probe: %w", err)
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<10))
if resp.StatusCode/100 != 2 {
return &apiHealthError{status: resp.StatusCode}
}
return nil
}
// apiHealthError distinguishes "reachable but unhealthy" from transport
// failure without pulling in the llm error taxonomy for a probe.
type apiHealthError struct{ status int }
func (e *apiHealthError) Error() string {
return fmt.Sprintf("llama-swap: health endpoint returned status %d", e.status)
}
+59
View File
@@ -0,0 +1,59 @@
package llamaswap
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestHealth(t *testing.T) {
var gotPath, gotAuth string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotAuth = r.Header.Get("Authorization")
_, _ = w.Write([]byte("OK"))
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithToken("tok"), WithHTTPClient(srv.Client()))
if err := p.Health(context.Background()); err != nil {
t.Fatalf("Health: %v", err)
}
if gotPath != "/health" || gotAuth != "Bearer tok" {
t.Errorf("path/auth = %q/%q", gotPath, gotAuth)
}
}
func TestHealthUnhealthyStatus(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadGateway)
}))
defer srv.Close()
p := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()))
err := p.Health(context.Background())
if err == nil || !strings.Contains(err.Error(), "502") {
t.Errorf("err = %v, want status-502 error", err)
}
}
func TestHealthUnreachable(t *testing.T) {
// A closed server: transport error, not an HTTP status.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
url := srv.URL
srv.Close()
p := New(WithBaseURL(url))
if err := p.Health(context.Background()); err == nil {
t.Error("want error for unreachable host")
}
}
func TestHealthNoBaseURL(t *testing.T) {
p := New()
if err := p.Health(context.Background()); err == nil {
t.Error("want error without base URL")
}
}
+64 -3
View File
@@ -85,8 +85,13 @@ func (m *imageModel) Generate(ctx context.Context, req imagegen.Request, opts ..
if err := m.p.doJSON(ctx, http.MethodPost, "/sdapi/v1/txt2img", m.id, &wire, &resp); err != nil {
return nil, err
}
return decodeImages(m.p.name, m.id, &resp)
}
out := &imagegen.Result{Raw: &resp}
// decodeImages converts an SDAPI response's base64 images into an
// imagegen.Result, erroring when nothing decodable came back.
func decodeImages(provider, model string, resp *txt2imgResponse) (*imagegen.Result, error) {
out := &imagegen.Result{Raw: resp}
for i, b64 := range resp.Images {
if b64 == "" {
continue
@@ -99,14 +104,70 @@ func (m *imageModel) Generate(ctx context.Context, req imagegen.Request, opts ..
}
if len(out.Images) == 0 {
return nil, &llm.APIError{
Provider: m.p.name,
Model: m.id,
Provider: provider,
Model: model,
Message: "image response contained no images",
}
}
return out, nil
}
// img2imgRequest is the stable-diffusion.cpp sd-server A1111 request shape
// (POST /sdapi/v1/img2img): txt2img's fields plus the init image(s) and
// denoising strength. Same endpoint-family choice as txt2img — the OpenAI
// /v1/images/edits route is multipart and drops `seed` on this sd-server
// build, while the SDAPI shape reuses doJSON and keeps seed parity.
type img2imgRequest struct {
txt2imgRequest
InitImages []string `json:"init_images"`
DenoisingStrength *float64 `json:"denoising_strength,omitempty"`
}
// Edit implements imagegen.Editor via POST {base}/sdapi/v1/img2img.
func (m *imageModel) Edit(ctx context.Context, req imagegen.EditRequest, opts ...imagegen.EditOption) (*imagegen.Result, error) {
req = req.Apply(opts...)
if strings.TrimSpace(req.Prompt) == "" {
return nil, fmt.Errorf("%w: image edit requires a prompt", llm.ErrUnsupported)
}
if len(req.Init.Data) == 0 {
return nil, fmt.Errorf("%w: image edit requires an init image", llm.ErrUnsupported)
}
if req.N < 0 {
return nil, fmt.Errorf("%w: image count N must be >= 0, got %d", llm.ErrUnsupported, req.N)
}
if req.Strength != nil && (*req.Strength < 0 || *req.Strength > 1) {
return nil, fmt.Errorf("%w: edit strength must be in [0,1], got %g", llm.ErrUnsupported, *req.Strength)
}
width, height, err := parseSize(req.Size)
if err != nil {
return nil, fmt.Errorf("%w: %v", llm.ErrUnsupported, err)
}
wire := img2imgRequest{
txt2imgRequest: txt2imgRequest{
Model: m.id,
Prompt: req.Prompt,
NegativePrompt: req.NegativePrompt,
Seed: req.Seed,
Steps: req.Steps,
CFGScale: req.CFGScale,
Width: width,
Height: height,
SampleMethod: req.Sampler,
BatchCount: req.N,
},
InitImages: []string{base64.StdEncoding.EncodeToString(req.Init.Data)},
DenoisingStrength: req.Strength,
}
var resp txt2imgResponse
if err := m.p.doJSON(ctx, http.MethodPost, "/sdapi/v1/img2img", m.id, &wire, &resp); err != nil {
return nil, err
}
return decodeImages(m.p.name, m.id, &resp)
}
// parseSize splits a "WxH" string into width/height pointers. "" yields
// (nil, nil) so the model's own default resolution applies.
func parseSize(size string) (*int, *int, error) {