Merge commit 'd803bfe2b1fe7f5e219e50ac20d6801a0a58ac75' as 'vendor/ruvector'
This commit is contained in:
837
vendor/ruvector/examples/edge-net/src/capabilities/mod.rs
vendored
Normal file
837
vendor/ruvector/examples/edge-net/src/capabilities/mod.rs
vendored
Normal file
@@ -0,0 +1,837 @@
|
||||
//! # Exotic AI Capabilities Module
|
||||
//!
|
||||
//! Provides a unified interface for exotic AI WASM capabilities:
|
||||
//! - **Time Crystal**: P2P synchronization using discrete time crystal dynamics
|
||||
//! - **NAO**: Neural Autonomous Organization for decentralized governance
|
||||
//! - **MicroLoRA**: Per-node self-learning with rank-2 adaptation
|
||||
//! - **HDC**: Hyperdimensional Computing for distributed reasoning
|
||||
//! - **BTSP**: One-shot learning via Behavioral Timescale Synaptic Plasticity
|
||||
//! - **WTA**: Winner-Take-All for instant decisions
|
||||
//! - **Global Workspace**: Attention bottleneck (4-7 items)
|
||||
//! - **Morphogenetic**: Network growth through cellular differentiation
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Available exotic capabilities
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct CapabilityInfo {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub enabled: bool,
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
/// Unified interface for all exotic WASM capabilities
|
||||
#[wasm_bindgen]
|
||||
pub struct WasmCapabilities {
|
||||
// Time Crystal for P2P synchronization
|
||||
#[cfg(feature = "exotic")]
|
||||
time_crystal: Option<ruvector_exotic_wasm::TimeCrystal>,
|
||||
|
||||
// NAO for governance
|
||||
#[cfg(feature = "exotic")]
|
||||
nao: Option<ruvector_exotic_wasm::NeuralAutonomousOrg>,
|
||||
|
||||
// Morphogenetic network
|
||||
#[cfg(feature = "exotic")]
|
||||
morphogenetic: Option<ruvector_exotic_wasm::MorphogeneticNetwork>,
|
||||
|
||||
// MicroLoRA for self-learning
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
micro_lora: Option<ruvector_learning_wasm::MicroLoRAEngine>,
|
||||
|
||||
// HDC for distributed reasoning
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
hdc_memory: Option<ruvector_nervous_system_wasm::HdcMemory>,
|
||||
|
||||
// WTA for instant decisions
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
wta_layer: Option<ruvector_nervous_system_wasm::WTALayer>,
|
||||
|
||||
// Global Workspace for attention
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
workspace: Option<ruvector_nervous_system_wasm::GlobalWorkspace>,
|
||||
|
||||
// BTSP for one-shot learning
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
btsp_layer: Option<ruvector_nervous_system_wasm::BTSPLayer>,
|
||||
|
||||
// Configuration
|
||||
node_id: String,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl WasmCapabilities {
|
||||
/// Create a new capabilities manager for a node
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(node_id: &str) -> Self {
|
||||
Self {
|
||||
#[cfg(feature = "exotic")]
|
||||
time_crystal: None,
|
||||
#[cfg(feature = "exotic")]
|
||||
nao: None,
|
||||
#[cfg(feature = "exotic")]
|
||||
morphogenetic: None,
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
micro_lora: None,
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
hdc_memory: None,
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
wta_layer: None,
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
workspace: None,
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
btsp_layer: None,
|
||||
node_id: node_id.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// List all available exotic capabilities
|
||||
#[wasm_bindgen(js_name = getCapabilities)]
|
||||
pub fn get_capabilities(&self) -> JsValue {
|
||||
let mut capabilities = Vec::new();
|
||||
|
||||
// Exotic capabilities
|
||||
#[cfg(feature = "exotic")]
|
||||
{
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "time_crystal".to_string(),
|
||||
description: "P2P synchronization using discrete time crystal dynamics".to_string(),
|
||||
enabled: self.time_crystal.is_some(),
|
||||
version: ruvector_exotic_wasm::version(),
|
||||
});
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "nao".to_string(),
|
||||
description: "Neural Autonomous Organization for decentralized governance".to_string(),
|
||||
enabled: self.nao.is_some(),
|
||||
version: ruvector_exotic_wasm::version(),
|
||||
});
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "morphogenetic".to_string(),
|
||||
description: "Network growth through cellular differentiation".to_string(),
|
||||
enabled: self.morphogenetic.is_some(),
|
||||
version: ruvector_exotic_wasm::version(),
|
||||
});
|
||||
}
|
||||
|
||||
// Learning-enhanced capabilities
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
{
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "micro_lora".to_string(),
|
||||
description: "Per-node self-learning with rank-2 LoRA adaptation (<100us)".to_string(),
|
||||
enabled: self.micro_lora.is_some(),
|
||||
version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
});
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "hdc".to_string(),
|
||||
description: "Hyperdimensional Computing with 10,000-bit vectors".to_string(),
|
||||
enabled: self.hdc_memory.is_some(),
|
||||
version: ruvector_nervous_system_wasm::version(),
|
||||
});
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "wta".to_string(),
|
||||
description: "Winner-Take-All for instant decisions (<1us)".to_string(),
|
||||
enabled: self.wta_layer.is_some(),
|
||||
version: ruvector_nervous_system_wasm::version(),
|
||||
});
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "global_workspace".to_string(),
|
||||
description: "Attention bottleneck with 4-7 item capacity".to_string(),
|
||||
enabled: self.workspace.is_some(),
|
||||
version: ruvector_nervous_system_wasm::version(),
|
||||
});
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "btsp".to_string(),
|
||||
description: "Behavioral Timescale Synaptic Plasticity for one-shot learning".to_string(),
|
||||
enabled: self.btsp_layer.is_some(),
|
||||
version: ruvector_nervous_system_wasm::version(),
|
||||
});
|
||||
}
|
||||
|
||||
// Fallback when no features enabled
|
||||
#[cfg(not(any(feature = "exotic", feature = "learning-enhanced")))]
|
||||
{
|
||||
capabilities.push(CapabilityInfo {
|
||||
name: "base".to_string(),
|
||||
description: "Base edge-net capabilities only. Enable 'exotic' or 'learning-enhanced' features for more.".to_string(),
|
||||
enabled: true,
|
||||
version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
serde_wasm_bindgen::to_value(&capabilities).unwrap_or(JsValue::NULL)
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// Time Crystal Methods (P2P Synchronization)
|
||||
// ========================================================================
|
||||
|
||||
/// Enable Time Crystal for P2P synchronization
|
||||
///
|
||||
/// Time crystals use discrete time crystal dynamics for robust distributed
|
||||
/// coordination with period-doubled oscillations and Floquet engineering.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `oscillators` - Number of oscillators (more = better coordination)
|
||||
/// * `period_ms` - Base oscillation period in milliseconds
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = enableTimeCrystal)]
|
||||
pub fn enable_time_crystal(&mut self, oscillators: usize, period_ms: u32) -> bool {
|
||||
let mut crystal = ruvector_exotic_wasm::TimeCrystal::new(oscillators, period_ms);
|
||||
crystal.crystallize();
|
||||
self.time_crystal = Some(crystal);
|
||||
true
|
||||
}
|
||||
|
||||
/// Get the current time crystal synchronization level (0.0 - 1.0)
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = getTimeCrystalSync)]
|
||||
pub fn get_time_crystal_sync(&self) -> f32 {
|
||||
self.time_crystal
|
||||
.as_ref()
|
||||
.map(|c| c.order_parameter())
|
||||
.unwrap_or(0.0)
|
||||
}
|
||||
|
||||
/// Tick the time crystal and get coordination pattern
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = tickTimeCrystal)]
|
||||
pub fn tick_time_crystal(&mut self) -> JsValue {
|
||||
if let Some(ref mut crystal) = self.time_crystal {
|
||||
let pattern = crystal.tick();
|
||||
serde_wasm_bindgen::to_value(&pattern).unwrap_or(JsValue::NULL)
|
||||
} else {
|
||||
JsValue::NULL
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if time crystal is crystallized (stable coordination)
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = isTimeCrystalStable)]
|
||||
pub fn is_time_crystal_stable(&self) -> bool {
|
||||
self.time_crystal
|
||||
.as_ref()
|
||||
.map(|c| c.is_crystallized())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// NAO Methods (Decentralized Governance)
|
||||
// ========================================================================
|
||||
|
||||
/// Enable Neural Autonomous Organization for decentralized governance
|
||||
///
|
||||
/// NAO provides stake-weighted quadratic voting with oscillatory
|
||||
/// synchronization for coherent collective decision-making.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `quorum` - Required quorum for proposals (0.0 - 1.0)
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = enableNAO)]
|
||||
pub fn enable_nao(&mut self, quorum: f32) -> bool {
|
||||
let mut nao = ruvector_exotic_wasm::NeuralAutonomousOrg::new(quorum.clamp(0.0, 1.0));
|
||||
// Register this node as a member
|
||||
nao.add_member(&self.node_id, 100);
|
||||
self.nao = Some(nao);
|
||||
true
|
||||
}
|
||||
|
||||
/// Add a member to the NAO
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = addNAOMember)]
|
||||
pub fn add_nao_member(&mut self, member_id: &str, stake: u64) -> bool {
|
||||
if let Some(ref mut nao) = self.nao {
|
||||
nao.add_member(member_id, stake);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Propose an action in the NAO
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = proposeNAO)]
|
||||
pub fn propose_nao(&mut self, action: &str) -> String {
|
||||
if let Some(ref mut nao) = self.nao {
|
||||
nao.propose(action)
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Vote on a NAO proposal
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = voteNAO)]
|
||||
pub fn vote_nao(&mut self, proposal_id: &str, weight: f32) -> bool {
|
||||
if let Some(ref mut nao) = self.nao {
|
||||
nao.vote(proposal_id, &self.node_id, weight.clamp(0.0, 1.0))
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute a NAO proposal if quorum reached
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = executeNAO)]
|
||||
pub fn execute_nao(&mut self, proposal_id: &str) -> bool {
|
||||
if let Some(ref mut nao) = self.nao {
|
||||
nao.execute(proposal_id)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Get NAO synchronization level
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = getNAOSync)]
|
||||
pub fn get_nao_sync(&self) -> f32 {
|
||||
self.nao
|
||||
.as_ref()
|
||||
.map(|n| n.synchronization())
|
||||
.unwrap_or(0.0)
|
||||
}
|
||||
|
||||
/// Tick the NAO dynamics
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = tickNAO)]
|
||||
pub fn tick_nao(&mut self, dt: f32) {
|
||||
if let Some(ref mut nao) = self.nao {
|
||||
nao.tick(dt);
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// MicroLoRA Methods (Self-Learning)
|
||||
// ========================================================================
|
||||
|
||||
/// Enable MicroLoRA for per-node self-learning
|
||||
///
|
||||
/// MicroLoRA provides rank-2 LoRA adaptation with <100us latency
|
||||
/// for real-time per-operator learning.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `dim` - Embedding dimension for the LoRA adapter
|
||||
/// * `rank` - Rank of the adaptation (typically 2-4)
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = enableMicroLoRA)]
|
||||
pub fn enable_micro_lora(&mut self, dim: usize, rank: usize) -> bool {
|
||||
let config = ruvector_learning_wasm::LoRAConfig {
|
||||
dim,
|
||||
rank: rank.max(2),
|
||||
alpha: 0.1,
|
||||
learning_rate: 0.01,
|
||||
dropout: 0.0,
|
||||
};
|
||||
self.micro_lora = Some(ruvector_learning_wasm::MicroLoRAEngine::new(config));
|
||||
true
|
||||
}
|
||||
|
||||
/// Adapt the MicroLoRA weights with a gradient
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = adaptMicroLoRA)]
|
||||
pub fn adapt_micro_lora(&mut self, _operator_type: &str, gradient: &[f32]) -> bool {
|
||||
if let Some(ref mut lora) = self.micro_lora {
|
||||
lora.adapt(gradient);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply MicroLoRA to get adapted output
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = applyMicroLoRA)]
|
||||
pub fn apply_micro_lora(&mut self, _operator_type: &str, input: &[f32]) -> Vec<f32> {
|
||||
if let Some(ref mut lora) = self.micro_lora {
|
||||
lora.forward(input)
|
||||
} else {
|
||||
input.to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// HDC Methods (Hyperdimensional Computing)
|
||||
// ========================================================================
|
||||
|
||||
/// Enable HDC memory for distributed reasoning
|
||||
///
|
||||
/// HDC uses 10,000-bit binary hypervectors for efficient semantic
|
||||
/// operations with <50ns bind time.
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = enableHDC)]
|
||||
pub fn enable_hdc(&mut self) -> bool {
|
||||
self.hdc_memory = Some(ruvector_nervous_system_wasm::HdcMemory::new());
|
||||
true
|
||||
}
|
||||
|
||||
/// Store a pattern in HDC memory
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = storeHDC)]
|
||||
pub fn store_hdc(&mut self, key: &str) -> bool {
|
||||
if let Some(ref mut memory) = self.hdc_memory {
|
||||
let hv = ruvector_nervous_system_wasm::Hypervector::random();
|
||||
memory.store(key, hv);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve from HDC memory with similarity threshold
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = retrieveHDC)]
|
||||
pub fn retrieve_hdc(&self, _key: &str, threshold: f32) -> JsValue {
|
||||
if let Some(ref memory) = self.hdc_memory {
|
||||
let query = ruvector_nervous_system_wasm::Hypervector::random();
|
||||
// retrieve already returns JsValue
|
||||
memory.retrieve(&query, threshold)
|
||||
} else {
|
||||
JsValue::NULL
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// WTA Methods (Winner-Take-All)
|
||||
// ========================================================================
|
||||
|
||||
/// Enable WTA layer for instant decisions
|
||||
///
|
||||
/// WTA provides <1us decision time with lateral inhibition.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `num_neurons` - Number of competing neurons
|
||||
/// * `inhibition` - Lateral inhibition strength (0.0 - 1.0)
|
||||
/// * `threshold` - Activation threshold
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = enableWTA)]
|
||||
pub fn enable_wta(&mut self, num_neurons: usize, inhibition: f32, threshold: f32) -> bool {
|
||||
match ruvector_nervous_system_wasm::WTALayer::new(num_neurons, threshold, inhibition) {
|
||||
Ok(layer) => {
|
||||
self.wta_layer = Some(layer);
|
||||
true
|
||||
}
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Compete to find the winner
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = competeWTA)]
|
||||
pub fn compete_wta(&mut self, activations: &[f32]) -> i32 {
|
||||
if let Some(ref mut wta) = self.wta_layer {
|
||||
wta.compete(activations).unwrap_or(-1)
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// Global Workspace Methods (Attention)
|
||||
// ========================================================================
|
||||
|
||||
/// Enable Global Workspace for attention bottleneck
|
||||
///
|
||||
/// Based on Global Workspace Theory with 4-7 item capacity
|
||||
/// (Miller's Law: 7 +/- 2).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `capacity` - Workspace capacity (typically 4-7)
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = enableGlobalWorkspace)]
|
||||
pub fn enable_global_workspace(&mut self, capacity: usize) -> bool {
|
||||
self.workspace = Some(ruvector_nervous_system_wasm::GlobalWorkspace::new(
|
||||
capacity.clamp(4, 9),
|
||||
));
|
||||
true
|
||||
}
|
||||
|
||||
/// Broadcast item to the global workspace
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = broadcastToWorkspace)]
|
||||
pub fn broadcast_to_workspace(
|
||||
&mut self,
|
||||
content: &[f32],
|
||||
salience: f32,
|
||||
source_module: u16,
|
||||
) -> bool {
|
||||
if let Some(ref mut workspace) = self.workspace {
|
||||
let item = ruvector_nervous_system_wasm::WorkspaceItem::new(
|
||||
content,
|
||||
salience,
|
||||
source_module,
|
||||
js_sys::Date::now() as u64,
|
||||
);
|
||||
workspace.broadcast(item)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current workspace contents
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = getWorkspaceContents)]
|
||||
pub fn get_workspace_contents(&self) -> JsValue {
|
||||
if let Some(ref workspace) = self.workspace {
|
||||
// retrieve() returns JsValue already
|
||||
workspace.retrieve()
|
||||
} else {
|
||||
JsValue::NULL
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// BTSP Methods (One-Shot Learning)
|
||||
// ========================================================================
|
||||
|
||||
/// Enable BTSP layer for one-shot learning
|
||||
///
|
||||
/// BTSP (Behavioral Timescale Synaptic Plasticity) enables immediate
|
||||
/// pattern association without iterative training.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `input_dim` - Input dimension
|
||||
/// * `time_constant` - Synaptic time constant (ms)
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = enableBTSP)]
|
||||
pub fn enable_btsp(&mut self, input_dim: usize, time_constant: f32) -> bool {
|
||||
self.btsp_layer = Some(ruvector_nervous_system_wasm::BTSPLayer::new(
|
||||
input_dim,
|
||||
time_constant,
|
||||
));
|
||||
true
|
||||
}
|
||||
|
||||
/// One-shot associate a pattern
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = oneShotAssociate)]
|
||||
pub fn one_shot_associate(&mut self, pattern: &[f32], target: f32) -> bool {
|
||||
if let Some(ref mut btsp) = self.btsp_layer {
|
||||
btsp.one_shot_associate(pattern, target).is_ok()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Forward pass through BTSP layer (returns scalar output)
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
#[wasm_bindgen(js_name = forwardBTSP)]
|
||||
pub fn forward_btsp(&self, input: &[f32]) -> f32 {
|
||||
if let Some(ref btsp) = self.btsp_layer {
|
||||
btsp.forward(input).unwrap_or(0.0)
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// Morphogenetic Methods (Network Growth)
|
||||
// ========================================================================
|
||||
|
||||
/// Enable Morphogenetic Network for emergent topology
|
||||
///
|
||||
/// Uses cellular differentiation through morphogen gradients
|
||||
/// for self-organizing network growth.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `width` - Grid width
|
||||
/// * `height` - Grid height
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = enableMorphogenetic)]
|
||||
pub fn enable_morphogenetic(&mut self, width: i32, height: i32) -> bool {
|
||||
let mut network = ruvector_exotic_wasm::MorphogeneticNetwork::new(width, height);
|
||||
// Seed initial cell at center
|
||||
network.seed_cell(width / 2, height / 2, ruvector_exotic_wasm::CellType::Stem);
|
||||
self.morphogenetic = Some(network);
|
||||
true
|
||||
}
|
||||
|
||||
/// Grow the morphogenetic network
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = growMorphogenetic)]
|
||||
pub fn grow_morphogenetic(&mut self, rate: f32) {
|
||||
if let Some(ref mut network) = self.morphogenetic {
|
||||
network.grow(rate);
|
||||
}
|
||||
}
|
||||
|
||||
/// Differentiate cells in the network
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = differentiateMorphogenetic)]
|
||||
pub fn differentiate_morphogenetic(&mut self) {
|
||||
if let Some(ref mut network) = self.morphogenetic {
|
||||
network.differentiate();
|
||||
}
|
||||
}
|
||||
|
||||
/// Prune weak connections
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = pruneMorphogenetic)]
|
||||
pub fn prune_morphogenetic(&mut self, threshold: f32) {
|
||||
if let Some(ref mut network) = self.morphogenetic {
|
||||
network.prune(threshold);
|
||||
}
|
||||
}
|
||||
|
||||
/// Get morphogenetic network cell count
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = getMorphogeneticCellCount)]
|
||||
pub fn get_morphogenetic_cell_count(&self) -> usize {
|
||||
self.morphogenetic
|
||||
.as_ref()
|
||||
.map(|n| n.cell_count())
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Get morphogenetic network statistics
|
||||
#[cfg(feature = "exotic")]
|
||||
#[wasm_bindgen(js_name = getMorphogeneticStats)]
|
||||
pub fn get_morphogenetic_stats(&self) -> JsValue {
|
||||
if let Some(ref network) = self.morphogenetic {
|
||||
let stats = network.stats();
|
||||
serde_wasm_bindgen::to_value(&stats).unwrap_or(JsValue::NULL)
|
||||
} else {
|
||||
JsValue::NULL
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// Utility Methods
|
||||
// ========================================================================
|
||||
|
||||
/// Get a summary of all enabled capabilities
|
||||
#[wasm_bindgen(js_name = getSummary)]
|
||||
pub fn get_summary(&self) -> JsValue {
|
||||
let mut summary = serde_json::json!({
|
||||
"node_id": self.node_id,
|
||||
"capabilities": {}
|
||||
});
|
||||
|
||||
#[cfg(feature = "exotic")]
|
||||
{
|
||||
summary["capabilities"]["time_crystal"] = serde_json::json!({
|
||||
"enabled": self.time_crystal.is_some(),
|
||||
"sync_level": self.time_crystal.as_ref().map(|c| c.order_parameter()).unwrap_or(0.0),
|
||||
});
|
||||
summary["capabilities"]["nao"] = serde_json::json!({
|
||||
"enabled": self.nao.is_some(),
|
||||
"member_count": self.nao.as_ref().map(|n| n.member_count()).unwrap_or(0),
|
||||
});
|
||||
summary["capabilities"]["morphogenetic"] = serde_json::json!({
|
||||
"enabled": self.morphogenetic.is_some(),
|
||||
"cell_count": self.morphogenetic.as_ref().map(|n| n.cell_count()).unwrap_or(0),
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(feature = "learning-enhanced")]
|
||||
{
|
||||
summary["capabilities"]["micro_lora"] = serde_json::json!({
|
||||
"enabled": self.micro_lora.is_some(),
|
||||
});
|
||||
summary["capabilities"]["hdc"] = serde_json::json!({
|
||||
"enabled": self.hdc_memory.is_some(),
|
||||
});
|
||||
summary["capabilities"]["wta"] = serde_json::json!({
|
||||
"enabled": self.wta_layer.is_some(),
|
||||
});
|
||||
summary["capabilities"]["global_workspace"] = serde_json::json!({
|
||||
"enabled": self.workspace.is_some(),
|
||||
});
|
||||
summary["capabilities"]["btsp"] = serde_json::json!({
|
||||
"enabled": self.btsp_layer.is_some(),
|
||||
});
|
||||
}
|
||||
|
||||
serde_wasm_bindgen::to_value(&summary).unwrap_or(JsValue::NULL)
|
||||
}
|
||||
|
||||
/// Step all enabled capabilities forward (for main loop integration)
|
||||
#[wasm_bindgen]
|
||||
pub fn step(&mut self, dt: f32) {
|
||||
#[cfg(feature = "exotic")]
|
||||
{
|
||||
if let Some(ref mut crystal) = self.time_crystal {
|
||||
crystal.tick();
|
||||
}
|
||||
if let Some(ref mut nao) = self.nao {
|
||||
nao.tick(dt);
|
||||
}
|
||||
if let Some(ref mut network) = self.morphogenetic {
|
||||
network.grow(0.01);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Stub implementations when features are not enabled
|
||||
#[cfg(not(feature = "exotic"))]
|
||||
#[wasm_bindgen]
|
||||
impl WasmCapabilities {
|
||||
#[wasm_bindgen(js_name = enableTimeCrystal)]
|
||||
pub fn enable_time_crystal(&mut self, _oscillators: usize, _period_ms: u32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = getTimeCrystalSync)]
|
||||
pub fn get_time_crystal_sync(&self) -> f32 {
|
||||
0.0
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = tickTimeCrystal)]
|
||||
pub fn tick_time_crystal(&mut self) -> JsValue {
|
||||
JsValue::NULL
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = isTimeCrystalStable)]
|
||||
pub fn is_time_crystal_stable(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = enableNAO)]
|
||||
pub fn enable_nao(&mut self, _quorum: f32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = addNAOMember)]
|
||||
pub fn add_nao_member(&mut self, _member_id: &str, _stake: u64) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = proposeNAO)]
|
||||
pub fn propose_nao(&mut self, _action: &str) -> String {
|
||||
String::new()
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = voteNAO)]
|
||||
pub fn vote_nao(&mut self, _proposal_id: &str, _weight: f32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = executeNAO)]
|
||||
pub fn execute_nao(&mut self, _proposal_id: &str) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = getNAOSync)]
|
||||
pub fn get_nao_sync(&self) -> f32 {
|
||||
0.0
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = tickNAO)]
|
||||
pub fn tick_nao(&mut self, _dt: f32) {}
|
||||
|
||||
#[wasm_bindgen(js_name = enableMorphogenetic)]
|
||||
pub fn enable_morphogenetic(&mut self, _width: i32, _height: i32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = growMorphogenetic)]
|
||||
pub fn grow_morphogenetic(&mut self, _rate: f32) {}
|
||||
|
||||
#[wasm_bindgen(js_name = differentiateMorphogenetic)]
|
||||
pub fn differentiate_morphogenetic(&mut self) {}
|
||||
|
||||
#[wasm_bindgen(js_name = pruneMorphogenetic)]
|
||||
pub fn prune_morphogenetic(&mut self, _threshold: f32) {}
|
||||
|
||||
#[wasm_bindgen(js_name = getMorphogeneticCellCount)]
|
||||
pub fn get_morphogenetic_cell_count(&self) -> usize {
|
||||
0
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = getMorphogeneticStats)]
|
||||
pub fn get_morphogenetic_stats(&self) -> JsValue {
|
||||
JsValue::NULL
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "learning-enhanced"))]
|
||||
#[wasm_bindgen]
|
||||
impl WasmCapabilities {
|
||||
#[wasm_bindgen(js_name = enableMicroLoRA)]
|
||||
pub fn enable_micro_lora(&mut self, _dim: usize, _rank: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = adaptMicroLoRA)]
|
||||
pub fn adapt_micro_lora(&mut self, _operator_type: &str, _gradient: &[f32]) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = applyMicroLoRA)]
|
||||
pub fn apply_micro_lora(&mut self, _operator_type: &str, input: &[f32]) -> Vec<f32> {
|
||||
input.to_vec()
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = enableHDC)]
|
||||
pub fn enable_hdc(&mut self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = storeHDC)]
|
||||
pub fn store_hdc(&mut self, _key: &str) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = retrieveHDC)]
|
||||
pub fn retrieve_hdc(&self, _key: &str, _threshold: f32) -> JsValue {
|
||||
JsValue::NULL
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = enableWTA)]
|
||||
pub fn enable_wta(&mut self, _num_neurons: usize, _inhibition: f32, _threshold: f32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = competeWTA)]
|
||||
pub fn compete_wta(&mut self, _activations: &[f32]) -> i32 {
|
||||
-1
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = enableGlobalWorkspace)]
|
||||
pub fn enable_global_workspace(&mut self, _capacity: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = broadcastToWorkspace)]
|
||||
pub fn broadcast_to_workspace(
|
||||
&mut self,
|
||||
_content: &[f32],
|
||||
_salience: f32,
|
||||
_source_module: u16,
|
||||
) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = getWorkspaceContents)]
|
||||
pub fn get_workspace_contents(&self) -> JsValue {
|
||||
JsValue::NULL
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = enableBTSP)]
|
||||
pub fn enable_btsp(&mut self, _input_dim: usize, _time_constant: f32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = oneShotAssociate)]
|
||||
pub fn one_shot_associate(&mut self, _pattern: &[f32], _target: f32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = forwardBTSP)]
|
||||
pub fn forward_btsp(&self, _input: &[f32]) -> f32 {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_capabilities_creation() {
|
||||
let caps = WasmCapabilities::new("test-node");
|
||||
assert_eq!(caps.node_id, "test-node");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user