factory.d.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * factory.ts - EmbeddingProvider factory with config precedence.
  3. *
  4. * Resolution order (first match wins):
  5. * 1. Explicit `kind` argument or `--provider` CLI flag → forces a kind
  6. * 2. `QMD_EMBED_ENDPOINT` env var present and non-empty → "openai"
  7. * 3. Config file (`~/.config/qmd/config.json`) commercial endpoint
  8. * 4. Otherwise → typed HOLD (there is no local or self-hosted fallback)
  9. */
  10. import { type OpenAIProviderConfig } from "./openai.js";
  11. import type { EmbeddingProvider, ProviderKind } from "./provider.js";
  12. export type EmbedProviderConfigFile = {
  13. embedProvider?: {
  14. kind?: ProviderKind;
  15. endpoint?: string;
  16. apiKey?: string;
  17. modelId?: string;
  18. upstreamModel?: string;
  19. batchSize?: number;
  20. /**
  21. * Max in-flight HTTP requests during a single `embedBatch` call. Default 4
  22. * (matches qmd-embed-worker's MAX_CONCURRENT_REQUESTS=4 semaphore). Set
  23. * to 1 to force legacy sequential dispatch.
  24. */
  25. concurrency?: number;
  26. timeoutMs?: number;
  27. };
  28. };
  29. export declare function defaultConfigPath(): string;
  30. /**
  31. * Load `~/.config/qmd/config.json` if present. Returns an empty object on
  32. * any read/parse error; provider construction then fails closed without an endpoint.
  33. */
  34. export declare function loadConfigFile(path?: string): EmbedProviderConfigFile;
  35. export type CreateEmbeddingProviderOptions = {
  36. /** Force the commercial provider kind. Overrides env + config. */
  37. kind?: ProviderKind;
  38. /** Override config file path (mostly for tests) */
  39. configPath?: string;
  40. /** OpenAI-provider overrides — merged on top of env/config */
  41. openai?: Partial<OpenAIProviderConfig>;
  42. /**
  43. * Custom env source (mostly for tests). Defaults to `process.env`.
  44. * Read keys: QMD_EMBED_PROVIDER, QMD_EMBED_ENDPOINT, QMD_EMBED_API_KEY,
  45. * QMD_EMBED_MODEL_ID, QMD_EMBED_UPSTREAM_MODEL, QMD_EMBED_BATCH_SIZE,
  46. * QMD_EMBED_TIMEOUT_MS, QMD_EMBED_AUTO_FALLBACK.
  47. */
  48. env?: Record<string, string | undefined>;
  49. };
  50. /**
  51. * Resolve the provider kind without instantiating anything. Useful for
  52. * logging and tests.
  53. */
  54. export declare function resolveProviderKind(opts?: CreateEmbeddingProviderOptions): ProviderKind;
  55. /**
  56. * Factory entry point — returns the appropriate `EmbeddingProvider`.
  57. * Throws if `openai` kind is requested but no endpoint is configured.
  58. */
  59. export declare function createEmbeddingProvider(opts?: CreateEmbeddingProviderOptions): EmbeddingProvider;
  60. export declare function assertCommercialEndpoint(endpoint: string): void;