auto regen on user message branch

This commit is contained in:
Aidan Bleser 2025-06-19 12:46:36 -05:00
parent 4ace17433d
commit 5814208d1d
3 changed files with 19 additions and 54 deletions

View file

@ -162,8 +162,6 @@ export const createBranched = mutation({
const existingConversation = await ctx.db.get(args.conversation_id); const existingConversation = await ctx.db.get(args.conversation_id);
console.log(existingConversation);
if (!existingConversation) throw new Error('Conversation not found'); if (!existingConversation) throw new Error('Conversation not found');
if (existingConversation.user_id !== session.userId && !existingConversation.public) if (existingConversation.user_id !== session.userId && !existingConversation.public)
throw new Error('Unauthorized'); throw new Error('Unauthorized');
@ -187,8 +185,6 @@ export const createBranched = mutation({
cost_usd: newMessages.reduce((acc, m) => acc + (m.cost_usd ?? 0), 0), cost_usd: newMessages.reduce((acc, m) => acc + (m.cost_usd ?? 0), 0),
}); });
console.log(newConversationId);
await Promise.all( await Promise.all(
newMessages.map((m) => { newMessages.map((m) => {
const newMessage = { const newMessage = {

View file

@ -91,7 +91,6 @@ export const set = mutation({
const session = await ctx.runQuery(api.betterAuth.publicGetSession, { const session = await ctx.runQuery(api.betterAuth.publicGetSession, {
session_token: args.session_token, session_token: args.session_token,
}); });
console.log('Session', session);
if (!session) throw new Error('Invalid session token'); if (!session) throw new Error('Invalid session token');
@ -102,9 +101,6 @@ export const set = mutation({
) )
.first(); .first();
console.log('Trying to set', args.model_id, 'for', session.userId);
console.log('Existing', !!existing);
if (args.enabled && existing) return; // nothing to do here if (args.enabled && existing) return; // nothing to do here
if (existing) { if (existing) {

View file

@ -18,6 +18,7 @@
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { callGenerateMessage } from '../../api/generate-message/call'; import { callGenerateMessage } from '../../api/generate-message/call';
import * as Icons from '$lib/components/icons'; import * as Icons from '$lib/components/icons';
import { settings } from '$lib/state/settings.svelte';
const style = tv({ const style = tv({
base: 'prose rounded-xl p-2 max-w-full', base: 'prose rounded-xl p-2 max-w-full',
@ -66,30 +67,13 @@
return; return;
} }
await goto(`/chat/${res.value}`);
}
async function branchAndGenerate() {
const res = await ResultAsync.fromPromise(
client.mutation(api.conversations.createBranched, {
conversation_id: message.conversation_id as Id<'conversations'>,
from_message_id: message._id,
session_token: session.current?.session.token ?? '',
}),
(e) => e
);
if (res.isErr()) {
console.error(res.error);
return;
}
const cid = res.value; const cid = res.value;
if (message.role === 'user' && settings.modelId) {
const generateRes = await callGenerateMessage({ const generateRes = await callGenerateMessage({
session_token: session.current?.session.token ?? '', session_token: session.current?.session.token ?? '',
conversation_id: cid, conversation_id: cid,
model_id: message.model_id!, model_id: settings.modelId,
images: message.images, images: message.images,
web_search_enabled: message.web_search_enabled, web_search_enabled: message.web_search_enabled,
}); });
@ -98,6 +82,7 @@
// TODO: add error toast // TODO: add error toast
return; return;
} }
}
await goto(`/chat/${cid}`); await goto(`/chat/${cid}`);
} }
@ -178,27 +163,15 @@
onClickPromise={createBranchedConversation} onClickPromise={createBranchedConversation}
{...tooltip.trigger} {...tooltip.trigger}
> >
<Icons.Branch class="group-data-[loading=true]:opacity-0" />
</Button>
{/snippet}
Branch off this message
</Tooltip>
{#if message.role === 'user'} {#if message.role === 'user'}
<Tooltip>
{#snippet trigger(tooltip)}
<Button
size="icon"
variant="ghost"
class={cn('group order-0 size-7')}
onClickPromise={branchAndGenerate}
{...tooltip.trigger}
>
<Icons.BranchAndRegen class="group-data-[loading=true]:opacity-0" /> <Icons.BranchAndRegen class="group-data-[loading=true]:opacity-0" />
{:else}
<Icons.Branch class="group-data-[loading=true]:opacity-0" />
{/if}
</Button> </Button>
{/snippet} {/snippet}
Branch and regenerate {message.role === 'user' ? 'Branch and regenerate message' : 'Branch off this message'}
</Tooltip> </Tooltip>
{/if}
{#if message.content.length > 0} {#if message.content.length > 0}
<Tooltip> <Tooltip>
{#snippet trigger(tooltip)} {#snippet trigger(tooltip)}