@kb-labs/core-platform 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 +108 -0
- package/dist/adapters/index.cjs +26 -0
- package/dist/adapters/index.cjs.map +1 -0
- package/dist/adapters/index.d.cts +125 -0
- package/dist/adapters/index.d.ts +125 -0
- package/dist/adapters/index.js +21 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/artifacts-BUghvkUU.d.cts +273 -0
- package/dist/artifacts-Bd-1UVTw.d.ts +273 -0
- package/dist/artifacts-DrVnkLzu.d.cts +1374 -0
- package/dist/artifacts-DrVnkLzu.d.ts +1374 -0
- package/dist/core/index.cjs +4 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +2 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +3 -0
- package/dist/core/index.js.map +1 -0
- package/dist/database-DGV6a1nj.d.cts +427 -0
- package/dist/database-DGV6a1nj.d.ts +427 -0
- package/dist/index.cjs +1405 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +579 -0
- package/dist/index.d.ts +579 -0
- package/dist/index.js +1381 -0
- package/dist/index.js.map +1 -0
- package/dist/log-reader-BVohbSMB.d.cts +314 -0
- package/dist/log-reader-uOHBLBax.d.ts +314 -0
- package/dist/noop/adapters/index.cjs +656 -0
- package/dist/noop/adapters/index.cjs.map +1 -0
- package/dist/noop/adapters/index.d.cts +71 -0
- package/dist/noop/adapters/index.d.ts +71 -0
- package/dist/noop/adapters/index.js +637 -0
- package/dist/noop/adapters/index.js.map +1 -0
- package/dist/noop/core/index.cjs +217 -0
- package/dist/noop/core/index.cjs.map +1 -0
- package/dist/noop/core/index.d.cts +94 -0
- package/dist/noop/core/index.d.ts +94 -0
- package/dist/noop/core/index.js +212 -0
- package/dist/noop/core/index.js.map +1 -0
- package/dist/noop/index.cjs +806 -0
- package/dist/noop/index.cjs.map +1 -0
- package/dist/noop/index.d.cts +36 -0
- package/dist/noop/index.d.ts +36 -0
- package/dist/noop/index.js +787 -0
- package/dist/noop/index.js.map +1 -0
- package/dist/resources-DaufJFad.d.cts +419 -0
- package/dist/resources-DaufJFad.d.ts +419 -0
- package/dist/serializable/index.cjs +162 -0
- package/dist/serializable/index.cjs.map +1 -0
- package/dist/serializable/index.d.cts +352 -0
- package/dist/serializable/index.d.ts +352 -0
- package/dist/serializable/index.js +152 -0
- package/dist/serializable/index.js.map +1 -0
- package/dist/snapshot-provider--COac4P-.d.ts +923 -0
- package/dist/snapshot-provider-nE9wuc1C.d.cts +923 -0
- package/package.json +92 -0
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @kb-labs/core-platform/serializable
|
|
3
|
+
* IPC serialization types for cross-process adapter communication.
|
|
4
|
+
*
|
|
5
|
+
* This module defines the protocol for serializing adapter method calls
|
|
6
|
+
* and responses across process boundaries using IPC (Inter-Process Communication).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { AdapterCall, AdapterResponse } from '@kb-labs/core-platform/serializable';
|
|
11
|
+
*
|
|
12
|
+
* const call: AdapterCall = {
|
|
13
|
+
* type: 'adapter:call',
|
|
14
|
+
* requestId: 'uuid-123',
|
|
15
|
+
* adapter: 'vectorStore',
|
|
16
|
+
* method: 'search',
|
|
17
|
+
* args: [[0.1, 0.2, 0.3], 10],
|
|
18
|
+
* timeout: 30000,
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Special type for serialized Buffer instances.
|
|
24
|
+
* Buffers are converted to base64 strings for transmission.
|
|
25
|
+
*/
|
|
26
|
+
interface SerializableBuffer {
|
|
27
|
+
__type: 'Buffer';
|
|
28
|
+
data: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Special type for serialized Date instances.
|
|
32
|
+
* Dates are converted to ISO 8601 strings for transmission.
|
|
33
|
+
*/
|
|
34
|
+
interface SerializableDate {
|
|
35
|
+
__type: 'Date';
|
|
36
|
+
iso: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Special type for serialized Error instances.
|
|
40
|
+
* Errors are converted to structured objects with name, message, stack, and optional code.
|
|
41
|
+
*/
|
|
42
|
+
interface SerializableError {
|
|
43
|
+
__type: 'Error';
|
|
44
|
+
name: string;
|
|
45
|
+
message: string;
|
|
46
|
+
stack?: string;
|
|
47
|
+
code?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Values that can be safely serialized over IPC/network.
|
|
51
|
+
*
|
|
52
|
+
* Supports:
|
|
53
|
+
* - Primitives: null, boolean, number, string
|
|
54
|
+
* - Special types: Buffer, Date, Error
|
|
55
|
+
* - Collections: Array, Object
|
|
56
|
+
*
|
|
57
|
+
* Does NOT support:
|
|
58
|
+
* - Functions
|
|
59
|
+
* - Symbols (except as object keys after serialization)
|
|
60
|
+
* - WeakMap/WeakSet
|
|
61
|
+
* - Circular references (will be detected and throw error)
|
|
62
|
+
*/
|
|
63
|
+
type SerializableValue = null | boolean | number | string | SerializableBuffer | SerializableDate | SerializableError | SerializableArray | SerializableObject;
|
|
64
|
+
/**
|
|
65
|
+
* Serializable array - all elements must be SerializableValue
|
|
66
|
+
*/
|
|
67
|
+
type SerializableArray = SerializableValue[];
|
|
68
|
+
/**
|
|
69
|
+
* Serializable object - all values must be SerializableValue
|
|
70
|
+
*/
|
|
71
|
+
type SerializableObject = {
|
|
72
|
+
[key: string]: SerializableValue;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Adapter types supported by the IPC protocol.
|
|
76
|
+
*
|
|
77
|
+
* These correspond to the adapters available in PlatformContainer:
|
|
78
|
+
* - vectorStore: IVectorStore
|
|
79
|
+
* - cache: ICache
|
|
80
|
+
* - llm: ILLM
|
|
81
|
+
* - embeddings: IEmbeddings
|
|
82
|
+
* - storage: IStorage
|
|
83
|
+
* - logger: ILogger
|
|
84
|
+
* - analytics: IAnalytics
|
|
85
|
+
* - eventBus: IEventBus
|
|
86
|
+
* - invoke: IInvoke
|
|
87
|
+
* - artifacts: IArtifacts
|
|
88
|
+
*/
|
|
89
|
+
type AdapterType = 'vectorStore' | 'cache' | 'config' | 'llm' | 'embeddings' | 'storage' | 'logger' | 'analytics' | 'eventBus' | 'invoke' | 'artifacts' | 'database.document' | 'database.sql';
|
|
90
|
+
/**
|
|
91
|
+
* Execution context for adapter calls.
|
|
92
|
+
*
|
|
93
|
+
* Used for tracing, debugging, security validation, and metrics.
|
|
94
|
+
* All fields are optional to maintain backward compatibility.
|
|
95
|
+
*/
|
|
96
|
+
interface AdapterCallContext {
|
|
97
|
+
/** Trace ID for distributed tracing (spans entire CLI → Worker → Adapter → Service flow) */
|
|
98
|
+
traceId?: string;
|
|
99
|
+
/** Session ID for user session tracking */
|
|
100
|
+
sessionId?: string;
|
|
101
|
+
/** Plugin ID making the adapter call */
|
|
102
|
+
pluginId?: string;
|
|
103
|
+
/** Workspace ID for multi-tenant scenarios */
|
|
104
|
+
workspaceId?: string;
|
|
105
|
+
/** Tenant ID for multi-tenant quota enforcement */
|
|
106
|
+
tenantId?: string;
|
|
107
|
+
/** Plugin permissions snapshot (for adapter-level validation) */
|
|
108
|
+
permissions?: {
|
|
109
|
+
/** Allowed adapter access (e.g., ['vectorStore', 'cache']) */
|
|
110
|
+
adapters?: string[];
|
|
111
|
+
/** Allowed storage paths (e.g., ['.kb/**', 'docs/**']) */
|
|
112
|
+
storagePaths?: string[];
|
|
113
|
+
/** Allowed network hosts (e.g., ['api.openai.com']) */
|
|
114
|
+
networkHosts?: string[];
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* IPC protocol version.
|
|
119
|
+
*
|
|
120
|
+
* Version history:
|
|
121
|
+
* - v1: Initial implementation (requestId, adapter, method, args, timeout)
|
|
122
|
+
* - v2: Added context (traceId, pluginId, sessionId, tenantId, permissions)
|
|
123
|
+
*
|
|
124
|
+
* When making breaking changes:
|
|
125
|
+
* 1. Increment version
|
|
126
|
+
* 2. Update IPCServer to handle both old and new versions
|
|
127
|
+
* 3. Update ADR-0038 with migration guide
|
|
128
|
+
*/
|
|
129
|
+
declare const IPC_PROTOCOL_VERSION = 2;
|
|
130
|
+
/**
|
|
131
|
+
* Adapter method call that can be sent over IPC.
|
|
132
|
+
*
|
|
133
|
+
* Represents a request from child process to parent process
|
|
134
|
+
* to execute a method on an adapter.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const call: AdapterCall = {
|
|
139
|
+
* version: 2,
|
|
140
|
+
* type: 'adapter:call',
|
|
141
|
+
* requestId: 'uuid-123',
|
|
142
|
+
* adapter: 'vectorStore',
|
|
143
|
+
* method: 'search',
|
|
144
|
+
* args: [[0.1, 0.2, 0.3], 10, { collectionId: 'docs' }],
|
|
145
|
+
* timeout: 30000,
|
|
146
|
+
* context: {
|
|
147
|
+
* traceId: 'trace-abc',
|
|
148
|
+
* pluginId: '@kb-labs/mind',
|
|
149
|
+
* sessionId: 'session-xyz',
|
|
150
|
+
* },
|
|
151
|
+
* };
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
interface AdapterCall {
|
|
155
|
+
/** Protocol version for backward compatibility */
|
|
156
|
+
version: number;
|
|
157
|
+
type: 'adapter:call';
|
|
158
|
+
/** Unique request ID to match with response */
|
|
159
|
+
requestId: string;
|
|
160
|
+
/** Adapter to call */
|
|
161
|
+
adapter: AdapterType;
|
|
162
|
+
/** Method name to call on adapter */
|
|
163
|
+
method: string;
|
|
164
|
+
/** Serialized method arguments */
|
|
165
|
+
args: SerializableValue[];
|
|
166
|
+
/** Optional timeout in milliseconds (default: 30000) */
|
|
167
|
+
timeout?: number;
|
|
168
|
+
/** Optional execution context for tracing, debugging, security (v2+) */
|
|
169
|
+
context?: AdapterCallContext;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Response from adapter method call.
|
|
173
|
+
*
|
|
174
|
+
* Represents a response from parent process back to child process
|
|
175
|
+
* after executing an adapter method.
|
|
176
|
+
*
|
|
177
|
+
* Either `result` or `error` will be present, never both.
|
|
178
|
+
*
|
|
179
|
+
* @example Success:
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const response: AdapterResponse = {
|
|
182
|
+
* type: 'adapter:response',
|
|
183
|
+
* requestId: 'uuid-123',
|
|
184
|
+
* result: [{ id: '1', score: 0.95, metadata: {} }],
|
|
185
|
+
* };
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @example Error:
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const response: AdapterResponse = {
|
|
191
|
+
* type: 'adapter:response',
|
|
192
|
+
* requestId: 'uuid-123',
|
|
193
|
+
* error: {
|
|
194
|
+
* __type: 'Error',
|
|
195
|
+
* name: 'VectorStoreError',
|
|
196
|
+
* message: 'Collection not found',
|
|
197
|
+
* stack: '...',
|
|
198
|
+
* },
|
|
199
|
+
* };
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
interface AdapterResponse {
|
|
203
|
+
type: 'adapter:response';
|
|
204
|
+
/** Request ID this response corresponds to */
|
|
205
|
+
requestId: string;
|
|
206
|
+
/** Serialized result (if successful) */
|
|
207
|
+
result?: SerializableValue;
|
|
208
|
+
/** Serialized error (if failed) */
|
|
209
|
+
error?: SerializableError;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* All possible IPC messages.
|
|
213
|
+
*
|
|
214
|
+
* Child → Parent: AdapterCall
|
|
215
|
+
* Parent → Child: AdapterResponse
|
|
216
|
+
*/
|
|
217
|
+
type IPCMessage = AdapterCall | AdapterResponse;
|
|
218
|
+
/**
|
|
219
|
+
* Type guard to check if message is an AdapterCall.
|
|
220
|
+
*
|
|
221
|
+
* Supports both v1 (no version field) and v2+ (with version field)
|
|
222
|
+
* for backward compatibility.
|
|
223
|
+
*/
|
|
224
|
+
declare function isAdapterCall(msg: unknown): msg is AdapterCall;
|
|
225
|
+
/**
|
|
226
|
+
* Type guard to check if message is an AdapterResponse.
|
|
227
|
+
*/
|
|
228
|
+
declare function isAdapterResponse(msg: unknown): msg is AdapterResponse;
|
|
229
|
+
/**
|
|
230
|
+
* Error thrown when serialization fails.
|
|
231
|
+
*/
|
|
232
|
+
declare class SerializationError extends Error {
|
|
233
|
+
constructor(message: string);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Error thrown when deserialization fails.
|
|
237
|
+
*/
|
|
238
|
+
declare class DeserializationError extends Error {
|
|
239
|
+
constructor(message: string);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* @module @kb-labs/core-platform/serializable
|
|
244
|
+
* Serializer for cross-process communication.
|
|
245
|
+
*
|
|
246
|
+
* Handles serialization/deserialization of complex types:
|
|
247
|
+
* - Buffer (to/from base64)
|
|
248
|
+
* - Date (to/from ISO 8601)
|
|
249
|
+
* - Error (with stack traces)
|
|
250
|
+
* - Circular reference detection
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* import { serialize, deserialize } from '@kb-labs/core-platform/serializable';
|
|
255
|
+
*
|
|
256
|
+
* const data = {
|
|
257
|
+
* buffer: Buffer.from('hello'),
|
|
258
|
+
* date: new Date(),
|
|
259
|
+
* error: new Error('test'),
|
|
260
|
+
* nested: { values: [1, 2, 3] },
|
|
261
|
+
* };
|
|
262
|
+
*
|
|
263
|
+
* const serialized = serialize(data);
|
|
264
|
+
* const deserialized = deserialize(serialized);
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Serialize value for IPC transmission.
|
|
270
|
+
*
|
|
271
|
+
* Handles special types:
|
|
272
|
+
* - Buffer → base64 string
|
|
273
|
+
* - Date → ISO 8601 string
|
|
274
|
+
* - Error → structured object with stack
|
|
275
|
+
* - Detects and throws on circular references
|
|
276
|
+
*
|
|
277
|
+
* @param value - Value to serialize
|
|
278
|
+
* @returns Serialized value safe for JSON transmission
|
|
279
|
+
* @throws SerializationError if value contains unsupported types or circular refs
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const result = serialize({
|
|
284
|
+
* buf: Buffer.from('hello'),
|
|
285
|
+
* date: new Date('2025-01-01'),
|
|
286
|
+
* error: new Error('test'),
|
|
287
|
+
* });
|
|
288
|
+
* // result = {
|
|
289
|
+
* // buf: { __type: 'Buffer', data: 'aGVsbG8=' },
|
|
290
|
+
* // date: { __type: 'Date', iso: '2025-01-01T00:00:00.000Z' },
|
|
291
|
+
* // error: { __type: 'Error', name: 'Error', message: 'test', stack: '...' },
|
|
292
|
+
* // }
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
declare function serialize(value: unknown): SerializableValue;
|
|
296
|
+
/**
|
|
297
|
+
* Deserialize value from IPC transmission.
|
|
298
|
+
*
|
|
299
|
+
* Reconstructs special types:
|
|
300
|
+
* - base64 string → Buffer
|
|
301
|
+
* - ISO 8601 string → Date
|
|
302
|
+
* - structured object → Error (with stack)
|
|
303
|
+
*
|
|
304
|
+
* @param value - Serialized value
|
|
305
|
+
* @returns Original value reconstructed from serialization
|
|
306
|
+
* @throws DeserializationError if value format is invalid
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* const serialized = {
|
|
311
|
+
* buf: { __type: 'Buffer', data: 'aGVsbG8=' },
|
|
312
|
+
* date: { __type: 'Date', iso: '2025-01-01T00:00:00.000Z' },
|
|
313
|
+
* error: { __type: 'Error', name: 'Error', message: 'test' },
|
|
314
|
+
* };
|
|
315
|
+
*
|
|
316
|
+
* const result = deserialize(serialized);
|
|
317
|
+
* // result = {
|
|
318
|
+
* // buf: Buffer.from('hello'),
|
|
319
|
+
* // date: new Date('2025-01-01'),
|
|
320
|
+
* // error: Error('test'),
|
|
321
|
+
* // }
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
declare function deserialize(value: SerializableValue): unknown;
|
|
325
|
+
/**
|
|
326
|
+
* Serialize multiple values (convenience for adapter method args).
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```typescript
|
|
330
|
+
* const args = serializeArgs([
|
|
331
|
+
* Buffer.from('hello'),
|
|
332
|
+
* new Date(),
|
|
333
|
+
* { nested: { value: 42 } },
|
|
334
|
+
* ]);
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
declare function serializeArgs(args: unknown[]): SerializableValue[];
|
|
338
|
+
/**
|
|
339
|
+
* Deserialize multiple values (convenience for adapter method args).
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```typescript
|
|
343
|
+
* const args = deserializeArgs([
|
|
344
|
+
* { __type: 'Buffer', data: 'aGVsbG8=' },
|
|
345
|
+
* { __type: 'Date', iso: '2025-01-01T00:00:00.000Z' },
|
|
346
|
+
* { nested: { value: 42 } },
|
|
347
|
+
* ]);
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
declare function deserializeArgs(args: SerializableValue[]): unknown[];
|
|
351
|
+
|
|
352
|
+
export { type AdapterCall, type AdapterCallContext, type AdapterResponse, type AdapterType, DeserializationError, type IPCMessage, IPC_PROTOCOL_VERSION, type SerializableArray, type SerializableBuffer, type SerializableDate, type SerializableError, type SerializableObject, type SerializableValue, SerializationError, deserialize, deserializeArgs, isAdapterCall, isAdapterResponse, serialize, serializeArgs };
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @kb-labs/core-platform/serializable
|
|
3
|
+
* IPC serialization types for cross-process adapter communication.
|
|
4
|
+
*
|
|
5
|
+
* This module defines the protocol for serializing adapter method calls
|
|
6
|
+
* and responses across process boundaries using IPC (Inter-Process Communication).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { AdapterCall, AdapterResponse } from '@kb-labs/core-platform/serializable';
|
|
11
|
+
*
|
|
12
|
+
* const call: AdapterCall = {
|
|
13
|
+
* type: 'adapter:call',
|
|
14
|
+
* requestId: 'uuid-123',
|
|
15
|
+
* adapter: 'vectorStore',
|
|
16
|
+
* method: 'search',
|
|
17
|
+
* args: [[0.1, 0.2, 0.3], 10],
|
|
18
|
+
* timeout: 30000,
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Special type for serialized Buffer instances.
|
|
24
|
+
* Buffers are converted to base64 strings for transmission.
|
|
25
|
+
*/
|
|
26
|
+
interface SerializableBuffer {
|
|
27
|
+
__type: 'Buffer';
|
|
28
|
+
data: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Special type for serialized Date instances.
|
|
32
|
+
* Dates are converted to ISO 8601 strings for transmission.
|
|
33
|
+
*/
|
|
34
|
+
interface SerializableDate {
|
|
35
|
+
__type: 'Date';
|
|
36
|
+
iso: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Special type for serialized Error instances.
|
|
40
|
+
* Errors are converted to structured objects with name, message, stack, and optional code.
|
|
41
|
+
*/
|
|
42
|
+
interface SerializableError {
|
|
43
|
+
__type: 'Error';
|
|
44
|
+
name: string;
|
|
45
|
+
message: string;
|
|
46
|
+
stack?: string;
|
|
47
|
+
code?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Values that can be safely serialized over IPC/network.
|
|
51
|
+
*
|
|
52
|
+
* Supports:
|
|
53
|
+
* - Primitives: null, boolean, number, string
|
|
54
|
+
* - Special types: Buffer, Date, Error
|
|
55
|
+
* - Collections: Array, Object
|
|
56
|
+
*
|
|
57
|
+
* Does NOT support:
|
|
58
|
+
* - Functions
|
|
59
|
+
* - Symbols (except as object keys after serialization)
|
|
60
|
+
* - WeakMap/WeakSet
|
|
61
|
+
* - Circular references (will be detected and throw error)
|
|
62
|
+
*/
|
|
63
|
+
type SerializableValue = null | boolean | number | string | SerializableBuffer | SerializableDate | SerializableError | SerializableArray | SerializableObject;
|
|
64
|
+
/**
|
|
65
|
+
* Serializable array - all elements must be SerializableValue
|
|
66
|
+
*/
|
|
67
|
+
type SerializableArray = SerializableValue[];
|
|
68
|
+
/**
|
|
69
|
+
* Serializable object - all values must be SerializableValue
|
|
70
|
+
*/
|
|
71
|
+
type SerializableObject = {
|
|
72
|
+
[key: string]: SerializableValue;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Adapter types supported by the IPC protocol.
|
|
76
|
+
*
|
|
77
|
+
* These correspond to the adapters available in PlatformContainer:
|
|
78
|
+
* - vectorStore: IVectorStore
|
|
79
|
+
* - cache: ICache
|
|
80
|
+
* - llm: ILLM
|
|
81
|
+
* - embeddings: IEmbeddings
|
|
82
|
+
* - storage: IStorage
|
|
83
|
+
* - logger: ILogger
|
|
84
|
+
* - analytics: IAnalytics
|
|
85
|
+
* - eventBus: IEventBus
|
|
86
|
+
* - invoke: IInvoke
|
|
87
|
+
* - artifacts: IArtifacts
|
|
88
|
+
*/
|
|
89
|
+
type AdapterType = 'vectorStore' | 'cache' | 'config' | 'llm' | 'embeddings' | 'storage' | 'logger' | 'analytics' | 'eventBus' | 'invoke' | 'artifacts' | 'database.document' | 'database.sql';
|
|
90
|
+
/**
|
|
91
|
+
* Execution context for adapter calls.
|
|
92
|
+
*
|
|
93
|
+
* Used for tracing, debugging, security validation, and metrics.
|
|
94
|
+
* All fields are optional to maintain backward compatibility.
|
|
95
|
+
*/
|
|
96
|
+
interface AdapterCallContext {
|
|
97
|
+
/** Trace ID for distributed tracing (spans entire CLI → Worker → Adapter → Service flow) */
|
|
98
|
+
traceId?: string;
|
|
99
|
+
/** Session ID for user session tracking */
|
|
100
|
+
sessionId?: string;
|
|
101
|
+
/** Plugin ID making the adapter call */
|
|
102
|
+
pluginId?: string;
|
|
103
|
+
/** Workspace ID for multi-tenant scenarios */
|
|
104
|
+
workspaceId?: string;
|
|
105
|
+
/** Tenant ID for multi-tenant quota enforcement */
|
|
106
|
+
tenantId?: string;
|
|
107
|
+
/** Plugin permissions snapshot (for adapter-level validation) */
|
|
108
|
+
permissions?: {
|
|
109
|
+
/** Allowed adapter access (e.g., ['vectorStore', 'cache']) */
|
|
110
|
+
adapters?: string[];
|
|
111
|
+
/** Allowed storage paths (e.g., ['.kb/**', 'docs/**']) */
|
|
112
|
+
storagePaths?: string[];
|
|
113
|
+
/** Allowed network hosts (e.g., ['api.openai.com']) */
|
|
114
|
+
networkHosts?: string[];
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* IPC protocol version.
|
|
119
|
+
*
|
|
120
|
+
* Version history:
|
|
121
|
+
* - v1: Initial implementation (requestId, adapter, method, args, timeout)
|
|
122
|
+
* - v2: Added context (traceId, pluginId, sessionId, tenantId, permissions)
|
|
123
|
+
*
|
|
124
|
+
* When making breaking changes:
|
|
125
|
+
* 1. Increment version
|
|
126
|
+
* 2. Update IPCServer to handle both old and new versions
|
|
127
|
+
* 3. Update ADR-0038 with migration guide
|
|
128
|
+
*/
|
|
129
|
+
declare const IPC_PROTOCOL_VERSION = 2;
|
|
130
|
+
/**
|
|
131
|
+
* Adapter method call that can be sent over IPC.
|
|
132
|
+
*
|
|
133
|
+
* Represents a request from child process to parent process
|
|
134
|
+
* to execute a method on an adapter.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const call: AdapterCall = {
|
|
139
|
+
* version: 2,
|
|
140
|
+
* type: 'adapter:call',
|
|
141
|
+
* requestId: 'uuid-123',
|
|
142
|
+
* adapter: 'vectorStore',
|
|
143
|
+
* method: 'search',
|
|
144
|
+
* args: [[0.1, 0.2, 0.3], 10, { collectionId: 'docs' }],
|
|
145
|
+
* timeout: 30000,
|
|
146
|
+
* context: {
|
|
147
|
+
* traceId: 'trace-abc',
|
|
148
|
+
* pluginId: '@kb-labs/mind',
|
|
149
|
+
* sessionId: 'session-xyz',
|
|
150
|
+
* },
|
|
151
|
+
* };
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
interface AdapterCall {
|
|
155
|
+
/** Protocol version for backward compatibility */
|
|
156
|
+
version: number;
|
|
157
|
+
type: 'adapter:call';
|
|
158
|
+
/** Unique request ID to match with response */
|
|
159
|
+
requestId: string;
|
|
160
|
+
/** Adapter to call */
|
|
161
|
+
adapter: AdapterType;
|
|
162
|
+
/** Method name to call on adapter */
|
|
163
|
+
method: string;
|
|
164
|
+
/** Serialized method arguments */
|
|
165
|
+
args: SerializableValue[];
|
|
166
|
+
/** Optional timeout in milliseconds (default: 30000) */
|
|
167
|
+
timeout?: number;
|
|
168
|
+
/** Optional execution context for tracing, debugging, security (v2+) */
|
|
169
|
+
context?: AdapterCallContext;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Response from adapter method call.
|
|
173
|
+
*
|
|
174
|
+
* Represents a response from parent process back to child process
|
|
175
|
+
* after executing an adapter method.
|
|
176
|
+
*
|
|
177
|
+
* Either `result` or `error` will be present, never both.
|
|
178
|
+
*
|
|
179
|
+
* @example Success:
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const response: AdapterResponse = {
|
|
182
|
+
* type: 'adapter:response',
|
|
183
|
+
* requestId: 'uuid-123',
|
|
184
|
+
* result: [{ id: '1', score: 0.95, metadata: {} }],
|
|
185
|
+
* };
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @example Error:
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const response: AdapterResponse = {
|
|
191
|
+
* type: 'adapter:response',
|
|
192
|
+
* requestId: 'uuid-123',
|
|
193
|
+
* error: {
|
|
194
|
+
* __type: 'Error',
|
|
195
|
+
* name: 'VectorStoreError',
|
|
196
|
+
* message: 'Collection not found',
|
|
197
|
+
* stack: '...',
|
|
198
|
+
* },
|
|
199
|
+
* };
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
interface AdapterResponse {
|
|
203
|
+
type: 'adapter:response';
|
|
204
|
+
/** Request ID this response corresponds to */
|
|
205
|
+
requestId: string;
|
|
206
|
+
/** Serialized result (if successful) */
|
|
207
|
+
result?: SerializableValue;
|
|
208
|
+
/** Serialized error (if failed) */
|
|
209
|
+
error?: SerializableError;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* All possible IPC messages.
|
|
213
|
+
*
|
|
214
|
+
* Child → Parent: AdapterCall
|
|
215
|
+
* Parent → Child: AdapterResponse
|
|
216
|
+
*/
|
|
217
|
+
type IPCMessage = AdapterCall | AdapterResponse;
|
|
218
|
+
/**
|
|
219
|
+
* Type guard to check if message is an AdapterCall.
|
|
220
|
+
*
|
|
221
|
+
* Supports both v1 (no version field) and v2+ (with version field)
|
|
222
|
+
* for backward compatibility.
|
|
223
|
+
*/
|
|
224
|
+
declare function isAdapterCall(msg: unknown): msg is AdapterCall;
|
|
225
|
+
/**
|
|
226
|
+
* Type guard to check if message is an AdapterResponse.
|
|
227
|
+
*/
|
|
228
|
+
declare function isAdapterResponse(msg: unknown): msg is AdapterResponse;
|
|
229
|
+
/**
|
|
230
|
+
* Error thrown when serialization fails.
|
|
231
|
+
*/
|
|
232
|
+
declare class SerializationError extends Error {
|
|
233
|
+
constructor(message: string);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Error thrown when deserialization fails.
|
|
237
|
+
*/
|
|
238
|
+
declare class DeserializationError extends Error {
|
|
239
|
+
constructor(message: string);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* @module @kb-labs/core-platform/serializable
|
|
244
|
+
* Serializer for cross-process communication.
|
|
245
|
+
*
|
|
246
|
+
* Handles serialization/deserialization of complex types:
|
|
247
|
+
* - Buffer (to/from base64)
|
|
248
|
+
* - Date (to/from ISO 8601)
|
|
249
|
+
* - Error (with stack traces)
|
|
250
|
+
* - Circular reference detection
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* import { serialize, deserialize } from '@kb-labs/core-platform/serializable';
|
|
255
|
+
*
|
|
256
|
+
* const data = {
|
|
257
|
+
* buffer: Buffer.from('hello'),
|
|
258
|
+
* date: new Date(),
|
|
259
|
+
* error: new Error('test'),
|
|
260
|
+
* nested: { values: [1, 2, 3] },
|
|
261
|
+
* };
|
|
262
|
+
*
|
|
263
|
+
* const serialized = serialize(data);
|
|
264
|
+
* const deserialized = deserialize(serialized);
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Serialize value for IPC transmission.
|
|
270
|
+
*
|
|
271
|
+
* Handles special types:
|
|
272
|
+
* - Buffer → base64 string
|
|
273
|
+
* - Date → ISO 8601 string
|
|
274
|
+
* - Error → structured object with stack
|
|
275
|
+
* - Detects and throws on circular references
|
|
276
|
+
*
|
|
277
|
+
* @param value - Value to serialize
|
|
278
|
+
* @returns Serialized value safe for JSON transmission
|
|
279
|
+
* @throws SerializationError if value contains unsupported types or circular refs
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const result = serialize({
|
|
284
|
+
* buf: Buffer.from('hello'),
|
|
285
|
+
* date: new Date('2025-01-01'),
|
|
286
|
+
* error: new Error('test'),
|
|
287
|
+
* });
|
|
288
|
+
* // result = {
|
|
289
|
+
* // buf: { __type: 'Buffer', data: 'aGVsbG8=' },
|
|
290
|
+
* // date: { __type: 'Date', iso: '2025-01-01T00:00:00.000Z' },
|
|
291
|
+
* // error: { __type: 'Error', name: 'Error', message: 'test', stack: '...' },
|
|
292
|
+
* // }
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
declare function serialize(value: unknown): SerializableValue;
|
|
296
|
+
/**
|
|
297
|
+
* Deserialize value from IPC transmission.
|
|
298
|
+
*
|
|
299
|
+
* Reconstructs special types:
|
|
300
|
+
* - base64 string → Buffer
|
|
301
|
+
* - ISO 8601 string → Date
|
|
302
|
+
* - structured object → Error (with stack)
|
|
303
|
+
*
|
|
304
|
+
* @param value - Serialized value
|
|
305
|
+
* @returns Original value reconstructed from serialization
|
|
306
|
+
* @throws DeserializationError if value format is invalid
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* const serialized = {
|
|
311
|
+
* buf: { __type: 'Buffer', data: 'aGVsbG8=' },
|
|
312
|
+
* date: { __type: 'Date', iso: '2025-01-01T00:00:00.000Z' },
|
|
313
|
+
* error: { __type: 'Error', name: 'Error', message: 'test' },
|
|
314
|
+
* };
|
|
315
|
+
*
|
|
316
|
+
* const result = deserialize(serialized);
|
|
317
|
+
* // result = {
|
|
318
|
+
* // buf: Buffer.from('hello'),
|
|
319
|
+
* // date: new Date('2025-01-01'),
|
|
320
|
+
* // error: Error('test'),
|
|
321
|
+
* // }
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
declare function deserialize(value: SerializableValue): unknown;
|
|
325
|
+
/**
|
|
326
|
+
* Serialize multiple values (convenience for adapter method args).
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```typescript
|
|
330
|
+
* const args = serializeArgs([
|
|
331
|
+
* Buffer.from('hello'),
|
|
332
|
+
* new Date(),
|
|
333
|
+
* { nested: { value: 42 } },
|
|
334
|
+
* ]);
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
declare function serializeArgs(args: unknown[]): SerializableValue[];
|
|
338
|
+
/**
|
|
339
|
+
* Deserialize multiple values (convenience for adapter method args).
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```typescript
|
|
343
|
+
* const args = deserializeArgs([
|
|
344
|
+
* { __type: 'Buffer', data: 'aGVsbG8=' },
|
|
345
|
+
* { __type: 'Date', iso: '2025-01-01T00:00:00.000Z' },
|
|
346
|
+
* { nested: { value: 42 } },
|
|
347
|
+
* ]);
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
declare function deserializeArgs(args: SerializableValue[]): unknown[];
|
|
351
|
+
|
|
352
|
+
export { type AdapterCall, type AdapterCallContext, type AdapterResponse, type AdapterType, DeserializationError, type IPCMessage, IPC_PROTOCOL_VERSION, type SerializableArray, type SerializableBuffer, type SerializableDate, type SerializableError, type SerializableObject, type SerializableValue, SerializationError, deserialize, deserializeArgs, isAdapterCall, isAdapterResponse, serialize, serializeArgs };
|