Skip to content

Commit

Permalink
Upgrade rkyv from 0.7 to 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
dialogflowchatbot committed Sep 14, 2024
1 parent da7e18e commit 423ea51
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 42 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/flow/rt/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static NUMBER_REGEX: LazyLock<Regex> =
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,
Expand Down
8 changes: 4 additions & 4 deletions src/flow/rt/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions src/flow/rt/convertor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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::<rkyv::rancor::Error>(&r).unwrap();
// let mut bytes = rkyv::to_bytes::<_, 256>(&node).unwrap();
// bytes.push(RuntimeNodeTypeId::TextNode as u8);
nodes.push((n.node_id.clone(), bytes));
Expand All @@ -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::<rkyv::rancor::Error>(&r).unwrap();
nodes.push((n.node_id.clone(), bytes));
}
Node::ConditionNode(n) => {
Expand All @@ -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::<rkyv::rancor::Error>(&r).unwrap();
// bytes.push(RuntimeNodeTypeId::GotoAnotherNode as u8);
nodes.push((node_id, bytes));
} else {
Expand All @@ -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::<rkyv::rancor::Error>(&r).unwrap();
// bytes.push(RuntimeNodeTypeId::ConditionNode as u8);
nodes.push((node_id, bytes));
}
Expand Down Expand Up @@ -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::<rkyv::rancor::Error>(&r).unwrap();
// bytes.push(RuntimeNodeTypeId::CollectNode as u8);
nodes.push((n.node_id.clone(), bytes));
}
Expand All @@ -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::<rkyv::rancor::Error>(&r).unwrap();
// bytes.push(RuntimeNodeTypeId::CollectNode as u8);
nodes.push((n.node_id.clone(), bytes));
}
Expand Down Expand Up @@ -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::<rkyv::rancor::Error>(&r).unwrap();
// bytes.push(RuntimeNodeTypeId::CollectNode as u8);
nodes.push((n.node_id.clone(), bytes));
}
Expand All @@ -308,15 +308,15 @@ 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::<rkyv::rancor::Error>(&r).unwrap();
nodes.push((n.node_id.clone(), bytes));
}
NextActionType::GotoSubFlow => {
let node = GotoAnotherNode {
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::<rkyv::rancor::Error>(&r).unwrap();
// bytes.push(RuntimeNodeTypeId::GotoAnotherNode as u8);
nodes.push((n.node_id.clone(), bytes));
}
Expand All @@ -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::<rkyv::rancor::Error>(&r).unwrap();
if n.ending_text.is_empty() {
nodes.push((n.node_id.clone(), ter_bytes));
} else {
Expand All @@ -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::<rkyv::rancor::Error>(&r).unwrap();
nodes.push((n.node_id.clone(), bytes));
nodes.push((end_node_id, ter_bytes));
}
Expand Down
2 changes: 1 addition & 1 deletion src/flow/rt/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/flow/rt/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 21 additions & 20 deletions src/flow/rt/node.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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<String>,
Expand Down Expand Up @@ -364,23 +364,23 @@ 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),
MaxChatTimes(u8),
}

#[derive(Archive, Clone, Deserialize, Serialize, serde::Deserialize)]
#[archive(compare(PartialEq), check_bytes)]
#[rkyv(compare(PartialEq))]
pub(crate) enum LlmChatNodeWhenTimeoutThen {
GotoAnotherNode,
ResponseAlternateText(String),
DoNothing,
}

#[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,
Expand Down Expand Up @@ -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::<rkyv::rancor::Error>(&r).unwrap();
ctx.node = Some(bytes.into_vec());
if self.streaming {
let r = super::facade::get_sender(&req.session_id);
Expand Down Expand Up @@ -538,9 +538,10 @@ impl RuntimeNode for LlmChatNode {
}

pub(crate) fn deser_node(bytes: &[u8]) -> Result<RuntimeNnodeEnum> {
let mut vec = rkyv::AlignedVec::new();
vec.extend_from_slice(bytes);
let archived = rkyv::check_archived_root::<RuntimeNnodeEnum>(&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::<RuntimeNnodeEnum, rkyv::rancor::Error>(&v).unwrap();
// let archived = rkyv::access::<ArchivedRuntimeNnodeEnum, rkyv::rancor::Error>(bytes).unwrap();
// let deserialized = rkyv::deserialize::<RuntimeNnodeEnum, rkyv::rancor::Error>(archived).unwrap();
return Ok(r);
}

0 comments on commit 423ea51

Please sign in to comment.