ai-powered-conversational-intelligence

Image Editing Architecture

⚠️ Partially superseded — read the note below before relying on this document. This document’s core claim when written was “attachments are never actually sent anywhere” (see “Known limitations” below). That is no longer true: a subsequent feature pass built a real backend attachment pipeline (attachments/) — an attached image now genuinely reaches a configured vision model or OCR, and explicit POST /attachments/{id}/extract-text|resize|remove-text actions exist for real, deterministic image operations. See CLAUDE.md’s “Chat attachments, image actions, and their security hardening” section and docs/API.md’s “Attachments” section for the current, accurate state. What remains genuinely accurate below: the local Konva-based editor’s own feature set (crop/rotate/draw/annotate) and its library-choice rationale.

Update, 2026-09-27 — “AI-guided editing” is now real, not a stub. Everything under “What does NOT exist: AI-guided (generative, prompt-driven) editing” below described a deliberate, permanent stub — that section is now historical: a real backend (POST /attachments/{id}/ai-edit, attachments/ai_edit.py + media_gen/image_edit_provider.py, IMA Studio’s image_to_image task category) exists, and lib/imageEditAdapter.ts’s AiGuidedEditAdapter calls it for real instead of always rejecting. See “AI-guided editing: the real implementation (2026-09-27)” below for the current design, mask convention, and residual limitations — read that section instead of the “What does NOT exist” one below, which is kept only as a historical record of what this feature used to be.

Status: implemented and tested end-to-end (local editing, real OCR/vision/resize/text-removal/blur, and now real AI-guided generative editing whenever ENABLE_IMAGE_EDITING=true and a provider credential is configured — see “AI-guided editing: the real implementation” below). When the server-side feature flag or credential is missing, the UI disables the AI-edit controls and says so honestly; local editing, OCR, resize, and deterministic blur all keep working regardless.

What exists today

AI-guided editing: the real implementation (2026-09-27)

Root cause of the old “backend endpoint hasn’t been built” message, confirmed by tracing the click: ImageEditor.tsx’s Generate button called AiGuidedEditAdapter.editWithInstruction() (lib/imageEditAdapter.ts), which — as documented in the “What does NOT exist” section below — was a deliberate, permanent stub that always threw AiEditNotConfiguredError. No backend route existed to call at all.

What was built to close that gap:

Verified with a fake provider (FakeImageEditProvider, media_gen/image_edit_provider.py) for every automated test — 2123 backend tests pass (ruff/black/mypy clean), 255 frontend tests pass (13 new, including pixel-level mask-overlay assertions and mocked-provider tests that assert real image/mask bytes were passed, not filenames), tsc/oxlint/npm run build all clean.

A real, live IMA Studio image_to_image call was also attempted end-to-end (a synthetic test image + a real mask, remove_object) — this is genuinely informative but not a full success: the upload leg completed against IMA’s real infrastructure, then POST /open/v1/tasks/create failed with IMA’s own business error {"code": 4008, "message": "Insufficient points"} — the configured account has no generation credits, a real external/account-balance condition, not a bug in this app. No credits were spent (the failure happened before any model inference ran). This confirms the request-construction, upload, and typed-error-propagation paths are correct against IMA’s real, live infrastructure — it does not confirm a successful end-to-end generation, since that requires an account with a positive credit balance. Re-run python against the same ImaImageEditProvider.edit() call once credits are topped up to get that final confirmation; nothing in the code needs to change to do so.

Known, disclosed limitations of this implementation:

What does NOT exist: AI-guided (generative, prompt-driven) editing — historical, see the section above

This section is now historical — kept only as a record of what this feature used to be before the 2026-09-27 pass above. Every claim below (“no backend endpoint accepts an image plus an edit instruction,” the AiGuidedEditAdapter stub that always rejects, the POST /media/edit contract sketch) has been superseded by the real implementation described above; do not act on anything in this section.

Update, later pass: the claim immediately below (“no backend endpoint accepts an image”) is no longer accurate in general — api/attachments.py now has real UploadFile routes (POST /attachments/upload, plus POST /attachments/{id}/resize|remove-text), and AskRequest does now carry attachment_ids. What remains true, specifically, is narrower: no endpoint accepts an image plus a free-text natural-language edit instruction and returns a generatively-edited result — POST /attachments/{id}/remove-text is real pixel editing, but it takes explicit region coordinates, not a prompt, and uses classical (non-generative) inpainting. See CLAUDE.md’s “Chat attachments, image actions, and their security hardening” section for what’s real today; the rest of this section (the AiGuidedEditAdapter stub, its own honest in-UI error message) remains accurate for that specific, narrower capability.

No backend endpoint accepts an image plus a natural-language edit instruction for AI-guided (generative) editing. media_gen’s existing image generation takes a text prompt and produces a brand-new image — it does not accept an uploaded image plus an edit instruction.

The editor’s “AI-guided editing” section (preset buttons like “Remove the selected object,” a free-text prompt field, mask painting) is real UI — it is not hidden or absent — but clicking “Generate” always fails with a clear, honest message:

“AI-guided editing isn’t available in this deployment yet — it needs a backend endpoint that hasn’t been built. Your local edits above are unaffected.”

This is implemented via lib/imageEditAdapter.ts’s AiGuidedEditAdapter, which always rejects with AiEditNotConfiguredError — a deliberate, permanent stub, not a bug or an unfinished TODO that happens to reject today. The seam (ImageEditAdapter interface) exists specifically so a real implementation can be swapped in later without touching the editor’s UI code at all.

The backend contract a real implementation would need (historical sketch — superseded by the actual, real contract above)

Not built in this pass — documented here so a future session (or you) doesn’t have to reverse-engineer it from the frontend:

POST /media/edit
{
  "image": "<base64 or multipart image>",
  "mask": "<base64 or multipart image, optional>",
  "instruction": "Remove the selected object"
}
→ { "media_id": "<opaque id>" }   // mirrors media_gen's own pattern

Should mirror media_gen’s existing, already-reviewed design (see CLAUDE.md’s “Media generation” section) rather than invent a new pattern:

What the actual, shipped implementation did differently from this sketch: no separate human-in-the-loop approval step was added (unlike media_gen’s own text-to-image/video generation) — an image edit’s own Generate button click, plus the existing per-caller/session rate limit and idempotency cache, was judged sufficient friction for this feature’s scope (there is no POST /attachments/{id}/ai-edit/confirm two-step flow). The result is served back as a data URL in the same response plus stored as a real attachment (register_derived_image), rather than a separate GET /media/{id} fetch — reusing the attachment system’s own existing storage/access-control model rather than media_gen.cache’s in-memory, FIFO-evicted one, since a derived image here is meant to be usable in a follow-up question the same way any other attachment is.

Canvas library choice: Konva (konva + react-konva)

No canvas/drawing library existed in package.json before this feature. Three options were considered (per the request’s own instruction to “select only one approach and justify it”):

Option Verdict
Raw Canvas API Rejected — hand-rolling hit-testing, layering, undo/redo, and resize handles from scratch is a large, error-prone amount of code for a chat-app image editor, not proportionate to the feature’s actual scope.
Konva + react-konva Chosen. React-idiomatic (declarative <Stage>/<Layer>/<Rect> components instead of imperative canvas calls), includes a built-in Transformer for resize/rotate handles “for free,” and is a moderate dependency (not a full app framework).
Fabric.js Rejected — a comparable feature set to Konva but a heavier, less React-idiomatic API (imperative object model, no first-class React bindings as actively maintained as react-konva).
tldraw Rejected — a full, opinionated whiteboard application; using it to edit one photo means fighting its own UI chrome and data model far more than building on top of it.

Real cost, disclosed honestly: Konva added the entire editor as its own ~343KB (106KB gzip) chunk. Lazy-loading (see above) means this cost is paid only by a user who actually opens the editor, not by every page load.

Non-destructive editing state

Known limitations (disclosed, not hidden)

Security