kaccy-ai 0.2.0

AI-powered intelligence for Kaccy Protocol - forecasting, optimization, and insights
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
//! AI-powered quality evaluation using LLM providers
//!
//! This module uses OpenAI/Anthropic to evaluate:
//! - Code quality and correctness
//! - Content quality and clarity
//! - Commitment evidence verification
//!
//! # Examples
//!
//! ```no_run
//! use kaccy_ai::AiEvaluator;
//! use kaccy_ai::llm::{LlmClient, OpenAiClient};
//! use kaccy_ai::evaluator::QualityEvaluator;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create an LLM client
//! let openai = OpenAiClient::with_default_model("your-api-key");
//! let llm_client = LlmClient::new(Box::new(openai));
//!
//! // Create an AI evaluator
//! let evaluator = AiEvaluator::new(llm_client);
//!
//! // Evaluate code quality
//! let code = "fn hello() { println!(\"Hello\"); }";
//! let result = evaluator.evaluate_code(code, "rust").await?;
//! println!("Quality: {}", result.quality_score);
//! # Ok(())
//! # }
//! ```

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use crate::error::{AiError, Result};
use crate::evaluator::{EvaluationResult, QualityEvaluator};
use crate::llm::{ChatRequest, LlmClient};

/// AI-powered quality evaluator
pub struct AiEvaluator {
    llm: LlmClient,
    config: EvaluatorConfig,
}

/// Configuration for the AI evaluator
#[derive(Debug, Clone)]
pub struct EvaluatorConfig {
    /// Maximum tokens for evaluation response
    pub max_response_tokens: u32,
    /// Temperature for evaluation (lower = more consistent)
    pub temperature: f32,
    /// Whether to include detailed feedback
    pub detailed_feedback: bool,
}

impl Default for EvaluatorConfig {
    fn default() -> Self {
        Self {
            max_response_tokens: 1024,
            temperature: 0.3, // Low temperature for consistent evaluations
            detailed_feedback: true,
        }
    }
}

impl AiEvaluator {
    /// Create a new AI evaluator with the given LLM client
    #[must_use]
    pub fn new(llm: LlmClient) -> Self {
        Self {
            llm,
            config: EvaluatorConfig::default(),
        }
    }

    /// Create with custom configuration
    #[must_use]
    pub fn with_config(llm: LlmClient, config: EvaluatorConfig) -> Self {
        Self { llm, config }
    }

    /// Build the code evaluation prompt
    fn build_code_prompt(code: &str, language: &str) -> String {
        format!(
            r#"You are an expert code reviewer evaluating a piece of {language} code.

Evaluate the following code and provide scores from 0-100 for each criterion:

1. **Quality Score**: Code correctness, best practices, proper error handling
2. **Complexity Score**: Appropriate complexity (not over-engineered, not too simple)
3. **Originality Score**: Creative solutions, good design patterns

Also provide brief feedback (2-3 sentences) on the code.

Respond in JSON format:
{{
    "quality_score": <number>,
    "complexity_score": <number>,
    "originality_score": <number>,
    "feedback": "<string>"
}}

Code to evaluate:
```{language}
{code}
```"#
        )
    }

    /// Build the content evaluation prompt
    fn build_content_prompt(content: &str, content_type: &str) -> String {
        format!(
            r#"You are an expert content evaluator assessing a piece of {content_type} content.

Evaluate the following content and provide scores from 0-100 for each criterion:

1. **Quality Score**: Clarity, accuracy, professionalism
2. **Complexity Score**: Appropriate depth for the audience
3. **Originality Score**: Unique insights, creative presentation

Also provide brief feedback (2-3 sentences) on the content.

Respond in JSON format:
{{
    "quality_score": <number>,
    "complexity_score": <number>,
    "originality_score": <number>,
    "feedback": "<string>"
}}

Content to evaluate:
---
{content}
---"#
        )
    }

    /// Parse the evaluation response
    ///
    /// # Errors
    ///
    /// Returns an error if the response cannot be parsed as valid JSON or does not match the expected format.
    fn parse_evaluation(response: &str) -> Result<EvaluationResult> {
        // Try to extract JSON from the response
        let json_str = if let Some(start) = response.find('{') {
            if let Some(end) = response.rfind('}') {
                &response[start..=end]
            } else {
                response
            }
        } else {
            response
        };

        let parsed: EvalResponse = serde_json::from_str(json_str).map_err(|e| {
            AiError::EvaluationFailed(format!("Failed to parse evaluation response: {e}"))
        })?;

        let overall =
            (parsed.quality_score + parsed.complexity_score + parsed.originality_score) / 3.0;

        Ok(EvaluationResult {
            quality_score: parsed.quality_score,
            complexity_score: parsed.complexity_score,
            originality_score: parsed.originality_score,
            overall_score: overall,
            feedback: parsed.feedback,
        })
    }
}

#[derive(Debug, Deserialize)]
struct EvalResponse {
    quality_score: f64,
    complexity_score: f64,
    originality_score: f64,
    feedback: String,
}

#[async_trait]
impl QualityEvaluator for AiEvaluator {
    async fn evaluate_code(&self, code: &str, language: &str) -> Result<EvaluationResult> {
        // Check code length
        if code.len() > 50000 {
            return Err(AiError::Validation(
                "Code too long for evaluation (max 50KB)".to_string(),
            ));
        }

        let prompt = Self::build_code_prompt(code, language);

        let request = ChatRequest::with_system(
            "You are an expert code reviewer. Always respond with valid JSON.",
            prompt,
        )
        .max_tokens(self.config.max_response_tokens)
        .temperature(self.config.temperature);

        let response = self.llm.chat(request).await?;

        Self::parse_evaluation(&response.message.content)
    }

    async fn evaluate_content(
        &self,
        content: &str,
        content_type: &str,
    ) -> Result<EvaluationResult> {
        // Check content length
        if content.len() > 100_000 {
            return Err(AiError::Validation(
                "Content too long for evaluation (max 100KB)".to_string(),
            ));
        }

        let prompt = Self::build_content_prompt(content, content_type);

        let request = ChatRequest::with_system(
            "You are an expert content evaluator. Always respond with valid JSON.",
            prompt,
        )
        .max_tokens(self.config.max_response_tokens)
        .temperature(self.config.temperature);

        let response = self.llm.chat(request).await?;

        Self::parse_evaluation(&response.message.content)
    }
}

/// Commitment evidence verifier using AI
pub struct AiCommitmentVerifier {
    llm: LlmClient,
}

impl AiCommitmentVerifier {
    /// Create a new `AiCommitmentVerifier` backed by the given LLM client.
    #[must_use]
    pub fn new(llm: LlmClient) -> Self {
        Self { llm }
    }

    /// Verify commitment evidence
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The LLM request fails
    /// - The response cannot be parsed as valid JSON
    /// - The response does not match the expected format
    pub async fn verify_evidence(
        &self,
        request: &VerificationRequest,
    ) -> Result<VerificationResult> {
        let prompt = Self::build_verification_prompt(request);

        let chat_request = ChatRequest::with_system(
            "You are an expert at verifying commitment fulfillment. Be fair but thorough. Always respond with valid JSON.",
            prompt,
        )
        .max_tokens(1024)
        .temperature(0.2);

        let response = self.llm.chat(chat_request).await?;

        Self::parse_verification(&response.message.content)
    }

    fn build_verification_prompt(request: &VerificationRequest) -> String {
        format!(
            r#"Verify if the following commitment has been fulfilled based on the evidence provided.

**Commitment Title:** {}
**Commitment Description:** {}
**Deadline:** {}
**Evidence URL:** {}
**Evidence Description:** {}

Evaluate the evidence and determine:
1. Is the evidence valid and accessible?
2. Does the evidence match the commitment requirements?
3. Was it completed on time?
4. What is your confidence level (0-100)?

Respond in JSON format:
{{
    "fulfilled": <boolean>,
    "confidence": <number 0-100>,
    "reasoning": "<string explaining your decision>",
    "suggestions": "<string with any suggestions for improvement, or null>"
}}"#,
            request.commitment_title,
            request
                .commitment_description
                .as_deref()
                .unwrap_or("Not provided"),
            request.deadline,
            request.evidence_url,
            request
                .evidence_description
                .as_deref()
                .unwrap_or("Not provided")
        )
    }

    /// Parse the verification response
    ///
    /// # Errors
    ///
    /// Returns an error if the response cannot be parsed as valid JSON or does not match the expected format.
    fn parse_verification(response: &str) -> Result<VerificationResult> {
        let json_str = if let Some(start) = response.find('{') {
            if let Some(end) = response.rfind('}') {
                &response[start..=end]
            } else {
                response
            }
        } else {
            response
        };

        let parsed: VerifyResponse = serde_json::from_str(json_str).map_err(|e| {
            AiError::VerificationFailed(format!("Failed to parse verification response: {e}"))
        })?;

        Ok(VerificationResult {
            fulfilled: parsed.fulfilled,
            confidence: parsed.confidence,
            reasoning: parsed.reasoning,
            suggestions: parsed.suggestions,
            needs_human_review: parsed.confidence < 70.0,
        })
    }
}

/// Request for commitment verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationRequest {
    /// Title of the commitment being verified.
    pub commitment_title: String,
    /// Optional longer description of the commitment.
    pub commitment_description: Option<String>,
    /// ISO 8601 deadline for the commitment.
    pub deadline: String,
    /// URL to the submitted evidence.
    pub evidence_url: String,
    /// Optional human-readable description of the evidence.
    pub evidence_description: Option<String>,
}

/// Result of commitment verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationResult {
    /// Whether the commitment was fulfilled
    pub fulfilled: bool,
    /// AI confidence in the decision (0-100)
    pub confidence: f64,
    /// Reasoning for the decision
    pub reasoning: String,
    /// Suggestions for improvement
    pub suggestions: Option<String>,
    /// Whether human review is recommended
    pub needs_human_review: bool,
}

#[derive(Debug, Deserialize)]
struct VerifyResponse {
    fulfilled: bool,
    confidence: f64,
    reasoning: String,
    suggestions: Option<String>,
}

/// Fraud detection using AI
pub struct AiFraudDetector {
    llm: LlmClient,
}

impl AiFraudDetector {
    /// Create a new `AiFraudDetector` backed by the given LLM client.
    #[must_use]
    pub fn new(llm: LlmClient) -> Self {
        Self { llm }
    }

    /// Check content for potential fraud indicators
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The LLM request fails
    /// - The response cannot be parsed as valid JSON
    /// - The response does not match the expected format
    pub async fn check_fraud(&self, request: &FraudCheckRequest) -> Result<FraudCheckResult> {
        let prompt = Self::build_fraud_prompt(request);

        let chat_request = ChatRequest::with_system(
            "You are a fraud detection specialist. Analyze content for signs of manipulation, gaming, or fraudulent activity. Be thorough but fair. Always respond with valid JSON.",
            prompt,
        )
        .max_tokens(1024)
        .temperature(0.2);

        let response = self.llm.chat(chat_request).await?;

        Self::parse_fraud_check(&response.message.content)
    }

    fn build_fraud_prompt(request: &FraudCheckRequest) -> String {
        format!(
            r#"Analyze the following for potential fraud or gaming indicators:

**Content Type:** {}
**Content:**
{}

**User History:**
- Commitments Made: {}
- Commitments Fulfilled: {}
- Average Quality Score: {}

Check for:
1. Plagiarized or generated content
2. Self-dealing or fake evidence
3. Pattern manipulation
4. Suspicious timing or velocity

Respond in JSON format:
{{
    "risk_level": "<low|medium|high|critical>",
    "risk_score": <number 0-100>,
    "indicators": ["<list of suspicious indicators found>"],
    "recommendation": "<string with recommended action>"
}}"#,
            request.content_type,
            request.content,
            request.commitments_made,
            request.commitments_fulfilled,
            request.avg_quality_score.unwrap_or(0.0)
        )
    }

    /// Parse the fraud check response
    ///
    /// # Errors
    ///
    /// Returns an error if the response cannot be parsed as valid JSON or does not match the expected format.
    fn parse_fraud_check(response: &str) -> Result<FraudCheckResult> {
        let json_str = if let Some(start) = response.find('{') {
            if let Some(end) = response.rfind('}') {
                &response[start..=end]
            } else {
                response
            }
        } else {
            response
        };

        let parsed: FraudResponse = serde_json::from_str(json_str).map_err(|e| {
            AiError::EvaluationFailed(format!("Failed to parse fraud check response: {e}"))
        })?;

        Ok(FraudCheckResult {
            risk_level: parsed.risk_level,
            risk_score: parsed.risk_score,
            indicators: parsed.indicators,
            recommendation: parsed.recommendation,
        })
    }
}

/// Request for fraud check
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FraudCheckRequest {
    /// Type of content being checked (e.g., `"code"`, `"text"`).
    pub content_type: String,
    /// The actual content to analyse.
    pub content: String,
    /// Total number of commitments the user has made.
    pub commitments_made: i32,
    /// Number of commitments the user has fulfilled.
    pub commitments_fulfilled: i32,
    /// User's average quality score if available.
    pub avg_quality_score: Option<f64>,
}

/// Result of fraud check
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FraudCheckResult {
    /// Risk level classification (`"low"`, `"medium"`, `"high"`, `"critical"`).
    pub risk_level: String,
    /// Numeric risk score (0–100).
    pub risk_score: f64,
    /// List of specific fraud indicators detected.
    pub indicators: Vec<String>,
    /// Recommended action to take.
    pub recommendation: String,
}

#[derive(Debug, Deserialize)]
struct FraudResponse {
    risk_level: String,
    risk_score: f64,
    indicators: Vec<String>,
    recommendation: String,
}