autofallback.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * autofallback.ts - AutoFallbackEmbeddingProvider.
  3. *
  4. * Historical internal helper for two commercial API providers. It is not
  5. * exported by the package or selected by the factory.
  6. *
  7. * Behavior summary:
  8. * - Primary call succeeds → return; record success.
  9. * - Primary throws CircuitOpenError → fall back, log WARN once per transition.
  10. * - Primary throws any other error → fall back for THIS call only;
  11. * count toward the failure-streak threshold.
  12. * - When failure streak crosses threshold (default 3) → set our own
  13. * "open until" timestamp; until expiry, route directly to fallback
  14. * (skip primary entirely).
  15. * - On expiry, retry primary opportunistically.
  16. * - getModelId / getDimensions / dispose are delegated to whichever
  17. * provider is currently active (or to the primary if both are usable).
  18. */
  19. import type { EmbeddingProvider, ProviderEmbedOptions, ProviderEmbedding, ProviderHealth, ProviderKind } from "./provider.js";
  20. export type AutoFallbackProviderConfig = {
  21. primary: EmbeddingProvider;
  22. fallback: EmbeddingProvider;
  23. /**
  24. * Number of consecutive non-CircuitOpenError failures before we suppress
  25. * primary calls and route directly to fallback. Default: 3.
  26. */
  27. failureStreakThreshold?: number;
  28. /**
  29. * Time in ms to keep routing through fallback after the breaker opens.
  30. * Default: 5 minutes (matches `OpenAIEmbeddingsProvider`'s circuit duration).
  31. */
  32. cooldownMs?: number;
  33. /**
  34. * Optional WARN sink. Defaults to writing to `process.stderr` once per
  35. * routing transition (closed→open and open→closed).
  36. */
  37. warn?: (msg: string) => void;
  38. /** Custom clock for tests */
  39. now?: () => number;
  40. };
  41. export type FallbackState = "primary" | "fallback";
  42. export declare class AutoFallbackEmbeddingProvider implements EmbeddingProvider {
  43. readonly kind: ProviderKind;
  44. readonly primary: EmbeddingProvider;
  45. readonly fallback: EmbeddingProvider;
  46. private readonly failureStreakThreshold;
  47. private readonly cooldownMs;
  48. private readonly warn;
  49. private readonly now;
  50. private failureStreak;
  51. private fallbackUntil;
  52. private lastTransitionState;
  53. constructor(config: AutoFallbackProviderConfig);
  54. /**
  55. * Stable model id reported by the primary. The model-id guard runs against
  56. * the primary's id because that's what callers actually want when the
  57. * remote endpoint is online; a secondary commercial provider must report a
  58. * compatible id.
  59. */
  60. getModelId(): string;
  61. getDimensions(): number | undefined;
  62. /**
  63. * Combined last-error from primary + fallback. Either, neither, or both legs
  64. * may have a tracked error after `embed()`/`embedBatch()` runs:
  65. * - Both clean → undefined
  66. * - Primary failed, fallback rescued → returns primary error (most useful)
  67. * - Both failed → returns "primary: <msg> | fallback: <msg>"
  68. * - Only primary skipped (cooldown), fallback also failed → returns fallback error
  69. */
  70. getLastError(): string | undefined;
  71. /** Current routing state (mostly for tests + observability) */
  72. getRoutingState(): FallbackState;
  73. /** Reset failure-streak + cooldown (mostly for tests / admin) */
  74. reset(): void;
  75. healthcheck(signal?: AbortSignal): Promise<ProviderHealth>;
  76. embed(text: string, options?: ProviderEmbedOptions): Promise<ProviderEmbedding | null>;
  77. embedBatch(texts: string[], options?: ProviderEmbedOptions): Promise<(ProviderEmbedding | null)[]>;
  78. dispose(): Promise<void>;
  79. /**
  80. * Generic dispatcher: try primary if not in cooldown, fall back on
  81. * `CircuitOpenError`, count other errors against the failure streak.
  82. * `op` is invoked with whichever provider is selected.
  83. */
  84. private run;
  85. private openCooldown;
  86. private transition;
  87. }