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_stream_info,
36            commands::get_agent_stream_infos,
37            commands::get_agent_stream_spec,
38            commands::set_agent_stream_spec,
39            commands::new_agent_stream,
40            commands::rename_agent_stream,
41            commands::unique_stream_name,
42            commands::add_agent_stream,
43            commands::remove_agent_stream,
44            commands::copy_sub_stream,
45            commands::start_agent_stream,
46            commands::stop_agent_stream,
47            commands::new_agent_spec,
48            commands::add_agent,
49            commands::remove_agent,
50            commands::add_channel,
51            commands::remove_channel,
52            commands::start_agent,
53            commands::stop_agent,
54            commands::write_board,
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        ])
61        .setup(|app, _api| {
62            let askit = ASKit::init()?;
63            askit.subscribe(Box::new(BoardObserver { app: app.clone() }));
64            app.manage(askit);
65            Ok(())
66        })
67        .on_event(|app, event| match event {
68            RunEvent::Ready => {
69                tauri::async_runtime::block_on(async move {
70                    let askit = app.state::<ASKit>();
71                    askit.ready().await.unwrap();
72                });
73            }
74            RunEvent::Exit => {
75                let askit = app.state::<ASKit>();
76                askit.quit();
77            }
78            _ => {}
79        })
80        .build()
81}