Files
atelier/web/Generate/useGenerateManager.ts
pastilhas 19e94129a0 refactor(providers): a provider is a vendor and a key, and the table is gone
The dropdown was listing "connections" — a row per adapter + model + key — which
made choosing a model an act of configuration, put a key icon and a bin inside a
menu item, and showed five accounts nobody had created.

A provider is now a vendor Officer curates. Every one is listed, set up or not,
because a vendor you have no key for is exactly what you need to see in order to
add one. The model is chosen per generation, from that provider's entry in the
catalogue, beside it.

With the model off the row, all that remained of a "provider" was the key for
OpenAI — and Officer already stores keys: one active value per purpose, 0600
inside a 0700 directory, outside the backup tarball, created on first use so
nothing needs provisioning. So `atelier_providers` is dropped, along with
`generations.provider_id` that pointed into it. `provider` and `model` stay as
text on each generation, which were always the historical fact rather than a
pointer to configuration.

`getSecret`, never `getKey`: this is a foreign credential, and getKey mints a
random value for a purpose it cannot find — which for someone else's API key
means authenticating with something invented.

Resolution follows: a request names a model, and the catalogue says whose it is,
rather than a database being asked which stored row served it. A model nobody
in the catalogue claims is still accepted — the catalogue is advisory, and a
model released today should work before we ship a file that mentions it.

Two routes replace five: PUT and DELETE on /providers/:name/key. Deleting a key
that came from the environment is refused rather than faked — it belongs to the
install, not to this screen.
2026-08-20 23:02:31 +01:00

92 lines
3.1 KiB
TypeScript

import { useEffect, useState } from 'react';
import { useAtelierRefresh } from 'officerdev';
import { useAtelierClient } from '../lib/api';
import type { JobResponse, ProviderStatus } from '../lib/api';
export const SIZES = ['auto', '1024x1024', '1536x1024', '1024x1536'] as const;
export const QUALITIES = ['auto', 'low', 'medium', 'high'] as const;
export const BACKGROUNDS = ['auto', 'transparent', 'opaque'] as const;
/**
* The form.
*
* The MODEL is owned above, by the provider/model pair at the top of the panel — it is a choice about
* what to generate with, made in the same place the provider is chosen, and duplicating it here gave
* two answers to one question. Everything else on the form is this hook's.
*/
export const useGenerateManager = (providers: ProviderStatus[], model: string) => {
const client = useAtelierClient();
const [, bumpRefresh] = useAtelierRefresh();
const [prompt, setPrompt] = useState('');
const [n, setN] = useState(1);
const [size, setSize] = useState<string>('auto');
const [quality, setQuality] = useState<string>('auto');
const [background, setBackground] = useState<string>('auto');
const [job, setJob] = useState<JobResponse | null>(null);
const [running, setRunning] = useState(false);
const [error, setError] = useState<string | null>(null);
// A provider we hold a key for and have code for. Both are required and neither implies the other.
const usableProviders = providers.filter((provider) => provider.configured && provider.usable);
const ready = Boolean(prompt.trim()) && Boolean(model) && usableProviders.length > 0 && !running;
async function generate() {
if (!ready) return;
setRunning(true);
setError(null);
setJob(null);
try {
// Only what the user actually chose. `auto` is the provider's own default, so sending it would
// be inventing a parameter they did not set.
const body: Record<string, unknown> = { prompt: prompt.trim(), model, n };
if (size !== 'auto') body.size = size;
if (quality !== 'auto') body.quality = quality;
if (background !== 'auto') body.background = background;
const { id } = await client.startGeneration(body);
// Long-poll in a loop: each call waits up to 30s server-side, so this is one request per half
// minute rather than a busy poll, and it survives a generation that takes longer.
for (;;) {
const result = await client.awaitGeneration(id, 30);
if (result.atelier && ['succeeded', 'failed', 'partial'].includes(result.atelier.status)) {
setJob(result);
bumpRefresh();
return;
}
setJob(result);
}
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
} finally {
setRunning(false);
}
}
return {
providers,
usableProviders,
prompt,
setPrompt,
n,
setN,
size,
setSize,
quality,
setQuality,
background,
setBackground,
job,
running,
error,
ready,
generate,
};
};
export type GenerateManager = ReturnType<typeof useGenerateManager>;