factory.d.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /**
  27. * Max in-flight HTTP requests during a single `embedBatch` call. Default 4
  28. * (matches qmd-embed-worker's MAX_CONCURRENT_REQUESTS=4 semaphore). Set
  29. * to 1 to force legacy sequential dispatch.
  30. */
  31. concurrency?: number;
  32. timeoutMs?: number;
  33. /** When true, wrap the openai provider in AutoFallback (local fallback). */
  34. autoFallback?: boolean;
  35. };
  36. };
  37. export declare function defaultConfigPath(): string;
  38. /**
  39. * Load `~/.config/qmd/config.json` if present. Returns an empty object on
  40. * any read/parse error so we silently fall back to env/local.
  41. */
  42. export declare function loadConfigFile(path?: string): EmbedProviderConfigFile;
  43. export type CreateEmbeddingProviderOptions = {
  44. /** Force a specific provider kind. Overrides env + config. */
  45. kind?: ProviderKind;
  46. /** Override config file path (mostly for tests) */
  47. configPath?: string;
  48. /** Local-provider overrides */
  49. local?: LocalLlamaCppProviderConfig;
  50. /** OpenAI-provider overrides — merged on top of env/config */
  51. openai?: Partial<OpenAIProviderConfig>;
  52. /**
  53. * Wrap the chosen provider in `AutoFallbackEmbeddingProvider` so that a
  54. * remote outage transparently falls back to local llama.cpp. Default:
  55. * `false` — opt-in, since the wrapper requires both backends to be
  56. * available and the local one will warm node-llama-cpp on first call.
  57. *
  58. * Resolution: explicit `autoFallback` wins → env `QMD_EMBED_AUTO_FALLBACK`
  59. * (`1`/`true`) → config-file `embedProvider.autoFallback` → false.
  60. *
  61. * Only applies when the resolved kind is `openai` (no fallback wrap when
  62. * the primary IS local already).
  63. */
  64. autoFallback?: boolean;
  65. /**
  66. * Override config for `AutoFallbackEmbeddingProvider` (failureStreak,
  67. * cooldownMs, etc.). Only used when `autoFallback` resolves true.
  68. * Primary + fallback are constructed automatically.
  69. */
  70. autoFallbackOverrides?: Omit<AutoFallbackProviderConfig, "primary" | "fallback">;
  71. /**
  72. * Custom env source (mostly for tests). Defaults to `process.env`.
  73. * Read keys: QMD_EMBED_PROVIDER, QMD_EMBED_ENDPOINT, QMD_EMBED_API_KEY,
  74. * QMD_EMBED_MODEL_ID, QMD_EMBED_UPSTREAM_MODEL, QMD_EMBED_BATCH_SIZE,
  75. * QMD_EMBED_TIMEOUT_MS, QMD_EMBED_AUTO_FALLBACK.
  76. */
  77. env?: Record<string, string | undefined>;
  78. };
  79. /**
  80. * Resolve the provider kind without instantiating anything. Useful for
  81. * logging and tests.
  82. */
  83. export declare function resolveProviderKind(opts?: CreateEmbeddingProviderOptions): ProviderKind;
  84. /**
  85. * Factory entry point — returns the appropriate `EmbeddingProvider`.
  86. * Throws if `openai` kind is requested but no endpoint is configured.
  87. */
  88. export declare function createEmbeddingProvider(opts?: CreateEmbeddingProviderOptions): EmbeddingProvider;