bless_plugins/
lib.rs

1use javy_plugin_api::{
2    import_namespace,
3    javy::{
4        hold, hold_and_release,
5        quickjs::{prelude::MutFn, Function},
6        to_js_error, Args,
7    },
8    Config,
9};
10
11#[cfg(feature = "crawl")]
12pub mod crawl;
13#[cfg(feature = "crypto")]
14pub mod crypto;
15#[cfg(feature = "fetch")]
16pub mod fetch;
17#[cfg(feature = "llm")]
18pub mod llm;
19#[cfg(feature = "wasip1")]
20pub mod wasi;
21
22#[cfg(feature = "crypto")]
23use crypto::bless_get_random_values;
24
25#[cfg(feature = "crawl")]
26use crawl::bless_crawl;
27
28#[cfg(feature = "fetch")]
29use fetch::bless_fetch_request;
30
31#[cfg(feature = "llm")]
32use llm::bless_llm_plugin;
33
34import_namespace!("bless_core_plugins");
35
36#[export_name = "initialize_runtime"]
37pub extern "C" fn initialize_runtime() {
38    let mut config = Config::default();
39    config.event_loop(true);
40    config.javy_stream_io(true);
41    config.text_encoding(true);
42
43    javy_plugin_api::initialize_runtime(config, |runtime| {
44        runtime
45            .context()
46            .with(|ctx| {
47                #[cfg(feature = "crypto")]
48                ctx.globals().set(
49                    "__javy_crypto_get_random_values",
50                    Function::new(
51                        ctx.clone(),
52                        MutFn::new(move |cx, args| {
53                            let (cx, args) = hold_and_release!(cx, args);
54                            bless_get_random_values(hold!(cx.clone(), args))
55                                .map_err(|e| to_js_error(cx, e))
56                        }),
57                    )?,
58                )?;
59
60                #[cfg(feature = "fetch")]
61                ctx.globals().set(
62                    "__javy_fetchio_request",
63                    Function::new(
64                        ctx.clone(),
65                        MutFn::new(move |cx, args| {
66                            let (cx, args) = hold_and_release!(cx, args);
67                            bless_fetch_request(hold!(cx.clone(), args))
68                                .map_err(|e| to_js_error(cx, e))
69                        }),
70                    )?,
71                )?;
72
73                #[cfg(feature = "wasip1")]
74                {
75                    macro_rules! bind {
76                        (function, $l: ident) => {
77                            let name = concat!("__javy_", stringify!($l));
78                            ctx.globals().set(
79                                name,
80                                Function::new(
81                                    ctx.clone(),
82                                    MutFn::new(move |cx, args| {
83                                        let (cx, args) = hold_and_release!(cx, args);
84                                        wasi::$l(hold!(cx.clone(), args))
85                                            .map_err(|e| to_js_error(cx, e))
86                                    }),
87                                )?,
88                            )?;
89                        };
90                    }
91                    bind!(function, wasi_preview1_open);
92                    bind!(function, wasi_preview1_fd_prestat_dir_name);
93                    bind!(function, wasi_preview1_path_create_directory);
94                    bind!(function, wasi_preview1_path_remove_directory);
95                    bind!(function, wasi_preview1_path_unlink_file);
96                    bind!(function, wasi_preview1_close);
97                    bind!(function, wasi_preview1_path_symlink);
98                    bind!(function, wasi_preview1_path_link);
99                    bind!(function, wasi_preview1_path_rename);
100                    bind!(function, wasi_preview1_path_filestat_get);
101                }
102
103                #[cfg(feature = "crawl")]
104                ctx.globals().set(
105                    "BlessCrawl",
106                    Function::new(
107                        ctx.clone(),
108                        MutFn::new(move |cx, args| {
109                            let (cx, args) = hold_and_release!(cx, args);
110                            bless_crawl(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e))
111                        }),
112                    )?,
113                )?;
114
115                #[cfg(feature = "llm")]
116                ctx.globals().set(
117                    "BlessLLM",
118                    Function::new(
119                        ctx.clone(),
120                        MutFn::new(move |cx, args| {
121                            let (cx, args) = hold_and_release!(cx, args);
122                            bless_llm_plugin(hold!(cx.clone(), args))
123                                .map_err(|e| to_js_error(cx, e))
124                        }),
125                    )?,
126                )?;
127
128                // Expose the suppported models object globally for JS
129                #[cfg(feature = "llm")]
130                ctx.globals().set(
131                    "MODELS",
132                    javy_plugin_api::javy::quickjs::Value::from_object(
133                        llm::supported_models_object(&ctx)?,
134                    ),
135                )?;
136
137                #[cfg(feature = "crypto")]
138                ctx.eval::<(), _>(include_str!("crypto/crypto.js"))?;
139                #[cfg(feature = "crawl")]
140                ctx.eval::<(), _>(include_str!("crawl/crawl.js"))?;
141                #[cfg(feature = "fetch")]
142                ctx.eval::<(), _>(include_str!("fetch/fetch.js"))?;
143                #[cfg(feature = "wasip1")]
144                ctx.eval::<(), _>(include_str!("wasi/preview_1.js"))?;
145                Ok::<_, anyhow::Error>(())
146            })
147            .unwrap();
148
149        runtime
150    })
151    .unwrap();
152}