@kb-labs/core-resource-broker 1.0.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 ADDED
@@ -0,0 +1,96 @@
1
+ # @kb-labs/core-resource-broker
2
+
3
+ Centralized queue, rate limiting, and retry management for heavy platform resources (LLM, embeddings, vector store).
4
+
5
+ ## Overview
6
+
7
+ Wraps platform adapters with a priority queue, configurable rate limits, and automatic retry with exponential backoff. Supports single-process (in-memory) and distributed (state-broker HTTP daemon) rate limiting backends.
8
+
9
+ ## Quick Start
10
+
11
+ ```typescript
12
+ import {
13
+ ResourceBroker,
14
+ InMemoryRateLimitBackend,
15
+ createQueuedLLM,
16
+ } from '@kb-labs/core-resource-broker';
17
+
18
+ const backend = new InMemoryRateLimitBackend();
19
+ const broker = new ResourceBroker(backend);
20
+
21
+ // Wrap existing LLM adapter — transparent drop-in replacement
22
+ const llm = createQueuedLLM(broker, rawLLMAdapter);
23
+
24
+ // Requests are now automatically queued, rate-limited, and retried
25
+ const response = await llm.complete({ prompt: 'Hello' });
26
+ ```
27
+
28
+ ## Queued Adapters
29
+
30
+ Drop-in wrappers that add queuing and rate limiting to existing adapters:
31
+
32
+ | Factory | Wraps |
33
+ |---------|-------|
34
+ | `createQueuedLLM()` | `LLMAdapter` |
35
+ | `createQueuedEmbeddings()` | `EmbeddingsAdapter` |
36
+ | `createQueuedVectorStore()` | `VectorStoreAdapter` |
37
+
38
+ ## Rate Limit Backends
39
+
40
+ | Backend | Use case |
41
+ |---------|----------|
42
+ | `InMemoryRateLimitBackend` | Single process — development, CLI tools |
43
+ | `StateBrokerRateLimitBackend` | Distributed — multi-instance REST API deployments |
44
+
45
+ ```typescript
46
+ // Distributed mode (requires core-state-daemon running)
47
+ import { StateBrokerRateLimitBackend } from '@kb-labs/core-resource-broker';
48
+
49
+ const backend = new StateBrokerRateLimitBackend({
50
+ url: 'http://localhost:7777',
51
+ });
52
+ ```
53
+
54
+ ## Rate Limit Presets
55
+
56
+ ```typescript
57
+ import { RATE_LIMIT_PRESETS, getRateLimitConfig } from '@kb-labs/core-resource-broker';
58
+
59
+ broker.register('llm', {
60
+ rateLimits: 'openai-tier-2', // preset: 3500 RPM, 90k TPM
61
+ executor: (op, args) => llmAdapter[op](...args),
62
+ });
63
+
64
+ // Or custom config
65
+ broker.register('embeddings', {
66
+ rateLimits: { requestsPerMinute: 1000, tokensPerMinute: 1_000_000 },
67
+ executor: (op, args) => embeddingsAdapter[op](...args),
68
+ });
69
+ ```
70
+
71
+ ## Priority Queue
72
+
73
+ ```typescript
74
+ // High-priority requests jump the queue
75
+ const result = await broker.execute('llm', 'complete', [prompt], {
76
+ priority: 'high', // 'high' | 'normal' | 'low'
77
+ });
78
+ ```
79
+
80
+ ## Retry Configuration
81
+
82
+ ```typescript
83
+ const broker = new ResourceBroker(backend, {
84
+ retry: {
85
+ maxAttempts: 3,
86
+ initialDelay: 1000, // ms
87
+ maxDelay: 30_000,
88
+ jitter: true,
89
+ },
90
+ });
91
+ // Automatically retries on 429 (respects Retry-After header) and 5xx errors
92
+ ```
93
+
94
+ ## License
95
+
96
+ KB Public License v1.1 © KB Labs