Programmatic editing
Programmatic editing is the application-level API for automated clients that need to create or change FreeCut projects without using the editor UI. It validates an edit request, applies the same timeline rules as interactive editing, and can return the new project or persist it to a workspace.
Editing system
The editing system turns a project plus an ordered list of edit operations into a new project. It is exposed inside the browser harness as window.freecut.editProject and is used by the command-line and HTTP entry points. See Headless automation for the wider automation model and Headless server and rendering for the service that hosts these calls.
Editing lifecycle
An automated client can run an edit as a dry operation or as a workspace change. The service reads the current project, runs the edit in one browser session, and only saves the result when the request asks for persistence.
flowchart LR
A[Project source] --> B[Request validation]
B --> C[Timeline edit layer]
M[Media metadata] --> C
C --> D[Edited project]
D --> E{Persist?}
E -->|No| F[Return project and results]
E -->|Yes| G[Check revision]
G --> H[Normalize and save]
H --> I[Return new revision]
The low-level API accepts a project object. The workspace-facing API can instead identify a project by ID. A persisted request also carries a revision check so an automated client does not silently overwrite a newer project.
References: src/…/edit.ts• L143-169 src/…/edit-op-support.ts• L42-49 headless/serve.mjs• L384-442
Client interfaces
The same edit capability is available through three useful interfaces. They share the operation contract, but they differ in who owns project loading and saving.
| Interface | Project input | Persistence behavior |
|---|---|---|
| Browser harness | A Project object passed to window.freecut.editProject | Returns an edited project; the caller decides what to do with it |
| CLI | A workspace project ID or a project JSON path | Dry run by default; --out writes a new file and --in-place replaces the source file |
| Lifecycle HTTP API | A workspace project ID at POST /v1/projects/:id/edit | persist: false returns a preview; persist: true saves with revision control |
The older POST /edit route also accepts a project ID or inline project object. New clients should use the versioned lifecycle route because it includes workspace resources, revisions, and persistence behavior.
References: src/…/main.ts• L1193-1203 headless/edit.mjs• L1-19 headless/serve.mjs• L11-21 headless/serve.mjs• L384-442 headless/…/contract.mjs• L679-703
Edit operation contract
An edit operation is a JSON object with an op name and operation-specific fields. The client sends a non-empty ordered list, and the edit layer applies each operation in order against the state produced by the prior operation. The browser edit layer dispatches each name to a focused operation handler while still driving the real timeline actions.
Operation groups
The operation set covers the main timeline actions needed by automated clients. Operations that create IDs return those IDs in their result so later operations can use them.
| Group | Operations | Purpose |
|---|---|---|
| Content | addText, addItem, updateItem, removeItems | Create, change, or remove timeline content |
| Placement | moveItem, split, trimStart, trimEnd | Change time ranges and track placement |
| Timeline structure | addTrack | Create video or audio tracks |
| Media | addClip | Create timeline content from seeded media metadata |
| Transitions | addTransition, updateTransition, removeTransition | Control changes between adjacent clips |
| Animation | addKeyframe, removeKeyframes, setTransform | Change animated properties and transforms |
| Transform hierarchy | setTransformParent | Attach, change, or detach an item's transform parent |
| Effects | addEffect, removeEffect | Add or remove registered GPU effects |
addClip uses media metadata to choose the timeline item type and its default duration. A video with an audio track becomes a linked video and audio pair, which keeps the imported clip's sound available to the timeline.
References: src/…/edit-op-support.ts• L18-40 src/…/edit-ops.ts• L60-335 src/…/edit-ops.ts• L440-478 headless/…/contract.mjs• L168-347 headless/edit-operations.mjs• L451-504
Operation results
Each successful operation produces a detail object. A caller can assign a unique callerId and refer to a prior result with a $ref in an ID-valued field. This lets a client use generated item, track, transition, effect, and media IDs without predicting them.
For example, an automation sequence can add a track and then place text on the returned track:
[
{ "callerId": "track", "op": "addTrack", "kind": "video" },
{
"op": "addText",
"text": "Title",
"trackId": { "$ref": "track#/detail/trackId" }
}
]
References must point to a prior successful operation and must resolve to a string ID. Duplicate caller IDs, forward references, references in ordinary data fields, and references to non-ID results are rejected. A failed operation stops the sequence; later operations do not run. Operations that affect linked media items can override linked selection for that operation, and the prior selection state is restored after success or failure.
References: src/…/edit.ts• L41-114 src/…/edit.ts• L117-141 src/…/edit.ts• L155-169 headless/…/contract.mjs• L450-539 headless/edit-operations.mjs• L507-546
Request validation
Request validation protects the boundary between an external client and the application edit model. The wire contract checks the operation name, required fields, numeric ranges, allowed enum values, reference shape, and the rule that an edit request has exactly one project source.
Schema checks
The versioned lifecycle API allows up to 1,000 operations in one request. Persisted edits must include expectedRevision unless the client explicitly sets force: true. The API exposes the edit schema and operation names through /v1/capabilities, so clients can discover the contract instead of copying it.
Validation errors return structured field information. They stop before the browser edit layer runs, which keeps malformed requests separate from valid edits that fail because of project state.
References: headless/…/contract.mjs• L351-362 headless/…/contract.mjs• L450-539 headless/…/contract.mjs• L658-677 headless/serve.mjs• L384-399
Project checks
The browser edit layer migrates the incoming project before it applies operations. It then rebuilds the project from the edited timeline state. This keeps older project data aligned with the current project shape before an automated client changes it.
The edit layer also checks state-dependent rules that a static schema cannot check. It requires referenced items, tracks, transitions, parents, and effects to exist. It requires metadata for addClip, and it detects when a transition update was rejected instead of reporting the old transition as successfully changed.
References: src/…/edit.ts• L143-169 src/…/edit-op-support.ts• L55-100 src/…/edit-op-support.ts• L76-93 src/…/edit-ops.ts• L167-197
Media metadata
Media seeding supplies the edit layer with metadata for media that an automated client references. It places metadata in the application media store before editing, so the edit layer can make the same type, duration, frame-rate, size, and audio decisions that an imported media item needs.
Metadata flow
The workspace and service collect metadata for media used by addClip. The browser harness receives that metadata as a small in-memory list. It does not need to download media bytes to perform an edit.
sequenceDiagram
participant C as Client
participant W as Workspace
participant H as Browser harness
participant E as Edit layer
C->>W: Request addClip with mediaId
W->>W: Read media metadata
W->>H: Project, operations, metadata
H->>E: Seed media store
E->>E: Build clip from metadata
E-->>C: Edited project and operation result
If the metadata is missing, addClip fails before it creates a timeline item. For a video, the metadata also controls whether the edit creates a linked audio companion. Media bytes are needed later for rendering, not for the project-only edit step.
References: src/…/seed-media.ts• L4-21 src/…/edit.ts• L143-152 src/…/edit-ops.ts• L318-335 headless/…/workspace.mjs• L121-143 headless/serve.mjs• L262-277
Media preparation
The media probe path is separate from editing. It reads a workspace source file in the browser, checks that the content is valid media, and returns metadata that can be saved for later edits. This gives automated clients a way to repair or create metadata before they use addClip.
References: src/…/main.ts• L1248-1273 headless/serve.mjs• L444-480 headless/…/contract.mjs• L541-551
Persistence boundary
Persistence turns an in-memory edit result into a workspace project. The service first reads the current revision, applies the edit, normalizes the result, and saves it with an atomic write. A revision mismatch returns a conflict instead of replacing another client's work.
Revision control
Revision control applies only when a client persists an edit. A preview returns the edited project with persisted: false and the base revision. A persisted response returns the saved project and its new revision. force: true is the explicit override for clients that accept the risk of replacing intervening changes.
Persisted requests also support an idempotency key. Repeating the same request can return the earlier result instead of applying the edit twice. This matters for clients that retry after a network failure.
References: headless/…/lifecycle-store.mjs• L153-166 headless/…/lifecycle-store.mjs• L189-213 headless/serve.mjs• L401-441 headless/…/idempotency.mjs• L1-120
Write ownership
Workspace writes use one writer lock, while browser operations use one serialized queue. The lock prevents multiple headless writers from changing the same workspace at once. The queue prevents concurrent page operations from contending for the shared browser session and gives failed operations a recovery path before the next one starts.
References: headless/serve.mjs• L154-221 headless/…/lifecycle-store.mjs• L228-306 headless/…/operation-queue.mjs• L25-111
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the operation names and field rules aligned between headless/…/contract.mjs, src/…/edit-op-support.ts, and the handler dispatch table in src/…/edit-ops.ts.
- Preserve
$refrules when adding an operation that creates or consumes an ID. The wire validator and browser resolver must accept the same reference fields. - Keep the public operation contract test in headless/edit-operations.mjs in parity with the schema and handler set. Its operation-name check can catch a new operation that is missing from one layer.
- When
addClipneeds new media facts, update the metadata collection path and the media-store seeding path together. - Keep persisted edit responses aligned with revision checks and idempotency handling in the lifecycle store.
- Keep the CLI, legacy HTTP route, and versioned lifecycle route on the same edit operation contract.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The request contains an unknown operation or invalid field | The API returns a validation error and does not start editing | Use /v1/capabilities and validate against the exposed edit schema |
| An operation references a missing item, track, transition, parent, or effect | The sequence stops at that operation and no later operation runs | Resolve IDs from earlier results and check project state before sending the batch |
addClip has no workspace metadata for its mediaId | No clip is created | Probe or import the media first and pass the collected metadata |
| A transition update exceeds the clip handles | The transition stays unchanged and the edit fails instead of reporting success | Use clip ranges that provide enough handles and inspect the operation result |
| A persisted edit has a stale or missing revision | The workspace is not overwritten; the service returns a revision error | Send the latest expectedRevision, or use force only when replacement is intended |
| The operation queue is full or an edit exceeds its deadline | The request returns a queue or timeout error and the browser session is recovered when possible | Bound client concurrency and retry only after checking the result and current revision |
The client runs a preview without persist: true | The response contains an edited project but the workspace does not change | Treat preview output as a candidate and send a separate persisted request |
Key files
| File | Why to read it |
|---|---|
| src/…/edit.ts | Defines project preparation, ordered operation execution, result references, and project reconstruction. |
| src/…/edit-ops.ts | Defines the application-level operation handlers and the supported timeline actions. |
| src/…/edit-op-support.ts | Defines the shared operation names, ID checks, track rules, and edit canvas used by handlers. |
| headless/…/contract.mjs | Defines the wire contract, operation names, reference rules, limits, and capability schemas. |
| src/…/seed-media.ts | Shows how media metadata enters the application state for programmatic edits. |
| headless/serve.mjs | Connects HTTP requests to the browser edit layer and to workspace persistence. |
| headless/edit.mjs | Shows the CLI dry-run and file-output model for automated project edits. |
| headless/edit-operations.mjs | Checks parity between the public operation contract and browser behavior across success and failure paths. |
| headless/…/lifecycle-store.mjs | Defines revisions, atomic project saves, writer ownership, and workspace resource boundaries. |
| headless/…/operation-queue.mjs | Defines serialized browser work, queue limits, deadlines, and recovery behavior. |