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) { didReceiveChatSettings: (settings) {
emit( emit(
state.copyWith(selectedSourceIds: settings.ragIds), state.copyWith(
selectedSourceIds: settings.ragIds.ragIds,
onlyUseSelectedSources: settings.ragOnly,
),
); );
}, },
updateSelectedSources: (selectedSourcesIds) async { updateSelectedSources: (selectedSourcesIds) async {

View File

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

View File

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

View File

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

View File

@ -4,7 +4,8 @@ use std::collections::HashMap;
use crate::local_ai::local_llm_resource::PendingResource; use crate::local_ai::local_llm_resource::PendingResource;
use flowy_ai_pub::cloud::{ use flowy_ai_pub::cloud::{
ChatMessage, LLMModel, RelatedQuestion, RepeatedChatMessage, RepeatedRelatedQuestion, ChatMessage, ChatSettings, LLMModel, RelatedQuestion, RepeatedChatMessage,
RepeatedRelatedQuestion,
}; };
use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use lib_infra::validator_fn::required_not_empty_str; use lib_infra::validator_fn::required_not_empty_str;
@ -542,7 +543,27 @@ pub struct CreateChatContextPB {
#[derive(Default, ProtoBuf, Clone, Debug)] #[derive(Default, ProtoBuf, Clone, Debug)]
pub struct ChatSettingsPB { pub struct ChatSettingsPB {
#[pb(index = 1)] #[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)] #[derive(Default, ProtoBuf, Clone, Debug, Validate)]

View File

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