Skip to content

Commit 09b7038

Browse files
authored
feat(conv): let user switch models on conversations with deprecated models (#1462)
* feat(conv): let user switch models on conversations with deprecated models * lint * fix checks
1 parent 111fb66 commit 09b7038

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

src/lib/components/chat/ChatWindow.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import UploadedFile from "./UploadedFile.svelte";
3636
import { useSettingsStore } from "$lib/stores/settings";
3737
import type { ToolFront } from "$lib/types/Tool";
38+
import ModelSwitch from "./ModelSwitch.svelte";
3839
3940
export let messages: Message[] = [];
4041
export let loading = false;
@@ -279,6 +280,9 @@
279280
on:vote
280281
on:continue
281282
/>
283+
{#if isReadOnly}
284+
<ModelSwitch {models} {currentModel} />
285+
{/if}
282286
</div>
283287
{:else if pending}
284288
<ChatMessage
@@ -403,9 +407,7 @@
403407
<ChatInput value="Sorry, something went wrong. Please try again." disabled={true} />
404408
{:else}
405409
<ChatInput
406-
placeholder={isReadOnly
407-
? "This conversation is read-only. Start a new one to continue!"
408-
: "Ask anything"}
410+
placeholder={isReadOnly ? "This conversation is read-only." : "Ask anything"}
409411
bind:value={message}
410412
on:submit={handleSubmit}
411413
on:beforeinput={(ev) => {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<script lang="ts">
2+
import { invalidateAll } from "$app/navigation";
3+
import { page } from "$app/stores";
4+
import { base } from "$app/paths";
5+
import type { Model } from "$lib/types/Model";
6+
7+
export let models: Model[];
8+
export let currentModel: Model;
9+
10+
let selectedModelId = models.map((m) => m.id).includes(currentModel.id)
11+
? currentModel.id
12+
: models[0].id;
13+
14+
async function handleModelChange() {
15+
if (!$page.params.id) return;
16+
17+
try {
18+
const response = await fetch(`${base}/conversation/${$page.params.id}`, {
19+
method: "PATCH",
20+
headers: {
21+
"Content-Type": "application/json",
22+
},
23+
body: JSON.stringify({ model: selectedModelId }),
24+
});
25+
26+
if (!response.ok) {
27+
throw new Error("Failed to update model");
28+
}
29+
30+
await invalidateAll();
31+
} catch (error) {
32+
console.error(error);
33+
}
34+
}
35+
</script>
36+
37+
<div
38+
class="mx-auto mt-0 flex w-fit flex-col items-center justify-center gap-2 rounded-lg border border-gray-200 bg-gray-500/20 p-4 dark:border-gray-800"
39+
>
40+
<span>
41+
This model is no longer available. Switch to a new one to continue this conversation:
42+
</span>
43+
<div class="flex items-center space-x-2">
44+
<select
45+
bind:value={selectedModelId}
46+
class="rounded-md bg-gray-100 px-2 py-1 dark:bg-gray-900 max-sm:max-w-32"
47+
>
48+
{#each models as model}
49+
<option value={model.id}>{model.name}</option>
50+
{/each}
51+
</select>
52+
<button
53+
on:click={handleModelChange}
54+
disabled={selectedModelId === currentModel.id}
55+
class="rounded-md bg-gray-100 px-2 py-1 dark:bg-gray-900"
56+
>
57+
Accept
58+
</button>
59+
</div>
60+
</div>

src/lib/server/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ export const models: ProcessedModel[] = await Promise.all(
321321
);
322322

323323
// super ugly but not sure how to make typescript happier
324-
const validModelIdSchema = z.enum(models.map((m) => m.id) as [string, ...string[]]);
324+
export const validModelIdSchema = z.enum(models.map((m) => m.id) as [string, ...string[]]);
325325

326326
export const defaultModel = models[0];
327327

src/routes/conversation/[id]/+server.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { env } from "$env/dynamic/private";
22
import { startOfHour } from "date-fns";
33
import { authCondition, requiresUser } from "$lib/server/auth";
44
import { collections } from "$lib/server/database";
5-
import { models } from "$lib/server/models";
5+
import { models, validModelIdSchema } from "$lib/server/models";
66
import { ERROR_MESSAGES } from "$lib/stores/errors";
77
import type { Message } from "$lib/types/Message";
88
import { error } from "@sveltejs/kit";
@@ -513,8 +513,11 @@ export async function DELETE({ locals, params }) {
513513
}
514514

515515
export async function PATCH({ request, locals, params }) {
516-
const { title } = z
517-
.object({ title: z.string().trim().min(1).max(100) })
516+
const values = z
517+
.object({
518+
title: z.string().trim().min(1).max(100).optional(),
519+
model: validModelIdSchema.optional(),
520+
})
518521
.parse(await request.json());
519522

520523
const convId = new ObjectId(params.id);
@@ -533,9 +536,7 @@ export async function PATCH({ request, locals, params }) {
533536
_id: convId,
534537
},
535538
{
536-
$set: {
537-
title,
538-
},
539+
$set: values,
539540
}
540541
);
541542

0 commit comments

Comments
 (0)