Skip to content

feat(chat): make images in Chat, pick one from the set and keep it in Files (W4-08, 00476, 00477) - #2103

Merged
gkhngyk merged 15 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-image-generation
Nov 5, 2026
Merged

gkhngyk merged 15 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-image-generation

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

W4-08: Chat makes images now instead of refusing (CHAT-15 §E, CHAT-20 §9 row 70). Ask in your own words, for example "Create a cover image for our GEO post".

The flow

  1. Model question: before the first image of a chat, Chat asks Which image model should I use? on the existing question card.
    • The options come from the price data (list_image_models, a read), the cheapest first and Recommended.
    • Each option shows its multiplier against the cheapest (1x / 2x / 4x / 1.2x; whole from 2x, as CHAT-16 item 11 writes them) and about how much of the monthly AI limit one image uses. No dollar amounts.
    • On the workspace's own OpenRouter key it shows Your key instead.
    • The pick holds for every image in the chat, unless the person asks for another model.
  2. Partial fit: when the AI limit left covers only some of the four images, the existing parts check asks "Your remaining AI limit covers about 1 of the 4 images." with Create 1 image now (Recommended), Get more AI credits and Cancel. No image is made before the answer. None covered: the run pauses at the AI limit, as for text.
    • In the preview the model sometimes went straight to the images. So generate_image's own check counts what the limit covers before the card. When the limit covers only some, it sends the model to the parts check first, so the person is always asked.
  3. Cost card: generate_image is a WRITE with the cost card in every mode, Full auto too. The card says "4 images · Nano Banana 2 Lite · about 3.1% of your monthly AI limit". Admins and Managers only (as build_report).
  4. Making the images: they run on OpenRouter, held and charged through feat(chat): meter image requests on the AI budget at the provider's real cost (W4-07) #2095's metering at what OpenRouter billed (no markup).
  5. Filing: the set is filed in Files as one file. Its object is the selected image; every image sits under the file's folder and counts toward storage. When Files is full, it says so before the card.
  6. Result card: the output card shows every image, the first one selected.
    • The person picks another in their own chat; Files then holds that one.
    • PNG downloads the selected image. It is converted in the browser (canvas), so no image library runs on the server.
    • A teammate of a shared chat sees the set but can't pick.
  7. No image made: when the model makes none, Chat says why in one line and offers a reworded prompt.

Whose key: the workspace's own OpenRouter key when Chat runs on it (own_key, the limit doesn't move). Otherwise Ansvisor's OPENROUTER_API_KEY. An own key of another provider can't make these images, so images run on the AI limit (CHAT-16 item 25). An own OpenRouter key that can't be read stops, as for Chat; Ansvisor's key never takes its place.

Engine (engine.ts): one thin change, in two places:

  • coveredUnits: the parts check counts images at their image model's price (plan_parts takes an optional image_model).
  • deliver(): a deliverable that filed itself (fileId) is named on its card, not filed again.

Also changed

  • Files: fileChatImages files a set (one row, every image counted). New routes: GET /api/chat/runs/{id}/files/{n}/images (count, selected, whether the person may pick), POST the same path (pick, own chat only, read-only refused), and GET …/images/{image} (one image, inline, private).
  • Downloads: Files downloads an image in its own format (jpg, png, webp).
  • Activity log: generate_image is recorded with no Undo (§9 row 70: the set is deleted in Files).
  • Migrations:
    • 00476: chat-files takes image/jpeg. Google's image models answer in JPEG, and the bucket had only PNG and WebP (00411). Run again, it changes nothing.
    • 00477: chat_files.formats takes 'jpg': the same list, with it. Run again, it ends the same.
    • Both are applied to the dev DB.
  • Environment: web/.env.example documents OPENROUTER_API_KEY for the web app.
  • Help: new article help/chat/images.md (approved: false).

Not in this PR (W4-08b):

Stacked on #2095. If #2032 (W4-03) merges first, web/.env.example may conflict on the OPENROUTER_API_KEY lines: keep one, with both comments merged.

Tried end to end on the dev DB (Growth plan for the preview, then put back):

  • The model question, then the cost card ("4 images · Nano Banana 2 Lite · about 0.6% of your monthly AI limit"), then four 1376×768 JPEGs on the card.
  • Its prompt_improve_usage row: feature = 'image', units = 4, cost_usd = 0.1345, what OpenRouter billed.
  • Files: one row, the four images' bytes counted in storage.
  • Picking image 3 moved the file to it, and it stayed selected after a reload.
  • PNG: the selected image came out as a 1376×768 PNG.

Screenshots (1024 px, light and dark): the image model question, the cost card, and the result card with image 3 selected.

Related issue

None. Chat Wave 4, W4-08 (CHAT-15 §E, CHAT-20 §9 row 70).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. cd web && yarn vitest run src/lib/chat/images src/lib/chat/file-store.test.ts src/lib/chat/partial-limit.test.ts src/components/chat/run/deliverable-card.test.tsx "src/app/api/chat/runs/[id]/files" src/lib/chat/registry src/lib/chat/undo.test.ts, and src/lib/chat/engine.test.ts -t "image".
  2. With CHAT_AGENTIC on and OPENROUTER_API_KEY set, open Chat as an Admin and ask "Create a cover image for our GEO post".
    • Pick Nano Banana 2 Lite on the question, then Approve the card.
    • Four images show, the first selected.
    • Pick another, reload: it stays selected.
    • PNG downloads it.
  3. In Files, the set is one row of type Image. Its download is the selected image.
  4. The AI usage meter rises by the images' share. The run's prompt_improve_usage row has feature = 'image', units = 4 and cost_usd equal to what OpenRouter billed.

Migrations 00476 and 00477 (apply both before deploying the web app). No new packages.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

CI passed on this branch (9d2e271), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41688852990): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

cihan-gyk and others added 4 commits November 3, 2026 20:02
… Files (W4-08, 00476)

Chat makes images now instead of refusing (CHAT-15 §E). Before the first
image of a chat it asks which image model to use, the options read from
the price data: the cheapest first and recommended, each with its
multiplier and its share of the monthly AI limit per image ("Your key"
on the workspace's own OpenRouter key). Each request makes four images
at 1K after a cost card that says how many, on which model, and their
share. When the limit left covers only some, the parts check asks first
("Create 1 image now"), with no image made before the answer.

The images run on OpenRouter, held and charged through W4-07's metering
at the provider's real cost, and are filed as one set in Files. Its card
shows every image, the selected one marked; the person picks another in
their own chat and downloads it as PNG, converted in the browser.

The engine change is thin: the parts check counts images at their
model's price, and a deliverable that filed itself is named, not filed
again. 00476 lets chat-files take JPEG, which Google's image models send.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…and round image multipliers (00477)

Found in the preview of W4-08:
- chat_files' formats check knew PNG and WebP only, so a set of JPEG
  images (Google's image models) wasn't filed: 00477 adds 'jpg'.
- The model may skip the parts check: when the AI limit left covers only
  some of the images, the request's own check now sends it there before
  the card, so the person is asked first.
- Shares come rounded to two decimals, so Nano Banana 2 read 2.1x:
  multipliers are whole from 2x, as CHAT-16 item 11 writes them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… images/access.ts

The Content Editor's images (W4-09) need the same: the workspace's own
OpenRouter key or Ansvisor's, and the image models with their multipliers
and shares. They move out of chat-images.ts as they are, with their tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 3, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things to fix:

  1. The image tool tells the model never to refuse. generate_image's description (web/src/lib/chat/registry/image-tools.ts:57) says "Never refuse an image request and never point to another image tool or app". That overrides Chat's other guardrails, so a request like a competitor's CEO in handcuffs is left to the image provider's own filter (Chat's test set requires that it insults no one). The intent, stopping "I can't make images", fits in: "Don't say you can't make images; make them with this tool", while keeping the normal rules (no real people's likeness, no insulting or misleading imagery of competitors, no trademarks the brand doesn't own).

  2. Admin provider switches aren't applied to images. The image models are google/* and openai/*, but imageModelOptions (images/access.ts), makeChatImages and the generate_image enum never check modelProviderOn / readProviderChoices (#2036). Once both land, an Admin who turns Google off still gets Nano Banana recommended and prompts sent to Google. Filter the models by providerOn in the options, refuse in the precheck, and limit the plan_parts image_model enum the same way (in whichever of #2036/#2103 merges second).

Small: when every request is refused, makeChatImages charges $0 and keeps units = the number held rather than releasing the row; selectRunImage updates only storage_path, so a mixed-type set keeps the first image's mime_type; the other images of a set (image-1..N) have no delete path yet.

cihan-gyk and others added 2 commits November 4, 2026 01:39
…odels whose maker an Admin turned off

Review on ansvisor#2103:
- generate_image no longer says "never refuse": it says don't claim Chat
  can't make images, and keeps Chat's rules (no real person's likeness, no
  insulting or misleading imagery of a competitor or anyone, no trademark
  the brand doesn't own).
- The provider switches of Settings › Chat (W4-34) apply to images: the
  model question offers only models whose maker is on, the precheck and
  the request refuse one that is off, and with every maker off Chat says
  it can't make images here. model-providers.ts is ansvisor#2036's, as it is.
- A request every provider refused, billing nothing, lets its hold go;
  picking an image of a set takes its own type and format.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…generation

# Conflicts:
#	server/src/lib/chat-budget.test.js
#	server/src/routes/internal-chat-budget.js
#	web/messages/en.json
#	web/src/lib/chat/budget.test.ts
#	web/src/lib/chat/engine.test.ts
#	web/src/lib/chat/registry/tools.test.ts
#	web/src/lib/chat/registry/tools.ts
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Done in 535581e and merged main: generate_image now keeps Chat's rules (no real person's likeness, no insulting or misleading imagery, no trademarks the brand doesn't own) instead of "never refuse", image models follow the Settings › Chat provider switches (model question, precheck and request), and the small ones are fixed (all-refused request releases its hold, a picked image keeps its own type); the plan_parts enum stays static and the precheck refuses an off provider instead.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both points are fixed; CI typecheck fails at web/src/lib/chat/partial-limit.ts:103 ("createImages" not assignable).

A merge left two "partial" blocks in en.json, and the second, without
createImages, replaced the first, so typecheck failed in partial-limit.ts.
The duplicate is gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMenyNyxsr2Doq4CaQ3
gkhngyk
gkhngyk previously approved these changes Nov 4, 2026
…neration

# Conflicts:
#	web/src/lib/chat/budget.test.ts
#	web/src/lib/chat/engine.test.ts
#	web/src/lib/chat/undo-output.ts
gkhngyk
gkhngyk previously approved these changes Nov 4, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@cihan-gyk
cihan-gyk dismissed gkhngyk’s stale review November 4, 2026 17:41

The merge-base changed after approval.

…generation

# Conflicts:
#	supabase/schema.sql
#	web/.env.example
#	web/messages/en.json
gkhngyk
gkhngyk previously approved these changes Nov 4, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

…KEY entry

Main already lists OPENROUTER_API_KEY for Chat's model menu (W4-03), so
the merge left the key twice in web/.env.example. Chat's images now join
that entry's comment instead of a second key line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMenyNyxsr2Doq4CaQ3
gkhngyk
gkhngyk previously approved these changes Nov 4, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

…generation

# Conflicts:
#	supabase/schema.sql
#	web/.env.example
#	web/src/components/chat/run/deliverable-card.test.tsx
#	web/src/components/chat/run/deliverable-card.tsx
#	web/src/lib/chat/engine.test.ts
#	web/src/lib/chat/engine.ts
#	web/src/lib/chat/images/access.test.ts
#	web/src/lib/chat/images/access.ts
gkhngyk
gkhngyk previously approved these changes Nov 4, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

…generation

# Conflicts:
#	supabase/schema.sql
…rned off (W4-34)

The editor's image list already hides such a model (imageModelOptions),
but POST /api/content/drafts/{id}/images still ran any listed model. It
now checks imageModelOn first, as Chat's image precheck does, and refuses
before anything is held, charged or sent to the provider: 409 with
{ ok: false, code: 'provider_off', model }, and the panel says the
model's maker is turned off in Settings › Chat. A failed read refuses too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMenyNyxsr2Doq4CaQ3
gkhngyk
gkhngyk previously approved these changes Nov 4, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@gkhngyk
gkhngyk merged commit ad68e33 into ansvisor:main Nov 5, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants