-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrator.rs
More file actions
111 lines (98 loc) · 3.64 KB
/
Copy pathorchestrator.rs
File metadata and controls
111 lines (98 loc) · 3.64 KB
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
// SPDX-License-Identifier: MPL-2.0
//! Orchestrator — Mobile AI Coordination Layer.
//!
//! This module implements the central pipeline for on-device AI decision
//! making. It orchestrates the interaction between symbolic safety rules,
//! neural routing models, and local/remote inference engines.
//!
//! CORE PIPELINE:
//! 1. **Evaluation**: The Expert System audits the query for safety
//! and policy compliance.
//! 2. **Routing**: An MLP-based model decides if the query should be
//! handled locally (SLM) or offloaded to a remote API (LLM).
//! 3. **Execution**: The chosen inference engine produces a response.
//! 4. **Persistence**: The turn is recorded in the Context Manager for
//! long-term memory.
use crate::{
context::ContextManager,
expert::ExpertSystem,
router::{Router, RouterConfig},
types::{ConversationTurn, Query, Response, ResponseMetadata, RoutingDecision},
};
/// Orchestrator: Coordinates the full AI pipeline.
pub struct Orchestrator {
router: Router,
expert: ExpertSystem,
context: ContextManager,
}
impl Orchestrator {
/// Create a new orchestrator with default configuration.
pub fn new() -> Self {
Self {
router: Router::new(RouterConfig::default()),
expert: ExpertSystem::new(),
context: ContextManager::new(),
}
}
/// PROCESS: Executes the full coordination pipeline for a single query.
///
/// HYBRID STRATEGY:
/// - `Local`: Low-latency, privacy-preserving on-device inference.
/// - `Remote`: High-capability cloud-based reasoning (feature-gated).
/// - `Hybrid`: Local preprocessing (e.g. summarization) followed by remote query.
pub fn process(&mut self, query: Query) -> Result<Response, String> {
// Step 1: Expert system evaluation
let eval = self.expert.evaluate(&query);
if !eval.allowed {
return Ok(Response {
text: "Request blocked by safety rules".to_string(),
route: RoutingDecision::Blocked,
confidence: 1.0,
latency_ms: 0,
metadata: ResponseMetadata {
model: Some("expert-system".to_string()),
tokens: None,
cached: false,
},
});
}
// Step 2: Routing decision
let (route, confidence) = self.router.route(&query);
// Step 3: Generate response (Phase 1: placeholder)
let response = Response {
text: format!("Response to: {}", query.text),
route,
confidence,
latency_ms: 10,
metadata: ResponseMetadata {
model: Some("orchestrator-phase1".to_string()),
tokens: Some(50),
cached: false,
},
};
// Step 4: Update context
self.context.add_turn(query, response.clone());
Ok(response)
}
/// Set the active project on the underlying ContextManager.
pub fn switch_project(&mut self, project: impl Into<String>) {
self.context.switch_project(project);
}
/// Borrow the active project name, if one is set.
pub fn current_project(&self) -> Option<&str> {
self.context.current_project()
}
/// Drop the active project's conversation history.
pub fn clear_history(&mut self) {
self.context.clear_history();
}
/// Borrow the N most recent turns from the active project's history.
pub fn recent_history(&self, n: usize) -> Vec<ConversationTurn> {
self.context.recent_history(n)
}
}
impl Default for Orchestrator {
fn default() -> Self {
Self::new()
}
}