chore: fetch rag only on open chat

This commit is contained in:
Richard Shiue 2024-12-27 10:10:24 +08:00
parent 36d8a32670
commit 8246af93c7
6 changed files with 37 additions and 15 deletions

View File

@ -244,7 +244,10 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
},
didReceiveChatSettings: (settings) {
emit(
state.copyWith(selectedSourceIds: settings.ragIds),
state.copyWith(
selectedSourceIds: settings.ragIds.ragIds,
onlyUseSelectedSources: settings.ragOnly,
),
);
},
updateSelectedSources: (selectedSourcesIds) async {

View File

@ -43,9 +43,10 @@ class _PromptInputDesktopSelectSourcesButtonState
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
cubit.updateSelectedSources(
context.read<ChatBloc>().state.selectedSourceIds,
);
final chatBlocState = context.read<ChatBloc>().state;
cubit
..updateSelectedSources(chatBlocState.selectedSourceIds)
..updateOnlyUseSelectedSources(chatBlocState.onlyUseSelectedSources);
});
}

View File

@ -213,7 +213,7 @@
"addToPageTitle": "Add message to...",
"addToNewPage": "Add to a new page",
"addToNewPageName": "Messages extracted from \"{}\"",
"onlyUseRags": "Only use selected sources to generate response"
"onlyUseRags": "Selected sources only"
},
"trash": {
"text": "Trash",

View File

@ -345,12 +345,12 @@ impl AIManager {
Ok(())
}
pub async fn get_rag_ids(&self, chat_id: &str) -> FlowyResult<Vec<String>> {
pub async fn get_chat_settings(&self, chat_id: &str) -> FlowyResult<ChatSettingsPB> {
if let Some(settings) = self
.store_preferences
.get_object::<ChatSettings>(&setting_store_key(chat_id))
{
return Ok(settings.rag_ids);
return Ok(settings.into());
}
let settings = refresh_chat_setting(
@ -360,7 +360,7 @@ impl AIManager {
chat_id,
)
.await?;
Ok(settings.rag_ids)
Ok(settings.into())
}
pub async fn update_settings(
@ -448,9 +448,7 @@ async fn refresh_chat_setting(
}
chat_notification_builder(chat_id, ChatNotification::DidUpdateChatSettings)
.payload(ChatSettingsPB {
rag_ids: settings.rag_ids.clone(),
})
.payload(ChatSettingsPB::from(settings.clone()))
.send();
Ok(settings)

View File

@ -4,7 +4,8 @@ use std::collections::HashMap;
use crate::local_ai::local_llm_resource::PendingResource;
use flowy_ai_pub::cloud::{
ChatMessage, LLMModel, RelatedQuestion, RepeatedChatMessage, RepeatedRelatedQuestion,
ChatMessage, ChatSettings, LLMModel, RelatedQuestion, RepeatedChatMessage,
RepeatedRelatedQuestion,
};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use lib_infra::validator_fn::required_not_empty_str;
@ -542,7 +543,27 @@ pub struct CreateChatContextPB {
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct ChatSettingsPB {
#[pb(index = 1)]
pub rag_ids: Vec<String>,
pub rag_ids: RepeatedRagId,
#[pb(index = 2)]
pub rag_only: bool,
}
impl From<ChatSettings> for ChatSettingsPB {
fn from(value: ChatSettings) -> Self {
let rag_ids = RepeatedRagId {
rag_ids: value.rag_ids.clone(),
};
let rag_only = value
.metadata
.as_object()
.and_then(|map| map.get("rag_only"))
.and_then(|value| value.as_bool())
.unwrap_or_default();
Self { rag_ids, rag_only }
}
}
#[derive(Default, ProtoBuf, Clone, Debug, Validate)]

View File

@ -446,8 +446,7 @@ pub(crate) async fn get_chat_settings_handler(
) -> DataResult<ChatSettingsPB, FlowyError> {
let chat_id = data.try_into_inner()?.value;
let ai_manager = upgrade_ai_manager(ai_manager)?;
let rag_ids = ai_manager.get_rag_ids(&chat_id).await?;
let pb = ChatSettingsPB { rag_ids };
let pb = ai_manager.get_chat_settings(&chat_id).await?;
data_result_ok(pb)
}