Files
trueref-legacy/src/routes/api/v1/context/+server.ts
Giancarmine Salucci 169df4d984 feat(TRUEREF-0020): add embedding profiles, default local embeddings, and version-scoped semantic retrieval
- Add embedding_profiles table with provider registry pattern
- Install @xenova/transformers as runtime dependency
- Update snippet_embeddings with composite PK (snippet_id, profile_id)
- Seed default local profile using Xenova/all-MiniLM-L6-v2
- Add provider registry (local-transformers, openai-compatible)
- Update EmbeddingService to persist and retrieve by profileId
- Add version-scoped VectorSearch with optional versionId filtering
- Add searchMode (auto|keyword|semantic|hybrid) to HybridSearchService
- Update API /context route to load active profile, support searchMode/alpha params
- Extend MCP query-docs tool with searchMode and alpha parameters
- Update settings API to work with embedding_profiles table
- Add comprehensive test coverage for profiles, registry, version scoping

Status: 445/451 tests passing, core feature complete
2026-03-25 19:16:37 +01:00

231 lines
7.0 KiB
TypeScript

/**
* GET /api/v1/context
*
* Fetch documentation snippets for a library. Compatible with context7's
* GET /api/v2/context interface.
*
* Query parameters:
* libraryId (required) — e.g. "/facebook/react" or "/facebook/react/v18.3.0"
* query (required) — specific question about the library
* type (optional) — "json" (default) or "txt"
* tokens (optional) — approximate max token count (default 10000)
*/
import type { RequestHandler } from './$types';
import { getClient } from '$lib/server/db/client';
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 { 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';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function getServices(db: ReturnType<typeof getClient>) {
const searchService = new SearchService(db);
// 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'
)
.get();
const provider = profileRow ? createProviderFromProfile(profileRow) : null;
const hybridService = new HybridSearchService(db, searchService, provider);
return { db, searchService, hybridService, profileId: profileRow?.id };
}
interface RawRepoConfig {
rules: string | null;
}
function getRules(db: ReturnType<typeof getClient>, repositoryId: string): string[] {
const row = db
.prepare<[string], RawRepoConfig>(`SELECT rules FROM repository_configs WHERE repository_id = ?`)
.get(repositoryId);
if (!row?.rules) return [];
try {
const parsed = JSON.parse(row.rules);
return Array.isArray(parsed) ? (parsed as string[]) : [];
} catch {
return [];
}
}
interface RawRepoState {
state: 'pending' | 'indexing' | 'indexed' | 'error';
title: string;
}
// ---------------------------------------------------------------------------
// Route handler
// ---------------------------------------------------------------------------
export const GET: RequestHandler = async ({ url }) => {
const libraryId = url.searchParams.get('libraryId');
if (!libraryId || !libraryId.trim()) {
return new Response(
JSON.stringify({ error: 'libraryId is required', code: 'MISSING_PARAMETER' }),
{
status: 400,
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }
}
);
}
const query = url.searchParams.get('query');
if (!query || !query.trim()) {
return new Response(
JSON.stringify({ error: 'query is required', code: 'MISSING_PARAMETER' }),
{
status: 400,
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }
}
);
}
const responseType = url.searchParams.get('type') ?? 'json';
const tokensRaw = parseInt(url.searchParams.get('tokens') ?? String(DEFAULT_TOKEN_BUDGET), 10);
const maxTokens = isNaN(tokensRaw) || tokensRaw < 1 ? DEFAULT_TOKEN_BUDGET : tokensRaw;
// Parse searchMode and alpha
const rawMode = url.searchParams.get('searchMode') ?? 'auto';
const searchMode = ['auto', 'keyword', 'semantic', 'hybrid'].includes(rawMode)
? (rawMode as 'auto' | 'keyword' | 'semantic' | 'hybrid')
: 'auto';
const alphaRaw = parseFloat(url.searchParams.get('alpha') ?? '0.5');
const alpha = isNaN(alphaRaw) ? 0.5 : Math.max(0, Math.min(1, alphaRaw));
// Parse the libraryId
let parsed: ReturnType<typeof parseLibraryId>;
try {
parsed = parseLibraryId(libraryId);
} catch {
return new Response(
JSON.stringify({ error: `Invalid libraryId: ${libraryId}`, code: 'MISSING_PARAMETER' }),
{
status: 400,
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }
}
);
}
try {
const db = getClient();
const { hybridService, profileId } = getServices(db);
// Verify the repository exists and check its state.
const repo = db
.prepare<[string], RawRepoState>(`SELECT state, title FROM repositories WHERE id = ?`)
.get(parsed.repositoryId);
if (!repo) {
return new Response(
JSON.stringify({
error: `Library ${parsed.repositoryId} not found or not yet indexed`,
code: 'LIBRARY_NOT_FOUND'
}),
{
status: 404,
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }
}
);
}
if (repo.state === 'indexing' || repo.state === 'pending') {
return new Response(
JSON.stringify({
error: 'Library is currently being indexed, please try again shortly',
code: 'INDEXING_IN_PROGRESS'
}),
{
status: 503,
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }
}
);
}
// Resolve version ID if a specific version was requested.
let versionId: string | undefined;
if (parsed.version) {
const versionRow = db
.prepare<[string, string], { id: string }>(
`SELECT id FROM repository_versions WHERE repository_id = ? AND tag = ?`
)
.get(parsed.repositoryId, parsed.version);
// Version not found is not fatal — fall back to default branch.
versionId = versionRow?.id;
}
// Execute hybrid search (falls back to FTS5 when no embedding provider is set).
const searchResults = await hybridService.search(query, {
repositoryId: parsed.repositoryId,
versionId,
limit: 50, // fetch more than needed; token budget will trim
searchMode,
alpha,
profileId
});
// Apply token budget.
const snippets = searchResults.map((r) => r.snippet);
const selected = selectSnippetsWithinBudget(snippets, maxTokens);
// Re-wrap selected snippets as SnippetSearchResult for formatters.
const selectedResults = selected.map((snippet) => {
const found = searchResults.find((r) => r.snippet.id === snippet.id)!;
return found;
});
// Load rules from repository_configs.
const rules = getRules(db, parsed.repositoryId);
if (responseType === 'txt') {
const text = formatContextTxt(selectedResults, rules);
return new Response(text, {
status: 200,
headers: {
'Content-Type': 'text/plain; charset=utf-8',
...CORS_HEADERS
}
});
}
// Default: JSON
const body = formatContextJson(selectedResults, rules);
return dtoJsonResponse(body, {
status: 200,
headers: CORS_HEADERS
});
} catch (err) {
const message = err instanceof Error ? err.message : 'Internal server error';
return new Response(JSON.stringify({ error: message, code: 'INTERNAL_ERROR' }), {
status: 500,
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }
});
}
};
export const OPTIONS: RequestHandler = () => {
return new Response(null, {
status: 204,
headers: CORS_HEADERS
});
};