factory.d.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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`) `embedProvider.kind` → that kind
  8. * 4. Otherwise → "local" (legacy / backward-compat)
  9. *
  10. * Backward compat invariant: when neither `QMD_EMBED_ENDPOINT` nor
  11. * `~/.config/qmd/config.json` mentions a provider, callers get the same
  12. * `LocalLlamaCppProvider` they had before this change.
  13. */
  14. import { type LocalLlamaCppProviderConfig } from "./local.js";
  15. import { type OpenAIProviderConfig } from "./openai.js";
  16. import { type AutoFallbackProviderConfig } from "./autofallback.js";
  17. import type { EmbeddingProvider, ProviderKind } from "./provider.js";
  18. export type EmbedProviderConfigFile = {
  19. embedProvider?: {
  20. kind?: ProviderKind;
  21. endpoint?: string;
  22. apiKey?: string;
  23. modelId?: string;
  24. upstreamModel?: string;
  25. batchSize?: number;
  26. timeoutMs?: number;
  27. /** When true, wrap the openai provider in AutoFallback (local fallback). */
  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 so we silently fall back to env/local.
  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. /** Local-provider overrides */
  43. local?: LocalLlamaCppProviderConfig;
  44. /** OpenAI-provider overrides — merged on top of env/config */
  45. openai?: Partial<OpenAIProviderConfig>;
  46. /**
  47. * Wrap the chosen provider in `AutoFallbackEmbeddingProvider` so that a
  48. * remote outage transparently falls back to local llama.cpp. Default:
  49. * `false` — opt-in, since the wrapper requires both backends to be
  50. * available and the local one will warm node-llama-cpp on first call.
  51. *
  52. * Resolution: explicit `autoFallback` wins → env `QMD_EMBED_AUTO_FALLBACK`
  53. * (`1`/`true`) → config-file `embedProvider.autoFallback` → false.
  54. *
  55. * Only applies when the resolved kind is `openai` (no fallback wrap when
  56. * the primary IS local already).
  57. */
  58. autoFallback?: boolean;
  59. /**
  60. * Override config for `AutoFallbackEmbeddingProvider` (failureStreak,
  61. * cooldownMs, etc.). Only used when `autoFallback` resolves true.
  62. * Primary + fallback are constructed automatically.
  63. */
  64. autoFallbackOverrides?: Omit<AutoFallbackProviderConfig, "primary" | "fallback">;
  65. /**
  66. * Custom env source (mostly for tests). Defaults to `process.env`.
  67. * Read keys: QMD_EMBED_PROVIDER, QMD_EMBED_ENDPOINT, QMD_EMBED_API_KEY,
  68. * QMD_EMBED_MODEL_ID, QMD_EMBED_UPSTREAM_MODEL, QMD_EMBED_BATCH_SIZE,
  69. * QMD_EMBED_TIMEOUT_MS, QMD_EMBED_AUTO_FALLBACK.
  70. */
  71. env?: Record<string, string | undefined>;
  72. };
  73. /**
  74. * Resolve the provider kind without instantiating anything. Useful for
  75. * logging and tests.
  76. */
  77. export declare function resolveProviderKind(opts?: CreateEmbeddingProviderOptions): ProviderKind;
  78. /**
  79. * Factory entry point — returns the appropriate `EmbeddingProvider`.
  80. * Throws if `openai` kind is requested but no endpoint is configured.
  81. */
  82. export declare function createEmbeddingProvider(opts?: CreateEmbeddingProviderOptions): EmbeddingProvider;