@cleocode/contracts 2026.3.74 → 2026.3.76

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,193 @@
1
+ /**
2
+ * Central WASM SDK for CLEO Core Contracts
3
+ *
4
+ * Provides unified access to all Rust crate WASM modules:
5
+ * - lafs-core: LAFS envelope types and validation
6
+ * - conduit-core: Conduit wire types and CANT metadata
7
+ * - cant-core: CANT grammar parser (via @cleocode/cant)
8
+ *
9
+ * Usage:
10
+ * ```typescript
11
+ * import { initWasm, lafs, conduit } from '@cleocode/contracts/wasm';
12
+ *
13
+ * await initWasm();
14
+ *
15
+ * // LAFS
16
+ * const meta = new lafs.WasmLafsMeta('tasks.list', 'http');
17
+ * const envelope = lafs.WasmLafsEnvelope.createSuccess('{"tasks":[]}', meta);
18
+ *
19
+ * // Conduit
20
+ * const msg = new conduit.WasmConduitMessage('msg-1', 'agent-a', 'Hello', '2026-03-25T00:00:00Z');
21
+ * const cant = new conduit.WasmCantMetadata('actionable', '["@agent"]', '["T123"]', '["#tag"]');
22
+ * ```
23
+ */
24
+
25
+ /** Resolved type of the lafs-core WASM module. */
26
+ type LafsWasmModule = typeof import('./lafs-core/lafs_core.js');
27
+ /** Resolved type of the conduit-core WASM module. */
28
+ type ConduitWasmModule = typeof import('./conduit-core/conduit_core.js');
29
+
30
+ // WASM module instances
31
+ let lafsModule: LafsWasmModule | null = null;
32
+ let conduitModule: ConduitWasmModule | null = null;
33
+ let isInitialized = false;
34
+ let isInitializing = false;
35
+ let initPromise: Promise<void> | null = null;
36
+
37
+ /**
38
+ * Initialize all WASM modules
39
+ * Must be called before using any WASM classes/functions
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * import { initWasm, lafs, conduit } from '@cleocode/contracts/wasm';
44
+ *
45
+ * await initWasm();
46
+ *
47
+ * // Now you can use WASM classes
48
+ * const meta = new lafs.WasmLafsMeta('tasks.list', 'http');
49
+ * ```
50
+ */
51
+ export async function initWasm(): Promise<void> {
52
+ if (isInitialized) return;
53
+ if (isInitializing) {
54
+ return initPromise!;
55
+ }
56
+
57
+ isInitializing = true;
58
+ initPromise = (async () => {
59
+ try {
60
+ // Dynamic imports to avoid loading if not needed
61
+ const [lafs, conduit] = await Promise.all([
62
+ import('./lafs-core/lafs_core.js'),
63
+ import('./conduit-core/conduit_core.js'),
64
+ ]);
65
+
66
+ // Initialize modules
67
+ await Promise.all([lafs.default(), conduit.default()]);
68
+
69
+ lafsModule = lafs;
70
+ conduitModule = conduit;
71
+ isInitialized = true;
72
+ } catch (_error) {
73
+ throw new Error('WASM initialization failed. Ensure WASM files are present.');
74
+ }
75
+ })();
76
+
77
+ await initPromise;
78
+ isInitializing = false;
79
+ }
80
+
81
+ /**
82
+ * Check if WASM is initialized and ready to use
83
+ *
84
+ * @returns true if WASM modules are loaded and initialized
85
+ */
86
+ export function isWasmReady(): boolean {
87
+ return isInitialized;
88
+ }
89
+
90
+ /**
91
+ * LAFS Core WASM exports
92
+ *
93
+ * Available after calling initWasm():
94
+ * - WasmLafsTransport - Transport type (Cli, Http, Grpc, Sdk)
95
+ * - WasmLafsMeta - Metadata for LAFS envelopes
96
+ * - WasmLafsEnvelope - The main LAFS response envelope
97
+ * - createTransport() - Helper to create transport from string
98
+ */
99
+ export const lafs = {
100
+ /**
101
+ * LAFS Transport enum
102
+ * Use WasmLafsTransport.cli(), .http(), .grpc(), or .sdk()
103
+ */
104
+ get WasmLafsTransport() {
105
+ if (!lafsModule) throw new Error('WASM not initialized. Call initWasm() first.');
106
+ return lafsModule.WasmLafsTransport;
107
+ },
108
+
109
+ /**
110
+ * LAFS Metadata constructor
111
+ * new WasmLafsMeta(operation: string, transport: string)
112
+ */
113
+ get WasmLafsMeta() {
114
+ if (!lafsModule) throw new Error('WASM not initialized. Call initWasm() first.');
115
+ return lafsModule.WasmLafsMeta;
116
+ },
117
+
118
+ /**
119
+ * LAFS Envelope class
120
+ * Use WasmLafsEnvelope.createSuccess() or .createError()
121
+ */
122
+ get WasmLafsEnvelope() {
123
+ if (!lafsModule) throw new Error('WASM not initialized. Call initWasm() first.');
124
+ return lafsModule.WasmLafsEnvelope;
125
+ },
126
+
127
+ /**
128
+ * Helper function to create transport from string
129
+ * @param transport - "cli", "http", "grpc", or "sdk"
130
+ */
131
+ get createTransport() {
132
+ if (!lafsModule) throw new Error('WASM not initialized. Call initWasm() first.');
133
+ return lafsModule.create_transport;
134
+ },
135
+ };
136
+
137
+ /**
138
+ * Conduit Core WASM exports
139
+ *
140
+ * Available after calling initWasm():
141
+ * - WasmConduitMessage - Agent-to-agent messages
142
+ * - WasmConduitState - Connection states (Disconnected, Connecting, Connected, etc.)
143
+ * - WasmCantMetadata - CANT parsing results
144
+ * - parseConduitMessage() - Parse message from JSON
145
+ * - createConduitState() - Create state from string
146
+ */
147
+ export const conduit = {
148
+ /**
149
+ * Conduit Message constructor
150
+ * new WasmConduitMessage(id, from, content, timestamp)
151
+ */
152
+ get WasmConduitMessage() {
153
+ if (!conduitModule) throw new Error('WASM not initialized. Call initWasm() first.');
154
+ return conduitModule.WasmConduitMessage;
155
+ },
156
+
157
+ /**
158
+ * Conduit State enum
159
+ * Use WasmConduitState.disconnected(), .connecting(), .connected(), etc.
160
+ */
161
+ get WasmConduitState() {
162
+ if (!conduitModule) throw new Error('WASM not initialized. Call initWasm() first.');
163
+ return conduitModule.WasmConduitState;
164
+ },
165
+
166
+ /**
167
+ * CANT Metadata constructor
168
+ * new WasmCantMetadata(directiveType, addressesJson, taskRefsJson, tagsJson)
169
+ */
170
+ get WasmCantMetadata() {
171
+ if (!conduitModule) throw new Error('WASM not initialized. Call initWasm() first.');
172
+ return conduitModule.WasmCantMetadata;
173
+ },
174
+
175
+ /**
176
+ * Parse a ConduitMessage from JSON string
177
+ * @param json - JSON string
178
+ * @returns WasmConduitMessage or undefined
179
+ */
180
+ get parseConduitMessage() {
181
+ if (!conduitModule) throw new Error('WASM not initialized. Call initWasm() first.');
182
+ return conduitModule.parse_conduit_message;
183
+ },
184
+
185
+ /**
186
+ * Create a ConduitState from string
187
+ * @param state - "disconnected", "connecting", "connected", "reconnecting", "error"
188
+ */
189
+ get createConduitState() {
190
+ if (!conduitModule) throw new Error('WASM not initialized. Call initWasm() first.');
191
+ return conduitModule.create_conduit_state;
192
+ },
193
+ };