@mog-sdk/node 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.
- package/README.md +277 -0
- package/dist/index.cjs +86096 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +377 -0
- package/dist/index.d.ts +377 -0
- package/dist/index.js +86064 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @mog/sdk — Type Declarations (auto-generated, do not edit)
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
// Opaque internal types — not part of the public SDK API.
|
|
6
|
+
// Access these through the Workbook/Worksheet interfaces instead.
|
|
7
|
+
/** @internal Rust compute engine bridge — use Workbook/Worksheet APIs instead */
|
|
8
|
+
export type ComputeBridge = any;
|
|
9
|
+
/** @internal Kernel document context — use Workbook/Worksheet APIs instead */
|
|
10
|
+
export type DocumentContext = any;
|
|
11
|
+
/** @internal NAPI engine interface */
|
|
12
|
+
export interface NapiComputeEngine { [method: string]: (...args: unknown[]) => unknown; }
|
|
13
|
+
/** @internal Kernel context for advanced consumers */
|
|
14
|
+
export type IKernelContext = any;
|
|
15
|
+
/** @internal Event bus */
|
|
16
|
+
export type IEventBus = any;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Save a workbook to a file path. Node.js only.
|
|
25
|
+
* Format determined by extension (.xlsx, .csv).
|
|
26
|
+
*/
|
|
27
|
+
declare function save(wb: Workbook, path: string): Promise<void>;
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A spreadsheet workbook — the primary API for all spreadsheet operations.
|
|
32
|
+
*
|
|
33
|
+
* Create with `createWorkbook()`. Access sheets via `getActiveSheet()`,
|
|
34
|
+
* `getSheet(index)`, or `getSheetByName(name)`.
|
|
35
|
+
*
|
|
36
|
+
* Sub-APIs: history, sheets, names, scenarios, styles, notifications,
|
|
37
|
+
* theme, viewport, protection, floatingObjects.
|
|
38
|
+
*/
|
|
39
|
+
export interface Workbook {
|
|
40
|
+
/** Get the currently active worksheet */
|
|
41
|
+
getActiveSheet(): Worksheet;
|
|
42
|
+
/** Get the active sheet ID */
|
|
43
|
+
getActiveSheetId(): string;
|
|
44
|
+
/** Set the active sheet by ID */
|
|
45
|
+
setActiveSheetId(sheetId: string): void;
|
|
46
|
+
/** Get sheet by 0-based index */
|
|
47
|
+
getSheet(index: number): Worksheet;
|
|
48
|
+
/** Get sheet by name */
|
|
49
|
+
getSheetByName(name: string): Worksheet | undefined;
|
|
50
|
+
|
|
51
|
+
/** Undo/redo, batch operations, checkpoints */
|
|
52
|
+
readonly history: WorkbookHistory;
|
|
53
|
+
/** Sheet CRUD: add, remove, rename, move, duplicate */
|
|
54
|
+
readonly sheets: WorkbookSheets;
|
|
55
|
+
|
|
56
|
+
/** Execute code in a sandboxed VM */
|
|
57
|
+
executeCode(code: string, options?: { timeout?: number }): Promise<unknown>;
|
|
58
|
+
/** Export workbook to XLSX buffer */
|
|
59
|
+
toBuffer(): Promise<Uint8Array>;
|
|
60
|
+
/** Dispose all resources — MUST call when done */
|
|
61
|
+
dispose(): void;
|
|
62
|
+
|
|
63
|
+
[key: string]: any;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface WorkbookHistory {
|
|
67
|
+
undo(): Promise<void>;
|
|
68
|
+
redo(): Promise<void>;
|
|
69
|
+
batch(fn: () => Promise<void>): Promise<void>;
|
|
70
|
+
[key: string]: any;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface WorkbookSheets {
|
|
74
|
+
add(name?: string): Promise<string>;
|
|
75
|
+
remove(sheetId: string): Promise<void>;
|
|
76
|
+
rename(sheetId: string, name: string): Promise<void>;
|
|
77
|
+
getAll(): Array<{ id: string; name: string; index: number }>;
|
|
78
|
+
[key: string]: any;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A single worksheet — read/write cells, formatting, charts, and more.
|
|
83
|
+
*
|
|
84
|
+
* Supports both A1 notation (`"A1"`) and numeric addressing (`(row, col)`).
|
|
85
|
+
*
|
|
86
|
+
* Sub-APIs: formats, layout, view, structure, charts, shapes, pictures,
|
|
87
|
+
* filters, validation, tables, pivots, slicers, comments, hyperlinks,
|
|
88
|
+
* outline, protection, print, settings, bindings.
|
|
89
|
+
*/
|
|
90
|
+
export interface Worksheet {
|
|
91
|
+
/** Sheet name */
|
|
92
|
+
readonly name: string;
|
|
93
|
+
/** Sheet ID */
|
|
94
|
+
readonly id: string;
|
|
95
|
+
/** 0-based sheet index */
|
|
96
|
+
getIndex(): number;
|
|
97
|
+
|
|
98
|
+
/** Set a cell value (A1 notation) */
|
|
99
|
+
setCell(address: string, value: unknown): Promise<void>;
|
|
100
|
+
/** Set a cell value (numeric) */
|
|
101
|
+
setCell(row: number, col: number, value: unknown): Promise<void>;
|
|
102
|
+
|
|
103
|
+
/** Get a cell's computed value */
|
|
104
|
+
getValue(address: string): Promise<unknown>;
|
|
105
|
+
getValue(row: number, col: number): Promise<unknown>;
|
|
106
|
+
|
|
107
|
+
/** Get a cell's formula (or undefined if none) */
|
|
108
|
+
getFormula(address: string): Promise<string | undefined>;
|
|
109
|
+
getFormula(row: number, col: number): Promise<string | undefined>;
|
|
110
|
+
|
|
111
|
+
/** Get full cell data */
|
|
112
|
+
getCell(address: string): Promise<CellData | undefined>;
|
|
113
|
+
getCell(row: number, col: number): Promise<CellData | undefined>;
|
|
114
|
+
|
|
115
|
+
/** Formatting sub-API */
|
|
116
|
+
readonly formats: WorksheetFormats;
|
|
117
|
+
/** Layout sub-API (row heights, column widths, merges) */
|
|
118
|
+
readonly layout: WorksheetLayout;
|
|
119
|
+
/** Chart operations */
|
|
120
|
+
readonly charts: WorksheetCharts;
|
|
121
|
+
/** Table operations */
|
|
122
|
+
readonly tables: WorksheetTables;
|
|
123
|
+
|
|
124
|
+
/** Describe the sheet for LLM consumption */
|
|
125
|
+
describe(): Promise<string>;
|
|
126
|
+
/** Describe a range for LLM consumption */
|
|
127
|
+
describeRange(range: string): Promise<string>;
|
|
128
|
+
|
|
129
|
+
[key: string]: any;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface CellData {
|
|
133
|
+
value: unknown;
|
|
134
|
+
formula?: string;
|
|
135
|
+
formattedValue?: string;
|
|
136
|
+
[key: string]: any;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface WorksheetFormats {
|
|
140
|
+
set(address: string, format: Record<string, unknown>): Promise<void>;
|
|
141
|
+
get(address: string): Promise<Record<string, unknown>>;
|
|
142
|
+
[key: string]: any;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface WorksheetLayout {
|
|
146
|
+
setRowHeight(row: number, height: number): Promise<void>;
|
|
147
|
+
setColumnWidth(col: number, width: number): Promise<void>;
|
|
148
|
+
merge(range: string): Promise<void>;
|
|
149
|
+
unmerge(range: string): Promise<void>;
|
|
150
|
+
[key: string]: any;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface WorksheetCharts {
|
|
154
|
+
add(type: string, dataRange: string, options?: Record<string, unknown>): Promise<string>;
|
|
155
|
+
remove(chartId: string): Promise<void>;
|
|
156
|
+
[key: string]: any;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface WorksheetTables {
|
|
160
|
+
add(range: string, options?: Record<string, unknown>): Promise<string>;
|
|
161
|
+
remove(tableId: string): Promise<void>;
|
|
162
|
+
[key: string]: any;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface CreateWorkbookOptions {
|
|
166
|
+
/** Document ID (auto-generated if omitted) */
|
|
167
|
+
documentId?: string;
|
|
168
|
+
/** XLSX source to import */
|
|
169
|
+
source?: { type: 'bytes'; data: Uint8Array } | { type: 'path'; path: string };
|
|
170
|
+
/** Import options */
|
|
171
|
+
importOptions?: Record<string, unknown>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface WorkbookConfig {
|
|
175
|
+
ctx: any;
|
|
176
|
+
getActiveSheetId?: () => string;
|
|
177
|
+
setActiveSheetId?: (sheetId: string) => void;
|
|
178
|
+
eventBus: any;
|
|
179
|
+
[key: string]: any;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Create a new headless workbook.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* import { createWorkbook } from '@mog/sdk';
|
|
188
|
+
* const wb = await createWorkbook();
|
|
189
|
+
* const ws = wb.getActiveSheet();
|
|
190
|
+
* await ws.setCell('A1', 42);
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
export declare function createWorkbook(options?: CreateWorkbookOptions): Promise<Workbook>;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* HeadlessLifecycleSystem - Headless Boot Wrapper for the Spreadsheet Engine
|
|
197
|
+
*
|
|
198
|
+
* Thin shim that delegates to DocumentLifecycleSystem via DocumentFactory.
|
|
199
|
+
* DocumentLifecycleSystem accepts `{ environment: 'headless' }` and handles
|
|
200
|
+
* all headless-specific behavior (skipPersistenceLoad, no schema bridge,
|
|
201
|
+
* headless environment stubs, NAPI transport auto-detection).
|
|
202
|
+
*
|
|
203
|
+
* Architecture:
|
|
204
|
+
* - Uses `DocumentFactory.create({ environment: 'headless' })` for blank documents
|
|
205
|
+
* - Uses `DocumentFactory.createFromXlsx(source, { environment: 'headless' })` for XLSX import
|
|
206
|
+
* - No duplicated actor implementations — all lifecycle logic lives in DocumentLifecycleSystem
|
|
207
|
+
* - HeadlessEngine and createHeadlessEngine() are preserved for backward compatibility
|
|
208
|
+
*
|
|
209
|
+
* @see kernel/src/api/document/document-factory.ts - DocumentFactory
|
|
210
|
+
* @see kernel/src/document/document-lifecycle-system.ts - DocumentLifecycleSystem
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* NapiAddonModule -- the object returned by require('compute-core-napi.node').
|
|
215
|
+
* Contains: ComputeEngine class + static free functions (compute_set_current_time, etc.)
|
|
216
|
+
*/
|
|
217
|
+
interface NapiAddonModule {
|
|
218
|
+
ComputeEngine: new (snapshotJson: string) => NapiComputeEngine;
|
|
219
|
+
[key: string]: unknown;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Options for creating a headless spreadsheet engine.
|
|
223
|
+
*/
|
|
224
|
+
interface HeadlessOptions {
|
|
225
|
+
/** Pre-loaded napi addon module (the object returned by require('compute-core-napi.node')) */
|
|
226
|
+
computeAddon: NapiAddonModule;
|
|
227
|
+
/** Document ID (defaults to random UUID) */
|
|
228
|
+
docId?: string;
|
|
229
|
+
/** XLSX buffer to import on boot (triggers hydrating state) */
|
|
230
|
+
xlsxSource?: Buffer;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Thin shim that delegates to DocumentFactory with `{ environment: 'headless' }`.
|
|
234
|
+
*
|
|
235
|
+
* Previously contained 5 duplicated actor implementations (createEngine,
|
|
236
|
+
* wireContext, startBridge, hydrateXlsx, disposeBridge). Now all lifecycle
|
|
237
|
+
* logic lives in DocumentLifecycleSystem, which handles headless-specific
|
|
238
|
+
* behavior when environment='headless'.
|
|
239
|
+
*
|
|
240
|
+
* Lifecycle:
|
|
241
|
+
* 1. Constructor stores options
|
|
242
|
+
* 2. create() calls DocumentFactory.create({ environment: 'headless' })
|
|
243
|
+
* 3. createFromXlsx() calls DocumentFactory.createFromXlsx(source, { environment: 'headless' })
|
|
244
|
+
* 4. dispose() calls handle.dispose()
|
|
245
|
+
*/
|
|
246
|
+
declare class HeadlessLifecycleSystem {
|
|
247
|
+
/** The document handle from DocumentFactory */
|
|
248
|
+
private handle;
|
|
249
|
+
/** The headless options (kept for backward compat) */
|
|
250
|
+
private readonly options;
|
|
251
|
+
constructor(options: HeadlessOptions);
|
|
252
|
+
/**
|
|
253
|
+
* Create a new blank document.
|
|
254
|
+
* Delegates to DocumentFactory.create() with headless environment.
|
|
255
|
+
*/
|
|
256
|
+
create(docId: string): Promise<void>;
|
|
257
|
+
/**
|
|
258
|
+
* Create a document from an XLSX buffer.
|
|
259
|
+
* Delegates to DocumentFactory.createFromXlsx() with headless environment.
|
|
260
|
+
*/
|
|
261
|
+
createFromXlsx(docId: string, xlsxSource: Buffer): Promise<void>;
|
|
262
|
+
/**
|
|
263
|
+
* Returns the DocumentContext from the handle.
|
|
264
|
+
* Throws if create() or createFromXlsx() has not been called.
|
|
265
|
+
*/
|
|
266
|
+
get context(): DocumentContext;
|
|
267
|
+
/**
|
|
268
|
+
* Returns the ComputeBridge from the context.
|
|
269
|
+
* Throws if create() or createFromXlsx() has not been called.
|
|
270
|
+
*/
|
|
271
|
+
get computeBridge(): ComputeBridge;
|
|
272
|
+
/**
|
|
273
|
+
* Dispose the document and clean up all resources.
|
|
274
|
+
* Safe to call multiple times (idempotent).
|
|
275
|
+
*/
|
|
276
|
+
dispose(): Promise<void>;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* A headless spreadsheet engine instance.
|
|
280
|
+
*
|
|
281
|
+
* Wraps the lifecycle system and provides a clean public API.
|
|
282
|
+
* Created by `createHeadlessEngine()`.
|
|
283
|
+
*
|
|
284
|
+
* Provides two levels of access:
|
|
285
|
+
* - `workbook` / `general`: High-level External API (A1-notation, agent-friendly)
|
|
286
|
+
* - `computeBridge` / `context`: Low-level kernel access (row/col, identity-based)
|
|
287
|
+
*/
|
|
288
|
+
declare class HeadlessEngine {
|
|
289
|
+
private readonly lifecycle;
|
|
290
|
+
/** Cached unified Workbook instance (owns sheet cache, create once) */
|
|
291
|
+
private _workbook;
|
|
292
|
+
/** @internal */
|
|
293
|
+
constructor(lifecycle: HeadlessLifecycleSystem);
|
|
294
|
+
/**
|
|
295
|
+
* The unified Workbook — THE single API for all data and compute operations.
|
|
296
|
+
*
|
|
297
|
+
* Provides:
|
|
298
|
+
* - Sheet access: getSheet(index | name), getActiveSheet()
|
|
299
|
+
* - Sheet management: addSheet, removeSheet, moveSheet, renameSheet, etc.
|
|
300
|
+
* - Orchestration: undo, redo, batch, checkpoints, calc control, events
|
|
301
|
+
* - Named ranges, scenarios, introspection, code execution
|
|
302
|
+
*
|
|
303
|
+
* Each Worksheet provides:
|
|
304
|
+
* - Cell read/write with A1 AND numeric (row, col) overloads
|
|
305
|
+
* - Formatting, structure, merges, sort, charts, shapes
|
|
306
|
+
* - Filters, conditional formatting, validation, comments
|
|
307
|
+
* - Grouping, hyperlinks, pivots, slicers
|
|
308
|
+
* - LLM presentation: describe(), describeRange(), summarize()
|
|
309
|
+
*
|
|
310
|
+
* @see contracts/src/api/ — Interface definitions
|
|
311
|
+
*/
|
|
312
|
+
get workbook(): Workbook;
|
|
313
|
+
/**
|
|
314
|
+
* Initialize the workbook instance asynchronously.
|
|
315
|
+
* Must be called after construction and before accessing `workbook`.
|
|
316
|
+
* @internal Called by `createHeadlessEngine()`.
|
|
317
|
+
*/
|
|
318
|
+
initWorkbook(): Promise<void>;
|
|
319
|
+
/**
|
|
320
|
+
* Get or set the active sheet ID.
|
|
321
|
+
* Delegates to the workbook's internal active-sheet tracking.
|
|
322
|
+
*/
|
|
323
|
+
get activeSheetId(): string;
|
|
324
|
+
set activeSheetId(id: string);
|
|
325
|
+
/**
|
|
326
|
+
* The fully wired DocumentContext -- provides access to all kernel services,
|
|
327
|
+
* bridges, event bus, and the compute bridge.
|
|
328
|
+
*/
|
|
329
|
+
get context(): DocumentContext;
|
|
330
|
+
/**
|
|
331
|
+
* The Rust compute bridge -- direct access to the compute engine
|
|
332
|
+
* for cell mutations, formula evaluation, sheet operations, etc.
|
|
333
|
+
*/
|
|
334
|
+
get computeBridge(): ComputeBridge;
|
|
335
|
+
/**
|
|
336
|
+
* Dispose the engine and clean up all resources.
|
|
337
|
+
*
|
|
338
|
+
* Sends DISPOSE event to the lifecycle machine and waits for
|
|
339
|
+
* 'disposed' state. Safe to call multiple times (idempotent).
|
|
340
|
+
*
|
|
341
|
+
* MUST be called when done to avoid resource leaks.
|
|
342
|
+
*/
|
|
343
|
+
dispose(): Promise<void>;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Create a headless spreadsheet engine.
|
|
347
|
+
*
|
|
348
|
+
* This boots the full kernel stack (compute-core, bridges, context) in Node.js
|
|
349
|
+
* without any browser dependencies. Uses the same lifecycle state machine as
|
|
350
|
+
* the browser app -- error handling, cleanup, and XLSX import support are shared.
|
|
351
|
+
*
|
|
352
|
+
* The engine uses `@mog/compute-core-napi` (a native Node.js addon) for
|
|
353
|
+
* the Rust compute core, providing the same performance as the Tauri desktop app.
|
|
354
|
+
*
|
|
355
|
+
* @param options - Configuration including the pre-loaded napi addon module
|
|
356
|
+
* @returns A ready-to-use HeadlessEngine
|
|
357
|
+
* @throws If engine initialization fails
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```typescript
|
|
361
|
+
* // Load the napi addon
|
|
362
|
+
* const addon = require('@mog/compute-core-napi');
|
|
363
|
+
*
|
|
364
|
+
* // Create the headless engine
|
|
365
|
+
* const engine = await createHeadlessEngine({ computeAddon: addon });
|
|
366
|
+
*
|
|
367
|
+
* // Use the engine
|
|
368
|
+
* const bridge = engine.computeBridge;
|
|
369
|
+
* await bridge.setCell('sheet1', 0, 0, '=1+1');
|
|
370
|
+
*
|
|
371
|
+
* // Clean up
|
|
372
|
+
* await engine.dispose();
|
|
373
|
+
* ```
|
|
374
|
+
*/
|
|
375
|
+
declare function createHeadlessEngine(options: HeadlessOptions): Promise<HeadlessEngine>;
|
|
376
|
+
|
|
377
|
+
export { HeadlessEngine, type HeadlessOptions, type NapiAddonModule, createHeadlessEngine, save };
|