Skip to content

Commit

Permalink
Fix model visibility toggles and refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithcheese committed Aug 5, 2024
1 parent 65001b0 commit 998199d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 202 deletions.
96 changes: 0 additions & 96 deletions src/routes/(app)/chat/[id]/[...rest]/config/$data.ts

This file was deleted.

112 changes: 31 additions & 81 deletions src/routes/(app)/settings/keys/$data.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { z } from "zod";
import { invalidateModel, type Model, modelTable, type Key, keyTable, useDb } from "@/database";
import type { Provider } from "$lib/providers";
import { and, asc, eq, notInArray } from "drizzle-orm";
import { invalidateModel, type Key, keyTable, type Model, modelTable, useDb } from "@/database";
import { and, eq, notInArray } from "drizzle-orm";
import { invalidate } from "$app/navigation";
import { nanoid } from "nanoid";

export type ServicesView = Awaited<ReturnType<typeof loadServices>>;
import type { SQLiteTransaction } from "drizzle-orm/sqlite-core";

export const formSchema = z.object({
serviceId: z.string().min(1, { message: "Service is required" }),
Expand All @@ -14,91 +12,43 @@ export const formSchema = z.object({
baseURL: z.string().nullable(),
});

export async function loadServices() {
return useDb().query.keyTable.findMany({
with: {
models: true,
},
});
}

export async function addService(provider: Provider) {
const newId = nanoid(10);
const result = await useDb()
.insert(keyTable)
.values({
id: newId,
name: "",
providerId: provider.id,
baseURL: provider.defaultBaseURL,
apiKey: "",
})
.returning();
if (result.length < 1) {
throw Error("Failed to add service");
}
await invalidate("view:services");
return result[0];
}

export async function deleteService(service: Key) {
await useDb().transaction(async (tx) => {
await tx.delete(modelTable).where(eq(modelTable.serviceId, service.id));
await tx.delete(keyTable).where(eq(keyTable.id, service.id));
});
await invalidateModel(keyTable, service);
}

export async function updateService(service: Key) {
console.time("updateService");
await useDb()
.update(keyTable)
.set({ ...service })
.where(eq(keyTable.id, service.id));
console.timeEnd("updateService");
await invalidateModel(keyTable, service);
}

export async function replaceModels(service: Key, newModels: any[]) {
console.time("replaceModels");
await useDb().transaction(async (tx) => {
// remove models that no longer exist
await tx.delete(modelTable).where(
and(
eq(modelTable.serviceId, service.id),
notInArray(
modelTable.name,
newModels.map((m) => m.name),
),
export async function refreshModels(
tx: SQLiteTransaction<any, any, any, any>,
key: Key,
models: any[],
) {
await tx.delete(modelTable).where(
and(
eq(modelTable.keyId, key.id),
notInArray(
modelTable.name,
models.map((m) => m.name),
),
);
console.log("newModels", newModels);
for (const model of newModels) {
await tx
.insert(modelTable)
.values({ id: nanoid(10), name: model.name, visible: 1, serviceId: service.id })
.onConflictDoNothing({
target: [modelTable.name, modelTable.serviceId],
});
}
});
console.timeEnd("replaceModels");
await invalidateModel(keyTable, service);
),
);
for (const model of models) {
await tx
.insert(modelTable)
.values({ id: nanoid(10), name: model.name, visible: 1, keyId: key.id })
.onConflictDoNothing({
target: [modelTable.name, modelTable.keyId],
});
}
}

export async function toggleVisible(service: Key, model: Model) {
export async function toggleVisible(key: Key, model: Model) {
await useDb()
.update(modelTable)
.set({ visible: model.visible ? 0 : 1 })
.where(and(eq(modelTable.id, model.id), eq(modelTable.serviceId, service.id)));
await invalidateModel(keyTable, service);
.where(and(eq(modelTable.id, model.id), eq(modelTable.keyId, key.id)));
await invalidateModel(keyTable, key);
await invalidateModel(modelTable, model);
}

export async function toggleAllVisible(service: Key, visible: 1 | 0) {
export async function toggleAllVisible(key: Key, visible: 1 | 0) {
console.time("toggleAllVisible");
await useDb().update(modelTable).set({ visible }).where(eq(modelTable.serviceId, service.id));
await useDb().update(modelTable).set({ visible }).where(eq(modelTable.keyId, key.id));
console.timeEnd("toggleAllVisible");
await invalidateModel(keyTable, service);
await invalidate("view:chat");
await invalidateModel(keyTable, key);
await invalidate("view:key");
}
36 changes: 12 additions & 24 deletions src/routes/(app)/settings/keys/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
} from "@/components/ui/select";
import { setError, superForm } from "sveltekit-superforms";
import { zodClient } from "sveltekit-superforms/adapters";
import { formSchema } from "../$data";
import { formSchema, refreshModels, toggleAllVisible, toggleVisible } from "../$data";
import {
FormButton,
FormControl,
FormField,
FormFieldErrors,
FormLabel,
} from "@/components/ui/form";
import { keyTable, modelTable, serviceTable, invalidateModel, useDb } from "@/database";
import { invalidateModel, keyTable, modelTable, serviceTable, useDb } from "@/database";
import { and, eq, notInArray } from "drizzle-orm";
import { nanoid } from "nanoid";
import { goto, invalidate } from "$app/navigation";
Expand All @@ -35,11 +35,9 @@
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { toast } from "svelte-french-toast";
import { route } from "$lib/route";
import { toggleAllVisible, toggleVisible } from "../../../chat/[id]/[...rest]/config/$data";
import { Separator } from "@/components/ui/separator";
import { Label } from "@/components/ui/label";
import { Table, TableBody, TableCell, TableRow } from "@/components/ui/table";
import { Toggle } from "@/components/ui/toggle";
import { page } from "$app/stores";
let { data } = $props();
Expand Down Expand Up @@ -91,23 +89,7 @@
await tx.update(keyTable).set(form.data).where(eq(keyTable.id, keyId));
// remove models that no longer exist
const models = (await resp.json()) as any[];
await tx.delete(modelTable).where(
and(
eq(modelTable.keyId, keyId),
notInArray(
modelTable.name,
models.map((m) => m.name),
),
),
);
for (const model of models) {
await tx
.insert(modelTable)
.values({ id: nanoid(10), name: model.name, visible: 1, keyId })
.onConflictDoNothing({
target: [modelTable.name, modelTable.keyId],
});
}
await refreshModels(tx, data.key, models);
});
await invalidate("view:account");
toast.success("Account updated");
Expand All @@ -126,7 +108,13 @@
};
});
async function updateModels() {}
async function handleRefreshModels() {
await useDb().transaction(async (tx) => {
await refreshModels(tx, data.key, data.key.models);
});
toast.success("Models refreshed");
await invalidateModel(keyTable, data.key);
}
</script>

<form use:enhance method="post">
Expand Down Expand Up @@ -208,7 +196,7 @@
<CardTitle>{data.key.service.name} models</CardTitle>
<CardDescription>Show or hide models displayed in the chat interface.</CardDescription>
</div>
<Button variant="outline" onclick={updateModels}>
<Button variant="outline" onclick={handleRefreshModels}>
<RefreshCwIcon class={cn("mr-2 h-4 w-4", false && "loading-icon")} />
Refresh Models
</Button>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(app)/settings/keys/[id]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function load({ params, depends }) {
return error(404, "Account not found");
}
const form = await superValidate(key, zod(formSchema));
depends("view:account");
registerModel(keyTable, key, depends);
return {
form,
key,
Expand Down

0 comments on commit 998199d

Please sign in to comment.