|
| 1 | +import { Duration, Effect, Schema } from "effect" |
| 2 | +import type { HttpClientResponse } from "effect/http" |
| 3 | +import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js" |
| 4 | +import { Media } from "../media.js" |
| 5 | +import { MediaProtocol } from "../route/media-protocol.js" |
| 6 | +import { MediaRoute } from "../route/media.js" |
| 7 | +import { AIError, QuotaExceededError, mergeJsonRecords, type OpenString } from "../schema/index.js" |
| 8 | +import { ProviderShared, optionalNull } from "./shared.js" |
| 9 | +import { ImageSizing } from "./utils/image-sizing.js" |
| 10 | +import { MediaInput } from "./utils/media-input.js" |
| 11 | + |
| 12 | +const route = MediaProtocol.identity({ id: "minimax-images", name: "MiniMax Images", provider: "minimax" }) |
| 13 | +export const DEFAULT_BASE_URL = "https://api.minimax.io/v1" |
| 14 | +export const PATH = "/image_generation" |
| 15 | +const OUTPUT_RETENTION = Duration.hours(24) |
| 16 | + |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | +// 1. Public model input |
| 19 | +// --------------------------------------------------------------------------- |
| 20 | + |
| 21 | +/** Provider-native options. Common `aspect` and `resolution` lower to `aspect_ratio` or `width` and `height`. */ |
| 22 | +export type MiniMaxImageOptions = { |
| 23 | + /** Defaults to `base64`, which returns the bytes inline; `url` links expire after 24 hours. */ |
| 24 | + readonly responseFormat?: OpenString<"url" | "base64"> |
| 25 | + readonly seed?: number |
| 26 | + readonly promptOptimizer?: boolean |
| 27 | + /** Native preset; it replaces `aspect` and `resolution`. */ |
| 28 | + readonly aspect_ratio?: string |
| 29 | + /** Native size, 512 to 2048 in steps of 8 (`image-01` only); it replaces `aspect` and `resolution`. */ |
| 30 | + readonly width?: number |
| 31 | + readonly height?: number |
| 32 | +} & Record<string, unknown> |
| 33 | + |
| 34 | +export type Request = ImageRequestFor<MiniMaxImageOptions> |
| 35 | + |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | +// 2. Response schema |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | + |
| 40 | +const MiniMaxImageResponse = Schema.Struct({ |
| 41 | + id: optionalNull(Schema.String), |
| 42 | + data: optionalNull( |
| 43 | + Schema.Struct({ |
| 44 | + image_urls: optionalNull(Schema.Array(Schema.String)), |
| 45 | + image_base64: optionalNull(Schema.Array(Schema.String)), |
| 46 | + }), |
| 47 | + ), |
| 48 | + metadata: optionalNull( |
| 49 | + Schema.Struct({ |
| 50 | + success_count: optionalNull(Schema.Union([Schema.Number, Schema.String])), |
| 51 | + failed_count: optionalNull(Schema.Union([Schema.Number, Schema.String])), |
| 52 | + }), |
| 53 | + ), |
| 54 | + base_resp: optionalNull( |
| 55 | + Schema.Struct({ |
| 56 | + status_code: Schema.Number, |
| 57 | + status_msg: optionalNull(Schema.String), |
| 58 | + }), |
| 59 | + ), |
| 60 | +}) |
| 61 | + |
| 62 | +const FAILURE: Readonly<Record<string, MediaProtocol.Failure>> = { |
| 63 | + 1002: "RateLimit", |
| 64 | + 1004: "Authentication", |
| 65 | + 2013: "InvalidRequest", |
| 66 | + 2049: "Authentication", |
| 67 | +} |
| 68 | +const INSUFFICIENT_BALANCE = 1008 |
| 69 | +const CONTENT_POLICY = 1026 |
| 70 | + |
| 71 | +// --------------------------------------------------------------------------- |
| 72 | +// 5. Request body construction |
| 73 | +// --------------------------------------------------------------------------- |
| 74 | + |
| 75 | +/** MiniMax's `aspect_ratio` presets and the sizes it renders them at. */ |
| 76 | +const PRESETS: Record<string, ImageSizing.Size> = { |
| 77 | + "1:1": { width: 1024, height: 1024 }, |
| 78 | + "16:9": { width: 1280, height: 720 }, |
| 79 | + "4:3": { width: 1152, height: 864 }, |
| 80 | + "3:2": { width: 1248, height: 832 }, |
| 81 | + "2:3": { width: 832, height: 1248 }, |
| 82 | + "3:4": { width: 864, height: 1152 }, |
| 83 | + "9:16": { width: 720, height: 1280 }, |
| 84 | + "21:9": { width: 1344, height: 576 }, |
| 85 | +} |
| 86 | + |
| 87 | +const LIMITS = { multiple: 8, minEdge: 512, maxEdge: 2048 } |
| 88 | + |
| 89 | +const nativeOptions = (options: MiniMaxImageOptions = {}) => { |
| 90 | + const { responseFormat, promptOptimizer, ...native } = options |
| 91 | + return { response_format: responseFormat ?? "base64", prompt_optimizer: promptOptimizer, ...native } |
| 92 | +} |
| 93 | + |
| 94 | +/** `image-01-live` rejects `21:9`. */ |
| 95 | +const presets = (model: string) => |
| 96 | + model === "image-01" ? PRESETS : Object.fromEntries(Object.entries(PRESETS).filter(([key]) => key !== "21:9")) |
| 97 | + |
| 98 | +/** Presets render at about 1K, so they serve a preset aspect with no resolution or `1K`. */ |
| 99 | +const preset = (request: Request) => { |
| 100 | + const aspect = request.aspect ?? "1:1" |
| 101 | + if (!Object.hasOwn(presets(request.model.id), aspect)) return undefined |
| 102 | + if (request.resolution !== undefined && ImageSizing.tier(request.resolution) !== 1) return undefined |
| 103 | + return aspect |
| 104 | +} |
| 105 | + |
| 106 | +/** Only `image-01` takes free `width` and `height`; other models render the presets alone. */ |
| 107 | +const sizing = (request: Request) => { |
| 108 | + if (!ImageSizing.isRequested(request)) return Effect.succeed({}) |
| 109 | + const aspect = preset(request) |
| 110 | + if (aspect !== undefined) return Effect.succeed({ aspect_ratio: aspect }) |
| 111 | + if (request.model.id === "image-01") return ImageSizing.exact(route, request, LIMITS) |
| 112 | + const available = presets(request.model.id) |
| 113 | + const closest = ImageSizing.nearest(request, Object.values(available)) |
| 114 | + const name = Object.keys(available).find((key) => available[key] === closest) |
| 115 | + return Effect.fail( |
| 116 | + route.unsupported( |
| 117 | + request.resolution === undefined ? "media.aspect" : "media.resolution", |
| 118 | + `${request.model.id} can't render ${[request.aspect, request.resolution].filter((value) => value !== undefined).join(" at ")}; the closest size it takes is ${closest.width}x${closest.height} (${name}). Change aspect or resolution, or set aspect_ratio in providerOptions`, |
| 119 | + ), |
| 120 | + ) |
| 121 | +} |
| 122 | + |
| 123 | +const isNativeSize = (options: MiniMaxImageOptions | undefined) => |
| 124 | + options?.aspect_ratio !== undefined || options?.width !== undefined || options?.height !== undefined |
| 125 | + |
| 126 | +// The API only takes a `character` (portrait) subject, one per request. |
| 127 | +const subject = (asset: Media.Asset) => |
| 128 | + ProviderShared.mediaReference(asset, undefined, `${route.name} input`).pipe( |
| 129 | + Effect.map((item) => ({ type: "character", image_file: item.value })), |
| 130 | + ) |
| 131 | + |
| 132 | +const fromRequest = Effect.fn("MiniMaxImages.fromRequest")(function* (request: Request) { |
| 133 | + if ((request.images?.length ?? 0) > 1) |
| 134 | + return yield* route.unsupported("media.images", `${route.name} takes one subject reference image`) |
| 135 | + if (isNativeSize(request.providerOptions) && ImageSizing.isRequested(request)) |
| 136 | + return yield* ProviderShared.invalidRequest( |
| 137 | + "MiniMax providerOptions aspect_ratio, width and height replace aspect and resolution; set one or the other", |
| 138 | + ) |
| 139 | + const references = yield* Effect.forEach(request.images ?? [], subject) |
| 140 | + return MediaProtocol.json( |
| 141 | + mergeJsonRecords( |
| 142 | + { |
| 143 | + model: request.model.id, |
| 144 | + prompt: request.prompt, |
| 145 | + subject_reference: references.length === 0 ? undefined : references, |
| 146 | + n: request.n, |
| 147 | + ...(yield* sizing(request)), |
| 148 | + }, |
| 149 | + nativeOptions(request.providerOptions), |
| 150 | + request.http?.body, |
| 151 | + ) ?? {}, |
| 152 | + ) |
| 153 | +}) |
| 154 | + |
| 155 | +// --------------------------------------------------------------------------- |
| 156 | +// 6. Response decoding |
| 157 | +// --------------------------------------------------------------------------- |
| 158 | + |
| 159 | +const decodeDocument = route.decodeJson(MiniMaxImageResponse) |
| 160 | + |
| 161 | +const decodeResponse = Effect.fn("MiniMaxImages.decodeResponse")(function* ( |
| 162 | + response: HttpClientResponse.HttpClientResponse, |
| 163 | +) { |
| 164 | + const output = yield* decodeDocument(response) |
| 165 | + const decoded = output.value |
| 166 | + // MiniMax answers errors with HTTP 200 and a non-zero `base_resp.status_code`. |
| 167 | + const status = decoded.base_resp |
| 168 | + if (status && status.status_code !== 0) { |
| 169 | + const message = `${route.name} failed (${status.status_code}): ${status.status_msg ?? "unknown error"}` |
| 170 | + if (status.status_code === CONTENT_POLICY) return yield* output.contentPolicy(message) |
| 171 | + if (status.status_code === INSUFFICIENT_BALANCE) |
| 172 | + return yield* new AIError({ reason: new QuotaExceededError({ message, body: output.body, http: output.http }) }) |
| 173 | + return yield* output.ended("failed", message, MediaProtocol.failure(FAILURE, status.status_code)) |
| 174 | + } |
| 175 | + const images = [ |
| 176 | + ...(yield* Effect.forEach(decoded.data?.image_base64 ?? [], (data, index) => |
| 177 | + MediaInput.decodedAsset(output.invalid, `${route.name} result ${index}`, data, undefined), |
| 178 | + )), |
| 179 | + // MiniMax returns no content type for URLs; the media type resolves when the asset is materialized. |
| 180 | + ...(yield* Effect.forEach(decoded.data?.image_urls ?? [], (url) => |
| 181 | + MediaProtocol.expiringUrl(url, OUTPUT_RETENTION), |
| 182 | + )), |
| 183 | + ] |
| 184 | + const failed = Number(decoded.metadata?.failed_count ?? 0) |
| 185 | + if (images.length === 0 && failed > 0) |
| 186 | + return yield* output.contentPolicy(`${route.name} blocked ${failed} image(s) for content safety`) |
| 187 | + if (images.length === 0) return yield* output.invalid(`${route.name} returned no images`) |
| 188 | + return new ImageResponse({ |
| 189 | + images, |
| 190 | + providerMetadata: { |
| 191 | + minimax: { |
| 192 | + id: decoded.id ?? undefined, |
| 193 | + successCount: decoded.metadata?.success_count ?? undefined, |
| 194 | + failedCount: decoded.metadata?.failed_count ?? undefined, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }) |
| 198 | +}) |
| 199 | + |
| 200 | +// --------------------------------------------------------------------------- |
| 201 | +// 7. Protocol and route |
| 202 | +// --------------------------------------------------------------------------- |
| 203 | + |
| 204 | +export const protocol = MediaProtocol.inline<Request, ImageResponse>(route, { |
| 205 | + body: { from: fromRequest }, |
| 206 | + response: { decode: decodeResponse }, |
| 207 | +}) |
| 208 | + |
| 209 | +export const model = (input: MediaRoute.ModelInput) => |
| 210 | + ImageModel.fromRoute<MiniMaxImageOptions>({ protocol, baseURL: DEFAULT_BASE_URL, path: PATH }, input) |
| 211 | + |
| 212 | +export const MiniMaxImages = { |
| 213 | + protocol, |
| 214 | + model, |
| 215 | +} as const |
0 commit comments