feat: Auto-select model from last assistant response

This commit is contained in:
Aleksander Grygier 2025-11-22 19:18:32 +01:00
parent dc913ec424
commit a39ef24c91
1 changed files with 46 additions and 1 deletions

View File

@ -1,17 +1,62 @@
<script lang="ts"> <script lang="ts">
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { page } from '$app/state'; import { page } from '$app/state';
import { afterNavigate } from '$app/navigation';
import { ChatScreen } from '$lib/components/app'; import { ChatScreen } from '$lib/components/app';
import { import {
chatStore, chatStore,
activeConversation, activeConversation,
isLoading, isLoading,
stopGeneration stopGeneration,
activeMessages
} from '$lib/stores/chat.svelte'; } from '$lib/stores/chat.svelte';
import { selectModel, modelOptions, selectedModelId } from '$lib/stores/models.svelte';
let chatId = $derived(page.params.id); let chatId = $derived(page.params.id);
let currentChatId: string | undefined = undefined; let currentChatId: string | undefined = undefined;
async function selectModelFromLastAssistantResponse() {
const messages = activeMessages();
if (messages.length === 0) return;
let lastMessageWithModel: DatabaseMessage | undefined;
for (let i = messages.length - 1; i >= 0; i--) {
if (messages[i].model) {
lastMessageWithModel = messages[i];
break;
}
}
if (!lastMessageWithModel) return;
const currentModelId = selectedModelId();
const currentModelName = modelOptions().find((m) => m.id === currentModelId)?.model;
if (currentModelName === lastMessageWithModel.model) {
return;
}
const matchingModel = modelOptions().find(
(option) => option.model === lastMessageWithModel.model
);
if (matchingModel) {
try {
await selectModel(matchingModel.id);
console.log(`Automatically loaded model: ${lastMessageWithModel.model} from last message`);
} catch (error) {
console.warn('Failed to automatically select model from last message:', error);
}
}
}
afterNavigate(() => {
setTimeout(() => {
selectModelFromLastAssistantResponse();
}, 100);
});
$effect(() => { $effect(() => {
if (chatId && chatId !== currentChatId) { if (chatId && chatId !== currentChatId) {
currentChatId = chatId; currentChatId = chatId;