factory.d.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /** Historical only. `true` is rejected because local fallback is forbidden. */
  28. autoFallback?: boolean;
  29. };
  30. };
  31. export declare function defaultConfigPath(): string;
  32. /**
  33. * Load `~/.config/qmd/config.json` if present. Returns an empty object on
  34. * any read/parse error; provider construction then fails closed without an endpoint.
  35. */
  36. export declare function loadConfigFile(path?: string): EmbedProviderConfigFile;
  37. export type CreateEmbeddingProviderOptions = {
  38. /** Force a specific provider kind. Overrides env + config. */
  39. kind?: ProviderKind;
  40. /** Override config file path (mostly for tests) */
  41. configPath?: string;
  42. /** OpenAI-provider overrides — merged on top of env/config */
  43. openai?: Partial<OpenAIProviderConfig>;
  44. /**
  45. * Historical compatibility input. Any truthy value produces typed HOLD;
  46. * commercial provider failures must never fall back to a local model.
  47. */
  48. autoFallback?: boolean;
  49. /**
  50. * Custom env source (mostly for tests). Defaults to `process.env`.
  51. * Read keys: QMD_EMBED_PROVIDER, QMD_EMBED_ENDPOINT, QMD_EMBED_API_KEY,
  52. * QMD_EMBED_MODEL_ID, QMD_EMBED_UPSTREAM_MODEL, QMD_EMBED_BATCH_SIZE,
  53. * QMD_EMBED_TIMEOUT_MS, QMD_EMBED_AUTO_FALLBACK.
  54. */
  55. env?: Record<string, string | undefined>;
  56. };
  57. /**
  58. * Resolve the provider kind without instantiating anything. Useful for
  59. * logging and tests.
  60. */
  61. export declare function resolveProviderKind(opts?: CreateEmbeddingProviderOptions): ProviderKind;
  62. /**
  63. * Factory entry point — returns the appropriate `EmbeddingProvider`.
  64. * Throws if `openai` kind is requested but no endpoint is configured.
  65. */
  66. export declare function createEmbeddingProvider(opts?: CreateEmbeddingProviderOptions): EmbeddingProvider;
  67. export declare function assertCommercialEndpoint(endpoint: string): void;