65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
/**
|
|
* Provider Registry — map providerKind to EmbeddingProvider instances.
|
|
*
|
|
* Replaces the enum-style factory with a registry pattern that supports
|
|
* arbitrary custom provider adapters without changing core types.
|
|
*/
|
|
|
|
import type { EmbeddingProvider } from './provider.js';
|
|
import { NoopEmbeddingProvider } from './provider.js';
|
|
import { OpenAIEmbeddingProvider } from './openai.provider.js';
|
|
import { LocalEmbeddingProvider } from './local.provider.js';
|
|
import type { EmbeddingProfile } from '../db/schema.js';
|
|
|
|
export type ProviderFactory = (config: Record<string, unknown>) => EmbeddingProvider;
|
|
|
|
const PROVIDER_REGISTRY: Record<string, ProviderFactory> = {
|
|
'local-transformers': () => new LocalEmbeddingProvider(),
|
|
'openai-compatible': (config) =>
|
|
new OpenAIEmbeddingProvider({
|
|
baseUrl: config.baseUrl as string,
|
|
apiKey: config.apiKey as string,
|
|
model: config.model as string,
|
|
dimensions: config.dimensions as number | undefined,
|
|
maxBatchSize: config.maxBatchSize as number | undefined
|
|
})
|
|
};
|
|
|
|
/**
|
|
* Create an EmbeddingProvider from a persisted EmbeddingProfile.
|
|
*
|
|
* Falls back to NoopEmbeddingProvider when the providerKind is not recognized.
|
|
*/
|
|
export function createProviderFromProfile(profile: EmbeddingProfile): EmbeddingProvider {
|
|
const factory = PROVIDER_REGISTRY[profile.providerKind];
|
|
if (!factory) return new NoopEmbeddingProvider();
|
|
const config = (profile.config as Record<string, unknown>) ?? {};
|
|
return factory(config);
|
|
}
|
|
|
|
/**
|
|
* Return metadata for the default local profile.
|
|
*
|
|
* Used by migration seeds and runtime defaults.
|
|
*/
|
|
export function getDefaultLocalProfile(): Pick<
|
|
EmbeddingProfile,
|
|
'id' | 'providerKind' | 'model' | 'dimensions'
|
|
> {
|
|
return {
|
|
id: 'local-default',
|
|
providerKind: 'local-transformers',
|
|
model: 'Xenova/all-MiniLM-L6-v2',
|
|
dimensions: 384
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Return all registered providerKind values.
|
|
*
|
|
* Useful for settings UI validation and provider discovery.
|
|
*/
|
|
export function getRegisteredProviderKinds(): string[] {
|
|
return Object.keys(PROVIDER_REGISTRY);
|
|
}
|