@cleocode/contracts 2026.3.73 → 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.
package/src/transport.ts CHANGED
@@ -1,12 +1,76 @@
1
1
  /**
2
- * Transport provider interface for CLEO provider adapters.
3
- * Allows providers to supply custom inter-agent transport mechanisms.
4
- * @task T5240
2
+ * Transport Low-level wire protocol adapters for agent messaging.
3
+ *
4
+ * Transport is the HOW layer: it moves messages over the wire using
5
+ * HTTP polling, SSE, WebSocket, or in-process napi-rs calls.
6
+ *
7
+ * The Conduit interface (conduit.ts) wraps Transport to provide
8
+ * high-level messaging semantics (WHAT the agent wants to do).
9
+ *
10
+ * @see docs/specs/SIGNALDOCK-UNIFIED-AGENT-REGISTRY.md Section 4
11
+ * @module transport
5
12
  */
6
13
 
14
+ import type { TransportConfig } from './agent-registry.js';
15
+ import type { ConduitMessage } from './conduit.js';
16
+
17
+ // ============================================================================
18
+ // Transport connection config
19
+ // ============================================================================
20
+
21
+ /** Configuration passed to Transport.connect(). */
22
+ export interface TransportConnectConfig extends TransportConfig {
23
+ /** Agent ID to connect as. */
24
+ agentId: string;
25
+ /** API key for authentication. */
26
+ apiKey: string;
27
+ /** Base URL of the messaging API. */
28
+ apiBaseUrl: string;
29
+ }
30
+
31
+ // ============================================================================
32
+ // Transport interface
33
+ // ============================================================================
34
+
35
+ /** Low-level wire transport for agent messaging. */
36
+ export interface Transport {
37
+ /** Transport name for logging/debugging (e.g. 'http', 'sse', 'ws', 'local'). */
38
+ readonly name: string;
39
+
40
+ /** Connect to the messaging backend. */
41
+ connect(config: TransportConnectConfig): Promise<void>;
42
+
43
+ /** Disconnect from the messaging backend. */
44
+ disconnect(): Promise<void>;
45
+
46
+ /** Send a message payload. */
47
+ push(
48
+ to: string,
49
+ content: string,
50
+ options?: {
51
+ conversationId?: string;
52
+ replyTo?: string;
53
+ },
54
+ ): Promise<{ messageId: string }>;
55
+
56
+ /** Poll for new messages (non-destructive peek). */
57
+ poll(options?: { limit?: number; since?: string }): Promise<ConduitMessage[]>;
58
+
59
+ /** Acknowledge processed messages (marks as delivered). */
60
+ ack(messageIds: string[]): Promise<void>;
61
+
62
+ /** Subscribe to real-time events (SSE/WebSocket). Returns unsubscribe. */
63
+ subscribe?(handler: (message: ConduitMessage) => void): () => void;
64
+ }
65
+
66
+ // ============================================================================
67
+ // Legacy adapter (kept for backward compatibility during migration)
68
+ // ============================================================================
69
+
70
+ /** @deprecated Use Transport instead. Will be removed after unification. */
7
71
  export interface AdapterTransportProvider {
8
- /** Create a transport instance for inter-agent communication */
72
+ /** Create a transport instance for inter-agent communication. */
9
73
  createTransport(): unknown;
10
- /** Name of this transport type for logging/debugging */
74
+ /** Name of this transport type for logging/debugging. */
11
75
  readonly transportName: string;
12
76
  }
@@ -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
+ };