Skip to content

Commit

Permalink
release v0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
EluvK committed Mar 11, 2023
1 parent 2a009fe commit 4438dd6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qbot"
version = "0.1.1"
version = "0.1.3"
edition = "2021"
authors = ["[email protected]"]

Expand Down
11 changes: 6 additions & 5 deletions src/cqbot/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Bot {
proxy_addr: proxy,
api_key,
web_socket_stream: Box::new(wss),
private_manager: PrivateManager::new(),
private_manager: PrivateManager::new(0),
group_manager: GroupManager::new(),
}
}
Expand Down Expand Up @@ -76,9 +76,10 @@ impl Bot {
async fn handler_group_message(&mut self, message: RecvMsg) -> SendMsg {
let group_id = message.group_id();
let user_id = message.user_id();
let ts = message.message_ts();
let gpm = self
.group_manager
.pre_handle_private_message(group_id, user_id, message.message().clone());
.pre_handle_private_message(group_id, user_id, ts, message.message().clone());

match gpm {
Ok(_) => {
Expand Down Expand Up @@ -119,9 +120,9 @@ impl Bot {
}
async fn handler_private_message(&mut self, message: RecvMsg) -> SendMsg {
let user_id = message.user_id();
let pm = self
.private_manager
.pre_handle_private_message(user_id, message.message().clone());
let pm =
self.private_manager
.pre_handle_private_message(user_id, message.message_ts(), message.message().clone());
match pm {
Ok(_) => {
// legal request
Expand Down
3 changes: 3 additions & 0 deletions src/cqbot/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ impl RecvMsg {
pub fn message(&self) -> &String {
&self.message
}
pub fn message_ts(&self) -> u64 {
self.time
}
}

#[cfg(test)]
Expand Down
34 changes: 30 additions & 4 deletions src/private_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct ChatContext {
_user_id: u64,
wait_gpt_reply: bool, // to make sure one question at a time, to make it linear
histories: Vec<GPTMessages>,
last_ts: u64,
_max_depth: usize,
}

Expand All @@ -21,6 +22,7 @@ impl ChatContext {
_user_id: user_id,
wait_gpt_reply: false,
histories: Vec::new(),
last_ts: 0,
_max_depth: DEFAULT_MAX_DEPTH,
};
new_context.set_default_role();
Expand Down Expand Up @@ -48,17 +50,28 @@ impl ChatContext {
.iter()
.for_each(|m| self.histories.push(m.clone()));
}

fn query_too_often(&mut self, interval: u64, ts: u64) -> (bool, u64) {
if self.last_ts + interval <= ts {
self.last_ts = ts;
(false, 0)
} else {
(true, self.last_ts + interval - ts)
}
}
}

/// manager each private friends conversations
pub struct PrivateManager {
contexts: HashMap<u64, ChatContext>,
interval: u64,
}

impl PrivateManager {
pub fn new() -> Self {
pub fn new(interval: u64) -> Self {
PrivateManager {
contexts: HashMap::new(),
interval,
}
}

Expand All @@ -79,7 +92,12 @@ impl PrivateManager {
/// when return error, return to user immediately.
/// when ok, continue generate chatgpt answer.
/// Noted: instructions should be the Err(), will not call chatgpt.
pub fn pre_handle_private_message(&mut self, user_id: u64, message: String) -> Result<(), PrivateManagerError> {
pub fn pre_handle_private_message(
&mut self,
user_id: u64,
ts: u64,
message: String,
) -> Result<(), PrivateManagerError> {
self.contexts
.entry(user_id)
.or_insert_with(|| ChatContext::new_chat_context(user_id));
Expand Down Expand Up @@ -111,6 +129,10 @@ impl PrivateManager {
if chat_context.wait_gpt_reply {
return Err(PrivateManagerError::OnceMessageATime);
}
let (f, _t) = chat_context.query_too_often(self.interval, ts);
if f {
return Err(PrivateManagerError::QueryTooOften(_t));
}
chat_context.histories.push(GPTMessages::new_user_message(message));
chat_context.wait_gpt_reply = true;

Expand Down Expand Up @@ -139,11 +161,12 @@ impl GroupManager {
&mut self,
group_id: u64,
user_id: u64,
ts: u64,
message: String,
) -> Result<(), PrivateManagerError> {
self.contexts.entry(group_id).or_insert_with(|| PrivateManager::new());
self.contexts.entry(group_id).or_insert_with(|| PrivateManager::new(10));
let group_private_manager = self.contexts.get_mut(&group_id).unwrap();
group_private_manager.pre_handle_private_message(user_id, message)
group_private_manager.pre_handle_private_message(user_id, ts, message)
}

pub fn get_histories(&mut self, group_id: u64, user_id: u64) -> Option<&Vec<GPTMessages>> {
Expand Down Expand Up @@ -174,6 +197,9 @@ mod error {
#[error("Error: one message a time please.")]
OnceMessageATime,

#[error("Error: query too frequently. Wait {0} second please")]
QueryTooOften(u64),

#[error("Error: invalid command")]
InvalidCommand,

Expand Down

0 comments on commit 4438dd6

Please sign in to comment.