Implementation:0xsline OpenChatCut CreateGenerationJob
Appearance
| Knowledge Sources | |
|---|---|
| Domains | AI_Generation, Job_Management, Asynchronous |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
Concrete function that creates an asynchronous AI media generation job, persists it to SQLite, and queues the task function to run via `queueMicrotask`.
Description
The createGenerationJob function is the entry point for all AI media generation. It:
- Initializes the job store (loads persisted jobs from SQLite on first call).
- Cleans up old terminal jobs past their retention period via `cleanOldJobs`.
- Deduplicates — if an operation ID already exists, returns the existing job without re-creating.
- Creates a `GenerationJob` record with status `queued`, progress 0, timestamps, and all options.
- Persists the job to the SQLite store via `persistJobs`.
- Queues the async task via `queueMicrotask(() => runGenerationJob(job, task, options))`.
- Returns `{ operationId, jobId, status: 'queued' }` immediately.
The `task` parameter is an async function that the job lifecycle calls to execute the generation. It receives:
- `id` — the job ID.
- `update` — a callback to report progress.
- `registerDownload` — a callback to register a provider task for resume support.
- `registerProviderTask` — registers the provider's queue task ID for tracking.
Usage
- Image generation: The image plugin creates a job with an image-generation task.
- Video generation: The video plugin creates a job with a Fal.ai video task.
- Voice/music/sound: Each plugin creates a job with its provider-specific task.
Code Reference
Source Location
- Repository: OpenChatCut
- File:*** server/plugins/generation-job-lifecycle.ts
Signature
export type GenerationJobTask = (
id: string,
update: (next: Partial<GenerationJobProgress>) => void,
registerDownload: (url: string, label: GeneratedResultKind) => Promise<void>,
registerProviderTask: (taskId: string) => void,
) => Promise<GenerationResult>;
export interface GenerationJobOptions {
operationId?: string;
toolName?: string;
label?: string;
provider?: string;
expectedResultCount?: number;
cleanupPolicy?: string;
retentionMs?: number; // default 60 minutes
}
export interface GenerationJobSubmission {
operationId: string;
jobId: string;
status: 'queued';
}
export async function createGenerationJob(
params: Record<string, unknown>,
task: GenerationJobTask,
options?: GenerationJobOptions,
): Promise<GenerationJobSubmission>;
Import
import { createGenerationJob, type GenerationJobTask } from '../server/plugins/generation-job-lifecycle.ts';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| params | Record<string, unknown> | Yes | Generation parameters (prompt, model, etc.) |
| task | GenerationJobTask | Yes | Async function that calls the provider |
| options.operationId | string | No | Dedup ID (default: random UUID) |
| options.retentionMs | number | No | Job retention (default 60 min) |
| options.expectedResultCount | number | No | Expected number of results |
| options.provider | string | No | Provider name for tracking |
Outputs
| Name | Type | Description |
|---|---|---|
| operationId | string | Unique operation ID |
| jobId | string | Same as operation ID |
| status | 'queued' | Initial job status |
Usage Examples
Create an Image Generation Job
import { createGenerationJob } from '../server/plugins/generation-job-lifecycle.ts';
const submission = await createGenerationJob(
{ model: 'fal-ai/flux', prompt: 'sunset over mountains', aspect: '16:9' },
async (id, update, registerDownload, registerProviderTask) => {
update({ phase: 'generating', progress: 10 });
const images = await generateFalCatalogImage({ model: 'fal-ai/flux', prompt: 'sunset over mountains' });
update({ phase: 'downloading', progress: 80 });
for (const img of images) {
await registerDownload(img.url, 'image');
}
update({ phase: 'completed', progress: 100, status: 'completed', resultUrls: images.map(i => i.url) });
return { urls: images.map(i => i.url) };
},
{ provider: 'fal', retentionMs: 30 * 60 * 1000 },
);
console.log(`Job created: ${submission.operationId}`);
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment