mirror of
https://github.com/nexu-io/openmotion.git
synced 2026-12-01 23:59:50 +08:00
* Fix cloud media save retry after CAS write failure * Address review feedback on retry_save state transition, cancellation, requestId, and docs - Transition retry_save rows atomically from failed to queued (stage=saving) before enqueuing so cancel before worker pickup marks the row cancelled and prevents worker claim. - Require and propagate caller requestId for BullMQ jobId deduplication (clearing stale terminal BullMQ entries on explicit re-retry). - Finalize cancelling rows cleanly to cancelled when CAS throws after a missed Redis cancel notification instead of stranding in cancelling or publishing a false failed event. - Update skills/openmotion-media/references/jobs.md to document Cloud gateway recovery for retry_save. * Harden cancel/retry_save concurrency, claim-epoch dedupe, and gateway docs - Guard cancelJob transitions with optimistic state checks and check cancelled state in the worker commit transaction. - Scope retry_save BullMQ jobId by claimToken epoch and record retrySaveRequestId so same-requestId retries recover cleanly after API restarts and Studio repeated retries never collide with a still-finishing BullMQ active job. - Guard upstream failed poll transitions against overwriting cancelling rows. - Update docs/model-gateway.md retry_save Cloud recovery guidance. * Guard retry_save enqueue-failure rollback against racing replays * fix(cloud): guard retry_save rollback by state/stage and clear failureStage on resume/completion * fix(cloud): return 400 for retry_save missing requestId and align timeout hint --------- Co-authored-by: 罗高 <luogao@users.noreply.github.com>
96 lines
6.7 KiB
TypeScript
96 lines
6.7 KiB
TypeScript
import type { LibraryGeneration } from './asset-library.ts';
|
|
import type { GenerationOptions } from './generation-options.ts';
|
|
import { openDesignImageReferenceCap } from './opendesign-models.ts';
|
|
/** Public contract shared by the model gateway, MCP and Studio. Never contains credentials. */
|
|
export const modalities = ['image', 'video', 'audio'] as const;
|
|
export type Modality = typeof modalities[number];
|
|
export { mediaProviders } from './provider-registry.ts';
|
|
/** Built-in media IDs and service-registered compatible connections share one route identity. */
|
|
export type ProviderId = string;
|
|
export type Model = {
|
|
id: string; provider: ProviderId; modelId: string; label: string; modality: Modality;
|
|
source: 'builtin' | 'live' | 'custom'; mode: string;
|
|
defaults?: Record<string, unknown>; description?: string;
|
|
/** Cloud: true/false is explicit provider evidence; absent means editing capability is unknown. */
|
|
supportsImageReference?: boolean;
|
|
/** Most input images the provider accepts: edit references for images, a first frame for video. */
|
|
maxInputImages?: number;
|
|
/** Provider-published native options (durations, resolutions, ratios, quality, sizes) shown during discovery. */
|
|
nativeOptions?: Record<string, unknown>;
|
|
/** Maintained, provider-native controls, shared by Studio and MCP discovery. */
|
|
generationOptions?: GenerationOptions;
|
|
};
|
|
export type Agent = { id: string; label: string; installed: boolean; authenticated: boolean;
|
|
imageGeneration: boolean; available: boolean; reason: string };
|
|
/** Advisory hints for signed-in accounts only; absent for signed-out or expired sessions. */
|
|
export type AccountMediaEntitlement = { mediaEntitlement: 'included' | 'not-included' | 'unknown'; plan?: string; hasBalance?: boolean };
|
|
export type ProviderState = { id: ProviderId; label: string; enabled: boolean; configured: boolean;
|
|
available: boolean; credentialSource: 'stored' | 'environment' | 'local-agent' | 'account' | 'none';
|
|
verification: 'unverified' | 'verified' | 'rejected'; reason: string; checkedAt?: number; baseUrl?: string } & Partial<AccountMediaEntitlement>;
|
|
export type GatewayStatus = { providers: ProviderState[]; agents: Agent[];
|
|
routes: Partial<Record<Modality, string>>;
|
|
capabilities: { modality: Modality; available: boolean; modelIds: string[]; selectedModelId?: string }[];
|
|
consoleUrl?: string };
|
|
export type GenerateInput = { modality: Modality; prompt: string; model?: string;
|
|
mode?: string;
|
|
projectId?: string; parameters?: Record<string, unknown>; idempotencyKey?: string;
|
|
/** Workspace assets can be generated without creating or selecting a film project. */
|
|
library?: LibraryGeneration;
|
|
/** Project-relative input image. Supported only by explicitly capable adapters. */
|
|
referenceAsset?: string;
|
|
/** Reserved for editor consumers. Recorded only; does not silently edit a composition. */
|
|
usage?: { compositionId?: string; elementId?: string; property?: string } };
|
|
export type GeneratedAsset = { path: string; mimeType: string; bytes: number; available?: boolean };
|
|
export type GenerationJob = { id: string; projectId?: string; library?: LibraryGeneration & { assetId: string; createNew?: boolean }; libraryAssets?: { assetId: string; versionId: string }[]; state: 'queued' | 'running' | 'completed' | 'failed' | 'cancelled';
|
|
/** New generation results also belong to the local workspace library. */
|
|
saveToLibrary?: boolean;
|
|
/** Existing outputs are eligible for retry_save without another generation. */
|
|
savePending?: boolean;
|
|
providerOrigin?: string;
|
|
providerRevision?: string;
|
|
modality: Modality; prompt: string; model: Model; parameters: Record<string, unknown>;
|
|
usage?: GenerateInput['usage']; idempotencyKey?: string; requestHash: string;
|
|
reference?: { path: string; sha256: string; mimeType: string; bytes: number };
|
|
/** Every snapshotted input image in submission order; `reference` remains the first one for older readers. */
|
|
references?: { path: string; sha256: string; mimeType: string; bytes: number }[];
|
|
/** Provider result metadata kept with the job, such as speech timestamps. */
|
|
result?: Record<string, unknown>;
|
|
/** Cloud confirmed failure/cancellation; polling cannot recover this task. */
|
|
remoteTerminal?: boolean;
|
|
/** Original account image transfer; signed URLs and credentials are never persisted. */
|
|
download?: { assetId: string; receivedBytes: number; totalBytes: number };
|
|
createdAt: number; updatedAt: number; startedAt?: number; workerPid?: number;
|
|
phase: string; errorCode?: string; error?: string; remoteId?: string; assets: GeneratedAsset[] };
|
|
|
|
/** Effective adapter input contract, shared by service validation and existing Studio controls. */
|
|
export function modelReferenceLimit(model: Model): number {
|
|
if (model.modality === 'image' && model.supportsImageReference) {
|
|
if (model.provider === 'codex') return 1;
|
|
const cap = ['opendesign', 'account'].includes(model.provider) ? openDesignImageReferenceCap(model.modelId) : undefined;
|
|
// Earlier Cloud syncs persisted a synthetic maxInputImages: 1 when nativeOptions omitted max_input_images.
|
|
const syntheticCloudFallback = model.provider === 'opendesign' && model.maxInputImages === 1
|
|
&& Boolean(model.nativeOptions) && !(Number.isInteger(model.nativeOptions?.max_input_images) && (model.nativeOptions!.max_input_images as number) > 0);
|
|
const declared = syntheticCloudFallback ? undefined : model.maxInputImages;
|
|
// OpenDesign catalogues without an explicit max_input_images fall back to the measured limit (unknown models stay at 1).
|
|
return Math.min(declared ?? cap ?? 1, cap ?? Infinity);
|
|
}
|
|
return model.modality === 'video' ? Math.min(model.maxInputImages ?? 0, 1) : 0;
|
|
}
|
|
/** Local eligibility; the service additionally rechecks project, origin and credentials. */
|
|
export function resumableJob(job: GenerationJob): boolean {
|
|
return (job.model.provider === 'opendesign' || job.model.provider === 'account' && job.modality === 'image') && typeof job.remoteId === 'string' && /^[a-zA-Z0-9_-]{1,180}$/.test(job.remoteId)
|
|
&& job.state === 'failed' && !job.savePending && !job.remoteTerminal
|
|
&& !confirmedRemoteTerminal(job)
|
|
&& !legacyRemoteTerminal(job);
|
|
}
|
|
function confirmedRemoteTerminal(job: GenerationJob): boolean {
|
|
const err = job.result && typeof job.result === 'object' && !Array.isArray(job.result)
|
|
? (job.result as { error?: unknown }).error
|
|
: undefined;
|
|
return Boolean(err && typeof err === 'object' && !Array.isArray(err) && (err as { outcomeUnknown?: unknown }).outcomeUnknown === false);
|
|
}
|
|
/** Earlier adapters recorded a Cloud-confirmed failure/cancellation only in the error message. */
|
|
export function legacyRemoteTerminal(job: GenerationJob): boolean {
|
|
return /^OpenDesign Cloud task (failed|cancelled)\b|^Account media task failed\./.test(job.error ?? '');
|
|
}
|