tauri_plugin_askit/
lib.rs

1use agent_stream_kit::ASKit;
2use tauri::{
3  plugin::{Builder, TauriPlugin},
4  Manager, Runtime, RunEvent,
5};
6
7mod commands;
8mod error;
9mod models;
10mod observer;
11
12pub use error::{Error, Result};
13pub use models::*;
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_flows,
35      commands::new_agent_flow,
36      commands::rename_agent_flow,
37      commands::unique_flow_name,
38      commands::add_agent_flow,
39      commands::remove_agent_flow,
40      commands::insert_agent_flow,
41      commands::copy_sub_flow,
42      commands::start_agent_flow,
43      commands::stop_agent_flow,
44      commands::new_agent_flow_node,
45      commands::add_agent_flow_node,
46      commands::remove_agent_flow_node,
47      commands::add_agent_flow_edge,
48      commands::remove_agent_flow_edge,
49      commands::start_agent,
50      commands::stop_agent,
51      commands::write_board,
52      commands::set_agent_configs,
53      commands::get_global_configs,
54      commands::get_global_configs_map,
55      commands::set_global_configs,
56      commands::set_global_configs_map,
57      commands::get_agent_default_configs,
58    ])
59    .setup(|app, _api| {
60      let askit = ASKit::init()?;
61      askit.subscribe(Box::new(BoardObserver {
62          app: app.clone(),
63      }));
64      app.manage(askit);
65      Ok(())
66    })
67    .on_event(|app, event| {
68      match event {
69        RunEvent::Ready => {
70          tauri::async_runtime::block_on(async move {
71            let askit = app.state::<ASKit>();
72            askit.ready().await.unwrap();
73          });
74        }
75        RunEvent::Exit => {
76          let askit = app.state::<ASKit>();
77          askit.quit();
78        }
79        _ => {}
80      }
81    })
82    .build()
83}