@papicandela/mcx-core 0.1.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.
@@ -0,0 +1,568 @@
1
+ import { z } from 'zod';
2
+ export { z } from 'zod';
3
+
4
+ /**
5
+ * Configuration for an adapter method parameter
6
+ */
7
+ interface AdapterMethodParameter {
8
+ name: string;
9
+ type: "string" | "number" | "boolean" | "object" | "array";
10
+ description?: string;
11
+ required?: boolean;
12
+ default?: unknown;
13
+ }
14
+ /**
15
+ * Configuration for an adapter method
16
+ */
17
+ interface AdapterMethodConfig {
18
+ name: string;
19
+ description?: string;
20
+ parameters?: AdapterMethodParameter[];
21
+ handler: (...args: unknown[]) => unknown | Promise<unknown>;
22
+ }
23
+ /**
24
+ * A method exposed by an adapter
25
+ */
26
+ interface AdapterMethod {
27
+ name: string;
28
+ description?: string;
29
+ parameters: AdapterMethodParameter[];
30
+ execute: (...args: unknown[]) => unknown | Promise<unknown>;
31
+ }
32
+ /**
33
+ * Configuration for defining an adapter
34
+ */
35
+ interface AdapterConfig<TSchema extends z.ZodTypeAny = z.ZodTypeAny> {
36
+ name: string;
37
+ description?: string;
38
+ version?: string;
39
+ configSchema?: TSchema;
40
+ methods: AdapterMethodConfig[];
41
+ }
42
+ /**
43
+ * A registered adapter instance
44
+ */
45
+ interface Adapter {
46
+ name: string;
47
+ description?: string;
48
+ version: string;
49
+ methods: Map<string, AdapterMethod>;
50
+ config?: unknown;
51
+ }
52
+ /**
53
+ * Configuration for the sandbox execution environment
54
+ */
55
+ interface SandboxConfig {
56
+ /** Timeout in milliseconds (default: 5000) */
57
+ timeout?: number;
58
+ /** Memory limit in MB (default: 128) */
59
+ memoryLimit?: number;
60
+ /** Whether to allow async/await (default: true) */
61
+ allowAsync?: boolean;
62
+ /** Custom global variables to inject */
63
+ globals?: Record<string, unknown>;
64
+ }
65
+ /**
66
+ * Result from sandbox code execution
67
+ */
68
+ interface SandboxResult<T = unknown> {
69
+ success: boolean;
70
+ value?: T;
71
+ error?: {
72
+ name: string;
73
+ message: string;
74
+ stack?: string;
75
+ };
76
+ logs: string[];
77
+ executionTime: number;
78
+ }
79
+ /**
80
+ * Skill run function signature
81
+ */
82
+ type SkillRunFunction = (ctx: Record<string, unknown>) => Promise<unknown> | unknown;
83
+ /**
84
+ * Configuration for defining a skill
85
+ */
86
+ interface SkillConfig {
87
+ name: string;
88
+ description?: string;
89
+ version?: string;
90
+ /** Required adapters for this skill */
91
+ adapters?: string[];
92
+ /** The skill code to execute (sandbox mode) */
93
+ code?: string;
94
+ /** Direct run function (native mode) */
95
+ run?: SkillRunFunction;
96
+ /** Sandbox configuration overrides */
97
+ sandbox?: SandboxConfig;
98
+ }
99
+ /**
100
+ * A registered skill instance
101
+ */
102
+ interface Skill {
103
+ name: string;
104
+ description?: string;
105
+ version: string;
106
+ adapters: string[];
107
+ /** Code string for sandbox execution */
108
+ code?: string;
109
+ /** Direct run function for native execution */
110
+ run?: SkillRunFunction;
111
+ sandboxConfig: SandboxConfig;
112
+ }
113
+ /**
114
+ * Main MCX framework configuration
115
+ */
116
+ interface MCXConfig {
117
+ /** Adapters to load */
118
+ adapters?: Adapter[];
119
+ /** Skills to register */
120
+ skills?: Skill[];
121
+ /** Default sandbox configuration */
122
+ sandbox?: SandboxConfig;
123
+ /** Path to adapters directory */
124
+ adaptersDir?: string;
125
+ /** Path to skills directory */
126
+ skillsDir?: string;
127
+ }
128
+ /**
129
+ * Context passed to sandbox execution
130
+ */
131
+ interface ExecutionContext {
132
+ /** Registered adapters accessible in sandbox */
133
+ adapters: Record<string, Record<string, (...args: unknown[]) => unknown | Promise<unknown>>>;
134
+ /** Custom variables */
135
+ variables?: Record<string, unknown>;
136
+ }
137
+
138
+ /**
139
+ * Interface for sandbox implementations
140
+ *
141
+ * Sandboxes provide isolated code execution environments
142
+ * with configurable resource limits and adapter injection.
143
+ */
144
+ interface ISandbox {
145
+ /**
146
+ * Execute code in the sandbox
147
+ *
148
+ * @param code - JavaScript code to execute
149
+ * @param context - Execution context with adapters and variables
150
+ * @returns Result containing the value, logs, and execution metadata
151
+ */
152
+ execute<T = unknown>(code: string, context: ExecutionContext): Promise<SandboxResult<T>>;
153
+ /**
154
+ * Get the current sandbox configuration
155
+ */
156
+ getConfig(): SandboxConfig;
157
+ /**
158
+ * Update sandbox configuration
159
+ *
160
+ * @param config - New configuration options to merge
161
+ */
162
+ configure(config: Partial<SandboxConfig>): void;
163
+ /**
164
+ * Dispose of sandbox resources
165
+ */
166
+ dispose(): void;
167
+ }
168
+ /**
169
+ * Factory function type for creating sandbox instances
170
+ */
171
+ type SandboxFactory = (config?: SandboxConfig) => ISandbox;
172
+
173
+ /**
174
+ * Isolated-vm based sandbox implementation.
175
+ * Provides secure code execution with memory limits, timeouts, and isolation.
176
+ *
177
+ * Note: This uses isolated-vm's Context.eval() API which runs code in a
178
+ * completely isolated V8 instance - this is the secure sandboxing mechanism,
179
+ * not the dangerous global eval().
180
+ */
181
+ declare class IsolatedVMSandbox implements ISandbox {
182
+ private config;
183
+ private isolate;
184
+ constructor(config?: SandboxConfig);
185
+ /**
186
+ * Get the current sandbox configuration.
187
+ */
188
+ getConfig(): SandboxConfig;
189
+ /**
190
+ * Update sandbox configuration.
191
+ */
192
+ configure(config: Partial<SandboxConfig>): void;
193
+ /**
194
+ * Execute code within the isolated VM sandbox.
195
+ */
196
+ execute<T = unknown>(code: string, context: ExecutionContext): Promise<SandboxResult<T>>;
197
+ /**
198
+ * Inject adapters into the sandbox context.
199
+ */
200
+ private injectAdapters;
201
+ /**
202
+ * Dispose of the current isolate.
203
+ */
204
+ private disposeIsolate;
205
+ /**
206
+ * Dispose of sandbox resources.
207
+ */
208
+ dispose(): void;
209
+ }
210
+ /**
211
+ * Create a new IsolatedVMSandbox instance.
212
+ */
213
+ declare function createSandbox(config?: SandboxConfig): ISandbox;
214
+
215
+ /**
216
+ * Define an adapter with type-safe configuration and zod validation.
217
+ *
218
+ * @example
219
+ * ```ts
220
+ * const fileAdapter = defineAdapter({
221
+ * name: 'file',
222
+ * description: 'File system operations',
223
+ * configSchema: z.object({
224
+ * basePath: z.string().default('./'),
225
+ * }),
226
+ * methods: [
227
+ * {
228
+ * name: 'read',
229
+ * parameters: [
230
+ * { name: 'path', type: 'string', required: true },
231
+ * ],
232
+ * handler: (path: string) => fs.readFileSync(path, 'utf-8'),
233
+ * },
234
+ * ],
235
+ * });
236
+ * ```
237
+ */
238
+ declare function defineAdapter<TSchema extends z.ZodTypeAny = z.ZodTypeAny>(config: AdapterConfig<TSchema>, adapterConfig?: z.infer<TSchema>): Adapter;
239
+ /**
240
+ * Type helper for creating strongly-typed adapter configurations.
241
+ */
242
+ type InferAdapterConfig<T extends AdapterConfig> = T extends AdapterConfig<infer TSchema> ? z.infer<TSchema> : never;
243
+ /**
244
+ * Create an adapter factory that can be configured later.
245
+ *
246
+ * @example
247
+ * ```ts
248
+ * const createFileAdapter = createAdapterFactory({
249
+ * name: 'file',
250
+ * configSchema: z.object({ basePath: z.string() }),
251
+ * methods: [...],
252
+ * });
253
+ *
254
+ * const adapter = createFileAdapter({ basePath: '/data' });
255
+ * ```
256
+ */
257
+ declare function createAdapterFactory<TSchema extends z.ZodTypeAny>(config: AdapterConfig<TSchema>): (adapterConfig: z.infer<TSchema>) => Adapter;
258
+
259
+ /**
260
+ * Define a skill with configuration.
261
+ *
262
+ * Skills are executable code snippets that run in a sandboxed environment
263
+ * with access to registered adapters.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * const mySkill = defineSkill({
268
+ * name: 'process-data',
269
+ * description: 'Processes data using file and http adapters',
270
+ * adapters: ['file', 'http'],
271
+ * code: `
272
+ * const data = await adapters.file.read('input.json');
273
+ * const result = await adapters.http.post('/api/process', { body: data });
274
+ * return result;
275
+ * `,
276
+ * sandbox: {
277
+ * timeout: 10000,
278
+ * },
279
+ * });
280
+ * ```
281
+ */
282
+ declare function defineSkill(config: SkillConfig): Skill;
283
+ /**
284
+ * Define multiple skills at once.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * const skills = defineSkills([
289
+ * { name: 'skill1', code: '...' },
290
+ * { name: 'skill2', code: '...' },
291
+ * ]);
292
+ * ```
293
+ */
294
+ declare function defineSkills(configs: SkillConfig[]): Skill[];
295
+ /**
296
+ * Create a skill builder for fluent skill definition.
297
+ *
298
+ * @example
299
+ * ```ts
300
+ * const skill = skillBuilder('my-skill')
301
+ * .description('Does something useful')
302
+ * .requires('file', 'http')
303
+ * .timeout(10000)
304
+ * .code(`
305
+ * // skill code here
306
+ * `)
307
+ * .build();
308
+ * ```
309
+ */
310
+ declare function skillBuilder(name: string): SkillBuilder;
311
+ /**
312
+ * Fluent builder for skill configuration.
313
+ */
314
+ declare class SkillBuilder {
315
+ private config;
316
+ constructor(name: string);
317
+ /**
318
+ * Set the skill description.
319
+ */
320
+ description(description: string): this;
321
+ /**
322
+ * Set the skill version.
323
+ */
324
+ version(version: string): this;
325
+ /**
326
+ * Specify required adapters.
327
+ */
328
+ requires(...adapters: string[]): this;
329
+ /**
330
+ * Set the execution timeout in milliseconds.
331
+ */
332
+ timeout(ms: number): this;
333
+ /**
334
+ * Set the memory limit in MB.
335
+ */
336
+ memoryLimit(mb: number): this;
337
+ /**
338
+ * Set the skill code.
339
+ */
340
+ code(code: string): this;
341
+ /**
342
+ * Set custom sandbox configuration.
343
+ */
344
+ sandbox(config: SandboxConfig): this;
345
+ /**
346
+ * Build the skill.
347
+ */
348
+ build(): Skill;
349
+ }
350
+
351
+ /**
352
+ * Define the MCX framework configuration.
353
+ *
354
+ * This is typically used in an `mcx.config.ts` file at the root of your project.
355
+ *
356
+ * @example
357
+ * ```ts
358
+ * // mcx.config.ts
359
+ * import { defineConfig } from '@mcx/core';
360
+ * import { fileAdapter } from './adapters/file';
361
+ * import { httpAdapter } from './adapters/http';
362
+ *
363
+ * export default defineConfig({
364
+ * adapters: [fileAdapter, httpAdapter],
365
+ * skills: [],
366
+ * sandbox: {
367
+ * timeout: 10000,
368
+ * memoryLimit: 256,
369
+ * },
370
+ * });
371
+ * ```
372
+ */
373
+ declare function defineConfig(config: MCXConfig): MCXConfig;
374
+ /**
375
+ * Configuration builder for fluent MCX configuration.
376
+ *
377
+ * @example
378
+ * ```ts
379
+ * const config = configBuilder()
380
+ * .adapter(fileAdapter)
381
+ * .adapter(httpAdapter)
382
+ * .skill(processSkill)
383
+ * .sandbox({ timeout: 10000 })
384
+ * .build();
385
+ * ```
386
+ */
387
+ declare function configBuilder(): MCXConfigBuilder;
388
+ /**
389
+ * Fluent builder for MCX configuration.
390
+ */
391
+ declare class MCXConfigBuilder {
392
+ private config;
393
+ /**
394
+ * Add an adapter to the configuration.
395
+ */
396
+ adapter(adapter: Adapter): this;
397
+ /**
398
+ * Add multiple adapters to the configuration.
399
+ */
400
+ adapters(...adapters: Adapter[]): this;
401
+ /**
402
+ * Add a skill to the configuration.
403
+ */
404
+ skill(skill: Skill): this;
405
+ /**
406
+ * Add multiple skills to the configuration.
407
+ */
408
+ skills(...skills: Skill[]): this;
409
+ /**
410
+ * Set the sandbox configuration.
411
+ */
412
+ sandbox(config: SandboxConfig): this;
413
+ /**
414
+ * Set the adapters directory path.
415
+ */
416
+ adaptersDir(path: string): this;
417
+ /**
418
+ * Set the skills directory path.
419
+ */
420
+ skillsDir(path: string): this;
421
+ /**
422
+ * Build the configuration.
423
+ */
424
+ build(): MCXConfig;
425
+ }
426
+ /**
427
+ * Merge multiple configurations together.
428
+ * Later configurations override earlier ones.
429
+ */
430
+ declare function mergeConfigs(...configs: MCXConfig[]): MCXConfig;
431
+
432
+ /**
433
+ * Options for executor initialization.
434
+ */
435
+ interface ExecutorOptions {
436
+ /** Path to the mcx.config.ts file */
437
+ configPath?: string;
438
+ /** Initial configuration (merged with loaded config) */
439
+ config?: MCXConfig;
440
+ /** Custom sandbox factory */
441
+ sandboxFactory?: (config?: SandboxConfig) => ISandbox;
442
+ }
443
+ /**
444
+ * Main MCX executor class.
445
+ *
446
+ * Manages adapters, skills, and code execution in a sandboxed environment.
447
+ *
448
+ * @example
449
+ * ```ts
450
+ * const executor = new MCXExecutor();
451
+ * await executor.loadConfig('./mcx.config.ts');
452
+ *
453
+ * // Execute raw code
454
+ * const result = await executor.execute(`
455
+ * const data = await adapters.file.read('input.txt');
456
+ * return data.toUpperCase();
457
+ * `);
458
+ *
459
+ * // Run a registered skill
460
+ * const skillResult = await executor.runSkill('process-data');
461
+ * ```
462
+ */
463
+ declare class MCXExecutor {
464
+ private adapters;
465
+ private skills;
466
+ private sandboxConfig;
467
+ private sandboxFactory;
468
+ private configPath?;
469
+ constructor(options?: ExecutorOptions);
470
+ /**
471
+ * Load configuration from an mcx.config.ts file.
472
+ *
473
+ * @param configPath - Path to the configuration file
474
+ */
475
+ loadConfig(configPath?: string): Promise<void>;
476
+ /**
477
+ * Register an adapter.
478
+ *
479
+ * @param adapter - The adapter to register
480
+ */
481
+ registerAdapter(adapter: Adapter): void;
482
+ /**
483
+ * Unregister an adapter.
484
+ *
485
+ * @param name - The adapter name
486
+ * @returns Whether the adapter was removed
487
+ */
488
+ unregisterAdapter(name: string): boolean;
489
+ /**
490
+ * Get a registered adapter by name.
491
+ *
492
+ * @param name - The adapter name
493
+ */
494
+ getAdapter(name: string): Adapter | undefined;
495
+ /**
496
+ * Get all registered adapter names.
497
+ */
498
+ getAdapterNames(): string[];
499
+ /**
500
+ * Register a skill.
501
+ *
502
+ * @param skill - The skill to register
503
+ */
504
+ registerSkill(skill: Skill): void;
505
+ /**
506
+ * Unregister a skill.
507
+ *
508
+ * @param name - The skill name
509
+ * @returns Whether the skill was removed
510
+ */
511
+ unregisterSkill(name: string): boolean;
512
+ /**
513
+ * Get a registered skill by name.
514
+ *
515
+ * @param name - The skill name
516
+ */
517
+ getSkill(name: string): Skill | undefined;
518
+ /**
519
+ * Get all registered skill names.
520
+ */
521
+ getSkillNames(): string[];
522
+ /**
523
+ * Execute code in the sandbox.
524
+ *
525
+ * @param code - The code to execute
526
+ * @param options - Execution options
527
+ */
528
+ execute<T = unknown>(code: string, options?: {
529
+ /** Override sandbox config for this execution */
530
+ sandbox?: Partial<SandboxConfig>;
531
+ /** Specific adapters to include (default: all) */
532
+ adapters?: string[];
533
+ /** Additional context variables */
534
+ variables?: Record<string, unknown>;
535
+ }): Promise<SandboxResult<T>>;
536
+ /**
537
+ * Run a registered skill by name.
538
+ *
539
+ * @param name - The skill name
540
+ * @param options - Execution options
541
+ */
542
+ runSkill<T = unknown>(name: string, options?: {
543
+ /** Additional context variables */
544
+ variables?: Record<string, unknown>;
545
+ }): Promise<SandboxResult<T>>;
546
+ /**
547
+ * Update the default sandbox configuration.
548
+ */
549
+ configureSandbox(config: Partial<SandboxConfig>): void;
550
+ /**
551
+ * Build the execution context for the sandbox.
552
+ */
553
+ private buildExecutionContext;
554
+ }
555
+ /**
556
+ * Create a new MCXExecutor instance.
557
+ *
558
+ * @example
559
+ * ```ts
560
+ * const executor = createExecutor({
561
+ * configPath: './mcx.config.ts',
562
+ * });
563
+ * await executor.loadConfig();
564
+ * ```
565
+ */
566
+ declare function createExecutor(options?: ExecutorOptions): MCXExecutor;
567
+
568
+ export { type Adapter, type AdapterConfig, type AdapterMethod, type AdapterMethodConfig, type AdapterMethodParameter, type ExecutionContext, type ExecutorOptions, type ISandbox, type InferAdapterConfig, IsolatedVMSandbox, type MCXConfig, MCXExecutor, type SandboxConfig, type SandboxFactory, type SandboxResult, type Skill, type SkillConfig, configBuilder, createAdapterFactory, createExecutor, createSandbox, defineAdapter, defineConfig, defineSkill, defineSkills, mergeConfigs, skillBuilder };