Skip to main content

tauri_plugin_modular_agent/
lib.rs

1#![recursion_limit = "256"]
2
3use modular_agent_core::ModularAgent;
4use tauri::{
5    plugin::{Builder, TauriPlugin},
6    Manager, RunEvent, Runtime,
7};
8
9mod commands;
10mod error;
11
12pub use error::{Error, Result};
13
14/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the ma APIs.
15pub trait ModularAgentExt<R: Runtime> {
16    fn ma(&self) -> &ModularAgent;
17}
18
19impl<R: Runtime, T: Manager<R>> crate::ModularAgentExt<R> for T {
20    fn ma(&self) -> &ModularAgent {
21        self.state::<ModularAgent>().inner()
22    }
23}
24
25/// Initializes the plugin.
26pub fn init<R: Runtime>() -> TauriPlugin<R> {
27    Builder::new("modular-agent")
28        .invoke_handler(tauri::generate_handler![
29            // Preset management
30            commands::new_preset,
31            commands::add_preset,
32            commands::add_preset_with_name,
33            commands::remove_preset,
34            commands::start_preset,
35            commands::stop_preset,
36            commands::open_preset_from_file,
37            commands::save_preset,
38            commands::get_preset_spec,
39            commands::update_preset_spec,
40            commands::get_preset_info,
41            commands::get_preset_infos,
42            // Agent management
43            commands::get_agent_definition,
44            commands::get_agent_definitions,
45            commands::get_agent_spec,
46            commands::update_agent_spec,
47            commands::new_agent_spec,
48            commands::add_agent,
49            commands::remove_agent,
50            commands::add_connection,
51            commands::remove_connection,
52            commands::add_agents_and_connections,
53            commands::start_agent,
54            commands::stop_agent,
55            commands::set_agent_configs,
56            commands::get_global_configs,
57            commands::get_global_configs_map,
58            commands::set_global_configs,
59            commands::set_global_configs_map,
60            commands::write_external_input,
61        ])
62        .setup(|app, _api| {
63            let ma = ModularAgent::init()?;
64            app.manage(ma);
65            Ok(())
66        })
67        .on_event(|app, event| match event {
68            RunEvent::Ready => {
69                tauri::async_runtime::block_on(async move {
70                    let ma = app.state::<ModularAgent>();
71                    ma.ready().await.unwrap();
72                });
73            }
74            RunEvent::Exit => {
75                let ma = app.state::<ModularAgent>();
76                ma.quit();
77            }
78            _ => {}
79        })
80        .build()
81}