import { schema } from '#triplit/schema'; import type { Entity } from '@triplit/client'; export type ModelWithProvider = Entity & { provider: Entity; }; export type ProviderWithModels = Entity & { models: Entity[]; }; export const useModels = async () => { const nuxtApp = useNuxtApp() as any; if (!nuxtApp._modelsSubscription) { if (!nuxtApp._modelsPromise) { const triplit = useTriplitClient(); const providersQuery = triplit .query('providers') .Include('models'); nuxtApp._modelsPromise = useQuery('providers', triplit, providersQuery).then((sub) => { nuxtApp._modelsSubscription = sub; return sub; }); } await nuxtApp._modelsPromise; } const { results: providers } = nuxtApp._modelsSubscription; if (!nuxtApp._allModels) { nuxtApp._allModels = computed(() => { if (!providers.value) return []; const list: ModelWithProvider[] = []; for (const provider of (providers.value as ProviderWithModels[])) { if (!provider.enabled) continue; for (const model of provider.models) { if (!model.enabled) continue; list.push({ ...model, provider } as unknown as ModelWithProvider); } } return list; }); } const getFirstAvailableModel = (): ModelWithProvider | null => { if (nuxtApp._allModels.value.length === 0) return null; return nuxtApp._allModels.value[0]!; }; return { providers: providers as Ref, allModels: nuxtApp._allModels as ComputedRef, getFirstAvailableModel, unsubscribe: () => { }, }; };