reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! ThinkTool Module Type Definitions
//!
//! This module provides the core type definitions and traits for ThinkTool modules,
//! which are structured reasoning components that implement specific analytical strategies.
//!
//! ## Architecture Note
//!
//! ThinkTool modules can be used in two ways:
//!
//! 1. **Direct Module Execution** - Use the module structs directly for local processing
//! 2. **Protocol Execution** - Use ProtocolExecutor with LLM integration
//!
//! ## Direct Module Usage
//!
//! ```rust,ignore
//! use reasonkit::thinktool::modules::{GigaThink, ThinkToolModule, ThinkToolContext};
//!
//! let module = GigaThink::new();
//! let context = ThinkToolContext {
//!     query: "What are the key factors for success?".to_string(),
//!     previous_steps: vec![],
//! };
//!
//! // Synchronous execution
//! let result = module.execute(&context)?;
//!
//! // Asynchronous execution (requires AsyncThinkToolModule)
//! let async_result = module.execute_async(&context).await?;
//! ```
//!
//! ## Protocol Execution (with LLM)
//!
//! ```rust,ignore
//! let executor = ProtocolExecutor::new()?;
//! let result = executor.execute("gigathink", ProtocolInput::query("question")).await?;
//! ```
//!
//! ## Available Modules
//!
//! | Module | Code | Purpose | Key Feature |
//! |--------|------|---------|-------------|
//! | `GigaThink` | `gt` | Expansive creative thinking | 10+ perspectives |
//! | `LaserLogic` | `ll` | Precision deductive reasoning | Fallacy detection |
//! | `BedRock` | `br` | First principles decomposition | Core axiom extraction |
//! | `ProofGuard` | `pg` | Multi-source verification | 3+ sources required |
//! | `BrutalHonesty` | `bh` | Adversarial self-critique | Skeptical scoring |
//! | `BrutalHonestyEnhanced` | `bhe` | Deep adversarial critique | Cognitive bias detection |
//! | `ESCoT` | `es` | Example Selection Chain-of-Thought | Embedding-based few-shot |
//! | `GraphOfThought` | `got` | DAG-based reasoning | Branching & aggregation |
//!
//! See `registry.rs` for full protocol definitions.

use crate::error::Result;
use serde::{Deserialize, Serialize};

// ============================================================================
// MODULE RE-EXPORTS
// ============================================================================

pub mod bedrock;
pub mod brutalhonesty;
pub mod brutalhonesty_enhanced;
pub mod escot;
pub mod gigathink;
#[cfg(feature = "got")]
pub mod graphofthought;
pub mod image_generation;
pub mod laserlogic;
pub mod proofguard;

// Re-export module structs
pub use bedrock::BedRock;
pub use brutalhonesty::BrutalHonesty;
pub use brutalhonesty_enhanced::BrutalHonestyEnhanced;
pub use escot::ESCoT;
pub use gigathink::GigaThink;
#[cfg(feature = "got")]
pub use graphofthought::GraphOfThought;
pub use laserlogic::LaserLogic;
pub use proofguard::ProofGuard;

// Re-export GigaThink types for comprehensive access
pub use gigathink::{
    AnalysisDimension, AsyncThinkToolModule, GigaThinkBuilder, GigaThinkConfig, GigaThinkError,
    GigaThinkMetadata, GigaThinkResult, Perspective, SynthesizedInsight, Theme,
};

// Re-export LaserLogic types for comprehensive access
pub use laserlogic::{
    Argument, ArgumentForm, Contradiction, ContradictionType, DetectedFallacy, Fallacy,
    LaserLogicConfig, LaserLogicResult, Premise, PremiseType, SoundnessStatus, ValidityStatus,
};

// Re-export BrutalHonesty types for comprehensive access
pub use brutalhonesty::{
    BrutalHonestyBuilder, BrutalHonestyConfig, CritiqueSeverity, CritiqueVerdict, DetectedFlaw,
    FlawCategory, FlawSeverity, IdentifiedStrength, ImplicitAssumption,
};

// Re-export BrutalHonestyEnhanced types
pub use brutalhonesty_enhanced::{
    ArgumentMap, BiasCategory, CognitiveBias, CognitiveBiasDepth, CulturalAssumption,
    EnhancedBuilder, EnhancedConfig, SteelmanArgument,
};

// Re-export ES-CoT types for comprehensive access
pub use escot::{ESCoTBuilder, ESCoTConfig, ESCoTResult, Example, ExampleDatabase, SimilarExample};

// Re-export GraphOfThought types for comprehensive access
#[cfg(feature = "got")]
pub use graphofthought::{
    ExecutedOperation, GoTBuilder, GoTConfig, GoTMetadata, GoTResult, GraphOfThoughtError, GraphOp,
    ReasoningGraphData, ThoughtMetadata, ThoughtNode,
};

// ============================================================================
// CORE TYPE DEFINITIONS
// ============================================================================

/// Configuration for a ThinkTool module
///
/// Defines the metadata and behavior parameters for a reasoning module.
#[derive(Debug, Clone, Serialize, Deserialize)]

pub struct ThinkToolModuleConfig {
    /// Module name (e.g., "GigaThink", "LaserLogic")
    pub name: String,

    /// Semantic version (e.g., "2.1.0")
    pub version: String,

    /// Human-readable description of module purpose
    pub description: String,

    /// Weight applied to this module's confidence in composite calculations
    /// Range: 0.0 - 1.0, typical: 0.10 - 0.30
    pub confidence_weight: f64,
}

impl ThinkToolModuleConfig {
    /// Create a new module configuration
    pub fn new(
        name: impl Into<String>,
        version: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            description: description.into(),
            confidence_weight: 0.20, // Default weight
        }
    }

    /// Builder: set confidence weight
    pub fn with_confidence_weight(mut self, weight: f64) -> Self {
        self.confidence_weight = weight.clamp(0.0, 1.0);
        self
    }
}

/// Context provided to a ThinkTool module for execution
///
/// Contains the query to analyze and any context from previous reasoning steps.
#[derive(Debug, Clone, Serialize, Deserialize)]

pub struct ThinkToolContext {
    /// The primary query or problem to analyze
    pub query: String,

    /// Results from previous reasoning steps (for chained execution)
    pub previous_steps: Vec<String>,
}

impl ThinkToolContext {
    /// Create a new context with just a query
    pub fn new(query: impl Into<String>) -> Self {
        Self {
            query: query.into(),
            previous_steps: Vec::new(),
        }
    }

    /// Create a context with previous step results
    pub fn with_previous_steps(query: impl Into<String>, steps: Vec<String>) -> Self {
        Self {
            query: query.into(),
            previous_steps: steps,
        }
    }

    /// Add a previous step result
    pub fn add_previous_step(&mut self, step: impl Into<String>) {
        self.previous_steps.push(step.into());
    }

    /// Check if this context has previous steps
    pub fn has_previous_steps(&self) -> bool {
        !self.previous_steps.is_empty()
    }

    /// Get the number of previous steps
    pub fn previous_step_count(&self) -> usize {
        self.previous_steps.len()
    }
}

/// Output produced by a ThinkTool module
///
/// Contains the module identification, confidence score, and structured output data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThinkToolOutput {
    /// Name of the module that produced this output
    pub module: String,

    /// Confidence score for the output (0.0 - 1.0)
    pub confidence: f64,

    /// Structured output data (module-specific format)
    pub output: serde_json::Value,
}

impl ThinkToolOutput {
    /// Create a new output
    pub fn new(module: impl Into<String>, confidence: f64, output: serde_json::Value) -> Self {
        Self {
            module: module.into(),
            confidence: confidence.clamp(0.0, 1.0),
            output,
        }
    }

    /// Get a field from the output
    pub fn get(&self, field: &str) -> Option<&serde_json::Value> {
        self.output.get(field)
    }

    /// Get a string field from the output
    pub fn get_str(&self, field: &str) -> Option<&str> {
        self.output.get(field).and_then(|v| v.as_str())
    }

    /// Get an array field from the output
    pub fn get_array(&self, field: &str) -> Option<&Vec<serde_json::Value>> {
        self.output.get(field).and_then(|v| v.as_array())
    }

    /// Check if the output has high confidence (>= 0.80)
    pub fn is_high_confidence(&self) -> bool {
        self.confidence >= 0.80
    }

    /// Check if the output meets a confidence threshold
    pub fn meets_threshold(&self, threshold: f64) -> bool {
        self.confidence >= threshold
    }
}

// ============================================================================
// CORE TRAIT DEFINITIONS
// ============================================================================

/// Core trait for ThinkTool modules
///
/// All ThinkTool modules must implement this trait to provide
/// synchronous execution capability and configuration access.
///
/// For async execution, also implement `AsyncThinkToolModule`.
pub trait ThinkToolModule: Send + Sync {
    /// Get the module configuration
    fn config(&self) -> &ThinkToolModuleConfig;

    /// Execute the module synchronously
    ///
    /// # Arguments
    /// * `context` - The execution context containing query and previous steps
    ///
    /// # Returns
    /// * `Ok(ThinkToolOutput)` - Successful execution with output and confidence
    /// * `Err(Error)` - Execution failed with error details
    fn execute(&self, context: &ThinkToolContext) -> Result<ThinkToolOutput>;

    /// Get the module name (convenience method)
    fn name(&self) -> &str {
        &self.config().name
    }

    /// Get the module version (convenience method)
    fn version(&self) -> &str {
        &self.config().version
    }

    /// Get the module description (convenience method)
    fn description(&self) -> &str {
        &self.config().description
    }

    /// Get the confidence weight for this module
    fn confidence_weight(&self) -> f64 {
        self.config().confidence_weight
    }
}

// ============================================================================
// TESTS
// ============================================================================

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

    #[test]
    fn test_module_config_creation() {
        let config = ThinkToolModuleConfig::new("TestModule", "1.0.0", "A test module");
        assert_eq!(config.name, "TestModule");
        assert_eq!(config.version, "1.0.0");
        assert_eq!(config.confidence_weight, 0.20);
    }

    #[test]
    fn test_module_config_with_weight() {
        let config = ThinkToolModuleConfig::new("TestModule", "1.0.0", "A test module")
            .with_confidence_weight(0.35);
        assert_eq!(config.confidence_weight, 0.35);
    }

    #[test]
    fn test_context_creation() {
        let context = ThinkToolContext::new("Test query");
        assert_eq!(context.query, "Test query");
        assert!(context.previous_steps.is_empty());
    }

    #[test]
    fn test_context_with_previous_steps() {
        let context =
            ThinkToolContext::with_previous_steps("Query", vec!["Step 1".into(), "Step 2".into()]);
        assert!(context.has_previous_steps());
        assert_eq!(context.previous_step_count(), 2);
    }

    #[test]
    fn test_output_creation() {
        let output =
            ThinkToolOutput::new("TestModule", 0.85, serde_json::json!({"result": "success"}));
        assert_eq!(output.module, "TestModule");
        assert_eq!(output.confidence, 0.85);
        assert!(output.is_high_confidence());
    }

    #[test]
    fn test_output_threshold() {
        let output =
            ThinkToolOutput::new("TestModule", 0.75, serde_json::json!({"result": "success"}));
        assert!(output.meets_threshold(0.70));
        assert!(!output.meets_threshold(0.80));
    }

    #[test]
    fn test_output_field_access() {
        let output = ThinkToolOutput::new(
            "TestModule",
            0.85,
            serde_json::json!({
                "name": "test",
                "values": [1, 2, 3]
            }),
        );

        assert_eq!(output.get_str("name"), Some("test"));
        assert!(output.get_array("values").is_some());
    }

    #[test]
    fn test_gigathink_module() {
        let module = GigaThink::new();
        assert_eq!(module.name(), "GigaThink");
        assert_eq!(module.version(), "2.1.0");
    }

    #[test]
    fn test_gigathink_execution() {
        let module = GigaThink::new();
        let context = ThinkToolContext::new("What are the implications of AI adoption?");

        let result = module.execute(&context).unwrap();
        assert_eq!(result.module, "GigaThink");
        assert!(result.confidence > 0.0);

        // Verify 10+ perspectives
        let perspectives = result.get_array("perspectives").unwrap();
        assert!(perspectives.len() >= 10);
    }

    #[test]
    fn test_laserlogic_module() {
        let module = LaserLogic::new();
        assert_eq!(module.name(), "LaserLogic");
        assert_eq!(module.version(), "3.0.0");
    }

    #[test]
    fn test_laserlogic_analyze_argument() {
        let module = LaserLogic::new();
        let result = module
            .analyze_argument(
                &["All humans are mortal", "Socrates is human"],
                "Socrates is mortal",
            )
            .unwrap();

        // Should detect categorical syllogism
        assert_eq!(
            result.argument_form,
            Some(ArgumentForm::CategoricalSyllogism)
        );
        assert!(result.confidence > 0.0);
    }

    #[test]
    fn test_laserlogic_fallacy_detection() {
        let module = LaserLogic::new();
        let result = module
            .analyze_argument(
                &["If it rains, then the ground is wet", "The ground is wet"],
                "It rained",
            )
            .unwrap();

        // Should detect affirming the consequent
        assert!(result.has_fallacies());
        assert!(result
            .fallacies
            .iter()
            .any(|f| f.fallacy == Fallacy::AffirmingConsequent));
    }

    #[test]
    fn test_brutalhonesty_module() {
        let module = BrutalHonesty::new();
        assert_eq!(module.name(), "BrutalHonesty");
        assert_eq!(module.version(), "3.0.0");
    }

    #[test]
    fn test_brutalhonesty_execution() {
        let module = BrutalHonesty::new();
        let context =
            ThinkToolContext::new("Our startup will succeed because we have the best team");

        let result = module.execute(&context).unwrap();
        assert_eq!(result.module, "BrutalHonesty");
        assert!(result.confidence > 0.0);
        assert!(result.confidence <= 0.95);

        // Verify output contains required fields
        assert!(result.get("verdict").is_some());
        assert!(result.get("analysis").is_some());
        assert!(result.get("devils_advocate").is_some());
    }

    #[test]
    fn test_brutalhonesty_enhanced_module() {
        let module = BrutalHonestyEnhanced::new();
        assert_eq!(module.name(), "BrutalHonestyEnhanced");
        assert_eq!(module.version(), "3.0.0");
    }

    #[test]
    fn test_brutalhonesty_enhanced_execution() {
        let module = BrutalHonestyEnhanced::new();
        let context = ThinkToolContext::new(
            "We're certain this will succeed because everyone agrees it's the best approach.",
        );

        let result = module.execute(&context).unwrap();
        assert_eq!(result.module, "BrutalHonestyEnhanced");
        assert!(result.confidence > 0.0);
        assert!(result.confidence <= 0.90);

        // Verify output contains enhanced analysis
        assert!(result.get("enhanced_analysis").is_some());
        assert!(result.get("base_analysis").is_some());
    }

    #[test]
    fn test_brutalhonesty_builder() {
        let module = BrutalHonesty::builder()
            .severity(CritiqueSeverity::Ruthless)
            .enable_devil_advocate(true)
            .build();

        assert_eq!(module.brutal_config().severity, CritiqueSeverity::Ruthless);
        assert!(module.brutal_config().enable_devil_advocate);
    }

    #[test]
    fn test_brutalhonesty_enhanced_builder() {
        let module = BrutalHonestyEnhanced::builder()
            .severity(CritiqueSeverity::Harsh)
            .cognitive_bias_depth(CognitiveBiasDepth::Deep)
            .enable_cultural_analysis(true)
            .build();

        assert_eq!(
            module.enhanced_config().base_config.severity,
            CritiqueSeverity::Harsh
        );
        assert_eq!(
            module.enhanced_config().cognitive_bias_depth,
            CognitiveBiasDepth::Deep
        );
    }
}