Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.
JavaScript agent-collaborationagent-harnessagent-orchestrationagent-teamagent-workflowai-assistantai-automationai-employeeai-productivityai-teamai-workflowautonomous-agentsclaude-code-guiclaude-coworkdesktop-agentlocal-firstmulti-agentmulti-agent-systemself-evolving-agentstask-automation
feat(video): restore public provider generation adapters
wytd9e003bcfa93
diff --git a/src/main/features/video_auth.ts b/src/main/features/video_auth.tsindex 69a079f9..30f37482 100644--- a/src/main/features/video_auth.ts+++ b/src/main/features/video_auth.ts@@ -20,10 +20,18 @@ import {const log = createLogger('video-auth');-const VIDEO_AUTH_MODELS_BY_PROVIDER: Readonly<Record<string, Array<{ id: string; name: string }>>> = {- [ORKAS_API_PROVIDER]: [- { id: ORKAS_API_VIDEO_MODEL, name: 'Video' },+export const VIDEO_MODELS_BY_PROVIDER: Readonly<Record<string, Array<{ id: string; name: string }>>> = {+ [ORKAS_API_PROVIDER]: [{ id: ORKAS_API_VIDEO_MODEL, name: 'Video' }],+ doubao: [+ { id: 'doubao-seedance-2-0-260128', name: 'Seedance 2.0' },+ { id: 'doubao-seedance-2-0-fast-260128', name: 'Seedance 2.0 Fast' },+ { id: 'doubao-seedance-2-0-mini', name: 'Seedance 2.0 Mini' },],+ minimax: [{ id: 'MiniMax-Hailuo-2.3', name: 'Hailuo 2.3' }],+ vidu: [{ id: 'viduq3-pro', name: 'Q3 Pro' }],+ kling: [{ id: 'kling-v3-0-turbo', name: '3.0 Turbo' }],+ google: [{ id: 'veo-3.1-generate-preview', name: 'Veo 3.1' }],+ runway: [{ id: 'gen4.5', name: 'Gen-4.5' }],};let _idCounter = 0;@@ -48,11 +56,11 @@ export interface AddVideoProfileInput {}export function isVideoProviderModelAllowed(provider: string, model: string): boolean {- return !!VIDEO_AUTH_MODELS_BY_PROVIDER[provider]?.some((m) => m.id === model);+ return !!VIDEO_MODELS_BY_PROVIDER[provider]?.some((m) => m.id === model);}function defaultVideoModel(provider: string): string {- return VIDEO_AUTH_MODELS_BY_PROVIDER[provider]?.[0]?.id || '';+ return VIDEO_MODELS_BY_PROVIDER[provider]?.[0]?.id || '';}export function addVideoProfile(input: AddVideoProfileInput): { ok: true; id: string } | { ok: false; error: string } {@@ -107,16 +115,22 @@ export function listVideoProviderOptions(): Array<{ id: string; label: string; dlabel: 'Orkas',docs: ORKAS_API_KEYS_URL,},+ { id: 'doubao', label: 'DouBao · Seedance', docs: 'https://console.volcengine.com/ark/region:ark+cn-beijing/apiKey' },+ { id: 'minimax', label: 'MiniMax', docs: 'https://platform.minimax.io/user-center/basic-information/interface-key' },+ { id: 'vidu', label: 'Vidu', docs: 'https://platform.vidu.com/' },+ { id: 'kling', label: 'Kling', docs: 'https://kling.ai/global/dev' },+ { id: 'google', label: 'Google', docs: 'https://aistudio.google.com/app/apikey' },+ { id: 'runway', label: 'Runway', docs: 'https://dev.runwayml.com/' },];}export function listVideoModelOptions(provider: string): Array<{ id: string; name: string }> {- return VIDEO_AUTH_MODELS_BY_PROVIDER[provider] || [];+ return VIDEO_MODELS_BY_PROVIDER[provider] || [];}export function listVideoModelsByProvider(): Record<string, Array<{ id: string; name: string }>> {return Object.fromEntries(- Object.keys(VIDEO_AUTH_MODELS_BY_PROVIDER).map((provider) => [+ Object.keys(VIDEO_MODELS_BY_PROVIDER).map((provider) => [provider,listVideoModelOptions(provider),]),diff --git a/src/main/features/video_gen.ts b/src/main/features/video_gen.tsindex 2394c018..28d48fa5 100644--- a/src/main/features/video_gen.ts+++ b/src/main/features/video_gen.ts@@ -1,9 +1,11 @@-/** Video generation through the API-key authenticated Orkas public API. */+/** Video generation through configured user-owned provider keys. */import * as fs from 'node:fs/promises';import * as path from 'node:path';-import { loadVideoProfiles } from './auth';+import { loadVideoProfiles, type VideoProfile } from './auth';+import { isVideoProviderModelAllowed } from './video_auth';+export { VIDEO_MODELS_BY_PROVIDER, isVideoProviderModelAllowed } from './video_auth';import {ORKAS_API_BASE_URL,ORKAS_API_PROVIDER,@@ -27,6 +29,16 @@ const POLL_TIMEOUT_MS = 20_000;const DOWNLOAD_TIMEOUT_MS = 10 * 60_000;const MAX_CONSECUTIVE_POLL_TIMEOUTS = 5;+const DOUBAO_BASE = 'https://ark.cn-beijing.volces.com';+const MINIMAX_BASE = 'https://api.minimax.io';+const VIDU_BASE = 'https://api.vidu.com';+const KLING_BASE = 'https://api-singapore.klingai.com';+const GOOGLE_VIDEO_BASE = 'https://generativelanguage.googleapis.com/v1beta';+const RUNWAY_BASE = 'https://api.dev.runwayml.com';+const VIDEO_CREATE_HTTP_TIMEOUT_MS = 130 * 1000;+const VIDEO_POLL_HTTP_TIMEOUT_MS = 20 * 1000;++export interface GenerateVideoInput {prompt: string;outputAbsPath: string;@@ -40,6 +52,7 @@ export interface GenerateVideoInput {resolution?: string;quality?: 'economy' | 'balanced' | 'quality';generateAudio?: boolean;+ watermark?: boolean;seed?: number;pollIntervalMs?: number;timeoutMs?: number;@@ -81,6 +94,7 @@ export type GenerateVideoResult =message: string;taskId?: string;postDispatch?: boolean;+ dispatched?: false;};class OrkasApiQuotaError extends Error {@@ -94,6 +108,22 @@ class VideoDeliveryError extends Error {}}+export interface PickedVideoGenProfile {+ entry: VideoProfile;+}+export function listVideoGenCandidates(): PickedVideoGenProfile[] {+ return loadVideoProfiles()+ .filter((profile) => isVideoProviderModelAllowed(profile.provider, profile.model))+ .map((entry) => ({ entry }));+}+export function pickVideoGenProfile(): PickedVideoGenProfile | null {+ return listVideoGenCandidates()[0] || null;+}+function videoGenCandidatesForCall(): PickedVideoGenProfile[] {+ return listVideoGenCandidates();+}++interface NormalizedVideoInput {prompt: string;referenceImageUrls: string[];@@ -102,13 +132,26 @@ interface NormalizedVideoInput {ratio: string;duration: number;resolution: string;- quality: 'balanced' | 'quality';+ quality: 'economy' | 'balanced' | 'quality';generateAudio: boolean;+ watermark: boolean;seed?: number;pollIntervalMs: number;timeoutMs: number;}+interface AdapterVideoRequest extends NormalizedVideoInput {+ apiKey: string;+ model: string;+ signal?: AbortSignal;+ onProgress?: VideoProgressReporter;+ usageContext?: OrkasApiUsageContext;+ /** The image API has no ratio override; distinguish the generic default. */+ ratioExplicit?: boolean;+ /** Host-only dispatch evidence; provider text never determines this state. */+ onRequestDisposition?: (disposition: 'sent' | 'accepted' | 'rejected') => void;+}+interface OrkasVideoRequest extends NormalizedVideoInput {apiKey: string;signal?: AbortSignal;@@ -129,99 +172,199 @@ interface OrkasVideoResult {export async function generateVideo(input: GenerateVideoInput): Promise<GenerateVideoResult> {try {throwIfAborted(input.signal);- if (!input.prompt?.trim()) return { ok: false, errorCode: 'BAD_INPUT', message: 'prompt is required' };- if (!input.outputAbsPath) return { ok: false, errorCode: 'BAD_INPUT', message: 'outputAbsPath is required' };-- const profile = loadVideoProfiles().find((candidate) => (- candidate.provider === ORKAS_API_PROVIDER && candidate.model === ORKAS_API_VIDEO_MODEL- ));- if (!profile) {- return { ok: false, errorCode: 'NO_CAPABLE_MODEL', message: t('video_gen.no_capable_model') };+ } catch (err) {+ return {+ ok: false,+ errorCode: 'BAD_INPUT',+ message: sanitizeLogTextForUpload((err as Error).message || String(err)),+ dispatched: false,+ };+ }+ if (!input.prompt || !input.prompt.trim()) {+ return { ok: false, errorCode: 'BAD_INPUT', message: 'prompt is required', dispatched: false };+ }+ if (!input.outputAbsPath) {+ return { ok: false, errorCode: 'BAD_INPUT', message: 'outputAbsPath is required', dispatched: false };+ }++ const candidates = videoGenCandidatesForCall();+ if (!candidates.length) {+ log.warn('video gen aborted: no video profile configured');+ return { ok: false, errorCode: 'NO_CAPABLE_MODEL', message: t('video_gen.no_capable_model'), dispatched: false };+ }++ // Preserve unsupported raw arguments before generic normalization/upload.+ // Do not bill another configured service for an invalid selected request.+ const firstInputError = candidates[0].entry.provider === 'kling' ? klingVideoInputError(input) : null;+ if (firstInputError) return { ok: false, errorCode: 'BAD_INPUT', message: firstInputError, dispatched: false };++ let normalized: NormalizedVideoInput;+ try {+ const refCount = (input.referenceImagePaths?.length || 0)+ + (input.referenceVideoPaths?.length || 0)+ + (input.referenceImageUrls?.length || 0)+ + (input.referenceVideoUrls?.length || 0);+ if (refCount) {+ emitProgress(input.onProgress, 'prepare_references', 'Preparing video references', { count: refCount });}+ const refs = await resolveReferenceUrls(input);+ normalized = normalizeVideoInput({+ ...input,+ referenceImageUrls: refs.referenceImageUrls,+ referenceVideoUrls: refs.referenceVideoUrls,+ });+ } catch (err) {+ // Reference preparation failed before any generation request was sent.+ const message = (err as Error).message || String(err);+ const safeMessage = sanitizeLogTextForUpload(message);+ // No generation request has left yet, so nothing billable happened.+ return { ok: false, errorCode: 'BAD_INPUT', message: safeMessage, dispatched: false };+ }- let normalized: NormalizedVideoInput;+ let lastProviderError = '';+ for (let i = 0; i < candidates.length; i += 1) {+ const candidate = candidates[i];+ const inputError = candidate.entry.provider === 'kling' ? klingVideoInputError(input) : null;+ if (inputError) return { ok: false, errorCode: 'BAD_INPUT', message: inputError, ...(i === 0 ? { dispatched: false as const } : {}) };+ let adapterRes: AdapterVideoResult;+ let requestDisposition: 'sent' | 'accepted' | 'rejected' | undefined;+ let acceptedTaskId: string | undefined;try {- const refs = await resolveReferenceUrls(input);- normalized = normalizeVideoInput({- ...input,- referenceImageUrls: refs.referenceImageUrls,- referenceVideoUrls: refs.referenceVideoUrls,- });+ adapterRes = await dispatchVideo({+ ...normalized,+ apiKey: candidate.entry.apiKey,+ model: candidate.entry.model,+ ...(candidate.entry.provider === 'kling' ? { ratioExplicit: input.ratio !== undefined, duration: input.duration ?? normalized.duration } : {}),+ signal: input.signal,+ onProgress: event => {+ if (event.phase === 'task_created' && typeof event.data?.taskId === 'string') acceptedTaskId = event.data.taskId;+ input.onProgress?.(event);+ },+ onRequestDisposition: disposition => { requestDisposition = disposition; },+ usageContext: input.usageContext,+ }, candidate.entry.provider);} catch (err) {- return {- ok: false,- errorCode: 'BAD_INPUT',- message: sanitizeLogTextForUpload((err as Error).message || String(err)),- };+ lastProviderError = (err as Error).message || String(err);+ if (candidate.entry.provider === ORKAS_API_PROVIDER) {+ // Preserve the public API's existing terminal error and quota contract.+ const message = sanitizeLogTextForUpload(lastProviderError);+ if (err instanceof VideoDeliveryError) {+ return { ok: false, errorCode: 'DELIVERY_ERROR', message, taskId: err.taskId, postDispatch: true };+ }+ if (err instanceof OrkasApiQuotaError) {+ return { ok: false, errorCode: 'CREDITS_EXHAUSTED', message };+ }+ return {+ ok: false,+ errorCode: input.signal?.aborted || /operation aborted/i.test(message) ? 'BAD_INPUT'+ : /timed out|timeout/i.test(message) ? 'TIMEOUT' : 'PROVIDER_API_ERROR',+ message,+ };+ }+ if (err instanceof VideoGenerationAcceptedTaskError) {+ log.warn('video task tracking stopped after creation', {+ provider: candidate.entry.provider, error_code: err.errorCode, has_task_receipt: true,+ });+ return { ok: false, errorCode: err.errorCode, message: err.message, taskId: err.taskId, postDispatch: true };+ }+ if (err instanceof VideoDeliveryError) {+ log.warn('video task completed but delivery retries were exhausted', {+ provider: candidate.entry.provider,+ model: candidate.entry.model,+ taskId: err.taskId,+ error: logErrorSummary(err),+ });+ return {+ ok: false,+ errorCode: 'DELIVERY_ERROR',+ message: sanitizeLogTextForUpload(lastProviderError),+ taskId: err.taskId,+ postDispatch: true,+ };+ }+ if (requestDisposition === 'sent' || requestDisposition === 'accepted') {+ log.warn('video generation stopped after dispatch without replay', {+ provider: candidate.entry.provider, request_disposition: requestDisposition,+ has_task_receipt: !!acceptedTaskId,+ });+ return {+ ok: false,+ errorCode: input.signal?.aborted ? 'BAD_INPUT' : lastProviderError.includes('timed out') ? 'TIMEOUT' : 'PROVIDER_API_ERROR',+ message: 'Video task tracking stopped after a generation request was sent. The task or charge status may be unknown.',+ ...(acceptedTaskId ? { taskId: acceptedTaskId } : {}),+ postDispatch: true,+ };+ }+ if (input.signal?.aborted || /operation aborted/i.test(lastProviderError)) {+ return {+ ok: false,+ errorCode: 'BAD_INPUT',+ message: sanitizeLogTextForUpload(lastProviderError),+ };+ }+ const suffix = i < candidates.length - 1 ? '; trying next provider' : '';+ log.warn(`video gen API call failed (${candidate.entry.provider}/${candidate.entry.model})${suffix}`, {+ error: logErrorSummary(err),+ });+ continue;}- const generated = await callOrkasApiVideo({- ...normalized,- apiKey: profile.apiKey,- signal: input.signal,- onProgress: input.onProgress,- ...(input.usageContext ? { usageContext: input.usageContext } : {}),- });+ const selectedModel = adapterRes.model || candidate.entry.model;const finalPath = ensureMp4Extension(input.outputAbsPath);try {+ emitProgress(input.onProgress, 'save', 'Saving generated video', { provider: candidate.entry.provider });throwIfAborted(input.signal);- emitProgress(input.onProgress, 'save', 'Saving generated video');await fs.mkdir(path.dirname(finalPath), { recursive: true });- await fs.writeFile(finalPath, generated.buffer);+ await fs.writeFile(finalPath, adapterRes.buffer);} catch (err) {+ const message = (err as Error).message || String(err);log.error('video write failed after provider completed', {- task_id: generated.taskId,+ provider: candidate.entry.provider,+ model: selectedModel,+ taskId: adapterRes.taskId,path: logPathRef(finalPath),error: logErrorSummary(err),});return {ok: false,- errorCode: 'IO_ERROR',+ errorCode: input.signal?.aborted || /operation aborted/i.test(message) ? 'BAD_INPUT' : 'IO_ERROR',message: sanitizeLogTextForUpload(- `The provider completed this potentially billable video, but it could not be saved locally. Do not retry automatically. ${(err as Error).message}`,+ `The provider completed this potentially billable video, but it could not be saved locally. `+ + `Do not retry automatically. Local write error: ${message}`,),- taskId: generated.taskId,+ taskId: adapterRes.taskId,postDispatch: true,};}- registerGeneratedMediaUrl(finalPath, generated.videoUrl);+ registerGeneratedMediaUrl(finalPath, adapterRes.videoUrl);+ log.info('video generated', {+ provider: candidate.entry.provider,+ model: selectedModel,+ taskId: adapterRes.taskId,+ path: logPathRef(finalPath),+ bytes: adapterRes.buffer.length,+ });return {ok: true,path: finalPath,- bytes: generated.buffer.length,- provider: ORKAS_API_PROVIDER,- model: ORKAS_API_VIDEO_MODEL,- taskId: generated.taskId,- videoUrl: generated.videoUrl,- status: generated.status,- ...(generated.ratio ? { ratio: generated.ratio } : {}),- ...(typeof generated.duration === 'number' ? { duration: generated.duration } : {}),- ...(generated.resolution ? { resolution: generated.resolution } : {}),- };- } catch (err) {- const message = sanitizeLogTextForUpload((err as Error).message || String(err));- if (err instanceof VideoDeliveryError) {- return {- ok: false,- errorCode: 'DELIVERY_ERROR',- message,- taskId: err.taskId,- postDispatch: true,- };- }- if (err instanceof OrkasApiQuotaError) {- return { ok: false, errorCode: 'CREDITS_EXHAUSTED', message };- }- if (input.signal?.aborted || /operation aborted/i.test(message)) {- return { ok: false, errorCode: 'BAD_INPUT', message };- }- return {- ok: false,- errorCode: /timed out|timeout/i.test(message) ? 'TIMEOUT' : 'PROVIDER_API_ERROR',- message,+ bytes: adapterRes.buffer.length,+ provider: candidate.entry.provider,+ model: selectedModel,+ taskId: adapterRes.taskId,+ videoUrl: adapterRes.videoUrl,+ status: adapterRes.status,+ ...(adapterRes.ratio ? { ratio: adapterRes.ratio } : {}),+ ...(typeof adapterRes.duration === 'number' ? { duration: adapterRes.duration } : {}),+ ...(adapterRes.resolution ? { resolution: adapterRes.resolution } : {}),};}++ return {+ ok: false,+ errorCode: lastProviderError.includes('timed out') ? 'TIMEOUT' : 'PROVIDER_API_ERROR',+ message: sanitizeLogTextForUpload(lastProviderError || 'video generation failed'),+ };}export const video_gen = generateVideo;@@ -230,11 +373,6 @@ async function resolveReferenceUrls(input: GenerateVideoInput): Promise<{referenceImageUrls: string[];referenceVideoUrls: string[];}> {- const count = (input.referenceImageUrls?.length || 0)- + (input.referenceImagePaths?.length || 0)- + (input.referenceVideoUrls?.length || 0)- + (input.referenceVideoPaths?.length || 0);- if (count) emitProgress(input.onProgress, 'prepare_references', 'Preparing video references', { count });const referenceImageUrls = await prepareReferenceUrls({kind: 'image',urls: input.referenceImageUrls,@@ -257,26 +395,33 @@ async function resolveReferenceUrls(input: GenerateVideoInput): Promise<{return { referenceImageUrls, referenceVideoUrls };}-function normalizeVideoInput(input: GenerateVideoInput): NormalizedVideoInput {- const operation = input.operation === 'edit' ? 'edit' : 'generate';- const referenceVideoUrls = (input.referenceVideoUrls || []).slice(0, 3);- if (operation === 'edit' && referenceVideoUrls.length < 1) {- throw new Error('operation=edit requires at least one reference video');+async function dispatchVideo(req: AdapterVideoRequest, provider: string): Promise<AdapterVideoResult> {+ if (provider === ORKAS_API_PROVIDER) {+ return callOrkasApiVideo({ ...req, quality: req.quality === 'quality' ? 'quality' : 'balanced' });}- return {- prompt: input.prompt.trim(),- referenceImageUrls: (input.referenceImageUrls || []).slice(0, 9),- referenceVideoUrls,- operation,- ratio: choice(input.ratio, ['16:9', '9:16', '1:1', '4:3', '3:4', '21:9'], '16:9'),- duration: integer(input.duration, 5, 4, 15),- resolution: choice(input.resolution, ['480p', '720p', '1080p'], '720p'),- quality: input.quality === 'quality' ? 'quality' : 'balanced',- generateAudio: input.generateAudio !== false,- ...(typeof input.seed === 'number' && Number.isFinite(input.seed) ? { seed: Math.trunc(input.seed) } : {}),- pollIntervalMs: integer(input.pollIntervalMs, DEFAULT_POLL_INTERVAL_MS, 1_000, 30_000),- timeoutMs: integer(input.timeoutMs, DEFAULT_TIMEOUT_MS, 30_000, DEFAULT_TIMEOUT_MS),- };++ if (provider === 'doubao') req = normalizeDoubaoVideoRequest(req);+ emitProgress(+ req.onProgress,+ 'configuration',+ `Video configuration: ${provider}/${videoModelDisplayName(req.model)}, ${req.resolution}, ${req.duration}s, `+ + `${req.generateAudio ? 'audio requested' : 'silent'}; user API key, 0 Orkas credits/second`,+ {+ provider,+ model: req.model,+ duration: req.duration,+ resolution: req.resolution,+ generateAudio: req.generateAudio,+ creditsMilliPerSecond: 0,+ },+ );+ if (provider === 'doubao') return callDoubaoVideo(req);+ if (provider === 'minimax') return callMiniMaxVideo(req);+ if (provider === 'vidu') return callViduVideo(req);+ if (provider === 'kling') return callKlingVideo(req);+ if (provider === 'google') return callGoogleVideo(req);+ if (provider === 'runway') return callRunwayVideo(req);+ throw new Error(`Unknown video-gen provider: ${provider}`);}async function callOrkasApiVideo(req: OrkasVideoRequest): Promise<OrkasVideoResult> {@@ -393,21 +538,27 @@ async function downloadVideo(taskId: string,signal?: AbortSignal,onProgress?: VideoProgressReporter,+ headers?: Record<string, string>,): Promise<Buffer> {emitProgress(onProgress, 'download', 'Downloading generated video');const timeout = composeAbortSignal(signal, DOWNLOAD_TIMEOUT_MS, 'video download timed out');try {const result = await downloadBinaryWithProxyPolicy(url, {- label: 'Orkas API generated video download',+ label: 'generated video download',+ ...(headers ? { headers } : {}),signal: timeout.signal,maxBytes: 512 * 1024 * 1024,validate: validateDownloadedVideo,});emitProgress(onProgress, 'download_done', `Video download complete: ${result.body.length} bytes`);+ throwIfAborted(signal);return result.body;} catch (err) {+ const message = signal?.aborted+ ? 'delivery was cancelled after the provider completed generation'+ : timeout.signal.aborted ? 'video download timed out' : (err as Error).message;throw new VideoDeliveryError(- `The provider completed task ${taskId}, but the generated video could not be downloaded after bounded retries. Do not submit another generation task. ${(err as Error).message}`,+ `The provider completed task ${taskId}, but the generated video could not be downloaded after bounded retries. Do not submit another generation task. ${message}`,taskId,{ cause: err },);@@ -420,25 +571,26 @@ export function validateDownloadedVideo(buffer: Buffer): void {const scanLimit = Math.min(buffer.length, 4096);let offset = 0;while (offset + 8 <= scanLimit) {- const boxSize = buffer.readUInt32BE(offset);+ let boxSize = buffer.readUInt32BE(offset);const boxType = buffer.toString('ascii', offset + 4, offset + 8);- if (boxType === 'ftyp' && boxSize >= 16 && offset + boxSize <= buffer.length) return;- if (boxSize < 8 || offset + boxSize > scanLimit) break;+ let headerSize = 8;+ if (boxSize === 1) {+ if (offset + 16 > scanLimit) break;+ const extendedSize = buffer.readBigUInt64BE(offset + 8);+ if (extendedSize > BigInt(Number.MAX_SAFE_INTEGER)) break;+ boxSize = Number(extendedSize);+ headerSize = 16;+ }+ if (boxType === 'ftyp') {+ if (boxSize >= headerSize + 8 && offset + boxSize <= buffer.length) return;+ break;+ }+ if (boxSize === 0 || boxSize < headerSize || offset + boxSize > scanLimit) break;offset += boxSize;}throw new Error('video download returned invalid or unsupported MP4 bytes');}-function integer(value: unknown, fallback: number, min: number, max: number): number {- const number = typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : fallback;- return Math.max(min, Math.min(max, number));-}--function choice(value: unknown, allowed: string[], fallback: string): string {- const normalized = typeof value === 'string' ? value.trim() : '';- return allowed.includes(normalized) ? normalized : fallback;-}-function ensureMp4Extension(value: string): string {const extension = path.extname(value);if (extension.toLowerCase() === '.mp4') return value;@@ -449,26 +601,691 @@ function truncate(value: string, max: number): string {return value.length <= max ? value : `${value.slice(0, max)}…`;}+function emitProgress(+ onProgress: VideoProgressReporter | undefined,+ phase: string,+ message: string,+ data?: Record<string, unknown>,+): void {+ onProgress?.({ phase, message, ...(data ? { data } : {}) });+}++class VideoGenerationAcceptedTaskError extends Error {+ constructor(message: string, readonly taskId: string, readonly errorCode: 'PROVIDER_API_ERROR' | 'TIMEOUT' | 'BAD_INPUT', options?: ErrorOptions) {+ super(message, options);+ }+}++function klingVideoInputError(input: GenerateVideoInput & { ratioExplicit?: boolean }): string | null {+ const imageCount = (input.referenceImageUrls?.length || 0) + (input.referenceImagePaths?.length || 0);+ const videoCount = (input.referenceVideoUrls?.length || 0) + (input.referenceVideoPaths?.length || 0);+ if (input.operation === 'edit' || videoCount) return 'Kling 3.0 Turbo supports text or one first-frame image; video editing is unsupported.';+ if (imageCount > 1) return 'Kling 3.0 Turbo accepts only one first-frame image.';+ if (input.seed !== undefined) return 'Kling 3.0 Turbo does not support a seed parameter.';+ // Turbo supports native audio but its API exposes no audio-off control.+ if (input.generateAudio === false) return 'Kling 3.0 Turbo does not expose a silent-video control. Use a video service that supports silent generation.';+ if (input.resolution !== undefined && !['720p', '1080p'].includes(input.resolution)) return 'Kling 3.0 Turbo resolution must be 720p or 1080p.';+ if (input.duration !== undefined && (!Number.isInteger(input.duration) || input.duration < 3 || input.duration > 15)) return 'Kling 3.0 Turbo duration must be an integer from 3 to 15 seconds.';+ if (imageCount && (input.ratioExplicit ?? (input.ratio !== undefined))) return 'Kling 3.0 Turbo image generation does not accept an aspect-ratio override. Omit ratio and use the first-frame image dimensions.';+ if (!imageCount && input.ratio !== undefined && !['16:9', '9:16', '1:1'].includes(input.ratio)) return 'Kling 3.0 Turbo text generation supports 16:9, 9:16 or 1:1.';+ if (input.prompt.trim().length > (imageCount ? 2500 : 3072)) return 'The prompt exceeds the Kling 3.0 Turbo limit. Shorten it before retrying.';+ return null;+}++async function fetchVideoControl<T>(+ url: string, init: RequestInit, req: AdapterVideoRequest, timeoutMs: number,+ timeoutMessage: string, readBody: (response: Response) => Promise<T>, creation = false,+): Promise<T> {+ throwIfAborted(req.signal);+ if (creation) req.onRequestDisposition?.('sent');+ const { body } = await fetchAndReadWithTimeout(url, init, timeoutMs, req.signal, timeoutMessage, response => {+ if (creation) {+ // A successful header is acceptance, not a usable task receipt. A 5xx+ // or lost connection does not prove that creation had no side effect.+ if (response.ok) req.onRequestDisposition?.('accepted');+ else if (response.status >= 400 && response.status < 500) req.onRequestDisposition?.('rejected');+ }+ return readBody(response);+ });+ return body;+}++async function readNativeVideoResponse(response: Response, label: string): Promise<any> {+ const text = await response.text();+ if (!response.ok) throw new Error(`${label} API ${response.status}`);+ try { return text ? JSON.parse(text) : {}; }+ catch { return {}; } // Invalid successful bodies remain missing receipts.+}++interface AdapterVideoResult {+ buffer: Buffer;+ taskId: string;+ videoUrl: string;+ status: string;+ ratio?: string;+ duration?: number;+ resolution?: string;+ model?: string;+}++function normalizeVideoInput(input: GenerateVideoInput): NormalizedVideoInput {+ if (input.operation === 'edit' && !(input.referenceVideoUrls?.length)) {+ throw new Error('operation=edit requires at least one reference video');+ }+ const duration = clampNumber(input.duration, 5, 4, 15);+ const pollIntervalMs = clampNumber(input.pollIntervalMs, DEFAULT_POLL_INTERVAL_MS, 1000, 30000);+ const timeoutMs = clampNumber(input.timeoutMs, DEFAULT_TIMEOUT_MS, 30_000, 60 * 60 * 1000);+ return {+ prompt: input.prompt.trim(),+ referenceImageUrls: (input.referenceImageUrls || []).map((u) => String(u || '').trim()).filter(Boolean).slice(0, 9),+ referenceVideoUrls: (input.referenceVideoUrls || []).map((u) => String(u || '').trim()).filter(Boolean).slice(0, 3),+ operation: input.operation === 'edit' ? 'edit' : 'generate',+ ratio: normalizeChoice(input.ratio, ['16:9', '9:16', '1:1', '4:3', '3:4', '21:9'], '16:9'),+ duration,+ resolution: normalizeChoice(input.resolution, ['480p', '720p', '1080p'], '720p'),+ quality: normalizeChoice(input.quality, ['economy', 'balanced', 'quality'], 'balanced') as NormalizedVideoInput['quality'],+ generateAudio: input.generateAudio !== false,+ watermark: input.watermark === true,+ ...(typeof input.seed === 'number' && Number.isFinite(input.seed) ? { seed: Math.trunc(input.seed) } : {}),+ pollIntervalMs,+ timeoutMs,+ };+}++function clampNumber(value: unknown, fallback: number, min: number, max: number): number {+ const n = typeof value === 'number' && Number.isFinite(value) ? value : fallback;+ return Math.max(min, Math.min(max, Math.trunc(n)));+}++function normalizeChoice(value: unknown, allowed: string[], fallback: string): string {+ const v = typeof value === 'string' ? value.trim() : '';+ return allowed.includes(v) ? v : fallback;+}++function normalizeDoubaoVideoRequest(req: AdapterVideoRequest): AdapterVideoRequest {+ // Official Fast/Mini contract permits480p/720p. Requester confirmed the+ // 1080p ->720p fallback; keep Standard and the selected model identity.+ // https://docs.volcengine.com/docs/ark/video-generation-tutorial?lang=zh+ const supports1080p = !['doubao-seedance-2-0-fast-260128', 'doubao-seedance-2-0-mini', 'doubao-seedance-2-0-mini-260615'].includes(req.model);+ return req.resolution === '1080p' && !supports1080p ? { ...req, resolution: '720p' } : req;+}++export async function callDoubaoVideo(req: AdapterVideoRequest): Promise<AdapterVideoResult> {+ throwIfAborted(req.signal);+ req = normalizeDoubaoVideoRequest(req);+ const content: Array<Record<string, unknown>> = [{ type: 'text', text: req.prompt }];+ for (const url of req.referenceImageUrls) {+ content.push({+ type: 'image_url',+ role: 'reference_image',+ image_url: { url },+ });+ }+ for (const url of req.referenceVideoUrls) {+ content.push({+ type: 'video_url',+ role: 'reference_video',+ video_url: { url },+ });+ }++ const createBody = {+ // Pin only the outgoing Mini ID; keep saved profiles and result identity.+ // https://docs.volcengine.com/docs/ark/seedance-2-5?lang=en+ model: req.model === 'doubao-seedance-2-0-mini' ? 'doubao-seedance-2-0-mini-260615' : req.model,+ content,+ ratio: req.ratio,+ duration: req.duration,+ resolution: req.resolution,+ generate_audio: req.generateAudio,+ watermark: req.watermark,+ ...(typeof req.seed === 'number' ? { seed: req.seed } : {}),+ };++ emitProgress(req.onProgress, 'create_task', 'Creating Doubao video task', {+ provider: 'doubao',+ duration: req.duration,+ resolution: req.resolution,+ });+ const created = await fetchVideoControl(+ `${DOUBAO_BASE}/api/v3/contents/generations/tasks`,+ {+ method: 'POST',+ headers: {+ Authorization: `Bearer ${req.apiKey}`,+ 'Content-Type': 'application/json',+ },+ body: JSON.stringify(createBody),+ },+ req,+ VIDEO_CREATE_HTTP_TIMEOUT_MS,+ `DouBao video create timed out after ${Math.round(VIDEO_CREATE_HTTP_TIMEOUT_MS / 1000)}s`,+ response => readNativeVideoResponse(response, 'DouBao video create'),+ true,+ );+ const taskId = extractTaskId(created);+ if (!taskId) throw new Error(`DouBao video create API returned no task id: ${truncate(JSON.stringify(created), 500)}`);+ emitProgress(req.onProgress, 'task_created', `Video task created: ${taskId}; status: ${formatVideoStatus((created as any)?.status)}`, {+ provider: 'doubao',+ taskId,+ status: String((created as any)?.status || 'queued'),+ });++ const started = Date.now();+ let last: unknown = created;+ let pollAttempt = 0;+ let consecutivePollTimeouts = 0;+ while (Date.now() - started < req.timeoutMs) {+ throwIfAborted(req.signal);+ const now = Date.now();+ const currentStatus = String((last as any)?.status || 'processing').toLowerCase();+ pollAttempt += 1;+ emitProgress(req.onProgress, 'poll_wait', `Waiting ${Math.round(req.pollIntervalMs / 1000)}s before video task poll ${pollAttempt}; status: ${formatVideoStatus(currentStatus)}`, {+ provider: 'doubao',+ taskId,+ attempt: pollAttempt,+ status: currentStatus,+ elapsedMs: now - started,+ });+ await sleep(req.pollIntervalMs, req.signal);+ emitProgress(req.onProgress, 'poll_request', `Polling video task status, attempt ${pollAttempt}`, {+ provider: 'doubao',+ taskId,+ attempt: pollAttempt,+ elapsedMs: Date.now() - started,+ });+ let status = '';+ try {+ last = await fetchVideoControl(+ `${DOUBAO_BASE}/api/v3/contents/generations/tasks/${encodeURIComponent(taskId)}`,+ {+ method: 'GET',+ headers: {+ Authorization: `Bearer ${req.apiKey}`,+ 'Content-Type': 'application/json',+ },+ },+ req,+ VIDEO_POLL_HTTP_TIMEOUT_MS,+ `DouBao video query timed out after ${Math.round(VIDEO_POLL_HTTP_TIMEOUT_MS / 1000)}s`,+ response => readNativeVideoResponse(response, 'DouBao video query'),+ );+ status = String((last as any)?.status || '').toLowerCase();+ consecutivePollTimeouts = 0;+ emitProgress(req.onProgress, 'poll_result', `Video task poll ${pollAttempt}: ${formatVideoStatus(status)} after ${Math.round((Date.now() - started) / 1000)}s`, {+ provider: 'doubao',+ taskId,+ attempt: pollAttempt,+ status: status || 'processing',+ elapsedMs: Date.now() - started,+ });+ } catch (err) {+ if (req.signal?.aborted || !isVideoPollTimeoutError(err)) throw err;+ consecutivePollTimeouts += 1;+ if (consecutivePollTimeouts >= MAX_CONSECUTIVE_POLL_TIMEOUTS) {+ throw new Error(`DouBao video status polling stopped: the connection timed out ${consecutivePollTimeouts} times in a row (task ${taskId}). The network looks persistently interrupted — stopped instead of retrying silently until the overall timeout.`);+ }+ emitProgress(req.onProgress, 'poll_timeout', `Video status poll timed out; retrying (${consecutivePollTimeouts}/${MAX_CONSECUTIVE_POLL_TIMEOUTS})`, {+ provider: 'doubao',+ taskId,+ attempt: pollAttempt,+ retry: consecutivePollTimeouts,+ maxRetry: MAX_CONSECUTIVE_POLL_TIMEOUTS,+ elapsedMs: Date.now() - started,+ error: errorMessage(err),+ });+ continue;+ }+ if (['failed', 'cancelled', 'canceled'].includes(status)) {+ throw new Error(`DouBao video task ${taskId} ${status}: ${truncate(JSON.stringify((last as any)?.error || last), 500)}`);+ }+ if (['succeeded', 'success', 'completed', 'done'].includes(status)) {+ const videoUrl = extractVideoUrl(last);+ if (!videoUrl) throw new Error(`DouBao video task ${taskId} succeeded without video_url`);+ emitProgress(req.onProgress, 'task_succeeded', `Video task completed: ${taskId}; preparing download`, {+ provider: 'doubao',+ taskId,+ status: status || 'succeeded',+ elapsedMs: Date.now() - started,+ });+ const buffer = await downloadVideo(videoUrl, taskId, req.signal, req.onProgress);+ return {+ buffer,+ taskId,+ videoUrl,+ status: status || 'succeeded',+ ...(typeof (last as any)?.duration === 'number' ? { duration: (last as any).duration } : {}),+ ...(typeof (last as any)?.ratio === 'string' ? { ratio: (last as any).ratio } : {}),+ resolution: typeof (last as any)?.resolution === 'string' ? (last as any).resolution : req.resolution,+ };+ }+ }++ throw new Error(`DouBao video task ${taskId} timed out after ${Math.round(req.timeoutMs / 1000)}s; last response: ${truncate(JSON.stringify(last), 500)}`);+}++export async function callMiniMaxVideo(req: AdapterVideoRequest): Promise<AdapterVideoResult> {+ if (req.referenceVideoUrls.length || req.operation === 'edit') {+ throw new Error('MiniMax Hailuo 2.3 does not support reference-video editing in this adapter');+ }+ const duration = req.duration >= 8 ? 10 : 6;+ const resolution = req.resolution === '1080p' && duration === 6 ? '1080P' : '768P';+ const headers = { Authorization: `Bearer ${req.apiKey}`, 'Content-Type': 'application/json' };+ const createBody: Record<string, unknown> = {+ model: req.model,+ prompt: req.prompt,+ duration,+ resolution,+ };+ if (req.referenceImageUrls[0]) createBody.first_frame_image = req.referenceImageUrls[0];+ const created = await requestJson(+ `${MINIMAX_BASE}/v1/video_generation`,+ { method: 'POST', headers, body: JSON.stringify(createBody) },+ req,+ 'MiniMax video create',+ );+ const taskId = extractTaskId(created);+ if (!taskId) throw new Error(`MiniMax video create returned no task id: ${truncate(JSON.stringify(created), 500)}`);+ emitProgress(req.onProgress, 'task_created', `MiniMax video task created: ${taskId}`, { provider: 'minimax', taskId });++ const started = Date.now();+ while (Date.now() - started < req.timeoutMs) {+ await sleep(req.pollIntervalMs, req.signal);+ const queried = await requestJson(+ `${MINIMAX_BASE}/v1/query/video_generation?task_id=${encodeURIComponent(taskId)}`,+ { method: 'GET', headers },+ req,+ 'MiniMax video query',+ );+ const status = String((queried as any)?.status || '').toLowerCase();+ emitProgress(req.onProgress, 'poll_result', `MiniMax video task: ${formatVideoStatus(status)}`, { provider: 'minimax', taskId, status });+ if (status === 'fail' || status === 'failed') {+ throw new Error(`MiniMax video task ${taskId} failed: ${truncate(JSON.stringify(queried), 500)}`);+ }+ if (status === 'success' || status === 'succeeded') {+ const fileId = String((queried as any)?.file_id || '').trim();+ if (!fileId) throw new Error(`MiniMax video task ${taskId} succeeded without file_id`);+ const file = await requestJson(+ `${MINIMAX_BASE}/v1/files/retrieve?file_id=${encodeURIComponent(fileId)}`,+ { method: 'GET', headers },+ req,+ 'MiniMax video file retrieve',+ );+ const videoUrl = String((file as any)?.file?.download_url || '').trim();+ if (!videoUrl) throw new Error(`MiniMax file ${fileId} returned no download_url`);+ const buffer = await downloadVideo(videoUrl, taskId, req.signal, req.onProgress);+ return { buffer, taskId, videoUrl, status: 'succeeded', duration, resolution: resolution.toLowerCase(), model: req.model };+ }+ }+ throw new Error(`MiniMax video task ${taskId} timed out after ${Math.round(req.timeoutMs / 1000)}s`);+}++export async function callViduVideo(req: AdapterVideoRequest): Promise<AdapterVideoResult> {+ if (req.referenceVideoUrls.length || req.operation === 'edit') {+ throw new Error('Vidu Q3 Pro supports text/image generation here; reference-video editing requires a different Vidu model');+ }+ const headers = { Authorization: `Token ${req.apiKey}`, 'Content-Type': 'application/json' };+ const firstImage = req.referenceImageUrls[0];+ const endpoint = firstImage ? 'img2video' : 'text2video';+ const resolution = req.resolution === '480p' ? '540p' : req.resolution;+ const createBody: Record<string, unknown> = {+ model: req.model,+ prompt: req.prompt,+ duration: req.duration,+ resolution,+ audio: req.generateAudio,+ off_peak: false,+ ...(typeof req.seed === 'number' ? { seed: req.seed } : {}),+ };+ if (firstImage) createBody.images = [firstImage];+ else createBody.aspect_ratio = normalizeChoice(req.ratio, ['16:9', '9:16', '1:1', '4:3', '3:4'], '16:9');+ const created = await requestJson(+ `${VIDU_BASE}/ent/v2/${endpoint}`,+ { method: 'POST', headers, body: JSON.stringify(createBody) },+ req,+ 'Vidu video create',+ );+ const taskId = extractTaskId(created);+ if (!taskId) throw new Error(`Vidu video create returned no task id: ${truncate(JSON.stringify(created), 500)}`);+ emitProgress(req.onProgress, 'task_created', `Vidu video task created: ${taskId}`, { provider: 'vidu', taskId });+ const started = Date.now();+ while (Date.now() - started < req.timeoutMs) {+ await sleep(req.pollIntervalMs, req.signal);+ const queried = await requestJson(+ `${VIDU_BASE}/ent/v2/tasks/${encodeURIComponent(taskId)}/creations`,+ { method: 'GET', headers },+ req,+ 'Vidu video query',+ );+ const status = String((queried as any)?.state || (queried as any)?.status || '').toLowerCase();+ emitProgress(req.onProgress, 'poll_result', `Vidu video task: ${formatVideoStatus(status)}`, { provider: 'vidu', taskId, status });+ if (status === 'failed' || status === 'fail') throw new Error(`Vidu video task ${taskId} failed: ${truncate(JSON.stringify(queried), 500)}`);+ if (status === 'success' || status === 'succeeded') {+ const videoUrl = String((queried as any)?.creations?.[0]?.url || (queried as any)?.video_url || '').trim();+ if (!videoUrl) throw new Error(`Vidu video task ${taskId} succeeded without a video URL`);+ const buffer = await downloadVideo(videoUrl, taskId, req.signal, req.onProgress);+ return { buffer, taskId, videoUrl, status: 'succeeded', duration: req.duration, resolution, model: req.model };+ }+ }+ throw new Error(`Vidu video task ${taskId} timed out after ${Math.round(req.timeoutMs / 1000)}s`);+}++export async function callKlingVideo(req: AdapterVideoRequest): Promise<AdapterVideoResult> {+ throwIfAborted(req.signal);+ const inputError = klingVideoInputError({ ...req, outputAbsPath: '' });+ if (inputError) throw new Error(inputError);+ const firstImage = req.referenceImageUrls[0];+ const endpoint = firstImage ? 'image-to-video' : 'text-to-video';+ const headers = { Authorization: `Bearer ${req.apiKey}`, 'Content-Type': 'application/json' };+ // https://kling.ai/document-api/api/video/3-0-turbo/text-to-video+ // https://kling.ai/document-api/api/video/3-0-turbo/image-to-video+ const createBody: Record<string, unknown> = {+ ...(firstImage ? { contents: [{ type: 'prompt', text: req.prompt }, { type: 'first_frame', url: firstImage }] } : { prompt: req.prompt }),+ settings: { duration: req.duration, resolution: req.resolution, ...(!firstImage ? { aspect_ratio: req.ratio } : {}) },+ options: { watermark_info: { enabled: req.watermark } },+ };+ const created = await requestKlingJson(+ `${KLING_BASE}/${endpoint}/kling-3.0-turbo`,+ { method: 'POST', headers, body: JSON.stringify(createBody) },+ req,+ 'Kling video create',+ );+ const taskId = typeof created?.data?.id === 'string' ? created.data.id.trim() : '';+ if (!taskId) throw new Error('Kling video create returned no native task id.');+ emitProgress(req.onProgress, 'task_created', `Kling video task created: ${taskId}`, { provider: 'kling', taskId });+ const started = Date.now();+ let consecutivePollTimeouts = 0;+ try {+ while (Date.now() - started < req.timeoutMs) {+ await sleep(req.pollIntervalMs, req.signal);+ let queried: any;+ try {+ queried = await requestKlingJson(`${KLING_BASE}/tasks?${new URLSearchParams({ task_ids: taskId })}`, { method: 'GET', headers }, req, 'Kling video query');+ consecutivePollTimeouts = 0;+ } catch (err) {+ if (req.signal?.aborted || !isVideoPollTimeoutError(err)) throw err;+ if (++consecutivePollTimeouts >= MAX_CONSECUTIVE_POLL_TIMEOUTS) throw err;+ emitProgress(req.onProgress, 'poll_timeout', 'Kling task status request timed out; retrying the same task query', { provider: 'kling', taskId, retry: consecutivePollTimeouts });+ continue;+ }+ const task = Array.isArray(queried.data) ? queried.data.find((item: any) => String(item?.id || '') === taskId) : null;+ if (!task) throw new Error('Kling task query did not contain the accepted task.');+ const status = String(task.status || '').toLowerCase();+ emitProgress(req.onProgress, 'poll_result', `Kling video task: ${formatVideoStatus(status)}`, { provider: 'kling', taskId, status });+ if (status === 'failed') throw new Error('Kling could not complete the accepted video task.');+ if (status === 'succeeded') {+ const video = Array.isArray(task.outputs) ? task.outputs.find((item: any) => item?.type === 'video') : null;+ const videoUrl = String((req.watermark ? video?.watermark_url : video?.url) || '').trim();+ if (!videoUrl) throw new Error('Kling completed the task without the requested video output.');+ const buffer = await downloadVideo(videoUrl, taskId, req.signal, req.onProgress);+ return { buffer, taskId, videoUrl, status, duration: req.duration, resolution: req.resolution, ...(!firstImage ? { ratio: req.ratio } : {}), model: req.model };+ }+ if (status !== 'submitted' && status !== 'processing') throw new Error('Kling returned an unknown task state.');+ }+ throw new VideoGenerationAcceptedTaskError('Kling task polling reached its deadline. The task may still be running; do not submit another generation automatically.', taskId, 'TIMEOUT');+ } catch (err) {+ if (err instanceof VideoDeliveryError || err instanceof VideoGenerationAcceptedTaskError) throw err;+ throw new VideoGenerationAcceptedTaskError('Kling task tracking stopped after creation. Preserve this task receipt and do not submit another generation automatically.', taskId, req.signal?.aborted ? 'BAD_INPUT' : 'PROVIDER_API_ERROR', { cause: err });+ }+}++async function requestKlingJson(url: string, init: RequestInit, req: AdapterVideoRequest, label: string): Promise<any> {+ throwIfAborted(req.signal);+ const data = await fetchVideoControl(url, init, req,+ init.method === 'POST' ? VIDEO_CREATE_HTTP_TIMEOUT_MS : VIDEO_POLL_HTTP_TIMEOUT_MS,+ `${label} timed out`, response => readNativeVideoResponse(response, label), init.method === 'POST');+ if (data?.code !== 0) {+ // Native business errors can arrive with HTTP200. Do not echo private+ // provider messages or mistake a rejected envelope for successful output.+ const code = typeof data?.code === 'number' ? data.code : 'unknown';+ if (init.method === 'POST' && typeof data?.code === 'number') req.onRequestDisposition?.('rejected');+ throw new Error(`Kling rejected the video request (code ${code}). Verify the video service settings and parameters before retrying.`);+ }+ return data;+}++export async function callGoogleVideo(req: AdapterVideoRequest): Promise<AdapterVideoResult> {+ if (req.referenceVideoUrls.length || req.operation === 'edit') {+ throw new Error('Google Veo 3.1 direct adapter currently supports text/image generation, not reference-video editing');+ }+ const duration = nearestChoice(req.resolution === '1080p' ? 8 : req.duration, [4, 6, 8]);+ const headers = { 'x-goog-api-key': req.apiKey, 'Content-Type': 'application/json' };+ const instance: Record<string, unknown> = { prompt: req.prompt };+ if (req.referenceImageUrls[0]) instance.image = await imageUrlToGoogleVideoImage(req.referenceImageUrls[0], req);+ const createBody = {+ instances: [instance],+ parameters: {+ aspectRatio: normalizeChoice(req.ratio, ['16:9', '9:16'], '16:9'),+ durationSeconds: duration,+ resolution: req.resolution === '1080p' ? '1080p' : '720p',+ },+ };+ const created = await requestJson(+ `${GOOGLE_VIDEO_BASE}/models/${encodeURIComponent(req.model)}:predictLongRunning`,+ { method: 'POST', headers, body: JSON.stringify(createBody) },+ req,+ 'Google Veo create',+ );+ const taskId = String((created as any)?.name || '').trim();+ if (!taskId) throw new Error(`Google Veo create returned no operation name: ${truncate(JSON.stringify(created), 500)}`);+ emitProgress(req.onProgress, 'task_created', `Google Veo operation created: ${taskId}`, { provider: 'google', taskId });+ const started = Date.now();+ while (Date.now() - started < req.timeoutMs) {+ await sleep(req.pollIntervalMs, req.signal);+ const queried = await requestJson(+ `${GOOGLE_VIDEO_BASE}/${taskId}`,+ { method: 'GET', headers },+ req,+ 'Google Veo query',+ );+ if ((queried as any)?.error) throw new Error(`Google Veo operation failed: ${truncate(JSON.stringify((queried as any).error), 500)}`);+ const done = (queried as any)?.done === true;+ emitProgress(req.onProgress, 'poll_result', `Google Veo operation: ${done ? 'completed' : 'processing'}`, { provider: 'google', taskId, status: done ? 'succeeded' : 'processing' });+ if (done) {+ const videoUrl = String((queried as any)?.response?.generateVideoResponse?.generatedSamples?.[0]?.video?.uri || '').trim();+ if (!videoUrl) throw new Error(`Google Veo operation ${taskId} completed without a video URI`);+ const buffer = await downloadVideo(videoUrl, taskId, req.signal, req.onProgress, { 'x-goog-api-key': req.apiKey });+ return { buffer, taskId, videoUrl, status: 'succeeded', duration, resolution: req.resolution === '1080p' ? '1080p' : '720p', model: req.model };+ }+ }+ throw new Error(`Google Veo operation ${taskId} timed out after ${Math.round(req.timeoutMs / 1000)}s`);+}++export async function callRunwayVideo(req: AdapterVideoRequest): Promise<AdapterVideoResult> {+ if (req.referenceVideoUrls.length || req.operation === 'edit') {+ throw new Error('Runway Gen-4.5 supports text/image generation; use a Runway edit model for reference-video editing');+ }+ const headers = {+ Authorization: `Bearer ${req.apiKey}`,+ 'Content-Type': 'application/json',+ 'X-Runway-Version': '2024-11-06',+ };+ const firstImage = req.referenceImageUrls[0];+ const endpoint = firstImage ? 'image_to_video' : 'text_to_video';+ const duration = nearestChoice(req.duration, [5, 10]);+ const createBody: Record<string, unknown> = {+ model: req.model,+ promptText: req.prompt,+ ratio: runwayRatio(req.ratio, !!firstImage),+ duration,+ };+ if (firstImage) createBody.promptImage = firstImage;+ const created = await requestJson(+ `${RUNWAY_BASE}/v1/${endpoint}`,+ { method: 'POST', headers, body: JSON.stringify(createBody) },+ req,+ firstImage ? 'Runway image-to-video create' : 'Runway text-to-video create',+ );+ const taskId = extractTaskId(created);+ if (!taskId) throw new Error(`Runway video create returned no task id: ${truncate(JSON.stringify(created), 500)}`);+ emitProgress(req.onProgress, 'task_created', `Runway video task created: ${taskId}`, { provider: 'runway', taskId });+ const started = Date.now();+ while (Date.now() - started < req.timeoutMs) {+ await sleep(req.pollIntervalMs, req.signal);+ const queried = await requestJson(+ `${RUNWAY_BASE}/v1/tasks/${encodeURIComponent(taskId)}`,+ { method: 'GET', headers },+ req,+ 'Runway video query',+ );+ const status = String((queried as any)?.status || '').toLowerCase();+ emitProgress(req.onProgress, 'poll_result', `Runway video task: ${formatVideoStatus(status)}`, { provider: 'runway', taskId, status });+ if (status === 'failed' || status === 'canceled' || status === 'cancelled') {+ throw new Error(`Runway video task ${taskId} ${status}: ${truncate(JSON.stringify(queried), 500)}`);+ }+ if (status === 'succeeded' || status === 'success') {+ const videoUrl = String((queried as any)?.output?.[0] || '').trim();+ if (!videoUrl) throw new Error(`Runway video task ${taskId} succeeded without output`);+ const buffer = await downloadVideo(videoUrl, taskId, req.signal, req.onProgress);+ return { buffer, taskId, videoUrl, status: 'succeeded', duration, resolution: '720p', model: req.model };+ }+ }+ throw new Error(`Runway video task ${taskId} timed out after ${Math.round(req.timeoutMs / 1000)}s`);+}++function extractTaskId(data: unknown): string {+ const d = data as any;+ return String(d?.id || d?.task_id || d?.taskId || d?.data?.id || d?.data?.task_id || d?.data?.taskId || '').trim();+}++function extractVideoUrl(data: unknown): string {+ const d = data as any;+ return String(+ d?.content?.video_url+ || d?.content?.videoUrl+ || d?.content?.url+ || d?.video_url+ || d?.videoUrl+ || d?.data?.content?.video_url+ || d?.data?.video_url+ || '',+ ).trim();+}++async function requestJson(+ url: string,+ init: RequestInit,+ req: AdapterVideoRequest,+ label: string,+): Promise<any> {+ throwIfAborted(req.signal);+ const data = await fetchVideoControl(+ url,+ init,+ req,+ init.method === 'POST' ? VIDEO_CREATE_HTTP_TIMEOUT_MS : VIDEO_POLL_HTTP_TIMEOUT_MS,+ `${label} timed out`,+ response => readNativeVideoResponse(response, label),+ init.method === 'POST',+ );+ const businessCode = data?.base_resp?.status_code;+ if (typeof businessCode === 'number' && businessCode !== 0) {+ if (init.method === 'POST') req.onRequestDisposition?.('rejected');+ throw new Error(`${label} failed (code ${businessCode})`);+ }+ return data;+}++async function imageUrlToGoogleVideoImage(url: string, req: AdapterVideoRequest): Promise<Record<string, unknown>> {+ const timeoutMessage = 'Google Veo reference image download timed out';+ const composed = composeAbortSignal(req.signal, DOWNLOAD_TIMEOUT_MS, timeoutMessage);+ try {+ const result = await downloadBinaryWithProxyPolicy(url, {+ label: 'Google Veo reference image download',+ signal: composed.signal,+ maxBytes: 100 * 1024 * 1024,+ validate: (body) => {+ if (!detectReferenceImageMimeType(body)) {+ throw new Error('Google Veo reference image download returned unsupported image bytes');+ }+ },+ });+ // Veo predictLongRunning uses the native Image object, not a+ // generateContent Part. Matches Google SDK imageToMldev conversion.+ return {+ mimeType: detectReferenceImageMimeType(result.body),+ bytesBase64Encoded: result.body.toString('base64'),+ };+ } catch (err) {+ if (req.signal?.aborted) throw new Error('operation aborted');+ if (composed.signal.aborted) throw new Error(timeoutMessage);+ throw err;+ } finally {+ composed.cleanup();+ }+}++function detectReferenceImageMimeType(buffer: Buffer): string {+ if (buffer.length >= 8+ && buffer[0] === 0x89 && buffer[1] === 0x50+ && buffer[2] === 0x4e && buffer[3] === 0x47) return 'image/png';+ if (buffer.length >= 3+ && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) return 'image/jpeg';+ if (buffer.length >= 12+ && buffer.toString('ascii', 0, 4) === 'RIFF'+ && buffer.toString('ascii', 8, 12) === 'WEBP') return 'image/webp';+ return '';+}++function nearestChoice(value: number, choices: number[]): number {+ return choices.reduce((best, current) => (+ Math.abs(current - value) < Math.abs(best - value) ? current : best+ ), choices[0]);+}++function runwayRatio(ratio: string, imageToVideo: boolean): string {+ // Gen-4.5 text-to-video accepts only landscape and portrait. Image-to-video+ // additionally accepts square and wider/taller fixed resolutions.+ if (!imageToVideo) return ratio === '9:16' || ratio === '3:4' ? '720:1280' : '1280:720';+ return ({+ '16:9': '1280:720',+ '9:16': '720:1280',+ '1:1': '960:960',+ '4:3': '1104:832',+ '3:4': '832:1104',+ '21:9': '1584:672',+ } as Record<string, string>)[ratio] || '1280:720';+}++function videoModelDisplayName(model: string): string {+ return ({+ 'doubao-seedance-2-0-260128': 'Seedance 2.0',+ 'doubao-seedance-2-0-fast-260128': 'Seedance 2.0 Fast',+ 'doubao-seedance-2-0-mini': 'Seedance 2.0 Mini',+ 'server-managed': 'Server managed',+ } as Record<string, string>)[model] || model;+}+function sleep(ms: number, signal?: AbortSignal): Promise<void> {return new Promise((resolve, reject) => {+ try { throwIfAborted(signal); } catch (err) { reject(err); return; }+ let timer: ReturnType<typeof setTimeout>;const onAbort = () => {clearTimeout(timer);reject(new Error('operation aborted'));};- const timer = setTimeout(() => {- signal?.removeEventListener('abort', onAbort);+ timer = setTimeout(() => {+ signal?.removeEventListener?.('abort', onAbort);resolve();}, ms);- signal?.addEventListener('abort', onAbort, { once: true });- if (signal?.aborted) onAbort();+ signal?.addEventListener?.('abort', onAbort, { once: true });});}-function emitProgress(- onProgress: VideoProgressReporter | undefined,- phase: string,- message: string,- data?: Record<string, unknown>,-): void {- onProgress?.({ phase, message, ...(data ? { data } : {}) });+function formatVideoStatus(value: unknown): string {+ const status = String(value || 'processing').trim().toLowerCase();+ if (status === 'queued') return 'queued';+ if (status === 'running' || status === 'processing') return 'processing';+ if (status === 'succeeded' || status === 'success' || status === 'completed' || status === 'done') return 'completed';+ if (status === 'failed') return 'failed';+ if (status === 'cancelled' || status === 'canceled') return 'canceled';+ return status || 'processing';+}++function errorMessage(err: unknown): string {+ return err instanceof Error ? err.message : String(err || '');+}++function isVideoPollTimeoutError(err: unknown): boolean {+ return /\b(timed out|timeout)\b/i.test(errorMessage(err));}diff --git a/test/main/features/video_auth.test.ts b/test/main/features/video_auth.test.tsindex f1480d57..72e14aa4 100644--- a/test/main/features/video_auth.test.ts+++ b/test/main/features/video_auth.test.ts@@ -97,16 +97,46 @@ describe('video auth profiles', () => {model: 'orkas-video',label: 'Orkas · Video',});- expect(options).toHaveLength(1);- expect(listVideoProviderOptions().map((option) => option.id)).toEqual(['orkas-api']);+ expect(options).toHaveLength(4);+ expect(listVideoProviderOptions().map((option) => option.id)).toEqual(['orkas-api', 'doubao', 'minimax', 'vidu', 'kling', 'google', 'runway']);});});it('retains legacy credentials for removal but refuses new unsupported video profiles', () => {- mocks.profiles = [{ id: 'legacy', provider: 'doubao', model: 'doubao-seedance-2-0-260128', apiKey: 'legacy-key', label: 'Legacy', createdAt: 1 }];- expect(addVideoProfile({ provider: 'doubao', model: 'doubao-seedance-2-0-260128', apiKey: 'new-key' })).toMatchObject({ ok: false });+ mocks.profiles = [{ id: 'legacy', provider: 'legacy-provider', model: 'retired-model', apiKey: 'legacy-key', label: 'Legacy', createdAt: 1 }];+ expect(addVideoProfile({ provider: 'legacy-provider', model: 'retired-model', apiKey: 'new-key' })).toMatchObject({ ok: false });expect(listVideoProfiles()).toEqual(mocks.profiles);expect(listVideoProfiles()).toHaveLength(1);expect(removeVideoProfile('legacy')).toEqual({ ok: true });expect(listVideoProfiles()).toEqual([]);});+++describe('public BYO video capability parity', () => {+ const supported = [+ ['doubao','doubao-seedance-2-0-260128'], ['doubao','doubao-seedance-2-0-fast-260128'],+ ['doubao','doubao-seedance-2-0-mini'], ['minimax','MiniMax-Hailuo-2.3'],+ ['vidu','viduq3-pro'], ['kling','kling-v3-0-turbo'],+ ['google','veo-3.1-generate-preview'], ['runway','gen4.5'],+ ];+ it.each(supported)('saves a selected %s model and returns its saved identity', (provider, model) => {+ const result = addVideoProfile({provider,model,apiKey:'fixture-user-key',label:'My video service'});+ expect(result.ok).toBe(true);+ expect(listVideoProfiles()).toEqual([expect.objectContaining({provider,model,label:'My video service'})]);+ });+ it('returns every concrete BYO choice while preserving the public API entry', () => {+ const choices = flattenVideoProviderOptions(listVideoProviderOptions());+ expect(choices.map(p=>[p.provider,p.model])).toEqual([+ ['orkas-api','orkas-video'], ...supported,+ ]);+ expect(choices.filter(p=>p.provider==='doubao').map(p=>p.id)).toEqual([+ 'doubao:doubao-seedance-2-0-260128','doubao:doubao-seedance-2-0-fast-260128','doubao:doubao-seedance-2-0-mini',+ ]);+ expect(choices.some(p=>p.provider==='orkas-video')).toBe(false);+ });+ it('rejects a foreign provider/model pairing and missing user key before saving', () => {+ expect(addVideoProfile({provider:'google',model:'gen4.5',apiKey:'fixture'}).ok).toBe(false);+ expect(addVideoProfile({provider:'google',model:'veo-3.1-generate-preview',apiKey:''})).toEqual({ok:false,error:'apiKey required'});+ expect(mocks.profiles).toEqual([]);+ });+});diff --git a/test/main/features/video_gen-byo.test.ts b/test/main/features/video_gen-byo.test.tsnew file mode 100644index 00000000..5b325217--- /dev/null+++ b/test/main/features/video_gen-byo.test.ts@@ -0,0 +1,764 @@+import * as fs from 'node:fs/promises';+import * as os from 'node:os';+import * as path from 'node:path';+import { afterEach, describe, expect, it, vi } from 'vitest';++const TEST_MP4 = Buffer.from([0,0,0,24,0x66,0x74,0x79,0x70,0x69,0x73,0x6f,0x6d,0,0,2,0,0x69,0x73,0x6f,0x6d,0x69,0x73,0x6f,0x32]);+const mockState = vi.hoisted(() => ({ videoProfiles: [] as any[], workspace: '' }));+vi.mock('../../../src/main/features/auth', () => ({ loadVideoProfiles: () => mockState.videoProfiles }));+vi.mock('../../../src/main/features/user_workspace', () => ({getWorkspacePath:()=>mockState.workspace}));+vi.mock('../../../src/main/logger', () => ({ createLogger: () => ({info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn()}) }));+vi.mock('../../../src/main/features/generation_reference_assets', () => ({+ prepareReferenceUrls: async ({urls,paths}: {urls?: string[],paths?:string[]}) => {+ if(paths?.length) throw new Error('local references are outside this protocol fixture');+ return [...(urls || [])];+ }, registerGeneratedMediaUrl: vi.fn(),+}));+afterEach(async () => {+ (await import('../../../src/main/util/proxy-dispatcher'))._resetProxyRoutingForTests();+ vi.useRealTimers(); vi.unstubAllGlobals(); vi.resetModules(); mockState.videoProfiles = [];+});+// Shared frozen-source provider contracts; every request is intercepted by the fixture.+describe('video control request lifecycle', () => {+ const req = (signal?: AbortSignal) => ({ apiKey: 'fixture-key', model: 'kling-v3-0-turbo',+ prompt: 'A quiet landscape', referenceImageUrls: [], referenceVideoUrls: [],+ operation: 'generate', ratio: '16:9', duration: 5, resolution: '720p',+ generateAudio: true, watermark: false, pollIntervalMs: 1, timeoutMs: 600_000, signal } as any);+ const stalled = (init?: RequestInit) => new Response(new ReadableStream({+ start(controller) {+ init?.signal?.addEventListener('abort', () => controller.error(init.signal!.reason), { once: true });+ },+ }), { headers: { 'content-type': 'application/json' } });++ it('bounds stalled poll bodies and retries only the accepted task query', async () => {+ const { callKlingVideo } = await import('../../../src/main/features/video_gen');+ vi.useFakeTimers();+ const fetch = vi.fn(async (_url: string, init?: RequestInit) => init?.method === 'POST'+ ? new Response(JSON.stringify({ code: 0, data: { id: 'accepted-stalled-task' } })) : stalled(init));+ vi.stubGlobal('fetch', fetch);+ const outcome = callKlingVideo(req()).then(() => null, error => error);+ await vi.advanceTimersByTimeAsync(100_010);+ const observed = await Promise.race([outcome, Promise.resolve('still pending')]);+ expect(observed).toMatchObject({ taskId: 'accepted-stalled-task' });+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1);+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'GET')).toHaveLength(5);+ expect(vi.getTimerCount()).toBe(0);+ });++ it('cancels a stalled creation body after headers', async () => {+ const { callKlingVideo } = await import('../../../src/main/features/video_gen');+ vi.useFakeTimers();+ const user = new AbortController();+ const fetch = vi.fn(async (_url: string, init?: RequestInit) => stalled(init));+ vi.stubGlobal('fetch', fetch);+ const outcome = callKlingVideo(req(user.signal)).then(() => null, error => error);+ await vi.advanceTimersByTimeAsync(0);+ user.abort();+ await vi.advanceTimersByTimeAsync(0);+ const observed = await Promise.race([outcome, Promise.resolve('still pending')]);+ expect(observed).toMatchObject({ message: 'operation aborted' });+ expect(fetch).toHaveBeenCalledOnce();+ expect(vi.getTimerCount()).toBe(0);+ });++ it.each([+ ['doubao', 'doubao-seedance-2-0-260128'], ['minimax', 'MiniMax-Hailuo-2.3'],+ ['vidu', 'viduq3-pro'], ['kling', 'kling-v3-0-turbo'],+ ['google', 'veo-3.1-generate-preview'], ['runway', 'gen4.5'],+ ])('does not create a fallback task after an accepted %s response loses its receipt', async (provider, model) => {+ mockState.videoProfiles = [+ { id: 'primary', provider, model, apiKey: 'fixture-key', createdAt: 1 },+ { id: 'fallback', provider: 'doubao', model: 'doubao-seedance-2-0-260128', apiKey: 'fixture-key', createdAt: 2 },+ ];+ const fetch = vi.fn(async () => new Response(JSON.stringify({ code: 0, data: {} })));+ vi.stubGlobal('fetch', fetch);+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ const result = await generateVideo({ prompt: 'A quiet landscape', outputAbsPath: path.join(os.tmpdir(), 'uncreated-video-receipt.mp4') });+ expect(result).toMatchObject({ ok: false, postDispatch: true });+ expect(result).not.toHaveProperty('taskId');+ expect(fetch).toHaveBeenCalledOnce();+ });++ it('retains unknown dispatch status after losing creation headers without sending another request', async () => {+ mockState.videoProfiles = [+ { id: 'primary', provider: 'kling', model: 'kling-v3-0-turbo', apiKey: 'fixture-key', createdAt: 1 },+ { id: 'fallback', provider: 'doubao', model: 'doubao-seedance-2-0-260128', apiKey: 'fixture-key', createdAt: 2 },+ ];+ const fetch = vi.fn(async () => { throw new Error('fixture connection lost after dispatch'); });+ vi.stubGlobal('fetch', fetch);+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ expect(await generateVideo({ prompt: 'A quiet landscape', outputAbsPath: path.join(os.tmpdir(), 'uncreated-video-receipt.mp4') }))+ .toMatchObject({ ok: false, postDispatch: true });+ expect(fetch).toHaveBeenCalledOnce();+ });++ it('keeps private rejection bodies out of the public failure receipt', async () => {+ mockState.videoProfiles = [{ id: 'primary', provider: 'doubao', model: 'doubao-seedance-2-0-260128', apiKey: 'fixture-key', createdAt: 1 }];+ const fetch = vi.fn(async () => new Response(JSON.stringify({ error: { message: 'private-generation-prompt' } }), { status: 400 }));+ vi.stubGlobal('fetch', fetch);+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ const result = await generateVideo({ prompt: 'A quiet landscape', outputAbsPath: path.join(os.tmpdir(), 'uncreated-video-receipt.mp4') });+ expect(result).toMatchObject({ ok: false, errorCode: 'PROVIDER_API_ERROR' });+ expect(JSON.stringify(result)).not.toContain('private-generation-prompt');+ expect(fetch).toHaveBeenCalledOnce();+ });++ it.each(['http', 'business'])('preserves configured fallback after a proven %s rejection', async rejection => {+ mockState.videoProfiles = [+ { id: 'primary', provider: 'kling', model: 'kling-v3-0-turbo', apiKey: 'fixture-key', createdAt: 1 },+ { id: 'fallback', provider: 'doubao', model: 'doubao-seedance-2-0-260128', apiKey: 'fixture-key', createdAt: 2 },+ ];+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'orkas-video-rejection-'));+ const fetch = vi.fn(async (url: string, init?: RequestInit) => {+ if (url.includes('klingai.com')) return new Response(JSON.stringify({ code: 123, message: 'private-generation-prompt' }), { status: rejection === 'http' ? 400 : 200 });+ if (init?.method === 'POST') return new Response(JSON.stringify({ id: 'fallback-task' }));+ if (url.includes('/tasks/fallback-task')) return new Response(JSON.stringify({ status: 'succeeded', content: { video_url: 'https://video.example/rejected-fallback.mp4' } }));+ return new Response(new Uint8Array(TEST_MP4));+ });+ vi.stubGlobal('fetch', fetch);+ try {+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ expect(await generateVideo({ prompt: 'A quiet landscape', outputAbsPath: path.join(root, 'clip.mp4'), pollIntervalMs: 1 }))+ .toMatchObject({ ok: true, provider: 'doubao', taskId: 'fallback-task' });+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(2);+ } finally { await fs.rm(root, { recursive: true, force: true }); }+ });+});++describe('video_gen › latest user-key provider adapters', () => {+ // Ark's direct contract: Fast/Mini support only480p/720p. Requester chose+ // 1080p ->720p fallback; Standard retains1080p. The confirmed Mini mapping+ // changes only the upstream request ID, preserving saved/internal identity.+ it.each([+ ['doubao-seedance-2-0-fast-260128', '1080p', '720p', 'doubao-seedance-2-0-fast-260128'],+ ['doubao-seedance-2-0-mini', '1080p', '720p', 'doubao-seedance-2-0-mini-260615'],+ ['doubao-seedance-2-0-mini-260615', '1080p', '720p', 'doubao-seedance-2-0-mini-260615'],+ ['doubao-seedance-2-0-fast-260128', '480p', '480p', 'doubao-seedance-2-0-fast-260128'],+ ['doubao-seedance-2-0-mini', '720p', '720p', 'doubao-seedance-2-0-mini-260615'],+ ['doubao-seedance-2-0-260128', '1080p', '1080p', 'doubao-seedance-2-0-260128'],+ ])('preserves %s selection and adapts resolution %s to %s before dispatch', async (model, resolution, expected, upstreamModel) => {+ const fetch = vi.fn(async (url: string, init?: RequestInit) => {+ if (init?.method === 'POST') {+ expect(JSON.parse(String(init.body))).toMatchObject({ model: upstreamModel, resolution: expected });+ return new Response(JSON.stringify({ id: 'fixture-doubao' }));+ }+ if (url.endsWith('/tasks/fixture-doubao')) return new Response(JSON.stringify({ status: 'succeeded', content: { video_url: 'https://video.example/seedance.mp4' } }));+ if (url === 'https://video.example/seedance.mp4') return new Response(TEST_MP4);+ throw new Error(`unexpected fixture request: ${url}`);+ });+ vi.stubGlobal('fetch', fetch);+ const { callDoubaoVideo } = await import('../../../src/main/features/video_gen');+ expect(await callDoubaoVideo({ apiKey: 'fixture-key', model, prompt: 'a quiet lake', resolution,+ referenceImageUrls: [], referenceVideoUrls: [], operation: 'generate', ratio: '16:9', duration: 5,+ generateAudio: true, watermark: false, pollIntervalMs: 1, timeoutMs: 1000 } as any))+ .toMatchObject({ buffer: TEST_MP4, resolution: expected });+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1);+ });++ it('uses the official Mini request ID from an existing profile without rewriting the profile', async () => {+ const profile = { id: 'saved-mini', provider: 'doubao', model: 'doubao-seedance-2-0-mini', apiKey: 'fixture-key', label: 'My Mini', createdAt: 1 };+ mockState.videoProfiles = [{ ...profile }];+ const fetch = vi.fn(async (url: string, init?: RequestInit) => {+ if (init?.method === 'POST') {+ expect(url).toBe('https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks');+ expect(init.headers).toMatchObject({ Authorization: 'Bearer fixture-key' });+ expect(JSON.parse(String(init.body))).toMatchObject({ model: 'doubao-seedance-2-0-mini-260615', resolution: '720p' });+ return new Response(JSON.stringify({ id: 'mini-task' }));+ }+ if (url.endsWith('/tasks/mini-task')) return new Response(JSON.stringify({ model: 'doubao-seedance-2-0-mini-260615', status: 'succeeded', content: { video_url: 'https://video.example/mini.mp4' } }));+ if (url === 'https://video.example/mini.mp4') return new Response(TEST_MP4);+ throw new Error('unexpected fixture request');+ });+ vi.stubGlobal('fetch', fetch);+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'mini-model-id-'));+ try {+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ const output = path.join(root, 'clip.mp4');+ expect(await generateVideo({ prompt: 'a quiet lake', outputAbsPath: output, resolution: '1080p', pollIntervalMs: 1, timeoutMs: 1000 }))+ .toMatchObject({ ok: true, provider: 'doubao', model: 'doubao-seedance-2-0-mini', taskId: 'mini-task', resolution: '720p' });+ expect(mockState.videoProfiles.find(p => p.id === 'saved-mini')).toEqual(profile);+ expect(await fs.readFile(output)).toEqual(TEST_MP4);+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1);+ } finally { await fs.rm(root, { recursive: true, force: true }); }+ });++ it('reports the confirmed720p fallback throughout the saved Fast profile journey', async () => {+ mockState.videoProfiles = [{ id: 'saved-fast', provider: 'doubao', model: 'doubao-seedance-2-0-fast-260128', apiKey: 'fixture-key', label: 'Doubao', createdAt: 1 }];+ const fetch = vi.fn(async (url: string, init?: RequestInit) => {+ if (init?.method === 'POST') return new Response(JSON.stringify({ id: 'fixture-fast' }));+ if (url.endsWith('/tasks/fixture-fast')) return new Response(JSON.stringify({ status: 'succeeded', content: { video_url: 'https://video.example/fast.mp4' } }));+ if (url === 'https://video.example/fast.mp4') return new Response(TEST_MP4);+ throw new Error(`unexpected fixture request: ${url}`);+ });+ vi.stubGlobal('fetch', fetch);+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'fast-resolution-'));+ const events: any[] = [];+ try {+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ expect(await generateVideo({ prompt: 'a quiet lake', outputAbsPath: path.join(root, 'clip.mp4'), resolution: '1080p', pollIntervalMs: 1, timeoutMs: 1000, onProgress: e => events.push(e) }))+ .toMatchObject({ ok: true, model: 'doubao-seedance-2-0-fast-260128', resolution: '720p' });+ expect(await fs.readFile(path.join(root, 'clip.mp4'))).toEqual(TEST_MP4);+ for (const event of events.filter(e => e.phase === 'configuration' || e.phase === 'create_task')) expect(event.data.resolution).toBe('720p');+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1);+ } finally { await fs.rm(root, { recursive: true, force: true }); }+ });++ it('creates, polls, and downloads with MiniMax, Vidu, Kling, Google, and Runway contracts', async () => {+ const calls: Array<{ url: string; init?: RequestInit }> = [];+ vi.stubGlobal('fetch', async (url: string, init?: RequestInit) => {+ calls.push({ url, init });+ if (url === 'https://api.minimax.io/v1/video_generation') {+ return new Response(JSON.stringify({ task_id: 'minimax_1' }), { status: 200 });+ }+ if (url === 'https://api.minimax.io/v1/query/video_generation?task_id=minimax_1') {+ return new Response(JSON.stringify({ status: 'Success', file_id: 'file_1' }), { status: 200 });+ }+ if (url === 'https://api.minimax.io/v1/files/retrieve?file_id=file_1') {+ return new Response(JSON.stringify({ file: { download_url: 'https://video.example/minimax.mp4' } }), { status: 200 });+ }+ if (url === 'https://api.vidu.com/ent/v2/text2video') {+ return new Response(JSON.stringify({ task_id: 'vidu_1' }), { status: 200 });+ }+ if (url === 'https://api.vidu.com/ent/v2/tasks/vidu_1/creations') {+ return new Response(JSON.stringify({ state: 'success', creations: [{ url: 'https://video.example/vidu.mp4' }] }), { status: 200 });+ }+ if (url === 'https://api-singapore.klingai.com/text-to-video/kling-3.0-turbo') {+ return new Response(JSON.stringify({ code: 0, data: { id: 'kling_1' } }), { status: 200 });+ }+ if (url === 'https://api-singapore.klingai.com/tasks?task_ids=kling_1') {+ return new Response(JSON.stringify({+ code: 0, data: [{ id: 'kling_1', status: 'succeeded', outputs: [{ type: 'video', url: 'https://video.example/kling.mp4' }] }],+ }), { status: 200 });+ }+ if (url === 'https://generativelanguage.googleapis.com/v1beta/models/veo-3.1-generate-preview:predictLongRunning') {+ return new Response(JSON.stringify({ name: 'operations/veo_1' }), { status: 200 });+ }+ if (url === 'https://generativelanguage.googleapis.com/v1beta/operations/veo_1') {+ return new Response(JSON.stringify({+ done: true,+ response: { generateVideoResponse: { generatedSamples: [{ video: { uri: 'https://video.example/google.mp4' } }] } },+ }), { status: 200 });+ }+ if (url === 'https://api.dev.runwayml.com/v1/text_to_video') {+ return new Response(JSON.stringify({ id: 'runway_1' }), { status: 200 });+ }+ if (url === 'https://api.dev.runwayml.com/v1/tasks/runway_1') {+ return new Response(JSON.stringify({ status: 'SUCCEEDED', output: ['https://video.example/runway.mp4'] }), { status: 200 });+ }+ if (url.startsWith('https://video.example/')) {+ return new Response(new Uint8Array(TEST_MP4), { status: 200 });+ }+ throw new Error(`unexpected fetch: ${url}`);+ });++ const m = await import('../../../src/main/features/video_gen');+ const req = (model: string) => ({+ apiKey: 'user-key',+ model,+ prompt: 'a quiet landscape',+ referenceImageUrls: [],+ referenceVideoUrls: [],+ operation: 'generate',+ ratio: '16:9',+ duration: 5,+ resolution: '720p',+ generateAudio: true,+ watermark: false,+ pollIntervalMs: 1,+ timeoutMs: 1000,+ } as any);++ const results = await Promise.all([+ m.callMiniMaxVideo(req('MiniMax-Hailuo-2.3')),+ m.callViduVideo(req('viduq3-pro')),+ m.callKlingVideo(req('kling-v3-0-turbo')),+ m.callGoogleVideo(req('veo-3.1-generate-preview')),+ m.callRunwayVideo(req('gen4.5')),+ ]);++ expect(results.map((result) => result.model)).toEqual([+ 'MiniMax-Hailuo-2.3',+ 'viduq3-pro',+ 'kling-v3-0-turbo',+ 'veo-3.1-generate-preview',+ 'gen4.5',+ ]);+ expect(results.every((result) => result.buffer.equals(TEST_MP4))).toBe(true);++ const createBodies = Object.fromEntries(calls+ .filter((call) => call.init?.method === 'POST')+ .map((call) => [call.url, JSON.parse(call.init?.body as string)]));+ expect(createBodies['https://api.minimax.io/v1/video_generation']).toMatchObject({+ model: 'MiniMax-Hailuo-2.3', duration: 6, resolution: '768P',+ });+ expect(createBodies['https://api.vidu.com/ent/v2/text2video']).toMatchObject({+ model: 'viduq3-pro', audio: true, aspect_ratio: '16:9',+ });+ expect(createBodies['https://api-singapore.klingai.com/text-to-video/kling-3.0-turbo']).toEqual({+ prompt: 'a quiet landscape', settings: { duration: 5, resolution: '720p', aspect_ratio: '16:9' },+ options: { watermark_info: { enabled: false } },+ });+ expect(createBodies['https://api.dev.runwayml.com/v1/text_to_video']).toMatchObject({+ model: 'gen4.5', ratio: '1280:720', duration: 5,+ });+ expect(createBodies['https://api.dev.runwayml.com/v1/text_to_video']).not.toHaveProperty('promptImage');+ const googleDownload = calls.find((call) => call.url === 'https://video.example/google.mp4');+ expect(googleDownload?.init?.headers).toMatchObject({ 'x-goog-api-key': 'user-key' });+ });++ // Independent native protocol oracle: Kling-owned3.0Turbo docs, rather than+ // the legacy/v1/videos request that happened to pass the former fixture.+ it.each([false, true])('delivers saved Kling Turbo text/image video with native task identity (image=%s)', async imageMode => {+ mockState.videoProfiles = [{ id: 'saved-kling', provider: 'kling', model: 'kling-v3-0-turbo', apiKey: 'fixture-key', label: 'Kling', createdAt: 1 }];+ const fetch = vi.fn(async (url: string, init?: RequestInit) => {+ if (init?.method === 'POST') {+ expect(url).toBe(`https://api-singapore.klingai.com/${imageMode ? 'image-to-video' : 'text-to-video'}/kling-3.0-turbo`);+ expect(init.headers).toEqual({ Authorization: 'Bearer fixture-key', 'Content-Type': 'application/json' });+ const settings = { duration: imageMode ? 5 : 3, resolution: '1080p', ...(!imageMode ? { aspect_ratio: '9:16' } : {}) };+ expect(JSON.parse(String(init.body))).toEqual({+ ...(imageMode ? { contents: [{ type: 'prompt', text: 'a quiet landscape' }, { type: 'first_frame', url: 'https://images.example/portrait.png' }] } : { prompt: 'a quiet landscape' }),+ settings, options: { watermark_info: { enabled: true } },+ });+ return new Response(JSON.stringify({ code: 0, data: { id: 'saved-task' } }));+ }+ if (url === 'https://api-singapore.klingai.com/tasks?task_ids=saved-task') return new Response(JSON.stringify({ code: 0, data: [+ { id: 'foreign-task', status: 'succeeded', outputs: [{ type: 'video', url: 'https://video.example/wrong.mp4' }] },+ { id: 'saved-task', status: 'succeeded', outputs: [{ type: 'audio', mp3_url: 'https://video.example/audio.mp3' }, { type: 'video', url: 'https://video.example/plain.mp4', watermark_url: 'https://video.example/watermarked.mp4' }] },+ ] }));+ if (url === 'https://video.example/watermarked.mp4') return new Response(TEST_MP4);+ throw new Error(`unexpected fixture request: ${url}`);+ });+ vi.stubGlobal('fetch', fetch);+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'kling-native-'));+ try {+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ const output = path.join(root, 'clip.mp4');+ expect(await generateVideo({ prompt: 'a quiet landscape', outputAbsPath: output,+ ...(imageMode ? { referenceImageUrls: ['https://images.example/portrait.png'] } : { ratio: '9:16' }),+ duration: imageMode ? 5 : 3, resolution: '1080p', watermark: true, pollIntervalMs: 1, timeoutMs: 1000 }))+ .toMatchObject({ ok: true, model: 'kling-v3-0-turbo', taskId: 'saved-task', videoUrl: 'https://video.example/watermarked.mp4', resolution: '1080p' });+ expect(await fs.readFile(output)).toEqual(TEST_MP4);+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1);+ } finally { await fs.rm(root, { recursive: true, force: true }); }+ });++ it.each([+ { resolution: '480p' }, { ratio: '21:9' }, { duration: 16 }, { duration: 4.5 }, { seed: 1 },+ { generateAudio: false }, { referenceVideoUrls: ['https://video.example/reference.mp4'] },+ { referenceImageUrls: ['https://images.example/1.png', 'https://images.example/2.png'] },+ { referenceImageUrls: ['https://images.example/portrait.png'], ratio: '16:9' },+ ])('rejects unsupported Kling parameters before side effects or provider fallback (%j)', async input => {+ mockState.videoProfiles = [+ { id: 'kling', provider: 'kling', model: 'kling-v3-0-turbo', apiKey: 'fixture-key', label: 'Kling', createdAt: 1 },+ { id: 'fallback', provider: 'doubao', model: 'doubao-seedance-2-0-260128', apiKey: 'fixture-key', label: 'Doubao', createdAt: 2 },+ ];+ const fetch = vi.fn(); vi.stubGlobal('fetch', fetch);+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'kling-preflight-'));+ try {+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ expect(await generateVideo({ prompt: 'a quiet landscape', outputAbsPath: path.join(root, 'clip.mp4'), ...input }))+ .toMatchObject({ ok: false, errorCode: 'BAD_INPUT', dispatched: false });+ expect(fetch).not.toHaveBeenCalled();+ expect(await fs.readdir(root)).toEqual([]);+ } finally { await fs.rm(root, { recursive: true, force: true }); }+ });++ it.each(['failed', 'foreign', 'business-error'])('retains an accepted Kling task receipt without creating a fallback task (%s)', async outcome => {+ mockState.videoProfiles = [+ { id: 'kling', provider: 'kling', model: 'kling-v3-0-turbo', apiKey: 'fixture-key', label: 'Kling', createdAt: 1 },+ { id: 'fallback', provider: 'doubao', model: 'doubao-seedance-2-0-260128', apiKey: 'fixture-key', label: 'Doubao', createdAt: 2 },+ ];+ const fetch = vi.fn(async (_url: string, init?: RequestInit) => {+ if (init?.method === 'POST') return new Response(JSON.stringify({ code: 0, data: { id: 'accepted-task' } }));+ return new Response(JSON.stringify(outcome === 'business-error' ? { code: 1002, message: 'private provider detail' }+ : { code: 0, data: [{ id: outcome === 'foreign' ? 'another-task' : 'accepted-task', status: 'failed', message: 'private provider detail' }] }));+ });+ vi.stubGlobal('fetch', fetch);+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'kling-accepted-'));+ try {+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ const result = await generateVideo({ prompt: 'a quiet landscape', outputAbsPath: path.join(root, 'clip.mp4'), pollIntervalMs: 1, timeoutMs: 1000 });+ expect(result).toMatchObject({ ok: false, errorCode: 'PROVIDER_API_ERROR', taskId: 'accepted-task', postDispatch: true });+ expect(JSON.stringify(result)).not.toContain('private provider detail');+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1);+ expect(await fs.readdir(root)).toEqual([]);+ } finally { await fs.rm(root, { recursive: true, force: true }); }+ });++ it.each(['recover', 'persistent', 'deadline'])('keeps Kling polling on the accepted task without a new creation (%s)', async outcome => {+ let polls = 0;+ const fetch = vi.fn(async (url: string, init?: RequestInit) => {+ if (init?.method === 'POST') return new Response(JSON.stringify({ code: 0, data: { id: 'poll-task' }, id: 'wrong-envelope-id' }));+ if (url === 'https://api-singapore.klingai.com/tasks?task_ids=poll-task') {+ if (++polls === 1 || outcome === 'persistent') throw new Error('Kling video query timed out');+ return new Response(JSON.stringify({ code: 0, data: [{ id: 'poll-task', status: 'succeeded', outputs: [{ type: 'video', url: 'https://video.example/poll.mp4' }] }] }));+ }+ if (url === 'https://video.example/poll.mp4') return new Response(TEST_MP4);+ throw new Error('unexpected fixture request');+ });+ vi.stubGlobal('fetch', fetch);+ const { callKlingVideo } = await import('../../../src/main/features/video_gen');+ const request = callKlingVideo({+ apiKey: 'fixture-key', model: 'kling-v3-0-turbo', prompt: 'a quiet landscape',+ referenceImageUrls: [], referenceVideoUrls: [], operation: 'generate',+ ratio: '16:9', duration: 3, resolution: '720p', quality: 'balanced', generateAudio: true,+ watermark: false, pollIntervalMs: 1, timeoutMs: outcome === 'deadline' ? 0 : 600_000,+ });+ if (outcome === 'recover') {+ expect(await request).toMatchObject({ taskId: 'poll-task', buffer: TEST_MP4, duration: 3 });+ expect(polls).toBe(2);+ } else {+ await expect(request).rejects.toMatchObject({ taskId: 'poll-task', errorCode: outcome === 'deadline' ? 'TIMEOUT' : 'PROVIDER_API_ERROR' });+ expect(polls).toBe(outcome === 'deadline' ? 0 : 5);+ }+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1);+ });++ it('sends the native Veo image object and delivers the generated video through the saved profile', async () => {+ // Google-owned SDK oracle: src/converters/_models_converters.ts,+ // imageToMldev + generateVideosParametersToMldev. predictLongRunning uses+ // bytesBase64Encoded/mimeType, unlike Gemini generateContent inlineData.+ mockState.videoProfiles = [{ id: 'saved-veo', provider: 'google', model: 'veo-3.1-generate-preview', apiKey: 'fixture-key', label: 'Google', createdAt: 1 }];+ const image = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==', 'base64');+ const fetch = vi.fn(async (url: string, init?: RequestInit) => {+ if (url === 'https://images.example/first.png') return new Response(image);+ if (url.endsWith(':predictLongRunning')) {+ const body = JSON.parse(String(init?.body));+ expect(body.instances).toEqual([{ prompt: 'Animate the first frame.', image: { bytesBase64Encoded: image.toString('base64'), mimeType: 'image/png' } }]);+ expect(body.parameters).toEqual({ aspectRatio: '16:9', durationSeconds: 8, resolution: '1080p' });+ return new Response(JSON.stringify({ name: 'operations/fixture-veo' }));+ }+ if (url.endsWith('/operations/fixture-veo')) return new Response(JSON.stringify({ done: true, response: { generateVideoResponse: { generatedSamples: [{ video: { uri: 'https://video.example/veo.mp4' } }] } } }));+ if (url === 'https://video.example/veo.mp4') {+ expect(init?.headers).toEqual({ 'x-goog-api-key': 'fixture-key' });+ return new Response(TEST_MP4);+ }+ throw new Error(`unexpected fixture request: ${url}`);+ });+ vi.stubGlobal('fetch', fetch);+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'veo-image-contract-'));+ try {+ const { generateVideo } = await import('../../../src/main/features/video_gen');+ const output = path.join(root, 'clip.mp4');+ expect(await generateVideo({ prompt: 'Animate the first frame.', outputAbsPath: output,+ referenceImageUrls: ['https://images.example/first.png'], resolution: '1080p', duration: 6, pollIntervalMs: 1, timeoutMs: 1000 }))+ .toMatchObject({ ok: true, provider: 'google', taskId: 'operations/fixture-veo', duration: 8 });+ expect(await fs.readFile(output)).toEqual(TEST_MP4);+ expect(fetch.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1);+ } finally { await fs.rm(root, { recursive: true, force: true }); }+ });++ it('uses Runway image-to-video only when a reference image is present', async () => {+ const calls: Array<{ url: string; init?: RequestInit }> = [];+ vi.stubGlobal('fetch', async (url: string, init?: RequestInit) => {+ calls.push({ url, init });+ if (url === 'https://api.dev.runwayml.com/v1/image_to_video') {+ return new Response(JSON.stringify({ id: 'runway_image_1' }), { status: 200 });+ }+ if (url === 'https://api.dev.runwayml.com/v1/tasks/runway_image_1') {+ return new Response(JSON.stringify({+ status: 'SUCCEEDED', output: ['https://video.example/runway-image.mp4'],+ }), { status: 200 });+ }+ if (url === 'https://video.example/runway-image.mp4') {+ return new Response(new Uint8Array(TEST_MP4), { status: 200 });+ }+ throw new Error(`unexpected fetch: ${url}`);+ });++ const m = await import('../../../src/main/features/video_gen');+ const result = await m.callRunwayVideo({+ apiKey: 'user-key',+ model: 'gen4.5',+ prompt: 'animate the supplied portrait',+ referenceImageUrls: ['https://images.example/portrait.png'],+ referenceVideoUrls: [],+ operation: 'generate',+ ratio: '1:1',+ duration: 8,+ resolution: '720p',+ generateAudio: false,+ watermark: false,+ pollIntervalMs: 1,+ timeoutMs: 1000,+ } as any);++ expect(result.buffer.equals(TEST_MP4)).toBe(true);+ const createCalls = calls.filter((call) => call.init?.method === 'POST');+ expect(createCalls).toHaveLength(1);+ expect(createCalls[0]?.url).toBe('https://api.dev.runwayml.com/v1/image_to_video');+ expect(JSON.parse(String(createCalls[0]?.init?.body || '{}'))).toMatchObject({+ model: 'gen4.5',+ promptImage: 'https://images.example/portrait.png',+ promptText: 'animate the supplied portrait',+ ratio: '960:960',+ duration: 10,+ });+ });+});++describe('accepted video delivery ownership', () => {+ it('rejects a provider HTTP success with corrupt bytes and leaves no MP4 artifact', async () => {+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'orkas-video-gen-test-'));+ const outputPath = path.join(tmpDir, 'corrupt-result.mp4');+ mockState.videoProfiles = [{id:'public-api',provider:'orkas-api',model:'orkas-video',apiKey:'fixture-key'}];+ let createCount = 0;+ let downloadCount = 0;+ vi.stubGlobal('fetch', async (url: string) => {+ if (url === 'https://orkas.ai/v1/videos') {+ createCount += 1;+ return new Response(JSON.stringify({+ code: 0,+ id: 'corrupt-task',+ status: 'succeeded',+ url: 'https://video.example/corrupt-task.mp4',+ }), { status: 202 });+ }+ if (url === 'https://video.example/corrupt-task.mp4') {+ downloadCount += 1;+ return new Response('<html>expired signed URL</html>', { status: 200 });+ }+ throw new Error(`unexpected fetch: ${url}`);+ });++ try {+ vi.useFakeTimers();+ const m = await import('../../../src/main/features/video_gen');+ const generation = m.generateVideo({+ prompt: 'make a quiet test clip',+ outputAbsPath: outputPath,+ pollIntervalMs: 1,+ timeoutMs: 1000,+ });+ await vi.advanceTimersByTimeAsync(3_100);+ const result = await generation;++ expect(result).toMatchObject({+ ok: false,+ errorCode: 'DELIVERY_ERROR',+ taskId: 'corrupt-task',+ postDispatch: true,+ message: expect.stringMatching(/invalid or unsupported MP4 bytes/i),+ });+ expect(createCount).toBe(1);+ expect(downloadCount).toBe(3);+ await expect(fs.access(outputPath)).rejects.toMatchObject({ code: 'ENOENT' });+ } finally {+ await fs.rm(tmpDir, { recursive: true, force: true });+ }+ });++ it('keeps provider task evidence when cancellation interrupts only the output download backoff', async () => {+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'orkas-video-cancelled-delivery-'));+ const outputPath = path.join(tmpDir, 'cancelled.mp4');+ mockState.videoProfiles = [{id:'public-api',provider:'orkas-api',model:'orkas-video',apiKey:'fixture-key'}];+ const controller = new AbortController();+ let createCount = 0;+ let downloadCount = 0;+ vi.stubGlobal('fetch', async (url: string) => {+ if (url === 'https://orkas.ai/v1/videos') {+ createCount += 1;+ return new Response(JSON.stringify({+ code: 0,+ id: 'cancelled-delivery-task',+ status: 'succeeded',+ url: 'https://video.example/cancelled.mp4',+ }), { status: 202 });+ }+ if (url === 'https://video.example/cancelled.mp4') {+ downloadCount += 1;+ throw new Error('temporary transfer failure');+ }+ throw new Error(`unexpected fetch: ${url}`);+ });++ try {+ vi.useFakeTimers();+ const m = await import('../../../src/main/features/video_gen');+ const generation = m.generateVideo({+ prompt: 'one potentially billable clip',+ outputAbsPath: outputPath,+ signal: controller.signal,+ });+ await vi.advanceTimersByTimeAsync(1);+ expect(downloadCount).toBe(1);+ controller.abort(new Error('user cancelled'));++ await expect(generation).resolves.toMatchObject({+ ok: false,+ errorCode: 'DELIVERY_ERROR',+ taskId: 'cancelled-delivery-task',+ postDispatch: true,+ message: expect.stringMatching(/cancelled after the provider completed generation/i),+ });+ expect(createCount).toBe(1);+ expect(downloadCount).toBe(1);+ expect(vi.getTimerCount()).toBe(0);+ await expect(fs.access(outputPath)).rejects.toMatchObject({ code: 'ENOENT' });+ } finally {+ await fs.rm(tmpDir, { recursive: true, force: true });+ }+ });++ it('does not switch to another configured provider after output download retries are exhausted', async () => {+ mockState.videoProfiles = [+ {+ id: 'doubao-primary',+ provider: 'doubao',+ model: 'doubao-seedance-2-0-260128',+ apiKey: 'sk-primary',+ label: 'Primary',+ createdAt: 2,+ },+ {+ id: 'doubao-fallback',+ provider: 'doubao',+ model: 'doubao-seedance-2-0-fast-260128',+ apiKey: 'sk-fallback',+ label: 'Fallback',+ createdAt: 1,+ },+ ];+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'orkas-video-delivery-failure-'));+ const outputPath = path.join(tmpDir, 'result.mp4');+ let createCount = 0;+ let downloadCount = 0;+ vi.stubGlobal('fetch', async (url: string) => {+ if (url === 'https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks') {+ createCount += 1;+ return new Response(JSON.stringify({ id: `task_${createCount}`, status: 'queued' }), { status: 200 });+ }+ if (url === 'https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks/task_1') {+ return new Response(JSON.stringify({+ status: 'succeeded',+ content: { video_url: 'https://video.example/task_1.mp4' },+ }), { status: 200 });+ }+ if (url === 'https://video.example/task_1.mp4') {+ downloadCount += 1;+ throw new Error('temporary CDN connection reset');+ }+ throw new Error(`unexpected fetch: ${url}`);+ });++ try {+ vi.useFakeTimers();+ const m = await import('../../../src/main/features/video_gen');+ const generation = m.generateVideo({+ prompt: 'a single paid clip',+ outputAbsPath: outputPath,+ pollIntervalMs: 1,+ timeoutMs: 1000,+ });+ await vi.advanceTimersByTimeAsync(3_100);+ await expect(generation).resolves.toMatchObject({+ ok: false,+ errorCode: 'DELIVERY_ERROR',+ taskId: 'task_1',+ });+ expect(createCount).toBe(1);+ expect(downloadCount).toBe(3);+ } finally {+ await fs.rm(tmpDir, { recursive: true, force: true });+ }+ });++ it('does not create a second billable provider task when the first video cannot be saved locally', async () => {+ mockState.videoProfiles = [+ {+ id: 'doubao-primary',+ provider: 'doubao',+ model: 'doubao-seedance-2-0-260128',+ apiKey: 'sk-primary',+ label: 'Primary',+ createdAt: 2,+ },+ {+ id: 'doubao-fallback',+ provider: 'doubao',+ model: 'doubao-seedance-2-0-fast-260128',+ apiKey: 'sk-fallback',+ label: 'Fallback',+ createdAt: 1,+ },+ ];+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'orkas-video-save-failure-'));+ const blockedParent = path.join(tmpDir, 'not-a-directory');+ await fs.writeFile(blockedParent, 'occupied');+ let createCount = 0;+ vi.stubGlobal('fetch', async (url: string) => {+ if (url === 'https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks') {+ createCount += 1;+ return new Response(JSON.stringify({+ id: `task_${createCount}`,+ status: 'queued',+ }), { status: 200 });+ }+ if (url.startsWith('https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks/task_')) {+ const taskId = url.split('/').pop();+ return new Response(JSON.stringify({+ status: 'succeeded',+ content: { video_url: `https://video.example/${taskId}.mp4` },+ }), { status: 200 });+ }+ if (url.startsWith('https://video.example/task_')) {+ return new Response(new Uint8Array(TEST_MP4), { status: 200 });+ }+ throw new Error(`unexpected fetch: ${url}`);+ });++ try {+ const m = await import('../../../src/main/features/video_gen');+ const result = await m.generateVideo({+ prompt: 'a single paid clip',+ outputAbsPath: path.join(blockedParent, 'out.mp4'),+ pollIntervalMs: 1,+ timeoutMs: 1000,+ });++ expect(result).toMatchObject({+ ok: false,+ errorCode: 'IO_ERROR',+ message: expect.stringMatching(/provider completed.*do not retry automatically/i),+ });+ expect(createCount).toBe(1);+ } finally {+ await fs.rm(tmpDir, { recursive: true, force: true });+ }+ });++});+++describe('existing generate_video tool with a BYO profile', () => {+ it('delivers a Vidu MP4 and notifies the owning conversation through the unchanged tool contract', async () => {+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'orkas-byo-video-tool-'));+ mockState.workspace = root;+ mockState.videoProfiles = [{id:'saved-vidu',provider:'vidu',model:'viduq3-pro',apiKey:'fixture-key',label:'My Vidu',createdAt:1}];+ const requested: string[] = [];+ vi.stubGlobal('fetch', async (url: string, init?: RequestInit) => {+ requested.push(url);+ if (url==='https://api.vidu.com/ent/v2/text2video') {+ expect(init?.headers).toMatchObject({Authorization:'Token fixture-key'});+ expect(JSON.parse(String(init?.body))).toMatchObject({model:'viduq3-pro',prompt:'A quiet river.'});+ return new Response(JSON.stringify({task_id:'vidu-owned-task'}));+ }+ if (url==='https://api.vidu.com/ent/v2/tasks/vidu-owned-task/creations') return new Response(JSON.stringify({state:'success',creations:[{url:'https://video.example/owned.mp4'}]}));+ if (url==='https://video.example/owned.mp4') return new Response(TEST_MP4);+ throw new Error('unexpected fixture request');+ });+ try {+ const {createVideoGenTool} = await import('../../../src/main/model/core-agent/video-gen-tool');+ const delivered: string[] = [];+ const tool = createVideoGenTool({userId:'fixture-video-owner',onFileWritten:file=>{delivered.push(file);}});+ expect(tool.name).toBe('generate_video');+ const result = await tool.execute({prompt:'A quiet river.',output_path:'clip.mp4'}, {workingDir:root} as any);+ expect(result.isError).not.toBe(true);+ expect(result.content).toContain('vidu/viduq3-pro');+ expect(result.content).toContain('vidu-owned-task');+ expect(delivered).toEqual([path.join(root,'clip.mp4')]);+ expect(await fs.readFile(delivered[0])).toEqual(TEST_MP4);+ expect(requested).toEqual(['https://api.vidu.com/ent/v2/text2video','https://api.vidu.com/ent/v2/tasks/vidu-owned-task/creations','https://video.example/owned.mp4']);+ } finally { await fs.rm(root,{recursive:true,force:true}); }+ }, 15_000);+});