Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions .context/TECHSTACK.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,35 @@ server/index.ts` in prod. `erasableSyntaxOnly` and `verbatimModuleSyntax` in tsc
response cap; `DOOP_CUSTOM_PROVIDER_PRIVATE_HOSTS` admits private hosts and http for a self-hosted
instance, `DOOP_CUSTOM_PROVIDERS=false` turns the feature off (existing ones read as disconnected).
Advanced: extra headers (routing and framing headers refused), `max_tokens` vs
`max_completion_tokens`, and echoing `reasoning_content`. Tests: `customProvider.test.ts`,
`customProvidersHttp.test.ts`, `customProviderForm.test.tsx`.
`max_completion_tokens`, and echoing `reasoning_content`. A Chat Completions provider can also list image models
(`imageModels: { id, name?, transparent? }[]` in the same `custom` jsonb, at most 20;
`.context/specs/custom-provider-image-models.md`): they join the Settings image-model list for whoever has the
provider on (picked as `custom:<id>/<model id>` in `image_prefs`), and `generate_image` draws with them through
`POST {baseUrl}/images/generations` over `customProviderFetch` on the provider's key and headers (OpenAI's Images API
body; `data[0].b64_json`, or a `data[0].url` fetched the same way, credentials only to the base URL's origin; PNG, JPEG
or WebP only). Billed as the person's own connection, a shared provider included; a pick that cannot run refuses, never
falling back to another model or a server key. `generate_image`'s `transparent` sends `background: "transparent"` only
to a model marked so. Tests: `customProvider.test.ts`,
`customProvidersHttp.test.ts`, `customProviderForm.test.tsx`, `customProviderImages.test.ts`,
`customProviderImagesHttp.test.ts`, `imageModelCard.test.tsx`.
- Shared model providers (`server/sharedModelAccounts.ts`, `src/components/SharedProviderSettings.tsx`):
a custom provider an instance admin sets up once in Admin → Shared models, for every signed-in
person. The definition lives in `shared_model_accounts` (kind `custom:<id>`, key and headers sealed,
`enabled`, at most 10; migration 0066) and each person's own model, effort and on/off in
`shared_model_account_prefs` (deleted with the provider). `getAccounts`/`getAccount` merge the
enabled ones after the person's own accounts as `ModelAccount`s marked `shared`, so the composer
pick, the default (someone with no account of their own defaults to the first one on), the free-tier
meter and runs treat it like any connection; picks write the prefs row, and edit, disconnect and the
set-up probes' stored-secret fallback refuse a shared kind (403). A person's view gives its origin
only, no header names. `GET/POST /api/admin/shared-providers`, `PUT/PATCH/DELETE
/api/admin/shared-providers/:kind` and `/models`, `/test` (the same validation, probes and form as
personal ones, in admin mode); the admin view never carries the key or header values either, only
`hasKey` and `headerNames`, and the stored key and headers only go to the origin they were saved for
(a probe or edit for another origin must bring its own). `DOOP_CUSTOM_PROVIDERS=false` hides them
from everyone. For image billing a shared provider is the person's own connection, so the server's
image keys do not draw for them; its image models, if it lists any, draw for everyone on the company's endpoint. Tests:
`sharedModelAccounts.test.ts`, `sharedModelAccountsStore.test.ts`, `sharedModelProvidersHttp.test.ts`,
`sharedProviderRow.test.tsx`, `customProviderForm.test.tsx`.
- Reasoning effort on a user's OpenAI account (ChatGPT subscription or OpenAI key): the pick lives
in `model_accounts.reasoning_effort` (`REASONING_EFFORTS` in `shared/modelMenu.ts`: low / medium /
high / xhigh), set through `PATCH /api/model-account/:kind { effort }`, read by `effortFor` in
Expand Down
7 changes: 7 additions & 0 deletions .context/specs/INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ lists every file in this folder.

## Active

- [shared-model-providers.md](shared-model-providers.md) - Shared model providers: custom
providers an admin sets up once for every user (Admin → Shared models), per-user model pick and
on/off, sealed key never returned; `shared_model_accounts` + prefs, `/api/admin/shared-providers`.
- [custom-provider-image-models.md](custom-provider-image-models.md) - Image models on custom providers
(personal and shared): listed beside the chat models, offered in Settings → Image model, drawn through the
provider's `POST {baseUrl}/images/generations` on its key, billed as the person's own connection, no server-key
fallback, `transparent` cut-outs; no migration (`custom` jsonb, `image_prefs`).
- [skills.md](skills.md) - Team skills: named markdown instructions a workspace or person writes
once, invoked by `/handle` in a request and added to the resident agent's system prompt for that
run; storage, `/api/skills`, the settings card. Issue #380.
Expand Down
118 changes: 118 additions & 0 deletions .context/specs/custom-provider-image-models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Image models on custom providers

Status: implemented

## Problem

Image generation (`generate_image`, MCP and resident) draws only with the built-in registry (`IMAGE_MODELS` in
`shared/modelMenu.ts`): GPT Image on a ChatGPT subscription or an OpenAI key, Nano Banana on a Gemini key, Seedream on
the server's `ARK_API_KEY`. A team that self-hosts an image model has no way in. The usual setup is an OpenAI
Images-compatible endpoint (`POST {baseUrl}/images/generations` answering `data[0].b64_json`): a LiteLLM gateway in front
of ComfyUI/FLUX, vLLM, LocalAI. `OPENAI_IMAGES_URL` does not help: it is server-wide, takes registry ids only and runs on
the server's or a person's OpenAI key, and since shared providers the server key no longer draws for people who have one.

## Goal

A custom provider, a person's own or an admin's shared one ([shared-model-providers.md](shared-model-providers.md)),
lists **image models** next to its chat models. Whoever has that provider connected and on finds them in Settings →
Doop Agent → Image model, and `generate_image` draws with the one they picked through the provider's endpoint, on the
provider's key and headers.

Out of scope: image models on an Anthropic-compatible provider (that API has no images endpoint), image editing or
reference images, a Test button for image models, `DOOP_IMAGE_MODEL` naming a custom image model.

## Behaviour

1. **Config.** A Chat Completions provider's description (`CustomProviderConfig`) gets an optional
`imageModels: { id, name?, transparent? }[]`. `transparent` says the endpoint honours `background: "transparent"`.
An id can also be a chat model of the same provider. The list is optional: a description stored before it existed stays valid and is
stored without the field when the list is empty.
2. **Form.** `CustomProviderForm` (Settings and Admin → Shared models) has an "Image models" section under the models,
for the Chat Completions API type only: id, optional name, a Transparent switch, remove, "Add image model". The ids
"Fetch models" found are offered for both lists, each list offering the ids it does not hold yet.
3. **Picker.** `GET /api/image-model` lists the registry entries as before, then one entry per image model of each
custom provider the person has connected and on (their own first, then shared ones), with
`custom: { provider, name, shared?, transparent? }`. `ImageModelCard` shows them as one row per provider ("Name
(shared)" for a shared one) with a dropdown when it lists several. The pick is stored in `image_prefs.model` as
`custom:<id>/<model id>` (`customImageModelRef` in `shared/customProvider.ts`); `PUT /api/image-model` accepts only an
id the person's picker lists.
4. **Default.** With no pick, the person's own credentials come first: `DOOP_IMAGE_MODEL` when it runs on an account
of theirs, else the first registry entry that does, else the first custom image model; only then what a server key
pays for (`DOOP_IMAGE_MODEL` first): any entry for someone who connected nothing, and Seedream on `ARK_API_KEY` for
everyone. Someone whose only connection is a shared provider with image models therefore draws on it from their
first sign-in, even where the operator set `ARK_API_KEY`. An explicit pick on Seedream still draws Seedream.
5. **Request.** `POST {baseUrl}/images/generations` through `customProviderFetch` (so `DOOP_CUSTOM_PROVIDER_PRIVATE_HOSTS`,
DNS pinning, no redirects and the 32 MB cap apply), with the provider's headers and `Authorization: Bearer <key>` when
it has one, and the body `imageApiGenerator` sends to OpenAI: `model`, `prompt`, `n: 1`, `size` from `SIZES`,
`quality`, `output_format: "png"`, plus `background: "transparent"` when the call asked for a cut-out and the model
says it can draw one.
6. **Response.** `data[0].b64_json` is decoded. A `data[0].url` (absolute or relative to the base URL) is fetched through
`customProviderFetch` too, with the provider's key and headers only when it is on the base URL's origin. The result must
decode as PNG, JPEG or WebP, or the call fails saying so; neither field fails with the endpoint's own `error.message`
when it gave one.
7. **Transparent.** `generate_image` (MCP and resident) takes an optional `transparent` boolean. Only a custom image
model marked `transparent` is sent the field; every other model draws opaque. The result says what was drawn, read
off the image (an alpha channel that is not opaque everywhere), not off the flag: `transparent` in the MCP result, a
note in the resident's. The stored WebP keeps the alpha; the preview is flattened on white.
8. **Billing.** A custom image model is the person's own connection, a shared provider included (the company's endpoint
pays). `billed_to` is the provider's name; analytics get `custom-personal` or `custom-shared` instead of the name.
9. **No fallback.** A pick on a custom image model that cannot run now (its provider switched off by the person, switched
off or deleted by its admin, removed, the model taken off the list, or `DOOP_CUSTOM_PROVIDERS=false`) refuses with the
fix ("switch it on, or pick another image model"); it never draws on another model, connection or a server key. When
nothing at all can draw, the refusal names a switched-off provider with image models like a switched-off image account.
Disconnecting a personal provider deletes its person's pick on it, and deleting a shared provider deletes everyone's
(`image_prefs` rows starting `custom:<id>/`, in the same transaction), so they fall back to the default rather than
being refused for a provider that is gone.
10. **Errors.** A 401/403 from a shared provider tells the person to ask their admin (Admin → Shared models), as a run
failure does; from a personal one, to update the key in Settings. A 404 or "no such model" names the model and the
missing `/images/generations`. Other failures carry the endpoint's status and text, like the registry providers',
with the provider's key and every header value masked (`redactSecrets`, as for its chat turns) on the whole text, before
it is shortened: an endpoint may quote
the key it refused, and a shared provider's key must never reach a person or a log.
11. **Security.** The key and headers never appear in any response; image models are part of the description, so a
person's view of a shared provider lists them (they name models, not secrets). Only an admin changes a shared
provider's image models (the admin routes; `PUT /api/custom-providers/:kind` on a shared kind is 403).

## Data

No migration: `imageModels` rides in the existing `custom` jsonb of `model_accounts` (0051) and
`shared_model_accounts` (0062), re-checked by `parseCustomProviderConfig` on every read, and the pick in the existing
`image_prefs.model` text column.

## Code shape

- `shared/customProvider.ts`: `CustomImageModel`, `MAX_CUSTOM_IMAGE_MODELS` (20), the parser, `customImageModelRef` /
`parseCustomImageModelRef`.
- `server/imagePrefs.ts` accepts a well-formed custom ref; whether it can run is decided per run.
- `server/imageGen.ts`: `customImageChoices(accounts)`, `customImageGenerator` (the request, link fetch, format check,
error wording), `chosenImageModel` returning a registry option, a custom choice or the missing custom pick,
`imageModelAvailability` listing the custom entries, `TRANSPARENT_NOTE` for both tool schemas.
- `server/modelAccounts.ts` `statusOf` passes a shared provider's image models to the person's view.
- UI: `CustomProviderForm.tsx`, `ImageModelCard.tsx`, the image-model count on the Admin row
(`SharedProviderSettings.tsx`); strings in `src/lib/i18n/{en,pt-BR}/models.ts` and `admin.ts`.

## Tests

- `customProvider.test.ts`: parsing (normalising, the cap, ids, duplicates, names, Anthropic drops them, stored configs
without the field), the preference ref.
- `customProviderImages.test.ts`: the request (URL, key, headers, body, transparent or not, keyless), personal and shared
draws, the default for someone with only a shared provider while server keys (`ARK_API_KEY` included) are set, the
picker's entries, switched-off / deleted / delisted picks refusing without touching the network, link responses with
and without credentials, non-image and empty answers, error wording, masked secrets (400, 429, an error beside a 200,
404), transparency read off the image, private hosts.
- `sharedModelAccounts.test.ts`: a shared provider's image models through the real `getAccounts`, and gone with
`DOOP_CUSTOM_PROVIDERS=false`.
- `customProviderImagesHttp.test.ts` (real server, stub endpoint): admin adds image models, a person cannot change them,
the picker lists and picks them, MCP `generate_image` stores the image drawn on the shared key, the 401 wording,
switched off draws nothing, a personal provider's image models, picks deleted with a disconnected or deleted
provider, no key in any response.
- `customProviderForm.test.tsx`, `imageModelCard.test.tsx`: the form section and the picker rows.

## Notes

- `quality` and `output_format` are OpenAI's GPT Image fields; a strict server that refuses unknown fields will refuse
them. LiteLLM and most gateways pass or drop them. A per-model "plain request" switch is a follow-up if one turns up.
- A provider still needs at least one chat model, and its image models are offered only while it is on, so it is also a
chat connection. An endpoint that only draws images (no chat model to list) is not supported on its own; a gateway
that serves both, the usual setup, is. Image-only providers would need the config, the agent default and the composer
to skip a provider without chat models, which is a separate change.
Loading
Loading