1use crate::ModelConfig;
8use anyhow::Result;
9use scirs2_core::ndarray_ext::Array1;
10use serde::{Deserialize, Serialize};
11use std::collections::HashMap;
12use uuid::Uuid;
13
14#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
16pub enum ConsciousnessLevel {
17 Reactive,
19 Associative,
21 SelfAware,
23 Reflective,
25 MetaCognitive,
27 Transcendent,
29}
30
31impl ConsciousnessLevel {
32 pub fn awareness_factor(&self) -> f32 {
33 match self {
34 ConsciousnessLevel::Reactive => 0.1,
35 ConsciousnessLevel::Associative => 0.3,
36 ConsciousnessLevel::SelfAware => 0.5,
37 ConsciousnessLevel::Reflective => 0.7,
38 ConsciousnessLevel::MetaCognitive => 0.9,
39 ConsciousnessLevel::Transcendent => 1.0,
40 }
41 }
42
43 pub fn complexity_threshold(&self) -> f32 {
44 match self {
45 ConsciousnessLevel::Reactive => 0.1,
46 ConsciousnessLevel::Associative => 0.2,
47 ConsciousnessLevel::SelfAware => 0.4,
48 ConsciousnessLevel::Reflective => 0.6,
49 ConsciousnessLevel::MetaCognitive => 0.8,
50 ConsciousnessLevel::Transcendent => 1.0,
51 }
52 }
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct AttentionMechanism {
58 pub focus_weights: HashMap<String, f32>,
60 pub attention_memory: Vec<(String, f32, chrono::DateTime<chrono::Utc>)>,
62 pub attention_capacity: usize,
64 pub decay_rate: f32,
66}
67
68impl AttentionMechanism {
69 pub fn new(capacity: usize, decay_rate: f32) -> Self {
70 Self {
71 focus_weights: HashMap::new(),
72 attention_memory: Vec::new(),
73 attention_capacity: capacity,
74 decay_rate,
75 }
76 }
77
78 pub fn focus_on(&mut self, concept: &str, intensity: f32) {
80 let now = chrono::Utc::now();
81
82 let current_weight = self.focus_weights.get(concept).unwrap_or(&0.0);
84 let new_weight = (current_weight + intensity).min(1.0);
85 self.focus_weights.insert(concept.to_string(), new_weight);
86
87 self.attention_memory
89 .push((concept.to_string(), intensity, now));
90
91 if self.attention_memory.len() > self.attention_capacity {
93 self.attention_memory.remove(0);
94 }
95
96 self.apply_decay();
98 }
99
100 fn apply_decay(&mut self) {
102 let now = chrono::Utc::now();
103
104 for (_, weight) in self.focus_weights.iter_mut() {
106 *weight *= (1.0 - self.decay_rate).max(0.0);
107 }
108
109 self.focus_weights.retain(|_, &mut w| w > 0.01);
111
112 for (_, intensity, timestamp) in self.attention_memory.iter_mut() {
114 let hours_passed = now.signed_duration_since(*timestamp).num_hours() as f32;
115 let time_decay = (-hours_passed * 0.1).exp(); *intensity *= time_decay;
117 }
118
119 self.attention_memory
121 .retain(|(_, intensity, _)| *intensity > 0.01);
122 }
123
124 pub fn get_attention_distribution(&self) -> Vec<(String, f32)> {
126 let mut attention: Vec<_> = self
127 .focus_weights
128 .iter()
129 .map(|(k, &v)| (k.clone(), v))
130 .collect();
131 attention.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
132 attention
133 }
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
138pub struct WorkingMemory {
139 pub active_concepts: HashMap<String, Array1<f32>>,
141 pub concept_relationships: HashMap<(String, String), f32>,
143 pub capacity: usize,
145 pub consolidation_threshold: f32,
147}
148
149impl WorkingMemory {
150 pub fn new(capacity: Option<usize>) -> Self {
151 Self {
152 active_concepts: HashMap::new(),
153 concept_relationships: HashMap::new(),
154 capacity: capacity.unwrap_or(7), consolidation_threshold: 0.7,
156 }
157 }
158
159 pub fn add_concept(&mut self, concept: String, embedding: Array1<f32>) -> Result<()> {
161 if self.active_concepts.len() >= self.capacity {
163 self.remove_least_active()?;
164 }
165
166 self.active_concepts.insert(concept, embedding);
167 Ok(())
168 }
169
170 fn remove_least_active(&mut self) -> Result<()> {
172 if let Some(concept) = self.active_concepts.keys().next().cloned() {
174 self.active_concepts.remove(&concept);
175 }
176 Ok(())
177 }
178
179 pub fn update_relationship(&mut self, concept1: &str, concept2: &str, strength: f32) {
181 let key = if concept1 < concept2 {
182 (concept1.to_string(), concept2.to_string())
183 } else {
184 (concept2.to_string(), concept1.to_string())
185 };
186
187 self.concept_relationships.insert(key, strength);
188 }
189
190 pub fn get_related_concepts(&self, concept: &str) -> Vec<(String, f32)> {
192 self.concept_relationships
193 .iter()
194 .filter_map(|((c1, c2), &strength)| {
195 if c1 == concept {
196 Some((c2.clone(), strength))
197 } else if c2 == concept {
198 Some((c1.clone(), strength))
199 } else {
200 None
201 }
202 })
203 .collect()
204 }
205}
206
207#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct MetaCognition {
210 pub understanding_confidence: f32,
212 pub knowledge_gaps: Vec<String>,
214 pub strategy_effectiveness: HashMap<String, f32>,
216 pub reflection_history: Vec<(chrono::DateTime<chrono::Utc>, String, f32)>,
218}
219
220impl MetaCognition {
221 pub fn new() -> Self {
222 Self {
223 understanding_confidence: 0.5,
224 knowledge_gaps: Vec::new(),
225 strategy_effectiveness: HashMap::new(),
226 reflection_history: Vec::new(),
227 }
228 }
229
230 pub fn update_confidence(&mut self, performance_score: f32) {
232 let alpha = 0.1; self.understanding_confidence =
234 (1.0 - alpha) * self.understanding_confidence + alpha * performance_score;
235 self.understanding_confidence = self.understanding_confidence.clamp(0.0, 1.0);
236 }
237
238 pub fn reflect(&mut self, insight: String, confidence: f32) {
240 let now = chrono::Utc::now();
241 self.reflection_history.push((now, insight, confidence));
242
243 if self.reflection_history.len() > 100 {
245 self.reflection_history.remove(0);
246 }
247 }
248
249 pub fn identify_knowledge_gap(&mut self, domain: String) {
251 if !self.knowledge_gaps.contains(&domain) {
252 self.knowledge_gaps.push(domain);
253 }
254 }
255
256 pub fn update_strategy_effectiveness(&mut self, strategy: &str, effectiveness: f32) {
258 let current = self.strategy_effectiveness.get(strategy).unwrap_or(&0.5);
259 let alpha = 0.2;
260 let new_effectiveness = (1.0 - alpha) * current + alpha * effectiveness;
261 self.strategy_effectiveness
262 .insert(strategy.to_string(), new_effectiveness);
263 }
264}
265
266impl Default for MetaCognition {
267 fn default() -> Self {
268 Self::new()
269 }
270}
271
272#[derive(Debug)]
274pub struct ConsciousnessAwareEmbedding {
275 pub model_id: Uuid,
277 pub config: ModelConfig,
279 pub consciousness_level: ConsciousnessLevel,
281 pub attention: AttentionMechanism,
283 pub working_memory: WorkingMemory,
285 pub meta_cognition: MetaCognition,
287 pub embeddings: HashMap<String, Array1<f32>>,
289 pub consciousness_state: Array1<f32>,
291 pub learning_history: Vec<(String, Array1<f32>, f32)>, }
294
295impl ConsciousnessAwareEmbedding {
296 pub fn new(config: ModelConfig) -> Self {
297 let dimensions = config.dimensions;
298
299 Self {
300 model_id: Uuid::new_v4(),
301 config,
302 consciousness_level: ConsciousnessLevel::SelfAware,
303 attention: AttentionMechanism::new(20, 0.05),
304 working_memory: WorkingMemory::new(Some(7)),
305 meta_cognition: MetaCognition::new(),
306 embeddings: HashMap::new(),
307 consciousness_state: Array1::zeros(dimensions),
308 learning_history: Vec::new(),
309 }
310 }
311
312 pub fn generate_conscious_embedding(
314 &mut self,
315 entity: &str,
316 context: &[String],
317 ) -> Result<Array1<f32>> {
318 for ctx in context {
320 self.attention.focus_on(ctx, 0.3);
321 }
322 self.attention.focus_on(entity, 0.8);
323
324 let base_embedding = self
326 .embeddings
327 .get(entity)
328 .cloned()
329 .unwrap_or_else(|| self.create_base_embedding(entity));
330
331 let conscious_embedding =
333 self.apply_consciousness_enhancement(&base_embedding, entity, context)?;
334
335 self.working_memory
337 .add_concept(entity.to_string(), conscious_embedding.clone())?;
338
339 self.embeddings
341 .insert(entity.to_string(), conscious_embedding.clone());
342
343 self.reflect_on_embedding(entity, &conscious_embedding);
345
346 Ok(conscious_embedding)
347 }
348
349 fn create_base_embedding(&self, entity: &str) -> Array1<f32> {
351 let dimensions = self.config.dimensions;
352
353 let hash = entity.chars().map(|c| c as u32).sum::<u32>();
355 let _seed = hash as u64;
356
357 {
358 #[allow(unused_imports)]
359 use scirs2_core::random::{Random, Rng};
360 Array1::from_shape_fn(dimensions, |_| {
362 let mut random = Random::default();
363 random.gen_range(-0.1..0.1)
364 })
365 }
366 }
367
368 fn apply_consciousness_enhancement(
370 &mut self,
371 base_embedding: &Array1<f32>,
372 entity: &str,
373 context: &[String],
374 ) -> Result<Array1<f32>> {
375 let mut enhanced = base_embedding.clone();
376
377 let attention_factor = self.attention.focus_weights.get(entity).unwrap_or(&0.0)
379 * self.consciousness_level.awareness_factor();
380
381 let consciousness_influence = &self.consciousness_state * attention_factor;
383 enhanced = enhanced + consciousness_influence;
384
385 for ctx in context {
387 if let Some(ctx_embedding) = self.embeddings.get(ctx) {
388 let context_weight = self.attention.focus_weights.get(ctx).unwrap_or(&0.0) * 0.2;
389 enhanced = enhanced + ctx_embedding * context_weight;
390 }
391 }
392
393 let related_concepts = self.working_memory.get_related_concepts(entity);
395 for (related_concept, relationship_strength) in related_concepts {
396 if let Some(related_embedding) =
397 self.working_memory.active_concepts.get(&related_concept)
398 {
399 enhanced = enhanced + related_embedding * (relationship_strength * 0.1);
400 }
401 }
402
403 let confidence_factor = self.meta_cognition.understanding_confidence;
405 enhanced = enhanced * confidence_factor + base_embedding * (1.0 - confidence_factor);
406
407 let norm = enhanced.mapv(|x| x * x).sum().sqrt();
409 if norm > 0.0 {
410 enhanced /= norm;
411 }
412
413 Ok(enhanced)
414 }
415
416 fn reflect_on_embedding(&mut self, entity: &str, embedding: &Array1<f32>) {
418 let norm = embedding.mapv(|x| x * x).sum().sqrt();
420 let quality_score = if norm > 0.0 && norm < 2.0 {
421 (1.0 - (norm - 1.0).abs()).max(0.0)
422 } else {
423 0.0
424 };
425
426 self.meta_cognition.update_confidence(quality_score);
428
429 let reflection = format!(
431 "Generated embedding for '{entity}' with quality {quality_score:.3} and norm {norm:.3}"
432 );
433 self.meta_cognition.reflect(reflection, quality_score);
434
435 self.learning_history
437 .push((entity.to_string(), embedding.clone(), quality_score));
438
439 if self.learning_history.len() > 1000 {
441 self.learning_history.remove(0);
442 }
443 }
444
445 pub fn evolve_consciousness(&mut self) -> Result<()> {
447 let total_experience = self.learning_history.len();
448 let average_quality = if !self.learning_history.is_empty() {
449 self.learning_history.iter().map(|(_, _, q)| q).sum::<f32>() / total_experience as f32
450 } else {
451 0.0
452 };
453
454 let evolution_threshold = self.consciousness_level.complexity_threshold();
456
457 if average_quality > evolution_threshold && total_experience > 100 {
458 self.consciousness_level = match self.consciousness_level {
459 ConsciousnessLevel::Reactive => ConsciousnessLevel::Associative,
460 ConsciousnessLevel::Associative => ConsciousnessLevel::SelfAware,
461 ConsciousnessLevel::SelfAware => ConsciousnessLevel::Reflective,
462 ConsciousnessLevel::Reflective => ConsciousnessLevel::MetaCognitive,
463 ConsciousnessLevel::MetaCognitive => ConsciousnessLevel::Transcendent,
464 ConsciousnessLevel::Transcendent => ConsciousnessLevel::Transcendent, };
466
467 self.update_consciousness_state()?;
469
470 self.meta_cognition.reflect(
472 format!("Consciousness evolved to {:?}", self.consciousness_level),
473 0.9,
474 );
475 }
476
477 Ok(())
478 }
479
480 fn update_consciousness_state(&mut self) -> Result<()> {
482 let dimensions = self.consciousness_state.len();
483 let awareness_factor = self.consciousness_level.awareness_factor();
484
485 if !self.learning_history.is_empty() {
487 let recent_embeddings = self
488 .learning_history
489 .iter()
490 .rev()
491 .take(50) .map(|(_, emb, quality)| emb * *quality)
493 .fold(Array1::<f32>::zeros(dimensions), |acc, emb| acc + emb);
494
495 let count = self.learning_history.len().min(50) as f32;
496 let average_recent = recent_embeddings / count;
497
498 self.consciousness_state =
500 &self.consciousness_state * 0.8f32 + &average_recent * 0.2f32;
501
502 self.consciousness_state = &self.consciousness_state * awareness_factor;
504 }
505
506 Ok(())
507 }
508
509 pub fn get_consciousness_insights(&self) -> ConsciousnessInsights {
511 let attention_distribution = self.attention.get_attention_distribution();
512 let active_concepts: Vec<String> = self
513 .working_memory
514 .active_concepts
515 .keys()
516 .cloned()
517 .collect();
518 let recent_reflections: Vec<String> = self
519 .meta_cognition
520 .reflection_history
521 .iter()
522 .rev()
523 .take(5)
524 .map(|(_, reflection, _)| reflection.clone())
525 .collect();
526
527 ConsciousnessInsights {
528 consciousness_level: self.consciousness_level,
529 understanding_confidence: self.meta_cognition.understanding_confidence,
530 attention_distribution,
531 active_concepts,
532 recent_reflections,
533 total_experiences: self.learning_history.len(),
534 knowledge_gaps: self.meta_cognition.knowledge_gaps.clone(),
535 }
536 }
537}
538
539#[derive(Debug, Clone, Serialize, Deserialize)]
541pub struct ConsciousnessInsights {
542 pub consciousness_level: ConsciousnessLevel,
543 pub understanding_confidence: f32,
544 pub attention_distribution: Vec<(String, f32)>,
545 pub active_concepts: Vec<String>,
546 pub recent_reflections: Vec<String>,
547 pub total_experiences: usize,
548 pub knowledge_gaps: Vec<String>,
549}
550
551#[cfg(test)]
552mod tests {
553 use super::*;
554
555 #[test]
556 fn test_consciousness_levels() {
557 assert_eq!(ConsciousnessLevel::Reactive.awareness_factor(), 0.1);
558 assert_eq!(ConsciousnessLevel::Transcendent.awareness_factor(), 1.0);
559 }
560
561 #[test]
562 fn test_attention_mechanism() {
563 let mut attention = AttentionMechanism::new(10, 0.1);
564
565 attention.focus_on("concept1", 0.8);
566 attention.focus_on("concept2", 0.6);
567
568 let distribution = attention.get_attention_distribution();
569 assert_eq!(distribution.len(), 2);
570 assert_eq!(distribution[0].0, "concept1");
571 assert!(distribution[0].1 > distribution[1].1);
572 }
573
574 #[test]
575 fn test_working_memory() {
576 let mut memory = WorkingMemory::new(Some(3));
577
578 let emb1 = Array1::from_vec(vec![1.0, 2.0, 3.0]);
579 let emb2 = Array1::from_vec(vec![4.0, 5.0, 6.0]);
580
581 assert!(memory.add_concept("concept1".to_string(), emb1).is_ok());
582 assert!(memory.add_concept("concept2".to_string(), emb2).is_ok());
583
584 memory.update_relationship("concept1", "concept2", 0.8);
585 let related = memory.get_related_concepts("concept1");
586 assert_eq!(related.len(), 1);
587 assert_eq!(related[0].0, "concept2");
588 assert_eq!(related[0].1, 0.8);
589 }
590
591 #[test]
592 fn test_consciousness_aware_embedding() {
593 let config = ModelConfig::default().with_dimensions(64);
594 let mut model = ConsciousnessAwareEmbedding::new(config);
595
596 let context = vec!["related_entity".to_string()];
597 let embedding = model.generate_conscious_embedding("test_entity", &context);
598
599 assert!(embedding.is_ok());
600 let emb = embedding.unwrap();
601 assert_eq!(emb.len(), 64);
602
603 assert!(model.attention.focus_weights.contains_key("test_entity"));
605
606 assert!(model
608 .working_memory
609 .active_concepts
610 .contains_key("test_entity"));
611 }
612
613 #[test]
614 fn test_consciousness_evolution() {
615 let config = ModelConfig::default().with_dimensions(32);
616 let mut model = ConsciousnessAwareEmbedding::new(config);
617
618 for i in 0..150 {
620 let entity = format!("entity_{i}");
621 let embedding = Array1::from_vec(vec![0.1; 32]);
622 model.learning_history.push((entity, embedding, 0.9)); }
624
625 let initial_level = model.consciousness_level;
626 assert!(model.evolve_consciousness().is_ok());
627
628 assert_ne!(model.consciousness_level, initial_level);
630 }
631
632 #[tokio::test]
633 async fn test_consciousness_insights() {
634 let config = ModelConfig::default().with_dimensions(16);
635 let mut model = ConsciousnessAwareEmbedding::new(config);
636
637 let context = vec!["context1".to_string()];
639 let _ = model.generate_conscious_embedding("entity1", &context);
640 let _ = model.generate_conscious_embedding("entity2", &context);
641
642 let insights = model.get_consciousness_insights();
643
644 assert_eq!(insights.consciousness_level, ConsciousnessLevel::SelfAware);
645 assert!(insights.total_experiences > 0);
646 assert!(!insights.active_concepts.is_empty());
647 assert!(!insights.attention_distribution.is_empty());
648 }
649}