Skip to content

feat(chat): hold a Content Editor image request on the AI budget, with no Chat run (W4-09) - #2101

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/image-budget-editor
Nov 4, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/image-budget-editor

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

The server part of W4-09 (images in the Content Editor): an image request made in the editor is held on the one AI budget, with no Chat run.

Chat's hold (reserve_chat_ai_usage) needs a Chat run, and the editor has none. The new endpoint uses the budget's own reservation (reserve_ai_usage), as the editor's other AI does, and ties the row to the draft.

POST /api/internal/chat/budget/reserve-draft-images { orgId, draftId, model, images, ownKey? } (CRON_SECRET, as the other internal budget endpoints):

  • Hold: each image at the model's image price (imageUnitUsd, feat(chat): meter image requests on the AI budget at the provider's real cost (W4-07) #2095). The row has feature = 'image', content_draft_id, and its images as units (00462).
  • Workspace's own key: the row is written as its own (own_key, cost 0) and nothing is held, so the limit doesn't move.
  • Plan without a limit (self-hosted): the row only records.
  • Refusals: 429 ai_limit when the budget is spent; 402 without an active plan; 403 chat_off; 404 for a draft that isn't the workspace's (its brand's organization is checked).
  • Charge and release: the existing /charge (with images: { produced, costUsd }) and /release. The charge takes exactly what the provider said it cost, no markup; credits draw the same.

The rate limiter already leaves /api/internal/chat/budget/* calls with the secret out.

Stacked on #2095 (W4-07). The web part (the editor's Generate with AI) comes in its own pull request on top of this one.

Related issue

None. Chat Wave 4, W4-09 (CHAT-25 §9, CHAT-15 §E).

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 server && npx vitest run src/lib/chat-budget.test.js src/routes/internal-chat-budget.test.js. Covered:
    • The hold is at the price per image, tied to the draft, with no run, and the charge is exactly the provider's cost.
    • With nothing made, the hold goes.
    • On the workspace's own key, nothing is held and the spend doesn't move.
    • The endpoint refuses a spent budget (429), another workspace's draft (404), Chat off, and a bad body (400).
    • On a plan without a limit, the row only records.

No migration (reserve_ai_usage takes feature = 'image' since 00405, units since 00462). No new environment variables. No user-visible change, so no Help article.

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 (08df762), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41684045979): 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 2 commits November 3, 2026 16:09
…eal cost (W4-07)

An image request now holds each image it may make at the model's image
price, then takes from the one AI budget exactly what OpenRouter said the
images cost (usage.cost), with no markup; credits draw the same amount and
the workspace's own key counts nothing. The server answers how many images
the limit left covers and what share of the plan's monthly limit one image
is, per model, so the menu can show it and pick the cheapest as default.

The web side adds the OpenRouter image client (plain fetch, four at a time,
1K, refusals cost nothing) and the image models list. Nothing calls them
yet: Chat (W4-08) and the editor (W4-09) wire them in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…h no Chat run (W4-09)

The editor makes images outside Chat, so it has no run for Chat's hold.
POST /api/internal/chat/budget/reserve-draft-images holds each image at
the model's image price through the budget's own reservation (as the
editor's other AI), the row tied to the draft and its images as its
units; on the workspace's own key it writes the row as its own and holds
nothing. It is charged and released as a Chat step is: at what the
provider said it cost, no markup. A draft of another workspace is 404.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
gkhngyk previously approved these changes Nov 3, 2026
…metering

# Conflicts:
#	server/src/lib/chat-budget.test.js
#	server/src/routes/internal-chat-budget.js
#	web/src/lib/chat/budget.test.ts
…editor

# Conflicts:
#	server/src/lib/chat-budget.test.js
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.

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:46

The merge-base changed after approval.

@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 6cebe63 into ansvisor:main Nov 4, 2026
7 checks passed
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 4, 2026
…mages

ansvisor#2095 (W4-07) is now in main. The budget files keep main's ansvisor#2095 code
plus ansvisor#2101's reserve-draft-images, identical to ansvisor#2101's latest
(feature/image-budget-editor f80a465), and this branch's W4-09 editor
additions in web/src/lib/chat/budget.ts.

# Conflicts:
#	server/src/lib/chat-budget.test.js
#	server/src/routes/internal-chat-budget.js
#	web/src/lib/chat/budget.test.ts

web/.env.example: main now documents OPENROUTER_API_KEY (W4-03); the
editor's images 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_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants