feat(catalogue): refresh is a button, not a schedule

The daily job is gone. A refresh only matters at one moment — you are picking a
provider and want to know its models are current — and at that moment a button
answers the question, where a nightly pass can only have already answered it,
invisibly, or not. It also bought a standing obligation: something hitting a
vendor from every install forever whether anyone opens Atelier or not.

So the add dialog is now the curated file made visible. Provider comes from the
catalogue, models come from that provider's entry in it, and two buttons re-read
it from the vendor: one for the provider in front of you, one for all of them.
`POST /providers/catalogue/refresh?provider=` is the only trigger there is.

The dialog also says how old the answer is — "as shipped" until a refresh has
ever run, which is worth showing, because it means the models listed are exactly
what the plugin was released with.

A provider with no refresher answers 400 rather than a silent success: from a
button, "nothing happened" and "nothing could happen" look identical.
This commit is contained in:
2026-08-20 22:21:15 +01:00
parent 0de38f0b13
commit 3c59bf4d0c
8 changed files with 143 additions and 61 deletions
+30 -43
View File
@@ -1,46 +1,59 @@
import { projectEnv } from '../env';
import { refreshOpenAI } from './openai-refresh';
import { SHIPPED_CATALOGUE } from './shipped';
import { catalogueAge, getCatalogue, mergeCatalogue, writeCatalogue } from './store';
import { getCatalogue, mergeCatalogue, writeCatalogue } from './store';
import type { Catalogue } from './types';
/**
* Keeping the catalogue current — the mechanism, with OpenAI as its first and only implementation.
* Bringing the catalogue up to date — on demand, never on a timer.
*
* A provider is refreshed by one function that returns what it could establish; this file decides
* when they run, merges the results, and writes the file. Adding a second provider is a second
* function and a line in `REFRESHERS` — the same restraint hono.ts keeps on its mounts.
* There was a daily schedule here and it is gone. A refresh only matters at one moment — you are
* choosing a provider and want to know its models are current — and at that moment a button answers
* the question while a nightly job can only have already answered it, or not, invisibly. A timer also
* buys a standing obligation: something that hits a vendor from every install forever, whether anyone
* is looking at Atelier or not.
*
* One refresher per provider, each returning what it could establish. Adding a second provider is a
* second function and a line in `REFRESHERS` — the same restraint hono.ts keeps on its mounts.
*/
/** Once a day. Model lists and prices move on the order of weeks; this is already generous. */
const INTERVAL_MS = Number(projectEnv('ATELIER_CATALOGUE_INTERVAL_MS') ?? 24 * 60 * 60 * 1000);
const REFRESHERS: Record<string, () => Promise<{ catalogue: Catalogue | null; listedIds: string[] | null }>> = {
openai: refreshOpenAI,
};
const REFRESHERS = [refreshOpenAI];
export const refreshableProviders = () => Object.keys(REFRESHERS);
/**
* Run every refresher and write the result.
* Refresh one provider, or every provider we have a refresher for.
*
* Retiring happens HERE rather than in a refresher, because it is the one judgement that needs the
* whole picture: a model we ship and the vendor no longer lists is deprecated, but only when the
* listing actually succeeded. "The endpoint returned three models" and "the request failed after
* three" are indistinguishable inside the refresher and completely different here.
*
* The merge starts from the catalogue as it stands, not from the shipped file, so refreshing OpenAI
* cannot discard what a previous refresh established about anything else.
*/
export async function refreshCatalogue(): Promise<Catalogue> {
const results = await Promise.all(REFRESHERS.map((run) => run()));
export async function refreshCatalogue(providerName?: string): Promise<Catalogue> {
const names = providerName ? [providerName] : Object.keys(REFRESHERS);
const unknown = names.filter((name) => !REFRESHERS[name]);
if (unknown.length > 0) throw new Error(`No refresher for provider "${unknown.join(', ')}".`);
let merged = SHIPPED_CATALOGUE;
for (const { catalogue, listedIds } of results) {
let merged = mergeCatalogue(SHIPPED_CATALOGUE, await getCatalogue());
for (const name of names) {
const { catalogue, listedIds } = await REFRESHERS[name]!();
if (!catalogue) continue;
merged = mergeCatalogue(merged, catalogue);
if (listedIds) {
const listed = new Set(listedIds);
for (const provider of merged.providers) {
if (!catalogue.providers.some((entry) => entry.name === provider.name)) continue;
if (provider.name !== name) continue;
for (const model of provider.models) {
// Deprecated, not deleted: the gallery labels historical rows from this list, and 88 images
// name the model that made them.
if (!listed.has(model.id)) model.status = 'deprecated';
model.status = listed.has(model.id) ? 'current' : 'deprecated';
}
}
}
@@ -50,34 +63,8 @@ export async function refreshCatalogue(): Promise<Catalogue> {
await writeCatalogue(written);
const count = written.providers.reduce((sum, provider) => sum + provider.models.length, 0);
console.log(`[atelier:catalogue] refreshed — ${written.providers.length} provider(s), ${count} model(s)`);
console.log(`[atelier:catalogue] refreshed ${names.join(', ')} — ${count} model(s) across ${written.providers.length} provider(s)`);
return written;
}
/**
* Start the schedule.
*
* Refreshes on boot only when the file is genuinely stale, so a sidecar that restarts twenty times in
* an afternoon — which this one has — does not hit OpenAI twenty times. `unref` so a pending timer
* never holds the process open at shutdown.
*/
export async function startCatalogueRefresh(): Promise<void> {
const globalForCatalogue = globalThis as unknown as { __atelierCatalogueStarted?: boolean };
if (globalForCatalogue.__atelierCatalogueStarted) return;
globalForCatalogue.__atelierCatalogueStarted = true;
const age = await catalogueAge();
if (age === null || age > INTERVAL_MS) {
// Not awaited: a slow or unreachable vendor must not delay the sidecar's own startup, and every
// failure path here already degrades to the shipped catalogue.
void refreshCatalogue().catch((err: unknown) => console.error('[atelier:catalogue] refresh failed:', err));
} else {
console.log(`[atelier:catalogue] ${Math.round(age / 3_600_000)}h old — next refresh on schedule`);
}
setInterval(() => {
void refreshCatalogue().catch((err: unknown) => console.error('[atelier:catalogue] refresh failed:', err));
}, INTERVAL_MS).unref();
}
export { getCatalogue };