provider.d.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * provider.ts - Embedding provider abstraction
  3. *
  4. * Defines the EmbeddingProvider interface that allows qmd to use either:
  5. * - LocalLlamaCppProvider (legacy, GGUF via node-llama-cpp)
  6. * - OpenAIEmbeddingsProvider (HTTP, OpenAI-compatible endpoint like ai.mm.mk)
  7. *
  8. * The factory in `./factory.ts` selects an implementation based on env vars,
  9. * a CLI flag, or `~/.config/qmd/config.json`.
  10. */
  11. /**
  12. * Single embedding result
  13. */
  14. export type ProviderEmbedding = {
  15. embedding: number[];
  16. /** Model identifier used to produce this embedding (matches content_vectors.model in DB) */
  17. model: string;
  18. };
  19. /**
  20. * Supported provider kinds
  21. */
  22. export type ProviderKind = "local" | "openai";
  23. /**
  24. * Healthcheck result for provider startup verification
  25. */
  26. export type ProviderHealth = {
  27. ok: boolean;
  28. /** Model identifier reported by the provider */
  29. model: string;
  30. /** Embedding dimensions (e.g. 768 for embeddinggemma-300M) */
  31. dimensions?: number;
  32. /** Detail message (error reason on failure, status on success) */
  33. detail?: string;
  34. };
  35. /**
  36. * Per-call options for provider embedding
  37. */
  38. export type ProviderEmbedOptions = {
  39. /** Optional model id override (rare; usually provider has a fixed model) */
  40. model?: string;
  41. /** Abort signal for cancellation / timeout */
  42. signal?: AbortSignal;
  43. };
  44. /**
  45. * Provider interface — both LocalLlamaCppProvider and OpenAIEmbeddingsProvider implement this.
  46. *
  47. * Implementations MUST:
  48. * - Return `null` (not throw) for individual texts that fail to embed;
  49. * the caller will count it as an error and continue.
  50. * - Honor `options.signal` for cancellation.
  51. * - Be safe to call concurrently for `embedBatch`.
  52. */
  53. export interface EmbeddingProvider {
  54. /** Provider kind tag — useful for logging and factory introspection */
  55. readonly kind: ProviderKind;
  56. /**
  57. * Stable model identifier reported to the caller.
  58. *
  59. * MUST match what's stored in `content_vectors.model` for the existing
  60. * index — otherwise the model-id guard refuses to embed.
  61. */
  62. getModelId(): string;
  63. /**
  64. * Embedding vector dimensions. May return `undefined` before the first call
  65. * (some providers probe lazily). Once known, MUST stay stable.
  66. */
  67. getDimensions(): number | undefined;
  68. /**
  69. * Healthcheck — verifies the provider is reachable and the model is loaded.
  70. * Should NOT throw — return `{ ok: false, detail: ... }` on failure.
  71. *
  72. * For HTTP providers: ping `/health` endpoint.
  73. * For local provider: ensure model loads.
  74. */
  75. healthcheck(signal?: AbortSignal): Promise<ProviderHealth>;
  76. /**
  77. * Embed a single text. Returns `null` on per-call failure.
  78. */
  79. embed(text: string, options?: ProviderEmbedOptions): Promise<ProviderEmbedding | null>;
  80. /**
  81. * Embed multiple texts in a batch (more efficient than calling `embed` N times).
  82. *
  83. * Output array length MUST equal input array length. Failed entries are `null`.
  84. * Implementations are responsible for chunking large batches per their
  85. * upstream limits (e.g. OpenAI provider chunks to 64).
  86. */
  87. embedBatch(texts: string[], options?: ProviderEmbedOptions): Promise<(ProviderEmbedding | null)[]>;
  88. /** Release any held resources (HTTP keep-alive sockets, model handles, …) */
  89. dispose(): Promise<void>;
  90. }
  91. /**
  92. * Error thrown when the provider's reported model id does not match the
  93. * model id baked into existing `content_vectors` rows. Forces user to
  94. * re-embed (`qmd embed -f`) or pin the matching model id.
  95. */
  96. export declare class ModelMismatchError extends Error {
  97. readonly providerModel: string;
  98. readonly existingModels: string[];
  99. constructor(providerModel: string, existingModels: string[]);
  100. }
  101. /**
  102. * Verify that the provider's model id is compatible with the existing
  103. * `content_vectors` entries. Pass-through (no-op) if the table is empty
  104. * (fresh DB) or if the model id appears in the distinct set.
  105. *
  106. * Caller passes `existingModels` (typically result of
  107. * `SELECT DISTINCT model FROM content_vectors`).
  108. */
  109. export declare function assertModelCompatible(providerModel: string, existingModels: string[]): void;