消息渠道支持会话相关指令
This commit is contained in:
35
src-tauri/src/db/entities/chat_channel_sender_context.rs
Normal file
35
src-tauri/src/db/entities/chat_channel_sender_context.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "chat_channel_sender_context")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub channel_id: i32,
|
||||
pub sender_id: String,
|
||||
pub current_folder_id: Option<i32>,
|
||||
pub current_agent_type: Option<String>,
|
||||
pub current_conversation_id: Option<i32>,
|
||||
pub current_connection_id: Option<String>,
|
||||
pub auto_approve: bool,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::chat_channel::Entity",
|
||||
from = "Column::ChannelId",
|
||||
to = "super::chat_channel::Column::Id"
|
||||
)]
|
||||
ChatChannel,
|
||||
}
|
||||
|
||||
impl Related<super::chat_channel::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::ChatChannel.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
@@ -2,6 +2,7 @@ pub mod agent_setting;
|
||||
pub mod app_metadata;
|
||||
pub mod chat_channel;
|
||||
pub mod chat_channel_message_log;
|
||||
pub mod chat_channel_sender_context;
|
||||
pub mod conversation;
|
||||
pub mod folder;
|
||||
pub mod folder_command;
|
||||
|
||||
@@ -4,6 +4,7 @@ pub use super::agent_setting::Entity as AgentSetting;
|
||||
pub use super::app_metadata::Entity as AppMetadata;
|
||||
pub use super::chat_channel::Entity as ChatChannel;
|
||||
pub use super::chat_channel_message_log::Entity as ChatChannelMessageLog;
|
||||
pub use super::chat_channel_sender_context::Entity as ChatChannelSenderContext;
|
||||
pub use super::conversation::Entity as Conversation;
|
||||
pub use super::folder::Entity as Folder;
|
||||
pub use super::folder_command::Entity as FolderCommand;
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(ChatChannelSenderContext::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::ChannelId)
|
||||
.integer()
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::SenderId)
|
||||
.string()
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::CurrentFolderId)
|
||||
.integer()
|
||||
.null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::CurrentAgentType)
|
||||
.string()
|
||||
.null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::CurrentConversationId)
|
||||
.integer()
|
||||
.null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::CurrentConnectionId)
|
||||
.string()
|
||||
.null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::AutoApprove)
|
||||
.boolean()
|
||||
.not_null()
|
||||
.default(false),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::CreatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(ChatChannelSenderContext::UpdatedAt)
|
||||
.timestamp_with_time_zone()
|
||||
.not_null(),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_ccsc_channel_id")
|
||||
.from(
|
||||
ChatChannelSenderContext::Table,
|
||||
ChatChannelSenderContext::ChannelId,
|
||||
)
|
||||
.to(ChatChannel::Table, ChatChannel::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_ccsc_channel_sender")
|
||||
.table(ChatChannelSenderContext::Table)
|
||||
.col(ChatChannelSenderContext::ChannelId)
|
||||
.col(ChatChannelSenderContext::SenderId)
|
||||
.unique()
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(
|
||||
Table::drop()
|
||||
.table(ChatChannelSenderContext::Table)
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum ChatChannelSenderContext {
|
||||
Table,
|
||||
Id,
|
||||
ChannelId,
|
||||
SenderId,
|
||||
CurrentFolderId,
|
||||
CurrentAgentType,
|
||||
CurrentConversationId,
|
||||
CurrentConnectionId,
|
||||
AutoApprove,
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum ChatChannel {
|
||||
Table,
|
||||
Id,
|
||||
}
|
||||
@@ -6,6 +6,7 @@ mod m20260221_000001_folder_is_open;
|
||||
mod m20260226_000001_agent_setting;
|
||||
mod m20260227_000001_folder_parent_branch;
|
||||
mod m20260330_000001_chat_channel;
|
||||
mod m20260401_000001_chat_channel_sender_context;
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
@@ -18,6 +19,7 @@ impl MigratorTrait for Migrator {
|
||||
Box::new(m20260226_000001_agent_setting::Migration),
|
||||
Box::new(m20260227_000001_folder_parent_branch::Migration),
|
||||
Box::new(m20260330_000001_chat_channel::Migration),
|
||||
Box::new(m20260401_000001_chat_channel_sender_context::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,3 +6,4 @@ pub mod conversation_service;
|
||||
pub mod folder_command_service;
|
||||
pub mod folder_service;
|
||||
pub mod import_service;
|
||||
pub mod sender_context_service;
|
||||
|
||||
101
src-tauri/src/db/service/sender_context_service.rs
Normal file
101
src-tauri/src/db/service/sender_context_service.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ActiveValue::NotSet, ColumnTrait, DatabaseConnection, EntityTrait,
|
||||
IntoActiveModel, QueryFilter, Set,
|
||||
};
|
||||
|
||||
use crate::db::entities::chat_channel_sender_context;
|
||||
use crate::db::error::DbError;
|
||||
|
||||
pub async fn get_or_create(
|
||||
conn: &DatabaseConnection,
|
||||
channel_id: i32,
|
||||
sender_id: &str,
|
||||
) -> Result<chat_channel_sender_context::Model, DbError> {
|
||||
let existing = chat_channel_sender_context::Entity::find()
|
||||
.filter(chat_channel_sender_context::Column::ChannelId.eq(channel_id))
|
||||
.filter(chat_channel_sender_context::Column::SenderId.eq(sender_id))
|
||||
.one(conn)
|
||||
.await?;
|
||||
|
||||
if let Some(model) = existing {
|
||||
return Ok(model);
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
let active = chat_channel_sender_context::ActiveModel {
|
||||
id: NotSet,
|
||||
channel_id: Set(channel_id),
|
||||
sender_id: Set(sender_id.to_string()),
|
||||
current_folder_id: Set(None),
|
||||
current_agent_type: Set(None),
|
||||
current_conversation_id: Set(None),
|
||||
current_connection_id: Set(None),
|
||||
auto_approve: Set(false),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
};
|
||||
Ok(active.insert(conn).await?)
|
||||
}
|
||||
|
||||
pub async fn update_folder(
|
||||
conn: &DatabaseConnection,
|
||||
channel_id: i32,
|
||||
sender_id: &str,
|
||||
folder_id: Option<i32>,
|
||||
) -> Result<chat_channel_sender_context::Model, DbError> {
|
||||
let model = get_or_create(conn, channel_id, sender_id).await?;
|
||||
let mut active = model.into_active_model();
|
||||
active.current_folder_id = Set(folder_id);
|
||||
active.updated_at = Set(Utc::now());
|
||||
Ok(active.update(conn).await?)
|
||||
}
|
||||
|
||||
pub async fn update_agent(
|
||||
conn: &DatabaseConnection,
|
||||
channel_id: i32,
|
||||
sender_id: &str,
|
||||
agent_type: Option<String>,
|
||||
) -> Result<chat_channel_sender_context::Model, DbError> {
|
||||
let model = get_or_create(conn, channel_id, sender_id).await?;
|
||||
let mut active = model.into_active_model();
|
||||
active.current_agent_type = Set(agent_type);
|
||||
active.updated_at = Set(Utc::now());
|
||||
Ok(active.update(conn).await?)
|
||||
}
|
||||
|
||||
pub async fn update_session(
|
||||
conn: &DatabaseConnection,
|
||||
channel_id: i32,
|
||||
sender_id: &str,
|
||||
conversation_id: Option<i32>,
|
||||
connection_id: Option<String>,
|
||||
) -> Result<chat_channel_sender_context::Model, DbError> {
|
||||
let model = get_or_create(conn, channel_id, sender_id).await?;
|
||||
let mut active = model.into_active_model();
|
||||
active.current_conversation_id = Set(conversation_id);
|
||||
active.current_connection_id = Set(connection_id);
|
||||
active.updated_at = Set(Utc::now());
|
||||
Ok(active.update(conn).await?)
|
||||
}
|
||||
|
||||
pub async fn clear_session(
|
||||
conn: &DatabaseConnection,
|
||||
channel_id: i32,
|
||||
sender_id: &str,
|
||||
) -> Result<chat_channel_sender_context::Model, DbError> {
|
||||
update_session(conn, channel_id, sender_id, None, None).await
|
||||
}
|
||||
|
||||
pub async fn update_auto_approve(
|
||||
conn: &DatabaseConnection,
|
||||
channel_id: i32,
|
||||
sender_id: &str,
|
||||
auto_approve: bool,
|
||||
) -> Result<chat_channel_sender_context::Model, DbError> {
|
||||
let model = get_or_create(conn, channel_id, sender_id).await?;
|
||||
let mut active = model.into_active_model();
|
||||
active.auto_approve = Set(auto_approve);
|
||||
active.updated_at = Set(Utc::now());
|
||||
Ok(active.update(conn).await?)
|
||||
}
|
||||
Reference in New Issue
Block a user