Add an AI provider
The contract lives in src/lib/ai/provider.ts:
tsgenerateImage({ prompt, userId }): Promise<{ url; width; height; model }>;
The default implementation is src/lib/ai/gateway.ts (Vercel AI Gateway).
Read both files before writing anything.
Steps
- Ask the user first — a new provider almost always means a new dependency, and dependencies need approval (AGENTS.md).
- Create
src/lib/ai/<provider>.tsexporting an object that satisfies theImageProviderinterface. Keep it thin: call the API, normalize the response to{ url, width, height, model }, throw on failure. - Config comes from env. Add the new vars to:
src/lib/env.ts(Zod schema, optional unless the provider is active).env.example(with a comment and a link to where keys come from)
- Wire selection where the provider is chosen (see how
gateway.tsand theAI_MOCKflag are dispatched) — selection stays env-driven, no provider conditionals in UI or actions. - Do not touch the credits flow. The generate action already spends before calling the provider and refunds on failure. Your provider just throws on error; the action handles money.
- Test: unit-test the response normalization with a mocked HTTP layer. Then run the e2e generate flow with the real provider once, manually.
Scope note
This repo intentionally ships one provider. Multi-provider image/video/audio with job queues, auto-refunds and gallery storage is what the Pro version is for (https://nikandr.com) — don't rebuild it ad hoc here.


