zen_engine/handler/function/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::rc::Rc;
use std::time::Duration;

use ::serde::{Deserialize, Serialize};
use anyhow::anyhow;
use rquickjs::{async_with, CatchResultExt, Object};
use serde_json::json;

use crate::handler::function::error::FunctionResult;
use crate::handler::function::function::{Function, HandlerResponse};
use crate::handler::function::serde::JsValue;
use crate::handler::node::{NodeRequest, NodeResponse, NodeResult};
use crate::model::{DecisionNodeKind, FunctionNodeContent};

pub(crate) mod error;
pub(crate) mod function;
pub(crate) mod listener;
pub(crate) mod module;
pub(crate) mod serde;

#[derive(Serialize, Deserialize)]
pub struct FunctionResponse {
    performance: String,
    data: Option<HandlerResponse>,
}

pub struct FunctionHandler {
    function: Rc<Function>,
    trace: bool,
    iteration: u8,
    max_depth: u8,
}

static MAX_DURATION: Duration = Duration::from_millis(5_000);

impl FunctionHandler {
    pub fn new(function: Rc<Function>, trace: bool, iteration: u8, max_depth: u8) -> Self {
        Self {
            function,
            trace,
            iteration,
            max_depth,
        }
    }

    pub async fn handle(&self, request: &NodeRequest<'_>) -> NodeResult {
        let content = match &request.node.kind {
            DecisionNodeKind::FunctionNode { content } => match content {
                FunctionNodeContent::Version2(content) => Ok(content),
                _ => Err(anyhow!("Unexpected node type")),
            },
            _ => Err(anyhow!("Unexpected node type")),
        }?;
        let start = std::time::Instant::now();

        let module_name = self
            .function
            .suggest_module_name(request.node.id.as_str(), request.node.name.as_str());
        let interrupt_handler = Box::new(move || start.elapsed() > MAX_DURATION);
        self.function
            .runtime()
            .set_interrupt_handler(Some(interrupt_handler))
            .await;

        self.attach_globals()
            .await
            .map_err(|e| anyhow!(e.to_string()))?;

        self.function
            .register_module(&module_name, content.source.as_str())
            .await
            .map_err(|e| anyhow!(e.to_string()))?;

        let response = self
            .function
            .call_handler(&module_name, JsValue(request.input.clone()))
            .await
            .map_err(|e| anyhow!(e.to_string()))?;

        self.function.runtime().set_interrupt_handler(None).await;

        Ok(NodeResponse {
            output: response.data,
            trace_data: self.trace.then(|| json!({ "log": response.logs })),
        })
    }

    async fn attach_globals(&self) -> FunctionResult {
        async_with!(self.function.context() => |ctx| {
            let config = Object::new(ctx.clone()).catch(&ctx)?;

            config.prop("iteration", self.iteration).catch(&ctx)?;
            config.prop("maxDepth", self.max_depth).catch(&ctx)?;
            config.prop("trace", self.trace).catch(&ctx)?;

            ctx.globals().set("config", config).catch(&ctx)?;

            Ok(())
        })
        .await
    }
}