Initial commit

This commit is contained in:
Slavi Pantaleev
2024-09-12 13:44:06 +03:00
commit 946aa9d9e9
220 changed files with 26033 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
use crate::strings;
use crate::{entity::MessageContext, Bot};
use mxlink::MessageResponseType;
use super::controller_type::{
ConfigControllerType, ConfigSettingRelatedControllerType, SettingsStorageSource,
};
mod speech_to_text;
mod text_generation;
mod text_to_speech;
pub async fn dispatch_controller(
handler: &ConfigControllerType,
message_context: &MessageContext,
bot: &Bot,
) -> anyhow::Result<()> {
// Anyone can access Help and Status.
// Settings-related access checks are done in dispatch_config_related_handler().
match handler {
ConfigControllerType::Help => super::help::handle(bot, message_context).await,
ConfigControllerType::Status => super::status::handle(bot, message_context).await,
ConfigControllerType::SettingsRelated(config_type, config_related_handler) => {
dispatch_config_related_handler(
config_type,
config_related_handler,
message_context,
bot,
)
.await
}
}
}
async fn dispatch_config_related_handler(
config_type: &SettingsStorageSource,
handler: &ConfigSettingRelatedControllerType,
message_context: &MessageContext,
bot: &Bot,
) -> anyhow::Result<()> {
if let SettingsStorageSource::Global = config_type {
if !message_context.sender_can_manage_global_config()? {
bot.messaging()
.send_error_markdown_no_fail(
message_context.room(),
strings::global_config::no_permissions_to_administrate(),
MessageResponseType::Reply(message_context.thread_info().root_event_id.clone()),
)
.await;
return Ok(());
}
};
let room_settings = match config_type {
SettingsStorageSource::Room => &message_context.room_config().settings,
SettingsStorageSource::Global => &message_context.global_config().fallback_room_settings,
};
match handler {
ConfigSettingRelatedControllerType::GetHandler(purpose) => match config_type {
SettingsStorageSource::Room => {
super::room_config::handler::handle_get(bot, message_context, *purpose).await
}
SettingsStorageSource::Global => {
super::global_config::handler::handle_get(bot, message_context, *purpose).await
}
},
ConfigSettingRelatedControllerType::SetHandler(purpose, agent_identifier) => {
match config_type {
SettingsStorageSource::Room => {
super::room_config::handler::handle_set(
bot,
bot.room_config_manager(),
message_context,
*purpose,
agent_identifier,
)
.await
}
SettingsStorageSource::Global => {
super::global_config::handler::handle_set(
bot,
bot.global_config_manager(),
message_context,
*purpose,
agent_identifier,
)
.await
}
}
}
ConfigSettingRelatedControllerType::TextGeneration(controller_type) => {
text_generation::dispatch(
controller_type,
message_context,
bot,
room_settings,
config_type,
)
.await
}
ConfigSettingRelatedControllerType::SpeechToText(controller_type) => {
speech_to_text::dispatch(
controller_type,
message_context,
bot,
room_settings,
config_type,
)
.await
}
ConfigSettingRelatedControllerType::TextToSpeech(controller_type) => {
text_to_speech::dispatch(
controller_type,
message_context,
bot,
room_settings,
config_type,
)
.await
}
}
}

View File

@@ -0,0 +1,78 @@
use crate::entity::roomconfig::{RoomSettings, SpeechToTextFlowType};
use crate::{entity::MessageContext, Bot};
use super::super::controller_type::{
ConfigSpeechToTextSettingRelatedControllerType, SettingsStorageSource,
};
use super::super::common::generic_setting::handle_get as setting_get;
use super::super::global_config::generic_setting::handle_set as global_setting_set;
use super::super::room_config::generic_setting::handle_set as room_setting_set;
pub(super) async fn dispatch(
handler: &ConfigSpeechToTextSettingRelatedControllerType,
message_context: &MessageContext,
bot: &Bot,
room_settings: &RoomSettings,
config_type: &SettingsStorageSource,
) -> anyhow::Result<()> {
match handler {
ConfigSpeechToTextSettingRelatedControllerType::GetFlowType => {
let value = &room_settings.speech_to_text.flow_type;
setting_get::<SpeechToTextFlowType>(bot, message_context, value).await
}
ConfigSpeechToTextSettingRelatedControllerType::SetFlowType(value) => {
let value = value.to_owned();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.speech_to_text.flow_type = value;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<SpeechToTextFlowType>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
SettingsStorageSource::Global => {
global_setting_set::<SpeechToTextFlowType>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
}
}
ConfigSpeechToTextSettingRelatedControllerType::GetLanguage => {
let value = &room_settings.speech_to_text.language;
setting_get::<String>(bot, message_context, value).await
}
ConfigSpeechToTextSettingRelatedControllerType::SetLanguage(value) => {
let value = value.to_owned();
let value_setter = value.clone();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.speech_to_text.language = value_setter;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<String>(bot, message_context, &value, setter_callback).await
}
SettingsStorageSource::Global => {
global_setting_set::<String>(bot, message_context, &value, setter_callback)
.await
}
}
}
}
}

View File

@@ -0,0 +1,155 @@
use crate::entity::roomconfig::{
RoomSettings, TextGenerationAutoUsage, TextGenerationPrefixRequirementType,
};
use crate::{entity::MessageContext, Bot};
use super::super::controller_type::{
ConfigTextGenerationSettingRelatedControllerType, SettingsStorageSource,
};
use super::super::common::generic_setting::handle_get as setting_get;
use super::super::global_config::generic_setting::handle_set as global_setting_set;
use super::super::room_config::generic_setting::handle_set as room_setting_set;
pub(super) async fn dispatch(
handler: &ConfigTextGenerationSettingRelatedControllerType,
message_context: &MessageContext,
bot: &Bot,
room_settings: &RoomSettings,
config_type: &SettingsStorageSource,
) -> anyhow::Result<()> {
match handler {
ConfigTextGenerationSettingRelatedControllerType::GetContextManagementEnabled => {
let value = &room_settings.text_generation.context_management_enabled;
setting_get::<bool>(bot, message_context, value).await
}
ConfigTextGenerationSettingRelatedControllerType::SetContextManagementEnabled(value) => {
let value = value.to_owned();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_generation.context_management_enabled = value;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<bool>(bot, message_context, &value, setter_callback).await
}
SettingsStorageSource::Global => {
global_setting_set::<bool>(bot, message_context, &value, setter_callback).await
}
}
}
ConfigTextGenerationSettingRelatedControllerType::GetPrefixRequirementType => {
let value = &room_settings.text_generation.prefix_requirement_type;
setting_get::<TextGenerationPrefixRequirementType>(bot, message_context, value).await
}
ConfigTextGenerationSettingRelatedControllerType::SetPrefixRequirementType(value) => {
let value = value.to_owned();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_generation.prefix_requirement_type = value;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<TextGenerationPrefixRequirementType>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
SettingsStorageSource::Global => {
global_setting_set::<TextGenerationPrefixRequirementType>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
}
}
ConfigTextGenerationSettingRelatedControllerType::GetAutoUsage => {
let value = &room_settings.text_generation.auto_usage;
setting_get::<TextGenerationAutoUsage>(bot, message_context, value).await
}
ConfigTextGenerationSettingRelatedControllerType::SetAutoUsage(value) => {
let value = value.to_owned();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_generation.auto_usage = value;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<TextGenerationAutoUsage>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
SettingsStorageSource::Global => {
global_setting_set::<TextGenerationAutoUsage>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
}
}
ConfigTextGenerationSettingRelatedControllerType::GetPromptOverride => {
let value = &room_settings.text_generation.prompt_override;
setting_get::<String>(bot, message_context, value).await
}
ConfigTextGenerationSettingRelatedControllerType::SetPromptOverride(value) => {
let value = value.to_owned();
let value_setter = value.clone();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_generation.prompt_override = value_setter;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<String>(bot, message_context, &value, setter_callback).await
}
SettingsStorageSource::Global => {
global_setting_set::<String>(bot, message_context, &value, setter_callback)
.await
}
}
}
ConfigTextGenerationSettingRelatedControllerType::GetTemperatureOverride => {
let value = &room_settings.text_generation.temperature_override;
setting_get::<f32>(bot, message_context, value).await
}
ConfigTextGenerationSettingRelatedControllerType::SetTemperatureOverride(value) => {
let value = value.to_owned();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_generation.temperature_override = value;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<f32>(bot, message_context, &value, setter_callback).await
}
SettingsStorageSource::Global => {
global_setting_set::<f32>(bot, message_context, &value, setter_callback).await
}
}
}
}
}

View File

@@ -0,0 +1,134 @@
use crate::entity::roomconfig::{
RoomSettings, TextToSpeechBotMessagesFlowType, TextToSpeechUserMessagesFlowType,
};
use crate::{entity::MessageContext, Bot};
use super::super::controller_type::{
ConfigTextToSpeechSettingRelatedControllerType, SettingsStorageSource,
};
use super::super::common::generic_setting::handle_get as setting_get;
use super::super::global_config::generic_setting::handle_set as global_setting_set;
use super::super::room_config::generic_setting::handle_set as room_setting_set;
pub(super) async fn dispatch(
handler: &ConfigTextToSpeechSettingRelatedControllerType,
message_context: &MessageContext,
bot: &Bot,
room_settings: &RoomSettings,
config_type: &SettingsStorageSource,
) -> anyhow::Result<()> {
match handler {
ConfigTextToSpeechSettingRelatedControllerType::GetBotMessagesFlowType => {
let value = &room_settings.text_to_speech.bot_msgs_flow_type;
setting_get::<TextToSpeechBotMessagesFlowType>(bot, message_context, value).await
}
ConfigTextToSpeechSettingRelatedControllerType::SetBotMessagesFlowType(value) => {
let value = value.to_owned();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_to_speech.bot_msgs_flow_type = value;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<TextToSpeechBotMessagesFlowType>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
SettingsStorageSource::Global => {
global_setting_set::<TextToSpeechBotMessagesFlowType>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
}
}
ConfigTextToSpeechSettingRelatedControllerType::GetUserMessagesFlowType => {
let value = &room_settings.text_to_speech.user_msgs_flow_type;
setting_get::<TextToSpeechUserMessagesFlowType>(bot, message_context, value).await
}
ConfigTextToSpeechSettingRelatedControllerType::SetUserMessagesFlowType(value) => {
let value = value.to_owned();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_to_speech.user_msgs_flow_type = value;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<TextToSpeechUserMessagesFlowType>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
SettingsStorageSource::Global => {
global_setting_set::<TextToSpeechUserMessagesFlowType>(
bot,
message_context,
&value,
setter_callback,
)
.await
}
}
}
ConfigTextToSpeechSettingRelatedControllerType::GetSpeedOverride => {
let value = &room_settings.text_to_speech.speed_override;
setting_get::<f32>(bot, message_context, value).await
}
ConfigTextToSpeechSettingRelatedControllerType::SetSpeedOverride(value) => {
let value = value.to_owned();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_to_speech.speed_override = value;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<f32>(bot, message_context, &value, setter_callback).await
}
SettingsStorageSource::Global => {
global_setting_set::<f32>(bot, message_context, &value, setter_callback).await
}
}
}
ConfigTextToSpeechSettingRelatedControllerType::GetVoiceOverride => {
let value = &room_settings.text_to_speech.voice_override;
setting_get::<String>(bot, message_context, value).await
}
ConfigTextToSpeechSettingRelatedControllerType::SetVoiceOverride(value) => {
let value = value.to_owned();
let value_setter = value.clone();
let setter_callback = Box::new(move |room_settings: &mut RoomSettings| {
room_settings.text_to_speech.voice_override = value_setter;
});
match config_type {
SettingsStorageSource::Room => {
room_setting_set::<String>(bot, message_context, &value, setter_callback).await
}
SettingsStorageSource::Global => {
global_setting_set::<String>(bot, message_context, &value, setter_callback)
.await
}
}
}
}
}