File size: 908 Bytes
0b9dc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { useState, useCallback } from 'react';

import { modelApi } from '../api';
import type { ModelCard } from '../api';

/**

 * Fetches candidate models for a given provider on demand.

 * Does not auto-fetch — call `fetch(provider)` explicitly.

 */
export function useModels() {
	const [models, setModels] = useState<ModelCard[]>([]);
	const [loading, setLoading] = useState(false);
	const [error, setError] = useState<Error | null>(null);

	/**

	 * Loads the model list for the specified provider type.

	 * @param provider - e.g. "openai", "dashscope"

	 */
	const fetch = useCallback(async (provider: string) => {
		setLoading(true);
		setError(null);
		try {
			const res = await modelApi.list(provider);
			setModels(res.models);
		} catch (e) {
			setError(e as Error);
		} finally {
			setLoading(false);
		}
	}, []);

	return { models, loading, error, fetch };
}