From 1711270c9f27d819a7aa9d1195745f1247a94f3b Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Tue, 8 Apr 2025 00:07:37 +0800 Subject: [PATCH] =?UTF-8?q?dev=20=E5=B0=B1=E6=98=AF=20dev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/src/py/class.rs | 58 ++++++++++++++++++++++++++++++++++-------- ica-rs/src/py/mod.rs | 8 +++++- readme.md | 7 ++--- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/ica-rs/src/py/class.rs b/ica-rs/src/py/class.rs index a7f4583..03bbf35 100644 --- a/ica-rs/src/py/class.rs +++ b/ica-rs/src/py/class.rs @@ -1,24 +1,37 @@ pub mod ica; pub mod tailchat; +use std::collections::HashMap; + use pyo3::{ - pyclass, pymethods, - types::{PyBool, PyString}, - Bound, IntoPyObject, PyAny, PyRef, + pyclass, pymethods, pymodule, + types::{PyBool, PyModule, PyModuleMethods, PyString}, + Bound, IntoPyObject, PyAny, PyRef, PyResult, }; use toml::Value as TomlValue; +use tracing::{event, Level}; + +#[derive(Debug, Clone)] +pub enum ConfigItem { + String(String), + Integer(i64), + Float(f64), + Boolean(bool), + Array(Vec), +} #[derive(Clone)] #[pyclass] -#[pyo3(name = "ConfigRequest")] -pub struct ConfigRequestPy { - pub path: String, +#[pyo3(name = "ConfigItem")] +pub struct ConfigItemPy { + pub item: ConfigItem, } -#[pymethods] -impl ConfigRequestPy { - #[new] - pub fn py_new(path: String) -> Self { Self { path } } +#[derive(Clone)] +#[pyclass] +#[pyo3(name = "ConfigStorage")] +pub struct ConfigStoragePy { + pub keys: HashMap, } #[derive(Clone)] @@ -61,3 +74,28 @@ impl ConfigDataPy { impl ConfigDataPy { pub fn new(data: TomlValue) -> Self { Self { data } } } + +#[pymodule] +#[pyo3(name = "shenbot_api")] +fn rs_api_module(m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + m.add_class::()?; + Ok(()) +} + +/// 在 python 初始化之前注册所有需要的类 +/// +/// WARNING: 这个函数需要在 Python 初始化之前调用,否则会导致报错 +/// +/// (pyo3 提供的宏会检查一遍, 不过我这里就直接用原始形式了) +pub fn regist_class() { + event!(Level::INFO, "向 Python 注册 Rust 侧模块/函数"); + unsafe { + pyo3::ffi::PyImport_AppendInittab( + rs_api_module::__PYO3_NAME.as_ptr(), + Some(rs_api_module::__pyo3_init), + ); + } + + event!(Level::INFO, "注册完成"); +} diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 839db97..b56483b 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -521,7 +521,7 @@ fn init_py_with_env_path(path: &str) { event!(Level::INFO, "根据配置初始化 python 完成"); } pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_EXIT => { - event!(Level::ERROR, "初始化 python 时发生错误: EXIT"); + event!(Level::ERROR, "不对啊, 怎么刚刚初始化 Python 就 EXIT 了"); } pyo3::ffi::_PyStatus_TYPE::_PyStatus_TYPE_ERROR => { event!(Level::ERROR, "初始化 python 时发生错误: ERROR"); @@ -537,9 +537,15 @@ pub fn init_py() { let span = span!(Level::INFO, "py init"); let _enter = span.enter(); + event!(Level::INFO, "开始初始化 python"); + + // 注册东西 + class::regist_class(); + let plugin_path = MainStatus::global_config().py().plugin_path; let cli_args = std::env::args().collect::>(); + if cli_args.contains(&"-env".to_string()) { let env_path = cli_args.iter().find(|&arg| arg != "-env").expect("未找到 -env 参数的值"); event!(Level::INFO, "找到 -env 参数: {} 正在初始化", env_path); diff --git a/readme.md b/readme.md index 80d0cd5..cc12126 100644 --- a/readme.md +++ b/readme.md @@ -1,9 +1,6 @@ # icalingua bot -这是一个基于 icalingua docker 版的 bot - -> 出于某个企鹅, 和保护 作者 和 原项目 ( ica ) 的原因 \ -> 功能请自行理解 +这是一个基于 icalingua-bridge 的 bot [插件市场(确信)](https://github.com/shenjackyuanjie/shenbot-plugins) @@ -24,7 +21,7 @@ uv venv - 启动 icalingua 后端 ```bash -# 用你自己的方法启动你的 icalingua 后端 +# 用你自己的方法启动你的 icalingua-bridge # 例如 docker start icalingua docker-compose up -d