@mog-sdk/node 0.1.2 → 0.1.6

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/dist/index.d.cts CHANGED
@@ -17,6 +17,8 @@ export type IEventBus = any;
17
17
 
18
18
 
19
19
 
20
+ import * as _mog_kernel_bridges from '@mog/kernel/bridges';
21
+
20
22
 
21
23
 
22
24
 
@@ -26,6 +28,48 @@ export type IEventBus = any;
26
28
  */
27
29
  declare function save(wb: Workbook, path: string): Promise<void>;
28
30
 
31
+ interface OverviewResult {
32
+ workbook: {
33
+ methods: string[];
34
+ subApis: string[];
35
+ };
36
+ worksheet: {
37
+ methods: string[];
38
+ subApis: string[];
39
+ };
40
+ }
41
+ interface MethodSummary {
42
+ name: string;
43
+ signature: string;
44
+ docstring: string;
45
+ tags: string[];
46
+ }
47
+ interface InterfaceResult {
48
+ name: string;
49
+ path: string;
50
+ docstring: string;
51
+ methods: MethodSummary[];
52
+ }
53
+ interface MethodResult {
54
+ name: string;
55
+ path: string;
56
+ signature: string;
57
+ docstring: string;
58
+ tags: string[];
59
+ types: Record<string, TypeResult>;
60
+ }
61
+ interface TypeResult {
62
+ name: string;
63
+ definition?: string;
64
+ isEnum?: boolean;
65
+ values?: Record<string, string>;
66
+ docstring?: string;
67
+ }
68
+ type DescribeResult = OverviewResult | InterfaceResult | MethodResult | TypeResult | null;
69
+ declare const api: {
70
+ describe: (path?: string) => DescribeResult;
71
+ };
72
+
29
73
 
30
74
  /**
31
75
  * A spreadsheet workbook — the primary API for all spreadsheet operations.
@@ -221,6 +265,19 @@ interface NapiAddonModule {
221
265
  /**
222
266
  * Options for creating a headless spreadsheet engine.
223
267
  */
268
+ /**
269
+ * A code-executor factory compatible with WorkbookImpl.setCodeExecutorFactory().
270
+ * Engine layers supply this to wire a VM executor without circular dependencies.
271
+ *
272
+ * The factory receives the HeadlessEngine so it can access the workbook/context.
273
+ * It returns an object whose `execute` method runs agent code and returns the
274
+ * contract CodeExecutionResult.
275
+ */
276
+ type HeadlessCodeExecutorFactory = (engine: HeadlessEngine) => {
277
+ execute(code: string, options?: CodeExecutionOptions): Promise<CodeExecutionResult>;
278
+ cancelExecution?(executionId: string): void;
279
+ dispose(): void;
280
+ };
224
281
  interface HeadlessOptions {
225
282
  /** Pre-loaded napi addon module (the object returned by require('compute-core-napi.node')) */
226
283
  computeAddon: NapiAddonModule;
@@ -228,6 +285,16 @@ interface HeadlessOptions {
228
285
  docId?: string;
229
286
  /** XLSX buffer to import on boot (triggers hydrating state) */
230
287
  xlsxSource?: Buffer;
288
+ /**
289
+ * Optional code-executor factory. When provided, `wb.executeCode()` will
290
+ * work by delegating to the executor returned by this factory.
291
+ * The factory is called lazily on the first `executeCode()` invocation.
292
+ */
293
+ codeExecutorFactory?: HeadlessCodeExecutorFactory;
294
+ /** Pre-built WorkbookSnapshot to initialize the engine from (for collaboration).
295
+ * When provided, the engine is created with this snapshot instead of an empty one.
296
+ * This ensures the engine shares the same CellIds as the source engine. */
297
+ initialSnapshot?: Record<string, unknown>;
231
298
  }
232
299
  /**
233
300
  * Thin shim that delegates to DocumentFactory with `{ environment: 'headless' }`.
@@ -252,6 +319,9 @@ declare class HeadlessLifecycleSystem {
252
319
  /**
253
320
  * Create a new blank document.
254
321
  * Delegates to DocumentFactory.create() with headless environment.
322
+ *
323
+ * If `initialSnapshot` is set on options, creates the engine from that snapshot
324
+ * instead of an empty one (for collaboration — ensures same CellIds as origin).
255
325
  */
256
326
  create(docId: string): Promise<void>;
257
327
  /**
@@ -310,6 +380,10 @@ declare class HeadlessEngine {
310
380
  * @see contracts/src/api/ — Interface definitions
311
381
  */
312
382
  get workbook(): Workbook;
383
+ /**
384
+ * @internal Access the WorkbookInternal for infrastructure wiring (e.g. code executor).
385
+ */
386
+ get _workbookInternal(): WorkbookInternal;
313
387
  /**
314
388
  * Initialize the workbook instance asynchronously.
315
389
  * Must be called after construction and before accessing `workbook`.
@@ -374,4 +448,119 @@ declare class HeadlessEngine {
374
448
  */
375
449
  declare function createHeadlessEngine(options: HeadlessOptions): Promise<HeadlessEngine>;
376
450
 
377
- export { HeadlessEngine, type HeadlessOptions, type NapiAddonModule, createHeadlessEngine, save };
451
+ type SyncMode = 'immediate' | 'batch' | 'manual';
452
+ interface LockScope {
453
+ type: 'sheet' | 'workbook';
454
+ sheetId?: string;
455
+ }
456
+ interface Lock {
457
+ id: string;
458
+ owner: string;
459
+ scope: LockScope;
460
+ }
461
+ interface CollaborativeEngineOptions {
462
+ /** Pre-loaded napi addon module. */
463
+ computeAddon: NapiAddonModule;
464
+ /** Coordinator handle (from coordinator_create). If not provided, one is created. */
465
+ coordinatorHandle?: number;
466
+ /** How sync is triggered. Default: 'immediate'. */
467
+ syncMode?: SyncMode;
468
+ /** For batch mode: flush after N mutations. Default: 10. */
469
+ batchSize?: number;
470
+ /** XLSX buffer to import on boot. */
471
+ xlsxSource?: Buffer;
472
+ /** Participant ID. Defaults to random UUID. */
473
+ participantId?: string;
474
+ /** Document ID. Defaults to random UUID. */
475
+ docId?: string;
476
+ }
477
+ interface FlushResult {
478
+ serverDiff: number[];
479
+ ok: boolean;
480
+ }
481
+ interface SyncResult {
482
+ pushed: boolean;
483
+ pulled: boolean;
484
+ }
485
+ declare class CollaborativeEngine {
486
+ private readonly _inner;
487
+ private readonly _coordinator;
488
+ private readonly _participantId;
489
+ private readonly _syncMode;
490
+ private readonly _batchSize;
491
+ private _pendingMutations;
492
+ private _touchedSheets;
493
+ private _disposed;
494
+ private _ownsCoordinator;
495
+ /** @internal — use CollaborativeEngine.create() */
496
+ private constructor();
497
+ static create(options: CollaborativeEngineOptions): Promise<CollaborativeEngine>;
498
+ /**
499
+ * Create a CollaborativeEngine from the coordinator's current Yrs state.
500
+ *
501
+ * Used for the 2nd, 3rd, ... participants. The coordinator already has state
502
+ * pushed by the first participant. This method:
503
+ * 1. Gets the coordinator's Yrs state bytes
504
+ * 2. Converts to a WorkbookSnapshot via yrs_state_to_snapshot_json (NAPI)
505
+ * 3. Creates a HeadlessEngine initialized from that snapshot (same CellIds)
506
+ * 4. Pushes/pulls to align Yrs docs with the coordinator
507
+ */
508
+ static createFromCoordinator(options: {
509
+ computeAddon: NapiAddonModule;
510
+ coordinatorHandle: number;
511
+ syncMode?: SyncMode;
512
+ participantId?: string;
513
+ }): Promise<CollaborativeEngine>;
514
+ /** The standard Workbook API. Agents use this. */
515
+ get workbook(): Workbook;
516
+ /** Convenience: active worksheet. */
517
+ get ws(): Worksheet;
518
+ /** This engine's participant ID. */
519
+ get participantId(): string;
520
+ /** The coordinator handle (for sharing between engines). */
521
+ get coordinatorHandle(): number;
522
+ /** Current sync mode. */
523
+ get syncMode(): SyncMode;
524
+ /** Access to the underlying compute bridge (for sync operations). */
525
+ get computeBridge(): _mog_kernel_bridges.ComputeBridge;
526
+ /** Track that a sheet was touched (for lock validation). */
527
+ trackSheetTouch(sheetId: string): void;
528
+ /**
529
+ * Push local changes to the coordinator.
530
+ * In manual mode, call this explicitly. In immediate mode, auto-called.
531
+ */
532
+ flush(): Promise<FlushResult>;
533
+ /**
534
+ * Pull remote changes from the coordinator.
535
+ */
536
+ pull(): Promise<void>;
537
+ /**
538
+ * Full sync cycle: push local changes, then pull remote changes.
539
+ */
540
+ sync(): Promise<SyncResult>;
541
+ /** Acquire a lock. Returns lock ID. */
542
+ lock(scope: LockScope, ttlSeconds?: number): Promise<string>;
543
+ /** Release a lock. */
544
+ unlock(lockId: string): Promise<void>;
545
+ /** List all active locks. */
546
+ locks(): Promise<Lock[]>;
547
+ /** Dispose the engine — leave coordinator, release locks, dispose engine. */
548
+ dispose(): Promise<void>;
549
+ }
550
+ /**
551
+ * Create a coordinator and N collaborative engines sharing it.
552
+ * All engines use manual sync mode (for test scenarios).
553
+ */
554
+ declare function createCollaborativeGroup(addon: NapiAddonModule, count: number, options?: {
555
+ syncMode?: SyncMode;
556
+ xlsxSource?: Buffer;
557
+ }): Promise<{
558
+ engines: CollaborativeEngine[];
559
+ coordinatorHandle: number;
560
+ /** Sync all engines: each flushes then pulls. */
561
+ sync: () => Promise<void>;
562
+ /** Dispose all engines and the coordinator. */
563
+ dispose: () => Promise<void>;
564
+ }>;
565
+
566
+ export { type Lock as CollabLock, type LockScope as CollabLockScope, CollaborativeEngine, type CollaborativeEngineOptions, type DescribeResult, type HeadlessCodeExecutorFactory, HeadlessEngine, type HeadlessOptions, type InterfaceResult, type MethodResult, type MethodSummary, type NapiAddonModule, type OverviewResult, type SyncMode, type TypeResult, api, createCollaborativeGroup, createHeadlessEngine, save };
package/dist/index.d.ts CHANGED
@@ -17,6 +17,8 @@ export type IEventBus = any;
17
17
 
18
18
 
19
19
 
20
+ import * as _mog_kernel_bridges from '@mog/kernel/bridges';
21
+
20
22
 
21
23
 
22
24
 
@@ -26,6 +28,48 @@ export type IEventBus = any;
26
28
  */
27
29
  declare function save(wb: Workbook, path: string): Promise<void>;
28
30
 
31
+ interface OverviewResult {
32
+ workbook: {
33
+ methods: string[];
34
+ subApis: string[];
35
+ };
36
+ worksheet: {
37
+ methods: string[];
38
+ subApis: string[];
39
+ };
40
+ }
41
+ interface MethodSummary {
42
+ name: string;
43
+ signature: string;
44
+ docstring: string;
45
+ tags: string[];
46
+ }
47
+ interface InterfaceResult {
48
+ name: string;
49
+ path: string;
50
+ docstring: string;
51
+ methods: MethodSummary[];
52
+ }
53
+ interface MethodResult {
54
+ name: string;
55
+ path: string;
56
+ signature: string;
57
+ docstring: string;
58
+ tags: string[];
59
+ types: Record<string, TypeResult>;
60
+ }
61
+ interface TypeResult {
62
+ name: string;
63
+ definition?: string;
64
+ isEnum?: boolean;
65
+ values?: Record<string, string>;
66
+ docstring?: string;
67
+ }
68
+ type DescribeResult = OverviewResult | InterfaceResult | MethodResult | TypeResult | null;
69
+ declare const api: {
70
+ describe: (path?: string) => DescribeResult;
71
+ };
72
+
29
73
 
30
74
  /**
31
75
  * A spreadsheet workbook — the primary API for all spreadsheet operations.
@@ -221,6 +265,19 @@ interface NapiAddonModule {
221
265
  /**
222
266
  * Options for creating a headless spreadsheet engine.
223
267
  */
268
+ /**
269
+ * A code-executor factory compatible with WorkbookImpl.setCodeExecutorFactory().
270
+ * Engine layers supply this to wire a VM executor without circular dependencies.
271
+ *
272
+ * The factory receives the HeadlessEngine so it can access the workbook/context.
273
+ * It returns an object whose `execute` method runs agent code and returns the
274
+ * contract CodeExecutionResult.
275
+ */
276
+ type HeadlessCodeExecutorFactory = (engine: HeadlessEngine) => {
277
+ execute(code: string, options?: CodeExecutionOptions): Promise<CodeExecutionResult>;
278
+ cancelExecution?(executionId: string): void;
279
+ dispose(): void;
280
+ };
224
281
  interface HeadlessOptions {
225
282
  /** Pre-loaded napi addon module (the object returned by require('compute-core-napi.node')) */
226
283
  computeAddon: NapiAddonModule;
@@ -228,6 +285,16 @@ interface HeadlessOptions {
228
285
  docId?: string;
229
286
  /** XLSX buffer to import on boot (triggers hydrating state) */
230
287
  xlsxSource?: Buffer;
288
+ /**
289
+ * Optional code-executor factory. When provided, `wb.executeCode()` will
290
+ * work by delegating to the executor returned by this factory.
291
+ * The factory is called lazily on the first `executeCode()` invocation.
292
+ */
293
+ codeExecutorFactory?: HeadlessCodeExecutorFactory;
294
+ /** Pre-built WorkbookSnapshot to initialize the engine from (for collaboration).
295
+ * When provided, the engine is created with this snapshot instead of an empty one.
296
+ * This ensures the engine shares the same CellIds as the source engine. */
297
+ initialSnapshot?: Record<string, unknown>;
231
298
  }
232
299
  /**
233
300
  * Thin shim that delegates to DocumentFactory with `{ environment: 'headless' }`.
@@ -252,6 +319,9 @@ declare class HeadlessLifecycleSystem {
252
319
  /**
253
320
  * Create a new blank document.
254
321
  * Delegates to DocumentFactory.create() with headless environment.
322
+ *
323
+ * If `initialSnapshot` is set on options, creates the engine from that snapshot
324
+ * instead of an empty one (for collaboration — ensures same CellIds as origin).
255
325
  */
256
326
  create(docId: string): Promise<void>;
257
327
  /**
@@ -310,6 +380,10 @@ declare class HeadlessEngine {
310
380
  * @see contracts/src/api/ — Interface definitions
311
381
  */
312
382
  get workbook(): Workbook;
383
+ /**
384
+ * @internal Access the WorkbookInternal for infrastructure wiring (e.g. code executor).
385
+ */
386
+ get _workbookInternal(): WorkbookInternal;
313
387
  /**
314
388
  * Initialize the workbook instance asynchronously.
315
389
  * Must be called after construction and before accessing `workbook`.
@@ -374,4 +448,119 @@ declare class HeadlessEngine {
374
448
  */
375
449
  declare function createHeadlessEngine(options: HeadlessOptions): Promise<HeadlessEngine>;
376
450
 
377
- export { HeadlessEngine, type HeadlessOptions, type NapiAddonModule, createHeadlessEngine, save };
451
+ type SyncMode = 'immediate' | 'batch' | 'manual';
452
+ interface LockScope {
453
+ type: 'sheet' | 'workbook';
454
+ sheetId?: string;
455
+ }
456
+ interface Lock {
457
+ id: string;
458
+ owner: string;
459
+ scope: LockScope;
460
+ }
461
+ interface CollaborativeEngineOptions {
462
+ /** Pre-loaded napi addon module. */
463
+ computeAddon: NapiAddonModule;
464
+ /** Coordinator handle (from coordinator_create). If not provided, one is created. */
465
+ coordinatorHandle?: number;
466
+ /** How sync is triggered. Default: 'immediate'. */
467
+ syncMode?: SyncMode;
468
+ /** For batch mode: flush after N mutations. Default: 10. */
469
+ batchSize?: number;
470
+ /** XLSX buffer to import on boot. */
471
+ xlsxSource?: Buffer;
472
+ /** Participant ID. Defaults to random UUID. */
473
+ participantId?: string;
474
+ /** Document ID. Defaults to random UUID. */
475
+ docId?: string;
476
+ }
477
+ interface FlushResult {
478
+ serverDiff: number[];
479
+ ok: boolean;
480
+ }
481
+ interface SyncResult {
482
+ pushed: boolean;
483
+ pulled: boolean;
484
+ }
485
+ declare class CollaborativeEngine {
486
+ private readonly _inner;
487
+ private readonly _coordinator;
488
+ private readonly _participantId;
489
+ private readonly _syncMode;
490
+ private readonly _batchSize;
491
+ private _pendingMutations;
492
+ private _touchedSheets;
493
+ private _disposed;
494
+ private _ownsCoordinator;
495
+ /** @internal — use CollaborativeEngine.create() */
496
+ private constructor();
497
+ static create(options: CollaborativeEngineOptions): Promise<CollaborativeEngine>;
498
+ /**
499
+ * Create a CollaborativeEngine from the coordinator's current Yrs state.
500
+ *
501
+ * Used for the 2nd, 3rd, ... participants. The coordinator already has state
502
+ * pushed by the first participant. This method:
503
+ * 1. Gets the coordinator's Yrs state bytes
504
+ * 2. Converts to a WorkbookSnapshot via yrs_state_to_snapshot_json (NAPI)
505
+ * 3. Creates a HeadlessEngine initialized from that snapshot (same CellIds)
506
+ * 4. Pushes/pulls to align Yrs docs with the coordinator
507
+ */
508
+ static createFromCoordinator(options: {
509
+ computeAddon: NapiAddonModule;
510
+ coordinatorHandle: number;
511
+ syncMode?: SyncMode;
512
+ participantId?: string;
513
+ }): Promise<CollaborativeEngine>;
514
+ /** The standard Workbook API. Agents use this. */
515
+ get workbook(): Workbook;
516
+ /** Convenience: active worksheet. */
517
+ get ws(): Worksheet;
518
+ /** This engine's participant ID. */
519
+ get participantId(): string;
520
+ /** The coordinator handle (for sharing between engines). */
521
+ get coordinatorHandle(): number;
522
+ /** Current sync mode. */
523
+ get syncMode(): SyncMode;
524
+ /** Access to the underlying compute bridge (for sync operations). */
525
+ get computeBridge(): _mog_kernel_bridges.ComputeBridge;
526
+ /** Track that a sheet was touched (for lock validation). */
527
+ trackSheetTouch(sheetId: string): void;
528
+ /**
529
+ * Push local changes to the coordinator.
530
+ * In manual mode, call this explicitly. In immediate mode, auto-called.
531
+ */
532
+ flush(): Promise<FlushResult>;
533
+ /**
534
+ * Pull remote changes from the coordinator.
535
+ */
536
+ pull(): Promise<void>;
537
+ /**
538
+ * Full sync cycle: push local changes, then pull remote changes.
539
+ */
540
+ sync(): Promise<SyncResult>;
541
+ /** Acquire a lock. Returns lock ID. */
542
+ lock(scope: LockScope, ttlSeconds?: number): Promise<string>;
543
+ /** Release a lock. */
544
+ unlock(lockId: string): Promise<void>;
545
+ /** List all active locks. */
546
+ locks(): Promise<Lock[]>;
547
+ /** Dispose the engine — leave coordinator, release locks, dispose engine. */
548
+ dispose(): Promise<void>;
549
+ }
550
+ /**
551
+ * Create a coordinator and N collaborative engines sharing it.
552
+ * All engines use manual sync mode (for test scenarios).
553
+ */
554
+ declare function createCollaborativeGroup(addon: NapiAddonModule, count: number, options?: {
555
+ syncMode?: SyncMode;
556
+ xlsxSource?: Buffer;
557
+ }): Promise<{
558
+ engines: CollaborativeEngine[];
559
+ coordinatorHandle: number;
560
+ /** Sync all engines: each flushes then pulls. */
561
+ sync: () => Promise<void>;
562
+ /** Dispose all engines and the coordinator. */
563
+ dispose: () => Promise<void>;
564
+ }>;
565
+
566
+ export { type Lock as CollabLock, type LockScope as CollabLockScope, CollaborativeEngine, type CollaborativeEngineOptions, type DescribeResult, type HeadlessCodeExecutorFactory, HeadlessEngine, type HeadlessOptions, type InterfaceResult, type MethodResult, type MethodSummary, type NapiAddonModule, type OverviewResult, type SyncMode, type TypeResult, api, createCollaborativeGroup, createHeadlessEngine, save };