This commit is contained in:
2026-05-14 00:03:53 +03:00
parent 5fd4b5e255
commit 21556aeb04
8 changed files with 82 additions and 234 deletions

View File

@@ -58,24 +58,17 @@ impl Display for PluginRequestType {
#[async_trait]
pub(crate) trait PluginConnection: Send + Sync + Debug {
async fn initialize_plugin(
&self,
config: String,
) -> Result<proto::PluginInitializeResponse, PluginConnectionError>;
async fn initialize_plugin(&self, config: String)
-> Result<proto::PluginInitializeResponse, PluginConnectionError>;
async fn request_plugin_command_list(
&self,
) -> Result<proto::PluginCommandListResponse, PluginConnectionError>;
async fn request_plugin_command_list(&self) -> Result<proto::PluginCommandListResponse, PluginConnectionError>;
async fn execute_command(
self: Arc<Self>,
command_id: String,
issuer_id: String,
argv: Vec<String>,
) -> Result<
mpsc::Receiver<Result<proto::CommandReply, PluginConnectionError>>,
PluginConnectionError,
>;
) -> Result<mpsc::Receiver<Result<proto::CommandReply, PluginConnectionError>>, PluginConnectionError>;
}
#[derive(Debug)]
@@ -138,8 +131,7 @@ pub(crate) async fn try_load_plugin(
cmd.env("DCRCBOT_PLUGIN_TRANSPORT", "stdio");
let plugin_process = cmd.spawn().context("Failed to start the plugin")?;
let plugin =
stdio::initialize_stdio_plugin(plugin_process, unique_name.clone(), config_json).await?;
let plugin = stdio::initialize_stdio_plugin(plugin_process, unique_name.clone(), config_json).await?;
let mut ctx_lock = ctx.lock().await;
for cmd in plugin.lock().await.commands.iter().cloned() {
@@ -147,12 +139,8 @@ pub(crate) async fn try_load_plugin(
if ctx_lock.plugin_commands.contains_key(&cmd.name) {
bail!("duplicate command specification");
}
ctx_lock
.plugin_commands
.insert(cmd.name.clone(), Arc::clone(&cmd));
ctx_lock
.plugin_cmd_aliases
.insert(cmd.name.clone(), Arc::clone(&cmd));
ctx_lock.plugin_commands.insert(cmd.name.clone(), Arc::clone(&cmd));
ctx_lock.plugin_cmd_aliases.insert(cmd.name.clone(), Arc::clone(&cmd));
for alias in cmd.aliases.iter() {
log::debug!(
"adding command alias /{} -> /{} of plugin {}",
@@ -160,9 +148,7 @@ pub(crate) async fn try_load_plugin(
&cmd.name,
&unique_name
);
ctx_lock
.plugin_cmd_aliases
.insert(alias.to_owned(), Arc::clone(&cmd));
ctx_lock.plugin_cmd_aliases.insert(alias.to_owned(), Arc::clone(&cmd));
}
}

View File

@@ -10,9 +10,7 @@ use tokio::{
};
use crate::{
plugin::{
LoadedPlugin, PluginCommand, PluginConnection, PluginConnectionError, PluginRequestType,
},
plugin::{LoadedPlugin, PluginCommand, PluginConnection, PluginConnectionError, PluginRequestType},
proto,
};
@@ -85,19 +83,16 @@ impl StdioPluginConnection {
async fn stdout_reader_loop(self: Arc<Self>) {
loop {
let frame =
match Self::read_length_delimited(self.buffered_stdout.lock().await.deref_mut())
.await
{
Ok(frame) => frame,
Err(e) => {
log::error!(
"Error while reading STDOUT of stdio plugin {}: {e}",
&self.plugin.lock().await.plugin_id
);
break;
}
};
let frame = match Self::read_length_delimited(self.buffered_stdout.lock().await.deref_mut()).await {
Ok(frame) => frame,
Err(e) => {
log::error!(
"Error while reading STDOUT of stdio plugin {}: {e}",
&self.plugin.lock().await.plugin_id
);
break;
}
};
let response = match proto::Response::decode(frame.as_slice()) {
Ok(response) => response,
Err(_) => {
@@ -133,9 +128,7 @@ impl StdioPluginConnection {
self.pending_requests.lock().await.clear();
}
async fn read_length_delimited<R>(
reader: &mut R,
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>>
async fn read_length_delimited<R>(reader: &mut R) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>>
where
R: AsyncRead + Unpin,
{