67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import { schema } from '#triplit/schema';
|
|
import type { Entity } from '@triplit/client';
|
|
|
|
export type ModelWithProvider = Entity<typeof schema, 'models'> & {
|
|
provider: Entity<typeof schema, 'providers'>;
|
|
};
|
|
|
|
export type ProviderWithModels = Entity<typeof schema, 'providers'> & {
|
|
models: Entity<typeof schema, 'models'>[];
|
|
};
|
|
|
|
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<ModelWithProvider[]>(() => {
|
|
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<ProviderWithModels[]>,
|
|
allModels: nuxtApp._allModels as ComputedRef<ModelWithProvider[]>,
|
|
getFirstAvailableModel,
|
|
unsubscribe: () => { },
|
|
};
|
|
};
|