feat(EMBEDDINGS-0001): enable local embedder by default and overhaul settings page
- Wire local embedding provider as the default on startup when no profile is configured - Refactor embedding settings into dedicated service, DTOs, mappers and models - Rebuild settings page with profile management UI and live test feedback - Expose index summary (indexed versions + embedding count) on repo endpoints - Harden indexing pipeline and context search with additional test coverage Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
<script lang="ts">
|
||||
import { onDestroy } from 'svelte';
|
||||
import StatBadge from '$lib/components/StatBadge.svelte';
|
||||
import type {
|
||||
EmbeddingProfileDto,
|
||||
EmbeddingSettingsDto,
|
||||
EmbeddingSettingsUpdateDto
|
||||
} from '$lib/dtos/embedding-settings';
|
||||
import type { PageProps } from './$types';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Provider presets
|
||||
@@ -30,11 +37,25 @@
|
||||
// State
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
let provider = $state<'none' | 'openai' | 'local'>('none');
|
||||
let baseUrl = $state('https://api.openai.com/v1');
|
||||
let { data }: PageProps = $props();
|
||||
|
||||
function getInitialSettings(): EmbeddingSettingsDto {
|
||||
return data.settings;
|
||||
}
|
||||
|
||||
function getInitialLocalProviderAvailability(): boolean {
|
||||
return data.localProviderAvailable;
|
||||
}
|
||||
|
||||
let settingsOverride = $state<EmbeddingSettingsDto | null>(null);
|
||||
let provider = $state<'none' | 'openai' | 'local'>(
|
||||
resolveProvider(getInitialSettings().activeProfile)
|
||||
);
|
||||
let baseUrl = $state(resolveBaseUrl(getInitialSettings()));
|
||||
let apiKey = $state('');
|
||||
let model = $state('text-embedding-3-small');
|
||||
let dimensions = $state<number | undefined>(1536);
|
||||
let model = $state(resolveModel(getInitialSettings()));
|
||||
let dimensions = $state<number | undefined>(resolveDimensions(getInitialSettings()));
|
||||
let openaiProfileId = $state(resolveOpenAiProfileId(getInitialSettings()));
|
||||
|
||||
let testStatus = $state<'idle' | 'testing' | 'ok' | 'error'>('idle');
|
||||
let testError = $state<string | null>(null);
|
||||
@@ -45,51 +66,12 @@
|
||||
let saveError = $state<string | null>(null);
|
||||
let saveStatusTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
let localAvailable = $state<boolean | null>(null);
|
||||
let loading = $state(true);
|
||||
const currentSettings = $derived(settingsOverride ?? data.settings);
|
||||
const activeProfile = $derived(currentSettings.activeProfile);
|
||||
const activeConfigEntries = $derived(activeProfile?.configEntries ?? []);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Load current config + probe local provider on mount
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
$effect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/v1/settings/embedding');
|
||||
if (!cancelled && res.ok) {
|
||||
const data = await res.json();
|
||||
provider = data.provider ?? 'none';
|
||||
if (data.openai) {
|
||||
baseUrl = data.openai.baseUrl ?? baseUrl;
|
||||
model = data.openai.model ?? model;
|
||||
dimensions = data.openai.dimensions ?? dimensions;
|
||||
// apiKey is intentionally not returned by the server; leave blank
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Non-fatal — fall back to defaults
|
||||
} finally {
|
||||
if (!cancelled) loading = false;
|
||||
}
|
||||
|
||||
// Probe whether the local provider is available
|
||||
try {
|
||||
const res = await fetch('/api/v1/settings/embedding/test');
|
||||
if (!cancelled && res.ok) {
|
||||
const data = await res.json();
|
||||
localAvailable = data.available ?? false;
|
||||
}
|
||||
} catch {
|
||||
if (!cancelled) localAvailable = false;
|
||||
}
|
||||
})();
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
if (saveStatusTimer) clearTimeout(saveStatusTimer);
|
||||
};
|
||||
onDestroy(() => {
|
||||
if (saveStatusTimer) clearTimeout(saveStatusTimer);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -106,6 +88,10 @@
|
||||
}
|
||||
|
||||
async function testConnection() {
|
||||
if (provider !== 'openai') {
|
||||
return;
|
||||
}
|
||||
|
||||
testStatus = 'testing';
|
||||
testError = null;
|
||||
testDimensions = null;
|
||||
@@ -113,7 +99,14 @@
|
||||
const res = await fetch('/api/v1/settings/embedding/test', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ provider, openai: { baseUrl, apiKey, model, dimensions } })
|
||||
body: JSON.stringify({
|
||||
id: openaiProfileId,
|
||||
title: 'OpenAI-compatible',
|
||||
providerKind: 'openai-compatible',
|
||||
model,
|
||||
dimensions: dimensions ?? 1536,
|
||||
config: { baseUrl, apiKey, model, ...(dimensions ? { dimensions } : {}) }
|
||||
})
|
||||
});
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
@@ -138,9 +131,10 @@
|
||||
const res = await fetch('/api/v1/settings/embedding', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ provider, openai: { baseUrl, apiKey, model, dimensions } })
|
||||
body: JSON.stringify(buildSaveRequest())
|
||||
});
|
||||
if (res.ok) {
|
||||
settingsOverride = (await res.json()) as EmbeddingSettingsDto;
|
||||
saveStatus = 'ok';
|
||||
if (saveStatusTimer) clearTimeout(saveStatusTimer);
|
||||
saveStatusTimer = setTimeout(() => {
|
||||
@@ -164,6 +158,74 @@
|
||||
event.preventDefault();
|
||||
void save();
|
||||
}
|
||||
|
||||
function getOpenAiProfile(settings: EmbeddingSettingsDto): EmbeddingProfileDto | null {
|
||||
return settings.profiles.find((profile) => profile.providerKind === 'openai-compatible') ?? null;
|
||||
}
|
||||
|
||||
function resolveProvider(profile: EmbeddingProfileDto | null): 'none' | 'openai' | 'local' {
|
||||
if (!profile) return 'none';
|
||||
if (profile.providerKind === 'local-transformers') return 'local';
|
||||
if (profile.providerKind === 'openai-compatible') return 'openai';
|
||||
return 'none';
|
||||
}
|
||||
|
||||
function resolveBaseUrl(settings: EmbeddingSettingsDto): string {
|
||||
const profile = settings.activeProfile?.providerKind === 'openai-compatible'
|
||||
? settings.activeProfile
|
||||
: getOpenAiProfile(settings);
|
||||
return typeof profile?.config.baseUrl === 'string'
|
||||
? profile.config.baseUrl
|
||||
: 'https://api.openai.com/v1';
|
||||
}
|
||||
|
||||
function resolveModel(settings: EmbeddingSettingsDto): string {
|
||||
const profile = settings.activeProfile?.providerKind === 'openai-compatible'
|
||||
? settings.activeProfile
|
||||
: getOpenAiProfile(settings);
|
||||
return typeof profile?.config.model === 'string'
|
||||
? profile.config.model
|
||||
: profile?.model ?? 'text-embedding-3-small';
|
||||
}
|
||||
|
||||
function resolveDimensions(settings: EmbeddingSettingsDto): number | undefined {
|
||||
const profile = settings.activeProfile?.providerKind === 'openai-compatible'
|
||||
? settings.activeProfile
|
||||
: getOpenAiProfile(settings);
|
||||
return profile?.dimensions ?? 1536;
|
||||
}
|
||||
|
||||
function resolveOpenAiProfileId(settings: EmbeddingSettingsDto): string {
|
||||
const profile = getOpenAiProfile(settings);
|
||||
return profile?.id ?? 'openai-default';
|
||||
}
|
||||
|
||||
function buildSaveRequest(): EmbeddingSettingsUpdateDto {
|
||||
if (provider === 'none') {
|
||||
return { activeProfileId: null };
|
||||
}
|
||||
|
||||
if (provider === 'local') {
|
||||
return { activeProfileId: 'local-default' };
|
||||
}
|
||||
|
||||
return {
|
||||
activeProfileId: openaiProfileId,
|
||||
profile: {
|
||||
id: openaiProfileId,
|
||||
providerKind: 'openai-compatible',
|
||||
title: 'OpenAI-compatible',
|
||||
model,
|
||||
dimensions: dimensions ?? 1536,
|
||||
config: { baseUrl, apiKey, model, ...(dimensions ? { dimensions } : {}) }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function formatTimestamp(timestamp: number): string {
|
||||
const normalizedTimestamp = timestamp > 1_000_000_000_000 ? timestamp : timestamp * 1000;
|
||||
return new Date(normalizedTimestamp).toLocaleString();
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -175,17 +237,109 @@
|
||||
<p class="mt-0.5 text-sm text-gray-500">Configure TrueRef embedding and indexing options</p>
|
||||
</div>
|
||||
|
||||
<!-- Embedding Provider Card -->
|
||||
<div class="mb-4 grid gap-4 lg:grid-cols-[1.2fr_0.8fr]">
|
||||
<div class="rounded-xl border border-gray-200 bg-white p-6">
|
||||
<h2 class="mb-1 text-base font-semibold text-gray-900">Current Active Profile</h2>
|
||||
<p class="mb-4 text-sm text-gray-500">
|
||||
This is the profile used for semantic indexing and retrieval right now.
|
||||
</p>
|
||||
|
||||
{#if activeProfile}
|
||||
<div class="grid gap-4 md:grid-cols-2">
|
||||
<div class="space-y-4">
|
||||
<div>
|
||||
<p class="text-lg font-semibold text-gray-900">{activeProfile.title}</p>
|
||||
<p class="mt-1 text-sm text-gray-500">Profile ID: {activeProfile.id}</p>
|
||||
</div>
|
||||
|
||||
<dl class="rounded-lg border border-gray-200 bg-gray-50 p-4 text-sm">
|
||||
<div class="grid grid-cols-[110px_1fr] gap-x-4 gap-y-1 border-b border-gray-200 pb-3">
|
||||
<dt class="font-medium text-gray-500">Provider</dt>
|
||||
<dd class="font-semibold text-gray-900">{activeProfile.providerKind}</dd>
|
||||
<dt class="font-medium text-gray-500">Model</dt>
|
||||
<dd class="break-all font-semibold text-gray-900">{activeProfile.model}</dd>
|
||||
<dt class="font-medium text-gray-500">Dimensions</dt>
|
||||
<dd class="font-semibold text-gray-900">{activeProfile.dimensions}</dd>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-[110px_1fr] gap-x-4 gap-y-2 pt-3">
|
||||
<dt class="text-gray-500">Enabled</dt>
|
||||
<dd class="font-medium text-gray-800">{activeProfile.enabled ? 'Yes' : 'No'}</dd>
|
||||
<dt class="text-gray-500">Default</dt>
|
||||
<dd class="font-medium text-gray-800">{activeProfile.isDefault ? 'Yes' : 'No'}</dd>
|
||||
<dt class="text-gray-500">Updated</dt>
|
||||
<dd class="font-medium text-gray-800">{formatTimestamp(activeProfile.updatedAt)}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg border border-gray-200 bg-gray-50 p-4">
|
||||
<p class="text-sm font-medium text-gray-800">Provider configuration</p>
|
||||
<p class="mb-3 mt-1 text-sm text-gray-500">
|
||||
These are the provider-specific settings currently saved for the active profile.
|
||||
</p>
|
||||
|
||||
{#if activeConfigEntries.length > 0}
|
||||
<ul class="space-y-2 text-sm">
|
||||
{#each activeConfigEntries as entry (entry.key)}
|
||||
<li class="flex items-start justify-between gap-4 border-b border-gray-200 pb-2 last:border-b-0 last:pb-0">
|
||||
<span class="font-medium text-gray-600">{entry.key}</span>
|
||||
<span class={entry.redacted ? 'text-gray-500' : 'text-gray-800'}>{entry.value}</span>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{:else}
|
||||
<p class="text-sm text-gray-500">
|
||||
No provider-specific configuration is stored for this profile.
|
||||
</p>
|
||||
<p class="mt-2 text-sm text-gray-500">
|
||||
For <span class="font-medium text-gray-700">OpenAI-compatible</span> profiles, edit the
|
||||
settings in the <span class="font-medium text-gray-700">Embedding Provider</span> form
|
||||
below. The built-in <span class="font-medium text-gray-700">Local Model</span> profile
|
||||
does not currently expose extra configurable fields.
|
||||
</p>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="rounded-lg border border-amber-200 bg-amber-50 p-4 text-sm text-amber-800">
|
||||
Embeddings are currently disabled. Keyword search remains available, but no embedding profile is active.
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl border border-gray-200 bg-white p-6">
|
||||
<h2 class="mb-1 text-base font-semibold text-gray-900">Profile Inventory</h2>
|
||||
<p class="mb-4 text-sm text-gray-500">Profiles stored in the database and available for activation.</p>
|
||||
<div class="grid grid-cols-2 gap-3">
|
||||
<StatBadge label="Profiles" value={String(currentSettings.profiles.length)} />
|
||||
<StatBadge label="Active" value={activeProfile ? '1' : '0'} />
|
||||
</div>
|
||||
<div class="mt-4 space-y-2">
|
||||
{#each currentSettings.profiles as profile (profile.id)}
|
||||
<div class="rounded-lg border border-gray-200 px-3 py-2 text-sm">
|
||||
<div class="flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<p class="font-medium text-gray-900">{profile.title}</p>
|
||||
<p class="text-gray-500">{profile.id}</p>
|
||||
</div>
|
||||
{#if profile.id === currentSettings.activeProfileId}
|
||||
<span class="rounded-full bg-blue-50 px-2 py-0.5 text-xs font-medium text-blue-700">Active</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl border border-gray-200 bg-white p-6">
|
||||
<h2 class="mb-1 text-base font-semibold text-gray-900">Embedding Provider</h2>
|
||||
<p class="mb-4 text-sm text-gray-500">
|
||||
Embeddings enable semantic search. Without them, only keyword search (FTS5) is used.
|
||||
</p>
|
||||
|
||||
{#if loading}
|
||||
<p class="text-sm text-gray-400">Loading current configuration…</p>
|
||||
{:else}
|
||||
<form class="space-y-4" onsubmit={handleSubmit}>
|
||||
<form class="space-y-4" onsubmit={handleSubmit}>
|
||||
<!-- Provider selector -->
|
||||
<div class="mb-4 flex gap-2">
|
||||
{#each ['none', 'openai', 'local'] as p (p)}
|
||||
@@ -314,9 +468,7 @@
|
||||
<div class="rounded-lg border border-gray-200 bg-gray-50 p-4 text-sm">
|
||||
<p class="font-medium text-gray-800">Local ONNX model via @xenova/transformers</p>
|
||||
<p class="mt-1 text-gray-500">Model: Xenova/all-MiniLM-L6-v2 · 384 dimensions</p>
|
||||
{#if localAvailable === null}
|
||||
<p class="mt-2 text-gray-400">Checking availability…</p>
|
||||
{:else if localAvailable}
|
||||
{#if getInitialLocalProviderAvailability()}
|
||||
<p class="mt-2 text-green-600">@xenova/transformers is installed and ready.</p>
|
||||
{:else}
|
||||
<p class="mt-2 text-amber-700">
|
||||
@@ -381,8 +533,7 @@
|
||||
{saving ? 'Saving…' : 'Save Settings'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
{/if}
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- About card -->
|
||||
|
||||
Reference in New Issue
Block a user