|
1 | 1 | mod ai_config; |
2 | | -mod ai_manager; |
3 | 2 | mod parameters; |
| 3 | +mod tasks; |
4 | 4 | mod tools; |
5 | 5 | mod utils; |
6 | 6 |
|
7 | 7 | use anyhow::Result; |
| 8 | +use rmcp::{ |
| 9 | + handler::server::{ServerHandler, router::tool::ToolRouter}, |
| 10 | + model::{ServerCapabilities, ServerInfo}, |
| 11 | + tool_handler, |
| 12 | +}; |
| 13 | +use tokio::sync::mpsc::{Receiver, Sender}; |
8 | 14 |
|
9 | 15 | use ai_config::AiConfig; |
10 | | -use ai_manager::ChipmunkAI; |
| 16 | +use tasks::Task; |
11 | 17 |
|
12 | | -async fn start() -> Result<()> { |
13 | | - let config = AiConfig::init(); |
14 | | - let service = ChipmunkAI::new(config); |
15 | | - Ok(()) |
| 18 | +/// ChipmunkAI is responsible for receiving the prompt from the UI and the processing |
| 19 | +/// the prompt with the LLM agent. If agent invokes some tool then |
| 20 | +pub struct ChipmunkAI { |
| 21 | + pub config: AiConfig, |
| 22 | + |
| 23 | + // Channel for sending the Task to Chipmunk Core |
| 24 | + pub task_tx: Sender<Task>, |
| 25 | + |
| 26 | + //Channel for receiving the prompt from the User |
| 27 | + pub prompt_rx: Receiver<String>, |
| 28 | + |
| 29 | + // Channel for sending the UI updates to Chipmunk Core. |
| 30 | + // This channel will receive the updated from AI agent, |
| 31 | + // e.g. UI messages to display, thinking..., Searching for tools... |
| 32 | + pub ui_tx: Sender<String>, |
| 33 | + |
| 34 | + pub tool_router: ToolRouter<Self>, |
| 35 | +} |
| 36 | + |
| 37 | +#[tool_handler] |
| 38 | +impl ServerHandler for ChipmunkAI { |
| 39 | + fn get_info(&self) -> ServerInfo { |
| 40 | + ServerInfo { |
| 41 | + instructions: Some("Chipmunk AI".to_string()), |
| 42 | + capabilities: ServerCapabilities::builder() |
| 43 | + .enable_tools() |
| 44 | + .enable_prompts() |
| 45 | + .enable_resources() |
| 46 | + .build(), |
| 47 | + ..Default::default() |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// ServerCommunication is responsible to receiving the tasks from |
| 53 | +/// AI. Once task is received then Chipmunk takes the necessary action on the tasks, |
| 54 | +/// apply those tasks to the UI. |
| 55 | +pub struct ServerCommunication { |
| 56 | + pub task_rx: Receiver<Task>, |
| 57 | +} |
| 58 | + |
| 59 | +impl ServerCommunication { |
| 60 | + pub fn new(task_rx: Receiver<Task>) -> Self { |
| 61 | + Self { task_rx } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// ClientCommuinication is responsible for communicating the tasks from |
| 66 | +/// UI to the AI agent. With this user can send the prompt to AI agent and |
| 67 | +/// AI agent can send the response over mpsc receiver to render on the UI |
| 68 | +pub struct ClientCommunication { |
| 69 | + pub ui_rx: Receiver<String>, |
| 70 | + pub prompt_tx: Sender<String>, |
| 71 | +} |
| 72 | + |
| 73 | +impl ClientCommunication { |
| 74 | + pub fn new(ui_receiver: Receiver<String>, prompt_sender: Sender<String>) -> Self { |
| 75 | + Self { |
| 76 | + ui_rx: ui_receiver, |
| 77 | + prompt_tx: prompt_sender, |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +pub fn start() -> Result<(ChipmunkAI, ServerCommunication, ClientCommunication)> { |
| 83 | + let (task_tx, task_rx) = tokio::sync::mpsc::channel::<Task>(10); |
| 84 | + let (prompt_tx, prompt_rx) = tokio::sync::mpsc::channel::<String>(1); |
| 85 | + let (ui_tx, ui_rx) = tokio::sync::mpsc::channel::<String>(1); |
| 86 | + |
| 87 | + let server_comm = ServerCommunication::new(task_rx); |
| 88 | + let client_comm = ClientCommunication::new(ui_rx, prompt_tx); |
| 89 | + |
| 90 | + let config = AiConfig::default(); |
| 91 | + let manager = ChipmunkAI::new(config, prompt_rx, task_tx, ui_tx); |
| 92 | + |
| 93 | + Ok((manager, server_comm, client_comm)) |
16 | 94 | } |
0 commit comments