@kb-labs/core-runtime 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 +260 -0
- package/dist/index.cjs +5449 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2267 -0
- package/dist/index.d.ts +2267 -0
- package/dist/index.js +5336 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# @kb-labs/core-runtime
|
|
2
|
+
|
|
3
|
+
> DI Container + Core Features Implementations for KB Labs Platform.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
- **PlatformContainer** - single DI container for all services
|
|
8
|
+
- **initPlatform()** - load adapters from kb.config.json
|
|
9
|
+
- **Core Implementations** - WorkflowEngine, JobScheduler, CronManager, ResourceManager
|
|
10
|
+
|
|
11
|
+
## Dependencies
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
@kb-labs/core-platform -> @kb-labs/core-runtime -> plugins
|
|
15
|
+
(interfaces) (DI + implementations)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Structure
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
src/
|
|
22
|
+
├── container.ts # PlatformContainer class
|
|
23
|
+
├── loader.ts # initPlatform() function
|
|
24
|
+
├── config.ts # PlatformConfig types
|
|
25
|
+
└── index.ts # Main entry point
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Platform Initialization
|
|
31
|
+
```typescript
|
|
32
|
+
import { initPlatform } from '@kb-labs/core-runtime';
|
|
33
|
+
|
|
34
|
+
await initPlatform({
|
|
35
|
+
adapters: {
|
|
36
|
+
analytics: '@kb-labs/analytics-adapter',
|
|
37
|
+
vectorStore: '@kb-labs/mind-qdrant',
|
|
38
|
+
llm: '@kb-labs/shared-openai',
|
|
39
|
+
},
|
|
40
|
+
core: {
|
|
41
|
+
resources: { defaultQuotas: { ... } },
|
|
42
|
+
jobs: { maxConcurrent: 10 },
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Accessing Services
|
|
48
|
+
```typescript
|
|
49
|
+
import { platform } from '@kb-labs/core-runtime';
|
|
50
|
+
|
|
51
|
+
// Adapters (replaceable)
|
|
52
|
+
platform.analytics.track('event');
|
|
53
|
+
platform.vectorStore.search([...]);
|
|
54
|
+
|
|
55
|
+
// Core features (built-in)
|
|
56
|
+
platform.workflows.execute('my-workflow', input);
|
|
57
|
+
platform.jobs.submit({ type: 'task', payload: {...} });
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Configuration via kb.config.json
|
|
61
|
+
```json
|
|
62
|
+
{
|
|
63
|
+
"platform": {
|
|
64
|
+
"adapters": {
|
|
65
|
+
"analytics": "@kb-labs/analytics-adapter",
|
|
66
|
+
"vectorStore": "@kb-labs/mind-qdrant",
|
|
67
|
+
"llm": "@kb-labs/shared-openai",
|
|
68
|
+
"embeddings": "@kb-labs/shared-openai",
|
|
69
|
+
"cache": "@kb-labs/core-redis",
|
|
70
|
+
"storage": "@kb-labs/core-fs",
|
|
71
|
+
"logger": "@kb-labs/core-pino",
|
|
72
|
+
"eventBus": null
|
|
73
|
+
},
|
|
74
|
+
"core": {
|
|
75
|
+
"resources": {
|
|
76
|
+
"defaultQuotas": {
|
|
77
|
+
"maxConcurrentWorkflows": 5,
|
|
78
|
+
"maxConcurrentJobs": 10
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
For development/testing - set adapters to `null` to use NoOp implementations:
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"platform": {
|
|
90
|
+
"adapters": {
|
|
91
|
+
"analytics": null,
|
|
92
|
+
"vectorStore": null,
|
|
93
|
+
"llm": null
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Adapters vs Core Features
|
|
100
|
+
|
|
101
|
+
| Aspect | Adapters | Core Features |
|
|
102
|
+
|--------|----------|---------------|
|
|
103
|
+
| **Replaceability** | Full (via config) | Built-in |
|
|
104
|
+
| **Implementation** | External packages | In core-runtime |
|
|
105
|
+
| **Examples** | analytics, llm | workflows, jobs |
|
|
106
|
+
|
|
107
|
+
## Graceful Degradation
|
|
108
|
+
|
|
109
|
+
All platform services have **NoOp fallback implementations** that activate automatically when adapters aren't configured. This enables:
|
|
110
|
+
|
|
111
|
+
### ✅ Development without Configuration
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
// No kb.config.json? No problem! Platform initializes with NoOp adapters
|
|
115
|
+
await initPlatform();
|
|
116
|
+
|
|
117
|
+
// These calls work but do nothing (safe no-ops)
|
|
118
|
+
platform.analytics.track('event'); // NoOpAnalytics - silent
|
|
119
|
+
platform.vectorStore.search([...]); // MemoryVectorStore - empty results
|
|
120
|
+
platform.llm.complete('prompt'); // MockLLM - returns placeholder
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### ✅ Testing without External Dependencies
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// Test without Qdrant, OpenAI, Redis, etc.
|
|
127
|
+
const result = await myFunction(platform.vectorStore);
|
|
128
|
+
// Uses MemoryVectorStore automatically
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### ✅ Partial Configuration
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
await initPlatform({
|
|
135
|
+
adapters: {
|
|
136
|
+
llm: '@kb-labs/shared-openai', // Real OpenAI
|
|
137
|
+
vectorStore: null, // MemoryVectorStore fallback
|
|
138
|
+
analytics: null, // NoOpAnalytics fallback
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Only LLM calls hit real API, rest are no-op
|
|
143
|
+
await platform.llm.complete('prompt'); // → OpenAI API call
|
|
144
|
+
await platform.analytics.track('event'); // → silent no-op
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### NoOp Behavior by Service
|
|
148
|
+
|
|
149
|
+
| Service | NoOp Behavior | Production Adapter |
|
|
150
|
+
|---------|---------------|-------------------|
|
|
151
|
+
| **analytics** | Silent (no tracking) | `@kb-labs/analytics-adapter` |
|
|
152
|
+
| **vectorStore** | In-memory (empty) | `@kb-labs/mind-qdrant` |
|
|
153
|
+
| **llm** | Mock responses | `@kb-labs/shared-openai` |
|
|
154
|
+
| **embeddings** | Deterministic vectors | `@kb-labs/shared-openai` |
|
|
155
|
+
| **cache** | In-memory Map | `@kb-labs/core-redis` |
|
|
156
|
+
| **storage** | In-memory Map | `@kb-labs/core-fs` |
|
|
157
|
+
| **logger** | Console output | `@kb-labs/core-pino` |
|
|
158
|
+
| **eventBus** | In-memory EventEmitter | Custom (future) |
|
|
159
|
+
| **invoke** | Throws error | Custom (future) |
|
|
160
|
+
| **artifacts** | In-memory storage | Custom (future) |
|
|
161
|
+
|
|
162
|
+
**Core Features** also have NoOp fallbacks but may throw errors for critical operations:
|
|
163
|
+
- `workflows.execute()` - throws (workflow execution is critical)
|
|
164
|
+
- `jobs.submit()` - executes synchronously (degraded mode)
|
|
165
|
+
- `resources.acquireSlot()` - always succeeds (no quota enforcement)
|
|
166
|
+
|
|
167
|
+
## API Reference
|
|
168
|
+
|
|
169
|
+
### `initPlatform(config?: PlatformConfig): Promise<PlatformContainer>`
|
|
170
|
+
|
|
171
|
+
Initialize the platform with configuration. Loads adapters and initializes core features.
|
|
172
|
+
|
|
173
|
+
### `platform: PlatformContainer`
|
|
174
|
+
|
|
175
|
+
Global singleton container. Access services through properties:
|
|
176
|
+
|
|
177
|
+
**Adapters:**
|
|
178
|
+
- `platform.analytics: IAnalytics`
|
|
179
|
+
- `platform.vectorStore: IVectorStore`
|
|
180
|
+
- `platform.llm: ILLM`
|
|
181
|
+
- `platform.embeddings: IEmbeddings`
|
|
182
|
+
- `platform.cache: ICache`
|
|
183
|
+
- `platform.storage: IStorage`
|
|
184
|
+
- `platform.logger: ILogger`
|
|
185
|
+
- `platform.eventBus: IEventBus`
|
|
186
|
+
- `platform.invoke: IInvoke`
|
|
187
|
+
- `platform.artifacts: IArtifacts`
|
|
188
|
+
|
|
189
|
+
**Core Features:**
|
|
190
|
+
- `platform.workflows: IWorkflowEngine`
|
|
191
|
+
- `platform.jobs: IJobScheduler`
|
|
192
|
+
- `platform.cron: ICronManager`
|
|
193
|
+
- `platform.resources: IResourceManager`
|
|
194
|
+
|
|
195
|
+
### `resetPlatform(): void`
|
|
196
|
+
|
|
197
|
+
Reset the platform (for testing).
|
|
198
|
+
|
|
199
|
+
## Adapter Manifest System
|
|
200
|
+
|
|
201
|
+
**NEW:** Version 1.0.0 enables multiple adapters of the same type with explicit dependencies and extension points.
|
|
202
|
+
|
|
203
|
+
### Key Features
|
|
204
|
+
|
|
205
|
+
- ✅ **Multiple adapters per type** - Run multiple loggers, storages, etc.
|
|
206
|
+
- ✅ **Explicit dependencies** - Declare required/optional adapter dependencies
|
|
207
|
+
- ✅ **Extension points** - Connect adapters via hooks (e.g., `logger.onLog()`)
|
|
208
|
+
- ✅ **Topological sorting** - Automatic dependency resolution
|
|
209
|
+
- ✅ **Circular dependency detection** - Fail-fast validation
|
|
210
|
+
- ✅ **Priority ordering** - Control extension execution order
|
|
211
|
+
|
|
212
|
+
### Example: Logger with Extensions
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
// Configure logger + extensions
|
|
216
|
+
await initPlatform({
|
|
217
|
+
adapters: {
|
|
218
|
+
logger: '@kb-labs/adapters-pino',
|
|
219
|
+
logRingBuffer: '@kb-labs/adapters-log-ring-buffer',
|
|
220
|
+
logPersistence: '@kb-labs/adapters-log-sqlite',
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Core logger (type-safe)
|
|
225
|
+
platform.logger.info('message');
|
|
226
|
+
|
|
227
|
+
// Extension adapters (generic)
|
|
228
|
+
const buffer = platform.getAdapter<ILogRingBuffer>('logRingBuffer');
|
|
229
|
+
const records = buffer?.getRecords();
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Type System
|
|
233
|
+
|
|
234
|
+
**Core Adapters** - Known at compile time:
|
|
235
|
+
```typescript
|
|
236
|
+
platform.setAdapter('logger', pinoLogger); // Type: ILogger
|
|
237
|
+
const logger = platform.getAdapter('logger'); // ILogger | undefined
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
**Extension Adapters** - Dynamic:
|
|
241
|
+
```typescript
|
|
242
|
+
platform.setAdapter('logRingBuffer', ringBuffer);
|
|
243
|
+
const buffer = platform.getAdapter<ILogRingBuffer>('logRingBuffer');
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Documentation
|
|
247
|
+
|
|
248
|
+
See [Adapter Manifest Guide](../../docs/ADAPTER_MANIFEST_GUIDE.md) for complete documentation.
|
|
249
|
+
|
|
250
|
+
## Rules
|
|
251
|
+
|
|
252
|
+
1. **Depends only on core-platform** - no other @kb-labs/* packages
|
|
253
|
+
2. **Singleton pattern** - `platform` is the global instance
|
|
254
|
+
3. **Lazy NoOp fallback** - if adapter not configured, uses NoOp
|
|
255
|
+
4. **Manifest required** - all adapters must export manifest metadata
|
|
256
|
+
|
|
257
|
+
## ADR
|
|
258
|
+
|
|
259
|
+
- [ADR-0040: Platform Core Adapter Architecture](../../docs/adr/0040-platform-core-adapter-architecture.md)
|
|
260
|
+
- [ADR-0043: Adapter Manifest System](../../docs/adr/0043-adapter-manifest-system.md)
|