fix: grok-image 补齐 aspect_ratio 与 resolution 传参 (#184)

* fix: grok-image 补齐 aspect_ratio 与 resolution 传参

- 能力配置开放比例与 1k/2k 分辨率
- 请求体映射 size→aspect_ratio、quality→resolution
- 画布设置展示「分辨率」选项
- 补充单测

Closes #182

* fix(create): 创作页图片质量支持 1k/2k 分辨率选项

创作页原先硬编码 auto/low/medium/high,导致 grok 能力配置里的 1k/2k
无法显示;改为按模型 quality.values 动态渲染,有 1k/2k 时标题显示「分辨率」。
This commit is contained in:
Andrew
2026-08-08 18:27:36 +08:00
committed by GitHub
parent 99cc880eb8
commit 1b0fd7fd49
8 changed files with 164 additions and 15 deletions

View File

@@ -124,6 +124,15 @@ function normalizeQuality(quality: string) {
return QUALITY_BASE[normalized] ? normalized : undefined;
}
/** grok2api / xAI Imagine:画布 quality 映射为 resolution(1k/2k)。 */
function normalizeGrokImageResolution(quality: string | undefined) {
const value = (quality || "").trim().toLowerCase();
if (!value || value === "auto") return undefined;
if (value === "1k" || value === "low" || value === "standard") return "1k";
if (value === "2k" || value === "medium" || value === "hd" || value === "high" || value === "4k") return "2k";
return undefined;
}
/** Map "quality + ratio" to an explicit pixel dimension like "3840x2160". */
function resolveSize(quality: string | undefined, ratio: string): string {
const parsedRatio = parseImageRatio(ratio);
@@ -814,6 +823,9 @@ export async function requestGeneration(config: AiConfig, prompt: string, option
}
if (requestConfig.interfaceType === "grok-image") {
try {
const size = normalizedImage.size && normalizedImage.size !== "auto" ? normalizedImage.size : undefined;
const aspectRatio = size?.includes(":") ? size : undefined;
const resolution = normalizeGrokImageResolution(normalizedImage.quality);
const responseData = await postChannelJSON<ImageApiResponse>(
requestConfig,
aiApiUrl(requestConfig, "/images/generations"),
@@ -822,6 +834,9 @@ export async function requestGeneration(config: AiConfig, prompt: string, option
prompt: withSystemPrompt(requestConfig, prompt),
n,
response_format: "url",
...(size ? { size } : {}),
...(aspectRatio ? { aspect_ratio: aspectRatio } : {}),
...(resolution ? { resolution } : {}),
},
options,
);
@@ -898,6 +913,9 @@ export async function requestEdit(config: AiConfig, prompt: string, references:
if (references.length !== 1) throw new Error("Grok 图片编辑必须提供且仅支持 1 张参考图");
try {
const imageUrl = await grokImageInputURL(references[0]);
const size = normalizedImage.size && normalizedImage.size !== "auto" ? normalizedImage.size : undefined;
const aspectRatio = size?.includes(":") ? size : undefined;
const resolution = normalizeGrokImageResolution(normalizedImage.quality);
const response = await postChannelJSON<ImageApiResponse>(
requestConfig,
aiApiUrl(requestConfig, "/images/edits"),
@@ -907,6 +925,9 @@ export async function requestEdit(config: AiConfig, prompt: string, references:
image: { url: imageUrl },
n,
response_format: "url",
...(size ? { size } : {}),
...(aspectRatio ? { aspect_ratio: aspectRatio } : {}),
...(resolution ? { resolution } : {}),
},
options,
);