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,30 +1,25 @@
|
||||
/**
|
||||
* GET /api/v1/settings/embedding — retrieve all embedding profiles
|
||||
* POST /api/v1/settings/embedding — create or update an embedding profile
|
||||
* PUT /api/v1/settings/embedding — alias for POST (backward compat)
|
||||
* GET /api/v1/settings/embedding — retrieve embedding settings
|
||||
* POST /api/v1/settings/embedding — update active embedding settings
|
||||
* PUT /api/v1/settings/embedding — alias for POST
|
||||
*/
|
||||
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import type { EmbeddingSettingsUpdateDto } from '$lib/dtos/embedding-settings.js';
|
||||
import { getClient } from '$lib/server/db/client';
|
||||
import { createProviderFromProfile } from '$lib/server/embeddings/registry';
|
||||
import type { EmbeddingProfile, NewEmbeddingProfile } from '$lib/server/db/schema';
|
||||
import { EmbeddingSettingsDtoMapper } from '$lib/server/mappers/embedding-settings.dto.mapper.js';
|
||||
import { EmbeddingSettingsService } from '$lib/server/services/embedding-settings.service.js';
|
||||
import { handleServiceError, InvalidInputError } from '$lib/server/utils/validation';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// GET — Return all profiles
|
||||
// GET — Return embedding settings
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export const GET: RequestHandler = () => {
|
||||
try {
|
||||
const db = getClient();
|
||||
const profiles = db
|
||||
.prepare('SELECT * FROM embedding_profiles ORDER BY is_default DESC, created_at ASC')
|
||||
.all() as EmbeddingProfile[];
|
||||
|
||||
// Sanitize: remove sensitive config fields like apiKey
|
||||
const safeProfiles = profiles.map(sanitizeProfile);
|
||||
return json({ profiles: safeProfiles });
|
||||
const service = new EmbeddingSettingsService(getClient());
|
||||
return json(EmbeddingSettingsDtoMapper.toDto(service.getSettings()));
|
||||
} catch (err) {
|
||||
return handleServiceError(err);
|
||||
}
|
||||
@@ -34,116 +29,23 @@ export const GET: RequestHandler = () => {
|
||||
// POST/PUT — Create or update a profile
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function upsertProfile(body: unknown) {
|
||||
async function upsertSettings(body: unknown) {
|
||||
if (typeof body !== 'object' || body === null) {
|
||||
throw new InvalidInputError('Request body must be a JSON object');
|
||||
}
|
||||
|
||||
const obj = body as Record<string, unknown>;
|
||||
|
||||
// Required fields
|
||||
if (typeof obj.id !== 'string' || !obj.id) {
|
||||
throw new InvalidInputError('id is required');
|
||||
}
|
||||
if (typeof obj.providerKind !== 'string' || !obj.providerKind) {
|
||||
throw new InvalidInputError('providerKind is required');
|
||||
}
|
||||
if (typeof obj.title !== 'string' || !obj.title) {
|
||||
throw new InvalidInputError('title is required');
|
||||
}
|
||||
if (typeof obj.model !== 'string' || !obj.model) {
|
||||
throw new InvalidInputError('model is required');
|
||||
}
|
||||
if (typeof obj.dimensions !== 'number') {
|
||||
throw new InvalidInputError('dimensions must be a number');
|
||||
}
|
||||
|
||||
const profile: NewEmbeddingProfile = {
|
||||
id: obj.id,
|
||||
providerKind: obj.providerKind,
|
||||
title: obj.title,
|
||||
enabled: typeof obj.enabled === 'boolean' ? obj.enabled : true,
|
||||
isDefault: typeof obj.isDefault === 'boolean' ? obj.isDefault : false,
|
||||
model: obj.model,
|
||||
dimensions: obj.dimensions,
|
||||
config: (obj.config as Record<string, unknown>) ?? {},
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
|
||||
// Validate provider availability before persisting
|
||||
const provider = createProviderFromProfile(profile as EmbeddingProfile);
|
||||
const available = await provider.isAvailable();
|
||||
if (!available) {
|
||||
throw new InvalidInputError(
|
||||
`Could not connect to the "${profile.providerKind}" provider. Check your configuration.`
|
||||
);
|
||||
}
|
||||
|
||||
const db = getClient();
|
||||
|
||||
// If setting as default, clear other defaults first
|
||||
if (profile.isDefault) {
|
||||
db.prepare('UPDATE embedding_profiles SET is_default = 0').run();
|
||||
}
|
||||
|
||||
// Upsert the profile
|
||||
db.prepare(
|
||||
`INSERT INTO embedding_profiles
|
||||
(id, provider_kind, title, enabled, is_default, model, dimensions, config, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
provider_kind = excluded.provider_kind,
|
||||
title = excluded.title,
|
||||
enabled = excluded.enabled,
|
||||
is_default = excluded.is_default,
|
||||
model = excluded.model,
|
||||
dimensions = excluded.dimensions,
|
||||
config = excluded.config,
|
||||
updated_at = excluded.updated_at`
|
||||
).run(
|
||||
profile.id,
|
||||
profile.providerKind,
|
||||
profile.title,
|
||||
profile.enabled ? 1 : 0,
|
||||
profile.isDefault ? 1 : 0,
|
||||
profile.model,
|
||||
profile.dimensions,
|
||||
JSON.stringify(profile.config),
|
||||
profile.createdAt,
|
||||
profile.updatedAt
|
||||
);
|
||||
|
||||
const inserted = db
|
||||
.prepare('SELECT * FROM embedding_profiles WHERE id = ?')
|
||||
.get(profile.id) as EmbeddingProfile;
|
||||
|
||||
return sanitizeProfile(inserted);
|
||||
const service = new EmbeddingSettingsService(getClient());
|
||||
const settings = await service.updateSettings(body as EmbeddingSettingsUpdateDto);
|
||||
return EmbeddingSettingsDtoMapper.toDto(settings);
|
||||
}
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const profile = await upsertProfile(body);
|
||||
return json(profile);
|
||||
return json(await upsertSettings(body));
|
||||
} catch (err) {
|
||||
return handleServiceError(err);
|
||||
}
|
||||
};
|
||||
|
||||
// Backward compat alias
|
||||
export const PUT: RequestHandler = POST;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sanitize — remove sensitive config fields before returning to clients
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function sanitizeProfile(profile: EmbeddingProfile): EmbeddingProfile {
|
||||
const config = profile.config as Record<string, unknown>;
|
||||
if (config && config.apiKey) {
|
||||
const rest = { ...config };
|
||||
delete rest.apiKey;
|
||||
return { ...profile, config: rest };
|
||||
}
|
||||
return profile;
|
||||
}
|
||||
|
||||
183
src/routes/api/v1/settings/embedding/server.test.ts
Normal file
183
src/routes/api/v1/settings/embedding/server.test.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import Database from 'better-sqlite3';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
|
||||
let db: Database.Database;
|
||||
|
||||
vi.mock('$lib/server/db/client', () => ({
|
||||
getClient: () => db
|
||||
}));
|
||||
|
||||
vi.mock('$lib/server/db/client.js', () => ({
|
||||
getClient: () => db
|
||||
}));
|
||||
|
||||
vi.mock('$lib/server/embeddings/registry', () => ({
|
||||
createProviderFromProfile: () => ({
|
||||
isAvailable: async () => true
|
||||
})
|
||||
}));
|
||||
|
||||
vi.mock('$lib/server/embeddings/registry.js', () => ({
|
||||
createProviderFromProfile: () => ({
|
||||
isAvailable: async () => true
|
||||
})
|
||||
}));
|
||||
|
||||
vi.mock('$lib/server/embeddings/local.provider', () => ({
|
||||
LocalEmbeddingProvider: class {
|
||||
readonly model = 'Xenova/all-MiniLM-L6-v2';
|
||||
readonly dimensions = 384;
|
||||
|
||||
async isAvailable() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
vi.mock('$lib/server/embeddings/local.provider.js', () => ({
|
||||
LocalEmbeddingProvider: class {
|
||||
readonly model = 'Xenova/all-MiniLM-L6-v2';
|
||||
readonly dimensions = 384;
|
||||
|
||||
async isAvailable() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
import { GET as getEmbeddingSettings, PUT as putEmbeddingSettings } from './+server.js';
|
||||
import { GET as getEmbeddingTest } from './test/+server.js';
|
||||
|
||||
function createTestDb(): Database.Database {
|
||||
const client = new Database(':memory:');
|
||||
client.pragma('foreign_keys = ON');
|
||||
|
||||
const migrationsFolder = join(import.meta.dirname, '../../../../../lib/server/db/migrations');
|
||||
const ftsFile = join(import.meta.dirname, '../../../../../lib/server/db/fts.sql');
|
||||
|
||||
for (const migration of [
|
||||
'0000_large_master_chief.sql',
|
||||
'0001_quick_nighthawk.sql',
|
||||
'0002_silky_stellaris.sql'
|
||||
]) {
|
||||
const statements = readFileSync(join(migrationsFolder, migration), 'utf-8')
|
||||
.split('--> statement-breakpoint')
|
||||
.map((statement) => statement.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
for (const statement of statements) {
|
||||
client.exec(statement);
|
||||
}
|
||||
}
|
||||
|
||||
client.exec(readFileSync(ftsFile, 'utf-8'));
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
describe('embedding settings routes', () => {
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
});
|
||||
|
||||
it('GET /api/v1/settings/embedding returns profile-based settings for the seeded default profile', async () => {
|
||||
const response = await getEmbeddingSettings({} as never);
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const body = await response.json();
|
||||
expect(body.activeProfileId).toBe('local-default');
|
||||
expect(body.activeProfile).toMatchObject({
|
||||
id: 'local-default',
|
||||
providerKind: 'local-transformers',
|
||||
title: 'Local (Xenova/all-MiniLM-L6-v2)'
|
||||
});
|
||||
expect(body.profiles).toHaveLength(1);
|
||||
expect(body.profiles[0].providerKind).toBe('local-transformers');
|
||||
expect(body.profiles[0].isDefault).toBe(true);
|
||||
});
|
||||
|
||||
it('PUT /api/v1/settings/embedding persists a clean profile-based OpenAI payload', async () => {
|
||||
const response = await putEmbeddingSettings({
|
||||
request: new Request('http://test/api/v1/settings/embedding', {
|
||||
method: 'PUT',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
activeProfileId: 'openai-default',
|
||||
profile: {
|
||||
id: 'openai-default',
|
||||
providerKind: 'openai-compatible',
|
||||
title: 'OpenAI-compatible',
|
||||
model: 'text-embedding-3-small',
|
||||
dimensions: 1536,
|
||||
config: {
|
||||
baseUrl: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-test',
|
||||
model: 'text-embedding-3-small'
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
} as never);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
const body = await response.json();
|
||||
expect(body.activeProfileId).toBe('openai-default');
|
||||
expect(body.activeProfile).toMatchObject({
|
||||
id: 'openai-default',
|
||||
providerKind: 'openai-compatible'
|
||||
});
|
||||
expect(body.activeProfile.config).toEqual({
|
||||
baseUrl: 'https://api.openai.com/v1',
|
||||
model: 'text-embedding-3-small'
|
||||
});
|
||||
expect(body.activeProfile.configEntries).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ key: 'apiKey', value: '[redacted]', redacted: true })
|
||||
])
|
||||
);
|
||||
expect(body.profiles).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: 'openai-default',
|
||||
providerKind: 'openai-compatible',
|
||||
model: 'text-embedding-3-small',
|
||||
dimensions: 1536,
|
||||
isDefault: true
|
||||
})
|
||||
])
|
||||
);
|
||||
|
||||
const activeProfile = db
|
||||
.prepare(
|
||||
'SELECT id, provider_kind, is_default, enabled, model, dimensions FROM embedding_profiles WHERE is_default = 1 LIMIT 1'
|
||||
)
|
||||
.get() as Record<string, unknown>;
|
||||
|
||||
expect(activeProfile).toMatchObject({
|
||||
id: 'openai-default',
|
||||
provider_kind: 'openai-compatible',
|
||||
is_default: 1,
|
||||
enabled: 1,
|
||||
model: 'text-embedding-3-small',
|
||||
dimensions: 1536
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /api/v1/settings/embedding/test checks local-provider availability directly', async () => {
|
||||
const response = await getEmbeddingTest({} as never);
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const body = await response.json();
|
||||
expect(body).toEqual({
|
||||
available: true,
|
||||
profile: {
|
||||
id: 'local-default',
|
||||
providerKind: 'local-transformers',
|
||||
model: 'Xenova/all-MiniLM-L6-v2',
|
||||
dimensions: 384
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -7,35 +7,24 @@
|
||||
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getClient } from '$lib/server/db/client';
|
||||
import { LocalEmbeddingProvider } from '$lib/server/embeddings/local.provider';
|
||||
import { createProviderFromProfile } from '$lib/server/embeddings/registry';
|
||||
import type { EmbeddingProfile } from '$lib/server/db/schema';
|
||||
import { EmbeddingProfileEntity } from '$lib/server/models/embedding-profile';
|
||||
import { EmbeddingProfileMapper } from '$lib/server/mappers/embedding-profile.mapper';
|
||||
import { handleServiceError } from '$lib/server/utils/validation';
|
||||
|
||||
export const GET: RequestHandler = async () => {
|
||||
try {
|
||||
const db = getClient();
|
||||
const profile = db
|
||||
.prepare<
|
||||
[],
|
||||
EmbeddingProfile
|
||||
>('SELECT * FROM embedding_profiles WHERE is_default = 1 AND enabled = 1 LIMIT 1')
|
||||
.get();
|
||||
|
||||
if (!profile) {
|
||||
return json({ available: false, error: 'No active embedding profile configured' });
|
||||
}
|
||||
|
||||
const provider = createProviderFromProfile(profile);
|
||||
const provider = new LocalEmbeddingProvider();
|
||||
const available = await provider.isAvailable();
|
||||
|
||||
return json({
|
||||
available,
|
||||
profile: {
|
||||
id: profile.id,
|
||||
providerKind: profile.providerKind,
|
||||
model: profile.model,
|
||||
dimensions: profile.dimensions
|
||||
id: 'local-default',
|
||||
providerKind: 'local-transformers',
|
||||
model: provider.model,
|
||||
dimensions: provider.dimensions
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -46,19 +35,43 @@ export const GET: RequestHandler = async () => {
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const config = validateConfig(body);
|
||||
|
||||
if (config.provider === 'none') {
|
||||
throw new InvalidInputError('Cannot test the "none" provider — no backend is configured.');
|
||||
if (typeof body !== 'object' || body === null) {
|
||||
throw new Error('Request body must be a JSON object');
|
||||
}
|
||||
|
||||
const provider = createProviderFromConfig(config);
|
||||
const candidate = body as Record<string, unknown>;
|
||||
if (candidate.providerKind !== 'openai-compatible') {
|
||||
throw new Error('Only openai-compatible providers can be tested via this endpoint');
|
||||
}
|
||||
if (typeof candidate.model !== 'string' || typeof candidate.dimensions !== 'number') {
|
||||
throw new Error('model and dimensions are required');
|
||||
}
|
||||
|
||||
const provider = createProviderFromProfile(
|
||||
EmbeddingProfileMapper.fromEntity(
|
||||
new EmbeddingProfileEntity({
|
||||
id: typeof candidate.id === 'string' ? candidate.id : 'test-openai-profile',
|
||||
provider_kind: 'openai-compatible',
|
||||
title: typeof candidate.title === 'string' ? candidate.title : 'Test Provider',
|
||||
enabled: true,
|
||||
is_default: false,
|
||||
model: candidate.model,
|
||||
dimensions: candidate.dimensions,
|
||||
config:
|
||||
typeof candidate.config === 'object' && candidate.config !== null
|
||||
? (candidate.config as Record<string, unknown>)
|
||||
: {},
|
||||
created_at: Date.now(),
|
||||
updated_at: Date.now()
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
const available = await provider.isAvailable();
|
||||
if (!available) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: `Provider "${config.provider}" is not available. Check your configuration.`
|
||||
error: 'Provider is not available. Check your configuration.'
|
||||
}),
|
||||
{ status: 400, headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user