diff --git a/Cargo.toml b/Cargo.toml index 3f5c695..4623413 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,13 +41,13 @@ itoa = "1.0" # jieba-rs = "0.6.7" # lancedb = "0.4" oasysdb = "0.7.3" -# once_cell = "1.19" +# once_cell = "1.20" #ort = { version = "=2.0.0-rc.0", default-features = false } redb = "2.1" regex = "1.10" reqwest = { version = "0.12", features = ["stream"] } -rkyv = {version = "0.7", features = ["validation"]} -scru128 = "3.0" +rkyv = {version = "0.8", features = ["aligned", "alloc", "bytecheck"]} +scru128 = "3.1" serde = { version = "1", features = ["derive"] } serde_json = "1.0" scraper = "0.20" diff --git a/src/flow/rt/collector.rs b/src/flow/rt/collector.rs index 0104dcc..363af36 100644 --- a/src/flow/rt/collector.rs +++ b/src/flow/rt/collector.rs @@ -7,7 +7,7 @@ static NUMBER_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"[1-9]([\d]+)?(.[\d]+)?").unwrap()); #[derive(Clone, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) enum CollectType { UserInput, Number, diff --git a/src/flow/rt/condition.rs b/src/flow/rt/condition.rs index 286f148..07af969 100644 --- a/src/flow/rt/condition.rs +++ b/src/flow/rt/condition.rs @@ -12,7 +12,7 @@ use crate::variable::dto::VariableType; #[derive( Clone, Copy, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, )] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) enum ConditionType { UserInput, UserIntent, @@ -24,7 +24,7 @@ pub(crate) enum ConditionType { #[derive( Clone, Copy, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, )] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) enum CompareType { HasValue, DoesNotHaveValue, @@ -43,7 +43,7 @@ pub(crate) enum CompareType { #[derive( Copy, Clone, Debug, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, )] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) enum TargetDataVariant { Const, Variable, @@ -90,7 +90,7 @@ macro_rules! compare_numbers { } #[derive(Clone, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct ConditionData { pub(crate) condition_type: ConditionType, pub(crate) compare_type: CompareType, diff --git a/src/flow/rt/convertor.rs b/src/flow/rt/convertor.rs index 77621c6..a03fe64 100644 --- a/src/flow/rt/convertor.rs +++ b/src/flow/rt/convertor.rs @@ -150,7 +150,7 @@ fn convert_subflow(mainflow_id: &str, flow_idx: usize, f: &SubFlowDetail) -> Res } fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { - let mut nodes: Vec<(String, rkyv::AlignedVec)> = Vec::with_capacity(32); + let mut nodes: Vec<(String, rkyv::util::AlignedVec)> = Vec::with_capacity(32); match node { Node::DialogNode(n) => { let node = TextNode { @@ -161,7 +161,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { }; // println!("Dialog {} {}", &n.node_id, &node.next_node_id); let r = RuntimeNnodeEnum::TextNode(node); - let bytes = rkyv::to_bytes::<_, 256>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); // let mut bytes = rkyv::to_bytes::<_, 256>(&node).unwrap(); // bytes.push(RuntimeNodeTypeId::TextNode as u8); nodes.push((n.node_id.clone(), bytes)); @@ -179,7 +179,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { next_node_id: n.branches[0].target_node_id.clone(), }; let r = RuntimeNnodeEnum::LlmChatNode(node); - let bytes = rkyv::to_bytes::<_, 512>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); nodes.push((n.node_id.clone(), bytes)); } Node::ConditionNode(n) => { @@ -197,7 +197,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { next_node_id: b.target_node_id.clone(), }; let r = RuntimeNnodeEnum::GotoAnotherNode(node); - let bytes = rkyv::to_bytes::<_, 64>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); // bytes.push(RuntimeNodeTypeId::GotoAnotherNode as u8); nodes.push((node_id, bytes)); } else { @@ -223,7 +223,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { conditions, }; let r = RuntimeNnodeEnum::ConditionNode(node); - let bytes = rkyv::to_bytes::<_, 256>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); // bytes.push(RuntimeNodeTypeId::ConditionNode as u8); nodes.push((node_id, bytes)); } @@ -255,7 +255,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { failed_node_id: failed_node_id, }; let r = RuntimeNnodeEnum::CollectNode(node); - let bytes = rkyv::to_bytes::<_, 128>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); // bytes.push(RuntimeNodeTypeId::CollectNode as u8); nodes.push((n.node_id.clone(), bytes)); } @@ -265,7 +265,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { http_api_id: n.http_api_id.clone(), }; let r = RuntimeNnodeEnum::ExternalHttpCallNode(node); - let bytes = rkyv::to_bytes::<_, 128>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); // bytes.push(RuntimeNodeTypeId::CollectNode as u8); nodes.push((n.node_id.clone(), bytes)); } @@ -295,7 +295,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { goto_node_id: goto_node_id, }; let r = RuntimeNnodeEnum::SendEmailNode(node); - let bytes = rkyv::to_bytes::<_, 128>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); // bytes.push(RuntimeNodeTypeId::CollectNode as u8); nodes.push((n.node_id.clone(), bytes)); } @@ -308,7 +308,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { next_node_id: n.goto_subflow_id.clone(), }; let r = RuntimeNnodeEnum::GotoMainFlowNode(node); - let bytes = rkyv::to_bytes::<_, 64>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); nodes.push((n.node_id.clone(), bytes)); } NextActionType::GotoSubFlow => { @@ -316,7 +316,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { next_node_id: n.goto_subflow_id.clone(), }; let r = RuntimeNnodeEnum::GotoAnotherNode(node); - let bytes = rkyv::to_bytes::<_, 64>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); // bytes.push(RuntimeNodeTypeId::GotoAnotherNode as u8); nodes.push((n.node_id.clone(), bytes)); } @@ -328,7 +328,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { // log::info!("EndNode {}", &n.node_id); let node = TerminateNode {}; let r = RuntimeNnodeEnum::TerminateNode(node); - let ter_bytes = rkyv::to_bytes::<_, 64>(&r).unwrap(); + let ter_bytes = rkyv::to_bytes::(&r).unwrap(); if n.ending_text.is_empty() { nodes.push((n.node_id.clone(), ter_bytes)); } else { @@ -341,7 +341,7 @@ fn convert_node(main_flow_id: &str, node: &mut Node) -> Result<()> { next_node_id: end_node_id.clone(), }; let r = RuntimeNnodeEnum::TextNode(node); - let bytes = rkyv::to_bytes::<_, 256>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); nodes.push((n.node_id.clone(), bytes)); nodes.push((end_node_id, ter_bytes)); } diff --git a/src/flow/rt/crud.rs b/src/flow/rt/crud.rs index d1481f0..10d6af4 100644 --- a/src/flow/rt/crud.rs +++ b/src/flow/rt/crud.rs @@ -26,7 +26,7 @@ pub(crate) fn get_runtime_node( pub(crate) fn save_runtime_nodes( main_flow_id: &str, - nodes: Vec<(String, rkyv::AlignedVec)>, + nodes: Vec<(String, rkyv::util::AlignedVec)>, ) -> Result<()> { let table_name = get_table_name(main_flow_id); let table: TableDefinition<&str, &[u8]> = TableDefinition::new(&table_name); diff --git a/src/flow/rt/dto.rs b/src/flow/rt/dto.rs index a17b703..1409b9e 100644 --- a/src/flow/rt/dto.rs +++ b/src/flow/rt/dto.rs @@ -36,7 +36,7 @@ pub(crate) struct CollectData { } #[derive(Clone, Deserialize, Serialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) enum AnswerType { TextPlain, TextHtml, diff --git a/src/flow/rt/node.rs b/src/flow/rt/node.rs index 1392ea2..22dc6ba 100644 --- a/src/flow/rt/node.rs +++ b/src/flow/rt/node.rs @@ -1,9 +1,9 @@ use core::time::Duration; -use std::ops::DerefMut; +// use std::ops::DerefMut; use enum_dispatch::enum_dispatch; use lettre::transport::smtp::PoolConfig; -use rkyv::{Archive, Deserialize, Serialize}; +use rkyv::{util::AlignedVec, Archive, Deserialize, Serialize}; use super::condition::ConditionData; use super::context::Context; @@ -31,7 +31,7 @@ const VAR_WRAP_SYMBOL: char = '`'; #[enum_dispatch] #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) enum RuntimeNnodeEnum { TextNode, ConditionNode, @@ -88,7 +88,7 @@ fn add_next_node(ctx: &mut Context, next_node_id: &str) { } #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct TextNode { pub(super) text: String, pub(crate) text_type: AnswerType, @@ -115,7 +115,7 @@ impl RuntimeNode for TextNode { } #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct GotoMainFlowNode { pub(super) main_flow_id: String, pub(super) next_node_id: String, @@ -132,7 +132,7 @@ impl RuntimeNode for GotoMainFlowNode { } #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct GotoAnotherNode { pub(super) next_node_id: String, } @@ -146,7 +146,7 @@ impl RuntimeNode for GotoAnotherNode { } #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct CollectNode { pub(super) var_name: String, pub(super) collect_type: collector::CollectType, @@ -176,7 +176,7 @@ impl RuntimeNode for CollectNode { } #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct ConditionNode { pub(super) next_node_id: String, pub(super) goto_node_id: String, @@ -205,7 +205,7 @@ impl RuntimeNode for ConditionNode { } #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct TerminateNode {} impl RuntimeNode for TerminateNode { @@ -217,7 +217,7 @@ impl RuntimeNode for TerminateNode { } #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct ExternalHttpCallNode { pub(super) next_node_id: String, pub(super) http_api_id: String, @@ -254,7 +254,7 @@ impl RuntimeNode for ExternalHttpCallNode { } #[derive(Archive, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct SendEmailNode { pub(super) from: String, pub(super) to_recipients: Vec, @@ -364,7 +364,7 @@ impl RuntimeNode for SendEmailNode { } #[derive(Archive, Clone, Deserialize, Serialize, serde::Deserialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) enum LlmChatNodeExitCondition { Intent(String), SpecialInputs(String), @@ -372,7 +372,7 @@ pub(crate) enum LlmChatNodeExitCondition { } #[derive(Archive, Clone, Deserialize, Serialize, serde::Deserialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) enum LlmChatNodeWhenTimeoutThen { GotoAnotherNode, ResponseAlternateText(String), @@ -380,7 +380,7 @@ pub(crate) enum LlmChatNodeWhenTimeoutThen { } #[derive(Archive, Clone, Deserialize, Serialize)] -#[archive(compare(PartialEq), check_bytes)] +#[rkyv(compare(PartialEq))] pub(crate) struct LlmChatNode { pub(super) prompt: String, pub(super) context_len: u8, @@ -420,7 +420,7 @@ impl RuntimeNode for LlmChatNode { } } let r = RuntimeNnodeEnum::LlmChatNode(self.clone()); - let bytes = rkyv::to_bytes::<_, 256>(&r).unwrap(); + let bytes = rkyv::to_bytes::(&r).unwrap(); ctx.node = Some(bytes.into_vec()); if self.streaming { let r = super::facade::get_sender(&req.session_id); @@ -538,9 +538,10 @@ impl RuntimeNode for LlmChatNode { } pub(crate) fn deser_node(bytes: &[u8]) -> Result { - let mut vec = rkyv::AlignedVec::new(); - vec.extend_from_slice(bytes); - let archived = rkyv::check_archived_root::(&vec).unwrap(); - let deserialized: RuntimeNnodeEnum = archived.deserialize(&mut rkyv::Infallible).unwrap(); - return Ok(deserialized); + let mut v = AlignedVec::<256>::with_capacity(bytes.len()); + v.extend_from_slice(bytes); + let r = rkyv::from_bytes::(&v).unwrap(); + // let archived = rkyv::access::(bytes).unwrap(); + // let deserialized = rkyv::deserialize::(archived).unwrap(); + return Ok(r); }