Proper handling of boolean template function args

This commit is contained in:
Gregory Schier
2025-05-28 13:08:43 -07:00
parent 862d85e48d
commit 72dd768f55
13 changed files with 11906 additions and 62 deletions

View File

@@ -840,7 +840,7 @@ pub struct CallTemplateFunctionResponse {
#[ts(export, export_to = "gen_events.ts")]
pub struct CallTemplateFunctionArgs {
pub purpose: RenderPurpose,
pub values: HashMap<String, String>,
pub values: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]

View File

@@ -11,6 +11,7 @@ use crate::events::{
GetHttpRequestActionsResponse, GetTemplateFunctionsResponse, ImportRequest, ImportResponse,
InternalEvent, InternalEventPayload, JsonPrimitive, PluginWindowContext, RenderPurpose,
};
use crate::native_template_functions::template_function_secure;
use crate::nodejs::start_nodejs_plugin_runtime;
use crate::plugin_handle::PluginHandle;
use crate::server_ws::PluginRuntimeServerWebsocket;
@@ -24,13 +25,12 @@ use tauri::path::BaseDirectory;
use tauri::{AppHandle, Manager, Runtime, WebviewWindow};
use tokio::fs::read_dir;
use tokio::net::TcpListener;
use tokio::sync::{mpsc, Mutex};
use tokio::time::{timeout, Instant};
use tokio::sync::{Mutex, mpsc};
use tokio::time::{Instant, timeout};
use yaak_models::query_manager::QueryManagerExt;
use yaak_models::util::generate_id;
use yaak_templates::error::Error::RenderError;
use yaak_templates::error::Result as TemplateResult;
use crate::native_template_functions::template_function_secure;
#[derive(Clone)]
pub struct PluginManager {
@@ -160,8 +160,7 @@ impl PluginManager {
})
.collect();
let plugins =
app_handle.db().list_plugins().unwrap_or_default();
let plugins = app_handle.db().list_plugins().unwrap_or_default();
let installed_plugin_dirs: Vec<PluginCandidate> = plugins
.iter()
.map(|p| PluginCandidate {
@@ -606,15 +605,12 @@ impl PluginManager {
&self,
window_context: &PluginWindowContext,
fn_name: &str,
args: HashMap<String, String>,
values: HashMap<String, serde_json::Value>,
purpose: RenderPurpose,
) -> TemplateResult<String> {
let req = CallTemplateFunctionRequest {
name: fn_name.to_string(),
args: CallTemplateFunctionArgs {
purpose,
values: args,
},
args: CallTemplateFunctionArgs { purpose, values },
};
let events = self

View File

@@ -34,7 +34,7 @@ pub(crate) fn template_function_secure() -> TemplateFunction {
pub fn template_function_secure_run<R: Runtime>(
app_handle: &AppHandle<R>,
args: HashMap<String, String>,
args: HashMap<String, serde_json::Value>,
window_context: &PluginWindowContext,
) -> Result<String> {
match window_context.clone() {
@@ -43,9 +43,10 @@ pub fn template_function_secure_run<R: Runtime>(
..
} => {
let value = args.get("value").map(|v| v.to_owned()).unwrap_or_default();
if value.is_empty() {
return Ok("".to_string());
}
let value = match value {
serde_json::Value::String(s) => s,
_ => return Ok("".to_string()),
};
let value = match value.strip_prefix("YENC_") {
None => {
@@ -118,7 +119,7 @@ pub fn decrypt_secure_template_function<R: Runtime>(
for a in args {
match a.clone().value {
Val::Str { text } => {
args_map.insert(a.name.to_string(), text);
args_map.insert(a.name.to_string(), serde_json::Value::String(text));
}
_ => continue,
}

View File

@@ -30,7 +30,7 @@ impl<R: Runtime> PluginTemplateCallback<R> {
}
impl<R: Runtime> TemplateCallback for PluginTemplateCallback<R> {
async fn run(&self, fn_name: &str, args: HashMap<String, String>) -> Result<String> {
async fn run(&self, fn_name: &str, args: HashMap<String, serde_json::Value>) -> Result<String> {
// The beta named the function `Response` but was changed in stable.
// Keep this here for a while because there's no easy way to migrate
let fn_name = if fn_name == "Response" { "response" } else { fn_name };