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.
89 lines
3.0 KiB
TypeScript
89 lines
3.0 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
import { useAtelierClient } from '../lib/api';
|
|
import type { ProviderStatus } from '../lib/api';
|
|
|
|
/**
|
|
* The providers Officer curates, and which of them we hold a key for.
|
|
*
|
|
* One list, one call. There used to be two — the configured rows and the adapter catalogue — and they
|
|
* were different shapes answering overlapping questions, which is most of why the old screen was
|
|
* confusing. `/providers` now returns the curated list with each entry's key status and its models
|
|
* attached, so "what can I use" and "what have I set up" are one answer.
|
|
*/
|
|
export const useProviders = () => {
|
|
const client = useAtelierClient();
|
|
const [providers, setProviders] = useState<ProviderStatus[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [busy, setBusy] = useState(false);
|
|
const [refreshing, setRefreshing] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const refresh = useCallback(async () => {
|
|
try {
|
|
setProviders(await client.listProviders());
|
|
setError(null);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void refresh();
|
|
}, [refresh]);
|
|
|
|
const guard = async (action: () => Promise<ProviderStatus[]>) => {
|
|
setBusy(true);
|
|
try {
|
|
setProviders(await action());
|
|
setError(null);
|
|
return true;
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
return false;
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Re-read a provider's models from the vendor, or every provider when omitted.
|
|
*
|
|
* Its own busy flag, separate from `busy`: this is a live fetch to a third party and takes seconds,
|
|
* where setting a key is a local write. One flag would grey out the screen for a network round trip.
|
|
*/
|
|
const refreshCatalogue = async (name?: string) => {
|
|
setRefreshing(true);
|
|
try {
|
|
const result = await client.refreshCatalogue(name);
|
|
setProviders(result.providers);
|
|
setError(null);
|
|
return result;
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err));
|
|
return null;
|
|
} finally {
|
|
setRefreshing(false);
|
|
}
|
|
};
|
|
|
|
return {
|
|
providers,
|
|
loading,
|
|
busy,
|
|
refreshing,
|
|
error,
|
|
refresh,
|
|
refreshCatalogue,
|
|
setKey: (name: string, apiKey: string) => guard(() => client.setProviderKey(name, apiKey)),
|
|
clearKey: (name: string) => guard(() => client.clearProviderKey(name)),
|
|
byName: (name: string) => providers.find((provider) => provider.name === name),
|
|
/** The one a generation would use if you pressed Generate now: your choice, else the first with a key. */
|
|
firstUsable: () => providers.find((provider) => provider.configured && provider.usable),
|
|
};
|
|
};
|
|
|
|
export type ProvidersManager = ReturnType<typeof useProviders>;
|