mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 21:59:58 -05:00
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:
parent
5b1b0559b5
commit
861e183bd2
5 changed files with 136 additions and 9 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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/>);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue