ai-resilience 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ai-resilience
2
2
 
3
- `ai-resilience` is an axios-retry++ toolkit for modern AI and backend systems. It keeps axios compatibility while adding configurable retry strategies, advanced jitter, semantic recovery, JSON validation and repair, hooks, structured logging, and strong TypeScript types.
3
+ `ai-resilience` is an axios-retry++ toolkit for modern AI and backend systems. It keeps axios compatibility while adding configurable retry strategies, semantic recovery, provider fallback, circuit breakers, distributed coordination, streaming recovery, telemetry hooks, and strong TypeScript types.
4
4
 
5
5
  ## Install
6
6
 
@@ -52,6 +52,10 @@ const result = await requestWithSemanticRetry(client, {
52
52
  - JSON validation, Zod schema validation, and JSON repair
53
53
  - AI-aware failure detection for empty responses, refusals, truncation, and schema mismatches
54
54
  - AI retry policies and semantic lifecycle hooks
55
+ - Provider fallback for OpenAI, Anthropic, Gemini, or custom providers
56
+ - Circuit breaker, provider health tracking, adaptive fallback delay, and provider metrics
57
+ - Redis-compatible retry coordination and distributed rate limiting through lightweight adapters
58
+ - Adaptive routing, streaming recovery, OpenTelemetry-compatible spans, and advanced metrics
55
59
  - TypeScript-first public API
56
60
 
57
61
  ## API
@@ -100,6 +104,98 @@ Parses JSON strings, optionally repairs common model-output issues, and validate
100
104
 
101
105
  Installs a response interceptor that validates and repairs successful axios responses. Use `requestWithSemanticRetry` when you also want semantic retries.
102
106
 
107
+ ### `createProviderFallback(providers, options)`
108
+
109
+ Creates a provider fallback engine with provider routing, circuit breakers, health tracking, adaptive delay, fallback hooks, and metrics.
110
+
111
+ ```ts
112
+ import { createProviderFallback } from "ai-resilience";
113
+
114
+ const fallback = createProviderFallback(
115
+ [
116
+ {
117
+ id: "openai-primary",
118
+ type: "openai",
119
+ priority: 1,
120
+ request: (input) => openaiClient.responses.create(input),
121
+ },
122
+ {
123
+ id: "anthropic-backup",
124
+ type: "anthropic",
125
+ priority: 2,
126
+ request: (input) => anthropicClient.messages.create(input),
127
+ },
128
+ ],
129
+ {
130
+ strategy: "priority",
131
+ circuitBreaker: { failureThreshold: 3, cooldownMs: 30_000 },
132
+ hooks: {
133
+ onProviderFallback: ({ fromProviderId, toProviderId }) => {
134
+ console.log(`fallback ${fromProviderId} -> ${toProviderId}`);
135
+ },
136
+ },
137
+ },
138
+ );
139
+
140
+ const response = await fallback.request({ prompt: "Summarize this" });
141
+ const metrics = fallback.snapshot();
142
+ ```
143
+
144
+ ### `RedisRetryCoordinator`
145
+
146
+ Coordinates retries and locks across processes. Pass an `ioredis`-compatible client through the `redis` option, or omit it for local in-memory coordination during tests and development.
147
+
148
+ ```ts
149
+ import { RedisRetryCoordinator } from "ai-resilience";
150
+
151
+ const coordinator = new RedisRetryCoordinator({
152
+ namespace: "my-api",
153
+ redis,
154
+ });
155
+
156
+ await coordinator.incrementRetry("tenant-a:request-123");
157
+ const locked = await coordinator.acquireLock("provider-routing");
158
+ ```
159
+
160
+ ### `DistributedRateLimiter`
161
+
162
+ Provides Redis-compatible fixed-window rate limiting.
163
+
164
+ ```ts
165
+ import { DistributedRateLimiter } from "ai-resilience";
166
+
167
+ const limiter = new DistributedRateLimiter(redis);
168
+ const result = await limiter.consume({
169
+ key: "tenant-a:openai",
170
+ limit: 100,
171
+ windowSeconds: 60,
172
+ });
173
+ ```
174
+
175
+ ### `AdaptiveRouter`
176
+
177
+ Ranks providers using latency, failure, and cost signals.
178
+
179
+ ```ts
180
+ import { AdaptiveRouter } from "ai-resilience";
181
+
182
+ const router = new AdaptiveRouter(providers, {
183
+ latencyWeight: 1,
184
+ failureWeight: 2,
185
+ costWeight: 0.5,
186
+ });
187
+
188
+ const provider = router.select(metricsByProvider);
189
+ ```
190
+
191
+ ### `recoverStream(stream, options)`
192
+
193
+ Collects streaming chunks and calls recovery hooks when chunk gaps exceed a configured threshold.
194
+
195
+ ### `withTelemetry(tracer, name, operation, attributes)`
196
+
197
+ Wraps an async operation with an OpenTelemetry-style tracer adapter. This package does not force `@opentelemetry/api` into runtime dependencies; pass a tracer with `startSpan`.
198
+
103
199
  ### Retry config
104
200
 
105
201
  ```ts