mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
* feat(images): wire /v1/images/generations to do-ai fal diffusion (zen3-image family)
The zen3-image diffusion family was catalog-ware: advertised in /v1/models but
POST /v1/images/generations 404'd and the models were treated as Chat. This
completes the OpenAI-compatible image path end to end.
Backend: the family routes to do-ai's fal-hosted diffusion models, invoked over
DigitalOcean's ASYNC image API (submit /async-invoke -> poll status -> retrieve),
NOT the OpenAI /v1/images/generations shape. FLUX.1 schnell is the flagship;
SDXL serves the -sdxl/-ssd variants. Verified live against inference.do-ai.run:
fal-ai/flux/schnell and fal-ai/fast-sdxl both return real image URLs.
- model/doai_image.go: one async image client (GenerateImageDOAI), bounded poll,
ctx-cancellable, key only in the Authorization header. Both callers drive it —
the new endpoint and the chat-stream image branch (DRY).
- getOpenAiModelType: recognize the zen3-image family + fal ids as image models
(fixes the chat-image path too).
- controllers/images_api.go: ImagesGenerations() mirrors Embeddings() — shared
auth+routing+billing; returns the OpenAI images JSON {created,data:[{url}]};
per-image balance reservation + settle, billed capped at requested n; provider
category check; authenticate-before-badreq (unauth => 401, never a probe-able
200/400).
- model_routes.go + conf/models.yaml: zen3-image* -> do-ai fal ids, premium,
owned_by hanzo (upstream never leaked).
- model_pricing.go: per-image billing seam (imageCostCents); recordUsage bills
per image when ImageCount>0. model/openai.go: fal/zen3-image price cases.
- routers/router.go: register POST /v1/images/generations (metered by the
balance filter; does its own bearer auth, not admin-gated).
Tests: async client submit/poll/retrieve + error/timeout paths; recognition;
route resolution; per-image cost; response shaping; route registration through
the real beego pipeline. A DO_AI_API_KEY-gated live e2e proves a real round-trip.
* fix(images): clamp n before cost math, truncate upstream errors, gofumpt
Red review follow-ups on #57 (SHIP, 0 blockers):
1. n-cap: clamp n to [1,10] BEFORE imageCostCents/reserveBudget in
ImagesGenerations, mirroring doaiImageSubmit's clamp. Removes the reliance
on int-overflow-rejects-Reserve for money-safety; the cost math is never fed
an unbounded n. The min(len(images), n) settle is unchanged.
2. Truncate the upstream error body surfaced to the API caller: new
shortUpstreamErr trims + caps at 200 chars (with an ellipsis), used at all
three do-ai async call sites (submit/poll/retrieve). The HTTP status/word is
preserved; fal internals are no longer echoed verbatim.
3. gofumpt: `gofumpt -w .` fixes pre-existing whole-repo drift (telemetry.go,
mount.go) so the CI Go-Linter (gofumpt -l .) gate passes. Both drift changes
are cosmetic (a call-arg line-wrap and import ordering).
Tests: added ErrorBodyTruncated + ShortUpstreamErr (model) and NClampCeiling
(controllers). build ./... = 0, vet clean, gofumpt -l . empty, all image
suites + full controllers/routers/root suites green. No go.mod/go.sum change.
77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestGenerateImageDOAI_Live is a REAL end-to-end image round-trip against
|
|
// DigitalOcean's fal image API. It is skipped unless DO_AI_API_KEY is set (the
|
|
// same credential the chat routes resolve), so it never runs — or leaks a key —
|
|
// in an environment without one. Run it where the do-ai key exists:
|
|
//
|
|
// DO_AI_API_KEY=… go test ./model/ -run TestGenerateImageDOAI_Live -v
|
|
//
|
|
// It proves submit → poll → retrieve returns an actual image URL. The key is
|
|
// never printed; only the resulting (public) fal.media URL is logged.
|
|
func TestGenerateImageDOAI_Live(t *testing.T) {
|
|
apiKey := os.Getenv("DO_AI_API_KEY")
|
|
if apiKey == "" {
|
|
t.Skip("DO_AI_API_KEY not set — skipping live do-ai image round-trip")
|
|
}
|
|
|
|
base := os.Getenv("DO_AI_BASE_URL")
|
|
if base == "" {
|
|
base = "https://inference.do-ai.run/v1"
|
|
}
|
|
|
|
model := os.Getenv("DO_AI_IMAGE_MODEL")
|
|
if model == "" {
|
|
model = "fal-ai/flux/schnell"
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 130*time.Second)
|
|
defer cancel()
|
|
|
|
res, err := GenerateImageDOAI(ctx, base, apiKey, ImageGenRequest{
|
|
UpstreamModel: model,
|
|
Prompt: "a photorealistic red fox sitting in fresh snow at golden hour",
|
|
N: 1,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("live image generation failed: %v", err)
|
|
}
|
|
if len(res.Images) == 0 {
|
|
t.Fatal("live image generation returned no images")
|
|
}
|
|
img := res.Images[0]
|
|
if img.URL == "" && img.B64JSON == "" {
|
|
t.Fatal("live image has neither url nor b64_json")
|
|
}
|
|
if img.URL != "" {
|
|
if !strings.HasPrefix(img.URL, "http") {
|
|
t.Fatalf("image url is not an http(s) URL: %q", img.URL)
|
|
}
|
|
t.Logf("LIVE image URL (%s): %s", model, img.URL)
|
|
} else {
|
|
t.Logf("LIVE image returned %d base64 bytes (%s)", len(img.B64JSON), model)
|
|
}
|
|
}
|