ai: cache the models manifest, skip it offline, show a loading state

The Manage tab fetched the CDN manifest and its signature on every
catalog request with a 30s timeout each, and the tab polls every 30s.
On a black-holed or offline network every poll paid up to 60s before
falling back to the bundled list, the frontend's 60s limit threw the
response away, and the tab showed a blank page the whole time.

get_models_catalog now skips the CDN while the have_internet flag is
explicitly clear, reuses the last attempt (success or failure) for an
hour, and uses a 5s timeout for the 2KB manifest. A skipped attempt is
not cached so the CDN is tried as soon as internet comes up. The cache
is per process and is cleared between tests by an autouse fixture.

The Manage tab shows "Loading the model catalog" while the first
response is pending, and on failure shows the error with a Retry
button and a note that the API may still be starting, instead of
rendering nothing.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-09-24 18:28:11 -06:00
parent 5b1b0559b5
commit 861e183bd2
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 136 additions and 9 deletions

View file

@ -8,7 +8,7 @@ import {StatusContext} from '../contexts/contexts';
import {AiChat} from './AiChat';
import {APIButton, humanFileSize, InfoMessage, PageContainer, TabLinks, useTitle, WROLModeMessage} from './Common';
import {SortableTable} from './SortableTable';
import {Button, Divider, Group, Header, NumberInput, Select, Table, Text, Toggle, toast} from './ui';
import {Button, Divider, Group, Header, Loading, Message, NumberInput, Select, Table, Text, Toggle, toast} from './ui';
import {Downloaders} from './Vars';
// The Controller's name for llama-server: the compose service in docker, the systemd unit natively.
@ -38,6 +38,7 @@ export function AiModelRow({model, onDownload}) {
export function ManageAi() {
const [catalog, setCatalog] = useState(null);
const [catalogError, setCatalogError] = useState(null);
const [serviceStatus, setServiceStatus] = useState(null);
const [pendingSave, setPendingSave] = useState(false);
const {status} = useContext(StatusContext);
@ -48,9 +49,13 @@ export function ManageAi() {
const data = await getAiCatalog();
if (data) {
setCatalog(data);
setCatalogError(null);
} else {
setCatalogError('The API did not return the model catalog.');
}
} catch (e) {
console.error(e);
setCatalogError(e.message || 'Failed to fetch the model catalog.');
}
};
@ -108,7 +113,18 @@ export function ManageAi() {
};
if (!catalog) {
return <WROLModeMessage content='Cannot modify AI'/>;
// Waiting on the first catalog response. On a slow first start this can take a while, so
// say so rather than showing an empty tab.
return <>
<WROLModeMessage content='Cannot modify AI'/>
{catalogError
? <Message kind='error' title='Could not load the model catalog'>
<p>{catalogError}</p>
<p>The API may still be starting. This page retries automatically, or</p>
<Button size='xs' onClick={fetchCatalog}>Retry now</Button>
</Message>
: <Loading>Loading the model catalog…</Loading>}
</>;
}
const {models, recommended_tier, total_ram, slow_hardware, disk_usage, enabled, active_model} = catalog;

View file

@ -122,6 +122,30 @@ describe('ManageAi', () => {
await waitFor(() => expect(postAiSettings).toHaveBeenCalledWith({active_model: 'My-Custom-Model-Q8_0.gguf'}));
});
it('shows a loading state, not a blank tab, until the catalog arrives', async () => {
let resolveCatalog;
getAiCatalog.mockReturnValue(new Promise(resolve => resolveCatalog = resolve));
render(<ManageAi/>);
expect(screen.getByText('Loading the model catalog…')).toBeInTheDocument();
expect(screen.queryByText(/tier is recommended/)).not.toBeInTheDocument();
resolveCatalog(catalogFixture());
await waitFor(() => expect(screen.getByText(/tier is recommended/)).toBeInTheDocument());
expect(screen.queryByText('Loading the model catalog…')).not.toBeInTheDocument();
});
it('shows the error with a retry when the catalog request fails', async () => {
getAiCatalog.mockRejectedValueOnce(new Error('promise timeout'));
render(<ManageAi/>);
await waitFor(() => expect(screen.getByText('Could not load the model catalog')).toBeInTheDocument());
expect(screen.getByText('promise timeout')).toBeInTheDocument();
getAiCatalog.mockResolvedValue(catalogFixture());
fireEvent.click(screen.getByText('Retry now'));
await waitFor(() => expect(screen.getByText(/tier is recommended/)).toBeInTheDocument());
expect(screen.queryByText('Could not load the model catalog')).not.toBeInTheDocument();
});
it('shows the slow-hardware warning on a Pi 4', async () => {
getAiCatalog.mockResolvedValue(catalogFixture({slow_hardware: true}));
render(<ManageAi/>);