Skip to content

Commit

Permalink
restructuring common structs into anything_pdk
Browse files Browse the repository at this point in the history
  • Loading branch information
carllippert committed May 17, 2024
1 parent 837f972 commit 3f13105
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 47 deletions.
5 changes: 5 additions & 0 deletions plugin-core/Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ default:

all: build-all test-all

mock: build-mock-plugin build-mock-host build-test-runner test-mock

build-all: build-mock-plugin build-mock-host build-test-runner build-anything-http-plugin

build-mock-plugin:
Expand All @@ -44,6 +46,9 @@ build-anything-http-plugin:
# Test All
test-all: test-mock-plugin test-http-plugin

# Test Mock
test-mock: test-mock-plugin

# Test Mock Plugin with Mock Host using the test runner
test-mock-plugin:
cd {{crates_dir}} && xtp plugin test {{mock_plugin_wasm}} --with {{test_runner_wasm}} --mock-host {{mock_host_wasm}}
Expand Down
1 change: 1 addition & 0 deletions plugin-core/crates/pdk-mock-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate_type = ["cdylib"]

[dependencies]
extism-pdk = "1.1.0"
anything-pdk = { path = "../pdk" }
serde = { version = "1.0.201", features = ["derive"] }
serde_json = "1.0.117"

Expand Down
25 changes: 1 addition & 24 deletions plugin-core/crates/pdk-mock-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
use anything_pdk::{Action, Log, Event};
use extism_pdk::*;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Log {
pub time: String,
pub message: String,
}

#[derive(Deserialize, Serialize)]
struct Event {
pub id: String,
pub name: String,
pub description: String,
pub timestamp: String,
}

#[derive(serde::Serialize, ToBytes)]
#[encoding(Json)]
struct Action {
pub id: String,
pub name: String,
pub description: String,
pub timestamp: String,
}

#[host_fn]
extern "ExtismHost" {
fn host_log(log: String) -> Json<Log>;
Expand All @@ -36,7 +14,6 @@ pub fn execute(message: String) -> FnResult<String> {
Ok(message)
}


#[plugin_fn]
pub fn register() -> FnResult<Action> {
let action = Action {
Expand Down
2 changes: 1 addition & 1 deletion plugin-core/crates/pdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ description = "Anything Plugin Development Kit (PDK) based on Extism PDK"
repository = "https://github.com/tryanything-ai/anything"

[dependencies]
extism = "1.2.0"
extism-pdk = "1.1.0"
serde = { version = "1.0.201", features = ["derive"] }
serde_json = "1.0.117"
70 changes: 48 additions & 22 deletions plugin-core/crates/pdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
use extism::{CurrentPlugin, Error, Function, UserData, Val, ValType};
use serde_json::Value;
// use extism::{CurrentPlugin, Error, Function, Json, UserData, Val, ValType};
// use serde_json::Value;

//How Proto calls this https://github.com/moonrepo/proto/blob/d423b236948211aa644c4a8389f1f92343936260/crates/core/src/tool.rs
pub fn create_host_functions() -> Vec<Function> {
vec![Function::new(
"host_log",
[ValType::I64],
[],
UserData::new(()),
host_log,
)]
use extism_pdk::*;
use serde::{Deserialize, Serialize};
//export a type used for register function
#[derive(serde::Serialize, ToBytes)]
#[encoding(Json)]
pub struct Action {
pub id: String,
pub name: String,
pub description: String,
pub timestamp: String,
}

#[derive(Deserialize, Serialize)]
pub struct Log {
pub time: String,
pub message: String,
}

fn host_log(
plugin: &mut CurrentPlugin,
inputs: &[Val],
_outputs: &mut [Val],
_user_data: UserData<()>,
) -> Result<(), Error> {
let input: Value = serde_json::from_str(plugin.memory_get_val(&inputs[0])?)?;
let message = input["message"].as_str().unwrap_or("No message provided");
#[derive(Deserialize, Serialize)]
pub struct Event {
pub id: String,
pub name: String,
pub description: String,
pub timestamp: String,
}

//How Proto calls this https://github.com/moonrepo/proto/blob/d423b236948211aa644c4a8389f1f92343936260/crates/core/src/tool.rs
// pub fn create_host_functions() -> Vec<Function> {
// vec![Function::new(
// "host_log",
// [ValType::I64],
// [],
// UserData::new(()),
// host_log,
// )]
// }

// fn host_log(
// plugin: &mut CurrentPlugin,
// inputs: &[Val],
// _outputs: &mut [Val],
// _user_data: UserData<()>,
// ) -> Result<(), Error> {
// let input: Value = serde_json::from_str(plugin.memory_get_val(&inputs[0])?)?;
// let message = input["message"].as_str().unwrap_or("No message provided");

println!("Host Function Logged: {}", message);
// println!("Host Function Logged: {}", message);

Ok(())
}
// Ok(())
// }

0 comments on commit 3f13105

Please sign in to comment.