Merge branch 'main' into model-picker

This commit is contained in:
Thomas G. Lopes 2025-06-18 15:29:52 +01:00
commit d2d60a4a46
8 changed files with 35 additions and 2 deletions

View file

@ -59,7 +59,7 @@ TODO: add instructions
### Extra
- [ ] Web Search
- [x] Web Search
- [ ] Chat branching
- [ ] Regenerate message
- [ ] Image generation

View file

@ -131,6 +131,7 @@ export const createAndAddMessage = mutation({
role: args.role,
conversation_id: conversationId,
session_token: args.session_token,
web_search_enabled: args.web_search_enabled,
images: args.images,
});

View file

@ -41,6 +41,7 @@ export const create = mutation({
model_id: v.optional(v.string()),
provider: v.optional(providerValidator),
token_count: v.optional(v.number()),
web_search_enabled: v.optional(v.boolean()),
// Optional image attachments
images: v.optional(
v.array(
@ -84,6 +85,7 @@ export const create = mutation({
model_id: args.model_id,
provider: args.provider,
token_count: args.token_count,
web_search_enabled: args.web_search_enabled,
// Optional image attachments
images: args.images,
}),

View file

@ -70,5 +70,6 @@ export default defineSchema({
),
cost_usd: v.optional(v.number()),
generation_id: v.optional(v.string()),
web_search_enabled: v.optional(v.boolean()),
}).index('by_conversation', ['conversation_id']),
});

View file

@ -2,4 +2,5 @@ import { createPersistedObj } from '$lib/spells/persisted-obj.svelte';
export const settings = createPersistedObj('settings', {
modelId: undefined as string | undefined,
webSearchEnabled: false,
});

View file

@ -21,6 +21,7 @@ const reqBodySchema = z.object({
session_token: z.string(),
conversation_id: z.string().optional(),
web_search_enabled: z.boolean().optional(),
images: z
.array(
z.object({
@ -292,10 +293,16 @@ ${attachedRules.map((r) => `- ${r.name}: ${r.rule}`).join('\n')}`,
return;
}
// Check if web search is enabled for the last user message
const lastUserMessage = messages.filter(m => m.role === 'user').pop();
const webSearchEnabled = lastUserMessage?.web_search_enabled ?? false;
const modelId = webSearchEnabled ? `${model.model_id}:online` : model.model_id;
const streamResult = await ResultAsync.fromPromise(
openai.chat.completions.create(
{
model: model.model_id,
model: modelId,
messages: messagesToSend,
temperature: 0.7,
stream: true,
@ -537,6 +544,7 @@ export const POST: RequestHandler = async ({ request }) => {
content_html: '',
role: 'user',
images: args.images,
web_search_enabled: args.web_search_enabled,
session_token: sessionToken,
}),
(e) => `Failed to create conversation: ${e}`
@ -572,6 +580,7 @@ export const POST: RequestHandler = async ({ request }) => {
model_id: args.model_id,
role: 'user',
images: args.images,
web_search_enabled: args.web_search_enabled,
}),
(e) => `Failed to create user message: ${e}`
);

View file

@ -32,6 +32,7 @@
import Settings2Icon from '~icons/lucide/settings-2';
import UploadIcon from '~icons/lucide/upload';
import XIcon from '~icons/lucide/x';
import SearchIcon from '~icons/lucide/search';
import { callGenerateMessage } from '../api/generate-message/call.js';
import { callCancelGeneration } from '../api/cancel-generation/call.js';
import ModelPicker from './model-picker.svelte';
@ -97,6 +98,7 @@
conversation_id: page.params.id ?? undefined,
model_id: settings.modelId,
images: imagesCopy.length > 0 ? imagesCopy : undefined,
web_search_enabled: settings.webSearchEnabled,
});
if (res.isErr()) {
@ -559,6 +561,17 @@
</div>
<div class="flex flex-col gap-2 pr-2 sm:flex-row sm:items-center">
<ModelPicker onlyImageModels={selectedImages.length > 0} />
<button
type="button"
class={cn(
'border-border flex items-center gap-1 rounded-full border px-2 py-1 text-xs transition-colors',
settings.webSearchEnabled ? 'bg-accent/50' : 'hover:bg-accent/20'
)}
onclick={() => (settings.webSearchEnabled = !settings.webSearchEnabled)}
>
<SearchIcon class="!size-3" />
<span class="whitespace-nowrap">Web search</span>
</button>
{#if currentModelSupportsImages}
<button
type="button"

View file

@ -46,6 +46,12 @@
settings.modelId = lastMessage.model_id;
}
// Auto-enable/disable web search based on last user message
const lastUserMessage = messages.data.filter((m) => m.role === 'user').pop();
if (lastUserMessage) {
settings.webSearchEnabled = Boolean(lastUserMessage.web_search_enabled);
}
changedRoute = false;
});
</script>