diff --git a/app/composables/useAgents.ts b/app/composables/useAgents.ts index 38930ec..3c5057a 100644 --- a/app/composables/useAgents.ts +++ b/app/composables/useAgents.ts @@ -22,8 +22,8 @@ export const useAgents = () => { initPromise: Promise | null; }; - const init = () => { - if (state.initPromise) return; + const init = (): Promise => { + if (state.initPromise) return state.initPromise; state.initPromise = (async () => { const query = triplit.query('agents') @@ -32,10 +32,13 @@ export const useAgents = () => { const { results } = await useQuery('agents', triplit, query) watch(results, (newAgents) => { - console.log("newAgents", newAgents); - state.list.value = newAgents as unknown as Agent[] || []; + if (newAgents && newAgents.length > 0) { + state.list.value = newAgents as unknown as Agent[]; + } }, { immediate: true, flush: 'sync' }); })(); + + return state.initPromise; }; const getAgent = (id: MaybeRef) => { diff --git a/app/composables/useModels.ts b/app/composables/useModels.ts index 311ae2c..d716800 100644 --- a/app/composables/useModels.ts +++ b/app/composables/useModels.ts @@ -29,17 +29,21 @@ export const useModels = () => { initPromise: Promise | null; }; - const init = () => { - if (state.initPromise) return; + const init = (): Promise => { + if (state.initPromise) return state.initPromise; state.initPromise = (async () => { const query = triplit.query('providers').Include('models'); const { results } = await useQuery('providers', triplit, query) watch(results, (newProviders) => { - state.providers.value = newProviders as unknown as ProviderWithModels[] || []; + if (newProviders && newProviders.length > 0) { + state.providers.value = newProviders as unknown as ProviderWithModels[]; + } }, { immediate: true, flush: 'sync' }); })(); + + return state.initPromise; }; const allModels = computed(() => { diff --git a/app/composables/useUserSettings.ts b/app/composables/useUserSettings.ts index fced016..d241966 100644 --- a/app/composables/useUserSettings.ts +++ b/app/composables/useUserSettings.ts @@ -33,7 +33,7 @@ export const useUserSettings = () => { const state = nuxtApp._userSettingsState as UserSettingsState; - const init = () => { + const init = (): Promise => { if (state.initPromise) return state.initPromise; state.initPromise = (async () => { diff --git a/app/plugins/global-data.ts b/app/plugins/global-data.ts index 1a0fa8c..435b4ae 100644 --- a/app/plugins/global-data.ts +++ b/app/plugins/global-data.ts @@ -1,11 +1,13 @@ -export default defineNuxtPlugin(async () => { +export default defineNuxtPlugin({ + name: 'global-data', + enforce: 'pre', + async setup() { + await Promise.all([ + useAgents().init(), + useModels().init(), + useUserSettings().init() + ]); - // Warm up the caches - await Promise.all([ - useAgents().init(), - useModels().init(), - useUserSettings().init() - ]); - - console.log('Global data ready'); + console.log('Global data ready'); + } });