spring_ai_rs/ai_interface/callback/
lua.rs

1use std::ffi::CString;
2
3use spring_ai_sys::{COMMAND_TO_ID_ENGINE, MAX_RESPONSE_SIZE};
4
5use crate::ai_interface::{
6    callback::{
7        command::{
8            command_data::{
9                lua::{CallLuaRulesCommandData, CallLuaUICommandData},
10                CommandData,
11            },
12            command_topic::CommandTopic,
13        },
14        engine::handle_command,
15    },
16    AIInterface,
17};
18
19pub struct Lua {
20    pub ai_id: i32,
21}
22
23impl AIInterface {
24    pub fn lua(&self) -> Lua {
25        Lua { ai_id: self.ai_id }
26    }
27}
28
29impl Lua {
30    pub fn call_rules(&self, in_data: &str) -> Result<String, String> {
31        let in_data_cstring = CString::new(in_data).map_err(|e| format!("{e}"))?;
32
33        let mut command_data = CallLuaRulesCommandData {
34            in_data: in_data_cstring,
35            in_size: in_data.len() as i32,
36            out_data: CString::new(String::with_capacity(MAX_RESPONSE_SIZE as usize))
37                .map_err(|e| format!("{e}"))?,
38        };
39
40        handle_command(
41            self.ai_id,
42            COMMAND_TO_ID_ENGINE,
43            -1,
44            CommandTopic::CallLuaRules.into(),
45            &mut command_data.c_data(),
46        )?;
47
48        Ok(command_data.out_data.to_string_lossy().to_string())
49    }
50    pub fn call_ui(&self, in_data: &str) -> Result<String, String> {
51        let in_data_cstring = CString::new(in_data).map_err(|e| format!("{e}"))?;
52
53        let mut command_data = CallLuaUICommandData {
54            in_data: in_data_cstring,
55            in_size: in_data.len() as i32,
56            out_data: CString::new(String::with_capacity(MAX_RESPONSE_SIZE as usize))
57                .map_err(|e| format!("{e}"))?,
58        };
59
60        handle_command(
61            self.ai_id,
62            COMMAND_TO_ID_ENGINE,
63            -1,
64            CommandTopic::CallLuaUI.into(),
65            &mut command_data.c_data(),
66        )?;
67
68        Ok(command_data.out_data.to_string_lossy().to_string())
69    }
70}