function object(value: unknown): Record { return value && typeof value === "object" && !Array.isArray(value) ? value as Record : {}; } export function canonicalVip98kVideoRatio(value: string) { return value === "landscape" ? "16:9" : value === "portrait" ? "9:16" : value; } export function vip98kFirstFrameRatio(schema: unknown): string | undefined { const current = object(schema); const content = object(object(object(current.if).properties).content); const role = object(object(object(content.contains).properties).role).const; const properties = object(object(current.then).properties); const ratio = object(properties.ratio || properties.aspect_ratio).const; if (role === "first_frame" && typeof ratio === "string") return ratio; for (const child of Array.isArray(current.allOf) ? current.allOf : []) { const value = vip98kFirstFrameRatio(child); if (value) return value; } } export function vip98kVideoDurationBounds(schema: unknown, resolution?: string) { let min = 0, max = Infinity; let durations: number[] | undefined; const visit = (value: unknown) => { const current = object(value), properties = object(current.properties); const field = object(properties.duration_seconds || properties.duration); const positiveRange = (Array.isArray(field.anyOf) ? field.anyOf : []).find((x: any) => x.minimum > 0); if (positiveRange) { min = Math.max(min, positiveRange.minimum); max = Math.min(max, positiveRange.maximum ?? Infinity); } if (Number.isFinite(field.minimum)) min = Math.max(min, field.minimum); if (Number.isFinite(field.maximum)) max = Math.min(max, field.maximum); const allowed = Array.isArray(field.enum) ? field.enum : Number.isFinite(field.const) ? [field.const] : undefined; if (allowed) { const numbers = allowed.filter((n: unknown): n is number => typeof n === "number" && Number.isFinite(n) && n > 0); durations = durations ? durations.filter(n => numbers.includes(n)) : numbers; } for (const child of Array.isArray(current.allOf) ? current.allOf : []) visit(child); const condition = object(object(current.if).properties).resolution; if (resolution && object(condition).const === resolution) visit(current.then); }; visit(schema); return { min, max, durations }; } export function mapVip98kVideoParameters(body: Record, schema: unknown) { const properties = object(object(schema).properties); const frameRatio = vip98kFirstFrameRatio(schema); if (frameRatio && Array.isArray(body.content) && body.content.some(item => object(item).role === "first_frame")) { const field = properties.ratio ? "ratio" : "aspect_ratio"; if (body[field] && body[field] !== "auto" && body[field] !== frameRatio) throw Error("当前首帧模式沿用原图比例,请选择自动比例"); body[field] = frameRatio; } const ratios = object(properties.aspect_ratio).enum; if (Array.isArray(ratios) && !ratios.includes(body.aspect_ratio)) { const alias = body.aspect_ratio === "16:9" ? "landscape" : body.aspect_ratio === "9:16" ? "portrait" : undefined; if (!alias || !ratios.includes(alias)) throw Error("供应商当前线路不支持所选视频比例"); body.aspect_ratio = alias; } const resolutions = object(properties.resolution).enum; if (body.resolution && Array.isArray(resolutions) && !resolutions.includes(body.resolution)) { const matched = resolutions.find(value => String(value).toLowerCase() === String(body.resolution).toLowerCase()); if (!matched) throw Error("供应商当前线路不支持所选视频清晰度,请同步模型规格后重试"); body.resolution = matched; } const seconds = Number(body.duration_seconds ?? body.duration); if (seconds) { const bounds = vip98kVideoDurationBounds(schema, String(body.resolution || "")); if (seconds < bounds.min || seconds > bounds.max || (bounds.durations && !bounds.durations.includes(seconds))) throw Error("供应商当前清晰度不支持所选视频时长,请调整镜头时长"); } } export function vip98kVideoContentTypes(schema: unknown): string[] { const content = object(object(object(schema).properties).content); const items = object(content.items); return (Array.isArray(items.oneOf) ? items.oneOf : []).flatMap((variant: unknown) => { const type = object(object(object(variant).properties).type).const; return typeof type === "string" ? [type] : []; }); } export function vip98kVideoImageRoles(schema: unknown): string[] | undefined { const variants = object(object(object(object(schema).properties).content).items).oneOf; const image = (Array.isArray(variants) ? variants : []).find(value => object(object(object(value).properties).type).const === "image_url"); const roles = object(object(object(image).properties).role).enum; return Array.isArray(roles) ? roles.map(String) : undefined; } export function bindVip98kVideoContent(body: Record, schema: unknown, input: { prompt: string; images: string[]; videos: string[]; audios: string[]; mode?: unknown; }) { const types = vip98kVideoContentTypes(schema); if (!types.length) return false; if (input.images.length && !types.includes("image_url")) throw Error("所选视频模型只支持文字生成,不能添加参考图片"); if (input.videos.length && !types.includes("video_url")) throw Error("所选模型不支持参考视频"); if (input.audios.length && !types.includes("audio_url")) throw Error("所选模型不支持参考音频"); const reference = input.mode === "reference-images"; const roles = vip98kVideoImageRoles(schema); const role = reference ? "reference_image" : "first_frame"; if (input.images.length && roles && !roles.includes(role)) throw Error("所选视频模型不支持这个参考图模式"); if (!reference && input.images.length > 2) throw Error("首尾帧模式最多支持两张图片"); if (!reference && input.images.length === 2 && roles && !roles.includes("last_frame")) throw Error("所选视频模型不支持尾帧"); if (["first-frame", "first-last-frame", "reference-images"].includes(String(input.mode)) && !input.images.length) throw Error("请先添加视频参考图"); body.content = [ ...(input.prompt ? [{ type: "text", text: input.prompt }] : []), ...input.images.map((url, index) => ({ type: "image_url", image_url: { url }, role: reference ? "reference_image" : index ? "last_frame" : "first_frame" })), ...input.videos.map(url => ({ type: "video_url", video_url: { url }, role: "reference_video" })), ...input.audios.map(url => ({ type: "audio_url", audio_url: { url }, role: "reference_audio" })), ]; delete body.prompt; delete body.file_urls; return true; }