tauri_plugin_askit/
lib.rs

1use agent_stream_kit::ASKit;
2use tauri::{
3    plugin::{Builder, TauriPlugin},
4    Manager, RunEvent, Runtime,
5};
6
7mod commands;
8mod error;
9mod models;
10mod observer;
11
12pub use error::{Error, Result};
13pub use models::BoardMessage;
14
15use observer::BoardObserver;
16
17/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the askit APIs.
18pub trait ASKitExt<R: Runtime> {
19    fn askit(&self) -> &ASKit;
20}
21
22impl<R: Runtime, T: Manager<R>> crate::ASKitExt<R> for T {
23    fn askit(&self) -> &ASKit {
24        self.state::<ASKit>().inner()
25    }
26}
27
28/// Initializes the plugin.
29pub fn init<R: Runtime>() -> TauriPlugin<R> {
30    Builder::new("askit")
31        .invoke_handler(tauri::generate_handler![
32            commands::get_agent_definition,
33            commands::get_agent_definitions,
34            commands::get_agent_spec,
35            commands::get_agent_streams,
36            commands::new_agent_stream,
37            commands::rename_agent_stream,
38            commands::unique_stream_name,
39            commands::add_agent_stream,
40            commands::remove_agent_stream,
41            commands::insert_agent_stream,
42            commands::copy_sub_stream,
43            commands::start_agent_stream,
44            commands::stop_agent_stream,
45            commands::new_agent_spec,
46            commands::add_agent,
47            commands::remove_agent,
48            commands::add_channel,
49            commands::remove_channel,
50            commands::start_agent,
51            commands::stop_agent,
52            commands::write_board,
53            commands::set_agent_configs,
54            commands::get_global_configs,
55            commands::get_global_configs_map,
56            commands::set_global_configs,
57            commands::set_global_configs_map,
58            commands::get_agent_config_specs,
59        ])
60        .setup(|app, _api| {
61            let askit = ASKit::init()?;
62            askit.subscribe(Box::new(BoardObserver { app: app.clone() }));
63            app.manage(askit);
64            Ok(())
65        })
66        .on_event(|app, event| match event {
67            RunEvent::Ready => {
68                tauri::async_runtime::block_on(async move {
69                    let askit = app.state::<ASKit>();
70                    askit.ready().await.unwrap();
71                });
72            }
73            RunEvent::Exit => {
74                let askit = app.state::<ASKit>();
75                askit.quit();
76            }
77            _ => {}
78        })
79        .build()
80}