59bb7fbc12
This is once again a huge commit, but its mostly performance improvements along with some bug fixes and refactoring. It also includes changes to the theming systems. I'm still not 100% happy with the theming system, but its better than before. Model fetching has been dramatically improved! Nearly all the important computation and pre-processing has been moved to the server. This has also somehow fixed the way model details are loaded, which was causing many models to be missing their details despite models.dev having them. The markdown renderer has once again been changed, but I'm mostly certain that this is the last time major changes will be made to it. The renderer is not spamming components, bloating memory usage, and its not using a bug prone custom written chunking system. There's also a lot more that I haven't mentioned and honestly forgot. I need to get better commit hygiene tbh.
69 lines
2.1 KiB
TypeScript
69 lines
2.1 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');
|
|
|
|
const start = Date.now();
|
|
nuxtApp._modelsPromise = useQuery('providers', triplit, providersQuery).then((sub) => {
|
|
nuxtApp._modelsSubscription = sub;
|
|
console.log("fetching providers took", Date.now() - start);
|
|
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: () => { },
|
|
};
|
|
};
|