Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ jobs:
strategy:
matrix:
build: [
{ name: 'standard', features: '' },
{ name: 'crypto', features: '--no-default-features --features crypto' },
{ name: 'fetch', features: '--no-default-features --features fetch' },
{ name: 'llm', features: '--no-default-features --features llm' },
{ name: 'wasip1', features: '--no-default-features --features wasip1' },
{ name: 'crypto', features: '--no-default-features --features runtime,crypto' },
{ name: 'fetch', features: '--no-default-features --features runtime,fetch' },
{ name: 'wasip1', features: '--no-default-features --features runtime,wasip1' },
{ name: 'llm', features: '--no-default-features --features runtime,llm' },
{ name: 'full', features: '--all-features' },
]

Expand Down Expand Up @@ -46,7 +45,7 @@ jobs:
id: prepare
run: |
VERSION=${{ github.event.release.tag_name }}
SUFFIX="${{ matrix.build.name != 'standard' && format('-{0}', matrix.build.name) || '' }}"
SUFFIX="${{ format('-{0}', matrix.build.name) || '' }}"
ARTIFACT_NAME="bless-plugins${SUFFIX}-${VERSION}.wasm"
mv bless_plugins.wasm "$ARTIFACT_NAME"
echo "artifact_name=$ARTIFACT_NAME" >> "$GITHUB_OUTPUT"
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/blessnetwork/javy-bless-plugins"

[lib]
name = "bless_plugins"
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
anyhow = "1.0.95"
Expand All @@ -27,7 +27,8 @@ lto = true
opt-level = 3

[features]
default = ["crypto", "fetch", "llm", "wasip1"]
default = ["runtime", "crypto", "fetch", "llm", "wasip1"]
runtime = []
crypto = []
fetch = ["blockless-sdk/http"]
llm = ["blockless-sdk/llm"]
Expand Down
187 changes: 97 additions & 90 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[allow(unused_imports)]
use javy_plugin_api::{
import_namespace,
javy::{
hold, hold_and_release,
quickjs::{prelude::MutFn, Function},
quickjs::{prelude::MutFn, Ctx, Function, Result},
to_js_error, Args,
},
Config,
Expand All @@ -17,112 +18,30 @@ pub mod llm;
#[cfg(feature = "wasip1")]
pub mod wasi;

#[cfg(feature = "crypto")]
use crypto::bless_get_random_values;

#[cfg(feature = "fetch")]
use fetch::bless_fetch_request;

#[cfg(feature = "llm")]
use llm::bless_llm_plugin;

#[cfg(feature = "runtime")]
import_namespace!("bless_core_plugins");

#[cfg(feature = "runtime")]
#[export_name = "initialize_runtime"]
pub extern "C" fn initialize_runtime() {
let mut config = Config::default();
config.event_loop(true);
config.javy_stream_io(true);
config.text_encoding(true);

#[allow(unused_variables)]
javy_plugin_api::initialize_runtime(config, |runtime| {
runtime
.context()
.with(|ctx| {
#[cfg(feature = "crypto")]
ctx.globals().set(
"__javy_crypto_get_random_values",
Function::new(
ctx.clone(),
MutFn::new(move |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
bless_get_random_values(hold!(cx.clone(), args))
.map_err(|e| to_js_error(cx, e))
}),
)?,
)?;

set_crypto_globals(&ctx)?;
#[cfg(feature = "fetch")]
ctx.globals().set(
"__javy_fetchio_request",
Function::new(
ctx.clone(),
MutFn::new(move |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
bless_fetch_request(hold!(cx.clone(), args))
.map_err(|e| to_js_error(cx, e))
}),
)?,
)?;

set_fetch_globals(&ctx)?;
#[cfg(feature = "wasip1")]
{
macro_rules! bind {
(function, $l: ident) => {
let name = concat!("__javy_", stringify!($l));
ctx.globals().set(
name,
Function::new(
ctx.clone(),
MutFn::new(move |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
wasi::$l(hold!(cx.clone(), args))
.map_err(|e| to_js_error(cx, e))
}),
)?,
)?;
};
}
bind!(function, wasi_preview1_open);
bind!(function, wasi_preview1_fd_prestat_dir_name);
bind!(function, wasi_preview1_path_create_directory);
bind!(function, wasi_preview1_path_remove_directory);
bind!(function, wasi_preview1_path_unlink_file);
bind!(function, wasi_preview1_close);
bind!(function, wasi_preview1_path_symlink);
bind!(function, wasi_preview1_path_link);
bind!(function, wasi_preview1_path_rename);
bind!(function, wasi_preview1_path_filestat_get);
}

#[cfg(feature = "llm")]
ctx.globals().set(
"BlessLLM",
Function::new(
ctx.clone(),
MutFn::new(move |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
bless_llm_plugin(hold!(cx.clone(), args))
.map_err(|e| to_js_error(cx, e))
}),
)?,
)?;

// Expose the suppported models object globally for JS
set_wasi_globals(&ctx)?;
#[cfg(feature = "llm")]
ctx.globals().set(
"MODELS",
javy_plugin_api::javy::quickjs::Value::from_object(
llm::supported_models_object(&ctx)?,
),
)?;

#[cfg(feature = "crypto")]
ctx.eval::<(), _>(include_str!("crypto/crypto.js"))?;
#[cfg(feature = "fetch")]
ctx.eval::<(), _>(include_str!("fetch/fetch.js"))?;
#[cfg(feature = "wasip1")]
ctx.eval::<(), _>(include_str!("wasi/preview_1.js"))?;
set_llm_globals(&ctx)?;
Ok::<_, anyhow::Error>(())
})
.unwrap();
Expand All @@ -131,3 +50,91 @@ pub extern "C" fn initialize_runtime() {
})
.unwrap();
}

#[cfg(feature = "crypto")]
pub fn set_crypto_globals(ctx: &Ctx<'_>) -> Result<()> {
ctx.globals().set(
"__javy_crypto_get_random_values",
Function::new(
ctx.clone(),
MutFn::new(move |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
crypto::bless_get_random_values(hold!(cx.clone(), args))
.map_err(|e| to_js_error(cx, e))
}),
)?,
)?;
ctx.eval::<(), _>(include_str!("crypto/crypto.js"))?;
Ok(())
}

#[cfg(feature = "fetch")]
pub fn set_fetch_globals(ctx: &Ctx<'_>) -> Result<()> {
ctx.globals().set(
"__javy_fetchio_request",
Function::new(
ctx.clone(),
MutFn::new(move |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
fetch::bless_fetch_request(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e))
}),
)?,
)?;
ctx.eval::<(), _>(include_str!("fetch/fetch.js"))?;
Ok(())
}

#[cfg(feature = "wasip1")]
pub fn set_wasi_globals(ctx: &Ctx<'_>) -> Result<()> {
macro_rules! bind {
(function, $l: ident) => {
let name = concat!("__javy_", stringify!($l));
ctx.globals().set(
name,
Function::new(
ctx.clone(),
MutFn::new(move |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
wasi::$l(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e))
}),
)?,
)?;
};
}
bind!(function, wasi_preview1_open);
bind!(function, wasi_preview1_fd_prestat_dir_name);
bind!(function, wasi_preview1_path_create_directory);
bind!(function, wasi_preview1_path_remove_directory);
bind!(function, wasi_preview1_path_unlink_file);
bind!(function, wasi_preview1_close);
bind!(function, wasi_preview1_path_symlink);
bind!(function, wasi_preview1_path_link);
bind!(function, wasi_preview1_path_rename);
bind!(function, wasi_preview1_path_filestat_get);
ctx.eval::<(), _>(include_str!("wasi/preview_1.js"))?;
Ok(())
}

#[cfg(feature = "llm")]
pub fn set_llm_globals(ctx: &Ctx<'_>) -> Result<()> {
ctx.globals().set(
"BlessLLM",
Function::new(
ctx.clone(),
MutFn::new(move |cx, args| {
let (cx, args) = hold_and_release!(cx, args);
llm::bless_llm_plugin(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e))
}),
)?,
)?;

// Expose the suppported models object globally for JS
let ctx_clone = ctx.clone();
ctx.globals().set(
"MODELS",
javy_plugin_api::javy::quickjs::Value::from_object(
llm::supported_models_object(ctx).map_err(|e| to_js_error(ctx_clone, e))?,
),
)?;
Ok(())
}
Loading