feat(video): 对齐 grok-imagine-video 接口并优化视频生成功能

- 将默认视频模型从 sora-2 更改为 grok-imagine-video
- 新增分辨率选项包括 720p 和 480p,替换原有的质量选项
- 扩展视频尺寸选项,新增方形、宽屏、长图等预设尺寸
- 添加分辨率和尺寸手动输入功能,支持自定义宽高
- 更新视频生成 API 参数为 resolution_name、preset 和 input_reference[]
- 支持最多 7 张参考图片上传
- 优化视频设置浮层定位,避免被工具栏遮挡
- 识别后端错误响应格式,改进视频生成失败处理逻辑
This commit is contained in:
HouYunFei
2026-05-25 17:15:42 +08:00
parent b21f8c3af9
commit 8c506f92f9
5 changed files with 146 additions and 38 deletions
+4
View File
@@ -4,3 +4,7 @@
- 管理后台新增/编辑渠道时,渠道可用模型支持通过弹窗按“新获取、已有”分组选择,并可在弹窗内手动增加模型或拉取模型列表后再写回表单。
- 管理后台编辑渠道时,API Key 留空不再触发必填校验,表示沿用已保存的密钥;新增渠道仍要求填写 API Key。
- 管理后台公开配置里的系统可用模型候选项改为由已启用渠道中选择的模型合并去重生成,最终开放哪些模型仍由公开配置里手动勾选。
- 视频生成请求参数对齐 `grok-imagine-video` 接口:使用 `resolution_name``preset=normal``input_reference[]`,支持清晰度、尺寸、秒数快捷选择和手动输入,并支持最多 7 张参考图。
- 画布视频设置浮层改为挂载到页面根层级,避免被节点悬浮工具栏遮挡。
- 视频清晰度输入框改为只输入数字,提交请求时再拼接 `p` 单位。
- 视频生成前端会识别后端 `{ code, msg }` 错误响应,创建失败不再继续轮询 `undefined`
+1 -1
View File
@@ -41,7 +41,7 @@ func AIVideoContent(w http.ResponseWriter, r *http.Request, id string) {
func proxyAIGetRequest(w http.ResponseWriter, r *http.Request, path string) {
modelName := r.URL.Query().Get("model")
if strings.TrimSpace(modelName) == "" {
modelName = "sora-2"
modelName = "grok-imagine-video"
}
channel, err := service.SelectModelChannel(modelName)
if err != nil {
@@ -9,15 +9,16 @@ import { useThemeStore } from "@/stores/use-theme-store";
import type { AiConfig } from "@/stores/use-config-store";
import { CanvasImageSettingsTheme } from "./canvas-image-settings-popover";
const qualityOptions = [
{ value: "auto", label: "自动" },
{ value: "high", label: "" },
{ value: "medium", label: "中" },
{ value: "low", label: "低" },
const resolutionOptions = [
{ value: "720", label: "720p" },
{ value: "480", label: "480p" },
];
const sizeOptions = [
{ value: "1280x720", label: "横屏" },
{ value: "720x1280", label: "竖屏" },
{ value: "1280x720", label: "横屏", width: 1280, height: 720 },
{ value: "720x1280", label: "竖屏", width: 720, height: 1280 },
{ value: "1024x1024", label: "方形", width: 1024, height: 1024 },
{ value: "1792x1024", label: "宽屏", width: 1792, height: 1024 },
{ value: "1024x1792", label: "长图", width: 1024, height: 1792 },
];
const secondOptions = [6, 10, 12, 16, 20];
@@ -30,9 +31,14 @@ type CanvasVideoSettingsPopoverProps = {
export function CanvasVideoSettingsPopover({ config, onConfigChange, buttonClassName, placement = "topLeft" }: CanvasVideoSettingsPopoverProps) {
const theme = canvasThemes[useThemeStore((state) => state.theme)];
const seconds = Math.max(1, Math.min(60, Math.floor(Math.abs(Number(config.videoSeconds)) || 6)));
const seconds = config.videoSeconds || "6";
const size = normalizeVideoSize(config.size);
const vquality = config.vquality || "auto";
const dimensions = readSizeDimensions(size);
const resolution = normalizeVideoResolution(config.vquality);
const updateDimension = (key: "width" | "height", value: number | null) => {
const next = Math.max(1, Math.floor(value || dimensions[key] || 720));
onConfigChange("size", `${key === "width" ? next : dimensions.width}x${key === "height" ? next : dimensions.height}`);
};
return (
<Popover
@@ -41,44 +47,59 @@ export function CanvasVideoSettingsPopover({ config, onConfigChange, buttonClass
arrow={false}
overlayClassName="canvas-image-settings-popover"
color={theme.toolbar.panel}
getPopupContainer={(triggerNode) => triggerNode.parentElement || document.body}
zIndex={1200}
getPopupContainer={() => document.body}
content={
<CanvasImageSettingsTheme theme={theme}>
<div className="w-[320px] space-y-5 rounded-3xl px-1 py-0.5" style={{ color: theme.node.text }} onMouseDown={(event) => event.stopPropagation()}>
<div className="w-[360px] space-y-5 rounded-3xl px-1 py-0.5" style={{ color: theme.node.text }} onMouseDown={(event) => event.stopPropagation()}>
<div className="text-xl font-semibold"></div>
<SettingGroup title="质量" color={theme.node.muted}>
<div className="grid grid-cols-4 gap-3">
{qualityOptions.map((item) => (
<OptionPill key={item.value} selected={vquality === item.value} theme={theme} onClick={() => onConfigChange("vquality", item.value)}>
<SettingGroup title="清晰度" color={theme.node.muted}>
<div className="grid grid-cols-2 gap-3">
{resolutionOptions.map((item) => (
<OptionPill key={item.value} selected={resolution === item.value} theme={theme} onClick={() => onConfigChange("vquality", item.value)}>
{item.label}
</OptionPill>
))}
</div>
<ResolutionInput value={resolution} theme={theme} onChange={(value) => onConfigChange("vquality", value)} />
</SettingGroup>
<SettingGroup title="尺寸" color={theme.node.muted}>
<div className="grid grid-cols-2 gap-3">
<div className="grid grid-cols-[1fr_auto_1fr] items-center gap-3">
<DimensionInput prefix="W" value={dimensions.width} theme={theme} onChange={(value) => updateDimension("width", value)} />
<span className="text-lg opacity-45"></span>
<DimensionInput prefix="H" value={dimensions.height} theme={theme} onChange={(value) => updateDimension("height", value)} />
</div>
<div className="grid grid-cols-5 gap-2">
{sizeOptions.map((item) => (
<OptionPill key={item.value} selected={size === item.value} theme={theme} onClick={() => onConfigChange("size", item.value)}>
{item.label}
</OptionPill>
<button
key={item.value}
type="button"
className="flex h-[82px] cursor-pointer flex-col items-center justify-center gap-2 rounded-2xl border bg-transparent text-xs transition hover:opacity-80"
style={{ borderColor: size === item.value ? theme.node.text : theme.node.stroke, color: theme.node.text }}
onMouseDown={(event) => event.stopPropagation()}
onClick={() => onConfigChange("size", item.value)}
>
<SizePreview width={item.width} height={item.height} color={theme.node.text} />
<span>{item.label}</span>
</button>
))}
</div>
</SettingGroup>
<SettingGroup title="秒数" color={theme.node.muted}>
<div className="grid grid-cols-5 gap-2">
{secondOptions.map((value) => (
<OptionPill key={value} selected={seconds === value} theme={theme} onClick={() => onConfigChange("videoSeconds", String(value))}>
<OptionPill key={value} selected={seconds === String(value)} theme={theme} onClick={() => onConfigChange("videoSeconds", String(value))}>
{value}s
</OptionPill>
))}
<input
type="number"
min={1}
max={60}
min={6}
max={20}
className="col-span-2 h-10 rounded-full border bg-transparent px-3 text-center text-sm outline-none [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
style={{ borderColor: theme.node.stroke, color: theme.node.text, WebkitTextFillColor: theme.node.text }}
value={seconds}
onChange={(event) => onConfigChange("videoSeconds", String(Number(event.target.value) || 6))}
onChange={(event) => onConfigChange("videoSeconds", event.target.value)}
onMouseDown={(event) => event.stopPropagation()}
/>
</div>
@@ -89,7 +110,7 @@ export function CanvasVideoSettingsPopover({ config, onConfigChange, buttonClass
>
<Button size="small" type="text" className={buttonClassName || "!h-8 !max-w-[170px] !justify-start !rounded-full !px-2.5"} style={{ background: theme.node.fill, color: theme.node.text }} icon={<Settings2 className="size-3.5" />}>
<span className="truncate">
{qualityLabel(vquality)} · {sizeLabel(size)} · {seconds}s
{resolution}p · {sizeLabel(size)} · {seconds}s
</span>
</Button>
</Popover>
@@ -115,15 +136,65 @@ function SettingGroup({ title, color, children }: { title: string; color: string
);
}
function ResolutionInput({ value, theme, onChange }: { value: string; theme: (typeof canvasThemes)[keyof typeof canvasThemes]; onChange: (value: string) => void }) {
return (
<label className="flex h-10 overflow-hidden rounded-xl border text-sm" style={{ borderColor: theme.node.stroke, color: theme.node.text }}>
<input
type="number"
min={1}
className="min-w-0 flex-1 bg-transparent px-3 outline-none [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
value={value}
onChange={(event) => onChange(event.target.value)}
onMouseDown={(event) => event.stopPropagation()}
/>
<span className="grid w-10 place-items-center" style={{ color: theme.node.muted }}>
p
</span>
</label>
);
}
function DimensionInput({ prefix, value, theme, onChange }: { prefix: string; value: number; theme: (typeof canvasThemes)[keyof typeof canvasThemes]; onChange: (value: number | null) => void }) {
return (
<label className="flex h-10 overflow-hidden rounded-xl text-sm" style={{ background: theme.node.fill, color: theme.node.text }}>
<span className="grid w-10 place-items-center" style={{ color: theme.node.muted }}>
{prefix}
</span>
<input
type="number"
min={1}
className="min-w-0 flex-1 bg-transparent px-2 outline-none [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
value={value || ""}
onChange={(event) => onChange(Number(event.target.value) || null)}
onMouseDown={(event) => event.stopPropagation()}
/>
</label>
);
}
function SizePreview({ width, height, color }: { width: number; height: number; color: string }) {
const longSide = Math.max(width, height);
const previewWidth = Math.max(12, Math.round((width / longSide) * 34));
const previewHeight = Math.max(12, Math.round((height / longSide) * 34));
return <span className="rounded-[4px] border" style={{ width: previewWidth, height: previewHeight, borderColor: color }} />;
}
function normalizeVideoSize(value: string) {
if (/^\d+x\d+$/.test(value || "")) return value;
return ["9:16", "2:3", "3:4"].includes(value) ? "720x1280" : "1280x720";
}
function qualityLabel(value: string) {
return ({ auto: "自动", high: "高", medium: "", low: "低" } as Record<string, string>)[value] || value;
function normalizeVideoResolution(value: string) {
if (value === "480p" || value === "low") return "480";
if (value === "720p" || value === "auto" || value === "high" || value === "medium") return "720";
return value.replace(/p$/i, "") || "720";
}
function readSizeDimensions(size: string) {
const match = size.match(/^(\d+)x(\d+)$/);
return { width: Number(match?.[1]) || 1280, height: Number(match?.[2]) || 720 };
}
function sizeLabel(value: string) {
return value === "720x1280" ? "竖屏" : "横屏";
return ({ "720x1280": "竖屏", "1280x720": "横屏", "1024x1024": "方形", "1792x1024": "宽屏", "1024x1792": "长图" } as Record<string, string>)[value] || value;
}
+40 -7
View File
@@ -7,6 +7,7 @@ import { useUserStore } from "@/stores/use-user-store";
import type { ReferenceImage } from "@/types/image";
type VideoResponse = { id: string; status?: string; error?: { message?: string } };
type ApiVideoResponse = VideoResponse | { code?: number; data?: VideoResponse | null; msg?: string };
function aiApiUrl(config: AiConfig, path: string) {
return config.channelMode === "remote" ? `/api/v1${path}` : buildApiUrl(config.baseUrl, path);
@@ -28,16 +29,20 @@ export async function requestVideoGeneration(config: AiConfig, prompt: string, r
body.append("prompt", prompt);
body.append("seconds", normalizeVideoSeconds(config.videoSeconds));
if (normalizeVideoSize(config.size)) body.append("size", normalizeVideoSize(config.size)!);
if (config.vquality) body.append("vquality", config.vquality);
if (references[0]) body.append("input_reference", dataUrlToFile({ ...references[0], dataUrl: await imageToDataUrl(references[0]) }));
const created = await axios.post<VideoResponse>(aiApiUrl(config, "/videos"), body, { headers: aiHeaders(config) });
body.append("resolution_name", normalizeVideoResolution(config.vquality));
body.append("preset", "normal");
const files = await Promise.all(references.slice(0, 7).map(async (image) => dataUrlToFile({ ...image, dataUrl: await imageToDataUrl(image) })));
files.forEach((file) => body.append("input_reference[]", file));
const created = unwrapVideoResponse((await axios.post<ApiVideoResponse>(aiApiUrl(config, "/videos"), body, { headers: aiHeaders(config) })).data);
if (!created.id) throw new Error("视频接口没有返回任务 ID");
for (;;) {
const video = await axios.get<VideoResponse>(aiApiUrl(config, `/videos/${created.data.id}`), { headers: aiHeaders(config), params: config.channelMode === "remote" ? { model } : undefined });
if (video.data.status === "completed") break;
if (video.data.status === "failed" || video.data.status === "cancelled") throw new Error(video.data.error?.message || "视频生成失败");
const video = unwrapVideoResponse((await axios.get<ApiVideoResponse>(aiApiUrl(config, `/videos/${created.id}`), { headers: aiHeaders(config), params: config.channelMode === "remote" ? { model } : undefined })).data);
if (video.status === "completed") break;
if (video.status === "failed" || video.status === "cancelled") throw new Error(video.error?.message || "视频生成失败");
await new Promise((resolve) => setTimeout(resolve, 2500));
}
const content = await axios.get<Blob>(aiApiUrl(config, `/videos/${created.data.id}/content`), { headers: aiHeaders(config), params: config.channelMode === "remote" ? { model } : undefined, responseType: "blob" });
const content = await axios.get<Blob>(aiApiUrl(config, `/videos/${created.id}/content`), { headers: aiHeaders(config), params: config.channelMode === "remote" ? { model } : undefined, responseType: "blob" });
await assertVideoBlob(content.data);
refreshRemoteUser(config);
return content.data;
}
@@ -51,3 +56,31 @@ function normalizeVideoSize(value: string) {
if (/^\d+x\d+$/.test(size)) return size;
return ["9:16", "2:3", "3:4"].includes(size) ? "720x1280" : "1280x720";
}
function normalizeVideoResolution(value: string) {
if (value === "low") return "480p";
if (value === "auto" || value === "high" || value === "medium") return "720p";
const resolution = value.replace(/p$/i, "") || "720";
return `${resolution}p`;
}
function unwrapVideoResponse(payload: ApiVideoResponse) {
if (!payload) throw new Error("接口没有返回视频任务");
if ("code" in payload && typeof payload.code === "number") {
if (payload.code !== 0) throw new Error(payload.msg || "请求失败");
if (!payload.data) throw new Error("接口没有返回视频任务");
return payload.data;
}
return payload;
}
async function assertVideoBlob(blob: Blob) {
if (!blob.type.includes("json")) return;
let payload: { code?: number; msg?: string };
try {
payload = JSON.parse(await blob.text()) as { code?: number; msg?: string };
} catch {
return;
}
if (typeof payload.code === "number" && payload.code !== 0) throw new Error(payload.msg || "视频下载失败");
}
+3 -3
View File
@@ -32,10 +32,10 @@ export const defaultConfig: AiConfig = {
apiKey: "",
model: "gpt-image-2",
imageModel: "gpt-image-2",
videoModel: "sora-2",
videoModel: "grok-imagine-video",
textModel: "gpt-5.5",
videoSeconds: "6",
vquality: "auto",
vquality: "720",
systemPrompt: "",
models: [],
quality: "auto",
@@ -112,7 +112,7 @@ export const useConfigStore = create<ConfigStore>()(
partialize: (state) => ({ config: state.config }),
merge: (persisted, current) => {
const config = { ...defaultConfig, ...((persisted as Partial<ConfigStore>).config || {}) };
return { ...current, config: { ...config, channelMode: config.channelMode || "remote", imageModel: config.imageModel || config.model, videoModel: config.videoModel || "sora-2", textModel: config.textModel || config.model, videoSeconds: config.videoSeconds || "6", vquality: config.vquality || "auto" } };
return { ...current, config: { ...config, channelMode: config.channelMode || "remote", imageModel: config.imageModel || config.model, videoModel: config.videoModel || "grok-imagine-video", textModel: config.textModel || config.model, videoSeconds: config.videoSeconds || "6", vquality: config.vquality || "720" } };
},
},
),