Skip to content

Commit

Permalink
refactor action to anything plugin to make it understandable for trig…
Browse files Browse the repository at this point in the history
…gers and actions. deprecated action_name
  • Loading branch information
carllippert committed May 24, 2024
1 parent b92ef57 commit 65524e9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 82 deletions.
17 changes: 8 additions & 9 deletions plugin-core/crates/pdk-mock-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anything_pdk::{Action, Event, Handle, Log};
use anything_pdk::{AnythingPlugin, Event, Handle, Log};
use extism_pdk::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand All @@ -16,14 +16,13 @@ pub fn execute(config: Value) -> FnResult<Value> {
}

#[plugin_fn]
pub fn register() -> FnResult<Action> {
//Used to let UI and users know how to configure actions
let action: Action = Action::builder()
pub fn register() -> FnResult<AnythingPlugin> {
//Used to let UI and users know how to configure
let plugin: AnythingPlugin = AnythingPlugin::builder()
.trigger(false)
.action_name("example_node".to_string())
.action_label("Example Node".to_string())
.label("Example Plugin".to_string())
.icon("<svg></svg>".to_string())
.description("This is an example action".to_string())
.description("This is an example plugin".to_string())
.variables(vec![])
.input(serde_json::json!({
"method": "GET",
Expand Down Expand Up @@ -51,8 +50,8 @@ pub fn register() -> FnResult<Action> {
"required": ["method", "url"],
"additionalProperties": false
}))
.plugin_id("example_extension".to_string())
.plugin_id("example_plugin".to_string())
.build();

Ok(action)
Ok(plugin)
}
49 changes: 6 additions & 43 deletions plugin-core/crates/pdk-mock-simple-host/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
// create a main
// have it load plugin from file
// have it call register on them
// have it call execute on them

// use anything_pdk::*;
// use extism::*;

// fn main() {
// let url =
// Wasm::url("https://github.com/extism/plugins/releases/latest/download/count_vowels.wasm");
// let manifest = Manifest::new([url]);
// let mut plugin = Plugin::new(&manifest, [], true).unwrap();
// let res = plugin
// .call::<&str, &str>("count_vowels", "Hello, world!")
// .unwrap();
// println!("{}", res);
// }

use anything_pdk::Action;
use anything_pdk::AnythingPlugin;
use extism::*;
// use jsonschema::{Draft, JSONSchema};
// use serde_json::json;

fn main() {
println!("Run `cargo test` to execute the tests.");
Expand Down Expand Up @@ -48,12 +27,12 @@ mod tests {

println!("{:?}", register_res);

let action: Action =
serde_json::from_str(&register_res).expect("Failed to deserialize JSON Action");
let plugin_registration_object: AnythingPlugin =
serde_json::from_str(&register_res).expect("Failed to deserialize Plugin Registration JSON");

let input_schema = action.input_schema;
let input_schema = plugin_registration_object.input_schema;
println!("input_schema {:?}\n", input_schema);
let input = action.input;
let input = plugin_registration_object.input;

assert!(
is_valid(&input_schema, &input),
Expand Down Expand Up @@ -84,7 +63,7 @@ mod tests {
"required": ["status"]
});

let output_schema = action.output_schema;
let output_schema = plugin_registration_object.output_schema;
println!("output_schema {:?}\n", output_schema);

let exec_res_json =
Expand All @@ -103,21 +82,5 @@ mod tests {
is_valid(&output_schema, &exec_res_json),
"Output does not match the expected schema"
);

//check output schema follows anything schema output schema
// assert!(
// is_valid(&anything_output_schema, &output_schema),
// // compiled_output_schema.is_valid(&anything_output_schema),
// "Output schema does not match the expected schema"
// );

// let output = serde_json::from_str::<Value>(&execute_res).expect("Invalid JSON");

// let compiled_output = JSONSchema::compile(&output).expect("An invalid schema");
// //check that the executions return matches the defined output schema
// assert!(
// compiled_output.is_valid(&compiled_output_schema),
// "Output does not match the expected schema"
// );
}
}
17 changes: 8 additions & 9 deletions plugin-core/crates/pdk-mock-trigger-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anything_pdk::{Action, Event, Handle, Log};
use anything_pdk::{AnythingPlugin, Event, Handle, Log};
use extism_pdk::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand All @@ -18,12 +18,11 @@ pub fn execute(config: Value) -> FnResult<Value> {
}

#[plugin_fn]
pub fn register() -> FnResult<Action> {
pub fn register() -> FnResult<AnythingPlugin> {
//Used to let UI and users know how to configure actions
let action: Action = Action::builder()
let plugin: AnythingPlugin = AnythingPlugin::builder()
.trigger(true)
.action_name("example_cron_trigger".to_string())
.action_label("Example Cron Trigger".to_string())
.label("Example Cron Trigger".to_string())
.icon("<svg></svg>".to_string())
.description("Example Of A Cron Trigger".to_string())
.variables(vec![])
Expand All @@ -44,7 +43,7 @@ pub fn register() -> FnResult<Action> {
"type": "object",
"properties": {
"status": {
"type": "string"
"type": "string",
"enum": ["success", "error"]
},
"output": {
Expand All @@ -57,8 +56,8 @@ pub fn register() -> FnResult<Action> {
"required": ["status"],
"additionalProperties": false
}))
.plugin_id("example_cron_extension".to_string())
.plugin_id("example_cron_plugin".to_string())
.build();

Ok(action)
}
Ok(plugin)
}
27 changes: 12 additions & 15 deletions plugin-core/crates/pdk-test-runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anything_pdk::{Action, Event, Handle, Log};
use anything_pdk::{AnythingPlugin, Event, Handle, Log};
use extism_pdk::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand All @@ -7,38 +7,35 @@ use xtp_test;
// You _must_ export a single `test` function for the runner to execute.
#[plugin_fn]
pub fn test() -> FnResult<()> {
let Json(action): Json<Action> = xtp_test::call("register", "")?;
let Json(plugin): Json<AnythingPlugin> = xtp_test::call("register", "")?;

// Testing basic top level info on trigger is returned.
// xtp_test::assert!("action trigger is false", action.trigger == false);
// xtp_test::assert!("plugin trigger is false", plugin.trigger == false);

xtp_test::assert!(
"action plugin_id is non-empty",
!action.plugin_id.is_empty()
"plugin plugin_id is non-empty",
!plugin.plugin_id.is_empty()
);


xtp_test::assert!("label is non-empty", !plugin.label.is_empty());
xtp_test::assert!("plugin icon is non-empty", !plugin.icon.is_empty());
xtp_test::assert!(
"action action_name is non-empty",
!action.action_name.is_empty()
);
xtp_test::assert!("action_label is non-empty", !action.action_label.is_empty());
xtp_test::assert!("action icon is non-empty", !action.icon.is_empty());
xtp_test::assert!(
"action description is non-empty",
!action.description.is_empty()
"plugin description is non-empty",
!plugin.description.is_empty()
);

// Validate the config field
xtp_test::assert!(
"input is valid JSON and has keys",
validate_schema(&action.input)
validate_schema(&plugin.input)
);

// Validate that the config schema is real
// Validate the config field
xtp_test::assert!(
"input_schema is valid JSON and has keys",
validate_schema(&action.input_schema)
validate_schema(&plugin.input_schema)
);
Ok(())
}
Expand Down
11 changes: 5 additions & 6 deletions plugin-core/crates/plugins/anything-http-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anything_pdk::{Action, Event, Handle, Log};
use anything_pdk::{AnythingPlugin, Event, Handle, Log};
use extism_pdk::*;
use serde::Deserialize;
use serde_json::{json, Value};
Expand All @@ -7,11 +7,10 @@ use serde_json::{json, Value};
//It also provides Information to generate a nice UI including icons and labels.
//Not all hosts will use all information ( likely )
#[plugin_fn]
pub fn register() -> FnResult<Action> {
let action: Action = Action::builder()
pub fn register() -> FnResult<AnythingPlugin> {
let plugin: AnythingPlugin = AnythingPlugin::builder()
.trigger(false)
.action_name("http_action".to_string())
.action_label("HTTP Action".to_string())
.label("HTTP Plugin".to_string())
.icon("<svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\"><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M2.998 5.58a5.55 5.55 0 0 1 1.62-3.88l-.71-.7a6.45 6.45 0 0 0 0 9.16l.71-.7a5.55 5.55 0 0 1-1.62-3.88zm1.06 0a4.42 4.42 0 0 0 1.32 3.17l.71-.71a3.27 3.27 0 0 1-.76-1.12 3.45 3.45 0 0 1 0-2.67 3.22 3.22 0 0 1 .76-1.13l-.71-.71a4.46 4.46 0 0 0-1.32 3.17zm7.65 3.21l-.71-.71c.33-.32.59-.704.76-1.13a3.449 3.449 0 0 0 0-2.67 3.22 3.22 0 0 0-.76-1.13l.71-.7a4.468 4.468 0 0 1 0 6.34zM13.068 1l-.71.71a5.43 5.43 0 0 1 0 7.74l.71.71a6.45 6.45 0 0 0 0-9.16zM9.993 5.43a1.5 1.5 0 0 1-.245.98 2 2 0 0 1-.27.23l3.44 7.73-.92.4-.77-1.73h-5.54l-.77 1.73-.92-.4 3.44-7.73a1.52 1.52 0 0 1-.33-1.63 1.55 1.55 0 0 1 .56-.68 1.5 1.5 0 0 1 2.325 1.1zm-1.595-.34a.52.52 0 0 0-.25.14.52.52 0 0 0-.11.22.48.48 0 0 0 0 .29c.04.09.102.17.18.23a.54.54 0 0 0 .28.08.51.51 0 0 0 .5-.5.54.54 0 0 0-.08-.28.58.58 0 0 0-.23-.18.48.48 0 0 0-.29 0zm.23 2.05h-.27l-.87 1.94h2l-.86-1.94zm2.2 4.94l-.89-2h-2.88l-.89 2h4.66z\"/></svg>".to_string())
.description("Make an HTTP request".to_string())
.variables(vec![])
Expand Down Expand Up @@ -61,7 +60,7 @@ pub fn register() -> FnResult<Action> {
.plugin_id("http".to_string())
.build();

Ok(action)
Ok(plugin)
}

#[plugin_fn]
Expand Down

0 comments on commit 65524e9

Please sign in to comment.