neomind-extension-sdk 0.6.3

Unified SDK for developing NeoMind Edge AI Platform extensions with ABI Version 3
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! NeoMind Extension SDK
//!
//! A unified SDK for developing NeoMind extensions that can be compiled
//! for both Native and WASM targets.
//!
//! # Features
//!
//! - Unified trait system for Native and WASM
//! - Automatic FFI export generation
//! - Helper macros for common patterns
//! - Type-safe metric and command definitions
//! - Single-source IPC boundary types
//!
//! # Architecture (V2 - Process Isolation)
//!
//! All extensions run in isolated processes by default:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                   NeoMind Main Process                       │
//! │  - UnifiedExtensionService manages all extensions           │
//! │  - IPC communication via stdin/stdout                       │
//! └─────────────────────────────────────────────────────────────┘
//!//!//! ┌─────────────────────────────────────────────────────────────┐
//! │                  Extension Runner Process                    │
//! │  - Your extension runs here in isolation                    │
//! │  - Native: loaded via FFI                                   │
//! │  - WASM: executed via wasmtime                              │
//! │  - Crashes don't affect main process                        │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # ABI Stability
//!
//! The IPC boundary types in `ipc_types` are the stable protocol between
//! extensions and the main process. Extensions compiled against older SDK
//! versions will continue to work because:
//!
//! 1. Types are serialized as JSON over IPC
//! 2. Only the JSON format matters, not the implementation
//! 3. New fields use `#[serde(default)]` for forward compatibility
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use neomind_extension_sdk::prelude::*;
//!
//! // Define your extension struct
//! pub struct MyExtension {
//!     counter: std::sync::atomic::AtomicI64,
//! }
//!
//! impl MyExtension {
//!     pub fn new() -> Self {
//!         Self {
//!             counter: std::sync::atomic::AtomicI64::new(0),
//!         }
//!     }
//! }
//!
//! // Implement the Extension trait
//! #[async_trait]
//! impl Extension for MyExtension {
//!     fn metadata(&self) -> &ExtensionMetadata {
//!         static META: ExtensionMetadata = ExtensionMetadata::new(
//!             "my-extension",
//!             "My Extension",
//!             "1.0.0",
//!         );
//!         &META
//!     }
//!
//!     fn metrics(&self) -> Vec<MetricDescriptor> {
//!         vec![
//!             MetricDescriptor::new("counter", "Counter", MetricDataType::Integer)
//!                 .with_unit("count")
//!         ]
//!     }
//!
//!     fn commands(&self) -> Vec<ExtensionCommand> {
//!         vec![
//!             CommandBuilder::new("increment")
//!                 .display_name("Increment")
//!                 .param(
//!                     ParamBuilder::new("amount", MetricDataType::Integer)
//!                         .display_name("Amount")
//!                         .default(MetricValue::Integer(1))
//!                         .build()
//!                 )
//!                 .build()
//!         ]
//!     }
//!
//!     async fn execute_command(&self, command: &str, args: &serde_json::Value) -> Result<serde_json::Value> {
//!         match command {
//!             "increment" => {
//!                 let amount = args.get("amount").and_then(|v| v.as_i64()).unwrap_or(1);
//!                 let new_value = self.counter.fetch_add(amount, std::sync::atomic::Ordering::SeqCst) + amount;
//!                 Ok(serde_json::json!({ "counter": new_value }))
//!             }
//!             _ => Err(ExtensionError::CommandNotFound(command.to_string())),
//!         }
//!     }
//!
//!     fn produce_metrics(&self) -> Result<Vec<ExtensionMetricValue>> {
//!         Ok(vec![
//!             ExtensionMetricValue::new(
//!                 "counter",
//!                 MetricValue::Integer(self.counter.load(std::sync::atomic::Ordering::SeqCst))
//!             )
//!         ])
//!     }
//! }
//!
//! // Export FFI functions
//! neomind_export!(MyExtension);
//! ```

// ============================================================================
// IPC Boundary Types (Stable - for IPC serialization)
// ============================================================================

mod ipc_types;

/// Stable IPC boundary types for extension communication.
pub mod ipc {
    pub use crate::ipc_types::*;
}

// ============================================================================
// Host API (Extension trait + capabilities + streaming)
// ============================================================================

mod host;

// ============================================================================
// Re-exports from ipc_types (Core Types)
// ============================================================================

pub use ipc_types::{
    BatchCommand,
    BatchResult,
    BatchResultsVec,
    CExtensionMetadata,
    CommandDefinition,
    CommandDescriptor,
    ErrorKind,
    ExtensionCommand,
    ExtensionDescriptor,
    ExtensionError,
    ExtensionMetadata,
    ExtensionMetricValue,
    ExtensionRuntimeState,
    ExtensionStats,
    IpcFrame,
    // IPC Protocol Types (for process isolation)
    IpcMessage,
    IpcResponse,
    MetricDataType,
    MetricDescriptor,
    MetricValue,
    ParamMetricValue,
    ParameterDefinition,
    ParameterGroup,
    PushOutputData,
    PushOutputMessage,
    Result,
    StreamClientInfo,
    StreamDataChunk,
    ValidationRule,
    ABI_VERSION,
};

// Alias for backward compatibility
pub type MetricDefinition = MetricDescriptor;

// ============================================================================
// Re-exports from host (Extension trait + capabilities)
// ============================================================================

pub use host::Extension;

pub use host::{
    send_push_output,

    set_native_capability_bridge,
    set_push_output_writer,
    AvailableCapabilities,
    CapabilityError,
    CapabilityManifest,
    ClientInfo,

    DataChunk,
    // Event system
    EventFilter,
    EventSubscription,
    // Capability system
    ExtensionCapability,
    ExtensionCapabilityProvider,
    ExtensionContext,
    ExtensionContextConfig,
    FlowControl,
    // Push mode
    PushOutputWriterFn,
    SessionStats,
    StreamCapability,
    StreamDataType,
    // Streaming types
    StreamDirection,
    StreamError,
    StreamMode,
    StreamResult,
    StreamSession,
};

// CapabilityContext requires tokio (not available on wasm32)
#[cfg(not(target_arch = "wasm32"))]
pub use host::CapabilityContext;

/// Capability name constants - re-exported from host module
pub mod capability_constants {
    pub use crate::host::capabilities::*;
}

// Native-only FFI types
#[cfg(not(target_arch = "wasm32"))]
pub use host::{NativeCapabilityFreeFn, NativeCapabilityInvokeFn};

// ============================================================================
// WASM-specific Types and Extension Trait
// ============================================================================

#[cfg(target_arch = "wasm32")]
mod wasm_types {
    pub use crate::extension::{
        SdkCommandDefinition as ExtensionCommand, SdkExtensionError as ExtensionError,
        SdkExtensionMetadata as ExtensionMetadata, SdkExtensionMetricValue as ExtensionMetricValue,
        SdkMetricDataType as MetricDataType, SdkMetricDefinition as MetricDescriptor,
        SdkMetricValue as ParamMetricValue, SdkParameterDefinition as ParameterDefinition,
        SdkParameterGroup as ParameterGroup,
    };

    pub type Result<T> = std::result::Result<T, crate::extension::SdkExtensionError>;
    pub const ABI_VERSION: u32 = 3;

    /// Simplified StreamCapability for WASM
    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
    pub struct StreamCapability {
        pub direction: StreamDirection,
        pub mode: StreamMode,
        pub max_chunk_size: usize,
        pub preferred_chunk_size: usize,
        pub max_concurrent_sessions: usize,
    }

    impl Default for StreamCapability {
        fn default() -> Self {
            Self {
                direction: StreamDirection::None,
                mode: StreamMode::Push,
                max_chunk_size: 0,
                preferred_chunk_size: 0,
                max_concurrent_sessions: 0,
            }
        }
    }

    /// Stream direction (WASM version)
    #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    #[serde(rename_all = "lowercase")]
    pub enum StreamDirection {
        None,
        Input,
        Output,
        Duplex,
    }

    /// Stream mode (WASM version)
    #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    #[serde(rename_all = "lowercase")]
    pub enum StreamMode {
        Stateless,
        Stateful,
        Push,
        Pull,
    }
}

#[cfg(target_arch = "wasm32")]
pub use wasm_types::*;

// Re-export pollster for WASM target (used by macros)
#[cfg(target_arch = "wasm32")]
pub use pollster;

// ============================================================================
// Utility Re-exports
// ============================================================================

pub use async_trait::async_trait;
pub use serde_json::{json, Value};

// ============================================================================
// Extension Types Module
// ============================================================================

mod extension;
pub use extension::*;

pub use extension::{
    ComponentSize, FrontendComponent, FrontendComponentType, FrontendManifest,
    FrontendManifestBuilder, I18nConfig,
};

// ============================================================================
// Additional Modules
// ============================================================================

mod macros;
pub mod prelude;

#[cfg(not(target_arch = "wasm32"))]
pub mod native;

#[cfg(target_arch = "wasm32")]
pub mod wasm;

pub mod capabilities;
pub mod utils;

// ============================================================================
// SDK Constants
// ============================================================================

/// SDK version
pub const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");

/// ABI version for the unified SDK
pub const SDK_ABI_VERSION: u32 = 3;

/// Minimum NeoMind core version required
pub const MIN_NEOMIND_VERSION: &str = "0.5.0";

// ============================================================================
// Builder Types
// ============================================================================

/// Helper type for building metric descriptors
#[derive(Debug, Clone)]
pub struct MetricBuilder {
    metric: MetricDescriptor,
}

impl MetricBuilder {
    pub fn new(name: impl Into<String>, display_name: impl Into<String>) -> Self {
        Self {
            metric: MetricDescriptor {
                name: name.into(),
                display_name: display_name.into(),
                data_type: MetricDataType::String,
                unit: String::new(),
                min: None,
                max: None,
                required: false,
            },
        }
    }

    pub fn data_type(mut self, data_type: MetricDataType) -> Self {
        self.metric.data_type = data_type;
        self
    }

    pub fn float(self) -> Self {
        self.data_type(MetricDataType::Float)
    }

    pub fn integer(self) -> Self {
        self.data_type(MetricDataType::Integer)
    }

    pub fn boolean(self) -> Self {
        self.data_type(MetricDataType::Boolean)
    }

    pub fn string(self) -> Self {
        self.data_type(MetricDataType::String)
    }

    pub fn enum_type(self, options: Vec<String>) -> Self {
        self.data_type(MetricDataType::Enum { options })
    }

    pub fn unit(mut self, unit: impl Into<String>) -> Self {
        self.metric.unit = unit.into();
        self
    }

    pub fn min(mut self, min: f64) -> Self {
        self.metric.min = Some(min);
        self
    }

    pub fn max(mut self, max: f64) -> Self {
        self.metric.max = Some(max);
        self
    }

    pub fn required(mut self) -> Self {
        self.metric.required = true;
        self
    }

    pub fn build(self) -> MetricDescriptor {
        self.metric
    }
}

/// Helper type for building command definitions
#[derive(Debug, Clone)]
pub struct CommandBuilder {
    command: ExtensionCommand,
}

impl CommandBuilder {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            command: ExtensionCommand {
                name: name.into(),
                display_name: String::new(),
                description: String::new(),
                payload_template: String::new(),
                parameters: Vec::new(),
                fixed_values: std::collections::HashMap::new(),
                samples: Vec::new(),
                parameter_groups: Vec::new(),
            },
        }
    }

    pub fn display_name(mut self, display_name: impl Into<String>) -> Self {
        self.command.display_name = display_name.into();
        self
    }

    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.command.description = description.into();
        self
    }

    pub fn param(mut self, param: ParameterDefinition) -> Self {
        self.command.parameters.push(param);
        self
    }

    pub fn param_simple(
        mut self,
        name: impl Into<String>,
        display_name: impl Into<String>,
        data_type: MetricDataType,
    ) -> Self {
        self.command.parameters.push(ParameterDefinition {
            name: name.into(),
            display_name: display_name.into(),
            description: String::new(),
            param_type: data_type,
            required: true,
            default_value: None,
            min: None,
            max: None,
            options: Vec::new(),
        });
        self
    }

    pub fn param_optional(
        mut self,
        name: impl Into<String>,
        display_name: impl Into<String>,
        data_type: MetricDataType,
    ) -> Self {
        self.command.parameters.push(ParameterDefinition {
            name: name.into(),
            display_name: display_name.into(),
            description: String::new(),
            param_type: data_type,
            required: false,
            default_value: None,
            min: None,
            max: None,
            options: Vec::new(),
        });
        self
    }

    pub fn param_with_default(
        mut self,
        name: impl Into<String>,
        display_name: impl Into<String>,
        data_type: MetricDataType,
        default: MetricValue,
    ) -> Self {
        self.command.parameters.push(ParameterDefinition {
            name: name.into(),
            display_name: display_name.into(),
            description: String::new(),
            param_type: data_type,
            required: false,
            default_value: Some(default),
            min: None,
            max: None,
            options: Vec::new(),
        });
        self
    }

    pub fn sample(mut self, sample: serde_json::Value) -> Self {
        self.command.samples.push(sample);
        self
    }

    pub fn build(self) -> ExtensionCommand {
        self.command
    }
}

/// Helper type for building parameter definitions
#[derive(Debug, Clone)]
pub struct ParamBuilder {
    param: ParameterDefinition,
}

impl ParamBuilder {
    pub fn new(name: impl Into<String>, data_type: MetricDataType) -> Self {
        Self {
            param: ParameterDefinition {
                name: name.into(),
                display_name: String::new(),
                description: String::new(),
                param_type: data_type,
                required: true,
                default_value: None,
                min: None,
                max: None,
                options: Vec::new(),
            },
        }
    }

    pub fn display_name(mut self, display_name: impl Into<String>) -> Self {
        self.param.display_name = display_name.into();
        self
    }

    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.param.description = description.into();
        self
    }

    pub fn optional(mut self) -> Self {
        self.param.required = false;
        self
    }

    pub fn required(mut self) -> Self {
        self.param.required = true;
        self
    }

    pub fn default(mut self, value: MetricValue) -> Self {
        self.param.default_value = Some(value);
        self.param.required = false;
        self
    }

    pub fn min(mut self, min: f64) -> Self {
        self.param.min = Some(min);
        self
    }

    pub fn max(mut self, max: f64) -> Self {
        self.param.max = Some(max);
        self
    }

    pub fn options(mut self, options: Vec<String>) -> Self {
        self.param.options = options;
        self
    }

    pub fn build(self) -> ParameterDefinition {
        self.param
    }
}

// ============================================================================
// Static Helper Macros
// ============================================================================

/// Create a static ExtensionMetadata
#[macro_export]
macro_rules! static_metadata {
    ($id:literal, $name:literal, $version:literal) => {{
        static META: $crate::ExtensionMetadata =
            $crate::ExtensionMetadata::new($id, $name, $version);
        &META
    }};
}

/// Create a static slice of metrics
#[macro_export]
macro_rules! static_metrics {
    ($($metric:expr),* $(,)?) => {{
        static METRICS: &[$crate::MetricDescriptor] = &[$($metric),*];
        METRICS
    }};
}

/// Create a static slice of commands
#[macro_export]
macro_rules! static_commands {
    ($($cmd:expr),* $(,)?) => {{
        static COMMANDS: &[$crate::ExtensionCommand] = &[$($cmd),*];
        COMMANDS
    }};
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_capability_constants() {
        assert_eq!(
            capability_constants::DEVICE_METRICS_READ,
            "device_metrics_read"
        );
        assert_eq!(
            capability_constants::DEVICE_METRICS_WRITE,
            "device_metrics_write"
        );
        assert_eq!(capability_constants::DEVICE_CONTROL, "device_control");
        assert_eq!(capability_constants::STORAGE_QUERY, "storage_query");
        assert_eq!(capability_constants::EVENT_PUBLISH, "event_publish");
        assert_eq!(capability_constants::EVENT_SUBSCRIBE, "event_subscribe");
        assert_eq!(capability_constants::TELEMETRY_HISTORY, "telemetry_history");
        assert_eq!(capability_constants::METRICS_AGGREGATE, "metrics_aggregate");
        assert_eq!(capability_constants::EXTENSION_CALL, "extension_call");
        assert_eq!(capability_constants::AGENT_TRIGGER, "agent_trigger");
        assert_eq!(capability_constants::RULE_TRIGGER, "rule_trigger");
    }

    #[test]
    fn test_metric_builder() {
        let metric = MetricBuilder::new("test", "Test Metric")
            .float()
            .unit("°C")
            .min(-40.0)
            .max(100.0)
            .required()
            .build();

        assert_eq!(metric.name, "test");
        assert_eq!(metric.display_name, "Test Metric");
        assert_eq!(metric.data_type, MetricDataType::Float);
        assert_eq!(metric.unit, "°C");
        assert_eq!(metric.min, Some(-40.0));
        assert_eq!(metric.max, Some(100.0));
        assert!(metric.required);
    }

    #[test]
    fn test_extension_metadata() {
        let meta = ExtensionMetadata::new("test-ext", "Test Extension", "1.0.0")
            .with_description("A test extension")
            .with_author("Test Author");

        assert_eq!(meta.id, "test-ext");
        assert_eq!(meta.name, "Test Extension");
        assert_eq!(meta.version, "1.0.0");
        assert_eq!(meta.description, Some("A test extension".to_string()));
        assert_eq!(meta.author, Some("Test Author".to_string()));
    }

    #[test]
    fn test_abi_version() {
        assert_eq!(ABI_VERSION, 3);
        assert_eq!(SDK_ABI_VERSION, 3);
    }
}