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:
@@ -34,6 +34,7 @@ vi.mock('$lib/server/embeddings/registry.js', () => ({
|
||||
}));
|
||||
|
||||
import { POST as postLibraries } from './libs/+server.js';
|
||||
import { GET as getLibraries } from './libs/+server.js';
|
||||
import { GET as getLibrary } from './libs/[id]/+server.js';
|
||||
import { GET as getJobs } from './jobs/+server.js';
|
||||
import { GET as getJob } from './jobs/[id]/+server.js';
|
||||
@@ -186,6 +187,16 @@ function seedSnippet(
|
||||
return snippetId;
|
||||
}
|
||||
|
||||
function seedEmbedding(client: Database.Database, snippetId: string, values: number[]): void {
|
||||
client
|
||||
.prepare(
|
||||
`INSERT INTO snippet_embeddings
|
||||
(snippet_id, profile_id, model, dimensions, embedding, created_at)
|
||||
VALUES (?, 'local-default', 'Xenova/all-MiniLM-L6-v2', ?, ?, ?)`
|
||||
)
|
||||
.run(snippetId, values.length, Buffer.from(Float32Array.from(values).buffer), NOW_S);
|
||||
}
|
||||
|
||||
function seedRules(client: Database.Database, repositoryId: string, rules: string[]) {
|
||||
client
|
||||
.prepare(
|
||||
@@ -249,6 +260,36 @@ describe('API contract integration', () => {
|
||||
expect(body).not.toHaveProperty('total_snippets');
|
||||
});
|
||||
|
||||
it('GET /api/v1/libs includes embedding counts and indexed versions per repository', async () => {
|
||||
const repositoryId = seedRepo(db);
|
||||
const versionId = seedVersion(db, repositoryId, 'v18.3.0');
|
||||
const baseDocId = seedDocument(db, repositoryId);
|
||||
const versionDocId = seedDocument(db, repositoryId, versionId);
|
||||
const baseSnippetId = seedSnippet(db, {
|
||||
documentId: baseDocId,
|
||||
repositoryId,
|
||||
content: 'Base branch snippet'
|
||||
});
|
||||
const versionSnippetId = seedSnippet(db, {
|
||||
documentId: versionDocId,
|
||||
repositoryId,
|
||||
versionId,
|
||||
content: 'Versioned snippet'
|
||||
});
|
||||
seedEmbedding(db, baseSnippetId, [1, 0]);
|
||||
seedEmbedding(db, versionSnippetId, [0, 1]);
|
||||
|
||||
const response = await getLibraries({
|
||||
url: new URL('http://test/api/v1/libs')
|
||||
} as never);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
const body = await response.json();
|
||||
expect(body.libraries).toHaveLength(1);
|
||||
expect(body.libraries[0].embeddingCount).toBe(2);
|
||||
expect(body.libraries[0].indexedVersions).toEqual(['main', 'v18.3.0']);
|
||||
});
|
||||
|
||||
it('GET /api/v1/jobs and /api/v1/jobs/:id return job DTOs in camelCase', async () => {
|
||||
const repoService = new RepositoryService(db);
|
||||
repoService.add({ source: 'github', sourceUrl: 'https://github.com/facebook/react' });
|
||||
|
||||
@@ -17,7 +17,11 @@ import { dtoJsonResponse } from '$lib/server/api/dto-response';
|
||||
import { SearchService } from '$lib/server/search/search.service';
|
||||
import { HybridSearchService } from '$lib/server/search/hybrid.search.service';
|
||||
import { createProviderFromProfile } from '$lib/server/embeddings/registry';
|
||||
import type { EmbeddingProfile } from '$lib/server/db/schema';
|
||||
import {
|
||||
EmbeddingProfileEntity,
|
||||
type EmbeddingProfileEntityProps
|
||||
} from '$lib/server/models/embedding-profile';
|
||||
import { EmbeddingProfileMapper } from '$lib/server/mappers/embedding-profile.mapper';
|
||||
import { parseLibraryId } from '$lib/server/api/library-id';
|
||||
import { selectSnippetsWithinBudget, DEFAULT_TOKEN_BUDGET } from '$lib/server/api/token-budget';
|
||||
import { formatContextJson, formatContextTxt, CORS_HEADERS } from '$lib/server/api/formatters';
|
||||
@@ -32,16 +36,18 @@ function getServices(db: ReturnType<typeof getClient>) {
|
||||
|
||||
// Load the active embedding profile from the database
|
||||
const profileRow = db
|
||||
.prepare<
|
||||
[],
|
||||
EmbeddingProfile
|
||||
>('SELECT * FROM embedding_profiles WHERE is_default = 1 AND enabled = 1 LIMIT 1')
|
||||
.prepare<[], EmbeddingProfileEntityProps>(
|
||||
'SELECT * FROM embedding_profiles WHERE is_default = 1 AND enabled = 1 LIMIT 1'
|
||||
)
|
||||
.get();
|
||||
|
||||
const provider = profileRow ? createProviderFromProfile(profileRow) : null;
|
||||
const profile = profileRow
|
||||
? EmbeddingProfileMapper.fromEntity(new EmbeddingProfileEntity(profileRow))
|
||||
: null;
|
||||
const provider = profile ? createProviderFromProfile(profile) : null;
|
||||
const hybridService = new HybridSearchService(db, searchService, provider);
|
||||
|
||||
return { db, searchService, hybridService, profileId: profileRow?.id };
|
||||
return { db, searchService, hybridService, profileId: profile?.id };
|
||||
}
|
||||
|
||||
interface RawRepoConfig {
|
||||
|
||||
@@ -32,7 +32,8 @@ export const GET: RequestHandler = ({ url }) => {
|
||||
|
||||
const enriched = libraries.map((repo) => ({
|
||||
...RepositoryMapper.toDto(repo),
|
||||
versions: service.getVersions(repo.id)
|
||||
versions: service.getVersions(repo.id),
|
||||
...service.getIndexSummary(repo.id)
|
||||
}));
|
||||
|
||||
return json({ libraries: enriched, total, limit, offset });
|
||||
|
||||
@@ -23,7 +23,7 @@ export const GET: RequestHandler = ({ params }) => {
|
||||
return json({ error: 'Repository not found', code: 'NOT_FOUND' }, { status: 404 });
|
||||
}
|
||||
const versions = service.getVersions(id);
|
||||
return json({ ...RepositoryMapper.toDto(repo), versions });
|
||||
return json({ ...RepositoryMapper.toDto(repo), versions, ...service.getIndexSummary(id) });
|
||||
} catch (err) {
|
||||
return handleServiceError(err);
|
||||
}
|
||||
|
||||
@@ -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' } }
|
||||
);
|
||||
|
||||
@@ -2,22 +2,24 @@
|
||||
import { goto } from '$app/navigation';
|
||||
import { resolve as resolveRoute } from '$app/paths';
|
||||
import type { PageData } from './$types';
|
||||
import type { Repository, RepositoryVersion, IndexingJob } from '$lib/types';
|
||||
import type { Repository, IndexingJob } from '$lib/types';
|
||||
import ConfirmDialog from '$lib/components/ConfirmDialog.svelte';
|
||||
import IndexingProgress from '$lib/components/IndexingProgress.svelte';
|
||||
import StatBadge from '$lib/components/StatBadge.svelte';
|
||||
|
||||
let { data }: { data: PageData } = $props();
|
||||
|
||||
// Initialized empty; $effect syncs from data prop on every navigation/reload.
|
||||
let repo = $state<Repository & { versions?: RepositoryVersion[] }>(
|
||||
{} as Repository & { versions?: RepositoryVersion[] }
|
||||
let repoOverride = $state<
|
||||
(Repository & { indexedVersions?: string[]; embeddingCount?: number }) | null
|
||||
>(null);
|
||||
const repo = $derived(
|
||||
repoOverride ??
|
||||
((data.repo ?? {}) as Repository & {
|
||||
indexedVersions?: string[];
|
||||
embeddingCount?: number;
|
||||
})
|
||||
);
|
||||
let recentJobs = $state<IndexingJob[]>([]);
|
||||
$effect(() => {
|
||||
if (data.repo) repo = data.repo;
|
||||
recentJobs = data.recentJobs ?? [];
|
||||
});
|
||||
const recentJobs = $derived((data.recentJobs ?? []) as IndexingJob[]);
|
||||
let showDeleteConfirm = $state(false);
|
||||
let activeJobId = $state<string | null>(null);
|
||||
let errorMessage = $state<string | null>(null);
|
||||
@@ -41,7 +43,7 @@
|
||||
try {
|
||||
const res = await fetch(`/api/v1/libs/${encodeURIComponent(repo.id)}`);
|
||||
if (res.ok) {
|
||||
repo = await res.json();
|
||||
repoOverride = await res.json();
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
@@ -92,7 +94,8 @@
|
||||
return new Date(ts as string).toLocaleString();
|
||||
}
|
||||
|
||||
const versions = $derived(repo.versions ?? []);
|
||||
const indexedVersions = $derived(repo.indexedVersions ?? []);
|
||||
const embeddingCount = $derived(repo.embeddingCount ?? 0);
|
||||
const totalSnippets = $derived(repo.totalSnippets ?? 0);
|
||||
const totalTokens = $derived(repo.totalTokens ?? 0);
|
||||
const trustScore = $derived(repo.trustScore ?? 0);
|
||||
@@ -180,6 +183,7 @@
|
||||
<!-- Stats -->
|
||||
<div class="mt-6 grid grid-cols-2 gap-3 sm:grid-cols-4">
|
||||
<StatBadge label="Snippets" value={totalSnippets.toLocaleString()} />
|
||||
<StatBadge label="Embeddings" value={embeddingCount.toLocaleString()} />
|
||||
<StatBadge label="Tokens" value={totalTokens.toLocaleString()} />
|
||||
<StatBadge label="Trust Score" value="{trustScore.toFixed(1)}/10" />
|
||||
{#if repo.stars != null}
|
||||
@@ -210,31 +214,17 @@
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<!-- Versions -->
|
||||
{#if versions.length > 0}
|
||||
<!-- Indexed Versions -->
|
||||
{#if indexedVersions.length > 0}
|
||||
<div class="mt-6 rounded-xl border border-gray-200 bg-white p-5">
|
||||
<h2 class="mb-3 text-sm font-semibold text-gray-700">Indexed Versions</h2>
|
||||
<div class="divide-y divide-gray-100">
|
||||
{#each versions as version (version.id)}
|
||||
<div class="flex items-center justify-between py-2.5">
|
||||
<div>
|
||||
<span class="font-mono text-sm font-medium text-gray-900">{version.tag}</span>
|
||||
{#if version.title}
|
||||
<span class="ml-2 text-sm text-gray-500">{version.title}</span>
|
||||
{/if}
|
||||
</div>
|
||||
<div class="flex items-center gap-3">
|
||||
<span
|
||||
class="rounded-full px-2 py-0.5 text-xs {stateColors[version.state] ??
|
||||
'bg-gray-100 text-gray-600'}"
|
||||
>
|
||||
{stateLabels[version.state] ?? version.state}
|
||||
</span>
|
||||
{#if version.indexedAt}
|
||||
<span class="text-xs text-gray-400">{formatDate(version.indexedAt)}</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
{#each indexedVersions as versionTag (versionTag)}
|
||||
<span
|
||||
class="rounded-full border border-green-200 bg-green-50 px-3 py-1 font-mono text-sm text-green-800"
|
||||
>
|
||||
{versionTag}
|
||||
</span>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -8,7 +8,11 @@ describe('/repos/[id] page server load', () => {
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({ id: '/facebook/react', title: 'React' })
|
||||
json: async () => ({
|
||||
id: '/facebook/react',
|
||||
title: 'React',
|
||||
indexedVersions: ['main', 'v18.3.0']
|
||||
})
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
@@ -27,7 +31,11 @@ describe('/repos/[id] page server load', () => {
|
||||
'/api/v1/jobs?repositoryId=%2Ffacebook%2Freact&limit=5'
|
||||
);
|
||||
expect(result).toEqual({
|
||||
repo: { id: '/facebook/react', title: 'React' },
|
||||
repo: {
|
||||
id: '/facebook/react',
|
||||
title: 'React',
|
||||
indexedVersions: ['main', 'v18.3.0']
|
||||
},
|
||||
recentJobs: [{ id: 'job-1', repositoryId: '/facebook/react' }]
|
||||
});
|
||||
});
|
||||
|
||||
22
src/routes/settings/+page.server.ts
Normal file
22
src/routes/settings/+page.server.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { getClient } from '$lib/server/db/client.js';
|
||||
import { LocalEmbeddingProvider } from '$lib/server/embeddings/local.provider.js';
|
||||
import { EmbeddingSettingsDtoMapper } from '$lib/server/mappers/embedding-settings.dto.mapper.js';
|
||||
import { EmbeddingSettingsService } from '$lib/server/services/embedding-settings.service.js';
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
const service = new EmbeddingSettingsService(getClient());
|
||||
const settings = EmbeddingSettingsDtoMapper.toDto(service.getSettings());
|
||||
|
||||
let localProviderAvailable = false;
|
||||
try {
|
||||
localProviderAvailable = await new LocalEmbeddingProvider().isAvailable();
|
||||
} catch {
|
||||
localProviderAvailable = false;
|
||||
}
|
||||
|
||||
return {
|
||||
settings,
|
||||
localProviderAvailable
|
||||
};
|
||||
};
|
||||
@@ -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 -->
|
||||
|
||||
103
src/routes/settings/page.server.test.ts
Normal file
103
src/routes/settings/page.server.test.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
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.js', () => ({
|
||||
getClient: () => db
|
||||
}));
|
||||
|
||||
vi.mock('$lib/server/embeddings/local.provider.js', () => ({
|
||||
LocalEmbeddingProvider: class {
|
||||
async isAvailable() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
import { load } from './+page.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('/settings page server load', () => {
|
||||
beforeEach(() => {
|
||||
db = createTestDb();
|
||||
});
|
||||
|
||||
it('returns the active profile and local provider availability', async () => {
|
||||
db.prepare(
|
||||
`INSERT INTO embedding_profiles
|
||||
(id, provider_kind, title, enabled, is_default, model, dimensions, config, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
).run(
|
||||
'openai-default',
|
||||
'openai-compatible',
|
||||
'OpenAI-compatible',
|
||||
1,
|
||||
1,
|
||||
'text-embedding-3-small',
|
||||
1536,
|
||||
JSON.stringify({
|
||||
baseUrl: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-test',
|
||||
model: 'text-embedding-3-small'
|
||||
}),
|
||||
1710000000,
|
||||
1710000000
|
||||
);
|
||||
db.prepare('UPDATE embedding_profiles SET is_default = 0 WHERE id = ?').run('local-default');
|
||||
|
||||
const result = (await load({} as never)) as {
|
||||
localProviderAvailable: boolean;
|
||||
settings: {
|
||||
activeProfileId: string | null;
|
||||
activeProfile: {
|
||||
config: Record<string, unknown>;
|
||||
configEntries: Array<{ key: string; value: string; redacted: boolean }>;
|
||||
} | null;
|
||||
};
|
||||
};
|
||||
|
||||
expect(result.localProviderAvailable).toBe(true);
|
||||
expect(result.settings.activeProfileId).toBe('openai-default');
|
||||
expect(result.settings.activeProfile).toMatchObject({
|
||||
id: 'openai-default',
|
||||
providerKind: 'openai-compatible'
|
||||
});
|
||||
expect(result.settings.activeProfile?.config).toEqual({
|
||||
baseUrl: 'https://api.openai.com/v1',
|
||||
model: 'text-embedding-3-small'
|
||||
});
|
||||
expect(result.settings.activeProfile?.configEntries).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ key: 'apiKey', value: '[redacted]', redacted: true })
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user