zoey-core 0.1.1

ZoeyAI core runtime and types — privacy-first AI agent framework optimized for local models
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
//! Complexity assessment for task planning

use crate::types::*;
use crate::Result;
use serde::{Deserialize, Serialize};

/// Complexity level for tasks
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ComplexityLevel {
    /// Trivial task (greeting, simple acknowledgment)
    Trivial,
    /// Simple task (basic Q&A, single-step)
    Simple,
    /// Moderate task (multi-step reasoning)
    Moderate,
    /// Complex task (requires research/multiple sources)
    Complex,
    /// Very complex task (multi-agent coordination needed)
    VeryComplex,
}

impl std::fmt::Display for ComplexityLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ComplexityLevel::Trivial => write!(f, "TRIVIAL"),
            ComplexityLevel::Simple => write!(f, "SIMPLE"),
            ComplexityLevel::Moderate => write!(f, "MODERATE"),
            ComplexityLevel::Complex => write!(f, "COMPLEX"),
            ComplexityLevel::VeryComplex => write!(f, "VERY_COMPLEX"),
        }
    }
}

/// Token estimate for a task
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenEstimate {
    /// Estimated input tokens
    pub input_tokens: usize,
    /// Estimated output tokens
    pub output_tokens: usize,
    /// Total estimated tokens
    pub total_tokens: usize,
    /// Confidence in estimate (0.0 - 1.0)
    pub confidence: f32,
}

/// Complexity assessment result
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ComplexityAssessment {
    /// Assessed complexity level
    pub level: ComplexityLevel,
    /// Reasoning for the assessment
    pub reasoning: String,
    /// Estimated steps needed
    pub estimated_steps: usize,
    /// Token estimate
    pub estimated_tokens: TokenEstimate,
    /// Confidence score (0.0 - 1.0)
    pub confidence: f32,
    /// Individual factor scores
    pub factors: ComplexityFactors,
}

/// Individual complexity factors
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ComplexityFactors {
    /// Length complexity (0.0 - 1.0)
    pub length_score: f32,
    /// Question complexity (0.0 - 1.0)
    pub question_score: f32,
    /// Domain complexity (0.0 - 1.0)
    pub domain_score: f32,
    /// Context requirement (0.0 - 1.0)
    pub context_score: f32,
    /// Reasoning depth (0.0 - 1.0)
    pub reasoning_score: f32,
}

impl ComplexityFactors {
    /// Calculate average complexity across all factors
    pub fn average(&self) -> f32 {
        (self.length_score
            + self.question_score
            + self.domain_score
            + self.context_score
            + self.reasoning_score)
            / 5.0
    }
}

/// Complexity analyzer
pub struct ComplexityAnalyzer;

impl ComplexityAnalyzer {
    /// Create a new complexity analyzer
    pub fn new() -> Self {
        Self
    }

    /// Assess complexity of a message
    pub async fn assess(&self, message: &Memory, state: &State) -> Result<ComplexityAssessment> {
        let text = &message.content.text;

        // Analyze individual factors
        let factors = ComplexityFactors {
            length_score: self.analyze_length(text),
            question_score: self.analyze_questions(text),
            domain_score: self.analyze_domain(text),
            context_score: self.analyze_context_needed(text, state),
            reasoning_score: self.analyze_reasoning_depth(text),
        };

        // Determine overall complexity level
        let level = self.determine_level(&factors);

        // Estimate steps and tokens
        let estimated_steps = self.estimate_steps(&level, &factors);
        let estimated_tokens = self.estimate_tokens(&level, &factors, text);

        // Calculate confidence
        let confidence = self.calculate_confidence(&factors);

        // Build reasoning explanation
        let reasoning = self.build_reasoning(&factors, &level);

        Ok(ComplexityAssessment {
            level,
            reasoning,
            estimated_steps,
            estimated_tokens,
            confidence,
            factors,
        })
    }

    /// Analyze length complexity
    fn analyze_length(&self, text: &str) -> f32 {
        let words = text.split_whitespace().count();

        // Score based on word count
        match words {
            0..=5 => 0.1,     // Very short
            6..=15 => 0.2,    // Short
            16..=50 => 0.4,   // Medium
            51..=150 => 0.6,  // Long
            151..=300 => 0.8, // Very long
            _ => 1.0,         // Extremely long
        }
    }

    /// Analyze question complexity
    fn analyze_questions(&self, text: &str) -> f32 {
        let lower = text.to_lowercase();
        let mut score = 0.0;

        // Count questions
        let question_marks = text.matches('?').count();
        score += (question_marks as f32 * 0.2).min(0.4);

        // Complex question patterns
        let complex_patterns = [
            "how do i",
            "how can i",
            "how would",
            "why does",
            "why is",
            "why would",
            "what's the difference",
            "what is the best way",
            "can you explain",
            "could you help me understand",
            "multiple",
            "several",
            "various",
        ];

        for pattern in &complex_patterns {
            if lower.contains(pattern) {
                score += 0.15;
            }
        }

        // Multi-part questions
        if lower.contains(" and ") || lower.contains(" or ") {
            score += 0.2;
        }

        score.min(1.0)
    }

    /// Analyze domain complexity
    fn analyze_domain(&self, text: &str) -> f32 {
        let lower = text.to_lowercase();
        let mut score: f32 = 0.0;

        // Technical domains (higher complexity)
        let technical_keywords = [
            "algorithm",
            "implement",
            "code",
            "function",
            "system",
            "architecture",
            "database",
            "optimization",
            "performance",
            "security",
            "encryption",
            "network",
            "protocol",
            "api",
            "machine learning",
            "neural network",
            "blockchain",
            "distributed",
            "concurrent",
            "async",
            "runtime",
        ];

        let mut hits = 0;
        for keyword in &technical_keywords {
            if lower.contains(keyword) {
                hits += 1;
            }
        }

        // Academic/research terms
        let academic_keywords = ["research", "study", "analysis", "theory", "hypothesis"];
        // Scale score based on hits (ensures complex technical prompts exceed 0.5)
        score = 0.2 + 0.15 * hits as f32;
        if lower.contains("consensus") {
            score += 0.15;
        }
        if lower.contains("raft") {
            score += 0.15;
        }
        if lower.contains("leader election") {
            score += 0.1;
        }
        if lower.contains("log replication") {
            score += 0.1;
        }

        score.min(1.0)
    }

    /// Analyze context requirements
    fn analyze_context_needed(&self, text: &str, state: &State) -> f32 {
        let lower = text.to_lowercase();
        let mut score: f32 = 0.0;

        // References to previous context
        let context_patterns = [
            "previous",
            "earlier",
            "before",
            "last time",
            "you said",
            "you mentioned",
            "as discussed",
            "continue",
            "following up",
            "regarding",
        ];

        for pattern in &context_patterns {
            if lower.contains(pattern) {
                score += 0.2;
            }
        }

        // Pronouns indicating context dependency
        let pronouns = ["it", "this", "that", "these", "those", "they"];
        for pronoun in &pronouns {
            if lower.contains(&format!(" {} ", pronoun)) {
                score += 0.1;
            }
        }

        // Check if state has recent messages (indicates ongoing conversation)
        if let Some(recent_messages) = state.data.get("recentMessages") {
            if let Some(arr) = recent_messages.as_array() {
                if arr.len() > 3 {
                    score += 0.2;
                }
            }
        }

        score.min(1.0)
    }

    /// Analyze reasoning depth required
    fn analyze_reasoning_depth(&self, text: &str) -> f32 {
        let lower = text.to_lowercase();
        let mut score: f32 = 0.2; // Base score

        // Multi-step reasoning indicators
        let reasoning_patterns = [
            "step by step",
            "first",
            "then",
            "finally",
            "process",
            "explain how",
            "explain why",
            "reasoning",
            "logic",
            "compare",
            "contrast",
            "analyze",
            "evaluate",
            "pros and cons",
            "advantages",
            "disadvantages",
            "consider",
            "think about",
            "take into account",
        ];

        for pattern in &reasoning_patterns {
            if lower.contains(pattern) {
                score += 0.15;
            }
        }

        // Causal reasoning
        if lower.contains("because") || lower.contains("therefore") || lower.contains("thus") {
            score += 0.1;
        }

        // Multiple conditions
        if lower.matches(" if ").count() > 1 {
            score += 0.2;
        }

        score.min(1.0)
    }

    /// Determine complexity level from factors
    fn determine_level(&self, factors: &ComplexityFactors) -> ComplexityLevel {
        let avg = factors.average();

        // Also consider individual high scores
        let max_score = factors
            .length_score
            .max(factors.question_score)
            .max(factors.domain_score)
            .max(factors.context_score)
            .max(factors.reasoning_score);

        // Weighted decision
        let weighted = avg * 0.7 + max_score * 0.3;

        match weighted {
            x if x < 0.2 => ComplexityLevel::Trivial,
            x if x < 0.4 => ComplexityLevel::Simple,
            x if x < 0.6 => ComplexityLevel::Moderate,
            x if x < 0.8 => ComplexityLevel::Complex,
            _ => ComplexityLevel::VeryComplex,
        }
    }

    /// Estimate steps needed
    fn estimate_steps(&self, level: &ComplexityLevel, factors: &ComplexityFactors) -> usize {
        let base_steps = match level {
            ComplexityLevel::Trivial => 1,
            ComplexityLevel::Simple => 2,
            ComplexityLevel::Moderate => 4,
            ComplexityLevel::Complex => 7,
            ComplexityLevel::VeryComplex => 12,
        };

        // Adjust based on reasoning depth
        let adjustment = (factors.reasoning_score * 3.0) as usize;

        base_steps + adjustment
    }

    /// Estimate tokens needed
    fn estimate_tokens(
        &self,
        level: &ComplexityLevel,
        factors: &ComplexityFactors,
        text: &str,
    ) -> TokenEstimate {
        // Input tokens: rough estimate (4 chars per token)
        let message_tokens = (text.len() / 4).max(1);

        // Base context tokens
        let context_tokens = (factors.context_score * 300.0) as usize;

        // System prompt tokens scale by complexity
        let system_tokens = match level {
            ComplexityLevel::Trivial => 50,
            ComplexityLevel::Simple => 100,
            ComplexityLevel::Moderate => 150,
            ComplexityLevel::Complex => 200,
            ComplexityLevel::VeryComplex => 300,
        };

        let input_tokens = message_tokens + context_tokens + system_tokens;

        // Output tokens based on complexity
        let base_output = match level {
            ComplexityLevel::Trivial => 50,
            ComplexityLevel::Simple => 150,
            ComplexityLevel::Moderate => 300,
            ComplexityLevel::Complex => 500,
            ComplexityLevel::VeryComplex => 1000,
        };

        // Adjust for domain complexity
        let domain_adjustment = (factors.domain_score * 200.0) as usize;
        let output_tokens = base_output + domain_adjustment;

        // Add 20% buffer
        let buffered_output = (output_tokens as f32 * 1.2) as usize;

        TokenEstimate {
            input_tokens,
            output_tokens: buffered_output,
            total_tokens: input_tokens + buffered_output,
            confidence: self.calculate_confidence(factors),
        }
    }

    /// Calculate confidence in assessment
    fn calculate_confidence(&self, factors: &ComplexityFactors) -> f32 {
        // Higher variance in factors = lower confidence
        let avg = factors.average();
        let variance = [
            (factors.length_score - avg).powi(2),
            (factors.question_score - avg).powi(2),
            (factors.domain_score - avg).powi(2),
            (factors.context_score - avg).powi(2),
            (factors.reasoning_score - avg).powi(2),
        ]
        .iter()
        .sum::<f32>()
            / 5.0;

        // Lower variance = higher confidence
        let confidence = 1.0 - variance.sqrt().min(0.5);

        confidence.max(0.5).min(0.95)
    }

    /// Build reasoning explanation
    fn build_reasoning(&self, factors: &ComplexityFactors, level: &ComplexityLevel) -> String {
        format!(
            "Complexity: {} | Factors: length={:.2}, questions={:.2}, domain={:.2}, context={:.2}, reasoning={:.2} | Average: {:.2}",
            level,
            factors.length_score,
            factors.question_score,
            factors.domain_score,
            factors.context_score,
            factors.reasoning_score,
            factors.average()
        )
    }
}

impl Default for ComplexityAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn create_test_message(text: &str) -> Memory {
        Memory {
            id: Uuid::new_v4(),
            entity_id: Uuid::new_v4(),
            agent_id: Uuid::new_v4(),
            room_id: Uuid::new_v4(),
            content: Content {
                text: text.to_string(),
                ..Default::default()
            },
            embedding: None,
            metadata: None,
            created_at: chrono::Utc::now().timestamp(),
            unique: None,
            similarity: None,
        }
    }

    #[tokio::test]
    async fn test_trivial_complexity() {
        let analyzer = ComplexityAnalyzer::new();
        let message = create_test_message("Hi");
        let state = State::new();

        let assessment = analyzer.assess(&message, &state).await.unwrap();
        assert!(matches!(
            assessment.level,
            ComplexityLevel::Trivial | ComplexityLevel::Simple
        ));
        assert!(assessment.estimated_tokens.total_tokens <= 300);
    }

    #[tokio::test]
    async fn test_simple_complexity() {
        let analyzer = ComplexityAnalyzer::new();
        let message = create_test_message("What's the weather like today?");
        let state = State::new();

        let assessment = analyzer.assess(&message, &state).await.unwrap();
        assert!(matches!(
            assessment.level,
            ComplexityLevel::Simple | ComplexityLevel::Trivial
        ));
    }

    #[tokio::test]
    async fn test_complex_technical() {
        let analyzer = ComplexityAnalyzer::new();
        let message = create_test_message(
            "Can you explain how to implement a distributed consensus algorithm \
             using Raft protocol, including the leader election process and log replication?",
        );
        let state = State::new();

        let assessment = analyzer.assess(&message, &state).await.unwrap();
        assert!(matches!(
            assessment.level,
            ComplexityLevel::Moderate | ComplexityLevel::Complex | ComplexityLevel::VeryComplex
        ));
        assert!(assessment.factors.domain_score > 0.5);
        assert!(assessment.factors.question_score > 0.3);
    }

    #[tokio::test]
    async fn test_token_estimation() {
        let analyzer = ComplexityAnalyzer::new();

        // Short message
        let short_msg = create_test_message("Hello");
        let state = State::new();
        let short_assessment = analyzer.assess(&short_msg, &state).await.unwrap();

        // Long message
        let long_msg = create_test_message(
            "This is a much longer message that contains many words and will require \
             more tokens to process and respond to appropriately.",
        );
        let long_assessment = analyzer.assess(&long_msg, &state).await.unwrap();

        assert!(
            long_assessment.estimated_tokens.total_tokens
                > short_assessment.estimated_tokens.total_tokens
        );
    }

    #[test]
    fn test_complexity_level_display() {
        assert_eq!(ComplexityLevel::Trivial.to_string(), "TRIVIAL");
        assert_eq!(ComplexityLevel::Complex.to_string(), "COMPLEX");
    }
}