Open History to see the last commit
1/**
2 * Provider options and request validation for Seedream image generation.
3 *
4 * Field names, enums, defaults and ranges come from the harvested Ark
5 * OpenAPI document for the `ImageGenerations` action; the Seedream 4.0
6 * behaviour noted below was confirmed live on 2026-07-31.
7 */
8import { BYTEPLUS_IMAGE_MAX_REFERENCE_IMAGES } from '../model-meta'
9import type {
10 BytePlusImageOutputFormat,
11 BytePlusImageResponseFormat,
12 BytePlusOptimizePromptOptions,
13 BytePlusSequentialImageGeneration,
14 BytePlusSequentialImageGenerationOptions,
15} from './wire-types'
16import type { BytePlusImageModel, BytePlusImageSize } from '../model-meta'
17
18/**
19 * BytePlus documents a 600-word ceiling on the image prompt. Word-based, so
20 * it is only meaningful for space-separated scripts — the check below never
21 * fires for Chinese or Japanese text, which is the intended behaviour.
22 */
23export const BYTEPLUS_IMAGE_MAX_PROMPT_WORDS = 600
24
25/**
26 * Upper bound of `sequential_image_generation_options.max_images`, i.e. the
27 * most images one request can return.
28 */
29export const BYTEPLUS_IMAGE_MAX_SEQUENTIAL_IMAGES = 15
30
31/**
32 * Models that accept `output_format`.
33 *
34 * The Ark OpenAPI document's field note claims 5.0-lite only, but its own
35 * request demo sends `output_format` on `seedream-5-0-260128`, so the whole
36 * 5.0 family is treated as supporting it. The Seedream 4.x snapshots are
37 * documented as not reading it, so `output_format` is omitted from their
38 * provider-options type — but, as with `sequential_image_generation`, it is
39 * not gated at runtime: a value that reaches a model which does not read it
40 * comes back as an Ark error rather than a local rejection.
41 */
42export const BYTEPLUS_OUTPUT_FORMAT_IMAGE_MODELS: ReadonlyArray<BytePlusImageModel> =
43 [
44 'dola-seedream-5-0-pro-260628',
45 'seedream-5-0-260128',
46 'seedream-5-0-lite-260128',
47 ]
48
49/**
50 * Base provider options shared by every Seedream model.
51 */
52export interface BytePlusImageBaseProviderOptions {
53 /**
54 * Return images as expiring links (`url`, valid 24 hours) or inline base64
55 * (`b64_json`).
56 *
57 * @default 'url'
58 */
59 response_format?: BytePlusImageResponseFormat
60
61 /**
62 * Whether to stamp an "AI generated" watermark in the bottom-right corner.
63 *
64 * **BytePlus defaults this to `true`.** Pass `false` for a clean image.
65 */
66 watermark?: boolean
67
68 /**
69 * Group-image mode. Set to `auto` to let the model return a set of related
70 * images (bounded by {@link BytePlusImageBaseProviderOptions.sequential_image_generation_options}).
71 * `generateImage()`'s `numberOfImages` sets this for you; an explicit value
72 * here wins.
73 *
74 * Documented on Seedream 5.0-lite, 4.5 and 4.0. It is sent as given on
75 * every model rather than gated locally — the shipped 5.0 ids post-date the
76 * published parameter table, and an unsupported combination comes back as a
77 * clear Ark error.
78 *
79 * @default 'disabled'
80 */
81 sequential_image_generation?: BytePlusSequentialImageGeneration
82
83 /** Bounds for group-image mode. Only read when the mode is `auto`. */
84 sequential_image_generation_options?: BytePlusSequentialImageGenerationOptions
85
86 /**
87 * Prompt-rewriting configuration. Documented on Seedream 5.0-lite, 4.5 and
88 * 4.0; `mode: 'fast'` is unsupported on 5.0-lite and 4.5.
89 */
90 optimize_prompt_options?: BytePlusOptimizePromptOptions
91}
92
93/**
94 * Provider options for the Seedream 5.0 family, which additionally chooses the
95 * generated file format.
96 */
97export interface BytePlusSeedream5ImageProviderOptions extends BytePlusImageBaseProviderOptions {
98 /**
99 * File format of the generated image.
100 *
101 * @default 'jpeg'
102 */
103 output_format?: BytePlusImageOutputFormat
104}
105
106/**
107 * Every Seedream provider option, used as the adapter's base option type.
108 * Call sites are narrowed per model by
109 * {@link BytePlusImageModelProviderOptionsByName}.
110 */
111export type BytePlusImageProviderOptions = BytePlusSeedream5ImageProviderOptions
112
113/**
114 * Type-only map from image model name to its provider options.
115 */
116export type BytePlusImageModelProviderOptionsByName = {
117 'dola-seedream-5-0-pro-260628': BytePlusSeedream5ImageProviderOptions
118 'seedream-5-0-260128': BytePlusSeedream5ImageProviderOptions
119 'seedream-5-0-lite-260128': BytePlusSeedream5ImageProviderOptions
120 'seedream-4-5-251128': BytePlusImageBaseProviderOptions
121 'seedream-4-0-250828': BytePlusImageBaseProviderOptions
122}
123
124/**
125 * Type-only map from image model name to the non-text prompt modalities it
126 * accepts. Every shipped Seedream model takes reference images for
127 * image-conditioned generation.
128 */
129export type BytePlusImageModelInputModalitiesByName = {
130 [K in BytePlusImageModel]: readonly ['image']
131}
132
133/**
134 * A parsed `size` value: either the shorthand token form or explicit pixels.
135 */
136export type ParsedBytePlusImageSize =
137 | { kind: 'token'; value: '1K' | '2K' | '4K' }
138 | { kind: 'pixels'; width: number; height: number }
139
140const SIZE_TOKENS = ['1K', '2K', '4K'] as const
141
142/**
143 * Parses a Seedream `size` string. Accepts a shorthand token (case-insensitive
144 * — `2k` normalizes to `2K`) or explicit `WIDTHxHEIGHT` pixels, and returns
145 * `undefined` for anything else, including mixtures such as `2K x 1024`.
146 */
147export function parseBytePlusImageSize(
148 size: string,
149): ParsedBytePlusImageSize | undefined {
150 const trimmed = size.trim()
151
152 const token = SIZE_TOKENS.find(
153 (candidate) => candidate.toLowerCase() === trimmed.toLowerCase(),
154 )
155 if (token) return { kind: 'token', value: token }
156
157 // ASCII "x" only: the docs render the separator as U+00D7 (`2048×2048`),
158 // which the API does not accept, so it must not slip through here either.
159 const pixels = /^(\d+)[xX](\d+)$/.exec(trimmed)
160 if (pixels) {
161 const width = Number(pixels[1])
162 const height = Number(pixels[2])
163 if (width > 0 && height > 0) return { kind: 'pixels', width, height }
164 }
165
166 return undefined
167}
168
169/**
170 * Validates the generic `size` option and returns the string to put on the
171 * wire (`2K`, `2048x2048`), or `undefined` when no size was requested.
172 *
173 * This checks the *form* only. Which pixel dimensions a given model actually
174 * accepts is not encoded here, so the message deliberately makes no per-model
175 * claim; an out-of-range size is left to the API to reject.
176 *
177 * @throws Error when the value is neither a size token nor `WIDTHxHEIGHT`.
178 */
179export function resolveBytePlusImageSize(
180 size: BytePlusImageSize | string | undefined,
181): string | undefined {
182 if (size === undefined) return undefined
183
184 const parsed = parseBytePlusImageSize(size)
185 if (!parsed) {
186 throw new Error(
187 `byteplus: size "${size}" is not a Seedream size. Use a size token ` +
188 `(${SIZE_TOKENS.join(', ')}) or explicit pixels with an ASCII "x" ` +
189 `("2048x2048") — never a mix of the two.`,
190 )
191 }
192
193 return parsed.kind === 'token'
194 ? parsed.value
195 : `${parsed.width}x${parsed.height}`
196}
197
198/**
199 * Validates the prompt text against BytePlus's documented limits.
200 *
201 * @throws Error when the prompt is empty or exceeds
202 * {@link BYTEPLUS_IMAGE_MAX_PROMPT_WORDS} words.
203 */
204export function validateBytePlusImagePrompt(
205 model: string,
206 prompt: string,
207): void {
208 if (prompt.trim().length === 0) {
209 throw new Error(
210 `byteplus: model "${model}" requires prompt text. Seedream takes an ` +
211 `instruction even when editing reference images.`,
212 )
213 }
214
215 const words = prompt.trim().split(/\s+/).length
216 if (words > BYTEPLUS_IMAGE_MAX_PROMPT_WORDS) {
217 throw new Error(
218 `byteplus: prompt is ${words} words; model "${model}" accepts at most ` +
219 `${BYTEPLUS_IMAGE_MAX_PROMPT_WORDS}.`,
220 )
221 }
222}
223
224/**
225 * Validates the reference-image count against the model's editing limit.
226 *
227 * A model this package has no limit for is left to Ark, deliberately and
228 * explicitly. `model` is typed closed, but `wire-types.ts` documents that the
229 * endpoint also accepts preconfigured endpoint ids (`ep-…`), so a JS caller
230 * can reach an id that is not in the table. Reading `undefined` out of it and
231 * comparing `count > undefined` — always false — would disable the guard by
232 * accident and look identical to passing; the explicit early return says the
233 * skip is intended.
234 *
235 * @throws Error when more references are supplied than a *known* model accepts.
236 */
237export function validateBytePlusReferenceImages(
238 model: BytePlusImageModel,
239 count: number,
240): void {
241 const max = BYTEPLUS_IMAGE_MAX_REFERENCE_IMAGES[model] as number | undefined
242 if (max === undefined) return
243 if (count > max) {
244 throw new Error(
245 `byteplus: model "${model}" accepts at most ${max} reference images; received ${count}.`,
246 )
247 }
248}
249
250/**
251 * Maps the generic `numberOfImages` option onto Seedream's group-image
252 * parameters.
253 *
254 * The endpoint has no `n`: more than one image per request is only reachable
255 * through `sequential_image_generation: 'auto'`, where `max_images` is an
256 * upper bound and the model decides how many images the prompt actually
257 * warrants. A request for N images can therefore come back with fewer — the
258 * one place BytePlus cannot honour `numberOfImages` exactly.
259 *
260 * @throws Error when the count is not an integer in `[1, 15]`.
261 */
262export function resolveBytePlusSequentialImages(
263 model: string,
264 numberOfImages: number | undefined,
265): {
266 sequential_image_generation?: BytePlusSequentialImageGeneration
267 sequential_image_generation_options?: BytePlusSequentialImageGenerationOptions
268} {
269 if (numberOfImages === undefined) return {}
270
271 if (
272 !Number.isInteger(numberOfImages) ||
273 numberOfImages < 1 ||
274 numberOfImages > BYTEPLUS_IMAGE_MAX_SEQUENTIAL_IMAGES
275 ) {
276 throw new Error(
277 `byteplus: numberOfImages must be a whole number between 1 and ` +
278 `${BYTEPLUS_IMAGE_MAX_SEQUENTIAL_IMAGES} on model "${model}"; received ${numberOfImages}.`,
279 )
280 }
281
282 if (numberOfImages === 1) return {}
283
284 return {
285 sequential_image_generation: 'auto',
286 sequential_image_generation_options: { max_images: numberOfImages },
287 }
288}