mirror of https://github.com/buster-so/buster.git
ok things are working, but more tweaks needed.
This commit is contained in:
parent
5de4a3961a
commit
03de39ac82
|
@ -353,6 +353,8 @@ impl Agent {
|
||||||
let mut content_buffer = String::new();
|
let mut content_buffer = String::new();
|
||||||
let mut message_id: Option<String> = None;
|
let mut message_id: Option<String> = None;
|
||||||
let mut is_complete = false;
|
let mut is_complete = false;
|
||||||
|
// Flag to track if we've sent the first message
|
||||||
|
let mut first_message_sent = false;
|
||||||
|
|
||||||
while let Some(chunk_result) = stream_rx.recv().await {
|
while let Some(chunk_result) = stream_rx.recv().await {
|
||||||
match chunk_result {
|
match chunk_result {
|
||||||
|
@ -367,17 +369,19 @@ impl Agent {
|
||||||
if let Some(content) = &delta.content {
|
if let Some(content) = &delta.content {
|
||||||
content_buffer.push_str(content);
|
content_buffer.push_str(content);
|
||||||
|
|
||||||
// Stream the content update
|
// Stream the content update with initial=true for the first message only
|
||||||
let partial_message = AgentMessage::assistant(
|
let partial_message = AgentMessage::assistant(
|
||||||
message_id.clone(),
|
message_id.clone(),
|
||||||
Some(content_buffer.clone()),
|
Some(content_buffer.clone()),
|
||||||
None,
|
None,
|
||||||
Some(MessageProgress::InProgress),
|
Some(MessageProgress::InProgress),
|
||||||
Some(false),
|
Some(!first_message_sent), // Set initial=true only for the first message
|
||||||
Some(self.name.clone()),
|
Some(self.name.clone()),
|
||||||
);
|
);
|
||||||
|
|
||||||
self.get_stream_sender().await.send(Ok(partial_message))?;
|
self.get_stream_sender().await.send(Ok(partial_message))?;
|
||||||
|
// Mark that we've sent the first message
|
||||||
|
first_message_sent = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process tool calls if present
|
// Process tool calls if present
|
||||||
|
@ -423,11 +427,13 @@ impl Agent {
|
||||||
},
|
},
|
||||||
Some(tool_calls_vec),
|
Some(tool_calls_vec),
|
||||||
Some(MessageProgress::InProgress),
|
Some(MessageProgress::InProgress),
|
||||||
Some(false),
|
Some(!first_message_sent), // Set initial=true only for the first message
|
||||||
Some(self.name.clone()),
|
Some(self.name.clone()),
|
||||||
);
|
);
|
||||||
|
|
||||||
self.get_stream_sender().await.send(Ok(partial_message))?;
|
self.get_stream_sender().await.send(Ok(partial_message))?;
|
||||||
|
// Mark that we've sent the first message
|
||||||
|
first_message_sent = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -20,7 +20,7 @@ impl StreamingParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_chunk(&mut self, chunk: &str) -> Result<Option<BusterChatContainer>> {
|
pub fn process_chunk(&mut self, id: String, chunk: &str) -> Result<Option<BusterChatContainer>> {
|
||||||
// Add new chunk to buffer
|
// Add new chunk to buffer
|
||||||
self.buffer.push_str(chunk);
|
self.buffer.push_str(chunk);
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ impl StreamingParser {
|
||||||
let has_yml_content = last_file.get("yml_content").is_some();
|
let has_yml_content = last_file.get("yml_content").is_some();
|
||||||
|
|
||||||
if has_name && has_file_type && has_yml_content {
|
if has_name && has_file_type && has_yml_content {
|
||||||
return self.convert_to_message(value);
|
return self.convert_to_message(id, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,7 +148,7 @@ impl StreamingParser {
|
||||||
processed
|
processed
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_to_message(&self, value: Value) -> Result<Option<BusterChatContainer>> {
|
fn convert_to_message(&self, id: String, value: Value) -> Result<Option<BusterChatContainer>> {
|
||||||
if let Some(files) = value.get("files").and_then(Value::as_array) {
|
if let Some(files) = value.get("files").and_then(Value::as_array) {
|
||||||
if let Some(last_file) = files.last().and_then(Value::as_object) {
|
if let Some(last_file) = files.last().and_then(Value::as_object) {
|
||||||
let name = last_file.get("name").and_then(Value::as_str).unwrap_or("");
|
let name = last_file.get("name").and_then(Value::as_str).unwrap_or("");
|
||||||
|
@ -171,7 +171,7 @@ impl StreamingParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(Some(BusterChatContainer::File(BusterFileMessage {
|
return Ok(Some(BusterChatContainer::File(BusterFileMessage {
|
||||||
id: name.to_string(),
|
id,
|
||||||
message_type: "file".to_string(),
|
message_type: "file".to_string(),
|
||||||
file_type: file_type.to_string(),
|
file_type: file_type.to_string(),
|
||||||
file_name: name.to_string(),
|
file_name: name.to_string(),
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use handlers::chats::post_chat_handler;
|
|
||||||
use handlers::chats::post_chat_handler::ChatCreateNewChat;
|
use handlers::chats::post_chat_handler::ChatCreateNewChat;
|
||||||
|
use handlers::chats::post_chat_handler::{self, ThreadEvent};
|
||||||
use handlers::chats::types::ChatWithMessages;
|
use handlers::chats::types::ChatWithMessages;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
database::models::User,
|
database::models::User,
|
||||||
routes::ws::{
|
routes::ws::{
|
||||||
threads_and_messages::threads_router::{ThreadEvent, ThreadRoute},
|
threads_and_messages::threads_router::{ThreadEvent as WSThreadEvent, ThreadRoute},
|
||||||
ws::{SubscriptionRwLock, WsEvent, WsResponseMessage, WsSendMethod},
|
ws::{SubscriptionRwLock, WsEvent, WsResponseMessage, WsSendMethod},
|
||||||
ws_router::WsRoutes,
|
ws_router::WsRoutes,
|
||||||
ws_utils::send_ws_message,
|
ws_utils::send_ws_message,
|
||||||
|
@ -25,32 +25,47 @@ pub async fn post_thread(
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let (tx, mut rx) = mpsc::channel(1000);
|
let (tx, mut rx) = mpsc::channel(1000);
|
||||||
|
|
||||||
// Call the shared handler
|
let user_id = user.id.to_string();
|
||||||
post_chat_handler::post_chat_handler(request, user.clone(), Some(tx)).await?;
|
|
||||||
|
|
||||||
while let Some(result) = rx.recv().await {
|
tokio::spawn(async move {
|
||||||
match result {
|
while let Some(result) = rx.recv().await {
|
||||||
Ok(chat_with_messages) => {
|
match result {
|
||||||
println!("MESSAGE SHOULD BE SENT: {:?}", chat_with_messages);
|
Ok((message, event)) => {
|
||||||
|
println!("MESSAGE SHOULD BE SENT: {:?}", message);
|
||||||
|
|
||||||
let response = WsResponseMessage::new_no_user(
|
let event = match event {
|
||||||
WsRoutes::Threads(ThreadRoute::Post),
|
ThreadEvent::GeneratingResponseMessage => {
|
||||||
WsEvent::Threads(ThreadEvent::InitializeChat),
|
WsEvent::Threads(WSThreadEvent::GeneratingResponseMessage)
|
||||||
&chat_with_messages,
|
}
|
||||||
None,
|
ThreadEvent::GeneratingReasoningMessage => {
|
||||||
WsSendMethod::All,
|
WsEvent::Threads(WSThreadEvent::GeneratingReasoningMessage)
|
||||||
);
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if let Err(e) = send_ws_message(&user.id.to_string(), &response).await {
|
let response = WsResponseMessage::new_no_user(
|
||||||
tracing::error!("Failed to send websocket message: {}", e);
|
WsRoutes::Threads(ThreadRoute::Post),
|
||||||
|
event,
|
||||||
|
&message,
|
||||||
|
None,
|
||||||
|
WsSendMethod::All,
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(e) = send_ws_message(&user_id, &response).await {
|
||||||
|
tracing::error!("Failed to send websocket message: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
tracing::error!("Error in message stream: {:?}", err);
|
||||||
|
return Err(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
|
||||||
tracing::error!("Error in message stream: {:?}", err);
|
|
||||||
return Err(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
// Call the shared handler
|
||||||
|
post_chat_handler::post_chat_handler(request, user.clone(), Some(tx)).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -59,7 +74,7 @@ pub async fn post_thread(
|
||||||
async fn send_ws_response(subscription: &str, chat_with_messages: &ChatWithMessages) -> Result<()> {
|
async fn send_ws_response(subscription: &str, chat_with_messages: &ChatWithMessages) -> Result<()> {
|
||||||
let response = WsResponseMessage::new_no_user(
|
let response = WsResponseMessage::new_no_user(
|
||||||
WsRoutes::Threads(ThreadRoute::Post),
|
WsRoutes::Threads(ThreadRoute::Post),
|
||||||
WsEvent::Threads(ThreadEvent::InitializeChat),
|
WsEvent::Threads(WSThreadEvent::InitializeChat),
|
||||||
chat_with_messages,
|
chat_with_messages,
|
||||||
None,
|
None,
|
||||||
WsSendMethod::All,
|
WsSendMethod::All,
|
||||||
|
|
Loading…
Reference in New Issue