Skip to content

Commit 972dde2

Browse files
committed
refactor(media): share image and video geometry normalization
1 parent 8f2850d commit 972dde2

7 files changed

Lines changed: 252 additions & 454 deletions

File tree

Lines changed: 44 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
/** Normalizes image generation request overrides against provider/model capabilities. */
2-
import {
3-
hasMediaNormalizationEntry,
4-
resolveClosestAspectRatio,
5-
resolveClosestResolution,
6-
resolveClosestSize,
7-
type MediaNormalizationEntry,
8-
} from "../media-generation/runtime-shared.js";
2+
import { resolveMediaGeometryOverrides } from "../media-generation/geometry-normalization.js";
3+
import { hasMediaNormalizationEntry } from "../media-generation/runtime-shared.js";
94
import type {
105
ImageGenerationBackground,
116
ImageGenerationIgnoredOverride,
@@ -28,16 +23,6 @@ type ResolvedImageGenerationOverrides = {
2823
normalization?: ImageGenerationNormalization;
2924
};
3025

31-
function finalizeImageNormalization(
32-
normalization: ImageGenerationNormalization,
33-
): ImageGenerationNormalization | undefined {
34-
return hasMediaNormalizationEntry(normalization.size) ||
35-
hasMediaNormalizationEntry(normalization.aspectRatio) ||
36-
hasMediaNormalizationEntry(normalization.resolution)
37-
? normalization
38-
: undefined;
39-
}
40-
4126
/** Returns supported image overrides plus ignored/normalized override metadata for replies. */
4227
export function resolveImageGenerationOverrides(params: {
4328
provider: ImageGenerationProvider;
@@ -50,216 +35,65 @@ export function resolveImageGenerationOverrides(params: {
5035
background?: ImageGenerationBackground;
5136
inputImages?: ImageGenerationSourceImage[];
5237
}): ResolvedImageGenerationOverrides {
53-
const hasInputImages = (params.inputImages?.length ?? 0) > 0;
54-
// Edit and generate modes can expose different knobs for the same provider,
55-
// so normalize requested overrides against the active mode only.
56-
const modeCaps = hasInputImages
38+
// Edit and generate modes can expose different knobs for the same provider.
39+
const modeCaps = params.inputImages?.length
5740
? params.provider.capabilities.edit
5841
: params.provider.capabilities.generate;
5942
const geometry = params.provider.capabilities.geometry;
60-
const modelGeometry = {
61-
sizes: params.model
62-
? (geometry?.sizesByModel?.[params.model] ?? geometry?.sizes)
63-
: geometry?.sizes,
64-
aspectRatios: params.model
65-
? (geometry?.aspectRatiosByModel?.[params.model] ?? geometry?.aspectRatios)
66-
: geometry?.aspectRatios,
67-
resolutions: params.model
68-
? (geometry?.resolutionsByModel?.[params.model] ?? geometry?.resolutions)
69-
: geometry?.resolutions,
70-
};
71-
const ignoredOverrides: ImageGenerationIgnoredOverride[] = [];
72-
const normalization: ImageGenerationNormalization = {};
73-
let size = params.size;
74-
let aspectRatio = params.aspectRatio;
75-
let resolution = params.resolution;
76-
let quality = params.quality;
77-
let outputFormat = params.outputFormat;
78-
let background = params.background;
79-
80-
if (size && (modelGeometry.sizes?.length ?? 0) > 0 && modeCaps.supportsSize) {
81-
const normalizedSize = resolveClosestSize({
82-
requestedSize: size,
83-
supportedSizes: modelGeometry.sizes,
84-
});
85-
if (normalizedSize && normalizedSize !== size) {
86-
normalization.size = {
87-
requested: size,
88-
applied: normalizedSize,
89-
};
90-
}
91-
size = normalizedSize;
92-
}
93-
94-
if (!modeCaps.supportsSize && size) {
95-
let translated = false;
96-
if (modeCaps.supportsAspectRatio) {
97-
// Prefer translating size into aspect ratio when the provider supports
98-
// shape but not exact dimensions; otherwise report the size as ignored.
99-
const normalizedAspectRatio = resolveClosestAspectRatio({
100-
requestedAspectRatio: aspectRatio,
101-
requestedSize: size,
102-
supportedAspectRatios: modelGeometry.aspectRatios,
103-
});
104-
if (normalizedAspectRatio) {
105-
aspectRatio = normalizedAspectRatio;
106-
normalization.aspectRatio = {
107-
applied: normalizedAspectRatio,
108-
derivedFrom: "size",
109-
};
110-
translated = true;
111-
}
112-
}
113-
if (!translated) {
114-
ignoredOverrides.push({ key: "size", value: size });
115-
}
116-
size = undefined;
117-
}
118-
119-
if (
120-
aspectRatio &&
121-
(modelGeometry.aspectRatios?.length ?? 0) > 0 &&
122-
modeCaps.supportsAspectRatio
123-
) {
124-
const normalizedAspectRatio = resolveClosestAspectRatio({
125-
requestedAspectRatio: aspectRatio,
126-
requestedSize: size,
127-
supportedAspectRatios: modelGeometry.aspectRatios,
128-
});
129-
if (normalizedAspectRatio && normalizedAspectRatio !== aspectRatio) {
130-
normalization.aspectRatio = {
131-
requested: aspectRatio,
132-
applied: normalizedAspectRatio,
133-
};
134-
}
135-
aspectRatio = normalizedAspectRatio;
136-
} else if (!modeCaps.supportsAspectRatio && aspectRatio) {
137-
const derivedSize =
138-
modeCaps.supportsSize && !size
139-
? resolveClosestSize({
140-
requestedSize: params.size,
141-
requestedAspectRatio: aspectRatio,
142-
// Unconstrained models still use provider-wide sizes as aspect-ratio hints.
143-
supportedSizes:
144-
modelGeometry.sizes?.length === 0 ? geometry?.sizes : modelGeometry.sizes,
145-
})
146-
: undefined;
147-
let translated = false;
148-
if (derivedSize) {
149-
// Reverse translation lets size-only providers still honor common
150-
// landscape/portrait requests when a supported size is close enough.
151-
size = derivedSize;
152-
normalization.size = {
153-
applied: derivedSize,
154-
derivedFrom: "aspectRatio",
155-
};
156-
translated = true;
157-
}
158-
if (!translated) {
159-
ignoredOverrides.push({ key: "aspectRatio", value: aspectRatio });
160-
}
161-
aspectRatio = undefined;
162-
}
163-
164-
if (resolution && (modelGeometry.resolutions?.length ?? 0) > 0 && modeCaps.supportsResolution) {
165-
const normalizedResolution = resolveClosestResolution({
166-
requestedResolution: resolution,
167-
supportedResolutions: modelGeometry.resolutions,
168-
});
169-
if (normalizedResolution && normalizedResolution !== resolution) {
170-
normalization.resolution = {
171-
requested: resolution,
172-
applied: normalizedResolution,
173-
};
174-
}
175-
resolution = normalizedResolution;
176-
} else if (!modeCaps.supportsResolution && resolution) {
177-
ignoredOverrides.push({ key: "resolution", value: resolution });
178-
resolution = undefined;
179-
}
180-
181-
if (size && !modeCaps.supportsSize) {
182-
ignoredOverrides.push({ key: "size", value: size });
183-
size = undefined;
184-
}
185-
186-
if (aspectRatio && !modeCaps.supportsAspectRatio) {
187-
ignoredOverrides.push({ key: "aspectRatio", value: aspectRatio });
188-
aspectRatio = undefined;
189-
}
190-
191-
if (resolution && !modeCaps.supportsResolution) {
192-
ignoredOverrides.push({ key: "resolution", value: resolution });
193-
resolution = undefined;
194-
}
195-
196-
const supportedQualities = params.provider.capabilities.output?.qualities;
197-
if (quality && !(supportedQualities ?? []).includes(quality)) {
43+
const sanitized = resolveMediaGeometryOverrides({
44+
size: params.size,
45+
aspectRatio: params.aspectRatio,
46+
resolution: params.resolution,
47+
capabilities: {
48+
...modeCaps,
49+
sizes: params.model
50+
? (geometry?.sizesByModel?.[params.model] ?? geometry?.sizes)
51+
: geometry?.sizes,
52+
aspectRatios: params.model
53+
? (geometry?.aspectRatiosByModel?.[params.model] ?? geometry?.aspectRatios)
54+
: geometry?.aspectRatios,
55+
resolutions: params.model
56+
? (geometry?.resolutionsByModel?.[params.model] ?? geometry?.resolutions)
57+
: geometry?.resolutions,
58+
},
59+
fallbackSizes: geometry?.sizes,
60+
});
61+
const ignoredOverrides: ImageGenerationIgnoredOverride[] = sanitized.ignoredOverrides;
62+
let { quality, outputFormat, background } = params;
63+
64+
if (quality && !(params.provider.capabilities.output?.qualities ?? []).includes(quality)) {
19865
ignoredOverrides.push({ key: "quality", value: quality });
19966
quality = undefined;
20067
}
201-
202-
const supportedFormats = params.provider.capabilities.output?.formats;
203-
if (outputFormat && !(supportedFormats ?? []).includes(outputFormat)) {
204-
ignoredOverrides.push({ key: "outputFormat", value: outputFormat });
205-
outputFormat = undefined;
206-
}
207-
208-
const supportedBackgrounds = params.provider.capabilities.output?.backgrounds;
209-
if (background && !(supportedBackgrounds ?? []).includes(background)) {
210-
ignoredOverrides.push({ key: "background", value: background });
211-
background = undefined;
212-
}
213-
21468
if (
215-
!normalization.aspectRatio &&
216-
aspectRatio &&
217-
((!params.aspectRatio && params.size) || params.aspectRatio !== aspectRatio)
69+
outputFormat &&
70+
!(params.provider.capabilities.output?.formats ?? []).includes(outputFormat)
21871
) {
219-
// Record derived aspect ratios even when the applied value is already in
220-
// place, otherwise callers cannot explain why a size became a shape.
221-
const entry: MediaNormalizationEntry<string> = {
222-
applied: aspectRatio,
223-
...(params.aspectRatio ? { requested: params.aspectRatio } : {}),
224-
...(!params.aspectRatio && params.size ? { derivedFrom: "size" } : {}),
225-
};
226-
normalization.aspectRatio = entry;
227-
}
228-
229-
if (!normalization.size && size && params.size && params.size !== size) {
230-
normalization.size = {
231-
requested: params.size,
232-
applied: size,
233-
};
234-
}
235-
236-
if (!normalization.aspectRatio && !params.aspectRatio && params.size && aspectRatio) {
237-
normalization.aspectRatio = {
238-
applied: aspectRatio,
239-
derivedFrom: "size",
240-
};
72+
ignoredOverrides.push({ key: "outputFormat", value: outputFormat });
73+
outputFormat = undefined;
24174
}
242-
24375
if (
244-
!normalization.resolution &&
245-
resolution &&
246-
params.resolution &&
247-
params.resolution !== resolution
76+
background &&
77+
!(params.provider.capabilities.output?.backgrounds ?? []).includes(background)
24878
) {
249-
normalization.resolution = {
250-
requested: params.resolution,
251-
applied: resolution,
252-
};
79+
ignoredOverrides.push({ key: "background", value: background });
80+
background = undefined;
25381
}
25482

83+
const { normalization } = sanitized;
25584
return {
256-
size,
257-
aspectRatio,
258-
resolution,
85+
size: sanitized.size,
86+
aspectRatio: sanitized.aspectRatio,
87+
resolution: sanitized.resolution,
25988
quality,
26089
outputFormat,
26190
background,
26291
ignoredOverrides,
263-
normalization: finalizeImageNormalization(normalization),
92+
normalization:
93+
hasMediaNormalizationEntry(normalization.size) ||
94+
hasMediaNormalizationEntry(normalization.aspectRatio) ||
95+
hasMediaNormalizationEntry(normalization.resolution)
96+
? normalization
97+
: undefined,
26498
};
26599
}

‎src/image-generation/provider-registry.ts‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,15 @@ import {
77
} from "../plugins/provider-registry-shared.js";
88
import type { ImageGenerationProviderPlugin } from "../plugins/types.js";
99

10-
// Image-generation providers come from plugin capability registration. The
11-
// registry keeps aliases separate from canonical ids for user config lookups.
12-
const BUILTIN_IMAGE_GENERATION_PROVIDERS: readonly ImageGenerationProviderPlugin[] = [];
13-
function resolvePluginImageGenerationProviders(
14-
cfg?: OpenClawConfig,
15-
): ImageGenerationProviderPlugin[] {
16-
return capabilityProviderRuntime.resolvePluginCapabilityProviders({
17-
key: "imageGenerationProviders",
18-
cfg,
19-
});
20-
}
21-
2210
function buildProviderMaps(cfg?: OpenClawConfig): {
2311
canonical: Map<string, ImageGenerationProviderPlugin>;
2412
aliases: Map<string, ImageGenerationProviderPlugin>;
2513
} {
2614
return buildCapabilityProviderMaps(
27-
[...BUILTIN_IMAGE_GENERATION_PROVIDERS, ...resolvePluginImageGenerationProviders(cfg)],
15+
capabilityProviderRuntime.resolvePluginCapabilityProviders({
16+
key: "imageGenerationProviders",
17+
cfg,
18+
}),
2819
normalizeCapabilityProviderId,
2920
);
3021
}

0 commit comments

Comments
 (0)