Files
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

89 lines
3.4 KiB
TypeScript

import { createRouter } from '../create-router';
import { clearKey, keySource, providerStatuses, setKey } from '../providers/credentials';
import { refreshCatalogue, refreshableProviders } from '../catalogue/refresh';
import { openaiError } from '../errors';
export const providersRouter = createRouter();
/**
* Providers: the curated list, plus whether we hold a key for each.
*
* There is no create and no delete, because there is nothing to create — the providers are the ones
* Officer curates, and they are all listed whether set up or not. The only thing that changes is a
* key, which is a PUT and a DELETE on one provider, and a refresh of what models it offers.
*
* No response includes a key, in any form.
*/
providersRouter.get('/', async (ctx) => ctx.json({ data: await providerStatuses() }));
const known = async (name: string) => (await providerStatuses()).some((provider) => provider.name === name);
/** Set this provider's key. Written to the platform's secret store, never to Postgres. */
providersRouter.put('/:name/key', async (ctx) => {
const name = ctx.req.param('name');
if (!(await known(name))) return ctx.json(openaiError(`No provider named "${name}".`, 'not_found'), 404);
const body = (await ctx.req.json().catch(() => null)) as { apiKey?: unknown } | null;
const apiKey = typeof body?.apiKey === 'string' ? body.apiKey.trim() : '';
if (!apiKey) {
return ctx.json(openaiError("'apiKey' must be a non-empty string.", 'invalid_request_error', 'apiKey'), 400);
}
setKey(name, apiKey);
return ctx.json({ data: await providerStatuses() });
});
/**
* Forget this provider's key.
*
* A key that came from the environment cannot be forgotten here and says so: it belongs to the install
* rather than to this screen, and a delete that appeared to work and did not is worse than a refusal.
*/
providersRouter.delete('/:name/key', async (ctx) => {
const name = ctx.req.param('name');
if (!(await known(name))) return ctx.json(openaiError(`No provider named "${name}".`, 'not_found'), 404);
if (keySource(name) === 'environment') {
return ctx.json(
openaiError(`"${name}" takes its key from the environment; remove it there.`, 'invalid_request_error'),
409,
);
}
clearKey(name);
return ctx.json({ data: await providerStatuses() });
});
/**
* Re-read one provider's models and prices from the vendor, or every provider when omitted.
*
* The only trigger there is — no schedule. A provider with no refresher answers 400 rather than a
* silent success: from a button, "nothing happened" and "nothing could happen" look identical.
*
* `changed` is the honest part. A refresh with no key reaches nothing and leaves the shipped catalogue
* standing, which used to report exactly like a successful pass.
*/
providersRouter.post('/catalogue/refresh', async (ctx) => {
const name = ctx.req.query('provider') || undefined;
try {
const before = await providerStatuses();
const catalogue = await refreshCatalogue(name);
const after = await providerStatuses();
return ctx.json({
data: {
updated_at: catalogue.updatedAt,
refreshable: refreshableProviders(),
providers: after,
changed: JSON.stringify(before.map((p) => p.model_details)) !== JSON.stringify(after.map((p) => p.model_details)),
},
});
} catch (err) {
return ctx.json(
openaiError(err instanceof Error ? err.message : String(err), 'invalid_request_error', 'provider'),
400,
);
}
});