6ee4087a29
- Centralize `useAgents` and `useModels` state within the Nuxt app context to prevent data leaks and improve initialization. - Migrate virtualization from `vue-virtual-scroller` to `@tanstack/vue-virtual` with new `RowVirtualizerFixed` and `RowVirtualizerDynamic` components. - Upgrade Nuxt to v4.3.1 and remove `@vue-macros/nuxt`. - Replace `big.js` with an optimized custom `lshDecimal` string manipulation logic for pricing calculations in the provider API. - Implement automatic focus redirection in `ChatInput` to capture standard keyboard input. - Refactor Sidenav and Settings components to utilize virtualization for long lists (topics, agents, models). - Enhance theme colors and mobile experience. More work to come on both of these.
73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
import type { Entity } from "@triplit/client";
|
|
import type schema from "#triplit/schema";
|
|
import { nanoid } from "nanoid";
|
|
import { assert } from "~~/utils/assert";
|
|
|
|
export type Agent = Readonly<Entity<typeof schema, 'agents'> & { topics: Readonly<Entity<typeof schema, 'topics'>>[] }>;
|
|
|
|
export const useAgents = () => {
|
|
const nuxtApp = useNuxtApp();
|
|
const triplit = useTriplitClient();
|
|
|
|
// dont leaking between different users/requests
|
|
if (!nuxtApp._agentsState) {
|
|
nuxtApp._agentsState = {
|
|
list: ref<Agent[]>([]),
|
|
initPromise: null as Promise<void> | null,
|
|
};
|
|
}
|
|
|
|
const state = nuxtApp._agentsState as {
|
|
list: Ref<Agent[]>;
|
|
initPromise: Promise<void> | null;
|
|
};
|
|
|
|
const init = () => {
|
|
if (state.initPromise) return;
|
|
|
|
state.initPromise = (async () => {
|
|
const query = triplit.query('agents')
|
|
.Include('topics', (rel) => rel('topics').Order('createdAt', 'DESC'))
|
|
.Order('createdAt', 'ASC');
|
|
|
|
const { results } = await useQuery('agents', triplit, query)
|
|
watch(results, (newAgents) => {
|
|
console.log("newAgents", newAgents);
|
|
state.list.value = newAgents as unknown as Agent[] || [];
|
|
}, { immediate: true, flush: 'sync' });
|
|
})();
|
|
};
|
|
|
|
const getAgent = (id: MaybeRef<string>) => {
|
|
return computed(() => state.list.value.find((agent) => agent.id === id) || null);
|
|
}
|
|
|
|
const createAgent = async () => {
|
|
const triplit = useTriplitClient();
|
|
const { user } = useAuth();
|
|
if (!user.value) throw new Error('No user');
|
|
|
|
const id = nanoid();
|
|
await triplit.insert('agents', {
|
|
id,
|
|
name: 'New Agent',
|
|
userId: user.value.id,
|
|
systemPrompt: 'You are a helpful assistant.',
|
|
defaultModelId: null,
|
|
imageUrl: null,
|
|
createdAt: new Date().toISOString(),
|
|
});
|
|
|
|
assert('flush' in triplit);
|
|
await triplit.flush();
|
|
|
|
return state.list.value.find((agent) => agent.id === id)!;
|
|
};
|
|
return {
|
|
init,
|
|
agents: state.list,
|
|
getAgent,
|
|
createAgent
|
|
};
|
|
};
|