fix bindings and improve clone accuracy

This commit is contained in:
Stefan
2024-09-27 15:18:53 +02:00
parent 298881ec05
commit d3622856c7
5 changed files with 21 additions and 16 deletions
+2 -2
View File
@@ -31,7 +31,7 @@ impl CustomNodeAdapter for CustomNode {
let promise: Promise<ZenEngineHandlerResponse> = function
.clone()
.call_async(ZenEngineHandlerRequest {
input: request.input.clone(),
input: request.input.to_value(),
node: node_data,
})
.await
@@ -40,7 +40,7 @@ impl CustomNodeAdapter for CustomNode {
let result = promise.await.map_err(|err| anyhow!(err.reason))?;
Ok(NodeResponse {
output: result.output,
output: result.output.into(),
trace_data: result.trace_data,
})
}
+3 -2
View File
@@ -39,13 +39,14 @@ impl ZenDecision {
async move {
decision
.evaluate_with_opts(
&context,
context.into(),
EvaluationOptions {
max_depth: options.max_depth,
trace: options.trace,
},
)
.await
.map(ZenEngineResponse::from)
}
})
.await
@@ -54,7 +55,7 @@ impl ZenDecision {
anyhow!(serde_json::to_string(e.as_ref()).unwrap_or_else(|_| e.to_string()))
})?;
Ok(ZenEngineResponse::from(result))
Ok(result)
}
#[napi(ts_return_type = "Promise<SafeResult<ZenEngineResponse>>")]
+3 -2
View File
@@ -118,13 +118,14 @@ impl ZenEngine {
graph
.evaluate_with_opts(
key,
&context,
context.into(),
EvaluationOptions {
max_depth: options.max_depth,
trace: options.trace,
},
)
.await
.map(ZenEngineResponse::from)
}
})
.await
@@ -133,7 +134,7 @@ impl ZenEngine {
anyhow!(serde_json::to_string(e.as_ref()).unwrap_or_else(|_| e.to_string()))
})?;
Ok(ZenEngineResponse::from(result))
Ok(result)
}
#[napi]
+7 -5
View File
@@ -7,8 +7,9 @@ pub fn evaluate_expression_sync(expression: String, context: Option<Value>) -> n
let ctx = context.unwrap_or(Value::Null);
Ok(
zen_expression::evaluate_expression(expression.as_str(), &ctx)
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?,
zen_expression::evaluate_expression(expression.as_str(), ctx.into())
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?
.to_value(),
)
}
@@ -16,7 +17,7 @@ pub fn evaluate_expression_sync(expression: String, context: Option<Value>) -> n
#[napi]
pub fn evaluate_unary_expression_sync(expression: String, context: Value) -> napi::Result<bool> {
Ok(
zen_expression::evaluate_unary_expression(expression.as_str(), &context)
zen_expression::evaluate_unary_expression(expression.as_str(), context.into())
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?,
)
}
@@ -24,8 +25,9 @@ pub fn evaluate_unary_expression_sync(expression: String, context: Value) -> nap
#[allow(dead_code)]
#[napi]
pub fn render_template_sync(template: String, context: Value) -> napi::Result<Value> {
Ok(zen_tmpl::render(template.as_str(), &context)
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?)
Ok(zen_tmpl::render(template.as_str(), context.into())
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?
.to_value())
}
#[allow(dead_code)]
+6 -5
View File
@@ -7,6 +7,7 @@ use serde_json::Value;
use zen_engine::handler::custom_node_adapter::CustomDecisionNode;
use zen_engine::{DecisionGraphResponse, DecisionGraphTrace};
use zen_expression::Variable;
#[napi(object)]
pub struct ZenEngineTrace {
@@ -23,8 +24,8 @@ impl From<DecisionGraphTrace> for ZenEngineTrace {
Self {
id: value.id,
name: value.name,
input: value.input,
output: value.output,
input: value.input.to_value(),
output: value.output.to_value(),
performance: value.performance,
trace_data: value.trace_data,
}
@@ -42,7 +43,7 @@ impl From<DecisionGraphResponse> for ZenEngineResponse {
fn from(value: DecisionGraphResponse) -> Self {
Self {
performance: value.performance,
result: value.result,
result: value.result.to_value(),
trace: value.trace.map(|opt| {
opt.into_iter()
.map(|(key, value)| (key, ZenEngineTrace::from(value)))
@@ -104,10 +105,10 @@ impl ZenEngineHandlerRequest {
return Ok(selected_value);
};
let template_value = zen_tmpl::render(template.as_str(), &self.input)
let template_value = zen_tmpl::render(template.as_str(), Variable::from(&self.input))
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?;
Ok(template_value)
Ok(template_value.to_value())
}
#[napi(ts_return_type = "unknown")]