onAiEditImage fills an area of the photo, or edits the photo whole. The person marks an area with the AI’s Brush, then, in Remove Object, presses Remove, to take away what is marked and fill it with what surrounds it, or, in Edit, writes what to put there and presses Generate. The editor sends the piece of the photo around the mark with its mask, and pastes what this function gives back into the mark alone. With nothing marked, a request in words that adds or changes something, as “Furnish this room with contemporary furniture”, edits the whole photo: the editor sends the photo with no mask, and lays what this function gives back over it, the room still the room, with furniture in it. With it, the AI’s conversation can fill an area and edit the photo too, through its retouch_run and retouch_remove tools, and retouch.run() and retouch.remove() run it from code. Without it, the AI can’t remove or generate.


Full and Trial only. The Free and the Lite accept
onAiEditImage and ignore it. It works alone: with it, the brush shows even without onAiChooseCommands, and Generate with nothing marked edits the whole photo by the words and the image the field holds. With onAiChooseCommands too, a Generate in words, on a mark, on the new background after Remove Background or with nothing marked, goes through the conversation, which fills the area or edits the photo with this function; a Generate with an image comes straight here, on a mark or, with nothing marked, as the whole photo’s edit.At Work for a Business
Each operation on a job a business does by hand today, with the real models: more, with the commands the AI ran, in Use Cases. The whole photo edited by words,'EDIT_PHOTO' on Seedream 5.5: an architecture studio’s facade renovated, then its materials named in Annotate; a landscape designer’s bare backyard made a garden; a café decorated for Christmas.



'GENERATE_AREA' on FLUX.1 Fill [pro]: a fashion store’s red dress brushed over, then asked in emerald silk, and in sapphire over the same areas.

'ERASE_AREA' on Bria Eraser: a pet photographer’s leash; a marketplace seller’s clutter round an armchair, then the photo made brighter and a scratch noted in Annotate.


'GENERATE_BACKGROUND' on Bria FIBO 2.0 Edit, after Remove Background, and borders, 'GENERATE_BORDERS' on Bria FIBO Edit 1.5 Expand: a car dealer’s SUV on a coastal road at sunset, its features then called out in Annotate; a cosmetics brand’s serum on water and petals, expanded to a 16:9 banner; a race car in a pit lane at night, expanded to 16:9.



'EDIT_PHOTO', Seedream 5.5), then the recycling bins brushed over and removed ('ERASE_AREA', Bria Eraser), the selling points marked and the agency’s logo.
onAiRemoveBackground), a wet street at night with neon reflections asked in words ('GENERATE_BACKGROUND', Bria FIBO 2.0 Edit), then Expand to 16:9 ('GENERATE_BORDERS', Bria FIBO Edit 1.5 Expand), and its selling points called out.
'EDIT_PHOTO', Seedream 5.5), the walls, windows and arch kept, then the day laid out on it.
Wire It
In the page,onAiEditImage posts what the person asked to your server, by its operation with that operation’s own fields, and reads the image back:
page.ts
route.ts
EmbedEditor, or as the same option of mountEditor and openEditor.
operation is onAiEditImage’s alone. onAiRemoveBackground and onAiDetectObjects do one job each, so the editor sends them none: it would only repeat the function’s name. To send every function to one route, add the operation you name yourself to their input:
one-route.ts
What the Editor Sends

onAiEditImage gets and gives back on a Generate in Edit, taken from the real editor: the area brushed over the plant, the image and mask the editor sent, the image the server gave back (FLUX.1 Fill [pro]‘s real answer), and the photo with it pasted into the mark alone.
What the person asked, each operation with its own fields alone, so your server routes the fill with one
switch, as the servers below do:'EDIT_PHOTO': the whole photo edited by words, with nothing marked: Generate in Edit with nothing marked and noonAiChooseCommands, the conversation’sretouch_runwith{ "kind": "ALL" }, andretouch.run()with{ kind: 'ALL' }, withpromptandreferenceImageand nomask.imageis the whole photo. Your image is that photo with the request done and the rest as it was: the editor lays it whole over the photo, so what the words don’t name must stay where it stood, as a model made to edit a photo by an instruction keeps it. A model that fills a mask, given the whole photo as its mask, draws another photo: on 2026-11-26, FLUX.1 Fill [pro] asked to furnish an empty room gave back another room, its walls, windows and light new. The servers below send it to Seedream 5.5 (Which Model).'ERASE_AREA': Remove in Remove Object, to take away what is marked and fill it with what surrounds it.image,maskandsignal, no words.'GENERATE_AREA': Generate on a mark, in Edit, withpromptandreferenceImage.'GENERATE_BACKGROUND': the new background’s Generate after Remove Background, withpromptandreferenceImage.imageis the whole photo, transparent where the cut took the background away, andmaskis white there and black over the subject. The editor lays your image whole under the cut photo, so the subject stays as the cut left it. A model made to put a subject on a new background follows the words better than one that fills a mark: the servers below send the words to Bria FIBO 2.0 Edit (Which Model), and an image, which onlyretouch.run()sends here, to the model that places the person’s image.'GENERATE_BORDERS': an Expand and its Generate Again, withbordersandprompt.
For Expand in the Aspect section, at 16:9: the lake and its hills carried into the new borders, with Generate Again on the card for another. Bria FIBO Edit 1.5 Expand’s real answer, played four times as fast as it came.
Expand to 16:9, before and after: the photo, then its borders filled. Bria FIBO Edit 1.5 Expand’s real answer.
'GENERATE_BORDERS': the widths in px of image of the borders the Expand adds on each side. image is then the bigger photo, its borders transparent, and mask is white on them alone. A model made to expand takes the photo and these widths, not a mask: the servers below cut the photo out of image by them and send it so.
A PNG of the piece of the photo around the mark: the mark’s box, grown by 30 % of its longer side on each side, then to at least 512 px of the photo a side (or the whole photo, when it is smaller), kept inside the photo. For
'EDIT_PHOTO', the whole photo. At most 2048 px on its longer side. It is the photo with the AI’s own edits alone, such as an earlier fill or a background taken off: no crop, rotation, adjustments, filter, frame, annotations or Obfuscate areas.For every operation but
'EDIT_PHOTO'. A PNG of the same size, opaque: white where to fill, black elsewhere, nothing in between.For every operation but
'ERASE_AREA'. What to put there: 'a small glass vase with red roses'; for 'EDIT_PHOTO', what to add or change: 'Furnish this room with contemporary furniture'. With onAiChooseCommands, a Generate in words, on a mark, on the new background after Remove Background or with nothing marked, goes through the conversation, and the model writes these words from the person’s, in English whatever their language, as the editor’s guide asks. Without onAiChooseCommands, and for a Generate with an image, they are the person’s words as typed, in their language; '' where they gave only an image. For 'GENERATE_BORDERS', the editor’s own words, in English: 'Extend the photo into the empty borders: continue the scene as it would be, matching its light and style.', for a model that reads words, or yours, your aiPrompts’ expandBorders.FLUX.1 Fill [pro] reads English best: on our plate, it ignored the words in Portuguese three times in three, where the same request in English worked, and on a sneaker’s photo a new background asked in Portuguese came back as flames and a second sneaker. It is the only model we tried in another language. Without onAiChooseCommands, the editor sends the words as the person typed them; where your people write in another language, translate them on your server before the model’s call.For a new background from words, the recommended server uses Bria FIBO 2.0 Edit: in our tests it followed requests that FLUX.1 Fill [pro] often ignored. See Which Model.For
'EDIT_PHOTO', 'GENERATE_AREA' and 'GENERATE_BACKGROUND'. The image the person added with the field’s Add Image button, to say what to put there: a PNG of at most 2048 px on its longer side, its transparency kept, so a logo stays cut out. null without one. With an image, the request is a Generate, even with no words: the recommended server places it with FLUX 3 Image, which fills the mark’s box edge to edge, so the mark sets the size of what it places. For 'EDIT_PHOTO', with nothing marked, it goes to Seedream 5.5 as its second image, with the server’s own words where the person gave none. Where the person marked only a dot, a tap of the brush, the AI’s card says to mark the whole area.Aborts when the request stops: the Stop on the AI’s card, Escape,
ask.stop() or ask.end(), the editor closing or leaving the page, or another image opening. Pass it on to your fetch and to your model’s call.What It Gives Back
The image you made: oneBlob a call. Each fill is one call: a model that fills an area of its own inside a request about another makes a second one. For another image, the person presses Generate again, or your code calls retouch.more(), and each image is one undo step. The images are held within 128 MB, one rule for every device, counted two ways. The images the photo on screen shows count as decoded pixels: past 128 MB, they are drawn into the photo as the next image lands, the picture identical to the last pixel, and each stays one undo step back; retouch.more() rejects NOT_FOUND for an image drawn into the photo. The images only Undo or Redo reach are held compressed, losslessly, and count compressed: past 128 MB of them, the history’s oldest steps leave it, as past its 100 steps, but never one whose going leaves it past the limit anyway, nor a step of a request under way. On a 12 MP photo, Undo reaches back ten Generates pressed again over half of it and every step of an Expand to 9:16 asked again twice. An Undo or a Redo onto a step whose images are compressed keeps the photo before it on screen until they are ready; meanwhile the editor waits as during a Save, Undo and Redo still take every press, and past 100 ms the one pressed shows a spinner. history.undo() and history.redo() resolve once the photo is on screen, and Save, Copy and Share pressed meanwhile wait for it.
It can be any size: the editor resizes it to image, then pastes it into the mark alone. Its edge is feathered inside the mark and its color matched to a ring just outside it, so the faint seam every model leaves at the mask’s edge doesn’t show, and a model that redraws the whole piece serves as well as one that keeps it. Outside the mark, the photo stays as it was, every pixel. For 'EDIT_PHOTO', the editor resizes it to the photo and lays it whole over the photo: what stays as it was is what your model kept, so give back an image of the photo’s ratio.
It lands on the photo as one undo step. A Remove of the person’s mark takes it with it. After a Generate, the areas the person marked stay, hidden, until they mark again, and shown while Remove Object is chosen. An area your code or the model names (the whole photo, the background, an outline) shows while its work runs, the whole photo with only its edge glowing, and is written into no area of the person’s: they stay as they were, whether the work lands, fails or is stopped. Generate pressed again over the same areas, and retouch.more(), call your function again with the same piece, mask and image, with new words or the same, and its image takes the place of the one before, as one undo step more: Undo brings the one before back. With nothing marked, Generate pressed again with the same words asks for another version of the last whole-photo edit the same way, its 'EDIT_PHOTO' again; other words make a new edit over the photo as it is. The filled pixels are part of the photo, never of the state a host keeps: Save’s src holds them.
When your function rejects, the editor shows Couldn’t Generate the Image (Couldn’t Remove the Object for a Remove, with no words and no image; Couldn’t Expand the Photo for an Expand), with your error’s message under it, and the person’s areas stay, so they can try again. A rejection that is no Error, an Error with no message, or one of the editor’s own (EmbedEditorError) shows Try again. under the title, and so does an answer that is no image: anything but a Blob, a list of them too, or a Blob the browser can’t read as an image. An image that changed nothing fails too. To tell, the editor compares the image with the piece it was sent, byte by byte, and takes the average difference inside the mark and outside it: when the inside’s is less than 1 in 255 above the outside’s, the mark changed no more than the rest. That catches a model that gives the piece back, and one that redraws the whole piece without putting anything new in the mark. Where the mark covers the whole piece, as for 'EDIT_PHOTO', the inside’s average alone must be under 1 in 255. When the image changed nothing, the editor shows Nothing Changed, with The marked area stayed the same. Try again. under it, and the person’s areas stay. retouch.run() and retouch.remove() reject with AI_FAILED in each case, your error as its cause when it rejected.
Inside a request, the failure goes to the model, not to the person: with onAiChooseCommands, a Generate in words is a request, on a mark or on the new background after Remove Background, and so is any fill the model asks for with its retouch_run, retouch_remove or crop_expand tool. The model gets the failure as its tool’s answer, with nothing on the card nor your error’s message, and the request goes on: the model can try again, or tell the person in its reply.
Which Model
We tested the models with the editor’s requests and evaluated each image after the editor’s paste. The recommended server uses the models below. Results are from small samples and prices are from November 2026; try them on your own photos and requests.- Remove an object — Bria Eraser on fal. Good results on the first three photos; it made unwanted objects on two harder removals. About US$ 0.04 a call. Object Eraser on Runware costs less but takes longer and runs at 1024 px.
- Generate from words in a mark — FLUX.1 Fill [pro] on Runware. Good results on the tested photos, with a mask, at US$ 0.05 a call. Ideogram V3 Edit Turbo on fal costs less; no ready server below runs it.
- Put a reference image in a mark — Seedream 5.5 on Runware. Given the mask as an image, it placed the product at the scene’s scale in 5 of 6 attempts. About US$ 0.044 a call. GPT Image 2.6 on fal takes the mask directly.
- Put a reference image on a new background — FLUX 3 Image on Runware. Fills the background’s box with the reference image. About US$ 0.048 a call. GPT Image 2.6 on fal is an alternative.
- Expand — Bria FIBO Edit 1.5 Expand on fal. No broken borders in ten Expands, at the canvas’s size. US$ 0.04 a call. FLUX Outpainting on Runware is an alternative, with a canvas size limit.
- Generate a new background from words — Bria FIBO 2.0 Edit on Runware. Followed both requests on all four test photos, keeping the subject in place. US$ 0.04 a call. The same model on fal was as good and faster in this test.
- Edit the whole photo — Seedream 5.5 on Runware. Kept the parts the request did not name well in our tests. About US$ 0.043 a call. Seedream 5.5 and Nano Banana 2.1 on fal are alternatives.
What the Editor Sends, What Your Server Adapts
The editor sends every model the same request, and your server turns it into what its model takes. What the editor sends, whichever model your server calls, as What the Editor Sends gives it field by field:operation, what the person asked:'EDIT_PHOTO','ERASE_AREA','GENERATE_AREA','GENERATE_BACKGROUND'or'GENERATE_BORDERS', each with its own fields alone.image, the piece of the photo around the mark, as a PNG: the mark’s box grown by 30 % of its longer side on each side, to at least 512 px a side where the photo is that large, then shrunk, where it is longer, to 2048 px on its longer side. For'GENERATE_BACKGROUND', the whole photo, transparent where the cut took the background away; for'EDIT_PHOTO', the whole photo.mask, a PNG of the piece’s size, white where to fill and black elsewhere; none for'EDIT_PHOTO'.prompt, for every operation but'ERASE_AREA': the person’s words,''where they gave only an image. For'GENERATE_BORDERS', the editor’s own words instead, asking to continue the scene into the borders; the servers below send them to neither of the models made to expand, which take no words.referenceImage, for'EDIT_PHOTO','GENERATE_AREA'and'GENERATE_BACKGROUND': the person’s image, a PNG of at most 2048 px on its longer side, ornull.borders, for'GENERATE_BORDERS':imageis then the photo on its bigger canvas, at most 2048 px on its longer side, its new borders transparent,maskis white on them, andbordersholds their four widths in that canvas’s px.
The image library. That adapting runs on photon (
@cf-wasm/photon), an image library compiled to WebAssembly, through one module, images.ts. We don’t use sharp, the usual image library on Node, because it runs native code, which Cloudflare Workers, Vercel’s Edge runtime and Deno Deploy don’t run: a server on sharp runs on Node alone. Photon runs on Node and on Cloudflare Workers with the same code, and its package also has builds for Next.js’s Edge runtime and other hosts; the editor’s playground runs these servers on Node and on a Worker. Install it beside your server:
Servers
Each server is one module for your server, with the sameeditImage function: it takes the editor’s input as it comes, and routes it with one switch on its operation, and resolves to { cost, image, model }, the image it made, what the call cost in US$, and the model that made it. Each call has two minutes, past which it rejects with “The model took too long.” The person’s Stop ends the wait at once; on fal, it also asks fal to cancel the job, which fal’s own client doesn’t do, and fal drops a job it hasn’t started yet but finishes, and bills, one it has (the fal job).
Bria Eraser, FLUX.1 Fill [pro], FLUX 3 Image, Bria FIBO Expand, Bria FIBO 2.0 Edit and Seedream 5.5
The recommended server:'ERASE_AREA' on fal’s Bria Eraser, 'EDIT_PHOTO' on Runware’s Seedream 5.5, 'GENERATE_AREA'’s words on Runware’s FLUX.1 Fill [pro] and its image on Runware’s Seedream 5.5, 'GENERATE_BACKGROUND'’s words on Runware’s Bria FIBO 2.0 Edit and its image placed by Runware’s FLUX 3 Image, and 'GENERATE_BORDERS' on fal’s Bria FIBO Edit 1.5 Expand. It runs retouch-fal.ts’s erase and outpaint and retouch-runware.ts’s editPhoto, generate, place, placeObject and replaceBackground, so put those two beside it, with bordered.ts, images.ts, fal-job.ts, runware-task.ts and limit.ts. It needs @fal-ai/client, @cf-wasm/photon, FAL_KEY and RUNWARE_API_KEY. The editor’s playground runs this file, word for word, on Node and on Cloudflare Workers.
retouch.ts
fal: Bria Eraser, FLUX.1 Fill [pro], GPT Image 2.6, Bria FIBO Expand, Bria FIBO 2.0 Edit and Seedream 5.5
Everything on fal, with one key. The images go up to fal’s storage once each, andfal-job.ts runs each job. fal bills FLUX.1 Fill [pro] in proportion to the megapixels it gives back, so a larger piece costs more: US$ 0.05 for a megapixel, US$ 0.21 for 2048 × 2048 px. GPT Image 2.6 takes the mask and the person’s image together and answers at about the size it is sent, so the server shrinks the piece and its mask to a 1024 px longer side with images.ts (the mask by its nearest px, so it stays black and white). fal bills it by its tokens and reports none, so its cost is our estimate, about US$ 0.07. Bria FIBO Edit 1.5 Expand takes the photo, the size of the bigger photo and the photo’s place in it, so the server cuts the photo out with bordered.ts, and answers at the bigger photo’s size, at US$ 0.04 a call. Bria FIBO 2.0 Edit takes the new background’s piece and mask as they come, with the words after “Replace the background with:”, and answers at the piece’s size, at US$ 0.04 a call. Seedream 5.5 takes the whole photo, and the person’s image after it, with no mask, at auto_2K, and answers larger than the photo at its ratio; fal bills US$ 0.0405 for the image and US$ 0.012 for each image it is sent, US$ 0.0525 with the photo alone. It needs @fal-ai/client, bordered.ts, images.ts and FAL_KEY.
retouch-fal.ts
Runware: Object Eraser, FLUX.1 Fill [pro], FLUX 3 Image, FLUX Outpainting, Bria FIBO 2.0 Edit and Seedream 5.5
Everything on Runware, with one key, and the cheapest Remove. Each call is one task POSTed to Runware’s API byrunware-task.ts, with the images as data URLs; the cost is what Runware billed. Object Eraser fails on any image whose longer side isn’t 512 or 1024 px, so the server stretches the piece and its mask to a 1024 px longer side with images.ts (the mask by its nearest px, so it stays black and white), and the image back to the piece’s size. The person’s image in a mark goes to Seedream 5.5, with the piece and the mask as its other references, at 1K, at a billed US$ 0.044; as the new background, to FLUX 3 Image, which takes boxes, not a mask: the server gives it the box around the mark, with the person’s image as its second reference, and the editor’s paste keeps what lands in the mark. Runware holds each call for up to two minutes, then answers a task still running with no image; the server rejects that in the time limit’s words. A mask that marks nothing rejects before any call. FLUX Outpainting takes the photo and the borders’ widths, so the server cuts the photo out with bordered.ts; in its fast mode it refuses a canvas past 4 megapixels once padded to 16 px, which a 2048 × 2048 one is, so the server shrinks such a canvas to a 2000 px longer side, the borders with it, and stretches the image back. Bria FIBO 2.0 Edit takes the new background’s piece and mask as they come, with the words after “Replace the background with:”, and answers at the piece’s size. Seedream 5.5 takes the whole photo as its first reference, the person’s image as its second, with no mask, at 2K, and answers larger than the photo at its ratio, at a billed US$ 0.043 with the photo alone. It needs bordered.ts, images.ts, runware-task.ts, limit.ts and RUNWARE_API_KEY, and no SDK: a plain fetch.
retouch-runware.ts
The bigger photo
For an Expand ('GENERATE_BORDERS'), the editor sends the bigger photo, its borders transparent, with the widths of its borders (borders). The models made to expand take the photo and where it stands, so the fal and Runware servers cut the photo back out of the bigger one with this module, every px made opaque. It needs images.ts beside it.
bordered.ts
The image library
Every image the servers touch goes through this module, on photon: a size read from the file’s header, a crop, the alpha dropped, the px read and a resize (Lanczos3 for a photo, the nearest px for a mask). It opens PNGs, the format of every image the servers open: the editor sends PNGs, and the servers ask the models for PNGs. It reads a size from a PNG’s header or a JPEG’s, as the face find reads the editor’s photo, a JPEG. Each function frees every image it opens or makes, on an error too: an image lives in WebAssembly’s memory, where photon frees its room on its own only once the garbage collector collects the image, late or never, and that memory, once grown, never shrinks, so a server that left its images to the collector could grow until its host stopped it. Photon writes every PNG with an alpha channel, so an image with no transparency comes back opaque. It needs@cf-wasm/photon.
Photon can’t recover from a failure. On a file it can’t decode, or an image too large for its memory, it panics, and a panic in WebAssembly leaves photon’s memory and its stack as they were: each one keeps what its call had taken, and after a few thousand (5,000 broken files did it on Node), photon fails every image, good ones too, until the server process restarts. So the module checks what it hands photon, and throws in words before photon sees it:
- A file that isn’t a whole PNG throws “The image couldn’t be read.”: anything but a PNG, a PNG cut short or without its end (its IEND), one that doesn’t start with its 13-byte header (its IHDR), or one whose bytes changed on the way, so a chunk’s checksum is wrong. That covers a model’s broken answer and an error page sent in place of an image.
- An image with less than 1 px on a side, read from its header or asked for, throws “The image needs at least 1 px on each side.”; so does a size whose side the request left out. One with more px than 8192 × 8192 throws “The image has more than 67,108,864 px.”: resizing an image that large took photon’s memory to 2.1 GB, and that memory holds 4 GB at most.
- A crop that leaves its image, or lacks a side, throws “The rectangle leaves the image.”
images.ts
The fal job
fal’s client, when its signal aborts, stops waiting for the job but doesn’t cancel it. The fal servers run each job through this module: when the person stops or the time limit passes, it stops waiting and asks fal to cancel the job by its id, even where the stop comes before fal gives the id, and it fetches the job’s image within the same limit. A cancel drops a job fal hasn’t started yet. A job fal has already started runs to its end and is billed: fal answers the cancel and finishes the job anyway (on 2026-11-10, a Bria cut canceled 6 seconds in completed at 25.5 seconds). It needs@fal-ai/client and limit.ts beside it.
fal-job.ts
The Runware task
The Runware server POSTs each task through this module, which asks Runware for the cost of each and turns Runware’s refusals into errors: its own words where it refuses the task, its status where the answer is no JSON, and the time limit’s words where Runware ends the call while the task still runs. It needslimit.ts beside it and RUNWARE_API_KEY.
runware-task.ts
The time limit
Every call to a model gets two minutes, its image’s fetch included: over twice the slowest call we measured of a model our servers run, Object Eraser’s 50 seconds on Runware, which fills a Remove. This module gives the call a signal that aborts when the person stops or the two minutes pass, whichever comes first, and past them rejects with “The model took too long.”, the words the editor shows. The fal job and the Runware server both run their calls through it.limit.ts